decompiled_code:
__int64 apic_local_is_pending()
{
int i; // ebx
unsigned int v1; // eax
int v2; // eax

for ( i = 8; ; --i )
{
v1 = apic_local_rdreg(4 * (unsigned int)(unsigned __int8)i + 512);
v2 = bsr(v1);
if ( (char)v2 > 0 )
break;
}
return (unsigned int)(32 * i + v2);
}

canonical_decompiled_code:
__int64 func9()
{
int i; // ebx
unsigned int v1; // eax
int v2; // eax

for ( i = 8; ; --i )
{
v1 = func0(4 * (unsigned int)(unsigned __int8)i + 512);
v2 = bsr(v1);
if ( (char)v2 > 0 )
break;
}
return (unsigned int)(32 * i + v2);
}

original_code:
uint8_t apic_local_is_pending(void)
{
  uint8_t i = 8;
  uint32_t irr;
  int8_t msb;
  while (i >= 0)
    {
      irr = _apic_local_rdreg(0x200 + i * sizeof (uint32_t));
      msb = bsr(irr);
      if (msb > 0)
 {
   return i * 32 + msb;
 }
      i--;
    }
  return 0;
}

canonical_original_code:
unsigned char apic_local_is_pending()
{
  unsigned char i = 8;
  unsigned int irr;
  char msb;
  while (i >= 0)
    {
      irr = _apic_local_rdreg(0x200 + i * sizeof (unsigned int));
      msb = bsr(irr);
      if (msb > 0)
 {
   return i * 32 + msb;
 }
      i--;
    }
  return 0;
}

variable_types:
i
unsigned char

variable_types:
irr
unsigned int

variable_types: msb char


repo: bmarcot/kemerald

decompiled_code:
__int64 console_clear()
{
memzero(&g_console_video, 4000LL);
g_console_line = 0;
g_console_column = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
memzero(&g_console_video, 4000LL);
g_console_line = 0;
g_console_column = 0;
return 0LL;
}

original_code:
int32_t console_clear(void)
{
  memzero((void *)g_console_video, 2 * 80 * 25);
  g_console_column = g_console_line = 0;
  return 0;
}

canonical_original_code:
int console_clear()
{
  memzero((void *)g_console_video, 2 * 80 * 25);
  g_console_column = g_console_line = 0;
  return 0;
}

return_type: int


decompiled_code:
__int64 console_init()
{
char *v0; // rax

g_console_line = 0;
g_console_column = 0;
console_clear();
v0 = &byte_221;
do
{
*v0 = 15;
v0 += 2;
}
while ( v0 != (char *)&unk_11C1 );
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
char *v0; // rax

g_console_line = 0;
g_console_column = 0;
func0();
v0 = &byte_221;
do
{
*v0 = 15;
v0 += 2;
}
while ( v0 != (char *)&unk_11C1 );
return 0LL;
}

original_code:
int32_t console_init(void)
{
uint32_t i = 0;
  g_console_column = g_console_line = 0;
  console_lastline = 0;
  console_clear();
  for (i = 1; i < 2 * 80 * 25; i+=2)
    g_console_video[i] = 0x7 | (0x0 << 4) | 0x8;
  return 0;
}

canonical_original_code:
int console_init()
{
unsigned int i = 0;
  g_console_column = g_console_line = 0;
  console_lastline = 0;
  console_clear();
  for (i = 1; i < 2 * 80 * 25; i+=2)
    g_console_video[i] = 0x7 | (0x0 << 4) | 0x8;
  return 0;
}

variable_types:
i
unsigned int


decompiled_code:
__int64 console_scrolldown()
{
memcpy(&g_console_video, &unk_2C0, 0xE60uLL);
memzero(&unk_1080, 160LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
memcpy(&g_console_video, &unk_2C0, 0xE60uLL);
memzero(&unk_1080, 160LL);
return 0LL;
}

original_code:
int32_t console_scrolldown(void)
{
  memcpy((void *)g_console_video,
  (void *)(&g_console_video[80 * 2]),
  80 * (25 - 2) * 2);
  memzero((void *)(&g_console_video[80
        * (25 - 2) * 2]),
   80 * 2);
  return 0;
}

canonical_original_code:
int console_scrolldown()
{
  memcpy((void *)g_console_video,
  (void *)(&g_console_video[80 * 2]),
  80 * (25 - 2) * 2);
  memzero((void *)(&g_console_video[80
        * (25 - 2) * 2]),
   80 * 2);
  return 0;
}


decompiled_code:
__int64 __fastcall console_printf(char *s, char a2)
{
unsigned int v4; // edi
__int64 result; // rax
int v6; // er8
unsigned int v7; // edx
char v8; // r9
char v9; // cl
int v10; // esi

if ( !s )
return 0xFFFFFFFFLL;
if ( (unsigned int)g_console_line > 0x17 )
{
console_scrolldown();
--g_console_line;
}
v4 = strlen(s);
result = 0LL;
if ( v4 )
{
v6 = g_console_line;
v7 = g_console_column;
v8 = 0;
do
{
v9 = s[(unsigned int)result];
if ( v9 == 9 )
{
v7 = (v7 & 0xFFFFFFF8) + 8;
}
else if ( v9 == 10 )
{
++v6;
v8 = 1;
v7 = 0;
}
else
{
v10 = 2 * (v7 + 80 * v6);
g_console_video[v10] = v9;
g_console_video[v10 + 1] = a2;
if ( ++v7 > 0x4F )
{
++v6;
v8 = 1;
v7 = 0;
}
}
LODWORD(result) = result + 1;
}
while ( v4 > (unsigned int)result && (unsigned int)result <= 0x4F );
g_console_column = v7;
if ( v8 )
g_console_line = v6;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s, char a2)
{
unsigned int v4; // edi
__int64 result; // rax
int v6; // er8
unsigned int v7; // edx
char v8; // r9
char v9; // cl
int v10; // esi

if ( !s )
return 0xFFFFFFFFLL;
if ( (unsigned int)g_console_line > 0x17 )
{
func2();
--g_console_line;
}
v4 = strlen(s);
result = 0LL;
if ( v4 )
{
v6 = g_console_line;
v7 = g_console_column;
v8 = 0;
do
{
v9 = s[(unsigned int)result];
if ( v9 == 9 )
{
v7 = (v7 & 0xFFFFFFF8) + 8;
}
else if ( v9 == 10 )
{
++v6;
v8 = 1;
v7 = 0;
}
else
{
v10 = 2 * (v7 + 80 * v6);
g_console_video[v10] = v9;
g_console_video[v10 + 1] = a2;
if ( ++v7 > 0x4F )
{
++v6;
v8 = 1;
v7 = 0;
}
}
LODWORD(result) = result + 1;
}
while ( v4 > (unsigned int)result && (unsigned int)result <= 0x4F );
g_console_column = v7;
if ( v8 )
g_console_line = v6;
result = 0LL;
}
return result;
}

original_code:
int32_t console_printf(char *str,
         char attribs)
{
uint32_t i = 0;
uint32_t length;
  if (str == 0)
    return -1;
  if (g_console_line >= 25 - 1)
    {
      console_scrolldown();
      g_console_line--;
    }
  length = strlen(str);
  if (length)
    {
      while ((i < length) && (i < 80))
 {
   switch (str[i])
     {
     case '\n':
       g_console_column = 0;
       g_console_line++;
       break;
     case '\t':
       g_console_column = (g_console_column / 8)
  * 8 + 8;
       break;
     default:
       g_console_video[((g_console_line * 80 + g_console_column) * 2)] = str[i];
       g_console_video[((g_console_line * 80 + g_console_column) * 2) + 1] = attribs;
       g_console_column++;
       if (g_console_column >= 80)
  {
    g_console_column = 0;
    g_console_line++;
  }
     }
   i++;
 }
    }
  return 0;
}

canonical_original_code:
int console_printf(char *str, char attribs)
{
unsigned int i = 0;
unsigned int length;
  if (str == 0)
    return -1;
  if (g_console_line >= 25 - 1)
    {
      console_scrolldown();
      g_console_line--;
    }
  length = strlen(str);
  if (length)
    {
      while ((i < length) && (i < 80))
 {
   switch (str[i])
     {
     case '\n':
       g_console_column = 0;
       g_console_line++;
       break;
     case '\t':
       g_console_column = (g_console_column / 8)
  * 8 + 8;
       break;
     default:
       g_console_video[((g_console_line * 80 + g_console_column) * 2)] = str[i];
       g_console_video[((g_console_line * 80 + g_console_column) * 2) + 1] = attribs;
       g_console_column++;
       if (g_console_column >= 80)
  {
    g_console_column = 0;
    g_console_line++;
  }
     }
   i++;
 }
    }
  return 0;
}

variable_types:
str
char *

variable_types: attribs char

variable_types:
length
unsigned int


decompiled_code:
__int64 __fastcall console_refresh(FILE *stream, __int64 off)
{
char v3[88]; // [rsp-58h] [rbp-58h] BYREF

if ( !stream )
return 4294967294LL;
if ( fseek(stream, (unsigned int)off, 0) )
return 0xFFFFFFFFLL;
memzero(v3, 80LL);
while ( fgets(v3, 80, stream) )
{
console_printf(v3, 7);
memzero(v3, 80LL);
}
return ftell(stream);
}

canonical_decompiled_code:
__int64 __fastcall func4(FILE *stream, __int64 off)
{
char v3[88]; // [rsp-58h] [rbp-58h] BYREF

if ( !stream )
return 4294967294LL;
if ( fseek(stream, (unsigned int)off, 0) )
return 0xFFFFFFFFLL;
memzero(v3, 80LL);
while ( fgets(v3, 80, stream) )
{
func3(v3, 7);
memzero(v3, 80LL);
}
return ftell(stream);
}

original_code:
int32_t console_refresh(FILE *stream,
   uint32_t lastline)
{
  char line[80];
  if (stream == 0)
    return -2;
  if (fseek(stream, lastline, 0) != 0)
    return -1;
  memzero(line, 80);
  while (0 != fgets(line, 80, stream))
    {
      console_printf(line, (0x0 << 4) | 0x7);
      memzero(line, 80);
    }
  return ftell(stream);
}

canonical_original_code:
int console_refresh(struct _s_file *stream, unsigned int lastline)
{
  char line[80];
  if (stream == 0)
    return -2;
  if (fseek(stream, lastline, 0) != 0)
    return -1;
  memzero(line, 80);
  while (0 != fgets(line, 80, stream))
    {
      console_printf(line, (0x0 << 4) | 0x7);
      memzero(line, 80);
    }
  return ftell(stream);
}

variable_types: struct _s_file { struct _s_buffer { unsigned char *data; unsigned int offset; unsigned int size; unsigned int mode; } buffer; unsigned int fileoffset; unsigned long long filesize; char filename[16]; int fileid; unsigned short filemode; } *

variable_types:
lastline
unsigned int

variable_types:
line
char [80]

user_defined_types: struct _s_file { struct _s_buffer buffer; unsigned int fileoffset; unsigned long long filesize; char filename[16]; int fileid; unsigned short filemode; }


decompiled_code:
void __fastcall write_char(char a1)
{
g_console_video[0] = a1;
byte_221 = -32;
}

canonical_decompiled_code:
void __fastcall func5(char a1)
{
g_console_video[0] = a1;
byte_221 = -32;
}

original_code:
void write_char(uint8_t c)
{
  g_console_video[0] = c;
  g_console_video[1] = (0x6 << 4) | 0x0 | (0x8 << 4);
}

canonical_original_code:
void write_char(unsigned char c)
{
  g_console_video[0] = c;
  g_console_video[1] = (0x6 << 4) | 0x0 | (0x8 << 4);
}

variable_types:
c
unsigned char

return_type: void


decompiled_code:
__int64 __fastcall pit_set_frequency(__int16 a1)
{
outb(0x40u, (unsigned __int8)a1);
outb(0x40u, HIBYTE(a1));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int16 a1)
{
outb(0x40u, (unsigned __int8)a1);
outb(0x40u, HIBYTE(a1));
return 0LL;
}

original_code:
int32_t pit_set_frequency(uint32_t freq)
{
  outb(0x40, (uint8_t)(freq & 0xff));
  outb(0x40, (uint8_t)(freq >> 8));
  return 0;
}

canonical_original_code:
int pit_set_frequency(unsigned int freq)
{
  outb(0x40, (unsigned char)(freq & 0xff));
  outb(0x40, (unsigned char)(freq >> 8));
  return 0;
}

variable_types:
freq
unsigned int


decompiled_code:
__int64 pit_init()
{
outb(0x43u, 0x36u);
pit_set_frequency(11931);
return 0LL;
}

original_code:
int32_t pit_init(void)
{
  outb(0x43, 0x36);
  pit_set_frequency(1193180 / 100);
  return 0;
}

canonical_original_code:
int pit_init()
{
  outb(0x43, 0x36);
  pit_set_frequency(1193180 / 100);
  return 0;
}


decompiled_code:
__int64 apic_io_init()
{
if ( (unsigned int)mmu_virt_page_reserve(4273995776LL, 538LL) )
{
printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
__halt();
}
apic_io_add_redentry(0x12u, 0x21u);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
if ( (unsigned int)mmu_virt_page_reserve(4273995776LL, 538LL) )
{
printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
__halt();
}
func1(0x12u, 0x21u);
return 0LL;
}

original_code:
int32_t apic_io_init(void)
{
  _apic_io_base = 0xfec00000;
  if (mmu_virt_page_reserve((vaddr_t) _apic_io_base,
       (1 << 1)
       | (1 << 4)
       | (1 << 3)
       | (1 << 9)))
    {
      printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
      while (1) __asm__ __volatile__ ("hlt");;;
      return -1;
    }
  _apic_io_add_redentry(18,
   33);
  return 0;
}

canonical_original_code:
int apic_io_init()
{
  _apic_io_base = 0xfec00000;
  if (mmu_virt_page_reserve((void *) _apic_io_base,
       (1 << 1)
       | (1 << 4)
       | (1 << 3)
       | (1 << 9)))
    {
      printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
      while (1) __asm__ __volatile__ ("hlt");;;
      return -1;
    }
  _apic_io_add_redentry(18,
   33);
  return 0;
}


fstrcat

original_code:
int32_t fstrcat(FILE *stream,
   const char *s)
{
uint32_t i = 0;
  if ((stream == 0) || (s == 0))
    return -2;
  while (i < strlen(s))
    {
      stream->buffer.data[stream->buffer.offset++] = s[i++];
      stream->fileoffset++;
      if ((stream->buffer.mode & (1 << 3))
   && (stream->buffer.offset >= stream->buffer.size))
 stream->fileoffset = stream->buffer.offset = 0;
    }
  return 0;
}

canonical_original_code:
int fstrcat(struct _s_file *stream, char *s)
{
unsigned int i = 0;
  if ((stream == 0) || (s == 0))
    return -2;
  while (i < strlen(s))
    {
      stream->buffer.data[stream->buffer.offset++] = s[i++];
      stream->fileoffset++;
      if ((stream->buffer.mode & (1 << 3))
   && (stream->buffer.offset >= stream->buffer.size))
 stream->fileoffset = stream->buffer.offset = 0;
    }
  return 0;
}

variable_types:
s
char *

user_defined_types: struct _s_buffer { unsigned char *data; unsigned int offset; unsigned int size; unsigned int mode; }


vfprintf

decompiled_code:
__int64 __fastcall vfprintf(__int64 a1, const char *a2, unsigned int *a3)
{
unsigned int v4; // er14
unsigned int i; // ebp
char v6; // al
unsigned int v7; // eax
unsigned int *v8; // rdx
unsigned int v9; // er15
unsigned int v10; // eax
unsigned int *v11; // rcx
unsigned int v13; // eax
unsigned int *v14; // rcx
unsigned int v15; // eax
unsigned int *v16; // rdx
unsigned int v17; // er15
__int64 j; // r15
__int64 v19; // rdx
unsigned int v20; // eax
char v21; // dl
__int64 v22; // rcx
unsigned int v23; // eax
char s[9]; // [rsp+7h] [rbp-C1h] BYREF
char src[184]; // [rsp+10h] [rbp-B8h] BYREF

s[8] = 0;
v4 = 0;
for ( i = 0; (unsigned int)strlen(a2) > i; ++i )
{
v21 = a2[i];
if ( v21 == 37 )
{
memzero(src, 128LL);
v6 = a2[++i];
if ( v6 == 114 )
{
v15 = *a3;
if ( *a3 > 0x2F )
{
v16 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v16 + 2;
}
else
{
v16 = (unsigned int *)(*((_QWORD *)a3 + 2) + v15);
*a3 = v15 + 8;
}
v17 = *v16;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
fstrcat((__int64 *)a1, "0x");
else
strcat(*(char **)a1, "0x");
itoa(src, v17, 16LL);
memset(s, 48, 8uLL);
for ( j = 0LL; (unsigned int)strlen(src) > (unsigned int)j; ++j )
s[(_DWORD)j + 8 - (unsigned int)strlen(src)] = src[j];
v4 += 10;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
fstrcat((__int64 *)a1, s);
else
strcat(*(char **)a1, s);
goto LABEL_48;
}
if ( v6 > 114 )
{
if ( v6 == 117 )
{
v13 = *a3;
if ( *a3 > 0x2F )
{
v14 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v14 + 2;
}
else
{
v14 = (unsigned int *)(*((_QWORD *)a3 + 2) + v13);
*a3 = v13 + 8;
}
v4 += itoa(src, *v14, 10LL);
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
goto LABEL_25;
}
else
{
if ( v6 != 120 )
return 0xFFFFFFFFLL;
v7 = *a3;
if ( *a3 > 0x2F )
{
v8 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v8 + 2;
}
else
{
v8 = (unsigned int *)(*((_QWORD *)a3 + 2) + v7);
*a3 = v7 + 8;
}
v9 = *v8;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
fstrcat((__int64 *)a1, "0x");
else
strcat(*(char **)a1, "0x");
v4 += itoa(src, v9, 16LL) + 2;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
{
LABEL_25:
fstrcat((__int64 *)a1, src);
goto LABEL_48;
}
}
goto LABEL_30;
}
if ( v6 != 37 )
{
if ( v6 != 100 )
return 0xFFFFFFFFLL;
v10 = *a3;
if ( *a3 > 0x2F )
{
v11 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v11 + 2;
}
else
{
v11 = (unsigned int *)(*((_QWORD *)a3 + 2) + v10);
*a3 = v10 + 8;
}
v4 += itoa(src, *v11, 10LL);
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
goto LABEL_25;
LABEL_30:
strcat(*(char **)a1, src);
goto LABEL_48;
}
v19 = *(_QWORD *)a1;
v20 = *(_DWORD *)(a1 + 8);
*(_DWORD *)(a1 + 8) = v20 + 1;
*(_BYTE *)(v19 + v20) = 37;
++*(_DWORD *)(a1 + 24);
}
else
{
v22 = *(_QWORD *)a1;
v23 = *(_DWORD *)(a1 + 8);
*(_DWORD *)(a1 + 8) = v23 + 1;
*(_BYTE *)(v22 + v23) = v21;
++*(_DWORD *)(a1 + 24);
}
LABEL_48:
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 && *(_DWORD *)(a1 + 8) >= *(_DWORD *)(a1 + 12) )
{
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 24) = 0;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, unsigned int *a3)
{
unsigned int v4; // er14
unsigned int i; // ebp
char v6; // al
unsigned int v7; // eax
unsigned int *v8; // rdx
unsigned int v9; // er15
unsigned int v10; // eax
unsigned int *v11; // rcx
unsigned int v13; // eax
unsigned int *v14; // rcx
unsigned int v15; // eax
unsigned int *v16; // rdx
unsigned int v17; // er15
__int64 j; // r15
__int64 v19; // rdx
unsigned int v20; // eax
char v21; // dl
__int64 v22; // rcx
unsigned int v23; // eax
char s[9]; // [rsp+7h] [rbp-C1h] BYREF
char src[184]; // [rsp+10h] [rbp-B8h] BYREF

s[8] = 0;
v4 = 0;
for ( i = 0; (unsigned int)strlen(a2) > i; ++i )
{
v21 = a2[i];
if ( v21 == 37 )
{
memzero(src, 128LL);
v6 = a2[++i];
if ( v6 == 114 )
{
v15 = *a3;
if ( *a3 > 0x2F )
{
v16 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v16 + 2;
}
else
{
v16 = (unsigned int *)(*((_QWORD *)a3 + 2) + v15);
*a3 = v15 + 8;
}
v17 = *v16;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
func0((__int64 *)a1, "0x");
else
strcat(*(char **)a1, "0x");
itoa(src, v17, 16LL);
memset(s, 48, 8uLL);
for ( j = 0LL; (unsigned int)strlen(src) > (unsigned int)j; ++j )
s[(_DWORD)j + 8 - (unsigned int)strlen(src)] = src[j];
v4 += 10;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
func0((__int64 *)a1, s);
else
strcat(*(char **)a1, s);
goto LABEL_48;
}
if ( v6 > 114 )
{
if ( v6 == 117 )
{
v13 = *a3;
if ( *a3 > 0x2F )
{
v14 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v14 + 2;
}
else
{
v14 = (unsigned int *)(*((_QWORD *)a3 + 2) + v13);
*a3 = v13 + 8;
}
v4 += itoa(src, *v14, 10LL);
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
goto LABEL_25;
}
else
{
if ( v6 != 120 )
return 0xFFFFFFFFLL;
v7 = *a3;
if ( *a3 > 0x2F )
{
v8 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v8 + 2;
}
else
{
v8 = (unsigned int *)(*((_QWORD *)a3 + 2) + v7);
*a3 = v7 + 8;
}
v9 = *v8;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
func0((__int64 *)a1, "0x");
else
strcat(*(char **)a1, "0x");
v4 += itoa(src, v9, 16LL) + 2;
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
{
LABEL_25:
func0((__int64 *)a1, src);
goto LABEL_48;
}
}
goto LABEL_30;
}
if ( v6 != 37 )
{
if ( v6 != 100 )
return 0xFFFFFFFFLL;
v10 = *a3;
if ( *a3 > 0x2F )
{
v11 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v11 + 2;
}
else
{
v11 = (unsigned int *)(*((_QWORD *)a3 + 2) + v10);
*a3 = v10 + 8;
}
v4 += itoa(src, *v11, 10LL);
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 )
goto LABEL_25;
LABEL_30:
strcat(*(char **)a1, src);
goto LABEL_48;
}
v19 = *(_QWORD *)a1;
v20 = *(_DWORD *)(a1 + 8);
*(_DWORD *)(a1 + 8) = v20 + 1;
*(_BYTE *)(v19 + v20) = 37;
++*(_DWORD *)(a1 + 24);
}
else
{
v22 = *(_QWORD *)a1;
v23 = *(_DWORD *)(a1 + 8);
*(_DWORD *)(a1 + 8) = v23 + 1;
*(_BYTE *)(v22 + v23) = v21;
++*(_DWORD *)(a1 + 24);
}
LABEL_48:
if ( (*(_BYTE *)(a1 + 16) & 8) != 0 && *(_DWORD *)(a1 + 8) >= *(_DWORD *)(a1 + 12) )
{
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 24) = 0;
}
}
return v4;
}

