decompiled_code:
__int64 __fastcall init_map(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // er12
__int64 result; // rax
int v5; // ecx
char v6; // r10
char v7; // r9

*(_BYTE *)(a1 + 9) = a2;
*(_BYTE *)(a1 + 8) = a3;
v3 = a3 * a3;
result = (__int64)malloc(6LL * a3 * a2);
*(_QWORD *)a1 = result;
if ( v3 )
{
v5 = 0;
do
{
result = get_tile(a1, v5 % a2, v5 / a2);
*(_BYTE *)result = v6;
*(_BYTE *)(result + 1) = v7;
*(_DWORD *)(result + 2) = 0x10000;
}
while ( v5 != v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // er12
__int64 result; // rax
int v5; // ecx
char v6; // r10
char v7; // r9

*(_BYTE *)(a1 + 9) = a2;
*(_BYTE *)(a1 + 8) = a3;
v3 = a3 * a3;
result = (__int64)malloc(6LL * a3 * a2);
*(_QWORD *)a1 = result;
if ( v3 )
{
v5 = 0;
do
{
result = func8(a1, v5 % a2, v5 / a2);
*(_BYTE *)result = v6;
*(_BYTE *)(result + 1) = v7;
*(_DWORD *)(result + 2) = 0x10000;
}
while ( v5 != v3 );
}
return result;
}

original_code:
void init_map(struct Map *map, uint8_t width, uint8_t height) {
 map->width = width;
 map->height = height;
 map->tiles = malloc((map->width * map->height) * sizeof(struct Tile));
 struct Tile *currentTile;
 for (int i = 0; i < map->height * map->height; i++) {
  currentTile = get_tile(map, i % map->width, i / map->width);
  currentTile->x = i % map->width;
  currentTile->y = i / map->width;
  currentTile->type = BLANK;
  currentTile->direction = UP;
 }
}

canonical_original_code:
void init_map(struct Map *map, unsigned char width, unsigned char height) {
 map->width = width;
 map->height = height;
 map->tiles = malloc((map->width * map->height) * sizeof(struct Tile));
 struct Tile *currentTile;
 for (int i = 0; i < map->height * map->height; i++) {
  currentTile = get_tile(map, i % map->width, i / map->width);
  currentTile->x = i % map->width;
  currentTile->y = i / map->width;
  currentTile->type = BLANK;
  currentTile->direction = UP;
 }
}

variable_types:
width
unsigned char

variable_types:
height
unsigned char

variable_types: struct Tile { unsigned char x; unsigned char y; short type; short direction; } *


decompiled_code:
__int64 __fastcall manhattan_tile_distance(unsigned __int8 *a1, unsigned __int8 *a2)
{
int v2; // er8
int v3; // eax
unsigned __int8 v4; // di
int v5; // er9
int v6; // eax
int v7; // edx
int v8; // er8
int v9; // edx

v2 = *a1;
v3 = *a2;
v4 = a1[1];
v5 = v2 - v3;
v6 = v3 - v2;
v7 = a2[1];
if ( (unsigned __int8)v2 > *a2 )
v6 = v5;
v8 = v4 - v7;
v9 = v7 - v4;
if ( v4 > a2[1] )
v9 = v8;
return (unsigned int)(v9 + v6);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 *a1, unsigned __int8 *a2)
{
int v2; // er8
int v3; // eax
unsigned __int8 v4; // di
int v5; // er9
int v6; // eax
int v7; // edx
int v8; // er8
int v9; // edx

v2 = *a1;
v3 = *a2;
v4 = a1[1];
v5 = v2 - v3;
v6 = v3 - v2;
v7 = a2[1];
if ( (unsigned __int8)v2 > *a2 )
v6 = v5;
v8 = v4 - v7;
v9 = v7 - v4;
if ( v4 > a2[1] )
v9 = v8;
return (unsigned int)(v9 + v6);
}

original_code:
int manhattan_tile_distance(struct Tile* tileA, struct Tile* tileB) {
 int xDistance = tileA->x > tileB->x ? tileA->x - tileB->x : tileB->x - tileA->x;
 int yDistance = tileA->y > tileB->y ? tileA->y - tileB->y : tileB->y - tileA->y;
 return xDistance + yDistance;
}

canonical_original_code:
int manhattan_tile_distance(struct Tile *tileA, struct Tile *tileB) {
 int xDistance = tileA->x > tileB->x ? tileA->x - tileB->x : tileB->x - tileA->x;
 int yDistance = tileA->y > tileB->y ? tileA->y - tileB->y : tileB->y - tileA->y;
 return xDistance + yDistance;
}

variable_types:
tileB
struct Tile *

variable_types: xDistance int

variable_types: yDistance int

return_type: int

user_defined_types: struct Tile { unsigned char x; unsigned char y; short type; short direction; }


decompiled_code:
__int64 __fastcall generate_random_end_point(__int64 a1, unsigned __int8 *a2)
{
int v2; // er12
int v3; // eax
int v4; // er10
int v5; // ecx
int v6; // eax
__int64 result; // rax

LABEL_1:
v2 = (unsigned __int8)(rand() % (*(unsigned __int8 *)(a1 + 9) - 1));
v3 = rand();
v4 = a2[1];
v5 = (unsigned __int8)(v3 % (*(unsigned __int8 *)(a1 + 8) - 1));
do
{
if ( v5 >= v4 - 2 )
{
v6 = *a2;
if ( v6 - 2 <= v2 && v5 <= v4 + 2 && v6 + 2 >= v2 )
goto LABEL_1;
}
result = get_tile(a1, v2, v5);
}
while ( *(_WORD *)(result + 2) );
*(_DWORD *)(result + 2) = 65542;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned __int8 *a2)
{
int v2; // er12
int v3; // eax
int v4; // er10
int v5; // ecx
int v6; // eax
__int64 result; // rax

LABEL_1:
v2 = (unsigned __int8)(rand() % (*(unsigned __int8 *)(a1 + 9) - 1));
v3 = rand();
v4 = a2[1];
v5 = (unsigned __int8)(v3 % (*(unsigned __int8 *)(a1 + 8) - 1));
do
{
if ( v5 >= v4 - 2 )
{
v6 = *a2;
if ( v6 - 2 <= v2 && v5 <= v4 + 2 && v6 + 2 >= v2 )
goto LABEL_1;
}
result = func8(a1, v2, v5);
}
while ( *(_WORD *)(result + 2) );
*(_DWORD *)(result + 2) = 65542;
return result;
}

original_code:
void generate_random_end_point(struct Map *map, struct Tile* start) {
 uint8_t randX = rand() % (map->width - 1);
 uint8_t randY = rand() % (map->height - 1);
 struct Tile* endTile;
 do {
  while (!(randY < start->y - 2 || randX < start->x - 2 || randY > start->y + 2 || randX > start->x + 2)) {
   randX = rand() % (map->width - 1);
   randY = rand() % (map->height - 1);
  }
  endTile = get_tile(map, randX, randY);
 } while (endTile->type != BLANK);
 endTile->type = END;
 endTile->direction = UP;
}

canonical_original_code:
void generate_random_end_point(struct Map *map, struct Tile *start) {
 unsigned char randX = rand() % (map->width - 1);
 unsigned char randY = rand() % (map->height - 1);
 struct Tile *endTile;
 do {
  while (!(randY < start->y - 2 || randX < start->x - 2 || randY > start->y + 2 || randX > start->x + 2)) {
   randX = rand() % (map->width - 1);
   randY = rand() % (map->height - 1);
  }
  endTile = get_tile(map, randX, randY);
 } while (endTile->type != BLANK);
 endTile->type = END;
 endTile->direction = UP;
}

variable_types: struct Map { struct Tile { unsigned char x; unsigned char y; short type; short direction; } *tiles; unsigned char height; } *

variable_types:
start
struct Tile *

variable_types:
randX
unsigned char

variable_types:
randY
unsigned char

return_type: void

user_defined_types: struct Map { struct Tile *tiles; unsigned char height; }


decompiled_code:
__int64 __fastcall get_possible_successor_orientations(__int64 a1, int a2)
{
__int16 v2; // dx
__int64 result; // rax

v2 = *(_WORD *)(a1 + 2);
if ( v2 == 3 )
{
result = 10LL;
if ( a2 != 1 )
{
result = 5LL;
if ( a2 != 2 )
{
result = 6LL;
if ( a2 != 8 )
result = 9LL;
}
}
}
else if ( v2 == 4 )
{
result = 14LL;
if ( a2 != 1 )
{
result = 13LL;
if ( a2 != 2 )
{
result = 7LL;
if ( a2 != 8 )
result = 11LL;
}
}
}
else
{
result = 1LL;
if ( v2 == 2 )
result = ((a2 - 4) & 0xFFFFFFFB) == 0 ? 8 : 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2)
{
__int16 v2; // dx
__int64 result; // rax

v2 = *(_WORD *)(a1 + 2);
if ( v2 == 3 )
{
result = 10LL;
if ( a2 != 1 )
{
result = 5LL;
if ( a2 != 2 )
{
result = 6LL;
if ( a2 != 8 )
result = 9LL;
}
}
}
else if ( v2 == 4 )
{
result = 14LL;
if ( a2 != 1 )
{
result = 13LL;
if ( a2 != 2 )
{
result = 7LL;
if ( a2 != 8 )
result = 11LL;
}
}
}
else
{
result = 1LL;
if ( v2 == 2 )
result = ((a2 - 4) & 0xFFFFFFFB) == 0 ? 8 : 1;
}
return result;
}

original_code:
int get_possible_successor_orientations(struct Tile *succ, int coming_from_direction) {
 switch (succ->type) {
  case PIPE_STRAIGHT:
   if (coming_from_direction == LEFT || coming_from_direction == RIGHT) {
    return RIGHT;
   } else {
    return UP;
   }
  break;
  case PIPE_TURN:
   if (coming_from_direction == UP) {
    return RIGHT | DOWN;
   } else if (coming_from_direction == DOWN) {
    return LEFT | UP;
   } else if (coming_from_direction == RIGHT) {
    return LEFT | DOWN;
   } else if (coming_from_direction == LEFT) {
    return RIGHT | UP;
   }
  case PIPE_INTERSECT:
   if (coming_from_direction == UP) {
    return RIGHT | LEFT | DOWN;
   } else if (coming_from_direction == DOWN) {
    return RIGHT | LEFT | UP;
   } else if (coming_from_direction == RIGHT) {
    return DOWN | LEFT | UP;
   } else if (coming_from_direction == LEFT) {
    return DOWN | RIGHT | UP;
   }
  break;
  default:
   return UP;
 }
}


decompiled_code:
void get_successors()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void get_successors(struct Map *map, struct TileGroup *group, struct Tile *tile) {
 switch (tile->type) {
  case START:
  case PIPE_CROSS:
   if (tile->x - 1 >= 0) {
   }
   if (tile->x + 1 < map->width) {
   }
   if (tile->y - 1 >= 0) {
   }
   if (tile->y + 1 < map->height) {
   }
  break;
  case PIPE_STRAIGHT:
  break;
  case PIPE_TURN:
  break;
  case PIPE_INTERSECT:
  break;
 }
}

variable_types: struct TileGroup { struct Tile *tile; struct TileGroup *nextTile; } *

variable_types:
tile
struct Tile *


decompiled_code:
__int64 __fastcall tile_is_in_group(__int64 *a1, _WORD *a2)
{
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
result = *a1;
while ( result )
{
if ( *(_WORD *)result == *a2 && *(_WORD *)(result + 4) == a2[2] )
return 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 *a1, _WORD *a2)
{
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
result = *a1;
while ( result )
{
if ( *(_WORD *)result == *a2 && *(_WORD *)(result + 4) == a2[2] )
return 1LL;
}
return result;
}

original_code:
int tile_is_in_group(struct TileGroup *path, struct Tile *tile) {
 if (tile == ((void *)0)) return -1;
 struct Tile *currentTile;
 while (currentTile = path->tile) {
  if (currentTile->x == tile->x && currentTile->y == tile->y && currentTile->direction == tile->direction) {
   return 1;
  }
 }
 return 0;
}

variable_types: struct TileGroup { struct Tile { unsigned char x; unsigned char y; short type; short direction; } *tile; struct TileGroup *nextTile; } *

user_defined_types: struct TileGroup { struct Tile *tile; struct TileGroup *nextTile; }


decompiled_code:
void __fastcall free_tile_group(void *ptr, int a2)
{
void **v3; // rbp

do
{
while ( 1 )
{
v3 = (void **)ptr;
ptr = (void *)*((_QWORD *)ptr + 1);
if ( a2 )
break;
free(v3);
if ( !ptr )
return;
}
free(*v3);
free(v3);
}
while ( ptr );
}

canonical_decompiled_code:
void __fastcall func15(void *ptr, int a2)
{
void **v3; // rbp

do
{
while ( 1 )
{
v3 = (void **)ptr;
ptr = (void *)*((_QWORD *)ptr + 1);
if ( a2 )
break;
free(v3);
if ( !ptr )
return;
}
free(*v3);
free(v3);
}
while ( ptr );
}

original_code:
void free_tile_group(struct TileGroup *group, int should_free_tiles) {
 struct TileGroup *nextGroup;
 do {
  nextGroup = group->nextTile;
  if (should_free_tiles) {
   free(group->tile);
  }
  free(group);
 } while (group = nextGroup);
}


decompiled_code:
__int64 __fastcall generate_map(__int64 a1, int a2)
{
int v2; // ebx
int v3; // eax
__int64 result; // rax
int v5; // er9
unsigned __int8 *v6; // rbp
int v7; // ebx
int v9; // ecx
int v10; // ebx
__int64 v11; // rbp
unsigned int v12; // eax

v2 = rand() % (*(unsigned __int8 *)(a1 + 9) - 1);
v3 = rand();
result = get_tile(a1, v2, v3 % (*(unsigned __int8 *)(a1 + 8) - 1));
*(_DWORD *)(result + 2) = 65537;
if ( a2 > 0 )
{
v6 = (unsigned __int8 *)result;
v7 = a2 - 1;
do
result = generate_random_end_point(a1, v6);
while ( v7-- != 0 );
v5 = *(unsigned __int8 *)(a1 + 8);
}
v9 = *(unsigned __int8 *)(a1 + 9);
v10 = 0;
if ( v9 * v5 )
{
do
{
while ( 1 )
{
v11 = get_tile(a1, v10 % v9, v10 / v9);
if ( !*(_WORD *)(v11 + 2) )
break;
++v10;
result = v9 * (unsigned int)*(unsigned __int8 *)(a1 + 8);
if ( (int)result <= v10 )
return result;
}
v12 = rand() % 9;
if ( v12 > 2 )
{
if ( v12 - 3 > 2 )
{
if ( v12 - 6 <= 1 )
{
*(_WORD *)(v11 + 2) = 4;
}
else if ( v12 == 8 )
{
*(_WORD *)(v11 + 2) = 5;
}
}
else
{
*(_WORD *)(v11 + 2) = 3;
}
}
else
{
*(_WORD *)(v11 + 2) = 2;
}
++v10;
*(_WORD *)(v11 + 4) = 1 << (rand() % 4);
v9 = *(unsigned __int8 *)(a1 + 9);
result = v9 * (unsigned int)*(unsigned __int8 *)(a1 + 8);
}
while ( (int)result > v10 );
}
return result;
}

original_code:
void generate_map(struct Map *map, int points) {
 unsigned short randX, randY;
 randX = rand() % (map->width - 1);
 randY = rand() % (map->height - 1);
 struct Tile* start = get_tile(map, randX, randY);
 start->type = START;
 start->direction = UP;
 while (points-- > 0) {
  generate_random_end_point(map, start);
 }
 uint8_t currentX, currentY;
 struct Tile* currentTilePointer;
 for (int i = 0; i < map->width * map->height; i++) {
  currentX = i % map->width, currentY = i / map->width;
  currentTilePointer = get_tile(map, currentX, currentY);
  if (currentTilePointer->type == BLANK) {
   int tileChoice = rand() % 9;
   if (tileChoice >= 0 && tileChoice <= 2) {
    currentTilePointer->type = PIPE_STRAIGHT;
   } else if (tileChoice >= 3 && tileChoice <= 5) {
    currentTilePointer->type = PIPE_TURN;
   } else if (tileChoice == 6 || tileChoice == 7) {
    currentTilePointer->type = PIPE_INTERSECT;
   } else if (tileChoice == 8) {
    currentTilePointer->type = PIPE_CROSS;
   }
   currentTilePointer->direction = 1 << rand() % 4;
  }
 }
}

canonical_original_code:
void generate_map(struct Map *map, int points) {
 unsigned short randX; unsigned short randY;
 randX = rand() % (map->width - 1);
 randY = rand() % (map->height - 1);
 struct Tile *start = get_tile(map, randX, randY);
 start->type = START;
 start->direction = UP;
 while (points-- > 0) {
  generate_random_end_point(map, start);
 }
 unsigned char currentX; unsigned char currentY;
 struct Tile *currentTilePointer;
 for (int i = 0; i < map->width * map->height; i++) {
  currentX = i % map->width, currentY = i / map->width;
  currentTilePointer = get_tile(map, currentX, currentY);
  if (currentTilePointer->type == BLANK) {
   int tileChoice = rand() % 9;
   if (tileChoice >= 0 && tileChoice <= 2) {
    currentTilePointer->type = PIPE_STRAIGHT;
   } else if (tileChoice >= 3 && tileChoice <= 5) {
    currentTilePointer->type = PIPE_TURN;
   } else if (tileChoice == 6 || tileChoice == 7) {
    currentTilePointer->type = PIPE_INTERSECT;
   } else if (tileChoice == 8) {
    currentTilePointer->type = PIPE_CROSS;
   }
   currentTilePointer->direction = 1 << rand() % 4;
  }
 }
}

variable_types: points int

variable_types:
randX
unsigned short

variable_types:
randY
unsigned short

variable_types:
currentX
unsigned char

variable_types:
currentY
unsigned char

variable_types: i int

variable_types: tileChoice int


decompiled_code:
__int64 free_images()
{
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
return SDL_FreeSurface();
}

canonical_decompiled_code:
__int64 func18()
{
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
return SDL_FreeSurface();
}

original_code:
void free_images() {
 SDL_FreeSurface(tileVertical);
 SDL_FreeSurface(tileHorizontal);
 SDL_FreeSurface(tileStart);
 SDL_FreeSurface(tileEnd);
 SDL_FreeSurface(tileCross);
 SDL_FreeSurface(tileIntersectUp);
 SDL_FreeSurface(tileIntersectDown);
 SDL_FreeSurface(tileIntersectLeft);
 SDL_FreeSurface(tileIntersectRight);
 SDL_FreeSurface(tileTurnDownRight);
 SDL_FreeSurface(tileTurnDownLeft);
 SDL_FreeSurface(tileTurnUpLeft);
 SDL_FreeSurface(tileTurnUpRight);
}


decompiled_code:
__int64 image_draw_test()
{
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
return SDL_UpperBlit();
}

canonical_decompiled_code:
__int64 func19()
{
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
return SDL_UpperBlit();
}

original_code:
void image_draw_test() {
 {
  SDL_Rect tmprect = {0, 0, 20, 20};
  SDL_UpperBlit(tileHorizontal, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {20, 0, 20, 20};
  SDL_UpperBlit(tileVertical, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {40, 0, 20, 20};
  SDL_UpperBlit(tileStart, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {60, 0, 20, 20};
  SDL_UpperBlit(tileEnd, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {80, 0, 20, 20};
  SDL_UpperBlit(tileCross, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {100, 0, 20, 20};
  SDL_UpperBlit(tileIntersectUp, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {0, 20, 20, 20};
  SDL_UpperBlit(tileIntersectRight, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {20, 20, 20, 20};
  SDL_UpperBlit(tileIntersectDown, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {40, 20, 20, 20};
  SDL_UpperBlit(tileIntersectLeft, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {60, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownRight, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {80, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownLeft, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {100, 20, 20, 20};
  SDL_UpperBlit(tileTurnUpLeft, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {0, 40, 20, 20};
  SDL_UpperBlit(tileTurnUpRight, ((void *)0), screen, &tmprect);
 }
}

canonical_original_code:
void image_draw_test() {
 {
  struct SDL_Rect tmprect = {0, 0, 20, 20};
  SDL_UpperBlit(tileHorizontal, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {20, 0, 20, 20};
  SDL_UpperBlit(tileVertical, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {40, 0, 20, 20};
  SDL_UpperBlit(tileStart, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {60, 0, 20, 20};
  SDL_UpperBlit(tileEnd, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {80, 0, 20, 20};
  SDL_UpperBlit(tileCross, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {100, 0, 20, 20};
  SDL_UpperBlit(tileIntersectUp, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {0, 20, 20, 20};
  SDL_UpperBlit(tileIntersectRight, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {20, 20, 20, 20};
  SDL_UpperBlit(tileIntersectDown, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {40, 20, 20, 20};
  SDL_UpperBlit(tileIntersectLeft, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {60, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownRight, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {80, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownLeft, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {100, 20, 20, 20};
  SDL_UpperBlit(tileTurnUpLeft, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {0, 40, 20, 20};
  SDL_UpperBlit(tileTurnUpRight, ((void *)0), screen, &tmprect);
 }
}

variable_types: struct SDL_Rect { short x; unsigned short w; }


decompiled_code:
__int64 __fastcall draw_tile(__int64 a1)
{
__int16 v1; // dx
__int64 result; // rax

v1 = *(_WORD *)(a1 + 4);
result = *(unsigned __int16 *)(a1 + 2);
switch ( *(_WORD *)(a1 + 2) )
{
case 2:
if ( (v1 & 0xC) != 0 || (v1 & 3) != 0 )
goto LABEL_16;
break;
case 3:
if ( (v1 & 8) != 0 || (v1 & 1) != 0 || (v1 & 4) != 0 || (v1 & 2) != 0 )
goto LABEL_16;
break;
case 4:
if ( (v1 & 8) != 0 || (v1 & 1) != 0 || (v1 & 4) != 0 || (v1 & 2) != 0 )
goto LABEL_16;
break;
case 6:
result = SDL_UpperBlit();
break;
default:
LABEL_16:
result = SDL_UpperBlit();
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1)
{
__int16 v1; // dx
__int64 result; // rax

v1 = *(_WORD *)(a1 + 4);
result = *(unsigned __int16 *)(a1 + 2);
switch ( *(_WORD *)(a1 + 2) )
{
case 2:
if ( (v1 & 0xC) != 0 || (v1 & 3) != 0 )
goto LABEL_16;
break;
case 3:
if ( (v1 & 8) != 0 || (v1 & 1) != 0 || (v1 & 4) != 0 || (v1 & 2) != 0 )
goto LABEL_16;
break;
case 4:
if ( (v1 & 8) != 0 || (v1 & 1) != 0 || (v1 & 4) != 0 || (v1 & 2) != 0 )
goto LABEL_16;
break;
case 6:
result = SDL_UpperBlit();
break;
default:
LABEL_16:
result = SDL_UpperBlit();
break;
}
return result;
}

original_code:
void draw_tile(struct Tile* tile) {
 SDL_Rect tmprect = {tile->x*20, tile->y*20, 20, 20};
 int dir = tile->direction;
 switch (tile->type) {
  case START:
   SDL_UpperBlit(tileStart, ((void *)0), screen, &tmprect);
  break;
  case END:
   SDL_UpperBlit(tileEnd, ((void *)0), screen, &tmprect);
  break;
  case PIPE_STRAIGHT:
   if (dir & (RIGHT | LEFT)) {
    SDL_UpperBlit(tileHorizontal, ((void *)0), screen, &tmprect);
   } else if (dir & (UP | DOWN)) {
    SDL_UpperBlit(tileVertical, ((void *)0), screen, &tmprect);
   }
  break;
  case PIPE_TURN:
   if (dir & RIGHT) {
    SDL_UpperBlit(tileTurnDownRight, ((void *)0), screen, &tmprect);
   } else if (dir & UP) {
    SDL_UpperBlit(tileTurnUpRight, ((void *)0), screen, &tmprect);
   } else if (dir & LEFT) {
    SDL_UpperBlit(tileTurnUpLeft, ((void *)0), screen, &tmprect);
   } else if (dir & DOWN) {
    SDL_UpperBlit(tileTurnDownLeft, ((void *)0), screen, &tmprect);
   }
  break;
  case PIPE_CROSS:
   SDL_UpperBlit(tileCross, ((void *)0), screen, &tmprect);
  break;
  case PIPE_INTERSECT:
   if (dir & RIGHT) {
    SDL_UpperBlit(tileIntersectRight, ((void *)0), screen, &tmprect);
   } else if (dir & UP) {
    SDL_UpperBlit(tileIntersectUp, ((void *)0), screen, &tmprect);
   } else if (dir & LEFT) {
    SDL_UpperBlit(tileIntersectLeft, ((void *)0), screen, &tmprect);
   } else if (dir & DOWN) {
    SDL_UpperBlit(tileIntersectDown, ((void *)0), screen, &tmprect);
   }
  break;
  default:
   SDL_UpperBlit(tileBlank, ((void *)0), screen, &tmprect);
  break;
 }
}

canonical_original_code:
void draw_tile(struct Tile *tile) {
 struct SDL_Rect tmprect = {tile->x*20, tile->y*20, 20, 20};
 int dir = tile->direction;
 switch (tile->type) {
  case START:
   SDL_UpperBlit(tileStart, ((void *)0), screen, &tmprect);
  break;
  case END:
   SDL_UpperBlit(tileEnd, ((void *)0), screen, &tmprect);
  break;
  case PIPE_STRAIGHT:
   if (dir & (RIGHT | LEFT)) {
    SDL_UpperBlit(tileHorizontal, ((void *)0), screen, &tmprect);
   } else if (dir & (UP | DOWN)) {
    SDL_UpperBlit(tileVertical, ((void *)0), screen, &tmprect);
   }
  break;
  case PIPE_TURN:
   if (dir & RIGHT) {
    SDL_UpperBlit(tileTurnDownRight, ((void *)0), screen, &tmprect);
   } else if (dir & UP) {
    SDL_UpperBlit(tileTurnUpRight, ((void *)0), screen, &tmprect);
   } else if (dir & LEFT) {
    SDL_UpperBlit(tileTurnUpLeft, ((void *)0), screen, &tmprect);
   } else if (dir & DOWN) {
    SDL_UpperBlit(tileTurnDownLeft, ((void *)0), screen, &tmprect);
   }
  break;
  case PIPE_CROSS:
   SDL_UpperBlit(tileCross, ((void *)0), screen, &tmprect);
  break;
  case PIPE_INTERSECT:
   if (dir & RIGHT) {
    SDL_UpperBlit(tileIntersectRight, ((void *)0), screen, &tmprect);
   } else if (dir & UP) {
    SDL_UpperBlit(tileIntersectUp, ((void *)0), screen, &tmprect);
   } else if (dir & LEFT) {
    SDL_UpperBlit(tileIntersectLeft, ((void *)0), screen, &tmprect);
   } else if (dir & DOWN) {
    SDL_UpperBlit(tileIntersectDown, ((void *)0), screen, &tmprect);
   }
  break;
  default:
   SDL_UpperBlit(tileBlank, ((void *)0), screen, &tmprect);
  break;
 }
}

variable_types: struct Tile { unsigned char x; unsigned char y; short type; short direction; } *

variable_types: dir int

user_defined_types: struct SDL_Rect { short x; unsigned short w; }


repo: elionne/lutopia

original_code:
dmx_handler dmx_open(struct dmx_controler* dmx, unsigned char* name)
{return dmx->dmx_open(name);}

canonical_original_code:
void *dmx_open(struct dmx_controler *dmx, unsigned char *name)
{return dmx->dmx_open(name);}

variable_types: struct dmx_controler { char *name; void *(*dmx_open)(unsigned char *); int (*dmx_send)(void *, unsigned char *); int (*dmx_close)(void *); } *

variable_types:
name
unsigned char *

return_type: void *


original_code:
int dmx_send(struct dmx_controler *dmx, dmx_handler handler, unsigned char *data)
{return dmx->dmx_send(handler, data);}

canonical_original_code:
int dmx_send(struct dmx_controler *dmx, void *handler, unsigned char *data)
{return dmx->dmx_send(handler, data);}

variable_types:
handler
void *

variable_types:
data
unsigned char *

user_defined_types: struct dmx_controler { char *name; void *(*dmx_open)(unsigned char *); int (*dmx_send)(void *, unsigned char *); int (*dmx_close)(void *); }


original_code:
int dmx_close(struct dmx_controler *dmx, dmx_handler handler)
{return dmx->dmx_close(handler);}

canonical_original_code:
int dmx_close(struct dmx_controler *dmx, void *handler)
{return dmx->dmx_close(handler);}


decompiled_code:
__int64 wake_up_task()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func11()
{
return 0LL;
}

original_code:
int wake_up_task(char* function)
{
    return 0;
}

canonical_original_code:
int wake_up_task(char *function)
{
    return 0;
}

variable_types:
function
char *


gcd

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

v2 = a1;
v3 = a2;
if ( !a1 )
return a2;
while ( 1 )
{
v4 = v3 % v2;
v3 = v2;
if ( !v4 )
break;
v2 = v4;
}
return (unsigned int)v2;
}

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

v2 = a1;
v3 = a2;
if ( !a1 )
return a2;
while ( 1 )
{
v4 = v3 % v2;
v3 = v2;
if ( !v4 )
break;
v2 = v4;
}
return (unsigned int)v2;
}

original_code:
int gcd(int a, int b) {
    while(a != 0) {
        int c = a;
        a = b % a;
        b = c;
    }
    return b;
}

variable_types: a int

variable_types: b int

variable_types: c int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er15
socklen_t v5; // edx
int v6; // er13
__int64 v7; // r14
int v8; // er12
unsigned int v9; // eax
int *v10; // rax
char *v11; // rax
unsigned int v13; // er12
unsigned int v14; // ebx
__time_t v15; // rsi
__suseconds_t v16; // rdx
__time_t v17; // rcx
bool v18; // sf
__suseconds_t v19; // rax
int *v20; // rax
char *v21; // rax
__int64 v22; // [rsp+0h] [rbp-328h]
__int64 v23; // [rsp+8h] [rbp-320h]
struct sockaddr v24; // [rsp+10h] [rbp-318h] BYREF
struct timeval tv; // [rsp+20h] [rbp-308h] BYREF
struct timeval v26; // [rsp+30h] [rbp-2F8h] BYREF
struct timeval timeout; // [rsp+40h] [rbp-2E8h] BYREF
struct timeval v28; // [rsp+50h] [rbp-2D8h] BYREF
fd_set readfds; // [rsp+60h] [rbp-2C8h] BYREF
char buf[584]; // [rsp+E0h] [rbp-248h] BYREF

v3 = socket(2, 2, 0);
if ( v3 <= 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
printf("Error while opening socket, abort. %s\n", v11);
}
else
{
v4 = v3;
*(_DWORD *)&v24.sa_data[2] = 0;
*(_DWORD *)v24.sa_data = (unsigned __int16)_bswap_16_constprop_0(2LL, &v24, 16LL);
v24.sa_family = 2;
if ( bind(v4, &v24, v5) < 0 )
{
v20 = __errno_location();
v21 = strerror(*v20);
printf("Could not bind the socket. %s\n", v21);
}
else
{
v23 = dmx_open(dmx_drv_list, 0LL);
if ( v23 )
{
v6 = v4 + 1;
v22 = 1LL << v4;
v7 = v4 >> 6;
while ( 1 )
{
timeout.tv_sec = 0LL;
timeout.tv_usec = 24000LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v22;
gettimeofday(&tv, 0LL);
v8 = select(v6, &readfds, 0LL, 0LL, &timeout);
gettimeofday(&v26, 0LL);
if ( v8 > 0 )
{
v28.tv_sec = 0LL;
v28.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v22;
v13 = 0;
do
{
++v13;
v14 = recv(v4, buf, 0x201uLL, 0);
}
while ( select(v6, &readfds, 0LL, 0LL, &v28) > 0 );
if ( v14 <= 0x200 )
puts("corrupted data receive");
if ( v13 != 1 )
printf("multiple data received %i\n", v13);
v15 = v26.tv_sec - tv.tv_sec;
v16 = v26.tv_usec - tv.tv_usec;
if ( v26.tv_usec - tv.tv_usec < 0 )
{
--v15;
v16 += 1000000LL;
}
v17 = timeout.tv_sec - v15;
v19 = timeout.tv_usec - v16;
v18 = timeout.tv_usec - v16 < 0;
timeout.tv_sec -= v15;
timeout.tv_usec -= v16;
if ( v18 )
{
timeout.tv_sec = v17 - 1;
timeout.tv_usec = v19 + 1000000;
}
}
buf[0] = 0;
v9 = dmx_send(dmx_drv_list, v23, buf);
if ( v9 != 513 )
printf("send %i bytes to dmx\r", v9);
}
}
}
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er15
socklen_t v5; // edx
int v6; // er13
__int64 v7; // r14
int v8; // er12
unsigned int v9; // eax
int *v10; // rax
char *v11; // rax
unsigned int v13; // er12
unsigned int v14; // ebx
__time_t v15; // rsi
__suseconds_t v16; // rdx
__time_t v17; // rcx
bool v18; // sf
__suseconds_t v19; // rax
int *v20; // rax
char *v21; // rax
__int64 v22; // [rsp+0h] [rbp-328h]
__int64 v23; // [rsp+8h] [rbp-320h]
struct sockaddr v24; // [rsp+10h] [rbp-318h] BYREF
struct timeval tv; // [rsp+20h] [rbp-308h] BYREF
struct timeval v26; // [rsp+30h] [rbp-2F8h] BYREF
struct timeval timeout; // [rsp+40h] [rbp-2E8h] BYREF
struct timeval v28; // [rsp+50h] [rbp-2D8h] BYREF
fd_set readfds; // [rsp+60h] [rbp-2C8h] BYREF
char buf[584]; // [rsp+E0h] [rbp-248h] BYREF

v3 = socket(2, 2, 0);
if ( v3 <= 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
printf("Error while opening socket, abort. %s\n", v11);
}
else
{
v4 = v3;
*(_DWORD *)&v24.sa_data[2] = 0;
*(_DWORD *)v24.sa_data = (unsigned __int16)func7(2LL, &v24, 16LL);
v24.sa_family = 2;
if ( bind(v4, &v24, v5) < 0 )
{
v20 = __errno_location();
v21 = strerror(*v20);
printf("Could not bind the socket. %s\n", v21);
}
else
{
v23 = func8(dmx_drv_list, 0LL);
if ( v23 )
{
v6 = v4 + 1;
v22 = 1LL << v4;
v7 = v4 >> 6;
while ( 1 )
{
timeout.tv_sec = 0LL;
timeout.tv_usec = 24000LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v22;
gettimeofday(&tv, 0LL);
v8 = select(v6, &readfds, 0LL, 0LL, &timeout);
gettimeofday(&v26, 0LL);
if ( v8 > 0 )
{
v28.tv_sec = 0LL;
v28.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v22;
v13 = 0;
do
{
++v13;
v14 = recv(v4, buf, 0x201uLL, 0);
}
while ( select(v6, &readfds, 0LL, 0LL, &v28) > 0 );
if ( v14 <= 0x200 )
puts("corrupted data receive");
if ( v13 != 1 )
printf("multiple data received %i\n", v13);
v15 = v26.tv_sec - tv.tv_sec;
v16 = v26.tv_usec - tv.tv_usec;
if ( v26.tv_usec - tv.tv_usec < 0 )
{
--v15;
v16 += 1000000LL;
}
v17 = timeout.tv_sec - v15;
v19 = timeout.tv_usec - v16;
v18 = timeout.tv_usec - v16 < 0;
timeout.tv_sec -= v15;
timeout.tv_usec -= v16;
if ( v18 )
{
timeout.tv_sec = v17 - 1;
timeout.tv_usec = v19 + 1000000;
}
}
buf[0] = 0;
v9 = func9(dmx_drv_list, v23, buf);
if ( v9 != 513 )
printf("send %i bytes to dmx\r", v9);
}
}
}
}
return 1;
}

original_code:
int main(int argc, char* argv[])
{
    dmx_handler h;
    int s = socket(2, SOCK_DGRAM, 0);
    if(s <= 0 ){
        printf("Error while opening socket, abort. %s\n", strerror((*__errno_location ())));
        return 1;
    }
    struct sockaddr_in in;
    in.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    in.sin_port = htons(21812);
    in.sin_family = 2;
    if( bind(s, (struct sockaddr*)&in, sizeof(in)) < 0 ){
        printf("Could not bind the socket. %s\n", strerror((*__errno_location ())));
        return 1;
    }
    h = dmx_open(dmx_drv_list[0], 0);
    if(!h)
      return 1;
    do{
        struct timeval before, after, timeout = { 0, 24e3 };
        unsigned char dmx_data[513];
        int len = 0;
        fd_set fd;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fd)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
        gettimeofday(&before, 0);
        int error = select(s+1, &fd, 0, 0, &timeout);
        gettimeofday(&after, 0);
        if( error == 0 )
            wake_up_task("popo");
        else if( error > 0 )
        {
            int count = 0;
            struct timeval flush = { 0, 0};
            do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd)->__fds_bits)[0]) : "memory"); } while (0);
            ((void) (((&fd)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
            do{
                len = recv(s, dmx_data, sizeof(dmx_data), 0);
                count++;
            }while( select(s+1, &fd, 0, 0, &flush ) > 0 );
            if( len < sizeof(dmx_data) )
                printf("corrupted data receive\n");
            if( count > 1 )
                printf("multiple data received %i\n", count);
            struct timeval timediff;
            do { (&timediff)->tv_sec = (&after)->tv_sec - (&before)->tv_sec; (&timediff)->tv_usec = (&after)->tv_usec - (&before)->tv_usec; if ((&timediff)->tv_usec < 0) { --(&timediff)->tv_sec; (&timediff)->tv_usec += 1000000; } } while (0);
            do { (&timeout)->tv_sec = (&timeout)->tv_sec - (&timediff)->tv_sec; (&timeout)->tv_usec = (&timeout)->tv_usec - (&timediff)->tv_usec; if ((&timeout)->tv_usec < 0) { --(&timeout)->tv_sec; (&timeout)->tv_usec += 1000000; } } while (0);
        }
        dmx_data[0] = 0;
        len = dmx_send(dmx_drv_list[0], h, dmx_data);
        if( len != 513)
            printf("send %i bytes to dmx\r", len);
    }while(1);
    dmx_close(dmx_drv_list[0], h);
    close(s);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    void *h;
    int s = socket(2, SOCK_DGRAM, 0);
    if(s <= 0 ){
        printf("Error while opening socket, abort. %s\n", strerror((*__errno_location ())));
        return 1;
    }
    struct sockaddr_in in;
    in.sin_addr.s_addr = ((unsigned int) 0x00000000);
    in.sin_port = htons(21812);
    in.sin_family = 2;
    if( bind(s, (struct sockaddr *)&in, sizeof(in)) < 0 ){
        printf("Could not bind the socket. %s\n", strerror((*__errno_location ())));
        return 1;
    }
    h = dmx_open(dmx_drv_list[0], 0);
    if(!h)
      return 1;
    do{
        struct timeval before; struct timeval after; struct timeval timeout = { 0, 24e3 };
        unsigned char dmx_data[513];
        int len = 0;
        struct fd_set fd;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fd)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
        gettimeofday(&before, 0);
        int error = select(s+1, &fd, 0, 0, &timeout);
        gettimeofday(&after, 0);
        if( error == 0 )
            wake_up_task("popo");
        else if( error > 0 )
        {
            int count = 0;
            struct timeval flush = { 0, 0};
            do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd)->__fds_bits)[0]) : "memory"); } while (0);
            ((void) (((&fd)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
            do{
                len = recv(s, dmx_data, sizeof(dmx_data), 0);
                count++;
            }while( select(s+1, &fd, 0, 0, &flush ) > 0 );
            if( len < sizeof(dmx_data) )
                printf("corrupted data receive\n");
            if( count > 1 )
                printf("multiple data received %i\n", count);
            struct timeval timediff;
            do { (&timediff)->tv_sec = (&after)->tv_sec - (&before)->tv_sec; (&timediff)->tv_usec = (&after)->tv_usec - (&before)->tv_usec; if ((&timediff)->tv_usec < 0) { --(&timediff)->tv_sec; (&timediff)->tv_usec += 1000000; } } while (0);
            do { (&timeout)->tv_sec = (&timeout)->tv_sec - (&timediff)->tv_sec; (&timeout)->tv_usec = (&timeout)->tv_usec - (&timediff)->tv_usec; if ((&timeout)->tv_usec < 0) { --(&timeout)->tv_sec; (&timeout)->tv_usec += 1000000; } } while (0);
        }
        dmx_data[0] = 0;
        len = dmx_send(dmx_drv_list[0], h, dmx_data);
        if( len != 513)
            printf("send %i bytes to dmx\r", len);
    }while(1);
    dmx_close(dmx_drv_list[0], h);
    close(s);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
h
void *

variable_types: s int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: len int

variable_types:
fd
struct fd_set { long __fds_bits[-1]; }

variable_types: error int

variable_types: count int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


repo: ellexus/tcshparser

decompiled_code:
char *__fastcall append_dup_string(void *ptr, char *src)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rax

v2 = strlen((const char *)ptr);
v3 = strlen(src);
v4 = (char *)realloc(ptr, v2 + v3 + 1);
return strcat(v4, src);
}

canonical_decompiled_code:
char *__fastcall func0(void *ptr, char *src)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rax

v2 = strlen((const char *)ptr);
v3 = strlen(src);
v4 = (char *)realloc(ptr, v2 + v3 + 1);
return strcat(v4, src);
}

original_code:
char *append_dup_string( char *s, char *suffix )
{
    s = realloc( s, (strlen(s) + strlen(suffix) + 1) );
    strcat( s, suffix );
    return s;
}

canonical_original_code:
char *append_dup_string(char *s, char *suffix)
{
    s = realloc( s, (strlen(s) + strlen(suffix) + 1) );
    strcat( s, suffix );
    return s;
}

variable_types:
s
char *

variable_types:
suffix
char *

return_type: char *


slice

decompiled_code:
char *__fastcall slice(const char *a1, int a2, int a3)
{
int v4; // ebx
int v5; // eax
char *result; // rax

v4 = a2;
v5 = strlen(a1);
if ( a2 >= 0 || (v4 = v5 + a2, v5 + a2 >= 0) )
{
if ( v5 <= v4 )
v4 = v5;
if ( a3 > 0 )
goto LABEL_5;
}
else
{
v4 = 0;
if ( a3 > 0 )
goto LABEL_5;
}
a3 += v5;
if ( a3 < 0 )
{
v5 = 0;
goto LABEL_7;
}
LABEL_5:
if ( v5 > a3 )
v5 = a3;
LABEL_7:
if ( v4 >= v5 )
result = strdup(&s);
else
result = strndup(&a1[v4], v5 - v4);
return result;
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1, int a2, int a3)
{
int v4; // ebx
int v5; // eax
char *result; // rax

v4 = a2;
v5 = strlen(a1);
if ( a2 >= 0 || (v4 = v5 + a2, v5 + a2 >= 0) )
{
if ( v5 <= v4 )
v4 = v5;
if ( a3 > 0 )
goto LABEL_5;
}
else
{
v4 = 0;
if ( a3 > 0 )
goto LABEL_5;
}
a3 += v5;
if ( a3 < 0 )
{
v5 = 0;
goto LABEL_7;
}
LABEL_5:
if ( v5 > a3 )
v5 = a3;
LABEL_7:
if ( v4 >= v5 )
result = strdup(&s);
else
result = strndup(&a1[v4], v5 - v4);
return result;
}

original_code:
char *slice( char *s, int relative_begin, int relative_end )
{
    int length = strlen( s );
    int absolute_begin;
    int absolute_end;
    char *result;
    if ( relative_begin >= 0 ) {
        absolute_begin = relative_begin;
    } else {
        absolute_begin = length + relative_begin;
    }
    if ( absolute_begin < 0 ) {
        absolute_begin = 0;
    } else if ( absolute_begin > length ) {
        absolute_begin = length;
    }
    if ( relative_end > 0 ) {
        absolute_end = relative_end;
    } else {
        absolute_end = length + relative_end;
    }
    if ( absolute_end < 0 ) {
        absolute_end = 0;
    } else if ( absolute_end > length ) {
        absolute_end = length;
    }
    if ( absolute_begin < absolute_end ) {
        result = strndup( &(s[absolute_begin]), absolute_end - absolute_begin );
    } else {
        result = strdup( "" );
    }
    return result;
}

canonical_original_code:
char *slice(char *s, int relative_begin, int relative_end)
{
    int length = strlen( s );
    int absolute_begin;
    int absolute_end;
    char *result;
    if ( relative_begin >= 0 ) {
        absolute_begin = relative_begin;
    } else {
        absolute_begin = length + relative_begin;
    }
    if ( absolute_begin < 0 ) {
        absolute_begin = 0;
    } else if ( absolute_begin > length ) {
        absolute_begin = length;
    }
    if ( relative_end > 0 ) {
        absolute_end = relative_end;
    } else {
        absolute_end = length + relative_end;
    }
    if ( absolute_end < 0 ) {
        absolute_end = 0;
    } else if ( absolute_end > length ) {
        absolute_end = length;
    }
    if ( absolute_begin < absolute_end ) {
        result = strndup( &(s[absolute_begin]), absolute_end - absolute_begin );
    } else {
        result = strdup( "" );
    }
    return result;
}

variable_types: length int

variable_types:
result
char *


decompiled_code:
void *__fastcall get_enclosed_string(void *ptr, char a2, char a3)
{
void *v3; // r12
char *v5; // rbx

v3 = ptr;
if ( *(_BYTE *)ptr == a2 && *((char *)ptr + strlen((const char *)ptr) - 1) == a3 )
{
v5 = slice((const char *)ptr, 1, -1);
free(ptr);
v3 = v5;
}
return v3;
}

canonical_decompiled_code:
void *__fastcall func2(void *ptr, char a2, char a3)
{
void *v3; // r12
char *v5; // rbx

v3 = ptr;
if ( *(_BYTE *)ptr == a2 && *((char *)ptr + strlen((const char *)ptr) - 1) == a3 )
{
v5 = func1((const char *)ptr, 1, -1);
free(ptr);
v3 = v5;
}
return v3;
}

original_code:
static char *get_enclosed_string( char *s, char left, char right )
{
    size_t length;
    char *result;
    length = strlen( s );
    if ( (s[0] == left) && (s[length-1] == right) ) {
        result = slice( s, 1, -1 );
        free( s );
    } else {
        result = s;
    }
    return result;
}

canonical_original_code:
static char *get_enclosed_string(char *s, char left, char right)
{
    unsigned long length;
    char *result;
    length = strlen( s );
    if ( (s[0] == left) && (s[length-1] == right) ) {
        result = slice( s, 1, -1 );
        free( s );
    } else {
        result = s;
    }
    return result;
}

variable_types: left char

variable_types: right char

variable_types:
length
unsigned long


decompiled_code:
__int64 __fastcall split_after_first_word(const char *a1)
{
__int64 v1; // rbx
int v2; // ebp
const unsigned __int16 *v3; // rdx
const char *v4; // rax
const char *v5; // r12
char *v6; // rax
__int64 v7; // rax
__int64 v8; // rbx
__int64 v9; // r13
const unsigned __int16 *v10; // rdx
const char *v11; // rax
char *v12; // rax

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = 0;
v3 = *_ctype_b_loc();
v4 = a1 + 1;
while ( (v3[v1] & 0x2000) == 0 )
{
v5 = v4;
v1 = *v4++;
++v2;
if ( !(_BYTE)v1 )
goto LABEL_6;
}
v5 = &a1[v2];
}
else
{
v5 = a1;
v2 = 0;
}
LABEL_6:
v6 = slice(a1, 0, v2);
v7 = append_to_list(0LL, v6);
v8 = *v5;
v9 = v7;
if ( (_BYTE)v8 )
{
v10 = *_ctype_b_loc();
v11 = &a1[v2 + 1];
do
{
if ( (v10[v8] & 0x2000) == 0 )
break;
v8 = *v11++;
++v2;
}
while ( (_BYTE)v8 );
}
v12 = slice(a1, v2, 0);
return append_to_list(v9, v12);
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
__int64 v1; // rbx
int v2; // ebp
const unsigned __int16 *v3; // rdx
const char *v4; // rax
const char *v5; // r12
char *v6; // rax
__int64 v7; // rax
__int64 v8; // rbx
__int64 v9; // r13
const unsigned __int16 *v10; // rdx
const char *v11; // rax
char *v12; // rax

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = 0;
v3 = *_ctype_b_loc();
v4 = a1 + 1;
while ( (v3[v1] & 0x2000) == 0 )
{
v5 = v4;
v1 = *v4++;
++v2;
if ( !(_BYTE)v1 )
goto LABEL_6;
}
v5 = &a1[v2];
}
else
{
v5 = a1;
v2 = 0;
}
LABEL_6:
v6 = func1(a1, 0, v2);
v7 = append_to_list(0LL, v6);
v8 = *v5;
v9 = v7;
if ( (_BYTE)v8 )
{
v10 = *_ctype_b_loc();
v11 = &a1[v2 + 1];
do
{
if ( (v10[v8] & 0x2000) == 0 )
break;
v8 = *v11++;
++v2;
}
while ( (_BYTE)v8 );
}
v12 = func1(a1, v2, 0);
return append_to_list(v9, v12);
}

original_code:
List *split_after_first_word( char *text )
{
    int i = 0;
    List *result = ((void *)0);
    while ( (text[i] != '\0') && !((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short int) _ISspace) ) {
        i++;
    }
    result = append_to_list( result, slice( text, 0, i ) );
    while ( (text[i] != '\0') && ((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short int) _ISspace) ) {
        i++;
    }
    result = append_to_list( result, slice( text, i, 0 ) );
    return result;
}

canonical_original_code:
struct list *split_after_first_word(char *text)
{
    int i = 0;
    struct list *result = ((void *)0);
    while ( (text[i] != '\0') && !((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short) _ISspace) ) {
        i++;
    }
    result = append_to_list( result, slice( text, 0, i ) );
    while ( (text[i] != '\0') && ((*__ctype_b_loc ())[(int) ((text[i]))] & (unsigned short) _ISspace) ) {
        i++;
    }
    result = append_to_list( result, slice( text, i, 0 ) );
    return result;
}

variable_types:
text
char *

variable_types: i int

variable_types: struct list { struct list *next; char *contents; } *


split

original_code:
List *split( char *text, char *delimiters )
{
    int length = strlen(text);
    int in_quote = 0;
    int i;
    List *result = ((void *)0);
    char *buffer;
    char *p;
    buffer = malloc( length + 1 );
    p = buffer;
    for ( i = 0; i < length; i++ ) {
        if ( text[i] == '"' ) {
            in_quote = !in_quote;
        } else if ( in_quote || (strchr(delimiters, text[i]) == ((void *)0)) ) {
            *p++ = text[i];
        } else if ( p != buffer ) {
            *p = '\0';
            result = append_to_list( result, strdup( buffer ) );
            p = buffer;
        }
    }
    if ( p != buffer ) {
        *p = '\0';
        result = append_to_list( result, strdup( buffer ) );
    }
    free( buffer );
    return result;
}

canonical_original_code:
struct list *split(char *text, char *delimiters)
{
    int length = strlen(text);
    int in_quote = 0;
    int i;
    struct list *result = ((void *)0);
    char *buffer;
    char *p;
    buffer = malloc( length + 1 );
    p = buffer;
    for ( i = 0; i < length; i++ ) {
        if ( text[i] == '"' ) {
            in_quote = !in_quote;
        } else if ( in_quote || (strchr(delimiters, text[i]) == ((void *)0)) ) {
            *p++ = text[i];
        } else if ( p != buffer ) {
            *p = '\0';
            result = append_to_list( result, strdup( buffer ) );
            p = buffer;
        }
    }
    if ( p != buffer ) {
        *p = '\0';
        result = append_to_list( result, strdup( buffer ) );
    }
    free( buffer );
    return result;
}

variable_types:
delimiters
char *

variable_types:
buffer
char *

variable_types:
p
char *

return_type: struct list { struct list *next; char *contents; } *


trim

decompiled_code:
char *__fastcall trim(void *ptr)
{
int v1; // ebx
int v2; // esi
const unsigned __int16 *v3; // rdx
char *v4; // rax
char *v5; // rax
char *v6; // r12

v1 = strlen((const char *)ptr) - 1;
if ( v1 <= 0 )
{
v2 = 0;
}
else
{
v2 = 0;
v3 = *_ctype_b_loc();
v4 = (char *)ptr;
while ( (v3[*v4] & 0x2000) != 0 )
{
++v2;
++v4;
if ( v1 == v2 )
goto LABEL_9;
}
if ( v1 > v2 )
{
v5 = (char *)ptr + v1;
do
{
if ( (v3[*v5] & 0x2000) == 0 )
break;
--v1;
--v5;
}
while ( v1 > v2 );
}
}
LABEL_9:
v6 = slice((const char *)ptr, v2, v1 + 1);
free(ptr);
return v6;
}

canonical_decompiled_code:
char *__fastcall func5(void *ptr)
{
int v1; // ebx
int v2; // esi
const unsigned __int16 *v3; // rdx
char *v4; // rax
char *v5; // rax
char *v6; // r12

v1 = strlen((const char *)ptr) - 1;
if ( v1 <= 0 )
{
v2 = 0;
}
else
{
v2 = 0;
v3 = *_ctype_b_loc();
v4 = (char *)ptr;
while ( (v3[*v4] & 0x2000) != 0 )
{
++v2;
++v4;
if ( v1 == v2 )
goto LABEL_9;
}
if ( v1 > v2 )
{
v5 = (char *)ptr + v1;
do
{
if ( (v3[*v5] & 0x2000) == 0 )
break;
--v1;
--v5;
}
while ( v1 > v2 );
}
}
LABEL_9:
v6 = func1((const char *)ptr, v2, v1 + 1);
free(ptr);
return v6;
}

original_code:
char *trim( char *s )
{
    int begin = 0;
    int end = strlen(s) - 1;
    char *result;
    while ( (begin < end) && ((*__ctype_b_loc ())[(int) ((s[begin]))] & (unsigned short int) _ISspace) ) {
        begin++;
    }
    while ( (end > begin) && ((*__ctype_b_loc ())[(int) ((s[end]))] & (unsigned short int) _ISspace) ) {
        end--;
    }
    result = slice( s, begin, (end+1) );
    free( s );
    return ( result );
}

canonical_original_code:
char *trim(char *s)
{
    int begin = 0;
    int end = strlen(s) - 1;
    char *result;
    while ( (begin < end) && ((*__ctype_b_loc ())[(int) ((s[begin]))] & (unsigned short) _ISspace) ) {
        begin++;
    }
    while ( (end > begin) && ((*__ctype_b_loc ())[(int) ((s[end]))] & (unsigned short) _ISspace) ) {
        end--;
    }
    result = slice( s, begin, (end+1) );
    free( s );
    return ( result );
}

variable_types: begin int

variable_types: end int


decompiled_code:
void *__fastcall get_string_in_brackets(void *a1)
{
return get_enclosed_string(a1, 40, 41);
}

canonical_decompiled_code:
void *__fastcall func6(void *a1)
{
return func2(a1, 40, 41);
}

original_code:
char *get_string_in_brackets( char *string )
{
    return get_enclosed_string( string, '(', ')' );
}

canonical_original_code:
char *get_string_in_brackets(char *string)
{
    return get_enclosed_string( string, '(', ')' );
}

variable_types:
string
char *


decompiled_code:
void *__fastcall get_string_in_quotes(void *a1)
{
return get_enclosed_string(a1, 34, 34);
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
return func2(a1, 34, 34);
}

original_code:
char *get_string_in_quotes( char *string )
{
    return get_enclosed_string( string, '"', '"' );
}

canonical_original_code:
char *get_string_in_quotes(char *string)
{
    return get_enclosed_string( string, '"', '"' );
}


decompiled_code:
__int64 __fastcall list_length(_QWORD *a1)
{
__int64 result; // rax

for ( result = 0LL; a1; result = (unsigned int)(result + 1) )
a1 = (_QWORD *)*a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
__int64 result; // rax

for ( result = 0LL; a1; result = (unsigned int)(result + 1) )
a1 = (_QWORD *)*a1;
return result;
}

original_code:
int list_length( List *list )
{
    int result = 0;
    while ( list != ((void *)0) ) {
        list = list->next;
        result++;
    }
    return result;
}

canonical_original_code:
int list_length(struct list *list)
{
    int result = 0;
    while ( list != ((void *)0) ) {
        list = list->next;
        result++;
    }
    return result;
}

variable_types: result int

user_defined_types: struct list { struct list *next; char *contents; }


decompiled_code:
char *__fastcall get_nth_word(_QWORD *a1, int a2)
{
const char *v2; // r8

if ( a1 && a2 > 0 )
{
do
{
--a2;
a1 = (_QWORD *)*a1;
}
while ( a2 > 0 && a1 );
}
v2 = &s;
if ( a1 )
v2 = (const char *)a1[1];
return strdup(v2);
}

canonical_decompiled_code:
char *__fastcall func1(_QWORD *a1, int a2)
{
const char *v2; // r8

if ( a1 && a2 > 0 )
{
do
{
--a2;
a1 = (_QWORD *)*a1;
}
while ( a2 > 0 && a1 );
}
v2 = &s;
if ( a1 )
v2 = (const char *)a1[1];
return strdup(v2);
}

original_code:
char *get_nth_word( List *list, int n )
{
    char *word;
    while ( (n > 0) && (list != ((void *)0)) ) {
        n--;
        list = list->next;
    }
    if ( list != ((void *)0) ) {
        word = list->contents;
    } else {
        word = "";
    }
    return strdup( word );
}

canonical_original_code:
char *get_nth_word(struct list *list, int n)
{
    char *word;
    while ( (n > 0) && (list != ((void *)0)) ) {
        n--;
        list = list->next;
    }
    if ( list != ((void *)0) ) {
        word = list->contents;
    } else {
        word = "";
    }
    return strdup( word );
}

variable_types: n int

variable_types:
word
char *


decompiled_code:
void __fastcall free_list(void *ptr)
{
_QWORD *i; // rbp
void *v2; // rdi
_QWORD *v3; // rbx

if ( ptr )
{
for ( i = ptr; ; i = v3 )
{
v2 = (void *)i[1];
if ( v2 )
free(v2);
v3 = (_QWORD *)*i;
free(i);
if ( !v3 )
break;
}
}
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
_QWORD *i; // rbp
void *v2; // rdi
_QWORD *v3; // rbx

if ( ptr )
{
for ( i = ptr; ; i = v3 )
{
v2 = (void *)i[1];
if ( v2 )
free(v2);
v3 = (_QWORD *)*i;
free(i);
if ( !v3 )
break;
}
}
}

original_code:
void free_list( List *list )
{
    List *entry;
    while ( list != ((void *)0) ) {
        if ( list->contents != ((void *)0) ) {
            free( list->contents );
        }
        entry = list;
        list = list->next;
        free( entry );
    }
}

canonical_original_code:
void free_list(struct list *list)
{
    struct list *entry;
    while ( list != ((void *)0) ) {
        if ( list->contents != ((void *)0) ) {
            free( list->contents );
        }
        entry = list;
        list = list->next;
        free( entry );
    }
}


decompiled_code:
char *__fastcall lookup_alias(char *s1, __int64 **a2)
{
__int64 **v2; // rbx

if ( !a2 )
return 0LL;
v2 = a2;
do
{
if ( !strcmp(s1, (const char *)v2[1]) )
return strdup((const char *)v2[2]);
v2 = (__int64 **)*v2;
}
while ( v2 );
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func1(char *s1, __int64 **a2)
{
__int64 **v2; // rbx

if ( !a2 )
return 0LL;
v2 = a2;
do
{
if ( !strcmp(s1, (const char *)v2[1]) )
return strdup((const char *)v2[2]);
v2 = (__int64 **)*v2;
}
while ( v2 );
return 0LL;
}

original_code:
char *lookup_alias( char *cmd, Alias *a )
{
    while ( (a != ((void *)0)) && (strcmp( cmd, a->lhs ) != 0) ) {
        a = a->next;
    }
    if ( a != ((void *)0) ) {
        return strdup( a->rhs );
    } else {
        return ((void *)0);
    }
}

canonical_original_code:
char *lookup_alias(char *cmd, struct alias *a)
{
    while ( (a != ((void *)0)) && (strcmp( cmd, a->lhs ) != 0) ) {
        a = a->next;
    }
    if ( a != ((void *)0) ) {
        return strdup( a->rhs );
    } else {
        return ((void *)0);
    }
}

variable_types:
cmd
char *

variable_types: struct alias { struct alias *next; char *lhs; char *rhs; } *


repo: ellysh/pci-dask

decompiled_code:
int __fastcall sig3_handler(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 60 )
++int3_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 2LL, &v2);
return printf("INT3 count = %d, COS Data: %x. \n", (unsigned int)int3_count, v2);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 60 )
++int3_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 2LL, &v2);
return printf("INT3 count = %d, COS Data: %x. \n", (unsigned int)int3_count, v2);
}

original_code:
void sig3_handler(int signo)
{
    U32 CosData = 0;
    switch(signo){
        case 60:
            int3_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 2, &CosData);
    printf("INT3 count = %d, COS Data: %x. \n", int3_count, CosData);
    return;
}

canonical_original_code:
void sig3_handler(int signo)
{
    unsigned long CosData = 0;
    switch(signo){
        case 60:
            int3_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 2, &CosData);
    printf("INT3 count = %d, COS Data: %x. \n", int3_count, CosData);
    return;
}

variable_types: signo int

variable_types:
CosData
unsigned long


decompiled_code:
int __fastcall sig4_handler(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 59 )
++int4_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 3LL, &v2);
return printf("INT4 count = %d, COS Data: %x. \n", (unsigned int)int4_count, v2);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 59 )
++int4_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 3LL, &v2);
return printf("INT4 count = %d, COS Data: %x. \n", (unsigned int)int4_count, v2);
}

original_code:
void sig4_handler(int signo)
{
    U32 CosData = 0;
    switch(signo){
        case 59:
            int4_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 3, &CosData);
    printf("INT4 count = %d, COS Data: %x. \n", int4_count, CosData);
    return;
}

canonical_original_code:
void sig4_handler(int signo)
{
    unsigned long CosData = 0;
    switch(signo){
        case 59:
            int4_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 3, &CosData);
    printf("INT4 count = %d, COS Data: %x. \n", int4_count, CosData);
    return;
}


decompiled_code:
int __fastcall sig1_handler(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 61 )
++int_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 0LL, &v2);
return printf("INT1 count = %d, COS Data: %x. \n", (unsigned int)int_count, v2);
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 61 )
++int_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 0LL, &v2);
return printf("INT1 count = %d, COS Data: %x. \n", (unsigned int)int_count, v2);
}

original_code:
void sig1_handler(int signo)
{
    U32 CosData = 0;
    switch(signo){
        case 61:
            int_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 0, &CosData);
    printf("INT1 count = %d, COS Data: %x. \n", int_count, CosData);
    return;
}

canonical_original_code:
void sig1_handler(int signo)
{
    unsigned long CosData = 0;
    switch(signo){
        case 61:
            int_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 0, &CosData);
    printf("INT1 count = %d, COS Data: %x. \n", int_count, CosData);
    return;
}


decompiled_code:
int __fastcall sig2_handler(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 62 )
++int2_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 1LL, &v2);
return printf("INT2 count = %d, COS Data: %x. \n", (unsigned int)int2_count, v2);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( a1 == 62 )
++int2_count;
DIO_GetCOSLatchDataInt32((unsigned __int16)card, 1LL, &v2);
return printf("INT2 count = %d, COS Data: %x. \n", (unsigned int)int2_count, v2);
}

original_code:
void sig2_handler(int signo)
{
    U32 CosData = 0;
    switch(signo){
        case 62:
            int2_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 1, &CosData);
    printf("INT2 count = %d, COS Data: %x. \n", int2_count, CosData);
    return;
}

canonical_original_code:
void sig2_handler(int signo)
{
    unsigned long CosData = 0;
    switch(signo){
        case 62:
            int2_count ++;
            break;
    }
    DIO_GetCOSLatchDataInt32(card, 1, &CosData);
    printf("INT2 count = %d, COS Data: %x. \n", int2_count, CosData);
    return;
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int16 v3; // ax
__int16 v4; // ax
__int64 v5; // rsi
__int16 v6; // ax
__int16 v7; // ax
__int16 v8; // ax

setbuf(stdout, 0LL);
puts("\nPCI-7443 Dual Interrupt Sample");
puts("\nThis program shows the number of occurrence of interrupt");
puts("for COS0, COS1, COS2 and COS3 interrupt lines.");
printf("\nPlease input a card number: ");
_isoc99_scanf(" %hd", &card_number);
v3 = Register_Card(38LL, (unsigned __int16)card_number);
card = v3;
if ( v3 >= 0 )
{
usleep(0x3E8u);
v4 = DIO_SetCOSInterrupt32((unsigned __int16)card, 0LL, 1LL, sig1_handler);
v5 = (unsigned int)v4;
if ( v4
|| (v6 = DIO_SetCOSInterrupt32((unsigned __int16)card, 1LL, 1LL, sig2_handler), v5 = (unsigned int)v6, v6)
|| (v7 = DIO_SetCOSInterrupt32((unsigned __int16)card, 2LL, 1LL, sig3_handler), v5 = (unsigned int)v7, v7)
|| (v8 = DIO_SetCOSInterrupt32((unsigned __int16)card, 3LL, 1LL, sig4_handler), v5 = (unsigned int)v8, v8) )
{
printf(" error from DIO_SetCOSInterrupt32 : %d \n", v5);
}
else
{
puts("\nPress any key to exit...");
getch();
DIO_SetCOSInterrupt32((unsigned __int16)card, 0LL, 0LL, 0LL);
DIO_SetCOSInterrupt32((unsigned __int16)card, 1LL, 0LL, 0LL);
DIO_SetCOSInterrupt32((unsigned __int16)card, 2LL, 0LL, 0LL);
DIO_SetCOSInterrupt32((unsigned __int16)card, 3LL, 0LL, 0LL);
}
if ( card >= 0 )
Release_Card();
exit(0);
}
printf("Register_Card Error = %d\n", (unsigned int)v3);
exit(0);
}

original_code:
int main(void)
{
    int err;
    char temp_char;
    setbuf( stdout, ((void *)0) );
    printf("\nPCI-7443 Dual Interrupt Sample\n" );
    printf("\nThis program shows the number of occurrence of interrupt\n" );
    printf("for COS0, COS1, COS2 and COS3 interrupt lines.\n" );
    printf("\nPlease input a card number: ");
    scanf(" %hd", &card_number);
    if((card=Register_Card(38, card_number))<0){
        printf("Register_Card Error = %d\n", card);
        exit(0);
    }
    usleep(1000);
    err = DIO_SetCOSInterrupt32(card, 0, 0x1, sig1_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    err = DIO_SetCOSInterrupt32(card, 1, 0x1, sig2_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    err = DIO_SetCOSInterrupt32(card, 2, 0x1, sig3_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    err = DIO_SetCOSInterrupt32(card, 3, 0x1, sig4_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    printf("\nPress any key to exit...\n" );
    temp_char = getch();
    DIO_SetCOSInterrupt32(card, 0, 0, ((void *)0));
    DIO_SetCOSInterrupt32(card, 1, 0, ((void *)0));
    DIO_SetCOSInterrupt32(card, 2, 0, ((void *)0));
    DIO_SetCOSInterrupt32(card, 3, 0, ((void *)0));
exit_after_error:
    if(card>=0)
        Release_Card(card);
    exit(0);
}

canonical_original_code:
int main()
{
    int err;
    char temp_char;
    setbuf( stdout, ((void *)0) );
    printf("\nPCI-7443 Dual Interrupt Sample\n" );
    printf("\nThis program shows the number of occurrence of interrupt\n" );
    printf("for COS0, COS1, COS2 and COS3 interrupt lines.\n" );
    printf("\nPlease input a card number: ");
    scanf(" %hd", &card_number);
    if((card=Register_Card(38, card_number))<0){
        printf("Register_Card Error = %d\n", card);
        exit(0);
    }
    usleep(1000);
    err = DIO_SetCOSInterrupt32(card, 0, 0x1, sig1_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    err = DIO_SetCOSInterrupt32(card, 1, 0x1, sig2_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    err = DIO_SetCOSInterrupt32(card, 2, 0x1, sig3_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    err = DIO_SetCOSInterrupt32(card, 3, 0x1, sig4_handler);
    if(err!=0){
        printf(" error from DIO_SetCOSInterrupt32 : %d \n" , err );
        goto exit_after_error;
    }
    printf("\nPress any key to exit...\n" );
    temp_char = getch();
    DIO_SetCOSInterrupt32(card, 0, 0, ((void *)0));
    DIO_SetCOSInterrupt32(card, 1, 0, ((void *)0));
    DIO_SetCOSInterrupt32(card, 2, 0, ((void *)0));
    DIO_SetCOSInterrupt32(card, 3, 0, ((void *)0));
exit_after_error:
    if(card>=0)
        Release_Card(card);
    exit(0);
}

variable_types: err int


original_code:
void exch_data(unsigned short * tbuf, U32 cnt)
{
    unsigned short temp;
    int i;
    for (i=0;i<cnt;i++) {
        if (!(i%2)) {
            temp = tbuf[i];
            tbuf[i] = tbuf[i+1];
            tbuf[i+1] = temp;
        }
    }
}

canonical_original_code:
void exch_data(unsigned short *tbuf, unsigned long cnt)
{
    unsigned short temp;
    int i;
    for (i=0;i<cnt;i++) {
        if (!(i%2)) {
            temp = tbuf[i];
            tbuf[i] = tbuf[i+1];
            tbuf[i+1] = temp;
        }
    }
}

variable_types:
tbuf
unsigned short *

variable_types:
cnt
unsigned long

variable_types:
temp
unsigned short


restore

decompiled_code:
int __fastcall restore(__int64 a1)
{
int result; // eax

result = ioctl(0, 0x5408uLL, a1);
if ( result == -1 )
{
perror("ioctl");
exit(5);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int result; // eax

result = ioctl(0, 0x5408uLL, a1);
if ( result == -1 )
{
perror("ioctl");
exit(5);
}
return result;
}

original_code:
void restore(struct termio *tty_save)
{
    if(ioctl(0, 0x5408, tty_save)==-1){
        perror("ioctl");
        exit(5);
    }
    return;
}

variable_types: struct termio { unsigned short c_iflag; unsigned short c_oflag; unsigned short c_cflag; unsigned short c_lflag; unsigned char c_line; unsigned char c_cc[8]; } *


getch

decompiled_code:
__int64 getch()
{
__m128i v0; // xmm0
int v1; // eax
unsigned __int8 buf; // [rsp+Fh] [rbp-29h] BYREF
__m128i v4; // [rsp+10h] [rbp-28h] BYREF
__int16 v5; // [rsp+20h] [rbp-18h]

if ( ioctl(0, 0x5405uLL, &v4) == -1 )
{
perror("ioctl");
exit(1);
}
v0 = _mm_loadu_si128(&v4);
v4.m128i_i16[3] &= 0xFFF4u;
word_2C0 = v5;
v4.m128i_i16[7] = 256;
ttysave_1 = (__int128)v0;
if ( ioctl(0, 0x5408uLL, &v4) == -1 )
{
restore((__int64)&ttysave_1);
perror("ioctl");
exit(2);
}
v1 = read(0, &buf, 1uLL);
if ( v1 == -1 )
{
restore((__int64)&ttysave_1);
perror("read");
exit(3);
}
if ( !v1 )
{
restore((__int64)&ttysave_1);
fwrite("EOF error!", 1uLL, 0xAuLL, stderr);
exit(4);
}
restore((__int64)&ttysave_1);
return buf;
}

canonical_decompiled_code:
__int64 func1()
{
__m128i v0; // xmm0
int v1; // eax
unsigned __int8 buf; // [rsp+Fh] [rbp-29h] BYREF
__m128i v4; // [rsp+10h] [rbp-28h] BYREF
__int16 v5; // [rsp+20h] [rbp-18h]

if ( ioctl(0, 0x5405uLL, &v4) == -1 )
{
perror("ioctl");
exit(1);
}
v0 = _mm_loadu_si128(&v4);
v4.m128i_i16[3] &= 0xFFF4u;
word_2C0 = v5;
v4.m128i_i16[7] = 256;
ttysave_1 = (__int128)v0;
if ( ioctl(0, 0x5408uLL, &v4) == -1 )
{
func0((__int64)&ttysave_1);
perror("ioctl");
exit(2);
}
v1 = read(0, &buf, 1uLL);
if ( v1 == -1 )
{
func0((__int64)&ttysave_1);
perror("read");
exit(3);
}
if ( !v1 )
{
func0((__int64)&ttysave_1);
fwrite("EOF error!", 1uLL, 0xAuLL, stderr);
exit(4);
}
func0((__int64)&ttysave_1);
return buf;
}

original_code:
char getch(void)
{
    char ch;
    int total, flag = 1;
    static struct termio ttysave;
    struct termio tty;
    if(flag){
        flag = 0;
        if(ioctl(0, 0x5405, &tty)==-1){
            perror("ioctl");
            exit(1);
        }
        ttysave = tty;
        tty.c_lflag &= ~(0000002|0000010|0000001);
        tty.c_cc[6] = 1;
        tty.c_cc[5] = 0;
        if(ioctl(0, 0x5408, &tty)==-1){
            restore(&ttysave);
            perror("ioctl");
            exit(2);
        }
    }
    switch(total = read(0, &ch, 1)){
        case -1:
            restore(&ttysave);
            perror("read");
            exit(3);
        case 0:
            restore(&ttysave);
            fputs("EOF error!", stderr );
            exit(4);
        default:
            ;
    }
    restore(&ttysave);
    return ch;
}

canonical_original_code:
char getch()
{
    char ch;
    int total; int flag = 1;
    static struct termio ttysave;
    struct termio tty;
    if(flag){
        flag = 0;
        if(ioctl(0, 0x5405, &tty)==-1){
            perror("ioctl");
            exit(1);
        }
        ttysave = tty;
        tty.c_lflag &= ~(0000002|0000010|0000001);
        tty.c_cc[6] = 1;
        tty.c_cc[5] = 0;
        if(ioctl(0, 0x5408, &tty)==-1){
            restore(&ttysave);
            perror("ioctl");
            exit(2);
        }
    }
    switch(total = read(0, &ch, 1)){
        case -1:
            restore(&ttysave);
            perror("read");
            exit(3);
        case 0:
            restore(&ttysave);
            fputs("EOF error!", stderr );
            exit(4);
        default:
            ;
    }
    restore(&ttysave);
    return ch;
}

variable_types: ch char

variable_types: total int

variable_types: flag int

variable_types: struct termio { unsigned short c_iflag; unsigned short c_oflag; unsigned short c_cflag; unsigned short c_lflag; unsigned char c_line; unsigned char c_cc[8]; }

return_type: char


original_code:
char kb_hit(void)
{
    int total, flag = 1;
    char result = 0x00;
    char temp_char;
    static struct termio ttysave;
    struct termio tty;
    if(flag){
        flag = 0;
        if(ioctl(0, 0x5405, &tty)==-1){
            perror("ioctl");
            exit(1);
        }
        ttysave = tty;
        tty.c_lflag &= ~(0000002|0000010|0000001);
        tty.c_cc[6] =0;
        tty.c_cc[5] =1;
        if(ioctl(0, 0x5408, &tty)==-1){
            restore(&ttysave);
            perror("ioctl");
            exit(2);
        }
    }
    switch(total = read(0, &temp_char, 1)){
        case -1:
            restore(&ttysave);
            perror("read");
            exit(3);
        case 0:
            result = 0x00;
            break;
        default:
            result = 0x01;
            break;
    }
    restore(&ttysave);
    return result;
}

canonical_original_code:
char kb_hit()
{
    int total; int flag = 1;
    char result = 0x00;
    char temp_char;
    static struct termio ttysave;
    struct termio tty;
    if(flag){
        flag = 0;
        if(ioctl(0, 0x5405, &tty)==-1){
            perror("ioctl");
            exit(1);
        }
        ttysave = tty;
        tty.c_lflag &= ~(0000002|0000010|0000001);
        tty.c_cc[6] =0;
        tty.c_cc[5] =1;
        if(ioctl(0, 0x5408, &tty)==-1){
            restore(&ttysave);
            perror("ioctl");
            exit(2);
        }
    }
    switch(total = read(0, &temp_char, 1)){
        case -1:
            restore(&ttysave);
            perror("read");
            exit(3);
        case 0:
            result = 0x00;
            break;
        default:
            result = 0x01;
            break;
    }
    restore(&ttysave);
    return result;
}

variable_types: result char

user_defined_types: struct termio { unsigned short c_iflag; unsigned short c_oflag; unsigned short c_cflag; unsigned short c_lflag; unsigned char c_line; unsigned char c_cc[8]; }


kbhit

decompiled_code:
__int64 kbhit()
{
struct timeval v1; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

v1.tv_sec = 0LL;
v1.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
select(1, &readfds, 0LL, 0LL, &v1);
return readfds.fds_bits[0] & 1;
}

canonical_decompiled_code:
__int64 func3()
{
struct timeval v1; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

v1.tv_sec = 0LL;
v1.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
select(1, &readfds, 0LL, 0LL, &v1);
return readfds.fds_bits[0] & 1;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types:
rdfs
struct fd_set { long __fds_bits[-1]; }


decompiled_code:
__int64 wait_pulse_start()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2[2]; // [rsp+8h] [rbp-10h] BYREF

do
{
CTR_Read((unsigned __int16)card, 1LL, &v1);
CTR_Read((unsigned __int16)card, 1LL, v2);
}
while ( v2[0] != v1 );
do
{
CTR_Read((unsigned __int16)card, 1LL, &v1);
CTR_Read((unsigned __int16)card, 1LL, v2);
result = v1;
}
while ( v2[0] == v1 );
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2[2]; // [rsp+8h] [rbp-10h] BYREF

do
{
CTR_Read((unsigned __int16)card, 1LL, &v1);
CTR_Read((unsigned __int16)card, 1LL, v2);
}
while ( v2[0] != v1 );
do
{
CTR_Read((unsigned __int16)card, 1LL, &v1);
CTR_Read((unsigned __int16)card, 1LL, v2);
result = v1;
}
while ( v2[0] == v1 );
return result;
}

original_code:
void wait_pulse_start(void)
{
    U32 oldvalue,newvalue;
    do
    {
        CTR_Read( card, 1, &oldvalue );
        CTR_Read( card, 1, &newvalue );
    } while( newvalue != oldvalue );
    do
    {
        CTR_Read( card, 1, &oldvalue );
        CTR_Read( card, 1, &newvalue );
    } while( newvalue == oldvalue );
}

canonical_original_code:
void wait_pulse_start()
{
    unsigned long oldvalue; unsigned long newvalue;
    do
    {
        CTR_Read( card, 1, &oldvalue );
        CTR_Read( card, 1, &newvalue );
    } while( newvalue != oldvalue );
    do
    {
        CTR_Read( card, 1, &oldvalue );
        CTR_Read( card, 1, &newvalue );
    } while( newvalue == oldvalue );
}

variable_types:
oldvalue
unsigned long

variable_types:
newvalue
unsigned long


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // ax
char v4; // al
float v5; // xmm0_4
__m128i v6; // xmm0
int v7; // ebx
double v8; // xmm0_8
unsigned __int64 v9; // rdx
__int64 v10; // rbx
double v11; // xmm0_8
float v12; // xmm0_4
__int64 v14; // [rsp+8h] [rbp-40h] BYREF
unsigned __int64 v15; // [rsp+10h] [rbp-38h] BYREF
unsigned __int64 v16[6]; // [rsp+18h] [rbp-30h] BYREF

setbuf(stdout, 0LL);
v3 = Register_Card(19LL, 0LL);
card = v3;
if ( v3 < 0 )
{
printf("Register_Card Error = %d\n", (unsigned int)v3);
exit(-1);
}
status[0] = 0;
do
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
printf("\x1B[H\x1B[J");
if ( status[0] )
{
puts("\n");
printf(status);
puts("\n\n\n\n\n\n");
}
puts(
" Please select the function for PCI8554 or Esc for quit:\n"
" (0) Square Wave\n"
" (1) Pluse Generator\n"
" (2) Measure Pulse Width\n"
" (3) Measure Frequency");
v4 = getch();
if ( v4 != 50 )
break;
v7 = 0;
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 2LL);
CTR_Setup((unsigned __int16)card, 1LL, 2LL, 0LL, 0LL);
wait_pulse_start();
time(&time1);
CTR_Read((unsigned __int16)card, 1LL, &v15);
while ( 1 )
{
CTR_Read((unsigned __int16)card, 1LL, v16);
time(&time2);
v8 = difftime(time2, time1);
v9 = v15;
if ( v16[0] == v15 || v8 > 5.0 )
break;
v15 = v16[0];
LOWORD(v7) = (v9 < v16[0]) + (_WORD)v7;
}
if ( v8 >= 5.0 )
{
status[42] = 0;
qmemcpy(status, "*** Measure inappropriat", 24);
*(_QWORD *)&status[32] = 0x2A2A206874646977LL;
*(_QWORD *)&status[24] = 0x2065736C75702065LL;
*(_WORD *)&status[40] = 2602;
}
else
{
v10 = 65537 - v16[0] + (v7 << 16);
if ( v10 < 0 )
v11 = (double)(int)(v10 & 1 | ((unsigned __int64)v10 >> 1))
+ (double)(int)(v10 & 1 | ((unsigned __int64)v10 >> 1));
else
v11 = (double)(int)v10;
v12 = v11 * 0.125;
sprintf(status, "*** High level pulse width = %f micro-second ***\n", v12);
}
}
if ( v4 <= 50 )
break;
if ( v4 == 51 )
{
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 0LL);
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 2LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 3LL, 1LL);
CTR_Setup((unsigned __int16)card, 1LL, 2LL, 0xFFFFLL, 0LL);
CTR_Setup((unsigned __int16)card, 2LL, 2LL, 4LL, 0LL);
CTR_Setup((unsigned __int16)card, 3LL, 0LL, 0xFFFFLL, 0LL);
usleep(0xF4240u);
CTR_Read((unsigned __int16)card, 1LL, &v14);
v5 = (double)(unsigned __int16)~(_WORD)v14 * 2000000.0 / 65535.0;
sprintf(status, "Measured Signal Frequency = %f Hz.\n", v5);
}
else
{
strcpy(status, " Unknown choice , please select again \n");
}
}
if ( v4 != 48 )
break;
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 2LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 3LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 4LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 5LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 6LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 7LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 8LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 9LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 10LL, 2LL);
CTR_Setup((unsigned __int16)card, 1LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 2LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 3LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 4LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 5LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 6LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 7LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 8LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 9LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 10LL, 3LL, 32LL, 0LL);
v6 = _mm_load_si128((const __m128i *)&LC11);
*(_QWORD *)&status[48] = 0xA2A2A2A203131LL;
*(__m128i *)status = v6;
*(__m128i *)&status[16] = _mm_load_si128((const __m128i *)&LC12);
*(__m128i *)&status[32] = _mm_load_si128((const __m128i *)&LC13);
}
if ( v4 != 49 )
break;
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 2LL, 1LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 3LL, 1LL);
CTR_Setup((unsigned __int16)card, 1LL, 3LL, 200LL, 0LL);
CTR_Setup((unsigned __int16)card, 2LL, 3LL, 200LL, 0LL);
CTR_Setup((unsigned __int16)card, 3LL, 3LL, 200LL, 0LL);
strcpy(&status[48], "*\n");
*(__m128i *)status = _mm_load_si128((const __m128i *)&LC14);
*(__m128i *)&status[16] = _mm_load_si128((const __m128i *)&LC15);
*(__m128i *)&status[32] = _mm_load_si128((const __m128i *)&LC16);
}
strcpy(status, " Unknown choice , please select again \n");
}
while ( v4 != 27 );
if ( card >= 0 )
Release_Card();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // ax
char v4; // al
float v5; // xmm0_4
__m128i v6; // xmm0
int v7; // ebx
double v8; // xmm0_8
unsigned __int64 v9; // rdx
__int64 v10; // rbx
double v11; // xmm0_8
float v12; // xmm0_4
__int64 v14; // [rsp+8h] [rbp-40h] BYREF
unsigned __int64 v15; // [rsp+10h] [rbp-38h] BYREF
unsigned __int64 v16[6]; // [rsp+18h] [rbp-30h] BYREF

setbuf(stdout, 0LL);
v3 = Register_Card(19LL, 0LL);
card = v3;
if ( v3 < 0 )
{
printf("Register_Card Error = %d\n", (unsigned int)v3);
exit(-1);
}
status[0] = 0;
do
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
printf("\x1B[H\x1B[J");
if ( status[0] )
{
puts("\n");
printf(status);
puts("\n\n\n\n\n\n");
}
puts(
" Please select the function for PCI8554 or Esc for quit:\n"
" (0) Square Wave\n"
" (1) Pluse Generator\n"
" (2) Measure Pulse Width\n"
" (3) Measure Frequency");
v4 = getch();
if ( v4 != 50 )
break;
v7 = 0;
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 2LL);
CTR_Setup((unsigned __int16)card, 1LL, 2LL, 0LL, 0LL);
func0();
time(&time1);
CTR_Read((unsigned __int16)card, 1LL, &v15);
while ( 1 )
{
CTR_Read((unsigned __int16)card, 1LL, v16);
time(&time2);
v8 = difftime(time2, time1);
v9 = v15;
if ( v16[0] == v15 || v8 > 5.0 )
break;
v15 = v16[0];
LOWORD(v7) = (v9 < v16[0]) + (_WORD)v7;
}
if ( v8 >= 5.0 )
{
status[42] = 0;
qmemcpy(status, "*** Measure inappropriat", 24);
*(_QWORD *)&status[32] = 0x2A2A206874646977LL;
*(_QWORD *)&status[24] = 0x2065736C75702065LL;
*(_WORD *)&status[40] = 2602;
}
else
{
v10 = 65537 - v16[0] + (v7 << 16);
if ( v10 < 0 )
v11 = (double)(int)(v10 & 1 | ((unsigned __int64)v10 >> 1))
+ (double)(int)(v10 & 1 | ((unsigned __int64)v10 >> 1));
else
v11 = (double)(int)v10;
v12 = v11 * 0.125;
sprintf(status, "*** High level pulse width = %f micro-second ***\n", v12);
}
}
if ( v4 <= 50 )
break;
if ( v4 == 51 )
{
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 0LL);
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 2LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 3LL, 1LL);
CTR_Setup((unsigned __int16)card, 1LL, 2LL, 0xFFFFLL, 0LL);
CTR_Setup((unsigned __int16)card, 2LL, 2LL, 4LL, 0LL);
CTR_Setup((unsigned __int16)card, 3LL, 0LL, 0xFFFFLL, 0LL);
usleep(0xF4240u);
CTR_Read((unsigned __int16)card, 1LL, &v14);
v5 = (double)(unsigned __int16)~(_WORD)v14 * 2000000.0 / 65535.0;
sprintf(status, "Measured Signal Frequency = %f Hz.\n", v5);
}
else
{
strcpy(status, " Unknown choice , please select again \n");
}
}
if ( v4 != 48 )
break;
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 2LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 3LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 4LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 5LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 6LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 7LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 8LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 9LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 10LL, 2LL);
CTR_Setup((unsigned __int16)card, 1LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 2LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 3LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 4LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 5LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 6LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 7LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 8LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 9LL, 3LL, 32LL, 0LL);
CTR_Setup((unsigned __int16)card, 10LL, 3LL, 32LL, 0LL);
v6 = _mm_load_si128((const __m128i *)&LC11);
*(_QWORD *)&status[48] = 0xA2A2A2A203131LL;
*(__m128i *)status = v6;
*(__m128i *)&status[16] = _mm_load_si128((const __m128i *)&LC12);
*(__m128i *)&status[32] = _mm_load_si128((const __m128i *)&LC13);
}
if ( v4 != 49 )
break;
CTR_8554_CK1_Config((unsigned __int16)card, 0LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 1LL, 2LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 2LL, 1LL);
CTR_8554_ClkSrc_Config((unsigned __int16)card, 3LL, 1LL);
CTR_Setup((unsigned __int16)card, 1LL, 3LL, 200LL, 0LL);
CTR_Setup((unsigned __int16)card, 2LL, 3LL, 200LL, 0LL);
CTR_Setup((unsigned __int16)card, 3LL, 3LL, 200LL, 0LL);
strcpy(&status[48], "*\n");
*(__m128i *)status = _mm_load_si128((const __m128i *)&LC14);
*(__m128i *)&status[16] = _mm_load_si128((const __m128i *)&LC15);
*(__m128i *)&status[32] = _mm_load_si128((const __m128i *)&LC16);
}
strcpy(status, " Unknown choice , please select again \n");
}
while ( v4 != 27 );
if ( card >= 0 )
Release_Card();
return 0;
}

