decompiled_code:
__int64 stop_motor()
{
unsigned int v0; // ebx

v0 = write_cmd(48LL, 15LL, 1LL);
if ( v0 == 1 )
syslog(30, "motor stopped");
else
syslog(27, "stopping motor failed");
return v0;
}

canonical_decompiled_code:
__int64 func9()
{
unsigned int v0; // ebx

v0 = write_cmd(48LL, 15LL, 1LL);
if ( v0 == 1 )
syslog(30, "motor stopped");
else
syslog(27, "stopping motor failed");
return v0;
}

original_code:
int stop_motor()
{
  int ok = 0;
  ok = write_cmd( 0x30, 0x0F, 1 );
  if( ok != 1 ) syslog( 3 | (3<<3), "stopping motor failed" );
  else syslog( 6 | (3<<3), "motor stopped" );
  return ok;
}


decompiled_code:
__int64 __fastcall turn_motor(int a1, int a2)
{
__int64 result; // rax

result = 0LL;
if ( a2 > 2 && maxcycles > a2 )
{
write_cmd(98LL, 2LL, 4LL);
if ( a1 == 1 )
{
write_cmd(99LL, 12319LL, 2LL);
syslog(30, "turn motor cw");
}
else if ( a1 == -1 )
{
write_cmd(99LL, 12335LL, 2LL);
syslog(30, "turn motor ccw");
}
write_cmd(100LL, 0LL, 1LL);
write_cmd(114LL, (unsigned int)a2, 4LL);
write_cmd(115LL, 12303LL, 2LL);
sprintf(message, "stop motor after %d PIC cycles", (unsigned int)a2);
syslog(30, "%s", message);
write_cmd(116LL, 0LL, 1LL);
write_cmd(97LL, 0LL, 0LL);
result = write_cmd(113LL, 0LL, 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2)
{
__int64 result; // rax

result = 0LL;
if ( a2 > 2 && maxcycles > a2 )
{
write_cmd(98LL, 2LL, 4LL);
if ( a1 == 1 )
{
write_cmd(99LL, 12319LL, 2LL);
syslog(30, "turn motor cw");
}
else if ( a1 == -1 )
{
write_cmd(99LL, 12335LL, 2LL);
syslog(30, "turn motor ccw");
}
write_cmd(100LL, 0LL, 1LL);
write_cmd(114LL, (unsigned int)a2, 4LL);
write_cmd(115LL, 12303LL, 2LL);
sprintf(message, "stop motor after %d PIC cycles", (unsigned int)a2);
syslog(30, "%s", message);
write_cmd(116LL, 0LL, 1LL);
write_cmd(97LL, 0LL, 0LL);
result = write_cmd(113LL, 0LL, 0LL);
}
return result;
}

original_code:
int turn_motor(int rotcw, int cycles)
{
  int ok = 0;
  if( cycles > 2 && cycles < maxcycles )
  {
     ok = write_cmd( 0x62, 2, 4 );
     if( rotcw == +1 )
     {
       ok = write_cmd( 0x63, 0x301F, 2 );
       syslog( 6 | (3<<3), "turn motor cw" );
     }
     else if( rotcw == -1 )
     {
       ok = write_cmd( 0x63, 0x302F, 2 );
       syslog( 6 | (3<<3), "turn motor ccw" );
     }
     ok = write_cmd( 0x64, 0, 1 );
     ok = write_cmd( 0x72, cycles, 4 );
     ok = write_cmd( 0x73, 0x300F, 2 );
     sprintf( message, "stop motor after %d PIC cycles", cycles );
     syslog( 6 | (3<<3), "%s", message );
     ok = write_cmd( 0x74, 0 ,1 );
     ok = write_cmd( 0x61, 0 , 0 );
     ok = write_cmd( 0x71, 0 , 0 );
  }
  return ok;
}

variable_types: rotcw int

variable_types: cycles int

variable_types: ok int

return_type: int


decompiled_code:
__int64 __fastcall goto_pos(int a1)
{
unsigned int v1; // ebx
int v2; // ebp
int v3; // eax
int v5; // er12

if ( minpos <= a1 && (v1 = 2 * (mpos >= a1) - 1, maxpos >= a1) && (unsigned int)mpos <= 0x3FF )
{
v5 = abs32((int)(*(float *)&rotmax * 60.0 * (double)(a1 - mpos) / (*(float *)&motrpm * 1024.0 * *(float *)&picycle)));
v2 = (int)(float)((float)v5 * *(float *)&picycle);
sprintf(message, "turning time %d PIC cycles and direction %d", (unsigned int)v5, v1);
syslog(30, "%s", message);
v3 = turn_motor(v1, v5);
}
else
{
sprintf(message, "motor position out of range [%d-%d]", (unsigned int)minpos, (unsigned int)maxpos);
syslog(29, "%s", message);
v2 = 0;
v3 = 0;
}
return (unsigned int)(v2 * v3);
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
unsigned int v1; // ebx
int v2; // ebp
int v3; // eax
int v5; // er12

if ( minpos <= a1 && (v1 = 2 * (mpos >= a1) - 1, maxpos >= a1) && (unsigned int)mpos <= 0x3FF )
{
v5 = abs32((int)(*(float *)&rotmax * 60.0 * (double)(a1 - mpos) / (*(float *)&motrpm * 1024.0 * *(float *)&picycle)));
v2 = (int)(float)((float)v5 * *(float *)&picycle);
sprintf(message, "turning time %d PIC cycles and direction %d", (unsigned int)v5, v1);
syslog(30, "%s", message);
v3 = func10(v1, v5);
}
else
{
sprintf(message, "motor position out of range [%d-%d]", (unsigned int)minpos, (unsigned int)maxpos);
syslog(29, "%s", message);
v2 = 0;
v3 = 0;
}
return (unsigned int)(v2 * v3);
}

original_code:
int goto_pos(int topos)
{
  int ok = 0;
  int dt = 0;
  int rotcw = 1;
  if( topos > mpos ) rotcw = -1;
  int cycles = 0;
  if( topos >= minpos && topos <= maxpos && mpos >= 0 && mpos < 1024 )
  {
    cycles = (int)abs( rotmax * 60.0 * ( topos - mpos ) / ( 1024.0 * motrpm * picycle ) );
    dt = cycles * picycle;
    sprintf( message, "turning time %d PIC cycles and direction %d", cycles, rotcw );
    syslog( 6 | (3<<3), "%s", message );
    ok = turn_motor( rotcw, cycles );
  }
  else
  {
    sprintf( message, "motor position out of range [%d-%d]", minpos, maxpos );
    syslog( 5 | (3<<3), "%s", message );
  }
  return ( dt * ok );
}

variable_types: topos int

variable_types: dt int


decompiled_code:
__int64 __fastcall set_pos(int a1)
{
int v1; // eax
int v2; // eax

v1 = goto_pos((int)((double)(a1 - mpos) * 0.9 + (double)mpos));
sleep(v1 + 1);
mpos = read_motorpos();
v2 = goto_pos(a1);
sleep(v2 + 1);
mpos = read_motorpos();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1)
{
int v1; // eax
int v2; // eax

v1 = func11((int)((double)(a1 - mpos) * 0.9 + (double)mpos));
sleep(v1 + 1);
mpos = func7();
v2 = func11(a1);
sleep(v2 + 1);
mpos = func7();
return 0LL;
}

original_code:
int set_pos(int topos)
{
  int ok = 0;
  int nxtpos = (int)( 0.9 * ( topos - mpos ) + mpos );
  int dt = goto_pos( nxtpos );
  sleep( dt + 1 );
  mpos = read_motorpos();
  dt = goto_pos( topos );
  sleep( dt + 1 );
  mpos = read_motorpos();
  return ok;
}

variable_types: nxtpos int


resetimer

decompiled_code:
__int64 resetimer()
{
return write_cmd(80LL, 0LL, 0LL);
}

canonical_decompiled_code:
__int64 func13()
{
return write_cmd(80LL, 0LL, 0LL);
}

original_code:
int resetimer()
{
  int ok = -1;
  ok = write_cmd( 0x50, 0, 0 );
  return ok;
}


printime

decompiled_code:
__int64 printime()
{
unsigned int v0; // ebx
struct tm *v1; // rax
char v3[40]; // [rsp+0h] [rbp-38h] BYREF
time_t timer; // [rsp+28h] [rbp-10h] BYREF

v0 = time(&timer);
v1 = localtime(&timer);
strftime(v3, 0x19uLL, "%Y-%m-%d %H:%M:%S ", v1);
printf("%s", v3);
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // ebx
struct tm *v1; // rax
char v3[40]; // [rsp+0h] [rbp-38h] BYREF
time_t timer; // [rsp+28h] [rbp-10h] BYREF

v0 = time(&timer);
v1 = localtime(&timer);
strftime(v3, 0x19uLL, "%Y-%m-%d %H:%M:%S ", v1);
printf("%s", v3);
return v0;
}

original_code:
int printime()
{
  time_t now;
  char tstr[25];
  struct tm* tm_info;
  int utime=0;
  utime=time(&now);
  tm_info=localtime(&now);
  strftime(tstr,25,"%Y-%m-%d %H:%M:%S ",tm_info);
  printf("%s",tstr);
  return utime;
}

canonical_original_code:
int printime()
{
  long now;
  char tstr[25];
  struct tm *tm_info;
  int utime = 0;
  utime=time(&now);
  tm_info=localtime(&now);
  strftime(tstr,25,"%Y-%m-%d %H:%M:%S ",tm_info);
  printf("%s",tstr);
  return utime;
}

variable_types: now long

variable_types:
tstr
char [25]

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: utime int


printusage

decompiled_code:
int printusage()
{
return puts("usage: pipictest -a address [-n N] [-i] [-c] [-0] [-1] [-3] [-h] [-v] [-V]");
}

canonical_decompiled_code:
int func1()
{
return puts("usage: pipictest -a address [-n N] [-i] [-c] [-0] [-1] [-3] [-h] [-v] [-V]");
}

original_code:
void printusage()
{
  printf("usage: pipictest -a address [-n N] [-i] [-c] [-0] [-1] [-3] [-h] [-v] [-V]\n");
}

return_type: void


printversion

decompiled_code:
int printversion()
{
return puts("pipictest v. 20140911, Jaakko Koivuniemi");
}

canonical_decompiled_code:
int func2()
{
return puts("pipictest v. 20140911, Jaakko Koivuniemi");
}

original_code:
void printversion()
{
  printf("pipictest v. 20140911, Jaakko Koivuniemi\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r15
int v4; // er14
int v5; // eax
int v6; // ebx
int v8; // eax
int v9; // er12
unsigned int v10; // edi
int v11; // er13
char v12; // bp
unsigned int v13; // edi
__int64 v14; // rbx
__int64 i; // rbx
char v16; // al
unsigned __int8 v17; // bl
int v18; // [rsp+8h] [rbp-F0h]
int v19; // [rsp+Ch] [rbp-ECh]
int v20; // [rsp+10h] [rbp-E8h]
int v21; // [rsp+14h] [rbp-E4h]
int v22; // [rsp+18h] [rbp-E0h]
char v23; // [rsp+1Fh] [rbp-D9h]
unsigned int v24; // [rsp+20h] [rbp-D8h]
unsigned int v25; // [rsp+24h] [rbp-D4h]
int v26; // [rsp+2Ch] [rbp-CCh]
char v27; // [rsp+37h] [rbp-C1h]
unsigned __int8 v28; // [rsp+38h] [rbp-C0h]
unsigned __int8 v29; // [rsp+39h] [rbp-BFh]
unsigned __int8 v30; // [rsp+3Ah] [rbp-BEh]
unsigned __int8 v31; // [rsp+3Bh] [rbp-BDh]
unsigned __int8 buf; // [rsp+42h] [rbp-B6h] BYREF
unsigned __int8 v33; // [rsp+43h] [rbp-B5h]
unsigned __int8 v34; // [rsp+44h] [rbp-B4h]
unsigned __int8 v35; // [rsp+45h] [rbp-B3h]
unsigned __int8 v36; // [rsp+46h] [rbp-B2h]
unsigned int v37; // [rsp+4Ch] [rbp-ACh] BYREF
char file[16]; // [rsp+50h] [rbp-A8h] BYREF
__int64 v39; // [rsp+60h] [rbp-98h]
__int64 v40; // [rsp+68h] [rbp-90h]
__int64 v41; // [rsp+70h] [rbp-88h]
__int64 v42; // [rsp+78h] [rbp-80h]
__int64 v43; // [rsp+80h] [rbp-78h]
__int64 v44; // [rsp+88h] [rbp-70h]
__int64 v45; // [rsp+90h] [rbp-68h]
__int64 v46; // [rsp+98h] [rbp-60h]
__int64 v47; // [rsp+A0h] [rbp-58h]
__int64 v48; // [rsp+A8h] [rbp-50h]
int v49; // [rsp+B0h] [rbp-48h]

strcpy(file, "/dev/i2c-1");
v39 = 0LL;
v40 = 0LL;
v41 = 0LL;
v42 = 0LL;
v43 = 0LL;
v44 = 0LL;
v45 = 0LL;
v46 = 0LL;
v47 = 0LL;
v48 = 0LL;
v49 = 0;
v37 = 0;
v21 = 1;
v20 = 0;
v18 = 0;
v19 = 0;
v22 = 0;
v3 = 0;
v4 = 0;
do
{
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "a:n:i013chvV");
v6 = v5;
switch ( v5 )
{
case 'a':
_isoc99_sscanf(optarg, "%X", &v37);
break;
case 'n':
v21 = atoi(optarg);
LABEL_18:
if ( v6 == 48 )
{
v19 = 1;
goto LABEL_20;
}
LABEL_12:
if ( v6 == 49 )
{
v18 = 1;
goto LABEL_14;
}
goto LABEL_5;
case 'i':
v3 = 1;
goto LABEL_12;
}
if ( v6 != 99 )
goto LABEL_18;
v22 = 1;
LABEL_5:
if ( v6 == 51 )
{
v20 = 1;
goto LABEL_7;
}
LABEL_20:
if ( v6 != 118 )
break;
v4 = 1;
}
LABEL_14:
if ( v6 == 104 )
{
printusage();
return 0;
}
if ( v6 == 86 )
{
printversion();
return 0;
}
LABEL_7:
;
}
while ( v6 != -1 );
if ( v37 - 3 > 0x74 )
{
printusage();
return v6;
}
if ( v4 != 1 )
{
v8 = open(file, 2);
v9 = v8;
if ( v8 >= 0 )
{
if ( ioctl(v8, 0x703uLL, v37) >= 0 )
{
v23 = 0;
LOBYTE(v4) = 0;
goto LABEL_29;
}
LABEL_38:
perror("Unable to get bus access to talk to slave");
return v6;
}
LABEL_37:
perror("Failed to open i2c port");
return v6;
}
printf("Open %s\n", file);
v9 = open(file, 2);
if ( v9 < 0 )
goto LABEL_37;
printf("Chip address 0x%02x\n", v37);
if ( ioctl(v9, 0x703uLL, v37) < 0 )
goto LABEL_38;
v12 = v3 & 1;
v23 = v3 & 1;
if ( (v3 & 1) != 0 )
{
puts(" send received");
v3 &= 1u;
LOBYTE(v4) = v12;
}
LABEL_29:
if ( v22 != 1 )
{
v10 = time(0LL);
srand(v10);
v26 = v22;
if ( v21 <= 0 )
{
v25 = v22;
goto LABEL_104;
}
goto LABEL_31;
}
puts("reset timer");
v26 = printime();
buf = 80;
if ( write(v9, &buf, 1uLL) != 1 )
{
perror("Error writing to i2c slave");
return v6;
}
puts("cycles");
v13 = time(0LL);
srand(v13);
if ( v21 > 0 )
{
LABEL_31:
v24 = 0;
v25 = 0;
v11 = 0;
while ( 1 )
{
if ( v3 )
{
if ( v23 )
printime();
for ( i = 1LL; i != 5; ++i )
{
v16 = rand();
*(&buf + i) = v16;
*(&v27 + i) = v16;
}
buf = 2;
if ( v23 )
{
printf("0x%02x%02x%02x%02x", v33, v34, v35, v36);
printf(" ");
}
if ( write(v9, &buf, 5uLL) != 5 )
{
LABEL_101:
v6 = -1;
perror("Error writing to i2c slave");
return v6;
}
if ( read(v9, &buf, 4uLL) != 4 )
goto LABEL_102;
if ( v23 )
printf("0x%02x%02x%02x%02x\n", buf, v33, v34, v35);
v17 = v28;
if ( v28 == buf && v29 == v33 && v30 == v34 && v31 == v35 )
{
v25 = 0;
}
else
{
printime();
printf("0x%02x%02x%02x%02x", v17, v29, v30, v31);
printf(" !=");
printf("0x%02x%02x%02x%02x\n", buf, v33, v34, v35);
v25 = 1;
}
}
if ( v22 == 1 )
{
if ( (_BYTE)v4 )
printime();
buf = 81;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 4uLL) != 4 )
goto LABEL_102;
if ( (_BYTE)v4 )
{
printf("0x%02x%02x%02x%02x ", buf, v33, v34, v35);
v24 = v35 + ((v34 + ((v33 + (buf << 8)) << 8)) << 8);
printf("%d\n", v24);
}
else
{
v24 = v35 + ((v34 + ((v33 + (buf << 8)) << 8)) << 8);
}
}
if ( !((unsigned __int8)v20 | (unsigned __int8)(v19 | v18)) )
goto LABEL_90;
printime();
if ( v19 == 1 )
{
buf = 64;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 2uLL) != 2 )
goto LABEL_102;
if ( (_BYTE)v4 )
printf("0x%02x%02x ", buf, v33);
printf("%d ", v33 + (buf << 8));
}
if ( v18 != 1 )
break;
buf = 65;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 2uLL) != 2 )
goto LABEL_102;
if ( (_BYTE)v4 )
printf("0x%02x%02x ", buf, v33);
printf("%d ", v33 + (buf << 8));
if ( v20 == 1 )
goto LABEL_84;
LABEL_89:
putchar(10);
LABEL_90:
if ( ++v11 >= v21 )
{
if ( v22 != 1 )
goto LABEL_104;
goto LABEL_42;
}
}
if ( v20 != 1 )
{
if ( !((unsigned __int8)v20 | (unsigned __int8)(v19 | v18)) )
goto LABEL_90;
goto LABEL_89;
}
LABEL_84:
buf = 67;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 2uLL) != 2 )
{
LABEL_102:
v6 = -1;
perror("Unable to read from slave");
return v6;
}
if ( (_BYTE)v4 )
printf("0x%02x%02x ", buf, v33);
printf("%d ", v33 + (buf << 8));
goto LABEL_89;
}
v24 = 0;
v25 = 0;
LABEL_42:
v14 = (unsigned int)printime() - v26;
printf("elapsed seconds %d for %d cycles\n", v14, v24);
if ( (int)v14 > 0 )
printf("%f s/cycle\n", (float)((float)(int)v14 / (float)(int)v24));
LABEL_104:
v6 = 0;
if ( v3 )
printf("i2c errors %d\n", v25);
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r15
int v4; // er14
int v5; // eax
int v6; // ebx
int v8; // eax
int v9; // er12
unsigned int v10; // edi
int v11; // er13
char v12; // bp
unsigned int v13; // edi
__int64 v14; // rbx
__int64 i; // rbx
char v16; // al
unsigned __int8 v17; // bl
int v18; // [rsp+8h] [rbp-F0h]
int v19; // [rsp+Ch] [rbp-ECh]
int v20; // [rsp+10h] [rbp-E8h]
int v21; // [rsp+14h] [rbp-E4h]
int v22; // [rsp+18h] [rbp-E0h]
char v23; // [rsp+1Fh] [rbp-D9h]
unsigned int v24; // [rsp+20h] [rbp-D8h]
unsigned int v25; // [rsp+24h] [rbp-D4h]
int v26; // [rsp+2Ch] [rbp-CCh]
char v27; // [rsp+37h] [rbp-C1h]
unsigned __int8 v28; // [rsp+38h] [rbp-C0h]
unsigned __int8 v29; // [rsp+39h] [rbp-BFh]
unsigned __int8 v30; // [rsp+3Ah] [rbp-BEh]
unsigned __int8 v31; // [rsp+3Bh] [rbp-BDh]
unsigned __int8 buf; // [rsp+42h] [rbp-B6h] BYREF
unsigned __int8 v33; // [rsp+43h] [rbp-B5h]
unsigned __int8 v34; // [rsp+44h] [rbp-B4h]
unsigned __int8 v35; // [rsp+45h] [rbp-B3h]
unsigned __int8 v36; // [rsp+46h] [rbp-B2h]
unsigned int v37; // [rsp+4Ch] [rbp-ACh] BYREF
char file[16]; // [rsp+50h] [rbp-A8h] BYREF
__int64 v39; // [rsp+60h] [rbp-98h]
__int64 v40; // [rsp+68h] [rbp-90h]
__int64 v41; // [rsp+70h] [rbp-88h]
__int64 v42; // [rsp+78h] [rbp-80h]
__int64 v43; // [rsp+80h] [rbp-78h]
__int64 v44; // [rsp+88h] [rbp-70h]
__int64 v45; // [rsp+90h] [rbp-68h]
__int64 v46; // [rsp+98h] [rbp-60h]
__int64 v47; // [rsp+A0h] [rbp-58h]
__int64 v48; // [rsp+A8h] [rbp-50h]
int v49; // [rsp+B0h] [rbp-48h]

