decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er14
unsigned int i; // ebx
unsigned int v5; // ebp
unsigned int v6; // ebx
unsigned int v7; // edx
__int64 v8; // r12
int v9; // ebp
double v10; // xmm0_8
__int64 v11; // r15
int v12; // er13
int v13; // er12
__m128 v14; // xmm0
__m128 v15; // xmm1
__m128 v16; // xmm3
unsigned int v17; // ebx
_BYTE *v18; // rbp
int v19; // er8
__m128 v20; // xmm2
float v21; // xmm0_4
__m128 v22; // xmm0
__int64 v23; // rdi
int v24; // ecx
unsigned int j; // edx
unsigned int v26; // eax
unsigned int v27; // er12
int v28; // er13
unsigned __int8 *v29; // rbx
unsigned int k; // ebx
int v31; // ebx
unsigned int v32; // ebp
unsigned int l; // ebx
float v35; // [rsp+0h] [rbp-68h]
unsigned int v36; // [rsp+Ch] [rbp-5Ch]
unsigned int v37; // [rsp+18h] [rbp-50h] BYREF
int v38[4]; // [rsp+1Ch] [rbp-4Ch] BYREF
int v39; // [rsp+2Ch] [rbp-3Ch]

v39 = 0;
nf2 = (__int64)"nf2c3";
if ( (unsigned int)check_iface(&nf2, argv, envp) )
exit(1);
v3 = openDescriptor(&nf2);
if ( v3 )
exit(1);
writeReg(&nf2, 33559552LL, 0LL);
init_data((__int64)det);
innitialize_generator_packet((__int64)&state, (__int64)det);
for ( i = 0x2000000; i != 33557504; i += 256 )
writeReg(&nf2, i, 0LL);
v5 = 33554464;
v6 = 0;
do
{
writeReg(&nf2, v5, v6);
writeReg(&nf2, v5 + 4, v6 + 4095);
writeReg(&nf2, v5 - 32, 2LL);
writeReg(&nf2, v5 + 256, v6 + 4096);
v7 = v6;
v6 += 67584;
writeReg(&nf2, v5 + 260, v7 + 67583);
writeReg(&nf2, v5 + 224, 2LL);
v5 += 512;
}
while ( v6 != 270336 );
writeReg(&nf2, 33556512LL, 270336LL);
v8 = 1LL;
v9 = 2304;
v10 = *(double *)&LC1;
do
{
writeReg(&nf2, (unsigned int)(v9 + 33554212), (unsigned int)(int)(v10 + 1.0 - 1.0));
writeReg(&nf2, (unsigned int)(v9 + 33554176), 2LL);
v38[v8] = v6;
v11 = (unsigned int)(int)(v10 + 1.0);
v6 = (int)(v10 + 1.0);
v12 = v8;
writeReg(&nf2, (unsigned int)(v9 + 33554464), v11);
v10 = (double)(int)v11;
v9 += 256;
++v8;
}
while ( v9 != 3072 );
writeReg(
&nf2,
33557284LL,
(unsigned int)(int)(v10 + (double)((unsigned int)det[31] >> 3) + (double)det[31] + 1.0 - 1.0));
writeReg(&nf2, 33557248LL, 2LL);
v38[v12 + 1] = v6;
v13 = *((_DWORD *)&state + 16);
v14 = 0LL;
v14.m128_f32[0] = (float)v13 * 0.125;
v35 = v14.m128_f32[0];
if ( COERCE_FLOAT(v14.m128_i32[0] & LC6) < 8388608.0 )
{
v15.m128_f32[0] = (float)(int)v14.m128_f32[0];
v16.m128_f32[0] = (float)v13 * 0.125;
LODWORD(v35) = COERCE_UNSIGNED_INT(v15.m128_f32[0] + COERCE_FLOAT(_mm_cmpgt_ss(v16, v15).m128_u32[0] & LC7)) | _mm_andnot_ps((__m128)(unsigned int)LC6, v14).m128_u32[0];
}
v17 = (int)((double)(v13 + 9) + v35);
v36 = v17;
v18 = malloc(v17);
memset(v18, 0, v17);
*v18 = -1;
*(_WORD *)(v18 + 1) = 0;
*(_WORD *)(v18 + 3) = (int)v35;
*(_WORD *)(v18 + 5) = 2048;
*(_WORD *)(v18 + 7) = v13;
printf("size: %d %d output: %d\n", (unsigned __int16)(int)v35, (unsigned int)v13, 2048LL);
v19 = *((_DWORD *)&state + 16);
v20 = 0LL;
v20.m128_f32[0] = (float)v19 * 0.125;
v21 = v20.m128_f32[0];
if ( COERCE_FLOAT(v20.m128_i32[0] & LC6) < 8388608.0 )
{
v22.m128_f32[0] = (float)(int)v20.m128_f32[0];
LODWORD(v21) = COERCE_UNSIGNED_INT(v22.m128_f32[0] - COERCE_FLOAT(_mm_cmpgt_ss(v22, v20).m128_u32[0] & LC7)) | _mm_andnot_ps((__m128)(unsigned int)LC6, v20).m128_u32[0];
}
v23 = *((_QWORD *)&state + 7);
v24 = v3;
for ( j = 9; ; j += 9 )
{
v26 = 8 * v24;
if ( v21 <= (double)v24 )
break;
v18[j] = 0;
*(_QWORD *)&v18[j + 1] = *(_QWORD *)(v23 + v26);
++v24;
}
v18[j] = v19 - v26;
memcpy(&v18[j + 1], (const void *)(v23 + v26), v19 - v26);
v27 = 16 * (v39 + 0x100000);
if ( v17 )
{
v28 = 0;
do
{
writeReg(&nf2, v27, *(unsigned int *)v18);
writeReg(&nf2, v27 + 4, *((unsigned int *)v18 + 1));
writeReg(&nf2, v27 + 8, *((unsigned int *)v18 + 2));
if ( v28 <= 63 )
{
v29 = v18;
do
{
printf("%02x%02x%02x%02x ", *v29, v29[1], v29[2], v29[3]);
v29 += 4;
}
while ( v28 - (int)v18 + (int)v29 < v28 + 16 );
putchar(10);
}
v27 += 12;
v3 += 3;
v18 += 12;
v28 += 12;
}
while ( v36 > v3 );
}
for ( k = 33558848; k != 33559360; k += 128 )
{
writeReg(&nf2, 2 * k - 33561216, 0LL);
writeReg(&nf2, k, 0LL);
writeReg(&nf2, k + 64, 0LL);
}
writeReg(&nf2, 33557248LL, 1LL);
writeReg(&nf2, 33557320LL, 1LL);
printf("drop 0x%X...\n", 15LL);
writeReg(&nf2, 33559552LL, 15LL);
sleep(0xAu);
writeReg(&nf2, 33559552LL, 0LL);
v31 = 1024;
do
{
writeReg(&nf2, 33558784LL, 1LL);
--v31;
}
while ( v31 );
puts("Transmit statistics:");
puts("====================\n");
v32 = 8;
for ( l = 33556508; l != 33557532; l += 256 )
{
readReg(&nf2, l, &v37);
readReg(&nf2, l + 40, v38);
printf("%d:\n", v32);
printf("\tPackets: %u\n", v37);
printf("\tCompleted iterations: %u\n", (unsigned int)v38[0]);
++v32;
}
puts("\n");
printf("sending packet completed...");
closeDescriptor(&nf2);
puts("Test successful");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er14
unsigned int i; // ebx
unsigned int v5; // ebp
unsigned int v6; // ebx
unsigned int v7; // edx
__int64 v8; // r12
int v9; // ebp
double v10; // xmm0_8
__int64 v11; // r15
int v12; // er13
int v13; // er12
__m128 v14; // xmm0
__m128 v15; // xmm1
__m128 v16; // xmm3
unsigned int v17; // ebx
_BYTE *v18; // rbp
int v19; // er8
__m128 v20; // xmm2
float v21; // xmm0_4
__m128 v22; // xmm0
__int64 v23; // rdi
int v24; // ecx
unsigned int j; // edx
unsigned int v26; // eax
unsigned int v27; // er12
int v28; // er13
unsigned __int8 *v29; // rbx
unsigned int k; // ebx
int v31; // ebx
unsigned int v32; // ebp
unsigned int l; // ebx
float v35; // [rsp+0h] [rbp-68h]
unsigned int v36; // [rsp+Ch] [rbp-5Ch]
unsigned int v37; // [rsp+18h] [rbp-50h] BYREF
int v38[4]; // [rsp+1Ch] [rbp-4Ch] BYREF
int v39; // [rsp+2Ch] [rbp-3Ch]

v39 = 0;
nf2 = (__int64)"nf2c3";
if ( (unsigned int)check_iface(&nf2, argv, envp) )
exit(1);
v3 = openDescriptor(&nf2);
if ( v3 )
exit(1);
writeReg(&nf2, 33559552LL, 0LL);
func1((__int64)det);
func4((__int64)&state, (__int64)det);
for ( i = 0x2000000; i != 33557504; i += 256 )
writeReg(&nf2, i, 0LL);
v5 = 33554464;
v6 = 0;
do
{
writeReg(&nf2, v5, v6);
writeReg(&nf2, v5 + 4, v6 + 4095);
writeReg(&nf2, v5 - 32, 2LL);
writeReg(&nf2, v5 + 256, v6 + 4096);
v7 = v6;
v6 += 67584;
writeReg(&nf2, v5 + 260, v7 + 67583);
writeReg(&nf2, v5 + 224, 2LL);
v5 += 512;
}
while ( v6 != 270336 );
writeReg(&nf2, 33556512LL, 270336LL);
v8 = 1LL;
v9 = 2304;
v10 = *(double *)&LC1;
do
{
writeReg(&nf2, (unsigned int)(v9 + 33554212), (unsigned int)(int)(v10 + 1.0 - 1.0));
writeReg(&nf2, (unsigned int)(v9 + 33554176), 2LL);
v38[v8] = v6;
v11 = (unsigned int)(int)(v10 + 1.0);
v6 = (int)(v10 + 1.0);
v12 = v8;
writeReg(&nf2, (unsigned int)(v9 + 33554464), v11);
v10 = (double)(int)v11;
v9 += 256;
++v8;
}
while ( v9 != 3072 );
writeReg(
&nf2,
33557284LL,
(unsigned int)(int)(v10 + (double)((unsigned int)det[31] >> 3) + (double)det[31] + 1.0 - 1.0));
writeReg(&nf2, 33557248LL, 2LL);
v38[v12 + 1] = v6;
v13 = *((_DWORD *)&state + 16);
v14 = 0LL;
v14.m128_f32[0] = (float)v13 * 0.125;
v35 = v14.m128_f32[0];
if ( COERCE_FLOAT(v14.m128_i32[0] & LC6) < 8388608.0 )
{
v15.m128_f32[0] = (float)(int)v14.m128_f32[0];
v16.m128_f32[0] = (float)v13 * 0.125;
LODWORD(v35) = COERCE_UNSIGNED_INT(v15.m128_f32[0] + COERCE_FLOAT(_mm_cmpgt_ss(v16, v15).m128_u32[0] & LC7)) | _mm_andnot_ps((__m128)(unsigned int)LC6, v14).m128_u32[0];
}
v17 = (int)((double)(v13 + 9) + v35);
v36 = v17;
v18 = malloc(v17);
memset(v18, 0, v17);
*v18 = -1;
*(_WORD *)(v18 + 1) = 0;
*(_WORD *)(v18 + 3) = (int)v35;
*(_WORD *)(v18 + 5) = 2048;
*(_WORD *)(v18 + 7) = v13;
printf("size: %d %d output: %d\n", (unsigned __int16)(int)v35, (unsigned int)v13, 2048LL);
v19 = *((_DWORD *)&state + 16);
v20 = 0LL;
v20.m128_f32[0] = (float)v19 * 0.125;
v21 = v20.m128_f32[0];
if ( COERCE_FLOAT(v20.m128_i32[0] & LC6) < 8388608.0 )
{
v22.m128_f32[0] = (float)(int)v20.m128_f32[0];
LODWORD(v21) = COERCE_UNSIGNED_INT(v22.m128_f32[0] - COERCE_FLOAT(_mm_cmpgt_ss(v22, v20).m128_u32[0] & LC7)) | _mm_andnot_ps((__m128)(unsigned int)LC6, v20).m128_u32[0];
}
v23 = *((_QWORD *)&state + 7);
v24 = v3;
for ( j = 9; ; j += 9 )
{
v26 = 8 * v24;
if ( v21 <= (double)v24 )
break;
v18[j] = 0;
*(_QWORD *)&v18[j + 1] = *(_QWORD *)(v23 + v26);
++v24;
}
v18[j] = v19 - v26;
memcpy(&v18[j + 1], (const void *)(v23 + v26), v19 - v26);
v27 = 16 * (v39 + 0x100000);
if ( v17 )
{
v28 = 0;
do
{
writeReg(&nf2, v27, *(unsigned int *)v18);
writeReg(&nf2, v27 + 4, *((unsigned int *)v18 + 1));
writeReg(&nf2, v27 + 8, *((unsigned int *)v18 + 2));
if ( v28 <= 63 )
{
v29 = v18;
do
{
printf("%02x%02x%02x%02x ", *v29, v29[1], v29[2], v29[3]);
v29 += 4;
}
while ( v28 - (int)v18 + (int)v29 < v28 + 16 );
putchar(10);
}
v27 += 12;
v3 += 3;
v18 += 12;
v28 += 12;
}
while ( v36 > v3 );
}
for ( k = 33558848; k != 33559360; k += 128 )
{
writeReg(&nf2, 2 * k - 33561216, 0LL);
writeReg(&nf2, k, 0LL);
writeReg(&nf2, k + 64, 0LL);
}
writeReg(&nf2, 33557248LL, 1LL);
writeReg(&nf2, 33557320LL, 1LL);
printf("drop 0x%X...\n", 15LL);
writeReg(&nf2, 33559552LL, 15LL);
sleep(0xAu);
writeReg(&nf2, 33559552LL, 0LL);
v31 = 1024;
do
{
writeReg(&nf2, 33558784LL, 1LL);
--v31;
}
while ( v31 );
puts("Transmit statistics:");
puts("====================\n");
v32 = 8;
for ( l = 33556508; l != 33557532; l += 256 )
{
readReg(&nf2, l, &v37);
readReg(&nf2, l + 40, v38);
printf("%d:\n", v32);
printf("\tPackets: %u\n", v37);
printf("\tCompleted iterations: %u\n", (unsigned int)v38[0]);
++v32;
}
puts("\n");
printf("sending packet completed...");
closeDescriptor(&nf2);
puts("Test successful");
return 0;
}

