addToPatternSet

original_code:
int addToPatternSet(struct PatternSet * set , char * name , unsigned int value , unsigned int score)
{
  unsigned int curSetNum = set->totalPatterns;
  ++set->totalPatterns;
  strncpy(set->pattern[curSetNum].name,name,126);
  set->pattern[curSetNum].value=value;
  set->pattern[curSetNum].acceptScore=score;
  set->pattern[curSetNum].use=1;
  char fName[512];
  unsigned int i=1;
  unsigned int totalTiles = 0;
  while (i<255)
  {
      snprintf(fName,512,"%s%u.pnm",name,i);
      if (patFileExists(fName))
      {
        set->pattern[curSetNum].tile[totalTiles] = readImage( fName , PNM_CODEC , 0 );
        if (set->pattern[curSetNum].tile[totalTiles]!=0)
        {
          fprintf(stderr,"\033[32m" "Loaded %s , we now have %u patterns for set %u\n" "\033[0m",fName, totalTiles , curSetNum);
          unsigned int totalPixels = set->pattern[curSetNum].tile[totalTiles]->width * set->pattern[curSetNum].tile[totalTiles]->height;
          unsigned int transPixels = countOccurancesOfRGBPixel(set->pattern[curSetNum].tile[totalTiles]->pixels,
                                                                                set->pattern[curSetNum].tile[totalTiles]->width,
                                                                                set->pattern[curSetNum].tile[totalTiles]->height,
                                                                                123,123,0);
          set->pattern[curSetNum].nonTransparentPixels[totalTiles] = totalPixels - transPixels;
          set->pattern[curSetNum].totalPixels[totalTiles]= totalPixels ;
          ++totalTiles;
        }
      } else
      {
        if (i==1) { fprintf(stderr,"\033[31m" "Failed to load any files for #%u -> %s \n" "\033[0m",i,fName); }
        set->pattern[curSetNum].totalTiles=totalTiles ;
        break;
      }
      ++i;
  }
  return 1;
}

canonical_original_code:
int addToPatternSet(struct PatternSet *set, char *name, unsigned int value, unsigned int score)
{
  unsigned int curSetNum = set->totalPatterns;
  ++set->totalPatterns;
  strncpy(set->pattern[curSetNum].name,name,126);
  set->pattern[curSetNum].value=value;
  set->pattern[curSetNum].acceptScore=score;
  set->pattern[curSetNum].use=1;
  char fName[512];
  unsigned int i = 1;
  unsigned int totalTiles = 0;
  while (i<255)
  {
      snprintf(fName,512,"%s%u.pnm",name,i);
      if (patFileExists(fName))
      {
        set->pattern[curSetNum].tile[totalTiles] = readImage( fName , PNM_CODEC , 0 );
        if (set->pattern[curSetNum].tile[totalTiles]!=0)
        {
          fprintf(stderr,"\033[32m" "Loaded %s , we now have %u patterns for set %u\n" "\033[0m",fName, totalTiles , curSetNum);
          unsigned int totalPixels = set->pattern[curSetNum].tile[totalTiles]->width * set->pattern[curSetNum].tile[totalTiles]->height;
          unsigned int transPixels = countOccurancesOfRGBPixel(set->pattern[curSetNum].tile[totalTiles]->pixels,
                                                                                set->pattern[curSetNum].tile[totalTiles]->width,
                                                                                set->pattern[curSetNum].tile[totalTiles]->height,
                                                                                123,123,0);
          set->pattern[curSetNum].nonTransparentPixels[totalTiles] = totalPixels - transPixels;
          set->pattern[curSetNum].totalPixels[totalTiles]= totalPixels ;
          ++totalTiles;
        }
      } else
      {
        if (i==1) { fprintf(stderr,"\033[31m" "Failed to load any files for #%u -> %s \n" "\033[0m",i,fName); }
        set->pattern[curSetNum].totalTiles=totalTiles ;
        break;
      }
      ++i;
  }
  return 1;
}

variable_types: struct PatternSet { unsigned int totalPatterns; struct PatternItem { unsigned int totalTiles; unsigned int value; unsigned char use; unsigned int acceptScore; struct Image { unsigned char *pixels; unsigned int width; unsigned int height; unsigned int channels; unsigned int bitsperpixel; unsigned int image_size; } *tile[256]; unsigned int nonTransparentPixels[256]; unsigned int totalPixels[256]; char name[512]; } pattern[256]; } *

variable_types:
name
char *

variable_types:
value
unsigned int

variable_types:
score
unsigned int

variable_types:
curSetNum
unsigned int

variable_types:
fName
char [512]

variable_types:
totalTiles
unsigned int

variable_types:
totalPixels
unsigned int

variable_types:
transPixels
unsigned int


dumpPatternSet

repo: AmmarkoV/VisualBotNGine