strcpy(file, "/dev/i2c-1");
v39 = 0LL;
v40 = 0LL;
v41 = 0LL;
v42 = 0LL;
v43 = 0LL;
v44 = 0LL;
v45 = 0LL;
v46 = 0LL;
v47 = 0LL;
v48 = 0LL;
v49 = 0;
v37 = 0;
v21 = 1;
v20 = 0;
v18 = 0;
v19 = 0;
v22 = 0;
v3 = 0;
v4 = 0;
do
{
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "a:n:i013chvV");
v6 = v5;
switch ( v5 )
{
case 'a':
_isoc99_sscanf(optarg, "%X", &v37);
break;
case 'n':
v21 = atoi(optarg);
LABEL_18:
if ( v6 == 48 )
{
v19 = 1;
goto LABEL_20;
}
LABEL_12:
if ( v6 == 49 )
{
v18 = 1;
goto LABEL_14;
}
goto LABEL_5;
case 'i':
v3 = 1;
goto LABEL_12;
}
if ( v6 != 99 )
goto LABEL_18;
v22 = 1;
LABEL_5:
if ( v6 == 51 )
{
v20 = 1;
goto LABEL_7;
}
LABEL_20:
if ( v6 != 118 )
break;
v4 = 1;
}
LABEL_14:
if ( v6 == 104 )
{
func1();
return 0;
}
if ( v6 == 86 )
{
func2();
return 0;
}
LABEL_7:
;
}
while ( v6 != -1 );
if ( v37 - 3 > 0x74 )
{
func1();
return v6;
}
if ( v4 != 1 )
{
v8 = open(file, 2);
v9 = v8;
if ( v8 >= 0 )
{
if ( ioctl(v8, 0x703uLL, v37) >= 0 )
{
v23 = 0;
LOBYTE(v4) = 0;
goto LABEL_29;
}
LABEL_38:
perror("Unable to get bus access to talk to slave");
return v6;
}
LABEL_37:
perror("Failed to open i2c port");
return v6;
}
printf("Open %s\n", file);
v9 = open(file, 2);
if ( v9 < 0 )
goto LABEL_37;
printf("Chip address 0x%02x\n", v37);
if ( ioctl(v9, 0x703uLL, v37) < 0 )
goto LABEL_38;
v12 = v3 & 1;
v23 = v3 & 1;
if ( (v3 & 1) != 0 )
{
puts(" send received");
v3 &= 1u;
LOBYTE(v4) = v12;
}
LABEL_29:
if ( v22 != 1 )
{
v10 = time(0LL);
srand(v10);
v26 = v22;
if ( v21 <= 0 )
{
v25 = v22;
goto LABEL_104;
}
goto LABEL_31;
}
puts("reset timer");
v26 = func0();
buf = 80;
if ( write(v9, &buf, 1uLL) != 1 )
{
perror("Error writing to i2c slave");
return v6;
}
puts("cycles");
v13 = time(0LL);
srand(v13);
if ( v21 > 0 )
{
LABEL_31:
v24 = 0;
v25 = 0;
v11 = 0;
while ( 1 )
{
if ( v3 )
{
if ( v23 )
func0();
for ( i = 1LL; i != 5; ++i )
{
v16 = rand();
*(&buf + i) = v16;
*(&v27 + i) = v16;
}
buf = 2;
if ( v23 )
{
printf("0x%02x%02x%02x%02x", v33, v34, v35, v36);
printf(" ");
}
if ( write(v9, &buf, 5uLL) != 5 )
{
LABEL_101:
v6 = -1;
perror("Error writing to i2c slave");
return v6;
}
if ( read(v9, &buf, 4uLL) != 4 )
goto LABEL_102;
if ( v23 )
printf("0x%02x%02x%02x%02x\n", buf, v33, v34, v35);
v17 = v28;
if ( v28 == buf && v29 == v33 && v30 == v34 && v31 == v35 )
{
v25 = 0;
}
else
{
func0();
printf("0x%02x%02x%02x%02x", v17, v29, v30, v31);
printf(" !=");
printf("0x%02x%02x%02x%02x\n", buf, v33, v34, v35);
v25 = 1;
}
}
if ( v22 == 1 )
{
if ( (_BYTE)v4 )
func0();
buf = 81;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 4uLL) != 4 )
goto LABEL_102;
if ( (_BYTE)v4 )
{
printf("0x%02x%02x%02x%02x ", buf, v33, v34, v35);
v24 = v35 + ((v34 + ((v33 + (buf << 8)) << 8)) << 8);
printf("%d\n", v24);
}
else
{
v24 = v35 + ((v34 + ((v33 + (buf << 8)) << 8)) << 8);
}
}
if ( !((unsigned __int8)v20 | (unsigned __int8)(v19 | v18)) )
goto LABEL_90;
func0();
if ( v19 == 1 )
{
buf = 64;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 2uLL) != 2 )
goto LABEL_102;
if ( (_BYTE)v4 )
printf("0x%02x%02x ", buf, v33);
printf("%d ", v33 + (buf << 8));
}
if ( v18 != 1 )
break;
buf = 65;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 2uLL) != 2 )
goto LABEL_102;
if ( (_BYTE)v4 )
printf("0x%02x%02x ", buf, v33);
printf("%d ", v33 + (buf << 8));
if ( v20 == 1 )
goto LABEL_84;
LABEL_89:
putchar(10);
LABEL_90:
if ( ++v11 >= v21 )
{
if ( v22 != 1 )
goto LABEL_104;
goto LABEL_42;
}
}
if ( v20 != 1 )
{
if ( !((unsigned __int8)v20 | (unsigned __int8)(v19 | v18)) )
goto LABEL_90;
goto LABEL_89;
}
LABEL_84:
buf = 67;
if ( write(v9, &buf, 1uLL) != 1 )
goto LABEL_101;
if ( read(v9, &buf, 2uLL) != 2 )
{
LABEL_102:
v6 = -1;
perror("Unable to read from slave");
return v6;
}
if ( (_BYTE)v4 )
printf("0x%02x%02x ", buf, v33);
printf("%d ", v33 + (buf << 8));
goto LABEL_89;
}
v24 = 0;
v25 = 0;
LABEL_42:
v14 = (unsigned int)func0() - v26;
printf("elapsed seconds %d for %d cycles\n", v14, v24);
if ( (int)v14 > 0 )
printf("%f s/cycle\n", (float)((float)(int)v14 / (float)(int)v24));
LABEL_104:
v6 = 0;
if ( v3 )
printf("i2c errors %d\n", v25);
return v6;
}

