decompiled_code:
__int64 pmmap_alloc()
{
int v0; // ebp
int v1; // ebx

v0 = 0x100000;
while ( 1 )
{
v1 = block_number_0;
if ( !pmmap_get_block_status(block_number_0) )
break;
block_number_0 = (v1 + 1) % 0x100000;
if ( !--v0 )
return 0xFFFFFFFFLL;
}
pmmap_mark_block(v1, 1);
return pmmap_addr(v1);
}

canonical_decompiled_code:
__int64 func17()
{
int v0; // ebp
int v1; // ebx

v0 = 0x100000;
while ( 1 )
{
v1 = block_number_0;
if ( !func6(block_number_0) )
break;
block_number_0 = (v1 + 1) % 0x100000;
if ( !--v0 )
return 0xFFFFFFFFLL;
}
func5(v1, 1);
return func8(v1);
}

original_code:
PhysicalAddress pmmap_alloc() {
  static int block_number = 0;
  for(int i = 0; i < 0x00100000; i++) {
    if(pmmap_get_block_status(block_number) == BLOCK_AVAIL) {
      pmmap_mark_block(block_number, BLOCK_UNAVAIL);
      return pmmap_addr(block_number);
    }
    block_number = (block_number + 1) % 0x00100000;
  }
  return ((PhysicalAddress *) 0xffffffff);
}

canonical_original_code:
void *pmmap_alloc() {
  static int block_number = 0;
  for(int i = 0; i < 0x00100000; i++) {
    if(pmmap_get_block_status(block_number) == BLOCK_AVAIL) {
      pmmap_mark_block(block_number, BLOCK_UNAVAIL);
      return pmmap_addr(block_number);
    }
    block_number = (block_number + 1) % 0x00100000;
  }
  return ((void **) 0xffffffff);
}

variable_types: i int


repo: wconrad/brazzle

decompiled_code:
__int64 __fastcall mostly_set_descriptor(__int64 a1)
{
sd_set_address(a1, 0LL);
sd_set_limit(a1, 0xFFFFFFLL);
sd_set_present(a1);
sd_set_priv(a1, 0LL);
sd_set_default_size_32_bits(a1);
sd_set_granularity_4k(a1);
return sd_set_code_or_data(a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
sd_set_address(a1, 0LL);
sd_set_limit(a1, 0xFFFFFFLL);
sd_set_present(a1);
sd_set_priv(a1, 0LL);
sd_set_default_size_32_bits(a1);
sd_set_granularity_4k(a1);
return sd_set_code_or_data(a1);
}

original_code:
static void mostly_set_descriptor(SegmentDescriptor * sd) {
  sd_set_address(sd, 0);
  sd_set_limit(sd, 0xffffff);
  sd_set_present(sd);
  sd_set_priv(sd, 0);
  sd_set_default_size_32_bits(sd);
  sd_set_granularity_4k(sd);
  sd_set_code_or_data(sd);
}

canonical_original_code:
static void mostly_set_descriptor(struct SegmentDescriptor *sd) {
  sd_set_address(sd, 0);
  sd_set_limit(sd, 0xffffff);
  sd_set_present(sd);
  sd_set_priv(sd, 0);
  sd_set_default_size_32_bits(sd);
  sd_set_granularity_4k(sd);
  sd_set_code_or_data(sd);
}

variable_types: struct SegmentDescriptor { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; } *

return_type: void


decompiled_code:
__int64 gdt_set()
{
sd_init(&gdt);
sd_init(&unk_120);
sd_init(&unk_128);
mostly_set_descriptor((__int64)&unk_120);
sd_set_code(&unk_120);
sd_set_readable(&unk_120);
mostly_set_descriptor((__int64)&unk_128);
sd_set_data(&unk_128);
return sd_set_writable(&unk_128);
}

canonical_decompiled_code:
__int64 func1()
{
sd_init(&gdt);
sd_init(&unk_120);
sd_init(&unk_128);
func0((__int64)&unk_120);
sd_set_code(&unk_120);
sd_set_readable(&unk_120);
func0((__int64)&unk_128);
sd_set_data(&unk_128);
return sd_set_writable(&unk_128);
}

original_code:
static void gdt_set() {
  for(unsigned i = 0; i < sizeof(gdt) / sizeof(*gdt); i++)
    sd_init(gdt + i);
  {
    SegmentDescriptor * sd = gdt + 1;
    mostly_set_descriptor(sd);
    sd_set_code(sd);
    sd_set_readable(sd);
  }
  {
    SegmentDescriptor * sd = gdt + 2;
    mostly_set_descriptor(sd);
    sd_set_data(sd);
    sd_set_writable(sd);
  }
}

canonical_original_code:
static void gdt_set() {
  for(unsigned int i = 0; i < sizeof(gdt) / sizeof(*gdt); i++)
    sd_init(gdt + i);
  {
    struct SegmentDescriptor *sd = gdt + 1;
    mostly_set_descriptor(sd);
    sd_set_code(sd);
    sd_set_readable(sd);
  }
  {
    struct SegmentDescriptor *sd = gdt + 2;
    mostly_set_descriptor(sd);
    sd_set_data(sd);
    sd_set_writable(sd);
  }
}

variable_types:
i
unsigned int

user_defined_types: struct SegmentDescriptor { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; }


decompiled_code:
__int64 gdt_install()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int16 v3; // [rsp+Ah] [rbp-Eh] BYREF
int v4; // [rsp+Ch] [rbp-Ch]

v3 = 23;
v4 = get_physaddr(&gdt);
lgdt(&v3);
v0 = selector_for_index(1LL);
set_code_selector(v0);
v1 = selector_for_index(2LL);
return set_data_selectors(v1);
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax
unsigned int v1; // eax
__int16 v3; // [rsp+Ah] [rbp-Eh] BYREF
int v4; // [rsp+Ch] [rbp-Ch]

v3 = 23;
v4 = get_physaddr(&gdt);
lgdt(&v3);
v0 = selector_for_index(1LL);
set_code_selector(v0);
v1 = selector_for_index(2LL);
return set_data_selectors(v1);
}

original_code:
static void gdt_install() {
  GdtAddr gdt_addr = {
    .addr = (uint32_t) get_physaddr(gdt),
    .size = sizeof(gdt) - 1,
  };
  lgdt(&gdt_addr);
  set_code_selector(selector_for_index(1));
  set_data_selectors(selector_for_index(2));
}

canonical_original_code:
static void gdt_install() {
  struct GdtAddr gdt_addr = {
    .addr = (uint32_t) get_physaddr(gdt),
    .size = sizeof(gdt) - 1,
  };
  lgdt(&gdt_addr);
  set_code_selector(selector_for_index(1));
  set_data_selectors(selector_for_index(2));
}

variable_types: struct GdtAddr { unsigned short size; unsigned int addr; }


decompiled_code:
__int64 gdt_init()
{
gdt_set();
return gdt_install();
}

canonical_decompiled_code:
__int64 func3()
{
func1();
return func2();
}

original_code:
void gdt_init() {
  gdt_set();
  gdt_install();
}


decompiled_code:
const char *__fastcall bmmap_type_str(__int64 a1)
{
int v1; // edx
const char *result; // rax

v1 = *(_DWORD *)(a1 + 16);
result = "AVAIL";
if ( v1 != 1 )
{
result = (const char *)&unk_9B;
if ( v1 == 2 )
result = "RESERVED";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(__int64 a1)
{
int v1; // edx
const char *result; // rax

v1 = *(_DWORD *)(a1 + 16);
result = "AVAIL";
if ( v1 != 1 )
{
result = (const char *)&unk_9B;
if ( v1 == 2 )
result = "RESERVED";
}
return result;
}

original_code:
static const char * bmmap_type_str(const BiosMemoryMapEntry * entry) {
  switch(entry->type) {
  case 1:
    return "AVAIL";
  case 2:
    return "RESERVED";
  default:
    return "?";
  }
}

canonical_original_code:
static char *bmmap_type_str(struct BiosMemoryMapEntry *entry) {
  switch(entry->type) {
  case 1:
    return "AVAIL";
  case 2:
    return "RESERVED";
  default:
    return "?";
  }
}

variable_types: struct BiosMemoryMapEntry { unsigned int base_addr_low; unsigned int base_addr_high; unsigned int length_low; unsigned int length_high; unsigned int type; unsigned int reserved; } *

return_type: char *


decompiled_code:
__int64 bmmap_print()
{
__int64 result; // rax
__int64 v1; // rbx
unsigned int v2; // ebp
const char *v3; // rax

vty_puts("BIOS memory map:\n");
result = vty_printf(" ADDR LEN TYPE RESERVED\n");
if ( loader_data )
{
v1 = (unsigned int)((_DWORD)&loader_data + 4);
v2 = 0;
do
{
v3 = bmmap_type_str(v1);
result = vty_printf(
"%08x:%08x %08x:%08x %8s %08x\n",
*(unsigned int *)(v1 + 4),
*(unsigned int *)v1,
*(unsigned int *)(v1 + 12),
*(unsigned int *)(v1 + 8),
v3,
*(unsigned int *)(v1 + 20));
++v2;
v1 += 24LL;
}
while ( loader_data > v2 );
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__int64 v1; // rbx
unsigned int v2; // ebp
const char *v3; // rax

vty_puts("BIOS memory map:\n");
result = vty_printf(" ADDR LEN TYPE RESERVED\n");
if ( loader_data )
{
v1 = (unsigned int)((_DWORD)&loader_data + 4);
v2 = 0;
do
{
v3 = func0(v1);
result = vty_printf(
"%08x:%08x %08x:%08x %8s %08x\n",
*(unsigned int *)(v1 + 4),
*(unsigned int *)v1,
*(unsigned int *)(v1 + 12),
*(unsigned int *)(v1 + 8),
v3,
*(unsigned int *)(v1 + 20));
++v2;
v1 += 24LL;
}
while ( loader_data > v2 );
}
return result;
}

original_code:
void bmmap_print() {
  vty_puts("BIOS memory map:\n");
  vty_printf("       ADDR               LEN           TYPE    RESERVED\n");
  for(unsigned i = 0; i < loader_data.bios_memory_map.count; i++) {
    BiosMemoryMapEntry * entry = &loader_data.bios_memory_map.entries[i];
    vty_printf("%08x:%08x  %08x:%08x  %8s  %08x\n",
               entry->base_addr_high,
               entry->base_addr_low,
               entry->length_high,
               entry->length_low,
               bmmap_type_str(entry),
               entry->reserved);
  }
}

canonical_original_code:
void bmmap_print() {
  vty_puts("BIOS memory map:\n");
  vty_printf("       ADDR               LEN           TYPE    RESERVED\n");
  for(unsigned int i = 0; i < loader_data.bios_memory_map.count; i++) {
    struct BiosMemoryMapEntry *entry = &loader_data.bios_memory_map.entries[i];
    vty_printf("%08x:%08x  %08x:%08x  %8s  %08x\n",
               entry->base_addr_high,
               entry->base_addr_low,
               entry->length_high,
               entry->length_low,
               bmmap_type_str(entry),
               entry->reserved);
  }
}

user_defined_types: struct BiosMemoryMapEntry { unsigned int base_addr_low; unsigned int base_addr_high; unsigned int length_low; unsigned int length_high; unsigned int type; unsigned int reserved; }


decompiled_code:
void *__fastcall sd_init(void *a1)
{
return memset(a1, 0, 8uLL);
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
return memset(a1, 0, 8uLL);
}

original_code:
void sd_init(SegmentDescriptor * sd) {
  memset(sd, 0, sizeof(*sd));
}

canonical_original_code:
void sd_init(struct PACKED *sd) {
  memset(sd, 0, sizeof(*sd));
}

variable_types: struct PACKED { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; } *


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

*(_WORD *)(a1 + 2) = a2;
result = HIWORD(a2);
*(_BYTE *)(a1 + 4) = BYTE2(a2);
*(_BYTE *)(a1 + 7) = HIBYTE(a2);
return result;
}

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

*(_WORD *)(a1 + 2) = a2;
result = HIWORD(a2);
*(_BYTE *)(a1 + 4) = BYTE2(a2);
*(_BYTE *)(a1 + 7) = HIBYTE(a2);
return result;
}

original_code:
void sd_set_address(SegmentDescriptor * sd, uint32_t address) {
  sd->base_low = address & 0xffff;
  sd->base_mid = (address >> 16) & 0xff;
  sd->base_high = (address >> 24) & 0xff;
}

canonical_original_code:
void sd_set_address(struct PACKED *sd, unsigned int address) {
  sd->base_low = address & 0xffff;
  sd->base_mid = (address >> 16) & 0xff;
  sd->base_high = (address >> 24) & 0xff;
}

variable_types:
address
unsigned int

user_defined_types: struct PACKED { unsigned short limit_low; unsigned short base_low; unsigned char base_mid; unsigned char access; unsigned char flags; unsigned char base_high; }


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

*(_WORD *)a1 = a2;
result = *(_BYTE *)(a1 + 6) & 0xF0;
*(_BYTE *)(a1 + 6) = *(_BYTE *)(a1 + 6) & 0xF0 | BYTE2(a2) & 0xF;
return result;
}

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

*(_WORD *)a1 = a2;
result = *(_BYTE *)(a1 + 6) & 0xF0;
*(_BYTE *)(a1 + 6) = *(_BYTE *)(a1 + 6) & 0xF0 | BYTE2(a2) & 0xF;
return result;
}

original_code:
void sd_set_limit(SegmentDescriptor * sd, uint32_t limit) {
  sd->limit_low = limit & 0xffff;
  sd->flags &= ~0x0f;
  sd->flags |= (limit >> 16) & 0x0f;
}

canonical_original_code:
void sd_set_limit(struct PACKED *sd, unsigned int limit) {
  sd->limit_low = limit & 0xffff;
  sd->flags &= ~0x0f;
  sd->flags |= (limit >> 16) & 0x0f;
}

variable_types:
limit
unsigned int