original_code:
int main(void)
{
    char func=0x00;
    U32 value;
    U16 under_flow;
    U32 old_value, new_value;
    float pulse_width, frequency;
    double period;
    setbuf( stdout, ((void *)0) );
    if ((card=Register_Card(19, 0)) < 0) {
        printf("Register_Card Error = %d\n", card);
        exit(-1);
    }
    status[0]=0x00;
    do{
        printf("\033[H\033[J");
        if( status[0] != 0x00 ){
            printf("\n\n");
            printf( status );
            printf("\n\n\n\n\n\n\n");
        }
        printf(" Please select the function for PCI8554 or Esc for quit:\n  (0) Square Wave\n  (1) Pluse Generator\n  (2) Measure Pulse Width\n  (3) Measure Frequency\n");
        func = getch();
        switch (func) {
            case 0x30:
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 1, 2);
                CTR_8554_ClkSrc_Config(card, 2, 2);
                CTR_8554_ClkSrc_Config(card, 3, 2);
                CTR_8554_ClkSrc_Config(card, 4, 2);
                CTR_8554_ClkSrc_Config(card, 5, 2);
                CTR_8554_ClkSrc_Config(card, 6, 2);
                CTR_8554_ClkSrc_Config(card, 7, 2);
                CTR_8554_ClkSrc_Config(card, 8, 2);
                CTR_8554_ClkSrc_Config(card, 9, 2);
                CTR_8554_ClkSrc_Config(card, 10, 2);
                CTR_Setup(card, 1, 3, 32, 0);
                CTR_Setup(card, 2, 3, 32, 0);
                CTR_Setup(card, 3, 3, 32, 0);
                CTR_Setup(card, 4, 3, 32, 0);
                CTR_Setup(card, 5, 3, 32, 0);
                CTR_Setup(card, 6, 3, 32, 0);
                CTR_Setup(card, 7, 3, 32, 0);
                CTR_Setup(card, 8, 3, 32, 0);
                CTR_Setup(card, 9, 3, 32, 0);
                CTR_Setup(card, 10, 3, 32, 0);
                sprintf( status, "*** 250 KHz square wave is ready on the COUT1 ~ 11 ***\n");
                break;
            case 0x31:
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 1, 2);
                CTR_8554_ClkSrc_Config(card, 2, 1);
                CTR_8554_ClkSrc_Config(card, 3, 1);
                CTR_Setup(card, 1, 3, 200, 0);
                CTR_Setup(card, 2, 3, 200, 0);
                CTR_Setup(card, 3, 3, 200, 0);
                sprintf( status, " *** A pulse per second is ready on the COUT3 ***\n");
                break;
            case 0x32:
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 1, 2);
                CTR_Setup(card, 1, 2, 0, 0);
                under_flow = 0;
                wait_pulse_start();
                time(&time1);
                CTR_Read(card, 1, &old_value);
                while (1) {
                    CTR_Read(card, 1, &new_value);
                    time(&time2);
                    period = difftime(time2,time1);
                    if ((new_value == old_value)||(period > 5))
                        break;
                    if ( old_value < new_value )
                        under_flow++;
                    old_value=new_value;
                }
                if (period < 5) {
                    pulse_width = (65536 - new_value + under_flow * 65536 + 1) * 0.125;
                    sprintf( status, "*** High level pulse width = %f micro-second ***\n", pulse_width);
                }
                else
                    sprintf( status, "*** Measure inappropriate pulse width ***\n");
                break;
            case 0x33:
                CTR_8554_ClkSrc_Config(card, 1, 0);
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 2, 2);
                CTR_8554_ClkSrc_Config(card, 3, 1);
                CTR_Setup(card, 1, 2, 65535, 0);
                CTR_Setup(card, 2, 2, 4, 0);
                CTR_Setup(card, 3, 0, 65535, 0);
                usleep(1000000);
                CTR_Read(card, 1, &value);
                period = 65535 - (value&0xffff);
                frequency = period * 2000000 / 65535;
                sprintf( status, "Measured Signal Frequency = %f Hz.\n",frequency);
                break;
            default:
                sprintf( status, " Unknown choice , please select again \n" );
                break;
        }
    }while( func != 0x1b );
    if (card>=0)
        Release_Card(card);
    return 0;
}

