decompiled_code:
__int64 __fastcall Person_cmp(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 4);
result = 1LL;
if ( *(_DWORD *)(a1 + 4) <= v2 )
result = (unsigned int)-(*(_DWORD *)(a1 + 4) != v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 4);
result = 1LL;
if ( *(_DWORD *)(a1 + 4) <= v2 )
result = (unsigned int)-(*(_DWORD *)(a1 + 4) != v2);
return result;
}

original_code:
int Person_cmp(Person *p1, Person *p2) {
    if (p1->age > p2->age) {
        return 1;
    }
    if (p1->age == p2->age) {
        return 0;
    }
    return -1;
}

canonical_original_code:
int Person_cmp(struct Person *p1, struct Person *p2) {
    if (p1->age > p2->age) {
        return 1;
    }
    if (p1->age == p2->age) {
        return 0;
    }
    return -1;
}


repo: josmardias/linkedlist

decompiled_code:
bool __fastcall Person_query(_DWORD *a1, int a2)
{
return *a1 == a2;
}

canonical_decompiled_code:
bool __fastcall func1(_DWORD *a1, int a2)
{
return *a1 == a2;
}

original_code:
_Bool Person_query(Person *p, int id) {
    return p->id == id;
}

canonical_original_code:
_Bool Person_query(struct Person *p, int id) {
    return p->id == id;
}

variable_types:
p
struct Person { int id; int age; } *

variable_types: id int


decompiled_code:
int __fastcall Person_print(unsigned int *a1)
{
if ( !a1 )
puts("nil\n");
return printf("id: %d, age: %d\n", *a1, a1[1]);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int *a1)
{
if ( !a1 )
puts("nil\n");
return printf("id: %d, age: %d\n", *a1, a1[1]);
}

original_code:
void Person_print(Person *p) {
    if(!p)
        puts("nil\n");
    printf("id: %d, age: %d\n", p->id, p->age);
}

canonical_original_code:
void Person_print(struct Person *p) {
    if(!p)
        puts("nil\n");
    printf("id: %d, age: %d\n", p->id, p->age);
}

return_type: void


decompiled_code:
__int64 Person_newlist()
{
return List_new(Person_cmp, Person_print, Person_query);
}

canonical_decompiled_code:
__int64 func3()
{
return List_new(Person_cmp, Person_print, Person_query);
}

original_code:
List * Person_newlist() {
    List *l = List_new(
        (int (*)(void *, void *))Person_cmp,
        (void (*)(void *))Person_print,
        (_Bool (*)(void *, void *))Person_query
        );
    return l;
}

canonical_original_code:
struct List *Person_newlist() {
    struct List *l = List_new(
        (int (*)(void *, void *))Person_cmp,
        (void (*)(void *))Person_print,
        (_Bool (*)(void *, void *))Person_query
        );
    return l;
}

variable_types: struct List { int length; struct node { void *info; struct node *next; } *head; int (*cmp)(void *, void *); void (*print)(void *); _Bool (*query)(void *, void *); } *

user_defined_types: struct List { int length; struct node *head; int (*cmp)(void *, void *); void (*print)(void *); _Bool (*query)(void *, void *); }


die

repo: jossk/fmio-dos