decompiled_code:
__int64 __fastcall vid_poke(int a1, int a2, char a3, char a4)
{
__int64 result; // rax
__int64 v6; // rdx

result = a1;
v6 = a2 + 80LL * a1;
*(_BYTE *)(2 * v6 + 0xB8000) = a3;
*(_BYTE *)(2 * v6 + 0xB8001) = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, char a3, char a4)
{
__int64 result; // rax
__int64 v6; // rdx

result = a1;
v6 = a2 + 80LL * a1;
*(_BYTE *)(2 * v6 + 0xB8000) = a3;
*(_BYTE *)(2 * v6 + 0xB8001) = a4;
return result;
}

original_code:
void vid_poke(int row, int col, char c, char attr) {
  (*video)[row][col][0] = c;
  (*video)[row][col][1] = attr;
}

variable_types: row int

variable_types: col int

variable_types: c char

variable_types: attr char


decompiled_code:
__int64 __fastcall vid_clear_row(int a1)
{
int i; // ebx
__int64 result; // rax

for ( i = 0; i != 80; ++i )
result = vid_poke(a1, i, 32, 7);
return result;
}

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

for ( i = 0; i != 80; ++i )
result = func0(a1, i, 32, 7);
return result;
}

original_code:
void vid_clear_row(int row) {
  for (int col = 0; col < 80; col++)
    vid_poke(row, col, ' ', 0x07);
}


decompiled_code:
__int64 vid_clear()
{
int i; // ebx
__int64 result; // rax

for ( i = 0; i != 25; ++i )
result = vid_clear_row(i);
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int i; // ebx
__int64 result; // rax

for ( i = 0; i != 25; ++i )
result = func1(i);
return result;
}

original_code:
void vid_clear() {
  for (int row = 0; row < 25; row++)
    vid_clear_row(row);
}


decompiled_code:
void *vid_scroll()
{
return memmove((void *)0xB8000, (const void *)0xB80A0, 0xF00uLL);
}

canonical_decompiled_code:
void *func3()
{
return memmove((void *)0xB8000, (const void *)0xB80A0, 0xF00uLL);
}