original_code:
int32_t vfprintf(FILE *stream,
    const char *format,
    __builtin_va_list arg)
{
 int32_t v = 0;
uint32_t i = 0;
uint32_t j = 0;
uint32_t k = 0;
  char buf[128];
  char reg[9];
  reg[8] = '\0';
  for (i = 0; i < strlen(format); i++)
    {
      if (format[i] != '%')
 {
   stream->buffer.data[stream->buffer.offset++] = format[i];
   stream->fileoffset++;
 }
      else
 {
   memzero(buf, 128);
   switch (format[++i])
     {
     case 'd':
       v = __builtin_va_arg(arg, int32_t);;
       j += itoa(buf, v, 10);
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, buf); else strcat(stream->buffer.data, buf);;
       break;
     case 'u':
       v = __builtin_va_arg(arg, uint32_t);;
       j += itoa(buf, (uint32_t)v, 10);
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, buf); else strcat(stream->buffer.data, buf);;
       break;
     case 'x':
       v = __builtin_va_arg(arg, uint32_t);;
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, "0x"); else strcat(stream->buffer.data, "0x");;
       j += (itoa(buf, (uint32_t)v, 16) + 2);
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, buf); else strcat(stream->buffer.data, buf);;
       break;
     case 'r':
       v = __builtin_va_arg(arg, uint32_t);;
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, "0x"); else strcat(stream->buffer.data, "0x");;
       itoa(buf, (uint32_t)v, 16);
       memset(reg, '0', 8);
       for (k = 0; k < strlen(buf); k++)
  reg[k + 8 - strlen(buf)] = buf[k];
       j+= 10;
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, reg); else strcat(stream->buffer.data, reg);;
       break;
     case '%':
       stream->buffer.data[stream->buffer.offset++] = '%';
       stream->fileoffset++;
       break;
     default:
       return -1;
     }
 }
      if ((stream->buffer.mode & (1 << 3))
   && (stream->buffer.offset >= stream->buffer.size))
 stream->fileoffset = stream->buffer.offset = 0;
    }
  return j;
}

canonical_original_code:
int vfprintf(struct _s_file *stream, char *format, __builtin_va_list arg)
{
 int v = 0;
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;
  char buf[128];
  char reg[9];
  reg[8] = '\0';
  for (i = 0; i < strlen(format); i++)
    {
      if (format[i] != '%')
 {
   stream->buffer.data[stream->buffer.offset++] = format[i];
   stream->fileoffset++;
 }
      else
 {
   memzero(buf, 128);
   switch (format[++i])
     {
     case 'd':
       v = __builtin_va_arg(arg, int32_t);;
       j += itoa(buf, v, 10);
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, buf); else strcat(stream->buffer.data, buf);;
       break;
     case 'u':
       v = __builtin_va_arg(arg, uint32_t);;
       j += itoa(buf, (unsigned int)v, 10);
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, buf); else strcat(stream->buffer.data, buf);;
       break;
     case 'x':
       v = __builtin_va_arg(arg, uint32_t);;
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, "0x"); else strcat(stream->buffer.data, "0x");;
       j += (itoa(buf, (unsigned int)v, 16) + 2);
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, buf); else strcat(stream->buffer.data, buf);;
       break;
     case 'r':
       v = __builtin_va_arg(arg, uint32_t);;
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, "0x"); else strcat(stream->buffer.data, "0x");;
       itoa(buf, (unsigned int)v, 16);
       memset(reg, '0', 8);
       for (k = 0; k < strlen(buf); k++)
  reg[k + 8 - strlen(buf)] = buf[k];
       j+= 10;
       if (stream->buffer.mode & (1 << 3)) fstrcat(stream, reg); else strcat(stream->buffer.data, reg);;
       break;
     case '%':
       stream->buffer.data[stream->buffer.offset++] = '%';
       stream->fileoffset++;
       break;
     default:
       return -1;
     }
 }
      if ((stream->buffer.mode & (1 << 3))
   && (stream->buffer.offset >= stream->buffer.size))
 stream->fileoffset = stream->buffer.offset = 0;
    }
  return j;
}

variable_types:
format
char *

variable_types: v int

variable_types:
j
unsigned int

variable_types:
k
unsigned int

variable_types:
buf
char [128]

variable_types:
reg
char [9]


fprintf

decompiled_code:
size_t fprintf(__int64 a1, const char *a2, ...)
{
size_t result; // rax
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(int *)(a1 + 56) < 0 )
return 0xFFFFFFFFLL;
result = strlen(a2);
if ( (_DWORD)result )
{
va_start(va, a2);
result = vfprintf(a1, a2, (unsigned int *)va);
*(_BYTE *)(*(_QWORD *)a1 + *(unsigned int *)(a1 + 8)) = 0;
}
return result;
}

canonical_decompiled_code:
size_t func2(__int64 a1, const char *a2, ...)
{
size_t result; // rax
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(int *)(a1 + 56) < 0 )
return 0xFFFFFFFFLL;
result = strlen(a2);
if ( (_DWORD)result )
{
va_start(va, a2);
result = func1(a1, a2, (unsigned int *)va);
*(_BYTE *)(*(_QWORD *)a1 + *(unsigned int *)(a1 + 8)) = 0;
}
return result;
}