decompiled_code:
__int64 dumpPatternSet()
{
fwrite("dumping Pattern sets deactivated\n", 1uLL, 0x21uLL, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
fwrite("dumping Pattern sets deactivated\n", 1uLL, 0x21uLL, stderr);
return 0LL;
}

original_code:
int dumpPatternSet(struct PatternSet * pattSet ,char * stage)
{
  fprintf(stderr,"dumping Pattern sets deactivated\n");
  return 0;
 unsigned int tileNum=0;
 unsigned int patternNum=0;
 char fName[512];
 for ( patternNum=0; patternNum < pattSet->totalPatterns; patternNum++ )
 {
   fprintf(stderr,"Dumping %s %s :  ",stage,pattSet->pattern[patternNum].name);
   if (pattSet->pattern[patternNum].totalTiles==0)
   {
    fprintf(stderr,"EMPTY");
   } else
   {
     for ( tileNum=0; tileNum < pattSet->pattern[patternNum].totalTiles; tileNum++ )
      {
        snprintf(fName,512,"Dump/%sPattern%uTile%u.pnm",stage,patternNum,tileNum);
        fprintf(stderr,"%s ",fName);
        writeImageFile(pattSet->pattern[patternNum].tile[tileNum],PNM_CODEC,fName);
      }
   }
   fprintf(stderr,"\n");
  }
}

canonical_original_code:
int dumpPatternSet(struct PatternSet *pattSet, char *stage)
{
  fprintf(stderr,"dumping Pattern sets deactivated\n");
  return 0;
 unsigned int tileNum = 0;
 unsigned int patternNum = 0;
 char fName[512];
 for ( patternNum=0; patternNum < pattSet->totalPatterns; patternNum++ )
 {
   fprintf(stderr,"Dumping %s %s :  ",stage,pattSet->pattern[patternNum].name);
   if (pattSet->pattern[patternNum].totalTiles==0)
   {
    fprintf(stderr,"EMPTY");
   } else
   {
     for ( tileNum=0; tileNum < pattSet->pattern[patternNum].totalTiles; tileNum++ )
      {
        snprintf(fName,512,"Dump/%sPattern%uTile%u.pnm",stage,patternNum,tileNum);
        fprintf(stderr,"%s ",fName);
        writeImageFile(pattSet->pattern[patternNum].tile[tileNum],PNM_CODEC,fName);
      }
   }
   fprintf(stderr,"\n");
  }
}

variable_types:
stage
char *

variable_types:
tileNum
unsigned int

variable_types:
patternNum
unsigned int

return_type: int

user_defined_types: struct PatternSet { unsigned int totalPatterns; struct PatternItem pattern[256]; }

user_defined_types: struct PatternItem { unsigned int totalTiles; unsigned int value; unsigned char use; unsigned int acceptScore; struct Image *tile[256]; unsigned int nonTransparentPixels[256]; unsigned int totalPixels[256]; char name[512]; }

user_defined_types: struct Image { unsigned char *pixels; unsigned int width; unsigned int height; unsigned int channels; unsigned int bitsperpixel; unsigned int image_size; }


emptyPatternSet

decompiled_code:
__int64 __fastcall emptyPatternSet(unsigned int *a1)
{
unsigned int v2; // er13
unsigned int v3; // ebp
unsigned int *v4; // r12
__int64 *v5; // rbx
__int64 v6; // rdi

if ( !*a1 )
return 1LL;
v2 = 0;
do
{
v3 = 0;
v4 = &a1[1156 * v2];
v5 = (__int64 *)(v4 + 6);
if ( v4[2] )
{
do
{
v6 = *v5;
++v3;
++v5;
destroyImage(v6);
*(v5 - 1) = 0LL;
}
while ( v4[2] > v3 );
}
++v2;
}
while ( *a1 > v2 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1)
{
unsigned int v2; // er13
unsigned int v3; // ebp
unsigned int *v4; // r12
__int64 *v5; // rbx
__int64 v6; // rdi

if ( !*a1 )
return 1LL;
v2 = 0;
do
{
v3 = 0;
v4 = &a1[1156 * v2];
v5 = (__int64 *)(v4 + 6);
if ( v4[2] )
{
do
{
v6 = *v5;
++v3;
++v5;
destroyImage(v6);
*(v5 - 1) = 0LL;
}
while ( v4[2] > v3 );
}
++v2;
}
while ( *a1 > v2 );
return 1LL;
}

original_code:
int emptyPatternSet(struct PatternSet * pattSet)
{
  unsigned int tileNum=0;
  unsigned int patternNum=0;
  for ( patternNum=0; patternNum < pattSet->totalPatterns; patternNum++ )
  {
   for ( tileNum=0; tileNum < pattSet->pattern[patternNum].totalTiles; tileNum++ )
   {
        destroyImage( pattSet->pattern[patternNum].tile[tileNum] );
        pattSet->pattern[patternNum].tile[tileNum]=0;
   }
  }
    return 1;
}

canonical_original_code:
int emptyPatternSet(struct PatternSet *pattSet)
{
  unsigned int tileNum = 0;
  unsigned int patternNum = 0;
  for ( patternNum=0; patternNum < pattSet->totalPatterns; patternNum++ )
  {
   for ( tileNum=0; tileNum < pattSet->pattern[patternNum].totalTiles; tileNum++ )
   {
        destroyImage( pattSet->pattern[patternNum].tile[tileNum] );
        pattSet->pattern[patternNum].tile[tileNum]=0;
   }
  }
    return 1;
}


getPatternSetItemWidth

decompiled_code:
__int64 __fastcall getPatternSetItemWidth(unsigned int *a1, unsigned int a2, int a3)
{
__int64 v3; // rcx

v3 = *a1;
if ( (unsigned int)v3 > a2 )
return *(unsigned int *)(*(_QWORD *)&a1[1156 * a2 + 6 + 2 * a3] + 8LL);
fprintf(stderr, "getPatternSetItemWidth asking for out of bounds pattern ( number %u / %u )\n", a2, v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, unsigned int a2, int a3)
{
__int64 v3; // rcx

v3 = *a1;
if ( (unsigned int)v3 > a2 )
return *(unsigned int *)(*(_QWORD *)&a1[1156 * a2 + 6 + 2 * a3] + 8LL);
fprintf(stderr, "getPatternSetItemWidth asking for out of bounds pattern ( number %u / %u )\n", a2, v3);
return 0LL;
}

original_code:
unsigned int getPatternSetItemWidth(struct PatternSet * pattSet , unsigned int patternNum , unsigned int tileNum)
{
    if (patternNum<pattSet->totalPatterns)
    {
      return pattSet->pattern[patternNum].tile[tileNum]->width;
    }
 fprintf(stderr,"getPatternSetItemWidth asking for out of bounds pattern ( number %u / %u )\n",patternNum,pattSet->totalPatterns);
 return 0;
}

canonical_original_code:
unsigned int getPatternSetItemWidth(struct PatternSet *pattSet, unsigned int patternNum, unsigned int tileNum)
{
    if (patternNum<pattSet->totalPatterns)
    {
      return pattSet->pattern[patternNum].tile[tileNum]->width;
    }
 fprintf(stderr,"getPatternSetItemWidth asking for out of bounds pattern ( number %u / %u )\n",patternNum,pattSet->totalPatterns);
 return 0;
}

return_type: unsigned int


compareToPatternSet

decompiled_code:
__int64 __fastcall compareToPatternSet(int *a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned int a6, unsigned int a7, unsigned int a8, unsigned int a9, unsigned int *a10, unsigned int *a11, unsigned int *a12)
{
int v15; // edx
int *v16; // r15
__int64 v17; // r15
int *v18; // rbx
unsigned int v19; // eax
__int64 result; // rax
__int64 v21; // rax
unsigned int v22; // [rsp+10h] [rbp-488h]
unsigned int v23; // [rsp+1Ch] [rbp-47Ch]
unsigned int v25; // [rsp+44h] [rbp-454h]
unsigned int v26; // [rsp+48h] [rbp-450h]
unsigned int v27; // [rsp+4Ch] [rbp-44Ch]
unsigned int v28; // [rsp+5Ch] [rbp-43Ch] BYREF
char v29[16]; // [rsp+60h] [rbp-438h] BYREF
char v30[496]; // [rsp+70h] [rbp-428h] BYREF
char s[16]; // [rsp+260h] [rbp-238h] BYREF
char v32[496]; // [rsp+270h] [rbp-228h] BYREF

v15 = *a1;
v28 = a9 + 1;
if ( v15 )
{
v23 = a9 + 1;
v22 = 0;
v27 = 0;
v26 = 0;
v25 = 0;
while ( 1 )
{
v16 = &a1[1156 * v22];
if ( v16[2] )
{
v17 = 0LL;
v18 = &a1[1156 * v22];
do
{
v28 = a9 + 1;
if ( (unsigned int)compareRGBPatchesIgnoreColor(
a2,
a5,
a6,
a3,
a4,
**(_QWORD **)&v18[2 * v17 + 6],
0LL,
0LL,
*(unsigned int *)(*(_QWORD *)&v18[2 * v17 + 6] + 8LL),
*(unsigned int *)(*(_QWORD *)&v18[2 * v17 + 6] + 12LL),
123LL,
123LL,
0LL,
a7,
a8,
&v28,
10 * a9) )
{
v19 = (int)(float)((float)(int)(v18[v17 + 774] / (unsigned int)v18[v17 + 518]) * (float)(int)v28);
v28 = v19;
if ( v19 < v23 )
{
v27 = v17;
v23 = v19;
v25 = v18[3];
v26 = v22;
}
}
++v17;
}
while ( v18[2] > (unsigned int)v17 );
if ( v18[5] > v23 )
{
LABEL_12:
fprintf(stderr, "INSTA-Selected %s with a score of %u \n", (const char *)&a1[1156 * v22 + 1030], v23);
*a10 = v25;
*a11 = v26;
*a12 = v27;
return 1LL;
}
}
else if ( v16[5] > v23 )
{
goto LABEL_12;
}
if ( *a1 <= ++v22 )
goto LABEL_15;
}
}
v27 = 0;
v26 = 0;
v25 = 0;
v23 = a9 + 1;
LABEL_15:
fprintf(stderr, "Thinking about best score\n");
result = 0LL;
if ( a9 > v23 )
{
memset(v30, 0, sizeof(v30));
memset(v32, 0, sizeof(v32));
*(_OWORD *)v29 = 0LL;
*(_OWORD *)s = 0LL;
snprintf(s, 0x200uLL, "Dump/tile%u_%u_like_%u_%u_score_%u", 0LL, 0LL, v26, v27, v23);
snprintf(v29, 0x200uLL, "Most like %u with score %u", v25, v23);
bitBltRGBToFile(s, v29, a2, a5, a6, a3, 0.0, a4, a7, a8);
snprintf(s, 0x200uLL, "Dump/tile%u_%u_like_%u_%u_score_%uB", 0LL, 0LL, v26, v27, v23);
v21 = *(_QWORD *)&a1[1156 * v26 + 6 + 2 * v27];
bitBltRGBToFile(
s,
v29,
*(_QWORD *)v21,
0LL,
0LL,
*(unsigned int *)(v21 + 8),
0.0,
*(unsigned int *)(v21 + 12),
*(unsigned int *)(v21 + 8),
*(_DWORD *)(v21 + 12));
fprintf(stderr, "Not so sure , but selected %u with a score of %u \n", v25, v23);
*a10 = v25;
*a11 = v26;
*a12 = v27;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int *a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned int a6, unsigned int a7, unsigned int a8, unsigned int a9, unsigned int *a10, unsigned int *a11, unsigned int *a12)
{
int v15; // edx
int *v16; // r15
__int64 v17; // r15
int *v18; // rbx
unsigned int v19; // eax
__int64 result; // rax
__int64 v21; // rax
unsigned int v22; // [rsp+10h] [rbp-488h]
unsigned int v23; // [rsp+1Ch] [rbp-47Ch]
unsigned int v25; // [rsp+44h] [rbp-454h]
unsigned int v26; // [rsp+48h] [rbp-450h]
unsigned int v27; // [rsp+4Ch] [rbp-44Ch]
unsigned int v28; // [rsp+5Ch] [rbp-43Ch] BYREF
char v29[16]; // [rsp+60h] [rbp-438h] BYREF
char v30[496]; // [rsp+70h] [rbp-428h] BYREF
char s[16]; // [rsp+260h] [rbp-238h] BYREF
char v32[496]; // [rsp+270h] [rbp-228h] BYREF

v15 = *a1;
v28 = a9 + 1;
if ( v15 )
{
v23 = a9 + 1;
v22 = 0;
v27 = 0;
v26 = 0;
v25 = 0;
while ( 1 )
{
v16 = &a1[1156 * v22];
if ( v16[2] )
{
v17 = 0LL;
v18 = &a1[1156 * v22];
do
{
v28 = a9 + 1;
if ( (unsigned int)compareRGBPatchesIgnoreColor(
a2,
a5,
a6,
a3,
a4,
**(_QWORD **)&v18[2 * v17 + 6],
0LL,
0LL,
*(unsigned int *)(*(_QWORD *)&v18[2 * v17 + 6] + 8LL),
*(unsigned int *)(*(_QWORD *)&v18[2 * v17 + 6] + 12LL),
123LL,
123LL,
0LL,
a7,
a8,
&v28,
10 * a9) )
{
v19 = (int)(float)((float)(int)(v18[v17 + 774] / (unsigned int)v18[v17 + 518]) * (float)(int)v28);
v28 = v19;
if ( v19 < v23 )
{
v27 = v17;
v23 = v19;
v25 = v18[3];
v26 = v22;
}
}
++v17;
}
while ( v18[2] > (unsigned int)v17 );
if ( v18[5] > v23 )
{
LABEL_12:
fprintf(stderr, "INSTA-Selected %s with a score of %u \n", (const char *)&a1[1156 * v22 + 1030], v23);
*a10 = v25;
*a11 = v26;
*a12 = v27;
return 1LL;
}
}
else if ( v16[5] > v23 )
{
goto LABEL_12;
}
if ( *a1 <= ++v22 )
goto LABEL_15;
}
}
v27 = 0;
v26 = 0;
v25 = 0;
v23 = a9 + 1;
LABEL_15:
fprintf(stderr, "Thinking about best score\n");
result = 0LL;
if ( a9 > v23 )
{
memset(v30, 0, sizeof(v30));
memset(v32, 0, sizeof(v32));
*(_OWORD *)v29 = 0LL;
*(_OWORD *)s = 0LL;
snprintf(s, 0x200uLL, "Dump/tile%u_%u_like_%u_%u_score_%u", 0LL, 0LL, v26, v27, v23);
snprintf(v29, 0x200uLL, "Most like %u with score %u", v25, v23);
bitBltRGBToFile(s, v29, a2, a5, a6, a3, 0.0, a4, a7, a8);
snprintf(s, 0x200uLL, "Dump/tile%u_%u_like_%u_%u_score_%uB", 0LL, 0LL, v26, v27, v23);
v21 = *(_QWORD *)&a1[1156 * v26 + 6 + 2 * v27];
bitBltRGBToFile(
s,
v29,
*(_QWORD *)v21,
0LL,
0LL,
*(unsigned int *)(v21 + 8),
0.0,
*(unsigned int *)(v21 + 12),
*(unsigned int *)(v21 + 8),
*(_DWORD *)(v21 + 12));
fprintf(stderr, "Not so sure , but selected %u with a score of %u \n", v25, v23);
*a10 = v25;
*a11 = v26;
*a12 = v27;
result = 1LL;
}
return result;
}

original_code:
int compareToPatternSet(struct PatternSet * pattSet ,
                        unsigned char * screen , unsigned int screenWidth ,unsigned int screenHeight ,
                        unsigned int sX,unsigned int sY , unsigned int width ,unsigned int height ,
                        unsigned int maximumAcceptedScore,
                        unsigned int * pick ,
                        unsigned int * resultPatternNum ,
                        unsigned int * resultTileNum
                        )
{
  unsigned int currentScore=maximumAcceptedScore+1;
  unsigned int bestScore=maximumAcceptedScore+1;
  unsigned int bestPick=0;
  unsigned int bestPattern=0;
  unsigned int bestTile=0;
  unsigned int bestCandidates=0;
  unsigned int tileNum=0;
  unsigned int patternNum=0;
  for ( patternNum=0; patternNum < pattSet->totalPatterns; patternNum++ )
  {
   if (pattSet->pattern[patternNum].totalTiles>0)
   {
    for ( tileNum=0; tileNum < pattSet->pattern[patternNum].totalTiles; tileNum++ )
    {
       currentScore=maximumAcceptedScore+1;
       if (
            compareRGBPatchesIgnoreColor(
                                         screen , sX , sY , screenWidth, screenHeight ,
                                         pattSet->pattern[patternNum].tile[tileNum]->pixels ,
                                         0, 0 ,
                                         pattSet->pattern[patternNum].tile[tileNum]->width ,
                                         pattSet->pattern[patternNum].tile[tileNum]->height,
                                         123,123,0,
                                         width, height ,
                                         &currentScore ,
                                         10*maximumAcceptedScore
                                        )
           )
      {
          float differentTransCompensationFactor = pattSet->pattern[patternNum].totalPixels[tileNum] / pattSet->pattern[patternNum].nonTransparentPixels[tileNum];
          currentScore = currentScore * differentTransCompensationFactor;
       if (currentScore<bestScore)
       {
         bestScore = currentScore;
         bestPick = pattSet->pattern[patternNum].value;
         bestPattern=patternNum;
         bestTile=tileNum;
         ++bestCandidates;
       }
      }
     }
    }
   if (bestScore < pattSet->pattern[patternNum].acceptScore )
    {
      fprintf(stderr,"INSTA-Selected %s with a score of %u \n",pattSet->pattern[patternNum].name,bestScore);
      *pick=bestPick;
      *resultPatternNum=bestPattern;
      *resultTileNum=bestTile;
      return 1;
    }
 }
  fprintf(stderr,"Thinking about best score\n",pattSet->totalPatterns);
   if (bestScore < maximumAcceptedScore )
    {
         unsigned int x=0,y=0;
         char comment[512]={0};
         char nameUsed[512]={0};
         snprintf(nameUsed,512,"Dump/tile%u_%u_like_%u_%u_score_%u",x,y,bestPattern,bestTile,bestScore);
         snprintf(comment,512,"Most like  %u   with score %u", bestPick,bestScore);
         bitBltRGBToFile( nameUsed ,
                           comment,
                           screen , sX , sY , screenWidth, screenHeight, width, height );
         snprintf(nameUsed,512,"Dump/tile%u_%u_like_%u_%u_score_%uB",x,y,bestPattern,bestTile,bestScore);
         bitBltRGBToFile( nameUsed , comment,
                           pattSet->pattern[bestPattern].tile[bestTile]->pixels ,
                           0 , 0
                           , pattSet->pattern[bestPattern].tile[bestTile]->width , pattSet->pattern[bestPattern].tile[bestTile]->height
                           , pattSet->pattern[bestPattern].tile[bestTile]->width , pattSet->pattern[bestPattern].tile[bestTile]->height );
      fprintf(stderr,"Not so sure , but selected %u with a score of %u \n",bestPick,bestScore);
      *pick=bestPick;
      *resultPatternNum=bestPattern;
      *resultTileNum=bestTile;
      return 1;
    }
 return 0;
}

canonical_original_code:
int compareToPatternSet(struct PatternSet *pattSet, unsigned char *screen, unsigned int screenWidth, unsigned int screenHeight, unsigned int sX, unsigned int sY, unsigned int width, unsigned int height, unsigned int maximumAcceptedScore, unsigned int *pick, unsigned int *resultPatternNum, unsigned int *resultTileNum)
{
  unsigned int currentScore = maximumAcceptedScore+1;
  unsigned int bestScore = maximumAcceptedScore+1;
  unsigned int bestPick = 0;
  unsigned int bestPattern = 0;
  unsigned int bestTile = 0;
  unsigned int bestCandidates = 0;
  unsigned int tileNum = 0;
  unsigned int patternNum = 0;
  for ( patternNum=0; patternNum < pattSet->totalPatterns; patternNum++ )
  {
   if (pattSet->pattern[patternNum].totalTiles>0)
   {
    for ( tileNum=0; tileNum < pattSet->pattern[patternNum].totalTiles; tileNum++ )
    {
       currentScore=maximumAcceptedScore+1;
       if (
            compareRGBPatchesIgnoreColor(
                                         screen , sX , sY , screenWidth, screenHeight ,
                                         pattSet->pattern[patternNum].tile[tileNum]->pixels ,
                                         0, 0 ,
                                         pattSet->pattern[patternNum].tile[tileNum]->width ,
                                         pattSet->pattern[patternNum].tile[tileNum]->height,
                                         123,123,0,
                                         width, height ,
                                         &currentScore ,
                                         10*maximumAcceptedScore
                                        )
           )
      {
          float differentTransCompensationFactor = pattSet->pattern[patternNum].totalPixels[tileNum] / pattSet->pattern[patternNum].nonTransparentPixels[tileNum];
          currentScore = currentScore * differentTransCompensationFactor;
       if (currentScore<bestScore)
       {
         bestScore = currentScore;
         bestPick = pattSet->pattern[patternNum].value;
         bestPattern=patternNum;
         bestTile=tileNum;
         ++bestCandidates;
       }
      }
     }
    }
   if (bestScore < pattSet->pattern[patternNum].acceptScore )
    {
      fprintf(stderr,"INSTA-Selected %s with a score of %u \n",pattSet->pattern[patternNum].name,bestScore);
      *pick=bestPick;
      *resultPatternNum=bestPattern;
      *resultTileNum=bestTile;
      return 1;
    }
 }
  fprintf(stderr,"Thinking about best score\n",pattSet->totalPatterns);
   if (bestScore < maximumAcceptedScore )
    {
         unsigned int x = 0; unsigned int y = 0;
         char comment[512] = {0};
         char nameUsed[512] = {0};
         snprintf(nameUsed,512,"Dump/tile%u_%u_like_%u_%u_score_%u",x,y,bestPattern,bestTile,bestScore);
         snprintf(comment,512,"Most like  %u   with score %u", bestPick,bestScore);
         bitBltRGBToFile( nameUsed ,
                           comment,
                           screen , sX , sY , screenWidth, screenHeight, width, height );
         snprintf(nameUsed,512,"Dump/tile%u_%u_like_%u_%u_score_%uB",x,y,bestPattern,bestTile,bestScore);
         bitBltRGBToFile( nameUsed , comment,
                           pattSet->pattern[bestPattern].tile[bestTile]->pixels ,
                           0 , 0
                           , pattSet->pattern[bestPattern].tile[bestTile]->width , pattSet->pattern[bestPattern].tile[bestTile]->height
                           , pattSet->pattern[bestPattern].tile[bestTile]->width , pattSet->pattern[bestPattern].tile[bestTile]->height );
      fprintf(stderr,"Not so sure , but selected %u with a score of %u \n",bestPick,bestScore);
      *pick=bestPick;
      *resultPatternNum=bestPattern;
      *resultTileNum=bestTile;
      return 1;
    }
 return 0;
}

variable_types:
screen
unsigned char *

variable_types:
screenWidth
unsigned int

variable_types:
screenHeight
unsigned int

variable_types:
sX
unsigned int

variable_types:
sY
unsigned int

variable_types:
width
unsigned int

variable_types:
height
unsigned int

variable_types:
maximumAcceptedScore
unsigned int

variable_types:
pick
unsigned int *

variable_types:
resultPatternNum
unsigned int *

variable_types:
resultTileNum
unsigned int *

variable_types:
currentScore
unsigned int

variable_types:
bestScore
unsigned int

variable_types:
bestPick
unsigned int

variable_types:
bestPattern
unsigned int

variable_types:
bestTile
unsigned int

variable_types:
bestCandidates
unsigned int

variable_types: differentTransCompensationFactor float

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types:
comment
char [512]

variable_types:
nameUsed
char [512]


original_code:
char *Get_Display_Name(
    int *pargc,
    char **argv)
{
    int argc = *pargc;
    char **pargv = argv+1;
    char *displayname = ((void *)0);
    int i;
    for (i = 1; i < argc; i++) {
 char *arg = argv[i];
 if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) {
     if (++i >= argc) usage ();
     displayname = argv[i];
     *pargc -= 2;
     continue;
 }
 if (!strcmp(arg,"-")) {
  while (i<argc)
   *pargv++ = argv[i++];
  break;
 }
 *pargv++ = arg;
    }
    *pargv = ((void *)0);
    return (displayname);
}

canonical_original_code:
char *Get_Display_Name(int *pargc, char **argv)
{
    int argc = *pargc;
    char **pargv = argv+1;
    char *displayname = ((void *)0);
    int i;
    for (i = 1; i < argc; i++) {
 char *arg = argv[i];
 if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) {
     if (++i >= argc) usage ();
     displayname = argv[i];
     *pargc -= 2;
     continue;
 }
 if (!strcmp(arg,"-")) {
  while (i<argc)
   *pargv++ = argv[i++];
  break;
 }
 *pargv++ = arg;
    }
    *pargv = ((void *)0);
    return (displayname);
}

variable_types:
pargc
int *

variable_types:
argv
char **

variable_types: argc int

variable_types:
pargv
char **

variable_types:
displayname
char *

variable_types: i int

variable_types:
arg
char *

return_type: char *


decompiled_code:
int __fastcall Setup_Display_And_Screen(int *a1, __int64 a2)
{
char *v2; // rax
int result; // eax

v2 = (char *)Get_Display_Name(a1, a2);
dpy = Open_Display(v2);
result = XDefaultScreen(dpy);
screen = result;
return result;
}

canonical_decompiled_code:
int __fastcall func1(int *a1, __int64 a2)
{
char *v2; // rax
int result; // eax

v2 = (char *)func0(a1, a2);
dpy = Open_Display(v2);
result = XDefaultScreen(dpy);
screen = result;
return result;
}

original_code:
void Setup_Display_And_Screen(
    int *argc,
    char **argv)
{
        char *displayname = ((void *)0);
        displayname = Get_Display_Name(argc, argv);
 dpy = Open_Display (displayname);
 screen = XDefaultScreen(dpy);
}

canonical_original_code:
void Setup_Display_And_Screen(int *argc, char **argv)
{
        char *displayname = ((void *)0);
        displayname = Get_Display_Name(argc, argv);
 dpy = Open_Display (displayname);
 screen = XDefaultScreen(dpy);
}

variable_types:
argc
int *

return_type: void


decompiled_code:
int Setup_Null_Display_And_Screen()
{
int result; // eax

dpy = Open_Display(0LL);
result = XDefaultScreen(dpy);
screen = result;
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax

dpy = Open_Display(0LL);
result = XDefaultScreen(dpy);
screen = result;
return result;
}

original_code:
void Setup_Null_Display_And_Screen( )
{
    char *displayname = ((void *)0);
 dpy = Open_Display (displayname);
 screen = XDefaultScreen(dpy);
}

canonical_original_code:
void Setup_Null_Display_And_Screen()
{
    char *displayname = ((void *)0);
 dpy = Open_Display (displayname);
 screen = XDefaultScreen(dpy);
}


decompiled_code:
int Close_Display()
{
int result; // eax

if ( dpy )
{
result = XCloseDisplay(dpy);
dpy = 0LL;
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax

if ( dpy )
{
result = XCloseDisplay(dpy);
dpy = 0LL;
}
return result;
}

original_code:
void Close_Display(void)
{
    if (dpy == ((void *)0)) return;
    XCloseDisplay(dpy);
    dpy = ((void *)0);
}

canonical_original_code:
void Close_Display()
{
    if (dpy == ((void *)0)) return;
    XCloseDisplay(dpy);
    dpy = ((void *)0);
}


getRootWindow

decompiled_code:
Window getRootWindow()
{
return dpy->screens[(__int64)screen].root;
}

canonical_decompiled_code:
Window func4()
{
return dpy->screens[(__int64)screen].root;
}

original_code:
Window getRootWindow()
{
  Window w=0;
  w=((&((_XPrivDisplay)(dpy))->screens[screen])->root);
  return (w);
}

canonical_original_code:
unsigned long getRootWindow()
{
  unsigned long w = 0;
  w=((&((_XPrivDisplay)(dpy))->screens[screen])->root);
  return (w);
}

variable_types:
w
unsigned long

return_type: unsigned long


outl

decompiled_code:
int outl(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fflush(stdout);
vfprintf(stderr, format, arg);
fputc(10, stderr);
return fflush(stderr);
}

canonical_decompiled_code:
int func6(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fflush(stdout);
vfprintf(stderr, format, arg);
fputc(10, stderr);
return fflush(stderr);
}

original_code:
void
outl(char *msg, ...)
{
 va_list args;
 fflush(stdout);
 __builtin_va_start(args,msg);
 vfprintf(stderr, msg, args);
 __builtin_va_end(args);
 fprintf(stderr, "\n");
 fflush(stderr);
}

canonical_original_code:
void outl(char *msg, ...)
{
 __builtin_va_list args;
 fflush(stdout);
 __builtin_va_start(args,msg);
 vfprintf(stderr, msg, args);
 __builtin_va_end(args);
 fprintf(stderr, "\n");
 fflush(stderr);
}

variable_types:
msg
char *


decompiled_code:
void __noreturn Fatal_Error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fflush(stdout);
fflush(stderr);
fprintf(stderr, "%s: error: ", program_name);
vfprintf(stderr, format, arg);
fputc(10, stderr);
Close_Display();
exit(1);
}

canonical_decompiled_code:
void __noreturn func7(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fflush(stdout);
fflush(stderr);
fprintf(stderr, "%s: error: ", program_name);
vfprintf(stderr, format, arg);
fputc(10, stderr);
func3();
exit(1);
}

original_code:
void Fatal_Error(char *msg, ...)
{
 va_list args;
 fflush(stdout);
 fflush(stderr);
 fprintf(stderr, "%s: error: ", program_name);
 __builtin_va_start(args,msg);
 vfprintf(stderr, msg, args);
 __builtin_va_end(args);
 fprintf(stderr, "\n");
        Close_Display();
 exit(1);
}

canonical_original_code:
void Fatal_Error(char *msg, ...)
{
 __builtin_va_list args;
 fflush(stdout);
 fflush(stderr);
 fprintf(stderr, "%s: error: ", program_name);
 __builtin_va_start(args,msg);
 vfprintf(stderr, msg, args);
 __builtin_va_end(args);
 fprintf(stderr, "\n");
        Close_Display();
 exit(1);
}


original_code:
Window Select_Window_Args(
    int *rargc,
    char **argv)
{
 int nargc=1;
 int argc;
 char **nargv;
 Window w=0;
 nargv = argv+1; argc = (*rargc);
 while (++argv, --argc>0) {
  if (!strcmp(argv[0], "-")) {
   nargv++[0]=argv[0], nargc++;
   while (++argv, --argc>0)
     nargv++[0]=argv[0], nargc++;
   break;
  }
  if (!strcmp(argv[0], "-root")) {
   w=((&((_XPrivDisplay)(dpy))->screens[screen])->root);
   continue;
  }
  if (!strcmp(argv[0], "-name")) {
   if (++argv, --argc==0) usage();
   w = Window_With_Name(dpy, ((&((_XPrivDisplay)(dpy))->screens[screen])->root),
          argv[0]);
   if (!w)
     Fatal_Error("No window with name %s exists!",argv[0]);
   continue;
  }
  if (!strcmp(argv[0], "-id")) {
   if (++argv, --argc==0) usage();
   w=0;
   sscanf(argv[0], "0x%lx", &w);
   if (!w)
     sscanf(argv[0], "%lu", &w);
   if (!w)
     Fatal_Error("Invalid window id format: %s.", argv[0]);
   continue;
  }
  nargv++[0]=argv[0], nargc++;
 }
 (*rargc) = nargc;
 return(w);
}

canonical_original_code:
unsigned long Select_Window_Args(int *rargc, char **argv)
{
 int nargc = 1;
 int argc;
 char **nargv;
 unsigned long w = 0;
 nargv = argv+1; argc = (*rargc);
 while (++argv, --argc>0) {
  if (!strcmp(argv[0], "-")) {
   nargv++[0]=argv[0], nargc++;
   while (++argv, --argc>0)
     nargv++[0]=argv[0], nargc++;
   break;
  }
  if (!strcmp(argv[0], "-root")) {
   w=((&((_XPrivDisplay)(dpy))->screens[screen])->root);
   continue;
  }
  if (!strcmp(argv[0], "-name")) {
   if (++argv, --argc==0) usage();
   w = Window_With_Name(dpy, ((&((_XPrivDisplay)(dpy))->screens[screen])->root),
          argv[0]);
   if (!w)
     Fatal_Error("No window with name %s exists!",argv[0]);
   continue;
  }
  if (!strcmp(argv[0], "-id")) {
   if (++argv, --argc==0) usage();
   w=0;
   sscanf(argv[0], "0x%lx", &w);
   if (!w)
     sscanf(argv[0], "%lu", &w);
   if (!w)
     Fatal_Error("Invalid window id format: %s.", argv[0]);
   continue;
  }
  nargv++[0]=argv[0], nargc++;
 }
 (*rargc) = nargc;
 return(w);
}

variable_types:
rargc
int *

variable_types: nargc int

variable_types:
nargv
char **


decompiled_code:
__int64 read_2_bytes()
{
int v0; // eax
int v1; // ebx
int v2; // eax
__int64 result; // rax

v0 = getc(myfile);
if ( v0 == -1 || (v1 = v0, v2 = getc(myfile), v2 == -1) )
result = 0LL;
else
result = (unsigned int)((v1 << 8) + v2);
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
int v1; // ebx
int v2; // eax
__int64 result; // rax

v0 = getc(myfile);
if ( v0 == -1 || (v1 = v0, v2 = getc(myfile), v2 == -1) )
result = 0LL;
else
result = (unsigned int)((v1 << 8) + v2);
return result;
}

original_code:
static unsigned int
read_2_bytes (void)
{
  int c1, c2;
  c1 = getc(myfile);
  if (c1 == (-1)) { read_byte_error_while_reading=1; return 0; }
  c2 = getc(myfile);
  if (c2 == (-1)) { read_byte_error_while_reading=1; return 0; }
  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
}

canonical_original_code:
static unsigned int read_2_bytes()
{
  int c1; int c2;
  c1 = getc(myfile);
  if (c1 == (-1)) { read_byte_error_while_reading=1; return 0; }
  c2 = getc(myfile);
  if (c2 == (-1)) { read_byte_error_while_reading=1; return 0; }
  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
}


decompiled_code:
int read_1_byte()
{
int result; // eax

result = getc(myfile);
if ( result == -1 )
result = 0;
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax

result = getc(myfile);
if ( result == -1 )
result = 0;
return result;
}

original_code:
static int
read_1_byte (void)
{
  int c;
  c = getc(myfile);
  if (c == (-1)) { read_byte_error_while_reading=1; return 0; }
  return c;
}

canonical_original_code:
static int read_1_byte()
{
  int c;
  c = getc(myfile);
  if (c == (-1)) { read_byte_error_while_reading=1; return 0; }
  return c;
}

variable_types: c int


GetJPGOrientationOfFile

decompiled_code:
__int64 __fastcall GetJPGOrientationOfFile(const char *a1)
{
char *v1; // r13
__int16 *v2; // rbx
int v3; // er12
unsigned int v5; // ebp
__int64 v6; // r13
__int64 v7; // rbx
unsigned int v8; // eax
int v9; // esi
unsigned int i; // eax
int v11; // ecx
int v12; // edx

v1 = exif_data;
v2 = (__int16 *)exif_data;
myfile = 0LL;
myfile = fopen(a1, "rb");
if ( myfile )
{
do
{
v2 = (__int16 *)((char *)v2 + 1);
*((_BYTE *)v2 - 1) = read_1_byte();
}
while ( &word_384 != v2 );
if ( byte_381 == -40 && ((unsigned __int8)byte_382 & exif_data[0]) == 0xFF && byte_383 == -31 )
{
v5 = read_2_bytes();
if ( v5 > 7 )
{
do
*v1++ = read_1_byte();
while ( v1 != &byte_386 );
if ( exif_data[0] == 69 && byte_381 == 120 && byte_382 == 105 && byte_383 == 102 && !word_384 )
{
v6 = v5 - 8;
v7 = 0LL;
if ( v5 != 8 )
{
do
*(_BYTE *)(++v7 + 895) = read_1_byte();
while ( v7 != v6 );
if ( (unsigned int)v6 > 0xB )
{
if ( exif_data[0] == 73 )
{
if ( byte_381 == 73 && !byte_383 && byte_382 == 42 && !byte_387 && !byte_386 )
{
v8 = (unsigned __int8)word_384 + (HIBYTE(word_384) << 8);
if ( v8 <= v5 - 10 )
{
v9 = 0;
v3 = ((unsigned __int8)exif_data[v8 + 1] << 8) + (unsigned __int8)exif_data[v8];
goto LABEL_26;
}
}
}
else if ( exif_data[0] == 77 && byte_381 == 77 && !byte_382 && byte_383 == 42 && !word_384 )
{
v8 = (unsigned __int8)byte_387 + ((unsigned __int8)byte_386 << 8);
if ( v8 <= v5 - 10 )
{
v9 = 1;
v3 = ((unsigned __int8)exif_data[v8] << 8) + (unsigned __int8)exif_data[v8 + 1];
LABEL_26:
if ( !v3 )
{
LABEL_48:
fclose(myfile);
return (unsigned int)v3;
}
for ( i = v8 + 2; v5 - 20 >= i; i += 12 )
{
v11 = (unsigned __int8)exif_data[i];
v12 = (unsigned __int8)exif_data[i + 1];
if ( v9 )
v11 <<= 8;
else
v12 <<= 8;
if ( v11 + v12 == 274 )
{
if ( v9 )
{
if ( exif_data[i + 8] )
goto LABEL_40;
v3 = (unsigned __int8)exif_data[i + 9];
}
else
{
if ( exif_data[i + 9] )
goto LABEL_40;
v3 = (unsigned __int8)exif_data[i + 8];
}
if ( v3 <= 8 )
goto LABEL_48;
goto LABEL_40;
}
if ( !--v3 )
goto LABEL_48;
}
goto LABEL_40;
}
}
LABEL_40:
fclose(myfile);
return 0;
}
}
}
}
}
v3 = 0;
fclose(myfile);
}
else
{
v3 = 0;
fprintf(stderr, " Can't open %s\n", a1);
}
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
char *v1; // r13
__int16 *v2; // rbx
int v3; // er12
unsigned int v5; // ebp
__int64 v6; // r13
__int64 v7; // rbx
unsigned int v8; // eax
int v9; // esi
unsigned int i; // eax
int v11; // ecx
int v12; // edx

v1 = exif_data;
v2 = (__int16 *)exif_data;
myfile = 0LL;
myfile = fopen(a1, "rb");
if ( myfile )
{
do
{
v2 = (__int16 *)((char *)v2 + 1);
*((_BYTE *)v2 - 1) = func1();
}
while ( &word_384 != v2 );
if ( byte_381 == -40 && ((unsigned __int8)byte_382 & exif_data[0]) == 0xFF && byte_383 == -31 )
{
v5 = func0();
if ( v5 > 7 )
{
do
*v1++ = func1();
while ( v1 != &byte_386 );
if ( exif_data[0] == 69 && byte_381 == 120 && byte_382 == 105 && byte_383 == 102 && !word_384 )
{
v6 = v5 - 8;
v7 = 0LL;
if ( v5 != 8 )
{
do
*(_BYTE *)(++v7 + 895) = func1();
while ( v7 != v6 );
if ( (unsigned int)v6 > 0xB )
{
if ( exif_data[0] == 73 )
{
if ( byte_381 == 73 && !byte_383 && byte_382 == 42 && !byte_387 && !byte_386 )
{
v8 = (unsigned __int8)word_384 + (HIBYTE(word_384) << 8);
if ( v8 <= v5 - 10 )
{
v9 = 0;
v3 = ((unsigned __int8)exif_data[v8 + 1] << 8) + (unsigned __int8)exif_data[v8];
goto LABEL_26;
}
}
}
else if ( exif_data[0] == 77 && byte_381 == 77 && !byte_382 && byte_383 == 42 && !word_384 )
{
v8 = (unsigned __int8)byte_387 + ((unsigned __int8)byte_386 << 8);
if ( v8 <= v5 - 10 )
{
v9 = 1;
v3 = ((unsigned __int8)exif_data[v8] << 8) + (unsigned __int8)exif_data[v8 + 1];
LABEL_26:
if ( !v3 )
{
LABEL_48:
fclose(myfile);
return (unsigned int)v3;
}
for ( i = v8 + 2; v5 - 20 >= i; i += 12 )
{
v11 = (unsigned __int8)exif_data[i];
v12 = (unsigned __int8)exif_data[i + 1];
if ( v9 )
v11 <<= 8;
else
v12 <<= 8;
if ( v11 + v12 == 274 )
{
if ( v9 )
{
if ( exif_data[i + 8] )
goto LABEL_40;
v3 = (unsigned __int8)exif_data[i + 9];
}
else
{
if ( exif_data[i + 9] )
goto LABEL_40;
v3 = (unsigned __int8)exif_data[i + 8];
}
if ( v3 <= 8 )
goto LABEL_48;
goto LABEL_40;
}
if ( !--v3 )
goto LABEL_48;
}
goto LABEL_40;
}
}
LABEL_40:
fclose(myfile);
return 0;
}
}
}
}
}
v3 = 0;
fclose(myfile);
}
else
{
v3 = 0;
fprintf(stderr, " Can't open %s\n", a1);
}
return (unsigned int)v3;
}

original_code:
unsigned int GetJPGOrientationOfFile(char * filename)
{
  read_byte_error_while_reading=0;
  myfile=0;
  int set_flag=0;
  unsigned int length=0, i=0;
  int is_motorola=0;
  unsigned int offset=0, number_of_tags=0, tagnum=0;
   set_flag = 0;
  i = 1;
  myfile = fopen(filename, "rb");
    if ( myfile == 0)
    {
      fprintf(stderr, " Can't open %s\n", filename);
      return 0;
    }
  for (i = 0; i < 4; i++) { exif_data[i] = (unsigned char) read_1_byte(); }
  if (exif_data[0] != 0xFF ||
      exif_data[1] != 0xD8 ||
      exif_data[2] != 0xFF ||
      exif_data[3] != 0xE1) { fclose(myfile); return 0; }
  length = read_2_bytes();
  if (length < 8) { fclose(myfile); return 0; }
  length -= 8;
  for (i = 0; i < 6; i++) { exif_data[i] = (unsigned char) read_1_byte(); }
  if (exif_data[0] != 0x45 ||
      exif_data[1] != 0x78 ||
      exif_data[2] != 0x69 ||
      exif_data[3] != 0x66 ||
      exif_data[4] != 0 ||
      exif_data[5] != 0) { fclose(myfile); return 0; }
  for (i = 0; i < length; i++) { exif_data[i] = (unsigned char) read_1_byte(); }
  if (length < 12) { fclose(myfile); return 0; }
  if (exif_data[0] == 0x49 && exif_data[1] == 0x49) { is_motorola = 0; } else
  if (exif_data[0] == 0x4D && exif_data[1] == 0x4D) { is_motorola = 1; } else
                                                    { fclose(myfile); return 0; }
  if (is_motorola)
  {
    if (exif_data[2] != 0) { fclose(myfile); return 0; }
    if (exif_data[3] != 0x2A) { fclose(myfile); return 0; }
  } else
  {
    if (exif_data[3] != 0) { fclose(myfile); return 0; }
    if (exif_data[2] != 0x2A) { fclose(myfile); return 0; }
  }
  if (is_motorola)
  {
    if (exif_data[4] != 0) { fclose(myfile); return 0; }
    if (exif_data[5] != 0) { fclose(myfile); return 0; }
    offset = exif_data[6];
    offset <<= 8;
    offset += exif_data[7];
  } else
  {
    if (exif_data[7] != 0) { fclose(myfile); return 0; }
    if (exif_data[6] != 0) { fclose(myfile); return 0; }
    offset = exif_data[5];
    offset <<= 8;
    offset += exif_data[4];
  }
  if (offset > length - 2) { fclose(myfile); return 0; }
  if (is_motorola)
  {
    number_of_tags = exif_data[offset];
    number_of_tags <<= 8;
    number_of_tags += exif_data[offset+1];
  } else
  {
    number_of_tags = exif_data[offset+1];
    number_of_tags <<= 8;
    number_of_tags += exif_data[offset];
  }
  if (number_of_tags == 0) { fclose(myfile); return 0; }
  offset += 2;
  for (;;)
  {
    if (offset > length - 12) { fclose(myfile); return 0; }
    if (is_motorola)
    {
      tagnum = exif_data[offset];
      tagnum <<= 8;
      tagnum += exif_data[offset+1];
    } else
    {
      tagnum = exif_data[offset+1];
      tagnum <<= 8;
      tagnum += exif_data[offset];
    }
    if (tagnum == 0x0112) break;
    if (--number_of_tags == 0) { fclose(myfile); return 0; }
    offset += 12;
  }
    if (is_motorola)
    {
      if (exif_data[offset+8] != 0) { fclose(myfile); return 0; }
      set_flag = exif_data[offset+9];
    } else
    {
      if (exif_data[offset+9] != 0) { fclose(myfile); return 0; }
      set_flag = exif_data[offset+8];
    }
    if (set_flag > 8) { fclose(myfile); return 0; }
    fclose(myfile);
  return (unsigned int) set_flag;
}

canonical_original_code:
unsigned int GetJPGOrientationOfFile(char *filename)
{
  read_byte_error_while_reading=0;
  myfile=0;
  int set_flag = 0;
  unsigned int length = 0; unsigned int i = 0;
  int is_motorola = 0;
  unsigned int offset = 0; unsigned int number_of_tags = 0; unsigned int tagnum = 0;
   set_flag = 0;
  i = 1;
  myfile = fopen(filename, "rb");
    if ( myfile == 0)
    {
      fprintf(stderr, " Can't open %s\n", filename);
      return 0;
    }
  for (i = 0; i < 4; i++) { exif_data[i] = (unsigned char) read_1_byte(); }
  if (exif_data[0] != 0xFF ||
      exif_data[1] != 0xD8 ||
      exif_data[2] != 0xFF ||
      exif_data[3] != 0xE1) { fclose(myfile); return 0; }
  length = read_2_bytes();
  if (length < 8) { fclose(myfile); return 0; }
  length -= 8;
  for (i = 0; i < 6; i++) { exif_data[i] = (unsigned char) read_1_byte(); }
  if (exif_data[0] != 0x45 ||
      exif_data[1] != 0x78 ||
      exif_data[2] != 0x69 ||
      exif_data[3] != 0x66 ||
      exif_data[4] != 0 ||
      exif_data[5] != 0) { fclose(myfile); return 0; }
  for (i = 0; i < length; i++) { exif_data[i] = (unsigned char) read_1_byte(); }
  if (length < 12) { fclose(myfile); return 0; }
  if (exif_data[0] == 0x49 && exif_data[1] == 0x49) { is_motorola = 0; } else
  if (exif_data[0] == 0x4D && exif_data[1] == 0x4D) { is_motorola = 1; } else
                                                    { fclose(myfile); return 0; }
  if (is_motorola)
  {
    if (exif_data[2] != 0) { fclose(myfile); return 0; }
    if (exif_data[3] != 0x2A) { fclose(myfile); return 0; }
  } else
  {
    if (exif_data[3] != 0) { fclose(myfile); return 0; }
    if (exif_data[2] != 0x2A) { fclose(myfile); return 0; }
  }
  if (is_motorola)
  {
    if (exif_data[4] != 0) { fclose(myfile); return 0; }
    if (exif_data[5] != 0) { fclose(myfile); return 0; }
    offset = exif_data[6];
    offset <<= 8;
    offset += exif_data[7];
  } else
  {
    if (exif_data[7] != 0) { fclose(myfile); return 0; }
    if (exif_data[6] != 0) { fclose(myfile); return 0; }
    offset = exif_data[5];
    offset <<= 8;
    offset += exif_data[4];
  }
  if (offset > length - 2) { fclose(myfile); return 0; }
  if (is_motorola)
  {
    number_of_tags = exif_data[offset];
    number_of_tags <<= 8;
    number_of_tags += exif_data[offset+1];
  } else
  {
    number_of_tags = exif_data[offset+1];
    number_of_tags <<= 8;
    number_of_tags += exif_data[offset];
  }
  if (number_of_tags == 0) { fclose(myfile); return 0; }
  offset += 2;
  for (;;)
  {
    if (offset > length - 12) { fclose(myfile); return 0; }
    if (is_motorola)
    {
      tagnum = exif_data[offset];
      tagnum <<= 8;
      tagnum += exif_data[offset+1];
    } else
    {
      tagnum = exif_data[offset+1];
      tagnum <<= 8;
      tagnum += exif_data[offset];
    }
    if (tagnum == 0x0112) break;
    if (--number_of_tags == 0) { fclose(myfile); return 0; }
    offset += 12;
  }
    if (is_motorola)
    {
      if (exif_data[offset+8] != 0) { fclose(myfile); return 0; }
      set_flag = exif_data[offset+9];
    } else
    {
      if (exif_data[offset+9] != 0) { fclose(myfile); return 0; }
      set_flag = exif_data[offset+8];
    }
    if (set_flag > 8) { fclose(myfile); return 0; }
    fclose(myfile);
  return (unsigned int) set_flag;
}

variable_types:
filename
char *

variable_types:
length
unsigned int

variable_types:
i
unsigned int

variable_types:
offset
unsigned int

variable_types:
tagnum
unsigned int


RGBfindImageInImage

decompiled_code:
__int64 __fastcall RGBfindImageInImage(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, unsigned int a5, unsigned int a6, unsigned int *a7, unsigned int *a8, unsigned int a9, unsigned int a10)
{
__int64 v11; // r15
__int64 v12; // r12
unsigned int v13; // er14
unsigned int v14; // er13
unsigned int v16; // [rsp+4h] [rbp-74h]
unsigned int v18; // [rsp+20h] [rbp-58h]
unsigned int v19; // [rsp+24h] [rbp-54h]
unsigned int v20; // [rsp+28h] [rbp-50h]
unsigned int v21; // [rsp+2Ch] [rbp-4Ch]
unsigned int v22[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

v22[0] = 10000000;
v21 = a3 - a6 - 1;
if ( v21 <= 1 )
{
v20 = 0;
v19 = 0;
v18 = 10000000;
LABEL_10:
if ( a9 < v18 )
{
fwrite("RGBfindImageInImage unable to find needle in haystack", 1uLL, 0x35uLL, stderr);
return 0LL;
}
fwrite("RGBfindImageInImage found an acceptable needle in the haystack\n", 1uLL, 0x3FuLL, stderr);
*a7 = v19;
*a8 = v20;
}
else
{
v20 = 0;
v11 = a5;
v12 = a6;
v13 = 1;
v19 = 0;
v16 = a2 - a5 - 1;
v18 = 10000000;
while ( 1 )
{
v14 = 1;
if ( v16 > 1 )
break;
LABEL_9:
if ( ++v13 == v21 )
goto LABEL_10;
}
while ( 1 )
{
if ( (unsigned int)compareRGBPatches(a1, v14, v13, a2, a3, a4, 0LL, 0LL, v11, v12, v11, v12, v22, a10) )
{
if ( v22[0] < v18 )
{
v20 = v13;
v19 = v14;
v18 = v22[0];
}
if ( v22[0] <= a9 )
break;
}
if ( v16 == ++v14 )
goto LABEL_9;
}
}
return v18 + 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, unsigned int a5, unsigned int a6, unsigned int *a7, unsigned int *a8, unsigned int a9, unsigned int a10)
{
__int64 v11; // r15
__int64 v12; // r12
unsigned int v13; // er14
unsigned int v14; // er13
unsigned int v16; // [rsp+4h] [rbp-74h]
unsigned int v18; // [rsp+20h] [rbp-58h]
unsigned int v19; // [rsp+24h] [rbp-54h]
unsigned int v20; // [rsp+28h] [rbp-50h]
unsigned int v21; // [rsp+2Ch] [rbp-4Ch]
unsigned int v22[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

v22[0] = 10000000;
v21 = a3 - a6 - 1;
if ( v21 <= 1 )
{
v20 = 0;
v19 = 0;
v18 = 10000000;
LABEL_10:
if ( a9 < v18 )
{
fwrite("RGBfindImageInImage unable to find needle in haystack", 1uLL, 0x35uLL, stderr);
return 0LL;
}
fwrite("RGBfindImageInImage found an acceptable needle in the haystack\n", 1uLL, 0x3FuLL, stderr);
*a7 = v19;
*a8 = v20;
}
else
{
v20 = 0;
v11 = a5;
v12 = a6;
v13 = 1;
v19 = 0;
v16 = a2 - a5 - 1;
v18 = 10000000;
while ( 1 )
{
v14 = 1;
if ( v16 > 1 )
break;
LABEL_9:
if ( ++v13 == v21 )
goto LABEL_10;
}
while ( 1 )
{
if ( (unsigned int)compareRGBPatches(a1, v14, v13, a2, a3, a4, 0LL, 0LL, v11, v12, v11, v12, v22, a10) )
{
if ( v22[0] < v18 )
{
v20 = v13;
v19 = v14;
v18 = v22[0];
}
if ( v22[0] <= a9 )
break;
}
if ( v16 == ++v14 )
goto LABEL_9;
}
}
return v18 + 1;
}

original_code:
int RGBfindImageInImage(
                        unsigned char * haystack , unsigned int haystackWidth , unsigned int haystackHeight ,
                        unsigned char * needle , unsigned int needleWidth , unsigned int needleHeight ,
                        unsigned int * resX ,
                        unsigned int * resY ,
                        unsigned int acceptableScore,
                        unsigned int maxScore
                       )
{
  unsigned int bestScore=10000000 , bestX=0 , bestY=0;
  unsigned int score=bestScore , x , y;
  for (y=1; y<haystackHeight-needleHeight-1; y++)
  {
    for (x=1; x<haystackWidth-needleWidth-1; x++)
    {
       if (
             compareRGBPatches( haystack , x , y , haystackWidth , haystackHeight,
                                needle , 0 , 0 , needleWidth , needleHeight ,
                                needleWidth , needleHeight
                                , &score , maxScore )
           )
           {
              if ( score < bestScore) { bestScore = score; bestX=x; bestY = y; }
              if (score <= acceptableScore ) { return bestScore+1; }
           }
    }
  }
  if (bestScore<=acceptableScore)
   {
    fprintf(stderr,"RGBfindImageInImage found an acceptable needle in the haystack\n");
    *resX = bestX;
    *resY = bestY;
    return bestScore+1;
   } else
   {
     fprintf(stderr,"RGBfindImageInImage unable to find needle in haystack");
   }
  return 0;
}

canonical_original_code:
int RGBfindImageInImage(unsigned char *haystack, unsigned int haystackWidth, unsigned int haystackHeight, unsigned char *needle, unsigned int needleWidth, unsigned int needleHeight, unsigned int *resX, unsigned int *resY, unsigned int acceptableScore, unsigned int maxScore)
{
  unsigned int bestScore = 10000000; unsigned int bestX = 0; unsigned int bestY = 0;
  unsigned int score = bestScore; unsigned int x; unsigned int y;
  for (y=1; y<haystackHeight-needleHeight-1; y++)
  {
    for (x=1; x<haystackWidth-needleWidth-1; x++)
    {
       if (
             compareRGBPatches( haystack , x , y , haystackWidth , haystackHeight,
                                needle , 0 , 0 , needleWidth , needleHeight ,
                                needleWidth , needleHeight
                                , &score , maxScore )
           )
           {
              if ( score < bestScore) { bestScore = score; bestX=x; bestY = y; }
              if (score <= acceptableScore ) { return bestScore+1; }
           }
    }
  }
  if (bestScore<=acceptableScore)
   {
    fprintf(stderr,"RGBfindImageInImage found an acceptable needle in the haystack\n");
    *resX = bestX;
    *resY = bestY;
    return bestScore+1;
   } else
   {
     fprintf(stderr,"RGBfindImageInImage unable to find needle in haystack");
   }
  return 0;
}

variable_types:
haystack
unsigned char *

variable_types:
haystackWidth
unsigned int

variable_types:
haystackHeight
unsigned int

variable_types:
needle
unsigned char *

variable_types:
needleWidth
unsigned int

variable_types:
needleHeight
unsigned int

variable_types:
resX
unsigned int *

variable_types:
resY
unsigned int *

variable_types:
acceptableScore
unsigned int

variable_types:
maxScore
unsigned int

variable_types:
bestScore
unsigned int

variable_types:
bestX
unsigned int

variable_types:
bestY
unsigned int

variable_types:
score
unsigned int

variable_types:
x
unsigned int

variable_types:
y
unsigned int


ReadPPM

decompiled_code:
_BOOL8 __fastcall ReadPPM(const char *a1, __int64 a2)
{
void *v2; // rax
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v2 = ReadPNM(*(void **)a2, a1, (unsigned int *)(a2 + 8), (unsigned int *)(a2 + 12), &v4);
*(_QWORD *)a2 = v2;
return v2 != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(const char *a1, __int64 a2)
{
void *v2; // rax
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v2 = func0(*(void **)a2, a1, (unsigned int *)(a2 + 8), (unsigned int *)(a2 + 12), &v4);
*(_QWORD *)a2 = v2;
return v2 != 0LL;
}

original_code:
int ReadPPM(char * filename,struct Image * pic,char read_only_header)
{
  unsigned long timestamp;
  pic->pixels = ReadPNM(pic->pixels , filename, &pic->width, &pic->height, &timestamp );
  return (pic->pixels!=0);
}

canonical_original_code:
int ReadPPM(char *filename, struct Image *pic, char read_only_header)
{
  unsigned long timestamp;
  pic->pixels = ReadPNM(pic->pixels , filename, &pic->width, &pic->height, &timestamp );
  return (pic->pixels!=0);
}

variable_types:
filename
char *

variable_types: struct Image { unsigned char *pixels; unsigned int width; unsigned int height; unsigned int channels; unsigned int bitsperpixel; unsigned int image_size; } *

variable_types:
timestamp
unsigned long


getPieceName

decompiled_code:
const char *__fastcall getPieceName(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 1:
result = "GREEN_PIECE";
break;
case 2:
result = "WHITE_PIECE";
break;
case 3:
result = "ORANGE_PIECE";
break;
case 4:
result = "YELLOW_PIECE";
break;
case 5:
result = "BLUE_PIECE";
break;
case 6:
result = "RED_PIECE";
break;
case 7:
result = "PINK_PIECE";
break;
case 8:
result = "HYPERCUBE_PIECE";
break;
default:
result = "UNKNOWN";
break;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 1:
result = "GREEN_PIECE";
break;
case 2:
result = "WHITE_PIECE";
break;
case 3:
result = "ORANGE_PIECE";
break;
case 4:
result = "YELLOW_PIECE";
break;
case 5:
result = "BLUE_PIECE";
break;
case 6:
result = "RED_PIECE";
break;
case 7:
result = "PINK_PIECE";
break;
case 8:
result = "HYPERCUBE_PIECE";
break;
default:
result = "UNKNOWN";
break;
}
return result;
}

original_code:
char * getPieceName(int pieceVal)
{
  switch (pieceVal)
  {
    case GREEN_PIECE : return "GREEN_PIECE"; break;
    case WHITE_PIECE : return "WHITE_PIECE"; break;
    case ORANGE_PIECE : return "ORANGE_PIECE"; break;
    case YELLOW_PIECE : return "YELLOW_PIECE"; break;
    case BLUE_PIECE : return "BLUE_PIECE"; break;
    case RED_PIECE : return "RED_PIECE"; break;
    case PINK_PIECE : return "PINK_PIECE"; break;
    case HYPERCUBE_PIECE : return "HYPERCUBE_PIECE"; break;
    default :
     return "UNKNOWN";
    break;
  }
  return "Nothing ";
}

canonical_original_code:
char *getPieceName(int pieceVal)
{
  switch (pieceVal)
  {
    case GREEN_PIECE : return "GREEN_PIECE"; break;
    case WHITE_PIECE : return "WHITE_PIECE"; break;
    case ORANGE_PIECE : return "ORANGE_PIECE"; break;
    case YELLOW_PIECE : return "YELLOW_PIECE"; break;
    case BLUE_PIECE : return "BLUE_PIECE"; break;
    case RED_PIECE : return "RED_PIECE"; break;
    case PINK_PIECE : return "PINK_PIECE"; break;
    case HYPERCUBE_PIECE : return "HYPERCUBE_PIECE"; break;
    default :
     return "UNKNOWN";
    break;
  }
  return "Nothing ";
}

variable_types: pieceVal int


writePieceChar

decompiled_code:
__int64 __fastcall writePieceChar(int a1)
{
__int64 result; // rax

switch ( a1 )
{
case 1:
printf("\x1B[32m G\x1B[0m");
result = 0LL;
break;
case 2:
printf("\x1B[37m W\x1B[0m");
result = 0LL;
break;
case 3:
printf("\x1B[33m O\x1B[0m");
result = 0LL;
break;
case 4:
printf("\x1B[33m Y\x1B[0m");
result = 0LL;
break;
case 5:
printf("\x1B[36m B\x1B[0m");
result = 0LL;
break;
case 6:
printf("\x1B[31m R\x1B[0m");
result = 0LL;
break;
case 7:
printf("\x1B[35m P\x1B[0m");
result = 0LL;
break;
case 8:
printf("\x1B[35m #\x1B[0m");
result = 0LL;
break;
default:
printf(" ?");
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 result; // rax

switch ( a1 )
{
case 1:
printf("\x1B[32m G\x1B[0m");
result = 0LL;
break;
case 2:
printf("\x1B[37m W\x1B[0m");
result = 0LL;
break;
case 3:
printf("\x1B[33m O\x1B[0m");
result = 0LL;
break;
case 4:
printf("\x1B[33m Y\x1B[0m");
result = 0LL;
break;
case 5:
printf("\x1B[36m B\x1B[0m");
result = 0LL;
break;
case 6:
printf("\x1B[31m R\x1B[0m");
result = 0LL;
break;
case 7:
printf("\x1B[35m P\x1B[0m");
result = 0LL;
break;
case 8:
printf("\x1B[35m #\x1B[0m");
result = 0LL;
break;
default:
printf(" ?");
result = 0LL;
break;
}
return result;
}

original_code:
int writePieceChar(int pieceVal)
{
  switch (pieceVal)
  {
    case GREEN_PIECE : printf("\033[32m" " G" "\033[0m"); break;
    case WHITE_PIECE : printf("\033[37m" " W" "\033[0m"); break;
    case ORANGE_PIECE : printf("\033[33m" " O" "\033[0m"); break;
    case YELLOW_PIECE : printf("\033[33m" " Y" "\033[0m"); break;
    case BLUE_PIECE : printf("\033[36m" " B" "\033[0m"); break;
    case RED_PIECE : printf("\033[31m" " R" "\033[0m"); break;
    case PINK_PIECE : printf("\033[35m" " P" "\033[0m"); break;
    case HYPERCUBE_PIECE : printf("\033[35m" " #" "\033[0m"); break;
    default :
     printf(" ?" );
    break;
  }
  return 0;
}


printTable

variable_types:
table
unsigned int [8][8]


formulatePlan

decompiled_code:
__int64 __fastcall formulatePlan(__int64 a1, unsigned int *a2, _DWORD *a3)
{
unsigned int v4; // er15
unsigned int v5; // er12
__int64 result; // rax
unsigned int v7; // edx
unsigned int *v8; // rbx
unsigned int *v9; // r14
unsigned int v10; // er15
__int64 v11; // rsi
__int64 v12; // rdx
__int64 v13; // rcx
__int64 v14; // r8
unsigned int v15; // edi
int v16; // er8
_DWORD *v17; // rdx
int v18; // eax
unsigned int v19; // esi
unsigned int v20; // ecx
int v21; // [rsp+4h] [rbp-44h]
unsigned int v22; // [rsp+8h] [rbp-40h]

v4 = settings[2];
v5 = settings[3];
*a3 = 0;
result = 0LL;
v7 = a2[1285];
if ( v7 )
{
v8 = a2;
if ( v7 > 3 )
a2[1285] = 3;
fprintf(stderr, "Old LastMove was %u,%u / ", lastMoveX, lastMoveY);
if ( a2[1285] )
{
v21 = 0;
v9 = a2;
v22 = v4 >> 1;
v10 = 0;
do
{
v11 = *v9;
if ( (unsigned int)v11 <= 7
&& (v12 = v9[1], (unsigned int)v12 <= 7)
&& (v13 = v9[2], (unsigned int)v13 <= 7)
&& (v14 = v9[3], (unsigned int)v14 <= 7) )
{
printf("Move %u,%u to %u,%u\n", v11, v12, v13, v14);
v15 = settings[0] + v22;
v16 = settings[1] + (v5 >> 1);
v17 = &a3[5 * v21++];
v17[1] = settings[0] + v22 + settings[2] * *v9;
v18 = settings[3];
v17[2] = v16 + v18 * v9[1];
v19 = v9[2];
v17[3] = v15 + v19 * v18;
v20 = v9[3];
*((_BYTE *)v17 + 20) = 2;
v17[4] = v16 + v20 * v18;
lastMoveX = v19;
lastMoveY = v20;
++*a3;
}
else
{
fwrite("Incorrect solution item\n", 1uLL, 0x18uLL, stderr);
}
++v10;
v9 += 5;
}
while ( v8[1285] > v10 );
}
fprintf(stderr, "New LastMove is %u,%u \n", lastMoveX, lastMoveY);
result = (unsigned int)*a3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int *a2, _DWORD *a3)
{
unsigned int v4; // er15
unsigned int v5; // er12
__int64 result; // rax
unsigned int v7; // edx
unsigned int *v8; // rbx
unsigned int *v9; // r14
unsigned int v10; // er15
__int64 v11; // rsi
__int64 v12; // rdx
__int64 v13; // rcx
__int64 v14; // r8
unsigned int v15; // edi
int v16; // er8
_DWORD *v17; // rdx
int v18; // eax
unsigned int v19; // esi
unsigned int v20; // ecx
int v21; // [rsp+4h] [rbp-44h]
unsigned int v22; // [rsp+8h] [rbp-40h]

v4 = settings[2];
v5 = settings[3];
*a3 = 0;
result = 0LL;
v7 = a2[1285];
if ( v7 )
{
v8 = a2;
if ( v7 > 3 )
a2[1285] = 3;
fprintf(stderr, "Old LastMove was %u,%u / ", lastMoveX, lastMoveY);
if ( a2[1285] )
{
v21 = 0;
v9 = a2;
v22 = v4 >> 1;
v10 = 0;
do
{
v11 = *v9;
if ( (unsigned int)v11 <= 7
&& (v12 = v9[1], (unsigned int)v12 <= 7)
&& (v13 = v9[2], (unsigned int)v13 <= 7)
&& (v14 = v9[3], (unsigned int)v14 <= 7) )
{
printf("Move %u,%u to %u,%u\n", v11, v12, v13, v14);
v15 = settings[0] + v22;
v16 = settings[1] + (v5 >> 1);
v17 = &a3[5 * v21++];
v17[1] = settings[0] + v22 + settings[2] * *v9;
v18 = settings[3];
v17[2] = v16 + v18 * v9[1];
v19 = v9[2];
v17[3] = v15 + v19 * v18;
v20 = v9[3];
*((_BYTE *)v17 + 20) = 2;
v17[4] = v16 + v20 * v18;
lastMoveX = v19;
lastMoveY = v20;
++*a3;
}
else
{
fwrite("Incorrect solution item\n", 1uLL, 0x18uLL, stderr);
}
++v10;
v9 += 5;
}
while ( v8[1285] > v10 );
}
fprintf(stderr, "New LastMove is %u,%u \n", lastMoveX, lastMoveY);
result = (unsigned int)*a3;
}
return result;
}

original_code:
int formulatePlan(unsigned int table[8][8] , struct solutionList * list , struct mouseMovements * ourPlan)
{
  unsigned int halfBlockX = (unsigned int) settings.blockX/2;
  unsigned int halfBlockY = (unsigned int) settings.blockY/2;
  ourPlan->totalMovements=0;
  if (list->currentSolutions==0) { return 0; }
  if (list->currentSolutions>3) { list->currentSolutions=3; }
 fprintf(stderr,"Old LastMove was %u,%u  / ",lastMoveX,lastMoveY);
  unsigned int i=0;
  unsigned int mC = 0;
  for (i=0; i<list->currentSolutions; i++)
  {
        if (
            (list->solution[i].fromX<8) &&
            (list->solution[i].fromY<8) &&
            (list->solution[i].toX<8) &&
            (list->solution[i].toY<8)
            )
        {
           printf("Move %u,%u to %u,%u\n",list->solution[i].fromX,list->solution[i].fromY,list->solution[i].toX,list->solution[i].toY);
           ourPlan->movement[mC].fromX = settings.clientX + halfBlockX + ( list->solution[i].fromX * settings.blockX);
           ourPlan->movement[mC].fromY = settings.clientY + halfBlockY + ( list->solution[i].fromY * settings.blockY);
           ourPlan->movement[mC].toX = settings.clientX + halfBlockX + ( list->solution[i].toX * settings.blockY);
           ourPlan->movement[mC].toY = settings.clientY + halfBlockY + ( list->solution[i].toY * settings.blockY);
           ourPlan->movement[mC].mode = MOVE_CLICK_AND_MOVE_CLICK;
           lastMoveX=list->solution[i].toX;
           lastMoveY=list->solution[i].toY;
           ++mC;
           ++ourPlan->totalMovements;
        } else
        { fprintf(stderr,"Incorrect solution item\n"); }
  }
  fprintf(stderr,"New LastMove is %u,%u \n",lastMoveX,lastMoveY );
  return ourPlan->totalMovements;
}

canonical_original_code:
int formulatePlan(unsigned int table[8][8], struct solutionList *list, struct mouseMovements *ourPlan)
{
  unsigned int halfBlockX = (unsigned int) settings.blockX/2;
  unsigned int halfBlockY = (unsigned int) settings.blockY/2;
  ourPlan->totalMovements=0;
  if (list->currentSolutions==0) { return 0; }
  if (list->currentSolutions>3) { list->currentSolutions=3; }
 fprintf(stderr,"Old LastMove was %u,%u  / ",lastMoveX,lastMoveY);
  unsigned int i = 0;
  unsigned int mC = 0;
  for (i=0; i<list->currentSolutions; i++)
  {
        if (
            (list->solution[i].fromX<8) &&
            (list->solution[i].fromY<8) &&
            (list->solution[i].toX<8) &&
            (list->solution[i].toY<8)
            )
        {
           printf("Move %u,%u to %u,%u\n",list->solution[i].fromX,list->solution[i].fromY,list->solution[i].toX,list->solution[i].toY);
           ourPlan->movement[mC].fromX = settings.clientX + halfBlockX + ( list->solution[i].fromX * settings.blockX);
           ourPlan->movement[mC].fromY = settings.clientY + halfBlockY + ( list->solution[i].fromY * settings.blockY);
           ourPlan->movement[mC].toX = settings.clientX + halfBlockX + ( list->solution[i].toX * settings.blockY);
           ourPlan->movement[mC].toY = settings.clientY + halfBlockY + ( list->solution[i].toY * settings.blockY);
           ourPlan->movement[mC].mode = MOVE_CLICK_AND_MOVE_CLICK;
           lastMoveX=list->solution[i].toX;
           lastMoveY=list->solution[i].toY;
           ++mC;
           ++ourPlan->totalMovements;
        } else
        { fprintf(stderr,"Incorrect solution item\n"); }
  }
  fprintf(stderr,"New LastMove is %u,%u \n",lastMoveX,lastMoveY );
  return ourPlan->totalMovements;
}

variable_types: struct solutionList { struct solutionItem { unsigned int fromX; unsigned int fromY; unsigned int toX; unsigned int toY; unsigned int score; } solution[-1]; unsigned int currentSolutions; } *

variable_types: struct mouseMovements { unsigned int totalMovements; struct mouseMovementItem { unsigned int fromX; unsigned int fromY; unsigned int toX; unsigned int toY; char mode; } movement[100]; } *

variable_types:
halfBlockX
unsigned int

variable_types:
halfBlockY
unsigned int

variable_types:
i
unsigned int

variable_types:
mC
unsigned int

user_defined_types: struct solutionList { struct solutionItem solution[-1]; unsigned int currentSolutions; }

user_defined_types: struct mouseMovements { unsigned int totalMovements; struct mouseMovementItem movement[100]; }


thinkWhatToPlay

decompiled_code:
__int64 __fastcall thinkWhatToPlay(__int64 a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned int *a6)
{
__int64 result; // rax
__int64 v9; // rdx
unsigned int v10; // er8
__int64 v11; // rcx
int v12; // eax
unsigned int *v13; // rdx
unsigned int v14; // [rsp+Ch] [rbp-155Ch] BYREF
char v15[256]; // [rsp+10h] [rbp-1558h] BYREF
unsigned int v16[1302]; // [rsp+110h] [rbp-1458h] BYREF

settings[0] = a4;
settings[1] = a5;
memset(v15, 0, sizeof(v15));
seeTable(v15, a1, a2, a3, a4, a5);
printTable((__int64)v15);
if ( (unsigned int)isSceneTooAmbiguous(v15) )
{
*a6 = 0;
printf("Scene is too ambiguous :( ( %u / %u ) \n", ++continuousUncertainty, 40LL);
result = 0LL;
if ( continuousUncertainty > 0x28 )
{
v9 = *a6;
v14 = 0;
v16[0] = 0;
LOBYTE(a6[5 * v9 + 5]) = 0;
*a6 = v9 + 1;
result = seeButtons(a1, a2, a3, &v14, v16);
if ( (_DWORD)result )
{
v10 = v14;
v11 = v16[0];
v12 = *a6;
v13 = &a6[5 * *a6];
v13[3] = v14 + 1;
v13[4] = v11 + 1;
v13[1] = v10;
v13[2] = v11;
*((_BYTE *)v13 + 20) = 1;
LOBYTE(a6[5 * (v12 + 1) + 5]) = 0;
*a6 = v12 + 2;
fprintf(stderr, "Grinding play , Found Button @ %u , %u \n", v10, v11);
result = 1LL;
}
}
}
else
{
memset(v16, 0, 0x1418uLL);
getValidMoves(v15, v16);
result = formulatePlan((__int64)v15, v16, a6);
continuousUncertainty = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5, unsigned int *a6)
{
__int64 result; // rax
__int64 v9; // rdx
unsigned int v10; // er8
__int64 v11; // rcx
int v12; // eax
unsigned int *v13; // rdx
unsigned int v14; // [rsp+Ch] [rbp-155Ch] BYREF
char v15[256]; // [rsp+10h] [rbp-1558h] BYREF
unsigned int v16[1302]; // [rsp+110h] [rbp-1458h] BYREF

settings[0] = a4;
settings[1] = a5;
memset(v15, 0, sizeof(v15));
seeTable(v15, a1, a2, a3, a4, a5);
func2((__int64)v15);
if ( (unsigned int)isSceneTooAmbiguous(v15) )
{
*a6 = 0;
printf("Scene is too ambiguous :( ( %u / %u ) \n", ++continuousUncertainty, 40LL);
result = 0LL;
if ( continuousUncertainty > 0x28 )
{
v9 = *a6;
v14 = 0;
v16[0] = 0;
LOBYTE(a6[5 * v9 + 5]) = 0;
*a6 = v9 + 1;
result = seeButtons(a1, a2, a3, &v14, v16);
if ( (_DWORD)result )
{
v10 = v14;
v11 = v16[0];
v12 = *a6;
v13 = &a6[5 * *a6];
v13[3] = v14 + 1;
v13[4] = v11 + 1;
v13[1] = v10;
v13[2] = v11;
*((_BYTE *)v13 + 20) = 1;
LOBYTE(a6[5 * (v12 + 1) + 5]) = 0;
*a6 = v12 + 2;
fprintf(stderr, "Grinding play , Found Button @ %u , %u \n", v10, v11);
result = 1LL;
}
}
}
else
{
memset(v16, 0, 0x1418uLL);
getValidMoves(v15, v16);
result = func3((__int64)v15, v16, a6);
continuousUncertainty = 0;
}
return result;
}

original_code:
int thinkWhatToPlay(unsigned char * screen , unsigned int screenWidth ,unsigned int screenHeight ,
                    unsigned int clientStartX , unsigned int clientStartY ,
                    struct mouseMovements * ourPlan )
{
    unsigned int resX = clientStartX , resY = clientStartY;
    settings.clientX = clientStartX;
    settings.clientY = clientStartY;
    unsigned int table[8][8]={0};
    seeTable(table , screen , screenWidth , screenHeight , clientStartX , clientStartY);
    printTable(table);
    if ( isSceneTooAmbiguous(table) )
       {
         ourPlan->totalMovements=0;
         ++continuousUncertainty;
         printf("Scene is too ambiguous :( ( %u / %u ) \n", continuousUncertainty , 40);
         if ( (1) && (continuousUncertainty>40) )
         {
           ourPlan->movement[ourPlan->totalMovements].mode = MOVE_TO_NEUTRAL;
           ++ourPlan->totalMovements;
           unsigned int clickX=0 , clickY=0;
           if ( seeButtons( screen , screenWidth , screenHeight , &clickX , &clickY) )
              {
                 ourPlan->movement[ourPlan->totalMovements].fromX = clickX;
                 ourPlan->movement[ourPlan->totalMovements].fromY = clickY;
                 ourPlan->movement[ourPlan->totalMovements].toX = clickX+1;
                 ourPlan->movement[ourPlan->totalMovements].toY = clickY+1;
                 ourPlan->movement[ourPlan->totalMovements].mode = SINGLE_MOVE_CLICK;
                 ++ourPlan->totalMovements;
                 ourPlan->movement[ourPlan->totalMovements].mode = MOVE_TO_NEUTRAL;
                 ++ourPlan->totalMovements;
                 fprintf(stderr,"Grinding play , Found Button @ %u , %u \n",clickX,clickY);
                 return 1;
              }
         }
         return 0;
       }
    struct solutionList list={0};
    list.currentSolutions=0;
    getValidMoves(table , &list );
    unsigned int totalSolutions = formulatePlan(table , &list ,ourPlan);
    continuousUncertainty=0;
    return totalSolutions;
}

canonical_original_code:
int thinkWhatToPlay(unsigned char *screen, unsigned int screenWidth, unsigned int screenHeight, unsigned int clientStartX, unsigned int clientStartY, struct mouseMovements *ourPlan)
{
    unsigned int resX = clientStartX; unsigned int resY = clientStartY;
    settings.clientX = clientStartX;
    settings.clientY = clientStartY;
    unsigned int table[8][8] = {0};
    seeTable(table , screen , screenWidth , screenHeight , clientStartX , clientStartY);
    printTable(table);
    if ( isSceneTooAmbiguous(table) )
       {
         ourPlan->totalMovements=0;
         ++continuousUncertainty;
         printf("Scene is too ambiguous :( ( %u / %u ) \n", continuousUncertainty , 40);
         if ( (1) && (continuousUncertainty>40) )
         {
           ourPlan->movement[ourPlan->totalMovements].mode = MOVE_TO_NEUTRAL;
           ++ourPlan->totalMovements;
           unsigned int clickX = 0; unsigned int clickY = 0;
           if ( seeButtons( screen , screenWidth , screenHeight , &clickX , &clickY) )
              {
                 ourPlan->movement[ourPlan->totalMovements].fromX = clickX;
                 ourPlan->movement[ourPlan->totalMovements].fromY = clickY;
                 ourPlan->movement[ourPlan->totalMovements].toX = clickX+1;
                 ourPlan->movement[ourPlan->totalMovements].toY = clickY+1;
                 ourPlan->movement[ourPlan->totalMovements].mode = SINGLE_MOVE_CLICK;
                 ++ourPlan->totalMovements;
                 ourPlan->movement[ourPlan->totalMovements].mode = MOVE_TO_NEUTRAL;
                 ++ourPlan->totalMovements;
                 fprintf(stderr,"Grinding play , Found Button @ %u , %u \n",clickX,clickY);
                 return 1;
              }
         }
         return 0;
       }
    struct solutionList list = {0};
    list.currentSolutions=0;
    getValidMoves(table , &list );
    unsigned int totalSolutions = formulatePlan(table , &list ,ourPlan);
    continuousUncertainty=0;
    return totalSolutions;
}

variable_types:
screen
unsigned char *

variable_types:
screenWidth
unsigned int

variable_types:
screenHeight
unsigned int

variable_types:
clientStartX
unsigned int

variable_types:
clientStartY
unsigned int

variable_types:
resX
unsigned int

variable_types:
resY
unsigned int

variable_types:
clickX
unsigned int

variable_types:
clickY
unsigned int

variable_types: struct solutionList { struct solutionItem { unsigned int fromX; unsigned int fromY; unsigned int toX; unsigned int toY; unsigned int score; } solution[-1]; unsigned int currentSolutions; }

variable_types:
totalSolutions
unsigned int

user_defined_types: struct mouseMovementItem { unsigned int fromX; unsigned int fromY; unsigned int toX; unsigned int toY; char mode; }


initializeEngine

decompiled_code:
__int64 __fastcall initializeEngine(char *s1)
{
if ( !strcasecmp(s1, "bejeweled blitz") )
{
*(_QWORD *)settings = 0x18F0000014DLL;
*(_QWORD *)&settings[2] = 0x2800000028LL;
*(_QWORD *)&settings[4] = 0x300000000LL;
settings[6] = 29;
return initVision();
}
if ( strcasecmp(s1, "gweled") )
return initVision();
*(_QWORD *)settings = 0x17E00000151LL;
*(_QWORD *)&settings[2] = 0x3000000030LL;
*(_QWORD *)&settings[4] = 0x300000000LL;
settings[6] = 29;
return initVision();
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s1)
{
if ( !strcasecmp(s1, "bejeweled blitz") )
{
*(_QWORD *)settings = 0x18F0000014DLL;
*(_QWORD *)&settings[2] = 0x2800000028LL;
*(_QWORD *)&settings[4] = 0x300000000LL;
settings[6] = 29;
return initVision();
}
if ( strcasecmp(s1, "gweled") )
return initVision();
*(_QWORD *)settings = 0x17E00000151LL;
*(_QWORD *)&settings[2] = 0x3000000030LL;
*(_QWORD *)&settings[4] = 0x300000000LL;
settings[6] = 29;
return initVision();
}

original_code:
int initializeEngine(char * settingsStr)
{
    if ( strcasecmp(settingsStr,"bejeweled blitz")==0 )
    {
      settings.clientX = 333;
      settings.clientY = 399;
      settings.blockX = 40;
      settings.blockY = 40;
      settings.offsetX = 0;
      settings.offsetY = 3;
      settings.threshold = 29;
    } else
    if ( strcasecmp(settingsStr,"gweled")==0 )
    {
      settings.clientX = 337;
      settings.clientY = 382;
      settings.blockX = 48;
      settings.blockY = 48;
      settings.offsetX = 0;
      settings.offsetY = 3;
      settings.threshold = 29;
    }
    return initVision();
}

canonical_original_code:
int initializeEngine(char *settingsStr)
{
    if ( strcasecmp(settingsStr,"bejeweled blitz")==0 )
    {
      settings.clientX = 333;
      settings.clientY = 399;
      settings.blockX = 40;
      settings.blockY = 40;
      settings.offsetX = 0;
      settings.offsetY = 3;
      settings.threshold = 29;
    } else
    if ( strcasecmp(settingsStr,"gweled")==0 )
    {
      settings.clientX = 337;
      settings.clientY = 382;
      settings.blockX = 48;
      settings.blockY = 48;
      settings.offsetX = 0;
      settings.offsetY = 3;
      settings.threshold = 29;
    }
    return initVision();
}

variable_types:
settingsStr
char *


stopEngine

decompiled_code:
__int64 stopEngine()
{
return stopVision();
}

canonical_decompiled_code:
__int64 func6()
{
return stopVision();
}

original_code:
int stopEngine()
{
    stopVision();
}

canonical_original_code:
int stopEngine()
{
    stopVision();
}


decompiled_code:
void __noreturn abort_(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
vfprintf(stderr, a1, arg);
fputc(10, stderr);
}

canonical_decompiled_code:
void __noreturn func0(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
vfprintf(stderr, a1, arg);
fputc(10, stderr);
}

original_code:
void abort_(const char * s, ...)
{
        va_list args;
        __builtin_va_start(args,s);
        vfprintf(stderr, s, args);
        fprintf(stderr, "\n");
        __builtin_va_end(args);
}

canonical_original_code:
void abort_(char *s, ...)
{
        __builtin_va_list args;
        __builtin_va_start(args,s);
        vfprintf(stderr, s, args);
        fprintf(stderr, "\n");
        __builtin_va_end(args);
}

variable_types:
s
char *


main

canonical_name: main

repo: AndoniZubimendi/directfb-render

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
Display *v3; // rdi
__int64 v4; // rdx
__int64 v5; // rcx
XVisualInfo *v6; // r8
Visual **v7; // r12
int v8; // er14
int v9; // er15
Visual *v10; // r13
Colormap v11; // rbp
__int64 v12; // rax
__int64 v13; // rbx
const char *v14; // rsi
int v17; // [rsp+1Ch] [rbp-20ECh] BYREF
XVisualInfo v18; // [rsp+20h] [rbp-20E8h] BYREF
XSetWindowAttributes v19; // [rsp+60h] [rbp-20A8h] BYREF
char s[4096]; // [rsp+D0h] [rbp-2038h] BYREF
char dest[4152]; // [rsp+10D0h] [rbp-1038h] BYREF

display = XOpenDisplay(0LL);
if ( !(unsigned int)XRenderQueryExtension() )
{
fwrite("RENDER extension missing!\n", 1uLL, 0x1AuLL, stderr);
abort();
}
v3 = display;
v18.screen = display->default_screen;
*(_QWORD *)&v18.depth = 0x400000020LL;
v6 = XGetVisualInfo(display, 14LL, &v18, &v17);
if ( v17 <= 0 )
main_cold(v3, 14LL, v4, v5, v6);
v7 = &v6->visual;
v8 = 0;
v9 = 0;
v10 = 0LL;
v11 = 0LL;
while ( 1 )
{
v12 = XRenderFindVisualFormat();
v13 = v12;
if ( *(_DWORD *)(v12 + 8) == 1 && *(_WORD *)(v12 + 30) )
{
v10 = *v7;
v9 = 1;
++v8;
v7 += 8;
v11 = XCreateColormap(display, display->screens[(__int64)display->default_screen].root, v10, 0);
if ( v17 <= v8 )
{
LABEL_8:
if ( v9 )
puts("ARGB Visual found.");
printf("Depth: %i\n", *(unsigned int *)(v13 + 12));
screen = display->default_screen;
root = display->screens[(__int64)screen].root;
v19.background_pixel = XBlackPixel(display, screen);
v19.border_pixel = XBlackPixel(display, screen);
v19.colormap = v11;
window = XCreateWindow(display, root, 0, 0, 0x2D0u, 0x240u, 0, *(_DWORD *)(v13 + 12), 1u, v10, 0x200AuLL, &v19);
printf("Window Id: 0x%lx\n", window);
XMapWindow(display, window);
XSync(display, window);
XSelectInput(display, window, 163847LL);
v14 = "main.ncl";
if ( argc > 1 )
v14 = argv[1];
strcpy(dest, v14);
sprintf(s, "DIRECTFB_WINDOWID=0x%lx ginga --ncl %s", window, dest);
printf("Lanzamos Ginga con el siguiente comando: %s\n", s);
system(s);
while ( 1 )
XNextEvent(display, &event);
}
}
else
{
++v8;
v7 += 8;
if ( v17 <= v8 )
goto LABEL_8;
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
Display *v3; // rdi
__int64 v4; // rdx
__int64 v5; // rcx
XVisualInfo *v6; // r8
Visual **v7; // r12
int v8; // er14
int v9; // er15
Visual *v10; // r13
Colormap v11; // rbp
__int64 v12; // rax
__int64 v13; // rbx
const char *v14; // rsi
int v17; // [rsp+1Ch] [rbp-20ECh] BYREF
XVisualInfo v18; // [rsp+20h] [rbp-20E8h] BYREF
XSetWindowAttributes v19; // [rsp+60h] [rbp-20A8h] BYREF
char s[4096]; // [rsp+D0h] [rbp-2038h] BYREF
char dest[4152]; // [rsp+10D0h] [rbp-1038h] BYREF

display = XOpenDisplay(0LL);
if ( !(unsigned int)XRenderQueryExtension() )
{
fwrite("RENDER extension missing!\n", 1uLL, 0x1AuLL, stderr);
abort();
}
v3 = display;
v18.screen = display->default_screen;
*(_QWORD *)&v18.depth = 0x400000020LL;
v6 = XGetVisualInfo(display, 14LL, &v18, &v17);
if ( v17 <= 0 )
func1(v3, 14LL, v4, v5, v6);
v7 = &v6->visual;
v8 = 0;
v9 = 0;
v10 = 0LL;
v11 = 0LL;
while ( 1 )
{
v12 = XRenderFindVisualFormat();
v13 = v12;
if ( *(_DWORD *)(v12 + 8) == 1 && *(_WORD *)(v12 + 30) )
{
v10 = *v7;
v9 = 1;
++v8;
v7 += 8;
v11 = XCreateColormap(display, display->screens[(__int64)display->default_screen].root, v10, 0);
if ( v17 <= v8 )
{
LABEL_8:
if ( v9 )
puts("ARGB Visual found.");
printf("Depth: %i\n", *(unsigned int *)(v13 + 12));
screen = display->default_screen;
root = display->screens[(__int64)screen].root;
v19.background_pixel = XBlackPixel(display, screen);
v19.border_pixel = XBlackPixel(display, screen);
v19.colormap = v11;
window = XCreateWindow(display, root, 0, 0, 0x2D0u, 0x240u, 0, *(_DWORD *)(v13 + 12), 1u, v10, 0x200AuLL, &v19);
printf("Window Id: 0x%lx\n", window);
XMapWindow(display, window);
XSync(display, window);
XSelectInput(display, window, 163847LL);
v14 = "main.ncl";
if ( argc > 1 )
v14 = argv[1];
strcpy(dest, v14);
sprintf(s, "DIRECTFB_WINDOWID=0x%lx ginga --ncl %s", window, dest);
printf("Lanzamos Ginga con el siguiente comando: %s\n", s);
system(s);
while ( 1 )
XNextEvent(display, &event);
}
}
else
{
++v8;
v7 += 8;
if ( v17 <= v8 )
goto LABEL_8;
}
}
}

original_code:
int main(int argc, char *argv[])
{
 display=XOpenDisplay(((void *)0));
 Colormap colormap = 0;
 Visual *visual = 0;
 int render_event_base, render_error_base;
 int render_present=XRenderQueryExtension(display, &render_event_base, &render_error_base);
 if (!render_present) {
  fprintf(stderr, "RENDER extension missing!\n");
  abort();
 }
 XVisualInfo templ;
 int nvi;
 templ.screen = (((_XPrivDisplay)(display))->default_screen);
 templ.depth = 32;
 templ.class = 4;
 XVisualInfo *xvi = XGetVisualInfo(display, 0x2 | 0x4 | 0x8, &templ, &nvi );
 int argbVisual = 0;
 XRenderPictFormat *fmt= 0;
 int i;
 for (i = 0;i < nvi; ++i) {
  fmt=XRenderFindVisualFormat(display, xvi[i].visual);
  if (fmt->type == 1 && fmt->direct.alphaMask) {
   visual = xvi[i].visual;
   colormap = XCreateColormap(display, ((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay)(display))->default_screen)])->root), visual, 0);
   argbVisual = 1;
  }
 }
 if (argbVisual) {
  printf("ARGB Visual found.\n");
 }
 printf("Depth: %i\n", fmt->depth);
 screen=(((_XPrivDisplay)(display))->default_screen);
 root=((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay)(display))->default_screen)])->root);
 int x_pos = 0;
 int y_pos = 0;
 int width = 720;
 int height = 576;
 int border_width = 0;
  XSetWindowAttributes winattr;
  winattr.background_pixel = XBlackPixel(display, screen);
  winattr.border_pixel = XBlackPixel(display, screen);
  winattr.colormap = colormap;
  long unsigned int winattr_mask = 0;
  winattr_mask |= (1L<<1);
  winattr_mask |= (1L<<3);
  winattr_mask |= (1L<<13);
 window=XCreateWindow(display, root, x_pos, y_pos,
     width, height, border_width,
  fmt->depth, 1,
  visual, winattr_mask, &winattr);
 printf("Window Id: 0x%lx\n", window);
 XMapWindow(display, window);
 XSync(display, window);
 XSelectInput(display, window, (1L<<0)|(1L<<1)|(1L<<15)
  |(1L<<2)|(1L<<17));
 char cmdGinga[4096];
 char nclApp[4096];
 strcpy(nclApp, ((argc>1)?argv[1]:"main.ncl"));
 sprintf(cmdGinga, "DIRECTFB_WINDOWID=0x%lx ginga --ncl %s", window, nclApp);
 printf("Lanzamos Ginga con el siguiente comando: %s\n", cmdGinga);
 system(cmdGinga);
 while(1) {
  XNextEvent(display, &event);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 display=XOpenDisplay(((void *)0));
 unsigned long colormap = 0;
 struct Visual *visual = 0;
 int render_event_base; int render_error_base;
 int render_present = XRenderQueryExtension(display, &render_event_base, &render_error_base);
 if (!render_present) {
  fprintf(stderr, "RENDER extension missing!\n");
  abort();
 }
 struct XVisualInfo templ;
 int nvi;
 templ.screen = (((_XPrivDisplay)(display))->default_screen);
 templ.depth = 32;
 templ.class = 4;
 struct XVisualInfo *xvi = XGetVisualInfo(display, 0x2 | 0x4 | 0x8, &templ, &nvi );
 int argbVisual = 0;
 struct XRenderPictFormat *fmt = 0;
 int i;
 for (i = 0;i < nvi; ++i) {
  fmt=XRenderFindVisualFormat(display, xvi[i].visual);
  if (fmt->type == 1 && fmt->direct.alphaMask) {
   visual = xvi[i].visual;
   colormap = XCreateColormap(display, ((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay)(display))->default_screen)])->root), visual, 0);
   argbVisual = 1;
  }
 }
 if (argbVisual) {
  printf("ARGB Visual found.\n");
 }
 printf("Depth: %i\n", fmt->depth);
 screen=(((_XPrivDisplay)(display))->default_screen);
 root=((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay)(display))->default_screen)])->root);
 int x_pos = 0;
 int y_pos = 0;
 int width = 720;
 int height = 576;
 int border_width = 0;
  struct XSetWindowAttributes winattr;
  winattr.background_pixel = XBlackPixel(display, screen);
  winattr.border_pixel = XBlackPixel(display, screen);
  winattr.colormap = colormap;
  unsigned long winattr_mask = 0;
  winattr_mask |= (1L<<1);
  winattr_mask |= (1L<<3);
  winattr_mask |= (1L<<13);
 window=XCreateWindow(display, root, x_pos, y_pos,
     width, height, border_width,
  fmt->depth, 1,
  visual, winattr_mask, &winattr);
 printf("Window Id: 0x%lx\n", window);
 XMapWindow(display, window);
 XSync(display, window);
 XSelectInput(display, window, (1L<<0)|(1L<<1)|(1L<<15)
  |(1L<<2)|(1L<<17));
 char cmdGinga[4096];
 char nclApp[4096];
 strcpy(nclApp, ((argc>1)?argv[1]:"main.ncl"));
 sprintf(cmdGinga, "DIRECTFB_WINDOWID=0x%lx ginga --ncl %s", window, nclApp);
 printf("Lanzamos Ginga con el siguiente comando: %s\n", cmdGinga);
 system(cmdGinga);
 while(1) {
  XNextEvent(display, &event);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
colormap
unsigned long

variable_types: nvi int

variable_types: struct XVisualInfo { struct Visual { struct _XExtData { int number; struct _XExtData *next; int (*free_private)(struct _XExtData *extension); char *private_data; } *ext_data; unsigned long visualid; int class; unsigned long red_mask; int bits_per_rgb; int map_entries; } *visual; unsigned long visualid; int screen; int depth; int class; unsigned long red_mask; unsigned long green_mask; unsigned long blue_mask; int colormap_size; int bits_per_rgb; } *

variable_types: argbVisual int

variable_types: struct XRenderPictFormat { unsigned long id; int type; int depth; struct XRenderDirectFormat { short red; short redMask; short green; short greenMask; short blue; short blueMask; short alpha; short alphaMask; } direct; unsigned long colormap; } *

variable_types: i int

variable_types: width int

variable_types: height int

variable_types: struct XSetWindowAttributes { unsigned long background_pixmap; unsigned long background_pixel; unsigned long border_pixmap; unsigned long border_pixel; int bit_gravity; int win_gravity; int backing_store; unsigned long backing_planes; unsigned long backing_pixel; int save_under; long event_mask; long do_not_propagate_mask; int override_redirect; unsigned long colormap; unsigned long cursor; }

variable_types:
cmdGinga
char [4096]

variable_types:
nclApp
char [4096]

user_defined_types: struct XVisualInfo { struct Visual *visual; unsigned long visualid; int screen; int depth; int class; unsigned long red_mask; unsigned long green_mask; unsigned long blue_mask; int colormap_size; int bits_per_rgb; }

user_defined_types: struct Visual { struct _XExtData *ext_data; unsigned long visualid; int class; unsigned long red_mask; int bits_per_rgb; int map_entries; }

user_defined_types: struct XRenderPictFormat { unsigned long id; int type; int depth; struct XRenderDirectFormat direct; unsigned long colormap; }


vpsmemPoolDataSize

decompiled_code:
__int64 vpsmemPoolDataSize()
{
return 32LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 32LL;
}

original_code:
static int
vpsmemPoolDataSize( void )
{
     return sizeof(VPSMemPoolData);
}

canonical_original_code:
static int vpsmemPoolDataSize()
{
     return sizeof(VPSMemPoolData);
}


vpsmemPoolLocalDataSize

decompiled_code:
__int64 vpsmemPoolLocalDataSize()
{
return 16LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 16LL;
}

original_code:
static int
vpsmemPoolLocalDataSize( void )
{
     return sizeof(VPSMemPoolLocalData);
}

canonical_original_code:
static int vpsmemPoolLocalDataSize()
{
     return sizeof(VPSMemPoolLocalData);
}


vpsmemAllocationDataSize

decompiled_code:
__int64 vpsmemAllocationDataSize()
{
return 24LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 24LL;
}

original_code:
static int
vpsmemAllocationDataSize( void )
{
     return sizeof(VPSMemAllocationData);
}

canonical_original_code:
static int vpsmemAllocationDataSize()
{
     return sizeof(VPSMemAllocationData);
}


decompiled_code:
__int64 __fastcall direct_util_align(int a1, int a2)
{
unsigned int v2; // er8
int v3; // edx
unsigned int v4; // esi

v2 = a1;
v3 = a1 % a2;
v4 = a1 + a2 - a1 % a2;
if ( v3 )
v2 = v4;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
unsigned int v2; // er8
int v3; // edx
unsigned int v4; // esi

v2 = a1;
v3 = a1 % a2;
v4 = a1 + a2 - a1 % a2;
if ( v3 )
v2 = v4;
return v2;
}

original_code:
static inline int
direct_util_align( int value,
                   int alignment )
{
     if (alignment > 1) {
          int tail = value % alignment;
          if (tail)
               value += alignment - tail;
     }
     return value;
}

canonical_original_code:
static inline int direct_util_align(int value, int alignment)
{
     if (alignment > 1) {
          int tail = value % alignment;
          if (tail)
               value += alignment - tail;
     }
     return value;
}

variable_types: value int

variable_types: alignment int

variable_types: tail int


check

repo: AndrewBelt/RMSTalk

decompiled_code:
void *check(char a1, const char *a2, ...)
{
void *result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
result = arg[0].reg_save_area;
if ( !a1 )
{
if ( a2 )
{
vfprintf(stderr, a2, arg);
fputc(10, stderr);
}
check_cold();
}
return result;
}

canonical_decompiled_code:
void *func8(char a1, const char *a2, ...)
{
void *result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
result = arg[0].reg_save_area;
if ( !a1 )
{
if ( a2 )
{
vfprintf(stderr, a2, arg);
fputc(10, stderr);
}
func1();
}
return result;
}

original_code:
void check(_Bool success, const char *err_format, ...)
{
 va_list vl;
 __builtin_va_start(vl,err_format);
 if (!success)
 {
  if (err_format)
  {
   vfprintf(stderr, err_format, vl);
   fprintf(stderr, "\n");
  }
  abort();
 }
 __builtin_va_end(vl);
}

canonical_original_code:
void check(_Bool success, char *err_format, ...)
{
 __builtin_va_list vl;
 __builtin_va_start(vl,err_format);
 if (!success)
 {
  if (err_format)
  {
   vfprintf(stderr, err_format, vl);
   fprintf(stderr, "\n");
  }
  abort();
 }
 __builtin_va_end(vl);
}


main

repo: AndrewBelt/pandemic

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx

v3 = 256;
world_init(argc, argv, envp);
model_init();
do
{
putchar(46);
fflush(stdout);
world_step();
--v3;
}
while ( v3 );
putchar(10);
field_save_png(fields[0], "output/elevation.png", -490.0, 2500.0);
field_save_png(fields[1], "output/prey_population.png", 0.0, 1.0);
field_save_png(fields[2], "output/predator_population.png", 0.0, 1.0);
world_destroy();
return 0;
}

