decompiled_code:
__int64 z80_irq_w()
{
return cpu_cause_interrupt(1LL, 255LL);
}

canonical_decompiled_code:
__int64 func7()
{
return cpu_cause_interrupt(1LL, 255LL);
}

original_code:
static void z80_irq_w(offs_t offset,data_t data)
{
 cpu_cause_interrupt( 1, 0xff );
}

canonical_original_code:
static void z80_irq_w(unsigned int offset, unsigned int data)
{
 cpu_cause_interrupt( 1, 0xff );
}


decompiled_code:
__int64 __fastcall vendetta_sound_r(int a1)
{
__int64 result; // rax
unsigned int v2; // edx

result = *(unsigned int *)(Machine + 836LL);
if ( (_DWORD)result )
return K053260_r((unsigned int)(a1 + 2));
v2 = ((_BYTE)res_0 + 1) & 7;
res_0 = v2;
if ( a1 )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
__int64 result; // rax
unsigned int v2; // edx

result = *(unsigned int *)(Machine + 836LL);
if ( (_DWORD)result )
return K053260_r((unsigned int)(a1 + 2));
v2 = ((_BYTE)res_0 + 1) & 7;
res_0 = v2;
if ( a1 )
result = v2;
return result;
}

original_code:
data_t vendetta_sound_r(offs_t offset)
{
 if (Machine->sample_rate != 0) return K053260_r(2 + offset);
 else
 {
  static int res = 0x00;
  res = ((res + 1) & 0x07);
  return offset ? res : 0x00;
 }
}

canonical_original_code:
unsigned int vendetta_sound_r(unsigned int offset)
{
 if (Machine->sample_rate != 0) return K053260_r(2 + offset);
 else
 {
  static int res = 0x00;
  res = ((res + 1) & 0x07);
  return offset ? res : 0x00;
 }
}

variable_types:
offset
unsigned int

variable_types: res int

return_type: unsigned int


decompiled_code:
__int64 init_vendetta()
{
konami_rom_deinterleave_2(137LL);
return konami_rom_deinterleave_4(138LL);
}

canonical_decompiled_code:
__int64 func9()
{
konami_rom_deinterleave_2(137LL);
return konami_rom_deinterleave_4(138LL);
}

original_code:
static void init_vendetta(void)
{
 konami_rom_deinterleave_2(REGION_GFX1);
 konami_rom_deinterleave_4(REGION_GFX2);
}

canonical_original_code:
static void init_vendetta()
{
 konami_rom_deinterleave_2(REGION_GFX1);
 konami_rom_deinterleave_4(REGION_GFX2);
}

return_type: void


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

result = 1LL;
if ( !irq_enabled )
result = ignore_interrupt();
return result;
}

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

result = 1LL;
if ( !irq_enabled )
result = ignore_interrupt();
return result;
}

original_code:
static int vendetta_irq( void )
{
 if (irq_enabled)
  return 1;
 else
  return ignore_interrupt();
}

canonical_original_code:
static int vendetta_irq()
{
 if (irq_enabled)
  return 1;
 else
  return ignore_interrupt();
}

return_type: int


decompiled_code:
char *__fastcall vendetta_banking(int a1)
{
char *result; // rax
unsigned int v2; // eax

result = (char *)memory_region(129LL);
if ( a1 <= 27 )
{
cpu_bankbase[1] = &result[0x2000 * a1 + 0x10000];
result = (char *)&ophw;
if ( ophw == 1 )
{
ophw = -1;
v2 = cpu_get_pc();
result = (char *)cpu_setOPbase16(v2);
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func12(int a1)
{
char *result; // rax
unsigned int v2; // eax

result = (char *)memory_region(129LL);
if ( a1 <= 27 )
{
cpu_bankbase[1] = &result[0x2000 * a1 + 0x10000];
result = (char *)&ophw;
if ( ophw == 1 )
{
ophw = -1;
v2 = cpu_get_pc();
result = (char *)cpu_setOPbase16(v2);
}
}
return result;
}

original_code:
static void vendetta_banking( int lines )
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 if ( lines >= 0x1c )
 {
 }
 else
  { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&RAM[ 0x10000 + ( lines * 0x2000 ) ]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

canonical_original_code:
static void vendetta_banking(int lines)
{
 unsigned char *RAM = memory_region(REGION_CPU1);
 if ( lines >= 0x1c )
 {
 }
 else
  { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&RAM[ 0x10000 + ( lines * 0x2000 ) ]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
}

variable_types: lines int

variable_types:
RAM
unsigned char *


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

if ( (a1 & 1) != 0 )
{
cpu_setbankhandler_r(2LL, &paletteram_r);
cpu_setbankhandler_w(2LL, &paletteram_xBBBBBGGGGGRRRRR_swap_w);
cpu_setbankhandler_r(3LL, &K053247_r);
result = cpu_setbankhandler_w(3LL, &K053247_w);
}
else
{
cpu_setbankhandler_r(2LL, vendetta_K052109_r);
cpu_setbankhandler_w(2LL, vendetta_K052109_w);
cpu_setbankhandler_r(3LL, &K052109_r);
result = cpu_setbankhandler_w(3LL, &K052109_w);
}
return result;
}

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

if ( (a1 & 1) != 0 )
{
cpu_setbankhandler_r(2LL, &paletteram_r);
cpu_setbankhandler_w(2LL, &paletteram_xBBBBBGGGGGRRRRR_swap_w);
cpu_setbankhandler_r(3LL, &K053247_r);
result = cpu_setbankhandler_w(3LL, &K053247_w);
}
else
{
cpu_setbankhandler_r(2LL, vendetta_K052109_r);
cpu_setbankhandler_w(2LL, vendetta_K052109_w);
cpu_setbankhandler_r(3LL, &K052109_r);
result = cpu_setbankhandler_w(3LL, &K052109_w);
}
return result;
}

original_code:
static void vendetta_video_banking( int select )
{
 if ( select & 1 )
 {
  cpu_setbankhandler_r( 2, paletteram_r );
  cpu_setbankhandler_w( 2, paletteram_xBBBBBGGGGGRRRRR_swap_w );
  cpu_setbankhandler_r( 3, K053247_r );
  cpu_setbankhandler_w( 3, K053247_w );
 }
 else
 {
  cpu_setbankhandler_r( 2, vendetta_K052109_r );
  cpu_setbankhandler_w( 2, vendetta_K052109_w );
  cpu_setbankhandler_r( 3, K052109_r );
  cpu_setbankhandler_w( 3, K052109_w );
 }
}

canonical_original_code:
static void vendetta_video_banking(int select)
{
 if ( select & 1 )
 {
  cpu_setbankhandler_r( 2, paletteram_r );
  cpu_setbankhandler_w( 2, paletteram_xBBBBBGGGGGRRRRR_swap_w );
  cpu_setbankhandler_r( 3, K053247_r );
  cpu_setbankhandler_w( 3, K053247_w );
 }
 else
 {
  cpu_setbankhandler_r( 2, vendetta_K052109_r );
  cpu_setbankhandler_w( 2, vendetta_K052109_w );
  cpu_setbankhandler_r( 3, K052109_r );
  cpu_setbankhandler_w( 3, K052109_w );
 }
}

variable_types: select int


decompiled_code:
__int64 vendetta_init_machine()
{
unsigned int v1; // eax

konami_cpu_setlines_callback = vendetta_banking;
paletteram = memory_region(129LL) + 294912;
irq_enabled = 0;
cpu_bankbase[1] = memory_region(129LL) + 0x10000;
if ( ophw == 1 )
{
ophw = -1;
v1 = cpu_get_pc();
cpu_setOPbase16(v1);
}
return vendetta_video_banking(0);
}

canonical_decompiled_code:
__int64 func14()
{
unsigned int v1; // eax

konami_cpu_setlines_callback = vendetta_banking;
paletteram = memory_region(129LL) + 294912;
irq_enabled = 0;
cpu_bankbase[1] = memory_region(129LL) + 0x10000;
if ( ophw == 1 )
{
ophw = -1;
v1 = cpu_get_pc();
cpu_setOPbase16(v1);
}
return func13(0);
}

original_code:
static void vendetta_init_machine( void )
{
 konami_cpu_setlines_callback = vendetta_banking;
 paletteram = &memory_region(REGION_CPU1)[0x48000];
 irq_enabled = 0;
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (UINT8 *)(&memory_region(REGION_CPU1)[0x10000]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
 vendetta_video_banking( 0 );
}

canonical_original_code:
static void vendetta_init_machine()
{
 konami_cpu_setlines_callback = vendetta_banking;
 paletteram = &memory_region(REGION_CPU1)[0x48000];
 irq_enabled = 0;
 { if (1 >= 1 && 1 <= 16) { cpu_bankbase[1] = (unsigned char *)(&memory_region(REGION_CPU1)[0x10000]); if (ophw == 1) { ophw = 0xff; cpu_setOPbase16(cpu_get_pc()); } } };
 vendetta_video_banking( 0 );
}


decompiled_code:
__int64 __fastcall vendetta_K052109_r(int a1)
{
return K052109_r((unsigned int)(a1 + 0x2000));
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1)
{
return K052109_r((unsigned int)(a1 + 0x2000));
}

original_code: static data_t vendetta_K052109_r(offs_t offset) { return K052109_r( offset + 0x2000 ); }

canonical_original_code: static unsigned int vendetta_K052109_r(unsigned int offset) { return K052109_r( offset + 0x2000 ); }


decompiled_code:
__int64 __fastcall vendetta_K052109_w(int a1)
{
return K052109_w((unsigned int)(a1 + 0x2000));
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
return K052109_w((unsigned int)(a1 + 0x2000));
}

original_code: static void vendetta_K052109_w(offs_t offset,data_t data) { K052109_w( offset + 0x2000, data ); }

canonical_original_code: static void vendetta_K052109_w(unsigned int offset, unsigned int data) { K052109_w( offset + 0x2000, data ); }

variable_types:
data
unsigned int


decompiled_code:
__int64 z80_nmi_callback()
{
return cpu_set_nmi_line(1LL, 1LL);
}

canonical_decompiled_code:
__int64 func17()
{
return cpu_set_nmi_line(1LL, 1LL);
}

original_code:
static void z80_nmi_callback( int param )
{
 cpu_set_nmi_line( 1, 1 );
}

canonical_original_code:
static void z80_nmi_callback(int param)
{
 cpu_set_nmi_line( 1, 1 );
}

variable_types: param int


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

if ( a2 != 255 )
{
EEPROM_write_bit(a2 & 0x20);
EEPROM_set_clock_line((a2 >> 4) & 1);
EEPROM_set_cs_line(((unsigned __int8)(a2 >> 3) ^ 1) & 1);
irq_enabled = (a2 >> 6) & 1;
result = vendetta_video_banking(a2 & 1);
}
return result;
}

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

if ( a2 != 255 )
{
EEPROM_write_bit(a2 & 0x20);
EEPROM_set_clock_line((a2 >> 4) & 1);
EEPROM_set_cs_line(((unsigned __int8)(a2 >> 3) ^ 1) & 1);
irq_enabled = (a2 >> 6) & 1;
result = func13(a2 & 1);
}
return result;
}

original_code:
static void vendetta_eeprom_w(offs_t offset,data_t data)
{
 if ( data == 0xff )
  return;
 EEPROM_write_bit(data & 0x20);
 EEPROM_set_clock_line((data & 0x10) ? 1 : 0);
 EEPROM_set_cs_line((data & 0x08) ? 0 : 1);
 irq_enabled = ( data >> 6 ) & 1;
 vendetta_video_banking( data & 1 );
}

canonical_original_code:
static void vendetta_eeprom_w(unsigned int offset, unsigned int data)
{
 if ( data == 0xff )
  return;
 EEPROM_write_bit(data & 0x20);
 EEPROM_set_clock_line((data & 0x10) ? 1 : 0);
 EEPROM_set_cs_line((data & 0x08) ? 0 : 1);
 irq_enabled = ( data >> 6 ) & 1;
 vendetta_video_banking( data & 1 );
}


decompiled_code:
__int64 __fastcall vendetta_5fe0_w(__int64 a1, unsigned int a2)
{
coin_counter_w(0LL, a2 & 1);
coin_counter_w(1LL, a2 & 2);
K052109_set_RMRD_line((a2 >> 3) & 1);
return K053246_set_OBJCHA_line((a2 & 0x20) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, unsigned int a2)
{
coin_counter_w(0LL, a2 & 1);
coin_counter_w(1LL, a2 & 2);
K052109_set_RMRD_line((a2 >> 3) & 1);
return K053246_set_OBJCHA_line((a2 & 0x20) != 0);
}

original_code:
static void vendetta_5fe0_w(offs_t offset,data_t data)
{
 coin_counter_w(0,data & 0x01);
 coin_counter_w(1,data & 0x02);
 K052109_set_RMRD_line((data & 0x08) ? 1 : 0);
 K053246_set_OBJCHA_line((data & 0x20) ? 1 : 0);
}

canonical_original_code:
static void vendetta_5fe0_w(unsigned int offset, unsigned int data)
{
 coin_counter_w(0,data & 0x01);
 coin_counter_w(1,data & 0x02);
 K052109_set_RMRD_line((data & 0x08) ? 1 : 0);
 K053246_set_OBJCHA_line((data & 0x20) ? 1 : 0);
}


decompiled_code:
__int64 vendetta_eeprom_r()
{
int v0; // ebx
int v1; // eax

v0 = EEPROM_read_bit();
v1 = v0 | readinputport(3LL) & 0xC;
if ( !init_eeprom_count )
return v1 | 2u;
--init_eeprom_count;
return v1 & 0xFB | 2u;
}

canonical_decompiled_code:
__int64 func21()
{
int v0; // ebx
int v1; // eax

v0 = EEPROM_read_bit();
v1 = v0 | readinputport(3LL) & 0xC;
if ( !init_eeprom_count )
return v1 | 2u;
--init_eeprom_count;
return v1 & 0xFB | 2u;
}

original_code:
static data_t vendetta_eeprom_r(offs_t offset)
{
 int res;
 res = EEPROM_read_bit();
 res |= 0x02;
 res |= readinputport( 3 ) & 0x0c;
 if (init_eeprom_count)
 {
  init_eeprom_count--;
  res &= 0xfb;
 }
 return res;
}

canonical_original_code:
static unsigned int vendetta_eeprom_r(unsigned int offset)
{
 int res;
 res = EEPROM_read_bit();
 res |= 0x02;
 res |= readinputport( 3 ) & 0x0c;
 if (init_eeprom_count)
 {
  init_eeprom_count--;
  res &= 0xfb;
 }
 return res;
}


decompiled_code:
__int64 __fastcall crbaloon_vh_convert_color_prom(_BYTE *a1, __int64 a2)
{
unsigned int v4; // esi
char v5; // cl
__int64 v6; // rcx
__int64 result; // rax

v4 = 0;
v5 = -1;
if ( *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 696LL) )
{
while ( 1 )
{
*a1 = v5 * ((v4 ^ 1) & 1);
a1[1] = v5 * (((int)~v4 >> 1) & 1);
a1 += 3;
*(a1 - 1) = v5 * (((int)~v4++ >> 2) & 1);
if ( *(_DWORD *)(*(_QWORD *)(Machine + 824LL) + 696LL) <= v4 )
break;
v5 = (v4 & 8) == 0 ? -1 : 85;
}
}
v6 = Machine;
result = (unsigned int)(*(_DWORD *)(*(_QWORD *)(Machine + 512LL) + 12LL)
* *(_DWORD *)(*(_QWORD *)(Machine + 512LL) + 24LL));
if ( (int)result > 0 )
{
LODWORD(result) = 0;
do
{
*(_WORD *)(a2 + 2LL * (*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v6 + 824) + 688LL) + 16LL) + (int)result)) = 15;
*(_WORD *)(a2 + 2LL * (*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(Machine + 824LL) + 688LL) + 16LL) + (int)result) + 2) = (int)result / 2;
result = (unsigned int)(result + 2);
v6 = Machine;
}
while ( *(_DWORD *)(*(_QWORD *)(Machine + 512LL) + 12LL) * *(_DWORD *)(*(_QWORD *)(Machine + 512LL) + 24LL) > (int)result );
}
return result;
}