original_code:
int
main() {
  unsigned val;
  int i;
  uint32_t queue_addr_offset = 0x2000120 - 0x2000020;
  uint32_t curr_addr = 0;
  uint32_t rx_queue_size = (0x80000 - 4*4096)/8;
  uint32_t src_port = 0, dst_port = 0x100;
  uint32_t word_len = 0, len = 0x100;
  uint32_t queue_base_addr[] = {0,0,0,0};
  char *data;
  uint32_t data_len, rate_limit_offset;
  uint32_t pointer = 0, pkt_pointer = 0;
  uint32_t drop;
  nf2.device_name = "nf2c3";
  if (check_iface(&nf2))
    exit(1);
  if (openDescriptor(&nf2))
    exit(1);
  writeReg(&nf2, 0x2001400, 0x00);
  init_data(&det);
  innitialize_generator_packet(&state, &det);
  for (i = 0; i < 3 * 4; i++) {
    writeReg (&nf2, (0x2000000 + i*queue_addr_offset), 0x00);
  }
  for (i = 0; i<4; i++) {
    writeReg (&nf2, (0x2000020 + (i*2)*queue_addr_offset), curr_addr);
    writeReg (&nf2, (0x2000024 + (i*2)*queue_addr_offset), curr_addr + 4096 - 1);
    writeReg (&nf2, (0x2000000 + (i*2)*queue_addr_offset), 0x02);
    curr_addr += 4096;
    writeReg (&nf2,0x2000020 + (i*2+1)*queue_addr_offset, curr_addr);
    writeReg (&nf2,0x2000024 + (i*2+1)*queue_addr_offset, curr_addr+rx_queue_size-1);
    writeReg (&nf2,0x2000000 + (i*2+1)*queue_addr_offset, 0x02);
    curr_addr += rx_queue_size;
  }
  for (i = 0; i < 4; i++) {
    writeReg (&nf2, 0x2000020 + (i + 2*4)*queue_addr_offset, curr_addr);
    writeReg (&nf2, 0x2000024 + (i + 2*4)*queue_addr_offset, curr_addr + ((i == 3)?
                  det.pkt_size + ceil(det.pkt_size/8) + 1:1) - 1);
    writeReg (&nf2,0x2000000 + (i+2*4)*queue_addr_offset, 0x02);
    queue_base_addr[i] = curr_addr;
    curr_addr += ((i == 3)? det.pkt_size + ceil(det.pkt_size/8) + 1:1);
  }
  data_len = state.data_len + 9 + ceil((float)state.data_len/8);
  data = malloc(data_len);
  bzero(data, data_len);
  pointer = 0;
  pkt_pointer = 0;
  data[pointer] = 0xff;
  pointer++;
  *(uint16_t *)(data + pointer ) = 0x0;
  pointer+=2;
  *(uint16_t *)(data + pointer ) = (uint16_t)ceil((float)state.data_len/8);
  pointer+=2;
  *(uint16_t *)(data + pointer ) = (uint16_t)(0x100 << 3);
  pointer+=2;
  *(uint16_t *)(data + pointer ) = (uint16_t)state.data_len;
  pointer+=2;
  printf("size: %d %d output: %d\n", (uint16_t)ceil((float)state.data_len/8), state.data_len, (uint16_t)(0x100 << 3));
  queue_addr_offset = 0x0000100;
  for(i = 0; i < floor((float)state.data_len/8); i++) {
    data[pointer] = 0x0;
    pointer++;
    memcpy(data+pointer, state.data + pkt_pointer, 8);
    pkt_pointer += 8;
    pointer += 8;
  }
  data[pointer] = state.data_len - pkt_pointer;
  pointer++;
  memcpy(data+pointer, state.data + pkt_pointer, state.data_len - pkt_pointer);
  pointer+= state.data_len - pkt_pointer;
  uint32_t sram_addr = 0x1000000 + queue_base_addr[3]*16;
  for (i = 0; i < data_len; i+=3) {
    writeReg (&nf2,sram_addr + 0x0, *((uint32_t *)(data + 4*i)));
    writeReg (&nf2,sram_addr + 0x4, *((uint32_t *)(data + 4*(i + 1))));
    writeReg (&nf2,sram_addr + 0x8, *((uint32_t *)(data + 4*(i + 2))));
    int j;
    if (4*i < 64) {
      for (j = 4*i; (j < 4*i+16); j+=4) {
 printf("%02x%02x%02x%02x ", (uint8_t)data[j], (uint8_t)data[j+1], (uint8_t)data[j+2], (uint8_t)data[j+3]);
      }
      printf("\n");
    }
    sram_addr += 12;
  }
  queue_addr_offset = 0x0000100;
  for (i = 0; i < 4; i++) {
    rate_limit_offset = 0x2001180 - 0x2001140;
    writeReg(&nf2, 0x2000000 + (i + 2 * 4) * queue_addr_offset, 0x0);
    writeReg (&nf2, 0x2001140 + 2 * i * rate_limit_offset, 0x0);
    writeReg (&nf2, 0x2001140 + (2*i + 1) * rate_limit_offset, 0x0);
  }
  writeReg(&nf2, 0x2000000 + (3 + 2 * 4) * queue_addr_offset, 0x1);
  writeReg(&nf2, 0x2000048 + (3 + 2 * 4) * queue_addr_offset, 1);
  drop = 0;
  printf("drop 0x%X...\n", drop | 0xF);
  writeReg(&nf2, 0x2001400, drop | 0xF);
  sleep(10);
  writeReg(&nf2, 0x2001400, 0x0);
  for (i = 0; i < 1024; i++) {
    writeReg(&nf2, 0x2001100, 1);
  }
  printf("Transmit statistics:\n");
  printf("====================\n\n");
  for (i = 0; i < 4; i++) {
    uint32_t pkt_cnt, iter_cnt;
    readReg(&nf2, 0x200001c + (i + 8) * queue_addr_offset, &pkt_cnt);
    readReg(&nf2, 0x2000044 + (i + 8) * queue_addr_offset, &iter_cnt);
      printf("%d:\n", i + 8);
      printf("\tPackets: %u\n", pkt_cnt);
      printf("\tCompleted iterations: %u\n", iter_cnt);
  }
  printf("\n\n");
  printf("sending packet completed...");
  closeDescriptor(&nf2);
  printf("Test successful\n");
}

canonical_original_code:
int main() {
  unsigned int val;
  int i;
  unsigned int queue_addr_offset = 0x2000120 - 0x2000020;
  unsigned int curr_addr = 0;
  unsigned int rx_queue_size = (0x80000 - 4*4096)/8;
  unsigned int src_port = 0; unsigned int dst_port = 0x100;
  unsigned int word_len = 0; unsigned int len = 0x100;
  unsigned int queue_base_addr[] = {0,0,0,0};
  char *data;
  unsigned int data_len; unsigned int rate_limit_offset;
  unsigned int pointer = 0; unsigned int pkt_pointer = 0;
  unsigned int drop;
  nf2.device_name = "nf2c3";
  if (check_iface(&nf2))
    exit(1);
  if (openDescriptor(&nf2))
    exit(1);
  writeReg(&nf2, 0x2001400, 0x00);
  init_data(&det);
  innitialize_generator_packet(&state, &det);
  for (i = 0; i < 3 * 4; i++) {
    writeReg (&nf2, (0x2000000 + i*queue_addr_offset), 0x00);
  }
  for (i = 0; i<4; i++) {
    writeReg (&nf2, (0x2000020 + (i*2)*queue_addr_offset), curr_addr);
    writeReg (&nf2, (0x2000024 + (i*2)*queue_addr_offset), curr_addr + 4096 - 1);
    writeReg (&nf2, (0x2000000 + (i*2)*queue_addr_offset), 0x02);
    curr_addr += 4096;
    writeReg (&nf2,0x2000020 + (i*2+1)*queue_addr_offset, curr_addr);
    writeReg (&nf2,0x2000024 + (i*2+1)*queue_addr_offset, curr_addr+rx_queue_size-1);
    writeReg (&nf2,0x2000000 + (i*2+1)*queue_addr_offset, 0x02);
    curr_addr += rx_queue_size;
  }
  for (i = 0; i < 4; i++) {
    writeReg (&nf2, 0x2000020 + (i + 2*4)*queue_addr_offset, curr_addr);
    writeReg (&nf2, 0x2000024 + (i + 2*4)*queue_addr_offset, curr_addr + ((i == 3)?
                  det.pkt_size + ceil(det.pkt_size/8) + 1:1) - 1);
    writeReg (&nf2,0x2000000 + (i+2*4)*queue_addr_offset, 0x02);
    queue_base_addr[i] = curr_addr;
    curr_addr += ((i == 3)? det.pkt_size + ceil(det.pkt_size/8) + 1:1);
  }
  data_len = state.data_len + 9 + ceil((float)state.data_len/8);
  data = malloc(data_len);
  bzero(data, data_len);
  pointer = 0;
  pkt_pointer = 0;
  data[pointer] = 0xff;
  pointer++;
  *(unsigned short *)(data + pointer ) = 0x0;
  pointer+=2;
  *(unsigned short *)(data + pointer ) = (unsigned short)ceil((float)state.data_len/8);
  pointer+=2;
  *(unsigned short *)(data + pointer ) = (unsigned short)(0x100 << 3);
  pointer+=2;
  *(unsigned short *)(data + pointer ) = (unsigned short)state.data_len;
  pointer+=2;
  printf("size: %d %d output: %d\n", (unsigned short)ceil((float)state.data_len/8), state.data_len, (unsigned short)(0x100 << 3));
  queue_addr_offset = 0x0000100;
  for(i = 0; i < floor((float)state.data_len/8); i++) {
    data[pointer] = 0x0;
    pointer++;
    memcpy(data+pointer, state.data + pkt_pointer, 8);
    pkt_pointer += 8;
    pointer += 8;
  }
  data[pointer] = state.data_len - pkt_pointer;
  pointer++;
  memcpy(data+pointer, state.data + pkt_pointer, state.data_len - pkt_pointer);
  pointer+= state.data_len - pkt_pointer;
  unsigned int sram_addr = 0x1000000 + queue_base_addr[3]*16;
  for (i = 0; i < data_len; i+=3) {
    writeReg (&nf2,sram_addr + 0x0, *((unsigned int *)(data + 4*i)));
    writeReg (&nf2,sram_addr + 0x4, *((unsigned int *)(data + 4*(i + 1))));
    writeReg (&nf2,sram_addr + 0x8, *((unsigned int *)(data + 4*(i + 2))));
    int j;
    if (4*i < 64) {
      for (j = 4*i; (j < 4*i+16); j+=4) {
 printf("%02x%02x%02x%02x ", (unsigned char)data[j], (unsigned char)data[j+1], (unsigned char)data[j+2], (unsigned char)data[j+3]);
      }
      printf("\n");
    }
    sram_addr += 12;
  }
  queue_addr_offset = 0x0000100;
  for (i = 0; i < 4; i++) {
    rate_limit_offset = 0x2001180 - 0x2001140;
    writeReg(&nf2, 0x2000000 + (i + 2 * 4) * queue_addr_offset, 0x0);
    writeReg (&nf2, 0x2001140 + 2 * i * rate_limit_offset, 0x0);
    writeReg (&nf2, 0x2001140 + (2*i + 1) * rate_limit_offset, 0x0);
  }
  writeReg(&nf2, 0x2000000 + (3 + 2 * 4) * queue_addr_offset, 0x1);
  writeReg(&nf2, 0x2000048 + (3 + 2 * 4) * queue_addr_offset, 1);
  drop = 0;
  printf("drop 0x%X...\n", drop | 0xF);
  writeReg(&nf2, 0x2001400, drop | 0xF);
  sleep(10);
  writeReg(&nf2, 0x2001400, 0x0);
  for (i = 0; i < 1024; i++) {
    writeReg(&nf2, 0x2001100, 1);
  }
  printf("Transmit statistics:\n");
  printf("====================\n\n");
  for (i = 0; i < 4; i++) {
    unsigned int pkt_cnt; unsigned int iter_cnt;
    readReg(&nf2, 0x200001c + (i + 8) * queue_addr_offset, &pkt_cnt);
    readReg(&nf2, 0x2000044 + (i + 8) * queue_addr_offset, &iter_cnt);
      printf("%d:\n", i + 8);
      printf("\tPackets: %u\n", pkt_cnt);
      printf("\tCompleted iterations: %u\n", iter_cnt);
  }
  printf("\n\n");
  printf("sending packet completed...");
  closeDescriptor(&nf2);
  printf("Test successful\n");
}

variable_types:
val
unsigned int

variable_types: i int

variable_types:
len
unsigned int

variable_types:
data
char *

variable_types:
pointer
unsigned int

variable_types:
drop
unsigned int

variable_types: j int


main

canonical_name: main

repo: andrakis/gleam

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int result; // eax

v3 = emu_new(argc, argv, envp);
result = 1;
if ( v3 )
{
emu_iterate(v3);
emu_free(v3);
result = 0;
}
return result;
}