original_code:
int main(int argc, const char **argv)
{
 world_init();
 model_init();
 for (int i = 0; i < 256; i++)
 {
  printf(".");
  fflush(stdout);
  world_step();
 }
 printf("\n");
 field_save_png(fields[ELEVATION],
  "output/elevation.png", -490, 2500);
 field_save_png(fields[PREY_POPULATION],
  "output/prey_population.png", 0.0, 1.0);
 field_save_png(fields[PREDATOR_POPULATION],
  "output/predator_population.png", 0.0, 1.0);
 world_destroy();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 world_init();
 model_init();
 for (int i = 0; i < 256; i++)
 {
  printf(".");
  fflush(stdout);
  world_step();
 }
 printf("\n");
 field_save_png(fields[ELEVATION],
  "output/elevation.png", -490, 2500);
 field_save_png(fields[PREY_POPULATION],
  "output/prey_population.png", 0.0, 1.0);
 field_save_png(fields[PREDATOR_POPULATION],
  "output/predator_population.png", 0.0, 1.0);
 world_destroy();
 return 0;
}

variable_types:
argv
char **


decompiled_code:
float __fastcall field_get(__int64 a1, int a2, int a3)
{
return *(float *)(a1 + 4LL * (a2 + (a3 << 10)));
}

canonical_decompiled_code:
float __fastcall func0(__int64 a1, int a2, int a3)
{
return *(float *)(a1 + 4LL * (a2 + (a3 << 10)));
}