decompiled_code:
__int64 __fastcall set_idt_entry(int a1, int a2, char a3)
{
__int16 *v4; // rbp
__int64 result; // rax

v4 = &idt[4 * a1];
*v4 = a2;
result = selector_for_index(1LL);
v4[1] = result;
*((_BYTE *)v4 + 4) = 0;
*((_BYTE *)v4 + 5) = a3;
v4[3] = HIWORD(a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, char a3)
{
__int16 *v4; // rbp
__int64 result; // rax

v4 = &idt[4 * a1];
*v4 = a2;
result = selector_for_index(1LL);
v4[1] = result;
*((_BYTE *)v4 + 4) = 0;
*((_BYTE *)v4 + 5) = a3;
v4[3] = HIWORD(a2);
return result;
}

original_code:
void
set_idt_entry(int interrupt_number,
              void (*stub)(),
              int flags) {
  IdtEntry * entry = idt + interrupt_number;
  entry->offset_low = (unsigned) stub & 0xffff;
  entry->code_selector = selector_for_index(1);
  entry->unused = 0;
  entry->flags = flags;
  entry->offset_high = (unsigned) stub >> 16;
}

canonical_original_code:
void set_idt_entry(int interrupt_number, void (*stub)(), int flags) {
  struct IdtEntry *entry = idt + interrupt_number;
  entry->offset_low = (unsigned int) stub & 0xffff;
  entry->code_selector = selector_for_index(1);
  entry->unused = 0;
  entry->flags = flags;
  entry->offset_high = (unsigned int) stub >> 16;
}

variable_types:
stub
void (*)()

variable_types: flags int

variable_types: struct IdtEntry { unsigned short offset_low; unsigned short code_selector; unsigned char unused; unsigned char flags; unsigned short offset_high; } *


init

decompiled_code:
__int64 init()
{
loader_data_init();
vty_init();
gdt_init();
int_init();
return mem_init();
}

canonical_decompiled_code:
__int64 func0()
{
loader_data_init();
vty_init();
gdt_init();
int_init();
return mem_init();
}

original_code:
void init() {
  loader_data_init();
  vty_init();
  gdt_init();
  int_init();
  mem_init();
}


repo: wejeus/xmem

decompiled_code:
int __fastcall quicklist_free(int a1, __int64 a2)
{
_QWORD **v2; // rax
_QWORD *i; // rbx

printf("\nquicklist_free");
if ( free_blocks[a1] )
{
printf("\nrelink with old blocks");
free_blocks[a1] = a2 - 16;
v2 = *(_QWORD ***)(a2 - 16);
for ( i = *v2; i; i = (_QWORD *)*i )
LODWORD(v2) = printf("\ntraversing..");
}
else
{
LODWORD(v2) = printf("\nnothing to connect to!");
free_blocks[a1] = a2 - 16;
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func0(int a1, __int64 a2)
{
_QWORD **v2; // rax
_QWORD *i; // rbx

printf("\nquicklist_free");
if ( free_blocks[a1] )
{
printf("\nrelink with old blocks");
free_blocks[a1] = a2 - 16;
v2 = *(_QWORD ***)(a2 - 16);
for ( i = *v2; i; i = (_QWORD *)*i )
LODWORD(v2) = printf("\ntraversing..");
}
else
{
LODWORD(v2) = printf("\nnothing to connect to!");
free_blocks[a1] = a2 - 16;
}
return (int)v2;
}

original_code:
void quicklist_free(int block_pos, void *ptr)
{
 printf("\nquicklist_free");
 Header *tmp = free_blocks[block_pos];
 if (tmp == ((void *)0)) {
  printf("\nnothing to connect to!");
  free_blocks[block_pos] = (Header *) ptr-1;
 } else {
  printf("\nrelink with old blocks");
  free_blocks[block_pos] = (Header *) ptr-1;
  Header *next = free_blocks[block_pos]->s.next;
  while ( (next = next->s.next) != ((void *)0)) {
   printf("\ntraversing..");
  }
  next = tmp;
 }
}

canonical_original_code:
void quicklist_free(int block_pos, void *ptr)
{
 printf("\nquicklist_free");
 union header *tmp = free_blocks[block_pos];
 if (tmp == ((void *)0)) {
  printf("\nnothing to connect to!");
  free_blocks[block_pos] = (union header *) ptr-1;
 } else {
  printf("\nrelink with old blocks");
  free_blocks[block_pos] = (union header *) ptr-1;
  union header *next = free_blocks[block_pos]->s.next;
  while ( (next = next->s.next) != ((void *)0)) {
   printf("\ntraversing..");
  }
  next = tmp;
 }
}

variable_types:
ptr
void *

variable_types: union header { struct { union header *next; unsigned int size; } s; long x; } *


decompiled_code:
int __fastcall quickfit_free(__int64 a1)
{
double v1; // xmm0_8
double v2; // xmm0_8
double v3; // xmm0_8
unsigned __int64 v4; // rbp
int result; // eax

v1 = (double)*(int *)(a1 - 8);
if ( v1 < 0.0 )
v2 = sqrt(v1);
else
v2 = sqrt(v1);
v3 = v2 - 4.0;
if ( v3 >= 9.223372036854776e18 )
v4 = (unsigned int)(int)(v3 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v4 = (unsigned int)(int)v3;
if ( v4 > 3 )
{
printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
result = linear_free(a1);
}
else
{
printf("\nquickfit_free(), within bounds of some quicklist");
result = quicklist_free(v4, a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
double v1; // xmm0_8
double v2; // xmm0_8
double v3; // xmm0_8
unsigned __int64 v4; // rbp
int result; // eax

v1 = (double)*(int *)(a1 - 8);
if ( v1 < 0.0 )
v2 = sqrt(v1);
else
v2 = sqrt(v1);
v3 = v2 - 4.0;
if ( v3 >= 9.223372036854776e18 )
v4 = (unsigned int)(int)(v3 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v4 = (unsigned int)(int)v3;
if ( v4 > 3 )
{
printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
result = linear_free(a1);
}
else
{
printf("\nquickfit_free(), within bounds of some quicklist");
result = func0(v4, a1);
}
return result;
}

original_code:
void quickfit_free(void *ptr) {
 Header *bp = (Header *)ptr - 1;
 size_t block_pos = sqrt(bp->s.size) - 4;
 if (block_pos <= 3) {
  printf("\nquickfit_free(), within bounds of some quicklist");
  quicklist_free(block_pos, ptr);
 } else {
  printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
  linear_free(ptr);
 }
}

canonical_original_code:
void quickfit_free(void *ptr) {
 union header *bp = (Header *)ptr - 1;
 unsigned long block_pos = sqrt(bp->s.size) - 4;
 if (block_pos <= 3) {
  printf("\nquickfit_free(), within bounds of some quicklist");
  quicklist_free(block_pos, ptr);
 } else {
  printf("\nquickfit_free(), large free requested, doing linear_free(ptr)");
  linear_free(ptr);
 }
}

user_defined_types: union header { struct { union header *next; unsigned int size; } s; long x; }


decompiled_code:
int quickfit_realloc()
{
return printf("\nquickfit realloc");
}

canonical_decompiled_code:
int func3()
{
return printf("\nquickfit realloc");
}

original_code:
void *quickfit_realloc(void *ptr, size_t size) {
 printf("\nquickfit realloc");
}

canonical_original_code:
void *quickfit_realloc(void *ptr, unsigned long size) {
 printf("\nquickfit realloc");
}

variable_types:
size
unsigned long

return_type: void *


morecore

decompiled_code:
__int64 __fastcall morecore(int a1)
{
int v1; // er13
char *v2; // rax
_DWORD *v3; // rsi
char *v4; // rdx

printf("\nquickfit_morecore");
v1 = (int)pow(2.0, (double)(a1 + 4));
printf("\nmorecore will request: %d units", (unsigned int)(8 * v1));
v2 = (char *)sbrk(16LL * (unsigned int)(8 * v1));
if ( v2 == (char *)-1LL )
return 0LL;
v3 = v2;
*((_DWORD *)v2 + 2) = v1;
v4 = v2 + 7;
do
{
++v2;
*(_QWORD *)(v2 - 1) = v2;
*(_DWORD *)(v2 + 7) = v1;
}
while ( v4 != v2 );
*(_QWORD *)((char *)v3 + 7) = 0LL;
quicklist_free(a1, (__int64)(v3 + 4));
return free_blocks[a1];
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // er13
char *v2; // rax
_DWORD *v3; // rsi
char *v4; // rdx

printf("\nquickfit_morecore");
v1 = (int)pow(2.0, (double)(a1 + 4));
printf("\nmorecore will request: %d units", (unsigned int)(8 * v1));
v2 = (char *)sbrk(16LL * (unsigned int)(8 * v1));
if ( v2 == (char *)-1LL )
return 0LL;
v3 = v2;
*((_DWORD *)v2 + 2) = v1;
v4 = v2 + 7;
do
{
++v2;
*(_QWORD *)(v2 - 1) = v2;
*(_DWORD *)(v2 + 7) = v1;
}
while ( v4 != v2 );
*(_QWORD *)((char *)v3 + 7) = 0LL;
func0(a1, (__int64)(v3 + 4));
return free_blocks[a1];
}

original_code:
static Header *morecore(int block_pos) {
 printf("\nquickfit_morecore");
 char *cp;
 Header *new_block;
 unsigned units = pow(2, block_pos + 4);
 printf("\nmorecore will request: %d units", 8 * units);
 cp = sbrk(8 * units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 int i;
 Header *first = (Header *) cp;
 new_block = (Header *) cp;
 new_block->s.size = units;
 for (i = 0; i < 8 -1; ++i) {
  ++cp;
  new_block->s.next = (Header *) cp;
  new_block->s.size = units;
  new_block = new_block->s.next;
 }
 new_block->s.next = ((void *)0);
 quicklist_free(block_pos, (void *)(first+1));
 return free_blocks[block_pos];
}

canonical_original_code:
static union header *morecore(int block_pos) {
 printf("\nquickfit_morecore");
 char *cp;
 union header *new_block;
 unsigned int units = pow(2, block_pos + 4);
 printf("\nmorecore will request: %d units", 8 * units);
 cp = sbrk(8 * units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 int i;
 union header *first = (Header *) cp;
 new_block = (union header *) cp;
 new_block->s.size = units;
 for (i = 0; i < 8 -1; ++i) {
  ++cp;
  new_block->s.next = (union header *) cp;
  new_block->s.size = units;
  new_block = new_block->s.next;
 }
 new_block->s.next = ((void *)0);
 quicklist_free(block_pos, (void *)(first+1));
 return free_blocks[block_pos];
}

variable_types:
cp
char *

variable_types:
units
unsigned int

variable_types: i int

return_type: union header { struct { union header *next; unsigned int size; } s; long x; } *


main

canonical_name: main

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

printf("-- START XMEMTEST");
v3 = xmalloc(400LL, argv);
*(_DWORD *)(v3 + 4) = 4711;
printf("\n*p1: %d", 4711LL);
xfree(v3);
v4 = (_DWORD *)xmalloc(4LL, 4711LL);
*v4 = 666;
printf("\n*p4: %d", 666LL);
xfree(v4);
puts("\n-- XMEMTEST DONE!");
return 0;
}

original_code:
int main(int argc, char **argv) {
 printf("-- START XMEMTEST");
 int *p1 = xmalloc(100*sizeof(int));
 p1[1] = 4711;
 printf("\n*p1: %d", p1[1]);
 xfree(p1);
 int *p4 = xmalloc(sizeof(int));
 *p4 = 666;
 printf("\n*p4: %d", *p4);
 xfree(p4);
 printf("\n-- XMEMTEST DONE!\n");
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


decompiled_code:
unsigned __int64 __fastcall linear_free(__int64 a1)
{
unsigned __int64 v1; // rcx
unsigned __int64 result; // rax
_QWORD *v3; // rdx
_QWORD *v4; // rsi

printf("\nlinear_free");
v1 = a1 - 16;
for ( result = p_free_blocks; result < v1; result = (unsigned __int64)v3 )
{
v3 = *(_QWORD **)result;
if ( *(_QWORD *)result > v1 || (unsigned __int64)v3 <= result )
goto LABEL_8;
LABEL_4:
;
}
v3 = *(_QWORD **)result;
if ( *(_QWORD *)result > result || (unsigned __int64)v3 <= v1 )
goto LABEL_4;
LABEL_8:
v4 = *(_QWORD **)result;
if ( *(_QWORD *)result == v1 + 16LL * *(unsigned int *)(a1 - 8) )
{
*(_DWORD *)(a1 - 8) += *((_DWORD *)v4 + 2);
*(_QWORD *)(a1 - 16) = **(_QWORD **)result;
}
else
{
*(_QWORD *)(a1 - 16) = v4;
}
if ( v1 == result + 16LL * *(unsigned int *)(result + 8) )
{
*(_DWORD *)(result + 8) += *(_DWORD *)(a1 - 8);
*(_QWORD *)result = *(_QWORD *)(a1 - 16);
}
else
{
*(_QWORD *)result = v1;
}
p_free_blocks = result;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
unsigned __int64 v1; // rcx
unsigned __int64 result; // rax
_QWORD *v3; // rdx
_QWORD *v4; // rsi

printf("\nlinear_free");
v1 = a1 - 16;
for ( result = p_free_blocks; result < v1; result = (unsigned __int64)v3 )
{
v3 = *(_QWORD **)result;
if ( *(_QWORD *)result > v1 || (unsigned __int64)v3 <= result )
goto LABEL_8;
LABEL_4:
;
}
v3 = *(_QWORD **)result;
if ( *(_QWORD *)result > result || (unsigned __int64)v3 <= v1 )
goto LABEL_4;
LABEL_8:
v4 = *(_QWORD **)result;
if ( *(_QWORD *)result == v1 + 16LL * *(unsigned int *)(a1 - 8) )
{
*(_DWORD *)(a1 - 8) += *((_DWORD *)v4 + 2);
*(_QWORD *)(a1 - 16) = **(_QWORD **)result;
}
else
{
*(_QWORD *)(a1 - 16) = v4;
}
if ( v1 == result + 16LL * *(unsigned int *)(result + 8) )
{
*(_DWORD *)(result + 8) += *(_DWORD *)(a1 - 8);
*(_QWORD *)result = *(_QWORD *)(a1 - 16);
}
else
{
*(_QWORD *)result = v1;
}
p_free_blocks = result;
return result;
}

original_code:
void linear_free(void *ptr) {
 printf("\nlinear_free");
 Header *bp, *p;
 bp = (Header *)ptr - 1;
 for (p = p_free_blocks; !(bp > p && bp < p->s.next); p = p->s.next)
  if (p >= p->s.next && (bp > p || bp < p->s.next))
   break;
 if (bp + bp->s.size == p->s.next) {
  bp->s.size += p->s.next->s.size;
  bp->s.next = p->s.next->s.next;
 } else
  bp->s.next = p->s.next;
 if (p + p->s.size == bp) {
  p->s.size += bp->s.size;
  p->s.next = bp->s.next;
 } else
  p->s.next = bp;
 p_free_blocks = p;
}

canonical_original_code:
void linear_free(void *ptr) {
 printf("\nlinear_free");
 union header *bp; union header *p;
 bp = (union header *)ptr - 1;
 for (p = p_free_blocks; !(bp > p && bp < p->s.next); p = p->s.next)
  if (p >= p->s.next && (bp > p || bp < p->s.next))
   break;
 if (bp + bp->s.size == p->s.next) {
  bp->s.size += p->s.next->s.size;
  bp->s.next = p->s.next->s.next;
 } else
  bp->s.next = p->s.next;
 if (p + p->s.size == bp) {
  p->s.size += bp->s.size;
  p->s.next = bp->s.next;
 } else
  p->s.next = bp;
 p_free_blocks = p;
}


firstfit

decompiled_code:
__int64 __fastcall firstfit(__int64 a1, unsigned int a2)
{
__int64 v2; // rbx
__int64 v3; // rax
unsigned int v4; // ecx
unsigned int v5; // ecx

v2 = a1;
printf("\n(void*) firstfit(...)");
v3 = *(_QWORD *)a1;
v4 = *(_DWORD *)(*(_QWORD *)a1 + 8LL);
if ( a2 > v4 )
{
while ( 1 )
{
v2 = v3;
if ( p_free_blocks == v3 )
{
v2 = morecore(a2);
if ( !v2 )
break;
}
v3 = *(_QWORD *)v2;
v4 = *(_DWORD *)(*(_QWORD *)v2 + 8LL);
if ( v4 >= a2 )
goto LABEL_2;
}
}
else
{
LABEL_2:
if ( a2 == v4 )
{
*(_QWORD *)v2 = *(_QWORD *)v3;
}
else
{
v5 = v4 - a2;
*(_DWORD *)(v3 + 8) = v5;
v3 += 16LL * v5;
*(_DWORD *)(v3 + 8) = a2;
}
p_free_blocks = v2;
v2 = v3 + 16;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
__int64 v2; // rbx
__int64 v3; // rax
unsigned int v4; // ecx
unsigned int v5; // ecx

v2 = a1;
printf("\n(void*) firstfit(...)");
v3 = *(_QWORD *)a1;
v4 = *(_DWORD *)(*(_QWORD *)a1 + 8LL);
if ( a2 > v4 )
{
while ( 1 )
{
v2 = v3;
if ( p_free_blocks == v3 )
{
v2 = func1(a2);
if ( !v2 )
break;
}
v3 = *(_QWORD *)v2;
v4 = *(_DWORD *)(*(_QWORD *)v2 + 8LL);
if ( v4 >= a2 )
goto LABEL_2;
}
}
else
{
LABEL_2:
if ( a2 == v4 )
{
*(_QWORD *)v2 = *(_QWORD *)v3;
}
else
{
v5 = v4 - a2;
*(_DWORD *)(v3 + 8) = v5;
v3 += 16LL * v5;
*(_DWORD *)(v3 + 8) = a2;
}
p_free_blocks = v2;
v2 = v3 + 16;
}
return v2;
}

original_code:
void *firstfit(Header *prev_block, unsigned num_units) {
 printf("\n(void*) firstfit(...)");
 Header *p;
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (p->s.size == num_units)
    prev_block->s.next = p->s.next;
   else {
    p->s.size -= num_units;
    p += p->s.size;
    p->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(p+1);
  }
  if (p == p_free_blocks && (p = morecore(num_units)) == ((void *)0))
    return ((void *)0);
 }
}

canonical_original_code:
void *firstfit(union header *prev_block, unsigned int num_units) {
 printf("\n(void*) firstfit(...)");
 union header *p;
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (p->s.size == num_units)
    prev_block->s.next = p->s.next;
   else {
    p->s.size -= num_units;
    p += p->s.size;
    p->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(p+1);
  }
  if (p == p_free_blocks && (p = morecore(num_units)) == ((void *)0))
    return ((void *)0);
 }
}


decompiled_code:
__int64 __fastcall linear_malloc(__int64 a1)
{
unsigned int v1; // esi
__int64 *v2; // rdi

printf("\nlinear_malloc, using strategy: %d", 4LL);
v1 = ((unsigned __int64)(a1 + 15) >> 4) + 1;
v2 = (__int64 *)p_free_blocks;
if ( !p_free_blocks )
{
p_free_blocks = (__int64)&base_block;
base_block = (__int64)&base_block;
dword_3D8 = 0;
v2 = &base_block;
}
return firstfit((__int64)v2, v1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // esi
__int64 *v2; // rdi

printf("\nlinear_malloc, using strategy: %d", 4LL);
v1 = ((unsigned __int64)(a1 + 15) >> 4) + 1;
v2 = (__int64 *)p_free_blocks;
if ( !p_free_blocks )
{
p_free_blocks = (__int64)&base_block;
base_block = (__int64)&base_block;
dword_3D8 = 0;
v2 = &base_block;
}
return func2((__int64)v2, v1);
}

original_code:
void *linear_malloc(size_t size) {
 printf("\nlinear_malloc, using strategy: %d", 4);
 Header *p, *prev_block;
 unsigned num_units;
 num_units = (size+sizeof(Header)-1)/sizeof(Header) + 1;
 if ((prev_block = p_free_blocks) == ((void *)0)) {
  base_block.s.next = p_free_blocks = prev_block = &base_block;
  base_block.s.size = 0;
 }
 switch (4) {
  case 1:
   return firstfit(prev_block, num_units);
  case 2:
   return bestfit(prev_block, num_units);
  case 3:
   return worstfit(prev_block, num_units);
  default:
   return firstfit(prev_block, num_units);
 }
}

canonical_original_code:
void *linear_malloc(unsigned long size) {
 printf("\nlinear_malloc, using strategy: %d", 4);
 union header *p; union header *prev_block;
 unsigned int num_units;
 num_units = (size+sizeof(Header)-1)/sizeof(Header) + 1;
 if ((prev_block = p_free_blocks) == ((void *)0)) {
  base_block.s.next = p_free_blocks = prev_block = &base_block;
  base_block.s.size = 0;
 }
 switch (4) {
  case 1:
   return firstfit(prev_block, num_units);
  case 2:
   return bestfit(prev_block, num_units);
  case 3:
   return worstfit(prev_block, num_units);
  default:
   return firstfit(prev_block, num_units);
 }
}


bestfit

decompiled_code:
__int64 __fastcall bestfit(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
unsigned int v3; // er13
__int64 v4; // rbx
_QWORD *v5; // r14
__int64 v6; // rbp
__int64 v7; // rsi
unsigned int v8; // eax

v2 = (__int64)a1;
v3 = a2;
printf("\n(void*) bestfit(...), looking for blocksize: %d", a2);
v4 = *a1;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = *(unsigned int *)(v4 + 8);
if ( (unsigned int)v7 >= v3 )
{
if ( v6 )
{
if ( (unsigned int)v7 < *(_DWORD *)(v6 + 8) )
{
printf("\nfound better block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
else
{
printf("\nfound first suitable block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
if ( v6 && p_free_blocks == v4 )
break;
if ( p_free_blocks == v4 )
{
v4 = morecore(v3);
if ( !v4 )
return v4;
}
v2 = v4;
v4 = *(_QWORD *)v4;
}
if ( *(_DWORD *)(v6 + 8) == v3 )
{
printf("\nblock size is exact");
*v5 = *(_QWORD *)v6;
}
else
{
printf("\nblocksize is to big, splitting");
v8 = *(_DWORD *)(v6 + 8) - v3;
*(_DWORD *)(v6 + 8) = v8;
v6 += 16LL * v8;
*(_DWORD *)(v6 + 8) = v3;
}
p_free_blocks = v2;
return v6 + 16;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
unsigned int v3; // er13
__int64 v4; // rbx
_QWORD *v5; // r14
__int64 v6; // rbp
__int64 v7; // rsi
unsigned int v8; // eax

v2 = (__int64)a1;
v3 = a2;
printf("\n(void*) bestfit(...), looking for blocksize: %d", a2);
v4 = *a1;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = *(unsigned int *)(v4 + 8);
if ( (unsigned int)v7 >= v3 )
{
if ( v6 )
{
if ( (unsigned int)v7 < *(_DWORD *)(v6 + 8) )
{
printf("\nfound better block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
else
{
printf("\nfound first suitable block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
if ( v6 && p_free_blocks == v4 )
break;
if ( p_free_blocks == v4 )
{
v4 = func1(v3);
if ( !v4 )
return v4;
}
v2 = v4;
v4 = *(_QWORD *)v4;
}
if ( *(_DWORD *)(v6 + 8) == v3 )
{
printf("\nblock size is exact");
*v5 = *(_QWORD *)v6;
}
else
{
printf("\nblocksize is to big, splitting");
v8 = *(_DWORD *)(v6 + 8) - v3;
*(_DWORD *)(v6 + 8) = v8;
v6 += 16LL * v8;
*(_DWORD *)(v6 + 8) = v3;
}
p_free_blocks = v2;
return v6 + 16;
}

original_code:
void *bestfit(Header *prev_block, unsigned num_units) {
 printf("\n(void*) bestfit(...), looking for blocksize: %d", num_units);
 Header *p;
 Header *currentBest = ((void *)0);
 Header *currentBestPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentBest == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentBest = p;
    currentBestPrev = prev_block;
   } else {
    if (p->s.size < currentBest->s.size) {
     printf("\nfound better block, size: %d", p->s.size);
     currentBest = p;
     currentBestPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentBest != ((void *)0)) ) {
   if (currentBest->s.size == num_units) {
    printf("\nblock size is exact");
    currentBestPrev->s.next = currentBest->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentBest->s.size -= num_units;
    currentBest += currentBest->s.size;
    currentBest->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentBest+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}

canonical_original_code:
void *bestfit(union header *prev_block, unsigned int num_units) {
 printf("\n(void*) bestfit(...), looking for blocksize: %d", num_units);
 union header *p;
 union header *currentBest = ((void *)0);
 union header *currentBestPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentBest == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentBest = p;
    currentBestPrev = prev_block;
   } else {
    if (p->s.size < currentBest->s.size) {
     printf("\nfound better block, size: %d", p->s.size);
     currentBest = p;
     currentBestPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentBest != ((void *)0)) ) {
   if (currentBest->s.size == num_units) {
    printf("\nblock size is exact");
    currentBestPrev->s.next = currentBest->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentBest->s.size -= num_units;
    currentBest += currentBest->s.size;
    currentBest->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentBest+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}


worstfit

decompiled_code:
__int64 __fastcall worstfit(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
unsigned int v3; // er13
__int64 v4; // rbx
_QWORD *v5; // r14
__int64 v6; // rbp
__int64 v7; // rsi
unsigned int v8; // eax

v2 = (__int64)a1;
v3 = a2;
printf("\n(void*) worstfit(...), looking for blocksize: %d", a2);
v4 = *a1;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = *(unsigned int *)(v4 + 8);
if ( (unsigned int)v7 >= v3 )
{
if ( v6 )
{
if ( (unsigned int)v7 > *(_DWORD *)(v6 + 8) )
{
printf("\nfound worse block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
else
{
printf("\nfound first suitable block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
if ( v6 && p_free_blocks == v4 )
break;
if ( p_free_blocks == v4 )
{
v4 = morecore(v3);
if ( !v4 )
return v4;
}
v2 = v4;
v4 = *(_QWORD *)v4;
}
if ( *(_DWORD *)(v6 + 8) == v3 )
{
printf("\nblock size is exact");
*v5 = *(_QWORD *)v6;
}
else
{
printf("\nblocksize is to big, splitting");
v8 = *(_DWORD *)(v6 + 8) - v3;
*(_DWORD *)(v6 + 8) = v8;
v6 += 16LL * v8;
*(_DWORD *)(v6 + 8) = v3;
}
p_free_blocks = v2;
return v6 + 16;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
unsigned int v3; // er13
__int64 v4; // rbx
_QWORD *v5; // r14
__int64 v6; // rbp
__int64 v7; // rsi
unsigned int v8; // eax

v2 = (__int64)a1;
v3 = a2;
printf("\n(void*) worstfit(...), looking for blocksize: %d", a2);
v4 = *a1;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = *(unsigned int *)(v4 + 8);
if ( (unsigned int)v7 >= v3 )
{
if ( v6 )
{
if ( (unsigned int)v7 > *(_DWORD *)(v6 + 8) )
{
printf("\nfound worse block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
else
{
printf("\nfound first suitable block, size: %d", v7);
v5 = (_QWORD *)v2;
v6 = v4;
}
}
if ( v6 && p_free_blocks == v4 )
break;
if ( p_free_blocks == v4 )
{
v4 = func1(v3);
if ( !v4 )
return v4;
}
v2 = v4;
v4 = *(_QWORD *)v4;
}
if ( *(_DWORD *)(v6 + 8) == v3 )
{
printf("\nblock size is exact");
*v5 = *(_QWORD *)v6;
}
else
{
printf("\nblocksize is to big, splitting");
v8 = *(_DWORD *)(v6 + 8) - v3;
*(_DWORD *)(v6 + 8) = v8;
v6 += 16LL * v8;
*(_DWORD *)(v6 + 8) = v3;
}
p_free_blocks = v2;
return v6 + 16;
}

original_code:
void *worstfit(Header *prev_block, unsigned num_units) {
 printf("\n(void*) worstfit(...), looking for blocksize: %d", num_units);
 Header *p;
 Header *currentWorst = ((void *)0);
 Header *currentWorstPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentWorst == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentWorst = p;
    currentWorstPrev = prev_block;
   } else {
    if (p->s.size > currentWorst->s.size) {
     printf("\nfound worse block, size: %d", p->s.size);
     currentWorst = p;
     currentWorstPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentWorst != ((void *)0)) ) {
   if (currentWorst->s.size == num_units) {
    printf("\nblock size is exact");
    currentWorstPrev->s.next = currentWorst->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentWorst->s.size -= num_units;
    currentWorst += currentWorst->s.size;
    currentWorst->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentWorst+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}

canonical_original_code:
void *worstfit(union header *prev_block, unsigned int num_units) {
 printf("\n(void*) worstfit(...), looking for blocksize: %d", num_units);
 union header *p;
 union header *currentWorst = ((void *)0);
 union header *currentWorstPrev = ((void *)0);
 for (p = prev_block->s.next; ; prev_block = p, p = p->s.next) {
  if (p->s.size >= num_units) {
   if (currentWorst == ((void *)0)) {
    printf("\nfound first suitable block, size: %d", p->s.size);
    currentWorst = p;
    currentWorstPrev = prev_block;
   } else {
    if (p->s.size > currentWorst->s.size) {
     printf("\nfound worse block, size: %d", p->s.size);
     currentWorst = p;
     currentWorstPrev = prev_block;
    }
   }
  }
  if ( (p == p_free_blocks) && (currentWorst != ((void *)0)) ) {
   if (currentWorst->s.size == num_units) {
    printf("\nblock size is exact");
    currentWorstPrev->s.next = currentWorst->s.next;
   } else {
    printf("\nblocksize is to big, splitting");
    currentWorst->s.size -= num_units;
    currentWorst += currentWorst->s.size;
    currentWorst->s.size = num_units;
   }
   p_free_blocks = prev_block;
   return (void *)(currentWorst+1);
  }
  if ( (p == p_free_blocks) && ((p = morecore(num_units)) == ((void *)0)) ) {
   return ((void *)0);
  }
 }
}


decompiled_code:
void linear_realloc()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void *linear_realloc(void *ptr, size_t size) {
}

canonical_original_code:
void *linear_realloc(void *ptr, unsigned long size) {
}


morecore

decompiled_code:
__int64 __fastcall morecore(unsigned int a1)
{
unsigned int v1; // ebx
_DWORD *v2; // rax

v1 = a1;
printf("\nlinear_morecore");
if ( a1 < 0x400 )
v1 = 1024;
v2 = sbrk(16LL * v1);
if ( v2 == (_DWORD *)-1LL )
return 0LL;
v2[2] = v1;
linear_free((__int64)(v2 + 4));
return p_free_blocks;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // ebx
_DWORD *v2; // rax

v1 = a1;
printf("\nlinear_morecore");
if ( a1 < 0x400 )
v1 = 1024;
v2 = sbrk(16LL * v1);
if ( v2 == (_DWORD *)-1LL )
return 0LL;
v2[2] = v1;
func0((__int64)(v2 + 4));
return p_free_blocks;
}

original_code:
static Header *morecore(unsigned units) {
 printf("\nlinear_morecore");
 char *cp;
 Header *new_block;
 if (units < 1024)
  units = 1024;
 cp = sbrk(units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 new_block = (Header *) cp;
 new_block->s.size = units;
 linear_free((void *)(new_block+1));
 return p_free_blocks;
}

canonical_original_code:
static union header *morecore(unsigned int units) {
 printf("\nlinear_morecore");
 char *cp;
 union header *new_block;
 if (units < 1024)
  units = 1024;
 cp = sbrk(units * sizeof(Header));
 if (cp == (char *) -1)
  return ((void *)0);
 new_block = (union header *) cp;
 new_block->s.size = units;
 linear_free((void *)(new_block+1));
 return p_free_blocks;
}


xmalloc

decompiled_code:
__int64 xmalloc()
{
return XMEM_CALLBACK();
}

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

original_code:
void *xmalloc(size_t size) {
 return XMEM_CALLBACK.XMEM_malloc(size);
}

canonical_original_code:
void *xmalloc(unsigned long size) {
 return XMEM_CALLBACK.XMEM_malloc(size);
}


xfree

decompiled_code:
__int64 xfree()
{
return (*(&XMEM_CALLBACK + 1))();
}

canonical_decompiled_code:
__int64 func1()
{
return (*(&XMEM_CALLBACK + 1))();
}

original_code:
void xfree(void *ptr) {
 return XMEM_CALLBACK.XMEM_free(ptr);
}


xrealloc

decompiled_code:
void xrealloc()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void *xrealloc(void *ptr, size_t size) {
}

canonical_original_code:
void *xrealloc(void *ptr, unsigned long size) {
}


repo: welschmorgan/calc

decompiled_code:
void __fastcall node_print(__int64 a1, int a2, int a3)
{
unsigned int v4; // edx
int i; // eax
bool v6; // zf
const char *v7; // rsi
const char *v8; // rsi

if ( a1 )
{
v4 = abs32(a2);
if ( (unsigned int)(a2 + 19) > 0x26 )
v4 = 20;
for ( i = 0; i != v4; ++i )
;
if ( a3 )
{
v6 = a2 == 0;
v8 = (const char *)&unk_223;
if ( v6 )
v8 = (const char *)&unk_225;
printf(
"%s%i%c (priority: %i) prev:0x%x next:0x%x\n",
v8,
(unsigned int)*(char *)(a1 + 24),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 16),
*(_QWORD *)(a1 + 8),
*(_QWORD *)a1);
}
else
{
v6 = a2 == 0;
v7 = (const char *)&unk_223;
if ( v6 )
v7 = (const char *)&unk_225;
printf(
"%s%i%c (priority: %i)\n",
v7,
(unsigned int)*(char *)(a1 + 24),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 16));
}
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, int a2, int a3)
{
unsigned int v4; // edx
int i; // eax
bool v6; // zf
const char *v7; // rsi
const char *v8; // rsi

if ( a1 )
{
v4 = abs32(a2);
if ( (unsigned int)(a2 + 19) > 0x26 )
v4 = 20;
for ( i = 0; i != v4; ++i )
;
if ( a3 )
{
v6 = a2 == 0;
v8 = (const char *)&unk_223;
if ( v6 )
v8 = (const char *)&unk_225;
printf(
"%s%i%c (priority: %i) prev:0x%x next:0x%x\n",
v8,
(unsigned int)*(char *)(a1 + 24),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 16),
*(_QWORD *)(a1 + 8),
*(_QWORD *)a1);
}
else
{
v6 = a2 == 0;
v7 = (const char *)&unk_223;
if ( v6 )
v7 = (const char *)&unk_225;
printf(
"%s%i%c (priority: %i)\n",
v7,
(unsigned int)*(char *)(a1 + 24),
*(unsigned int *)(a1 + 20),
*(unsigned int *)(a1 + 16));
}
}
}

original_code:
void node_print(t_node*n, int sub_level, int debug)
{
 if(!n) return;
 char subs[20];
 int i=0;
 for( i=0; i<((((sub_level<0)?-sub_level:sub_level)<20)?((sub_level<0)?-sub_level:sub_level):20);i++ )
  subs[i] = '\t';
 subs[i] = '\0';
 if( !debug )
  printf("%s%i%c (priority: %i)\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level);
 else
  printf("%s%i%c (priority: %i) prev:0x%x next:0x%x\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level,
    n->prev,
    n->next);
}

canonical_original_code:
void node_print(struct s_node *n, int sub_level, int debug)
{
 if(!n) return;
 char subs[20];
 int i = 0;
 for( i=0; i<((((sub_level<0)?-sub_level:sub_level)<20)?((sub_level<0)?-sub_level:sub_level):20);i++ )
  subs[i] = '\t';
 subs[i] = '\0';
 if( !debug )
  printf("%s%i%c (priority: %i)\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level);
 else
  printf("%s%i%c (priority: %i) prev:0x%x next:0x%x\n",
    sub_level?"\t":"",
    n->op,
    n->value,
    n->level,
    n->prev,
    n->next);
}

variable_types: struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *

variable_types: debug int

variable_types:
subs
char [20]


original_code:
void list_init(t_list*l)
{
 if(!l) return;
 l->tail=l->head=((void *)0);
 l->level=0;
}

canonical_original_code:
void list_init(struct s_list *l)
{
 if(!l) return;
 l->tail=l->head=((void *)0);
 l->level=0;
}

variable_types: struct s_list { struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *tail; struct s_node *head; int level; } *

user_defined_types: struct s_list { struct s_node *tail; struct s_node *head; int level; }


decompiled_code:
void *list_alloc()
{
void *v0; // rbx

v0 = malloc(0x18uLL);
list_init((__int64)v0);
return v0;
}

canonical_decompiled_code:
void *func3()
{
void *v0; // rbx

v0 = malloc(0x18uLL);
func2((__int64)v0);
return v0;
}

original_code:
t_list* list_alloc()
{
 t_list* l = (t_list*)malloc(sizeof(t_list));
 list_init(l);
 return l;
}

canonical_original_code:
struct s_list *list_alloc()
{
 struct s_list *l = (t_list*)malloc(sizeof(t_list));
 list_init(l);
 return l;
}

return_type: struct s_list { struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *tail; struct s_node *head; int level; } *


decompiled_code:
void __fastcall list_free(void *ptr)
{
if ( ptr )
{
list_clear((__int64)ptr);
free(ptr);
}
}

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

original_code:
void list_free(t_list*l)
{
 if(!l) return;
 list_clear(l);
 free(l);
}

canonical_original_code:
void list_free(struct s_list *l)
{
 if(!l) return;
 list_clear(l);
 free(l);
}

user_defined_types: struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; }


original_code:
void list_push(t_list*l,t_node*n)
{
 t_node * oldHead;
 if(!l) return;
 if(!l->head)
 {
  l->head=n;
 }
 else
 {
  oldHead=l->head;
  oldHead->next=n;
  n->prev = oldHead;
  l->head = n;
 }
}

canonical_original_code:
void list_push(struct s_list *l, struct s_node *n)
{
 struct s_node *oldHead;
 if(!l) return;
 if(!l->head)
 {
  l->head=n;
 }
 else
 {
  oldHead=l->head;
  oldHead->next=n;
  n->prev = oldHead;
  l->head = n;
 }
}

variable_types:
n
struct s_node *


original_code:
void node_init(t_node*n, int lvl, char op, int value)
{
 if(!n) return;
 n->level=lvl;
 n->op=op;
 n->value=value;
 n->next=n->prev=((void *)0);
}

canonical_original_code:
void node_init(struct s_node *n, int lvl, char op, int value)
{
 if(!n) return;
 n->level=lvl;
 n->op=op;
 n->value=value;
 n->next=n->prev=((void *)0);
}

variable_types: lvl int

variable_types: op char

variable_types: value int


decompiled_code:
void *__fastcall node_alloc(int a1, char a2, int a3)
{
void *v4; // rbp

v4 = malloc(0x20uLL);
node_init((__int64)v4, a1, a2, a3);
return v4;
}

canonical_decompiled_code:
void *__fastcall func8(int a1, char a2, int a3)
{
void *v4; // rbp

v4 = malloc(0x20uLL);
func7((__int64)v4, a1, a2, a3);
return v4;
}

original_code:
t_node* node_alloc(int lvl, char op, int value)
{
 t_node * n=(t_node*)malloc(sizeof(t_node));
 node_init(n,lvl,op,value);
 return n;
}

canonical_original_code:
struct s_node *node_alloc(int lvl, char op, int value)
{
 struct s_node *n = (t_node*)malloc(sizeof(t_node));
 node_init(n,lvl,op,value);
 return n;
}

return_type: struct s_node { struct s_node *next; struct s_node *prev; int level; int value; char op; } *


decompiled_code:
void __fastcall node_free(_QWORD *a1)
{
_QWORD *v1; // rax

if ( a1 )
{
v1 = (_QWORD *)a1[1];
if ( v1 )
*v1 = *a1;
free(a1);
}
}

canonical_decompiled_code:
void __fastcall func9(_QWORD *a1)
{
_QWORD *v1; // rax

if ( a1 )
{
v1 = (_QWORD *)a1[1];
if ( v1 )
*v1 = *a1;
free(a1);
}
}

original_code:
void node_free( t_node*n ){
 if(!n) return;
 if(n->prev)
  n->prev->next = n->next;
 free(n);
}

canonical_original_code:
void node_free(struct s_node *n){
 if(!n) return;
 if(n->prev)
  n->prev->next = n->next;
 free(n);
}


decompiled_code:
void __fastcall list_pop(__int64 a1)
{
_QWORD *v1; // rdi

if ( a1 )
{
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
node_free(v1);
}
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
_QWORD *v1; // rdi

if ( a1 )
{
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
func9(v1);
}
}

original_code:
void list_pop(t_list*l)
{
 if(!l||!l->head) return;
 node_free(l->head);
}

canonical_original_code:
void list_pop(struct s_list *l)
{
 if(!l||!l->head) return;
 node_free(l->head);
}


decompiled_code:
void signal_handler()
{
interrupted = 1;
}

canonical_decompiled_code:
void func7()
{
interrupted = 1;
}

original_code:
static void signal_handler(int signal) {
 interrupted = 1;
}

variable_types: signal int


decompiled_code:
void *__fastcall cpu_init(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
a1[3] = 0LL;
return memset(a1 + 4, 0, 0xFFFFuLL);
}

canonical_decompiled_code:
void *__fastcall func0(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
a1[3] = 0LL;
return memset(a1 + 4, 0, 0xFFFFuLL);
}

original_code:
void cpu_init(context_t *c) {
 memset(c->reg, 0, 16 * (sizeof(word_t) / sizeof(uint8_t)));
 memset(c->mem, 0, 0xFFFF * (sizeof(byte_t) / sizeof(uint8_t)));
}

canonical_original_code:
void cpu_init(struct context_t *c) {
 memset(c->reg, 0, 16 * (sizeof(word_t) / sizeof(unsigned char)));
 memset(c->mem, 0, 0xFFFF * (sizeof(byte_t) / sizeof(unsigned char)));
}

variable_types: struct context_t { struct word_t { unsigned char l; unsigned char h; } reg[16]; unsigned char mem[65535]; } *

user_defined_types: struct context_t { struct word_t reg[16]; unsigned char mem[65535]; }


decompiled_code:
void __fastcall cpu_wregw(__int64 a1, unsigned __int8 a2, __int16 a3)
{
*(_WORD *)(a1 + 2LL * a2) = a3;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, unsigned __int8 a2, __int16 a3)
{
*(_WORD *)(a1 + 2LL * a2) = a3;
}

original_code:
void cpu_wregw(context_t *c, raddr_t r, word_t v) {
 memcpy(&c->reg[r], &v, sizeof(word_t));
}

canonical_original_code:
void cpu_wregw(struct context_t *c, unsigned char r, struct word_t v) {
 memcpy(&c->reg[r], &v, sizeof(word_t));
}

variable_types:
r
unsigned char

variable_types:
v
struct word_t

user_defined_types: struct word_t { unsigned char l; unsigned char h; }


decompiled_code:
__int64 __fastcall cpu_rregw(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

original_code:
word_t *cpu_rregw(context_t *c, raddr_t r) {
 return &c->reg[r];
}

canonical_original_code:
struct word_t *cpu_rregw(struct context_t *c, unsigned char r) {
 return &c->reg[r];
}

variable_types: struct context_t { struct word_t reg[16]; unsigned char mem[65535]; } *

return_type: struct word_t { unsigned char l; unsigned char h; } *


decompiled_code:
__int64 __fastcall cpu_wregb(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 result; // rax

result = a3;
*(_WORD *)(a1 + 2LL * a2) = a3;
return result;
}

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

result = a3;
*(_WORD *)(a1 + 2LL * a2) = a3;
return result;
}

original_code:
void cpu_wregb(context_t *c, raddr_t r, byte_t v) {
 word_t w;
 w.l = v;
 w.h = 0x00;
 memcpy(&c->reg[r], &w, sizeof(word_t));
}

canonical_original_code:
void cpu_wregb(struct context_t *c, unsigned char r, unsigned char v) {
 struct word_t w;
 w.l = v;
 w.h = 0x00;
 memcpy(&c->reg[r], &w, sizeof(word_t));
}

variable_types:
v
unsigned char

variable_types: struct word_t { unsigned char l; unsigned char h; }


decompiled_code:
__int64 __fastcall cpu_rregb(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int8 a2)
{
return a1 + 2LL * a2;
}

original_code:
byte_t *cpu_rregb(context_t *c, raddr_t r) {
 return &c->reg[r].l;
}

canonical_original_code:
unsigned char *cpu_rregb(struct context_t *c, unsigned char r) {
 return &c->reg[r].l;
}

return_type: unsigned char *


decompiled_code:
__int64 __fastcall cpu_rmemw(__int64 a1, unsigned __int16 a2)
{
return a2 + a1 + 32;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int16 a2)
{
return a2 + a1 + 32;
}

original_code:
word_t *cpu_rmemw(context_t *c, maddr_t a) {
 word_t *p = (word_t *)(&c->mem[a]);
 return p;
}

canonical_original_code:
struct word_t *cpu_rmemw(struct context_t *c, unsigned short a) {
 struct word_t *p = (word_t *)(&c->mem[a]);
 return p;
}

variable_types:
a
unsigned short

variable_types: struct word_t { unsigned char l; unsigned char h; } *


decompiled_code:
void __fastcall cpu_reset(__int64 a1)
{
__int16 *v1; // rax

v1 = (__int16 *)cpu_rmemw(a1, 0xFFFEu);
cpu_wregw(a1, 0, *v1);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
__int16 *v1; // rax

v1 = (__int16 *)func6(a1, 0xFFFEu);
func1(a1, 0, *v1);
}

original_code:
void cpu_reset(context_t *c) {
 word_t *rv = cpu_rmemw(c, 0xfffe);
 cpu_wregw(c, 0, *rv);
}

canonical_original_code:
void cpu_reset(struct context_t *c) {
 struct word_t *rv = cpu_rmemw(c, 0xfffe);
 cpu_wregw(c, 0, *rv);
}


decompiled_code:
__int64 __fastcall cpu_rmemb(__int64 a1, unsigned __int16 a2)
{
return a2 + a1 + 32;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int16 a2)
{
return a2 + a1 + 32;
}

original_code:
byte_t *cpu_rmemb(context_t *c, maddr_t a) {
 byte_t *p = (byte_t *)(&c->mem[a]);
 return p;
}

canonical_original_code:
unsigned char *cpu_rmemb(struct context_t *c, unsigned short a) {
 unsigned char *p = (byte_t *)(&c->mem[a]);
 return p;
}

variable_types:
p
unsigned char *


decompiled_code:
__int64 __fastcall cpu_process_instruction(__int64 a1)
{
unsigned __int16 *v1; // r12
unsigned __int16 v2; // r15
char *v3; // rax
unsigned __int8 v4; // si
char v5; // bl
char v6; // r13
_BOOL4 v7; // er14
__int64 result; // rax
unsigned __int8 v9; // bl
unsigned __int8 v10; // si
unsigned __int8 *v11; // rax
__int16 *v12; // rax
unsigned __int8 *v13; // rax
unsigned __int16 *v14; // rax
char *v15; // [rsp+0h] [rbp-38h]

v1 = (unsigned __int16 *)cpu_rregw(a1, 0);
v2 = *v1;
v3 = (char *)cpu_rmemw(a1, *v1);
*v1 = v2 + 2;
v4 = v3[1];
v5 = *v3;
v6 = (unsigned __int8)*v3 >> 7;
v7 = (*v3 & 0x40) != 0;
result = ((unsigned __int8)*v3 >> 4) & 3;
v9 = v5 & 0xF;
if ( v4 >> 4 != 4 )
return 0xFFFFFFFFLL;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 3 )
{
v13 = (unsigned __int8 *)cpu_rmemw(a1, v2 + 2);
v15 = (char *)v13;
*v1 = v2 + 4;
if ( v6 )
{
v14 = (unsigned __int16 *)cpu_rmemw(a1, v2 + 4);
*v1 = v2 + 6;
if ( v7 )
cpu_wmemb(a1, *v14, *v15);
else
cpu_wmemw(a1, *v14, *(_WORD *)v15);
result = 0LL;
}
else
{
if ( v7 )
cpu_wregb(a1, v9, *v13);
else
cpu_wregw(a1, v9, *(_WORD *)v13);
result = 0LL;
}
}
else
{
result = 0LL;
}
}
else if ( !v6 )
{
v10 = v4 & 0xF;
if ( v7 )
{
v11 = (unsigned __int8 *)cpu_rregb(a1, v10);
cpu_wregb(a1, v9, *v11);
}
else
{
v12 = (__int16 *)cpu_rregw(a1, v10);
cpu_wregw(a1, v9, *v12);
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
unsigned __int16 *v1; // r12
unsigned __int16 v2; // r15
char *v3; // rax
unsigned __int8 v4; // si
char v5; // bl
char v6; // r13
_BOOL4 v7; // er14
__int64 result; // rax
unsigned __int8 v9; // bl
unsigned __int8 v10; // si
unsigned __int8 *v11; // rax
__int16 *v12; // rax
unsigned __int8 *v13; // rax
unsigned __int16 *v14; // rax
char *v15; // [rsp+0h] [rbp-38h]

v1 = (unsigned __int16 *)func2(a1, 0);
v2 = *v1;
v3 = (char *)func6(a1, *v1);
*v1 = v2 + 2;
v4 = v3[1];
v5 = *v3;
v6 = (unsigned __int8)*v3 >> 7;
v7 = (*v3 & 0x40) != 0;
result = ((unsigned __int8)*v3 >> 4) & 3;
v9 = v5 & 0xF;
if ( v4 >> 4 != 4 )
return 0xFFFFFFFFLL;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 3 )
{
v13 = (unsigned __int8 *)func6(a1, v2 + 2);
v15 = (char *)v13;
*v1 = v2 + 4;
if ( v6 )
{
v14 = (unsigned __int16 *)func6(a1, v2 + 4);
*v1 = v2 + 6;
if ( v7 )
func8(a1, *v14, *v15);
else
func5(a1, *v14, *(_WORD *)v15);
result = 0LL;
}
else
{
if ( v7 )
func3(a1, v9, *v13);
else
func1(a1, v9, *(_WORD *)v13);
result = 0LL;
}
}
else
{
result = 0LL;
}
}
else if ( !v6 )
{
v10 = v4 & 0xF;
if ( v7 )
{
v11 = (unsigned __int8 *)func4(a1, v10);
func3(a1, v9, *v11);
}
else
{
v12 = (__int16 *)func2(a1, v10);
func1(a1, v9, *v12);
}
result = 0LL;
}
return result;
}

original_code:
int cpu_process_instruction(context_t *c) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 word_t *sv;
 word_t *dv;
 word_t *pc = cpu_rregw(c, 0);
 maddr_t a = *((maddr_t *)(pc));
 word_t *inst = cpu_rmemw(c, a);
 (*(maddr_t *)(pc)) += sizeof(word_t);
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  switch(op) {
   case 0b0100:
    if(as == 0) {
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, *cpu_rregb(c, sr)); else cpu_wregw(c, dr, *cpu_rregw(c, sr));
     }
    }
    else if(as == 3) {
     a = *((maddr_t *)(pc));
     sv = cpu_rmemw(c, a);
     (*(maddr_t *)(pc)) += sizeof(word_t);
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, sv->l); else cpu_wregw(c, dr, *sv);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + sizeof(word_t));
      (*(maddr_t *)(pc)) += sizeof(word_t);
      if(bw) cpu_wmemb(c, *((maddr_t *)(dv)), sv->l); else cpu_wmemw(c, *((maddr_t *)(dv)), *sv);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

canonical_original_code:
int cpu_process_instruction(struct context_t *c) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 struct word_t *sv;
 struct word_t *dv;
 struct word_t *pc = cpu_rregw(c, 0);
 unsigned short a = *((maddr_t *)(pc));
 struct word_t *inst = cpu_rmemw(c, a);
 (*(unsigned short *)(pc)) += sizeof(word_t);
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  switch(op) {
   case 0b0100:
    if(as == 0) {
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, *cpu_rregb(c, sr)); else cpu_wregw(c, dr, *cpu_rregw(c, sr));
     }
    }
    else if(as == 3) {
     a = *((unsigned short *)(pc));
     sv = cpu_rmemw(c, a);
     (*(unsigned short *)(pc)) += sizeof(word_t);
     if(ad == 0) {
      if(bw) cpu_wregb(c, dr, sv->l); else cpu_wregw(c, dr, *sv);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + sizeof(word_t));
      (*(unsigned short *)(pc)) += sizeof(word_t);
      if(bw) cpu_wmemb(c, *((unsigned short *)(dv)), sv->l); else cpu_wmemw(c, *((unsigned short *)(dv)), *sv);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

variable_types: op int

variable_types: ad int

variable_types: bw int

variable_types: as int

variable_types: sr int

variable_types: dr int


original_code:
int cpu_disasm_instruction(context_t *c, maddr_t a, char *buf) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 word_t *sv;
 word_t *dv;
 word_t *inst = cpu_rmemw(c, a);
 buf[0] = 0;
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  sprintf(&buf[strlen(buf)], "[op=%02x, sr=%02x, ad=%02x, bw=%02x, as=%02x, dr=%02x]\t", op, sr, ad, bw, as, dr);
  switch(op) {
   case 0b0100:
    strcat(buf, "mov.");
    if(bw) strcat(buf, "b"); else strcat(buf, "w");
    if(as == 0) {
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " R%d, R%d", sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      sprintf(&buf[strlen(buf)], " R%d, 0x%02x%02x(R%d)", sr, dv->h, dv->l, dr);
     }
    }
    else if(as == 1) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), R%d", sv->h, sv->l, sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(dr == 2) {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), &0x%02x%02x",
        sv->h, sv->l, sr, dv->h, dv->l);
      }
      else {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), 0x%02x%02x(R%d)",
        sv->h, sv->l, sr, dv->h, dv->l, dr);
      }
     }
    }
    else if(as == 3) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, R%d", sv->l, dr);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, R%d", sv->h, sv->l, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, &0x%02x%02x", sv->l, dv->h, dv->l);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, &0x%02x%02x", sv->h, sv->l, dv->h, dv->l);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

canonical_original_code:
int cpu_disasm_instruction(struct context_t *c, unsigned short a, char *buf) {
 int op;
 int ad;
 int bw;
 int as;
 int sr;
 int dr;
 struct word_t *sv;
 struct word_t *dv;
 struct word_t *inst = cpu_rmemw(c, a);
 buf[0] = 0;
 if((inst->h & 0x11110000) == 0x11110000) {
 }
 else if((inst->h & 0x11100000) == 0x11100000) {
 }
 else {
  op = inst->h >> 4;
  sr = inst->h & 0b0001111;
  ad = inst->l >> 7;
  bw = (inst->l >> 6) & 0b00000001;
  as = (inst->l >> 4) & 0b00000011;
  dr = inst->l & 0b0001111;
  sprintf(&buf[strlen(buf)], "[op=%02x, sr=%02x, ad=%02x, bw=%02x, as=%02x, dr=%02x]\t", op, sr, ad, bw, as, dr);
  switch(op) {
   case 0b0100:
    strcat(buf, "mov.");
    if(bw) strcat(buf, "b"); else strcat(buf, "w");
    if(as == 0) {
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " R%d, R%d", sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      sprintf(&buf[strlen(buf)], " R%d, 0x%02x%02x(R%d)", sr, dv->h, dv->l, dr);
     }
    }
    else if(as == 1) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), R%d", sv->h, sv->l, sr, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(dr == 2) {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), &0x%02x%02x",
        sv->h, sv->l, sr, dv->h, dv->l);
      }
      else {
       sprintf(&buf[strlen(buf)], " 0x%02x%02x(R%d), 0x%02x%02x(R%d)",
        sv->h, sv->l, sr, dv->h, dv->l, dr);
      }
     }
    }
    else if(as == 3) {
     sv = cpu_rmemw(c, a + 1 * sizeof(word_t));
     if(ad == 0) {
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, R%d", sv->l, dr);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, R%d", sv->h, sv->l, dr);
     }
     else if(ad == 1) {
      dv = cpu_rmemw(c, a + 2 * sizeof(word_t));
      if(bw == 1)
       sprintf(&buf[strlen(buf)], " #0x%02x, &0x%02x%02x", sv->l, dv->h, dv->l);
      else
       sprintf(&buf[strlen(buf)], " #0x%02x%02x, &0x%02x%02x", sv->h, sv->l, dv->h, dv->l);
     }
    }
    break;
   default:
    return -1;
  }
 }
 return 0;
}

variable_types:
buf
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // ebx
_BYTE v6[65572]; // [rsp+0h] [rbp-10058h] BYREF
unsigned int v7; // [rsp+10024h] [rbp-34h] BYREF
unsigned int v8; // [rsp+10028h] [rbp-30h] BYREF
char v9[41]; // [rsp+1002Fh] [rbp-29h] BYREF

opterr = 0;
v3 = 0LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "i:");
if ( v4 == -1 )
break;
if ( v4 == 105 )
v3 = optarg;
}
if ( v3 )
{
puts("\n\n*** MSP430SIM ***\n");
puts("- s\tstep");
puts("- r\treset");
puts("- q\tquit\n");
printf("Loading bin file from: %s\n", v3);
v4 = bin_load(v3, v6, 63488LL);
if ( v4 != -1 )
{
cpu_reset(v6);
while ( 1 )
{
while ( 1 )
{
_isoc99_scanf("%c", v9);
if ( v9[0] != 114 )
break;
bin_load(v3, v6, 63488LL);
cpu_reset(v6);
puts("RESET");
LABEL_15:
if ( v9[0] == 113 )
return 0;
}
if ( v9[0] > 114 )
{
if ( v9[0] == 115 )
{
util_printreg(v6);
util_printmem(v6, 4294967288LL, 8LL);
if ( (unsigned int)cpu_process_instruction(v6) )
puts("\nERROR illigal instruction");
else
puts("\nOK");
goto LABEL_15;
}
}
else
{
if ( v9[0] == 109 )
{
printf("\nfrom address (hex): ");
_isoc99_scanf("%04x", &v8);
printf("to address (hex): ");
_isoc99_scanf("%04x", &v7);
util_printmem(v6, v8, v7);
goto LABEL_15;
}
if ( v9[0] == 113 )
{
puts("QUIT");
goto LABEL_15;
}
}
}
}
printf("ERROR opening file: %s\n", v3);
}
else
{
puts("ERROR no input file (-i) given.");
}
return v4;
}

original_code:
int main(int argc, char **argv) {
 char *binfile = ((void *)0);
 char c;
 int o;
 int s;
 int e;
 opterr = 0;
 while ((o = getopt (argc, argv, "i:")) != -1) {
  switch (o) {
   case 'i':
     binfile = optarg;
    break;
    }
 }
 if(!binfile) {
  printf("ERROR no input file (-i) given.\n");
  return -1;
 }
 context_t ctx;
 printf("\n\n*** MSP430SIM ***\n\n");
 printf("- s\tstep\n");
 printf("- r\treset\n");
 printf("- q\tquit\n\n");
 printf("Loading bin file from: %s\n", binfile);
 if(bin_load(binfile, &ctx, 0xF800) == -1) {
  printf("ERROR opening file: %s\n", binfile);
  return -1;
 }
 cpu_reset(&ctx);
 do {
  scanf("%c", &c);
  switch(c) {
   case 's':
    util_printreg(&ctx);
    util_printmem(&ctx, -8, 8);
    if(cpu_process_instruction(&ctx) != 0) {
     printf("\nERROR illigal instruction\n");
    }
    else {
     printf("\nOK\n");
    }
    break;
   case 'r':
    bin_load(binfile, &ctx, 0xF800);
    cpu_reset(&ctx);
    printf("RESET\n");
    break;
   case 'm':
    printf("\nfrom address (hex): ");
    scanf("%04x", &s);
    printf("to   address (hex): ");
    scanf("%04x", &e);
    util_printmem(&ctx, s, e);
    break;
   case 'q':
    printf("QUIT\n");
    break;
  }
 }
 while(c != 'q');
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 char *binfile = ((void *)0);
 char c;
 int o;
 int s;
 int e;
 opterr = 0;
 while ((o = getopt (argc, argv, "i:")) != -1) {
  switch (o) {
   case 'i':
     binfile = optarg;
    break;
    }
 }
 if(!binfile) {
  printf("ERROR no input file (-i) given.\n");
  return -1;
 }
 struct context_t ctx;
 printf("\n\n*** MSP430SIM ***\n\n");
 printf("- s\tstep\n");
 printf("- r\treset\n");
 printf("- q\tquit\n\n");
 printf("Loading bin file from: %s\n", binfile);
 if(bin_load(binfile, &ctx, 0xF800) == -1) {
  printf("ERROR opening file: %s\n", binfile);
  return -1;
 }
 cpu_reset(&ctx);
 do {
  scanf("%c", &c);
  switch(c) {
   case 's':
    util_printreg(&ctx);
    util_printmem(&ctx, -8, 8);
    if(cpu_process_instruction(&ctx) != 0) {
     printf("\nERROR illigal instruction\n");
    }
    else {
     printf("\nOK\n");
    }
    break;
   case 'r':
    bin_load(binfile, &ctx, 0xF800);
    cpu_reset(&ctx);
    printf("RESET\n");
    break;
   case 'm':
    printf("\nfrom address (hex): ");
    scanf("%04x", &s);
    printf("to   address (hex): ");
    scanf("%04x", &e);
    util_printmem(&ctx, s, e);
    break;
   case 'q':
    printf("QUIT\n");
    break;
  }
 }
 while(c != 'q');
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
binfile
char *

variable_types: c char

variable_types: o int

variable_types: s int

variable_types: e int

variable_types: struct context_t { struct word_t { unsigned char l; unsigned char h; } reg[16]; unsigned char mem[65535]; }


decompiled_code:
int __fastcall util_printreg(unsigned __int16 *a1)
{
unsigned int i; // ebx
unsigned __int16 *v2; // rbx

puts("\n\nRegister Dump\n-------------");
for ( i = 0; i != 16; ++i )
printf("R%02d |", i);
putchar(10);
v2 = a1;
do
printf("%04x|", *v2++);
while ( v2 != a1 + 16 );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(unsigned __int16 *a1)
{
unsigned int i; // ebx
unsigned __int16 *v2; // rbx

puts("\n\nRegister Dump\n-------------");
for ( i = 0; i != 16; ++i )
printf("R%02d |", i);
putchar(10);
v2 = a1;
do
printf("%04x|", *v2++);
while ( v2 != a1 + 16 );
return putchar(10);
}

original_code:
void util_printreg(context_t *c) {
 int i;
 printf("\n\nRegister Dump\n-------------\n");
 for(i = 0; i < 16; i++) {
  printf("R%02d |", i);
 }
 printf("\n");
 for(i = 0; i < 16; i++) {
  printf("%04x|", *(uint16_t *)(&c->reg[i]));
 }
 printf("\n");
}

canonical_original_code:
void util_printreg(struct context_t *c) {
 int i;
 printf("\n\nRegister Dump\n-------------\n");
 for(i = 0; i < 16; i++) {
  printf("R%02d |", i);
 }
 printf("\n");
 for(i = 0; i < 16; i++) {
  printf("%04x|", *(unsigned short *)(&c->reg[i]));
 }
 printf("\n");
}

variable_types: struct context_t { struct word_t { unsigned char l; unsigned char h; } reg[16]; unsigned char mem[65535]; } *

variable_types: i int


decompiled_code:
int __fastcall util_printmem(__int64 a1, int a2, int a3)
{
int v3; // ebx
unsigned __int16 v5; // r15
int result; // eax
unsigned __int8 *v7; // rbp
char v8[120]; // [rsp+0h] [rbp-78h] BYREF

v3 = a2;
v5 = 0;
if ( a2 < 0 )
{
v5 = *(_WORD *)cpu_rregw(a1, 0LL);
v3 = v5 + a2;
if ( v3 < 0 )
v3 = 0;
a3 += v5;
if ( a3 > 0xFFFF )
a3 = 0xFFFF;
}
for ( result = printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", (unsigned int)v3, (unsigned int)a3);
a3 >= v3;
v3 += 2 )
{
v7 = (unsigned __int8 *)cpu_rmemw(a1, (unsigned __int16)v3);
if ( v5 == v3 )
printf("-> ");
else
printf(" ");
cpu_disasm_instruction(a1, (unsigned __int16)v3, v8);
result = printf("%04x|%02x%02x\t%s\n", (unsigned int)v3, v7[1], *v7, v8);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, int a2, int a3)
{
int v3; // ebx
unsigned __int16 v5; // r15
int result; // eax
unsigned __int8 *v7; // rbp
char v8[120]; // [rsp+0h] [rbp-78h] BYREF

v3 = a2;
v5 = 0;
if ( a2 < 0 )
{
v5 = *(_WORD *)cpu_rregw(a1, 0LL);
v3 = v5 + a2;
if ( v3 < 0 )
v3 = 0;
a3 += v5;
if ( a3 > 0xFFFF )
a3 = 0xFFFF;
}
for ( result = printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", (unsigned int)v3, (unsigned int)a3);
a3 >= v3;
v3 += 2 )
{
v7 = (unsigned __int8 *)cpu_rmemw(a1, (unsigned __int16)v3);
if ( v5 == v3 )
printf("-> ");
else
printf(" ");
cpu_disasm_instruction(a1, (unsigned __int16)v3, v8);
result = printf("%04x|%02x%02x\t%s\n", (unsigned int)v3, v7[1], *v7, v8);
}
return result;
}

original_code:
void util_printmem(context_t *c, int s, int e) {
 char buf[64];
 word_t *w;
 int i;
 int ds;
 int de;
 word_t *pc;
 maddr_t a = 0;
 if(s < 0) {
  pc = cpu_rregw(c, 0);
  a = *((maddr_t *)(pc));
  ds = (int)(a) + s;
  if(ds < 0) ds = 0;
  de = a + e;
  if(de > 0xFFFF) de = 0xFFFF;
 }
 else {
  ds = s;
  de = e;
 }
 printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", ds,de);
 for(i = ds; i <= de; i += sizeof(word_t)) {
  w = cpu_rmemw(c, i);
  if(i == a) printf("-> "); else printf("   ");
  cpu_disasm_instruction(c, i, buf);
  printf("%04x|%02x%02x\t%s\n", i, w->h, w->l, buf);
 }
}

canonical_original_code:
void util_printmem(struct context_t *c, int s, int e) {
 char buf[64];
 struct word_t *w;
 int i;
 int ds;
 int de;
 struct word_t *pc;
 unsigned short a = 0;
 if(s < 0) {
  pc = cpu_rregw(c, 0);
  a = *((unsigned short *)(pc));
  ds = (int)(a) + s;
  if(ds < 0) ds = 0;
  de = a + e;
  if(de > 0xFFFF) de = 0xFFFF;
 }
 else {
  ds = s;
  de = e;
 }
 printf("\n\nMemory Dump (%04x..%04x)\n-------------------------\n", ds,de);
 for(i = ds; i <= de; i += sizeof(word_t)) {
  w = cpu_rmemw(c, i);
  if(i == a) printf("-> "); else printf("   ");
  cpu_disasm_instruction(c, i, buf);
  printf("%04x|%02x%02x\t%s\n", i, w->h, w->l, buf);
 }
}

variable_types:
buf
char [64]

variable_types: struct word_t { unsigned char l; unsigned char h; } *

variable_types: ds int

variable_types: de int

variable_types:
a
unsigned short


runPsProg

decompiled_code:
__int64 runPsProg()
{
initArray((unsigned int)arraySize);
readPs();
return print();
}

canonical_decompiled_code:
__int64 func7()
{
func8((unsigned int)arraySize);
func13();
return func17();
}

original_code:
void runPsProg()
{
   initArray(arraySize);
   readPs();
   print();
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
runPsProg(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7(argc, argv, envp);
return 0;
}

original_code:
int main(int argc, char ** argv)
{
   runPsProg();
   return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
   runPsProg();
   return 0;
}


initArray

decompiled_code:
void *__fastcall initArray(int a1)
{
void *result; // rax

result = calloc(a1, 0x408uLL);
array = result;
return result;
}

canonical_decompiled_code:
void *__fastcall func8(int a1)
{
void *result; // rax

result = calloc(a1, 0x408uLL);
array = result;
return result;
}

original_code:
void initArray(int size)
{
   array = calloc(size, sizeof(PrintOut));
}

variable_types: size int


expandArray

decompiled_code:
int __fastcall expandArray(int a1, int a2)
{
void *v2; // rax

v2 = realloc(array, 1032LL * (a2 + a1));
array = v2;
if ( v2 )
{
arraySize = a2 + a1;
}
else
{
LODWORD(v2) = puts("Cannot allocate more memory.");
arraySize = 0;
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func9(int a1, int a2)
{
void *v2; // rax

v2 = realloc(array, 1032LL * (a2 + a1));
array = v2;
if ( v2 )
{
arraySize = a2 + a1;
}
else
{
LODWORD(v2) = puts("Cannot allocate more memory.");
arraySize = 0;
}
return (int)v2;
}

original_code:
void expandArray(int curSize, int expSize)
{
    int totalSize = expSize + curSize;
 array = realloc(array, totalSize*sizeof(PrintOut));
 if (array == ((void *)0))
    {
        printf("Cannot allocate more memory.\n");
        arraySize = 0;
    }
    else
    {
        arraySize = totalSize;
    }
}

variable_types: curSize int

variable_types: expSize int

variable_types: totalSize int


checkFull

decompiled_code:
_BOOL8 checkFull()
{
return arraySize - 2 == elements;
}

canonical_decompiled_code:
_BOOL8 func10()
{
return arraySize - 2 == elements;
}

original_code:
int checkFull()
{
  if(elements == (arraySize-2))
  {return 1;}
  else
  {return 0;}
}


saveData

decompiled_code:
int __fastcall saveData(char *src, char *nptr, char *a3, __int64 a4)
{
const char *v6; // rbx
__int64 v7; // r15
__int64 v8; // rbp
int result; // eax
char v10[1024]; // [rsp+0h] [rbp-838h] BYREF
char dest[1080]; // [rsp+400h] [rbp-438h] BYREF

strcpy(v10, src);
v6 = stripSyms(v10, "()");
v7 = a4 + 1032LL * elements;
strcpy((char *)(v7 + 8), a3);
strcpy(dest, (const char *)(v7 + 8));
v10[strlen(dest) + 1023] = 0;
strcpy((char *)(v7 + 8), dest);
*(_DWORD *)v7 = atoi(v6);
LODWORD(v6) = elements;
v8 = a4 + 1032LL * elements;
result = atoi(nptr);
*(_DWORD *)(v8 + 4) = result;
elements = (_DWORD)v6 + 1;
return result;
}

canonical_decompiled_code:
int __fastcall func11(char *src, char *nptr, char *a3, __int64 a4)
{
const char *v6; // rbx
__int64 v7; // r15
__int64 v8; // rbp
int result; // eax
char v10[1024]; // [rsp+0h] [rbp-838h] BYREF
char dest[1080]; // [rsp+400h] [rbp-438h] BYREF

strcpy(v10, src);
v6 = stripSyms(v10, "()");
v7 = a4 + 1032LL * elements;
strcpy((char *)(v7 + 8), a3);
strcpy(dest, (const char *)(v7 + 8));
v10[strlen(dest) + 1023] = 0;
strcpy((char *)(v7 + 8), dest);
*(_DWORD *)v7 = atoi(v6);
LODWORD(v6) = elements;
v8 = a4 + 1032LL * elements;
result = atoi(nptr);
*(_DWORD *)(v8 + 4) = result;
elements = (_DWORD)v6 + 1;
return result;
}

original_code:
void saveData(char * processID, char * parentID, char * command, PrintOut *emptyArr)
{
    char temp [1024];
    char temp2 [1024];
    strcpy(temp2, processID);
    char * strippedPID = stripSyms(temp2,"()");
 strcpy(emptyArr[elements].CMD, command);
    strcpy(temp,emptyArr[elements].CMD);
    temp[strlen(temp)-1] = '\0';
    strcpy(emptyArr[elements].CMD,temp);
 emptyArr[elements].PID = atoi(strippedPID);
 emptyArr[elements].PPID = atoi(parentID);
    elements++;
}

canonical_original_code:
void saveData(char *processID, char *parentID, char *command, struct PrintOut *emptyArr)
{
    char temp[1024];
    char temp2[1024];
    strcpy(temp2, processID);
    char *strippedPID = stripSyms(temp2,"()");
 strcpy(emptyArr[elements].CMD, command);
    strcpy(temp,emptyArr[elements].CMD);
    temp[strlen(temp)-1] = '\0';
    strcpy(emptyArr[elements].CMD,temp);
 emptyArr[elements].PID = atoi(strippedPID);
 emptyArr[elements].PPID = atoi(parentID);
    elements++;
}

variable_types:
processID
char *

variable_types:
parentID
char *

variable_types:
command
char *

variable_types: struct PrintOut { int PID; int PPID; char CMD[1024]; } *

variable_types:
temp
char [1024]

variable_types:
strippedPID
char *


parsePsLine

decompiled_code:
int __fastcall parsePsLine(char *a1)
{
char *v1; // r12
char *v2; // r13
char *v3; // rax
char *v4; // rbp
int v5; // ebx
char *v6; // rax

v3 = strtok(a1, " ");
if ( v3 )
{
v4 = v3;
v5 = 1;
while ( 1 )
{
if ( v5 == 2 )
{
v2 = v4;
}
else if ( v5 == 3 )
{
v1 = v4;
}
v6 = strtok(0LL, " ");
++v5;
if ( !v6 )
break;
v4 = v6;
}
}
else
{
v4 = 0LL;
}
return saveData(v2, v1, v4, (__int64)array);
}

canonical_decompiled_code:
int __fastcall func12(char *a1)
{
char *v1; // r12
char *v2; // r13
char *v3; // rax
char *v4; // rbp
int v5; // ebx
char *v6; // rax

v3 = strtok(a1, " ");
if ( v3 )
{
v4 = v3;
v5 = 1;
while ( 1 )
{
if ( v5 == 2 )
{
v2 = v4;
}
else if ( v5 == 3 )
{
v1 = v4;
}
v6 = strtok(0LL, " ");
++v5;
if ( !v6 )
break;
v4 = v6;
}
}
else
{
v4 = 0LL;
}
return func11(v2, v1, v4, (__int64)array);
}

original_code:
void parsePsLine(char * bufLine)
{
       char * processID; char * parentID; char * command; char * current;
       int c = 1; char * nextWordPtr; char * buf = bufLine; char * currentTemp;
          int PID; int PPID; char tempCMD[1024];
          PID = 2; PPID = 3;
       nextWordPtr = strtok(buf," ");
          while (nextWordPtr != ((void *)0)) {
          current = nextWordPtr;
          if(c == PID)
          {processID = nextWordPtr;}
          else if ( c == PPID)
          {parentID = nextWordPtr;}
           nextWordPtr = strtok(((void *)0)," ");
           c++;
          }
          command = current;
          saveData(processID,parentID,command,array);
}

canonical_original_code:
void parsePsLine(char *bufLine)
{
       char *processID; char *parentID; char *command; char *current;
       int c = 1; char *nextWordPtr; char *buf = bufLine; char *currentTemp;
          int PID; int PPID; char tempCMD[1024];
          PID = 2; PPID = 3;
       nextWordPtr = strtok(buf," ");
          while (nextWordPtr != ((void *)0)) {
          current = nextWordPtr;
          if(c == PID)
          {processID = nextWordPtr;}
          else if ( c == PPID)
          {parentID = nextWordPtr;}
           nextWordPtr = strtok(((void *)0)," ");
           c++;
          }
          command = current;
          saveData(processID,parentID,command,array);
}

variable_types:
bufLine
char *

variable_types:
current
char *

variable_types: c int

variable_types:
nextWordPtr
char *

variable_types:
buf
char *

variable_types:
currentTemp
char *

variable_types: PID int

variable_types: PPID int

variable_types:
tempCMD
char [1024]


readPs

decompiled_code:
int readPs()
{
FILE *v0; // rax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = popen("/bin/ps -aefc", "r");
psAux = v0;
if ( v0 )
{
fgets(v2, 1024, v0);
while ( fgets(v2, 1024, psAux) )
{
if ( checkFull() )
expandArray(arraySize, expandSize);
parsePsLine(v2);
}
}
return pclose(psAux);
}

canonical_decompiled_code:
int func13()
{
FILE *v0; // rax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = popen("/bin/ps -aefc", "r");
psAux = v0;
if ( v0 )
{
fgets(v2, 1024, v0);
while ( fgets(v2, 1024, psAux) )
{
if ( func10() )
func9(arraySize, expandSize);
func12(v2);
}
}
return pclose(psAux);
}

original_code:
void readPs(void)
{
      char buf[1024];
      psAux = popen("/bin/ps -aefc", "r");
      if (psAux)
      {
          fgets(buf, sizeof(buf), psAux);
          while(fgets(buf, sizeof(buf), psAux))
          {
             if(checkFull() == 1)
             {expandArray(arraySize,expandSize);}
             parsePsLine(buf);
          }
     }
     pclose(psAux);
}

canonical_original_code:
void readPs()
{
      char buf[1024];
      psAux = popen("/bin/ps -aefc", "r");
      if (psAux)
      {
          fgets(buf, sizeof(buf), psAux);
          while(fgets(buf, sizeof(buf), psAux))
          {
             if(checkFull() == 1)
             {expandArray(arraySize,expandSize);}
             parsePsLine(buf);
          }
     }
     pclose(psAux);
}

variable_types:
buf
char [1024]


stripThree

decompiled_code:
__int64 __fastcall stripThree(__int64 a1)
{
return a1 + 3;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return a1 + 3;
}

original_code:
char *stripThree(char *string)
{
    return string+3;
}

variable_types:
string
char *


printTabs

decompiled_code:
int __fastcall printTabs(int a1)
{
int i; // ebx
int result; // eax

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
result = putchar(9);
}
return result;
}

canonical_decompiled_code:
int __fastcall func15(int a1)
{
int i; // ebx
int result; // eax

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
result = putchar(9);
}
return result;
}

original_code:
void printTabs(int numTabs)
{
    int j;
    for(j = 0; j < numTabs; j++)
    {
        printf("\t");
    }
}

variable_types: numTabs int

variable_types: j int


printTree

decompiled_code:
void *__fastcall printTree(int a1, int a2)
{
__int64 v2; // rbx
int v3; // ebp
void *result; // rax

if ( elements > 0 )
{
v2 = 0LL;
v3 = 0;
do
{
result = array;
if ( *(_DWORD *)((char *)array + v2 + 4) == a2 )
{
printTabs(a1);
printf(
"%s [PID: %d] [PPID: %d]\n",
(const char *)array + v2 + 8,
*(unsigned int *)((char *)array + v2),
*(unsigned int *)((char *)array + v2 + 4));
result = (void *)printTree((unsigned int)(a1 + 1), *(unsigned int *)((char *)array + v2));
}
++v3;
v2 += 1032LL;
}
while ( elements > v3 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func16(int a1, int a2)
{
__int64 v2; // rbx
int v3; // ebp
void *result; // rax

if ( elements > 0 )
{
v2 = 0LL;
v3 = 0;
do
{
result = array;
if ( *(_DWORD *)((char *)array + v2 + 4) == a2 )
{
func15(a1);
printf(
"%s [PID: %d] [PPID: %d]\n",
(const char *)array + v2 + 8,
*(unsigned int *)((char *)array + v2),
*(unsigned int *)((char *)array + v2 + 4));
result = (void *)func16((unsigned int)(a1 + 1), *(unsigned int *)((char *)array + v2));
}
++v3;
v2 += 1032LL;
}
while ( elements > v3 );
}
return result;
}

original_code:
void printTree(int depth, int curID)
{
     int j;
     int r;
     for(j = 0; j < elements; j ++)
     {
        if(array[j].PPID == curID)
        {
           printTabs(depth);
           printf("%s [PID: %d] [PPID: %d]\n",array[j].CMD, array[j].PID, array[j].PPID);
           r = array[j].PID;
           printTree(depth+1,r);
        }
     }
     return;
}

variable_types: depth int

variable_types: curID int

variable_types: r int


print

decompiled_code:
void print()
{
__int64 v0; // rbx
int v1; // ebp

if ( elements > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
if ( !*(_DWORD *)((char *)array + v0 + 4) )
{
printf("%s [PID %d] [PPID %d]\n", (const char *)array + v0 + 8, *(unsigned int *)((char *)array + v0), 0LL);
printTree(2, *(_DWORD *)((char *)array + v0));
}
++v1;
v0 += 1032LL;
}
while ( elements > v1 );
}
free(array);
}

canonical_decompiled_code:
void func17()
{
__int64 v0; // rbx
int v1; // ebp

if ( elements > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
if ( !*(_DWORD *)((char *)array + v0 + 4) )
{
printf("%s [PID %d] [PPID %d]\n", (const char *)array + v0 + 8, *(unsigned int *)((char *)array + v0), 0LL);
func16(2, *(_DWORD *)((char *)array + v0));
}
++v1;
v0 += 1032LL;
}
while ( elements > v1 );
}
free(array);
}

original_code:
void print(void)
{
   int j;
   int r;
   int p = 0;
   for(j = 0; j < elements; j ++)
   {
       if(array[j].PPID == p)
       {
          printf("%s [PID %d] [PPID %d]\n",array[j].CMD, array[j].PID, array[j].PPID);
          r = array[j].PID;
          printTree(2,r);
       }
   }
   free(array);
}

canonical_original_code:
void print()
{
   int j;
   int r;
   int p = 0;
   for(j = 0; j < elements; j ++)
   {
       if(array[j].PPID == p)
       {
          printf("%s [PID %d] [PPID %d]\n",array[j].CMD, array[j].PID, array[j].PPID);
          r = array[j].PID;
          printTree(2,r);
       }
   }
   free(array);
}

variable_types: p int


testArrayInit

decompiled_code:
_BOOL8 testArrayInit()
{
int v0; // eax
int v1; // eax

initArray(arraySize);
if ( arraySize <= 0 )
{
v1 = 0;
}
else
{
v0 = 0;
do
++v0;
while ( v0 != arraySize );
v1 = arraySize;
}
return arraySize != v1;
}

canonical_decompiled_code:
_BOOL8 func18()
{
int v0; // eax
int v1; // eax

func8(arraySize);
if ( arraySize <= 0 )
{
v1 = 0;
}
else
{
v0 = 0;
do
++v0;
while ( v0 != arraySize );
v1 = arraySize;
}
return arraySize != v1;
}

original_code:
int testArrayInit(void) {
    int expectedStartingArraySize = 100;
    int i;
    int countSize = 0;
    initArray(arraySize);
    for(i = 0; i < arraySize; i++)
 {
        array[i];
        countSize++;
    }
    if(countSize == arraySize)
    {return 0;}
    else
    {return 1;}
}

canonical_original_code:
int testArrayInit() {
    int expectedStartingArraySize = 100;
    int i;
    int countSize = 0;
    initArray(arraySize);
    for(i = 0; i < arraySize; i++)
 {
        array[i];
        countSize++;
    }
    if(countSize == arraySize)
    {return 0;}
    else
    {return 1;}
}

variable_types: expectedStartingArraySize int

variable_types: i int

variable_types: countSize int


testReadPs

decompiled_code:
_BOOL8 testReadPs()
{
readPs();
return elements <= 0;
}

canonical_decompiled_code:
_BOOL8 func19()
{
func13();
return elements <= 0;
}

original_code:
int testReadPs(void) {
    int countSize = 0;
    int numRead = 0;
    readPs();
    numRead = elements;
    if(numRead > 0)
    {
      if( numRead > 100)
      {return 0;}
      else if( numRead <= 100)
      {return 0;}
    }
    else
    {return 1;}
}

canonical_original_code:
int testReadPs() {
    int countSize = 0;
    int numRead = 0;
    readPs();
    numRead = elements;
    if(numRead > 0)
    {
      if( numRead > 100)
      {return 0;}
      else if( numRead <= 100)
      {return 0;}
    }
    else
    {return 1;}
}

variable_types: numRead int


teststripSyms

decompiled_code:
__int64 teststripSyms()
{
stripSyms("(9)", "()");
return 1LL;
}

original_code:
int teststripSyms()
{
   char * expected = "9";
   char * input = "(9)";
   char * actual = stripSyms(input,"()");
   if(expected == input)
   {return 0;}
   else
   {return 1;}
}

canonical_original_code:
int teststripSyms()
{
   char *expected = "9";
   char *input = "(9)";
   char *actual = stripSyms(input,"()");
   if(expected == input)
   {return 0;}
   else
   {return 1;}
}

variable_types:
expected
char *

variable_types:
input
char *

variable_types:
actual
char *


runTests

decompiled_code:
__int64 runTests()
{
_BOOL4 v0; // ebx
_BOOL4 v1; // ebx
int v2; // ebp

v0 = testArrayInit();
v1 = testReadPs() + v0;
v2 = teststripSyms();
free(array);
return (unsigned int)(v1 + v2);
}

canonical_decompiled_code:
__int64 func21()
{
_BOOL4 v0; // ebx
_BOOL4 v1; // ebx
int v2; // ebp

v0 = func18();
v1 = func19() + v0;
v2 = func20();
free(array);
return (unsigned int)(v1 + v2);
}

original_code:
int runTests() {
    int passCount = 0;
    passCount += testArrayInit();
    passCount += testReadPs();
    passCount += teststripSyms();
    free(array);
    return passCount;
}

variable_types: passCount int


repo: weongyo/varnish-perf

decompiled_code:
void *__fastcall cot_callwhell_alloc(__int64 a1)
{
int v2; // edx
int v3; // eax
int i; // edi
int v5; // ecx
void *result; // rax
int *v7; // rax

*(_DWORD *)(a1 + 20) = 1;
*(_DWORD *)(a1 + 24) = 0;
v2 = *(_DWORD *)(a1 + 16);
if ( v2 <= 1 )
{
i = 1;
}
else
{
v3 = 1;
for ( i = 1; i < v2; i *= 2 )
v5 = v3++;
*(_DWORD *)(a1 + 20) = i;
*(_DWORD *)(a1 + 24) = v5;
}
*(_DWORD *)(a1 + 28) = i - 1;
result = calloc(i, 0x10uLL);
*(_QWORD *)(a1 + 32) = result;
if ( !result )
{
v7 = _errno_location();
result = (void *)VAS_Fail(
"cot_callwhell_alloc",
"vcallout.c",
202LL,
"(cb->callwheel) != 0",
(unsigned int)*v7,
0LL);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1)
{
int v2; // edx
int v3; // eax
int i; // edi
int v5; // ecx
void *result; // rax
int *v7; // rax

*(_DWORD *)(a1 + 20) = 1;
*(_DWORD *)(a1 + 24) = 0;
v2 = *(_DWORD *)(a1 + 16);
if ( v2 <= 1 )
{
i = 1;
}
else
{
v3 = 1;
for ( i = 1; i < v2; i *= 2 )
v5 = v3++;
*(_DWORD *)(a1 + 20) = i;
*(_DWORD *)(a1 + 24) = v5;
}
*(_DWORD *)(a1 + 28) = i - 1;
result = calloc(i, 0x10uLL);
*(_QWORD *)(a1 + 32) = result;
if ( !result )
{
v7 = _errno_location();
result = (void *)VAS_Fail(
"cot_callwhell_alloc",
"vcallout.c",
202LL,
"(cb->callwheel) != 0",
(unsigned int)*v7,
0LL);
}
return result;
}

original_code:
static void
cot_callwhell_alloc(struct callout_block *cb)
{
 for (cb->callwheelsize = 1, cb->callwheelbits = 0;
      cb->callwheelsize < cb->ncallout;
      cb->callwheelsize <<= 1, ++cb->callwheelbits)
  ;
 cb->callwheelmask = cb->callwheelsize - 1;
 cb->callwheel = calloc(cb->callwheelsize, sizeof(struct callout_tailq));
 do { do { if (!((cb->callwheel) != 0)) VAS_Fail(__func__, "vcallout.c", 202, "(cb->callwheel) != 0", (*__errno_location ()), 0); } while (0); } while (0);
}

variable_types: struct callout_block { long ticks; long softticks; int ncallout; int callwheelsize; int callwheelbits; int callwheelmask; struct callout_tailq { struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; } *vtqh_first; struct callout **vtqh_last; } *callwheel; struct callout *nextsoftcheck; } *

user_defined_types: struct callout_tailq { struct callout *vtqh_first; struct callout **vtqh_last; }

user_defined_types: struct callout_block { long ticks; long softticks; int ncallout; int callwheelsize; int callwheelbits; int callwheelmask; struct callout_tailq *callwheel; struct callout *nextsoftcheck; }


original_code:
void
callout_init(struct callout *c, int id)
{
 bzero(c, sizeof *c);
 c->magic = 0x2d634820;
 c->c_id = id;
}

variable_types: struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; } *

variable_types: id int


original_code:
void
COT_clock(struct callout_block *cb)
{
 struct callout *c;
 struct callout_tailq *bucket;
 clock_t curticks;
 int steps;
 int depth;
 int mpcalls;
 int mtxcalls;
 int gcalls;
 mpcalls = 0;
 mtxcalls = 0;
 gcalls = 0;
 depth = 0;
 steps = 0;
 while (cb->softticks != cb->ticks) {
  cb->softticks++;
  curticks = cb->softticks;
  bucket = &cb->callwheel[curticks & cb->callwheelmask];
  c = ((bucket)->vtqh_first);
  while (c) {
   depth++;
   if (c->c_time != curticks) {
    c = ((c)->c_links.tqe.vtqe_next);
    ++steps;
    if (steps >= 100) {
     cb->nextsoftcheck = c;
     c = cb->nextsoftcheck;
     steps = 0;
    }
   } else {
    void (*c_func)(void *);
    void *c_arg;
    cb->nextsoftcheck = ((c)->c_links.tqe.vtqe_next);
    do { if (((((c))->c_links.tqe.vtqe_next)) != ((void *)0)) (((c))->c_links.tqe.vtqe_next)->c_links.tqe.vtqe_prev = (c)->c_links.tqe.vtqe_prev; else { (bucket)->vtqh_last = (c)->c_links.tqe.vtqe_prev; } *(c)->c_links.tqe.vtqe_prev = (((c))->c_links.tqe.vtqe_next); } while (0);
    c_func = c->c_func;
    c_arg = c->c_arg;
    c->c_flags = (c->c_flags & ~0x0004);
    mpcalls++;
    if (callout_debug)
     fprintf(stdout,
         "callout mpsafe %p func %p "
         "arg %p", c, c_func, c_arg);
    c_func(c_arg);
    if (callout_debug)
     fprintf(stdout,
         "callout %p finished", c);
    steps = 0;
    c = cb->nextsoftcheck;
   }
  }
 }
 avg_depth += (depth * 1000 - avg_depth) >> 8;
 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
 avg_mtxcalls += (mtxcalls * 1000 - avg_mtxcalls) >> 8;
 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
 cb->nextsoftcheck = ((void *)0);
}

canonical_original_code:
void COT_clock(struct callout_block *cb)
{
 struct callout *c;
 struct callout_tailq *bucket;
 long curticks;
 int steps;
 int depth;
 int mpcalls;
 int mtxcalls;
 int gcalls;
 mpcalls = 0;
 mtxcalls = 0;
 gcalls = 0;
 depth = 0;
 steps = 0;
 while (cb->softticks != cb->ticks) {
  cb->softticks++;
  curticks = cb->softticks;
  bucket = &cb->callwheel[curticks & cb->callwheelmask];
  c = ((bucket)->vtqh_first);
  while (c) {
   depth++;
   if (c->c_time != curticks) {
    c = ((c)->c_links.tqe.vtqe_next);
    ++steps;
    if (steps >= 100) {
     cb->nextsoftcheck = c;
     c = cb->nextsoftcheck;
     steps = 0;
    }
   } else {
    void (*c_func)(void *);
    void *c_arg;
    cb->nextsoftcheck = ((c)->c_links.tqe.vtqe_next);
    do { if (((((c))->c_links.tqe.vtqe_next)) != ((void *)0)) (((c))->c_links.tqe.vtqe_next)->c_links.tqe.vtqe_prev = (c)->c_links.tqe.vtqe_prev; else { (bucket)->vtqh_last = (c)->c_links.tqe.vtqe_prev; } *(c)->c_links.tqe.vtqe_prev = (((c))->c_links.tqe.vtqe_next); } while (0);
    c_func = c->c_func;
    c_arg = c->c_arg;
    c->c_flags = (c->c_flags & ~0x0004);
    mpcalls++;
    if (callout_debug)
     fprintf(stdout,
         "callout mpsafe %p func %p "
         "arg %p", c, c_func, c_arg);
    c_func(c_arg);
    if (callout_debug)
     fprintf(stdout,
         "callout %p finished", c);
    steps = 0;
    c = cb->nextsoftcheck;
   }
  }
 }
 avg_depth += (depth * 1000 - avg_depth) >> 8;
 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
 avg_mtxcalls += (mtxcalls * 1000 - avg_mtxcalls) >> 8;
 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
 cb->nextsoftcheck = ((void *)0);
}

variable_types: struct callout_tailq { struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; } *vtqh_first; struct callout **vtqh_last; } *

variable_types: curticks long

variable_types: steps int

variable_types: mpcalls int

variable_types: mtxcalls int

variable_types: gcalls int


decompiled_code:
clock_t __fastcall COT_ticks(clock_t *a1)
{
clock_t result; // rax
struct tms v2; // [rsp+0h] [rbp-28h] BYREF

result = times(&v2);
*a1 = result;
return result;
}

canonical_decompiled_code:
clock_t __fastcall func5(clock_t *a1)
{
clock_t result; // rax
struct tms v2; // [rsp+0h] [rbp-28h] BYREF

result = times(&v2);
*a1 = result;
return result;
}

original_code:
void
COT_ticks(struct callout_block *cb)
{
 struct tms tms;
 cb->ticks = times(&tms);
}

variable_types: struct tms { long tms_utime; long tms_stime; long tms_cutime; long tms_cstime; }

user_defined_types: struct callout { unsigned int magic; union { struct { struct callout *vsle_next; } sle; struct { struct callout *vtqe_next; struct callout **vtqe_prev; } tqe; } c_links; long c_time; void *c_arg; void (*c_func)(void *); int c_flags; int c_id; char *d_func; int d_line; }


decompiled_code:
void __fastcall COT_fini(__int64 a1)
{
free(*(void **)(a1 + 32));
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
free(*(void **)(a1 + 32));
}

original_code:
void
COT_fini(struct callout_block *cb)
{
 free(cb->callwheel);
}