original_code:
int32_t fprintf(FILE *stream,
   const char *format,
   ...)
{
 int32_t status = 0;
  __builtin_va_list arg;
  if (stream && (stream->fileid >= 0))
    {
      if (strlen(format))
 {
   __builtin_va_start(arg, format);;
   status = vfprintf(stream, format, arg);
   stream->buffer.data[stream->buffer.offset] = '\0';
   __builtin_va_end(arg);;
 }
    }
  else
    status = -1;
  return status;
}

canonical_original_code:
int fprintf(struct _s_file *stream, char *format, ...)
{
 int status = 0;
  __builtin_va_list arg;
  if (stream && (stream->fileid >= 0))
    {
      if (strlen(format))
 {
   __builtin_va_start(arg, format);;
   status = vfprintf(stream, format, arg);
   stream->buffer.data[stream->buffer.offset] = '\0';
   __builtin_va_end(arg);;
 }
    }
  else
    status = -1;
  return status;
}

variable_types: status int


vsprintf

decompiled_code:
__int64 __fastcall vsprintf(char *dest, char *s, unsigned int *a3)
{
unsigned int v4; // ebp
unsigned int i; // ebx
char v6; // al
char v7; // al
unsigned int v8; // eax
unsigned int *v9; // rdx
unsigned int v10; // er15
unsigned int v11; // eax
unsigned int *v12; // rcx
unsigned int v14; // eax
unsigned int v15; // eax
unsigned int *v16; // rdx
unsigned int v17; // er15
unsigned int j; // er15
char v19[184]; // [rsp+0h] [rbp-B8h] BYREF

v4 = 0;
for ( i = 0; (unsigned int)strlen(s) > i; ++i )
{
v6 = s[i];
if ( v6 != 37 )
{
dest[v4++] = v6;
continue;
}
memzero(v19, 128LL);
v7 = s[++i];
if ( v7 == 114 )
{
v15 = *a3;
if ( *a3 > 0x2F )
{
v16 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v16 + 2;
}
else
{
v16 = (unsigned int *)(*((_QWORD *)a3 + 2) + v15);
*a3 = v15 + 8;
}
v17 = *v16;
strcat(dest, "0x");
itoa(v19, v17, 16LL);
for ( j = 0; 8 - (unsigned int)strlen(v19) > j; ++j )
dest[v4 + 2 + j] = 48;
strcat(dest, v19);
v4 += 10;
}
else if ( v7 <= 114 )
{
if ( v7 != 37 )
{
if ( v7 != 100 )
return 0xFFFFFFFFLL;
v11 = *a3;
if ( *a3 > 0x2F )
{
v12 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v12 + 2;
}
else
{
v12 = (unsigned int *)(*((_QWORD *)a3 + 2) + v11);
*a3 = v11 + 8;
}
goto LABEL_17;
}
dest[v4++] = 37;
}
else
{
if ( v7 == 117 )
{
v14 = *a3;
if ( *a3 > 0x2F )
{
v12 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v12 + 2;
}
else
{
v12 = (unsigned int *)(*((_QWORD *)a3 + 2) + v14);
*a3 = v14 + 8;
}
LABEL_17:
v4 += itoa(v19, *v12, 10LL);
strcat(dest, v19);
continue;
}
if ( v7 != 120 )
return 0xFFFFFFFFLL;
v8 = *a3;
if ( *a3 > 0x2F )
{
v9 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v9 + 2;
}
else
{
v9 = (unsigned int *)(*((_QWORD *)a3 + 2) + v8);
*a3 = v8 + 8;
}
v10 = *v9;
strcat(dest, "0x");
v4 += itoa(v19, v10, 16LL) + 2;
strcat(dest, v19);
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *dest, char *s, unsigned int *a3)
{
unsigned int v4; // ebp
unsigned int i; // ebx
char v6; // al
char v7; // al
unsigned int v8; // eax
unsigned int *v9; // rdx
unsigned int v10; // er15
unsigned int v11; // eax
unsigned int *v12; // rcx
unsigned int v14; // eax
unsigned int v15; // eax
unsigned int *v16; // rdx
unsigned int v17; // er15
unsigned int j; // er15
char v19[184]; // [rsp+0h] [rbp-B8h] BYREF

v4 = 0;
for ( i = 0; (unsigned int)strlen(s) > i; ++i )
{
v6 = s[i];
if ( v6 != 37 )
{
dest[v4++] = v6;
continue;
}
memzero(v19, 128LL);
v7 = s[++i];
if ( v7 == 114 )
{
v15 = *a3;
if ( *a3 > 0x2F )
{
v16 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v16 + 2;
}
else
{
v16 = (unsigned int *)(*((_QWORD *)a3 + 2) + v15);
*a3 = v15 + 8;
}
v17 = *v16;
strcat(dest, "0x");
itoa(v19, v17, 16LL);
for ( j = 0; 8 - (unsigned int)strlen(v19) > j; ++j )
dest[v4 + 2 + j] = 48;
strcat(dest, v19);
v4 += 10;
}
else if ( v7 <= 114 )
{
if ( v7 != 37 )
{
if ( v7 != 100 )
return 0xFFFFFFFFLL;
v11 = *a3;
if ( *a3 > 0x2F )
{
v12 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v12 + 2;
}
else
{
v12 = (unsigned int *)(*((_QWORD *)a3 + 2) + v11);
*a3 = v11 + 8;
}
goto LABEL_17;
}
dest[v4++] = 37;
}
else
{
if ( v7 == 117 )
{
v14 = *a3;
if ( *a3 > 0x2F )
{
v12 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v12 + 2;
}
else
{
v12 = (unsigned int *)(*((_QWORD *)a3 + 2) + v14);
*a3 = v14 + 8;
}
LABEL_17:
v4 += itoa(v19, *v12, 10LL);
strcat(dest, v19);
continue;
}
if ( v7 != 120 )
return 0xFFFFFFFFLL;
v8 = *a3;
if ( *a3 > 0x2F )
{
v9 = (unsigned int *)*((_QWORD *)a3 + 1);
*((_QWORD *)a3 + 1) = v9 + 2;
}
else
{
v9 = (unsigned int *)(*((_QWORD *)a3 + 2) + v8);
*a3 = v8 + 8;
}
v10 = *v9;
strcat(dest, "0x");
v4 += itoa(v19, v10, 16LL) + 2;
strcat(dest, v19);
}
}
return v4;
}

original_code:
int32_t vsprintf(char *s,
    const char *format,
    __builtin_va_list arg)
{
 int32_t v;
uint32_t i;
uint32_t j = 0;
uint32_t k;
  char buffer[128];
  for (i = 0; i < strlen(format); i++)
    {
      if (format[i] != '%')
 s[j++] = format[i];
      else
 {
   memzero(buffer, 128);
   switch (format[++i])
     {
     case 'd':
       v = __builtin_va_arg(arg, int32_t);;
       j += itoa(buffer, v, 10);
       strcat(s, buffer);
       break;
     case 'u':
       v = __builtin_va_arg(arg, uint32_t);;
       j += itoa(buffer, (uint32_t)v, 10);
       strcat(s, buffer);
       break;
     case 'x':
       v = __builtin_va_arg(arg, uint32_t);;
       strcat(s, "0x");
       j += (itoa(buffer, (uint32_t)v, 16) + 2);
       strcat(s, buffer);
       break;
     case 'r':
       v = __builtin_va_arg(arg, uint32_t);;
       strcat(s, "0x");
       itoa(buffer, (uint32_t)v, 16);
       for (k = 0; k < 8 - strlen(buffer); k++)
  s[j + k + 2] = '0';
       strcat(s, buffer);
       j+= 10;
       break;
     case '%':
       s[j++] = '%';
       break;
     default:
       return -1;
     }
 }
    }
  return j;
}

canonical_original_code:
int vsprintf(char *s, char *format, __builtin_va_list arg)
{
 int v;
unsigned int i;
unsigned int j = 0;
unsigned int k;
  char buffer[128];
  for (i = 0; i < strlen(format); i++)
    {
      if (format[i] != '%')
 s[j++] = format[i];
      else
 {
   memzero(buffer, 128);
   switch (format[++i])
     {
     case 'd':
       v = __builtin_va_arg(arg, int32_t);;
       j += itoa(buffer, v, 10);
       strcat(s, buffer);
       break;
     case 'u':
       v = __builtin_va_arg(arg, uint32_t);;
       j += itoa(buffer, (unsigned int)v, 10);
       strcat(s, buffer);
       break;
     case 'x':
       v = __builtin_va_arg(arg, uint32_t);;
       strcat(s, "0x");
       j += (itoa(buffer, (unsigned int)v, 16) + 2);
       strcat(s, buffer);
       break;
     case 'r':
       v = __builtin_va_arg(arg, uint32_t);;
       strcat(s, "0x");
       itoa(buffer, (unsigned int)v, 16);
       for (k = 0; k < 8 - strlen(buffer); k++)
  s[j + k + 2] = '0';
       strcat(s, buffer);
       j+= 10;
       break;
     case '%':
       s[j++] = '%';
       break;
     default:
       return -1;
     }
 }
    }
  return j;
}

variable_types:
i
unsigned int

variable_types:
buffer
char [128]


sprintf

decompiled_code:
__int64 sprintf(char *dest, char *s, ...)
{
__int64 result; // rax
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

if ( !dest )
return 0xFFFFFFFFLL;
result = strlen(s);
if ( (_DWORD)result )
{
va_start(va, s);
result = vsprintf(dest, s, (unsigned int *)va);
}
return result;
}

canonical_decompiled_code:
__int64 func4(char *dest, char *s, ...)
{
__int64 result; // rax
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

if ( !dest )
return 0xFFFFFFFFLL;
result = strlen(s);
if ( (_DWORD)result )
{
va_start(va, s);
result = func3(dest, s, (unsigned int *)va);
}
return result;
}

original_code:
int32_t sprintf(char *s,
   const char *format,
   ...)
{
  int32_t status = 0;
  __builtin_va_list arg;
  if (s)
    {
      if (strlen(format))
 {
   __builtin_va_start(arg, format);;
   status = vsprintf(s, format, arg);
   __builtin_va_end(arg);;
 }
    }
  else
    status = -1;
  return status;
}

canonical_original_code:
int sprintf(char *s, char *format, ...)
{
  int status = 0;
  __builtin_va_list arg;
  if (s)
    {
      if (strlen(format))
 {
   __builtin_va_start(arg, format);;
   status = vsprintf(s, format, arg);
   __builtin_va_end(arg);;
 }
    }
  else
    status = -1;
  return status;
}


fseek

decompiled_code:
__int64 __fastcall fseek(__int64 a1, int a2, int a3)
{
if ( !a1 )
return 4294967294LL;
if ( a3 )
return (unsigned int)-((unsigned int)(a3 - 1) > 1);
*(_DWORD *)(a1 + 24) = a2;
*(_DWORD *)(a1 + 8) = a2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, int a3)
{
if ( !a1 )
return 4294967294LL;
if ( a3 )
return (unsigned int)-((unsigned int)(a3 - 1) > 1);
*(_DWORD *)(a1 + 24) = a2;
*(_DWORD *)(a1 + 8) = a2;
return 0LL;
}

original_code:
int32_t fseek(FILE *stream,
 int64_t offset,
 int32_t whence)
{
  if (stream == 0)
    return -2;
  switch (whence)
    {
    case 0:
      stream->fileoffset = offset;
      stream->buffer.offset = offset;
      break;
    case 1:
      break;
    case 2:
      break;
    default:
      return -1;
    }
  return 0;
}

canonical_original_code:
int fseek(struct _s_file *stream, long long offset, int whence)
{
  if (stream == 0)
    return -2;
  switch (whence)
    {
    case 0:
      stream->fileoffset = offset;
      stream->buffer.offset = offset;
      break;
    case 1:
      break;
    case 2:
      break;
    default:
      return -1;
    }
  return 0;
}

variable_types:
offset
long long

variable_types: whence int


ftell

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

if ( a1 )
result = *(unsigned int *)(a1 + 24);
else
result = -2LL;
return result;
}

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

if ( a1 )
result = *(unsigned int *)(a1 + 24);
else
result = -2LL;
return result;
}

original_code:
int64_t ftell(FILE *stream)
{
  if (stream == 0)
    return -2;
  return stream->fileoffset;
}

canonical_original_code:
long long ftell(struct _s_file *stream)
{
  if (stream == 0)
    return -2;
  return stream->fileoffset;
}

variable_types: struct _s_file { struct _s_buffer { unsigned char *data; unsigned int offset; unsigned int size; unsigned int mode; } buffer; unsigned int fileoffset; unsigned long long filesize; char filename[16]; int fileid; unsigned short filemode; } *

return_type: long long


setvbuf

original_code:
int32_t setvbuf(FILE *stream,
   char *buf,
 int32_t mode,
   unsigned long size)
{
  if (stream == 0)
    return -2;
  if (buf == 0)
    {
      stream->buffer.data = 0;
      stream->buffer.size = 0;
      stream->buffer.mode = (1 << 2);
    }
  else
    {
      stream->buffer.data = buf;
      stream->buffer.size = size;
      stream->buffer.mode = mode;
    }
  return 0;
}

canonical_original_code:
int setvbuf(struct _s_file *stream, char *buf, int mode, unsigned long size)
{
  if (stream == 0)
    return -2;
  if (buf == 0)
    {
      stream->buffer.data = 0;
      stream->buffer.size = 0;
      stream->buffer.mode = (1 << 2);
    }
  else
    {
      stream->buffer.data = buf;
      stream->buffer.size = size;
      stream->buffer.mode = mode;
    }
  return 0;
}

variable_types:
buf
char *

variable_types: mode int

variable_types:
size
unsigned long


fopen

decompiled_code:
char *__fastcall fopen(char *src, char *s)
{
unsigned int i; // ebx
char v3; // al

if ( !src )
return 0LL;
if ( !s )
return 0LL;
if ( *(_DWORD *)&stdio_filedes[56] != -1 )
return 0LL;
strcpy((char *)(unsigned int)((_DWORD)stdio_filedes + 40), src);
*(_DWORD *)&stdio_filedes[56] = 0;
*(_DWORD *)&stdio_filedes[24] = 0;
*(_QWORD *)&stdio_filedes[32] = 512LL;
setvbuf((__int64)stdio_filedes, (__int64)&stdio_filebuf, 1, 512);
*(_DWORD *)&stdio_filedes[8] = 0;
memzero(*(_QWORD *)stdio_filedes, 512LL);
for ( i = 0; (unsigned int)strlen(s) > i; ++i )
{
v3 = s[i];
if ( v3 == 114 )
{
*(_WORD *)&stdio_filedes[60] |= 1u;
}
else if ( v3 > 114 )
{
if ( v3 != 119 )
return 0LL;
*(_WORD *)&stdio_filedes[60] |= 2u;
}
else if ( v3 == 99 )
{
*(_DWORD *)&stdio_filedes[16] = 8;
}
else
{
if ( v3 > 99 )
return 0LL;
if ( v3 != 43 && v3 != 97 )
return 0LL;
}
}
return stdio_filedes;
}