original_code:
static inline float field_get(const float *field, int x, int y)
{
 return field[x + 1024 * y];
}

canonical_original_code:
static inline float field_get(float *field, int x, int y)
{
 return field[x + 1024 * y];
}

variable_types:
field
float *

variable_types: x int

variable_types: y int

return_type: float


decompiled_code:
bool __fastcall land_boundary(unsigned int a1, unsigned int a2)
{
return field_get_safe(fields, a1, a2) > -490.0;
}

canonical_decompiled_code:
bool __fastcall func1(unsigned int a1, unsigned int a2)
{
return field_get_safe(fields, a1, a2) > -490.0;
}

original_code:
_Bool land_boundary(int x, int y)
{
 return field_get_safe(fields[ELEVATION], x, y) > -490.0f;
}


decompiled_code:
float __fastcall prey_callback(unsigned int a1, unsigned int a2)
{
int v2; // edx
__int64 v5; // rsi
__int64 v6; // rdi
double v7; // xmm0_8
float v9; // [rsp+Ch] [rbp-1Ch]

v2 = a2;
v5 = a1;
v6 = fields[1];
v9 = field_get(v6, v5, v2);
v7 = field_laplacian_boundary(v6, v5, a2);
return (float)((float)((float)((float)((float)(v9 + v9) * (float)(1.0 - v9)) + (float)(*(float *)&v7 + *(float *)&v7))
- (float)((float)(4.0 * v9) * field_get(fields[2], a1, a2)))
* 0.1)
+ v9;
}

