decompiled_code:
__int64 wait_vsync()
{
__int64 v0; // rbx
__int64 v1; // rbx
__int64 result; // rax

v0 = next_time;
v1 = v0 - get_tick();
if ( tick_error || v1 <= 0 )
{
next_time += delta_time;
result = (unsigned int)(wait_counter + 1);
wait_counter = result;
if ( (int)result > 9 )
result = wait_vsync_reset();
if ( use_auto_skip )
{
result = (unsigned int)(skip_counter + 1);
skip_counter = result;
if ( (int)result > 14 )
{
skip_counter = 0;
do_skip_draw = 0;
already_skip_draw = 0;
result = reset_frame_counter();
}
else
{
do_skip_draw = 1;
}
}
}
else
{
if ( wait_by_sleep )
{
if ( sleep_min_time <= v1 )
{
delay_usec(v1);
}
else
{
while ( get_tick() < next_time && !tick_error )
;
}
}
else
{
while ( get_tick() < next_time && !tick_error )
;
}
result = delta_time;
next_time += delta_time;
wait_counter = 0;
if ( use_auto_skip )
{
skip_counter = 0;
do_skip_draw = 0;
if ( already_skip_draw )
{
already_skip_draw = 0;
result = reset_frame_counter();
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rbx
__int64 v1; // rbx
__int64 result; // rax

v0 = next_time;
v1 = v0 - func1();
if ( tick_error || v1 <= 0 )
{
next_time += delta_time;
result = (unsigned int)(wait_counter + 1);
wait_counter = result;
if ( (int)result > 9 )
result = func4();
if ( use_auto_skip )
{
result = (unsigned int)(skip_counter + 1);
skip_counter = result;
if ( (int)result > 14 )
{
skip_counter = 0;
do_skip_draw = 0;
already_skip_draw = 0;
result = reset_frame_counter();
}
else
{
do_skip_draw = 1;
}
}
}
else
{
if ( wait_by_sleep )
{
if ( sleep_min_time <= v1 )
{
func2(v1);
}
else
{
while ( func1() < next_time && !tick_error )
;
}
}
else
{
while ( func1() < next_time && !tick_error )
;
}
result = delta_time;
next_time += delta_time;
wait_counter = 0;
if ( use_auto_skip )
{
skip_counter = 0;
do_skip_draw = 0;
if ( already_skip_draw )
{
already_skip_draw = 0;
result = reset_frame_counter();
}
}
}
return result;
}

original_code:
void wait_vsync( void )
{
  int on_time = (0);
  T_WAIT_TICK diff_us;
  diff_us = next_time - get_tick();
  if( tick_error == (0) ){
    if( diff_us > 0 ){
      if( wait_by_sleep ){
 if( diff_us < sleep_min_time ){
   while( tick_error == (0) ){
     if( next_time <= get_tick() )
       break;
   }
 }else{
   delay_usec( diff_us );
 }
      }else{
 while( tick_error == (0) ){
   if( next_time <= get_tick() )
     break;
 }
      }
      on_time = (1);
    }
  }
  next_time += delta_time;
  if( on_time ){
    wait_counter = 0;
  }else{
    wait_counter ++;
    if( wait_counter >= wait_count_max ){
      wait_vsync_reset();
    }
  }
  if( use_auto_skip ){
    if( on_time ){
      skip_counter = 0;
      do_skip_draw = (0);
      if( already_skip_draw ){
 already_skip_draw = (0);
 reset_frame_counter();
      }
    }else{
      skip_counter++;
      if( skip_counter >= skip_count_max ){
 skip_counter = 0;
 do_skip_draw = (0);
 already_skip_draw = (0);
 reset_frame_counter();
      }else{
 do_skip_draw = (1);
      }
    }
  }
  return;
}

canonical_original_code:
void wait_vsync()
{
  int on_time = (0);
  long diff_us;
  diff_us = next_time - get_tick();
  if( tick_error == (0) ){
    if( diff_us > 0 ){
      if( wait_by_sleep ){
 if( diff_us < sleep_min_time ){
   while( tick_error == (0) ){
     if( next_time <= get_tick() )
       break;
   }
 }else{
   delay_usec( diff_us );
 }
      }else{
 while( tick_error == (0) ){
   if( next_time <= get_tick() )
     break;
 }
      }
      on_time = (1);
    }
  }
  next_time += delta_time;
  if( on_time ){
    wait_counter = 0;
  }else{
    wait_counter ++;
    if( wait_counter >= wait_count_max ){
      wait_vsync_reset();
    }
  }
  if( use_auto_skip ){
    if( on_time ){
      skip_counter = 0;
      do_skip_draw = (0);
      if( already_skip_draw ){
 already_skip_draw = (0);
 reset_frame_counter();
      }
    }else{
      skip_counter++;
      if( skip_counter >= skip_count_max ){
 skip_counter = 0;
 do_skip_draw = (0);
 already_skip_draw = (0);
 reset_frame_counter();
      }else{
 do_skip_draw = (1);
      }
    }
  }
  return;
}


decompiled_code:
int wait_menu()
{
return delay_usec(0x411Au);
}

canonical_decompiled_code:
int func7()
{
return func2(0x411Au);
}

original_code:
void wait_menu( void )
{
  delay_usec( 1000000 / 60 );
}

canonical_original_code:
void wait_menu()
{
  delay_usec( 1000000 / 60 );
}

return_type: void


decompiled_code:
int print_gettimeofday()
{
int result; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v1, 0LL);
if ( !result )
result = printf("%ld.%06ld\n", v1.tv_sec, v1.tv_usec);
return result;
}

canonical_decompiled_code:
int func8()
{
int result; // eax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v1, 0LL);
if ( !result )
result = printf("%ld.%06ld\n", v1.tv_sec, v1.tv_usec);
return result;
}

original_code:
void print_gettimeofday(void)
{
  struct timeval tv;
  if( gettimeofday( &tv, 0 ) == 0 ){
    printf( "%ld.%06ld\n", tv.tv_sec, tv.tv_usec );
  }
}

canonical_original_code:
void print_gettimeofday()
{
  struct timeval tv;
  if( gettimeofday( &tv, 0 ) == 0 ){
    printf( "%ld.%06ld\n", tv.tv_sec, tv.tv_usec );
  }
}

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


decompiled_code:
int __fastcall print_lapse(int a1)
{
if ( !t0_2.tv_sec || a1 < 0 )
return gettimeofday(&t0_2, 0LL);
gettimeofday(&t1_1, 0LL);
if ( a1 > 0 )
{
dt_0 = t1_1.tv_sec - t0_2.tv_sec;
qword_3E8 = t1_1.tv_usec - t0_2.tv_usec;
if ( t1_1.tv_usec - t0_2.tv_usec < 0 )
{
dt_0 = t1_1.tv_sec - t0_2.tv_sec - 1;
qword_3E8 = t1_1.tv_usec - t0_2.tv_usec + 1000000;
}
printf("%ld.%06ld\n", dt_0, qword_3E8);
}
t0_2 = t1_1;
return t1_1.tv_usec;
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
if ( !t0_2.tv_sec || a1 < 0 )
return gettimeofday(&t0_2, 0LL);
gettimeofday(&t1_1, 0LL);
if ( a1 > 0 )
{
dt_0 = t1_1.tv_sec - t0_2.tv_sec;
qword_3E8 = t1_1.tv_usec - t0_2.tv_usec;
if ( t1_1.tv_usec - t0_2.tv_usec < 0 )
{
dt_0 = t1_1.tv_sec - t0_2.tv_sec - 1;
qword_3E8 = t1_1.tv_usec - t0_2.tv_usec + 1000000;
}
printf("%ld.%06ld\n", dt_0, qword_3E8);
}
t0_2 = t1_1;
return t1_1.tv_usec;
}

original_code:
void print_lapse(int flag)
{
  static struct timeval t0, t1, dt;
  if( t0.tv_sec == 0 || flag < 0 ){
    gettimeofday( &t0, 0 );
  }else{
    gettimeofday( &t1, 0 );
    if( flag > 0 ){
      dt.tv_sec = t1.tv_sec - t0.tv_sec;
      dt.tv_usec = t1.tv_usec - t0.tv_usec;
      if( dt.tv_usec < 0 ){
 dt.tv_sec --;
 dt.tv_usec += 1000000;
      }
      printf( "%ld.%06ld\n", dt.tv_sec, dt.tv_usec );
    }
    t0.tv_sec = t1.tv_sec;
    t0.tv_usec = t1.tv_usec;
  }
}

canonical_original_code:
void print_lapse(int flag)
{
  static struct timeval t0; static struct timeval t1; static struct timeval dt;
  if( t0.tv_sec == 0 || flag < 0 ){
    gettimeofday( &t0, 0 );
  }else{
    gettimeofday( &t1, 0 );
    if( flag > 0 ){
      dt.tv_sec = t1.tv_sec - t0.tv_sec;
      dt.tv_usec = t1.tv_usec - t0.tv_usec;
      if( dt.tv_usec < 0 ){
 dt.tv_sec --;
 dt.tv_usec += 1000000;
      }
      printf( "%ld.%06ld\n", dt.tv_sec, dt.tv_usec );
    }
    t0.tv_sec = t1.tv_sec;
    t0.tv_usec = t1.tv_usec;
  }
}

variable_types: flag int

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


decompiled_code:
__int64 __fastcall z80_line_disasm(__int64 a1, unsigned __int16 a2)
{
__int64 v2; // r13
unsigned int v3; // er14
unsigned __int8 v4; // bl
unsigned __int8 v5; // r14
unsigned int v6; // ebx
unsigned int v7; // ebx
unsigned __int8 v8; // al
const char **v9; // r15
unsigned int v10; // er13
unsigned __int8 v11; // bl
unsigned __int8 v12; // al
unsigned __int8 v13; // al
unsigned __int8 v14; // bl
unsigned __int8 v15; // r13
unsigned int v16; // ebx
char v17; // al
unsigned __int8 v18; // r13
unsigned __int8 v19; // al
char v20; // al
const char **v21; // r14
char v23; // al
unsigned __int8 v24; // r15
unsigned __int8 v25; // bl
unsigned __int8 v26; // al
unsigned __int8 v27; // [rsp+Fh] [rbp-39h]

v2 = (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))(a2);
v3 = Instruction[4 * v2];
switch ( v3 )
{
case 0u:
printf_head(a1, a2, 1);
printf((&off_9E28)[2 * (int)v2]);
return 1;
case 1u:
v4 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
printf_head(a1, a2, 2);
printf((&off_9E28)[2 * (int)v2], v4);
return 2;
case 2u:
v5 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
v6 = v5 + ((*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2)) << 8);
printf_head(a1, a2, 3);
printf((&off_9E28)[2 * (int)v2], v6);
return 3;
case 3u:
v7 = a2 + (*(char (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)) + 2;
printf_head(a1, a2, 2);
printf((&off_9E28)[2 * (int)v2], v7);
return 2;
case 4u:
v8 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
if ( v8 == 0xED )
{
v9 = (const char **)((char *)&Instruction_ED
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
}
else if ( v8 > 0xEDu )
{
if ( v8 != 0xFD )
goto LABEL_16;
v9 = (const char **)((char *)&Instruction_FD
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
}
else
{
if ( v8 != 0xCB )
{
if ( v8 == 0xDD )
{
v9 = (const char **)((char *)&Instruction_DD
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
goto LABEL_14;
}
LABEL_16:
printf(" - Internal Error ! - ");
return 2;
}
v9 = (const char **)((char *)&Instruction_CB
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
}
LABEL_14:
v10 = *(_DWORD *)v9;
switch ( *(_DWORD *)v9 )
{
case 0:
printf_head(a1, a2, 2);
printf(v9[1]);
return 2;
case 1:
v14 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf_head(a1, a2, 3);
printf(v9[1], v14);
return 3;
case 2:
v15 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
v16 = v15 + ((*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3)) << 8);
printf_head(a1, a2, 4);
printf(v9[1], v16);
return v3;
case 4:
v20 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
if ( v20 == -35 )
{
v21 = (const char **)((char *)&Instruction_DD_CB
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3)));
}
else
{
if ( v20 != -3 )
{
printf(" - Internal Error - ");
return v10;
}
v21 = (const char **)((char *)&Instruction_FD_CB
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3)));
}
if ( *(_DWORD *)v21 == 5 )
{
printf_head(a1, a2, 4);
v23 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf(v21[1], (unsigned int)v23);
v3 = v10;
}
else
{
if ( *(_DWORD *)v21 != 7 )
goto LABEL_30;
printf_head(a1, a2, 4);
v27 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3));
v24 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
v25 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
v26 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
printf(v21[1], v26, v25, v24, v27);
v3 = v10;
}
break;
case 5:
printf_head(a1, a2, 3);
v17 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf(v9[1], (unsigned int)v17);
return 3;
case 6:
v18 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3));
printf_head(a1, a2, 4);
v19 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf(v9[1], v19, v18);
return v3;
case 7:
printf_head(a1, a2, 2);
v11 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
v12 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
printf(v9[1], v12, v11);
return 2;
case 8:
printf_head(a1, a2, 1);
v13 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
printf(v9[1], v13);
return 1;
default:
goto LABEL_30;
}
return v3;
default:
LABEL_30:
printf(" - Internal Error - ");
return 1;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int16 a2)
{
__int64 v2; // r13
unsigned int v3; // er14
unsigned __int8 v4; // bl
unsigned __int8 v5; // r14
unsigned int v6; // ebx
unsigned int v7; // ebx
unsigned __int8 v8; // al
const char **v9; // r15
unsigned int v10; // er13
unsigned __int8 v11; // bl
unsigned __int8 v12; // al
unsigned __int8 v13; // al
unsigned __int8 v14; // bl
unsigned __int8 v15; // r13
unsigned int v16; // ebx
char v17; // al
unsigned __int8 v18; // r13
unsigned __int8 v19; // al
char v20; // al
const char **v21; // r14
char v23; // al
unsigned __int8 v24; // r15
unsigned __int8 v25; // bl
unsigned __int8 v26; // al
unsigned __int8 v27; // [rsp+Fh] [rbp-39h]

v2 = (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))(a2);
v3 = Instruction[4 * v2];
switch ( v3 )
{
case 0u:
func0(a1, a2, 1);
printf((&off_9E28)[2 * (int)v2]);
return 1;
case 1u:
v4 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
func0(a1, a2, 2);
printf((&off_9E28)[2 * (int)v2], v4);
return 2;
case 2u:
v5 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
v6 = v5 + ((*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2)) << 8);
func0(a1, a2, 3);
printf((&off_9E28)[2 * (int)v2], v6);
return 3;
case 3u:
v7 = a2 + (*(char (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)) + 2;
func0(a1, a2, 2);
printf((&off_9E28)[2 * (int)v2], v7);
return 2;
case 4u:
v8 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
if ( v8 == 0xED )
{
v9 = (const char **)((char *)&Instruction_ED
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
}
else if ( v8 > 0xEDu )
{
if ( v8 != 0xFD )
goto LABEL_16;
v9 = (const char **)((char *)&Instruction_FD
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
}
else
{
if ( v8 != 0xCB )
{
if ( v8 == 0xDD )
{
v9 = (const char **)((char *)&Instruction_DD
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
goto LABEL_14;
}
LABEL_16:
printf(" - Internal Error ! - ");
return 2;
}
v9 = (const char **)((char *)&Instruction_CB
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1)));
}
LABEL_14:
v10 = *(_DWORD *)v9;
switch ( *(_DWORD *)v9 )
{
case 0:
func0(a1, a2, 2);
printf(v9[1]);
return 2;
case 1:
v14 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
func0(a1, a2, 3);
printf(v9[1], v14);
return 3;
case 2:
v15 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
v16 = v15 + ((*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3)) << 8);
func0(a1, a2, 4);
printf(v9[1], v16);
return v3;
case 4:
v20 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
if ( v20 == -35 )
{
v21 = (const char **)((char *)&Instruction_DD_CB
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3)));
}
else
{
if ( v20 != -3 )
{
printf(" - Internal Error - ");
return v10;
}
v21 = (const char **)((char *)&Instruction_FD_CB
+ 16 * (*(unsigned __int8 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3)));
}
if ( *(_DWORD *)v21 == 5 )
{
func0(a1, a2, 4);
v23 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf(v21[1], (unsigned int)v23);
v3 = v10;
}
else
{
if ( *(_DWORD *)v21 != 7 )
goto LABEL_30;
func0(a1, a2, 4);
v27 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3));
v24 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
v25 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
v26 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
printf(v21[1], v26, v25, v24, v27);
v3 = v10;
}
break;
case 5:
func0(a1, a2, 3);
v17 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf(v9[1], (unsigned int)v17);
return 3;
case 6:
v18 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 3));
func0(a1, a2, 4);
v19 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 2));
printf(v9[1], v19, v18);
return v3;
case 7:
func0(a1, a2, 2);
v11 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))((unsigned __int16)(a2 + 1));
v12 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
printf(v9[1], v12, v11);
return 2;
case 8:
func0(a1, a2, 1);
v13 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 64))(a2);
printf(v9[1], v13);
return 1;
default:
goto LABEL_30;
}
return v3;
default:
LABEL_30:
printf(" - Internal Error - ");
return 1;
}
}