original_code:
int main(int argc, char* argv[])
{
 gleam_emu_t *emu = emu_new();
 if (!emu) {
  ;
  return 1;
 }
 emu_iterate(emu);
 emu_free(emu);
 ;
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 struct gleam_emu *emu = emu_new();
 if (!emu) {
  ;
  return 1;
 }
 emu_iterate(emu);
 emu_free(emu);
 ;
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct gleam_emu { struct gleam_mem { struct mem_node { long long start; long long range; char *page; struct mem_node *prev; } base; struct mem_node *head; } *mem; } *

return_type: int

user_defined_types: struct gleam_emu { struct gleam_mem *mem; }

user_defined_types: struct gleam_mem { struct mem_node base; struct mem_node *head; }


decompiled_code:
__int64 __fastcall emu_rom(_QWORD *a1)
{
__int64 v1; // rbx

v1 = *a1;
mem_write_gnum(0LL, 0LL, *a1);
mem_write_gnum(1LL, 0LL, v1);
return mem_write_gnum(2LL, 11LL, v1);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
__int64 v1; // rbx

v1 = *a1;
mem_write_gnum(0LL, 0LL, *a1);
mem_write_gnum(1LL, 0LL, v1);
return mem_write_gnum(2LL, 11LL, v1);
}

original_code:
void emu_rom (gleam_emu_t *emu) {
 gleam_mem_t *mem = emu->mem;
 mem_write_gnum(0, 0, mem);
 mem_write_gnum(1, 0, mem);
 mem_write_gnum(2, cp, mem);
}

canonical_original_code:
void emu_rom(struct gleam_emu *emu) {
 struct gleam_mem *mem = emu->mem;
 mem_write_gnum(0, 0, mem);
 mem_write_gnum(1, 0, mem);
 mem_write_gnum(2, cp, mem);
}

variable_types: struct gleam_mem { struct mem_node { long long start; long long range; char *page; struct mem_node *prev; } base; struct mem_node *head; } *

return_type: void


decompiled_code:
_QWORD *emu_new()
{
_QWORD *v0; // rbx
__int64 v1; // rax

v0 = malloc(8uLL);
v1 = mem_new();
*v0 = v1;
if ( v1 )
{
if ( mem_test(v1) )
{
puts("Mem test failed!");
mem_free(*v0);
free(v0);
v0 = 0LL;
}
else
{
emu_rom(v0);
}
}
else
{
puts("No mem!");
free(v0);
v0 = 0LL;
}
return v0;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *v0; // rbx
__int64 v1; // rax

v0 = malloc(8uLL);
v1 = mem_new();
*v0 = v1;
if ( v1 )
{
if ( mem_test(v1) )
{
puts("Mem test failed!");
mem_free(*v0);
free(v0);
v0 = 0LL;
}
else
{
func0(v0);
}
}
else
{
puts("No mem!");
free(v0);
v0 = 0LL;
}
return v0;
}

original_code:
gleam_emu_t *emu_new (void) {
 gleam_emu_t *emu = (gleam_emu_t*)malloc(sizeof(gleam_emu_t));
  emu->mem = mem_new();
  if (!emu->mem) {
   printf("No mem!\n");
   free(emu);
   return 0;
  }
  if (mem_test(emu->mem)) {
   printf("Mem test failed!\n");
   mem_free(emu->mem);
   free(emu);
   return 0;
  }
  emu_rom(emu);
 return emu;
}

canonical_original_code:
struct gleam_emu *emu_new() {
 struct gleam_emu *emu = (gleam_emu_t*)malloc(sizeof(gleam_emu_t));
  emu->mem = mem_new();
  if (!emu->mem) {
   printf("No mem!\n");
   free(emu);
   return 0;
  }
  if (mem_test(emu->mem)) {
   printf("Mem test failed!\n");
   mem_free(emu->mem);
   free(emu);
   return 0;
  }
  emu_rom(emu);
 return emu;
}

return_type: struct gleam_emu { struct gleam_mem { struct mem_node { long long start; long long range; char *page; struct mem_node *prev; } base; struct mem_node *head; } *mem; } *


decompiled_code:
__int64 __fastcall emu_test(_QWORD *a1)
{
__int64 v1; // rbp

v1 = mem_test(*a1);
mem_dump(*a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
__int64 v1; // rbp

v1 = mem_test(*a1);
mem_dump(*a1);
return v1;
}

original_code:
gnum emu_test (gleam_emu_t *emu) {
 gnum ret;
 ret = mem_test(emu->mem);
 mem_dump(emu->mem);
 return ret;
}

canonical_original_code:
long long emu_test(struct gleam_emu *emu) {
 long long ret;
 ret = mem_test(emu->mem);
 mem_dump(emu->mem);
 return ret;
}

variable_types:
ret
long long

return_type: long long

user_defined_types: struct mem_node { long long start; long long range; char *page; struct mem_node *prev; }


decompiled_code:
void __fastcall emu_free(void *ptr)
{
mem_free(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
mem_free(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void emu_free (gleam_emu_t *emu) {
 mem_free(emu->mem);
 free(emu);
}

canonical_original_code:
void emu_free(struct gleam_emu *emu) {
 mem_free(emu->mem);
 free(emu);
}


decompiled_code:
const char *__fastcall emu_decode_pos(__int64 a1)
{
const char *result; // rax

switch ( a1 )
{
case 10LL:
result = "a0";
break;
case 11LL:
result = "cp";
break;
case 12LL:
result = "rp";
break;
case 13LL:
result = "sp";
break;
case 14LL:
result = "psp";
break;
case 15LL:
result = "eq";
break;
case 16LL:
result = "mt";
break;
case 17LL:
result = "lt";
break;
case 18LL:
result = "ac";
break;
case 19LL:
result = "dc";
break;
case 20LL:
result = "r0";
break;
case 21LL:
result = "r1";
break;
case 22LL:
result = "r2";
break;
case 23LL:
result = "r3";
break;
case 24LL:
result = "r4";
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func4(__int64 a1)
{
const char *result; // rax

switch ( a1 )
{
case 10LL:
result = "a0";
break;
case 11LL:
result = "cp";
break;
case 12LL:
result = "rp";
break;
case 13LL:
result = "sp";
break;
case 14LL:
result = "psp";
break;
case 15LL:
result = "eq";
break;
case 16LL:
result = "mt";
break;
case 17LL:
result = "lt";
break;
case 18LL:
result = "ac";
break;
case 19LL:
result = "dc";
break;
case 20LL:
result = "r0";
break;
case 21LL:
result = "r1";
break;
case 22LL:
result = "r2";
break;
case 23LL:
result = "r3";
break;
case 24LL:
result = "r4";
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
char *emu_decode_pos (gnum pos) {
 switch(pos) {
  case a0: return "a0";
  case cp: return "cp";
  case rp: return "rp";
  case sp: return "sp";
  case psp: return "psp";
  case eq: return "eq";
  case mt: return "mt";
  case lt: return "lt";
  case ac: return "ac";
  case dc: return "dc";
  case r0: return "r0";
  case r1: return "r1";
  case r2: return "r2";
  case r3: return "r3";
  case r4: return "r4";
 }
 return 0;
}

canonical_original_code:
char *emu_decode_pos(long long pos) {
 switch(pos) {
  case a0: return "a0";
  case cp: return "cp";
  case rp: return "rp";
  case sp: return "sp";
  case psp: return "psp";
  case eq: return "eq";
  case mt: return "mt";
  case lt: return "lt";
  case ac: return "ac";
  case dc: return "dc";
  case r0: return "r0";
  case r1: return "r1";
  case r2: return "r2";
  case r3: return "r3";
  case r4: return "r4";
 }
 return 0;
}

variable_types:
pos
long long

return_type: char *


decompiled_code:
__int64 __fastcall emu_encode_pos(char *a1)
{
char v1; // al
__int64 result; // rax
char v3; // al

v1 = *a1;
if ( *a1 == 97 )
{
if ( a1[1] != 48 )
{
if ( a1[1] == 99 )
return 18LL;
return atoll(a1);
}
result = 10LL;
}
else
{
switch ( v1 )
{
case 'c':
if ( a1[1] != 112 )
return atoll(a1);
result = 11LL;
break;
case 'r':
if ( a1[1] == 112 )
{
result = 12LL;
}
else
{
v3 = a1[1];
switch ( v3 )
{
case '0':
result = 20LL;
break;
case '1':
result = 21LL;
break;
case '2':
result = 22LL;
break;
case '3':
return 23LL;
default:
return atoll(a1);
}
}
break;
case 's':
if ( a1[1] == 112 )
return 13LL;
return atoll(a1);
case 'p':
if ( a1[1] == 115 && a1[2] == 112 )
return 14LL;
return atoll(a1);
case 'e':
if ( a1[1] != 113 )
return atoll(a1);
result = 15LL;
break;
case 'm':
if ( a1[1] != 116 )
return atoll(a1);
result = 16LL;
break;
case 'l':
if ( a1[1] != 116 )
return atoll(a1);
result = 17LL;
break;
default:
if ( v1 != 100 || a1[1] != 99 )
return atoll(a1);
result = 19LL;
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *a1)
{
char v1; // al
__int64 result; // rax
char v3; // al

v1 = *a1;
if ( *a1 == 97 )
{
if ( a1[1] != 48 )
{
if ( a1[1] == 99 )
return 18LL;
return atoll(a1);
}
result = 10LL;
}
else
{
switch ( v1 )
{
case 'c':
if ( a1[1] != 112 )
return atoll(a1);
result = 11LL;
break;
case 'r':
if ( a1[1] == 112 )
{
result = 12LL;
}
else
{
v3 = a1[1];
switch ( v3 )
{
case '0':
result = 20LL;
break;
case '1':
result = 21LL;
break;
case '2':
result = 22LL;
break;
case '3':
return 23LL;
default:
return atoll(a1);
}
}
break;
case 's':
if ( a1[1] == 112 )
return 13LL;
return atoll(a1);
case 'p':
if ( a1[1] == 115 && a1[2] == 112 )
return 14LL;
return atoll(a1);
case 'e':
if ( a1[1] != 113 )
return atoll(a1);
result = 15LL;
break;
case 'm':
if ( a1[1] != 116 )
return atoll(a1);
result = 16LL;
break;
case 'l':
if ( a1[1] != 116 )
return atoll(a1);
result = 17LL;
break;
default:
if ( v1 != 100 || a1[1] != 99 )
return atoll(a1);
result = 19LL;
break;
}
}
return result;
}

original_code:
gnum emu_encode_pos (char *symbol) {
 if (*symbol == 'a' && *(symbol + 1) == '0') return a0;
 if (*symbol == 'c' && *(symbol + 1) == 'p') return cp;
 if (*symbol == 'r' && *(symbol + 1) == 'p') return rp;
 if (*symbol == 's' && *(symbol + 1) == 'p') return sp;
 if (*symbol == 'p' && *(symbol + 1) == 's' && *(symbol + 2) == 'p')
   return psp;
 if (*symbol == 'e' && *(symbol + 1) == 'q') return eq;
 if (*symbol == 'm' && *(symbol + 1) == 't') return mt;
 if (*symbol == 'l' && *(symbol + 1) == 't') return lt;
 if (*symbol == 'a' && *(symbol + 1) == 'c') return ac;
 if (*symbol == 'd' && *(symbol + 1) == 'c') return dc;
 if (*symbol == 'r' && *(symbol + 1) == '0') return r0;
 if (*symbol == 'r' && *(symbol + 1) == '1') return r1;
 if (*symbol == 'r' && *(symbol + 1) == '2') return r2;
 if (*symbol == 'r' && *(symbol + 1) == '3') return r3;
 if (*symbol == 'r' && *(symbol + 1) == '3') return r2;
 return atoll(symbol);
}

canonical_original_code:
long long emu_encode_pos(char *symbol) {
 if (*symbol == 'a' && *(symbol + 1) == '0') return a0;
 if (*symbol == 'c' && *(symbol + 1) == 'p') return cp;
 if (*symbol == 'r' && *(symbol + 1) == 'p') return rp;
 if (*symbol == 's' && *(symbol + 1) == 'p') return sp;
 if (*symbol == 'p' && *(symbol + 1) == 's' && *(symbol + 2) == 'p')
   return psp;
 if (*symbol == 'e' && *(symbol + 1) == 'q') return eq;
 if (*symbol == 'm' && *(symbol + 1) == 't') return mt;
 if (*symbol == 'l' && *(symbol + 1) == 't') return lt;
 if (*symbol == 'a' && *(symbol + 1) == 'c') return ac;
 if (*symbol == 'd' && *(symbol + 1) == 'c') return dc;
 if (*symbol == 'r' && *(symbol + 1) == '0') return r0;
 if (*symbol == 'r' && *(symbol + 1) == '1') return r1;
 if (*symbol == 'r' && *(symbol + 1) == '2') return r2;
 if (*symbol == 'r' && *(symbol + 1) == '3') return r3;
 if (*symbol == 'r' && *(symbol + 1) == '3') return r2;
 return atoll(symbol);
}

variable_types:
symbol
char *


original_code:
void emu_iterate (gleam_emu_t *emu) {
 register gleam_mem_t *mem = emu->mem;
 gnum _sp,
      from,
      add,
      to,
      val;
 _sp = mem_read_gnum(mem_read_gnum(sp, mem) + sp, mem);
 from = mem_read_gnum(_sp + mem_read_gnum(_sp + cp, mem), mem);
 mem_write_gnum(_sp + cp, mem_read_gnum(_sp + cp, mem) + 1, mem);
 add = mem_read_gnum(_sp + mem_read_gnum(_sp + cp, mem), mem);
 mem_write_gnum(_sp + cp, mem_read_gnum(_sp + cp, mem) + 1, mem);
 to = mem_read_gnum(_sp + mem_read_gnum(_sp + cp, mem), mem);
 mem_write_gnum(_sp + cp, mem_read_gnum(_sp + cp, mem) + 1, mem);
 val = mem_read_gnum(_sp + from, mem) + add;
 mem_write_gnum(_sp + to, val, mem);
 gleam_debug(from, add, to, val);
 mem_write_gnum(_sp + eq, (val == 0 ? 3 : 0), mem);
 mem_write_gnum(_sp + mt, (val > 0 ? 3 : 0), mem);
 mem_write_gnum(_sp + lt, (val < 0 ? 3 : 0), mem);
 mem_write_gnum(_sp + ac, mem_read_gnum(_sp + ac, mem) + val, mem);
 mem_write_gnum(_sp + dc, mem_read_gnum(_sp + dc, mem) - val, mem);
}

canonical_original_code:
void emu_iterate(struct gleam_emu *emu) {
 register struct gleam_mem *mem = emu->mem;
 long long _sp; long long from; long long add; long long to; long long val;
 _sp = mem_read_gnum(mem_read_gnum(sp, mem) + sp, mem);
 from = mem_read_gnum(_sp + mem_read_gnum(_sp + cp, mem), mem);
 mem_write_gnum(_sp + cp, mem_read_gnum(_sp + cp, mem) + 1, mem);
 add = mem_read_gnum(_sp + mem_read_gnum(_sp + cp, mem), mem);
 mem_write_gnum(_sp + cp, mem_read_gnum(_sp + cp, mem) + 1, mem);
 to = mem_read_gnum(_sp + mem_read_gnum(_sp + cp, mem), mem);
 mem_write_gnum(_sp + cp, mem_read_gnum(_sp + cp, mem) + 1, mem);
 val = mem_read_gnum(_sp + from, mem) + add;
 mem_write_gnum(_sp + to, val, mem);
 gleam_debug(from, add, to, val);
 mem_write_gnum(_sp + eq, (val == 0 ? 3 : 0), mem);
 mem_write_gnum(_sp + mt, (val > 0 ? 3 : 0), mem);
 mem_write_gnum(_sp + lt, (val < 0 ? 3 : 0), mem);
 mem_write_gnum(_sp + ac, mem_read_gnum(_sp + ac, mem) + val, mem);
 mem_write_gnum(_sp + dc, mem_read_gnum(_sp + dc, mem) - val, mem);
}

variable_types:
from
long long

variable_types:
add
long long

variable_types:
to
long long

variable_types:
val
long long


original_code:
gleam_mem_t *mem_new ()
{
 gleam_mem_t *mem = (gleam_mem_t*)malloc(sizeof(gleam_mem_t));
 if (!mem) return 0;
 mem->head = &mem->base;
 mem->base.start = 0;
 mem->base.range = 0x10000;
 mem->base.next = mem->base.prev = 0;
 mem->base.page = (char*)malloc(0x10000);
 if (!mem->base.page) {
  free(mem);
  return 0;
 }
 memset(mem->base.page, 0, 0x10000);
 return mem;
}

canonical_original_code:
struct gleam_mem *mem_new()
{
 struct gleam_mem *mem = (gleam_mem_t*)malloc(sizeof(gleam_mem_t));
 if (!mem) return 0;
 mem->head = &mem->base;
 mem->base.start = 0;
 mem->base.range = 0x10000;
 mem->base.next = mem->base.prev = 0;
 mem->base.page = (char *)malloc(0x10000);
 if (!mem->base.page) {
  free(mem);
  return 0;
 }
 memset(mem->base.page, 0, 0x10000);
 return mem;
}

return_type: struct gleam_mem { struct mem_node { long long start; long long range; char *page; struct mem_node *prev; } base; struct mem_node *head; } *


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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 24);
}
while ( a1 );
do
result = *(_QWORD *)(result + 32);
while ( result );
return result;
}

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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 24);
}
while ( a1 );
do
result = *(_QWORD *)(result + 32);
while ( result );
return result;
}

original_code:
void mem_free(gleam_mem_t *mem) {
 mem_node_t *it = &mem->base;
 mem_node_t *next;
 while(it->prev) it = it->prev;
 do {
  next = it->next;
  it = next;
 } while(it);
}

canonical_original_code:
void mem_free(struct gleam_mem *mem) {
 struct mem_node *it = &mem->base;
 struct mem_node *next;
 while(it->prev) it = it->prev;
 do {
  next = it->next;
  it = next;
 } while(it);
}

variable_types: struct mem_node { long long start; long long range; char *page; struct mem_node *prev; } *


decompiled_code:
int __fastcall mem_dump(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 v2; // rbp

do
{
v1 = a1;
a1 = (_QWORD *)a1[3];
}
while ( a1 );
v2 = 0LL;
do
{
printf("Page %lld: %lld - %lld\n", ++v2, *v1, v1[1] + *v1);
v1 = (_QWORD *)v1[4];
}
while ( v1 );
return printf("Total pages: %lld", v2);
}

canonical_decompiled_code:
int __fastcall func2(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 v2; // rbp

do
{
v1 = a1;
a1 = (_QWORD *)a1[3];
}
while ( a1 );
v2 = 0LL;
do
{
printf("Page %lld: %lld - %lld\n", ++v2, *v1, v1[1] + *v1);
v1 = (_QWORD *)v1[4];
}
while ( v1 );
return printf("Total pages: %lld", v2);
}

original_code:
void mem_dump (gleam_mem_t *src) {
 mem_node_t *it = &src->base;
 gnum pages = 0;
 while(it->prev) it = it->prev;
 do {
  pages++;
  printf("Page %lld: %lld - %lld\n", pages, it->start, it->start + it->range);
  it = it->next;
 } while(it);
 printf("Total pages: %lld", pages);
}

canonical_original_code:
void mem_dump(struct gleam_mem *src) {
 struct mem_node *it = &src->base;
 long long pages = 0;
 while(it->prev) it = it->prev;
 do {
  pages++;
  printf("Page %lld: %lld - %lld\n", pages, it->start, it->start + it->range);
  it = it->next;
 } while(it);
 printf("Total pages: %lld", pages);
}

variable_types:
pages
long long


decompiled_code:
__int64 __fastcall mem_align(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdi

if ( a1 < 0 )
{
v2 = -a1;
if ( (_WORD)v2 )
v2 -= v2 % 0x10000;
result = -65536 - v2;
}
else if ( (_WORD)a1 )
{
result = a1 - a1 % 0x10000;
}
else
{
result = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdi

if ( a1 < 0 )
{
v2 = -a1;
if ( (_WORD)v2 )
v2 -= v2 % 0x10000;
result = -65536 - v2;
}
else if ( (_WORD)a1 )
{
result = a1 - a1 % 0x10000;
}
else
{
result = a1;
}
return result;
}

original_code:
gnum mem_align(gnum offset)
{
 long sign;
 if (offset < 0) {
  sign = -1;
  offset = -offset;
 } else sign = 0;
 if (offset % 0x10000 !=0)
  offset -= offset % 0x10000;
 if (sign)
  return -offset - 0x10000;
 else
  return offset;
}

canonical_original_code:
long long mem_align(long long offset)
{
 long sign;
 if (offset < 0) {
  sign = -1;
  offset = -offset;
 } else sign = 0;
 if (offset % 0x10000 !=0)
  offset -= offset % 0x10000;
 if (sign)
  return -offset - 0x10000;
 else
  return offset;
}

variable_types:
offset
long long

variable_types: sign long


original_code:
gnum mem_read_byte(gnum location, gleam_mem_t *mem) {
 register gnum offset = 0;
 gnum page_offset;
 gnum dst;
 void *b = &dst;
 gnum loc = location * sizeof(gnum);
 mem->head = mem_select(loc, mem->head);
 for(offset = 0; offset < sizeof(gnum); offset++) {
  page_offset = (loc - mem->head->start) + offset;
  *((char*)b + offset) = *(mem->head->page + page_offset);
  mem->head = mem_select(loc, mem->head);
 }
 return dst;
}

canonical_original_code:
long long mem_read_byte(long long location, struct gleam_mem *mem) {
 register long long offset = 0;
 long long page_offset;
 long long dst;
 void *b = &dst;
 long long loc = location * sizeof(gnum);
 mem->head = mem_select(loc, mem->head);
 for(offset = 0; offset < sizeof(gnum); offset++) {
  page_offset = (loc - mem->head->start) + offset;
  *((char *)b + offset) = *(mem->head->page + page_offset);
  mem->head = mem_select(loc, mem->head);
 }
 return dst;
}

variable_types:
location
long long

variable_types:
dst
long long

variable_types:
b
void *

variable_types:
loc
long long


original_code:
gnum mem_read_gnum(gnum location, gleam_mem_t *mem) {
 gnum page_offset;
 gnum loc = location * sizeof(gnum);
 mem->head = mem_select(loc, mem->head);
 page_offset = (loc - mem->head->start);
 return *(gnum*)(mem->head->page + page_offset);
}

canonical_original_code:
long long mem_read_gnum(long long location, struct gleam_mem *mem) {
 long long page_offset;
 long long loc = location * sizeof(gnum);
 mem->head = mem_select(loc, mem->head);
 page_offset = (loc - mem->head->start);
 return *(long long *)(mem->head->page + page_offset);
}


decompiled_code:
__int64 __fastcall mem_write_gnum(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
__int64 v5; // rbx
__int64 result; // rax

v4 = (_QWORD *)mem_select(8 * a1, *(__int64 **)(a3 + 40));
*(_QWORD *)(a3 + 40) = v4;
v5 = 8 * a1 - *v4;
result = v4[2];
*(_QWORD *)(result + v5) = a2;
return result;
}

original_code:
void mem_write_gnum(gnum location, gnum value, gleam_mem_t *mem) {
 gnum page_offset;
 gnum loc = location * sizeof(gnum);
 mem->head = mem_select(loc, mem->head);
 page_offset = (loc - mem->head->start);
 *(gnum*)(mem->head->page + page_offset) = value;
}

canonical_original_code:
void mem_write_gnum(long long location, long long value, struct gleam_mem *mem) {
 long long page_offset;
 long long loc = location * sizeof(gnum);
 mem->head = mem_select(loc, mem->head);
 page_offset = (loc - mem->head->start);
 *(long long *)(mem->head->page + page_offset) = value;
}

variable_types:
value
long long


original_code:
gnum mem_test (gleam_mem_t *mem) {
 gnum offset = 0xFF;
 while(offset < 0xFFFFFFFF) {
  mem_write_gnum(offset + 0, 'H', mem);
  mem_write_gnum(offset + 1, 'e', mem);
  mem_write_gnum(offset + 2, 'l', mem);
  mem_write_gnum(offset + 3, 'l', mem);
  mem_write_gnum(offset + 4, 'o', mem);
  if (mem_read_gnum(offset + 0, mem) != 'H' ||
   mem_read_gnum(offset + 1, mem) != 'e' ||
   mem_read_gnum(offset + 2, mem) != 'l' ||
   mem_read_gnum(offset + 3, mem) != 'l' ||
   mem_read_gnum(offset + 4, mem) != 'o') {
    return offset;
  }
  offset *= 10;
 }
 offset = -0xFF;
 while(offset > (gnum)-0xFFFFFF) {
  mem_write_gnum(offset + 0, 'H', mem);
  mem_write_gnum(offset + 1, 'e', mem);
  mem_write_gnum(offset + 2, 'l', mem);
  mem_write_gnum(offset + 3, 'l', mem);
  mem_write_gnum(offset + 4, 'o', mem);
  if (mem_read_gnum(offset + 0, mem) != 'H' ||
   mem_read_gnum(offset + 1, mem) != 'e' ||
   mem_read_gnum(offset + 2, mem) != 'l' ||
   mem_read_gnum(offset + 3, mem) != 'l' ||
   mem_read_gnum(offset + 4, mem) != 'o') {
    return offset;
  }
  offset *= 10;
 }
 return 0;
}

canonical_original_code:
long long mem_test(struct gleam_mem *mem) {
 long long offset = 0xFF;
 while(offset < 0xFFFFFFFF) {
  mem_write_gnum(offset + 0, 'H', mem);
  mem_write_gnum(offset + 1, 'e', mem);
  mem_write_gnum(offset + 2, 'l', mem);
  mem_write_gnum(offset + 3, 'l', mem);
  mem_write_gnum(offset + 4, 'o', mem);
  if (mem_read_gnum(offset + 0, mem) != 'H' ||
   mem_read_gnum(offset + 1, mem) != 'e' ||
   mem_read_gnum(offset + 2, mem) != 'l' ||
   mem_read_gnum(offset + 3, mem) != 'l' ||
   mem_read_gnum(offset + 4, mem) != 'o') {
    return offset;
  }
  offset *= 10;
 }
 offset = -0xFF;
 while(offset > (gnum)-0xFFFFFF) {
  mem_write_gnum(offset + 0, 'H', mem);
  mem_write_gnum(offset + 1, 'e', mem);
  mem_write_gnum(offset + 2, 'l', mem);
  mem_write_gnum(offset + 3, 'l', mem);
  mem_write_gnum(offset + 4, 'o', mem);
  if (mem_read_gnum(offset + 0, mem) != 'H' ||
   mem_read_gnum(offset + 1, mem) != 'e' ||
   mem_read_gnum(offset + 2, mem) != 'l' ||
   mem_read_gnum(offset + 3, mem) != 'l' ||
   mem_read_gnum(offset + 4, mem) != 'o') {
    return offset;
  }
  offset *= 10;
 }
 return 0;
}

variable_types: struct gleam_mem { struct mem_node { long long start; long long range; char *page; struct mem_node *prev; } base; struct mem_node *head; } *


decompiled_code:
int __fastcall print_part(const char *a1, __int64 a2)
{
const char *v2; // rbx
int result; // eax

v2 = (const char *)emu_decode_pos(a2);
printf("%s", a1);
if ( v2 )
result = printf("%s", v2);
else
result = printf("[%lld]", a2);
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, __int64 a2)
{
const char *v2; // rbx
int result; // eax

v2 = (const char *)emu_decode_pos(a2);
printf("%s", a1);
if ( v2 )
result = printf("%s", v2);
else
result = printf("[%lld]", a2);
return result;
}

original_code:
void print_part (const char *prefix, gnum pos) {
 char *decoded = emu_decode_pos(pos);
 printf("%s", prefix);
 if (decoded) {
  printf("%s", decoded);
 } else {
  printf("[%lld]", pos);
 }
}

canonical_original_code:
void print_part(char *prefix, long long pos) {
 char *decoded = emu_decode_pos(pos);
 printf("%s", prefix);
 if (decoded) {
  printf("%s", decoded);
 } else {
  printf("[%lld]", pos);
 }
}

variable_types:
prefix
char *

variable_types:
decoded
char *


decompiled_code:
int __fastcall gleam_debug(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
print_part((const char *)&unk_AE, a1);
print_part(" + ", a2);
print_part(" -> ", a3);
return printf(" (%lld)\n", a4);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
func0((const char *)&unk_AE, a1);
func0(" + ", a2);
func0(" -> ", a3);
return printf(" (%lld)\n", a4);
}

original_code:
void gleam_debug(gnum from, gnum add, gnum to, gnum val)
{
 print_part("", from);
 print_part(" + ", add);
 print_part(" -> ", to);
 printf(" (%lld)\n", val);
}

canonical_original_code:
void gleam_debug(long long from, long long add, long long to, long long val)
{
 print_part("", from);
 print_part(" + ", add);
 print_part(" -> ", to);
 printf(" (%lld)\n", val);
}


repo: andreasimonetto/rancio

decompiled_code:
__int64 __fastcall mi_firstsibling(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a2 )
return 0LL;
result = *(_QWORD *)(a2 + 8);
if ( result )
{
v3 = *(_QWORD *)(a2 + 16);
result = a1;
if ( v3 )
result = *(_QWORD *)(v3 + 24);
}
return result;
}

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

if ( !a2 )
return 0LL;
result = *(_QWORD *)(a2 + 8);
if ( result )
{
v3 = *(_QWORD *)(a2 + 16);
result = a1;
if ( v3 )
result = *(_QWORD *)(v3 + 24);
}
return result;
}

original_code:
static rancio_item_t* mi_firstsibling(rancio_item_t *root, rancio_item_t *item)
{
    if(!(!(item) || !((item)->label)))
        return (item->parent ? item->parent->childs : root);
    return 0;
}

canonical_original_code:
static struct _rancio_item *mi_firstsibling(struct _rancio_item *root, struct _rancio_item *item)
{
    if(!(!(item) || !((item)->label)))
        return (item->parent ? item->parent->childs : root);
    return 0;
}

variable_types:
root
struct _rancio_item *

variable_types:
item
struct _rancio_item *

return_type: struct _rancio_item { char *id; char *label; struct _rancio_item *parent; struct _rancio_item *childs; void (*handler)(struct _rancio_menu { struct _rancio_item *root; struct _rancio_item *curitem; void (*printer)(struct _rancio_menu *menu, char *label, unsigned int id, unsigned int len); void *userdata; } *menu, struct _rancio_item *item); } *

user_defined_types: struct _rancio_item { char *id; char *label; struct _rancio_item *parent; struct _rancio_item *childs; void (*handler)(struct _rancio_menu *menu, struct _rancio_item *item); }


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

result = mi_firstsibling(a1, a2);
if ( result )
{
if ( a2 - 40 < result )
{
result = 0LL;
}
else
{
result = *(_QWORD *)(a2 - 32);
if ( result )
result = a2 - 40;
}
}
return result;
}

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

result = func10(a1, a2);
if ( result )
{
if ( a2 - 40 < result )
{
result = 0LL;
}
else
{
result = *(_QWORD *)(a2 - 32);
if ( result )
result = a2 - 40;
}
}
return result;
}

original_code:
static rancio_item_t* mi_prevsibling(rancio_item_t *root, rancio_item_t *item)
{
rancio_item_t *prev_sibling = item - 1, *first_sibling = mi_firstsibling(root, item);
    if(first_sibling)
       return (prev_sibling >= first_sibling && prev_sibling->label ? prev_sibling : 0);
    return 0;
}

canonical_original_code:
static struct _rancio_item *mi_prevsibling(struct _rancio_item *root, struct _rancio_item *item)
{
struct _rancio_item *prev_sibling = item - 1; struct _rancio_item *first_sibling = mi_firstsibling(root, item);
    if(first_sibling)
       return (prev_sibling >= first_sibling && prev_sibling->label ? prev_sibling : 0);
    return 0;
}

variable_types: struct _rancio_item { char *id; char *label; struct _rancio_item *parent; struct _rancio_item *childs; void (*handler)(struct _rancio_menu { struct _rancio_item *root; struct _rancio_item *curitem; void (*printer)(struct _rancio_menu *menu, char *label, unsigned int id, unsigned int len); void *userdata; } *menu, struct _rancio_item *item); } *


decompiled_code:
__int64 __fastcall mi_init_r(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 24);
if ( v1 )
{
while ( *(_QWORD *)(v1 + 8) )
{
*(_QWORD *)(v1 + 16) = a1;
result = mi_init_r(v1);
v1 += 40LL;
}
}
return result;
}

original_code:
static void mi_init_r(rancio_item_t *root)
{
rancio_item_t *pchild;
    for(pchild = root->childs; !(!(pchild) || !((pchild)->label)); pchild++) {
        pchild->parent = root;
        mi_init_r(pchild);
    }
}

canonical_original_code:
static void mi_init_r(struct _rancio_item *root)
{
struct _rancio_item *pchild;
    for(pchild = root->childs; !(!(pchild) || !((pchild)->label)); pchild++) {
        pchild->parent = root;
        mi_init_r(pchild);
    }
}

user_defined_types: struct _rancio_menu { struct _rancio_item *root; struct _rancio_item *curitem; void (*printer)(struct _rancio_menu *menu, char *label, unsigned int id, unsigned int len); void *userdata; }


decompiled_code:
__int64 __fastcall rancio_menu_print(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int v3; // ebp
__int64 i; // rdx
unsigned int v5; // er13
__int64 v6; // rsi

result = mi_firstsibling(*(_QWORD *)a1, *(_QWORD *)(a1 + 8));
v2 = result;
v3 = 0;
if ( result )
{
while ( *(_QWORD *)(result + 8) )
{
++v3;
result += 40LL;
}
for ( i = 0LL; ; i = v5 )
{
v6 = *(_QWORD *)(v2 + 8);
if ( !v6 )
break;
v5 = i + 1;
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD))(a1 + 16))(a1, v6, i, v3);
v2 += 40LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int v3; // ebp
__int64 i; // rdx
unsigned int v5; // er13
__int64 v6; // rsi

result = func10(*(_QWORD *)a1, *(_QWORD *)(a1 + 8));
v2 = result;
v3 = 0;
if ( result )
{
while ( *(_QWORD *)(result + 8) )
{
++v3;
result += 40LL;
}
for ( i = 0LL; ; i = v5 )
{
v6 = *(_QWORD *)(v2 + 8);
if ( !v6 )
break;
v5 = i + 1;
result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD))(a1 + 16))(a1, v6, i, v3);
v2 += 40LL;
}
}
return result;
}