canonical_original_code:
int main()
{
    char func = 0x00;
    unsigned long value;
    unsigned short under_flow;
    unsigned long old_value; unsigned long new_value;
    float pulse_width; float frequency;
    double period;
    setbuf( stdout, ((void *)0) );
    if ((card=Register_Card(19, 0)) < 0) {
        printf("Register_Card Error = %d\n", card);
        exit(-1);
    }
    status[0]=0x00;
    do{
        printf("\033[H\033[J");
        if( status[0] != 0x00 ){
            printf("\n\n");
            printf( status );
            printf("\n\n\n\n\n\n\n");
        }
        printf(" Please select the function for PCI8554 or Esc for quit:\n  (0) Square Wave\n  (1) Pluse Generator\n  (2) Measure Pulse Width\n  (3) Measure Frequency\n");
        func = getch();
        switch (func) {
            case 0x30:
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 1, 2);
                CTR_8554_ClkSrc_Config(card, 2, 2);
                CTR_8554_ClkSrc_Config(card, 3, 2);
                CTR_8554_ClkSrc_Config(card, 4, 2);
                CTR_8554_ClkSrc_Config(card, 5, 2);
                CTR_8554_ClkSrc_Config(card, 6, 2);
                CTR_8554_ClkSrc_Config(card, 7, 2);
                CTR_8554_ClkSrc_Config(card, 8, 2);
                CTR_8554_ClkSrc_Config(card, 9, 2);
                CTR_8554_ClkSrc_Config(card, 10, 2);
                CTR_Setup(card, 1, 3, 32, 0);
                CTR_Setup(card, 2, 3, 32, 0);
                CTR_Setup(card, 3, 3, 32, 0);
                CTR_Setup(card, 4, 3, 32, 0);
                CTR_Setup(card, 5, 3, 32, 0);
                CTR_Setup(card, 6, 3, 32, 0);
                CTR_Setup(card, 7, 3, 32, 0);
                CTR_Setup(card, 8, 3, 32, 0);
                CTR_Setup(card, 9, 3, 32, 0);
                CTR_Setup(card, 10, 3, 32, 0);
                sprintf( status, "*** 250 KHz square wave is ready on the COUT1 ~ 11 ***\n");
                break;
            case 0x31:
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 1, 2);
                CTR_8554_ClkSrc_Config(card, 2, 1);
                CTR_8554_ClkSrc_Config(card, 3, 1);
                CTR_Setup(card, 1, 3, 200, 0);
                CTR_Setup(card, 2, 3, 200, 0);
                CTR_Setup(card, 3, 3, 200, 0);
                sprintf( status, " *** A pulse per second is ready on the COUT3 ***\n");
                break;
            case 0x32:
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 1, 2);
                CTR_Setup(card, 1, 2, 0, 0);
                under_flow = 0;
                wait_pulse_start();
                time(&time1);
                CTR_Read(card, 1, &old_value);
                while (1) {
                    CTR_Read(card, 1, &new_value);
                    time(&time2);
                    period = difftime(time2,time1);
                    if ((new_value == old_value)||(period > 5))
                        break;
                    if ( old_value < new_value )
                        under_flow++;
                    old_value=new_value;
                }
                if (period < 5) {
                    pulse_width = (65536 - new_value + under_flow * 65536 + 1) * 0.125;
                    sprintf( status, "*** High level pulse width = %f micro-second ***\n", pulse_width);
                }
                else
                    sprintf( status, "*** Measure inappropriate pulse width ***\n");
                break;
            case 0x33:
                CTR_8554_ClkSrc_Config(card, 1, 0);
                CTR_8554_CK1_Config(card, 0);
                CTR_8554_ClkSrc_Config(card, 2, 2);
                CTR_8554_ClkSrc_Config(card, 3, 1);
                CTR_Setup(card, 1, 2, 65535, 0);
                CTR_Setup(card, 2, 2, 4, 0);
                CTR_Setup(card, 3, 0, 65535, 0);
                usleep(1000000);
                CTR_Read(card, 1, &value);
                period = 65535 - (value&0xffff);
                frequency = period * 2000000 / 65535;
                sprintf( status, "Measured Signal Frequency = %f Hz.\n",frequency);
                break;
            default:
                sprintf( status, " Unknown choice , please select again \n" );
                break;
        }
    }while( func != 0x1b );
    if (card>=0)
        Release_Card(card);
    return 0;
}