original_code:
void crbaloon_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
{
 int i;
 for (i = 0;i < Machine->drv->total_colors;i++)
 {
  int intensity;
  intensity = (~i & 0x08) ? 0xff : 0x55;
  *(palette++) = intensity * ((~i >> 0) & 1);
  *(palette++) = intensity * ((~i >> 1) & 1);
  *(palette++) = intensity * ((~i >> 2) & 1);
 }
 for (i = 0;i < (Machine->gfx[0]->total_colors * Machine->gfx[0]->color_granularity);i += 2)
 {
  (colortable[Machine->drv->gfxdecodeinfo[0].color_codes_start + i]) = 15;
  (colortable[Machine->drv->gfxdecodeinfo[0].color_codes_start + i + 1]) = i / 2;
 }
}

canonical_original_code:
void crbaloon_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable, unsigned char *color_prom)
{
 int i;
 for (i = 0;i < Machine->drv->total_colors;i++)
 {
  int intensity;
  intensity = (~i & 0x08) ? 0xff : 0x55;
  *(palette++) = intensity * ((~i >> 0) & 1);
  *(palette++) = intensity * ((~i >> 1) & 1);
  *(palette++) = intensity * ((~i >> 2) & 1);
 }
 for (i = 0;i < (Machine->gfx[0]->total_colors * Machine->gfx[0]->color_granularity);i += 2)
 {
  (colortable[Machine->drv->gfxdecodeinfo[0].color_codes_start + i]) = 15;
  (colortable[Machine->drv->gfxdecodeinfo[0].color_codes_start + i + 1]) = i / 2;
 }
}

variable_types:
palette
unsigned char *

variable_types:
colortable
unsigned short *

variable_types: i int

variable_types: intensity int


decompiled_code:
int *__fastcall crbaloon_spritectrl_w(int a1, int a2)
{
int *result; // rax

result = spritectrl;
spritectrl[a1] = a2;
return result;
}

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

result = spritectrl;
spritectrl[a1] = a2;
return result;
}

original_code:
void crbaloon_spritectrl_w(offs_t offset,data_t data)
{
 spritectrl[offset] = data;
}

canonical_original_code:
void crbaloon_spritectrl_w(unsigned int offset, unsigned int data)
{
 spritectrl[offset] = data;
}


decompiled_code:
__int64 __fastcall crbaloon_vh_screenrefresh(__int64 a1)
{
int v1; // ebx
__int64 v2; // rbp
int v3; // ecx
int v4; // esi
int v5; // ebp
__int64 v6; // r12
__int64 v7; // rax
int *v8; // rsi
int v10; // ebx
_DWORD *v11; // rax
int v12; // er13
unsigned int v13; // [rsp+Ch] [rbp-3Ch]

v1 = videoram_size - 1;
if ( videoram_size - 1 >= 0 )
{
v2 = v1;
do
{
if ( *(_BYTE *)(dirtybuffer + v2) )
{
*(_BYTE *)(dirtybuffer + v2) = 0;
v3 = v1 % 32;
v4 = v1 / 32;
if ( !flipscreen )
{
v3 = 31 - v1 % 32;
v4 = 35 - v1 / 32;
}
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 512LL),
*(unsigned __int8 *)(videoram + v2),
*(_BYTE *)(colorram + v2) & 0xF,
(unsigned int)flipscreen,
(unsigned int)flipscreen,
(unsigned int)(8 * v3),
(unsigned int)(8 * v4),
Machine + 776LL,
0LL,
0LL);
}
--v2;
--v1;
}
while ( v1 >= 0 );
}
copybitmap(a1, tmpbitmap, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 0LL, 0LL);
v5 = dword_C0C;
v13 = dword_C0C;
v6 = (unsigned int)dword_C10;
drawgfx(
a1,
*(_QWORD *)(Machine + 520LL),
spritectrl[0] & 0xF,
15LL,
0LL,
0LL,
(unsigned int)dword_C0C,
(unsigned int)dword_C10,
Machine + 776LL,
2LL,
0LL);
crbaloon_collision = 0;
v7 = Machine;
v8 = *(int **)(Machine + 520LL);
if ( *v8 > 0 )
{
do
{
v11 = (_DWORD *)Machine;
v10 = v6;
if ( *(int *)(*(_QWORD *)(Machine + 520LL) + 4LL) > 0 )
{
while ( 1 )
{
if ( v11[194] <= v5 && v11[195] >= v5 && v11[196] <= v10 && v11[197] >= v10 )
{
v12 = read_pixel(a1, (unsigned int)v5, (unsigned int)v10);
if ( v12 != (unsigned int)read_pixel(tmpbitmap, (unsigned int)v5, (unsigned int)v10) )
break;
}
++v10;
v11 = (_DWORD *)Machine;
if ( *(_DWORD *)(*(_QWORD *)(Machine + 520LL) + 4LL) + (int)v6 <= v10 )
goto LABEL_17;
}
crbaloon_collision = -1;
}
LABEL_17:
++v5;
v7 = Machine;
v8 = *(int **)(Machine + 520LL);
}
while ( (int)(*v8 + v13) > v5 );
}
return drawgfx(a1, v8, spritectrl[0] & 0xF, (spritectrl[0] >> 4) & 0xF, 0LL, 0LL, v13, v6, v7 + 776, 2LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // ebx
__int64 v2; // rbp
int v3; // ecx
int v4; // esi
int v5; // ebp
__int64 v6; // r12
__int64 v7; // rax
int *v8; // rsi
int v10; // ebx
_DWORD *v11; // rax
int v12; // er13
unsigned int v13; // [rsp+Ch] [rbp-3Ch]

v1 = videoram_size - 1;
if ( videoram_size - 1 >= 0 )
{
v2 = v1;
do
{
if ( *(_BYTE *)(dirtybuffer + v2) )
{
*(_BYTE *)(dirtybuffer + v2) = 0;
v3 = v1 % 32;
v4 = v1 / 32;
if ( !flipscreen )
{
v3 = 31 - v1 % 32;
v4 = 35 - v1 / 32;
}
drawgfx(
tmpbitmap,
*(_QWORD *)(Machine + 512LL),
*(unsigned __int8 *)(videoram + v2),
*(_BYTE *)(colorram + v2) & 0xF,
(unsigned int)flipscreen,
(unsigned int)flipscreen,
(unsigned int)(8 * v3),
(unsigned int)(8 * v4),
Machine + 776LL,
0LL,
0LL);
}
--v2;
--v1;
}
while ( v1 >= 0 );
}
copybitmap(a1, tmpbitmap, 0LL, 0LL, 0LL, 0LL, Machine + 776LL, 0LL, 0LL);
v5 = dword_C0C;
v13 = dword_C0C;
v6 = (unsigned int)dword_C10;
drawgfx(
a1,
*(_QWORD *)(Machine + 520LL),
spritectrl[0] & 0xF,
15LL,
0LL,
0LL,
(unsigned int)dword_C0C,
(unsigned int)dword_C10,
Machine + 776LL,
2LL,
0LL);
crbaloon_collision = 0;
v7 = Machine;
v8 = *(int **)(Machine + 520LL);
if ( *v8 > 0 )
{
do
{
v11 = (_DWORD *)Machine;
v10 = v6;
if ( *(int *)(*(_QWORD *)(Machine + 520LL) + 4LL) > 0 )
{
while ( 1 )
{
if ( v11[194] <= v5 && v11[195] >= v5 && v11[196] <= v10 && v11[197] >= v10 )
{
v12 = read_pixel(a1, (unsigned int)v5, (unsigned int)v10);
if ( v12 != (unsigned int)read_pixel(tmpbitmap, (unsigned int)v5, (unsigned int)v10) )
break;
}
++v10;
v11 = (_DWORD *)Machine;
if ( *(_DWORD *)(*(_QWORD *)(Machine + 520LL) + 4LL) + (int)v6 <= v10 )
goto LABEL_17;
}
crbaloon_collision = -1;
}
LABEL_17:
++v5;
v7 = Machine;
v8 = *(int **)(Machine + 520LL);
}
while ( (int)(*v8 + v13) > v5 );
}
return drawgfx(a1, v8, spritectrl[0] & 0xF, (spritectrl[0] >> 4) & 0xF, 0LL, 0LL, v13, v6, v7 + 776, 2LL, 0LL);
}

original_code:
void crbaloon_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
{
 int offs,x,y;
 int bx,by;
 for (offs = videoram_size - 1;offs >= 0;offs--)
 {
  if (dirtybuffer[offs])
  {
   int sx,sy;
   dirtybuffer[offs] = 0;
   sx = offs % 32;
   sy = offs / 32;
   if (flipscreen == 0)
   {
    sx = 31 - sx;
    sy = 35 - sy;
   }
   drawgfx(tmpbitmap,Machine->gfx[0],
     videoram[offs],
     colorram[offs] & 0x0f,
     flipscreen,flipscreen,
     8*sx,8*sy,
     &Machine->visible_area,TRANSPARENCY_NONE,0);
  }
 }
 copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
 bx = spritectrl[1];
 by = spritectrl[2];
 drawgfx(bitmap,Machine->gfx[1],
   spritectrl[0] & 0x0f,
   15,
   0,0,
   bx,by,
   &Machine->visible_area,TRANSPARENCY_PEN,0);
    crbaloon_collision = 0;
 for (x = bx; x < bx + Machine->gfx[1]->width; x++)
 {
  for (y = by; y < by + Machine->gfx[1]->height; y++)
        {
   if ((x < Machine->visible_area.min_x) ||
       (x > Machine->visible_area.max_x) ||
       (y < Machine->visible_area.min_y) ||
       (y > Machine->visible_area.max_y))
   {
    continue;
   }
         if (read_pixel(bitmap, x, y) != read_pixel(tmpbitmap, x, y))
         {
    crbaloon_collision = -1;
    break;
   }
        }
 }
 drawgfx(bitmap,Machine->gfx[1],
   spritectrl[0] & 0x0f,
   (spritectrl[0] & 0xf0) >> 4,
   0,0,
   bx,by,
   &Machine->visible_area,TRANSPARENCY_PEN,0);
}

canonical_original_code:
void crbaloon_vh_screenrefresh(struct osd_bitmap *bitmap, int full_refresh)
{
 int offs; int x; int y;
 int bx; int by;
 for (offs = videoram_size - 1;offs >= 0;offs--)
 {
  if (dirtybuffer[offs])
  {
   int sx; int sy;
   dirtybuffer[offs] = 0;
   sx = offs % 32;
   sy = offs / 32;
   if (flipscreen == 0)
   {
    sx = 31 - sx;
    sy = 35 - sy;
   }
   drawgfx(tmpbitmap,Machine->gfx[0],
     videoram[offs],
     colorram[offs] & 0x0f,
     flipscreen,flipscreen,
     8*sx,8*sy,
     &Machine->visible_area,TRANSPARENCY_NONE,0);
  }
 }
 copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
 bx = spritectrl[1];
 by = spritectrl[2];
 drawgfx(bitmap,Machine->gfx[1],
   spritectrl[0] & 0x0f,
   15,
   0,0,
   bx,by,
   &Machine->visible_area,TRANSPARENCY_PEN,0);
    crbaloon_collision = 0;
 for (x = bx; x < bx + Machine->gfx[1]->width; x++)
 {
  for (y = by; y < by + Machine->gfx[1]->height; y++)
        {
   if ((x < Machine->visible_area.min_x) ||
       (x > Machine->visible_area.max_x) ||
       (y < Machine->visible_area.min_y) ||
       (y > Machine->visible_area.max_y))
   {
    continue;
   }
         if (read_pixel(bitmap, x, y) != read_pixel(tmpbitmap, x, y))
         {
    crbaloon_collision = -1;
    break;
   }
        }
 }
 drawgfx(bitmap,Machine->gfx[1],
   spritectrl[0] & 0x0f,
   (spritectrl[0] & 0xf0) >> 4,
   0,0,
   bx,by,
   &Machine->visible_area,TRANSPARENCY_PEN,0);
}

variable_types: struct osd_bitmap { int width; int depth; void *_private; unsigned char **line; } *

variable_types: offs int

variable_types: x int

variable_types: y int

variable_types: bx int

variable_types: by int

variable_types: sx int

variable_types: sy int


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

val06 = a2;
result = interrupt_enable_w(a1, a2 & 1);
if ( (a2 & 2) != 0 )
{
if ( (a2 & 8) != 0 )
SN76477_enable_w(0LL, 1LL);
else
SN76477_enable_w(0LL, 0LL);
if ( (a2 & 0x10) != 0 )
SN76477_set_slf_res(0LL, 10000.0);
else
SN76477_set_slf_res(0LL, 20000.0);
if ( (a2 & 0x20) != 0 )
SN76477_mixer_b_w(0LL, 1LL);
else
SN76477_mixer_b_w(0LL, 4LL);
result = SN76477_envelope_w(0LL, 1LL);
}
return result;
}

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

val06 = a2;
result = interrupt_enable_w(a1, a2 & 1);
if ( (a2 & 2) != 0 )
{
if ( (a2 & 8) != 0 )
SN76477_enable_w(0LL, 1LL);
else
SN76477_enable_w(0LL, 0LL);
if ( (a2 & 0x10) != 0 )
SN76477_set_slf_res(0LL, 10000.0);
else
SN76477_set_slf_res(0LL, 20000.0);
if ( (a2 & 0x20) != 0 )
SN76477_mixer_b_w(0LL, 1LL);
else
SN76477_mixer_b_w(0LL, 4LL);
result = SN76477_envelope_w(0LL, 1LL);
}
return result;
}

original_code:
void crbaloon_06_w(offs_t offset,data_t data)
{
 val06 = data;
 interrupt_enable_w(offset,data & 1);
    if( data & 0x02 )
 {
  if( data & 0x08 )
  {
   SN76477_enable_w(0, 1);
  }
  else
  {
   SN76477_enable_w(0, 0);
  }
  if( data & 0x10 )
  {
   SN76477_set_slf_res(0, ((float)10*1e3));
  }
  else
  {
   SN76477_set_slf_res(0, ((float)20*1e3));
  }
  if( data & 0x20 )
  {
   SN76477_mixer_b_w(0, 1);
  }
  else
  {
   SN76477_mixer_b_w(0, 4);
  }
  SN76477_envelope_w(0, 1);
 }
}