original_code:
void rancio_menu_print(rancio_menu_t *menu)
{
rancio_item_t *p;
unsigned i = 0, len = 0;
    for(p = mi_firstsibling(menu->root, menu->curitem); !(!(p) || !((p)->label)); p++)
        len++;
    for(p = mi_firstsibling(menu->root, menu->curitem); !(!(p) || !((p)->label)); p++)
        menu->printer(menu, p->label, i++, len);
}

canonical_original_code:
void rancio_menu_print(struct _rancio_menu *menu)
{
struct _rancio_item *p;
unsigned int i = 0; unsigned int len = 0;
    for(p = mi_firstsibling(menu->root, menu->curitem); !(!(p) || !((p)->label)); p++)
        len++;
    for(p = mi_firstsibling(menu->root, menu->curitem); !(!(p) || !((p)->label)); p++)
        menu->printer(menu, p->label, i++, len);
}

variable_types: struct _rancio_menu { struct _rancio_item { char *id; char *label; struct _rancio_item *parent; struct _rancio_item *childs; void (*handler)(struct _rancio_menu *menu, struct _rancio_item *item); } *root; struct _rancio_item *curitem; void (*printer)(struct _rancio_menu *menu, char *label, unsigned int id, unsigned int len); void *userdata; } *

variable_types:
i
unsigned int

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall rancio_menu_action(__int64 *a1, int a2)
{
__int64 result; // rax
__int64 v3; // rsi
void (*v4)(void); // rdx
unsigned __int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx

switch ( a2 )
{
case 0:
result = 1LL;
break;
case 1:
v3 = a1[1];
if ( v3 && !*(_QWORD *)(v3 + 24) )
{
v4 = *(void (**)(void))(v3 + 32);
result = 1LL;
if ( v4 )
{
v4();
result = 1LL;
}
}
else
{
a1[1] = *(_QWORD *)(v3 + 24);
result = 1LL;
}
break;
case 2:
v5 = mi_prevsibling(*a1, a1[1]);
result = 1LL;
if ( v5 )
a1[1] = v5;
break;
case 3:
v6 = mi_nextsibling(*a1, a1[1]);
result = 1LL;
if ( v6 )
a1[1] = v6;
break;
case 4:
v7 = a1[1];
if ( !v7 || (result = 1LL, *(_QWORD *)(v7 + 16)) )
{
a1[1] = *(_QWORD *)(v7 + 16);
result = 1LL;
}
break;
case 5:
result = 0LL;
break;
default:
result = 1LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 *a1, int a2)
{
__int64 result; // rax
__int64 v3; // rsi
void (*v4)(void); // rdx
unsigned __int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx

switch ( a2 )
{
case 0:
result = 1LL;
break;
case 1:
v3 = a1[1];
if ( v3 && !*(_QWORD *)(v3 + 24) )
{
v4 = *(void (**)(void))(v3 + 32);
result = 1LL;
if ( v4 )
{
v4();
result = 1LL;
}
}
else
{
a1[1] = *(_QWORD *)(v3 + 24);
result = 1LL;
}
break;
case 2:
v5 = func12(*a1, a1[1]);
result = 1LL;
if ( v5 )
a1[1] = v5;
break;
case 3:
v6 = func11(*a1, a1[1]);
result = 1LL;
if ( v6 )
a1[1] = v6;
break;
case 4:
v7 = a1[1];
if ( !v7 || (result = 1LL, *(_QWORD *)(v7 + 16)) )
{
a1[1] = *(_QWORD *)(v7 + 16);
result = 1LL;
}
break;
case 5:
result = 0LL;
break;
default:
result = 1LL;
break;
}
return result;
}

original_code:
int rancio_menu_action(rancio_menu_t *menu, rancio_action_t act)
{
rancio_item_t *p;
    switch(act) {
        case RANCIO_ACTION_SELECT:
            if(((menu->curitem) && !((menu->curitem)->childs))) {
                if(menu->curitem->handler)
                    menu->curitem->handler(menu, menu->curitem);
            }
            else
                menu->curitem = ((menu->curitem)->childs);
            break;
        case RANCIO_ACTION_UP:
            if((p = mi_prevsibling(menu->root, menu->curitem)))
                menu->curitem = p;
            break;
        case RANCIO_ACTION_DOWN:
            if((p = mi_nextsibling(menu->root, menu->curitem)))
                menu->curitem = p;
            break;
        case RANCIO_ACTION_BACK:
            if(!((menu->curitem) && !((menu->curitem)->parent)))
                menu->curitem = ((menu->curitem)->parent);
            break;
        case RANCIO_ACTION_CLOSE:
            return 0;
            break;
        case RANCIO_ACTION_NONE:
        default:
            break;
    }
    return 1;
}

canonical_original_code:
int rancio_menu_action(struct _rancio_menu *menu, enum rancio_action_t act)
{
struct _rancio_item *p;
    switch(act) {
        case RANCIO_ACTION_SELECT:
            if(((menu->curitem) && !((menu->curitem)->childs))) {
                if(menu->curitem->handler)
                    menu->curitem->handler(menu, menu->curitem);
            }
            else
                menu->curitem = ((menu->curitem)->childs);
            break;
        case RANCIO_ACTION_UP:
            if((p = mi_prevsibling(menu->root, menu->curitem)))
                menu->curitem = p;
            break;
        case RANCIO_ACTION_DOWN:
            if((p = mi_nextsibling(menu->root, menu->curitem)))
                menu->curitem = p;
            break;
        case RANCIO_ACTION_BACK:
            if(!((menu->curitem) && !((menu->curitem)->parent)))
                menu->curitem = ((menu->curitem)->parent);
            break;
        case RANCIO_ACTION_CLOSE:
            return 0;
            break;
        case RANCIO_ACTION_NONE:
        default:
            break;
    }
    return 1;
}

variable_types: enum rancio_action_t { RANCIO_ACTION_NONE=0, RANCIO_ACTION_SELECT=1, RANCIO_ACTION_UP=2, RANCIO_ACTION_DOWN=3, RANCIO_ACTION_BACK=4, RANCIO_ACTION_CLOSE=5 }


decompiled_code:
unsigned __int64 __fastcall rancio_menu_curitem_id(__int64 *a1)
{
__int64 v1; // rbx

v1 = a1[1];
return 0xCCCCCCCCCCCCCCCDLL * ((v1 - mi_firstsibling(*a1, v1)) >> 3);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func16(__int64 *a1)
{
__int64 v1; // rbx

v1 = a1[1];
return 0xCCCCCCCCCCCCCCCDLL * ((v1 - func10(*a1, v1)) >> 3);
}

original_code:
unsigned rancio_menu_curitem_id(rancio_menu_t *menu)
{
    return (menu->curitem - mi_firstsibling(menu->root, menu->curitem));
}

canonical_original_code:
unsigned int rancio_menu_curitem_id(struct _rancio_menu *menu)
{
    return (menu->curitem - mi_firstsibling(menu->root, menu->curitem));
}

return_type: unsigned int


fastInsertEntry

original_code:
static char fastInsertEntry(HashTable T, HashEntry E, unsigned long hashVal,
       char allowDups) {
  int c = 0;
  HashEntry P, N;
  hashVal %= T->size;
  for (P = ((void *)0), N = T->bin[hashVal];
       N && (c = T->entryCompare(N->key, E->key)) < 0; P = N, N = N->next);
  if (!allowDups && N && c == 0) {
    N->data = E->data;
    smartFree(E, T->memType);
    return 0;
  }
  if (!P) T->bin[hashVal] = E;
  else P->next = E;
  E->next = N;
  return 1;
}

canonical_original_code:
static char fastInsertEntry(struct hashTable *T, struct hashEntry *E, unsigned long hashVal, char allowDups) {
  int c = 0;
  struct hashEntry *P; struct hashEntry *N;
  hashVal %= T->size;
  for (P = ((void *)0), N = T->bin[hashVal];
       N && (c = T->entryCompare(N->key, E->key)) < 0; P = N, N = N->next);
  if (!allowDups && N && c == 0) {
    N->data = E->data;
    smartFree(E, T->memType);
    return 0;
  }
  if (!P) T->bin[hashVal] = E;
  else P->next = E;
  E->next = N;
  return 1;
}

variable_types: struct hashTable { int memType; unsigned long size; unsigned long entries; float maxAvgBinSize; unsigned long (*hashFunc)(void *, unsigned long); int (*entryCompare)(void *, void *); struct hashEntry { void *key; void *data; struct hashEntry *next; } **bin; } *

variable_types:
E
struct hashEntry *

variable_types:
hashVal
unsigned long

variable_types: allowDups char

variable_types: c int

return_type: char

user_defined_types: struct hashTable { int memType; unsigned long size; unsigned long entries; float maxAvgBinSize; unsigned long (*hashFunc)(void *, unsigned long); int (*entryCompare)(void *, void *); struct hashEntry **bin; }


insertEntry

decompiled_code:
__int64 __fastcall insertEntry(__int64 a1, _QWORD *a2, char a3)
{
unsigned __int64 v4; // rax

v4 = (*(__int64 (__fastcall **)(_QWORD, _QWORD))(a1 + 32))(*a2, *(_QWORD *)(a1 + 8));
return fastInsertEntry((unsigned int *)a1, a2, v4, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, _QWORD *a2, char a3)
{
unsigned __int64 v4; // rax

v4 = (*(__int64 (__fastcall **)(_QWORD, _QWORD))(a1 + 32))(*a2, *(_QWORD *)(a1 + 8));
return func0((unsigned int *)a1, a2, v4, a3);
}

original_code:
static char insertEntry(HashTable T, HashEntry E, char allowDups) {
  return fastInsertEntry(T, E, T->hashFunc(E->key, T->size), allowDups);
}

canonical_original_code:
static char insertEntry(struct hashTable *T, struct hashEntry *E, char allowDups) {
  return fastInsertEntry(T, E, T->hashFunc(E->key, T->size), allowDups);
}

user_defined_types: struct hashEntry { void *key; void *data; struct hashEntry *next; }


hashTableCreate

decompiled_code:
__int64 __fastcall hashTableCreate(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, __m128i a5)
{
__int64 v7; // rbx
__int64 v8; // rax

v7 = smartCalloc(1LL, 56LL, a4);
*(_DWORD *)v7 = a4;
*(_QWORD *)(v7 + 8) = a1;
*(_DWORD *)(v7 + 24) = _mm_cvtsi128_si32(a5);
*(_QWORD *)(v7 + 32) = a2;
*(_QWORD *)(v7 + 40) = a3;
v8 = smartCalloc((unsigned int)a1, 8LL, a4);
*(_QWORD *)(v7 + 48) = v8;
if ( !v8 )
{
smartFree(v7, a4);
v7 = 0LL;
}
return v7;
}

original_code:
HashTable hashTableCreate(unsigned long initialSize, float maxAvgBinSize,
     unsigned long (*hashFunc)(void *, unsigned long),
     int (*entryCompare)(void *, void *), int memType) {
  HashTable T = (HashTable) smartCalloc(1, sizeof(struct hashTable), memType);
  T->memType = memType;
  T->size = initialSize;
  T->maxAvgBinSize = maxAvgBinSize;
  T->hashFunc = hashFunc;
  T->entryCompare = entryCompare;
  T->bin = (HashEntry *) smartCalloc(initialSize, sizeof(HashEntry), memType);
  if (!T->bin) {
    smartFree(T, memType);
    return ((void *)0);
  }
  return T;
}

canonical_original_code:
struct hashTable *hashTableCreate(unsigned long initialSize, float maxAvgBinSize, unsigned long (*hashFunc)(void *, unsigned long), int (*entryCompare)(void *, void *), int memType) {
  struct hashTable *T = (HashTable) smartCalloc(1, sizeof(struct hashTable), memType);
  T->memType = memType;
  T->size = initialSize;
  T->maxAvgBinSize = maxAvgBinSize;
  T->hashFunc = hashFunc;
  T->entryCompare = entryCompare;
  T->bin = (struct hashEntry **) smartCalloc(initialSize, sizeof(HashEntry), memType);
  if (!T->bin) {
    smartFree(T, memType);
    return ((void *)0);
  }
  return T;
}

variable_types:
initialSize
unsigned long

variable_types: maxAvgBinSize float

variable_types: unsigned long (*)(void *, unsigned long)

variable_types:
entryCompare
int (*)(void *, void *)

variable_types: memType int

return_type: struct hashTable { int memType; unsigned long size; unsigned long entries; float maxAvgBinSize; unsigned long (*hashFunc)(void *, unsigned long); int (*entryCompare)(void *, void *); struct hashEntry { void *key; void *data; struct hashEntry *next; } **bin; } *


hashTableFree

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

if ( a1 )
{
hashTableClear(a1);
smartFree(a1[6], *(unsigned int *)a1);
result = smartFree(a1, *(unsigned int *)a1);
}
return result;
}

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

if ( a1 )
{
func3(a1);
smartFree(a1[6], *(unsigned int *)a1);
result = smartFree(a1, *(unsigned int *)a1);
}
return result;
}

original_code:
void hashTableFree(HashTable T) {
  if (!T) return;
  hashTableClear(T);
  smartFree(T->bin, T->memType);
  smartFree(T, T->memType);
}

canonical_original_code:
void hashTableFree(struct hashTable *T) {
  if (!T) return;
  hashTableClear(T);
  smartFree(T->bin, T->memType);
  smartFree(T, T->memType);
}


hashVal

original_code:
unsigned long hashVal(HashTable T, void *key) {
  return T->hashFunc(key, T->size);
}

canonical_original_code:
unsigned long hashVal(struct hashTable *T, void *key) {
  return T->hashFunc(key, T->size);
}

variable_types:
key
void *

return_type: unsigned long


hashTableFastLookup

decompiled_code:
__int64 __fastcall hashTableFastLookup(__int64 a1, __int64 a2, unsigned __int64 a3)
{
_QWORD *v3; // rbx
int v4; // eax
__int64 result; // rax

if ( !a1 )
return 0LL;
v3 = *(_QWORD **)(*(_QWORD *)(a1 + 48) + 8 * (a3 % *(_QWORD *)(a1 + 8)));
if ( !v3 )
return 0LL;
while ( 1 )
{
v4 = (*(__int64 (__fastcall **)(_QWORD, __int64))(a1 + 40))(*v3, a2);
if ( v4 >= 0 )
break;
v3 = (_QWORD *)v3[2];
if ( !v3 )
return 0LL;
}
if ( v4 )
result = 0LL;
else
result = v3[1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, unsigned __int64 a3)
{
_QWORD *v3; // rbx
int v4; // eax
__int64 result; // rax

if ( !a1 )
return 0LL;
v3 = *(_QWORD **)(*(_QWORD *)(a1 + 48) + 8 * (a3 % *(_QWORD *)(a1 + 8)));
if ( !v3 )
return 0LL;
while ( 1 )
{
v4 = (*(__int64 (__fastcall **)(_QWORD, __int64))(a1 + 40))(*v3, a2);
if ( v4 >= 0 )
break;
v3 = (_QWORD *)v3[2];
if ( !v3 )
return 0LL;
}
if ( v4 )
result = 0LL;
else
result = v3[1];
return result;
}

original_code:
void *hashTableFastLookup(HashTable T, void *key, unsigned long hashVal) {
  int val = -1;
  HashEntry E;
  if (!T) return ((void *)0);
  hashVal %= T->size;
  for (E = T->bin[hashVal]; E && (val = T->entryCompare(E->key, key)) < 0;
       E = E->next);
  if (!E) return ((void *)0);
  if (val == 0) return E->data;
  return ((void *)0);
}

canonical_original_code:
void *hashTableFastLookup(struct hashTable *T, void *key, unsigned long hashVal) {
  int val = -1;
  struct hashEntry *E;
  if (!T) return ((void *)0);
  hashVal %= T->size;
  for (E = T->bin[hashVal]; E && (val = T->entryCompare(E->key, key)) < 0;
       E = E->next);
  if (!E) return ((void *)0);
  if (val == 0) return E->data;
  return ((void *)0);
}

variable_types: val int

variable_types: struct hashEntry { void *key; void *data; struct hashEntry *next; } *

return_type: void *


hashTableLookup

decompiled_code:
__int64 __fastcall hashTableLookup(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rax

if ( !a1 )
return 0LL;
v2 = (*(__int64 (__fastcall **)(__int64, _QWORD))(a1 + 32))(a2, *(_QWORD *)(a1 + 8));
return hashTableFastLookup(a1, a2, v2);
}

original_code:
void *hashTableLookup(HashTable T, void *key) {
  if (!T) return ((void *)0);
  return hashTableFastLookup(T, key, T->hashFunc(key, T->size));
}

canonical_original_code:
void *hashTableLookup(struct hashTable *T, void *key) {
  if (!T) return ((void *)0);
  return hashTableFastLookup(T, key, T->hashFunc(key, T->size));
}


hashTableGrow

original_code:
void hashTableGrow(HashTable T) {
  HashEntry E, N, *oldBin = T->bin;
  unsigned long i, oldSize = T->size;
  T->size *= 2;
  T->bin = (HashEntry *) smartCalloc(T->size, sizeof(HashEntry), T->memType);
  for (i = 0; i < oldSize; i++)
    for (E = oldBin[i]; E; E = N) {
      N = E->next;
      insertEntry(T, E, 1);
    }
  smartFree(oldBin, T->memType);
}

canonical_original_code:
void hashTableGrow(struct hashTable *T) {
  struct hashEntry *E; struct hashEntry *N; struct hashEntry **oldBin = T->bin;
  unsigned long i; unsigned long oldSize = T->size;
  T->size *= 2;
  T->bin = (struct hashEntry **) smartCalloc(T->size, sizeof(HashEntry), T->memType);
  for (i = 0; i < oldSize; i++)
    for (E = oldBin[i]; E; E = N) {
      N = E->next;
      insertEntry(T, E, 1);
    }
  smartFree(oldBin, T->memType);
}

variable_types: struct hashEntry { void *key; void *data; struct hashEntry *next; } **

variable_types:
i
unsigned long

variable_types:
oldSize
unsigned long


hashTableInsert

decompiled_code:
__int64 __fastcall hashTableInsert(unsigned int *a1, __int64 a2, __int64 a3, char a4)
{
__int64 v6; // rax
float v7; // xmm1_4
__int64 v8; // rax
float v9; // xmm0_4
_QWORD *v10; // rax
__int64 v12; // rdx
__int64 v13; // rdx

if ( !a1 )
return 1LL;
v6 = *((_QWORD *)a1 + 2);
if ( v6 < 0 )
{
v12 = *((_QWORD *)a1 + 2) & 1LL | ((unsigned __int64)v6 >> 1);
v7 = (float)(int)v12 + (float)(int)v12;
}
else
{
v7 = (float)(int)v6;
}
v8 = *((_QWORD *)a1 + 1);
if ( v8 < 0 )
{
v13 = *((_QWORD *)a1 + 1) & 1LL | ((unsigned __int64)v8 >> 1);
v9 = (float)(int)v13 + (float)(int)v13;
}
else
{
v9 = (float)(int)v8;
}
if ( v7 >= (float)(v9 * *((float *)a1 + 6)) )
hashTableGrow(a1);
v10 = (_QWORD *)smartMalloc(24LL, *a1);
*v10 = a2;
v10[1] = a3;
*((_QWORD *)a1 + 2) += (char)insertEntry((__int64)a1, v10, a4);
return 0LL;
}

original_code:
char hashTableInsert(HashTable T, void *key, void *data, char allowDups) {
  HashEntry E;
  if (!T) return 1;
  if (T->entries >= (T->maxAvgBinSize * T->size))
    hashTableGrow(T);
  E = (HashEntry) smartMalloc(sizeof(struct hashEntry), T->memType);
  E->key = key;
  E->data = data;
  T->entries += insertEntry(T, E, allowDups);
  return 0;
}

canonical_original_code:
char hashTableInsert(struct hashTable *T, void *key, void *data, char allowDups) {
  struct hashEntry *E;
  if (!T) return 1;
  if (T->entries >= (T->maxAvgBinSize * T->size))
    hashTableGrow(T);
  E = (struct hashEntry *) smartMalloc(sizeof(struct hashEntry), T->memType);
  E->key = key;
  E->data = data;
  T->entries += insertEntry(T, E, allowDups);
  return 0;
}

variable_types:
data
void *


hashTableFastInsert

decompiled_code:
__int64 __fastcall hashTableFastInsert(unsigned int *a1, __int64 a2, __int64 a3, unsigned __int64 a4, char a5)
{
__int64 v8; // rax
float v9; // xmm1_4
__int64 v10; // rax
float v11; // xmm0_4
_QWORD *v12; // rax
__int64 v14; // rdx
__int64 v15; // rdx

if ( !a1 )
return 1LL;
v8 = *((_QWORD *)a1 + 2);
if ( v8 < 0 )
{
v14 = *((_QWORD *)a1 + 2) & 1LL | ((unsigned __int64)v8 >> 1);
v9 = (float)(int)v14 + (float)(int)v14;
}
else
{
v9 = (float)(int)v8;
}
v10 = *((_QWORD *)a1 + 1);
if ( v10 < 0 )
{
v15 = *((_QWORD *)a1 + 1) & 1LL | ((unsigned __int64)v10 >> 1);
v11 = (float)(int)v15 + (float)(int)v15;
}
else
{
v11 = (float)(int)v10;
}
if ( v9 >= (float)(v11 * *((float *)a1 + 6)) )
{
hashTableGrow(a1);
a4 = (*((__int64 (__fastcall **)(__int64, _QWORD))a1 + 4))(a2, *((_QWORD *)a1 + 1));
}
v12 = (_QWORD *)smartMalloc(24LL, *a1);
*v12 = a2;
v12[1] = a3;
*((_QWORD *)a1 + 2) += (char)fastInsertEntry(a1, v12, a4, a5);
return 0LL;
}

original_code:
char hashTableFastInsert(HashTable T, void *key, void *data,
    unsigned long hashVal, char allowDups) {
  HashEntry E;
  if (!T) return 1;
  if (T->entries >= (T->maxAvgBinSize * T->size)) {
    hashTableGrow(T);
    hashVal = T->hashFunc(key, T->size);
  }
  E = (HashEntry) smartMalloc(sizeof(struct hashEntry), T->memType);
  E->key = key;
  E->data = data;
  T->entries += fastInsertEntry(T, E, hashVal, allowDups);
  return 0;
}

canonical_original_code:
char hashTableFastInsert(struct hashTable *T, void *key, void *data, unsigned long hashVal, char allowDups) {
  struct hashEntry *E;
  if (!T) return 1;
  if (T->entries >= (T->maxAvgBinSize * T->size)) {
    hashTableGrow(T);
    hashVal = T->hashFunc(key, T->size);
  }
  E = (struct hashEntry *) smartMalloc(sizeof(struct hashEntry), T->memType);
  E->key = key;
  E->data = data;
  T->entries += fastInsertEntry(T, E, hashVal, allowDups);
  return 0;
}

variable_types: struct hashTable { int memType; unsigned long size; unsigned long entries; float maxAvgBinSize; unsigned long (*hashFunc)(void *, unsigned long); int (*entryCompare)(void *, void *); struct hashEntry { void *key; void *data; struct hashEntry *next; } **bin; } *


hashTableRemove

original_code:
void *hashTableRemove(HashTable T, void *key) {
  int val = -1;
  HashEntry P, E;
  void *data;
  unsigned long hashVal;
  if (!T) return ((void *)0);
  hashVal = T->hashFunc(key, T->size);
  for (P = ((void *)0), E = T->bin[hashVal];
       E && (val = T->entryCompare(E->key, key)) < 0; P = E, E = E->next);
  if (E && val == 0) {
    if (P) P->next = E->next;
    else T->bin[hashVal] = E->next;
    data = E->data;
    smartFree(E, T->memType);
    T->entries--;
    return data;
  } else return ((void *)0);
}

canonical_original_code:
void *hashTableRemove(struct hashTable *T, void *key) {
  int val = -1;
  struct hashEntry *P; struct hashEntry *E;
  void *data;
  unsigned long hashVal;
  if (!T) return ((void *)0);
  hashVal = T->hashFunc(key, T->size);
  for (P = ((void *)0), E = T->bin[hashVal];
       E && (val = T->entryCompare(E->key, key)) < 0; P = E, E = E->next);
  if (E && val == 0) {
    if (P) P->next = E->next;
    else T->bin[hashVal] = E->next;
    data = E->data;
    smartFree(E, T->memType);
    T->entries--;
    return data;
  } else return ((void *)0);
}


hashTableForEach

original_code:
void hashTableForEach(HashTable T,
        char (*func)(HashTable, void *, void *, void *),
        void *userData) {
  unsigned long i;
  HashEntry P, E, N;
  if (!T) return;
  for (i = 0; i < T->size; i++)
    for (E = T->bin[i], P = ((void *)0); E; E = N) {
      N = E->next;
      if (func(T, E->key, E->data, userData)) {
 if (!P) T->bin[i] = N;
 else P->next = N;
 smartFree(E, T->memType);
 T->entries--;
      } else P = E;
    }
}

canonical_original_code:
void hashTableForEach(struct hashTable *T, char (*func)(struct hashTable *, void *, void *, void *), void *userData) {
  unsigned long i;
  struct hashEntry *P; struct hashEntry *E; struct hashEntry *N;
  if (!T) return;
  for (i = 0; i < T->size; i++)
    for (E = T->bin[i], P = ((void *)0); E; E = N) {
      N = E->next;
      if (func(T, E->key, E->data, userData)) {
 if (!P) T->bin[i] = N;
 else P->next = N;
 smartFree(E, T->memType);
 T->entries--;
      } else P = E;
    }
}

variable_types: char (*)(struct hashTable *, void *, void *, void *)

variable_types:
userData
void *


hashTableForEachMatch

original_code:
void hashTableForEachMatch(HashTable T, void *key,
      char (*func)(HashTable, void *, void *, void *),
      void *userData) {
  int val;
  HashEntry P, E, N;
  unsigned long hashVal;
  if (!T) return;
  hashVal = T->hashFunc(key, T->size);
  for (E = T->bin[hashVal], P = ((void *)0);
       E && (val = T->entryCompare(E->key, key)) <= 0; E = N) {
    N = E->next;
    if (val == 0 && func(T, E->key, E->data, userData)) {
      if (!P) T->bin[hashVal] = N;
      else P->next = N;
      smartFree(E, T->memType);
      T->entries--;
    } else P = E;
  }
}

canonical_original_code:
void hashTableForEachMatch(struct hashTable *T, void *key, char (*func)(struct hashTable *, void *, void *, void *), void *userData) {
  int val;
  struct hashEntry *P; struct hashEntry *E; struct hashEntry *N;
  unsigned long hashVal;
  if (!T) return;
  hashVal = T->hashFunc(key, T->size);
  for (E = T->bin[hashVal], P = ((void *)0);
       E && (val = T->entryCompare(E->key, key)) <= 0; E = N) {
    N = E->next;
    if (val == 0 && func(T, E->key, E->data, userData)) {
      if (!P) T->bin[hashVal] = N;
      else P->next = N;
      smartFree(E, T->memType);
      T->entries--;
    } else P = E;
  }
}

variable_types:
hashVal
unsigned long


hashTableNumEntries

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

if ( a1 )
result = *(_QWORD *)(a1 + 16);
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)(a1 + 16);
else
result = 0LL;
return result;
}