canonical_decompiled_code:
char *__fastcall func10(char *src, char *s)
{
unsigned int i; // ebx
char v3; // al

if ( !src )
return 0LL;
if ( !s )
return 0LL;
if ( *(_DWORD *)&stdio_filedes[56] != -1 )
return 0LL;
strcpy((char *)(unsigned int)((_DWORD)stdio_filedes + 40), src);
*(_DWORD *)&stdio_filedes[56] = 0;
*(_DWORD *)&stdio_filedes[24] = 0;
*(_QWORD *)&stdio_filedes[32] = 512LL;
func8((__int64)stdio_filedes, (__int64)&stdio_filebuf, 1, 512);
*(_DWORD *)&stdio_filedes[8] = 0;
memzero(*(_QWORD *)stdio_filedes, 512LL);
for ( i = 0; (unsigned int)strlen(s) > i; ++i )
{
v3 = s[i];
if ( v3 == 114 )
{
*(_WORD *)&stdio_filedes[60] |= 1u;
}
else if ( v3 > 114 )
{
if ( v3 != 119 )
return 0LL;
*(_WORD *)&stdio_filedes[60] |= 2u;
}
else if ( v3 == 99 )
{
*(_DWORD *)&stdio_filedes[16] = 8;
}
else
{
if ( v3 > 99 )
return 0LL;
if ( v3 != 43 && v3 != 97 )
return 0LL;
}
}
return stdio_filedes;
}

original_code:
FILE *fopen(const char *filename,
         const char *mode)
{
uint32_t i = 0;
uint32_t j = 0;
  if ((filename == 0) || (mode == 0))
    return 0;
  while ((i < 1) && (stdio_filedes[i].fileid != -1))
    i++;
  if (i == 1)
    return 0;
  strcpy(stdio_filedes[i].filename, filename);
  stdio_filedes[i].fileid = i;
  stdio_filedes[i].fileoffset = 0;
  stdio_filedes[i].filesize = 512;
  setvbuf(&(stdio_filedes[i]),
   stdio_filebuf[i],
   (1 << 0),
   512);
  stdio_filedes[i].buffer.offset = 0;
  memzero(stdio_filedes[i].buffer.data, 512);
  j = 0;
  while (j < strlen(mode))
    {
      switch (mode[j])
 {
 case 'r':
   stdio_filedes[i].filemode |= (1 << 0);
   break;
 case 'w':
   stdio_filedes[i].filemode |= (1 << 1);
   break;
 case '+':
   break;
 case 'a':
   break;
 case 'c':
   stdio_filedes[i].buffer.mode = (1 << 3);
   break;
 default:
   return 0;
 }
      j++;
    }
  return &(stdio_filedes[i]);
}

canonical_original_code:
struct _s_file *fopen(char *filename, char *mode)
{
unsigned int i = 0;
unsigned int j = 0;
  if ((filename == 0) || (mode == 0))
    return 0;
  while ((i < 1) && (stdio_filedes[i].fileid != -1))
    i++;
  if (i == 1)
    return 0;
  strcpy(stdio_filedes[i].filename, filename);
  stdio_filedes[i].fileid = i;
  stdio_filedes[i].fileoffset = 0;
  stdio_filedes[i].filesize = 512;
  setvbuf(&(stdio_filedes[i]),
   stdio_filebuf[i],
   (1 << 0),
   512);
  stdio_filedes[i].buffer.offset = 0;
  memzero(stdio_filedes[i].buffer.data, 512);
  j = 0;
  while (j < strlen(mode))
    {
      switch (mode[j])
 {
 case 'r':
   stdio_filedes[i].filemode |= (1 << 0);
   break;
 case 'w':
   stdio_filedes[i].filemode |= (1 << 1);
   break;
 case '+':
   break;
 case 'a':
   break;
 case 'c':
   stdio_filedes[i].buffer.mode = (1 << 3);
   break;
 default:
   return 0;
 }
      j++;
    }
  return &(stdio_filedes[i]);
}

variable_types:
filename
char *

variable_types:
mode
char *

return_type: struct _s_file { struct _s_buffer { unsigned char *data; unsigned int offset; unsigned int size; unsigned int mode; } buffer; unsigned int fileoffset; unsigned long long filesize; char filename[16]; int fileid; unsigned short filemode; } *


printf

decompiled_code:
__int64 printf(char *s, ...)
{
char v2[136]; // [rsp+0h] [rbp-158h] BYREF
gcc_va_list va; // [rsp+88h] [rbp-D0h] BYREF

memzero(v2, 128LL);
if ( !s )
return 0xFFFFFFFFLL;
if ( (unsigned int)strlen(s) )
{
va_start(va, s);
vsprintf(v2, s, (unsigned int *)va);
}
return console_printf(v2, 7LL);
}

canonical_decompiled_code:
__int64 func11(char *s, ...)
{
char v2[136]; // [rsp+0h] [rbp-158h] BYREF
gcc_va_list va; // [rsp+88h] [rbp-D0h] BYREF

memzero(v2, 128LL);
if ( !s )
return 0xFFFFFFFFLL;
if ( (unsigned int)strlen(s) )
{
va_start(va, s);
func3(v2, s, (unsigned int *)va);
}
return console_printf(v2, 7LL);
}

original_code:
int32_t printf(const char *format,
         ...)
{
 int32_t status = 0;
  __builtin_va_list arg;
  char buffer[128];
  memzero(buffer, 128);
  if (format)
    {
      if (strlen(format))
 {
   __builtin_va_start(arg, format);;
   status = vsprintf(buffer, format, arg);
   __builtin_va_end(arg);;
 }
    }
  else
    return -1;
  status = console_printf(buffer, 0x7 | (0x0 << 4) );
  return status;
}

canonical_original_code:
int printf(char *format, ...)
{
 int status = 0;
  __builtin_va_list arg;
  char buffer[128];
  memzero(buffer, 128);
  if (format)
    {
      if (strlen(format))
 {
   __builtin_va_start(arg, format);;
   status = vsprintf(buffer, format, arg);
   __builtin_va_end(arg);;
 }
    }
  else
    return -1;
  status = console_printf(buffer, 0x7 | (0x0 << 4) );
  return status;
}

variable_types:
format
char *


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

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
result = *a1;
if ( *a1 )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 16);
}
while ( result );
*(_QWORD *)(v3 + 16) = a2;
*(_QWORD *)(a2 + 8) = v3;
*(_QWORD *)(a2 + 16) = 0LL;
}
else
{
*a1 = a2;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
result = 0LL;
}
return result;
}

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

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
result = *a1;
if ( *a1 )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 16);
}
while ( result );
*(_QWORD *)(v3 + 16) = a2;
*(_QWORD *)(a2 + 8) = v3;
*(_QWORD *)(a2 + 16) = 0LL;
}
else
{
*a1 = a2;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
result = 0LL;
}
return result;
}

original_code:
int32_t list_insert(list_s **list,
       list_s *new)
{
  list_s *iterator;
  if ((list == 0) || (new == 0))
    {
      return -1;
    }
  if (*list != 0)
    {
      iterator = *list;
      while (iterator->next)
 iterator = iterator->next;
      iterator->next = new;
      new->prev = iterator;
      new->next = 0;
    }
  else
    {
      *list = new;
      new->prev = 0;
      new->next = 0;
    }
  return 0;
}

canonical_original_code:
int list_insert(struct _list_s **list, struct _list_s *new)
{
  struct _list_s *iterator;
  if ((list == 0) || (new == 0))
    {
      return -1;
    }
  if (*list != 0)
    {
      iterator = *list;
      while (iterator->next)
 iterator = iterator->next;
      iterator->next = new;
      new->prev = iterator;
      new->next = 0;
    }
  else
    {
      *list = new;
      new->prev = 0;
      new->next = 0;
    }
  return 0;
}

variable_types: struct _list_s { void *e; struct _list_s *prev; struct _list_s *next; } **

variable_types:
new
struct _list_s *


original_code:
int32_t list_remove(list_s **list,
       list_s *rem)
{
  if ((list == 0) || (*list == 0) || (rem == 0))
    {
      return -1;
    }
  if (rem->prev == 0)
    {
      if (rem->next == 0)
 {
   *list = 0;
 }
      else
 {
   rem->next->prev = 0;
   *list = rem->next;
 }
    }
  else
    {
      if (rem->next == 0)
 {
   rem->prev->next = 0;
 }
      else
 {
   rem->prev->next = rem->next;
   rem->next->prev = rem->prev;
 }
    }
  rem->prev = 0;
  rem->next = 0;
  return 0;
}

canonical_original_code:
int list_remove(struct _list_s **list, struct _list_s *rem)
{
  if ((list == 0) || (*list == 0) || (rem == 0))
    {
      return -1;
    }
  if (rem->prev == 0)
    {
      if (rem->next == 0)
 {
   *list = 0;
 }
      else
 {
   rem->next->prev = 0;
   *list = rem->next;
 }
    }
  else
    {
      if (rem->next == 0)
 {
   rem->prev->next = 0;
 }
      else
 {
   rem->prev->next = rem->next;
   rem->next->prev = rem->prev;
 }
    }
  rem->prev = 0;
  rem->next = 0;
  return 0;
}

variable_types:
rem
struct _list_s *

user_defined_types: struct _list_s { void *e; struct _list_s *prev; struct _list_s *next; }