canonical_original_code:
void crbaloon_06_w(unsigned int offset, unsigned int data)
{
 val06 = data;
 interrupt_enable_w(offset,data & 1);
    if( data & 0x02 )
 {
  if( data & 0x08 )
  {
   SN76477_enable_w(0, 1);
  }
  else
  {
   SN76477_enable_w(0, 0);
  }
  if( data & 0x10 )
  {
   SN76477_set_slf_res(0, ((float)10*1e3));
  }
  else
  {
   SN76477_set_slf_res(0, ((float)20*1e3));
  }
  if( data & 0x20 )
  {
   SN76477_mixer_b_w(0, 1);
  }
  else
  {
   SN76477_mixer_b_w(0, 4);
  }
  SN76477_envelope_w(0, 1);
 }
}

variable_types:
offset
unsigned int


decompiled_code:
__int64 crbaloon_machine_init()
{
SN76477_mixer_a_w(0LL, 0LL);
SN76477_mixer_c_w(0LL, 1LL);
SN76477_envelope_w(0LL, 1LL);
SN76477_enable_w(0LL, 1LL);
return SN76477_enable_w(0LL, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
SN76477_mixer_a_w(0LL, 0LL);
SN76477_mixer_c_w(0LL, 1LL);
SN76477_envelope_w(0LL, 1LL);
SN76477_enable_w(0LL, 1LL);
return SN76477_enable_w(0LL, 0LL);
}

original_code:
static void crbaloon_machine_init(void)
{
 SN76477_mixer_a_w(0, 0);
 SN76477_mixer_c_w(0, 1);
 SN76477_envelope_w(0, 1);
    SN76477_enable_w(0, 1);
    SN76477_enable_w(0, 0);
}

canonical_original_code:
static void crbaloon_machine_init()
{
 SN76477_mixer_a_w(0, 0);
 SN76477_mixer_c_w(0, 1);
 SN76477_envelope_w(0, 1);
    SN76477_enable_w(0, 1);
    SN76477_enable_w(0, 0);
}


decompiled_code:
void *__fastcall crbaloon_flipscreen_w(__int64 a1, char a2)
{
void *result; // rax

result = (void *)(a2 & 1);
if ( (_DWORD)result != flipscreen )
{
flipscreen = a2 & 1;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, char a2)
{
void *result; // rax

result = (void *)(a2 & 1);
if ( (_DWORD)result != flipscreen )
{
flipscreen = a2 & 1;
result = memset(dirtybuffer, 1, videoram_size);
}
return result;
}

original_code:
void crbaloon_flipscreen_w(offs_t offset,data_t data)
{
 if (flipscreen != (data & 1))
 {
  flipscreen = data & 1;
  memset(dirtybuffer,1,videoram_size);
 }
}

canonical_original_code:
void crbaloon_flipscreen_w(unsigned int offset, unsigned int data)
{
 if (flipscreen != (data & 1))
 {
  flipscreen = data & 1;
  memset(dirtybuffer,1,videoram_size);
 }
}


decompiled_code:
void *__fastcall crbaloon_08_w(__int64 a1, int a2)
{
val08 = a2;
return crbaloon_flipscreen_w(a1, a2 & 1);
}

canonical_decompiled_code:
void *__fastcall func6(__int64 a1, int a2)
{
val08 = a2;
return func5(a1, a2 & 1);
}

original_code:
void crbaloon_08_w(offs_t offset,data_t data)
{
 val08 = data;
 crbaloon_flipscreen_w(offset,data & 1);
}

canonical_original_code:
void crbaloon_08_w(unsigned int offset, unsigned int data)
{
 val08 = data;
 crbaloon_flipscreen_w(offset,data & 1);
}


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

if ( crbaloon_collision )
result = input_port_2_r(0LL) & 0xF0 | 8;
else
result = input_port_2_r(0LL) & 0xF0 | 7;
return result;
}

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

if ( crbaloon_collision )
result = input_port_2_r(0LL) & 0xF0 | 8;
else
result = input_port_2_r(0LL) & 0xF0 | 7;
return result;
}

original_code:
data_t crbaloon_IN2_r(offs_t offset)
{
 extern int crbaloon_collision;
 if (crbaloon_collision != 0)
 {
  return (input_port_2_r(0) & 0xf0) | 0x08;
    }
 if (val06 & 0x80)
 {
  return (input_port_2_r(0) & 0xf0) | 0x07;
 }
 else
 {
  return (input_port_2_r(0) & 0xf0) | 0x07;
 }
}

canonical_original_code:
unsigned int crbaloon_IN2_r(unsigned int offset)
{
 extern int crbaloon_collision;
 if (crbaloon_collision != 0)
 {
  return (input_port_2_r(0) & 0xf0) | 0x08;
    }
 if (val06 & 0x80)
 {
  return (input_port_2_r(0) & 0xf0) | 0x07;
 }
 else
 {
  return (input_port_2_r(0) & 0xf0) | 0x07;
 }
}


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

if ( (val08 & 2) != 0 )
result = input_port_3_r(0LL);
else
result = input_port_3_r(0LL) & 0xF;
return result;
}

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

if ( (val08 & 2) != 0 )
result = input_port_3_r(0LL);
else
result = input_port_3_r(0LL) & 0xF;
return result;
}

original_code:
data_t crbaloon_IN3_r(offs_t offset)
{
 if (val08 & 0x02)
  return input_port_3_r(0);
 if (val0a & 0x01)
 {
  return (input_port_3_r(0) & 0x0f) | 0x00;
 }
 else
 {
  return (input_port_3_r(0) & 0x0f) | 0x00;
 }
}

canonical_original_code:
unsigned int crbaloon_IN3_r(unsigned int offset)
{
 if (val08 & 0x02)
  return input_port_3_r(0);
 if (val0a & 0x01)
 {
  return (input_port_3_r(0) & 0x0f) | 0x00;
 }
 else
 {
  return (input_port_3_r(0) & 0x0f) | 0x00;
 }
}


decompiled_code:
__int64 __fastcall crbaloon_IN_r(char a1)
{
unsigned int v1; // eax

v1 = a1 & 3;
if ( v1 == 2 )
return crbaloon_IN2_r();
if ( v1 > 2 )
return crbaloon_IN3_r();
if ( (a1 & 3) != 0 )
return input_port_1_r();
return input_port_0_r();
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1)
{
unsigned int v1; // eax

v1 = a1 & 3;
if ( v1 == 2 )
return func7();
if ( v1 > 2 )
return func8();
if ( (a1 & 3) != 0 )
return input_port_1_r();
return input_port_0_r();
}

original_code:
data_t crbaloon_IN_r(offs_t offset)
{
 switch (offset & 0x03)
 {
  case 0:
   return input_port_0_r(offset);
  case 1:
   return input_port_1_r(offset);
  case 2:
   return crbaloon_IN2_r(offset);
  case 3:
   return crbaloon_IN3_r(offset);
 }
 return 0;
}

canonical_original_code:
unsigned int crbaloon_IN_r(unsigned int offset)
{
 switch (offset & 0x03)
 {
  case 0:
   return input_port_0_r(offset);
  case 1:
   return input_port_1_r(offset);
  case 2:
   return crbaloon_IN2_r(offset);
  case 3:
   return crbaloon_IN3_r(offset);
 }
 return 0;
}


decompiled_code:
int header_col_begin()
{
return putc(33, _bss_start);
}

canonical_decompiled_code:
int func7()
{
return putc(33, _bss_start);
}

original_code:
static void header_col_begin(void)
{
   putc('!', stdout);
}

canonical_original_code:
static void header_col_begin()
{
   putc('!', stdout);
}


decompiled_code:
int col_begin()
{
return putc(124, _bss_start);
}

canonical_decompiled_code:
int func8()
{
return putc(124, _bss_start);
}

original_code:
static void col_begin(void)
{
   putc('|', stdout);
}

canonical_original_code:
static void col_begin()
{
   putc('|', stdout);
}


decompiled_code:
int header_col_end()
{
return putc(10, _bss_start);
}

canonical_decompiled_code:
int func9()
{
return putc(10, _bss_start);
}

original_code:
static void header_col_end(void)
{
   putc('\n', stdout);
}

canonical_original_code:
static void header_col_end()
{
   putc('\n', stdout);
}


decompiled_code:
int table_begin()
{
return puts("{| class=\"wikitable\"");
}

canonical_decompiled_code:
int func11()
{
return puts("{| class=\"wikitable\"");
}

original_code:
static void table_begin(void)
{
   printf("{| class=\"wikitable\"\n");
}

canonical_original_code:
static void table_begin()
{
   printf("{| class=\"wikitable\"\n");
}


decompiled_code:
int row_begin()
{
return puts("|-");
}

canonical_decompiled_code:
int func13()
{
return puts("|-");
}

original_code:
static void row_begin(void)
{
   printf("|-\n");
}

canonical_original_code:
static void row_begin()
{
   printf("|-\n");
}


decompiled_code:
int table_end()
{
return puts("|}");
}

canonical_decompiled_code:
int func14()
{
return puts("|}");
}

original_code:
static void table_end(void)
{
   printf("|}\n");
}

canonical_original_code:
static void table_end()
{
   printf("|}\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // er12
int v5; // ebp
int v6; // ebx

table_begin();
v3 = 1;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getc(stdin);
if ( v6 == -1 )
break;
if ( v6 == 44 )
{
if ( v4 )
{
if ( v5 )
{
if ( v3 )
{
header_col_end();
v4 = v3;
}
else
{
col_end();
v4 = v5;
}
v5 = 0;
}
else if ( v3 )
{
header_col_begin();
header_col_end();
v4 = v3;
}
else
{
col_begin();
col_end();
v5 = 0;
}
}
else if ( v3 )
{
header_row_begin();
v4 = v3;
}
else
{
row_begin();
v4 = 1;
}
}
else if ( v6 == 10 )
{
if ( v5 )
{
if ( v3 )
header_col_end();
else
col_end();
}
v3 = 0;
v4 = 0;
v5 = 0;
}
else
{
if ( v4 )
{
if ( !v5 )
{
if ( !v3 )
goto LABEL_20;
LABEL_28:
header_col_begin();
goto LABEL_21;
}
}
else
{
if ( !v3 )
{
row_begin();
if ( v5 )
goto LABEL_21;
LABEL_20:
col_begin();
goto LABEL_21;
}
header_row_begin();
if ( !v5 )
goto LABEL_28;
}
LABEL_21:
putc(v6, _bss_start);
v4 = 1;
v5 = 1;
}
}
if ( v5 )
{
if ( v3 )
header_col_end();
else
col_end();
}
table_end();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // er12
int v5; // ebp
int v6; // ebx

func11();
v3 = 1;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getc(stdin);
if ( v6 == -1 )
break;
if ( v6 == 44 )
{
if ( v4 )
{
if ( v5 )
{
if ( v3 )
{
func9();
v4 = v3;
}
else
{
func10();
v4 = v5;
}
v5 = 0;
}
else if ( v3 )
{
func7();
func9();
v4 = v3;
}
else
{
func8();
func10();
v5 = 0;
}
}
else if ( v3 )
{
func12();
v4 = v3;
}
else
{
func13();
v4 = 1;
}
}
else if ( v6 == 10 )
{
if ( v5 )
{
if ( v3 )
func9();
else
func10();
}
v3 = 0;
v4 = 0;
v5 = 0;
}
else
{
if ( v4 )
{
if ( !v5 )
{
if ( !v3 )
goto LABEL_20;
LABEL_28:
func7();
goto LABEL_21;
}
}
else
{
if ( !v3 )
{
func13();
if ( v5 )
goto LABEL_21;
LABEL_20:
func8();
goto LABEL_21;
}
func12();
if ( !v5 )
goto LABEL_28;
}
LABEL_21:
putc(v6, _bss_start);
v4 = 1;
v5 = 1;
}
}
if ( v5 )
{
if ( v3 )
func9();
else
func10();
}
func14();
return 0;
}

original_code:
int main(int argc, char* argv)
{
   int c;
   int col_started = (0);
   int row_started = (0);
   int header_row = (1);
   table_begin();
   while((c = getc(stdin)) != (-1))
   {
      if ((',') == c)
      {
         if (row_started)
         {
            if (col_started)
            {
               if (header_row)
               {
                  header_col_end();
               }
               else
               {
                  col_end();
               }
               col_started = (0);
            }
            else
            {
               if (header_row)
               {
                  header_col_begin();
                  header_col_end();
               }
               else
               {
                  col_begin();
                  col_end();
               }
            }
         }
         else
         {
            if (header_row)
            {
               header_row_begin();
            }
            else
            {
               row_begin();
            }
            row_started = (1);
         }
      }
      else if (('\n') == c)
      {
         if (col_started)
         {
            if (header_row)
            {
               header_col_end();
            }
            else
            {
               col_end();
            }
         }
         if (header_row)
         {
            header_row_end();
            header_row = (0);
         }
         else
         {
            row_end();
         }
         row_started = (0);
         col_started = (0);
      }
      else
      {
         if (!row_started)
         {
            if (header_row)
            {
               header_row_begin();
               if (!col_started)
               {
                  header_col_begin();
               }
            }
            else
            {
               row_begin();
               if (!col_started)
               {
                  col_begin();
               }
            }
            row_started = (1);
            col_started = (1);
         }
         else if (!col_started)
         {
            if(header_row)
            {
               header_col_begin();
            }
            else
            {
               col_begin();
            }
            col_started = (1);
         }
         putc(c, stdout);
      }
   }
   if (col_started)
   {
      if (header_row)
      {
         header_col_end();
      }
      else
      {
         col_end();
      }
   }
   if (header_row)
   {
      header_row_end();
   }
   else
   {
      row_end();
   }
   table_end();
   return (0);
}

canonical_original_code:
int main(int argc, char *argv)
{
   int c;
   int col_started = (0);
   int row_started = (0);
   int header_row = (1);
   table_begin();
   while((c = getc(stdin)) != (-1))
   {
      if ((',') == c)
      {
         if (row_started)
         {
            if (col_started)
            {
               if (header_row)
               {
                  header_col_end();
               }
               else
               {
                  col_end();
               }
               col_started = (0);
            }
            else
            {
               if (header_row)
               {
                  header_col_begin();
                  header_col_end();
               }
               else
               {
                  col_begin();
                  col_end();
               }
            }
         }
         else
         {
            if (header_row)
            {
               header_row_begin();
            }
            else
            {
               row_begin();
            }
            row_started = (1);
         }
      }
      else if (('\n') == c)
      {
         if (col_started)
         {
            if (header_row)
            {
               header_col_end();
            }
            else
            {
               col_end();
            }
         }
         if (header_row)
         {
            header_row_end();
            header_row = (0);
         }
         else
         {
            row_end();
         }
         row_started = (0);
         col_started = (0);
      }
      else
      {
         if (!row_started)
         {
            if (header_row)
            {
               header_row_begin();
               if (!col_started)
               {
                  header_col_begin();
               }
            }
            else
            {
               row_begin();
               if (!col_started)
               {
                  col_begin();
               }
            }
            row_started = (1);
            col_started = (1);
         }
         else if (!col_started)
         {
            if(header_row)
            {
               header_col_begin();
            }
            else
            {
               col_begin();
            }
            col_started = (1);
         }
         putc(c, stdout);
      }
   }
   if (col_started)
   {
      if (header_row)
      {
         header_col_end();
      }
      else
      {
         col_end();
      }
   }
   if (header_row)
   {
      header_row_end();
   }
   else
   {
      row_end();
   }
   table_end();
   return (0);
}