decompiled_code:
void __fastcall __noreturn die(int a1)
{
radio_drv_free();
radio_cleanup();
exit(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(int a1)
{
radio_drv_free();
radio_cleanup();
exit(a1);
}

original_code:
void
die(int sig) {
 radio_drv_free();
 radio_cleanup();
 exit(sig);
}

variable_types: sig int


usage

decompiled_code:
void __noreturn usage()
{
unsigned int v0; // er12
__int64 v1; // rax
char v2[392]; // [rsp+0h] [rbp-188h] BYREF

strcpy(
v2,
"Usage: %s [-d driver] [-f frequency] [-i] [-m] [-s] [-v volume]\n"
"\t%s [-d driver] -S [-l begin] [-h end] [-c count]\n"
"\t%s [-d driver] -W frequency\n"
"\t%s -D - detect driver\n"
"\n"
"\t-f frequency in Mhz, -f 98.0 for example\n"
"\t-i information\n"
"\t-m mono\n"
"\t-s stat\n"
"\t-v volume, -v 0 set tuner off\n"
"\t-S scan -l start frequency, -h end frequency\n"
"\t-c number of probes for each scanned frequency\n"
"\t-W search\n");
printf("%s version %s\n", (const char *)pn, "2.0.8");
printf("Default driver: ");
v0 = radio_info_port();
v1 = radio_info_name();
radio_info_show(stdout, v1, v0);
printf(v2, pn, pn, pn, pn);
die(0);
}

canonical_decompiled_code:
void __noreturn func1()
{
unsigned int v0; // er12
__int64 v1; // rax
char v2[392]; // [rsp+0h] [rbp-188h] BYREF

strcpy(
v2,
"Usage: %s [-d driver] [-f frequency] [-i] [-m] [-s] [-v volume]\n"
"\t%s [-d driver] -S [-l begin] [-h end] [-c count]\n"
"\t%s [-d driver] -W frequency\n"
"\t%s -D - detect driver\n"
"\n"
"\t-f frequency in Mhz, -f 98.0 for example\n"
"\t-i information\n"
"\t-m mono\n"
"\t-s stat\n"
"\t-v volume, -v 0 set tuner off\n"
"\t-S scan -l start frequency, -h end frequency\n"
"\t-c number of probes for each scanned frequency\n"
"\t-W search\n");
printf("%s version %s\n", (const char *)pn, "2.0.8");
printf("Default driver: ");
v0 = radio_info_port();
v1 = radio_info_name();
radio_info_show(stdout, v1, v0);
printf(v2, pn, pn, pn, pn);
func0(0);
}

original_code:
void
usage(void) {
 const char usage_string[] =
  "Usage:  %s [-d driver] [-f frequency] [-i] [-m] [-s] [-v volume]\n"
  "\t%s [-d driver] -S [-l begin] [-h end] [-c count]\n"
  "\t%s [-d driver] -W frequency\n"
  "\t%s -D - detect driver\n\n"
  "\t-f frequency in Mhz, -f 98.0 for example\n"
  "\t-i information\n"
  "\t-m mono\n"
  "\t-s stat\n"
  "\t-v volume, -v 0 set tuner off\n"
  "\t-S scan -l start frequency, -h end frequency\n"
  "\t-c number of probes for each scanned frequency\n"
  "\t-W search\n"
 ;
 printf("%s version %s\n", pn, "2.0.8");
 printf("Default driver: ");
 radio_info_show(stdout, radio_info_name(), radio_info_port());
 printf(usage_string, pn, pn, pn, pn);
 die(0);
}

canonical_original_code:
void usage() {
 char usage_string[] = "Usage:  %s [-d driver] [-f frequency] [-i] [-m] [-s] [-v volume]\n"
  "\t%s [-d driver] -S [-l begin] [-h end] [-c count]\n"
  "\t%s [-d driver] -W frequency\n"
  "\t%s -D - detect driver\n\n"
  "\t-f frequency in Mhz, -f 98.0 for example\n"
  "\t-i information\n"
  "\t-m mono\n"
  "\t-s stat\n"
  "\t-v volume, -v 0 set tuner off\n"
  "\t-S scan -l start frequency, -h end frequency\n"
  "\t-c number of probes for each scanned frequency\n"
  "\t-W search\n";
 printf("%s version %s\n", pn, "2.0.8");
 printf("Default driver: ");
 radio_info_show(stdout, radio_info_name(), radio_info_port());
 printf(usage_string, pn, pn, pn, pn);
 die(0);
}


decompiled_code:
__int64 __fastcall invalid_driver_error(const char *a1)
{
warnx("Invalid driver `%s', using default `%s'", a1, "sf4r");
radio_drv_free();
return radio_drv_init("sf4r");
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
warnx("Invalid driver `%s', using default `%s'", a1, "sf4r");
radio_drv_free();
return radio_drv_init("sf4r");
}

original_code:
void
invalid_driver_error(char *invl_drv) {
 warnx("Invalid driver `%s', using default `%s'", invl_drv, "sf4r");
 radio_drv_free();
 radio_drv_init("sf4r");
}


gouser

decompiled_code:
__int64 gouser()
{
__uid_t v0; // eax
__uid_t v2; // eax

v0 = getuid();
if ( seteuid(v0) >= 0 )
return 0LL;
v2 = getuid();
warn("set effective user %d privs error", v2);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func3()
{
__uid_t v0; // eax
__uid_t v2; // eax

v0 = getuid();
if ( seteuid(v0) >= 0 )
return 0LL;
v2 = getuid();
warn("set effective user %d privs error", v2);
return 0xFFFFFFFFLL;
}

original_code:
int
gouser(void) {
 if (seteuid(getuid()) < 0) {
  warn("set effective user %d privs error", getuid());
  return -1;
 }
 return 0;
}

canonical_original_code:
int gouser() {
 if (seteuid(getuid()) < 0) {
  warn("set effective user %d privs error", getuid());
  return -1;
 }
 return 0;
}

return_type: int


goroot

decompiled_code:
__int64 goroot()
{
if ( seteuid(0) >= 0 )
return 0LL;
warn("set effective root privs error");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func4()
{
if ( seteuid(0) >= 0 )
return 0LL;
warn("set effective root privs error");
return 0xFFFFFFFFLL;
}

original_code:
int
goroot(void) {
 if (seteuid(0) < 0) {
  warn("set effective root privs error");
  return -1;
 }
 return 0;
}

canonical_original_code:
int goroot() {
 if (seteuid(0) < 0) {
  warn("set effective root privs error");
  return -1;
 }
 return 0;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rdx
char *v5; // rax
const char *v6; // r13
unsigned int v7; // er13
int v8; // er15
int v9; // ebx
int v10; // er14
const char **v11; // rsi
int v12; // eax
double v13; // xmm0_8
__int16 v14; // dx
unsigned int v15; // eax
unsigned int v16; // ebp
__int64 v17; // rdx
const char *v18; // rsi
unsigned __int64 v19; // rdi
int v20; // eax
__int64 v21; // rdx
__int64 v22; // rdx
int v24; // ebx
__int64 v25; // rdx
__int64 v26; // rdx
int v27; // eax
__int64 v28; // rdx
__int64 v29; // rdi
int v30; // ebx
unsigned int v31; // ebp
unsigned int v32; // er12
__int64 v33; // rax
int v34; // eax
int v35; // eax
int v36; // er12
int v37; // eax
int v38; // ebp
unsigned int v39; // er12
__int64 v40; // rax
int v41; // [rsp+4h] [rbp-44h]
unsigned int v42; // [rsp+8h] [rbp-40h]
unsigned __int16 v43; // [rsp+Eh] [rbp-3Ah]

if ( (int)gouser(argc, argv, envp) < 0 )
return -1;
pn = (__int64)strrchr(*argv, 47);
v4 = (const char *)(pn + 1);
if ( !pn )
v4 = *argv;
pn = (__int64)v4;
radio_init();
v5 = getenv("FMTUNER");
v6 = v5;
if ( v5 )
{
if ( !*v5 )
v6 = "sf4r";
}
else
{
v6 = "sf4r";
}
if ( (unsigned int)radio_drv_init(v6) == -1 )
invalid_driver_error(v6);
if ( argc <= 1 )
LABEL_115:
usage();
v7 = 1;
LOWORD(v8) = 0;
LOWORD(v41) = 0;
v9 = 0;
v10 = 0;
v42 = 0;
v43 = 10630;
while ( 2 )
{
v11 = argv;
v12 = getopt(argc, (char *const *)argv, "c:Dd:f:h:il:mSsv:W:X:x:");
if ( v12 != -1 )
{
switch ( v12 )
{
case 'D':
v9 = 512;
continue;
case 'S':
v9 = 256;
continue;
case 'W':
v9 = 1024;
v10 = (int)(atof(optarg) * 100.0);
continue;
case 'c':
v7 = 1;
v15 = strtol(optarg, 0LL, 10);
if ( v15 )
v7 = v15;
continue;
case 'd':
radio_drv_free();
if ( (unsigned int)radio_drv_init(optarg) == -1 )
invalid_driver_error(optarg);
continue;
case 'f':
v13 = strtod(optarg, 0LL);
v14 = 10630;
if ( (unsigned __int16)(int)(v13 * 100.0) )
v14 = (int)(v13 * 100.0);
v9 |= 2u;
v43 = v14;
continue;
case 'h':
v8 = (int)(atof(optarg) * 100.0);
continue;
case 'i':
v9 |= 0x10u;
continue;
case 'l':
v41 = (int)(atof(optarg) * 100.0);
continue;
case 'm':
v9 |= 8u;
continue;
case 's':
v9 |= 1u;
continue;
case 'v':
v9 |= 4u;
v42 = strtoul(optarg, 0LL, 10);
continue;
default:
goto LABEL_115;
}
}
break;
}
if ( (_BYTE)v9 )
LOWORD(v9) = (unsigned __int8)v9;
if ( (unsigned int)radio_info_root() && (int)goroot() < 0 || (int)radio_get_port() < 0 )
goto LABEL_54;
v16 = radio_test_port();
if ( v16 != 1 )
{
fprintf(stderr, "%s: card not found: ", (const char *)pn);
v39 = radio_info_port();
v40 = radio_info_name();
radio_info_show(stderr, v40, v39);
radio_free_port();
goto LABEL_54;
}
if ( (unsigned int)radio_info_root() && (int)gouser((unsigned int)argc, v11, v17) < 0 )
LABEL_54:
die(1);
signal(2, (__sighandler_t)die);
signal(1, (__sighandler_t)die);
v18 = (const char *)die;
v19 = 15LL;
signal(15, (__sighandler_t)die);
v20 = v9 & 0xFF00;
if ( v20 == 512 )
{
if ( (int)goroot() < 0 )
goto LABEL_54;
radio_detect();
if ( (int)gouser(15LL, die, v26) < 0 )
goto LABEL_54;
goto LABEL_42;
}
if ( (v9 & 0xFF00u) > 0x200 )
{
if ( v20 == 1024 )
{
if ( (unsigned int)radio_info_root() && (int)goroot() < 0 )
goto LABEL_54;
if ( v10 < 0 )
{
v19 = 0LL;
v18 = (const char *)(unsigned __int16)-(__int16)v10;
v24 = (unsigned __int16)radio_search(0LL, v18);
}
else
{
v18 = (const char *)(unsigned __int16)v10;
v19 = 1LL;
v24 = (unsigned __int16)radio_search(1LL, (unsigned __int16)v10);
}
if ( (unsigned int)radio_info_root() && (int)gouser(v19, v18, v25) < 0 )
goto LABEL_54;
if ( (_WORD)v24 )
{
v18 = (const char *)pn;
v19 = (unsigned __int64)"%s: %.2f MHz\n";
printf("%s: %.2f MHz\n", (const char *)pn, (float)((float)v24 / 100.0));
}
}
goto LABEL_42;
}
if ( (v9 & 0xFF00) != 0 )
{
if ( v20 == 256 )
{
if ( (unsigned int)radio_info_root() && (int)goroot() < 0 )
goto LABEL_54;
v19 = (unsigned __int16)v41;
v18 = (const char *)(unsigned __int16)v8;
radio_scan((unsigned __int16)v41, (unsigned __int16)v8, v7);
if ( (unsigned int)radio_info_root() )
{
if ( (int)gouser((unsigned __int16)v41, (unsigned __int16)v8, v21) < 0 )
goto LABEL_54;
}
}
goto LABEL_42;
}
if ( (unsigned int)radio_info_root() && (int)goroot() < 0 )
goto LABEL_54;
if ( (v9 & 8) != 0 )
radio_set_mono();
v27 = radio_info_policy();
if ( v27 )
{
if ( v27 == 1 )
{
if ( (v9 & 2) == 0 )
goto LABEL_74;
v29 = v42;
if ( (v9 & 4) == 0 )
v29 = (unsigned __int8)radio_info_maxvol(v42);
radio_set_volume(v29);
}
else if ( (v9 & 2) == 0 )
{
goto LABEL_74;
}
goto LABEL_85;
}
if ( (v9 & 2) != 0 )
{
if ( (v9 & 4) != 0 )
v16 = v42;
radio_set_volume(v16);
LABEL_85:
v19 = v43;
radio_set_freq(v43);
}
LABEL_74:
if ( (v9 & 4) != 0 )
{
v19 = v42;
radio_set_volume(v42);
}
if ( (v9 & 1) == 0 )
goto LABEL_77;
v36 = radio_info_stereo();
v37 = radio_info_signal();
v38 = v37;
if ( v36 == -1 )
{
if ( v37 == -1 )
goto LABEL_77;
goto LABEL_111;
}
v18 = "stereo";
if ( !v36 )
v18 = "mono";
printf("%s", v18);
if ( v38 != -1 )
{
printf(" : ");
LABEL_111:
v18 = "signal";
v19 = (unsigned __int64)"%s";
if ( !v38 )
v18 = "noise";
printf("%s", v18);
if ( (v36 & v38) == -1 )
goto LABEL_77;
}
v19 = 10LL;
putchar(10);
LABEL_77:
if ( (v9 & 0x10) != 0 )
{
v30 = (unsigned __int16)radio_info_freq();
v31 = radio_info_volume();
printf("Driver: ");
v32 = radio_info_port();
v33 = radio_info_name();
v19 = (unsigned __int64)stdout;
v18 = (const char *)v33;
radio_info_show(stdout, v33, v32);
if ( (_WORD)v30 )
{
v19 = (unsigned __int64)"Frequency: %.2f MHz\n";
printf("Frequency: %.2f MHz\n", (float)((float)v30 / 100.0));
}
if ( v31 )
{
v18 = (const char *)v31;
v19 = (unsigned __int64)"Volume: %u\n";
printf("Volume: %u\n", v31);
}
v34 = radio_info_signal();
if ( v34 != -1 )
{
v18 = "on";
v19 = (unsigned __int64)"Signal: %s\n";
if ( !v34 )
v18 = "off";
printf("Signal: %s\n", v18);
}
v35 = radio_info_stereo();
if ( v35 != -1 )
{
v18 = "on";
v19 = (unsigned __int64)"Stereo: %s\n";
if ( !v35 )
v18 = "off";
printf("Stereo: %s\n", v18);
}
}
if ( (unsigned int)radio_info_root() )
gouser(v19, v18, v28);
LABEL_42:
if ( (unsigned int)radio_info_root() && (int)goroot() < 0 )
goto LABEL_54;
radio_free_port();
if ( (unsigned int)radio_info_root() )
gouser(v19, v18, v22);
radio_drv_free();
radio_cleanup();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rdx
char *v5; // rax
const char *v6; // r13
unsigned int v7; // er13
int v8; // er15
int v9; // ebx
int v10; // er14
const char **v11; // rsi
int v12; // eax
double v13; // xmm0_8
__int16 v14; // dx
unsigned int v15; // eax
unsigned int v16; // ebp
__int64 v17; // rdx
const char *v18; // rsi
unsigned __int64 v19; // rdi
int v20; // eax
__int64 v21; // rdx
__int64 v22; // rdx
int v24; // ebx
__int64 v25; // rdx
__int64 v26; // rdx
int v27; // eax
__int64 v28; // rdx
__int64 v29; // rdi
int v30; // ebx
unsigned int v31; // ebp
unsigned int v32; // er12
__int64 v33; // rax
int v34; // eax
int v35; // eax
int v36; // er12
int v37; // eax
int v38; // ebp
unsigned int v39; // er12
__int64 v40; // rax
int v41; // [rsp+4h] [rbp-44h]
unsigned int v42; // [rsp+8h] [rbp-40h]
unsigned __int16 v43; // [rsp+Eh] [rbp-3Ah]

if ( (int)func3(argc, argv, envp) < 0 )
return -1;
pn = (__int64)strrchr(*argv, 47);
v4 = (const char *)(pn + 1);
if ( !pn )
v4 = *argv;
pn = (__int64)v4;
radio_init();
v5 = getenv("FMTUNER");
v6 = v5;
if ( v5 )
{
if ( !*v5 )
v6 = "sf4r";
}
else
{
v6 = "sf4r";
}
if ( (unsigned int)radio_drv_init(v6) == -1 )
func2(v6);
if ( argc <= 1 )
LABEL_115:
func1();
v7 = 1;
LOWORD(v8) = 0;
LOWORD(v41) = 0;
v9 = 0;
v10 = 0;
v42 = 0;
v43 = 10630;
while ( 2 )
{
v11 = argv;
v12 = getopt(argc, (char *const *)argv, "c:Dd:f:h:il:mSsv:W:X:x:");
if ( v12 != -1 )
{
switch ( v12 )
{
case 'D':
v9 = 512;
continue;
case 'S':
v9 = 256;
continue;
case 'W':
v9 = 1024;
v10 = (int)(atof(optarg) * 100.0);
continue;
case 'c':
v7 = 1;
v15 = strtol(optarg, 0LL, 10);
if ( v15 )
v7 = v15;
continue;
case 'd':
radio_drv_free();
if ( (unsigned int)radio_drv_init(optarg) == -1 )
func2(optarg);
continue;
case 'f':
v13 = strtod(optarg, 0LL);
v14 = 10630;
if ( (unsigned __int16)(int)(v13 * 100.0) )
v14 = (int)(v13 * 100.0);
v9 |= 2u;
v43 = v14;
continue;
case 'h':
v8 = (int)(atof(optarg) * 100.0);
continue;
case 'i':
v9 |= 0x10u;
continue;
case 'l':
v41 = (int)(atof(optarg) * 100.0);
continue;
case 'm':
v9 |= 8u;
continue;
case 's':
v9 |= 1u;
continue;
case 'v':
v9 |= 4u;
v42 = strtoul(optarg, 0LL, 10);
continue;
default:
goto LABEL_115;
}
}
break;
}
if ( (_BYTE)v9 )
LOWORD(v9) = (unsigned __int8)v9;
if ( (unsigned int)radio_info_root() && (int)func4() < 0 || (int)radio_get_port() < 0 )
goto LABEL_54;
v16 = radio_test_port();
if ( v16 != 1 )
{
fprintf(stderr, "%s: card not found: ", (const char *)pn);
v39 = radio_info_port();
v40 = radio_info_name();
radio_info_show(stderr, v40, v39);
radio_free_port();
goto LABEL_54;
}
if ( (unsigned int)radio_info_root() && (int)func3((unsigned int)argc, v11, v17) < 0 )
LABEL_54:
func0(1);
signal(2, (__sighandler_t)die);
signal(1, (__sighandler_t)die);
v18 = (const char *)die;
v19 = 15LL;
signal(15, (__sighandler_t)die);
v20 = v9 & 0xFF00;
if ( v20 == 512 )
{
if ( (int)func4() < 0 )
goto LABEL_54;
radio_detect();
if ( (int)func3(15LL, die, v26) < 0 )
goto LABEL_54;
goto LABEL_42;
}
if ( (v9 & 0xFF00u) > 0x200 )
{
if ( v20 == 1024 )
{
if ( (unsigned int)radio_info_root() && (int)func4() < 0 )
goto LABEL_54;
if ( v10 < 0 )
{
v19 = 0LL;
v18 = (const char *)(unsigned __int16)-(__int16)v10;
v24 = (unsigned __int16)radio_search(0LL, v18);
}
else
{
v18 = (const char *)(unsigned __int16)v10;
v19 = 1LL;
v24 = (unsigned __int16)radio_search(1LL, (unsigned __int16)v10);
}
if ( (unsigned int)radio_info_root() && (int)func3(v19, v18, v25) < 0 )
goto LABEL_54;
if ( (_WORD)v24 )
{
v18 = (const char *)pn;
v19 = (unsigned __int64)"%s: %.2f MHz\n";
printf("%s: %.2f MHz\n", (const char *)pn, (float)((float)v24 / 100.0));
}
}
goto LABEL_42;
}
if ( (v9 & 0xFF00) != 0 )
{
if ( v20 == 256 )
{
if ( (unsigned int)radio_info_root() && (int)func4() < 0 )
goto LABEL_54;
v19 = (unsigned __int16)v41;
v18 = (const char *)(unsigned __int16)v8;
radio_scan((unsigned __int16)v41, (unsigned __int16)v8, v7);
if ( (unsigned int)radio_info_root() )
{
if ( (int)func3((unsigned __int16)v41, (unsigned __int16)v8, v21) < 0 )
goto LABEL_54;
}
}
goto LABEL_42;
}
if ( (unsigned int)radio_info_root() && (int)func4() < 0 )
goto LABEL_54;
if ( (v9 & 8) != 0 )
radio_set_mono();
v27 = radio_info_policy();
if ( v27 )
{
if ( v27 == 1 )
{
if ( (v9 & 2) == 0 )
goto LABEL_74;
v29 = v42;
if ( (v9 & 4) == 0 )
v29 = (unsigned __int8)radio_info_maxvol(v42);
radio_set_volume(v29);
}
else if ( (v9 & 2) == 0 )
{
goto LABEL_74;
}
goto LABEL_85;
}
if ( (v9 & 2) != 0 )
{
if ( (v9 & 4) != 0 )
v16 = v42;
radio_set_volume(v16);
LABEL_85:
v19 = v43;
radio_set_freq(v43);
}
LABEL_74:
if ( (v9 & 4) != 0 )
{
v19 = v42;
radio_set_volume(v42);
}
if ( (v9 & 1) == 0 )
goto LABEL_77;
v36 = radio_info_stereo();
v37 = radio_info_signal();
v38 = v37;
if ( v36 == -1 )
{
if ( v37 == -1 )
goto LABEL_77;
goto LABEL_111;
}
v18 = "stereo";
if ( !v36 )
v18 = "mono";
printf("%s", v18);
if ( v38 != -1 )
{
printf(" : ");
LABEL_111:
v18 = "signal";
v19 = (unsigned __int64)"%s";
if ( !v38 )
v18 = "noise";
printf("%s", v18);
if ( (v36 & v38) == -1 )
goto LABEL_77;
}
v19 = 10LL;
putchar(10);
LABEL_77:
if ( (v9 & 0x10) != 0 )
{
v30 = (unsigned __int16)radio_info_freq();
v31 = radio_info_volume();
printf("Driver: ");
v32 = radio_info_port();
v33 = radio_info_name();
v19 = (unsigned __int64)stdout;
v18 = (const char *)v33;
radio_info_show(stdout, v33, v32);
if ( (_WORD)v30 )
{
v19 = (unsigned __int64)"Frequency: %.2f MHz\n";
printf("Frequency: %.2f MHz\n", (float)((float)v30 / 100.0));
}
if ( v31 )
{
v18 = (const char *)v31;
v19 = (unsigned __int64)"Volume: %u\n";
printf("Volume: %u\n", v31);
}
v34 = radio_info_signal();
if ( v34 != -1 )
{
v18 = "on";
v19 = (unsigned __int64)"Signal: %s\n";
if ( !v34 )
v18 = "off";
printf("Signal: %s\n", v18);
}
v35 = radio_info_stereo();
if ( v35 != -1 )
{
v18 = "on";
v19 = (unsigned __int64)"Stereo: %s\n";
if ( !v35 )
v18 = "off";
printf("Stereo: %s\n", v18);
}
}
if ( (unsigned int)radio_info_root() )
func3(v19, v18, v28);
LABEL_42:
if ( (unsigned int)radio_info_root() && (int)func4() < 0 )
goto LABEL_54;
radio_free_port();
if ( (unsigned int)radio_info_root() )
func3(v19, v18, v22);
radio_drv_free();
radio_cleanup();
return 0;
}

original_code:
int
main(int argc, char **argv) {
 int optchar;
 int search = 0;
 char *drv = ((void *)0);
 u_int16_t freq = 10630;
 int volu = 0;
 u_int16_t action = 0x0000;
 u_int16_t lower = 0, higher = 0;
 u_int32_t cycle = 1;
 if (gouser() < 0)
  return -1;
 pn = strrchr(argv[0], '/');
 if (pn == ((void *)0))
  pn = argv[0];
 else
  pn++;
 radio_init();
 drv = getenv("FMTUNER");
 if (drv == ((void *)0) || *drv == '\0')
  drv = "sf4r";
 if (radio_drv_init(drv) == -1)
  invalid_driver_error(drv);
 if (argc < 2)
  usage();
 while ((optchar = getopt(argc, argv, "c:Dd:f:h:il:mSsv:W:X:x:")) != -1) {
  switch (optchar) {
  case 'c':
   if ((cycle = strtol(optarg, (char **)((void *)0), 10)) == 0)
    cycle = 1;
   break;
  case 'D':
   action = 0x0200;
   break;
  case 'd':
   radio_drv_free();
   if (radio_drv_init(optarg) == -1)
    invalid_driver_error(optarg);
   break;
  case 'f':
   freq = 100.0 * strtod(optarg, (char **)((void *)0));
   if (freq == 0)
    freq = 10630;
   action |= 0x0002;
   break;
  case 'h':
   higher = atof(optarg) * 100;
   break;
  case 'i':
   action |= 0x0010;
   break;
  case 'l':
   lower = atof(optarg) * 100;
   break;
  case 'm':
   action |= 0x0008;
   break;
  case 'S':
   action = 0x0100;
   break;
  case 's':
   action |= 0x0001;
   break;
  case 'v':
   volu = strtoul(optarg, (char **)((void *)0), 10);
   action |= 0x0004;
   break;
  case 'W':
   action = 0x0400;
   search = atof(optarg) * 100;
   break;
  default:
   usage();
  }
 }
 if (action & 0x00FF) action &= 0x00FF;
 if (radio_info_root())
  if (goroot() < 0)
   die(1);
 if (radio_get_port() < 0)
  die(1);
 if (radio_test_port() != 1) {
  fprintf(stderr, "%s: card not found: ", pn);
  radio_info_show(stderr, radio_info_name(), radio_info_port());
  radio_free_port();
  die(1);
 }
 if (radio_info_root())
  if (gouser() < 0)
   die(1);
 signal(2 , die);
 signal(1 , die);
 signal(15, die);
 switch (action & ~0x00FF) {
 case 0x0000:
  if (radio_info_root())
   if (goroot() < 0)
    die(1);
  if (action & 0x0008)
   radio_set_mono();
  switch (radio_info_policy()) {
  case 0:
   if (action & 0x0002)
    radio_set_volume(action & 0x0004 ? volu : 1);
   break;
  case 1:
   if (action & 0x0002)
    radio_set_volume(action & 0x0004 ?
      volu : radio_info_maxvol());
   break;
  }
  if (action & 0x0002)
   radio_set_freq(freq);
  if (action & 0x0004)
   radio_set_volume(volu);
  if (action & 0x0001) {
   int st = radio_info_stereo();
   int si = radio_info_signal();
   if (st != -1)
    printf("%s", st ? "stereo" : "mono");
   if (st != -1 && si != -1)
    printf(" : ");
   if (si != -1)
    printf("%s", si ? "signal" : "noise");
   if (st != -1 || si != -1)
    printf("\n");
  }
  if (action & 0x0010) {
   u_int16_t f = radio_info_freq();
   int v = radio_info_volume();
   printf("Driver: ");
   radio_info_show(stdout, radio_info_name(), radio_info_port());
   if (f)
    printf("Frequency: %.2f MHz\n", (float) f / 100);
   if (v)
    printf("Volume: %u\n", v);
   v = radio_info_signal();
   if (v != -1)
    printf("Signal: %s\n", v ? "on" : "off");
   v = radio_info_stereo();
   if (v != -1)
    printf("Stereo: %s\n", v ? "on" : "off");
  }
  if (radio_info_root())
   gouser();
  break;
 case 0x0200:
  if (goroot() < 0)
   die(1);
  radio_detect();
  if (gouser() < 0)
   die(1);
  break;
 case 0x0100:
  if (radio_info_root())
   if (goroot() < 0)
    die(1);
  radio_scan(lower, higher, cycle);
  if (radio_info_root())
   if (gouser() < 0)
    die(1);
  break;
 case 0x0400:
  if (radio_info_root())
   if (goroot() < 0)
    die(1);
  if (search < 0)
   freq = radio_search(0, -1 * search);
  else
   freq = radio_search(1, search);
  if (radio_info_root())
   if (gouser() < 0)
    die(1);
  if (freq)
   printf("%s: %.2f MHz\n", pn, (float)freq / 100);
  break;
 default:
  break;
 }
 if (radio_info_root())
  if (goroot() < 0)
   die(1);
 radio_free_port();
 if (radio_info_root())
  gouser();
 radio_drv_free();
 radio_cleanup();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 int optchar;
 int search = 0;
 char *drv = ((void *)0);
 unsigned int freq = 10630;
 int volu = 0;
 unsigned int action = 0x0000;
 unsigned int lower = 0; unsigned int higher = 0;
 unsigned int cycle = 1;
 if (gouser() < 0)
  return -1;
 pn = strrchr(argv[0], '/');
 if (pn == ((void *)0))
  pn = argv[0];
 else
  pn++;
 radio_init();
 drv = getenv("FMTUNER");
 if (drv == ((void *)0) || *drv == '\0')
  drv = "sf4r";
 if (radio_drv_init(drv) == -1)
  invalid_driver_error(drv);
 if (argc < 2)
  usage();
 while ((optchar = getopt(argc, argv, "c:Dd:f:h:il:mSsv:W:X:x:")) != -1) {
  switch (optchar) {
  case 'c':
   if ((cycle = strtol(optarg, (char **)((void *)0), 10)) == 0)
    cycle = 1;
   break;
  case 'D':
   action = 0x0200;
   break;
  case 'd':
   radio_drv_free();
   if (radio_drv_init(optarg) == -1)
    invalid_driver_error(optarg);
   break;
  case 'f':
   freq = 100.0 * strtod(optarg, (char **)((void *)0));
   if (freq == 0)
    freq = 10630;
   action |= 0x0002;
   break;
  case 'h':
   higher = atof(optarg) * 100;
   break;
  case 'i':
   action |= 0x0010;
   break;
  case 'l':
   lower = atof(optarg) * 100;
   break;
  case 'm':
   action |= 0x0008;
   break;
  case 'S':
   action = 0x0100;
   break;
  case 's':
   action |= 0x0001;
   break;
  case 'v':
   volu = strtoul(optarg, (char **)((void *)0), 10);
   action |= 0x0004;
   break;
  case 'W':
   action = 0x0400;
   search = atof(optarg) * 100;
   break;
  default:
   usage();
  }
 }
 if (action & 0x00FF) action &= 0x00FF;
 if (radio_info_root())
  if (goroot() < 0)
   die(1);
 if (radio_get_port() < 0)
  die(1);
 if (radio_test_port() != 1) {
  fprintf(stderr, "%s: card not found: ", pn);
  radio_info_show(stderr, radio_info_name(), radio_info_port());
  radio_free_port();
  die(1);
 }
 if (radio_info_root())
  if (gouser() < 0)
   die(1);
 signal(2 , die);
 signal(1 , die);
 signal(15, die);
 switch (action & ~0x00FF) {
 case 0x0000:
  if (radio_info_root())
   if (goroot() < 0)
    die(1);
  if (action & 0x0008)
   radio_set_mono();
  switch (radio_info_policy()) {
  case 0:
   if (action & 0x0002)
    radio_set_volume(action & 0x0004 ? volu : 1);
   break;
  case 1:
   if (action & 0x0002)
    radio_set_volume(action & 0x0004 ?
      volu : radio_info_maxvol());
   break;
  }
  if (action & 0x0002)
   radio_set_freq(freq);
  if (action & 0x0004)
   radio_set_volume(volu);
  if (action & 0x0001) {
   int st = radio_info_stereo();
   int si = radio_info_signal();
   if (st != -1)
    printf("%s", st ? "stereo" : "mono");
   if (st != -1 && si != -1)
    printf(" : ");
   if (si != -1)
    printf("%s", si ? "signal" : "noise");
   if (st != -1 || si != -1)
    printf("\n");
  }
  if (action & 0x0010) {
   unsigned int f = radio_info_freq();
   int v = radio_info_volume();
   printf("Driver: ");
   radio_info_show(stdout, radio_info_name(), radio_info_port());
   if (f)
    printf("Frequency: %.2f MHz\n", (float) f / 100);
   if (v)
    printf("Volume: %u\n", v);
   v = radio_info_signal();
   if (v != -1)
    printf("Signal: %s\n", v ? "on" : "off");
   v = radio_info_stereo();
   if (v != -1)
    printf("Stereo: %s\n", v ? "on" : "off");
  }
  if (radio_info_root())
   gouser();
  break;
 case 0x0200:
  if (goroot() < 0)
   die(1);
  radio_detect();
  if (gouser() < 0)
   die(1);
  break;
 case 0x0100:
  if (radio_info_root())
   if (goroot() < 0)
    die(1);
  radio_scan(lower, higher, cycle);
  if (radio_info_root())
   if (gouser() < 0)
    die(1);
  break;
 case 0x0400:
  if (radio_info_root())
   if (goroot() < 0)
    die(1);
  if (search < 0)
   freq = radio_search(0, -1 * search);
  else
   freq = radio_search(1, search);
  if (radio_info_root())
   if (gouser() < 0)
    die(1);
  if (freq)
   printf("%s: %.2f MHz\n", pn, (float)freq / 100);
  break;
 default:
  break;
 }
 if (radio_info_root())
  if (goroot() < 0)
   die(1);
 radio_free_port();
 if (radio_info_root())
  gouser();
 radio_drv_free();
 radio_cleanup();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: optchar int

variable_types: search int

variable_types:
drv
char *

variable_types:
freq
unsigned int

variable_types: volu int

variable_types:
action
unsigned int

variable_types:
lower
unsigned int

variable_types:
higher
unsigned int

variable_types:
cycle
unsigned int

variable_types: st int

variable_types: si int

variable_types:
f
unsigned int

variable_types: v int


decompiled_code:
__int64 __fastcall lm700x_encode_ref(unsigned __int16 a1)
{
__int64 result; // rax

result = 0x200000LL;
if ( a1 > 0x23u )
result = (unsigned __int8)((unsigned __int16)(a1 - 36) < 0x27u) << 22;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1)
{
__int64 result; // rax

result = 0x200000LL;
if ( a1 > 0x23u )
result = (unsigned __int8)((unsigned __int16)(a1 - 36) < 0x27u) << 22;
return result;
}

original_code:
u_int32_t
lm700x_encode_ref(u_int16_t rf) {
 u_int32_t ret;
 if (rf < 36)
  ret = (2 << 20);
 else if (rf > 35 && rf < 75)
  ret = (4 << 20);
 else
  ret = (0 << 20);
 return ret;
}

canonical_original_code:
unsigned int lm700x_encode_ref(unsigned int rf) {
 unsigned int ret;
 if (rf < 36)
  ret = (2 << 20);
 else if (rf > 35 && rf < 75)
  ret = (4 << 20);
 else
  ret = (0 << 20);
 return ret;
}

variable_types:
rf
unsigned int

variable_types:
ret
unsigned int

return_type: unsigned int


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

result = 100LL;
if ( a1 )
{
result = 50LL;
if ( a1 == 0x200000 )
result = 25LL;
}
return result;
}

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

result = 100LL;
if ( a1 )
{
result = 50LL;
if ( a1 == 0x200000 )
result = 25LL;
}
return result;
}

original_code:
u_int16_t
lm700x_decode_ref(u_int32_t rf) {
 switch (rf) {
 case (2 << 20):
  return 25;
 case (4 << 20):
  return 50;
 case (0 << 20):
  return 100;
 }
 return 50;
}

canonical_original_code:
unsigned int lm700x_decode_ref(unsigned int rf) {
 switch (rf) {
 case (2 << 20):
  return 25;
 case (4 << 20):
  return 50;
 case (0 << 20):
  return 100;
 }
 return 50;
}


decompiled_code:
__int64 __fastcall lm700x_encode_freq(__int64 a1, int a2)
{
unsigned __int16 v2; // ax
int v3; // ecx

v2 = lm700x_decode_ref(a2);
return 2 * v3 / (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
unsigned __int16 v2; // ax
int v3; // ecx

v2 = func1(a2);
return 2 * v3 / (unsigned int)v2;
}

original_code:
u_int32_t
lm700x_encode_freq(u_int16_t f, u_int32_t rf) {
 u_int32_t reg;
 reg = f + 1070;
 reg *= 10;
 reg /= lm700x_decode_ref(rf);
 return reg;
}

canonical_original_code:
unsigned int lm700x_encode_freq(unsigned int f, unsigned int rf) {
 unsigned int reg;
 reg = f + 1070;
 reg *= 10;
 reg /= lm700x_decode_ref(rf);
 return reg;
}

variable_types:
reg
unsigned int


decompiled_code:
__int64 __fastcall pt2254a_encode_volume(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // eax
int v4; // edx
signed int v5; // edx

result = 2112LL;
if ( a1 )
{
if ( a2 / 3 < a1 )
{
if ( 2 * a2 / 3 < a1 )
{
v3 = 18 * a1 / a2;
v4 = 18;
}
else
{
v3 = 42 * a1 / a2;
v4 = 34;
}
}
else
{
v3 = 144 * a1 / a2;
v4 = 68;
}
v5 = v4 - v3;
LODWORD(result) = 1 << (v5 / 10);
_bittestandset((int *)&result, v5 % 10 / 2 + 7);
result = (int)result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // eax
int v4; // edx
signed int v5; // edx

result = 2112LL;
if ( a1 )
{
if ( a2 / 3 < a1 )
{
if ( 2 * a2 / 3 < a1 )
{
v3 = 18 * a1 / a2;
v4 = 18;
}
else
{
v3 = 42 * a1 / a2;
v4 = 34;
}
}
else
{
v3 = 144 * a1 / a2;
v4 = 68;
}
v5 = v4 - v3;
LODWORD(result) = 1 << (v5 / 10);
_bittestandset((int *)&result, v5 % 10 / 2 + 7);
result = (int)result;
}
return result;
}

original_code:
unsigned long
pt2254a_encode_volume(unsigned int current, unsigned int max) {
 unsigned long ret = 0ul;
 int vol, tens, ones;
 if (current > 0 && current <= max / 3) {
  vol = 68 - (48 * 3 * current / max);
 } else if (current > max / 3 && current <= 2 * max / 3) {
  vol = 34 - (42 * current / max);
 } else if (current > 2 * max / 3) {
  vol = 18 - (18 * current / max);
 } else vol = 68;
 tens = vol / 10;
 ones = vol - tens * 10;
 ret = (1 << tens) | (1 << (7 + ones / 2));
 return ret;
}

canonical_original_code:
unsigned long pt2254a_encode_volume(unsigned int current, unsigned int max) {
 unsigned long ret = 0ul;
 int vol; int tens; int ones;
 if (current > 0 && current <= max / 3) {
  vol = 68 - (48 * 3 * current / max);
 } else if (current > max / 3 && current <= 2 * max / 3) {
  vol = 34 - (42 * current / max);
 } else if (current > 2 * max / 3) {
  vol = 18 - (18 * current / max);
 } else vol = 68;
 tens = vol / 10;
 ones = vol - tens * 10;
 ret = (1 << tens) | (1 << (7 + ones / 2));
 return ret;
}

variable_types:
current
unsigned int

variable_types:
max
unsigned int

variable_types:
ret
unsigned long

variable_types: vol int

variable_types: tens int

variable_types: ones int

return_type: unsigned long


decompiled_code:
__int64 __fastcall pt2254a_compose_register(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 v4; // rdi
__int64 result; // rax
__int64 v6; // rsi

v4 = a1 | 0x2000;
result = 0LL;
if ( a3 == 1 )
result = v4;
v6 = result | a2 | 0x4000;
if ( a4 == 1 )
result = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 v4; // rdi
__int64 result; // rax
__int64 v6; // rsi

v4 = a1 | 0x2000;
result = 0LL;
if ( a3 == 1 )
result = v4;
v6 = result | a2 | 0x4000;
if ( a4 == 1 )
result = v6;
return result;
}

original_code:
unsigned long
pt2254a_compose_register(unsigned long lvol, unsigned long rvol, int left, int right) {
 unsigned long ret = 0ul;
 if (left == 1) {
  ret |= (1 << 13);
  ret |= lvol;
 }
 if (right == 1) {
  ret |= (1 << 14);
  ret |= rvol;
 }
 ret |= (0 << 15);
 ret |= (0 << 12);
 return ret;
}

variable_types:
lvol
unsigned long

variable_types:
rvol
unsigned long

variable_types: left int

variable_types: right int


chartoint

repo: jouyouyun/serial

decompiled_code:
__int64 __fastcall chartoint(char a1)
{
__int64 result; // rax

result = (unsigned int)(a1 - 55);
if ( (unsigned __int8)(a1 - 48) <= 9u )
result = (unsigned int)(char)(a1 - 48);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
__int64 result; // rax

result = (unsigned int)(a1 - 55);
if ( (unsigned __int8)(a1 - 48) <= 9u )
result = (unsigned int)(char)(a1 - 48);
return result;
}

original_code:
int chartoint( char c )
{
 if ( c >= '0' && c <= '9' ) {
  return ( c - '0' );
 }
 return ( c - 'A' + 10 );
}

canonical_original_code:
int chartoint(char c)
{
 if ( c >= '0' && c <= '9' ) {
  return ( c - '0' );
 }
 return ( c - 'A' + 10 );
}

variable_types: c char


original_code:
unsigned short modbus_crc( unsigned char *p, int len )
{
 unsigned short ret = 0xFFFF;
 int i = 0;
 int k = 0;
 for ( i = 0; i < len; ++i ) {
  ret ^= p[i];
  for ( k = 0; k < 8; ++k ) {
   ret = ( ret & 0x01) ? ( (ret >> 1) ^ 0xA001 ) : ( ret >> 1 );
  }
 }
 ret = ((ret & 0x00FF) << 8) | ((ret & 0xFF00) >> 8);
 return ret;
}

canonical_original_code:
unsigned short modbus_crc(unsigned char *p, int len)
{
 unsigned short ret = 0xFFFF;
 int i = 0;
 int k = 0;
 for ( i = 0; i < len; ++i ) {
  ret ^= p[i];
  for ( k = 0; k < 8; ++k ) {
   ret = ( ret & 0x01) ? ( (ret >> 1) ^ 0xA001 ) : ( ret >> 1 );
  }
 }
 ret = ((ret & 0x00FF) << 8) | ((ret & 0xFF00) >> 8);
 return ret;
}

variable_types:
p
unsigned char *

variable_types: len int

variable_types:
ret
unsigned short

variable_types: i int

variable_types: k int

return_type: unsigned short


main

original_code:
int main( int argc, char *argv[] )
{
 int saveadd = 0, func = 0, startadd = 0, num = 0, i = 0, len = 0, k = 0;
 unsigned short val = 0;
 unsigned char recv[65];
 unsigned char recvtmp[65];
 unsigned char str[65];
 unsigned char fstr[4];
 float *pf = ((void *)0);
 while ( scanf( "%d, %d, %d, %d", &saveadd, &func, &startadd, &num )
   != (-1) ) {
  str[0] = saveadd;
  str[1] = func;
  str[3] = startadd & 0xFF;
  startadd >>= 8;
  str[2] = startadd & 0xFF;
  str[5] = num & 0xFF;
  num >>= 8;
  str[4] = num & 0xFF;
  val = modbus_crc( str, 6 );
  str[7] = val & 0xFF;
  val >>= 8;
  str[6] = val & 0xFF;
  for ( i = 0; i < 8; ++i ) {
   printf( "%02X", str[i] );
  }
  printf( "\n" );
  scanf( "%s", recvtmp );
  len = strlen( (char*)recvtmp ) / 2;
  for( i = 0; i < len; ++i ) {
   recv[i] = (unsigned char) ( chartoint( recvtmp[i * 2] ) * 16 +
     chartoint( recvtmp[i * 2 + 1] ) );
  }
  for ( i = 0; i < len; ++i ) {
   printf( "%d ", recv[i] );
  }
  val = modbus_crc( recv, len - 2 );
  printf( "\n%02X\n", val );
  if ( ( (val & 0xFF) != recv[len - 1] ) || ( ( (val >> 8) & 0xFF )
    != recv[len - 2] ) ) {
   printf( "CRC ERROR\n" );
   continue;
  }
  k = 2;
  num = recv[k] / 4;
  printf( "num: %d\n", num );
  ++k;
  for ( i = 0; i < num; ++i ) {
   fstr[0] = recv[k + 3];
   fstr[1] = recv[k + 2];
   fstr[2] = recv[k + 1];
   fstr[3] = recv[k];
   pf = (float*)(fstr);
   printf( "%.lf", *pf );
   if ( num - 1 != i ) {
    printf( ", " );
   }
   k += 4;
  }
  if ( num != 0 ) {
   printf( "\n" );
  }
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int saveadd = 0; int func = 0; int startadd = 0; int num = 0; int i = 0; int len = 0; int k = 0;
 unsigned short val = 0;
 unsigned char recv[65];
 unsigned char recvtmp[65];
 unsigned char str[65];
 unsigned char fstr[4];
 float *pf = ((void *)0);
 while ( scanf( "%d, %d, %d, %d", &saveadd, &func, &startadd, &num )
   != (-1) ) {
  str[0] = saveadd;
  str[1] = func;
  str[3] = startadd & 0xFF;
  startadd >>= 8;
  str[2] = startadd & 0xFF;
  str[5] = num & 0xFF;
  num >>= 8;
  str[4] = num & 0xFF;
  val = modbus_crc( str, 6 );
  str[7] = val & 0xFF;
  val >>= 8;
  str[6] = val & 0xFF;
  for ( i = 0; i < 8; ++i ) {
   printf( "%02X", str[i] );
  }
  printf( "\n" );
  scanf( "%s", recvtmp );
  len = strlen( (char *)recvtmp ) / 2;
  for( i = 0; i < len; ++i ) {
   recv[i] = (unsigned char) ( chartoint( recvtmp[i * 2] ) * 16 +
     chartoint( recvtmp[i * 2 + 1] ) );
  }
  for ( i = 0; i < len; ++i ) {
   printf( "%d ", recv[i] );
  }
  val = modbus_crc( recv, len - 2 );
  printf( "\n%02X\n", val );
  if ( ( (val & 0xFF) != recv[len - 1] ) || ( ( (val >> 8) & 0xFF )
    != recv[len - 2] ) ) {
   printf( "CRC ERROR\n" );
   continue;
  }
  k = 2;
  num = recv[k] / 4;
  printf( "num: %d\n", num );
  ++k;
  for ( i = 0; i < num; ++i ) {
   fstr[0] = recv[k + 3];
   fstr[1] = recv[k + 2];
   fstr[2] = recv[k + 1];
   fstr[3] = recv[k];
   pf = (float *)(fstr);
   printf( "%.lf", *pf );
   if ( num - 1 != i ) {
    printf( ", " );
   }
   k += 4;
  }
  if ( num != 0 ) {
   printf( "\n" );
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: saveadd int

variable_types: func int

variable_types: startadd int

variable_types: num int

variable_types:
val
unsigned short

variable_types:
recv
unsigned char [65]

variable_types:
recvtmp
unsigned char [65]

variable_types:
str
unsigned char [65]

variable_types:
fstr
unsigned char [4]

variable_types:
pf
float *


decompiled_code:
__int64 __fastcall open_port(const char *a1)
{
unsigned int v1; // er12
int *v3; // rax
char *v4; // rax

v1 = open(a1, 2306);
if ( v1 == -1 )
{
v3 = __errno_location();
v4 = strerror(*v3);
fprintf(stderr, "Cannot open the port: %s\n", v4);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
unsigned int v1; // er12
int *v3; // rax
char *v4; // rax

v1 = open(a1, 2306);
if ( v1 == -1 )
{
v3 = __errno_location();
v4 = strerror(*v3);
fprintf(stderr, "Cannot open the port: %s\n", v4);
}
return v1;
}

original_code:
int open_port( char *port )
{
 int fd;
 if ( (fd = open( port, 02 | 0400 | 04000 )) == -1 ) {
  fprintf( stderr, "Cannot open the port: %s\n", strerror((*__errno_location ())) );
  return -1;
 }
 return fd;
}

canonical_original_code:
int open_port(char *port)
{
 int fd;
 if ( (fd = open( port, 02 | 0400 | 04000 )) == -1 ) {
  fprintf( stderr, "Cannot open the port: %s\n", strerror((*__errno_location ())) );
  return -1;
 }
 return fd;
}

variable_types:
port
char *

variable_types: fd int


decompiled_code:
// attributes: thunk
int close_port(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func8(int fd)
{
return close(fd);
}

original_code:
void close_port( int fd )
{
 close(fd);
}

canonical_original_code:
void close_port(int fd)
{
 close(fd);
}


decompiled_code:
__int64 __fastcall get_baud_rate(unsigned __int64 a1)
{
__int64 result; // rax

if ( a1 == 1200 )
return 9LL;
if ( a1 > 0x4B0 )
{
if ( a1 == 19200 )
{
result = 14LL;
}
else if ( a1 <= 0x4B00 )
{
result = 12LL;
if ( a1 != 4800 )
{
if ( a1 <= 0x12C0 )
{
result = 10LL;
if ( a1 != 1800 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 2400 )
result = 11LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 9600 )
result = 13LL;
}
}
}
else
{
result = 4098LL;
if ( a1 != 115200 )
{
if ( a1 <= 0x1C200 )
{
result = 15LL;
if ( a1 != 38400 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 57600 )
result = 4097LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 230400 )
result = 4099LL;
}
}
}
}
else if ( a1 == 134 )
{
result = 4LL;
}
else if ( a1 <= 0x86 )
{
result = 2LL;
if ( a1 != 75 )
{
if ( a1 <= 0x4B )
{
result = 0LL;
if ( a1 )
result = 2 * (unsigned int)(a1 == 50) - 1;
}
else
{
result = 4 * (unsigned int)(a1 == 110) - 1;
}
}
}
else
{
result = 7LL;
if ( a1 != 300 )
{
if ( a1 <= 0x12C )
{
result = 5LL;
if ( a1 != 150 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 200 )
result = 6LL;
}
}
else
{
result = 9 * (unsigned int)(a1 == 600) - 1;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int64 a1)
{
__int64 result; // rax

if ( a1 == 1200 )
return 9LL;
if ( a1 > 0x4B0 )
{
if ( a1 == 19200 )
{
result = 14LL;
}
else if ( a1 <= 0x4B00 )
{
result = 12LL;
if ( a1 != 4800 )
{
if ( a1 <= 0x12C0 )
{
result = 10LL;
if ( a1 != 1800 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 2400 )
result = 11LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 9600 )
result = 13LL;
}
}
}
else
{
result = 4098LL;
if ( a1 != 115200 )
{
if ( a1 <= 0x1C200 )
{
result = 15LL;
if ( a1 != 38400 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 57600 )
result = 4097LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 230400 )
result = 4099LL;
}
}
}
}
else if ( a1 == 134 )
{
result = 4LL;
}
else if ( a1 <= 0x86 )
{
result = 2LL;
if ( a1 != 75 )
{
if ( a1 <= 0x4B )
{
result = 0LL;
if ( a1 )
result = 2 * (unsigned int)(a1 == 50) - 1;
}
else
{
result = 4 * (unsigned int)(a1 == 110) - 1;
}
}
}
else
{
result = 7LL;
if ( a1 != 300 )
{
if ( a1 <= 0x12C )
{
result = 5LL;
if ( a1 != 150 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 200 )
result = 6LL;
}
}
else
{
result = 9 * (unsigned int)(a1 == 600) - 1;
}
}
}
return result;
}


decompiled_code:
__int64 __fastcall send_data(int fd, const void *a2, int a3)
{
unsigned int v4; // eax

v4 = write(fd, a2, a3);
if ( a3 == v4 )
return v4;
tcflush(fd, 1);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd, const void *a2, int a3)
{
unsigned int v4; // eax

v4 = write(fd, a2, a3);
if ( a3 == v4 )
return v4;
tcflush(fd, 1);
return (unsigned int)-1;
}

original_code:
int send_data( int fd, char *data, int data_len )
{
 int len = 0;
 len = write( fd, data, data_len );
 if ( len == data_len ) {
  return len;
 } else {
  tcflush(fd, 1 );
  return -1;
 }
}

canonical_original_code:
int send_data(int fd, char *data, int data_len)
{
 int len = 0;
 len = write( fd, data, data_len );
 if ( len == data_len ) {
  return len;
 } else {
  tcflush(fd, 1 );
  return -1;
 }
}

variable_types:
data
char *

variable_types: len int


decompiled_code:
ssize_t __fastcall recv_data(int fd, void *buf, int a3)
{
ssize_t result; // rax
struct timeval v5; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

memset(&readfds, 0, sizeof(readfds));
v5.tv_sec = 10LL;
v5.tv_usec = 0LL;
readfds.fds_bits[fd / 64] |= 1LL << (fd % 64);
if ( select(fd + 1, &readfds, 0LL, 0LL, &v5) )
result = read(fd, buf, a3);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(int fd, void *buf, int a3)
{
ssize_t result; // rax
struct timeval v5; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

memset(&readfds, 0, sizeof(readfds));
v5.tv_sec = 10LL;
v5.tv_usec = 0LL;
readfds.fds_bits[fd / 64] |= 1LL << (fd % 64);
if ( select(fd + 1, &readfds, 0LL, 0LL, &v5) )
result = read(fd, buf, a3);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int recv_data( int fd, char *data, int data_len )
{
 int len, fs_sel;
 fd_set fs_read;
 struct timeval tv;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fs_read)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fs_read)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 tv.tv_sec = 10;
 tv.tv_usec = 0;
 fs_sel = select( fd + 1, &fs_read, ((void *)0), ((void *)0), &tv );
 if ( fs_sel ) {
  len = read( fd, data, data_len );
  return len;
 } else {
  return -1;
 }
}

canonical_original_code:
int recv_data(int fd, char *data, int data_len)
{
 int len; int fs_sel;
 struct fd_set fs_read;
 struct timeval tv;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fs_read)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fs_read)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 tv.tv_sec = 10;
 tv.tv_usec = 0;
 fs_sel = select( fd + 1, &fs_read, ((void *)0), ((void *)0), &tv );
 if ( fs_sel ) {
  len = read( fd, data, data_len );
  return len;
 } else {
  return -1;
 }
}

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


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

if ( a1 == 1200 )
return 9LL;
if ( a1 > 0x4B0 )
{
if ( a1 == 19200 )
{
result = 14LL;
}
else if ( a1 <= 0x4B00 )
{
result = 12LL;
if ( a1 != 4800 )
{
if ( a1 <= 0x12C0 )
{
result = 10LL;
if ( a1 != 1800 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 2400 )
result = 11LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 9600 )
result = 13LL;
}
}
}
else
{
result = 4098LL;
if ( a1 != 115200 )
{
if ( a1 <= 0x1C200 )
{
result = 15LL;
if ( a1 != 38400 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 57600 )
result = 4097LL;
}
}
else
{
result = 0xFFFFFFFFLL;
if ( a1 == 230400 )
result = 4099LL;
}
}
}
}
else if ( a1 == 134 )
{
result = 4LL;
}
else if ( a1 <= 0x86 )
{
result = 2LL;
if ( a1 != 75 )
{
if ( a1 <= 0x4B )
{
result = 0LL;
if ( a1 )
result = 2 * (unsigned int)(a1 == 50) - 1;
}
else
{
result = 4 * (unsigned int)(a1 == 110) - 1;
}
}
}
else
{
result = 7LL;
if ( a1 != 300 )
{
if ( a1 <= 0x12C )
{
result = 5LL;
if ( a1 != 150 )
{
result = 0xFFFFFFFFLL;
if ( a1 == 200 )
result = 6LL;
}
}
else
{
result = 9 * (unsigned int)(a1 == 600) - 1;
}
}
}
return result;
}


decompiled_code:
__int64 __fastcall set_flow_ctrl(__int64 a1, __int64 a2)
{
char v2; // al
__int64 result; // rax

if ( a1 && a2 )
{
v2 = *(_BYTE *)(a1 + 10);
if ( v2 == 49 )
{
*(_DWORD *)(a2 + 8) |= 0x80000000;
result = 0LL;
}
else if ( v2 == 50 )
{
*(_DWORD *)(a2 + 8) |= 0x1C00u;
result = 0LL;
}
else
{
if ( v2 == 48 )
*(_DWORD *)(a2 + 8) &= 0x80000000;
result = 0LL;
}
}
else
{
fwrite("set_flow_ctrl arguments err\n", 1uLL, 0x1CuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( a1 && a2 )
{
v2 = *(_BYTE *)(a1 + 10);
if ( v2 == 49 )
{
*(_DWORD *)(a2 + 8) |= 0x80000000;
result = 0LL;
}
else if ( v2 == 50 )
{
*(_DWORD *)(a2 + 8) |= 0x1C00u;
result = 0LL;
}
else
{
if ( v2 == 48 )
*(_DWORD *)(a2 + 8) &= 0x80000000;
result = 0LL;
}
}
else
{
fwrite("set_flow_ctrl arguments err\n", 1uLL, 0x1CuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_flow_ctrl( pport_info p_info, struct termios *opt )
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_flow_ctrl arguments err\n" );
  return -1;
 }
 switch (p_info->flow_ctrl) {
  case '0':
   opt->c_cflag &= 020000000000;
   break;
  case '1':
   opt->c_cflag |= 020000000000;
   break;
  case '2':
   opt->c_cflag |= 0002000 | 0010000 | 0004000;
   break;
  default :
   break;
 }
 return 0;
}

canonical_original_code:
int set_flow_ctrl(struct port_info *p_info, struct termios *opt)
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_flow_ctrl arguments err\n" );
  return -1;
 }
 switch (p_info->flow_ctrl) {
  case '0':
   opt->c_cflag &= 020000000000;
   break;
  case '1':
   opt->c_cflag |= 020000000000;
   break;
  case '2':
   opt->c_cflag |= 0002000 | 0010000 | 0004000;
   break;
  default :
   break;
 }
 return 0;
}

variable_types: struct port_info { int baud_rate; int port_fd; char parity; char stop_bit; char flow_ctrl; char data_bits; } *

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } *


decompiled_code:
__int64 __fastcall set_data_bit(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
char v3; // dl
__int64 result; // rax

if ( a1 && a2 )
{
v2 = *(_DWORD *)(a2 + 8) & 0xFFFFFFCF;
*(_DWORD *)(a2 + 8) = v2;
v3 = *(_BYTE *)(a1 + 11);
if ( v3 == 54 )
{
*(_DWORD *)(a2 + 8) = v2 | 0x10;
result = 0LL;
}
else if ( v3 == 55 )
{
*(_DWORD *)(a2 + 8) = v2 | 0x20;
result = 0LL;
}
else
{
if ( v3 != 53 )
*(_DWORD *)(a2 + 8) = v2 | 0x30;
result = 0LL;
}
}
else
{
fwrite("set_data_bit arguments err\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
char v3; // dl
__int64 result; // rax

if ( a1 && a2 )
{
v2 = *(_DWORD *)(a2 + 8) & 0xFFFFFFCF;
*(_DWORD *)(a2 + 8) = v2;
v3 = *(_BYTE *)(a1 + 11);
if ( v3 == 54 )
{
*(_DWORD *)(a2 + 8) = v2 | 0x10;
result = 0LL;
}
else if ( v3 == 55 )
{
*(_DWORD *)(a2 + 8) = v2 | 0x20;
result = 0LL;
}
else
{
if ( v3 != 53 )
*(_DWORD *)(a2 + 8) = v2 | 0x30;
result = 0LL;
}
}
else
{
fwrite("set_data_bit arguments err\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_data_bit( pport_info p_info, struct termios *opt )
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_data_bit arguments err\n" );
  return -1;
 }
 opt->c_cflag &= ~0000060;
 switch (p_info->data_bits) {
  case '5':
   opt->c_cflag |= 0000000;
   break;
  case '6':
   opt->c_cflag |= 0000020;
   break;
  case '7':
   opt->c_cflag |= 0000040;
   break;
  case '8':
   opt->c_cflag |= 0000060;
   break;
  default :
   opt->c_cflag |= 0000060;
   break;
 }
 return 0;
}

canonical_original_code:
int set_data_bit(struct port_info *p_info, struct termios *opt)
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_data_bit arguments err\n" );
  return -1;
 }
 opt->c_cflag &= ~0000060;
 switch (p_info->data_bits) {
  case '5':
   opt->c_cflag |= 0000000;
   break;
  case '6':
   opt->c_cflag |= 0000020;
   break;
  case '7':
   opt->c_cflag |= 0000040;
   break;
  case '8':
   opt->c_cflag |= 0000060;
   break;
  default :
   opt->c_cflag |= 0000060;
   break;
 }
 return 0;
}

user_defined_types: struct port_info { int baud_rate; int port_fd; char parity; char stop_bit; char flow_ctrl; char data_bits; }

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
__int64 __fastcall set_parity(__int64 a1, __int64 a2)
{
char v2; // al
__int64 result; // rax

if ( a1 && a2 )
{
v2 = *(_BYTE *)(a1 + 8);
if ( v2 == 49 )
{
*(_DWORD *)(a2 + 8) |= 0xFFFFFDFF;
result = 0LL;
}
else if ( v2 == 50 )
{
*(_DWORD *)(a2 + 8) |= 0x300u;
result = 0LL;
}
else
{
if ( v2 == 48 )
*(_DWORD *)(a2 + 8) |= 0xFFFFFEFF;
result = 0LL;
}
}
else
{
fwrite("set_parity arguments err\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
char v2; // al
__int64 result; // rax

if ( a1 && a2 )
{
v2 = *(_BYTE *)(a1 + 8);
if ( v2 == 49 )
{
*(_DWORD *)(a2 + 8) |= 0xFFFFFDFF;
result = 0LL;
}
else if ( v2 == 50 )
{
*(_DWORD *)(a2 + 8) |= 0x300u;
result = 0LL;
}
else
{
if ( v2 == 48 )
*(_DWORD *)(a2 + 8) |= 0xFFFFFEFF;
result = 0LL;
}
}
else
{
fwrite("set_parity arguments err\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_parity( pport_info p_info, struct termios *opt )
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_parity arguments err\n" );
  return -1;
 }
 switch(p_info->parity) {
  case '0':
   opt->c_cflag |= ~0000400;
   break;
  case '1':
   opt->c_cflag |= 0000400;
   opt->c_cflag |= ~0001000;
   break;
  case '2':
   opt->c_cflag |= 0000400;
   opt->c_cflag |= 0001000;
   break;
  default :
   break;
 }
 return 0;
}

canonical_original_code:
int set_parity(struct port_info *p_info, struct termios *opt)
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_parity arguments err\n" );
  return -1;
 }
 switch(p_info->parity) {
  case '0':
   opt->c_cflag |= ~0000400;
   break;
  case '1':
   opt->c_cflag |= 0000400;
   opt->c_cflag |= ~0001000;
   break;
  case '2':
   opt->c_cflag |= 0000400;
   opt->c_cflag |= 0001000;
   break;
  default :
   break;
 }
 return 0;
}


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

if ( a1 && a2 )
{
v2 = *(_DWORD *)(a2 + 8);
if ( *(_BYTE *)(a1 + 9) == 50 )
*(_DWORD *)(a2 + 8) = v2 | 0x40;
else
*(_DWORD *)(a2 + 8) = v2 & 0xFFFFFFBF;
result = 0LL;
}
else
{
fwrite("set_stop_bit arguments err\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( a1 && a2 )
{
v2 = *(_DWORD *)(a2 + 8);
if ( *(_BYTE *)(a1 + 9) == 50 )
*(_DWORD *)(a2 + 8) = v2 | 0x40;
else
*(_DWORD *)(a2 + 8) = v2 & 0xFFFFFFBF;
result = 0LL;
}
else
{
fwrite("set_stop_bit arguments err\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_stop_bit( pport_info p_info, struct termios *opt )
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_stop_bit arguments err\n" );
  return -1;
 }
 if ( p_info->stop_bit == '2' ) {
  opt->c_cflag |= 0000100;
 } else {
  opt->c_cflag &= ~0000100;
 }
 return 0;
}

canonical_original_code:
int set_stop_bit(struct port_info *p_info, struct termios *opt)
{
 if ( p_info == ((void *)0) || opt == ((void *)0) ) {
  fprintf( stderr, "set_stop_bit arguments err\n" );
  return -1;
 }
 if ( p_info->stop_bit == '2' ) {
  opt->c_cflag |= 0000100;
 } else {
  opt->c_cflag &= ~0000100;
 }
 return 0;
}


CustomerRun

repo: jovizhu/courseDemo

decompiled_code:
void __fastcall __noreturn CustomerRun(void *a1)
{
sleep((unsigned int)a1);
printf("Custom[%d]: Customer #%d come in\n", (unsigned int)a1, (unsigned int)a1);
fflush(stdout);
sem_post(&Customer2Casher);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
sleep((unsigned int)a1);
printf("Custom[%d]: Customer #%d come in\n", (unsigned int)a1, (unsigned int)a1);
fflush(stdout);
sem_post(&Customer2Casher);
pthread_exit(0LL);
}

original_code:
void * CustomerRun(void * args)
{
  int num = (int) args;
  sleep(num);
  printf("Custom[%d]: Customer #%d come in\n", num,num);
  fflush(stdout);
  sem_post(&Customer2Casher);
  pthread_exit(((void *)0));
}

canonical_original_code:
void *CustomerRun(void *args)
{
  int num = (int) args;
  sleep(num);
  printf("Custom[%d]: Customer #%d come in\n", num,num);
  fflush(stdout);
  sem_post(&Customer2Casher);
  pthread_exit(((void *)0));
}

variable_types:
args
void *

return_type: void *


CasherRun

decompiled_code:
void __fastcall __noreturn CasherRun(void *a1)
{
while ( 1 )
{
fflush(stdout);
sem_wait(&Customer2Casher);
printf("Casher[%d]: Accept an order\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&Burger);
printf("Casher[%d]: takes a burger to customer\n", (unsigned int)a1);
fflush(stdout);
sem_post(&Rack);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
while ( 1 )
{
fflush(stdout);
sem_wait(&Customer2Casher);
printf("Casher[%d]: Accept an order\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&Burger);
printf("Casher[%d]: takes a burger to customer\n", (unsigned int)a1);
fflush(stdout);
sem_post(&Rack);
}
}

original_code:
void * CasherRun(void * args)
{
  int i=0;
  int num=(int) args;
  while(i<100)
  {
    fflush(stdout);
    sem_wait(&Customer2Casher);
    printf("Casher[%d]: Accept an order\n",num);
    fflush(stdout);
    sem_wait(&Burger);
    printf("Casher[%d]: takes a burger to customer\n",num);
    fflush(stdout);
    sem_post(&Rack);
  }
  pthread_exit(((void *)0));
}

canonical_original_code:
void *CasherRun(void *args)
{
  int i = 0;
  int num = (int) args;
  while(i<100)
  {
    fflush(stdout);
    sem_wait(&Customer2Casher);
    printf("Casher[%d]: Accept an order\n",num);
    fflush(stdout);
    sem_wait(&Burger);
    printf("Casher[%d]: takes a burger to customer\n",num);
    fflush(stdout);
    sem_post(&Rack);
  }
  pthread_exit(((void *)0));
}

variable_types: i int


CookerRun

decompiled_code:
void __fastcall __noreturn CookerRun(void *a1)
{
while ( 1 )
{
sleep(2u);
sem_wait(&Rack);
printf("Cooker[%d]: Make a burger\n", (unsigned int)a1);
fflush(stdout);
sem_post(&Burger);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func9(void *a1)
{
while ( 1 )
{
sleep(2u);
sem_wait(&Rack);
printf("Cooker[%d]: Make a burger\n", (unsigned int)a1);
fflush(stdout);
sem_post(&Burger);
}
}

original_code:
void * CookerRun(void * args)
{
  int i=0;
  int num=(int)args;
  while(i<100)
  {
    sleep(2);
    sem_wait(&Rack);
    printf("Cooker[%d]: Make a burger\n",num);
    fflush(stdout);
    sem_post(&Burger);
  }
  pthread_exit(((void *)0));
}

canonical_original_code:
void *CookerRun(void *args)
{
  int i = 0;
  int num = (int)args;
  while(i<100)
  {
    sleep(2);
    sem_wait(&Rack);
    printf("Cooker[%d]: Make a burger\n",num);
    fflush(stdout);
    sem_post(&Burger);
  }
  pthread_exit(((void *)0));
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
int v4; // er14
int v5; // er15
__int64 v6; // r12
pthread_t *v7; // rax
pthread_t *v8; // rbx
char *i; // rbp
void *v10; // rcx
pthread_t *v11; // rdi
pthread_t *v12; // rbp
char *v13; // r13
char *v14; // rcx
pthread_t *v15; // rdi
pthread_t *v16; // rbp
char *v17; // r13
pthread_t *v18; // r12
char *v19; // rcx
pthread_t *v20; // rdi
pthread_t *v21; // rbp
unsigned int v22; // eax
pthread_t *v23; // rbp
pthread_t *v25; // [rsp+8h] [rbp-90h]
pthread_t *newthread; // [rsp+10h] [rbp-88h]
pthread_t *v27; // [rsp+18h] [rbp-80h]
pthread_attr_t attr; // [rsp+20h] [rbp-78h] BYREF

if ( argc != 4 )
{
puts("Usage: BBC cooker casher racksize ");
exit(0);
}
v3 = (char *)atoi(argv[1]);
v4 = (int)v3;
v5 = atoi(argv[2]);
v6 = (unsigned int)atoi(argv[3]);
printf("cookers[%d], cahsers[%d], customers[%d], racksize[%d] \n", (unsigned int)v3, (unsigned int)v5, 100LL, v6);
fflush(stdout);
puts("Begin run");
fflush(stdout);
sem_init(&Customer2Casher, 0, 0);
sem_init(&Burger, 0, 0);
sem_init(&Rack, 0, v6);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 0);
v25 = (pthread_t *)calloc(0x320uLL, 1uLL);
newthread = (pthread_t *)calloc(8LL * v5, 1uLL);
v7 = (pthread_t *)calloc(8LL * (_QWORD)v3, 1uLL);
v27 = v7;
if ( (int)v3 > 0 )
{
v8 = v7;
for ( i = 0LL; i != v3; ++i )
{
v10 = i;
v11 = v8++;
pthread_create(v11, &attr, CookerRun, v10);
}
}
if ( v5 > 0 )
{
v12 = newthread;
v13 = 0LL;
do
{
v14 = v13;
v15 = v12;
++v13;
++v12;
pthread_create(v15, &attr, CasherRun, v14);
}
while ( v13 != (char *)v5 );
}
v16 = v25;
v17 = 0LL;
v18 = v25;
do
{
v19 = v17;
v20 = v16;
++v17;
++v16;
pthread_create(v20, &attr, CustomerRun, v19);
}
while ( v17 != (char *)100 );
pthread_attr_destroy(&attr);
if ( v4 > 0 )
{
v21 = v27;
do
{
v22 = pthread_join(*v21, 0LL);
if ( v22 )
{
LABEL_22:
printf("ERROR; return code from pthread_join() is %d\n", v22);
exit(-1);
}
++v21;
}
while ( v21 != &v27[(unsigned int)(v4 - 1) + 1] );
}
if ( v5 <= 0 )
goto LABEL_19;
v23 = newthread;
do
{
v22 = pthread_join(*v23, 0LL);
if ( v22 )
goto LABEL_22;
++v23;
}
while ( v23 != &newthread[(unsigned int)(v5 - 1) + 1] );
do
{
LABEL_19:
v22 = pthread_join(*v18, 0LL);
if ( v22 )
goto LABEL_22;
++v18;
}
while ( v25 + 100 != v18 );
puts("End run ");
sem_destroy(&Burger);
sem_destroy(&Rack);
sem_destroy(&Customer2Casher);
return 0;
}

original_code:
int main(int argc, char * argv[])
{
  if ( argc !=4)
  {
    printf("Usage: BBC cooker casher racksize \n");
    exit(0);
  }
  int cookernum = atoi(argv[1]);
  int cashernum = atoi(argv[2]);
  int racksize = atoi(argv[3]);
  printf("cookers[%d],  cahsers[%d], customers[%d],   racksize[%d] \n",cookernum,cashernum,100,racksize);
  fflush(stdout);
  printf("Begin run\n");
  fflush(stdout);
  sem_init(&Customer2Casher,0,0);
  sem_init(&Burger,0,0);
  sem_init(&Rack,0,racksize);
     pthread_t *CasherThread;
  pthread_t *CustomerThread;
  pthread_t *CookerThread;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        CustomerThread = malloc(sizeof(pthread_t)*100);
        memset(CustomerThread,0,sizeof(pthread_t)*100);
  CasherThread = malloc(sizeof(pthread_t)*cashernum);
  memset(CasherThread,0,sizeof(pthread_t)*cashernum);
  CookerThread = malloc(sizeof(pthread_t)*cookernum);
  memset(CookerThread,0,sizeof(pthread_t)*cookernum);
  int CookerRc;
  int CasherRc;
  int CustomerRc;
  int i=0;
  for(i=0;i<cookernum;i++)
  {
   CookerRc = pthread_create(&CookerThread[i],&attr,CookerRun,(void *)i);
  }
  for(i=0;i<cashernum;i++)
  {
   CasherRc = pthread_create(&CasherThread[i],&attr,CasherRun,(void *)i);
  }
  for(i=0;i<100;i++)
  {
   CustomerRc = pthread_create(&CustomerThread[i],&attr,CustomerRun,(void *)i);
  }
  pthread_attr_destroy(&attr);
   for(i=0; i<cookernum; i++)
  {
   CookerRc = pthread_join(CookerThread[i], ((void *)0));
      if (CookerRc)
   {
         printf("ERROR; return code from pthread_join() is %d\n", CookerRc);
         exit(-1);
   }
  }
  for(i=0; i<cashernum; i++)
  {
      CasherRc = pthread_join(CasherThread[i], ((void *)0));
   if (CasherRc)
   {
         printf("ERROR; return code from pthread_join() is %d\n", CasherRc);
         exit(-1);
      }
  }
  for(i=0; i<100; i++)
  {
      CustomerRc = pthread_join(CustomerThread[i], ((void *)0));
      if (CustomerRc)
      {
         printf("ERROR; return code from pthread_join() is %d\n", CustomerRc);
         exit(-1);
      }
  }
  printf("End run \n");
  sem_destroy(&Burger);
  sem_destroy(&Rack);
  sem_destroy(&Customer2Casher);
}

canonical_original_code:
int main(int argc, char *argv[])
{
  if ( argc !=4)
  {
    printf("Usage: BBC cooker casher racksize \n");
    exit(0);
  }
  int cookernum = atoi(argv[1]);
  int cashernum = atoi(argv[2]);
  int racksize = atoi(argv[3]);
  printf("cookers[%d],  cahsers[%d], customers[%d],   racksize[%d] \n",cookernum,cashernum,100,racksize);
  fflush(stdout);
  printf("Begin run\n");
  fflush(stdout);
  sem_init(&Customer2Casher,0,0);
  sem_init(&Burger,0,0);
  sem_init(&Rack,0,racksize);
     unsigned long *CasherThread;
  unsigned long *CustomerThread;
  unsigned long *CookerThread;
  union pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        CustomerThread = malloc(sizeof(pthread_t)*100);
        memset(CustomerThread,0,sizeof(pthread_t)*100);
  CasherThread = malloc(sizeof(pthread_t)*cashernum);
  memset(CasherThread,0,sizeof(pthread_t)*cashernum);
  CookerThread = malloc(sizeof(pthread_t)*cookernum);
  memset(CookerThread,0,sizeof(pthread_t)*cookernum);
  int CookerRc;
  int CasherRc;
  int CustomerRc;
  int i = 0;
  for(i=0;i<cookernum;i++)
  {
   CookerRc = pthread_create(&CookerThread[i],&attr,CookerRun,(void *)i);
  }
  for(i=0;i<cashernum;i++)
  {
   CasherRc = pthread_create(&CasherThread[i],&attr,CasherRun,(void *)i);
  }
  for(i=0;i<100;i++)
  {
   CustomerRc = pthread_create(&CustomerThread[i],&attr,CustomerRun,(void *)i);
  }
  pthread_attr_destroy(&attr);
   for(i=0; i<cookernum; i++)
  {
   CookerRc = pthread_join(CookerThread[i], ((void *)0));
      if (CookerRc)
   {
         printf("ERROR; return code from pthread_join() is %d\n", CookerRc);
         exit(-1);
   }
  }
  for(i=0; i<cashernum; i++)
  {
      CasherRc = pthread_join(CasherThread[i], ((void *)0));
   if (CasherRc)
   {
         printf("ERROR; return code from pthread_join() is %d\n", CasherRc);
         exit(-1);
      }
  }
  for(i=0; i<100; i++)
  {
      CustomerRc = pthread_join(CustomerThread[i], ((void *)0));
      if (CustomerRc)
      {
         printf("ERROR; return code from pthread_join() is %d\n", CustomerRc);
         exit(-1);
      }
  }
  printf("End run \n");
  sem_destroy(&Burger);
  sem_destroy(&Rack);
  sem_destroy(&Customer2Casher);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: cookernum int

variable_types: cashernum int

variable_types: racksize int

variable_types:
CasherThread
unsigned long *

variable_types:
CustomerThread
unsigned long *

variable_types:
CookerThread
unsigned long *

variable_types: union pthread_attr_t { char __size[56]; long __align; }

variable_types: CookerRc int

variable_types: CasherRc int

variable_types: CustomerRc int


SantaRun

decompiled_code:
void __fastcall __noreturn SantaRun(void *a1)
{
int v1; // ebp
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx

v1 = 8;
while ( 1 )
{
puts("Santa : I am sleeping ");
fflush(stdout);
sem_wait(&SantaSleep);
if ( rein_ct == 9 )
{
v3 = 8;
puts("Santa : I was waked up by Reindeer ");
fflush(stdout);
sem_wait(&Reinmutex);
rein_ct = 0;
sem_post(&Reinmutex);
do
{
sem_post(&ReinWait);
--v3;
}
while ( v3 );
v4 = 9;
do
{
sem_post(&Sleigh);
--v4;
}
while ( v4 );
v5 = 9;
puts("Santa : Deliver all toys and return ");
fflush(stdout);
puts("Santa : Reindeer, You can bakc to vacation now");
fflush(stdout);
do
{
sem_post(&ReinDone);
--v5;
}
while ( v5 );
if ( !--v1 )
LABEL_6:
pthread_exit(0LL);
}
else
{
v2 = 3;
puts("Santa : I was waked up by Elf ");
fflush(stdout);
sem_post(&Santa);
sem_post(&Santa);
sem_wait(&Elfmutex);
elf_ct = 0;
sem_post(&Elfmutex);
do
{
puts("Santa : Solve Elf Problem ");
fflush(stdout);
sem_post(&ElfProblem);
sem_post(&ElfDone);
--v2;
}
while ( v2 );
if ( !--v1 )
goto LABEL_6;
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
int v1; // ebp
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx

v1 = 8;
while ( 1 )
{
puts("Santa : I am sleeping ");
fflush(stdout);
sem_wait(&SantaSleep);
if ( rein_ct == 9 )
{
v3 = 8;
puts("Santa : I was waked up by Reindeer ");
fflush(stdout);
sem_wait(&Reinmutex);
rein_ct = 0;
sem_post(&Reinmutex);
do
{
sem_post(&ReinWait);
--v3;
}
while ( v3 );
v4 = 9;
do
{
sem_post(&Sleigh);
--v4;
}
while ( v4 );
v5 = 9;
puts("Santa : Deliver all toys and return ");
fflush(stdout);
puts("Santa : Reindeer, You can bakc to vacation now");
fflush(stdout);
do
{
sem_post(&ReinDone);
--v5;
}
while ( v5 );
if ( !--v1 )
LABEL_6:
pthread_exit(0LL);
}
else
{
v2 = 3;
puts("Santa : I was waked up by Elf ");
fflush(stdout);
sem_post(&Santa);
sem_post(&Santa);
sem_wait(&Elfmutex);
elf_ct = 0;
sem_post(&Elfmutex);
do
{
puts("Santa : Solve Elf Problem ");
fflush(stdout);
sem_post(&ElfProblem);
sem_post(&ElfDone);
--v2;
}
while ( v2 );
if ( !--v1 )
goto LABEL_6;
}
}
}

original_code:
void * SantaRun(void * args)
{
  int i = 0;
  int j = 0;
  while(i<8)
  {
  printf("Santa      : I am sleeping \n");
  fflush(stdout);
  sem_wait(&SantaSleep);
  if(rein_ct == 9)
  {
    printf("Santa      : I was waked up by Reindeer \n");
    fflush(stdout);
    sem_wait(&Reinmutex);
    rein_ct = 0;
    sem_post(&Reinmutex);
    for(j=0;j<9 -1;j++)
    {
      sem_post(&ReinWait);
    }
    for(j=0;j<9;j++)
    {
      sem_post(&Sleigh);
    }
    printf("Santa      : Deliver all toys and return \n");
    fflush(stdout);
    printf("Santa      : Reindeer, You can bakc to vacation now\n");
    fflush(stdout);
    for(j=0;j<9;j++)
    {
      sem_post(&ReinDone);
    }
  }
  else
  {
    printf("Santa      : I was waked up by Elf \n");
    fflush(stdout);
    for (j=0;j<3 -1;j++)
      sem_post(&Santa);
    sem_wait(&Elfmutex);
    elf_ct=0;
    sem_post(&Elfmutex);
    for(j=0;j<3;j++)
    {
      printf("Santa      : Solve Elf Problem \n");
      fflush(stdout);
      sem_post(&ElfProblem);
      sem_post(&ElfDone);
    }
  }
  i++;
  }
  pthread_exit(((void *)0));
}

canonical_original_code:
void *SantaRun(void *args)
{
  int i = 0;
  int j = 0;
  while(i<8)
  {
  printf("Santa      : I am sleeping \n");
  fflush(stdout);
  sem_wait(&SantaSleep);
  if(rein_ct == 9)
  {
    printf("Santa      : I was waked up by Reindeer \n");
    fflush(stdout);
    sem_wait(&Reinmutex);
    rein_ct = 0;
    sem_post(&Reinmutex);
    for(j=0;j<9 -1;j++)
    {
      sem_post(&ReinWait);
    }
    for(j=0;j<9;j++)
    {
      sem_post(&Sleigh);
    }
    printf("Santa      : Deliver all toys and return \n");
    fflush(stdout);
    printf("Santa      : Reindeer, You can bakc to vacation now\n");
    fflush(stdout);
    for(j=0;j<9;j++)
    {
      sem_post(&ReinDone);
    }
  }
  else
  {
    printf("Santa      : I was waked up by Elf \n");
    fflush(stdout);
    for (j=0;j<3 -1;j++)
      sem_post(&Santa);
    sem_wait(&Elfmutex);
    elf_ct=0;
    sem_post(&Elfmutex);
    for(j=0;j<3;j++)
    {
      printf("Santa      : Solve Elf Problem \n");
      fflush(stdout);
      sem_post(&ElfProblem);
      sem_post(&ElfDone);
    }
  }
  i++;
  }
  pthread_exit(((void *)0));
}

variable_types: j int


ReindeerRun

decompiled_code:
void __fastcall __noreturn ReindeerRun(void *a1)
{
int v1; // ebp

v1 = 2;
printf("Reindeer[%d]: I am Reineer # %d \n", a1, (unsigned int)a1);
fflush(stdout);
while ( 1 )
{
sleep(1u);
printf("Reindeer[%d]: Wow, it is Christmas, back to Santa\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&Reinmutex);
if ( ++rein_ct == 9 )
{
sem_post(&Reinmutex);
printf("Reindeer[%d]: Hey, Wakeup, Santa\n", (unsigned int)a1);
fflush(stdout);
sem_post(&SantaSleep);
}
else
{
sem_post(&Reinmutex);
printf("Reindeer[%d]: Oh, Wait outsied Santa's house\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&ReinWait);
}
sem_wait(&Sleigh);
printf("Reindeer[%d]: Fly off to deliver the toys\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&ReinDone);
printf("Reindeer[%d]: I will back for long vacation on the beaches\n", (unsigned int)a1);
fflush(stdout);
if ( v1 == 1 )
pthread_exit(0LL);
v1 = 1;
}
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
int v1; // ebp

v1 = 2;
printf("Reindeer[%d]: I am Reineer # %d \n", a1, (unsigned int)a1);
fflush(stdout);
while ( 1 )
{
sleep(1u);
printf("Reindeer[%d]: Wow, it is Christmas, back to Santa\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&Reinmutex);
if ( ++rein_ct == 9 )
{
sem_post(&Reinmutex);
printf("Reindeer[%d]: Hey, Wakeup, Santa\n", (unsigned int)a1);
fflush(stdout);
sem_post(&SantaSleep);
}
else
{
sem_post(&Reinmutex);
printf("Reindeer[%d]: Oh, Wait outsied Santa's house\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&ReinWait);
}
sem_wait(&Sleigh);
printf("Reindeer[%d]: Fly off to deliver the toys\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&ReinDone);
printf("Reindeer[%d]: I will back for long vacation on the beaches\n", (unsigned int)a1);
fflush(stdout);
if ( v1 == 1 )
pthread_exit(0LL);
v1 = 1;
}
}

original_code:
void * ReindeerRun(void * args)
{
  int i=0;
  int num = (int)args;
  printf("Reindeer[%d]: I am Reineer # %d \n", num,num);
  fflush(stdout);
  while(i<2)
  {
    sleep(1);
    printf("Reindeer[%d]: Wow, it is Christmas, back to Santa\n",num);
    fflush(stdout);
    sem_wait(&Reinmutex);
    rein_ct++;
    if(rein_ct == 9)
    {
      sem_post(&Reinmutex);
      printf("Reindeer[%d]: Hey, Wakeup, Santa\n", num);
      fflush(stdout);
      sem_post(&SantaSleep);
    }
    else
    {
      sem_post(&Reinmutex);
      printf("Reindeer[%d]: Oh, Wait outsied Santa's house\n",num);
      fflush(stdout);
      sem_wait(&ReinWait);
    }
    sem_wait(&Sleigh);
    printf("Reindeer[%d]: Fly off to deliver the toys\n",num);
    fflush(stdout);
    sem_wait(&ReinDone);
    printf("Reindeer[%d]: I will back for long vacation on the beaches\n",num);
    fflush(stdout);
    i++;
  }
  pthread_exit(((void *)0));
}

canonical_original_code:
void *ReindeerRun(void *args)
{
  int i = 0;
  int num = (int)args;
  printf("Reindeer[%d]: I am Reineer # %d \n", num,num);
  fflush(stdout);
  while(i<2)
  {
    sleep(1);
    printf("Reindeer[%d]: Wow, it is Christmas, back to Santa\n",num);
    fflush(stdout);
    sem_wait(&Reinmutex);
    rein_ct++;
    if(rein_ct == 9)
    {
      sem_post(&Reinmutex);
      printf("Reindeer[%d]: Hey, Wakeup, Santa\n", num);
      fflush(stdout);
      sem_post(&SantaSleep);
    }
    else
    {
      sem_post(&Reinmutex);
      printf("Reindeer[%d]: Oh, Wait outsied Santa's house\n",num);
      fflush(stdout);
      sem_wait(&ReinWait);
    }
    sem_wait(&Sleigh);
    printf("Reindeer[%d]: Fly off to deliver the toys\n",num);
    fflush(stdout);
    sem_wait(&ReinDone);
    printf("Reindeer[%d]: I will back for long vacation on the beaches\n",num);
    fflush(stdout);
    i++;
  }
  pthread_exit(((void *)0));
}

variable_types: num int


ElfRun

decompiled_code:
void __fastcall __noreturn ElfRun(void *a1)
{
int i; // ebp

for ( i = 2; ; i = 1 )
{
sleep(1u);
printf("Elf[%d] : Oh, it is so difficult, neet to vist Santa\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&only_elves);
sem_wait(&Elfmutex);
if ( ++elf_ct == 3 )
{
sem_post(&Elfmutex);
printf("Elf[%d] : Hey, Wake up, Santa\n", (unsigned int)a1);
fflush(stdout);
sem_post(&SantaSleep);
}
else
{
sem_post(&Elfmutex);
printf("Elf[%d] : Wait outsied Santa's house\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&Santa);
}
printf("Elf[%d] : Ask a question and wait to be solved\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&ElfProblem);
sem_wait(&ElfDone);
printf("Elf[%d] : My Problem was solved\n", (unsigned int)a1);
fflush(stdout);
printf("Elf[%d] : I will back to do the toys\n", (unsigned int)a1);
fflush(stdout);
sem_post(&only_elves);
if ( i == 1 )
pthread_exit(0LL);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func9(void *a1)
{
int i; // ebp

for ( i = 2; ; i = 1 )
{
sleep(1u);
printf("Elf[%d] : Oh, it is so difficult, neet to vist Santa\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&only_elves);
sem_wait(&Elfmutex);
if ( ++elf_ct == 3 )
{
sem_post(&Elfmutex);
printf("Elf[%d] : Hey, Wake up, Santa\n", (unsigned int)a1);
fflush(stdout);
sem_post(&SantaSleep);
}
else
{
sem_post(&Elfmutex);
printf("Elf[%d] : Wait outsied Santa's house\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&Santa);
}
printf("Elf[%d] : Ask a question and wait to be solved\n", (unsigned int)a1);
fflush(stdout);
sem_wait(&ElfProblem);
sem_wait(&ElfDone);
printf("Elf[%d] : My Problem was solved\n", (unsigned int)a1);
fflush(stdout);
printf("Elf[%d] : I will back to do the toys\n", (unsigned int)a1);
fflush(stdout);
sem_post(&only_elves);
if ( i == 1 )
pthread_exit(0LL);
}
}

original_code:
void * ElfRun(void * args)
{
  int i=0;
  int num=(int)args;
  while(i<2)
  {
    sleep(1);
    printf("Elf[%d]     : Oh, it is so difficult, neet to vist Santa\n",num);
    fflush(stdout);
    sem_wait(&only_elves);
    sem_wait(&Elfmutex);
    elf_ct++;
    if(elf_ct == 3)
    {
      sem_post(&Elfmutex);
      printf("Elf[%d]     : Hey, Wake up, Santa\n", num);
      fflush(stdout);
      sem_post(&SantaSleep);
    }
    else
    {
      sem_post(&Elfmutex);
      printf("Elf[%d]     : Wait outsied Santa's house\n",num);
      fflush(stdout);
      sem_wait(&Santa);
    }
    printf("Elf[%d]     : Ask a question and wait to be solved\n",num);
    fflush(stdout);
    sem_wait(&ElfProblem);
    sem_wait(&ElfDone);
    printf("Elf[%d]     : My Problem was solved\n",num);
    fflush(stdout);
    printf("Elf[%d]     : I will back to do the toys\n",num);
    fflush(stdout);
    sem_post(&only_elves);
    i++;
  }
  pthread_exit(((void *)0));
}

canonical_original_code:
void *ElfRun(void *args)
{
  int i = 0;
  int num = (int)args;
  while(i<2)
  {
    sleep(1);
    printf("Elf[%d]     : Oh, it is so difficult, neet to vist Santa\n",num);
    fflush(stdout);
    sem_wait(&only_elves);
    sem_wait(&Elfmutex);
    elf_ct++;
    if(elf_ct == 3)
    {
      sem_post(&Elfmutex);
      printf("Elf[%d]     : Hey, Wake up, Santa\n", num);
      fflush(stdout);
      sem_post(&SantaSleep);
    }
    else
    {
      sem_post(&Elfmutex);
      printf("Elf[%d]     : Wait outsied Santa's house\n",num);
      fflush(stdout);
      sem_wait(&Santa);
    }
    printf("Elf[%d]     : Ask a question and wait to be solved\n",num);
    fflush(stdout);
    sem_wait(&ElfProblem);
    sem_wait(&ElfDone);
    printf("Elf[%d]     : My Problem was solved\n",num);
    fflush(stdout);
    printf("Elf[%d]     : I will back to do the toys\n",num);
    fflush(stdout);
    sem_post(&only_elves);
    i++;
  }
  pthread_exit(((void *)0));
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
pthread_t *v4; // r14
pthread_t *v5; // rbp
pthread_t *v6; // r12
pthread_t *v7; // r13
char *v8; // rcx
pthread_t *v9; // rdi
pthread_t *v10; // rbx
pthread_t *v11; // r15
char *v12; // r12
char *v13; // rcx
pthread_t *v14; // rdi
pthread_t *v15; // r14
unsigned int v16; // eax
pthread_t *v17; // r13
unsigned int v18; // eax
pthread_t newthread; // [rsp+8h] [rbp-80h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-78h] BYREF

if ( argc != 1 )
{
puts("Usage: SER ");
exit(0);
}
puts("Begin run");
fflush(stdout);
sem_init(&only_elves, 0, 3u);
sem_init(&Elfmutex, 0, 1u);
sem_init(&Reinmutex, 0, 1u);
sem_init(&ReinWait, 0, 0);
sem_init(&Sleigh, 0, 0);
sem_init(&ElfDone, 0, 0);
sem_init(&Santa, 0, 0);
sem_init(&SantaSleep, 0, 0);
sem_init(&ElfProblem, 0, 0);
sem_init(&ReinDone, 0, 0);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 0);
v3 = 0LL;
newthread = (pthread_t)calloc(8uLL, 1uLL);
v4 = (pthread_t *)calloc(0x48uLL, 1uLL);
v5 = v4;
v6 = v4;
v7 = (pthread_t *)calloc(0x48uLL, 1uLL);
do
{
v8 = v3;
v9 = v6;
++v3;
++v6;
pthread_create(v9, &attr, ElfRun, v8);
}
while ( v3 != (char *)9 );
v10 = v7;
v11 = v7;
v12 = 0LL;
do
{
v13 = v12;
v14 = v11;
++v12;
++v11;
pthread_create(v14, &attr, ReindeerRun, v13);
}
while ( v12 != (char *)9 );
v15 = v4 + 9;
pthread_create(&newthread, &attr, SantaRun, 0LL);
pthread_attr_destroy(&attr);
do
{
while ( 1 )
{
v16 = pthread_join(*v5, 0LL);
if ( v16 )
break;
if ( v15 == ++v5 )
goto LABEL_11;
}
++v5;
printf("ERROR: return code from pthread_join() is %d \n", v16);
}
while ( v15 != v5 );
LABEL_11:
v17 = v7 + 9;
do
{
while ( 1 )
{
v18 = pthread_join(*v10, 0LL);
if ( v18 )
break;
if ( v17 == ++v10 )
goto LABEL_15;
}
++v10;
printf("ERROR: return code from pthread_join() is %d \n", v18);
}
while ( v17 != v10 );
LABEL_15:
if ( pthread_join(newthread, 0LL) )
{
printf("ERROR; return code from pthread_join() is %d\n", 0LL);
exit(-1);
}
puts("End run ");
sem_destroy(&only_elves);
sem_destroy(&Elfmutex);
sem_destroy(&Reinmutex);
sem_destroy(&ReinWait);
sem_destroy(&Sleigh);
sem_destroy(&ElfDone);
sem_destroy(&Santa);
sem_destroy(&SantaSleep);
sem_destroy(&ElfProblem);
sem_destroy(&ReinDone);
return 0;
}

original_code:
int main(int argc, char * argv[])
{
  if ( argc !=1)
  {
    printf("Usage: SER \n");
    exit(0);
  }
  printf("Begin run\n");
  fflush(stdout);
  sem_init(&only_elves,0,3);
  sem_init(&Elfmutex,0,1);
  sem_init(&Reinmutex,0,1);
  sem_init(&ReinWait,0,0);
  sem_init(&Sleigh,0,0);
  sem_init(&ElfDone,0,0);
  sem_init(&Santa,0,0);
  sem_init(&SantaSleep,0,0);
  sem_init(&ElfProblem,0,0);
  sem_init(&ReinDone,0,0);
     pthread_t *SantaThread;
  pthread_t *ElfThread;
  pthread_t *ReinThread;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        SantaThread = malloc(sizeof(pthread_t));
  ElfThread = malloc(sizeof(pthread_t)*9);
  ReinThread = malloc(sizeof(pthread_t)*9);
        memset(SantaThread,0,sizeof(pthread_t));
  memset(ElfThread,0,sizeof(pthread_t)*9);
  memset(ReinThread,0,sizeof(pthread_t)*9);
  int SantaRc;
  int ElfRc;
  int ReinRc;
  int rc;
  int i=0;
  for(i=0;i<9;i++)
  {
    ElfRc=pthread_create(&ElfThread[i],&attr,ElfRun,(void *)i);
  }
  for(i=0;i<9;i++)
  {
    ReinRc=pthread_create(&ReinThread[i],&attr,ReindeerRun,(void *)i);
  }
  SantaRc = pthread_create(&SantaThread,&attr,SantaRun,((void *)0));
                           pthread_attr_destroy(&attr);
  for(i=0;i<9;i++)
  {
           ElfRc=pthread_join(ElfThread[i],((void *)0));
   if(ElfRc)
   {
     printf("ERROR: return code from pthread_join() is %d \n",ElfRc);
   }
  }
     for(i=0;i<9;i++)
     {
            ReinRc=pthread_join(ReinThread[i],((void *)0));
   if(ReinRc)
   {
     printf("ERROR: return code from pthread_join() is %d \n", ReinRc);
   }
     }
     SantaRc = pthread_join(SantaThread,((void *)0));
     if (SantaRc)
  {
        printf("ERROR; return code from pthread_join() is %d\n", rc);
        exit(-1);
  }
  printf("End run \n");
  sem_destroy(&only_elves);
  sem_destroy(&Elfmutex);
     sem_destroy(&Reinmutex);
        sem_destroy(&ReinWait);
     sem_destroy(&Sleigh);
  sem_destroy(&ElfDone);
  sem_destroy(&Santa);
  sem_destroy(&SantaSleep);
     sem_destroy(&ElfProblem);
     sem_destroy(&ReinDone);
}

canonical_original_code:
int main(int argc, char *argv[])
{
  if ( argc !=1)
  {
    printf("Usage: SER \n");
    exit(0);
  }
  printf("Begin run\n");
  fflush(stdout);
  sem_init(&only_elves,0,3);
  sem_init(&Elfmutex,0,1);
  sem_init(&Reinmutex,0,1);
  sem_init(&ReinWait,0,0);
  sem_init(&Sleigh,0,0);
  sem_init(&ElfDone,0,0);
  sem_init(&Santa,0,0);
  sem_init(&SantaSleep,0,0);
  sem_init(&ElfProblem,0,0);
  sem_init(&ReinDone,0,0);
     unsigned long *SantaThread;
  unsigned long *ElfThread;
  unsigned long *ReinThread;
  union pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        SantaThread = malloc(sizeof(pthread_t));
  ElfThread = malloc(sizeof(pthread_t)*9);
  ReinThread = malloc(sizeof(pthread_t)*9);
        memset(SantaThread,0,sizeof(pthread_t));
  memset(ElfThread,0,sizeof(pthread_t)*9);
  memset(ReinThread,0,sizeof(pthread_t)*9);
  int SantaRc;
  int ElfRc;
  int ReinRc;
  int rc;
  int i = 0;
  for(i=0;i<9;i++)
  {
    ElfRc=pthread_create(&ElfThread[i],&attr,ElfRun,(void *)i);
  }
  for(i=0;i<9;i++)
  {
    ReinRc=pthread_create(&ReinThread[i],&attr,ReindeerRun,(void *)i);
  }
  SantaRc = pthread_create(&SantaThread,&attr,SantaRun,((void *)0));
                           pthread_attr_destroy(&attr);
  for(i=0;i<9;i++)
  {
           ElfRc=pthread_join(ElfThread[i],((void *)0));
   if(ElfRc)
   {
     printf("ERROR: return code from pthread_join() is %d \n",ElfRc);
   }
  }
     for(i=0;i<9;i++)
     {
            ReinRc=pthread_join(ReinThread[i],((void *)0));
   if(ReinRc)
   {
     printf("ERROR: return code from pthread_join() is %d \n", ReinRc);
   }
     }
     SantaRc = pthread_join(SantaThread,((void *)0));
     if (SantaRc)
  {
        printf("ERROR; return code from pthread_join() is %d\n", rc);
        exit(-1);
  }
  printf("End run \n");
  sem_destroy(&only_elves);
  sem_destroy(&Elfmutex);
     sem_destroy(&Reinmutex);
        sem_destroy(&ReinWait);
     sem_destroy(&Sleigh);
  sem_destroy(&ElfDone);
  sem_destroy(&Santa);
  sem_destroy(&SantaSleep);
     sem_destroy(&ElfProblem);
     sem_destroy(&ReinDone);
}

variable_types:
SantaThread
unsigned long *

variable_types:
ElfThread
unsigned long *

variable_types:
ReinThread
unsigned long *

variable_types: SantaRc int

variable_types: ElfRc int

variable_types: ReinRc int

variable_types: rc int

user_defined_types: union pthread_attr_t { char __size[56]; long __align; }


LarryRun

decompiled_code:
void __fastcall __noreturn LarryRun(void *a1)
{
unsigned int i; // ebx

for ( i = 0; i != 100; ++i )
{
sem_wait(&DigHole);
sem_wait(&Shovel);
printf("Larry digs another hole # %d \n", i);
sem_post(&Shovel);
sem_post(&Moe2go);
}
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
unsigned int i; // ebx

for ( i = 0; i != 100; ++i )
{
sem_wait(&DigHole);
sem_wait(&Shovel);
printf("Larry digs another hole # %d \n", i);
sem_post(&Shovel);
sem_post(&Moe2go);
}
pthread_exit(0LL);
}

original_code:
void * LarryRun(void * args)
{
  int i = 0;
  while(i<100)
  {
  sem_wait(&DigHole);
  sem_wait(&Shovel);
  printf("Larry digs another hole # %d \n", i);
  i++;
  sem_post(&Shovel);
  sem_post(&Moe2go);
  }
  pthread_exit(((void *)0));
}

canonical_original_code:
void *LarryRun(void *args)
{
  int i = 0;
  while(i<100)
  {
  sem_wait(&DigHole);
  sem_wait(&Shovel);
  printf("Larry digs another hole # %d \n", i);
  i++;
  sem_post(&Shovel);
  sem_post(&Moe2go);
  }
  pthread_exit(((void *)0));
}

variable_types:
args
void *

variable_types: i int


parseLine

decompiled_code:
__int64 __fastcall parseLine(char *a1, char **a2)
{
unsigned int v2; // er12
char *i; // rax

v2 = 0;
for ( i = strtok(a1, " "); i; i = strtok(0LL, " ") )
{
if ( *i == 124 && !i[1] )
break;
*a2 = i;
++v2;
++a2;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, char **a2)
{
unsigned int v2; // er12
char *i; // rax

v2 = 0;
for ( i = strtok(a1, " "); i; i = strtok(0LL, " ") )
{
if ( *i == 124 && !i[1] )
break;
*a2 = i;
++v2;
++a2;
}
return v2;
}

original_code:
int parseLine(char * line, char * command_array[]) {
  char * p;
  p=((void *)0);
  int count = 0;
  p = strtok(line, " ");
  while (p && strcmp(p,"|") != 0) {
    command_array[count] = p;
    count++;
    p = strtok(((void *)0)," ");
  }
  return count;
}

canonical_original_code:
int parseLine(char *line, char *command_array[]) {
  char *p;
  p=((void *)0);
  int count = 0;
  p = strtok(line, " ");
  while (p && strcmp(p,"|") != 0) {
    command_array[count] = p;
    count++;
    p = strtok(((void *)0)," ");
  }
  return count;
}

variable_types:
line
char *

variable_types:
p
char *

variable_types: count int