decompiled_code:
void __noreturn vmx_supported()
{
_RAX = 1LL;
__asm { cpuid }
printf(
"< CPUID >\n\teax: %x\n\tebx: %x\n\tecx: %x\n\tedx: %x\n",
(unsigned int)_RAX,
(unsigned int)_RBX,
(unsigned int)_RCX,
(unsigned int)_RDX);
printf("VMX-operations supported:\t");
printf("[NO]\n");
printf("SYSENTER and SYSEXIT supported:\t");
printf("[NO]\n");
_RAX = 1LL;
__asm { cpuid }
printf("PAE-ext supported:\t\t");
printf("[NO]\n");
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func0()
{
_RAX = 1LL;
__asm { cpuid }
printf(
"< CPUID >\n\teax: %x\n\tebx: %x\n\tecx: %x\n\tedx: %x\n",
(unsigned int)_RAX,
(unsigned int)_RBX,
(unsigned int)_RCX,
(unsigned int)_RDX);
printf("VMX-operations supported:\t");
printf("[NO]\n");
printf("SYSENTER and SYSEXIT supported:\t");
printf("[NO]\n");
_RAX = 1LL;
__asm { cpuid }
printf("PAE-ext supported:\t\t");
printf("[NO]\n");
while ( 1 )
;
}

original_code:
void vmx_supported(void)
{
  uint32_t eax, ebx, ecx, edx;
  uint32_t value = 1;
  __asm__ __volatile__ ("	cpuid			\n\t" "	movl	%%eax, %0	\n\t" "	movl	%%ebx, %1	\n\t" "	movl	%%ecx, %2	\n\t" "	movl	%%edx, %3" : "=m" (*&eax), "=m" (*&ebx), "=m" (*&ecx), "=m" (*&edx) : "a" (value) : "%ebx", "%ecx", "%edx" );;
  printf("< CPUID >\n\teax: %x\n\tebx: %x\n\tecx: %x\n\tedx: %x\n",
  eax, ebx, ecx, edx);
  printf("VMX-operations supported:\t");
  if ((ecx & (1 << 5)) == 1)
    {
      printf("[OK]\n");
    }
  else
    {
      printf("[NO]\n");
    }
  printf("SYSENTER and SYSEXIT supported:\t");
  if ((edx & (1 << 11)) == 1)
    {
      printf("[OK]\n");
    }
  else
    {
      printf("[NO]\n");
    }
  value = 1;
  __asm__ __volatile__ ("	cpuid			\n\t" "	movl	%%eax, %0	\n\t" "	movl	%%ebx, %1	\n\t" "	movl	%%ecx, %2	\n\t" "	movl	%%edx, %3" : "=m" (*&eax), "=m" (*&ebx), "=m" (*&ecx), "=m" (*&edx) : "a" (value) : "%ebx", "%ecx", "%edx" );;
  printf("PAE-ext supported:\t\t");
  if ((edx & (1 << 6)) == 1)
    {
      printf("[OK]\n");
    }
  else
    {
      printf("[NO]\n");
    }
  while (1)
    ;
}

canonical_original_code:
void vmx_supported()
{
  unsigned int eax; unsigned int ebx; unsigned int ecx; unsigned int edx;
  unsigned int value = 1;
  __asm__ __volatile__ ("	cpuid			\n\t" "	movl	%%eax, %0	\n\t" "	movl	%%ebx, %1	\n\t" "	movl	%%ecx, %2	\n\t" "	movl	%%edx, %3" : "=m" (*&eax), "=m" (*&ebx), "=m" (*&ecx), "=m" (*&edx) : "a" (value) : "%ebx", "%ecx", "%edx" );;
  printf("< CPUID >\n\teax: %x\n\tebx: %x\n\tecx: %x\n\tedx: %x\n",
  eax, ebx, ecx, edx);
  printf("VMX-operations supported:\t");
  if ((ecx & (1 << 5)) == 1)
    {
      printf("[OK]\n");
    }
  else
    {
      printf("[NO]\n");
    }
  printf("SYSENTER and SYSEXIT supported:\t");
  if ((edx & (1 << 11)) == 1)
    {
      printf("[OK]\n");
    }
  else
    {
      printf("[NO]\n");
    }
  value = 1;
  __asm__ __volatile__ ("	cpuid			\n\t" "	movl	%%eax, %0	\n\t" "	movl	%%ebx, %1	\n\t" "	movl	%%ecx, %2	\n\t" "	movl	%%edx, %3" : "=m" (*&eax), "=m" (*&ebx), "=m" (*&ecx), "=m" (*&edx) : "a" (value) : "%ebx", "%ecx", "%edx" );;
  printf("PAE-ext supported:\t\t");
  if ((edx & (1 << 6)) == 1)
    {
      printf("[OK]\n");
    }
  else
    {
      printf("[NO]\n");
    }
  while (1)
    ;
}

variable_types:
eax
unsigned int

variable_types:
ebx
unsigned int

variable_types:
ecx
unsigned int

variable_types:
edx
unsigned int

variable_types:
value
unsigned int


decompiled_code:
int test_malloc()
{
unsigned int *v0; // rbx
unsigned int *v1; // r12
unsigned int *v2; // rbp
int v3; // eax
unsigned int *v4; // rax
unsigned int *v5; // rbp

v0 = (unsigned int *)malloc(4uLL);
*v0 = -559038737;
v1 = (unsigned int *)malloc(0x1060uLL);
*v1 = -86066498;
v2 = (unsigned int *)malloc(4uLL);
*v2 = -1414673666;
printf("malloc() test\n");
printf("\tpointer @ =\t%x\n", (unsigned int)v0);
printf("\tpointer val =\t%x\n", *v0);
printf("\tpointer @ =\t%x\n", (unsigned int)v1);
printf("\tpointer val =\t%x\n", *v1);
printf("\tpointer @ =\t%x\n", (unsigned int)v2);
printf("\tpointer val =\t%x\n", *v2);
printf("free() test\n");
free(v0);
if ( v3 )
{
printf("free() FAILED...\n");
__halt();
}
v4 = (unsigned int *)malloc(4uLL);
v5 = v4;
*v4 = -17960947;
if ( v0 != v4 )
{
printf("A free block is not reused...\n");
__halt();
}
printf("\tpointer @ =\t%x\n", (unsigned int)v4);
return printf("\tpointer val =\t%x\n", *v5);
}

canonical_decompiled_code:
int func1()
{
unsigned int *v0; // rbx
unsigned int *v1; // r12
unsigned int *v2; // rbp
int v3; // eax
unsigned int *v4; // rax
unsigned int *v5; // rbp

v0 = (unsigned int *)malloc(4uLL);
*v0 = -559038737;
v1 = (unsigned int *)malloc(0x1060uLL);
*v1 = -86066498;
v2 = (unsigned int *)malloc(4uLL);
*v2 = -1414673666;
printf("malloc() test\n");
printf("\tpointer @ =\t%x\n", (unsigned int)v0);
printf("\tpointer val =\t%x\n", *v0);
printf("\tpointer @ =\t%x\n", (unsigned int)v1);
printf("\tpointer val =\t%x\n", *v1);
printf("\tpointer @ =\t%x\n", (unsigned int)v2);
printf("\tpointer val =\t%x\n", *v2);
printf("free() test\n");
free(v0);
if ( v3 )
{
printf("free() FAILED...\n");
__halt();
}
v4 = (unsigned int *)malloc(4uLL);
v5 = v4;
*v4 = -17960947;
if ( v0 != v4 )
{
printf("A free block is not reused...\n");
__halt();
}
printf("\tpointer @ =\t%x\n", (unsigned int)v4);
return printf("\tpointer val =\t%x\n", *v5);
}

original_code:
void test_malloc()
{
  uint32_t *u, *v, *w, *x;
  u = (uint32_t *) malloc(sizeof (uint32_t));
  *u = 0xdeadbeef;
  v = (uint32_t *) malloc(1048 * sizeof (uint32_t));
  *v = 0xfadebabe;
  w = (uint32_t *) malloc(sizeof (uint32_t));
  *w = 0xabadcafe;
  printf("malloc() test\n");
  printf("\tpointer @ =\t%x\n", (uint32_t) u);
  printf("\tpointer val =\t%x\n", (uint32_t) *u);
  printf("\tpointer @ =\t%x\n", (uint32_t) v);
  printf("\tpointer val =\t%x\n", (uint32_t) *v);
  printf("\tpointer @ =\t%x\n", (uint32_t) w);
  printf("\tpointer val =\t%x\n", (uint32_t) *w);
  printf("free() test\n");
  if (free(u) != 0)
    {
      printf("free() FAILED...\n");
      while (1)
 __asm__ __volatile__ ("hlt");;
    }
  x = (uint32_t *) malloc(sizeof (uint32_t));
  *x = 0xfeedf00d;
  if (x != u)
    {
      printf("A free block is not reused...\n");
      while (1)
 __asm__ __volatile__ ("hlt");;
    }
  printf("\tpointer @ =\t%x\n", (uint32_t) x);
  printf("\tpointer val =\t%x\n", (uint32_t) *x);
}

canonical_original_code:
void test_malloc()
{
  unsigned int *u; unsigned int *v; unsigned int *w; unsigned int *x;
  u = (unsigned int *) malloc(sizeof (unsigned int));
  *u = 0xdeadbeef;
  v = (unsigned int *) malloc(1048 * sizeof (unsigned int));
  *v = 0xfadebabe;
  w = (unsigned int *) malloc(sizeof (unsigned int));
  *w = 0xabadcafe;
  printf("malloc() test\n");
  printf("\tpointer @ =\t%x\n", (unsigned int) u);
  printf("\tpointer val =\t%x\n", (unsigned int) *u);
  printf("\tpointer @ =\t%x\n", (unsigned int) v);
  printf("\tpointer val =\t%x\n", (unsigned int) *v);
  printf("\tpointer @ =\t%x\n", (unsigned int) w);
  printf("\tpointer val =\t%x\n", (unsigned int) *w);
  printf("free() test\n");
  if (free(u) != 0)
    {
      printf("free() FAILED...\n");
      while (1)
 __asm__ __volatile__ ("hlt");;
    }
  x = (unsigned int *) malloc(sizeof (unsigned int));
  *x = 0xfeedf00d;
  if (x != u)
    {
      printf("A free block is not reused...\n");
      while (1)
 __asm__ __volatile__ ("hlt");;
    }
  printf("\tpointer @ =\t%x\n", (unsigned int) x);
  printf("\tpointer val =\t%x\n", (unsigned int) *x);
}

variable_types:
u
unsigned int *

variable_types:
v
unsigned int *

variable_types:
w
unsigned int *

variable_types:
x
unsigned int *


decompiled_code:
__int64 ex_init()
{
isr_stage2_bind((unsigned int)ex_divide_error, 0LL);
isr_stage2_bind((unsigned int)ex_double_fault, 8LL);
isr_stage2_bind((unsigned int)ex_general_protection, 13LL);
isr_stage2_bind((unsigned int)ex_page_fault, 14LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
isr_stage2_bind((unsigned int)ex_divide_error, 0LL);
isr_stage2_bind((unsigned int)ex_double_fault, 8LL);
isr_stage2_bind((unsigned int)ex_general_protection, 13LL);
isr_stage2_bind((unsigned int)ex_page_fault, 14LL);
return 0LL;
}

original_code:
int32_t ex_init(void)
{
  isr_stage2_bind((uint32_t) _ex_divide_error, 0);
  isr_stage2_bind((uint32_t) _ex_double_fault, 8);
  isr_stage2_bind((uint32_t) _ex_general_protection, 13);
  isr_stage2_bind((uint32_t) _ex_page_fault, 14);
  return 0;
}

canonical_original_code:
int ex_init()
{
  isr_stage2_bind((unsigned int) _ex_divide_error, 0);
  isr_stage2_bind((unsigned int) _ex_double_fault, 8);
  isr_stage2_bind((unsigned int) _ex_general_protection, 13);
  isr_stage2_bind((unsigned int) _ex_page_fault, 14);
  return 0;
}


decompiled_code:
__int64 int_init()
{
isr_stage2_bind((unsigned int)int_apic_local_timer, 32LL);
isr_stage2_bind((unsigned int)int_apic_io_timer, 34LL);
isr_stage2_bind((unsigned int)int_apic_io_timer2, 35LL);
isr_stage2_bind((unsigned int)int_apic_io_keyboard, 33LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
isr_stage2_bind((unsigned int)int_apic_local_timer, 32LL);
isr_stage2_bind((unsigned int)int_apic_io_timer, 34LL);
isr_stage2_bind((unsigned int)int_apic_io_timer2, 35LL);
isr_stage2_bind((unsigned int)int_apic_io_keyboard, 33LL);
return 0LL;
}

original_code:
int32_t int_init(void)
{
  isr_stage2_bind((uint32_t) _int_apic_local_timer, 32);
  isr_stage2_bind((uint32_t) _int_apic_io_timer, 34);
  isr_stage2_bind((uint32_t) _int_apic_io_timer2, 35);
  isr_stage2_bind((uint32_t) _int_apic_io_keyboard, 33);
  return 0;
}

canonical_original_code:
int int_init()
{
  isr_stage2_bind((unsigned int) _int_apic_local_timer, 32);
  isr_stage2_bind((unsigned int) _int_apic_io_timer, 34);
  isr_stage2_bind((unsigned int) _int_apic_io_timer2, 35);
  isr_stage2_bind((unsigned int) _int_apic_io_keyboard, 33);
  return 0;
}


decompiled_code:
__int64 video_init()
{
unsigned __int16 v0; // ax
__int64 i; // rbx
unsigned __int16 v2; // ax
unsigned __int16 v3; // ax
unsigned __int16 v4; // ax

outw(0x1CEu, 0);
v0 = inw(0x1CFu);
printf("BGA version: %x\n", v0);
for ( i = 3758096384LL; i != 3761242112LL; i += 4096LL )
{
printf("Page reserve... %x\n", (unsigned int)i);
if ( (unsigned int)mmu_virt_page_reserve(i, 538LL) )
{
printf("Cant reserve video RAM pages...\n");
__halt();
}
}
outw(0x1CEu, 4u);
outw(0x1CFu, 0);
outw(0x1CEu, 1u);
outw(0x1CFu, 0x400u);
outw(0x1CEu, 2u);
outw(0x1CFu, 0x300u);
outw(0x1CEu, 3u);
outw(0x1CFu, 0x20u);
outw(0x1CEu, 1u);
v2 = inw(0x1CFu);
printf("Xres: %d\n", v2);
outw(0x1CEu, 2u);
v3 = inw(0x1CFu);
printf("Yres: %d\n", v3);
outw(0x1CEu, 4u);
v4 = inw(0x1CFu);
printf("Enable: %d\n", v4);
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned __int16 v0; // ax
__int64 i; // rbx
unsigned __int16 v2; // ax
unsigned __int16 v3; // ax
unsigned __int16 v4; // ax

outw(0x1CEu, 0);
v0 = inw(0x1CFu);
printf("BGA version: %x\n", v0);
for ( i = 3758096384LL; i != 3761242112LL; i += 4096LL )
{
printf("Page reserve... %x\n", (unsigned int)i);
if ( (unsigned int)mmu_virt_page_reserve(i, 538LL) )
{
printf("Cant reserve video RAM pages...\n");
__halt();
}
}
outw(0x1CEu, 4u);
outw(0x1CFu, 0);
outw(0x1CEu, 1u);
outw(0x1CFu, 0x400u);
outw(0x1CEu, 2u);
outw(0x1CFu, 0x300u);
outw(0x1CEu, 3u);
outw(0x1CFu, 0x20u);
outw(0x1CEu, 1u);
v2 = inw(0x1CFu);
printf("Xres: %d\n", v2);
outw(0x1CEu, 2u);
v3 = inw(0x1CFu);
printf("Yres: %d\n", v3);
outw(0x1CEu, 4u);
v4 = inw(0x1CFu);
printf("Enable: %d\n", v4);
return 0LL;
}

original_code:
int32_t video_init(void)
{
  uint16_t val;
  uint32_t i;
  uint32_t fb_size;
  outw(0x1ce, 0x0);
  val = inw(0x1cf);
  printf("BGA version: %x\n", val);
  fb_size = 1024 * 768 * 4;
  for (i = 0; i < fb_size / 0x1000; i++)
    {
      printf("Page reserve... %x\n", 0xE0000000 + i * 0x1000);
      if (mmu_virt_page_reserve((vaddr_t)(0xE0000000 + i * 0x1000),
    (1 << 1)
    | (1 << 3)
    | (1 << 4)
    | (1 << 9)) != 0)
 {
   printf("Cant reserve video RAM pages...\n");
   while (1) __asm__ __volatile__ ("hlt");;;
 }
    }
  outw(0x1ce, 0x4);
  outw(0x1cf, 0x00);
  outw(0x1ce, 0x1);
  outw(0x1cf, 1024);
  outw(0x1ce, 0x2);
  outw(0x1cf, 768);
  outw(0x1ce, 0x3);
  outw(0x1cf, 0x20);
  outw(0x1ce, 0x1);
  val = inw(0x1cf);
  printf("Xres: %d\n", val);
  outw(0x1ce, 0x2);
  val = inw(0x1cf);
  printf("Yres: %d\n", val);
  outw(0x1ce, 0x4);
  val = inw(0x1cf);
  printf("Enable: %d\n", val);
  return 0;
}

canonical_original_code:
int video_init()
{
  unsigned short val;
  unsigned int i;
  unsigned int fb_size;
  outw(0x1ce, 0x0);
  val = inw(0x1cf);
  printf("BGA version: %x\n", val);
  fb_size = 1024 * 768 * 4;
  for (i = 0; i < fb_size / 0x1000; i++)
    {
      printf("Page reserve... %x\n", 0xE0000000 + i * 0x1000);
      if (mmu_virt_page_reserve((vaddr_t)(0xE0000000 + i * 0x1000),
    (1 << 1)
    | (1 << 3)
    | (1 << 4)
    | (1 << 9)) != 0)
 {
   printf("Cant reserve video RAM pages...\n");
   while (1) __asm__ __volatile__ ("hlt");;;
 }
    }
  outw(0x1ce, 0x4);
  outw(0x1cf, 0x00);
  outw(0x1ce, 0x1);
  outw(0x1cf, 1024);
  outw(0x1ce, 0x2);
  outw(0x1cf, 768);
  outw(0x1ce, 0x3);
  outw(0x1cf, 0x20);
  outw(0x1ce, 0x1);
  val = inw(0x1cf);
  printf("Xres: %d\n", val);
  outw(0x1ce, 0x2);
  val = inw(0x1cf);
  printf("Yres: %d\n", val);
  outw(0x1ce, 0x4);
  val = inw(0x1cf);
  printf("Enable: %d\n", val);
  return 0;
}

variable_types:
val
unsigned short

variable_types:
i
unsigned int


repo: bmartini/zynq-xdma

decompiled_code:
__int64 __fastcall xdma_calc_offset(__int64 a1)
{
return a1 - (_QWORD)map;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return a1 - (_QWORD)map;
}

original_code:
uint32_t xdma_calc_offset(void *ptr)
{
 return (((uint8_t *) ptr) - &map[0]);
}

canonical_original_code:
unsigned int xdma_calc_offset(void *ptr)
{
 return (((unsigned char *) ptr) - &map[0]);
}

variable_types:
ptr
void *

return_type: unsigned int


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

result = (unsigned int)(a2 * a1);
if ( (((_BYTE)a2 * (_BYTE)a1) & 0x3F) != 0 )
result = a2 * a1
+ ((unsigned int)((a2 * a1) >> 31) >> 26)
- (((_BYTE)a2 * (_BYTE)a1 + ((unsigned int)((a2 * a1) >> 31) >> 26)) & 0x3F)
+ 64;
return result;
}

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

result = (unsigned int)(a2 * a1);
if ( (((_BYTE)a2 * (_BYTE)a1) & 0x3F) != 0 )
result = a2 * a1
+ ((unsigned int)((a2 * a1) >> 31) >> 26)
- (((_BYTE)a2 * (_BYTE)a1 + ((unsigned int)((a2 * a1) >> 31) >> 26)) & 0x3F)
+ 64;
return result;
}

original_code:
uint32_t xdma_calc_size(int length, int byte_num)
{
 const int block = (4 * 16);
 length = length * byte_num;
 if (0 != (length % block)) {
  length += (block - (length % block));
 }
 return length;
}

canonical_original_code:
unsigned int xdma_calc_size(int length, int byte_num)
{
 int block = (4 * 16);
 length = length * byte_num;
 if (0 != (length % block)) {
  length += (block - (length % block));
 }
 return length;
}

variable_types: length int

variable_types: block int


decompiled_code:
char *xdma_alloc()
{
char *v0; // rbx

v0 = (char *)map + (unsigned int)alloc_offset;
alloc_offset += xdma_calc_size();
return v0;
}

canonical_decompiled_code:
char *func8()
{
char *v0; // rbx

v0 = (char *)map + (unsigned int)alloc_offset;
alloc_offset += func7();
return v0;
}

original_code:
void *xdma_alloc(int length, int byte_num)
{
 void *array = &map[alloc_offset];
 alloc_offset += xdma_calc_size(length, byte_num);
 return array;
}

variable_types:
array
void *

return_type: void *


decompiled_code:
void xdma_alloc_reset()
{
alloc_offset = 0;
}

canonical_decompiled_code:
void func9()
{
alloc_offset = 0;
}

original_code:
void xdma_alloc_reset(void)
{
 alloc_offset = 0;
}


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

if ( munmap(map, 0x2000000uLL) == -1 )
{
perror("Error un-mmapping the file");
result = 1LL;
}
else
{
close(fd);
result = 0LL;
}
return result;
}

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