variable_types: argc int

variable_types:
argv
char *

variable_types: c int


repo: Joshun/cli-quiz

original_code:
int get_max_length(const char *data, const int numlines)
{
 int i, index = 0, count;
 int lastlength = 0;
 for(i=0; i<(numlines + 1); i++)
 {
  count=0;
  while(data[index] != '\n')
  {
   count++;
   index++;
  }
  index++;
  if( count > lastlength )
   lastlength = count;
 }
 return lastlength;
}

canonical_original_code:
int get_max_length(char *data, int numlines)
{
 int i; int index = 0; int count;
 int lastlength = 0;
 for(i=0; i<(numlines + 1); i++)
 {
  count=0;
  while(data[index] != '\n')
  {
   count++;
   index++;
  }
  index++;
  if( count > lastlength )
   lastlength = count;
 }
 return lastlength;
}

variable_types:
data
char *

variable_types: numlines int

variable_types: index int

variable_types: count int

variable_types: lastlength int


decompiled_code:
float __fastcall get_percentage(int a1, int a2)
{
return (float)((float)a1 / (float)a2) * 100.0;
}

canonical_decompiled_code:
float __fastcall func1(int a1, int a2)
{
return (float)((float)a1 / (float)a2) * 100.0;
}

original_code:
float get_percentage(const int num, const int tot)
{
 return( (( float)num / (float)tot) * 100);
}

canonical_original_code:
float get_percentage(int num, int tot)
{
 return( (( float)num / (float)tot) * 100);
}

variable_types: num int

variable_types: tot int

return_type: float