canonical_decompiled_code:
float __fastcall func2(unsigned int a1, unsigned int a2)
{
int v2; // edx
__int64 v5; // rsi
__int64 v6; // rdi
double v7; // xmm0_8
float v9; // [rsp+Ch] [rbp-1Ch]

v2 = a2;
v5 = a1;
v6 = fields[1];
v9 = func0(v6, v5, v2);
v7 = field_laplacian_boundary(v6, v5, a2);
return (float)((float)((float)((float)((float)(v9 + v9) * (float)(1.0 - v9)) + (float)(*(float *)&v7 + *(float *)&v7))
- (float)((float)(4.0 * v9) * func0(fields[2], a1, a2)))
* 0.1)
+ v9;
}

original_code:
float prey_callback(int x, int y)
{
 static const float difn_alpha = 2.0f;
 static const float growth_rate = 2.0f;
 static const float carrying_capacity = 1.0f;
 static const float predator_beta = 4.0f;
 float u = field_get(fields[PREY_POPULATION], x, y);
 float lapl_u = field_laplacian_boundary(fields[PREY_POPULATION],
  x, y, land_boundary);
 float v = field_get(fields[PREDATOR_POPULATION], x, y);
 float D_u = difn_alpha * lapl_u +
  growth_rate * u * (1.0f - u / carrying_capacity) -
  predator_beta * u * v;
 return u + 0.1f * D_u;
}