original_code:
int z80_line_disasm( z80arch *z80, word pc )
{
  int num;
  Mnemonics *Inst;
  Inst = &Instruction[ (z80->mem_read)(pc) ];
  switch( Inst->type ){
  case OP_NOTHING:
    printf_head( z80, pc, 1 );
    printf( Inst->str );
    return 1;
  case OP_NUM_8:
    num = (z80->mem_read)(pc+1);
    printf_head( z80, pc, 2 );
    printf( Inst->str, num );
    return 2;
  case OP_NUM_16:
    num = (z80->mem_read)(pc+1) + (z80->mem_read)(pc+2)*256;
    printf_head( z80, pc, 3 );
    printf( Inst->str, num );
    return 3;
  case OP_ADR_REL:
    num = pc +2 + (offset)(z80->mem_read)(pc+1);
    printf_head( z80, pc, 2 );
    printf( Inst->str, num );
    return 2;
  case OP_PREFIX:
    switch( (z80->mem_read)(pc) ){
    case 0xCB: Inst = &Instruction_CB[ (z80->mem_read)(pc+1) ]; break;
    case 0xDD: Inst = &Instruction_DD[ (z80->mem_read)(pc+1) ]; break;
    case 0xED: Inst = &Instruction_ED[ (z80->mem_read)(pc+1) ]; break;
    case 0xFD: Inst = &Instruction_FD[ (z80->mem_read)(pc+1) ]; break;
    default: printf(" - Internal Error ! - "); return 2;
    }
    switch( Inst->type ){
    case OP_NOTHING:
      printf_head( z80, pc, 2 );
      printf( Inst->str );
      return 2;
    case OP_UNEXIST:
      printf_head( z80, pc, 2 );
      printf( Inst->str, (z80->mem_read)(pc), (z80->mem_read)(pc+1) );
      return 2;
    case OP_SKIP:
      printf_head( z80, pc, 1 );
      printf( Inst->str, (z80->mem_read)(pc) );
      return 1;
    case OP_NUM_8:
      num = (z80->mem_read)(pc+2);
      printf_head( z80, pc, 3 );
      printf( Inst->str, num );
      return 3;
    case OP_NUM_16:
      num = (z80->mem_read)(pc+2) + (z80->mem_read)(pc+3)*256;
      printf_head( z80, pc, 4 );
      printf( Inst->str, num );
      return 4;
    case OP_INDEX:
      printf_head( z80, pc, 3 );
      printf( Inst->str, (offset)(z80->mem_read)(pc+2) );
      return 3;
    case OP_IDX_NUM:
      num = (int)(z80->mem_read)(pc+3);
      printf_head( z80, pc, 4 );
      printf( Inst->str, (int)(z80->mem_read)(pc+2) ,num );
      return 4;
    case OP_PREFIX:
      switch( (z80->mem_read)(pc) ){
      case 0xDD: Inst = &Instruction_DD_CB[ (z80->mem_read)(pc+3) ]; break;
      case 0xFD: Inst = &Instruction_FD_CB[ (z80->mem_read)(pc+3) ]; break;
      default: printf(" - Internal Error - "); return 4;
      }
      switch( Inst->type ){
      case OP_INDEX:
 printf_head( z80, pc, 4 );
 printf( Inst->str, (offset)(z80->mem_read)(pc+2) );
 return 4;
      case OP_UNEXIST:
 printf_head( z80, pc, 4 );
 printf( Inst->str, (z80->mem_read)(pc), (z80->mem_read)(pc+1),
                    (z80->mem_read)(pc+2),(z80->mem_read)(pc+3));
 return 4;
      }
      break;
    }
    break;
  }
  printf(" - Internal Error - ");
  return 1;
}

canonical_original_code:
int z80_line_disasm(struct z80arch *z80, unsigned short pc)
{
  int num;
  struct Mnemonics *Inst;
  Inst = &Instruction[ (z80->mem_read)(pc) ];
  switch( Inst->type ){
  case OP_NOTHING:
    printf_head( z80, pc, 1 );
    printf( Inst->str );
    return 1;
  case OP_NUM_8:
    num = (z80->mem_read)(pc+1);
    printf_head( z80, pc, 2 );
    printf( Inst->str, num );
    return 2;
  case OP_NUM_16:
    num = (z80->mem_read)(pc+1) + (z80->mem_read)(pc+2)*256;
    printf_head( z80, pc, 3 );
    printf( Inst->str, num );
    return 3;
  case OP_ADR_REL:
    num = pc +2 + (offset)(z80->mem_read)(pc+1);
    printf_head( z80, pc, 2 );
    printf( Inst->str, num );
    return 2;
  case OP_PREFIX:
    switch( (z80->mem_read)(pc) ){
    case 0xCB: Inst = &Instruction_CB[ (z80->mem_read)(pc+1) ]; break;
    case 0xDD: Inst = &Instruction_DD[ (z80->mem_read)(pc+1) ]; break;
    case 0xED: Inst = &Instruction_ED[ (z80->mem_read)(pc+1) ]; break;
    case 0xFD: Inst = &Instruction_FD[ (z80->mem_read)(pc+1) ]; break;
    default: printf(" - Internal Error ! - "); return 2;
    }
    switch( Inst->type ){
    case OP_NOTHING:
      printf_head( z80, pc, 2 );
      printf( Inst->str );
      return 2;
    case OP_UNEXIST:
      printf_head( z80, pc, 2 );
      printf( Inst->str, (z80->mem_read)(pc), (z80->mem_read)(pc+1) );
      return 2;
    case OP_SKIP:
      printf_head( z80, pc, 1 );
      printf( Inst->str, (z80->mem_read)(pc) );
      return 1;
    case OP_NUM_8:
      num = (z80->mem_read)(pc+2);
      printf_head( z80, pc, 3 );
      printf( Inst->str, num );
      return 3;
    case OP_NUM_16:
      num = (z80->mem_read)(pc+2) + (z80->mem_read)(pc+3)*256;
      printf_head( z80, pc, 4 );
      printf( Inst->str, num );
      return 4;
    case OP_INDEX:
      printf_head( z80, pc, 3 );
      printf( Inst->str, (offset)(z80->mem_read)(pc+2) );
      return 3;
    case OP_IDX_NUM:
      num = (int)(z80->mem_read)(pc+3);
      printf_head( z80, pc, 4 );
      printf( Inst->str, (int)(z80->mem_read)(pc+2) ,num );
      return 4;
    case OP_PREFIX:
      switch( (z80->mem_read)(pc) ){
      case 0xDD: Inst = &Instruction_DD_CB[ (z80->mem_read)(pc+3) ]; break;
      case 0xFD: Inst = &Instruction_FD_CB[ (z80->mem_read)(pc+3) ]; break;
      default: printf(" - Internal Error - "); return 4;
      }
      switch( Inst->type ){
      case OP_INDEX:
 printf_head( z80, pc, 4 );
 printf( Inst->str, (offset)(z80->mem_read)(pc+2) );
 return 4;
      case OP_UNEXIST:
 printf_head( z80, pc, 4 );
 printf( Inst->str, (z80->mem_read)(pc), (z80->mem_read)(pc+1),
                    (z80->mem_read)(pc+2),(z80->mem_read)(pc+3));
 return 4;
      }
      break;
    }
    break;
  }
  printf(" - Internal Error - ");
  return 1;
}