decompiled_code:
__int64 __fastcall question_not_asked(int a1, __int64 a2)
{
return (unsigned int)*(char *)(*(_QWORD *)(a2 + 24) + a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
return (unsigned int)*(char *)(*(_QWORD *)(a2 + 24) + a1);
}

original_code:
int question_not_asked(int question_number, Questiondata *questions)
{
 return questions->not_asked[question_number];
}

canonical_original_code:
int question_not_asked(int question_number, struct Questiondata *questions)
{
 return questions->not_asked[question_number];
}

variable_types: struct Questiondata { int numquestions; int *question_answers; char **question_text; char *not_asked; } *


decompiled_code:
int __fastcall ask_questions(int *a1)
{
int v1; // ebx
__int64 v2; // rax
int v3; // ebx
unsigned int v4; // er14
unsigned int v5; // er13
int i; // er12
float v7; // xmm0_4
int v9; // [rsp+Ch] [rbp-2Ch] BYREF

v1 = *a1;
*((_QWORD *)a1 + 3) = malloc(*a1);
if ( v1 > 0 )
{
v2 = 0LL;
do
{
*(_BYTE *)(*((_QWORD *)a1 + 3) + v2) = 1;
v1 = *a1;
++v2;
}
while ( *a1 > (int)v2 );
}
v3 = get_random(0, v1);
v4 = *a1;
v5 = 0;
for ( i = 0; i < *a1; v4 = *a1 )
{
while ( !(unsigned int)question_not_asked(v3, (__int64)a1) )
v3 = get_random(0, *a1);
printf("Question %d: %s\n", (unsigned int)++i, *(const char **)(*((_QWORD *)a1 + 2) + 8LL * v3));
_isoc99_fscanf(stdin, "%d", &v9);
if ( *(_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * v3) == v9 )
{
puts("Correct!");
++v5;
}
else
{
puts("Wrong answer.");
}
*(_BYTE *)(*((_QWORD *)a1 + 3) + v3) = 0;
}
v7 = get_percentage(v5, v4);
return printf("You scored %d out of %d: %.1f%%\n", v5, v4, v7);
}

canonical_decompiled_code:
int __fastcall func3(int *a1)
{
int v1; // ebx
__int64 v2; // rax
int v3; // ebx
unsigned int v4; // er14
unsigned int v5; // er13
int i; // er12
float v7; // xmm0_4
int v9; // [rsp+Ch] [rbp-2Ch] BYREF

v1 = *a1;
*((_QWORD *)a1 + 3) = malloc(*a1);
if ( v1 > 0 )
{
v2 = 0LL;
do
{
*(_BYTE *)(*((_QWORD *)a1 + 3) + v2) = 1;
v1 = *a1;
++v2;
}
while ( *a1 > (int)v2 );
}
v3 = func0(0, v1);
v4 = *a1;
v5 = 0;
for ( i = 0; i < *a1; v4 = *a1 )
{
while ( !(unsigned int)func2(v3, (__int64)a1) )
v3 = func0(0, *a1);
printf("Question %d: %s\n", (unsigned int)++i, *(const char **)(*((_QWORD *)a1 + 2) + 8LL * v3));
_isoc99_fscanf(stdin, "%d", &v9);
if ( *(_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * v3) == v9 )
{
puts("Correct!");
++v5;
}
else
{
puts("Wrong answer.");
}
*(_BYTE *)(*((_QWORD *)a1 + 3) + v3) = 0;
}
v7 = func1(v5, v4);
return printf("You scored %d out of %d: %.1f%%\n", v5, v4, v7);
}

original_code:
void ask_questions(Questiondata *questions)
{
 questions->not_asked = malloc(questions->numquestions * sizeof(char));
 int i; for(i=0; i<questions->numquestions; i++)
 {
  questions->not_asked[i] = 1;
 }
 int question_no, answer, question_count = 0;
 int question_correct = 0;
 question_no = get_random(0, questions->numquestions);
 while(question_count < questions->numquestions)
 {
  while( ! question_not_asked(question_no, questions) ) question_no = get_random(0, questions->numquestions);
  printf("Question %d: %s\n", question_count + 1, questions->question_text[question_no]);
  fscanf(stdin, "%d", &answer);
  if( questions->question_answers[question_no] == answer )
  {
   printf("Correct!\n");
   question_correct++;
  }
  else
   printf("Wrong answer.\n");
  questions->not_asked[question_no] = 0;
  question_count++;
 }
 printf("You scored %d out of %d: %.1f%%\n", question_correct, questions->numquestions, get_percentage(question_correct, questions->numquestions));
}

canonical_original_code:
void ask_questions(struct Questiondata *questions)
{
 questions->not_asked = malloc(questions->numquestions * sizeof(char));
 int i; for(i=0; i<questions->numquestions; i++)
 {
  questions->not_asked[i] = 1;
 }
 int question_no; int answer; int question_count = 0;
 int question_correct = 0;
 question_no = get_random(0, questions->numquestions);
 while(question_count < questions->numquestions)
 {
  while( ! question_not_asked(question_no, questions) ) question_no = get_random(0, questions->numquestions);
  printf("Question %d: %s\n", question_count + 1, questions->question_text[question_no]);
  fscanf(stdin, "%d", &answer);
  if( questions->question_answers[question_no] == answer )
  {
   printf("Correct!\n");
   question_correct++;
  }
  else
   printf("Wrong answer.\n");
  questions->not_asked[question_no] = 0;
  question_count++;
 }
 printf("You scored %d out of %d: %.1f%%\n", question_correct, questions->numquestions, get_percentage(question_correct, questions->numquestions));
}

variable_types: i int

variable_types: answer int

user_defined_types: struct Questiondata { int numquestions; int *question_answers; char **question_text; char *not_asked; }


repo: Jotschi/SPISniffer

decompiled_code:
__int64 __fastcall BP_WriteToPirate(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rsi
char v4[9]; // [rsp+Fh] [rbp-9h] BYREF

v4[0] = 0;
serial_write(a1, a2, 1LL);
result = 0LL;
if ( disable_comport != 1 )
{
v3 = (unsigned int)serial_read((unsigned int)a1, v4, 1LL);
result = 0LL;
if ( v4[0] != 1 )
{
if ( modem == 1 )
{
printf("Modem responded with %i byte and with a value of 0X%X\n", v3, (unsigned int)v4[0]);
result = 0LL;
}
else
{
printf("ERROR");
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rsi
char v4[9]; // [rsp+Fh] [rbp-9h] BYREF

v4[0] = 0;
serial_write(a1, a2, 1LL);
result = 0LL;
if ( disable_comport != 1 )
{
v3 = (unsigned int)serial_read((unsigned int)a1, v4, 1LL);
result = 0LL;
if ( v4[0] != 1 )
{
if ( modem == 1 )
{
printf("Modem responded with %i byte and with a value of 0X%X\n", v3, (unsigned int)v4[0]);
result = 0LL;
}
else
{
printf("ERROR");
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
uint32_t BP_WriteToPirate(int fd, char * val) {
 int res = -1;
 char ret = 0;
    serial_write(fd, val, 1);
    if (disable_comport != 1) {
          res = serial_read(fd, &ret, 1);
       if( ret != '\x01') {
           if (modem==1){
              printf("Modem responded with %i byte and with a value of 0X%X\n",res,ret);
           } else {
            printf("ERROR");
            return -1;
           }
         }
        }
 return 0;
}

canonical_original_code:
unsigned int BP_WriteToPirate(int fd, char *val) {
 int res = -1;
 char ret = 0;
    serial_write(fd, val, 1);
    if (disable_comport != 1) {
          res = serial_read(fd, &ret, 1);
       if( ret != '\x01') {
           if (modem==1){
              printf("Modem responded with %i byte and with a value of 0X%X\n",res,ret);
           } else {
            printf("ERROR");
            return -1;
           }
         }
        }
 return 0;
}

variable_types: fd int

variable_types:
val
char *

variable_types: res int

variable_types: ret char


decompiled_code:
unsigned int __fastcall BP_EnableMode(unsigned int a1, char a2)
{
int v4; // ebx
unsigned int result; // eax
bool v6; // zf
unsigned int v7; // eax
__int64 v8[12]; // [rsp+0h] [rbp-98h] BYREF
int v9; // [rsp+60h] [rbp-38h]

v8[0] = 0LL;
v8[1] = 0LL;
v8[2] = 0LL;
v8[3] = 0LL;
v8[4] = 0LL;
v8[5] = 0LL;
v8[6] = 0LL;
v8[7] = 0LL;
v8[8] = 0LL;
v8[9] = 0LL;
v8[10] = 0LL;
v8[11] = 0LL;
v9 = 0;
puts(" Entering binary mode...");
v4 = 0;
if ( a1 == -1 )
return printf("Port does not exist!");
while ( 1 )
{
LOBYTE(v8[0]) = 0;
serial_write(a1, v8, 1LL);
++v4;
usleep(1u);
v7 = serial_read(a1, v8, 5LL);
if ( modem == 1 )
break;
if ( v7 != 5 && v4 > 20 )
{
fprintf(stderr, "Buspirate did not respond correctly :( %i \n", v7);
exit(-1);
}
v6 = memcmp(v8, "BBIO1", 5uLL) == 0;
if ( v4 == 26 )
goto LABEL_4;
if ( v6 )
goto LABEL_11;
}
printf("\nModem Responded = %i\n", v7);
if ( v4 == 26 )
{
LABEL_4:
puts("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable");
exit(-1);
}
LABEL_11:
LOBYTE(v8[0]) = a2;
serial_write(a1, v8, 1LL);
usleep(1u);
result = serial_read(a1, v8, 4LL);
if ( modem == 1 )
return printf("Modem Responded = %i with value %#X\n", result, (unsigned int)SLOBYTE(v8[0]));
if ( result != 4 || memcmp(v8, "SPI1", 4uLL) )
{
fprintf(stderr, "Buspirate did not respond correctly :( %i \n", result);
exit(-1);
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func1(unsigned int a1, char a2)
{
int v4; // ebx
unsigned int result; // eax
bool v6; // zf
unsigned int v7; // eax
__int64 v8[12]; // [rsp+0h] [rbp-98h] BYREF
int v9; // [rsp+60h] [rbp-38h]

v8[0] = 0LL;
v8[1] = 0LL;
v8[2] = 0LL;
v8[3] = 0LL;
v8[4] = 0LL;
v8[5] = 0LL;
v8[6] = 0LL;
v8[7] = 0LL;
v8[8] = 0LL;
v8[9] = 0LL;
v8[10] = 0LL;
v8[11] = 0LL;
v9 = 0;
puts(" Entering binary mode...");
v4 = 0;
if ( a1 == -1 )
return printf("Port does not exist!");
while ( 1 )
{
LOBYTE(v8[0]) = 0;
serial_write(a1, v8, 1LL);
++v4;
usleep(1u);
v7 = serial_read(a1, v8, 5LL);
if ( modem == 1 )
break;
if ( v7 != 5 && v4 > 20 )
{
fprintf(stderr, "Buspirate did not respond correctly :( %i \n", v7);
exit(-1);
}
v6 = memcmp(v8, "BBIO1", 5uLL) == 0;
if ( v4 == 26 )
goto LABEL_4;
if ( v6 )
goto LABEL_11;
}
printf("\nModem Responded = %i\n", v7);
if ( v4 == 26 )
{
LABEL_4:
puts("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable");
exit(-1);
}
LABEL_11:
LOBYTE(v8[0]) = a2;
serial_write(a1, v8, 1LL);
usleep(1u);
result = serial_read(a1, v8, 4LL);
if ( modem == 1 )
return printf("Modem Responded = %i with value %#X\n", result, (unsigned int)SLOBYTE(v8[0]));
if ( result != 4 || memcmp(v8, "SPI1", 4uLL) )
{
fprintf(stderr, "Buspirate did not respond correctly :( %i \n", result);
exit(-1);
}
return result;
}

original_code:
void BP_EnableMode(int fd, char bbmode)
{
 int ret;
 char tmp[100] = { [0 ... 20] = 0x00 };
 int done = 0;
 int tries=0;
 printf(" Entering binary mode...\n");
 if (fd==-1)
 {
     printf("Port does not exist!");
     return;
 }
 while (!done) {
  tmp[0]=0x00;
  serial_write(fd, tmp, 1);
  tries++;
  usleep(1);
  ret = serial_read(fd, tmp, 5);
  if (modem==1)
  {
            printf("\nModem Responded = %i\n",ret);
            done=1;
  }
  else {
       if (ret != 5 && tries>20) {
            fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret );
            exit(-1);
        } else if (strncmp(tmp, "BBIO1", 5) == 0) {
          done=1;
             }
  }
  if (tries>25){
  printf("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n");
  exit(-1);
  }
 }
 done=0;
 tmp[0] = bbmode;
 serial_write(fd, tmp, 1);
 tries++;
  usleep(1);
 ret = serial_read(fd, tmp, 4);
 if (modem==1)
  {
            printf("Modem Responded = %i with value %#X\n",ret,tmp[0]);
  }
        else {
          if ( (ret==4) && (strncmp(tmp, "SPI1", 4) == 0)) {
          } else{
          fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret );
          exit(-1);
         }
        }
}

canonical_original_code:
void BP_EnableMode(int fd, char bbmode)
{
 int ret;
 char tmp[100] = { [0 ... 20] = 0x00 };
 int done = 0;
 int tries = 0;
 printf(" Entering binary mode...\n");
 if (fd==-1)
 {
     printf("Port does not exist!");
     return;
 }
 while (!done) {
  tmp[0]=0x00;
  serial_write(fd, tmp, 1);
  tries++;
  usleep(1);
  ret = serial_read(fd, tmp, 5);
  if (modem==1)
  {
            printf("\nModem Responded = %i\n",ret);
            done=1;
  }
  else {
       if (ret != 5 && tries>20) {
            fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret );
            exit(-1);
        } else if (strncmp(tmp, "BBIO1", 5) == 0) {
          done=1;
             }
  }
  if (tries>25){
  printf("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n");
  exit(-1);
  }
 }
 done=0;
 tmp[0] = bbmode;
 serial_write(fd, tmp, 1);
 tries++;
  usleep(1);
 ret = serial_read(fd, tmp, 4);
 if (modem==1)
  {
            printf("Modem Responded = %i with value %#X\n",ret,tmp[0]);
  }
        else {
          if ( (ret==4) && (strncmp(tmp, "SPI1", 4) == 0)) {
          } else{
          fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret );
          exit(-1);
         }
        }
}

variable_types: bbmode char

variable_types: ret int

variable_types:
tmp
char [100]

variable_types: done int

variable_types: tries int


decompiled_code:
__int64 __fastcall print_usage(const char *a1)
{
puts("\n");
puts("-------------------------------------------------------");
putchar(10);
puts(" Usage: ");
printf(" %s -d device -e 1 -p 0 \n ", a1);
putchar(10);
printf(" Example Usage: %s COM1 -s Speed -e 1 -p 0 \n", a1);
putchar(10);
puts(" Where: -d device is port e.g. COM1 ");
puts(" -s Speed is port Speed default is 115200 ");
puts(" -e ClockEdge is 0 or 1 default is 1 ");
puts(" -p Polarity is 0 or 1 default is 0 ");
puts(" -r RawData is 0 or 1 default is 0 ");
putchar(10);
putchar(10);
puts("-------------------------------------------------------");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
puts("\n");
puts("-------------------------------------------------------");
putchar(10);
puts(" Usage: ");
printf(" %s -d device -e 1 -p 0 \n ", a1);
putchar(10);
printf(" Example Usage: %s COM1 -s Speed -e 1 -p 0 \n", a1);
putchar(10);
puts(" Where: -d device is port e.g. COM1 ");
puts(" -s Speed is port Speed default is 115200 ");
puts(" -e ClockEdge is 0 or 1 default is 1 ");
puts(" -p Polarity is 0 or 1 default is 0 ");
puts(" -r RawData is 0 or 1 default is 0 ");
putchar(10);
putchar(10);
puts("-------------------------------------------------------");
return 0LL;
}

original_code:
int print_usage(char * appname)
 {
  printf("\n\n");
   printf("-------------------------------------------------------\n");
  printf("\n");
        printf(" Usage:              \n");
  printf("   %s  -d device -e 1 -p 0 \n ",appname);
  printf("\n");
  printf("   Example Usage:   %s COM1 -s Speed -e 1 -p 0 \n",appname);
  printf("\n");
  printf("           Where: -d device is port e.g.  COM1  \n");
  printf("                  -s Speed is port Speed  default is 115200 \n");
  printf("                  -e ClockEdge is 0 or 1  default is 1 \n");
  printf("                  -p Polarity  is 0 or 1  default is 0 \n");
  printf("                  -r RawData  is 0 or 1  default is 0 \n");
  printf("\n");
        printf("\n");
       printf("-------------------------------------------------------\n");
  return 0;
 }

canonical_original_code:
int print_usage(char *appname)
 {
  printf("\n\n");
   printf("-------------------------------------------------------\n");
  printf("\n");
        printf(" Usage:              \n");
  printf("   %s  -d device -e 1 -p 0 \n ",appname);
  printf("\n");
  printf("   Example Usage:   %s COM1 -s Speed -e 1 -p 0 \n",appname);
  printf("\n");
  printf("           Where: -d device is port e.g.  COM1  \n");
  printf("                  -s Speed is port Speed  default is 115200 \n");
  printf("                  -e ClockEdge is 0 or 1  default is 1 \n");
  printf("                  -p Polarity  is 0 or 1  default is 0 \n");
  printf("                  -r RawData  is 0 or 1  default is 0 \n");
  printf("\n");
        printf("\n");
       printf("-------------------------------------------------------\n");
  return 0;
 }

variable_types:
appname
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
char *v4; // r14
char *v5; // r13
char *v6; // r12
unsigned int v7; // eax
int v8; // eax
unsigned int v9; // er12
int v10; // er14
unsigned __int8 *v11; // r13
int v12; // ebx
char *v14; // [rsp+8h] [rbp-150h]
char v15; // [rsp+1Fh] [rbp-139h] BYREF
__int64 v16[39]; // [rsp+20h] [rbp-138h] BYREF

v16[0] = 0LL;
v16[1] = 0LL;
v16[2] = 0LL;
v16[3] = 0LL;
v16[4] = 0LL;
v16[5] = 0LL;
v16[6] = 0LL;
v16[7] = 0LL;
v16[8] = 0LL;
v16[9] = 0LL;
v16[10] = 0LL;
v16[11] = 0LL;
v16[12] = 0LL;
v16[13] = 0LL;
v16[14] = 0LL;
v16[15] = 0LL;
v16[16] = 0LL;
v16[17] = 0LL;
v16[18] = 0LL;
v16[19] = 0LL;
v16[20] = 0LL;
v16[21] = 0LL;
v16[22] = 0LL;
v16[23] = 0LL;
v16[24] = 0LL;
v16[25] = 0LL;
v16[26] = 0LL;
v16[27] = 0LL;
v16[28] = 0LL;
v16[29] = 0LL;
v16[30] = 0LL;
v16[31] = 0LL;
puts("-------------------------------------------------------");
putchar(10);
puts(" Bus Pirate binary mode SPI SNIFFER utility v0.3 (CC-0)");
puts(" http://dangerousprototypes.com");
putchar(10);
puts("-------------------------------------------------------");
puts("\n\n");
if ( argc <= 1 )
{
puts("ERROR: Invalid argument(s).\n");
puts("Help Menu");
print_usage(*argv);
exit(-1);
}
v3 = 0LL;
v4 = 0LL;
v14 = 0LL;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "ms:p:e:d:r:");
if ( v7 == -1 )
break;
switch ( v7 )
{
case 'd':
if ( v6 )
{
puts("Device/PORT error!");
exit(-1);
}
v6 = strdup(optarg);
break;
case 'e':
if ( v4 )
{
puts("Clock Edge should be 0 or 1");
exit(-1);
}
v4 = strdup(optarg);
break;
case 'm':
modem = 1;
break;
case 'p':
if ( v14 )
{
puts("Polarity must be 0 or 1");
exit(-1);
}
v14 = strdup(optarg);
break;
case 'r':
if ( v3 )
{
puts("Raw Data should be 0 or 1");
exit(-1);
}
v3 = strdup(optarg);
break;
case 's':
if ( v5 )
{
puts("Speed should be set: eg 115200 ");
exit(-1);
}
v5 = strdup(optarg);
break;
default:
printf(" Invalid argument %c", v7);
print_usage(*argv);
break;
}
}
puts(" Press escape to exit ");
putchar(10);
if ( !v6 )
{
puts(" No serial port set");
print_usage(*argv);
exit(-1);
}
if ( !v4 )
v4 = strdup("1");
if ( !v14 )
v14 = strdup("0");
if ( !v5 )
v5 = strdup("115200");
if ( !v3 )
v3 = strdup("0");
puts("\n Parameters used:");
printf(" Device = %s, Speed = %s, Clock Edge= %s, Polarity= %s RawData= %s \n\n", v6, v5, v4, v14, v3);
printf(" Opening Bus Pirate on %s at %sbps...\n", v6, v5);
v8 = serial_open(v6);
v9 = v8;
if ( v8 >= 0 )
{
serial_setup((unsigned int)v8, (unsigned int)v5);
puts(" Starting SPI sniffer...");
if ( modem == 1 )
{
serial_write(v9, "ATI3\r", 5LL);
usleep(1u);
serial_read(v9, v16, 256LL);
printf("\n %s\n", (const char *)v16);
serial_write(v9, "ATI4\r", 5LL);
usleep(1u);
serial_read(v9, v16, 256LL);
printf("\n %s\n", (const char *)v16);
serial_write(v9, "ATI7\r", 5LL);
usleep(1u);
serial_read(v9, v16, 256LL);
printf("\n %s\n", (const char *)v16);
}
else
{
fwrite(" Configuring Bus Pirate...\n", 1uLL, 0x1BuLL, stderr);
BP_EnableMode(v9, 1LL);
v15 = 0x80;
if ( *v4 == 49 )
v15 = -126;
if ( *v14 == 49 )
v15 |= 4u;
BP_WriteToPirate(v9, &v15);
serial_write(v9, &unk_972, 1LL);
}
puts(" (OK) Happy sniffing! Press ESC to stop.");
v10 = 0;
do
{
LABEL_44:
usleep(1u);
v12 = serial_read(v9, v16, 100LL);
}
while ( v12 <= 0 );
v11 = (unsigned __int8 *)v16;
while ( 1 )
{
printf("%02X ", *v11);
if ( *v3 == 49 )
{
printf("%02X ", *v11);
}
else
{
switch ( v10 )
{
case 2:
printf("0x%02X(", *v11);
v10 = 3;
goto LABEL_43;
case 3:
printf("0x%02X)", *v11);
v10 = 1;
goto LABEL_43;
case 1:
v10 = 2;
if ( *v11 == 92 )
goto LABEL_43;
if ( *v11 == 93 )
{
puts("]");
v10 = 0;
goto LABEL_43;
}
break;
default:
if ( *v11 == 91 )
{
putchar(91);
v10 = 1;
goto LABEL_43;
}
break;
}
puts("Sync");
v10 = 0;
}
LABEL_43:
if ( v12 <= (int)((_DWORD)++v11 - (unsigned int)v16) )
goto LABEL_44;
}
}
fwrite(" Error opening serial port\n", 1uLL, 0x1BuLL, stderr);
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r15
char *v4; // r14
char *v5; // r13
char *v6; // r12
unsigned int v7; // eax
int v8; // eax
unsigned int v9; // er12
int v10; // er14
unsigned __int8 *v11; // r13
int v12; // ebx
char *v14; // [rsp+8h] [rbp-150h]
char v15; // [rsp+1Fh] [rbp-139h] BYREF
__int64 v16[39]; // [rsp+20h] [rbp-138h] BYREF

v16[0] = 0LL;
v16[1] = 0LL;
v16[2] = 0LL;
v16[3] = 0LL;
v16[4] = 0LL;
v16[5] = 0LL;
v16[6] = 0LL;
v16[7] = 0LL;
v16[8] = 0LL;
v16[9] = 0LL;
v16[10] = 0LL;
v16[11] = 0LL;
v16[12] = 0LL;
v16[13] = 0LL;
v16[14] = 0LL;
v16[15] = 0LL;
v16[16] = 0LL;
v16[17] = 0LL;
v16[18] = 0LL;
v16[19] = 0LL;
v16[20] = 0LL;
v16[21] = 0LL;
v16[22] = 0LL;
v16[23] = 0LL;
v16[24] = 0LL;
v16[25] = 0LL;
v16[26] = 0LL;
v16[27] = 0LL;
v16[28] = 0LL;
v16[29] = 0LL;
v16[30] = 0LL;
v16[31] = 0LL;
puts("-------------------------------------------------------");
putchar(10);
puts(" Bus Pirate binary mode SPI SNIFFER utility v0.3 (CC-0)");
puts(" http://dangerousprototypes.com");
putchar(10);
puts("-------------------------------------------------------");
puts("\n\n");
if ( argc <= 1 )
{
puts("ERROR: Invalid argument(s).\n");
puts("Help Menu");
func0(*argv);
exit(-1);
}
v3 = 0LL;
v4 = 0LL;
v14 = 0LL;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "ms:p:e:d:r:");
if ( v7 == -1 )
break;
switch ( v7 )
{
case 'd':
if ( v6 )
{
puts("Device/PORT error!");
exit(-1);
}
v6 = strdup(optarg);
break;
case 'e':
if ( v4 )
{
puts("Clock Edge should be 0 or 1");
exit(-1);
}
v4 = strdup(optarg);
break;
case 'm':
modem = 1;
break;
case 'p':
if ( v14 )
{
puts("Polarity must be 0 or 1");
exit(-1);
}
v14 = strdup(optarg);
break;
case 'r':
if ( v3 )
{
puts("Raw Data should be 0 or 1");
exit(-1);
}
v3 = strdup(optarg);
break;
case 's':
if ( v5 )
{
puts("Speed should be set: eg 115200 ");
exit(-1);
}
v5 = strdup(optarg);
break;
default:
printf(" Invalid argument %c", v7);
func0(*argv);
break;
}
}
puts(" Press escape to exit ");
putchar(10);
if ( !v6 )
{
puts(" No serial port set");
func0(*argv);
exit(-1);
}
if ( !v4 )
v4 = strdup("1");
if ( !v14 )
v14 = strdup("0");
if ( !v5 )
v5 = strdup("115200");
if ( !v3 )
v3 = strdup("0");
puts("\n Parameters used:");
printf(" Device = %s, Speed = %s, Clock Edge= %s, Polarity= %s RawData= %s \n\n", v6, v5, v4, v14, v3);
printf(" Opening Bus Pirate on %s at %sbps...\n", v6, v5);
v8 = serial_open(v6);
v9 = v8;
if ( v8 >= 0 )
{
serial_setup((unsigned int)v8, (unsigned int)v5);
puts(" Starting SPI sniffer...");
if ( modem == 1 )
{
serial_write(v9, "ATI3\r", 5LL);
usleep(1u);
serial_read(v9, v16, 256LL);
printf("\n %s\n", (const char *)v16);
serial_write(v9, "ATI4\r", 5LL);
usleep(1u);
serial_read(v9, v16, 256LL);
printf("\n %s\n", (const char *)v16);
serial_write(v9, "ATI7\r", 5LL);
usleep(1u);
serial_read(v9, v16, 256LL);
printf("\n %s\n", (const char *)v16);
}
else
{
fwrite(" Configuring Bus Pirate...\n", 1uLL, 0x1BuLL, stderr);
BP_EnableMode(v9, 1LL);
v15 = 0x80;
if ( *v4 == 49 )
v15 = -126;
if ( *v14 == 49 )
v15 |= 4u;
BP_WriteToPirate(v9, &v15);
serial_write(v9, &unk_972, 1LL);
}
puts(" (OK) Happy sniffing! Press ESC to stop.");
v10 = 0;
do
{
LABEL_44:
usleep(1u);
v12 = serial_read(v9, v16, 100LL);
}
while ( v12 <= 0 );
v11 = (unsigned __int8 *)v16;
while ( 1 )
{
printf("%02X ", *v11);
if ( *v3 == 49 )
{
printf("%02X ", *v11);
}
else
{
switch ( v10 )
{
case 2:
printf("0x%02X(", *v11);
v10 = 3;
goto LABEL_43;
case 3:
printf("0x%02X)", *v11);
v10 = 1;
goto LABEL_43;
case 1:
v10 = 2;
if ( *v11 == 92 )
goto LABEL_43;
if ( *v11 == 93 )
{
puts("]");
v10 = 0;
goto LABEL_43;
}
break;
default:
if ( *v11 == 91 )
{
putchar(91);
v10 = 1;
goto LABEL_43;
}
break;
}
puts("Sync");
v10 = 0;
}
LABEL_43:
if ( v12 <= (int)((_DWORD)++v11 - (unsigned int)v16) )
goto LABEL_44;
}
}
fwrite(" Error opening serial port\n", 1uLL, 0x1BuLL, stderr);
return -1;
}

original_code:
int main(int argc, char** argv)
 {
  int opt;
   char buffer[256] = {0}, i;
   int fd;
   int res,c;
  int new_state,state=0;
   char *param_port = ((void *)0);
   char *param_speed = ((void *)0);
   char *param_polarity=((void *)0);
   char *param_clockedge=((void *)0);
   char *param_rawdata=((void *)0);
    printf("-------------------------------------------------------\n");
    printf("\n");
    printf(" Bus Pirate binary mode SPI SNIFFER utility v0.3 (CC-0)\n");
    printf(" http://dangerousprototypes.com\n");
    printf("\n");
    printf("-------------------------------------------------------\n");
   printf("\n\n\n");
 if (argc <= 1) {
     printf("ERROR: Invalid argument(s).\n\n");
     printf("Help Menu\n");
  print_usage(argv[0]);
     exit(-1);
  }
while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) {
  switch (opt) {
   case 'd':
    if ( param_port != ((void *)0)){
     printf("Device/PORT error!\n");
     exit(-1);
                }
     param_port = strdup(optarg);
                break;
   case 'e':
     if (param_clockedge != ((void *)0)) {
     printf("Clock Edge should be 0 or 1\n");
     exit(-1);
    }
     param_clockedge = strdup(optarg);
    break;
            case 'p':
    if (param_polarity != ((void *)0)) {
     printf("Polarity must be 0 or 1\n");
     exit(-1);
    }
     param_polarity = strdup(optarg);
     break;
   case 's':
    if (param_speed != ((void *)0)) {
     printf("Speed should be set: eg  115200 \n");
     exit(-1);
    }
     param_speed = strdup(optarg);
    break;
   case 'r':
    if (param_rawdata != ((void *)0)) {
     printf("Raw Data should be 0 or 1\n");
     exit(-1);
    }
    param_rawdata = strdup(optarg);
    break;
  case 'm':
                    modem =1;
     break;
   default:
    printf(" Invalid argument %c", opt);
    print_usage(argv[0]);
       break;
  }
 }
    printf(" Press escape to exit \n");
    printf("\n");
     if (param_port==((void *)0)){
        printf(" No serial port set\n");
  print_usage(argv[0]);
   exit(-1);
     }
     if (param_clockedge==((void *)0))
           param_clockedge=strdup("1");
     if (param_polarity==((void *)0))
           param_polarity=strdup("0");
     if (param_speed==((void *)0))
           param_speed=strdup("115200");
    if (param_rawdata==((void *)0))
         param_rawdata=strdup("0");
    printf("\n Parameters used:\n");
   printf(" Device = %s,  Speed = %s, Clock Edge= %s, Polarity= %s RawData= %s \n\n",param_port,param_speed,param_clockedge,param_polarity,param_rawdata);
    printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed);
  fd = serial_open(param_port);
 if (fd < 0) {
  fprintf(stderr, " Error opening serial port\n");
  return -1;
  }
  serial_setup(fd,(speed_t) param_speed);
  printf(" Starting SPI sniffer...\n");
  if (modem==1){
              serial_write( fd, "ATI3\x0D\0",5 );
           usleep(1);
            res= serial_read(fd, buffer, sizeof(buffer));
            printf("\n %s\n",buffer);
              serial_write( fd, "ATI4\x0D\0",5 );
           usleep(1);
            res= serial_read(fd, buffer, sizeof(buffer));
            printf("\n %s\n",buffer);
               serial_write( fd, "ATI7\x0D\0",5 );
           usleep(1);
            res= serial_read(fd, buffer, sizeof(buffer));
            printf("\n %s\n",buffer);
  }
 else
 {
    fprintf(stderr, " Configuring Bus Pirate...\n");
          BP_EnableMode(fd, 0x01);
    i=0x80;
            if(strncmp(param_clockedge, "1", 1)==0)
        i|=0x02;
            if(strncmp(param_polarity, "1", 1)==0)
        i|=0x04;
            BP_WriteToPirate(fd, &i);
    serial_write( fd, "\x0E", 1);
 }
 printf(" (OK) Happy sniffing! Press ESC to stop.\n");
   while(1){
        usleep(1);
         res= serial_read(fd, buffer, 100);
         if(res>0){
             for(c=0; c<res; c++){
                 printf("%02X ", (uint8_t)buffer[c]);
             if(strncmp(param_rawdata, "1", 1)==0) printf("%02X ", (uint8_t)buffer[c]);
          else {
           switch(state) {
                  default:
                           case 0:
                                if (buffer[c]==0x5B) {
                                    printf("[");
                                    new_state=1;
                                } else {
                                    printf("Sync\n");
                                    new_state=0;
                                }
                                break;
                           case 1:
                                if (buffer[c]==0x5C) {
                                    new_state=2;
    } else if (buffer[c]==0x5D) {
                                        printf("]\n");
                                        new_state=0;
                                    } else {
                                        printf("Sync\n");
                                        new_state=0;
                                        }
                                break;
                            case 2:
                                 printf("0x%02X(", (uint8_t) buffer[c]);
                                 new_state=3;
                                 break;
                            case 3:
                                printf("0x%02X)", (uint8_t) buffer[c]);
                                new_state=1;
                                break;
                            }
                state=new_state;
                }
             }
        }
    }
 if(param_port) free(param_port);;
  if(param_speed) free(param_speed);;
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
 {
  int opt;
   char buffer[256] = {0}; char i;
   int fd;
   int res; int c;
  int new_state; int state = 0;
   char *param_port = ((void *)0);
   char *param_speed = ((void *)0);
   char *param_polarity = ((void *)0);
   char *param_clockedge = ((void *)0);
   char *param_rawdata = ((void *)0);
    printf("-------------------------------------------------------\n");
    printf("\n");
    printf(" Bus Pirate binary mode SPI SNIFFER utility v0.3 (CC-0)\n");
    printf(" http://dangerousprototypes.com\n");
    printf("\n");
    printf("-------------------------------------------------------\n");
   printf("\n\n\n");
 if (argc <= 1) {
     printf("ERROR: Invalid argument(s).\n\n");
     printf("Help Menu\n");
  print_usage(argv[0]);
     exit(-1);
  }
while ((opt = getopt(argc, argv, "ms:p:e:d:r:")) != -1) {
  switch (opt) {
   case 'd':
    if ( param_port != ((void *)0)){
     printf("Device/PORT error!\n");
     exit(-1);
                }
     param_port = strdup(optarg);
                break;
   case 'e':
     if (param_clockedge != ((void *)0)) {
     printf("Clock Edge should be 0 or 1\n");
     exit(-1);
    }
     param_clockedge = strdup(optarg);
    break;
            case 'p':
    if (param_polarity != ((void *)0)) {
     printf("Polarity must be 0 or 1\n");
     exit(-1);
    }
     param_polarity = strdup(optarg);
     break;
   case 's':
    if (param_speed != ((void *)0)) {
     printf("Speed should be set: eg  115200 \n");
     exit(-1);
    }
     param_speed = strdup(optarg);
    break;
   case 'r':
    if (param_rawdata != ((void *)0)) {
     printf("Raw Data should be 0 or 1\n");
     exit(-1);
    }
    param_rawdata = strdup(optarg);
    break;
  case 'm':
                    modem =1;
     break;
   default:
    printf(" Invalid argument %c", opt);
    print_usage(argv[0]);
       break;
  }
 }
    printf(" Press escape to exit \n");
    printf("\n");
     if (param_port==((void *)0)){
        printf(" No serial port set\n");
  print_usage(argv[0]);
   exit(-1);
     }
     if (param_clockedge==((void *)0))
           param_clockedge=strdup("1");
     if (param_polarity==((void *)0))
           param_polarity=strdup("0");
     if (param_speed==((void *)0))
           param_speed=strdup("115200");
    if (param_rawdata==((void *)0))
         param_rawdata=strdup("0");
    printf("\n Parameters used:\n");
   printf(" Device = %s,  Speed = %s, Clock Edge= %s, Polarity= %s RawData= %s \n\n",param_port,param_speed,param_clockedge,param_polarity,param_rawdata);
    printf(" Opening Bus Pirate on %s at %sbps...\n", param_port, param_speed);
  fd = serial_open(param_port);
 if (fd < 0) {
  fprintf(stderr, " Error opening serial port\n");
  return -1;
  }
  serial_setup(fd,(unsigned int) param_speed);
  printf(" Starting SPI sniffer...\n");
  if (modem==1){
              serial_write( fd, "ATI3\x0D\0",5 );
           usleep(1);
            res= serial_read(fd, buffer, sizeof(buffer));
            printf("\n %s\n",buffer);
              serial_write( fd, "ATI4\x0D\0",5 );
           usleep(1);
            res= serial_read(fd, buffer, sizeof(buffer));
            printf("\n %s\n",buffer);
               serial_write( fd, "ATI7\x0D\0",5 );
           usleep(1);
            res= serial_read(fd, buffer, sizeof(buffer));
            printf("\n %s\n",buffer);
  }
 else
 {
    fprintf(stderr, " Configuring Bus Pirate...\n");
          BP_EnableMode(fd, 0x01);
    i=0x80;
            if(strncmp(param_clockedge, "1", 1)==0)
        i|=0x02;
            if(strncmp(param_polarity, "1", 1)==0)
        i|=0x04;
            BP_WriteToPirate(fd, &i);
    serial_write( fd, "\x0E", 1);
 }
 printf(" (OK) Happy sniffing! Press ESC to stop.\n");
   while(1){
        usleep(1);
         res= serial_read(fd, buffer, 100);
         if(res>0){
             for(c=0; c<res; c++){
                 printf("%02X ", (unsigned char)buffer[c]);
             if(strncmp(param_rawdata, "1", 1)==0) printf("%02X ", (unsigned char)buffer[c]);
          else {
           switch(state) {
                  default:
                           case 0:
                                if (buffer[c]==0x5B) {
                                    printf("[");
                                    new_state=1;
                                } else {
                                    printf("Sync\n");
                                    new_state=0;
                                }
                                break;
                           case 1:
                                if (buffer[c]==0x5C) {
                                    new_state=2;
    } else if (buffer[c]==0x5D) {
                                        printf("]\n");
                                        new_state=0;
                                    } else {
                                        printf("Sync\n");
                                        new_state=0;
                                        }
                                break;
                            case 2:
                                 printf("0x%02X(", (unsigned char) buffer[c]);
                                 new_state=3;
                                 break;
                            case 3:
                                printf("0x%02X)", (unsigned char) buffer[c]);
                                new_state=1;
                                break;
                            }
                state=new_state;
                }
             }
        }
    }
 if(param_port) free(param_port);;
  if(param_speed) free(param_speed);;
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types:
buffer
char [256]

variable_types: i char

variable_types: c int

variable_types: state int


decompiled_code:
__int64 __fastcall serial_setup(int fd, speed_t speed)
{
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

fcntl(fd, 4, 0LL);
tcgetattr(fd, &v3);
cfsetispeed(&v3, speed);
cfsetospeed(&v3, speed);
*(_QWORD *)&v3.c_cflag = __PAIR64__(v3.c_lflag, v3.c_cflag & 0xFFFFFE8F | 0x8B0) & 0xFFFFFFE4FFFFFFFFLL;
v3.c_iflag &= 0xFFFFE3FF;
v3.c_oflag &= 0xFFFFFFFE;
v3.c_cc[6] = 0;
v3.c_cc[5] = 1;
tcflush(fd, 0);
tcsetattr(fd, 0, &v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, speed_t speed)
{
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

fcntl(fd, 4, 0LL);
tcgetattr(fd, &v3);
cfsetispeed(&v3, speed);
cfsetospeed(&v3, speed);
*(_QWORD *)&v3.c_cflag = __PAIR64__(v3.c_lflag, v3.c_cflag & 0xFFFFFE8F | 0x8B0) & 0xFFFFFFE4FFFFFFFFLL;
v3.c_iflag &= 0xFFFFE3FF;
v3.c_oflag &= 0xFFFFFFFE;
v3.c_cc[6] = 0;
v3.c_cc[5] = 1;
tcflush(fd, 0);
tcsetattr(fd, 0, &v3);
return 0LL;
}

original_code:
int serial_setup(int fd, speed_t speed)
{
 struct termios t_opt;
 fcntl(fd, 4, 0);
 tcgetattr(fd, &t_opt);
 cfsetispeed(&t_opt, speed);
 cfsetospeed(&t_opt, speed);
 t_opt.c_cflag |= (0004000 | 0000200);
 t_opt.c_cflag &= ~0000400;
 t_opt.c_cflag &= ~0000100;
 t_opt.c_cflag &= ~0000060;
 t_opt.c_cflag |= 0000060;
 t_opt.c_lflag &= ~(0000002 | 0000010 | 0000020 | 0000001);
 t_opt.c_iflag &= ~(0002000 | 0010000 | 0004000);
 t_opt.c_oflag &= ~0000001;
 t_opt.c_cc[6] = 0;
 t_opt.c_cc[5] = 1;
 tcflush(fd, 0);
 tcsetattr(fd, 0, &t_opt);
 return 0;
}

canonical_original_code:
int serial_setup(int fd, unsigned int speed)
{
 struct termios t_opt;
 fcntl(fd, 4, 0);
 tcgetattr(fd, &t_opt);
 cfsetispeed(&t_opt, speed);
 cfsetospeed(&t_opt, speed);
 t_opt.c_cflag |= (0004000 | 0000200);
 t_opt.c_cflag &= ~0000400;
 t_opt.c_cflag &= ~0000100;
 t_opt.c_cflag &= ~0000060;
 t_opt.c_cflag |= 0000060;
 t_opt.c_lflag &= ~(0000002 | 0000010 | 0000020 | 0000001);
 t_opt.c_iflag &= ~(0002000 | 0010000 | 0004000);
 t_opt.c_oflag &= ~0000001;
 t_opt.c_cc[6] = 0;
 t_opt.c_cc[5] = 1;
 tcflush(fd, 0);
 tcsetattr(fd, 0, &t_opt);
 return 0;
}

variable_types: fd int

variable_types:
speed
unsigned int

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


decompiled_code:
__int64 __fastcall serial_write(int a1, const void *a2, int a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx

v4 = write(a1, a2, a3);
v5 = v4;
if ( disable_comport != 1 && a3 != v4 )
fwrite("Error sending data", 1uLL, 0x12uLL, stderr);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, const void *a2, int a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx

v4 = write(a1, a2, a3);
v5 = v4;
if ( disable_comport != 1 && a3 != v4 )
fwrite("Error sending data", 1uLL, 0x12uLL, stderr);
return v5;
}

original_code:
int serial_write(int fd, char *buf, int size)
{
 int ret = 0;
 ret = write(fd, buf, size);
    if (disable_comport !=1)
    {
     if (ret != size)
      fprintf(stderr, "Error sending data");
     return ret;
    }
    return ret;
}

variable_types:
buf
char *

variable_types: size int


decompiled_code:
__int64 __fastcall serial_read(int fd, __int64 a2, int a3)
{
int v4; // er14
unsigned int v5; // ebx
int v6; // eax

if ( a3 <= 0 )
return 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = read(fd, (void *)(a2 + (int)v5), (int)(a3 - v5));
if ( v6 == -1 )
break;
if ( v6 )
{
v5 += v6;
}
else if ( ++v4 > 9 )
{
return v5;
}
if ( a3 <= (int)v5 )
return v5;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2, int a3)
{
int v4; // er14
unsigned int v5; // ebx
int v6; // eax

if ( a3 <= 0 )
return 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = read(fd, (void *)(a2 + (int)v5), (int)(a3 - v5));
if ( v6 == -1 )
break;
if ( v6 )
{
v5 += v6;
}
else if ( ++v4 > 9 )
{
return v5;
}
if ( a3 <= (int)v5 )
return v5;
}
return (unsigned int)-1;
}

original_code:
int serial_read(int fd, char *buf, int size)
{
 int len = 0;
 int ret = 0;
 int timeout = 0;
 while (len < size) {
  ret = read(fd, buf+len, size-len);
  if (ret == -1){
   return -1;
  }
  if (ret == 0) {
   timeout++;
   if (timeout >= 10)
    break;
   continue;
  }
  len += ret;
 }
if (disable_comport!=1)
{
 return len;
}
else
return len;
}

variable_types: len int

variable_types: ret int

variable_types: timeout int


decompiled_code:
__int64 __fastcall serial_open(const char *a1)
{
unsigned int v1; // ebx

v1 = open(a1, 258);
if ( v1 == -1 )
fwrite("Could not open serial port.", 1uLL, 0x1BuLL, stderr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
unsigned int v1; // ebx

v1 = open(a1, 258);
if ( v1 == -1 )
fwrite("Could not open serial port.", 1uLL, 0x1BuLL, stderr);
return v1;
}

original_code:
int serial_open(char *port)
{
 int fd;
 fd = open(port, 02 | 0400);
 if (fd == -1) {
  fprintf(stderr, "Could not open serial port.");
  return -1;
 }
 return fd;
}

variable_types:
port
char *


decompiled_code:
__int64 __fastcall serial_close(int a1)
{
close(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
close(a1);
return 0LL;
}

original_code:
int serial_close(int fd)
{
 close(fd);
 return 0;
}


xmalloc

repo: JuEeHa/minimake

decompiled_code:
void *__fastcall xmalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fprintf(stderr, "make: can't allocate %u bytes\n", a1);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fprintf(stderr, "make: can't allocate %u bytes\n", a1);
exit(1);
}
return result;
}

original_code:
void*
xmalloc(size_t size) {
 void *ret;
 if((ret=malloc(size)) == 0) {
  fprintf(stderr, "make: can't allocate %u bytes\n", size);
  exit(1);
 }
 return ret;
}

canonical_original_code:
void *xmalloc(unsigned long size) {
 void *ret;
 if((ret=malloc(size)) == 0) {
  fprintf(stderr, "make: can't allocate %u bytes\n", size);
  exit(1);
 }
 return ret;
}

variable_types:
size
unsigned long

variable_types:
ret
void *

return_type: void *


xrealloc

decompiled_code:
void *__fastcall xrealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
fprintf(stderr, "make: can't reallocate %p to %u bytes", a1, a2);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
fprintf(stderr, "make: can't reallocate %p to %u bytes", a1, a2);
exit(1);
}
return result;
}