canonical_original_code:
float prey_callback(int x, int y)
{
 static float difn_alpha = 2.0f;
 static float growth_rate = 2.0f;
 static float carrying_capacity = 1.0f;
 static float predator_beta = 4.0f;
 float u = field_get(fields[PREY_POPULATION], x, y);
 float lapl_u = field_laplacian_boundary(fields[PREY_POPULATION],
  x, y, land_boundary);
 float v = field_get(fields[PREDATOR_POPULATION], x, y);
 float D_u = difn_alpha * lapl_u +
  growth_rate * u * (1.0f - u / carrying_capacity) -
  predator_beta * u * v;
 return u + 0.1f * D_u;
}

variable_types: u float

variable_types: v float


decompiled_code:
float __fastcall predator_callback(unsigned int a1, unsigned int a2)
{
int v2; // edx
__int64 v5; // rsi
__int64 v6; // rdi
double v7; // xmm0_8
float v9; // [rsp+Ch] [rbp-1Ch]

v2 = a2;
v5 = a1;
v6 = fields[2];
v9 = field_get(v6, v5, v2);
v7 = field_laplacian(v6, v5, a2);
return (float)((float)((float)((float)((float)(-0.1 * v9) * (float)(1.0 - v9)) + *(float *)&v7)
+ (float)(field_get(fields[1], a1, a2) * v9))
* 0.1)
+ v9;
}