original_code:
unsigned long hashTableNumEntries(HashTable T) {
  if (!T) return 0;
  return T->entries;
}

canonical_original_code:
unsigned long hashTableNumEntries(struct hashTable *T) {
  if (!T) return 0;
  return T->entries;
}


hashInt

decompiled_code:
__int64 __fastcall hashInt(int a1, unsigned __int64 a2)
{
return (int)abs32(a1 % a2);
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1, unsigned __int64 a2)
{
return (int)abs32(a1 % a2);
}

original_code:
unsigned long hashInt(void *v, unsigned long n) {
  return abs(((int) v) % n);
}

variable_types:
v
void *

variable_types:
n
unsigned long


compareInts

decompiled_code:
__int64 __fastcall compareInts(int a1, int a2)
{
return (unsigned int)(a1 - a2);
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1, int a2)
{
return (unsigned int)(a1 - a2);
}

original_code:
int compareInts(void *a, void *b) {
  return (int) a - (int) b;
}

variable_types:
a
void *

variable_types:
b
void *


hashString

decompiled_code:
unsigned __int64 __fastcall hashString(char *a1, unsigned __int64 a2)
{
char v2; // dl
unsigned __int64 v3; // rax

v2 = *a1;
if ( *a1 )
{
v3 = 0LL;
do
{
v3 = v2 + 31 * v3;
v2 = *++a1;
}
while ( *a1 );
}
else
{
v3 = 0LL;
}
return v3 % a2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func17(char *a1, unsigned __int64 a2)
{
char v2; // dl
unsigned __int64 v3; // rax

v2 = *a1;
if ( *a1 )
{
v3 = 0LL;
do
{
v3 = v2 + 31 * v3;
v2 = *++a1;
}
while ( *a1 );
}
else
{
v3 = 0LL;
}
return v3 % a2;
}

original_code:
unsigned long hashString(void *v, unsigned long n) {
  unsigned long val;
  char *s = (char *) v;
  for (val = 0; *s; s++)
    val = *s + 31 * val;
  return val % n;
}

variable_types:
val
unsigned long

variable_types:
s
char *


compareStrings

decompiled_code:
int __fastcall compareStrings(const char *a1, const char *a2)
{
return strcmp(a1, a2);
}

canonical_decompiled_code:
int __fastcall func18(const char *a1, const char *a2)
{
return strcmp(a1, a2);
}

original_code:
int compareStrings(void *a, void *b) {
  return strcmp((char *) a, (char *) b);
}


hashTableAvgBinSize

original_code:
real hashTableAvgBinSize(HashTable T) {
  unsigned long i, n, cost = 0;
  HashEntry E;
  if (!T) return 0.0;
  for (i = 0; i < T->size; i++) {
    for (E = T->bin[i], n = 0; E; E = E->next) n++;
    cost += n * n;
  }
  return ((real) sqrt((double) ((real) cost / T->entries)));
}

canonical_original_code:
float hashTableAvgBinSize(struct hashTable *T) {
  unsigned long i; unsigned long n; unsigned long cost = 0;
  struct hashEntry *E;
  if (!T) return 0.0;
  for (i = 0; i < T->size; i++) {
    for (E = T->bin[i], n = 0; E; E = E->next) n++;
    cost += n * n;
  }
  return ((float) sqrt((double) ((float) cost / T->entries)));
}

variable_types:
cost
unsigned long

return_type: float


decompiled_code:
__int64 __fastcall path_has_rooms_with_degree_lt_2(int a1)
{
int v1; // er11
int v3; // edi
int v4; // er8
int v5; // er9
__int64 v6; // rsi
int v7; // ecx
int v8; // er10

v1 = height * width;
if ( height * width <= 0 )
return 0LL;
v3 = width;
v4 = -width;
v5 = -1;
v6 = 1LL;
v7 = 0;
while ( 1 )
{
if ( !*((char *)visited + v6 - 1) && end != v7 )
{
v8 = 0;
if ( v4 >= 0 )
v8 = v4 == a1 || *((_BYTE *)visited + v4) == 0;
if ( v1 > (int)v6 && v7 / width == (int)v6 / width && (!*((_BYTE *)visited + v6) || a1 == (_DWORD)v6) )
++v8;
if ( v3 < v1 && (!*((_BYTE *)visited + v3) || v3 == a1) )
++v8;
if ( v7 > 0 && v7 / width == v5 / width && (!*((char *)visited + v6 - 2) || v5 == a1) )
++v8;
if ( v8 <= 1 )
break;
}
++v7;
++v6;
++v5;
++v3;
++v4;
if ( v7 == v1 )
return 0LL;
}
return 1LL;
}

original_code:
int path_has_rooms_with_degree_lt_2(int crt) {
  int degree, i;
  for (i = 0; i < width * height; ++i) {
    degree = 0;
    if ((visited[i] == 0) && i != end) {
      if (((i - width) >= 0) && ((visited[(i - width)] == 0) || (i - width) == crt))
        ++degree;
      if (((i + 1) < width * height && (i / width == (i + 1) / width)) && ((visited[(i + 1)] == 0) || (i + 1) == crt))
        ++degree;
      if (((i + width) < width * height) && ((visited[(i + width)] == 0) || (i + width) == crt))
        ++degree;
      if (((i - 1) >= 0 && (i / width == (i - 1) / width)) && ((visited[(i - 1)] == 0) || (i - 1) == crt))
        ++degree;
      if (degree < 2)
        return 1;
    }
  }
  return 0;
}

canonical_original_code:
int path_has_rooms_with_degree_lt_2(int crt) {
  int degree; int i;
  for (i = 0; i < width * height; ++i) {
    degree = 0;
    if ((visited[i] == 0) && i != end) {
      if (((i - width) >= 0) && ((visited[(i - width)] == 0) || (i - width) == crt))
        ++degree;
      if (((i + 1) < width * height && (i / width == (i + 1) / width)) && ((visited[(i + 1)] == 0) || (i + 1) == crt))
        ++degree;
      if (((i + width) < width * height) && ((visited[(i + width)] == 0) || (i + width) == crt))
        ++degree;
      if (((i - 1) >= 0 && (i / width == (i - 1) / width)) && ((visited[(i - 1)] == 0) || (i - 1) == crt))
        ++degree;
      if (degree < 2)
        return 1;
    }
  }
  return 0;
}

variable_types: crt int

variable_types: degree int

variable_types: i int


decompiled_code:
__int64 __fastcall count_paths(int a1, int a2)
{
unsigned int v3; // ebp
__int64 v4; // rdi
__int64 v5; // rdi
__int64 v6; // rdi

if ( end != a1 || (v3 = 1, path_length != a2) )
{
v3 = path_has_rooms_with_degree_lt_2(a1);
if ( v3 )
{
v3 = 0;
}
else
{
v4 = (unsigned int)(a1 - width);
if ( a1 - width >= 0 && !*((_BYTE *)visited + (int)v4) )
v3 = go_into_room(v4, (unsigned int)(a2 + 1));
v5 = (unsigned int)(a1 + 1);
if ( (int)v5 < height * width && a1 / width == (int)v5 / width && !*((_BYTE *)visited + a1 + 1) )
v3 += go_into_room(v5, (unsigned int)(a2 + 1));
v6 = (unsigned int)(width + a1);
if ( (int)v6 < height * width && !*((_BYTE *)visited + (int)v6) )
v3 += go_into_room(v6, (unsigned int)(a2 + 1));
if ( a1 > 0 && a1 / width == (a1 - 1) / width && !*((char *)visited + a1 - 1) )
v3 += go_into_room((unsigned int)(a1 - 1), (unsigned int)(a2 + 1));
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
unsigned int v3; // ebp
__int64 v4; // rdi
__int64 v5; // rdi
__int64 v6; // rdi

if ( end != a1 || (v3 = 1, path_length != a2) )
{
v3 = func7(a1);
if ( v3 )
{
v3 = 0;
}
else
{
v4 = (unsigned int)(a1 - width);
if ( a1 - width >= 0 && !*((_BYTE *)visited + (int)v4) )
v3 = func9(v4, (unsigned int)(a2 + 1));
v5 = (unsigned int)(a1 + 1);
if ( (int)v5 < height * width && a1 / width == (int)v5 / width && !*((_BYTE *)visited + a1 + 1) )
v3 += func9(v5, (unsigned int)(a2 + 1));
v6 = (unsigned int)(width + a1);
if ( (int)v6 < height * width && !*((_BYTE *)visited + (int)v6) )
v3 += func9(v6, (unsigned int)(a2 + 1));
if ( a1 > 0 && a1 / width == (a1 - 1) / width && !*((char *)visited + a1 - 1) )
v3 += func9((unsigned int)(a1 - 1), (unsigned int)(a2 + 1));
}
}
return v3;
}

original_code:
int count_paths(int crt, int length) {
  if (crt == end && length == path_length)
    return 1;
  if (path_has_rooms_with_degree_lt_2(crt))
    return 0;
  int number_of_paths = 0;
  if (((crt - width) >= 0) && (visited[(crt - width)] == 0))
    number_of_paths += go_into_room((crt - width), length + 1);
  if (((crt + 1) < width * height && (crt / width == (crt + 1) / width)) && (visited[(crt + 1)] == 0))
    number_of_paths += go_into_room((crt + 1), length + 1);
  if (((crt + width) < width * height) && (visited[(crt + width)] == 0))
    number_of_paths += go_into_room((crt + width), length + 1);
  if (((crt - 1) >= 0 && (crt / width == (crt - 1) / width)) && (visited[(crt - 1)] == 0))
    number_of_paths += go_into_room((crt - 1), length + 1);
  return number_of_paths;
}

variable_types: length int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 i; // rbx
unsigned int v5; // eax
int v7; // [rsp+Ch] [rbp-1Ch]

__isoc99_scanf();
v3 = width * height;
visited = calloc(1uLL, width * height);
if ( v3 > 0 )
{
for ( i = 0LL; height * width > (int)i; ++i )
{
__isoc99_scanf();
switch ( v7 )
{
case 2:
start = i;
*((_BYTE *)visited + i) = 1;
break;
case 3:
end = i;
LABEL_9:
++path_length;
continue;
case 1:
*((_BYTE *)visited + i) = 1;
break;
default:
goto LABEL_9;
}
if ( v7 != 1 )
goto LABEL_9;
}
}
v5 = count_paths(start, 1);
printf("%d\n", v5);
free(visited);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 i; // rbx
unsigned int v5; // eax
int v7; // [rsp+Ch] [rbp-1Ch]

__isoc99_scanf();
v3 = width * height;
visited = calloc(1uLL, width * height);
if ( v3 > 0 )
{
for ( i = 0LL; height * width > (int)i; ++i )
{
__isoc99_scanf();
switch ( v7 )
{
case 2:
start = i;
*((_BYTE *)visited + i) = 1;
break;
case 3:
end = i;
LABEL_9:
++path_length;
continue;
case 1:
*((_BYTE *)visited + i) = 1;
break;
default:
goto LABEL_9;
}
if ( v7 != 1 )
goto LABEL_9;
}
}
v5 = func8(start, 1);
printf("%d\n", v5);
free(visited);
return 0;
}

original_code:
int main() {
  scanf("%d %d\n", &width, &height);
  int i, crt;
  visited = calloc(sizeof(char), height * width);
  for (i = 0; i < width * height; ++i) {
    scanf("%d", &crt);
    switch (crt) {
      case 1:
        visited[i] = crt;
        break;
      case 2:
        start = i;
        visited[start] = 1;
        break;
      case 3:
        end = i;
        break;
    }
    if (crt != 1)
      ++path_length;
  }
  printf("%d\n", count_paths(start, 1));
  free(visited);
  return 0;
}

canonical_original_code:
int main() {
  scanf("%d %d\n", &width, &height);
  int i; int crt;
  visited = calloc(sizeof(char), height * width);
  for (i = 0; i < width * height; ++i) {
    scanf("%d", &crt);
    switch (crt) {
      case 1:
        visited[i] = crt;
        break;
      case 2:
        start = i;
        visited[start] = 1;
        break;
      case 3:
        end = i;
        break;
    }
    if (crt != 1)
      ++path_length;
  }
  printf("%d\n", count_paths(start, 1));
  free(visited);
  return 0;
}


repo: vlohacks/misc

decompiled_code:
__int64 output_init()
{
return sub_2();
}

canonical_decompiled_code:
__int64 func0()
{
return func1();
}

original_code:
int output_init(output_opts_t * output_opts)
{
    int i;
    for (i = 0; i < 3; i++) {
        if (output_drivers[i].driver == output_opts->driver) {
            output_selected_driver_index = i;
            return output_drivers[i].init(output_opts);
        }
    }
    return -1;
}

canonical_original_code:
int output_init(struct output_opts_t *output_opts)
{
    int i;
    for (i = 0; i < 3; i++) {
        if (output_drivers[i].driver == output_opts->driver) {
            output_selected_driver_index = i;
            return output_drivers[i].init(output_opts);
        }
    }
    return -1;
}

variable_types: struct output_opts_t { enum output_drivers_t { output_driver_portaudio=0, output_driver_alsa=1, output_driver_raw=2, output_driver_benchmark=3, output_driver_sb16=4 } driver; int buffer_size; int sample_rate; char *output_device; } *

user_defined_types: struct output_opts_t { enum output_drivers_t driver; int buffer_size; int sample_rate; char *output_device; }


decompiled_code:
__int64 __fastcall output_cleanup(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( output_selected_driver_index < 0 )
return 0xFFFFFFFFLL;
result = ((__int64 (__fastcall *)(__int64, __int64, __int64))*(&funcs_5B + 5 * output_selected_driver_index))(
a1,
a2,
a3);
output_selected_driver_index = -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( output_selected_driver_index < 0 )
return 0xFFFFFFFFLL;
result = ((__int64 (__fastcall *)(__int64, __int64, __int64))*(&funcs_5B + 5 * output_selected_driver_index))(
a1,
a2,
a3);
output_selected_driver_index = -1;
return result;
}

original_code:
int output_cleanup() {
    int ret;
    if (output_selected_driver_index >= 0) {
        ret = output_drivers[output_selected_driver_index].cleanup();
        output_selected_driver_index = -1;
        return ret;
    }
    return -1;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall output_start(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( output_selected_driver_index < 0 )
result = 0xFFFFFFFFLL;
else
result = ((__int64 (__fastcall *)(__int64, __int64, __int64))*(&funcs_8B + 5 * output_selected_driver_index))(
a1,
a2,
a3);
return result;
}

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

if ( output_selected_driver_index < 0 )
result = 0xFFFFFFFFLL;
else
result = ((__int64 (__fastcall *)(__int64, __int64, __int64))*(&funcs_8B + 5 * output_selected_driver_index))(
a1,
a2,
a3);
return result;
}

original_code:
int output_start(player_t * player) {
    if (output_selected_driver_index >= 0) {
        return output_drivers[output_selected_driver_index].start(player);
    }
    return -1;
}

canonical_original_code:
int output_start(struct player_t *player) {
    if (output_selected_driver_index >= 0) {
        return output_drivers[output_selected_driver_index].start(player);
    }
    return -1;
}

variable_types: struct player_t { struct player_channel_t { unsigned char sample_num; float sample_pos; float sample_step; int period_index; unsigned short period; unsigned int frequency; unsigned char sample_delay; unsigned short dest_period; unsigned char dest_sample_num; char dest_volume; unsigned char panning; char volume; char volume_master; unsigned char effect_num; unsigned char effect_value; unsigned char effect_last_value[26]; unsigned char effect_last_value_y[26]; int vibrato_state; int tremolo_state; int tremor_state; unsigned char vibrato_waveform; unsigned char tremolo_waveform; unsigned char pattern_loop_position; unsigned char pattern_loop_count; } *channels; struct module_t { char song_title[21]; struct module_info_t { char default_file_extension[8]; char description[64]; union { struct { char signature[8]; } flags_mod; struct { unsigned char st2vibrato; unsigned char st2tempo; unsigned char amigaslides; unsigned char amigalimits; unsigned char st30volumeslides; unsigned short st3_version; unsigned char default_panning; unsigned char mono; } flags_s3m; struct { unsigned char version_major; unsigned char version_minor; } flags_stm; struct { unsigned char stereo; unsigned char use_instruments; unsigned char linear_slides; unsigned char old_effects; unsigned char g_effect_link_memory; unsigned short created_with; unsigned short compatible_with; } flags_it; }; } module_info; enum module_type_t { module_type_mod=0, module_type_s3m=1, module_type_mtm=2, module_type_stm=3, module_type_it=4 } module_type; unsigned short num_channels; unsigned short num_samples; unsigned short num_instruments; unsigned short num_patterns; unsigned short num_orders; unsigned char orders[256]; struct module_sample_t { struct module_sample_header_t { char name[29]; unsigned int length; char finetune; unsigned short c2spd; unsigned char volume; unsigned char loop_enabled; unsigned int loop_start; unsigned int loop_length; unsigned int loop_end; unsigned int susloop_start; unsigned int susloop_end; unsigned char global_volume; unsigned char vibrato_speed; unsigned char vibrato_depth; unsigned char vibrato_waveform; unsigned char vibrato_rate; unsigned char pan; } header; short *data; } *samples; struct module_instrument_t { char name[26]; unsigned char new_note_action; unsigned char duplicate_check_type; unsigned char duplicate_check_action; unsigned char fadeout; char pitch_pan_separation; unsigned char pitch_pan_center; unsigned char default_pan; unsigned char random_volume_variation; unsigned char random_panning_variation; unsigned char note_sample_table[240]; struct module_instrument_envelope_t { unsigned char flags; unsigned char num_nodes; unsigned char loop_start; unsigned char loop_end; unsigned char susloop_start; unsigned char susloop_end; struct module_instrument_envelope_node_t { unsigned char value; unsigned short tick; } *nodes; } volume_envelope; struct module_instrument_envelope_t pan_envelope; struct module_instrument_envelope_t pitch_envelope; } *instruments; struct module_pattern_t { unsigned short num_rows; struct module_pattern_row_t { struct module_pattern_data_t { unsigned char sample_num; int period_index; char volume; unsigned char effect_num; unsigned char effect_value; } *data; } *rows; } *patterns; unsigned char initial_speed; unsigned char initial_bpm; unsigned char initial_master_volume; unsigned char mix_volume; unsigned char initial_panning[64]; unsigned char channel_volume[64]; unsigned char panning_separation; char *song_message; } *module; void (**effect_map)(struct player_t *, int); void (*newrow_action)(struct player_t *, struct module_pattern_data_t *, int); void *callback_user_ptr; void (*order_callback)(struct player_t *, void *user_ptr); void (*row_callback)(struct player_t *, void *user_ptr); void (*tick_callback)(struct player_t *, void *user_ptr); void (*channel_sample_callback)(struct player_t *, void *user_ptr); unsigned int channel_sample_callback_mask; unsigned short *period_table; enum player_resampling_t { player_resampling_none=0, player_resampling_linear=1 } resampling; enum defs_mod_paulafreq_index_t { PAL=0, NTSC=1 } paula_freq_index; int protracker_strict_mode; unsigned short period_top; unsigned short period_bottom; unsigned char speed; unsigned short bpm; float tick_pos; float tick_duration; unsigned int sample_rate; int current_tick; int current_pattern; int current_order; int current_row; int do_break; int next_order; int next_row; int pattern_delay; int pattern_delay_active; int loop_module; int loop_pattern; int solo_channel; int playing; } *

user_defined_types: struct player_t { struct player_channel_t *channels; struct module_t *module; void (**effect_map)(struct player_t *, int); void (*newrow_action)(struct player_t *, struct module_pattern_data_t *, int); void *callback_user_ptr; void (*order_callback)(struct player_t *, void *user_ptr); void (*row_callback)(struct player_t *, void *user_ptr); void (*tick_callback)(struct player_t *, void *user_ptr); void (*channel_sample_callback)(struct player_t *, void *user_ptr); unsigned int channel_sample_callback_mask; unsigned short *period_table; enum player_resampling_t resampling; enum defs_mod_paulafreq_index_t paula_freq_index; int protracker_strict_mode; unsigned short period_top; unsigned short period_bottom; unsigned char speed; unsigned short bpm; float tick_pos; float tick_duration; unsigned int sample_rate; int current_tick; int current_pattern; int current_order; int current_row; int do_break; int next_order; int next_row; int pattern_delay; int pattern_delay_active; int loop_module; int loop_pattern; int solo_channel; int playing; }

user_defined_types: struct module_t { char song_title[21]; struct module_info_t module_info; enum module_type_t module_type; unsigned short num_channels; unsigned short num_samples; unsigned short num_instruments; unsigned short num_patterns; unsigned short num_orders; unsigned char orders[256]; struct module_sample_t *samples; struct module_instrument_t *instruments; struct module_pattern_t *patterns; unsigned char initial_speed; unsigned char initial_bpm; unsigned char initial_master_volume; unsigned char mix_volume; unsigned char initial_panning[64]; unsigned char channel_volume[64]; unsigned char panning_separation; char *song_message; }

user_defined_types: struct module_sample_t { struct module_sample_header_t header; short *data; }

user_defined_types: struct module_instrument_t { char name[26]; unsigned char new_note_action; unsigned char duplicate_check_type; unsigned char duplicate_check_action; unsigned char fadeout; char pitch_pan_separation; unsigned char pitch_pan_center; unsigned char default_pan; unsigned char random_volume_variation; unsigned char random_panning_variation; unsigned char note_sample_table[240]; struct module_instrument_envelope_t volume_envelope; struct module_instrument_envelope_t pan_envelope; struct module_instrument_envelope_t pitch_envelope; }

user_defined_types: struct module_pattern_t { unsigned short num_rows; struct module_pattern_row_t *rows; }

user_defined_types: struct module_instrument_envelope_t { unsigned char flags; unsigned char num_nodes; unsigned char loop_start; unsigned char loop_end; unsigned char susloop_start; unsigned char susloop_end; struct module_instrument_envelope_node_t *nodes; }

user_defined_types: struct module_pattern_row_t { struct module_pattern_data_t *data; }


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

if ( output_selected_driver_index < 0 )
result = 0xFFFFFFFFLL;
else
result = ((__int64 (__fastcall *)(__int64, __int64, __int64))funcs_B6[5 * output_selected_driver_index])(
a1,
a2,
5LL * output_selected_driver_index);
return result;
}

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

if ( output_selected_driver_index < 0 )
result = 0xFFFFFFFFLL;
else
result = ((__int64 (__fastcall *)(__int64, __int64, __int64))funcs_B6[5 * output_selected_driver_index])(
a1,
a2,
5LL * output_selected_driver_index);
return result;
}