if ( munmap(map, 0x2000000uLL) == -1 )
{
perror("Error un-mmapping the file");
result = 1LL;
}
else
{
close(fd);
result = 0LL;
}
return result;
}

original_code:
int xdma_exit(void)
{
 if (munmap(map, ((33554432) * sizeof(uint8_t))) == -1) {
  perror("Error un-mmapping the file");
  return 1;
 }
 close(fd);
 return 0;
}

canonical_original_code:
int xdma_exit()
{
 if (munmap(map, ((33554432) * sizeof(unsigned char))) == -1) {
  perror("Error un-mmapping the file");
  return 1;
 }
 close(fd);
 return 0;
}


decompiled_code:
__int64 xdma_num_of_devices()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 0;
if ( ioctl(fd, 0x5700uLL, v1) >= 0 )
return v1[0];
perror("Error ioctl getting device num");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func11()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 0;
if ( ioctl(fd, 0x5700uLL, v1) >= 0 )
return v1[0];
perror("Error ioctl getting device num");
return 0xFFFFFFFFLL;
}

original_code:
int xdma_num_of_devices(void)
{
 int num_devices = 0;
 if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((0)) << 0) | ((0) << ((0 +8)+8))), &num_devices) < 0) {
  perror("Error ioctl getting device num");
  return -1;
 }
 return num_devices;
}

canonical_original_code:
int xdma_num_of_devices()
{
 int num_devices = 0;
 if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((0)) << 0) | ((0) << ((0 +8)+8))), &num_devices) < 0) {
  perror("Error ioctl getting device num");
  return -1;
 }
 return num_devices;
}


decompiled_code:
__int64 xdma_init()
{
int v0; // eax
int v1; // ebx
int *v2; // rbx
int i; // ebp
__int64 result; // rax
int v5; // er13
int v6[8]; // [rsp+0h] [rbp-68h] BYREF
int v7[18]; // [rsp+20h] [rbp-48h] BYREF

v0 = open("/dev/xdma", 578, 384LL);
fd = v0;
if ( v0 == -1 )
{
perror("Error opening file for writing");
result = 1LL;
}
else
{
v1 = v0;
map = mmap(0LL, 0x2000000uLL, 3, 1, v0, 0LL);
if ( map == (void *)-1LL )
{
close(v1);
perror("Error mmapping the file");
result = 1LL;
}
else
{
xdma_alloc_reset();
num_of_devices = xdma_num_of_devices();
if ( num_of_devices <= 0 )
{
perror("Error no DMA devices found");
result = 1LL;
}
else
{
v2 = (int *)&xdma_devices;
for ( i = 0; i != 4; ++i )
{
*v2 = 0;
v2[1] = 0;
v2[2] = 0;
v2[3] = 0;
v2[4] = i;
if ( i < num_of_devices )
{
v5 = fd;
if ( ioctl(fd, 0x5701uLL, v2) < 0 )
{
perror("Error ioctl getting device info");
return 1LL;
}
v7[0] = v2[2];
v7[1] = 1;
v7[2] = 1;
v7[3] = 0;
v7[4] = 0;
if ( ioctl(v5, 0x5702uLL, v7) < 0 )
{
perror("Error ioctl config dst (rx) chan");
return 1LL;
}
v6[0] = *v2;
v6[1] = 0;
v6[2] = 1;
v6[3] = 0;
v6[4] = 0;
if ( ioctl(v5, 0x5702uLL, v6) < 0 )
{
perror("Error ioctl config src (tx) chan");
return 1LL;
}
}
v2 += 5;
}
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // eax
int v1; // ebx
int *v2; // rbx
int i; // ebp
__int64 result; // rax
int v5; // er13
int v6[8]; // [rsp+0h] [rbp-68h] BYREF
int v7[18]; // [rsp+20h] [rbp-48h] BYREF

v0 = open("/dev/xdma", 578, 384LL);
fd = v0;
if ( v0 == -1 )
{
perror("Error opening file for writing");
result = 1LL;
}
else
{
v1 = v0;
map = mmap(0LL, 0x2000000uLL, 3, 1, v0, 0LL);
if ( map == (void *)-1LL )
{
close(v1);
perror("Error mmapping the file");
result = 1LL;
}
else
{
func9();
num_of_devices = func11();
if ( num_of_devices <= 0 )
{
perror("Error no DMA devices found");
result = 1LL;
}
else
{
v2 = (int *)&xdma_devices;
for ( i = 0; i != 4; ++i )
{
*v2 = 0;
v2[1] = 0;
v2[2] = 0;
v2[3] = 0;
v2[4] = i;
if ( i < num_of_devices )
{
v5 = fd;
if ( ioctl(fd, 0x5701uLL, v2) < 0 )
{
perror("Error ioctl getting device info");
return 1LL;
}
v7[0] = v2[2];
v7[1] = 1;
v7[2] = 1;
v7[3] = 0;
v7[4] = 0;
if ( ioctl(v5, 0x5702uLL, v7) < 0 )
{
perror("Error ioctl config dst (rx) chan");
return 1LL;
}
v6[0] = *v2;
v6[1] = 0;
v6[2] = 1;
v6[3] = 0;
v6[4] = 0;
if ( ioctl(v5, 0x5702uLL, v6) < 0 )
{
perror("Error ioctl config src (tx) chan");
return 1LL;
}
}
v2 += 5;
}
result = 0LL;
}
}
}
return result;
}

original_code:
int xdma_init(void)
{
 int i;
 struct xdma_chan_cfg dst_config;
 struct xdma_chan_cfg src_config;
 fd = open("/dev/xdma", 02 | 0100 | 01000, (mode_t) 0600);
 if (fd == -1) {
  perror("Error opening file for writing");
  return 1;
 }
 map = mmap(0, ((33554432) * sizeof(uint8_t)), 0x1 | 0x2, 0x01, fd, 0);
 if (map == ((void *) -1)) {
  close(fd);
  perror("Error mmapping the file");
  return 1;
 }
 xdma_alloc_reset();
 num_of_devices = xdma_num_of_devices();
 if (num_of_devices <= 0) {
  perror("Error no DMA devices found");
  return 1;
 }
 for (i = 0; i < 4; i++) {
  xdma_devices[i].tx_chan = (uint32_t) ((void *)0);
  xdma_devices[i].tx_cmp = (uint32_t) ((void *)0);
  xdma_devices[i].rx_chan = (uint32_t) ((void *)0);
  xdma_devices[i].rx_cmp = (uint32_t) ((void *)0);
  xdma_devices[i].device_id = i;
  if (i < num_of_devices) {
   if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((1)) << 0) | ((0) << ((0 +8)+8))), &xdma_devices[i]) < 0) {
    perror("Error ioctl getting device info");
    return 1;
   }
   dst_config.chan = xdma_devices[i].rx_chan;
   dst_config.dir = XDMA_DEV_TO_MEM;
   dst_config.coalesc = 1;
   dst_config.delay = 0;
   dst_config.reset = 0;
   if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((2)) << 0) | ((0) << ((0 +8)+8))), &dst_config) < 0) {
    perror("Error ioctl config dst (rx) chan");
    return 1;
   }
   src_config.chan = xdma_devices[i].tx_chan;
   src_config.dir = XDMA_MEM_TO_DEV;
   src_config.coalesc = 1;
   src_config.delay = 0;
   src_config.reset = 0;
   if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((2)) << 0) | ((0) << ((0 +8)+8))), &src_config) < 0) {
    perror("Error ioctl config src (tx) chan");
    return 1;
   }
  }
 }
 return 0;
}

canonical_original_code:
int xdma_init()
{
 int i;
 struct xdma_chan_cfg dst_config;
 struct xdma_chan_cfg src_config;
 fd = open("/dev/xdma", 02 | 0100 | 01000, (unsigned int) 0600);
 if (fd == -1) {
  perror("Error opening file for writing");
  return 1;
 }
 map = mmap(0, ((33554432) * sizeof(unsigned char)), 0x1 | 0x2, 0x01, fd, 0);
 if (map == ((void *) -1)) {
  close(fd);
  perror("Error mmapping the file");
  return 1;
 }
 xdma_alloc_reset();
 num_of_devices = xdma_num_of_devices();
 if (num_of_devices <= 0) {
  perror("Error no DMA devices found");
  return 1;
 }
 for (i = 0; i < 4; i++) {
  xdma_devices[i].tx_chan = (unsigned int) ((void *)0);
  xdma_devices[i].tx_cmp = (unsigned int) ((void *)0);
  xdma_devices[i].rx_chan = (unsigned int) ((void *)0);
  xdma_devices[i].rx_cmp = (unsigned int) ((void *)0);
  xdma_devices[i].device_id = i;
  if (i < num_of_devices) {
   if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((1)) << 0) | ((0) << ((0 +8)+8))), &xdma_devices[i]) < 0) {
    perror("Error ioctl getting device info");
    return 1;
   }
   dst_config.chan = xdma_devices[i].rx_chan;
   dst_config.dir = XDMA_DEV_TO_MEM;
   dst_config.coalesc = 1;
   dst_config.delay = 0;
   dst_config.reset = 0;
   if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((2)) << 0) | ((0) << ((0 +8)+8))), &dst_config) < 0) {
    perror("Error ioctl config dst (rx) chan");
    return 1;
   }
   src_config.chan = xdma_devices[i].tx_chan;
   src_config.dir = XDMA_MEM_TO_DEV;
   src_config.coalesc = 1;
   src_config.delay = 0;
   src_config.reset = 0;
   if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((2)) << 0) | ((0) << ((0 +8)+8))), &src_config) < 0) {
    perror("Error ioctl config src (tx) chan");
    return 1;
   }
  }
 }
 return 0;
}

variable_types: i int

variable_types: struct xdma_chan_cfg { unsigned int chan; enum xdma_direction { XDMA_MEM_TO_DEV=0, XDMA_DEV_TO_MEM=1, XDMA_TRANS_NONE=2 } dir; int coalesc; int delay; int reset; }

user_defined_types: struct xdma_chan_cfg { unsigned int chan; enum xdma_direction dir; int coalesc; int delay; int reset; }