canonical_decompiled_code:
float __fastcall func3(unsigned int a1, unsigned int a2)
{
int v2; // edx
__int64 v5; // rsi
__int64 v6; // rdi
double v7; // xmm0_8
float v9; // [rsp+Ch] [rbp-1Ch]

v2 = a2;
v5 = a1;
v6 = fields[2];
v9 = func0(v6, v5, v2);
v7 = field_laplacian(v6, v5, a2);
return (float)((float)((float)((float)((float)(-0.1 * v9) * (float)(1.0 - v9)) + *(float *)&v7)
+ (float)(func0(fields[1], a1, a2) * v9))
* 0.1)
+ v9;
}

original_code:
float predator_callback(int x, int y)
{
 static const float difn_alpha = 1.0f;
 static const float growth_rate = -0.1f;
 static const float carrying_capacity = 1.0f;
 static const float prey_beta = 1.0f;
 float u = field_get(fields[PREDATOR_POPULATION], x, y);
 float lapl_u = field_laplacian(fields[PREDATOR_POPULATION], x, y);
 float v = field_get(fields[PREY_POPULATION], x, y);
 float D_u = difn_alpha * lapl_u +
  growth_rate * u * (1.0f - u / carrying_capacity) +
  prey_beta * u * v;
 return u + 0.1f * D_u;
}