variable_types: struct z80arch { union pair { struct { unsigned char l; } B; unsigned short W; } AF; union pair IX; union pair AF1; unsigned char I; unsigned char R_saved; unsigned char IFF; unsigned char IM; unsigned char HALT; int INT_active; int icount; int state0; int skip_intr_chk; unsigned char log; unsigned char break_if_halt; unsigned char (*fetch)(unsigned short); unsigned char (*mem_read)(unsigned short); void (*mem_write)(unsigned short, unsigned char); unsigned char (*io_read)(unsigned char); void (*io_write)(unsigned char, unsigned char); void (*intr_update)(); int (*intr_ack)(); union pair PC_prev; } *

variable_types:
pc
unsigned short

variable_types: num int

variable_types: struct Mnemonics { int type; char *str; } *

return_type: int

user_defined_types: struct z80arch { union pair AF; union pair IX; union pair AF1; unsigned char I; unsigned char R_saved; unsigned char IFF; unsigned char IM; unsigned char HALT; int INT_active; int icount; int state0; int skip_intr_chk; unsigned char log; unsigned char break_if_halt; unsigned char (*fetch)(unsigned short); unsigned char (*mem_read)(unsigned short); void (*mem_write)(unsigned short, unsigned char); unsigned char (*io_read)(unsigned char); void (*io_write)(unsigned char, unsigned char); void (*intr_update)(); int (*intr_ack)(); union pair PC_prev; }


variable_types:
mes
char *

variable_types:
flags
char [8]

variable_types:
fbuf
char [10]

variable_types: i int

variable_types: j int

user_defined_types: union pair { struct { unsigned char l; } B; unsigned short W; }


original_code:
static void printf_head( z80arch *z80, word pc, int num )
{
  printf("%04X ",pc);
  switch( num ){
  case 1:
    printf("%02X       ", (z80->mem_read)(pc));
    break;
  case 2:
    printf("%02X%02X     ", (z80->mem_read)(pc), (z80->mem_read)(pc+1));
    break;
  case 3:
    printf("%02X%02X%02X   ", (z80->mem_read)(pc), (z80->mem_read)(pc+1),
                        (z80->mem_read)(pc+2));
    break;
  case 4:
    printf("%02X%02X%02X%02X ",(z80->mem_read)(pc), (z80->mem_read)(pc+1),
                        (z80->mem_read)(pc+2),(z80->mem_read)(pc+3));
    break;
  default:
    printf("Internal Error -");
    break;
  }
}

canonical_original_code:
static void printf_head(struct z80arch *z80, unsigned short pc, int num)
{
  printf("%04X ",pc);
  switch( num ){
  case 1:
    printf("%02X       ", (z80->mem_read)(pc));
    break;
  case 2:
    printf("%02X%02X     ", (z80->mem_read)(pc), (z80->mem_read)(pc+1));
    break;
  case 3:
    printf("%02X%02X%02X   ", (z80->mem_read)(pc), (z80->mem_read)(pc+1),
                        (z80->mem_read)(pc+2));
    break;
  case 4:
    printf("%02X%02X%02X%02X ",(z80->mem_read)(pc), (z80->mem_read)(pc+1),
                        (z80->mem_read)(pc+2),(z80->mem_read)(pc+3));
    break;
  default:
    printf("Internal Error -");
    break;
  }
}


variable_types: struct SYSTEM_PALETTE_T { unsigned char red; unsigned char green; unsigned char blue; unsigned char padding; } [16]

variable_types:
pal
struct { int pal; int col; } [16]


decompiled_code:
void menu_set_status()
{
menu_draw_force = 1;
}

canonical_decompiled_code:
void func1()
{
menu_draw_force = 1;
}

original_code:
void menu_set_status( void )
{
  menu_draw_force = (1);
}

canonical_original_code:
void menu_set_status()
{
  menu_draw_force = (1);
}


decompiled_code:
void menu_trans_palette()
{
menu_need_trans_palette = 1;
}

canonical_decompiled_code:
void func2()
{
menu_need_trans_palette = 1;
}

original_code:
void menu_trans_palette( void )
{
  menu_need_trans_palette = (1);
}

canonical_original_code:
void menu_trans_palette()
{
  menu_need_trans_palette = (1);
}


decompiled_code:
__int64 menu_draw_screen()
{
__int64 result; // rax

if ( menu_need_trans_palette )
{
menu_palette();
menu_need_trans_palette = 0;
}
if ( menu_draw_force )
{
result = draw_menu_screen_force();
menu_draw_force = 0;
}
else
{
result = draw_menu_screen();
}
menu_screen_current ^= 1u;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

if ( menu_need_trans_palette )
{
func0();
menu_need_trans_palette = 0;
}
if ( menu_draw_force )
{
result = draw_menu_screen_force();
menu_draw_force = 0;
}
else
{
result = draw_menu_screen();
}
menu_screen_current ^= 1u;
return result;
}

original_code:
void menu_draw_screen( void )
{
  if( menu_need_trans_palette ){
    menu_palette();
    menu_need_trans_palette = (0);
  }
  if( menu_draw_force ){
    draw_menu_screen_force();
    menu_draw_force = (0);
  }else{
    draw_menu_screen();
  }
  menu_screen_current ^= 1;
}

canonical_original_code:
void menu_draw_screen()
{
  if( menu_need_trans_palette ){
    menu_palette();
    menu_need_trans_palette = (0);
  }
  if( menu_draw_force ){
    draw_menu_screen_force();
    menu_draw_force = (0);
  }else{
    draw_menu_screen();
  }
  menu_screen_current ^= 1;
}


decompiled_code:
__int64 __fastcall YM_DELTAT_ADPCM_Read(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // eax
void (__fastcall *v3)(_QWORD, _QWORD); // rax
unsigned __int8 v4; // si

v1 = *(unsigned __int8 *)(a1 + 93);
if ( (_BYTE)v1 )
{
*(_BYTE *)(a1 + 93) = v1 - 1;
return 0;
}
v2 = *(_DWORD *)(a1 + 40);
if ( v2 == 2 * *(_DWORD *)(a1 + 60) )
{
v3 = *(void (__fastcall **)(_QWORD, _QWORD))(a1 + 96);
if ( !v3 )
return v1;
v4 = *(_BYTE *)(a1 + 113);
if ( !v4 )
return *(unsigned __int8 *)(a1 + 113);
goto LABEL_5;
}
v1 = *(unsigned __int8 *)(*(_QWORD *)a1 + (v2 >> 1));
*(_DWORD *)(a1 + 40) = v2 + 2;
v3 = *(void (__fastcall **)(_QWORD, _QWORD))(a1 + 96);
if ( v3 )
{
v4 = *(_BYTE *)(a1 + 114);
if ( v4 )
{
LABEL_5:
v3(*(unsigned __int8 *)(a1 + 112), v4);
return v1;
}
}
return v1;
}

original_code:
UINT8 YM_DELTAT_ADPCM_Read(YM_DELTAT *DELTAT)
{
 UINT8 v = 0;
 if (DELTAT->memread)
 {
  DELTAT->memread--;
  return 0;
 }
 if ( DELTAT->now_addr != (DELTAT->end<<1) )
 {
  v = DELTAT->memory[DELTAT->now_addr>>1];
  DELTAT->now_addr+=2;
  if(DELTAT->status_set_handler)
   if(DELTAT->status_change_BRDY_bit)
    (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit);
 }
 else
 {
  if(DELTAT->status_set_handler)
   if(DELTAT->status_change_EOS_bit)
    (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit);
 }
 return v;
}

canonical_original_code:
unsigned char YM_DELTAT_ADPCM_Read(struct deltat_adpcm_state *DELTAT)
{
 unsigned char v = 0;
 if (DELTAT->memread)
 {
  DELTAT->memread--;
  return 0;
 }
 if ( DELTAT->now_addr != (DELTAT->end<<1) )
 {
  v = DELTAT->memory[DELTAT->now_addr>>1];
  DELTAT->now_addr+=2;
  if(DELTAT->status_set_handler)
   if(DELTAT->status_change_BRDY_bit)
    (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit);
 }
 else
 {
  if(DELTAT->status_set_handler)
   if(DELTAT->status_change_EOS_bit)
    (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit);
 }
 return v;
}

variable_types: struct deltat_adpcm_state { unsigned char *memory; int *output_pointer; int *pan; double freqbase; unsigned int memory_size; int output_range; unsigned int now_addr; unsigned int now_step; unsigned int step; unsigned int start; unsigned int limit; unsigned int end; unsigned int delta; int volume; int acc; int adpcmd; int adpcml; int prev_acc; unsigned char now_data; unsigned char portstate; unsigned char control2; unsigned char portshift; unsigned char DRAMportshift; unsigned char memread; void (*status_set_handler)(unsigned char which_chip, unsigned char status_bits); void (*status_reset_handler)(unsigned char which_chip, unsigned char status_bits); unsigned char status_change_which_chip; unsigned char status_change_EOS_bit; unsigned char status_change_BRDY_bit; unsigned char status_change_ZERO_bit; unsigned char PCM_BSY; unsigned char reg[16]; } *

variable_types:
v
unsigned char

return_type: unsigned char


original_code:
void YM_DELTAT_ADPCM_Write(YM_DELTAT *DELTAT,int r,int v)
{
 if(r>=0x10) return;
 DELTAT->reg[r] = v;
 switch( r )
 {
 case 0x00:
  DELTAT->portstate = v & (0x80|0x40|0x20|0x10);
  if( DELTAT->portstate&0x80 )
  {
   DELTAT->PCM_BSY = 1;
   DELTAT->now_step = 0;
   DELTAT->acc = 0;
   DELTAT->prev_acc = 0;
   DELTAT->adpcml = 0;
   DELTAT->adpcmd = (127);
  }
  if( DELTAT->portstate&0x20 )
  {
   DELTAT->now_addr = DELTAT->start << 1;
   DELTAT->memread = 2;
   if(DELTAT->memory == 0)
   {
    logerror("YM Delta-T ADPCM rom not mapped\n");
    DELTAT->portstate = 0x00;
    DELTAT->PCM_BSY = 0;
   }
   else
   {
    if( DELTAT->end >= DELTAT->memory_size )
    {
     logerror("YM Delta-T ADPCM end out of range: $%08x\n", DELTAT->end);
     DELTAT->end = DELTAT->memory_size - 1;
    }
    if( DELTAT->start >= DELTAT->memory_size )
    {
     logerror("YM Delta-T ADPCM start out of range: $%08x\n", DELTAT->start);
     DELTAT->portstate = 0x00;
     DELTAT->PCM_BSY = 0;
    }
   }
  }
  else
  {
   DELTAT->now_addr = 0;
  }
  if( v & 0x01 )
  {
   DELTAT->portstate = 0x00;
   DELTAT->PCM_BSY = 0;
  }
  break;
 case 0x01:
  DELTAT->pan = &DELTAT->output_pointer[(v>>6)&0x03];
  if ((DELTAT->control2 & 3) != (v & 3))
  {
   static UINT8 dram_rightshift[4]={3,0,0,0};
   if (DELTAT->DRAMportshift != dram_rightshift[v&3])
   {
    DELTAT->DRAMportshift = dram_rightshift[v&3];
    DELTAT->start = (DELTAT->reg[0x3]*0x0100 | DELTAT->reg[0x2]) << (DELTAT->portshift - DELTAT->DRAMportshift);
    DELTAT->end = (DELTAT->reg[0x5]*0x0100 | DELTAT->reg[0x4]) << (DELTAT->portshift - DELTAT->DRAMportshift);
    DELTAT->end += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
    DELTAT->limit = (DELTAT->reg[0xd]*0x0100 | DELTAT->reg[0xc]) << (DELTAT->portshift - DELTAT->DRAMportshift);
    DELTAT->limit += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
   }
  }
  DELTAT->control2 = v;
  break;
 case 0x02:
 case 0x03:
  DELTAT->start = (DELTAT->reg[0x3]*0x0100 | DELTAT->reg[0x2]) << (DELTAT->portshift - DELTAT->DRAMportshift);
  break;
 case 0x04:
 case 0x05:
  DELTAT->end = (DELTAT->reg[0x5]*0x0100 | DELTAT->reg[0x4]) << (DELTAT->portshift - DELTAT->DRAMportshift);
  DELTAT->end += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
  break;
 case 0x06:
 case 0x07:
  break;
 case 0x08:
  if ( (DELTAT->portstate & 0xe0)==0x60 )
  {
   if ( DELTAT->now_addr != (DELTAT->end<<1) )
   {
    DELTAT->memory[DELTAT->now_addr>>1] = v;
     DELTAT->now_addr+=2;
    if(DELTAT->status_set_handler)
     if(DELTAT->status_change_BRDY_bit)
      (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit);
   }
   else
   {
    if(DELTAT->status_set_handler)
     if(DELTAT->status_change_EOS_bit)
      (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit);
   }
            return;
  }
  if ( (DELTAT->portstate & 0xe0)==0x80 )
  {
   DELTAT->now_data = v;
   return;
  }
   break;
 case 0x09:
 case 0x0a:
  DELTAT->delta = (DELTAT->reg[0xa]*0x0100 | DELTAT->reg[0x9]);
  DELTAT->step = (UINT32)( (double)(DELTAT->delta ) * (DELTAT->freqbase) );
  break;
 case 0x0b:
  {
   INT32 oldvol = DELTAT->volume;
   DELTAT->volume = (v&0xff) * (DELTAT->output_range/256) / 32768;
   if( oldvol != 0 )
   {
    DELTAT->adpcml = (int)((double)DELTAT->adpcml / (double)oldvol * (double)DELTAT->volume);
   }
  }
  break;
 case 0x0c:
 case 0x0d:
  DELTAT->limit = (DELTAT->reg[0xd]*0x0100 | DELTAT->reg[0xc]) << (DELTAT->portshift - DELTAT->DRAMportshift);
  DELTAT->limit += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
  break;
 }
}