original_code:
void*
xrealloc(void *ptr, size_t size) {
 void *ret;
 if((ret=realloc(ptr, size)) == 0) {
  fprintf(stderr, "make: can't reallocate %p to %u bytes", ptr, size);
  exit(1);
 }
 return ret;
}

canonical_original_code:
void *xrealloc(void *ptr, unsigned long size) {
 void *ret;
 if((ret=realloc(ptr, size)) == 0) {
  fprintf(stderr, "make: can't reallocate %p to %u bytes", ptr, size);
  exit(1);
 }
 return ret;
}

variable_types:
ptr
void *


readtobuf

decompiled_code:
__int64 __fastcall readtobuf(int fd)
{
int v1; // ebx
unsigned int v2; // er12
__int64 v3; // rbp
unsigned int v4; // eax
unsigned int v5; // er14
int v6; // eax

filebuf = xmalloc(0x400uLL);
v1 = 0;
v2 = 1024;
while ( 1 )
{
v3 = v1;
v4 = read(fd, (char *)filebuf + v1, 0x400uLL);
v5 = v4;
if ( v4 + 1 <= 1 )
break;
v1 += v4;
if ( v1 + 1024 > v2 )
{
v6 = v1 + 1535;
if ( v1 + 512 >= 0 )
v6 = v1 + 512;
v2 = (v6 & 0xFFFFFC00) + 1024;
filebuf = xrealloc(filebuf, v2);
}
}
if ( v4 == -1 )
{
free(filebuf);
}
else
{
if ( *((char *)filebuf + v1 - 1) != 10 )
{
filebuf = xrealloc(filebuf, ++v1);
*((_BYTE *)filebuf + v3) = 10;
}
filebuflen = v1;
v5 = 0;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
int v1; // ebx
unsigned int v2; // er12
__int64 v3; // rbp
unsigned int v4; // eax
unsigned int v5; // er14
int v6; // eax

filebuf = func0(0x400uLL);
v1 = 0;
v2 = 1024;
while ( 1 )
{
v3 = v1;
v4 = read(fd, (char *)filebuf + v1, 0x400uLL);
v5 = v4;
if ( v4 + 1 <= 1 )
break;
v1 += v4;
if ( v1 + 1024 > v2 )
{
v6 = v1 + 1535;
if ( v1 + 512 >= 0 )
v6 = v1 + 512;
v2 = (v6 & 0xFFFFFC00) + 1024;
filebuf = func1(filebuf, v2);
}
}
if ( v4 == -1 )
{
free(filebuf);
}
else
{
if ( *((char *)filebuf + v1 - 1) != 10 )
{
filebuf = func1(filebuf, ++v1);
*((_BYTE *)filebuf + v3) = 10;
}
filebuflen = v1;
v5 = 0;
}
return v5;
}

original_code:
int
readtobuf(int f) {
 unsigned int bufsize;
 int w, i;
 bufsize=1024;
 filebuf=xmalloc(bufsize);
 for(i=0; (w=read(f, filebuf+i, 1024)) && w!=-1; i+=w)
  if(i+w+1024>bufsize) {
   bufsize=((i+w+512)/1024+1)*1024;
   filebuf=xrealloc(filebuf, bufsize);
  }
 if(w==-1) {
  free(filebuf);
  return -1;
 }
 i+=w;
 if(filebuf[i-1]!='\n') {
  filebuf=xrealloc(filebuf, i+1);
  filebuf[i]='\n';
  i++;
 }
 filebuflen=i;
 return 0;
}

canonical_original_code:
int readtobuf(int f) {
 unsigned int bufsize;
 int w; int i;
 bufsize=1024;
 filebuf=xmalloc(bufsize);
 for(i=0; (w=read(f, filebuf+i, 1024)) && w!=-1; i+=w)
  if(i+w+1024>bufsize) {
   bufsize=((i+w+512)/1024+1)*1024;
   filebuf=xrealloc(filebuf, bufsize);
  }
 if(w==-1) {
  free(filebuf);
  return -1;
 }
 i+=w;
 if(filebuf[i-1]!='\n') {
  filebuf=xrealloc(filebuf, i+1);
  filebuf[i]='\n';
  i++;
 }
 filebuflen=i;
 return 0;
}

variable_types: f int

variable_types:
bufsize
unsigned int

variable_types: w int

variable_types: i int


parseline

decompiled_code:
int __fastcall parseline(void *src, _DWORD *a2, __int64 a3, unsigned int a4)
{
unsigned __int64 v6; // r13
_QWORD *v7; // rax
bool v8; // si
__int64 v9; // rax
_QWORD *v10; // rdx
int v11; // ebx
_QWORD *v12; // rbp
_QWORD *v13; // rax
int v14; // ebp
__int64 v15; // rbx
_QWORD *v16; // r13
__m128i *v17; // rax
unsigned int v18; // ebx
size_t v19; // rbp
unsigned int v20; // ebx
void *v21; // r13
char *v22; // rbp
char v23; // r13
_QWORD *v24; // rax
int v25; // er13
char v26; // di
unsigned int i; // er13
void *v28; // r14
_BYTE *v29; // rbp
_QWORD *v30; // rdx
_QWORD *v31; // rax
char v32; // r13
int v33; // eax
unsigned int v34; // ebx
void *v35; // r15
int v36; // er13
__int64 v37; // rbp
void **v38; // r14
_QWORD *v39; // r14
char *v40; // r14
char j; // di
__int64 v42; // rbx

if ( *(_BYTE *)src == 9 )
{
if ( (unsigned int)(*a2 - 1) > 1 )
{
fprintf(stderr, "make: %u: unexpected tab\n", a4);
exit(1);
}
v10 = *(_QWORD **)(a3 + 24);
if ( v10 )
{
if ( *v10 )
{
v11 = 0;
do
v12 = &v10[++v11];
while ( *v12 );
}
else
{
v12 = v10;
v11 = 0;
}
}
else
{
v12 = xmalloc(8uLL);
*(_QWORD *)(a3 + 24) = v12;
*v12 = 0LL;
v11 = 0;
}
*v12 = preprocess((char *)src + 1, a4);
v13 = xrealloc(*(void **)(a3 + 24), 8LL * (unsigned int)(v11 + 2));
*(_QWORD *)(a3 + 24) = v13;
v13[v11 + 1] = 0LL;
*a2 = 2;
}
else if ( (unsigned int)(*a2 - 1) <= 1 )
{
if ( targetlist )
{
if ( *(_QWORD *)targetlist )
{
v14 = 0;
do
{
v15 = 8LL * (unsigned int)++v14;
v16 = (_QWORD *)(targetlist + v15);
}
while ( *(_QWORD *)(targetlist + v15) );
}
else
{
v16 = (_QWORD *)targetlist;
v14 = 0;
v15 = 0LL;
}
}
else
{
v16 = xmalloc(8uLL);
targetlist = (__int64)v16;
*v16 = 0LL;
v14 = 0;
v15 = 0LL;
}
*v16 = xmalloc(0x20uLL);
v17 = *(__m128i **)(targetlist + v15);
*v17 = _mm_loadu_si128((const __m128i *)a3);
v17[1] = _mm_loadu_si128((const __m128i *)(a3 + 16));
targetlist = (__int64)xrealloc((void *)targetlist, 8LL * (unsigned int)(v14 + 2));
*(_QWORD *)(targetlist + 8LL * (unsigned int)(v14 + 1)) = 0LL;
*(_QWORD *)a3 = 0LL;
*(_QWORD *)(a3 + 8) = 0LL;
*(_QWORD *)(a3 + 16) = 0LL;
*(_QWORD *)(a3 + 24) = 0LL;
}
LODWORD(v6) = *(unsigned __int8 *)src;
LODWORD(v7) = isalnum((char)v6);
v8 = (_DWORD)v7 != 0;
if ( (unsigned __int8)(v6 - 36) <= 0x3Bu )
{
v9 = 0x800000000000403LL;
if ( !_bittest64(&v9, (unsigned int)(v6 - 36)) && !v8 )
{
LABEL_6:
fprintf(stderr, "make: %u: cannot parse\n", a4);
exit(1);
}
LABEL_24:
if ( (unsigned __int8)v6 > 0x3Fu )
{
v18 = 0;
}
else
{
v7 = (_QWORD *)0xA400080000000000LL;
if ( _bittest64((const __int64 *)&v7, (unsigned int)v6) )
{
v18 = 0;
v19 = 0LL;
LABEL_32:
if ( (_BYTE)v6 == 58 )
{
v20 = v18 + 1;
v21 = xmalloc(v20);
memcpy(v21, src, v19);
*((_BYTE *)v21 + v19) = 0;
*(_QWORD *)(a3 + 8) = preprocess((char *)v21, a4);
free(v21);
while ( 1 )
{
v22 = (char *)src + v20;
v23 = *v22;
if ( !isblank(*v22) )
break;
++v20;
}
if ( v23 )
{
do
{
v26 = *((_BYTE *)src + v20);
for ( i = 0; v26; v26 = *((_BYTE *)src + ++v20) )
{
if ( isblank(v26) )
break;
++i;
}
v28 = xmalloc(i + 1);
memcpy(v28, v22, i);
*((_BYTE *)v28 + i) = 0;
v29 = preprocess((char *)v28, a4);
free(v28);
v30 = *(_QWORD **)(a3 + 16);
if ( v30 )
{
if ( *v30 )
{
v25 = 0;
do
v24 = &v30[++v25];
while ( *v24 );
}
else
{
v24 = *(_QWORD **)(a3 + 16);
v25 = 0;
}
}
else
{
v24 = xmalloc(8uLL);
*(_QWORD *)(a3 + 16) = v24;
*v24 = 0LL;
v25 = 0;
}
*v24 = v29;
free(v29);
v31 = xrealloc(*(void **)(a3 + 16), 8LL * (unsigned int)(v25 + 2));
*(_QWORD *)(a3 + 16) = v31;
v31[v25 + 1] = 0LL;
while ( 1 )
{
v22 = (char *)src + v20;
v32 = *v22;
v33 = isblank(*v22);
if ( !v32 || !v33 )
break;
++v20;
}
}
while ( v32 );
}
LODWORD(v7) = (_DWORD)a2;
*a2 = 1;
}
else if ( (_BYTE)v6 == 61 )
{
if ( !vars )
{
vars = (__int64)xmalloc(8uLL);
*(_QWORD *)vars = 0LL;
}
v34 = v18 + 1;
v35 = xmalloc(v34);
memcpy(v35, src, v19);
*((_BYTE *)v35 + v19) = 0;
if ( *(_QWORD *)vars )
{
v36 = 0;
do
{
v37 = 8LL * (unsigned int)++v36;
v38 = (void **)(vars + v37);
}
while ( *(_QWORD *)(vars + v37) );
}
else
{
v38 = (void **)vars;
v36 = 0;
v37 = 0LL;
}
*v38 = xmalloc(0x10uLL);
v39 = *(_QWORD **)(vars + v37);
*v39 = preprocess((char *)v35, a4);
free(v35);
v40 = (char *)src + v34;
for ( j = *v40; *v40; j = *v40 )
{
if ( !isblank(j) )
break;
v40 = (char *)src + ++v34;
}
v42 = *(_QWORD *)(vars + v37);
*(_QWORD *)(v42 + 8) = preprocess(v40, a4);
v7 = xrealloc((void *)vars, 8LL * (unsigned int)(v36 + 2));
vars = (__int64)v7;
v7[v36 + 1] = 0LL;
}
return (int)v7;
}
v18 = 0;
}
v7 = (_QWORD *)0xA400080000000000LL;
do
{
if ( !(_BYTE)v6 )
{
fprintf(stderr, "make: %u: missing ':', '=', '?=' or '+='\n", a4);
exit(1);
}
v19 = ++v18;
v6 = *((unsigned __int8 *)src + v18);
}
while ( (unsigned __int8)v6 > 0x3Fu || !_bittest64((const __int64 *)&v7, v6) );
goto LABEL_32;
}
if ( (_DWORD)v7 )
goto LABEL_24;
if ( !(_BYTE)v6 || (_BYTE)v6 == 35 )
{
LODWORD(v7) = (_DWORD)a2;
*a2 = 0;
}
else if ( (_BYTE)v6 != 9 )
{
goto LABEL_6;
}
return (int)v7;
}

canonical_decompiled_code:
int __fastcall func3(void *src, _DWORD *a2, __int64 a3, unsigned int a4)
{
unsigned __int64 v6; // r13
_QWORD *v7; // rax
bool v8; // si
__int64 v9; // rax
_QWORD *v10; // rdx
int v11; // ebx
_QWORD *v12; // rbp
_QWORD *v13; // rax
int v14; // ebp
__int64 v15; // rbx
_QWORD *v16; // r13
__m128i *v17; // rax
unsigned int v18; // ebx
size_t v19; // rbp
unsigned int v20; // ebx
void *v21; // r13
char *v22; // rbp
char v23; // r13
_QWORD *v24; // rax
int v25; // er13
char v26; // di
unsigned int i; // er13
void *v28; // r14
_BYTE *v29; // rbp
_QWORD *v30; // rdx
_QWORD *v31; // rax
char v32; // r13
int v33; // eax
unsigned int v34; // ebx
void *v35; // r15
int v36; // er13
__int64 v37; // rbp
void **v38; // r14
_QWORD *v39; // r14
char *v40; // r14
char j; // di
__int64 v42; // rbx

if ( *(_BYTE *)src == 9 )
{
if ( (unsigned int)(*a2 - 1) > 1 )
{
fprintf(stderr, "make: %u: unexpected tab\n", a4);
exit(1);
}
v10 = *(_QWORD **)(a3 + 24);
if ( v10 )
{
if ( *v10 )
{
v11 = 0;
do
v12 = &v10[++v11];
while ( *v12 );
}
else
{
v12 = v10;
v11 = 0;
}
}
else
{
v12 = func0(8uLL);
*(_QWORD *)(a3 + 24) = v12;
*v12 = 0LL;
v11 = 0;
}
*v12 = preprocess((char *)src + 1, a4);
v13 = func1(*(void **)(a3 + 24), 8LL * (unsigned int)(v11 + 2));
*(_QWORD *)(a3 + 24) = v13;
v13[v11 + 1] = 0LL;
*a2 = 2;
}
else if ( (unsigned int)(*a2 - 1) <= 1 )
{
if ( targetlist )
{
if ( *(_QWORD *)targetlist )
{
v14 = 0;
do
{
v15 = 8LL * (unsigned int)++v14;
v16 = (_QWORD *)(targetlist + v15);
}
while ( *(_QWORD *)(targetlist + v15) );
}
else
{
v16 = (_QWORD *)targetlist;
v14 = 0;
v15 = 0LL;
}
}
else
{
v16 = func0(8uLL);
targetlist = (__int64)v16;
*v16 = 0LL;
v14 = 0;
v15 = 0LL;
}
*v16 = func0(0x20uLL);
v17 = *(__m128i **)(targetlist + v15);
*v17 = _mm_loadu_si128((const __m128i *)a3);
v17[1] = _mm_loadu_si128((const __m128i *)(a3 + 16));
targetlist = (__int64)func1((void *)targetlist, 8LL * (unsigned int)(v14 + 2));
*(_QWORD *)(targetlist + 8LL * (unsigned int)(v14 + 1)) = 0LL;
*(_QWORD *)a3 = 0LL;
*(_QWORD *)(a3 + 8) = 0LL;
*(_QWORD *)(a3 + 16) = 0LL;
*(_QWORD *)(a3 + 24) = 0LL;
}
LODWORD(v6) = *(unsigned __int8 *)src;
LODWORD(v7) = isalnum((char)v6);
v8 = (_DWORD)v7 != 0;
if ( (unsigned __int8)(v6 - 36) <= 0x3Bu )
{
v9 = 0x800000000000403LL;
if ( !_bittest64(&v9, (unsigned int)(v6 - 36)) && !v8 )
{
LABEL_6:
fprintf(stderr, "make: %u: cannot parse\n", a4);
exit(1);
}
LABEL_24:
if ( (unsigned __int8)v6 > 0x3Fu )
{
v18 = 0;
}
else
{
v7 = (_QWORD *)0xA400080000000000LL;
if ( _bittest64((const __int64 *)&v7, (unsigned int)v6) )
{
v18 = 0;
v19 = 0LL;
LABEL_32:
if ( (_BYTE)v6 == 58 )
{
v20 = v18 + 1;
v21 = func0(v20);
memcpy(v21, src, v19);
*((_BYTE *)v21 + v19) = 0;
*(_QWORD *)(a3 + 8) = preprocess((char *)v21, a4);
free(v21);
while ( 1 )
{
v22 = (char *)src + v20;
v23 = *v22;
if ( !isblank(*v22) )
break;
++v20;
}
if ( v23 )
{
do
{
v26 = *((_BYTE *)src + v20);
for ( i = 0; v26; v26 = *((_BYTE *)src + ++v20) )
{
if ( isblank(v26) )
break;
++i;
}
v28 = func0(i + 1);
memcpy(v28, v22, i);
*((_BYTE *)v28 + i) = 0;
v29 = preprocess((char *)v28, a4);
free(v28);
v30 = *(_QWORD **)(a3 + 16);
if ( v30 )
{
if ( *v30 )
{
v25 = 0;
do
v24 = &v30[++v25];
while ( *v24 );
}
else
{
v24 = *(_QWORD **)(a3 + 16);
v25 = 0;
}
}
else
{
v24 = func0(8uLL);
*(_QWORD *)(a3 + 16) = v24;
*v24 = 0LL;
v25 = 0;
}
*v24 = v29;
free(v29);
v31 = func1(*(void **)(a3 + 16), 8LL * (unsigned int)(v25 + 2));
*(_QWORD *)(a3 + 16) = v31;
v31[v25 + 1] = 0LL;
while ( 1 )
{
v22 = (char *)src + v20;
v32 = *v22;
v33 = isblank(*v22);
if ( !v32 || !v33 )
break;
++v20;
}
}
while ( v32 );
}
LODWORD(v7) = (_DWORD)a2;
*a2 = 1;
}
else if ( (_BYTE)v6 == 61 )
{
if ( !vars )
{
vars = (__int64)func0(8uLL);
*(_QWORD *)vars = 0LL;
}
v34 = v18 + 1;
v35 = func0(v34);
memcpy(v35, src, v19);
*((_BYTE *)v35 + v19) = 0;
if ( *(_QWORD *)vars )
{
v36 = 0;
do
{
v37 = 8LL * (unsigned int)++v36;
v38 = (void **)(vars + v37);
}
while ( *(_QWORD *)(vars + v37) );
}
else
{
v38 = (void **)vars;
v36 = 0;
v37 = 0LL;
}
*v38 = func0(0x10uLL);
v39 = *(_QWORD **)(vars + v37);
*v39 = preprocess((char *)v35, a4);
free(v35);
v40 = (char *)src + v34;
for ( j = *v40; *v40; j = *v40 )
{
if ( !isblank(j) )
break;
v40 = (char *)src + ++v34;
}
v42 = *(_QWORD *)(vars + v37);
*(_QWORD *)(v42 + 8) = preprocess(v40, a4);
v7 = func1((void *)vars, 8LL * (unsigned int)(v36 + 2));
vars = (__int64)v7;
v7[v36 + 1] = 0LL;
}
return (int)v7;
}
v18 = 0;
}
v7 = (_QWORD *)0xA400080000000000LL;
do
{
if ( !(_BYTE)v6 )
{
fprintf(stderr, "make: %u: missing ':', '=', '?=' or '+='\n", a4);
exit(1);
}
v19 = ++v18;
v6 = *((unsigned __int8 *)src + v18);
}
while ( (unsigned __int8)v6 > 0x3Fu || !_bittest64((const __int64 *)&v7, v6) );
goto LABEL_32;
}
if ( (_DWORD)v7 )
goto LABEL_24;
if ( !(_BYTE)v6 || (_BYTE)v6 == 35 )
{
LODWORD(v7) = (_DWORD)a2;
*a2 = 0;
}
else if ( (_BYTE)v6 != 9 )
{
goto LABEL_6;
}
return (int)v7;
}

original_code:
void
parseline(char *start, unsigned int *mode, struct target *t, unsigned int line) {
 unsigned int i, j, len;
 char *name, *ppdname, *dep;
 ;
 if(*start=='\t') {
  ;
  if(*mode==1 || *mode==2) {
   if(!(*t).cmds) {
    ;
    (*t).cmds=xmalloc(sizeof(char*));
    (*t).cmds[0]=0;
   }
   ;
   for(i=0; (*t).cmds[i]; i++) ;
   (*t).cmds[i]=preprocess(start+1, line);
   (*t).cmds=xrealloc((*t).cmds, (i+2)*sizeof(char*));
   (*t).cmds[i+1]=0;
  } else {
   fprintf(stderr, "make: %u: unexpected tab\n", line);
   exit(1);
  }
  *mode=2;
 } else if(*mode==2 || *mode==1) {
  ;
  if(!targetlist) {
   targetlist=xmalloc(sizeof(struct target*));
   targetlist[0]=0;
  }
  for(i=0; targetlist[i]; i++);
  targetlist[i]=xmalloc(sizeof(struct target));
  memcpy(targetlist[i], t, sizeof(struct target));
  targetlist=xrealloc(targetlist, (i+2)*sizeof(struct target*));
  targetlist[i+1]=0;
  memset(t, 0, sizeof(struct target));
 }
 if(isalnum(*start) || *start=='.' || *start=='_' || *start=='%' || *start=='$') {
  ;
  for(i=0; start[i]!=':' && start[i]!='=' && start[i]!='?' && start[i]!='+'; i++)
   if(!start[i]) {
    fprintf(stderr, "make: %u: missing ':', '=', '?=' or '+='\n", line);
    exit(1);
   }
  if(start[i]==':') {
   ;
   name=xmalloc(i+1);
   memcpy(name, start, i);
   name[i]=0;
   (*t).name=preprocess(name, line);
   free(name);
   i++;
   while(isblank(start[i])) i++;
   while(start[i]) {
    ;
    dep=start+i;
    for(len=0; start[i] && !isblank(start[i]); len++, i++);
    name=xmalloc(len+1);
    memcpy(name, dep, len);
    name[len]=0;
    ppdname=preprocess(name, line);
    free(name);
    if(!(*t).deps) {
     (*t).deps=xmalloc(sizeof(char*));
     (*t).deps[0]=0;
    }
    for(j=0; (*t).deps[j]; j++);
    (*t).deps[j]=ppdname;
    free(ppdname);
    (*t).deps=xrealloc((*t).deps, (j+2)*sizeof(char*));
    (*t).deps[j+1]=0;
    while(isblank(start[i]) && start[i]) i++;
   }
   *mode=1;
  } else if (start[i]=='=') {
   if(!vars) {
    vars=xmalloc(sizeof(char**));
    vars[0]=0;
   }
   name=xmalloc(i+1);
   memcpy(name, start, i);
   name[i]=0;
   for(j=0; vars[j]; j++);
   vars[j]=xmalloc(2*sizeof(char*));
   vars[j][0]=preprocess(name, line);
   free(name);
   for(i++; start[i] && isblank(start[i]); i++);
   vars[j][1]=preprocess(start+i, line);
   vars=xrealloc(vars, (j+2)*sizeof(char**));
   vars[j+1]=0;
  }
 } else if(!*start || *start=='#') {
  *mode=0;
 } else if(*start!='\t') {
  fprintf(stderr, "make: %u: cannot parse\n", line);
  exit(1);
 }
}

canonical_original_code:
void parseline(char *start, unsigned int *mode, struct target *t, unsigned int line) {
 unsigned int i; unsigned int j; unsigned int len;
 char *name; char *ppdname; char *dep;
 ;
 if(*start=='\t') {
  ;
  if(*mode==1 || *mode==2) {
   if(!(*t).cmds) {
    ;
    (*t).cmds=xmalloc(sizeof(char *));
    (*t).cmds[0]=0;
   }
   ;
   for(i=0; (*t).cmds[i]; i++) ;
   (*t).cmds[i]=preprocess(start+1, line);
   (*t).cmds=xrealloc((*t).cmds, (i+2)*sizeof(char *));
   (*t).cmds[i+1]=0;
  } else {
   fprintf(stderr, "make: %u: unexpected tab\n", line);
   exit(1);
  }
  *mode=2;
 } else if(*mode==2 || *mode==1) {
  ;
  if(!targetlist) {
   targetlist=xmalloc(sizeof(struct target *));
   targetlist[0]=0;
  }
  for(i=0; targetlist[i]; i++);
  targetlist[i]=xmalloc(sizeof(struct target));
  memcpy(targetlist[i], t, sizeof(struct target));
  targetlist=xrealloc(targetlist, (i+2)*sizeof(struct target *));
  targetlist[i+1]=0;
  memset(t, 0, sizeof(struct target));
 }
 if(isalnum(*start) || *start=='.' || *start=='_' || *start=='%' || *start=='$') {
  ;
  for(i=0; start[i]!=':' && start[i]!='=' && start[i]!='?' && start[i]!='+'; i++)
   if(!start[i]) {
    fprintf(stderr, "make: %u: missing ':', '=', '?=' or '+='\n", line);
    exit(1);
   }
  if(start[i]==':') {
   ;
   name=xmalloc(i+1);
   memcpy(name, start, i);
   name[i]=0;
   (*t).name=preprocess(name, line);
   free(name);
   i++;
   while(isblank(start[i])) i++;
   while(start[i]) {
    ;
    dep=start+i;
    for(len=0; start[i] && !isblank(start[i]); len++, i++);
    name=xmalloc(len+1);
    memcpy(name, dep, len);
    name[len]=0;
    ppdname=preprocess(name, line);
    free(name);
    if(!(*t).deps) {
     (*t).deps=xmalloc(sizeof(char *));
     (*t).deps[0]=0;
    }
    for(j=0; (*t).deps[j]; j++);
    (*t).deps[j]=ppdname;
    free(ppdname);
    (*t).deps=xrealloc((*t).deps, (j+2)*sizeof(char *));
    (*t).deps[j+1]=0;
    while(isblank(start[i]) && start[i]) i++;
   }
   *mode=1;
  } else if (start[i]=='=') {
   if(!vars) {
    vars=xmalloc(sizeof(char **));
    vars[0]=0;
   }
   name=xmalloc(i+1);
   memcpy(name, start, i);
   name[i]=0;
   for(j=0; vars[j]; j++);
   vars[j]=xmalloc(2*sizeof(char *));
   vars[j][0]=preprocess(name, line);
   free(name);
   for(i++; start[i] && isblank(start[i]); i++);
   vars[j][1]=preprocess(start+i, line);
   vars=xrealloc(vars, (j+2)*sizeof(char **));
   vars[j+1]=0;
  }
 } else if(!*start || *start=='#') {
  *mode=0;
 } else if(*start!='\t') {
  fprintf(stderr, "make: %u: cannot parse\n", line);
  exit(1);
 }
}

variable_types:
start
char *

variable_types:
mode
unsigned int *

variable_types: struct target { int isliteral; char *name; char **deps; char **cmds; } *

variable_types:
line
unsigned int

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
len
unsigned int

variable_types:
name
char *

variable_types:
ppdname
char *

variable_types:
dep
char *