canonical_original_code:
float predator_callback(int x, int y)
{
 static float difn_alpha = 1.0f;
 static float growth_rate = -0.1f;
 static float carrying_capacity = 1.0f;
 static float prey_beta = 1.0f;
 float u = field_get(fields[PREDATOR_POPULATION], x, y);
 float lapl_u = field_laplacian(fields[PREDATOR_POPULATION], x, y);
 float v = field_get(fields[PREY_POPULATION], x, y);
 float D_u = difn_alpha * lapl_u +
  growth_rate * u * (1.0f - u / carrying_capacity) +
  prey_beta * u * v;
 return u + 0.1f * D_u;
}


decompiled_code:
void model_init()
{
int v0; // ebx
unsigned int v1; // eax
int v2; // ebp
int v3; // eax
int v4; // ebp
int v5; // eax

v0 = 20;
field_callbacks = 0LL;
*(&field_callbacks + 1) = (__int64)prey_callback;
*(&field_callbacks + 2) = (__int64)predator_callback;
v1 = time(0LL);
srand(v1);
field_load(fields[0], "fields/elevation.bin");
do
{
v2 = rand();
v3 = rand();
field_set_constprop_0(fields[1], v3 % 1024, v2 % 512);
v4 = rand();
v5 = rand();
field_set_constprop_0(fields[2], v5 % 1024, v4 % 512);
--v0;
}
while ( v0 );
}

canonical_decompiled_code:
void func5()
{
int v0; // ebx
unsigned int v1; // eax
int v2; // ebp
int v3; // eax
int v4; // ebp
int v5; // eax

v0 = 20;
field_callbacks = 0LL;
*(&field_callbacks + 1) = (__int64)prey_callback;
*(&field_callbacks + 2) = (__int64)predator_callback;
v1 = time(0LL);
srand(v1);
field_load(fields[0], "fields/elevation.bin");
do
{
v2 = rand();
v3 = rand();
func4(fields[1], v3 % 1024, v2 % 512);
v4 = rand();
v5 = rand();
func4(fields[2], v5 % 1024, v4 % 512);
--v0;
}
while ( v0 );
}

original_code:
void model_init()
{
 field_callbacks[ELEVATION] = ((void *)0);
 field_callbacks[PREY_POPULATION] = prey_callback;
 field_callbacks[PREDATOR_POPULATION] = predator_callback;
 srand(time(((void *)0)));
 field_load(fields[ELEVATION], "fields/elevation.bin");
 for (int i = 0; i < 20; i++)
 {
  field_set(fields[PREY_POPULATION],
   rand() % 1024, rand() % 512, 1.0f);
  field_set(fields[PREDATOR_POPULATION],
   rand() % 1024, rand() % 512, 1.0f);
 }
}


decompiled_code:
void __fastcall field_set(__int64 a1, int a2, int a3, float a4)
{
*(float *)(a1 + 4LL * (a2 + (a3 << 10))) = a4;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, int a2, int a3, float a4)
{
*(float *)(a1 + 4LL * (a2 + (a3 << 10))) = a4;
}

original_code:
static inline void field_set(float *field, int x, int y, float value)
{
 field[x + 1024 * y] = value;
}

variable_types: value float


decompiled_code:
void *field_create()
{
return calloc(0x80000uLL, 4uLL);
}

canonical_decompiled_code:
void *func2()
{
return calloc(0x80000uLL, 4uLL);
}

original_code:
float *field_create()
{
 return calloc(1024 * 512, sizeof(float));
}

return_type: float *


decompiled_code:
// attributes: thunk
void field_destroy(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func5(void *ptr)
{
free(ptr);
}

original_code:
void field_destroy(float *field)
{
 free(field);
}


decompiled_code:
float __fastcall field_get_safe(__int64 a1, __int16 a2, __int16 a3)
{
return field_get(a1, a2 & 0x3FF, a3 & 0x1FF);
}

canonical_decompiled_code:
float __fastcall func6(__int64 a1, __int16 a2, __int16 a3)
{
return func0(a1, a2 & 0x3FF, a3 & 0x1FF);
}

original_code:
float field_get_safe(const float *field, int x, int y)
{
 x &= 1024 - 1;
 y &= 512 - 1;
 return field_get(field, x, y);
}

canonical_original_code:
float field_get_safe(float *field, int x, int y)
{
 x &= 1024 - 1;
 y &= 512 - 1;
 return field_get(field, x, y);
}


decompiled_code:
void __fastcall field_set_safe(__int64 a1, __int16 a2, __int16 a3, float a4)
{
field_set(a1, a2 & 0x3FF, a3 & 0x1FF, a4);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, __int16 a2, __int16 a3, float a4)
{
func1(a1, a2 & 0x3FF, a3 & 0x1FF, a4);
}

original_code:
void field_set_safe(float *field, int x, int y, float value)
{
 x &= 1024 - 1;
 y &= 512 - 1;
 field_set(field, x, y, value);
}