canonical_original_code:
void YM_DELTAT_ADPCM_Write(struct deltat_adpcm_state *DELTAT, int r, int v)
{
 if(r>=0x10) return;
 DELTAT->reg[r] = v;
 switch( r )
 {
 case 0x00:
  DELTAT->portstate = v & (0x80|0x40|0x20|0x10);
  if( DELTAT->portstate&0x80 )
  {
   DELTAT->PCM_BSY = 1;
   DELTAT->now_step = 0;
   DELTAT->acc = 0;
   DELTAT->prev_acc = 0;
   DELTAT->adpcml = 0;
   DELTAT->adpcmd = (127);
  }
  if( DELTAT->portstate&0x20 )
  {
   DELTAT->now_addr = DELTAT->start << 1;
   DELTAT->memread = 2;
   if(DELTAT->memory == 0)
   {
    logerror("YM Delta-T ADPCM rom not mapped\n");
    DELTAT->portstate = 0x00;
    DELTAT->PCM_BSY = 0;
   }
   else
   {
    if( DELTAT->end >= DELTAT->memory_size )
    {
     logerror("YM Delta-T ADPCM end out of range: $%08x\n", DELTAT->end);
     DELTAT->end = DELTAT->memory_size - 1;
    }
    if( DELTAT->start >= DELTAT->memory_size )
    {
     logerror("YM Delta-T ADPCM start out of range: $%08x\n", DELTAT->start);
     DELTAT->portstate = 0x00;
     DELTAT->PCM_BSY = 0;
    }
   }
  }
  else
  {
   DELTAT->now_addr = 0;
  }
  if( v & 0x01 )
  {
   DELTAT->portstate = 0x00;
   DELTAT->PCM_BSY = 0;
  }
  break;
 case 0x01:
  DELTAT->pan = &DELTAT->output_pointer[(v>>6)&0x03];
  if ((DELTAT->control2 & 3) != (v & 3))
  {
   static unsigned char dram_rightshift[4] = {3,0,0,0};
   if (DELTAT->DRAMportshift != dram_rightshift[v&3])
   {
    DELTAT->DRAMportshift = dram_rightshift[v&3];
    DELTAT->start = (DELTAT->reg[0x3]*0x0100 | DELTAT->reg[0x2]) << (DELTAT->portshift - DELTAT->DRAMportshift);
    DELTAT->end = (DELTAT->reg[0x5]*0x0100 | DELTAT->reg[0x4]) << (DELTAT->portshift - DELTAT->DRAMportshift);
    DELTAT->end += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
    DELTAT->limit = (DELTAT->reg[0xd]*0x0100 | DELTAT->reg[0xc]) << (DELTAT->portshift - DELTAT->DRAMportshift);
    DELTAT->limit += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
   }
  }
  DELTAT->control2 = v;
  break;
 case 0x02:
 case 0x03:
  DELTAT->start = (DELTAT->reg[0x3]*0x0100 | DELTAT->reg[0x2]) << (DELTAT->portshift - DELTAT->DRAMportshift);
  break;
 case 0x04:
 case 0x05:
  DELTAT->end = (DELTAT->reg[0x5]*0x0100 | DELTAT->reg[0x4]) << (DELTAT->portshift - DELTAT->DRAMportshift);
  DELTAT->end += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
  break;
 case 0x06:
 case 0x07:
  break;
 case 0x08:
  if ( (DELTAT->portstate & 0xe0)==0x60 )
  {
   if ( DELTAT->now_addr != (DELTAT->end<<1) )
   {
    DELTAT->memory[DELTAT->now_addr>>1] = v;
     DELTAT->now_addr+=2;
    if(DELTAT->status_set_handler)
     if(DELTAT->status_change_BRDY_bit)
      (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit);
   }
   else
   {
    if(DELTAT->status_set_handler)
     if(DELTAT->status_change_EOS_bit)
      (DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit);
   }
            return;
  }
  if ( (DELTAT->portstate & 0xe0)==0x80 )
  {
   DELTAT->now_data = v;
   return;
  }
   break;
 case 0x09:
 case 0x0a:
  DELTAT->delta = (DELTAT->reg[0xa]*0x0100 | DELTAT->reg[0x9]);
  DELTAT->step = (UINT32)( (double)(DELTAT->delta ) * (DELTAT->freqbase) );
  break;
 case 0x0b:
  {
   int oldvol = DELTAT->volume;
   DELTAT->volume = (v&0xff) * (DELTAT->output_range/256) / 32768;
   if( oldvol != 0 )
   {
    DELTAT->adpcml = (int)((double)DELTAT->adpcml / (double)oldvol * (double)DELTAT->volume);
   }
  }
  break;
 case 0x0c:
 case 0x0d:
  DELTAT->limit = (DELTAT->reg[0xd]*0x0100 | DELTAT->reg[0xc]) << (DELTAT->portshift - DELTAT->DRAMportshift);
  DELTAT->limit += (1 << (DELTAT->portshift-DELTAT->DRAMportshift) ) - 1;
  break;
 }
}

variable_types: r int

variable_types: v int

variable_types: oldvol int

user_defined_types: struct deltat_adpcm_state { unsigned char *memory; int *output_pointer; int *pan; double freqbase; unsigned int memory_size; int output_range; unsigned int now_addr; unsigned int now_step; unsigned int step; unsigned int start; unsigned int limit; unsigned int end; unsigned int delta; int volume; int acc; int adpcmd; int adpcml; int prev_acc; unsigned char now_data; unsigned char portstate; unsigned char control2; unsigned char portshift; unsigned char DRAMportshift; unsigned char memread; void (*status_set_handler)(unsigned char which_chip, unsigned char status_bits); void (*status_reset_handler)(unsigned char which_chip, unsigned char status_bits); unsigned char status_change_which_chip; unsigned char status_change_EOS_bit; unsigned char status_change_BRDY_bit; unsigned char status_change_ZERO_bit; unsigned char PCM_BSY; unsigned char reg[16]; }


original_code:
void YM_DELTAT_ADPCM_Reset(YM_DELTAT *DELTAT,int pan)
{
 DELTAT->now_addr = 0;
 DELTAT->now_step = 0;
 DELTAT->step = 0;
 DELTAT->start = 0;
 DELTAT->end = 0;
 DELTAT->limit = ~0;
 DELTAT->volume = 0;
 DELTAT->pan = &DELTAT->output_pointer[pan];
 DELTAT->acc = 0;
 DELTAT->prev_acc = 0;
 DELTAT->adpcmd = 127;
 DELTAT->adpcml = 0;
 DELTAT->portstate = 0;
 DELTAT->DRAMportshift = 3;
 DELTAT->control2 = 0;
}

canonical_original_code:
void YM_DELTAT_ADPCM_Reset(struct deltat_adpcm_state *DELTAT, int pan)
{
 DELTAT->now_addr = 0;
 DELTAT->now_step = 0;
 DELTAT->step = 0;
 DELTAT->start = 0;
 DELTAT->end = 0;
 DELTAT->limit = ~0;
 DELTAT->volume = 0;
 DELTAT->pan = &DELTAT->output_pointer[pan];
 DELTAT->acc = 0;
 DELTAT->prev_acc = 0;
 DELTAT->adpcmd = 127;
 DELTAT->adpcml = 0;
 DELTAT->portstate = 0;
 DELTAT->DRAMportshift = 3;
 DELTAT->control2 = 0;
}

variable_types: pan int


uclock

decompiled_code:
__suseconds_t uclock()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
if ( !init_sec_0 )
init_sec_0 = v1.tv_sec;
return v1.tv_usec + 1000000 * (v1.tv_sec - init_sec_0);
}

canonical_decompiled_code:
__suseconds_t func0()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
if ( !init_sec_0 )
init_sec_0 = v1.tv_sec;
return v1.tv_usec + 1000000 * (v1.tv_sec - init_sec_0);
}

original_code:
uclock_t uclock(void)
{
  static uclock_t init_sec = 0;
  struct timeval tv;
  gettimeofday(&tv, 0);
  if (init_sec == 0) init_sec = tv.tv_sec;
  return (tv.tv_sec - init_sec) * 1000000 + tv.tv_usec;
}

canonical_original_code:
long uclock()
{
  static long init_sec = 0;
  struct timeval tv;
  gettimeofday(&tv, 0);
  if (init_sec == 0) init_sec = tv.tv_sec;
  return (tv.tv_sec - init_sec) * 1000000 + tv.tv_usec;
}

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

return_type: long


decompiled_code:
int __fastcall print_columns(char *a1, char *a2)
{
return fprint_columns(stdout, a1, a2);
}

canonical_decompiled_code:
int __fastcall func2(char *a1, char *a2)
{
return func1(stdout, a1, a2);
}

original_code:
void print_columns(const char *text1, const char *text2)
{
   fprint_columns(stdout, text1, text2);
}