original_code:
int output_stop() {
    if (output_selected_driver_index >= 0) {
        return output_drivers[output_selected_driver_index].stop();
    }
    return -1;
}


original_code:
int loader_it_check(io_handle_t * h)
{
    char signature[4];
    size_t saved_pos;
    memset(signature, 0, 4);
    saved_pos = h->tell(h);
    h->seek(h, 0x0, io_seek_set);
    h->read(signature, 1, 4, h);
    h->seek(h, saved_pos, io_seek_set);
    if (memcmp(signature, "IMPM", 4))
        return 0;
    return 1;
}

canonical_original_code:
int loader_it_check(struct io_handle_t *h)
{
    char signature[4];
    unsigned long saved_pos;
    memset(signature, 0, 4);
    saved_pos = h->tell(h);
    h->seek(h, 0x0, io_seek_set);
    h->read(signature, 1, 4, h);
    h->seek(h, saved_pos, io_seek_set);
    if (memcmp(signature, "IMPM", 4))
        return 0;
    return 1;
}

variable_types: struct io_handle_t { void *native_handle; unsigned long (*read)(void *ptr, unsigned long size, unsigned long n, struct io_handle_t *handle); unsigned long (*write)(void *ptr, unsigned long size, unsigned long n, struct io_handle_t *handle); int (*seek)(struct io_handle_t *handle, unsigned long n, enum io_seek_direction_t { io_seek_set=0, io_seek_end=1, io_seek_cur=2 } direction); unsigned long (*tell)(struct io_handle_t *handle); int (*feof)(struct io_handle_t *handle); } *

