Search code examples
cpseudocodedecompiling

Trying to understand a pseudo code thrown by a decompiler


I'm new to this topic and i got stuck while reviewing a pseudo code thrown by a de-compiler.

I got this:

int __cdecl update_FILES(const char *a1, const char *a2, int a3)
{
  int result; // eax
  int v4; // esi
  int v5; // eax
  FILE *v6; // [esp+20h] [ebp-98h]
  FILE *v7; // [esp+24h] [ebp-94h]
  FILE *v8; // [esp+28h] [ebp-90h]
  char Str1[9]; // [esp+38h] [ebp-80h] BYREF
  char v10; // [esp+41h] [ebp-77h]
  float v11; // [esp+44h] [ebp-74h]
  char Destination[50]; // [esp+4Ah] [ebp-6Eh] BYREF
  char Buffer[56]; // [esp+7Ch] [ebp-3Ch] BYREF
  float v14; // [esp+B4h] [ebp-4h]
  _BYTE v15[336]; // [esp+B8h] [ebp+0h] BYREF

  crear_cola_res(v15);
  v6 = fopen(a1, "r+b");
  result = 1;
  if ( v6 )
  {
    v7 = fopen(a2, "rt");
    if ( v7 )
    {
      strcpy(Destination, a2);
      strcpy(strrchr(Destination, 46), "_myFile.txt");
      v8 = fopen(Destination, "wb");
      if ( v8 )
      {
        fread(Buffer, 0x3Cu, 1u, v6);
        v4 = read_movements(Str1, v7, a3);
LABEL_5:
        if ( !feof(v6) )
        {
          while ( v4 )
          {
            v5 = strcmp(Str1, Buffer);
            if ( v5 < 0 )
            {
              v4 = read_movements(Str1, v7, a3);
              goto LABEL_5;
            }
            if ( !v5 )
            {
              do
              {
                if ( v10 == 68 )
                {
                  v14 = v14 - v11;
                }
                else if ( v10 == 67 )
                {
                  v14 = v14 + v11;
                }
                put_in_FIFO(v15, Str1, 16);
                v4 = read_movements(Str1, v7, a3);
              }
              while ( v4 && !strcmp(Str1, Buffer) );
              fseek(v6, -60, 1);
              fwrite(Buffer, 0x3Cu, 1u, v6);
              fseek(v6, 0, 1);
              if ( v14 < 0.0 )
              {
                while ( remove_from_FIFO(v15, Str1, 16) )
                  fprintf(v8, "%s, %c, %.2f\n", Str1, v10, v11);
              }
              else
              {
                erase_FIFO(v15);
              }
            }
            fread(Buffer, 0x3Cu, 1u, v6);
            if ( feof(v6) )
              break;
          }
        }
        fclose(v6);
        fclose(v7);
        fclose(v8);
        result = 0;
      }
      else
      {
        fclose(v6);
        fclose(v7);
        result = 3;
      }
    }
    else
    {
      fclose(v6);
      result = 2;
    }
  }
  return result;
}

I was wondering how would it be the best way to obtain pure C code from the pseudo code shown above. The code shown does not make a lot of sense to me

Is there exist any way? How would you do that?.

I'm kinda confused.


EDIT:

Sorry, i'm new to the decompilation process.

My original function has this prototype:

update_FILES("../File/accounts.dat","../Files/movements.txt", key);

Why do the parameters differ each other?. I dont get it


Solution

  • I'm still not at all sure what you're confused about... ... but this pseudo-code might help:

    CALLER:

    update_FILES("../File/accounts.dat","../Files/movements.txt", key);

    P-CODE:

    int __cdecl update_FILES(const char *a1, const char *a2, int a3)
    {
      <<Declare some local variables (result, v4, v5, v6, ...)>>
      v6 = fopen("../File/accounts.dat")
      if (v6 <> 0) // If "fopen()" succeed...
        v7 = fopen("../Files/movements.txt")
        if (v7 <> 0)
          Destination = a2
          ...