variable_types: func char

variable_types:
value
unsigned long

variable_types: frequency float

variable_types: period double


decompiled_code:
int signal_handler2()
{
return puts("\nfinish");
}

canonical_decompiled_code:
int func0()
{
return puts("\nfinish");
}

original_code:
void signal_handler2(int sig_no)
{
     printf("\nfinish\n");
}


decompiled_code:
__int64 __fastcall write_to_file(const void *a1)
{
fwrite(a1, 2uLL, data_size, fp);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1)
{
fwrite(a1, 2uLL, data_size, fp);
return 1LL;
}

original_code:
BOOLEAN write_to_file(U16 *buf)
{
    fwrite(buf, sizeof(U16), data_size, fp);
    return TRUE;
}

canonical_original_code:
enum BOOLEAN write_to_file(unsigned short *buf)
{
    fwrite(buf, sizeof(U16), data_size, fp);
    return TRUE;
}

variable_types:
buf
unsigned short *

return_type: enum BOOLEAN { FALSE=0, TRUE=1 }


decompiled_code:
int signal_handler()
{
__int16 v0; // ax
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
DI_AsyncMultiBufferNextReady((unsigned __int16)card, v2, &viewidx[j % 0x2710uLL]);
v0 = viewidx[j % 0x2710uLL];
if ( v0 )
{
if ( v0 == 1 )
write_to_file(&in_buf2);
}
else
{
write_to_file(&in_buf);
}
DI_AsyncDblBufferTransfer((unsigned __int16)card, 0LL);
DI_AsyncDblBufferOverrun((unsigned __int16)card, 0LL, &sts);
if ( sts == 1 )
{
++t;
DI_AsyncDblBufferOverrun((unsigned __int16)card, 1LL, &sts);
}
result = printf("\roverrun:%d cb:%d", t, j);
++j;
return result;
}