variable_types:
signature
char [4]

user_defined_types: struct io_handle_t { void *native_handle; unsigned long (*read)(void *ptr, unsigned long size, unsigned long n, struct io_handle_t *handle); unsigned long (*write)(void *ptr, unsigned long size, unsigned long n, struct io_handle_t *handle); int (*seek)(struct io_handle_t *handle, unsigned long n, enum io_seek_direction_t direction); unsigned long (*tell)(struct io_handle_t *handle); int (*feof)(struct io_handle_t *handle); }


decompiled_code:
__int64 __fastcall sample_from_s8(char a1)
{
return (unsigned int)(a1 << 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
return (unsigned int)(a1 << 8);
}

original_code: static inline sample_t sample_from_s8(int8_t s) { return (sample_t)s << 8; }

canonical_original_code: static inline short sample_from_s8(char s) { return (short)s << 8; }

variable_types: s char

return_type: short


original_code:
int loader_stm_check(io_handle_t * h)
{
    char signature[8];
    char id;
    size_t saved_pos;
    saved_pos = h->tell(h);
    h->seek(h, 0x14, io_seek_set);
    h->read(signature, 1, 8, h);
    h->read(&id, 1, 1, h);
    h->seek(h, saved_pos, io_seek_set);
    if (memcmp(signature, "!Scream!", 8))
        return 0;
    if (id != 0x1a)
        return 0;
    return 1;
}

canonical_original_code:
int loader_stm_check(struct io_handle_t *h)
{
    char signature[8];
    char id;
    unsigned long saved_pos;
    saved_pos = h->tell(h);
    h->seek(h, 0x14, io_seek_set);
    h->read(signature, 1, 8, h);
    h->read(&id, 1, 1, h);
    h->seek(h, saved_pos, io_seek_set);
    if (memcmp(signature, "!Scream!", 8))
        return 0;
    if (id != 0x1a)
        return 0;
    return 1;
}

variable_types:
signature
char [8]

variable_types: id char

user_defined_types: enum io_seek_direction_t { io_seek_set=0, io_seek_end=1, io_seek_cur=2 }