decompiled_code:
__int64 __fastcall xdma_perform_transaction(int a1, unsigned int a2, __int64 a3, int a4, __int64 a5, int a6)
{
bool v6; // r13
bool v7; // r15
int v11; // ebp
int v13; // [rsp+10h] [rbp-98h] BYREF
int v14; // [rsp+14h] [rbp-94h]
int v15; // [rsp+18h] [rbp-90h]
unsigned int v16; // [rsp+1Ch] [rbp-8Ch]
int v17[4]; // [rsp+20h] [rbp-88h] BYREF
int v18[2]; // [rsp+30h] [rbp-78h] BYREF
int v19; // [rsp+38h] [rbp-70h]
int v20; // [rsp+3Ch] [rbp-6Ch]
int v21; // [rsp+40h] [rbp-68h]
int v22; // [rsp+44h] [rbp-64h]
int v23[2]; // [rsp+50h] [rbp-58h] BYREF
int v24; // [rsp+58h] [rbp-50h]
int v25; // [rsp+5Ch] [rbp-4Ch]
int v26; // [rsp+60h] [rbp-48h]
int v27; // [rsp+64h] [rbp-44h]

v6 = a4 != 0 && a3 != 0;
v7 = a6 != 0 && a5 != 0;
if ( num_of_devices <= a1 )
{
perror("Error invalid device ID");
return (unsigned int)-1;
}
if ( v6 )
{
v18[0] = xdma_devices[5 * a1];
v18[1] = xdma_devices[5 * a1 + 1];
v19 = 0;
v20 = xdma_calc_offset(a3);
v21 = 4 * a4;
v22 = 0;
v11 = ioctl(fd, 0x5703uLL, v18);
if ( v11 < 0 )
{
perror("Error ioctl set src (tx) buf");
return (unsigned int)v11;
}
if ( !v7 )
{
v13 = xdma_devices[5 * a1];
v14 = xdma_devices[5 * a1 + 1];
v15 = v19;
v16 = a2 & 1;
v11 = ioctl(fd, 0x5704uLL, &v13);
if ( v11 >= 0 )
return (unsigned int)v11;
LABEL_10:
perror("Error ioctl start src (tx) trans");
return (unsigned int)v11;
}
}
else if ( !v7 )
{
return 0;
}
v23[0] = xdma_devices[5 * a1 + 2];
v23[1] = xdma_devices[5 * a1 + 3];
v24 = 0;
v25 = xdma_calc_offset(a5);
v26 = 4 * a6;
v27 = 1;
v11 = ioctl(fd, 0x5703uLL, v23);
if ( v11 < 0 )
{
perror("Error ioctl set dst (rx) buf");
return (unsigned int)v11;
}
if ( v6 )
{
v13 = xdma_devices[5 * a1];
v14 = xdma_devices[5 * a1 + 1];
v15 = v19;
v16 = a2 & 1;
v11 = ioctl(fd, 0x5704uLL, &v13);
if ( v11 < 0 )
goto LABEL_10;
}
v17[0] = xdma_devices[5 * a1 + 2];
v17[1] = xdma_devices[5 * a1 + 3];
v17[2] = v24;
v17[3] = (a2 >> 1) & 1;
v11 = ioctl(fd, 0x5704uLL, v17);
if ( v11 < 0 )
perror("Error ioctl start dst (rx) trans");
return (unsigned int)v11;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1, unsigned int a2, __int64 a3, int a4, __int64 a5, int a6)
{
bool v6; // r13
bool v7; // r15
int v11; // ebp
int v13; // [rsp+10h] [rbp-98h] BYREF
int v14; // [rsp+14h] [rbp-94h]
int v15; // [rsp+18h] [rbp-90h]
unsigned int v16; // [rsp+1Ch] [rbp-8Ch]
int v17[4]; // [rsp+20h] [rbp-88h] BYREF
int v18[2]; // [rsp+30h] [rbp-78h] BYREF
int v19; // [rsp+38h] [rbp-70h]
int v20; // [rsp+3Ch] [rbp-6Ch]
int v21; // [rsp+40h] [rbp-68h]
int v22; // [rsp+44h] [rbp-64h]
int v23[2]; // [rsp+50h] [rbp-58h] BYREF
int v24; // [rsp+58h] [rbp-50h]
int v25; // [rsp+5Ch] [rbp-4Ch]
int v26; // [rsp+60h] [rbp-48h]
int v27; // [rsp+64h] [rbp-44h]

v6 = a4 != 0 && a3 != 0;
v7 = a6 != 0 && a5 != 0;
if ( num_of_devices <= a1 )
{
perror("Error invalid device ID");
return (unsigned int)-1;
}
if ( v6 )
{
v18[0] = xdma_devices[5 * a1];
v18[1] = xdma_devices[5 * a1 + 1];
v19 = 0;
v20 = func6(a3);
v21 = 4 * a4;
v22 = 0;
v11 = ioctl(fd, 0x5703uLL, v18);
if ( v11 < 0 )
{
perror("Error ioctl set src (tx) buf");
return (unsigned int)v11;
}
if ( !v7 )
{
v13 = xdma_devices[5 * a1];
v14 = xdma_devices[5 * a1 + 1];
v15 = v19;
v16 = a2 & 1;
v11 = ioctl(fd, 0x5704uLL, &v13);
if ( v11 >= 0 )
return (unsigned int)v11;
LABEL_10:
perror("Error ioctl start src (tx) trans");
return (unsigned int)v11;
}
}
else if ( !v7 )
{
return 0;
}
v23[0] = xdma_devices[5 * a1 + 2];
v23[1] = xdma_devices[5 * a1 + 3];
v24 = 0;
v25 = func6(a5);
v26 = 4 * a6;
v27 = 1;
v11 = ioctl(fd, 0x5703uLL, v23);
if ( v11 < 0 )
{
perror("Error ioctl set dst (rx) buf");
return (unsigned int)v11;
}
if ( v6 )
{
v13 = xdma_devices[5 * a1];
v14 = xdma_devices[5 * a1 + 1];
v15 = v19;
v16 = a2 & 1;
v11 = ioctl(fd, 0x5704uLL, &v13);
if ( v11 < 0 )
goto LABEL_10;
}
v17[0] = xdma_devices[5 * a1 + 2];
v17[1] = xdma_devices[5 * a1 + 3];
v17[2] = v24;
v17[3] = (a2 >> 1) & 1;
v11 = ioctl(fd, 0x5704uLL, v17);
if ( v11 < 0 )
perror("Error ioctl start dst (rx) trans");
return (unsigned int)v11;
}

original_code:
int xdma_perform_transaction(int device_id, enum xdma_wait wait,
        uint32_t * src_ptr, uint32_t src_length,
        uint32_t * dst_ptr, uint32_t dst_length)
{
 int ret = 0;
 struct xdma_buf_info dst_buf;
 struct xdma_buf_info src_buf;
 struct xdma_transfer dst_trans;
 struct xdma_transfer src_trans;
 const _Bool src_used = ((src_ptr != ((void *)0)) && (src_length != 0));
 const _Bool dst_used = ((dst_ptr != ((void *)0)) && (dst_length != 0));
 if (device_id >= num_of_devices) {
  perror("Error invalid device ID");
  return -1;
 }
 if (src_used) {
  src_buf.chan = xdma_devices[device_id].tx_chan;
  src_buf.completion = xdma_devices[device_id].tx_cmp;
  src_buf.cookie = 0;
  src_buf.buf_offset = (uint32_t) xdma_calc_offset(src_ptr);
  src_buf.buf_size = (uint32_t) (src_length * sizeof(src_ptr[0]));
  src_buf.dir = XDMA_MEM_TO_DEV;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((3)) << 0) | ((0) << ((0 +8)+8))), &src_buf);
  if (ret < 0) {
   perror("Error ioctl set src (tx) buf");
   return ret;
  }
 }
 if (dst_used) {
  dst_buf.chan = xdma_devices[device_id].rx_chan;
  dst_buf.completion = xdma_devices[device_id].rx_cmp;
  dst_buf.cookie = 0;
  dst_buf.buf_offset = (uint32_t) xdma_calc_offset(dst_ptr);
  dst_buf.buf_size = (uint32_t) (dst_length * sizeof(dst_ptr[0]));
  dst_buf.dir = XDMA_DEV_TO_MEM;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((3)) << 0) | ((0) << ((0 +8)+8))), &dst_buf);
  if (ret < 0) {
   perror("Error ioctl set dst (rx) buf");
   return ret;
  }
 }
 if (src_used) {
  src_trans.chan = xdma_devices[device_id].tx_chan;
  src_trans.completion = xdma_devices[device_id].tx_cmp;
  src_trans.cookie = src_buf.cookie;
  src_trans.wait = (0 != (wait & XDMA_WAIT_SRC));
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((4)) << 0) | ((0) << ((0 +8)+8))), &src_trans);
  if (ret < 0) {
   perror("Error ioctl start src (tx) trans");
   return ret;
  }
 }
 if (dst_used) {
  dst_trans.chan = xdma_devices[device_id].rx_chan;
  dst_trans.completion = xdma_devices[device_id].rx_cmp;
  dst_trans.cookie = dst_buf.cookie;
  dst_trans.wait = (0 != (wait & XDMA_WAIT_DST));
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((4)) << 0) | ((0) << ((0 +8)+8))), &dst_trans);
  if (ret < 0) {
   perror("Error ioctl start dst (rx) trans");
   return ret;
  }
 }
 return ret;
}

canonical_original_code:
int xdma_perform_transaction(int device_id, enum xdma_wait wait, unsigned int *src_ptr, unsigned int src_length, unsigned int *dst_ptr, unsigned int dst_length)
{
 int ret = 0;
 struct xdma_buf_info dst_buf;
 struct xdma_buf_info src_buf;
 struct xdma_transfer dst_trans;
 struct xdma_transfer src_trans;
 _Bool src_used = ((src_ptr != ((void *)0)) && (src_length != 0));
 _Bool dst_used = ((dst_ptr != ((void *)0)) && (dst_length != 0));
 if (device_id >= num_of_devices) {
  perror("Error invalid device ID");
  return -1;
 }
 if (src_used) {
  src_buf.chan = xdma_devices[device_id].tx_chan;
  src_buf.completion = xdma_devices[device_id].tx_cmp;
  src_buf.cookie = 0;
  src_buf.buf_offset = (unsigned int) xdma_calc_offset(src_ptr);
  src_buf.buf_size = (unsigned int) (src_length * sizeof(src_ptr[0]));
  src_buf.dir = XDMA_MEM_TO_DEV;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((3)) << 0) | ((0) << ((0 +8)+8))), &src_buf);
  if (ret < 0) {
   perror("Error ioctl set src (tx) buf");
   return ret;
  }
 }
 if (dst_used) {
  dst_buf.chan = xdma_devices[device_id].rx_chan;
  dst_buf.completion = xdma_devices[device_id].rx_cmp;
  dst_buf.cookie = 0;
  dst_buf.buf_offset = (unsigned int) xdma_calc_offset(dst_ptr);
  dst_buf.buf_size = (unsigned int) (dst_length * sizeof(dst_ptr[0]));
  dst_buf.dir = XDMA_DEV_TO_MEM;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((3)) << 0) | ((0) << ((0 +8)+8))), &dst_buf);
  if (ret < 0) {
   perror("Error ioctl set dst (rx) buf");
   return ret;
  }
 }
 if (src_used) {
  src_trans.chan = xdma_devices[device_id].tx_chan;
  src_trans.completion = xdma_devices[device_id].tx_cmp;
  src_trans.cookie = src_buf.cookie;
  src_trans.wait = (0 != (wait & XDMA_WAIT_SRC));
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((4)) << 0) | ((0) << ((0 +8)+8))), &src_trans);
  if (ret < 0) {
   perror("Error ioctl start src (tx) trans");
   return ret;
  }
 }
 if (dst_used) {
  dst_trans.chan = xdma_devices[device_id].rx_chan;
  dst_trans.completion = xdma_devices[device_id].rx_cmp;
  dst_trans.cookie = dst_buf.cookie;
  dst_trans.wait = (0 != (wait & XDMA_WAIT_DST));
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((4)) << 0) | ((0) << ((0 +8)+8))), &dst_trans);
  if (ret < 0) {
   perror("Error ioctl start dst (rx) trans");
   return ret;
  }
 }
 return ret;
}

variable_types: enum xdma_wait { XDMA_WAIT_NONE=0, XDMA_WAIT_SRC=expr, XDMA_WAIT_DST=expr, XDMA_WAIT_BOTH=expr }

variable_types: ret int

variable_types: struct xdma_buf_info { unsigned int chan; unsigned int completion; int cookie; unsigned int buf_offset; unsigned int buf_size; enum xdma_direction { XDMA_MEM_TO_DEV=0, XDMA_DEV_TO_MEM=1, XDMA_TRANS_NONE=2 } dir; }

variable_types: struct xdma_transfer { unsigned int chan; unsigned int completion; int cookie; unsigned int wait; }

user_defined_types: struct xdma_buf_info { unsigned int chan; unsigned int completion; int cookie; unsigned int buf_offset; unsigned int buf_size; enum xdma_direction dir; }


decompiled_code:
__int64 __fastcall xdma_stop_transaction(int a1, __int64 a2, int a3, __int64 a4, int a5)
{
bool v5; // cl
bool v6; // r12
int v7; // ebp
int v9[4]; // [rsp+0h] [rbp-38h] BYREF
int v10[10]; // [rsp+10h] [rbp-28h] BYREF

v5 = a5 != 0 && a4 != 0;
if ( num_of_devices <= a1 )
{
perror("Error invalid device ID");
v7 = -1;
}
else
{
v6 = v5;
v7 = 0;
if ( a3 != 0 && a2 != 0 && (v9[0] = xdma_devices[5 * a1], v7 = ioctl(fd, 0x5705uLL, v9), v7 < 0) )
{
perror("Error ioctl stop src (tx) trans");
}
else if ( v6 )
{
v10[0] = xdma_devices[5 * a1 + 2];
v7 = ioctl(fd, 0x5705uLL, v10);
if ( v7 < 0 )
perror("Error ioctl stop dst (rx) trans");
}
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, __int64 a2, int a3, __int64 a4, int a5)
{
bool v5; // cl
bool v6; // r12
int v7; // ebp
int v9[4]; // [rsp+0h] [rbp-38h] BYREF
int v10[10]; // [rsp+10h] [rbp-28h] BYREF

v5 = a5 != 0 && a4 != 0;
if ( num_of_devices <= a1 )
{
perror("Error invalid device ID");
v7 = -1;
}
else
{
v6 = v5;
v7 = 0;
if ( a3 != 0 && a2 != 0 && (v9[0] = xdma_devices[5 * a1], v7 = ioctl(fd, 0x5705uLL, v9), v7 < 0) )
{
perror("Error ioctl stop src (tx) trans");
}
else if ( v6 )
{
v10[0] = xdma_devices[5 * a1 + 2];
v7 = ioctl(fd, 0x5705uLL, v10);
if ( v7 < 0 )
perror("Error ioctl stop dst (rx) trans");
}
}
return (unsigned int)v7;
}

original_code:
int xdma_stop_transaction(int device_id,
     uint32_t * src_ptr, uint32_t src_length,
     uint32_t * dst_ptr, uint32_t dst_length)
{
 int ret = 0;
 struct xdma_transfer dst_trans;
 struct xdma_transfer src_trans;
 const _Bool src_used = ((src_ptr != ((void *)0)) && (src_length != 0));
 const _Bool dst_used = ((dst_ptr != ((void *)0)) && (dst_length != 0));
 if (device_id >= num_of_devices) {
  perror("Error invalid device ID");
  return -1;
 }
 if (src_used) {
  src_trans.chan = xdma_devices[device_id].tx_chan;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((5)) << 0) | ((0) << ((0 +8)+8))), &(src_trans.chan));
  if (ret < 0) {
   perror("Error ioctl stop src (tx) trans");
   return ret;
  }
 }
 if (dst_used) {
  dst_trans.chan = xdma_devices[device_id].rx_chan;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((5)) << 0) | ((0) << ((0 +8)+8))), &(dst_trans.chan));
  if (ret < 0) {
   perror("Error ioctl stop dst (rx) trans");
   return ret;
  }
 }
 return ret;
}