canonical_decompiled_code:
int func2()
{
__int16 v0; // ax
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
DI_AsyncMultiBufferNextReady((unsigned __int16)card, v2, &viewidx[j % 0x2710uLL]);
v0 = viewidx[j % 0x2710uLL];
if ( v0 )
{
if ( v0 == 1 )
func1(&in_buf2);
}
else
{
func1(&in_buf);
}
DI_AsyncDblBufferTransfer((unsigned __int16)card, 0LL);
DI_AsyncDblBufferOverrun((unsigned __int16)card, 0LL, &sts);
if ( sts == 1 )
{
++t;
DI_AsyncDblBufferOverrun((unsigned __int16)card, 1LL, &sts);
}
result = printf("\roverrun:%d cb:%d", t, j);
++j;
return result;
}

original_code:
void signal_handler(int sig_no)
{
    BOOLEAN HalfReady;
    HalfReady=0;
    DI_AsyncMultiBufferNextReady(card, &HalfReady, &viewidx[j%10000]);
    switch(viewidx[j%10000]) {
        case 0:
            write_to_file( in_buf );
            break;
        case 1:
            write_to_file( in_buf2 );
            break;
    }
    DI_AsyncDblBufferTransfer(card, ((void *)0));
    DI_AsyncDblBufferOverrun(card, 0, &sts);
    if(sts==1) {
        t++;
        DI_AsyncDblBufferOverrun(card, 1, &sts);
    }
    printf("\roverrun:%d cb:%d", t, j);
    j++;
}

canonical_original_code:
void signal_handler(int sig_no)
{
    enum BOOLEAN HalfReady;
    HalfReady=0;
    DI_AsyncMultiBufferNextReady(card, &HalfReady, &viewidx[j%10000]);
    switch(viewidx[j%10000]) {
        case 0:
            write_to_file( in_buf );
            break;
        case 1:
            write_to_file( in_buf2 );
            break;
    }
    DI_AsyncDblBufferTransfer(card, ((void *)0));
    DI_AsyncDblBufferOverrun(card, 0, &sts);
    if(sts==1) {
        t++;
        DI_AsyncDblBufferOverrun(card, 1, &sts);
    }
    printf("\roverrun:%d cb:%d", t, j);
    j++;
}

variable_types:
HalfReady
enum BOOLEAN { FALSE=0, TRUE=1 }