canonical_original_code:
void print_columns(char *text1, char *text2)
{
   fprint_columns(stdout, text1, text2);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
const char *v4; // r15
bool v5; // cf
bool v6; // zf
const char **v8; // rax
__int64 v9; // rdx
const char **v10; // r10
int v11; // er9
const char *v12; // rdi
__int64 v13; // rcx
const char *v14; // rsi
FILE *v15; // r14
FILE *v16; // r12
int v17; // ebx
int v18; // ebp
char ptr; // [rsp+8h] [rbp-40h] BYREF
char v21; // [rsp+9h] [rbp-3Fh]
char v22; // [rsp+Ah] [rbp-3Eh]
char v23; // [rsp+Bh] [rbp-3Dh]
char v24; // [rsp+Ch] [rbp-3Ch]
char v25; // [rsp+Dh] [rbp-3Bh]
char v26; // [rsp+Eh] [rbp-3Ah]
char v27; // [rsp+Fh] [rbp-39h]

v5 = argc == 0;
v6 = argc == 1;
if ( argc == 1 )
help(argc, argv, envp);
if ( argc <= 1 )
goto LABEL_27;
v8 = argv + 1;
v9 = (unsigned int)(argc - 2);
v10 = &argv[v9 + 2];
v11 = 0;
do
{
v12 = "-help";
v13 = 6LL;
v14 = *v8;
do
{
if ( !v13 )
break;
v5 = *v14 < (unsigned int)*v12;
v6 = *v14++ == *v12++;
--v13;
}
while ( v6 );
LOBYTE(v9) = (!v5 && !v6) - v5;
if ( !(_BYTE)v9 )
help(v12, v14, v9);
if ( **v8 == 45 )
{
fwrite("Unknown option.\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
if ( v11 )
{
if ( v11 != 1 )
{
fwrite("Bad argument ( Too many argument ).\n", 1uLL, 0x24uLL, stderr);
exit(1);
}
v3 = *v8;
v11 = 2;
}
else
{
v4 = *v8;
v11 = 1;
}
v5 = v10 < ++v8;
v6 = v10 == v8;
}
while ( v10 != v8 );
if ( v11 != 2 )
{
LABEL_27:
fwrite("Bad argument ( Too short argument ).\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
v15 = fopen(v4, "rb");
if ( !v15 )
{
fprintf(stderr, "\n%s: Can't open file %s\n", *argv, v4);
exit(1);
}
v16 = fopen(v3, "wb");
if ( !v16 )
{
fprintf(stderr, "\n%s: Can't open file %s\n", *argv, v3);
exit(1);
}
v17 = fseek(v15, 4096LL, 0);
if ( v17 )
error(0, v4);
v18 = 256;
do
{
if ( fread(&ptr, 1uLL, 8uLL, v15) != 8 )
error(1, v4);
if ( fwrite(&ptr, 1uLL, 8uLL, v16) != 8 )
error(2, v3);
--v18;
}
while ( v18 );
do
{
if ( (v17 & 1) != 0 )
{
v21 = -16;
ptr = -16;
}
else
{
v21 = 0;
ptr = 0;
}
if ( (v17 & 2) != 0 )
{
v23 = -16;
v22 = -16;
}
else
{
v23 = 0;
v22 = 0;
}
if ( (v17 & 4) != 0 )
{
v25 = -16;
v24 = -16;
}
else
{
v25 = 0;
v24 = 0;
}
if ( (v17 & 8) != 0 )
{
v27 = -16;
v26 = -16;
}
else
{
v27 = 0;
v26 = 0;
}
if ( (v17 & 0x10) != 0 )
{
v21 |= 0xFu;
ptr = v21;
}
if ( (v17 & 0x20) != 0 )
{
v23 |= 0xFu;
v22 = v23;
}
if ( (v17 & 0x40) != 0 )
{
v25 |= 0xFu;
v24 = v25;
}
if ( (v17 & 0x80u) != 0 )
{
v27 |= 0xFu;
v26 = v27;
}
if ( fwrite(&ptr, 1uLL, 8uLL, v16) != 8 )
error(2, v3);
++v17;
}
while ( v17 != 256 );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
const char *v4; // r15
bool v5; // cf
bool v6; // zf
const char **v8; // rax
__int64 v9; // rdx
const char **v10; // r10
int v11; // er9
const char *v12; // rdi
__int64 v13; // rcx
const char *v14; // rsi
FILE *v15; // r14
FILE *v16; // r12
int v17; // ebx
int v18; // ebp
char ptr; // [rsp+8h] [rbp-40h] BYREF
char v21; // [rsp+9h] [rbp-3Fh]
char v22; // [rsp+Ah] [rbp-3Eh]
char v23; // [rsp+Bh] [rbp-3Dh]
char v24; // [rsp+Ch] [rbp-3Ch]
char v25; // [rsp+Dh] [rbp-3Bh]
char v26; // [rsp+Eh] [rbp-3Ah]
char v27; // [rsp+Fh] [rbp-39h]

v5 = argc == 0;
v6 = argc == 1;
if ( argc == 1 )
func7(argc, argv, envp);
if ( argc <= 1 )
goto LABEL_27;
v8 = argv + 1;
v9 = (unsigned int)(argc - 2);
v10 = &argv[v9 + 2];
v11 = 0;
do
{
v12 = "-help";
v13 = 6LL;
v14 = *v8;
do
{
if ( !v13 )
break;
v5 = *v14 < (unsigned int)*v12;
v6 = *v14++ == *v12++;
--v13;
}
while ( v6 );
LOBYTE(v9) = (!v5 && !v6) - v5;
if ( !(_BYTE)v9 )
func7(v12, v14, v9);
if ( **v8 == 45 )
{
fwrite("Unknown option.\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
if ( v11 )
{
if ( v11 != 1 )
{
fwrite("Bad argument ( Too many argument ).\n", 1uLL, 0x24uLL, stderr);
exit(1);
}
v3 = *v8;
v11 = 2;
}
else
{
v4 = *v8;
v11 = 1;
}
v5 = v10 < ++v8;
v6 = v10 == v8;
}
while ( v10 != v8 );
if ( v11 != 2 )
{
LABEL_27:
fwrite("Bad argument ( Too short argument ).\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
v15 = fopen(v4, "rb");
if ( !v15 )
{
fprintf(stderr, "\n%s: Can't open file %s\n", *argv, v4);
exit(1);
}
v16 = fopen(v3, "wb");
if ( !v16 )
{
fprintf(stderr, "\n%s: Can't open file %s\n", *argv, v3);
exit(1);
}
v17 = fseek(v15, 4096LL, 0);
if ( v17 )
func8(0, v4);
v18 = 256;
do
{
if ( fread(&ptr, 1uLL, 8uLL, v15) != 8 )
func8(1, v4);
if ( fwrite(&ptr, 1uLL, 8uLL, v16) != 8 )
func8(2, v3);
--v18;
}
while ( v18 );
do
{
if ( (v17 & 1) != 0 )
{
v21 = -16;
ptr = -16;
}
else
{
v21 = 0;
ptr = 0;
}
if ( (v17 & 2) != 0 )
{
v23 = -16;
v22 = -16;
}
else
{
v23 = 0;
v22 = 0;
}
if ( (v17 & 4) != 0 )
{
v25 = -16;
v24 = -16;
}
else
{
v25 = 0;
v24 = 0;
}
if ( (v17 & 8) != 0 )
{
v27 = -16;
v26 = -16;
}
else
{
v27 = 0;
v26 = 0;
}
if ( (v17 & 0x10) != 0 )
{
v21 |= 0xFu;
ptr = v21;
}
if ( (v17 & 0x20) != 0 )
{
v23 |= 0xFu;
v22 = v23;
}
if ( (v17 & 0x40) != 0 )
{
v25 |= 0xFu;
v24 = v25;
}
if ( (v17 & 0x80u) != 0 )
{
v27 |= 0xFu;
v26 = v27;
}
if ( fwrite(&ptr, 1uLL, 8uLL, v16) != 8 )
func8(2, v3);
++v17;
}
while ( v17 != 256 );
return 0;
}

original_code:
int main( int argc, char *argv[] )
{
  int x = 1;
  x11_system_init();
  if( setuid( getuid() ) != 0 ){
    fprintf( stderr, "%s : setuid error\n", argv[0] );
    x11_system_term();
    return -1;
  }
  if( getuid() == 0 ) snapshot_cmd_enable = (0);
  if( *(char *)&x != 1 ){
    fprintf( stderr,
      "%s CAN'T EXCUTE !\n"
      "This machine is Big-Endian.\n"
      "Compile again comment-out 'LSB_FIRST = 1' in Makefile.\n",
      argv[0] );
    x11_system_term();
    return -1;
  }
  xmame_system_init();
  quasi88_atexit( the_end );
  if( osd_environment() ){
    if( config_init( argc, argv ) ){
      quasi88();
    }
  }
  xmame_system_term();
  x11_system_term();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int x = 1;
  x11_system_init();
  if( setuid( getuid() ) != 0 ){
    fprintf( stderr, "%s : setuid error\n", argv[0] );
    x11_system_term();
    return -1;
  }
  if( getuid() == 0 ) snapshot_cmd_enable = (0);
  if( *(char *)&x != 1 ){
    fprintf( stderr,
      "%s CAN'T EXCUTE !\n"
      "This machine is Big-Endian.\n"
      "Compile again comment-out 'LSB_FIRST = 1' in Makefile.\n",
      argv[0] );
    x11_system_term();
    return -1;
  }
  xmame_system_init();
  quasi88_atexit( the_end );
  if( osd_environment() ){
    if( config_init( argc, argv ) ){
      quasi88();
    }
  }
  xmame_system_term();
  x11_system_term();
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: x int


repo: tphaster/smime-gate

decompiled_code:
void __fastcall __noreturn thread_r(void *a1)
{
unsigned int v1; // ebx
__int64 buf[3]; // [rsp+0h] [rbp-18h] BYREF

while ( 1 )
{
v1 = Socket(2LL, 1LL, 0LL);
buf[0] = 2LL;
buf[1] = 0LL;
WORD1(buf[0]) = _bswap_16(5555);
inet_pton(2, addr, (char *)buf + 4);
Connect(v1, buf, 16LL);
str_cli(v1, mails_per_session);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
unsigned int v1; // ebx
__int64 buf[3]; // [rsp+0h] [rbp-18h] BYREF

while ( 1 )
{
v1 = Socket(2LL, 1LL, 0LL);
buf[0] = 2LL;
buf[1] = 0LL;
WORD1(buf[0]) = func0(5555);
inet_pton(2, addr, (char *)buf + 4);
Connect(v1, buf, 16LL);
func1(v1, mails_per_session);
}
}

original_code:
void* thread_r (void* arg __attribute__((__unused__)))
{
    int sockfd;
    struct sockaddr_in servaddr;
    for (;;) {
        sockfd = Socket(2, SOCK_STREAM, 0);
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = 2;
        servaddr.sin_port = htons(5555);
        inet_pton(2, addr, &servaddr.sin_addr);
        Connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
        str_cli(sockfd, mails_per_session);
    }
    exit(0);
}

canonical_original_code:
void *thread_r(void *arg)
{
    int sockfd;
    struct sockaddr_in servaddr;
    for (;;) {
        sockfd = Socket(2, SOCK_STREAM, 0);
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = 2;
        servaddr.sin_port = htons(5555);
        inet_pton(2, addr, &servaddr.sin_addr);
        Connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
        str_cli(sockfd, mails_per_session);
    }
    exit(0);
}

variable_types:
arg
void *

variable_types: sockfd int

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

return_type: void *

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

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


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // ebp
__int64 v5; // rdx
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

if ( argc != 4 )
err_quit("usage: client <IPaddress> <mails per session> <threads>", argv, envp);
addr = (char *)argv[1];
mails_per_session = atoi(argv[2]);
v3 = atoi(argv[3]);
interval = time(0LL) + 60;
if ( v3 > 0 )
{
v4 = 0;
do
{
v4 += pthread_create(newthread, 0LL, thread_r, 0LL);
--v3;
}
while ( v3 );
if ( v4 )
err_quit("ERROR: Some threads did not start, exiting...\n", 0LL, v5);
}
puts("mails,kbytes,errors,sessions");
while ( 1 )
{
do
usleep(0xC8u);
while ( time(0LL) <= interval );
interval += 60LL;
pthread_mutex_lock(&mutex);
printf("%d,%d,%d,%d\n", (unsigned int)mails, (unsigned int)kbytes, (unsigned int)errors, (unsigned int)sessions);
mails = 0;
kbytes = 0;
errors = 0;
sessions = 0;
pthread_mutex_unlock(&mutex);
}
}

original_code:
int main (int argc, char **argv)
{
    pthread_t th_id;
    int ret = 0, threads;
    if (argc != 4)
        err_quit("usage: client <IPaddress> <mails per session> <threads>");
    addr = argv[1];
    mails_per_session = atoi(argv[2]);
    threads = atoi(argv[3]);
    interval = time(((void *)0)) + 60;
    for (;threads > 0; --threads) {
        ret += pthread_create(&th_id, ((void *)0), thread_r, ((void *)0));
    }
    if (ret != 0)
        err_quit("ERROR: Some threads did not start, exiting...\n");
    printf("mails,kbytes,errors,sessions\n");
    for (;;) {
        usleep(200);
        if (time(((void *)0)) > interval) {
            interval += 60;
            pthread_mutex_lock(&mutex);
            printf("%d,%d,%d,%d\n", mails, kbytes, errors, sessions);
            mails = 0;
            kbytes = 0;
            errors = 0;
            sessions = 0;
            pthread_mutex_unlock(&mutex);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    unsigned long th_id;
    int ret = 0; int threads;
    if (argc != 4)
        err_quit("usage: client <IPaddress> <mails per session> <threads>");
    addr = argv[1];
    mails_per_session = atoi(argv[2]);
    threads = atoi(argv[3]);
    interval = time(((void *)0)) + 60;
    for (;threads > 0; --threads) {
        ret += pthread_create(&th_id, ((void *)0), thread_r, ((void *)0));
    }
    if (ret != 0)
        err_quit("ERROR: Some threads did not start, exiting...\n");
    printf("mails,kbytes,errors,sessions\n");
    for (;;) {
        usleep(200);
        if (time(((void *)0)) > interval) {
            interval += 60;
            pthread_mutex_lock(&mutex);
            printf("%d,%d,%d,%d\n", mails, kbytes, errors, sessions);
            mails = 0;
            kbytes = 0;
            errors = 0;
            sessions = 0;
            pthread_mutex_unlock(&mutex);
        }
    }
    return 0;
}

variable_types:
argv
char **

variable_types: ret int

variable_types: threads int


decompiled_code:
int __fastcall str_cli(unsigned int a1, int a2)
{
int v2; // er13
int result; // eax
int v4; // er12
unsigned int v5; // ebp
int v6; // ebx
_BYTE v7[88]; // [rsp+0h] [rbp-58h] BYREF

v2 = a2;
if ( !a2 )
{
v4 = 5;
v5 = 1;
LABEL_13:
pthread_mutex_lock(&mutex);
++sessions;
pthread_mutex_unlock(&mutex);
goto LABEL_14;
}
if ( a2 == 1 )
{
v4 = 1;
v5 = 3;
goto LABEL_13;
}
pthread_mutex_lock(&mutex);
++sessions;
result = pthread_mutex_unlock(&mutex);
if ( a2 <= 0 )
return result;
v4 = a2;
v2 = 1;
v5 = 1;
LABEL_14:
v6 = 0;
do
{
if ( v6 + 1 == v4 && v4 != 1 )
v5 = 2;
pthread_mutex_lock(&mutex);
load_mail_from_file("./mail-test", v7);
if ( (unsigned int)smtp_send_mail(a1, v7, v5) )
{
++errors;
}
else
{
++mails;
kbytes += 96;
}
free_mail_object(v7);
result = pthread_mutex_unlock(&mutex);
v6 += v2;
v5 = 0;
}
while ( v6 < v4 );
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1, int a2)
{
int v2; // er13
int result; // eax
int v4; // er12
unsigned int v5; // ebp
int v6; // ebx
_BYTE v7[88]; // [rsp+0h] [rbp-58h] BYREF

v2 = a2;
if ( !a2 )
{
v4 = 5;
v5 = 1;
LABEL_13:
pthread_mutex_lock(&mutex);
++sessions;
pthread_mutex_unlock(&mutex);
goto LABEL_14;
}
if ( a2 == 1 )
{
v4 = 1;
v5 = 3;
goto LABEL_13;
}
pthread_mutex_lock(&mutex);
++sessions;
result = pthread_mutex_unlock(&mutex);
if ( a2 <= 0 )
return result;
v4 = a2;
v2 = 1;
v5 = 1;
LABEL_14:
v6 = 0;
do
{
if ( v6 + 1 == v4 && v4 != 1 )
v5 = 2;
pthread_mutex_lock(&mutex);
load_mail_from_file("./mail-test", v7);
if ( (unsigned int)smtp_send_mail(a1, v7, v5) )
{
++errors;
}
else
{
++mails;
kbytes += 96;
}
free_mail_object(v7);
result = pthread_mutex_unlock(&mutex);
v6 += v2;
v5 = 0;
}
while ( v6 < v4 );
return result;
}

original_code:
void str_cli (int sockfd, int mps)
{
    int ret, i, cli, inc = 1;
    struct mail_object mail;
    if (mps == 0) {
        inc = 0;
        mps = 5;
    }
    if (mps == 1)
        cli = 0x1 | 0x2;
    else
        cli = 0x1 | 0x0;
    pthread_mutex_lock(&mutex);
    ++sessions;
    pthread_mutex_unlock(&mutex);
    for (i = 0; i < mps; i += inc) {
        if (i+1 == mps && mps != 1)
            cli = 0x0 | 0x2;
        pthread_mutex_lock(&mutex);
        load_mail_from_file("./mail-test", &mail);
        if (0 == (ret = smtp_send_mail(sockfd, &mail, cli))) {
            ++mails;
            kbytes += 96;
        }
        else {
            ++errors;
        }
        free_mail_object(&mail);
        pthread_mutex_unlock(&mutex);
        cli = 0x0 | 0x0;
    }
}

canonical_original_code:
void str_cli(int sockfd, int mps)
{
    int ret; int i; int cli; int inc = 1;
    struct mail_object mail;
    if (mps == 0) {
        inc = 0;
        mps = 5;
    }
    if (mps == 1)
        cli = 0x1 | 0x2;
    else
        cli = 0x1 | 0x0;
    pthread_mutex_lock(&mutex);
    ++sessions;
    pthread_mutex_unlock(&mutex);
    for (i = 0; i < mps; i += inc) {
        if (i+1 == mps && mps != 1)
            cli = 0x0 | 0x2;
        pthread_mutex_lock(&mutex);
        load_mail_from_file("./mail-test", &mail);
        if (0 == (ret = smtp_send_mail(sockfd, &mail, cli))) {
            ++mails;
            kbytes += 96;
        }
        else {
            ++errors;
        }
        free_mail_object(&mail);
        pthread_mutex_unlock(&mutex);
        cli = 0x0 | 0x0;
    }
}

variable_types: mps int

variable_types: i int

variable_types: cli int

variable_types: inc int

variable_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; }


decompiled_code:
char *__fastcall generate_filename(unsigned int a1)
{
char *v1; // rbx
unsigned int v2; // er12
unsigned int v3; // eax

v1 = (char *)malloc(0x30uLL);
if ( v1 )
{
v2 = time(0LL);
v3 = getpid();
snprintf(v1, 0x30uLL, "/var/run/smime-gate/mail%d_%d-%d", v2, v3, a1);
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func0(unsigned int a1)
{
char *v1; // rbx
unsigned int v2; // er12
unsigned int v3; // eax

v1 = (char *)malloc(0x30uLL);
if ( v1 )
{
v2 = time(0LL);
v3 = getpid();
snprintf(v1, 0x30uLL, "/var/run/smime-gate/mail%d_%d-%d", v2, v3, a1);
}
return v1;
}

original_code:
static char *generate_filename (unsigned int nr)
{
    char *fn = malloc(48);
    unsigned int t, p;
    if (((void *)0) != fn) {
        t = time(((void *)0));
        p = getpid();
        snprintf(fn, 48, "/var/run/smime-gate" "/mail%d_%d-%d", t, p, nr);
    }
    return fn;
}

canonical_original_code:
static char *generate_filename(unsigned int nr)
{
    char *fn = malloc(48);
    unsigned int t; unsigned int p;
    if (((void *)0) != fn) {
        t = time(((void *)0));
        p = getpid();
        snprintf(fn, 48, "/var/run/smime-gate" "/mail%d_%d-%d", t, p, nr);
    }
    return fn;
}

variable_types:
nr
unsigned int

variable_types:
fn
char *

variable_types:
t
unsigned int

variable_types:
p
unsigned int

return_type: char *


decompiled_code:
__int64 __fastcall smime_process_mails(const char ***a1, const char **a2, int a3)
{
const char ***v3; // r13
unsigned int v5; // er12
__int64 v6; // rbp
__int64 v7; // rbp
const char *v8; // rsi
int v9; // er15
unsigned int v10; // er12
__int64 v11; // rbp
__int64 v12; // rbp
const char *v13; // rsi
int v14; // ebp
unsigned int v15; // er12
__int64 v16; // rbp
__int64 v17; // rbp
const char *v18; // rsi
int v19; // ebp
unsigned int v20; // ebp
__int64 v21; // rax
__int64 v22; // r12
const char *v23; // rsi
int v24; // ebp
__int64 v26; // [rsp+0h] [rbp-248h]
char s[568]; // [rsp+10h] [rbp-238h] BYREF

if ( a3 <= 0 )
return 0LL;
v3 = a1;
v26 = (__int64)&a1[(unsigned int)(a3 - 1) + 1];
do
{
v5 = 0;
v6 = 0LL;
if ( *((_QWORD *)&conf + 8) )
{
while ( 1 )
{
v7 = 32 * v6;
v8 = *(const char **)(*((_QWORD *)&conf + 7) + v7);
if ( v8 )
{
if ( strcasestr(**v3, v8) )
break;
}
v6 = ++v5;
if ( (unsigned __int64)v5 >= *((_QWORD *)&conf + 8) )
goto LABEL_20;
}
snprintf(
s,
0x200uLL,
"smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 7) + v7 + 8),
*(const char **)(*((_QWORD *)&conf + 7) + v7 + 16),
*(const char **)(*((_QWORD *)&conf + 7) + v7 + 24),
*a2,
*a2);
v9 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( v9 )
{
v9 = 0;
}
else if ( !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
v9 = 1;
}
remove(s);
}
else
{
LABEL_20:
v9 = 0;
}
if ( *((_QWORD *)&conf + 6) )
{
v10 = 0;
v11 = 0LL;
while ( 1 )
{
v12 = 16 * v11;
v13 = *(const char **)(*((_QWORD *)&conf + 5) + v12);
if ( v13 )
{
if ( (*v3)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*v3)[1], v13) )
break;
}
v11 = ++v10;
if ( (unsigned __int64)v10 >= *((_QWORD *)&conf + 6) )
goto LABEL_17;
}
snprintf(
s,
0x200uLL,
"smime-tool -encrypt -cert %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 5) + v12 + 8),
*a2,
*a2);
v14 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( !v14 && !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
v9 = 1;
}
remove(s);
}
LABEL_17:
if ( !v9 )
{
if ( *((_QWORD *)&conf + 10) )
{
v15 = 0;
v16 = 0LL;
while ( 1 )
{
v17 = 32 * v16;
v18 = *(const char **)(*((_QWORD *)&conf + 9) + v17);
if ( v18 )
{
if ( (*v3)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*v3)[1], v18) )
break;
}
v16 = ++v15;
if ( (unsigned __int64)v15 >= *((_QWORD *)&conf + 10) )
goto LABEL_33;
}
snprintf(
s,
0x200uLL,
"smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 9) + v17 + 8),
*(const char **)(*((_QWORD *)&conf + 9) + v17 + 16),
*(const char **)(*((_QWORD *)&conf + 9) + v17 + 24),
*a2,
*a2);
v19 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( !v19 && !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
}
remove(s);
}
LABEL_33:
if ( *((_QWORD *)&conf + 12) )
{
v20 = 0;
v21 = 0LL;
while ( 1 )
{
v22 = 24 * v21;
v23 = *(const char **)(*((_QWORD *)&conf + 11) + 24 * v21);
if ( v23 )
{
if ( strcasestr(**v3, v23) )
break;
}
v21 = ++v20;
if ( (unsigned __int64)v20 >= *((_QWORD *)&conf + 12) )
goto LABEL_18;
}
snprintf(
s,
0x200uLL,
"smime-tool -verify -cert %s -ca %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 11) + v22 + 8),
*(const char **)(*((_QWORD *)&conf + 11) + v22 + 16),
*a2,
*a2);
v24 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( !v24 && !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
}
remove(s);
}
}
LABEL_18:
++v3;
++a2;
}
while ( (const char ***)v26 != v3 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char ***a1, const char **a2, int a3)
{
const char ***v3; // r13
unsigned int v5; // er12
__int64 v6; // rbp
__int64 v7; // rbp
const char *v8; // rsi
int v9; // er15
unsigned int v10; // er12
__int64 v11; // rbp
__int64 v12; // rbp
const char *v13; // rsi
int v14; // ebp
unsigned int v15; // er12
__int64 v16; // rbp
__int64 v17; // rbp
const char *v18; // rsi
int v19; // ebp
unsigned int v20; // ebp
__int64 v21; // rax
__int64 v22; // r12
const char *v23; // rsi
int v24; // ebp
__int64 v26; // [rsp+0h] [rbp-248h]
char s[568]; // [rsp+10h] [rbp-238h] BYREF

if ( a3 <= 0 )
return 0LL;
v3 = a1;
v26 = (__int64)&a1[(unsigned int)(a3 - 1) + 1];
do
{
v5 = 0;
v6 = 0LL;
if ( *((_QWORD *)&conf + 8) )
{
while ( 1 )
{
v7 = 32 * v6;
v8 = *(const char **)(*((_QWORD *)&conf + 7) + v7);
if ( v8 )
{
if ( strcasestr(**v3, v8) )
break;
}
v6 = ++v5;
if ( (unsigned __int64)v5 >= *((_QWORD *)&conf + 8) )
goto LABEL_20;
}
snprintf(
s,
0x200uLL,
"smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 7) + v7 + 8),
*(const char **)(*((_QWORD *)&conf + 7) + v7 + 16),
*(const char **)(*((_QWORD *)&conf + 7) + v7 + 24),
*a2,
*a2);
v9 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( v9 )
{
v9 = 0;
}
else if ( !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
v9 = 1;
}
remove(s);
}
else
{
LABEL_20:
v9 = 0;
}
if ( *((_QWORD *)&conf + 6) )
{
v10 = 0;
v11 = 0LL;
while ( 1 )
{
v12 = 16 * v11;
v13 = *(const char **)(*((_QWORD *)&conf + 5) + v12);
if ( v13 )
{
if ( (*v3)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*v3)[1], v13) )
break;
}
v11 = ++v10;
if ( (unsigned __int64)v10 >= *((_QWORD *)&conf + 6) )
goto LABEL_17;
}
snprintf(
s,
0x200uLL,
"smime-tool -encrypt -cert %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 5) + v12 + 8),
*a2,
*a2);
v14 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( !v14 && !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
v9 = 1;
}
remove(s);
}
LABEL_17:
if ( !v9 )
{
if ( *((_QWORD *)&conf + 10) )
{
v15 = 0;
v16 = 0LL;
while ( 1 )
{
v17 = 32 * v16;
v18 = *(const char **)(*((_QWORD *)&conf + 9) + v17);
if ( v18 )
{
if ( (*v3)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*v3)[1], v18) )
break;
}
v16 = ++v15;
if ( (unsigned __int64)v15 >= *((_QWORD *)&conf + 10) )
goto LABEL_33;
}
snprintf(
s,
0x200uLL,
"smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 9) + v17 + 8),
*(const char **)(*((_QWORD *)&conf + 9) + v17 + 16),
*(const char **)(*((_QWORD *)&conf + 9) + v17 + 24),
*a2,
*a2);
v19 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( !v19 && !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
}
remove(s);
}
LABEL_33:
if ( *((_QWORD *)&conf + 12) )
{
v20 = 0;
v21 = 0LL;
while ( 1 )
{
v22 = 24 * v21;
v23 = *(const char **)(*((_QWORD *)&conf + 11) + 24 * v21);
if ( v23 )
{
if ( strcasestr(**v3, v23) )
break;
}
v21 = ++v20;
if ( (unsigned __int64)v20 >= *((_QWORD *)&conf + 12) )
goto LABEL_18;
}
snprintf(
s,
0x200uLL,
"smime-tool -verify -cert %s -ca %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 11) + v22 + 8),
*(const char **)(*((_QWORD *)&conf + 11) + v22 + 16),
*a2,
*a2);
v24 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *a2);
if ( !v24 && !rename(s, *a2) )
{
free_mail_object(*v3);
load_mail_from_file(*a2, *v3);
}
remove(s);
}
}
LABEL_18:
++v3;
++a2;
}
while ( (const char ***)v26 != v3 );
return 0LL;
}

original_code:
int smime_process_mails (struct mail_object **mails, char **fns, int no_mails)
{
    int m, toprcs, sign_encr, ret;
    unsigned int r;
    char cmd[512];
    for (m = 0; m < no_mails; ++m) {
        toprcs = 0;
        sign_encr = 0;
        for (r = 0; r < conf.sign_rules_size; ++r) {
            if (((void *)0) != conf.sign_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.sign_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
                conf.sign_rules[r].cert_path, conf.sign_rules[r].key_path,
                conf.sign_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.encr_rules_size; ++r) {
            if (((void *)0) != conf.encr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.encr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                    "smime-tool -encrypt -cert %s %s > %s.prcs",
                    conf.encr_rules[r].cert_path, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        if (sign_encr)
            continue;
        toprcs = 0;
        for (r = 0; r < conf.decr_rules_size; ++r) {
            if (((void *)0) != conf.decr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.decr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
                conf.decr_rules[r].cert_path, conf.decr_rules[r].key_path,
                conf.decr_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.vrfy_rules_size; ++r) {
            if (((void *)0) != conf.vrfy_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.vrfy_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -verify -cert %s -ca %s %s > %s.prcs",
                conf.vrfy_rules[r].cert_path, conf.vrfy_rules[r].cacert_path,
                fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
    }
    return 0;
}

canonical_original_code:
int smime_process_mails(struct mail_object **mails, char **fns, int no_mails)
{
    int m; int toprcs; int sign_encr; int ret;
    unsigned int r;
    char cmd[512];
    for (m = 0; m < no_mails; ++m) {
        toprcs = 0;
        sign_encr = 0;
        for (r = 0; r < conf.sign_rules_size; ++r) {
            if (((void *)0) != conf.sign_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.sign_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
                conf.sign_rules[r].cert_path, conf.sign_rules[r].key_path,
                conf.sign_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.encr_rules_size; ++r) {
            if (((void *)0) != conf.encr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.encr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                    "smime-tool -encrypt -cert %s %s > %s.prcs",
                    conf.encr_rules[r].cert_path, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        if (sign_encr)
            continue;
        toprcs = 0;
        for (r = 0; r < conf.decr_rules_size; ++r) {
            if (((void *)0) != conf.decr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.decr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
                conf.decr_rules[r].cert_path, conf.decr_rules[r].key_path,
                conf.decr_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.vrfy_rules_size; ++r) {
            if (((void *)0) != conf.vrfy_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.vrfy_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -verify -cert %s -ca %s %s > %s.prcs",
                conf.vrfy_rules[r].cert_path, conf.vrfy_rules[r].cacert_path,
                fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
    }
    return 0;
}

variable_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; } **

variable_types:
fns
char **

variable_types: m int

variable_types: toprcs int

variable_types:
r
unsigned int

variable_types:
cmd
char [512]


decompiled_code:
__int64 __fastcall smime_gate_service(unsigned int a1)
{
const char **v1; // r14
const char ***v2; // r13
const char **v3; // r12
char *v4; // rbp
__int64 v5; // rbx
__int64 v6; // rcx
int v7; // er15
unsigned int v9; // er12
__int64 v10; // rdx
__int64 v11; // rbx
char **v12; // rbx
void **v13; // rbp
char *v14; // rax
char *v15; // rax
int v16; // [rsp+14h] [rbp-44h]
char *ptr; // [rsp+18h] [rbp-40h]

v1 = (const char **)Calloc(10LL, 8LL);
ptr = (char *)Malloc(48LL);
v2 = (const char ***)Calloc(10LL, 8LL);
v3 = (const char **)Malloc(40LL);
v4 = generate_filename(0);
if ( !v4 )
err_sys("malloc error");
v5 = 1LL;
v6 = 0LL;
while ( 1 )
{
v16 = v5;
v7 = v5 - 1;
if ( (unsigned int)smtp_recv_mail(a1, v3, v4, v6) )
break;
v2[v5 - 1] = v3;
v1[v5 - 1] = v4;
v4 = generate_filename(v5);
if ( v4 )
{
v3 = (const char **)malloc(0x28uLL);
if ( v3 )
{
v6 = 2 * (unsigned int)((_DWORD)v5 != 10) - 1;
}
else
{
free(v4);
v4 = 0LL;
v6 = 0xFFFFFFFFLL;
}
}
else
{
v3 = 0LL;
v6 = 0xFFFFFFFFLL;
}
++v5;
}
free(v4);
free(v3);
if ( (_DWORD)v5 != 1 )
{
smime_process_mails(v2, v1, v7);
v9 = Socket(2LL, 1LL, 0LL);
if ( connect(v9, (const struct sockaddr *)(unsigned int)((_DWORD)&conf + 104), 0x10u) < 0 )
{
if ( v7 > 0 )
{
v12 = (char **)v1;
v13 = (void **)v2;
do
{
v14 = _xpg_basename(*v12);
snprintf(ptr, 0x30uLL, "/var/run/smime-gate/unsent/%s", v14);
rename(*v12, ptr);
free_mail_object(*v13);
free(*v13);
free(*v12++);
++v13;
}
while ( v12 != (char **)&v1[(unsigned int)(v16 - 2) + 1] );
}
err_sys("connect error");
}
if ( v7 == 1 )
{
v10 = 3LL;
}
else
{
if ( v7 <= 0 )
goto LABEL_12;
v10 = 1LL;
}
v11 = 0LL;
do
{
if ( (unsigned int)smtp_send_mail(v9, v2[v11], v10) )
{
v15 = _xpg_basename((char *)v1[v11]);
snprintf(ptr, 0x30uLL, "/var/run/smime-gate/unsent/%s", v15);
rename(v1[v11], ptr);
}
else
{
remove(v1[v11]);
}
free_mail_object(v2[v11]);
free(v2[v11]);
free((void *)v1[v11]);
v10 = 2 * (unsigned int)(v16 - 3 == (_DWORD)v11++);
}
while ( v7 > (int)v11 );
}
LABEL_12:
free(ptr);
free(v2);
free(v1);
return free_config();
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
const char **v1; // r14
const char ***v2; // r13
const char **v3; // r12
char *v4; // rbp
__int64 v5; // rbx
__int64 v6; // rcx
int v7; // er15
unsigned int v9; // er12
__int64 v10; // rdx
__int64 v11; // rbx
char **v12; // rbx
void **v13; // rbp
char *v14; // rax
char *v15; // rax
int v16; // [rsp+14h] [rbp-44h]
char *ptr; // [rsp+18h] [rbp-40h]

v1 = (const char **)Calloc(10LL, 8LL);
ptr = (char *)Malloc(48LL);
v2 = (const char ***)Calloc(10LL, 8LL);
v3 = (const char **)Malloc(40LL);
v4 = func0(0);
if ( !v4 )
err_sys("malloc error");
v5 = 1LL;
v6 = 0LL;
while ( 1 )
{
v16 = v5;
v7 = v5 - 1;
if ( (unsigned int)smtp_recv_mail(a1, v3, v4, v6) )
break;
v2[v5 - 1] = v3;
v1[v5 - 1] = v4;
v4 = func0(v5);
if ( v4 )
{
v3 = (const char **)malloc(0x28uLL);
if ( v3 )
{
v6 = 2 * (unsigned int)((_DWORD)v5 != 10) - 1;
}
else
{
free(v4);
v4 = 0LL;
v6 = 0xFFFFFFFFLL;
}
}
else
{
v3 = 0LL;
v6 = 0xFFFFFFFFLL;
}
++v5;
}
free(v4);
free(v3);
if ( (_DWORD)v5 != 1 )
{
func1(v2, v1, v7);
v9 = Socket(2LL, 1LL, 0LL);
if ( connect(v9, (const struct sockaddr *)(unsigned int)((_DWORD)&conf + 104), 0x10u) < 0 )
{
if ( v7 > 0 )
{
v12 = (char **)v1;
v13 = (void **)v2;
do
{
v14 = _xpg_basename(*v12);
snprintf(ptr, 0x30uLL, "/var/run/smime-gate/unsent/%s", v14);
rename(*v12, ptr);
free_mail_object(*v13);
free(*v13);
free(*v12++);
++v13;
}
while ( v12 != (char **)&v1[(unsigned int)(v16 - 2) + 1] );
}
err_sys("connect error");
}
if ( v7 == 1 )
{
v10 = 3LL;
}
else
{
if ( v7 <= 0 )
goto LABEL_12;
v10 = 1LL;
}
v11 = 0LL;
do
{
if ( (unsigned int)smtp_send_mail(v9, v2[v11], v10) )
{
v15 = _xpg_basename((char *)v1[v11]);
snprintf(ptr, 0x30uLL, "/var/run/smime-gate/unsent/%s", v15);
rename(v1[v11], ptr);
}
else
{
remove(v1[v11]);
}
free_mail_object(v2[v11]);
free(v2[v11]);
free((void *)v1[v11]);
v10 = 2 * (unsigned int)(v16 - 3 == (_DWORD)v11++);
}
while ( v7 > (int)v11 );
}
LABEL_12:
free(ptr);
free(v2);
free(v1);
return free_config();
}

original_code:
void smime_gate_service (int sockfd)
{
    int i, srvfd;
    int srv = 0;
    int no_mails = 0;
    char **fns = Calloc(10, sizeof(char *));
    char *filename;
    char *unsent = Malloc(48);
    struct mail_object **mails = Calloc(10, sizeof(struct mail_object *));
    struct mail_object *mail = Malloc(sizeof(struct mail_object));
    if (((void *)0) == (filename = generate_filename(no_mails)))
        err_sys("malloc error");
    while (0 == smtp_recv_mail(sockfd, mail, filename, srv)) {
        mails[no_mails] = mail;
        fns[no_mails] = filename;
        ++no_mails;
        if (((void *)0) == (filename = generate_filename(no_mails))) {
            srv = -1;
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (((void *)0) == (mail = malloc(sizeof(struct mail_object)))) {
            srv = -1;
            free(filename);
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (10 == no_mails)
            srv = -1;
        else
            srv = 1;
    }
    free(filename);
    free(mail);
    filename = ((void *)0);
    mail = ((void *)0);
    if (0 == no_mails)
        goto end_service;
    smime_process_mails(mails, fns, no_mails);
    srvfd = Socket(2, SOCK_STREAM, 0);
    if (connect(srvfd, (struct sockaddr *) &(conf.mail_srv), sizeof(conf.mail_srv)) < 0) {
        for (i = 0; i < no_mails; ++i) {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
            free_mail_object(mails[i]);
            free(mails[i]);
            free(fns[i]);
        }
        err_sys("connect error");
    }
    if (1 == no_mails)
        srv = 0x1 | 0x2;
    else
        srv = 0x1 | 0x0;
    for (i = 0; i < no_mails; ++i) {
        if (0 == smtp_send_mail(srvfd, mails[i], srv)) {
            remove(fns[i]);
        }
        else {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
        }
        free_mail_object(mails[i]);
        free(mails[i]);
        free(fns[i]);
        if (no_mails-2 == i)
            srv = 0x0 | 0x2;
        else
            srv = 0x0 | 0x0;
    }
end_service:
    free(unsent);
    free(mails);
    free(fns);
    free_config();
}

canonical_original_code:
void smime_gate_service(int sockfd)
{
    int i; int srvfd;
    int srv = 0;
    int no_mails = 0;
    char **fns = Calloc(10, sizeof(char *));
    char *filename;
    char *unsent = Malloc(48);
    struct mail_object **mails = Calloc(10, sizeof(struct mail_object *));
    struct mail_object *mail = Malloc(sizeof(struct mail_object));
    if (((void *)0) == (filename = generate_filename(no_mails)))
        err_sys("malloc error");
    while (0 == smtp_recv_mail(sockfd, mail, filename, srv)) {
        mails[no_mails] = mail;
        fns[no_mails] = filename;
        ++no_mails;
        if (((void *)0) == (filename = generate_filename(no_mails))) {
            srv = -1;
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (((void *)0) == (mail = malloc(sizeof(struct mail_object)))) {
            srv = -1;
            free(filename);
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (10 == no_mails)
            srv = -1;
        else
            srv = 1;
    }
    free(filename);
    free(mail);
    filename = ((void *)0);
    mail = ((void *)0);
    if (0 == no_mails)
        goto end_service;
    smime_process_mails(mails, fns, no_mails);
    srvfd = Socket(2, SOCK_STREAM, 0);
    if (connect(srvfd, (struct sockaddr *) &(conf.mail_srv), sizeof(conf.mail_srv)) < 0) {
        for (i = 0; i < no_mails; ++i) {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
            free_mail_object(mails[i]);
            free(mails[i]);
            free(fns[i]);
        }
        err_sys("connect error");
    }
    if (1 == no_mails)
        srv = 0x1 | 0x2;
    else
        srv = 0x1 | 0x0;
    for (i = 0; i < no_mails; ++i) {
        if (0 == smtp_send_mail(srvfd, mails[i], srv)) {
            remove(fns[i]);
        }
        else {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
        }
        free_mail_object(mails[i]);
        free(mails[i]);
        free(fns[i]);
        if (no_mails-2 == i)
            srv = 0x0 | 0x2;
        else
            srv = 0x0 | 0x0;
    }
end_service:
    free(unsent);
    free(mails);
    free(fns);
    free_config();
}

variable_types: sockfd int

variable_types: srvfd int

variable_types: srv int

variable_types:
filename
char *

variable_types:
unsent
char *

variable_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; } *


decompiled_code:
void __noreturn unsent_service()
{
prctl(1, 15LL);
while ( 1 )
{
if ( (unsigned int)send_mails_from_dir("/var/run/smime-gate/unsent", (unsigned int)((_DWORD)&conf + 104)) == -1 )
err_sys("failed to open unsent directory");
sleep(0x384u);
}
}

canonical_decompiled_code:
void __noreturn func3()
{
prctl(1, 15LL);
while ( 1 )
{
if ( (unsigned int)send_mails_from_dir("/var/run/smime-gate/unsent", (unsigned int)((_DWORD)&conf + 104)) == -1 )
err_sys("failed to open unsent directory");
sleep(0x384u);
}
}

original_code:
void unsent_service (void)
{
    prctl(1, 15);
    for (;;) {
        if (-1 == send_mails_from_dir("/var/run/smime-gate/unsent", &(conf.mail_srv)) )
            err_sys("failed to open unsent directory");
        sleep(900);
    }
}

canonical_original_code:
void unsent_service()
{
    prctl(1, 15);
    for (;;) {
        if (-1 == send_mails_from_dir("/var/run/smime-gate/unsent", &(conf.mail_srv)) )
            err_sys("failed to open unsent directory");
        sleep(900);
    }
}


decompiled_code:
void __fastcall err_doit(int a1, int a2, const char *a3, void *a4)
{
int v6; // er14
int v7; // ebx
char *v8; // rax
char v9[4136]; // [rsp+0h] [rbp-1028h] BYREF

v6 = *_errno_location();
vsprintf(v9, a3, a4);
if ( a1 )
{
v7 = strlen(v9);
v8 = strerror(v6);
snprintf(&v9[v7], 4096LL - v7, ": %s", v8);
}
*(_WORD *)&v9[strlen(v9)] = 10;
if ( daemon_proc )
{
syslog(a2, v9);
}
else
{
fflush(stdout);
fputs(v9, stderr);
fflush(stderr);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2, const char *a3, void *a4)
{
int v6; // er14
int v7; // ebx
char *v8; // rax
char v9[4136]; // [rsp+0h] [rbp-1028h] BYREF

v6 = *_errno_location();
vsprintf(v9, a3, a4);
if ( a1 )
{
v7 = strlen(v9);
v8 = strerror(v6);
snprintf(&v9[v7], 4096LL - v7, ": %s", v8);
}
*(_WORD *)&v9[strlen(v9)] = 10;
if ( daemon_proc )
{
syslog(a2, v9);
}
else
{
fflush(stdout);
fputs(v9, stderr);
fflush(stderr);
}
}

original_code:
static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
{
    int errno_save, n;
    char buf[4096];
    errno_save = (*__errno_location ());
    vsprintf(buf, fmt, ap);
    n = strlen(buf);
    if (errnoflag)
        snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
    strcat(buf, "\n");
    if (daemon_proc) {
        syslog(level, buf);
    }
    else {
        fflush(stdout);
        fputs(buf, stderr);
        fflush(stderr);
    }
    return;
}

canonical_original_code:
static void err_doit(int errnoflag, int level, char *fmt, __builtin_va_list ap)
{
    int errno_save; int n;
    char buf[4096];
    errno_save = (*__errno_location ());
    vsprintf(buf, fmt, ap);
    n = strlen(buf);
    if (errnoflag)
        snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
    strcat(buf, "\n");
    if (daemon_proc) {
        syslog(level, buf);
    }
    else {
        fflush(stdout);
        fputs(buf, stderr);
        fflush(stderr);
    }
    return;
}

variable_types: errnoflag int

variable_types: level int

variable_types:
fmt
char *

variable_types: n int

variable_types:
buf
char [4096]


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

va_start(va, a1);
err_doit(1, 6, a1, va);
}

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

va_start(va, a1);
func0(1, 6, a1, va);
}

original_code:
void err_ret (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}

canonical_original_code:
void err_ret(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}


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

va_start(va, a1);
err_doit(1, 3, a1, va);
exit(1);
}

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

va_start(va, a1);
func0(1, 3, a1, va);
exit(1);
}

original_code:
void err_sys (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}

canonical_original_code:
void err_sys(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}


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

va_start(va, a1);
err_doit(0, 6, a1, va);
}

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

va_start(va, a1);
func0(0, 6, a1, va);
}

original_code:
void err_msg (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}

canonical_original_code:
void err_msg(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}


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

va_start(va, a1);
err_doit(0, 3, a1, va);
exit(1);
}

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

va_start(va, a1);
func0(0, 3, a1, va);
exit(1);
}

original_code:
void err_quit (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}

canonical_original_code:
void err_quit(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}


readn

decompiled_code:
size_t __fastcall readn(int fd, void *buf, size_t nbytes)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = nbytes;
if ( nbytes )
{
v5 = nbytes;
while ( 1 )
{
v6 = read(fd, buf, v5);
if ( v6 < 0 )
{
if ( *_errno_location() != 4 )
return -1LL;
v6 = 0LL;
}
else if ( !v6 )
{
return v3 - v5;
}
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3 - v5;
}
}
v5 = 0LL;
return v3 - v5;
}

canonical_decompiled_code:
size_t __fastcall func0(int fd, void *buf, size_t nbytes)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = nbytes;
if ( nbytes )
{
v5 = nbytes;
while ( 1 )
{
v6 = read(fd, buf, v5);
if ( v6 < 0 )
{
if ( *_errno_location() != 4 )
return -1LL;
v6 = 0LL;
}
else if ( !v6 )
{
return v3 - v5;
}
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3 - v5;
}
}
v5 = 0LL;
return v3 - v5;
}

original_code:
ssize_t readn (int fd, void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nread;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nread = read(fd, ptr, nleft)) < 0) {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return(-1);
        }
        else if (nread == 0)
            break;
        nleft -= nread;
        ptr += nread;
    }
    return (n - nleft);
}

canonical_original_code:
long readn(int fd, void *vptr, unsigned long n)
{
    unsigned long nleft;
    long nread;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nread = read(fd, ptr, nleft)) < 0) {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return(-1);
        }
        else if (nread == 0)
            break;
        nleft -= nread;
        ptr += nread;
    }
    return (n - nleft);
}

variable_types: fd int

variable_types:
vptr
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
ptr
char *


writen

decompiled_code:
size_t __fastcall writen(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func1(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

original_code:
ssize_t writen (int fd, const void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nwritten;
    const char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
            if ((*__errno_location ()) == 4)
                nwritten = 0;
            else
                return -1;
        }
        nleft -= nwritten;
        ptr += nwritten;
    }
    return n;
}

canonical_original_code:
long writen(int fd, void *vptr, unsigned long n)
{
    unsigned long nleft;
    long nwritten;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
            if ((*__errno_location ()) == 4)
                nwritten = 0;
            else
                return -1;
        }
        nleft -= nwritten;
        ptr += nwritten;
    }
    return n;
}

variable_types: nwritten long


decompiled_code:
__int64 __fastcall buf_read(int fd, _BYTE *a2)
{
_BYTE *v2; // rax
__int64 result; // rax
int v4; // edx

if ( read_cnt_2 > 0 )
goto LABEL_2;
while ( 1 )
{
v4 = read(fd, &read_buf_1, 0x200uLL);
read_cnt_2 = v4;
if ( v4 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
result = 0LL;
if ( v4 )
{
read_ptr_0 = (__int64)&read_buf_1;
LABEL_2:
--read_cnt_2;
v2 = (_BYTE *)read_ptr_0++;
*a2 = *v2;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, _BYTE *a2)
{
_BYTE *v2; // rax
__int64 result; // rax
int v4; // edx

if ( read_cnt_2 > 0 )
goto LABEL_2;
while ( 1 )
{
v4 = read(fd, &read_buf_1, 0x200uLL);
read_cnt_2 = v4;
if ( v4 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
result = 0LL;
if ( v4 )
{
read_ptr_0 = (__int64)&read_buf_1;
LABEL_2:
--read_cnt_2;
v2 = (_BYTE *)read_ptr_0++;
*a2 = *v2;
result = 1LL;
}
return result;
}

original_code:
static ssize_t buf_read (int fd, char *ptr)
{
    static int read_cnt = 0;
    static char *read_ptr;
    static char read_buf[512];
    if (read_cnt <= 0) {
again:
        if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
            if ((*__errno_location ()) == 4)
                goto again;
            return -1;
        }
        else if (read_cnt == 0)
            return 0;
        read_ptr = read_buf;
    }
    read_cnt--;
    *ptr = *read_ptr++;
    return 1;
}

canonical_original_code:
static long buf_read(int fd, char *ptr)
{
    static int read_cnt = 0;
    static char *read_ptr;
    static char read_buf[512];
    if (read_cnt <= 0) {
again:
        if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
            if ((*__errno_location ()) == 4)
                goto again;
            return -1;
        }
        else if (read_cnt == 0)
            return 0;
        read_ptr = read_buf;
    }
    read_cnt--;
    *ptr = *read_ptr++;
    return 1;
}