original_code:
int main(int argc, char **argv)
{
  int verb=0;
  int testi2c=0;
  int ccycle=0;
  int analog0=0;
  int analog1=0;
  int analog3=0;
  int fd;
  char fileName[100]="/dev/i2c-1";
  int address=0x00;
  unsigned char buf[10];
  unsigned char send[10];
  int i=0,j=0;
  int errcnt=0;
  int nrpt=1;
  int rtime=0;
  int t1=0,t2=0,dt=0;
  int ain0=0,ain1=0,ain3=0;
  int optch=0;
  while(optch!=-1)
    {
      optch=getopt(argc,argv,"a:n:i013chvV");
      if(optch=='a')
 {
   sscanf(optarg,"%X",&address);
 }
      if(optch=='n')
 {
   nrpt=atoi(optarg);
 }
      if(optch=='i')
 {
          testi2c=1;
 }
      if(optch=='c')
 {
          ccycle=1;
 }
      if(optch=='0')
 {
          analog0=1;
 }
      if(optch=='1')
 {
          analog1=1;
 }
      if(optch=='3')
 {
          analog3=1;
 }
      if(optch=='v')
 {
          verb=1;
 }
      if(optch=='h')
 {
   printusage();
   return 0;
 }
      if(optch=='V')
 {
   printversion();
   return 0;
 }
    }
  if((address<0x03)||(address>0x77))
    {
      printusage();
      return -1;
    }
  if(verb==1) printf("Open %s\n", fileName);
  if((fd = open(fileName, 02)) < 0)
  {
     perror("Failed to open i2c port");
     return -1;
  }
  if(verb==1) printf("Chip address 0x%02x\n", address);
  if(ioctl(fd, 0x0703, address) < 0)
  {
     perror("Unable to get bus access to talk to slave");
     return -1;
  }
  if((verb==1)&&(testi2c==1)) printf("                    send         received\n");
  if(ccycle==1)
  {
     printf("reset timer\n");
     t1=printime();
     buf[0]=0x50;
     if((write(fd, buf, 1)) != 1)
     {
          perror("Error writing to i2c slave");
          return -1;
     }
     printf("cycles\n");
  }
  srand((unsigned int)time(((void *)0)));
  for(j=0;j<nrpt;j++)
  {
     if(testi2c==1)
     {
        if(verb==1) printime();
        errcnt=0;
        for(i=0;i<4;i++)
        {
           buf[i+1]=rand();
           send[i]=buf[i+1];
        }
        buf[0]=2;
        if(verb==1)
        {
           printf("0x%02x%02x%02x%02x",buf[1],buf[2],buf[3],buf[4]);
           printf("   ");
        }
        if((write(fd, buf, 5)) != 5)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,4)!=4)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x%02x%02x\n",buf[0],buf[1],buf[2],buf[3]);
           if((send[0]!=buf[0])||(send[1]!=buf[1])||(send[2]!=buf[2])||(send[3]!=buf[3]))
           {
              printime();
              printf("0x%02x%02x%02x%02x",send[0],send[1],send[2],send[3]);
              printf(" !=");
              printf("0x%02x%02x%02x%02x\n",buf[0],buf[1],buf[2],buf[3]);
              errcnt++;
           }
        }
     }
     if(ccycle==1)
     {
        if(verb==1) t2=printime();
        buf[0]=0x51;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,4)!=4)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x%02x%02x ",buf[0],buf[1],buf[2],buf[3]);
           rtime=16777216*buf[0]+65536*buf[1]+256*buf[2]+buf[3];
           if(verb==1) printf ("%d\n",rtime);
        }
     }
     if((analog0==1)||(analog1==1)||(analog3==1)) t2=printime();
     if(analog0==1)
     {
        buf[0]=0x40;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain0=256*buf[0]+buf[1];
           printf ("%d ",ain0);
        }
     }
     if(analog1==1)
     {
        buf[0]=0x41;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain1=256*buf[0]+buf[1];
           printf ("%d ",ain1);
        }
     }
     if(analog3==1)
     {
        buf[0]=0x43;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain3=256*buf[0]+buf[1];
           printf ("%d ",ain3);
        }
     }
     if((analog0==1)||(analog1==1)||(analog3==1)) printf("\n");
  }
  if(ccycle==1)
  {
    t2=printime();
    dt=t2-t1;
    printf("elapsed seconds %d for %d cycles\n",dt,rtime);
    if(dt>0) printf("%f s/cycle\n",((float)dt)/((float)rtime));
  }
  if(testi2c==1) printf("i2c errors %d\n",errcnt);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int verb = 0;
  int testi2c = 0;
  int ccycle = 0;
  int analog0 = 0;
  int analog1 = 0;
  int analog3 = 0;
  int fd;
  char fileName[100] = "/dev/i2c-1";
  int address = 0x00;
  unsigned char buf[10];
  unsigned char send[10];
  int i = 0; int j = 0;
  int errcnt = 0;
  int nrpt = 1;
  int rtime = 0;
  int t1 = 0; int t2 = 0; int dt = 0;
  int ain0 = 0; int ain1 = 0; int ain3 = 0;
  int optch = 0;
  while(optch!=-1)
    {
      optch=getopt(argc,argv,"a:n:i013chvV");
      if(optch=='a')
 {
   sscanf(optarg,"%X",&address);
 }
      if(optch=='n')
 {
   nrpt=atoi(optarg);
 }
      if(optch=='i')
 {
          testi2c=1;
 }
      if(optch=='c')
 {
          ccycle=1;
 }
      if(optch=='0')
 {
          analog0=1;
 }
      if(optch=='1')
 {
          analog1=1;
 }
      if(optch=='3')
 {
          analog3=1;
 }
      if(optch=='v')
 {
          verb=1;
 }
      if(optch=='h')
 {
   printusage();
   return 0;
 }
      if(optch=='V')
 {
   printversion();
   return 0;
 }
    }
  if((address<0x03)||(address>0x77))
    {
      printusage();
      return -1;
    }
  if(verb==1) printf("Open %s\n", fileName);
  if((fd = open(fileName, 02)) < 0)
  {
     perror("Failed to open i2c port");
     return -1;
  }
  if(verb==1) printf("Chip address 0x%02x\n", address);
  if(ioctl(fd, 0x0703, address) < 0)
  {
     perror("Unable to get bus access to talk to slave");
     return -1;
  }
  if((verb==1)&&(testi2c==1)) printf("                    send         received\n");
  if(ccycle==1)
  {
     printf("reset timer\n");
     t1=printime();
     buf[0]=0x50;
     if((write(fd, buf, 1)) != 1)
     {
          perror("Error writing to i2c slave");
          return -1;
     }
     printf("cycles\n");
  }
  srand((unsigned int)time(((void *)0)));
  for(j=0;j<nrpt;j++)
  {
     if(testi2c==1)
     {
        if(verb==1) printime();
        errcnt=0;
        for(i=0;i<4;i++)
        {
           buf[i+1]=rand();
           send[i]=buf[i+1];
        }
        buf[0]=2;
        if(verb==1)
        {
           printf("0x%02x%02x%02x%02x",buf[1],buf[2],buf[3],buf[4]);
           printf("   ");
        }
        if((write(fd, buf, 5)) != 5)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,4)!=4)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x%02x%02x\n",buf[0],buf[1],buf[2],buf[3]);
           if((send[0]!=buf[0])||(send[1]!=buf[1])||(send[2]!=buf[2])||(send[3]!=buf[3]))
           {
              printime();
              printf("0x%02x%02x%02x%02x",send[0],send[1],send[2],send[3]);
              printf(" !=");
              printf("0x%02x%02x%02x%02x\n",buf[0],buf[1],buf[2],buf[3]);
              errcnt++;
           }
        }
     }
     if(ccycle==1)
     {
        if(verb==1) t2=printime();
        buf[0]=0x51;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,4)!=4)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x%02x%02x ",buf[0],buf[1],buf[2],buf[3]);
           rtime=16777216*buf[0]+65536*buf[1]+256*buf[2]+buf[3];
           if(verb==1) printf ("%d\n",rtime);
        }
     }
     if((analog0==1)||(analog1==1)||(analog3==1)) t2=printime();
     if(analog0==1)
     {
        buf[0]=0x40;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain0=256*buf[0]+buf[1];
           printf ("%d ",ain0);
        }
     }
     if(analog1==1)
     {
        buf[0]=0x41;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain1=256*buf[0]+buf[1];
           printf ("%d ",ain1);
        }
     }
     if(analog3==1)
     {
        buf[0]=0x43;
        if((write(fd, buf, 1)) != 1)
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2)
        {
           perror("Unable to read from slave");
           return -1;
        }
        else
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain3=256*buf[0]+buf[1];
           printf ("%d ",ain3);
        }
     }
     if((analog0==1)||(analog1==1)||(analog3==1)) printf("\n");
  }
  if(ccycle==1)
  {
    t2=printime();
    dt=t2-t1;
    printf("elapsed seconds %d for %d cycles\n",dt,rtime);
    if(dt>0) printf("%f s/cycle\n",((float)dt)/((float)rtime));
  }
  if(testi2c==1) printf("i2c errors %d\n",errcnt);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: verb int

variable_types: ccycle int

variable_types: fd int

variable_types:
fileName
char [100]

variable_types: address int

variable_types:
buf
unsigned char [10]

variable_types:
send
unsigned char [10]

variable_types: i int

variable_types: j int

variable_types: errcnt int

variable_types: nrpt int

variable_types: rtime int

variable_types: optch int