canonical_original_code:
int xdma_stop_transaction(int device_id, unsigned int *src_ptr, unsigned int src_length, unsigned int *dst_ptr, unsigned int dst_length)
{
 int ret = 0;
 struct xdma_transfer dst_trans;
 struct xdma_transfer src_trans;
 _Bool src_used = ((src_ptr != ((void *)0)) && (src_length != 0));
 _Bool dst_used = ((dst_ptr != ((void *)0)) && (dst_length != 0));
 if (device_id >= num_of_devices) {
  perror("Error invalid device ID");
  return -1;
 }
 if (src_used) {
  src_trans.chan = xdma_devices[device_id].tx_chan;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((5)) << 0) | ((0) << ((0 +8)+8))), &(src_trans.chan));
  if (ret < 0) {
   perror("Error ioctl stop src (tx) trans");
   return ret;
  }
 }
 if (dst_used) {
  dst_trans.chan = xdma_devices[device_id].rx_chan;
  ret = (int)ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('W')) << (0 +8)) | (((5)) << 0) | ((0) << ((0 +8)+8))), &(dst_trans.chan));
  if (ret < 0) {
   perror("Error ioctl stop dst (rx) trans");
   return ret;
  }
 }
 return ret;
}

user_defined_types: struct xdma_transfer { unsigned int chan; unsigned int completion; int cookie; unsigned int wait; }


commandline

repo: bmcmanus/blog-examples

decompiled_code:
void __fastcall __noreturn commandline(const char **a1)
{
fprintf(stderr, "Usage: %s <Port>\n", *a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char **a1)
{
fprintf(stderr, "Usage: %s <Port>\n", *a1);
exit(1);
}

original_code:
void commandline(char *argv[])
{
 fprintf(stderr, "Usage: %s <Port>\n", argv[0]);
 exit(1);
}

variable_types:
argv
char *[]


error

decompiled_code:
void __noreturn error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void error(char *error, ...)
{
 va_list argp;
 __builtin_va_start(argp,error);
 vfprintf(stderr, error, argp);
 __builtin_va_end(argp);
 fprintf(stderr, "\n");
 exit(1);
}

canonical_original_code:
void error(char *error, ...)
{
 __builtin_va_list argp;
 __builtin_va_start(argp,error);
 vfprintf(stderr, error, argp);
 __builtin_va_end(argp);
 fprintf(stderr, "\n");
 exit(1);
}

variable_types:
error
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int *v3; // rbx
__int16 v4; // bp
int v5; // er12
char *v6; // rax
int v7; // ebp
char *v8; // rax
char *v9; // rax
size_t v10; // rax
char *v11; // rax
char *v12; // rax
char *v13; // rax
__int64 v14[2]; // [rsp+0h] [rbp-248h] BYREF
char v15[496]; // [rsp+10h] [rbp-238h] BYREF
struct sockaddr v16; // [rsp+200h] [rbp-48h] BYREF
struct sockaddr addr; // [rsp+210h] [rbp-38h] BYREF
socklen_t addr_len[7]; // [rsp+22Ch] [rbp-1Ch] BYREF

addr_len[0] = 0;
v14[0] = 0LL;
v14[1] = 0LL;
memset(v15, 0, sizeof(v15));
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&v16.sa_family = 0LL;
*(_QWORD *)&v16.sa_data[6] = 0LL;
if ( argc == 2 )
{
v3 = __errno_location();
if ( *v3 )
commandline(argv);
v4 = atoi(argv[1]);
v5 = socket(2, 1, 6);
if ( v5 < 0 || *v3 )
{
v6 = strerror(*v3);
error("Unable to establish TCP Socket for Server. Errno: %s", v6);
}
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(v4);
if ( bind(v5, &addr, 0x10u) >= 0 )
{
if ( listen(v5, 1) >= 0 )
{
while ( 1 )
{
addr_len[0] = 16;
v7 = accept(v5, &v16, addr_len);
if ( v7 < 0 )
break;
v8 = inet_ntoa(*(struct in_addr *)&v16.sa_data[2]);
fprintf(stdout, "Handling client %s\n", v8);
v9 = inet_ntoa(*(struct in_addr *)&v16.sa_data[2]);
snprintf((char *)v14, 0x1FFuLL, "Hello %s, it's very nice to meet you.\n", v9);
v10 = strlen((const char *)v14);
send(v7, v14, v10, 0);
close(v7);
}
v13 = strerror(*v3);
error(
"Socket ACCEPT failed. clientsocket = %d, serversocket = %d, errno = %d, strerror = %s",
(unsigned int)v7,
(unsigned int)v5,
(unsigned int)*v3,
v13);
}
v12 = strerror(*v3);
error("Unable to mark the socket to listen for connections. Error: %s", v12);
}
v11 = strerror(*v3);
error("Opening the TCP server socket failed on this host. Error: %s", v11);
}
commandline(argv);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int *v3; // rbx
__int16 v4; // bp
int v5; // er12
char *v6; // rax
int v7; // ebp
char *v8; // rax
char *v9; // rax
size_t v10; // rax
char *v11; // rax
char *v12; // rax
char *v13; // rax
__int64 v14[2]; // [rsp+0h] [rbp-248h] BYREF
char v15[496]; // [rsp+10h] [rbp-238h] BYREF
struct sockaddr v16; // [rsp+200h] [rbp-48h] BYREF
struct sockaddr addr; // [rsp+210h] [rbp-38h] BYREF
socklen_t addr_len[7]; // [rsp+22Ch] [rbp-1Ch] BYREF

addr_len[0] = 0;
v14[0] = 0LL;
v14[1] = 0LL;
memset(v15, 0, sizeof(v15));
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&v16.sa_family = 0LL;
*(_QWORD *)&v16.sa_data[6] = 0LL;
if ( argc == 2 )
{
v3 = __errno_location();
if ( *v3 )
func9(argv);
v4 = atoi(argv[1]);
v5 = socket(2, 1, 6);
if ( v5 < 0 || *v3 )
{
v6 = strerror(*v3);
func10("Unable to establish TCP Socket for Server. Errno: %s", v6);
}
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = func7(0);
*(_WORD *)addr.sa_data = func8(v4);
if ( bind(v5, &addr, 0x10u) >= 0 )
{
if ( listen(v5, 1) >= 0 )
{
while ( 1 )
{
addr_len[0] = 16;
v7 = accept(v5, &v16, addr_len);
if ( v7 < 0 )
break;
v8 = inet_ntoa(*(struct in_addr *)&v16.sa_data[2]);
fprintf(stdout, "Handling client %s\n", v8);
v9 = inet_ntoa(*(struct in_addr *)&v16.sa_data[2]);
snprintf((char *)v14, 0x1FFuLL, "Hello %s, it's very nice to meet you.\n", v9);
v10 = strlen((const char *)v14);
send(v7, v14, v10, 0);
close(v7);
}
v13 = strerror(*v3);
func10(
"Socket ACCEPT failed. clientsocket = %d, serversocket = %d, errno = %d, strerror = %s",
(unsigned int)v7,
(unsigned int)v5,
(unsigned int)*v3,
v13);
}
v12 = strerror(*v3);
func10("Unable to mark the socket to listen for connections. Error: %s", v12);
}
v11 = strerror(*v3);
func10("Opening the TCP server socket failed on this host. Error: %s", v11);
}
func9(argv);
}

original_code:
int main(int argc, char *argv[])
{
 int serverport = 0,serversocket=-1,clientsocket=-1;
 unsigned int clientlen=0;
 struct sockaddr_in serveraddress;
 struct sockaddr_in clientaddress;
 static char * bannerfmt = "Hello %s, it's very nice to meet you.\n";
 char banner[512] = "\000";
 memset(&serveraddress, 0x00, sizeof(serveraddress));
 memset(&clientaddress, 0x00, sizeof(clientaddress));
 if (argc != 2)
 {
  commandline(argv);
 }
 serverport = atoi(argv[1]);
 if ((*__errno_location ()) != 0)
 {
  commandline(argv);
 }
 serversocket = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (serversocket < 0 || (*__errno_location ()) != 0)
 {
  error("Unable to establish TCP Socket for Server.  Errno: %s", strerror((*__errno_location ())));
 }
 serveraddress.sin_family = 2;
 serveraddress.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 serveraddress.sin_port = htons(serverport);
 if (bind(serversocket, (struct sockaddr *) &serveraddress, sizeof(serveraddress)) < 0)
 {
  error("Opening the TCP server socket failed on this host. Error: %s", strerror((*__errno_location ())));
 }
 if (listen(serversocket, 1) < 0)
 {
  error("Unable to mark the socket to listen for connections. Error: %s", strerror((*__errno_location ())));
 }
 while(1)
 {
 clientlen = sizeof(clientaddress);
 if ((clientsocket = accept(serversocket, (struct sockaddr *) &clientaddress, &clientlen)) < 0)
 {
  error("Socket ACCEPT failed.  clientsocket = %d, serversocket = %d, errno = %d, strerror = %s", clientsocket, serversocket, (*__errno_location ()), strerror((*__errno_location ())));
 }
 fprintf(stdout, "Handling client %s\n", inet_ntoa(clientaddress.sin_addr));
 snprintf(banner, (size_t) sizeof(banner) - 1, bannerfmt, inet_ntoa(clientaddress.sin_addr));
 send(clientsocket, banner, strlen(banner), 0);
 close(clientsocket);
 }
return (0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int serverport = 0; int serversocket = -1; int clientsocket = -1;
 unsigned int clientlen = 0;
 struct sockaddr_in serveraddress;
 struct sockaddr_in clientaddress;
 static char *bannerfmt = "Hello %s, it's very nice to meet you.\n";
 char banner[512] = "\000";
 memset(&serveraddress, 0x00, sizeof(serveraddress));
 memset(&clientaddress, 0x00, sizeof(clientaddress));
 if (argc != 2)
 {
  commandline(argv);
 }
 serverport = atoi(argv[1]);
 if ((*__errno_location ()) != 0)
 {
  commandline(argv);
 }
 serversocket = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (serversocket < 0 || (*__errno_location ()) != 0)
 {
  error("Unable to establish TCP Socket for Server.  Errno: %s", strerror((*__errno_location ())));
 }
 serveraddress.sin_family = 2;
 serveraddress.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 serveraddress.sin_port = htons(serverport);
 if (bind(serversocket, (struct sockaddr *) &serveraddress, sizeof(serveraddress)) < 0)
 {
  error("Opening the TCP server socket failed on this host. Error: %s", strerror((*__errno_location ())));
 }
 if (listen(serversocket, 1) < 0)
 {
  error("Unable to mark the socket to listen for connections. Error: %s", strerror((*__errno_location ())));
 }
 while(1)
 {
 clientlen = sizeof(clientaddress);
 if ((clientsocket = accept(serversocket, (struct sockaddr *) &clientaddress, &clientlen)) < 0)
 {
  error("Socket ACCEPT failed.  clientsocket = %d, serversocket = %d, errno = %d, strerror = %s", clientsocket, serversocket, (*__errno_location ()), strerror((*__errno_location ())));
 }
 fprintf(stdout, "Handling client %s\n", inet_ntoa(clientaddress.sin_addr));
 snprintf(banner, (unsigned long) sizeof(banner) - 1, bannerfmt, inet_ntoa(clientaddress.sin_addr));
 send(clientsocket, banner, strlen(banner), 0);
 close(clientsocket);
 }
return (0);
}

variable_types: argc int

variable_types: serverport int

variable_types: serversocket int

variable_types: clientsocket int

variable_types:
clientlen
unsigned int

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

variable_types:
bannerfmt
char *

variable_types:
banner
char [512]

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

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


repo: bmoscon/HashSet

decompiled_code:
__int64 __fastcall hash_int(_DWORD *a1)
{
return (unsigned int)(*a1 % 50000);
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
return (unsigned int)(*a1 % 50000);
}

original_code:
uint32_t hash_int(const void *number) {
  return ((*(int *)number) % 50000);
}

canonical_original_code:
unsigned int hash_int(void *number) {
  return ((*(int *)number) % 50000);
}

variable_types:
number
void *


chksum

decompiled_code:
__int64 __fastcall chksum(const char *a1)
{
int v1; // eax
const char *v2; // rdx
__int64 v3; // rsi
__int64 result; // rax

v1 = strlen(a1);
if ( v1 <= 0 )
return 0LL;
v2 = a1;
v3 = (__int64)&a1[v1 - 1 + 1];
LODWORD(result) = 0;
do
result = (unsigned int)(*v2++ + __ROR4__(result, 1));
while ( v2 != (const char *)v3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
int v1; // eax
const char *v2; // rdx
__int64 v3; // rsi
__int64 result; // rax

v1 = strlen(a1);
if ( v1 <= 0 )
return 0LL;
v2 = a1;
v3 = (__int64)&a1[v1 - 1 + 1];
LODWORD(result) = 0;
do
result = (unsigned int)(*v2++ + __ROR4__(result, 1));
while ( v2 != (const char *)v3 );
return result;
}

original_code:
uint32_t chksum(const void *str)
{
  char *s = (char *)str;
  int len = strlen(s);
  int i;
  uint32_t c = 0;
  for (i = 0; i < len; ++i) {
    c = (c >> 1) + ((c & 1) << (32-1));
    c += s[i];
  }
  return (c);
}

canonical_original_code:
unsigned int chksum(void *str)
{
  char *s = (char *)str;
  int len = strlen(s);
  int i;
  unsigned int c = 0;
  for (i = 0; i < len; ++i) {
    c = (c >> 1) + ((c & 1) << (32-1));
    c += s[i];
  }
  return (c);
}

variable_types:
str
void *

variable_types:
s
char *

variable_types: len int

variable_types: i int

variable_types:
c
unsigned int