decompiled_code:
__int64 __fastcall read_data(int a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // ebx
int v4; // er12
__int64 v6[26]; // [rsp+0h] [rbp-108h] BYREF
unsigned __int8 buf; // [rsp+D6h] [rbp-32h] BYREF
unsigned __int8 v8; // [rsp+D7h] [rbp-31h]
unsigned __int8 v9; // [rsp+D8h] [rbp-30h]
unsigned __int8 v10; // [rsp+D9h] [rbp-2Fh]

v6[0] = 0LL;
v6[1] = 0LL;
v6[2] = 0LL;
v6[3] = 0LL;
v6[4] = 0LL;
v6[5] = 0LL;
v6[6] = 0LL;
v6[7] = 0LL;
v6[8] = 0LL;
v6[9] = 0LL;
v6[10] = 0LL;
v6[11] = 0LL;
v6[12] = 0LL;
v6[13] = 0LL;
v6[14] = 0LL;
v6[15] = 0LL;
v6[16] = 0LL;
v6[17] = 0LL;
v6[18] = 0LL;
v6[19] = 0LL;
v6[20] = 0LL;
v6[21] = 0LL;
v6[22] = 0LL;
v6[23] = 0LL;
v6[24] = 0LL;
v1 = open(i2cdev, 2);
if ( v1 < 0 )
{
syslog(3, "Failed to open i2c port");
return (unsigned int)-1;
}
v2 = v1;
v3 = flock(v1, 6);
v4 = i2lockmax;
if ( v3 && i2lockmax > 0 )
{
do
{
sleep(1u);
v3 = flock(v2, 6);
--v4;
}
while ( v3 && v4 > 0 );
}
if ( v3 )
{
syslog(3, "Failed to lock i2c port");
close(v2);
return (unsigned int)-2;
}
if ( ioctl(v2, 0x703uLL, address) < 0 )
{
syslog(3, "Unable to get bus access to talk to slave");
close(v2);
return (unsigned int)-3;
}
switch ( a1 )
{
case 1:
if ( read(v2, &buf, 1uLL) == 1 )
{
sprintf((char *)v6, "Receive 0x%02x", buf);
syslog(7, "%s", (const char *)v6);
v3 = buf;
break;
}
LABEL_24:
syslog(3, "Unable to read from slave");
close(v2);
return (unsigned int)-4;
case 2:
if ( read(v2, &buf, 2uLL) == 2 )
{
sprintf((char *)v6, "Receive 0x%02x%02x", buf, v8);
syslog(7, "%s", (const char *)v6);
v3 = v8 + (buf << 8);
break;
}
goto LABEL_24;
case 4:
if ( read(v2, &buf, 4uLL) == 4 )
{
sprintf((char *)v6, "Receive 0x%02x%02x%02x%02x", buf, v8, v9, v10);
syslog(7, "%s", (const char *)v6);
v3 = v10 + ((v9 + ((v8 + (buf << 8)) << 8)) << 8);
break;
}
goto LABEL_24;
}
close(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // ebx
int v4; // er12
__int64 v6[26]; // [rsp+0h] [rbp-108h] BYREF
unsigned __int8 buf; // [rsp+D6h] [rbp-32h] BYREF
unsigned __int8 v8; // [rsp+D7h] [rbp-31h]
unsigned __int8 v9; // [rsp+D8h] [rbp-30h]
unsigned __int8 v10; // [rsp+D9h] [rbp-2Fh]

v6[0] = 0LL;
v6[1] = 0LL;
v6[2] = 0LL;
v6[3] = 0LL;
v6[4] = 0LL;
v6[5] = 0LL;
v6[6] = 0LL;
v6[7] = 0LL;
v6[8] = 0LL;
v6[9] = 0LL;
v6[10] = 0LL;
v6[11] = 0LL;
v6[12] = 0LL;
v6[13] = 0LL;
v6[14] = 0LL;
v6[15] = 0LL;
v6[16] = 0LL;
v6[17] = 0LL;
v6[18] = 0LL;
v6[19] = 0LL;
v6[20] = 0LL;
v6[21] = 0LL;
v6[22] = 0LL;
v6[23] = 0LL;
v6[24] = 0LL;
v1 = open(i2cdev, 2);
if ( v1 < 0 )
{
syslog(3, "Failed to open i2c port");
return (unsigned int)-1;
}
v2 = v1;
v3 = flock(v1, 6);
v4 = i2lockmax;
if ( v3 && i2lockmax > 0 )
{
do
{
sleep(1u);
v3 = flock(v2, 6);
--v4;
}
while ( v3 && v4 > 0 );
}
if ( v3 )
{
syslog(3, "Failed to lock i2c port");
close(v2);
return (unsigned int)-2;
}
if ( ioctl(v2, 0x703uLL, address) < 0 )
{
syslog(3, "Unable to get bus access to talk to slave");
close(v2);
return (unsigned int)-3;
}
switch ( a1 )
{
case 1:
if ( read(v2, &buf, 1uLL) == 1 )
{
sprintf((char *)v6, "Receive 0x%02x", buf);
syslog(7, "%s", (const char *)v6);
v3 = buf;
break;
}
LABEL_24:
syslog(3, "Unable to read from slave");
close(v2);
return (unsigned int)-4;
case 2:
if ( read(v2, &buf, 2uLL) == 2 )
{
sprintf((char *)v6, "Receive 0x%02x%02x", buf, v8);
syslog(7, "%s", (const char *)v6);
v3 = v8 + (buf << 8);
break;
}
goto LABEL_24;
case 4:
if ( read(v2, &buf, 4uLL) == 4 )
{
sprintf((char *)v6, "Receive 0x%02x%02x%02x%02x", buf, v8, v9, v10);
syslog(7, "%s", (const char *)v6);
v3 = v10 + ((v9 + ((v8 + (buf << 8)) << 8)) << 8);
break;
}
goto LABEL_24;
}
close(v2);
return v3;
}

original_code:
int read_data(int length)
{
  int rdata = 0;
  int fd, rd;
  int cnt = 0;
  unsigned char buf[ 10 ];
  char message[ 200 ] = "";
  if( (fd = open( i2cdev, 02) ) < 0)
  {
    syslog( 3, "Failed to open i2c port");
    return -1;
  }
  rd = flock(fd, 2 | 4);
  cnt = i2lockmax;
  while( rd != 0 && cnt > 0 )
  {
    sleep( 1 );
    rd = flock( fd, 2 | 4);
    cnt--;
  }
  if( rd )
  {
    syslog( 3, "Failed to lock i2c port");
    close( fd );
    return -2;
  }
  if( ioctl( fd, 0x0703, address) < 0 )
  {
    syslog( 3, "Unable to get bus access to talk to slave");
    close( fd );
    return -3;
  }
  if( length == 1 )
  {
     if( read( fd, buf, 1 ) != 1 )
     {
       syslog( 3, "Unable to read from slave");
       close( fd );
       return -4;
     }
     else
     {
       sprintf( message, "Receive 0x%02x", buf[ 0 ] );
       syslog( 7, "%s", message);
       rdata = buf[ 0 ];
     }
  }
  else if( length == 2 )
  {
     if( read( fd, buf, 2 ) != 2 )
     {
       syslog( 3, "Unable to read from slave");
       close( fd );
       return -4;
     }
     else
     {
       sprintf( message, "Receive 0x%02x%02x", buf[0], buf[1] );
       syslog( 7, "%s", message);
       rdata = 256 * buf[0] + buf[1];
     }
  }
  else if( length == 4 )
  {
     if( read( fd, buf, 4) != 4 )
     {
       syslog( 3, "Unable to read from slave");
       close( fd );
       return -4;
     }
     else
     {
        sprintf( message, "Receive 0x%02x%02x%02x%02x", buf[0], buf[1], buf[2], buf[3] );
        syslog( 7, "%s", message);
        rdata = 16777216 * buf[ 0 ] + 65536 * buf[ 1 ] + 256 * buf[ 2 ] + buf[ 3 ];
     }
  }
  close( fd );
  return rdata;
}

canonical_original_code:
int read_data(int length)
{
  int rdata = 0;
  int fd; int rd;
  int cnt = 0;
  unsigned char buf[10];
  char message[200] = "";
  if( (fd = open( i2cdev, 02) ) < 0)
  {
    syslog( 3, "Failed to open i2c port");
    return -1;
  }
  rd = flock(fd, 2 | 4);
  cnt = i2lockmax;
  while( rd != 0 && cnt > 0 )
  {
    sleep( 1 );
    rd = flock( fd, 2 | 4);
    cnt--;
  }
  if( rd )
  {
    syslog( 3, "Failed to lock i2c port");
    close( fd );
    return -2;
  }
  if( ioctl( fd, 0x0703, address) < 0 )
  {
    syslog( 3, "Unable to get bus access to talk to slave");
    close( fd );
    return -3;
  }
  if( length == 1 )
  {
     if( read( fd, buf, 1 ) != 1 )
     {
       syslog( 3, "Unable to read from slave");
       close( fd );
       return -4;
     }
     else
     {
       sprintf( message, "Receive 0x%02x", buf[ 0 ] );
       syslog( 7, "%s", message);
       rdata = buf[ 0 ];
     }
  }
  else if( length == 2 )
  {
     if( read( fd, buf, 2 ) != 2 )
     {
       syslog( 3, "Unable to read from slave");
       close( fd );
       return -4;
     }
     else
     {
       sprintf( message, "Receive 0x%02x%02x", buf[0], buf[1] );
       syslog( 7, "%s", message);
       rdata = 256 * buf[0] + buf[1];
     }
  }
  else if( length == 4 )
  {
     if( read( fd, buf, 4) != 4 )
     {
       syslog( 3, "Unable to read from slave");
       close( fd );
       return -4;
     }
     else
     {
        sprintf( message, "Receive 0x%02x%02x%02x%02x", buf[0], buf[1], buf[2], buf[3] );
        syslog( 7, "%s", message);
        rdata = 16777216 * buf[ 0 ] + 65536 * buf[ 1 ] + 256 * buf[ 2 ] + buf[ 3 ];
     }
  }
  close( fd );
  return rdata;
}

variable_types: length int

variable_types: rdata int

variable_types: rd int

variable_types: cnt int

variable_types:
message
char [200]


decompiled_code:
__int64 testi2c()
{
unsigned int v0; // eax
unsigned int v1; // ebx
unsigned int v2; // ebp
unsigned int v4; // eax

v0 = time(0LL);
srand(v0);
v1 = rand();
if ( v1 >= 0xFFFFFFFC )
{
do
v1 = rand();
while ( v1 + 4 <= 3 );
}
v2 = write_cmd(2LL, v1, 4LL);
if ( v2 == 1 )
{
v4 = read_data(4LL);
if ( v4 >= 0xFFFFFFFC )
syslog(3, "failed to read 4 test bytes");
else
v2 = v4 == v1;
}
else
{
syslog(3, "failed to write 4 test bytes");
}
return v2;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax
unsigned int v1; // ebx
unsigned int v2; // ebp
unsigned int v4; // eax

v0 = time(0LL);
srand(v0);
v1 = rand();
if ( v1 >= 0xFFFFFFFC )
{
do
v1 = rand();
while ( v1 + 4 <= 3 );
}
v2 = write_cmd(2LL, v1, 4LL);
if ( v2 == 1 )
{
v4 = read_data(4LL);
if ( v4 >= 0xFFFFFFFC )
syslog(3, "failed to read 4 test bytes");
else
v2 = v4 == v1;
}
else
{
syslog(3, "failed to write 4 test bytes");
}
return v2;
}

original_code:
int testi2c()
{
  int ok=-1;
  int testint=0;
  int testres=1;
  srand((unsigned int)time(((void *)0)));
  testint=rand();
  while((testint==-1)||(testint==-2)||(testint==-3)||(testint==-4))
  {
    testint=rand();
  }
  ok=write_cmd(0x02,testint,4);
  if(ok!=1) syslog(3, "failed to write 4 test bytes");
  else
  {
    testres=read_data(4);
    if((testres==-1)||(testres==-2)||(testres==-3)||(testres==-4))
      syslog(3, "failed to read 4 test bytes");
    else
    {
      if(testint==testres) ok=1; else ok=0;
    }
  }
  return ok;
}

canonical_original_code:
int testi2c()
{
  int ok = -1;
  int testint = 0;
  int testres = 1;
  srand((unsigned int)time(((void *)0)));
  testint=rand();
  while((testint==-1)||(testint==-2)||(testint==-3)||(testint==-4))
  {
    testint=rand();
  }
  ok=write_cmd(0x02,testint,4);
  if(ok!=1) syslog(3, "failed to write 4 test bytes");
  else
  {
    testres=read_data(4);
    if((testres==-1)||(testres==-2)||(testres==-3)||(testres==-4))
      syslog(3, "failed to read 4 test bytes");
    else
    {
      if(testint==testres) ok=1; else ok=0;
    }
  }
  return ok;
}

variable_types: ok int

variable_types: testint int

variable_types: testres int


original_code:
int write_cmd(int cmd, int data, int length)
{
  int ok = 0;
  int fd, rd;
  int rnxt = 0;
  int cnt = 0;
  unsigned char buf[ 10 ];
  char message[ 200 ] = "";
  if( cmd >= 0 && cmd <= 255 )
  {
    if( ( fd = open( i2cdev, 02) ) < 0 )
    {
      syslog( 3, "Failed to open i2c port");
      return -1;
    }
    rd = flock( fd, 2 | 4);
    cnt = i2lockmax;
    while( rd != 0 && cnt > 0 )
    {
      sleep( 1 );
      rd = flock( fd, 2 | 4);
      cnt--;
    }
    if( rd )
    {
      syslog( 3, "Failed to lock i2c port");
      close( fd );
      return -2;
    }
    if( ioctl( fd, 0x0703, address) < 0 )
    {
      syslog( 3, "Unable to get bus access to talk to slave");
      close( fd );
      return -3;
    }
    buf[ 0 ] = cmd;
    if( length == 1 )
    {
      buf[ 1 ] = data;
      sprintf( message, "Send 0x%02x%02x", buf[ 0 ], buf[ 1 ] );
      syslog( 7, "%s", message);
      if( write(fd, buf, 2) != 2 )
      {
        syslog( 3, "Error writing to i2c slave");
        close( fd );
        return -4;
      }
   }
   else if( length == 2 )
   {
      buf[ 1 ]=(int)( data / 256 );
      buf[ 2 ] = data % 256;
      sprintf( message, "Send 0x%02x%02x%02x", buf[ 0 ], buf[ 1 ], buf[ 2 ] );
      syslog( 7, "%s", message);
      if( write( fd, buf, 3) != 3 )
      {
        syslog( 3, "Error writing to i2c slave");
        close( fd );
        return -4;
      }
   }
   else if( length == 4 )
   {
      buf[ 1 ] = (int)( data / 16777216 );
      rnxt = data % 16777216;
      buf[ 2 ] = (int)( rnxt / 65536 );
      rnxt = rnxt % 65536;
      buf[ 3 ] = (int)( rnxt / 256 );
      buf[ 4 ] = rnxt % 256;
      sprintf( message, "Send 0x%02x%02x%02x%02x%02x", buf[ 0 ], buf[ 1 ], buf[ 2 ], buf[ 3 ], buf[ 4 ] );
      syslog( 7, "%s", message);
      if( write(fd, buf, 5) != 5 )
      {
        syslog( 3, "Error writing to i2c slave" );
        close( fd );
        return -4;
      }
   }
   else
   {
      sprintf( message, "Send 0x%02x", buf[ 0 ] );
      syslog( 7, "%s", message );
      if( write(fd, buf, 1) != 1 )
      {
        syslog( 3, "Error writing to i2c slave" );
        close( fd );
        return -4;
      }
   }
   close( fd );
   ok = 1;
  }
  return ok;
}

canonical_original_code:
int write_cmd(int cmd, int data, int length)
{
  int ok = 0;
  int fd; int rd;
  int rnxt = 0;
  int cnt = 0;
  unsigned char buf[10];
  char message[200] = "";
  if( cmd >= 0 && cmd <= 255 )
  {
    if( ( fd = open( i2cdev, 02) ) < 0 )
    {
      syslog( 3, "Failed to open i2c port");
      return -1;
    }
    rd = flock( fd, 2 | 4);
    cnt = i2lockmax;
    while( rd != 0 && cnt > 0 )
    {
      sleep( 1 );
      rd = flock( fd, 2 | 4);
      cnt--;
    }
    if( rd )
    {
      syslog( 3, "Failed to lock i2c port");
      close( fd );
      return -2;
    }
    if( ioctl( fd, 0x0703, address) < 0 )
    {
      syslog( 3, "Unable to get bus access to talk to slave");
      close( fd );
      return -3;
    }
    buf[ 0 ] = cmd;
    if( length == 1 )
    {
      buf[ 1 ] = data;
      sprintf( message, "Send 0x%02x%02x", buf[ 0 ], buf[ 1 ] );
      syslog( 7, "%s", message);
      if( write(fd, buf, 2) != 2 )
      {
        syslog( 3, "Error writing to i2c slave");
        close( fd );
        return -4;
      }
   }
   else if( length == 2 )
   {
      buf[ 1 ]=(int)( data / 256 );
      buf[ 2 ] = data % 256;
      sprintf( message, "Send 0x%02x%02x%02x", buf[ 0 ], buf[ 1 ], buf[ 2 ] );
      syslog( 7, "%s", message);
      if( write( fd, buf, 3) != 3 )
      {
        syslog( 3, "Error writing to i2c slave");
        close( fd );
        return -4;
      }
   }
   else if( length == 4 )
   {
      buf[ 1 ] = (int)( data / 16777216 );
      rnxt = data % 16777216;
      buf[ 2 ] = (int)( rnxt / 65536 );
      rnxt = rnxt % 65536;
      buf[ 3 ] = (int)( rnxt / 256 );
      buf[ 4 ] = rnxt % 256;
      sprintf( message, "Send 0x%02x%02x%02x%02x%02x", buf[ 0 ], buf[ 1 ], buf[ 2 ], buf[ 3 ], buf[ 4 ] );
      syslog( 7, "%s", message);
      if( write(fd, buf, 5) != 5 )
      {
        syslog( 3, "Error writing to i2c slave" );
        close( fd );
        return -4;
      }
   }
   else
   {
      sprintf( message, "Send 0x%02x", buf[ 0 ] );
      syslog( 7, "%s", message );
      if( write(fd, buf, 1) != 1 )
      {
        syslog( 3, "Error writing to i2c slave" );
        close( fd );
        return -4;
      }
   }
   close( fd );
   ok = 1;
  }
  return ok;
}

variable_types: cmd int

variable_types: data int

variable_types: fd int

variable_types: rnxt int


decompiled_code:
ssize_t __fastcall send_poll(const char *a1)
{
int v1; // eax
ssize_t result; // rax
const char *v3; // rax
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF
__int64 buf; // [rsp+42h] [rbp-16h] BYREF
__int16 v7; // [rsp+4Ah] [rbp-Eh]
char v8; // [rsp+4Ch] [rbp-Ch]
char v9; // [rsp+4Dh] [rbp-Bh]
char v10; // [rsp+4Eh] [rbp-Ah]
char v11; // [rsp+4Fh] [rbp-9h]

buf = 0x74654E2D747241LL;
v7 = 0x2000;
v8 = 0;
v9 = 14;
v10 = 0;
v11 = -1;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v1 = getaddrinfo(a1, "6454", &req, &pai);
if ( v1 )
{
v3 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n", v3);
exit(1);
}
result = sendto(art_socket, &buf, 0xEuLL, 0, pai->ai_addr, pai->ai_addrlen);
if ( (_DWORD)result == -1 )
{
perror("sendto");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func8(const char *a1)
{
int v1; // eax
ssize_t result; // rax
const char *v3; // rax
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF
__int64 buf; // [rsp+42h] [rbp-16h] BYREF
__int16 v7; // [rsp+4Ah] [rbp-Eh]
char v8; // [rsp+4Ch] [rbp-Ch]
char v9; // [rsp+4Dh] [rbp-Bh]
char v10; // [rsp+4Eh] [rbp-Ah]
char v11; // [rsp+4Fh] [rbp-9h]

buf = 0x74654E2D747241LL;
v7 = 0x2000;
v8 = 0;
v9 = 14;
v10 = 0;
v11 = -1;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v1 = getaddrinfo(a1, "6454", &req, &pai);
if ( v1 )
{
v3 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n", v3);
exit(1);
}
result = sendto(art_socket, &buf, 0xEuLL, 0, pai->ai_addr, pai->ai_addrlen);
if ( (_DWORD)result == -1 )
{
perror("sendto");
exit(1);
}
return result;
}

original_code:
void send_poll(char *broadcast_address)
{
 struct pkt_ArtPoll pkt;
 struct addrinfo hints, *dest;
 int i;
 int rv;
 int numbytes;
 strcpy(pkt.ID, "Art-Net");
 pkt.OpCode = 0x2000;
 pkt.ProtVerHi = 0;
 pkt.ProtVerLo = 14;
 pkt.TalkToMe = 0;
 pkt.Priority = 0xff;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 if ((rv = getaddrinfo(broadcast_address, "6454", &hints, &dest))
   != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  exit(1);
 }
 if ((numbytes = sendto(art_socket, &pkt, sizeof(pkt), 0,
             dest->ai_addr, dest->ai_addrlen)) == -1) {
  perror("sendto");
  exit(1);
 }
}

canonical_original_code:
void send_poll(char *broadcast_address)
{
 struct pkt_ArtPoll pkt;
 struct addrinfo hints; struct addrinfo *dest;
 int i;
 int rv;
 int numbytes;
 strcpy(pkt.ID, "Art-Net");
 pkt.OpCode = 0x2000;
 pkt.ProtVerHi = 0;
 pkt.ProtVerLo = 14;
 pkt.TalkToMe = 0;
 pkt.Priority = 0xff;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 if ((rv = getaddrinfo(broadcast_address, "6454", &hints, &dest))
   != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  exit(1);
 }
 if ((numbytes = sendto(art_socket, &pkt, sizeof(pkt), 0,
             dest->ai_addr, dest->ai_addrlen)) == -1) {
  perror("sendto");
  exit(1);
 }
}

variable_types: struct pkt_ArtPoll { char ID[8]; unsigned short OpCode; unsigned char ProtVerHi; unsigned char ProtVerLo; unsigned char TalkToMe; unsigned char Priority; }

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: i int

variable_types: rv int

variable_types: numbytes int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
int __fastcall dump_table(FILE *stream)
{
__int64 i; // rbx
int result; // eax

for ( i = 0LL; i != 256; ++i )
{
if ( device_table[2 * i] )
result = fprintf(
stream,
"%02x: %d.%d.%d.%d\n",
(unsigned int)i,
(unsigned __int8)byte_404904[8 * i],
(unsigned __int8)byte_404905[8 * i],
(unsigned __int8)byte_404906[8 * i],
(unsigned __int8)byte_404907[8 * i]);
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(FILE *stream)
{
__int64 i; // rbx
int result; // eax

for ( i = 0LL; i != 256; ++i )
{
if ( device_table[2 * i] )
result = fprintf(
stream,
"%02x: %d.%d.%d.%d\n",
(unsigned int)i,
(unsigned __int8)byte_404904[8 * i],
(unsigned __int8)byte_404905[8 * i],
(unsigned __int8)byte_404906[8 * i],
(unsigned __int8)byte_404907[8 * i]);
}
return result;
}

original_code:
void dump_table(void) {
 unsigned int count = 0;
 int i;
 for (i = 0; i < 256; i++) {
  if (device_table[i].present) {
   printf("%02x: %d.%d.%d.%d\n", i,
    device_table[i].ip[0],
    device_table[i].ip[1],
    device_table[i].ip[2],
    device_table[i].ip[3]);
   count++;
  }
 }
 printf("%d device%s found\n", count, (count==1)?"":"s");
}

canonical_original_code:
void dump_table() {
 unsigned int count = 0;
 int i;
 for (i = 0; i < 256; i++) {
  if (device_table[i].present) {
   printf("%02x: %d.%d.%d.%d\n", i,
    device_table[i].ip[0],
    device_table[i].ip[1],
    device_table[i].ip[2],
    device_table[i].ip[3]);
   count++;
  }
 }
 printf("%d device%s found\n", count, (count==1)?"":"s");
}

variable_types:
count
unsigned int


original_code:
void generate_pattern(char *dmx_universe)
{
 static int pos;
 int i;
 bool direction = 0;
 static int colour[450];
 for (i = 0; i < 150; i++) {
  float distance = ((float)abs(pos-i)) / 150;
  if (distance < 0.0)
   distance = 0.0;
  if (distance > 1.0)
   distance = 1.0;
  int intensity = (int) (255.0 * (1.0-distance));
  dmx_universe[3*i] = (colour[i]==0)?255:intensity;
  dmx_universe[3*i+1] = (colour[i]==1)?255:intensity;
  dmx_universe[3*i+2] = (colour[i]==2)?255:intensity;
 }
 if ((pos >= 0) && (pos < 150)) {
  colour[pos]++;
  if (colour[pos] > 2)
   colour[pos] = 0;
 }
 if (direction) {
  pos++;
  if (pos >= 2*150) {
   pos = -150;
  }
 } else {
  pos--;
  if (pos <= -150) {
   pos = 2*150;
  }
 }
}

canonical_original_code:
void generate_pattern(char *dmx_universe)
{
 static int pos;
 int i;
 int direction = 0;
 static int colour[450];
 for (i = 0; i < 150; i++) {
  float distance = ((float)abs(pos-i)) / 150;
  if (distance < 0.0)
   distance = 0.0;
  if (distance > 1.0)
   distance = 1.0;
  int intensity = (int) (255.0 * (1.0-distance));
  dmx_universe[3*i] = (colour[i]==0)?255:intensity;
  dmx_universe[3*i+1] = (colour[i]==1)?255:intensity;
  dmx_universe[3*i+2] = (colour[i]==2)?255:intensity;
 }
 if ((pos >= 0) && (pos < 150)) {
  colour[pos]++;
  if (colour[pos] > 2)
   colour[pos] = 0;
 }
 if (direction) {
  pos++;
  if (pos >= 2*150) {
   pos = -150;
  }
 } else {
  pos--;
  if (pos <= -150) {
   pos = 2*150;
  }
 }
}

variable_types: pos int

variable_types: direction int

variable_types:
colour
int [450]

variable_types: distance float

variable_types: intensity int


decompiled_code:
ssize_t __fastcall send_dmx(__int64 a1, const void *a2, int a3)
{
char v4; // al
ssize_t result; // rax
__int64 v6; // [rsp+0h] [rbp-238h] BYREF
__int16 v7; // [rsp+8h] [rbp-230h]
char v8; // [rsp+Ah] [rbp-22Eh]
char v9; // [rsp+Bh] [rbp-22Dh]
char v10; // [rsp+Ch] [rbp-22Ch]
char v11; // [rsp+Dh] [rbp-22Bh]
char v12; // [rsp+Eh] [rbp-22Ah]
char v13; // [rsp+Fh] [rbp-229h]
char v14; // [rsp+10h] [rbp-228h]
char v15; // [rsp+11h] [rbp-227h]
char dest[550]; // [rsp+12h] [rbp-226h] BYREF

v6 = 0x74654E2D747241LL;
v7 = 20480;
v8 = 0;
v9 = 14;
v10 = dmx_sequence_0;
v4 = dmx_sequence_0 + 1;
if ( dmx_sequence_0 == -1 )
v4 = 1;
dmx_sequence_0 = v4;
v11 = 0;
v12 = 0;
v13 = 0;
v14 = BYTE1(a3);
v15 = a3;
memcpy(dest, a2, a3);
result = sendto(art_socket, &v6, a3 + 18, 0, *(const struct sockaddr **)(a1 + 24), *(_DWORD *)(a1 + 16));
if ( (_DWORD)result == -1 )
{
perror("sendto");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(__int64 a1, const void *a2, int a3)
{
char v4; // al
ssize_t result; // rax
__int64 v6; // [rsp+0h] [rbp-238h] BYREF
__int16 v7; // [rsp+8h] [rbp-230h]
char v8; // [rsp+Ah] [rbp-22Eh]
char v9; // [rsp+Bh] [rbp-22Dh]
char v10; // [rsp+Ch] [rbp-22Ch]
char v11; // [rsp+Dh] [rbp-22Bh]
char v12; // [rsp+Eh] [rbp-22Ah]
char v13; // [rsp+Fh] [rbp-229h]
char v14; // [rsp+10h] [rbp-228h]
char v15; // [rsp+11h] [rbp-227h]
char dest[550]; // [rsp+12h] [rbp-226h] BYREF

v6 = 0x74654E2D747241LL;
v7 = 20480;
v8 = 0;
v9 = 14;
v10 = dmx_sequence_0;
v4 = dmx_sequence_0 + 1;
if ( dmx_sequence_0 == -1 )
v4 = 1;
dmx_sequence_0 = v4;
v11 = 0;
v12 = 0;
v13 = 0;
v14 = BYTE1(a3);
v15 = a3;
memcpy(dest, a2, a3);
result = sendto(art_socket, &v6, a3 + 18, 0, *(const struct sockaddr **)(a1 + 24), *(_DWORD *)(a1 + 16));
if ( (_DWORD)result == -1 )
{
perror("sendto");
exit(1);
}
return result;
}

original_code:
void send_dmx(struct addrinfo *dest, char *data, int length)
{
 struct pkt_ArtOutput pkt;
 int i;
 int numbytes;
 static unsigned char dmx_sequence;
 strcpy(pkt.ID, "Art-Net");
 pkt.OpCode = 0x5000;
 pkt.ProtVerHi = 0;
 pkt.ProtVerLo = 14;
 pkt.Sequence = dmx_sequence++;
 if (dmx_sequence == 0)
  dmx_sequence++;
 pkt.Physical = 0;
 pkt.SubUni = 0;
 pkt.Net = 0;
 pkt.LengthHi = (length >> 8);
 pkt.Length = length & 0xff;
 memcpy(pkt.Data, data, length);
 if ((numbytes = sendto(art_socket, &pkt, (length+18), 0,
             dest->ai_addr, dest->ai_addrlen)) == -1) {
  perror("sendto");
  exit(1);
 }
}

variable_types:
data
char *

variable_types: length int

variable_types: struct pkt_ArtOutput { char ID[8]; unsigned short OpCode; unsigned char ProtVerHi; unsigned char ProtVerLo; unsigned char Sequence; unsigned char Physical; unsigned char SubUni; unsigned char Net; unsigned char LengthHi; unsigned char Length; unsigned char Data[512]; }

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


decompiled_code:
int delay_setup()
{
return gettimeofday(&delay_time, 0LL);
}

canonical_decompiled_code:
int func13()
{
return gettimeofday(&delay_time, 0LL);
}

original_code:
void delay_setup(void)
{
 gettimeofday(&delay_time, ((void *)0));
}

canonical_original_code:
void delay_setup()
{
 gettimeofday(&delay_time, ((void *)0));
}


decompiled_code:
__time_t __fastcall frame_delay(unsigned int a1)
{
__time_t v1; // rdx
__suseconds_t v2; // rdi
__time_t result; // rax
__time_t v4; // rdx
__suseconds_t v5; // rax
struct timespec v6; // [rsp+0h] [rbp-28h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

v1 = delay_time.tv_sec + a1 / 0xF4240;
delay_time.tv_sec = v1;
v2 = delay_time.tv_usec + a1 % 0xF4240;
delay_time.tv_usec = v2;
if ( v2 > 999999 )
{
delay_time.tv_sec = v1 + 1;
delay_time.tv_usec = v2 - 1000000;
}
gettimeofday(&tv, 0LL);
do
{
while ( 1 )
{
v4 = delay_time.tv_sec - tv.tv_sec;
v5 = delay_time.tv_usec - tv.tv_usec;
if ( delay_time.tv_usec - tv.tv_usec < 0 )
{
--v4;
v5 += 1000000LL;
}
v6.tv_sec = v4;
v6.tv_nsec = 1000 * v5;
nanosleep(&v6, 0LL);
gettimeofday(&tv, 0LL);
result = tv.tv_sec;
if ( delay_time.tv_sec != tv.tv_sec )
break;
result = tv.tv_usec;
if ( delay_time.tv_usec <= tv.tv_usec )
return result;
}
}
while ( delay_time.tv_sec > tv.tv_sec );
return result;
}

canonical_decompiled_code:
__time_t __fastcall func14(unsigned int a1)
{
__time_t v1; // rdx
__suseconds_t v2; // rdi
__time_t result; // rax
__time_t v4; // rdx
__suseconds_t v5; // rax
struct timespec v6; // [rsp+0h] [rbp-28h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

v1 = delay_time.tv_sec + a1 / 0xF4240;
delay_time.tv_sec = v1;
v2 = delay_time.tv_usec + a1 % 0xF4240;
delay_time.tv_usec = v2;
if ( v2 > 999999 )
{
delay_time.tv_sec = v1 + 1;
delay_time.tv_usec = v2 - 1000000;
}
gettimeofday(&tv, 0LL);
do
{
while ( 1 )
{
v4 = delay_time.tv_sec - tv.tv_sec;
v5 = delay_time.tv_usec - tv.tv_usec;
if ( delay_time.tv_usec - tv.tv_usec < 0 )
{
--v4;
v5 += 1000000LL;
}
v6.tv_sec = v4;
v6.tv_nsec = 1000 * v5;
nanosleep(&v6, 0LL);
gettimeofday(&tv, 0LL);
result = tv.tv_sec;
if ( delay_time.tv_sec != tv.tv_sec )
break;
result = tv.tv_usec;
if ( delay_time.tv_usec <= tv.tv_usec )
return result;
}
}
while ( delay_time.tv_sec > tv.tv_sec );
return result;
}

original_code:
void frame_delay(unsigned int microseconds)
{
 struct timeval interval;
 struct timeval now;
 struct timespec int_ts;
 interval.tv_sec = microseconds / 1000000;
 interval.tv_usec = microseconds % 1000000;
 do { (&delay_time)->tv_sec = (&delay_time)->tv_sec + (&interval)->tv_sec; (&delay_time)->tv_usec = (&delay_time)->tv_usec + (&interval)->tv_usec; if ((&delay_time)->tv_usec >= 1000000) { ++(&delay_time)->tv_sec; (&delay_time)->tv_usec -= 1000000; } } while (0);
 gettimeofday(&now, ((void *)0));
 do {
  do { (&interval)->tv_sec = (&delay_time)->tv_sec - (&now)->tv_sec; (&interval)->tv_usec = (&delay_time)->tv_usec - (&now)->tv_usec; if ((&interval)->tv_usec < 0) { --(&interval)->tv_sec; (&interval)->tv_usec += 1000000; } } while (0);
  int_ts.tv_sec = interval.tv_sec;
  int_ts.tv_nsec = interval.tv_usec * 1000;
  nanosleep(&int_ts, ((void *)0));
  gettimeofday(&now, ((void *)0));
 } while ((((&delay_time)->tv_sec == (&now)->tv_sec) ? ((&delay_time)->tv_usec > (&now)->tv_usec) : ((&delay_time)->tv_sec > (&now)->tv_sec)));
}

variable_types:
microseconds
unsigned int

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

variable_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
int __fastcall setup_socket(char *name)
{
int v1; // eax
int result; // eax
const char *v3; // rax
int optval; // [rsp+4h] [rbp-44h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-40h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-38h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
getaddrinfo("255.255.255.255", "6454", &req, &pai);
v1 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
art_socket = v1;
if ( v1 == -1 )
{
perror("socket");
exit(1);
}
if ( setsockopt(v1, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( setsockopt(art_socket, 1, 6, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
result = getaddrinfo(name, "6454", &req, &dmx_dest);
if ( result )
{
v3 = gai_strerror(result);
fprintf(edata, "getaddrinfo: %s\n", v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(char *name)
{
int v1; // eax
int result; // eax
const char *v3; // rax
int optval; // [rsp+4h] [rbp-44h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-40h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-38h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
getaddrinfo("255.255.255.255", "6454", &req, &pai);
v1 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
art_socket = v1;
if ( v1 == -1 )
{
perror("socket");
exit(1);
}
if ( setsockopt(v1, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( setsockopt(art_socket, 1, 6, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
result = getaddrinfo(name, "6454", &req, &dmx_dest);
if ( result )
{
v3 = gai_strerror(result);
fprintf(edata, "getaddrinfo: %s\n", v3);
exit(1);
}
return result;
}

original_code:
void setup_socket(char *universe_address)
{
 struct addrinfo hints, *res;
 struct sockaddr_in myaddr;
 struct timeval timeout;
 int rv;
 timeout.tv_sec = 3;
 timeout.tv_usec = 0;
 int yes = 1;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 getaddrinfo("255.255.255.255", "6454", &hints, &res);
 art_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 if (art_socket == -1) {
  perror("socket");
  exit(1);
 }
 if (bind(art_socket, res->ai_addr, res->ai_addrlen) == -1) {
  perror("bind");
  exit(1);
 }
 if (setsockopt(art_socket,1,2,&yes,sizeof(int))
  == -1) {
     perror("setsockopt");
     exit(1);
 }
 if (setsockopt(art_socket,1,6,&yes,sizeof(int))
  == -1) {
     perror("setsockopt");
     exit(1);
 }
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 if ((rv = getaddrinfo(universe_address, "6454", &hints, &dmx_dest))
   != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  exit(1);
 }
}

canonical_original_code:
void setup_socket(char *universe_address)
{
 struct addrinfo hints; struct addrinfo *res;
 struct sockaddr_in myaddr;
 struct timeval timeout;
 int rv;
 timeout.tv_sec = 3;
 timeout.tv_usec = 0;
 int yes = 1;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 getaddrinfo("255.255.255.255", "6454", &hints, &res);
 art_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 if (art_socket == -1) {
  perror("socket");
  exit(1);
 }
 if (bind(art_socket, res->ai_addr, res->ai_addrlen) == -1) {
  perror("bind");
  exit(1);
 }
 if (setsockopt(art_socket,1,2,&yes,sizeof(int))
  == -1) {
     perror("setsockopt");
     exit(1);
 }
 if (setsockopt(art_socket,1,6,&yes,sizeof(int))
  == -1) {
     perror("setsockopt");
     exit(1);
 }
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_DGRAM;
 if ((rv = getaddrinfo(universe_address, "6454", &hints, &dmx_dest))
   != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  exit(1);
 }
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

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: yes 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 timeval { long tv_sec; long tv_usec; }


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
if ( argc != 2 )
{
fprintf(edata, "Usage: %s <IP address>\n", *argv);
exit(1);
}
setup_socket((char *)argv[1]);
init_pattern(dmx_universe);
delay_setup();
while ( 1 )
{
generate_pattern((__int64)dmx_universe);
send_dmx((__int64)dmx_dest, dmx_universe, 450);
frame_delay(0x4E20u);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
if ( argc != 2 )
{
fprintf(edata, "Usage: %s <IP address>\n", *argv);
exit(1);
}
func11((char *)argv[1]);
func7(dmx_universe);
func13();
while ( 1 )
{
func10((__int64)dmx_universe);
func12((__int64)dmx_dest, dmx_universe, 450);
func14(0x4E20u);
}
}

original_code:
int main(int argc, char *argv[])
{
 if (argc != 2) {
  fprintf(stderr, "Usage: artdmxtest <IP address>\n");
  exit(1);
 }
 setup_socket(argv[1]);
 delay_setup();
 while (1) {
  generate_pattern(dmx_universe);
  send_dmx(dmx_dest, dmx_universe, 450);
  frame_delay(20000);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void **__fastcall array_init(void ***a1, int a2)
{
void **v2; // r12
__int64 v3; // rax
void **result; // rax

if ( !*a1 )
*a1 = (void **)malloc(0x38uLL);
pthread_mutex_init((pthread_mutex_t *)(*a1 + 2), 0LL);
v2 = *a1;
*v2 = malloc(8LL * a2);
if ( a2 > 0 )
{
v3 = 0LL;
do
{
*(_QWORD *)((char *)**a1 + v3) = 0LL;
v3 += 8LL;
}
while ( v3 != 8LL * (unsigned int)a2 );
}
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
*((_DWORD *)*a1 + 3) = 0;
return result;
}

canonical_decompiled_code:
void **__fastcall func0(void ***a1, int a2)
{
void **v2; // r12
__int64 v3; // rax
void **result; // rax

if ( !*a1 )
*a1 = (void **)malloc(0x38uLL);
pthread_mutex_init((pthread_mutex_t *)(*a1 + 2), 0LL);
v2 = *a1;
*v2 = malloc(8LL * a2);
if ( a2 > 0 )
{
v3 = 0LL;
do
{
*(_QWORD *)((char *)**a1 + v3) = 0LL;
v3 += 8LL;
}
while ( v3 != 8LL * (unsigned int)a2 );
}
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
*((_DWORD *)*a1 + 3) = 0;
return result;
}

original_code:
void array_init(array_t **array,int size) {
  int i;
  if (*array == ((void *)0))
    *array = (array_t*)malloc(sizeof(array_t));
  pthread_mutex_init(&((*array)->mutex),((void *)0));
  (*array)->data = (void**)malloc(sizeof(void*)*size);
  for(i = 0; i < size; i++)
    (*array)->data[i] = ((void *)0);
  (*array)->size = size;
  (*array)->after = 0;
}

canonical_original_code:
void array_init(struct array_tag **array, int size) {
  int i;
  if (*array == ((void *)0))
    *array = (struct array_tag *)malloc(sizeof(array_t));
  pthread_mutex_init(&((*array)->mutex),((void *)0));
  (*array)->data = (void **)malloc(sizeof(void *)*size);
  for(i = 0; i < size; i++)
    (*array)->data[i] = ((void *)0);
  (*array)->size = size;
  (*array)->after = 0;
}

variable_types: struct array_tag { void **data; int size; int after; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } **

variable_types: size int

variable_types: i int

user_defined_types: struct array_tag { void **data; int size; int after; union pthread_mutex_t mutex; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
void **__fastcall varray_init(void ***a1, int a2)
{
void **v2; // r12
__int64 v3; // rax
void **result; // rax

if ( !*a1 )
*a1 = (void **)malloc(0x38uLL);
pthread_mutex_init((pthread_mutex_t *)(*a1 + 2), 0LL);
v2 = *a1;
*v2 = malloc(8LL * a2);
if ( a2 > 0 )
{
v3 = 0LL;
do
{
*(_QWORD *)((char *)**a1 + v3) = 0LL;
v3 += 8LL;
}
while ( v3 != 8LL * (unsigned int)a2 );
}
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
*((_DWORD *)*a1 + 3) = 0;
return result;
}

canonical_decompiled_code:
void **__fastcall func1(void ***a1, int a2)
{
void **v2; // r12
__int64 v3; // rax
void **result; // rax

if ( !*a1 )
*a1 = (void **)malloc(0x38uLL);
pthread_mutex_init((pthread_mutex_t *)(*a1 + 2), 0LL);
v2 = *a1;
*v2 = malloc(8LL * a2);
if ( a2 > 0 )
{
v3 = 0LL;
do
{
*(_QWORD *)((char *)**a1 + v3) = 0LL;
v3 += 8LL;
}
while ( v3 != 8LL * (unsigned int)a2 );
}
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
*((_DWORD *)*a1 + 3) = 0;
return result;
}

original_code:
void varray_init(varray_t **array,int size) {
  int i;
  if (*array == ((void *)0))
    *array = (varray_t*)malloc(sizeof(varray_t));
  pthread_mutex_init(&((*array)->mutex),((void *)0));
  (*array)->data = (volatile void**)malloc(sizeof(void*)*size);
  for(i = 0; i < size; i++)
    (*array)->data[i] = ((void *)0);
  (*array)->size = size;
  (*array)->after = 0;
}

canonical_original_code:
void varray_init(struct varray_tag **array, int size) {
  int i;
  if (*array == ((void *)0))
    *array = (struct varray_tag *)malloc(sizeof(varray_t));
  pthread_mutex_init(&((*array)->mutex),((void *)0));
  (*array)->data = (void **)malloc(sizeof(void *)*size);
  for(i = 0; i < size; i++)
    (*array)->data[i] = ((void *)0);
  (*array)->size = size;
  (*array)->after = 0;
}

variable_types: struct varray_tag { void **data; int size; int after; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } **

user_defined_types: struct varray_tag { void **data; int size; int after; union pthread_mutex_t mutex; }


decompiled_code:
void **__fastcall array_resize(void ***a1, int a2)
{
void **v2; // r12
void **result; // rax
int v4; // edx

v2 = *a1;
*v2 = realloc(**a1, 8LL * a2);
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
v4 = *((_DWORD *)*a1 + 3);
if ( a2 > v4 )
{
result = (void **)(8LL * v4);
do
*(void **)((char *)result++ + (_QWORD)**a1) = 0LL;
while ( result != (void **)(8 * (v4 + (unsigned __int64)(unsigned int)(a2 - 1 - v4) + 1)) );
}
return result;
}

canonical_decompiled_code:
void **__fastcall func2(void ***a1, int a2)
{
void **v2; // r12
void **result; // rax
int v4; // edx

v2 = *a1;
*v2 = realloc(**a1, 8LL * a2);
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
v4 = *((_DWORD *)*a1 + 3);
if ( a2 > v4 )
{
result = (void **)(8LL * v4);
do
*(void **)((char *)result++ + (_QWORD)**a1) = 0LL;
while ( result != (void **)(8 * (v4 + (unsigned __int64)(unsigned int)(a2 - 1 - v4) + 1)) );
}
return result;
}

original_code:
void array_resize(array_t **array,int size) {
  int i;
  (*array)->data = (void**)realloc((*array)->data,sizeof(void*)*size);
  (*array)->size = size;
  for(i = (*array)->after; i < size; i++)
    (*array)->data[i] = ((void *)0);
}

canonical_original_code:
void array_resize(struct array_tag **array, int size) {
  int i;
  (*array)->data = (void **)realloc((*array)->data,sizeof(void *)*size);
  (*array)->size = size;
  for(i = (*array)->after; i < size; i++)
    (*array)->data[i] = ((void *)0);
}

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
void **__fastcall varray_resize(void ***a1, int a2)
{
void **v2; // r12
void **result; // rax
int v4; // edx

v2 = *a1;
*v2 = realloc(**a1, 8LL * a2);
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
v4 = *((_DWORD *)*a1 + 3);
if ( a2 > v4 )
{
result = (void **)(8LL * v4);
do
*(void **)((char *)result++ + (_QWORD)**a1) = 0LL;
while ( result != (void **)(8 * (v4 + (unsigned __int64)(unsigned int)(a2 - 1 - v4) + 1)) );
}
return result;
}

canonical_decompiled_code:
void **__fastcall func3(void ***a1, int a2)
{
void **v2; // r12
void **result; // rax
int v4; // edx

v2 = *a1;
*v2 = realloc(**a1, 8LL * a2);
*((_DWORD *)*a1 + 2) = a2;
result = *a1;
v4 = *((_DWORD *)*a1 + 3);
if ( a2 > v4 )
{
result = (void **)(8LL * v4);
do
*(void **)((char *)result++ + (_QWORD)**a1) = 0LL;
while ( result != (void **)(8 * (v4 + (unsigned __int64)(unsigned int)(a2 - 1 - v4) + 1)) );
}
return result;
}

original_code:
void varray_resize(varray_t **array,int size) {
  int i;
  (*array)->data = (volatile void**)realloc((*array)->data,sizeof(volatile void*)*size);
  (*array)->size = size;
  for(i = (*array)->after; i < size; i++)
    (*array)->data[i] = ((void *)0);
}

canonical_original_code:
void varray_resize(struct varray_tag **array, int size) {
  int i;
  (*array)->data = (void **)realloc((*array)->data,sizeof(void *)*size);
  (*array)->size = size;
  for(i = (*array)->after; i < size; i++)
    (*array)->data[i] = ((void *)0);
}


decompiled_code:
void __fastcall array_delete(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void array_delete(array_t *array) {
  free(array->data);
  free(array);
}

canonical_original_code:
void array_delete(struct array_tag *array) {
  free(array->data);
  free(array);
}

variable_types: struct array_tag { void **data; int size; int after; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *


decompiled_code:
void __fastcall varray_delete(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

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

original_code:
void varray_delete(varray_t *array) {
  free(array->data);
  free(array);
}

canonical_original_code:
void varray_delete(struct varray_tag *array) {
  free(array->data);
  free(array);
}

variable_types: struct varray_tag { void **data; int size; int after; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *


decompiled_code:
_BOOL8 __fastcall is_valid(__int64 a1, int a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 12) > a2 )
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) != 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(__int64 a1, int a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 12) > a2 )
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) != 0LL;
return result;
}

original_code:
int is_valid(array_t *arr, int id) {
  if ((id >= 0) && (id < arr->after) &&
      (arr->data[id] != ((void *)0)))
    return 1;
  else
    return 0;
}

canonical_original_code:
int is_valid(struct array_tag *arr, int id) {
  if ((id >= 0) && (id < arr->after) &&
      (arr->data[id] != ((void *)0)))
    return 1;
  else
    return 0;
}

variable_types: id int


decompiled_code:
_BOOL8 __fastcall vis_valid(__int64 a1, int a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 12) > a2 )
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) != 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(__int64 a1, int a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 12) > a2 )
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) != 0LL;
return result;
}

original_code:
int vis_valid(varray_t *arr, int id) {
  if ((id >= 0) && (id < arr->after) &&
      (arr->data[id] != ((void *)0)))
    return 1;
  else
    return 0;
}

canonical_original_code:
int vis_valid(struct varray_tag *arr, int id) {
  if ((id >= 0) && (id < arr->after) &&
      (arr->data[id] != ((void *)0)))
    return 1;
  else
    return 0;
}


repo: ojuba-org/univaruints

decompiled_code:
__int64 __fastcall bisect_right(__int64 a1, unsigned __int64 a2, unsigned int a3, unsigned __int8 a4)
{
int v7; // eax

while ( a4 > (unsigned __int8)a3 )
{
v7 = ((unsigned __int8)a3 + a4) >> 1;
if ( *(_QWORD *)(a1 + 8LL * (unsigned __int8)v7) <= a2 )
a3 = v7 + 1;
else
a4 = ((unsigned __int8)a3 + a4) >> 1;
}
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int64 a2, unsigned int a3, unsigned __int8 a4)
{
int v7; // eax

while ( a4 > (unsigned __int8)a3 )
{
v7 = ((unsigned __int8)a3 + a4) >> 1;
if ( *(_QWORD *)(a1 + 8LL * (unsigned __int8)v7) <= a2 )
a3 = v7 + 1;
else
a4 = ((unsigned __int8)a3 + a4) >> 1;
}
return a3;
}

original_code:
unsigned char bisect_right(const uint64_t *a, uint64_t x, unsigned char lo, unsigned char hi) {
    unsigned char mid;
    while(lo<hi) {
        mid = (lo+hi)>>1;
        if (x<a[mid]) hi = mid;
        else lo = mid+1;
    }
    return lo;
}

canonical_original_code:
unsigned char bisect_right(unsigned long *a, unsigned long x, unsigned char lo, unsigned char hi) {
    unsigned char mid;
    while(lo<hi) {
        mid = (lo+hi)>>1;
        if (x<a[mid]) hi = mid;
        else lo = mid+1;
    }
    return lo;
}

variable_types:
a
unsigned long *

variable_types:
x
unsigned long

variable_types:
lo
unsigned char

variable_types:
hi
unsigned char

variable_types:
mid
unsigned char

return_type: unsigned char


decompiled_code:
char __fastcall bisect_right7(unsigned __int64 *a1, unsigned __int64 a2)
{
char result; // al

if ( a1[3] <= a2 )
{
if ( a1[5] <= a2 )
result = (a1[6] <= a2) + 6;
else
result = (a1[4] <= a2) + 4;
}
else if ( a1[1] <= a2 )
{
result = (a1[2] <= a2) + 2;
}
else
{
result = *a1 <= a2;
}
return result;
}

canonical_decompiled_code:
char __fastcall func8(unsigned __int64 *a1, unsigned __int64 a2)
{
char result; // al

if ( a1[3] <= a2 )
{
if ( a1[5] <= a2 )
result = (a1[6] <= a2) + 6;
else
result = (a1[4] <= a2) + 4;
}
else if ( a1[1] <= a2 )
{
result = (a1[2] <= a2) + 2;
}
else
{
result = *a1 <= a2;
}
return result;
}

original_code:
unsigned char bisect_right7(const uint64_t *a, uint64_t x) {
    if (x<a[3]) {
        if (x<a[1]) {
            if (x<a[0]) {
                return 0;
            } else {
                return 1;
            }
        } else {
            if (x<a[2]) {
                return 2;
            } else {
                return 3;
            }
        }
    } else {
        if (x<a[5]) {
            if (x<a[4]) {
                return 4;
            } else {
                return 5;
            }
        } else {
            if (x<a[6]) {
                return 6;
            } else {
                return 7;
            }
        }
    }
}

canonical_original_code:
unsigned char bisect_right7(unsigned long *a, unsigned long x) {
    if (x<a[3]) {
        if (x<a[1]) {
            if (x<a[0]) {
                return 0;
            } else {
                return 1;
            }
        } else {
            if (x<a[2]) {
                return 2;
            } else {
                return 3;
            }
        }
    } else {
        if (x<a[5]) {
            if (x<a[4]) {
                return 4;
            } else {
                return 5;
            }
        } else {
            if (x<a[6]) {
                return 6;
            } else {
                return 7;
            }
        }
    }
}


decompiled_code:
unsigned __int64 __fastcall univaruints_decode_single(__int64 a1, _QWORD *a2)
{
unsigned __int64 result; // rax
unsigned __int8 v3; // r8
_QWORD *v4; // rcx
__int64 v5; // rsi
unsigned __int64 v6[3]; // [rsp+8h] [rbp-18h] BYREF

v6[0] = 0LL;
LOBYTE(result) = *(_BYTE *)a1;
if ( *(char *)a1 < 0 )
{
v3 = n_by_chr[(unsigned __int8)result];
*a2 = v3 + 1;
v4 = (unsigned __int64 *)((char *)v6 + 8LL - v3);
v5 = a1 + 1;
if ( v3 >= 8u )
{
*v4 = *(_QWORD *)(a1 + 1);
v6[0] = *(_QWORD *)(v5 + v3 - 8);
qmemcpy(
(void *)((unsigned __int64)(v4 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v5 - ((_QWORD)v4 - ((unsigned __int64)(v4 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * ((v3 + (unsigned int)v4 - (((_DWORD)v4 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v3 & 4) != 0 )
{
*(_DWORD *)v4 = *(_DWORD *)(a1 + 1);
*(_DWORD *)((char *)v4 + v3 - 4) = *(_DWORD *)(v5 + v3 - 4);
}
else if ( v3 )
{
*(_BYTE *)v4 = *(_BYTE *)(a1 + 1);
if ( (v3 & 2) != 0 )
*(_WORD *)((char *)v4 + v3 - 2) = *(_WORD *)(v5 + v3 - 2);
}
result = shifts[v3]
+ (((unsigned __int64)((unsigned __int8)(127 >> v3) & (unsigned __int8)result) << (8 * v3)) | _bswap_64(v6[0]));
}
else
{
*a2 = 1LL;
result = (unsigned __int8)result;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func11(__int64 a1, _QWORD *a2)
{
unsigned __int64 result; // rax
unsigned __int8 v3; // r8
_QWORD *v4; // rcx
__int64 v5; // rsi
unsigned __int64 v6[3]; // [rsp+8h] [rbp-18h] BYREF

v6[0] = 0LL;
LOBYTE(result) = *(_BYTE *)a1;
if ( *(char *)a1 < 0 )
{
v3 = n_by_chr[(unsigned __int8)result];
*a2 = v3 + 1;
v4 = (unsigned __int64 *)((char *)v6 + 8LL - v3);
v5 = a1 + 1;
if ( v3 >= 8u )
{
*v4 = *(_QWORD *)(a1 + 1);
v6[0] = *(_QWORD *)(v5 + v3 - 8);
qmemcpy(
(void *)((unsigned __int64)(v4 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v5 - ((_QWORD)v4 - ((unsigned __int64)(v4 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * ((v3 + (unsigned int)v4 - (((_DWORD)v4 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v3 & 4) != 0 )
{
*(_DWORD *)v4 = *(_DWORD *)(a1 + 1);
*(_DWORD *)((char *)v4 + v3 - 4) = *(_DWORD *)(v5 + v3 - 4);
}
else if ( v3 )
{
*(_BYTE *)v4 = *(_BYTE *)(a1 + 1);
if ( (v3 & 2) != 0 )
*(_WORD *)((char *)v4 + v3 - 2) = *(_WORD *)(v5 + v3 - 2);
}
result = shifts[v3]
+ (((unsigned __int64)((unsigned __int8)(127 >> v3) & (unsigned __int8)result) << (8 * v3)) | func6(v6[0]));
}
else
{
*a2 = 1LL;
result = (unsigned __int8)result;
}
return result;
}

original_code:
uint64_t univaruints_decode_single(const unsigned char *buf, uint64_t *used) {
    unsigned char o, n, mask;
    uint64_t pack=0;
    unsigned char *ptr=(unsigned char *)&pack;
    if (128>(o=*buf)) {
        *used=1;
        return (uint64_t)o;
    }
    n=n_by_chr[o];
    mask=127>>n;
    *used=n+1;
    memcpy(ptr+8-n, buf+1, n);
    return shifts[n] + ( (((uint64_t)(o & mask))<< (n<<3)) | __bswap_64 (pack) );
}

canonical_original_code:
unsigned long univaruints_decode_single(unsigned char *buf, unsigned long *used) {
    unsigned char o; unsigned char n; unsigned char mask;
    unsigned long pack = 0;
    unsigned char *ptr = (unsigned char *)&pack;
    if (128>(o=*buf)) {
        *used=1;
        return (unsigned long)o;
    }
    n=n_by_chr[o];
    mask=127>>n;
    *used=n+1;
    memcpy(ptr+8-n, buf+1, n);
    return shifts[n] + ( (((unsigned long)(o & mask))<< (n<<3)) | __bswap_64 (pack) );
}

variable_types:
buf
unsigned char *

variable_types:
used
unsigned long *

variable_types:
o
unsigned char

variable_types:
n
unsigned char

variable_types:
mask
unsigned char

variable_types:
pack
unsigned long

variable_types:
ptr
unsigned char *

return_type: unsigned long


original_code:
uint64_t univaruints_encode_single(unsigned char *buf, uint64_t v) {
    unsigned char n;
    uint64_t offset, pack=0;
    unsigned char *ptr=(unsigned char *)&pack;
    if (v<128) {
        *buf=v;
        return 1;
    }
    n=bisect_right7(shifts2, v)+1;
    offset=shifts[n];
    v-=offset;
    *buf=((65280>>n) & 255) | ( (127>>n) & (v>>(n<<3)) );
    pack=__bswap_64 (v);
    memcpy(buf+1, ptr+8-n, n);
    return 1+n;
}

canonical_original_code:
unsigned long univaruints_encode_single(unsigned char *buf, unsigned long v) {
    unsigned char n;
    unsigned long offset; unsigned long pack = 0;
    unsigned char *ptr = (unsigned char *)&pack;
    if (v<128) {
        *buf=v;
        return 1;
    }
    n=bisect_right7(shifts2, v)+1;
    offset=shifts[n];
    v-=offset;
    *buf=((65280>>n) & 255) | ( (127>>n) & (v>>(n<<3)) );
    pack=__bswap_64 (v);
    memcpy(buf+1, ptr+8-n, n);
    return 1+n;
}

variable_types:
v
unsigned long

variable_types:
offset
unsigned long


decompiled_code:
__int64 __fastcall univaruints_decode(unsigned __int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v3; // rbx
__int64 v5; // rbp
__int64 v7[6]; // [rsp+8h] [rbp-30h] BYREF

v7[0] = 0LL;
if ( a1 >= a1 + a2 )
return 0LL;
v3 = a1;
v5 = 0LL;
do
{
*(_QWORD *)(a3 + 8 * v5) = univaruints_decode_single(v3, v7);
v3 += v7[0];
++v5;
}
while ( a1 + a2 > v3 );
return v5;
}

original_code:
uint64_t univaruints_decode(const unsigned char *buf, uint64_t size, uint64_t *v ) {
    const unsigned char *end=buf+size;
    uint64_t used_i=0, used_sum=0, i=0;
    while(buf<end) {
        *v=univaruints_decode_single(buf, &used_i);
        ++v;
        buf+=used_i;
        used_sum+=used_i;
        ++i;
    }
    return i;
}

canonical_original_code:
unsigned long univaruints_decode(unsigned char *buf, unsigned long size, unsigned long *v) {
    unsigned char *end = buf+size;
    unsigned long used_i = 0; unsigned long used_sum = 0; unsigned long i = 0;
    while(buf<end) {
        *v=univaruints_decode_single(buf, &used_i);
        ++v;
        buf+=used_i;
        used_sum+=used_i;
        ++i;
    }
    return i;
}

variable_types:
size
unsigned long

variable_types:
v
unsigned long *

variable_types:
end
unsigned char *

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall univaruints_encode(__int64 a1, unsigned __int64 a2, __int64 a3)
{
unsigned __int64 v3; // r13
unsigned __int64 v5; // rbx
__int64 v6; // r12
__int64 v7; // rax

v3 = a2 + 8 * a3;
if ( a2 >= v3 )
return 0LL;
v5 = a2;
v6 = 0LL;
do
{
v5 += 8LL;
v7 = univaruints_encode_single(a1, *(_QWORD *)(v5 - 8));
v6 += v7;
a1 += v7;
}
while ( v3 > v5 );
return v6;
}

original_code:
uint64_t univaruints_encode(unsigned char *buf, const uint64_t *v, uint64_t count) {
    const uint64_t *end=v+count;
    uint64_t used=0, used_sum=0;
    while(v<end) {
        used=univaruints_encode_single(buf, *v++);
        used_sum+=used;
        buf+=used;
    }
    return used_sum;
}

canonical_original_code:
unsigned long univaruints_encode(unsigned char *buf, unsigned long *v, unsigned long count) {
    unsigned long *end = v+count;
    unsigned long used = 0; unsigned long used_sum = 0;
    while(v<end) {
        used=univaruints_encode_single(buf, *v++);
        used_sum+=used;
        buf+=used;
    }
    return used_sum;
}

variable_types:
count
unsigned long

variable_types:
end
unsigned long *

variable_types:
used
unsigned long


repo: okirch/pam-test-utils

decompiled_code:
__int64 __fastcall fake_conv(int a1, unsigned int **a2, _QWORD *a3)
{
__int64 result; // rax
char *v6; // rax
char *v7; // r13
char *v8; // r12
char *v9; // rbp
const char *v10; // r15
char *v11; // r15
__int64 v12; // rax
__int64 v13; // rdx

result = 19LL;
if ( a1 <= 0 )
return result;
v6 = (char *)calloc(a1, 0x10uLL);
v7 = v6;
if ( !v6 )
return 19LL;
v8 = v6;
v9 = &v6[16 * a1];
while ( 1 )
{
v12 = (__int64)*a2;
v13 = **a2;
if ( (_DWORD)v13 == 3 )
{
if ( opt_debug )
fprintf(stderr, "%s\n", *(const char **)(v12 + 8));
goto LABEL_20;
}
if ( (int)v13 <= 3 )
break;
if ( (_DWORD)v13 != 4 )
goto LABEL_29;
if ( opt_debug )
fprintf(stdout, "%s\n", *(const char **)(v12 + 8));
LABEL_20:
++a2;
v8 += 16;
if ( v8 == v9 )
{
*a3 = v7;
return 0LL;
}
}
if ( (_DWORD)v13 == 1 )
{
if ( opt_debug )
fprintf(stderr, " >> Prompt: %s", *(const char **)(v12 + 8));
if ( verify_mode )
{
v11 = strdup(opt_password);
}
else
{
v10 = (const char *)*((_QWORD *)*a2 + 1);
if ( strcasestr(v10, "new") || strcasestr(v10, "neu") )
v11 = strdup(opt_new_password);
else
v11 = strdup(opt_old_password);
}
goto LABEL_16;
}
if ( (_DWORD)v13 == 2 )
{
if ( opt_debug )
fputs(*(const char **)(v12 + 8), stderr);
v11 = strdup(opt_username);
LABEL_16:
if ( v11 )
{
if ( opt_debug )
fprintf(stderr, "\n << Answer: %s\n", v11);
*((_DWORD *)v8 + 2) = 0;
*(_QWORD *)v8 = v11;
}
goto LABEL_20;
}
LABEL_29:
result = 19LL;
if ( opt_debug )
{
fprintf(stderr, "erroneous conversation (%d)\n", v13);
result = 19LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, unsigned int **a2, _QWORD *a3)
{
__int64 result; // rax
char *v6; // rax
char *v7; // r13
char *v8; // r12
char *v9; // rbp
const char *v10; // r15
char *v11; // r15
__int64 v12; // rax
__int64 v13; // rdx

result = 19LL;
if ( a1 <= 0 )
return result;
v6 = (char *)calloc(a1, 0x10uLL);
v7 = v6;
if ( !v6 )
return 19LL;
v8 = v6;
v9 = &v6[16 * a1];
while ( 1 )
{
v12 = (__int64)*a2;
v13 = **a2;
if ( (_DWORD)v13 == 3 )
{
if ( opt_debug )
fprintf(stderr, "%s\n", *(const char **)(v12 + 8));
goto LABEL_20;
}
if ( (int)v13 <= 3 )
break;
if ( (_DWORD)v13 != 4 )
goto LABEL_29;
if ( opt_debug )
fprintf(stdout, "%s\n", *(const char **)(v12 + 8));
LABEL_20:
++a2;
v8 += 16;
if ( v8 == v9 )
{
*a3 = v7;
return 0LL;
}
}
if ( (_DWORD)v13 == 1 )
{
if ( opt_debug )
fprintf(stderr, " >> Prompt: %s", *(const char **)(v12 + 8));
if ( verify_mode )
{
v11 = strdup(opt_password);
}
else
{
v10 = (const char *)*((_QWORD *)*a2 + 1);
if ( strcasestr(v10, "new") || strcasestr(v10, "neu") )
v11 = strdup(opt_new_password);
else
v11 = strdup(opt_old_password);
}
goto LABEL_16;
}
if ( (_DWORD)v13 == 2 )
{
if ( opt_debug )
fputs(*(const char **)(v12 + 8), stderr);
v11 = strdup(opt_username);
LABEL_16:
if ( v11 )
{
if ( opt_debug )
fprintf(stderr, "\n << Answer: %s\n", v11);
*((_DWORD *)v8 + 2) = 0;
*(_QWORD *)v8 = v11;
}
goto LABEL_20;
}
LABEL_29:
result = 19LL;
if ( opt_debug )
{
fprintf(stderr, "erroneous conversation (%d)\n", v13);
result = 19LL;
}
return result;
}

original_code:
static int
fake_conv (int num_msg, const struct pam_message **msgm,
    struct pam_response **response, void *appdata_ptr)
{
  struct pam_response *reply;
  int count;
  if (num_msg <= 0)
    return 19;
  reply = calloc (num_msg, sizeof (struct pam_response));
  if (reply == ((void *)0))
    return 19;
  for (count=0; count < num_msg; ++count) {
    char *string=((void *)0);
    switch (msgm[count]->msg_style) {
    case 1:
      if (opt_debug)
 fprintf(stderr, " >> Prompt: %s", msgm[count]->msg);
      if (verify_mode)
 string = strdup (opt_password);
      else if ((strcasestr (msgm[count]->msg, "new") != ((void *)0)) ||
        (strcasestr (msgm[count]->msg, "neu") != ((void *)0)))
 string = strdup (opt_new_password);
      else
 string = strdup (opt_old_password);
      break;
    case 2:
      if (opt_debug)
 fprintf(stderr, "%s", msgm[count]->msg);
      string = strdup (opt_username);
      break;
    case 3:
      if (opt_debug)
 fprintf(stderr, "%s\n",msgm[count]->msg);
      break;
    case 4:
      if (opt_debug)
 fprintf(stdout, "%s\n",msgm[count]->msg);
      break;
    default:
      if (opt_debug)
 fprintf(stderr,"erroneous conversation (%d)\n",
  msgm[count]->msg_style);
      return 19;
    }
    if (string) {
      if (opt_debug)
 fprintf(stderr, "\n << Answer: %s\n", string);
      reply[count].resp_retcode = 0;
      reply[count].resp = string;
      string = ((void *)0);
    }
  }
  *response = reply;
  return 0;
}

canonical_original_code:
static int fake_conv(int num_msg, struct pam_message **msgm, struct pam_response **response, void *appdata_ptr)
{
  struct pam_response *reply;
  int count;
  if (num_msg <= 0)
    return 19;
  reply = calloc (num_msg, sizeof (struct pam_response));
  if (reply == ((void *)0))
    return 19;
  for (count=0; count < num_msg; ++count) {
    char *string = ((void *)0);
    switch (msgm[count]->msg_style) {
    case 1:
      if (opt_debug)
 fprintf(stderr, " >> Prompt: %s", msgm[count]->msg);
      if (verify_mode)
 string = strdup (opt_password);
      else if ((strcasestr (msgm[count]->msg, "new") != ((void *)0)) ||
        (strcasestr (msgm[count]->msg, "neu") != ((void *)0)))
 string = strdup (opt_new_password);
      else
 string = strdup (opt_old_password);
      break;
    case 2:
      if (opt_debug)
 fprintf(stderr, "%s", msgm[count]->msg);
      string = strdup (opt_username);
      break;
    case 3:
      if (opt_debug)
 fprintf(stderr, "%s\n",msgm[count]->msg);
      break;
    case 4:
      if (opt_debug)
 fprintf(stdout, "%s\n",msgm[count]->msg);
      break;
    default:
      if (opt_debug)
 fprintf(stderr,"erroneous conversation (%d)\n",
  msgm[count]->msg_style);
      return 19;
    }
    if (string) {
      if (opt_debug)
 fprintf(stderr, "\n << Answer: %s\n", string);
      reply[count].resp_retcode = 0;
      reply[count].resp = string;
      string = ((void *)0);
    }
  }
  *response = reply;
  return 0;
}

variable_types: struct pam_message { int msg_style; char *msg; } **

variable_types: struct pam_response { char *resp; int resp_retcode; } **

variable_types: count int

variable_types:
string
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v5; // eax
int result; // eax
int v7; // eax
int v8; // eax
const char *v9; // r12
bool v10; // cf
bool v11; // zf
const char *v12; // rdi
__int64 v13; // rcx
const char *v14; // rsi
char v15; // al
bool v16; // cf
bool v17; // zf
const char *v18; // rdi
__int64 v19; // rcx
const char *v20; // rsi
struct option v21; // [rsp+0h] [rbp-F8h] BYREF
const char *v22; // [rsp+20h] [rbp-D8h]
int v23; // [rsp+28h] [rbp-D0h]
int v24; // [rsp+38h] [rbp-C0h]
const char *v25; // [rsp+40h] [rbp-B8h]
int v26; // [rsp+48h] [rbp-B0h]
int v27; // [rsp+58h] [rbp-A0h]
void *v28; // [rsp+60h] [rbp-98h]
int v29; // [rsp+68h] [rbp-90h]
int v30; // [rsp+78h] [rbp-80h]
const char *v31; // [rsp+80h] [rbp-78h]
int v32; // [rsp+88h] [rbp-70h]
int v33; // [rsp+98h] [rbp-60h]
const char *v34; // [rsp+A0h] [rbp-58h]
int v35; // [rsp+A8h] [rbp-50h]
int v36; // [rsp+B8h] [rbp-40h]

memset(&v21.has_arg, 0, 0xD8uLL);
v21.name = "debug";
v22 = "username";
v23 = 1;
v24 = 1;
v25 = "password";
v26 = 1;
v27 = 2;
v28 = &unk_402089;
v29 = 1;
v30 = 3;
v31 = "new-password";
v32 = 1;
v33 = 4;
v34 = "pam-service";
v35 = 1;
v36 = 5;
while ( 2 )
{
v5 = getopt_long(argc, (char *const *)argv, "dupON", &v21, 0LL);
if ( v5 != -1 )
{
switch ( v5 )
{
case 0:
++opt_debug;
continue;
case 1:
opt_username = (char *)optarg;
continue;
case 2:
opt_password = (char *)optarg;
continue;
case 3:
opt_old_password = (char *)optarg;
continue;
case 4:
opt_new_password = (char *)optarg;
continue;
case 5:
opt_pam_service = (char *)optarg;
continue;
default:
fwrite("Invalid option, please see source code for documentation\n", 1uLL, 0x39uLL, stderr);
result = 1;
break;
}
return result;
}
break;
}
v7 = optind;
if ( optind >= argc )
{
test_authenticate((unsigned int)argc);
LABEL_13:
puts("All verifications completed successfully");
return 0;
}
while ( 1 )
{
optind = v7 + 1;
v9 = argv[v7];
v10 = 0;
v11 = opt_debug == 0;
if ( opt_debug )
fprintf(stderr, "%s: service=%s user=%s\n", argv[v7], opt_pam_service, opt_username);
v12 = "pam_authenticate" + 4;
v13 = 13LL;
v14 = v9;
do
{
if ( !v13 )
break;
v10 = *v14 < (unsigned int)*v12;
v11 = *v14++ == *v12++;
--v13;
}
while ( v11 );
v15 = (!v10 && !v11) - v10;
v16 = 0;
v17 = v15 == 0;
if ( !v15 )
{
v8 = test_authenticate(v12);
goto LABEL_15;
}
v18 = "pam_chauthtok" + 4;
v19 = 10LL;
v20 = v9;
do
{
if ( !v19 )
break;
v16 = *v20 < (unsigned int)*v18;
v17 = *v20++ == *v18++;
--v19;
}
while ( v17 );
if ( (!v16 && !v17) != v16 )
break;
v8 = test_chauthtok(v18, v20);
LABEL_15:
if ( !v8 )
goto LABEL_29;
printf("%s: success\n", v9);
v7 = optind;
if ( optind >= argc )
goto LABEL_13;
}
fprintf(stderr, "unsupported action \"%s\"\n", v9);
LABEL_29:
fprintf(stderr, "%s: verification failed\n", v9);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v5; // eax
int result; // eax
int v7; // eax
int v8; // eax
const char *v9; // r12
bool v10; // cf
bool v11; // zf
const char *v12; // rdi
__int64 v13; // rcx
const char *v14; // rsi
char v15; // al
bool v16; // cf
bool v17; // zf
const char *v18; // rdi
__int64 v19; // rcx
const char *v20; // rsi
struct option v21; // [rsp+0h] [rbp-F8h] BYREF
const char *v22; // [rsp+20h] [rbp-D8h]
int v23; // [rsp+28h] [rbp-D0h]
int v24; // [rsp+38h] [rbp-C0h]
const char *v25; // [rsp+40h] [rbp-B8h]
int v26; // [rsp+48h] [rbp-B0h]
int v27; // [rsp+58h] [rbp-A0h]
void *v28; // [rsp+60h] [rbp-98h]
int v29; // [rsp+68h] [rbp-90h]
int v30; // [rsp+78h] [rbp-80h]
const char *v31; // [rsp+80h] [rbp-78h]
int v32; // [rsp+88h] [rbp-70h]
int v33; // [rsp+98h] [rbp-60h]
const char *v34; // [rsp+A0h] [rbp-58h]
int v35; // [rsp+A8h] [rbp-50h]
int v36; // [rsp+B8h] [rbp-40h]

memset(&v21.has_arg, 0, 0xD8uLL);
v21.name = "debug";
v22 = "username";
v23 = 1;
v24 = 1;
v25 = "password";
v26 = 1;
v27 = 2;
v28 = &unk_402089;
v29 = 1;
v30 = 3;
v31 = "new-password";
v32 = 1;
v33 = 4;
v34 = "pam-service";
v35 = 1;
v36 = 5;
while ( 2 )
{
v5 = getopt_long(argc, (char *const *)argv, "dupON", &v21, 0LL);
if ( v5 != -1 )
{
switch ( v5 )
{
case 0:
++opt_debug;
continue;
case 1:
opt_username = (char *)optarg;
continue;
case 2:
opt_password = (char *)optarg;
continue;
case 3:
opt_old_password = (char *)optarg;
continue;
case 4:
opt_new_password = (char *)optarg;
continue;
case 5:
opt_pam_service = (char *)optarg;
continue;
default:
fwrite("Invalid option, please see source code for documentation\n", 1uLL, 0x39uLL, stderr);
result = 1;
break;
}
return result;
}
break;
}
v7 = optind;
if ( optind >= argc )
{
func9((unsigned int)argc);
LABEL_13:
puts("All verifications completed successfully");
return 0;
}
while ( 1 )
{
optind = v7 + 1;
v9 = argv[v7];
v10 = 0;
v11 = opt_debug == 0;
if ( opt_debug )
fprintf(stderr, "%s: service=%s user=%s\n", argv[v7], opt_pam_service, opt_username);
v12 = "pam_authenticate" + 4;
v13 = 13LL;
v14 = v9;
do
{
if ( !v13 )
break;
v10 = *v14 < (unsigned int)*v12;
v11 = *v14++ == *v12++;
--v13;
}
while ( v11 );
v15 = (!v10 && !v11) - v10;
v16 = 0;
v17 = v15 == 0;
if ( !v15 )
{
v8 = func9(v12);
goto LABEL_15;
}
v18 = "pam_chauthtok" + 4;
v19 = 10LL;
v20 = v9;
do
{
if ( !v19 )
break;
v16 = *v20 < (unsigned int)*v18;
v17 = *v20++ == *v18++;
--v19;
}
while ( v17 );
if ( (!v16 && !v17) != v16 )
break;
v8 = func10(v18, v20);
LABEL_15:
if ( !v8 )
goto LABEL_29;
printf("%s: success\n", v9);
v7 = optind;
if ( optind >= argc )
goto LABEL_13;
}
fprintf(stderr, "unsupported action \"%s\"\n", v9);
LABEL_29:
fprintf(stderr, "%s: verification failed\n", v9);
return 1;
}

original_code:
int
main(int argc, char *argv[])
{
  enum { OPT_DEBUG, OPT_USERNAME, OPT_PASSWORD, OPT_OLD_PASSWORD, OPT_NEW_PASSWORD, OPT_PAM_SERVICE };
  struct option long_options[] = {
   { "debug", 0, ((void *)0), OPT_DEBUG },
   { "username", 1, ((void *)0), OPT_USERNAME },
   { "password", 1, ((void *)0), OPT_PASSWORD },
   { "old-password", 1, ((void *)0), OPT_OLD_PASSWORD },
   { "new-password", 1, ((void *)0), OPT_NEW_PASSWORD },
   { "pam-service", 1, ((void *)0), OPT_PAM_SERVICE },
   { ((void *)0) }
  };
  int c;
  while ((c = getopt_long(argc, argv, "dupON", long_options, ((void *)0))) != (-1)) {
    switch (c) {
    case OPT_DEBUG:
      opt_debug++;
      break;
    case OPT_USERNAME:
      opt_username = optarg;
      break;
    case OPT_PASSWORD:
      opt_password = optarg;
      break;
    case OPT_OLD_PASSWORD:
      opt_old_password = optarg;
      break;
    case OPT_NEW_PASSWORD:
      opt_new_password = optarg;
      break;
    case OPT_PAM_SERVICE:
      opt_pam_service = optarg;
      break;
    default:
      fprintf (stderr, "Invalid option, please see source code for documentation\n");
      return 1;
    }
  }
  if (optind >= argc)
    test_authenticate();
  else
    {
      while (optind < argc)
        {
          char *action = argv[optind++];
   int ok = 0;
   if (opt_debug)
     fprintf (stderr, "%s: service=%s user=%s\n", action, opt_pam_service, opt_username);
   if (!strcmp (action, "authenticate"))
     ok = test_authenticate();
   else if (!strcmp (action, "chauthtok"))
     ok = test_chauthtok();
   else
     fprintf (stderr, "unsupported action \"%s\"\n", action);
   if (!ok)
     {
       fprintf (stderr, "%s: verification failed\n", action);
       return 1;
     }
   printf("%s: success\n", action);
 }
    }
  printf ("All verifications completed successfully\n");
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: c int

variable_types:
action
char *

variable_types: ok int