dividesections

decompiled_code:
void __fastcall dividesections(__int64 a1, int a2, int a3, int a4)
{
int v4; // er13
__int64 v5; // r12
__int64 v6; // rbx
void *v7; // r8
__int64 v8; // rax
__int64 v9; // rax
signed int v10; // er14
int v11; // er15
__int64 v12; // r13
int v13; // eax
__int64 v14; // rbp
unsigned int v15; // eax
unsigned int v16; // ebx
unsigned int v17; // esi
int v18; // eax
__int64 v19; // r12
__int64 v20; // rdi
__int64 v21; // rax
__int64 v22; // rsi
void *v23; // [rsp+0h] [rbp-50h]
unsigned int v24; // [rsp+0h] [rbp-50h]
__int64 v25; // [rsp+8h] [rbp-48h] BYREF
__int64 v26[8]; // [rsp+10h] [rbp-40h] BYREF

if ( a1 && a2 )
{
v4 = a3;
v5 = a4;
if ( a3 )
{
if ( ((unsigned int)iself64() == 0 ? 40 : 64) != a3 )
{
v18 = iself64();
warn("section table entry size: %d instead of %d.", (unsigned int)v4, v18 == 0 ? 40 : 64);
}
}
else
{
warn("ignoring zero value for shentsize.");
v4 = (unsigned int)iself64() == 0 ? 40 : 64;
}
v6 = v4 * a2;
v25 = v6;
v7 = (void *)getptrto(a1, &v25);
v8 = v25;
if ( v6 != v25 )
{
if ( !v25 )
{
warn("invalid section header table offset: %ld.", a1);
return;
}
v23 = v7;
warn("section table extends %ld bytes past EOF.", v6 - v25);
v8 = v25;
v7 = v23;
}
v9 = v8 / v4;
v10 = v9;
v11 = v9;
if ( (_DWORD)v9 )
{
v12 = copytable(v7, v9, v4);
v13 = iself64();
v14 = 0LL;
v15 = recordpiece(a1, v25, 17LL, "shdrs", v13 == 0 ? 4 : 8);
if ( (_DWORD)v5 )
{
if ( v10 > (int)v5 )
{
v24 = v15;
v19 = v12 + (v5 << 6);
v20 = *(_QWORD *)(v19 + 24);
v26[0] = *(_QWORD *)(v19 + 32);
v21 = getptrto(v20, v26);
v22 = *(_QWORD *)(v19 + 24);
v14 = v21;
if ( v26[0] <= 0 )
{
v14 = 0LL;
warn("invalid section header string table offset: %ld.", v22);
}
else
{
setpiecestrtable(v24, v22, v26[0]);
}
}
else
{
warn("invalid section header string table index: %d.", (unsigned int)v5);
}
}
v16 = 0;
if ( v10 > 0 )
{
do
{
v17 = v16++;
dividesection(v12, v17, v14, v26[0]);
}
while ( v11 != v16 );
}
if ( sectionenumcount )
{
enumfinalize();
addenumname(v10, "COUNT");
}
}
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2, int a3, int a4)
{
int v4; // er13
__int64 v5; // r12
__int64 v6; // rbx
void *v7; // r8
__int64 v8; // rax
__int64 v9; // rax
signed int v10; // er14
int v11; // er15
__int64 v12; // r13
int v13; // eax
__int64 v14; // rbp
unsigned int v15; // eax
unsigned int v16; // ebx
unsigned int v17; // esi
int v18; // eax
__int64 v19; // r12
__int64 v20; // rdi
__int64 v21; // rax
__int64 v22; // rsi
void *v23; // [rsp+0h] [rbp-50h]
unsigned int v24; // [rsp+0h] [rbp-50h]
__int64 v25; // [rsp+8h] [rbp-48h] BYREF
__int64 v26[8]; // [rsp+10h] [rbp-40h] BYREF

if ( a1 && a2 )
{
v4 = a3;
v5 = a4;
if ( a3 )
{
if ( ((unsigned int)iself64() == 0 ? 40 : 64) != a3 )
{
v18 = iself64();
warn("section table entry size: %d instead of %d.", (unsigned int)v4, v18 == 0 ? 40 : 64);
}
}
else
{
warn("ignoring zero value for shentsize.");
v4 = (unsigned int)iself64() == 0 ? 40 : 64;
}
v6 = v4 * a2;
v25 = v6;
v7 = (void *)getptrto(a1, &v25);
v8 = v25;
if ( v6 != v25 )
{
if ( !v25 )
{
warn("invalid section header table offset: %ld.", a1);
return;
}
v23 = v7;
warn("section table extends %ld bytes past EOF.", v6 - v25);
v8 = v25;
v7 = v23;
}
v9 = v8 / v4;
v10 = v9;
v11 = v9;
if ( (_DWORD)v9 )
{
v12 = func1(v7, v9, v4);
v13 = iself64();
v14 = 0LL;
v15 = recordpiece(a1, v25, 17LL, "shdrs", v13 == 0 ? 4 : 8);
if ( (_DWORD)v5 )
{
if ( v10 > (int)v5 )
{
v24 = v15;
v19 = v12 + (v5 << 6);
v20 = *(_QWORD *)(v19 + 24);
v26[0] = *(_QWORD *)(v19 + 32);
v21 = getptrto(v20, v26);
v22 = *(_QWORD *)(v19 + 24);
v14 = v21;
if ( v26[0] <= 0 )
{
v14 = 0LL;
warn("invalid section header string table offset: %ld.", v22);
}
else
{
setpiecestrtable(v24, v22, v26[0]);
}
}
else
{
warn("invalid section header string table index: %d.", (unsigned int)v5);
}
}
v16 = 0;
if ( v10 > 0 )
{
do
{
v17 = v16++;
func5(v12, v17, v14, v26[0]);
}
while ( v11 != v16 );
}
if ( sectionenumcount )
{
func3();
func4(v10, "COUNT");
}
}
}
}

original_code:
void dividesections(long offset, int count, int entsize, int shstrndx)
{
    void const *ptr;
    Elf64_Shdr const *shdrs;
    char const *strtab = ((void *)0);
    long size, strsize;
    int i, n;
    if (!offset || !count)
 return;
    if (!entsize) {
 warn("ignoring zero value for shentsize.");
 entsize = ((int)(iself64() ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr)));
    } else if (entsize != ((int)(iself64() ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr)))) {
 warn("section table entry size: %d instead of %d.",
      entsize, ((int)(iself64() ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr))));
    }
    size = count * entsize;
    ptr = getptrto(offset, &size);
    if (size != count * entsize) {
 if (size == 0) {
     warn("invalid section header table offset: %ld.", offset);
     return;
 }
 warn("section table extends %ld bytes past EOF.",
      count * entsize - size);
    }
    count = size / entsize;
    if (!count)
 return;
    shdrs = copytable(ptr, count, entsize);
    n = recordpiece(offset, size, P_SHDRTAB, "shdrs", ((int)(iself64() ? sizeof(Elf64_Addr) : sizeof(Elf32_Addr))));
    if (shstrndx) {
 if (shstrndx >= count) {
     warn("invalid section header string table index: %d.", shstrndx);
 } else {
     strsize = shdrs[shstrndx].sh_size;
     strtab = getptrto(shdrs[shstrndx].sh_offset, &strsize);
     if (strsize <= 0) {
  strtab = ((void *)0);
  warn("invalid section header string table offset: %ld.",
       shdrs[shstrndx].sh_offset);
     } else {
  setpiecestrtable(n, shdrs[shstrndx].sh_offset, strsize);
     }
 }
    }
    for (i = 0 ; i < count ; ++i)
 dividesection(shdrs, i, strtab, strsize);
    if (sectionenumcount) {
 enumfinalize();
 addenumname(count, "COUNT");
    }
}

canonical_original_code:
void dividesections(long offset, int count, int entsize, int shstrndx)
{
    void *ptr;
    struct Elf64_Shdr *shdrs;
    char *strtab = ((void *)0);
    long size; long strsize;
    int i; int n;
    if (!offset || !count)
 return;
    if (!entsize) {
 warn("ignoring zero value for shentsize.");
 entsize = ((int)(iself64() ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr)));
    } else if (entsize != ((int)(iself64() ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr)))) {
 warn("section table entry size: %d instead of %d.",
      entsize, ((int)(iself64() ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr))));
    }
    size = count * entsize;
    ptr = getptrto(offset, &size);
    if (size != count * entsize) {
 if (size == 0) {
     warn("invalid section header table offset: %ld.", offset);
     return;
 }
 warn("section table extends %ld bytes past EOF.",
      count * entsize - size);
    }
    count = size / entsize;
    if (!count)
 return;
    shdrs = copytable(ptr, count, entsize);
    n = recordpiece(offset, size, P_SHDRTAB, "shdrs", ((int)(iself64() ? sizeof(Elf64_Addr) : sizeof(Elf32_Addr))));
    if (shstrndx) {
 if (shstrndx >= count) {
     warn("invalid section header string table index: %d.", shstrndx);
 } else {
     strsize = shdrs[shstrndx].sh_size;
     strtab = getptrto(shdrs[shstrndx].sh_offset, &strsize);
     if (strsize <= 0) {
  strtab = ((void *)0);
  warn("invalid section header string table offset: %ld.",
       shdrs[shstrndx].sh_offset);
     } else {
  setpiecestrtable(n, shdrs[shstrndx].sh_offset, strsize);
     }
 }
    }
    for (i = 0 ; i < count ; ++i)
 dividesection(shdrs, i, strtab, strsize);
    if (sectionenumcount) {
 enumfinalize();
 addenumname(count, "COUNT");
    }
}

variable_types: offset long

variable_types: shstrndx int

variable_types:
ptr
void *

variable_types: size long

variable_types: strsize long


getsectionid

decompiled_code:
char *__fastcall getsectionid(unsigned int a1)
{
__int64 v1; // rax
char *result; // rax

v1 = sectionenums;
if ( sectionenums && sectionenumcount > 0 )
{
while ( *(_DWORD *)(v1 + 8) != a1 )
{
v1 += 16LL;
if ( v1 == sectionenums + 16LL * (unsigned int)(sectionenumcount - 1) + 16 )
goto LABEL_8;
}
result = *(char **)v1;
}
else
{
LABEL_8:
result = (char *)findname((int)a1, "SHN_");
if ( !result )
{
sprintf(buf_0, "%d", a1);
result = buf_0;
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func7(unsigned int a1)
{
__int64 v1; // rax
char *result; // rax

v1 = sectionenums;
if ( sectionenums && sectionenumcount > 0 )
{
while ( *(_DWORD *)(v1 + 8) != a1 )
{
v1 += 16LL;
if ( v1 == sectionenums + 16LL * (unsigned int)(sectionenumcount - 1) + 16 )
goto LABEL_8;
}
result = *(char **)v1;
}
else
{
LABEL_8:
result = (char *)findname((int)a1, "SHN_");
if ( !result )
{
sprintf(buf_0, "%d", a1);
result = buf_0;
}
}
return result;
}

original_code:
char const *getsectionid(int ndx)
{
    static char buf[16];
    char const *name;
    int i;
    if (sectionenums)
 for (i = 0 ; i < sectionenumcount ; ++i)
     if (sectionenums[i].ndx == ndx)
  return sectionenums[i].name;
    name = findname(ndx, "SHN_");
    if (name)
 return name;
    sprintf(buf, "%d", ndx);
    return buf;
}

canonical_original_code:
char *getsectionid(int ndx)
{
    static char buf[16];
    char *name;
    int i;
    if (sectionenums)
 for (i = 0 ; i < sectionenumcount ; ++i)
     if (sectionenums[i].ndx == ndx)
  return sectionenums[i].name;
    name = findname(ndx, "SHN_");
    if (name)
 return name;
    sprintf(buf, "%d", ndx);
    return buf;
}

variable_types: ndx int

variable_types:
buf
char [16]

variable_types:
name
char *

variable_types: i int

return_type: char *


outputenum

decompiled_code:
void outputenum()
{
unsigned __int64 v0; // rsi
__int64 v1; // rbx
const char *v2; // rdi
__int64 v3; // rdx
__int64 v4; // rax
__int64 v5; // rbp
_QWORD *v6; // rax
unsigned int v7; // er8

v0 = (unsigned int)sectionenumcount;
if ( sectionenumcount )
{
v1 = 0LL;
out("enum sections");
v2 = (_BYTE *)(getstringptr + 1);
beginblock(1LL);
v3 = 0xFFFFFFFFLL;
v4 = sectionenums;
if ( sectionenumcount > 0 )
{
do
{
v5 = 16 * v1;
v6 = (_QWORD *)(16 * v1 + v4);
v7 = *((_DWORD *)v6 + 2);
v0 = *v6;
if ( v7 == (_DWORD)v3 + 1 )
{
v2 = (const char *)*v6;
out(*v6);
}
else
{
v2 = "%s = %d";
outf("%s = %d", (const char *)v0, v7);
}
v4 = sectionenums;
++v1;
v3 = *(unsigned int *)(sectionenums + v5 + 8);
}
while ( sectionenumcount > (int)v1 );
}
endblock(v2, v0, v3);
out("\n");
}
}

canonical_decompiled_code:
void func8()
{
unsigned __int64 v0; // rsi
__int64 v1; // rbx
const char *v2; // rdi
__int64 v3; // rdx
__int64 v4; // rax
__int64 v5; // rbp
_QWORD *v6; // rax
unsigned int v7; // er8

v0 = (unsigned int)sectionenumcount;
if ( sectionenumcount )
{
v1 = 0LL;
out("enum sections");
v2 = (_BYTE *)(getstringptr + 1);
beginblock(1LL);
v3 = 0xFFFFFFFFLL;
v4 = sectionenums;
if ( sectionenumcount > 0 )
{
do
{
v5 = 16 * v1;
v6 = (_QWORD *)(16 * v1 + v4);
v7 = *((_DWORD *)v6 + 2);
v0 = *v6;
if ( v7 == (_DWORD)v3 + 1 )
{
v2 = (const char *)*v6;
out(*v6);
}
else
{
v2 = "%s = %d";
outf("%s = %d", (const char *)v0, v7);
}
v4 = sectionenums;
++v1;
v3 = *(unsigned int *)(sectionenums + v5 + 8);
}
while ( sectionenumcount > (int)v1 );
}
endblock(v2, v0, v3);
out("\n");
}
}

original_code:
void outputenum(void)
{
    int prev = -1;
    int i;
    if (sectionenumcount == 0)
 return;
    out("enum sections");
    beginblock(1);
    for (i = 0 ; i < sectionenumcount ; ++i) {
 if (sectionenums[i].ndx == prev + 1)
     out(sectionenums[i].name);
 else
     outf("%s = %d", sectionenums[i].name, sectionenums[i].ndx);
 prev = sectionenums[i].ndx;
    }
    endblock();
    out("\n");
}

canonical_original_code:
void outputenum()
{
    int prev = -1;
    int i;
    if (sectionenumcount == 0)
 return;
    out("enum sections");
    beginblock(1);
    for (i = 0 ; i < sectionenumcount ; ++i) {
 if (sectionenums[i].ndx == prev + 1)
     out(sectionenums[i].name);
 else
     outf("%s = %d", sectionenums[i].name, sectionenums[i].ndx);
 prev = sectionenums[i].ndx;
    }
    endblock();
    out("\n");
}

variable_types: prev int

return_type: void


strf

decompiled_code:
__int64 strf(char *format, ...)
{
char *i; // rdi
unsigned int v3; // eax
__int64 v4; // rsi
int v5; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

for ( i = (char *)buf_7; ; i = (char *)buf_7 )
{
va_start(arg, format);
v5 = vsnprintf(i, bufsize_8, format, arg);
if ( v5 < 0 )
goto LABEL_7;
if ( bufsize_8 > v5 )
break;
if ( v5 > 0 )
{
v3 = v5 + 1;
v4 = v3;
goto LABEL_5;
}
LABEL_7:
if ( bufsize_8 )
{
v3 = 2 * bufsize_8;
v4 = (unsigned int)(2 * bufsize_8);
}
else
{
v4 = 64LL;
v3 = 64;
}
LABEL_5:
bufsize_8 = v3;
buf_7 = reallocate(buf_7, v4);
}
return buf_7;
}

canonical_decompiled_code:
__int64 func0(char *format, ...)
{
char *i; // rdi
unsigned int v3; // eax
__int64 v4; // rsi
int v5; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

for ( i = (char *)buf_7; ; i = (char *)buf_7 )
{
va_start(arg, format);
v5 = vsnprintf(i, bufsize_8, format, arg);
if ( v5 < 0 )
goto LABEL_7;
if ( bufsize_8 > v5 )
break;
if ( v5 > 0 )
{
v3 = v5 + 1;
v4 = v3;
goto LABEL_5;
}
LABEL_7:
if ( bufsize_8 )
{
v3 = 2 * bufsize_8;
v4 = (unsigned int)(2 * bufsize_8);
}
else
{
v4 = 64LL;
v3 = 64;
}
LABEL_5:
bufsize_8 = v3;
buf_7 = reallocate(buf_7, v4);
}
return buf_7;
}

original_code:
char const *strf(char const *fmt, ...)
{
    static char *buf = ((void *)0);
    static int bufsize = 0;
    va_list args;
    int n;
    for (;;) {
 __builtin_va_start(args,fmt);
 n = vsnprintf(buf, bufsize, fmt, args);
 __builtin_va_end(args);
 if (n >= 0 && n < bufsize)
     break;
 bufsize = n > 0 ? n + 1 : bufsize ? bufsize * 2 : 64;
 buf = reallocate(buf, bufsize);
    }
    return buf;
}

canonical_original_code:
char *strf(char *fmt, ...)
{
    static char *buf = ((void *)0);
    static int bufsize = 0;
    __builtin_va_list args;
    int n;
    for (;;) {
 __builtin_va_start(args,fmt);
 n = vsnprintf(buf, bufsize, fmt, args);
 __builtin_va_end(args);
 if (n >= 0 && n < bufsize)
     break;
 bufsize = n > 0 ? n + 1 : bufsize ? bufsize * 2 : 64;
 buf = reallocate(buf, bufsize);
    }
    return buf;
}

variable_types:
fmt
char *

variable_types:
buf
char *

variable_types: bufsize int

variable_types: n int


outf

decompiled_code:
__int64 outf(char *format, ...)
{
char *i; // rdi
unsigned int v3; // eax
__int64 v4; // rsi
int v5; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

for ( i = (char *)buf_5; ; i = (char *)buf_5 )
{
va_start(arg, format);
v5 = vsnprintf(i, bufsize_6, format, arg);
if ( v5 < 0 )
goto LABEL_7;
if ( bufsize_6 > v5 )
break;
if ( v5 > 0 )
{
v3 = v5 + 1;
v4 = v3;
goto LABEL_5;
}
LABEL_7:
if ( bufsize_6 )
{
v3 = 2 * bufsize_6;
v4 = (unsigned int)(2 * bufsize_6);
}
else
{
v4 = 64LL;
v3 = 64;
}
LABEL_5:
bufsize_6 = v3;
buf_5 = reallocate(buf_5, v4);
}
return out(buf_5);
}

canonical_decompiled_code:
__int64 func1(char *format, ...)
{
char *i; // rdi
unsigned int v3; // eax
__int64 v4; // rsi
int v5; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

for ( i = (char *)buf_5; ; i = (char *)buf_5 )
{
va_start(arg, format);
v5 = vsnprintf(i, bufsize_6, format, arg);
if ( v5 < 0 )
goto LABEL_7;
if ( bufsize_6 > v5 )
break;
if ( v5 > 0 )
{
v3 = v5 + 1;
v4 = v3;
goto LABEL_5;
}
LABEL_7:
if ( bufsize_6 )
{
v3 = 2 * bufsize_6;
v4 = (unsigned int)(2 * bufsize_6);
}
else
{
v4 = 64LL;
v3 = 64;
}
LABEL_5:
bufsize_6 = v3;
buf_5 = reallocate(buf_5, v4);
}
return out(buf_5);
}

original_code:
void outf(char const *fmt, ...)
{
    static char *buf = ((void *)0);
    static int bufsize = 0;
    va_list args;
    int n;
    for (;;) {
 __builtin_va_start(args,fmt);
 n = vsnprintf(buf, bufsize, fmt, args);
 __builtin_va_end(args);
 if (n >= 0 && n < bufsize)
     break;
 bufsize = n > 0 ? n + 1 : bufsize ? bufsize * 2 : 64;
 buf = reallocate(buf, bufsize);
    }
    out(buf);
}

canonical_original_code:
void outf(char *fmt, ...)
{
    static char *buf = ((void *)0);
    static int bufsize = 0;
    __builtin_va_list args;
    int n;
    for (;;) {
 __builtin_va_start(args,fmt);
 n = vsnprintf(buf, bufsize, fmt, args);
 __builtin_va_end(args);
 if (n >= 0 && n < bufsize)
     break;
 bufsize = n > 0 ? n + 1 : bufsize ? bufsize * 2 : 64;
 buf = reallocate(buf, bufsize);
    }
    out(buf);
}


strdec

decompiled_code:
char *__fastcall strdec(__int64 a1)
{
sprintf(buf_4, "%lu", a1);
return buf_4;
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1)
{
sprintf(buf_4, "%lu", a1);
return buf_4;
}

original_code:
char const *strdec(unsigned long number)
{
    static char buf[24];
    sprintf(buf, "%lu", number);
    return buf;
}

canonical_original_code:
char *strdec(unsigned long number)
{
    static char buf[24];
    sprintf(buf, "%lu", number);
    return buf;
}

variable_types:
number
unsigned long

variable_types:
buf
char [24]


strhex

decompiled_code:
char *__fastcall strhex(unsigned __int64 a1)
{
const char *v1; // rsi

if ( a1 <= 0xFFFFFFFF )
{
v1 = "0x%08lX";
if ( a1 <= 0xFFFFFF )
{
v1 = "0x%06lX";
if ( a1 <= 0xFFFF )
{
v1 = "0x%04lX";
if ( a1 <= 0xFF )
{
v1 = "0x%lX";
if ( a1 <= 0xF )
v1 = "%lu";
}
}
}
}
else
{
v1 = "0x%016lX";
if ( a1 <= 0xFFFFFFFFFFFFFFFLL )
v1 = "0x%012lX";
}
sprintf(buf_3, v1, a1);
return buf_3;
}

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

if ( a1 <= 0xFFFFFFFF )
{
v1 = "0x%08lX";
if ( a1 <= 0xFFFFFF )
{
v1 = "0x%06lX";
if ( a1 <= 0xFFFF )
{
v1 = "0x%04lX";
if ( a1 <= 0xFF )
{
v1 = "0x%lX";
if ( a1 <= 0xF )
v1 = "%lu";
}
}
}
}
else
{
v1 = "0x%016lX";
if ( a1 <= 0xFFFFFFFFFFFFFFFLL )
v1 = "0x%012lX";
}
sprintf(buf_3, v1, a1);
return buf_3;
}

original_code:
char const *strhex(unsigned long number)
{
    static char buf[20];
    if (sizeof number > 4 && number > 0xFFFFFFFFL) {
 sprintf(buf, number > ((0x7fffffffffffffffL * 2UL + 1UL) >> 4) ? "0x%016lX" : "0x%012lX",
       number);
    } else {
 sprintf(buf,number > 0x00FFFFFFL ? "0x%08lX" :
      number > 0x0000FFFFL ? "0x%06lX" :
      number > 0x000000FFL ? "0x%04lX" :
      number > 0x0000000FL ? "0x%lX" : "%lu", number);
    }
    return buf;
}

canonical_original_code:
char *strhex(unsigned long number)
{
    static char buf[20];
    if (sizeof number > 4 && number > 0xFFFFFFFFL) {
 sprintf(buf, number > ((0x7fffffffffffffffL * 2UL + 1UL) >> 4) ? "0x%016lX" : "0x%012lX",
       number);
    } else {
 sprintf(buf,number > 0x00FFFFFFL ? "0x%08lX" :
      number > 0x0000FFFFL ? "0x%06lX" :
      number > 0x000000FFL ? "0x%04lX" :
      number > 0x0000000FL ? "0x%lX" : "%lu", number);
    }
    return buf;
}

variable_types:
buf
char [20]


strchar

decompiled_code:
char *__fastcall strchar(unsigned __int8 a1)
{
__int64 v1; // rbp
char *result; // rax

v1 = a1;
switch ( a1 )
{
case 0u:
result = "0";
break;
case 1u:
case 2u:
case 3u:
case 4u:
case 5u:
case 6u:
case 0xEu:
case 0xFu:
case 0x10u:
case 0x11u:
case 0x12u:
case 0x13u:
case 0x14u:
case 0x15u:
case 0x16u:
case 0x17u:
case 0x18u:
case 0x19u:
case 0x1Au:
case 0x1Bu:
case 0x1Cu:
case 0x1Du:
case 0x1Eu:
case 0x1Fu:
case 0x20u:
case 0x21u:
case 0x22u:
case 0x23u:
case 0x24u:
case 0x25u:
case 0x26u:
goto LABEL_4;
case 7u:
result = "'\\a'";
break;
case 8u:
result = "'\\b'";
break;
case 9u:
result = "'\\t'";
break;
case 0xAu:
result = "'\\n'";
break;
case 0xBu:
result = "'\\v'";
break;
case 0xCu:
result = "'\\f'";
break;
case 0xDu:
result = "'\\r'";
break;
case 0x27u:
result = "'\\''";
break;
default:
result = "'\\\\'";
v1 = a1;
if ( a1 != 92 )
{
LABEL_4:
if ( ((*_ctype_b_loc())[v1] & 0x4000) != 0 || a1 == 32 )
{
buf_2 = 39;
word_662 = 39;
result = &buf_2;
byte_661 = a1;
}
else
{
sprintf(&buf_2, "0x%02X", a1);
result = &buf_2;
}
}
break;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(unsigned __int8 a1)
{
__int64 v1; // rbp
char *result; // rax

v1 = a1;
switch ( a1 )
{
case 0u:
result = "0";
break;
case 1u:
case 2u:
case 3u:
case 4u:
case 5u:
case 6u:
case 0xEu:
case 0xFu:
case 0x10u:
case 0x11u:
case 0x12u:
case 0x13u:
case 0x14u:
case 0x15u:
case 0x16u:
case 0x17u:
case 0x18u:
case 0x19u:
case 0x1Au:
case 0x1Bu:
case 0x1Cu:
case 0x1Du:
case 0x1Eu:
case 0x1Fu:
case 0x20u:
case 0x21u:
case 0x22u:
case 0x23u:
case 0x24u:
case 0x25u:
case 0x26u:
goto LABEL_4;
case 7u:
result = "'\\a'";
break;
case 8u:
result = "'\\b'";
break;
case 9u:
result = "'\\t'";
break;
case 0xAu:
result = "'\\n'";
break;
case 0xBu:
result = "'\\v'";
break;
case 0xCu:
result = "'\\f'";
break;
case 0xDu:
result = "'\\r'";
break;
case 0x27u:
result = "'\\''";
break;
default:
result = "'\\\\'";
v1 = a1;
if ( a1 != 92 )
{
LABEL_4:
if ( ((*_ctype_b_loc())[v1] & 0x4000) != 0 || a1 == 32 )
{
buf_2 = 39;
word_662 = 39;
result = &buf_2;
byte_661 = a1;
}
else
{
sprintf(&buf_2, "0x%02X", a1);
result = &buf_2;
}
}
break;
}
return result;
}

original_code:
char const *strchar(unsigned char ch)
{
    static char buf[8];
    switch (ch) {
      case '\a': return "'\\a'"; case '\t': return "'\\t'";
      case '\b': return "'\\b'"; case '\v': return "'\\v'";
      case '\f': return "'\\f'"; case '\'': return "'\\''";
      case '\n': return "'\\n'"; case '\\': return "'\\\\'";
      case '\r': return "'\\r'"; case '\0': return "0";
    }
    if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISprint) || ch == ' ') {
 buf[0] = '\'';
 buf[1] = ch;
 buf[2] = '\'';
 buf[3] = '\0';
    } else {
 sprintf(buf, "0x%02X", (unsigned char)ch);
    }
    return buf;
}

canonical_original_code:
char *strchar(unsigned char ch)
{
    static char buf[8];
    switch (ch) {
      case '\a': return "'\\a'"; case '\t': return "'\\t'";
      case '\b': return "'\\b'"; case '\v': return "'\\v'";
      case '\f': return "'\\f'"; case '\'': return "'\\''";
      case '\n': return "'\\n'"; case '\\': return "'\\\\'";
      case '\r': return "'\\r'"; case '\0': return "0";
    }
    if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISprint) || ch == ' ') {
 buf[0] = '\'';
 buf[1] = ch;
 buf[2] = '\'';
 buf[3] = '\0';
    } else {
 sprintf(buf, "0x%02X", (unsigned char)ch);
    }
    return buf;
}

variable_types:
ch
unsigned char

variable_types:
buf
char [8]


straddress

decompiled_code:
char *__fastcall straddress(unsigned __int64 a1)
{
const char *v1; // r12
const char *v3; // rax
unsigned int v4; // eax
const char *v5; // rax
unsigned int v6; // eax
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = "0";
if ( !a1 )
return (char *)v1;
v3 = (const char *)getbaseaddress(a1, v7);
v1 = v3;
if ( !v3 )
return strhex(a1);
if ( !*v3 )
{
v6 = getindexfromoffset(a1);
return (char *)getoffsetstr(a1, v6);
}
if ( v7[0] == a1 )
return (char *)v1;
v4 = getindexfromoffset(a1 - v7[0]);
v5 = (const char *)getoffsetstr(a1 - v7[0], v4);
return (char *)strf("%s + %s", v1, v5);
}

canonical_decompiled_code:
char *__fastcall func5(unsigned __int64 a1)
{
const char *v1; // r12
const char *v3; // rax
unsigned int v4; // eax
const char *v5; // rax
unsigned int v6; // eax
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = "0";
if ( !a1 )
return (char *)v1;
v3 = (const char *)getbaseaddress(a1, v7);
v1 = v3;
if ( !v3 )
return func3(a1);
if ( !*v3 )
{
v6 = getindexfromoffset(a1);
return (char *)getoffsetstr(a1, v6);
}
if ( v7[0] == a1 )
return (char *)v1;
v4 = getindexfromoffset(a1 - v7[0]);
v5 = (const char *)getoffsetstr(a1 - v7[0], v4);
return (char *)func0("%s + %s", v1, v5);
}

original_code:
char const *straddress(long address)
{
    char const *name, *offset;
    long base;
    if (address == 0)
 return "0";
    name = getbaseaddress(address, &base);
    if (!name)
 return strhex(address);
    if (!*name)
 return getoffsetstr(address, getindexfromoffset(address));
    if (address == base)
 return name;
    offset = getoffsetstr(address - base, getindexfromoffset(address - base));
    return strf("%s + %s", name, offset);
}

canonical_original_code:
char *straddress(long address)
{
    char *name; char *offset;
    long base;
    if (address == 0)
 return "0";
    name = getbaseaddress(address, &base);
    if (!name)
 return strhex(address);
    if (!*name)
 return getoffsetstr(address, getindexfromoffset(address));
    if (address == base)
 return name;
    offset = getoffsetstr(address - base, getindexfromoffset(address - base));
    return strf("%s + %s", name, offset);
}

variable_types: address long

variable_types:
offset
char *

variable_types: base long


strdefbyte

decompiled_code:
char *__fastcall strdefbyte(unsigned __int8 a1)
{
char *result; // rax

result = (char *)findname(a1);
if ( !result )
{
result = "0";
if ( a1 )
result = strchar(a1);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func6(unsigned __int8 a1)
{
char *result; // rax

result = (char *)findname(a1);
if ( !result )
{
result = "0";
if ( a1 )
result = func4(a1);
}
return result;
}

original_code:
char const *strdefbyte(unsigned char byte, char const *prefix)
{
    char const *name;
    name = findname(byte, prefix);
    if (name)
 return name;
    else if (byte == 0)
 return "0";
    else
 return strchar(byte);
}

canonical_original_code:
char *strdefbyte(unsigned char byte, char *prefix)
{
    char *name;
    name = findname(byte, prefix);
    if (name)
 return name;
    else if (byte == 0)
 return "0";
    else
 return strchar(byte);
}

variable_types:
byte
unsigned char

variable_types:
prefix
char *


strdefint

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

result = (char *)findname(a1);
if ( !result )
result = strdec(a1);
return result;
}

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

result = (char *)findname(a1);
if ( !result )
result = func2(a1);
return result;
}

original_code:
char const *strdefint(unsigned long number, char const *prefix)
{
    char const *name;
    name = findname(number, prefix);
    if (name)
 return name;
    return strdec(number);
}

canonical_original_code:
char *strdefint(unsigned long number, char *prefix)
{
    char *name;
    name = findname(number, prefix);
    if (name)
 return name;
    return strdec(number);
}

variable_types:
name
char *


strdefflags

decompiled_code:
const char *__fastcall strdefflags(unsigned __int64 a1, __int64 a2)
{
const char *result; // rax
__int64 i; // rbx
unsigned __int64 v5; // rdi
const char *v6; // rax
const char *v7; // r12
int v8; // eax
int v9; // esi
__int64 v10; // rdi
int v11; // eax
int v12; // er13
char *v13; // rax
unsigned __int64 v14[6]; // [rsp+8h] [rbp-30h] BYREF

result = "0";
v14[0] = a1;
if ( a1 )
{
for ( i = 0LL; ; i = v12 )
{
v6 = (const char *)findflag(v14, a2);
v7 = v6;
if ( !v6 )
break;
v8 = strlen(v6);
v9 = bufsize_1;
v10 = buf_0;
v11 = v8 + 3;
v12 = v11 + i;
if ( v11 + (int)i > bufsize_1 )
{
bufsize_1 += v11;
buf_0 = reallocate(buf_0, (unsigned int)(v11 + v9 + 32));
v10 = buf_0;
}
sprintf((char *)(i + v10), " | %s", v7);
v5 = v14[0];
if ( !v14[0] )
{
if ( v12 )
return (const char *)(buf_0 + 3);
return strhex(v5);
}
}
v5 = v14[0];
if ( !(_DWORD)i )
return strhex(v5);
if ( !v14[0] )
return (const char *)(buf_0 + 3);
v13 = strhex(v14[0]);
sprintf((char *)(buf_0 + (int)i), " | %s", v13);
result = (const char *)(buf_0 + 3);
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(unsigned __int64 a1, __int64 a2)
{
const char *result; // rax
__int64 i; // rbx
unsigned __int64 v5; // rdi
const char *v6; // rax
const char *v7; // r12
int v8; // eax
int v9; // esi
__int64 v10; // rdi
int v11; // eax
int v12; // er13
char *v13; // rax
unsigned __int64 v14[6]; // [rsp+8h] [rbp-30h] BYREF

result = "0";
v14[0] = a1;
if ( a1 )
{
for ( i = 0LL; ; i = v12 )
{
v6 = (const char *)findflag(v14, a2);
v7 = v6;
if ( !v6 )
break;
v8 = strlen(v6);
v9 = bufsize_1;
v10 = buf_0;
v11 = v8 + 3;
v12 = v11 + i;
if ( v11 + (int)i > bufsize_1 )
{
bufsize_1 += v11;
buf_0 = reallocate(buf_0, (unsigned int)(v11 + v9 + 32));
v10 = buf_0;
}
sprintf((char *)(i + v10), " | %s", v7);
v5 = v14[0];
if ( !v14[0] )
{
if ( v12 )
return (const char *)(buf_0 + 3);
return func3(v5);
}
}
v5 = v14[0];
if ( !(_DWORD)i )
return func3(v5);
if ( !v14[0] )
return (const char *)(buf_0 + 3);
v13 = func3(v14[0]);
sprintf((char *)(buf_0 + (int)i), " | %s", v13);
result = (const char *)(buf_0 + 3);
}
return result;
}

original_code:
char const *strdefflags(unsigned long flags, char const *prefix)
{
    static char *buf = ((void *)0);
    static int bufsize = 0;
    char const *name;
    int size, n;
    if (flags == 0)
 return "0";
    size = 0;
    while (flags) {
 name = findflag(&flags, prefix);
 if (!name)
     break;
 n = 3 + strlen(name);
 if (size + n > bufsize) {
     bufsize += n;
     buf = reallocate(buf, bufsize + 32);
 }
 sprintf(buf + size, " | %s", name);
 size += n;
    }
    if (size == 0)
 return strhex(flags);
    if (flags)
 sprintf(buf + size, " | %s", strhex(flags));
    return buf + 3;
}

canonical_original_code:
char *strdefflags(unsigned long flags, char *prefix)
{
    static char *buf = ((void *)0);
    static int bufsize = 0;
    char *name;
    int size; int n;
    if (flags == 0)
 return "0";
    size = 0;
    while (flags) {
 name = findflag(&flags, prefix);
 if (!name)
     break;
 n = 3 + strlen(name);
 if (size + n > bufsize) {
     bufsize += n;
     buf = reallocate(buf, bufsize + 32);
 }
 sprintf(buf + size, " | %s", name);
 size += n;
    }
    if (size == 0)
 return strhex(flags);
    if (flags)
 sprintf(buf + size, " | %s", strhex(flags));
    return buf + 3;
}

variable_types:
flags
unsigned long

variable_types: size int


relnew

original_code:
static _Bool relnew(elfpart *part)
{
    part->shtype = 9;
    part->shname = ".rel";
    part->entsize = sizeof(Elf64_Rel);
    return 1;
}

canonical_original_code:
static _Bool relnew(struct elfpart *part)
{
    part->shtype = 9;
    part->shname = ".rel";
    part->entsize = sizeof(Elf64_Rel);
    return 1;
}

variable_types: struct elfpart { _Bool (*new)(struct elfpart *); _Bool (*init)(struct elfpart *, struct blueprint { int filetype; int partcount; struct elfpart *parts; } *); _Bool (*fill)(struct elfpart *, struct blueprint *); _Bool (*complete)(struct elfpart *, struct blueprint *); int shtype; int entsize; int count; int size; void *part; char *shname; unsigned long offset; long addr; struct elfpart *link; int info; short flags; _Bool done; } *

user_defined_types: struct elfpart { _Bool (*new)(struct elfpart *); _Bool (*init)(struct elfpart *, struct blueprint *); _Bool (*fill)(struct elfpart *, struct blueprint *); _Bool (*complete)(struct elfpart *, struct blueprint *); int shtype; int entsize; int count; int size; void *part; char *shname; unsigned long offset; long addr; struct elfpart *link; int info; short flags; _Bool done; }


relanew

original_code:
static _Bool relanew(elfpart *part)
{
    part->shtype = 4;
    part->shname = ".rela";
    part->entsize = sizeof(Elf64_Rela);
    return 1;
}

canonical_original_code:
static _Bool relanew(struct elfpart *part)
{
    part->shtype = 4;
    part->shname = ".rela";
    part->entsize = sizeof(Elf64_Rela);
    return 1;
}

user_defined_types: struct blueprint { int filetype; int partcount; struct elfpart *parts; }


relcomplete

original_code:
static _Bool relcomplete(elfpart *part, blueprint const *bp)
{
    Elf64_Rel *rel;
    int sym, i;
    if (!part->link->done)
 return 1;
    for (i = 0, rel = part->part ; i < part->count ; ++i, ++rel) {
 sym = ((rel->r_info) >> 32);
 if (sym < 0)
     sym = part->link->info - sym - 1;
 rel->r_info = ((((Elf64_Xword) (sym)) << 32) + (((rel->r_info) & 0xffffffff)));
    }
    part->done = 1;
    return 1;
    (void)bp;
}

canonical_original_code:
static _Bool relcomplete(struct elfpart *part, struct blueprint *bp)
{
    struct Elf64_Rel *rel;
    int sym; int i;
    if (!part->link->done)
 return 1;
    for (i = 0, rel = part->part ; i < part->count ; ++i, ++rel) {
 sym = ((rel->r_info) >> 32);
 if (sym < 0)
     sym = part->link->info - sym - 1;
 rel->r_info = ((((Elf64_Xword) (sym)) << 32) + (((rel->r_info) & 0xffffffff)));
    }
    part->done = 1;
    return 1;
    (void)bp;
}

variable_types:
bp
struct blueprint *

variable_types: struct Elf64_Rel { unsigned long r_offset; unsigned long r_info; } *

variable_types: sym int

variable_types: i int


relacomplete

original_code:
static _Bool relacomplete(elfpart *part, blueprint const *bp)
{
    Elf64_Rela *rela;
    int sym, i;
    if (!part->link->done)
 return 1;
    for (i = 0, rela = part->part ; i < part->count ; ++i, ++rela) {
 sym = ((rela->r_info) >> 32);
 if (sym < 0)
     sym = part->link->info - sym - 1;
 rela->r_info = ((((Elf64_Xword) (sym)) << 32) + (((rela->r_info) & 0xffffffff)));
    }
    part->done = 1;
    return 1;
    (void)bp;
}

canonical_original_code:
static _Bool relacomplete(struct elfpart *part, struct blueprint *bp)
{
    struct Elf64_Rela *rela;
    int sym; int i;
    if (!part->link->done)
 return 1;
    for (i = 0, rela = part->part ; i < part->count ; ++i, ++rela) {
 sym = ((rela->r_info) >> 32);
 if (sym < 0)
     sym = part->link->info - sym - 1;
 rela->r_info = ((((Elf64_Xword) (sym)) << 32) + (((rela->r_info) & 0xffffffff)));
    }
    part->done = 1;
    return 1;
    (void)bp;
}

variable_types: struct Elf64_Rela { unsigned long r_offset; unsigned long r_info; long r_addend; } *


addentrytorel

decompiled_code:
__int64 __fastcall addentrytorel(__int64 a1, unsigned int a2, unsigned int a3, int a4)
{
__int64 v4; // r13
__int64 v5; // rbp
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // rbp
__int64 result; // rax
_QWORD *v10; // rdx
_QWORD *v11; // rdx

v4 = a4;
v5 = a3;
if ( !(unsigned int)validate(
(*(_DWORD *)(a1 + 32) == 4) | (unsigned __int8)(*(_DWORD *)(a1 + 32) == 9),
"not a relocation section")
|| !resizepart(a1, (unsigned int)(*(_DWORD *)(a1 + 44) + *(_DWORD *)(a1 + 36))) )
{
return 0xFFFFFFFFLL;
}
v6 = *(int *)(a1 + 40);
v7 = *(_QWORD *)(a1 + 48);
v8 = v4 + (v5 << 32);
result = v6;
if ( *(_DWORD *)(a1 + 32) == 9 )
{
v11 = (_QWORD *)(v7 + 16 * v6);
*v11 = a2;
v11[1] = v8;
}
else
{
v10 = (_QWORD *)(v7 + 24 * v6);
*v10 = a2;
v10[1] = v8;
v10[2] = 0LL;
}
*(_DWORD *)(a1 + 40) = result + 1;
return result;
}

original_code:
int addentrytorel(elfpart *part, unsigned int offset, int sym, int type)
{
    if (!_validate(part->shtype == 9 || part->shtype == 4,
                   "not a relocation section"))
        return -1;
    if (!_resizepart(part, part->size + part->entsize))
        return -1;
    if (part->shtype == 9) {
        Elf64_Rel *rel = part->part;
        rel += part->count;
        rel->r_offset = offset;
        rel->r_info = ((((Elf64_Xword) (sym)) << 32) + (type));
    } else {
        Elf64_Rela *rela = part->part;
        rela += part->count;
        rela->r_offset = offset;
        rela->r_info = ((((Elf64_Xword) (sym)) << 32) + (type));
        rela->r_addend = 0;
    }
    return part->count++;
}

canonical_original_code:
int addentrytorel(struct elfpart *part, unsigned int offset, int sym, int type)
{
    if (!_validate(part->shtype == 9 || part->shtype == 4,
                   "not a relocation section"))
        return -1;
    if (!_resizepart(part, part->size + part->entsize))
        return -1;
    if (part->shtype == 9) {
        struct Elf64_Rel *rel = part->part;
        rel += part->count;
        rel->r_offset = offset;
        rel->r_info = ((((Elf64_Xword) (sym)) << 32) + (type));
    } else {
        struct Elf64_Rela *rela = part->part;
        rela += part->count;
        rela->r_offset = offset;
        rela->r_info = ((((Elf64_Xword) (sym)) << 32) + (type));
        rela->r_addend = 0;
    }
    return part->count++;
}

variable_types:
offset
unsigned int

variable_types: type int

return_type: int

user_defined_types: struct Elf64_Rel { unsigned long r_offset; unsigned long r_info; }

user_defined_types: struct Elf64_Rela { unsigned long r_offset; unsigned long r_info; long r_addend; }


addsymbolrel

decompiled_code:
__int64 __fastcall addsymbolrel(__int64 a1, unsigned int a2, int a3, __int64 a4, unsigned int a5, unsigned int a6, unsigned int a7)
{
unsigned int v10; // edx

if ( !(unsigned int)validate(
(*(_DWORD *)(a1 + 32) == 4) | (unsigned __int8)(*(_DWORD *)(a1 + 32) == 9),
"not a relocation section") )
return 0xFFFFFFFFLL;
if ( !a4 || (v10 = getsymindex(*(_QWORD *)(a1 + 80), a4)) == 0 )
v10 = addtosymtab(*(_QWORD *)(a1 + 80), a4, a5, a6, a7);
return addentrytorel(a1, a2, v10, a3);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2, int a3, __int64 a4, unsigned int a5, unsigned int a6, unsigned int a7)
{
unsigned int v10; // edx

if ( !(unsigned int)validate(
(*(_DWORD *)(a1 + 32) == 4) | (unsigned __int8)(*(_DWORD *)(a1 + 32) == 9),
"not a relocation section") )
return 0xFFFFFFFFLL;
if ( !a4 || (v10 = getsymindex(*(_QWORD *)(a1 + 80), a4)) == 0 )
v10 = addtosymtab(*(_QWORD *)(a1 + 80), a4, a5, a6, a7);
return func5(a1, a2, v10, a3);
}

original_code:
int addsymbolrel(elfpart *part, unsigned int offset, int reltype,
                 char const *sym, int symbind, int symtype, int shndx)
{
    int n;
    if (!_validate(part->shtype == 9 || part->shtype == 4,
                   "not a relocation section"))
        return -1;
    if (!sym || (n = getsymindex(part->link, sym)) == 0)
 n = addtosymtab(part->link, sym, symbind, symtype, shndx);
    return addentrytorel(part, offset, n, reltype);
}

canonical_original_code:
int addsymbolrel(struct elfpart *part, unsigned int offset, int reltype, char *sym, int symbind, int symtype, int shndx)
{
    int n;
    if (!_validate(part->shtype == 9 || part->shtype == 4,
                   "not a relocation section"))
        return -1;
    if (!sym || (n = getsymindex(part->link, sym)) == 0)
 n = addtosymtab(part->link, sym, symbind, symtype, shndx);
    return addentrytorel(part, offset, n, reltype);
}

variable_types: reltype int

variable_types:
sym
char *

variable_types: symbind int

variable_types: symtype int

variable_types: shndx int

variable_types: n int


init

original_code:
static _Bool init(elfpart *part, blueprint const *bp)
{
    int n;
    if (!_validate(bp->parts[part->info].shtype == 1,
                   ".rel requires a valid info setting"))
        return 0;
    if (bp->parts[part->info].shname) {
        char const *destname = bp->parts[part->info].shname;
        int len1 = strlen(part->shname);
        int len2 = strlen(destname);
        char *str = malloc(len1 + len2 + 1);
        if (!_validate(str, "out of memory"))
            return 0;
 memcpy(str, part->shname, len1);
        memcpy(str + len1, destname, len2 + 1);
 part->shname = str;
    }
    n = part->info;
    part->info = 1;
    while (n--)
        if (((&bp->parts[n])->shtype > 0))
     ++part->info;
    part->done = 1;
    return 1;
}

canonical_original_code:
static _Bool init(struct elfpart *part, struct blueprint *bp)
{
    int n;
    if (!_validate(bp->parts[part->info].shtype == 1,
                   ".rel requires a valid info setting"))
        return 0;
    if (bp->parts[part->info].shname) {
        char *destname = bp->parts[part->info].shname;
        int len1 = strlen(part->shname);
        int len2 = strlen(destname);
        char *str = malloc(len1 + len2 + 1);
        if (!_validate(str, "out of memory"))
            return 0;
 memcpy(str, part->shname, len1);
        memcpy(str + len1, destname, len2 + 1);
 part->shname = str;
    }
    n = part->info;
    part->info = 1;
    while (n--)
        if (((&bp->parts[n])->shtype > 0))
     ++part->info;
    part->done = 1;
    return 1;
}

variable_types:
destname
char *

variable_types:
str
char *


setnames

decompiled_code:
__int64 __fastcall setnames(int *a1, int a2, const char *a3)
{
__int64 result; // rax
char *v4; // rbp
char *v5; // rax
int v6; // ebx
int v7; // er14
_BYTE *v8; // rbx
const unsigned __int16 **v9; // rax
const unsigned __int16 *v10; // rcx
__int64 i; // rax
int v12; // eax

if ( !a3 )
return 0LL;
result = 0LL;
v4 = (char *)a3;
if ( *a3 )
{
v5 = strrchr(a3, 47);
if ( v5 )
v4 = v5 + 1;
v6 = strlen(v4);
v7 = v6;
if ( v6 )
{
if ( !srcfilename )
srcfilename = (__int64)strallocate(v4);
if ( v6 > 2 && *(_WORD *)&v4[v6 - 2] == 25134 )
{
v7 = v6 - 2;
}
else if ( v6 > 3 && !memcmp(&v4[v6 - 3], ".bf", 3uLL) )
{
v7 = v6 - 3;
}
if ( !outfilename )
{
v12 = *a1;
if ( *a1 == 1 )
{
outfilename = (char *)allocate(v7 + 3);
sprintf(outfilename, "%*.*s.o", v7, v7, v4);
}
else if ( v12 == 3 )
{
outfilename = (char *)allocate(v7 + 7);
sprintf(outfilename, "lib%*.*s.so", v7, v7, v4);
}
else if ( v12 == 2 && v4[v7] )
{
outfilename = (char *)allocate(v7 + 1);
*((_BYTE *)memcpy(outfilename, v4, v7) + v7) = 0;
}
else
{
outfilename = (char *)strallocate("a.out");
}
}
if ( !functionname )
{
if ( a2 == 1 )
{
functionname = (__int64)strallocate("_start");
}
else
{
v8 = allocate(v7 + 1);
functionname = (__int64)v8;
memcpy(v8, v4, v7);
v8[v7] = 0;
v9 = _ctype_b_loc();
v10 = *v9;
if ( ((*v9)[(char)*v8] & 0x400) == 0 )
*v8 = 95;
for ( i = 1LL; v7 > (int)i; ++i )
{
if ( (v10[(char)v8[i]] & 8) == 0 )
v8[i] = 95;
}
}
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1, int a2, const char *a3)
{
__int64 result; // rax
char *v4; // rbp
char *v5; // rax
int v6; // ebx
int v7; // er14
_BYTE *v8; // rbx
const unsigned __int16 **v9; // rax
const unsigned __int16 *v10; // rcx
__int64 i; // rax
int v12; // eax

if ( !a3 )
return 0LL;
result = 0LL;
v4 = (char *)a3;
if ( *a3 )
{
v5 = strrchr(a3, 47);
if ( v5 )
v4 = v5 + 1;
v6 = strlen(v4);
v7 = v6;
if ( v6 )
{
if ( !srcfilename )
srcfilename = (__int64)func1(v4);
if ( v6 > 2 && *(_WORD *)&v4[v6 - 2] == 25134 )
{
v7 = v6 - 2;
}
else if ( v6 > 3 && !memcmp(&v4[v6 - 3], ".bf", 3uLL) )
{
v7 = v6 - 3;
}
if ( !outfilename )
{
v12 = *a1;
if ( *a1 == 1 )
{
outfilename = (char *)func0(v7 + 3);
sprintf(outfilename, "%*.*s.o", v7, v7, v4);
}
else if ( v12 == 3 )
{
outfilename = (char *)func0(v7 + 7);
sprintf(outfilename, "lib%*.*s.so", v7, v7, v4);
}
else if ( v12 == 2 && v4[v7] )
{
outfilename = (char *)func0(v7 + 1);
*((_BYTE *)memcpy(outfilename, v4, v7) + v7) = 0;
}
else
{
outfilename = (char *)func1("a.out");
}
}
if ( !functionname )
{
if ( a2 == 1 )
{
functionname = (__int64)func1("_start");
}
else
{
v8 = func0(v7 + 1);
functionname = (__int64)v8;
memcpy(v8, v4, v7);
v8[v7] = 0;
v9 = _ctype_b_loc();
v10 = *v9;
if ( ((*v9)[(char)*v8] & 0x400) == 0 )
*v8 = 95;
for ( i = 1LL; v7 > (int)i; ++i )
{
if ( (v10[(char)v8[i]] & 8) == 0 )
v8[i] = 95;
}
}
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
static _Bool setnames(blueprint const *bp, int codetype, char const *filename)
{
    char const *base;
    int len, i;
    if (!filename || !filename[0])
        return 0;
    base = strrchr(filename, '/');
    if (base)
        ++base;
    else
        base = filename;
    len = strlen(base);
    if (!len)
        return 0;
    if (!srcfilename)
        srcfilename = strallocate(base);
    if (len > 2 && !memcmp(base + len - 2, ".b", 2))
 len -= 2;
    else if (len > 3 && !memcmp(base + len - 3, ".bf", 3))
        len -= 3;
    if (!outfilename) {
 if (bp->filetype == 1) {
            outfilename = allocate(len + 3);
            sprintf(outfilename, "%*.*s.o", len, len, base);
 } else if (bp->filetype == 3) {
            outfilename = allocate(len + 7);
     sprintf(outfilename, "lib%*.*s.so", len, len, base);
        } else if (bp->filetype == 2 && base[len] != '\0') {
            outfilename = allocate(len + 1);
            memcpy(outfilename, base, len);
            outfilename[len] = '\0';
        } else {
            outfilename = strallocate("a.out");
        }
    }
    if (!functionname) {
 if (codetype == CODE_EXEC) {
            functionname = strallocate("_start");
 } else {
            functionname = allocate(len + 1);
            memcpy(functionname, base, len);
            functionname[len] = '\0';
            if (!((*__ctype_b_loc ())[(int) ((functionname[0]))] & (unsigned short int) _ISalpha))
                functionname[0] = '_';
     for (i = 1 ; i < len ; ++i)
  if (!((*__ctype_b_loc ())[(int) ((functionname[i]))] & (unsigned short int) _ISalnum))
      functionname[i] = '_';
 }
    }
    return 1;
}

canonical_original_code:
static _Bool setnames(struct blueprint *bp, int codetype, char *filename)
{
    char *base;
    int len; int i;
    if (!filename || !filename[0])
        return 0;
    base = strrchr(filename, '/');
    if (base)
        ++base;
    else
        base = filename;
    len = strlen(base);
    if (!len)
        return 0;
    if (!srcfilename)
        srcfilename = strallocate(base);
    if (len > 2 && !memcmp(base + len - 2, ".b", 2))
 len -= 2;
    else if (len > 3 && !memcmp(base + len - 3, ".bf", 3))
        len -= 3;
    if (!outfilename) {
 if (bp->filetype == 1) {
            outfilename = allocate(len + 3);
            sprintf(outfilename, "%*.*s.o", len, len, base);
 } else if (bp->filetype == 3) {
            outfilename = allocate(len + 7);
     sprintf(outfilename, "lib%*.*s.so", len, len, base);
        } else if (bp->filetype == 2 && base[len] != '\0') {
            outfilename = allocate(len + 1);
            memcpy(outfilename, base, len);
            outfilename[len] = '\0';
        } else {
            outfilename = strallocate("a.out");
        }
    }
    if (!functionname) {
 if (codetype == CODE_EXEC) {
            functionname = strallocate("_start");
 } else {
            functionname = allocate(len + 1);
            memcpy(functionname, base, len);
            functionname[len] = '\0';
            if (!((*__ctype_b_loc ())[(int) ((functionname[0]))] & (unsigned short) _ISalpha))
                functionname[0] = '_';
     for (i = 1 ; i < len ; ++i)
  if (!((*__ctype_b_loc ())[(int) ((functionname[i]))] & (unsigned short) _ISalnum))
      functionname[i] = '_';
 }
    }
    return 1;
}

variable_types: struct blueprint { int filetype; int partcount; struct elfpart { _Bool (*new)(struct elfpart *); _Bool (*init)(struct elfpart *, struct blueprint *); _Bool (*fill)(struct elfpart *, struct blueprint *); _Bool (*complete)(struct elfpart *, struct blueprint *); int shtype; int entsize; int count; int size; void *part; char *shname; unsigned long offset; long addr; struct elfpart *link; int info; short flags; _Bool done; } *parts; } *

variable_types: codetype int

variable_types:
filename
char *

variable_types:
base
char *

variable_types: len int


populateparts

decompiled_code:
__int64 __fastcall populateparts(__int64 a1, unsigned int a2, char *a3, unsigned int a4)
{
__int64 v8; // r13
__int64 result; // rax
__int64 v10; // rax
void *v11; // rax
_DWORD *v12; // rsi
__int64 v13; // rcx
_DWORD *v14; // rdi

if ( addextras )
{
v8 = *(_QWORD *)(a1 + 8);
if ( *(_DWORD *)(v8 + 1088) )
{
*(_QWORD *)(v8 + 1112) = ".comment";
*(_DWORD *)(v8 + 1100) = 28;
v11 = allocate(0x1CuLL);
v12 = &comment_0;
v13 = 7LL;
*(_QWORD *)(v8 + 1104) = v11;
v14 = *(_DWORD **)(*(_QWORD *)(a1 + 8) + 1104LL);
while ( v13 )
{
*v14++ = *v12++;
--v13;
}
v8 = *(_QWORD *)(a1 + 8);
}
if ( *(_DWORD *)(v8 + 1184) )
addtosymtab(v8 + 1152, srcfilename, 0LL, 4LL, 65521LL);
}
else if ( *(_DWORD *)a1 == 2 )
{
v10 = *(_QWORD *)(a1 + 8);
*(_DWORD *)(v10 + 1376) = 0;
*(_DWORD *)(v10 + 992) = 0;
}
thefilename = a3;
result = compilebrainfuck(a3, a1, a2, functionname, a4);
if ( (_BYTE)result )
thefilename = 0LL;
return result;
}

original_code:
static _Bool populateparts(blueprint *bp, int codetype,
                          char const *filename, int compressed)
{
    static char const comment[] = "\0ELF Brainfuck Compiler 2.0";
    if (addextras) {
        if (((&bp->parts[P_COMMENT])->shtype != 0)) {
     bp->parts[P_COMMENT].shname = ".comment";
     bp->parts[P_COMMENT].size = sizeof comment;
            bp->parts[P_COMMENT].part = allocate(sizeof comment);
            memcpy(bp->parts[P_COMMENT].part, comment, sizeof comment);
 }
        if (((&bp->parts[P_SYMTAB])->shtype != 0))
     addtosymtab(&bp->parts[P_SYMTAB], srcfilename,
   0, 4, 0xfff1);
    } else {
        if (bp->filetype == 2) {
            ((&bp->parts[P_SHDRTAB])->shtype = 0);
            ((&bp->parts[P_SHSTRTAB])->shtype = 0);
        }
    }
    thefilename = filename;
    if (!compilebrainfuck(filename, bp, codetype, functionname, compressed))
 return 0;
    thefilename = ((void *)0);
    return 1;
}

canonical_original_code:
static _Bool populateparts(struct blueprint *bp, int codetype, char *filename, int compressed)
{
    static char comment[] = "\0ELF Brainfuck Compiler 2.0";
    if (addextras) {
        if (((&bp->parts[P_COMMENT])->shtype != 0)) {
     bp->parts[P_COMMENT].shname = ".comment";
     bp->parts[P_COMMENT].size = sizeof comment;
            bp->parts[P_COMMENT].part = allocate(sizeof comment);
            memcpy(bp->parts[P_COMMENT].part, comment, sizeof comment);
 }
        if (((&bp->parts[P_SYMTAB])->shtype != 0))
     addtosymtab(&bp->parts[P_SYMTAB], srcfilename,
   0, 4, 0xfff1);
    } else {
        if (bp->filetype == 2) {
            ((&bp->parts[P_SHDRTAB])->shtype = 0);
            ((&bp->parts[P_SHSTRTAB])->shtype = 0);
        }
    }
    thefilename = filename;
    if (!compilebrainfuck(filename, bp, codetype, functionname, compressed))
 return 0;
    thefilename = ((void *)0);
    return 1;
}

variable_types: compressed int

variable_types:
comment
char []


compile

decompiled_code:
__int64 __fastcall compile(int *a1, unsigned int a2, char *a3, unsigned int a4)
{
unsigned int v6; // er12
const char *v8; // rbp
__mode_t v9; // esi
struct stat v10; // [rsp+0h] [rbp-B8h] BYREF

enforcevalidation(1LL);
newparts(a1);
connectparts((__int64)a1);
initparts(a1);
if ( !(unsigned __int8)setnames(a1, a2, a3) )
return 0;
if ( !(unsigned __int8)populateparts((__int64)a1, a2, a3, a4) )
return 0;
fillparts(a1);
measureparts(a1);
if ( !(unsigned __int8)createfixups(a1, a2, functionname) )
return 0;
completeparts(a1);
thefilename = outfilename;
v6 = outputelf(a1);
if ( !(_BYTE)v6 )
err(0LL);
v8 = outfilename;
if ( stat(outfilename, &v10) )
err(0LL);
v9 = v10.st_mode & 0xFFFFFFB6;
if ( *a1 == 2 )
v9 = v10.st_mode | 0x49;
v10.st_mode = v9;
chmod(v8, v9);
thefilename = 0LL;
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(int *a1, unsigned int a2, char *a3, unsigned int a4)
{
unsigned int v6; // er12
const char *v8; // rbp
__mode_t v9; // esi
struct stat v10; // [rsp+0h] [rbp-B8h] BYREF

enforcevalidation(1LL);
newparts(a1);
connectparts((__int64)a1);
initparts(a1);
if ( !(unsigned __int8)func2(a1, a2, a3) )
return 0;
if ( !(unsigned __int8)func3((__int64)a1, a2, a3, a4) )
return 0;
fillparts(a1);
measureparts(a1);
if ( !(unsigned __int8)createfixups(a1, a2, functionname) )
return 0;
completeparts(a1);
thefilename = outfilename;
v6 = outputelf(a1);
if ( !(_BYTE)v6 )
func4(0LL);
v8 = outfilename;
if ( stat(outfilename, &v10) )
func4(0LL);
v9 = v10.st_mode & 0xFFFFFFB6;
if ( *a1 == 2 )
v9 = v10.st_mode | 0x49;
v10.st_mode = v9;
chmod(v8, v9);
thefilename = 0LL;
return v6;
}

original_code:
static _Bool compile(blueprint *bp, int codetype,
                    char const *filename, int compressed)
{
    struct stat s;
    enforcevalidation(1);
    newparts(bp);
    connectparts(bp);
    initparts(bp);
    if (!setnames(bp, codetype, filename))
 return 0;
    if (!populateparts(bp, codetype, filename, compressed))
 return 0;
    fillparts(bp);
    measureparts(bp);
    if (!createfixups(bp, codetype, functionname))
        return 0;
    completeparts(bp);
    thefilename = outfilename;
    if (!outputelf(bp, outfilename)) {
 err(((void *)0));
 remove(outfilename);
 return 0;
    }
    if (stat(outfilename, &s) == 0) {
 if (bp->filetype == 2)
     s.st_mode |= 0100 | (0100 >> 3) | ((0100 >> 3) >> 3);
 else
     s.st_mode &= ~(0100 | (0100 >> 3) | ((0100 >> 3) >> 3));
 chmod(outfilename, s.st_mode);
    } else {
 err(((void *)0));
    }
    thefilename = ((void *)0);
    return 1;
}

canonical_original_code:
static _Bool compile(struct blueprint *bp, int codetype, char *filename, int compressed)
{
    struct stat s;
    enforcevalidation(1);
    newparts(bp);
    connectparts(bp);
    initparts(bp);
    if (!setnames(bp, codetype, filename))
 return 0;
    if (!populateparts(bp, codetype, filename, compressed))
 return 0;
    fillparts(bp);
    measureparts(bp);
    if (!createfixups(bp, codetype, functionname))
        return 0;
    completeparts(bp);
    thefilename = outfilename;
    if (!outputelf(bp, outfilename)) {
 err(((void *)0));
 remove(outfilename);
 return 0;
    }
    if (stat(outfilename, &s) == 0) {
 if (bp->filetype == 2)
     s.st_mode |= 0100 | (0100 >> 3) | ((0100 >> 3) >> 3);
 else
     s.st_mode &= ~(0100 | (0100 >> 3) | ((0100 >> 3) >> 3));
 chmod(outfilename, s.st_mode);
    } else {
 err(((void *)0));
    }
    thefilename = ((void *)0);
    return 1;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


allocate

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

result = malloc(a1);
if ( !result )
{
fwrite("memory allocation failed\n", 1uLL, 0x19uLL, stderr);
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
fwrite("memory allocation failed\n", 1uLL, 0x19uLL, stderr);
exit(1);
}
return result;
}

original_code:
static void *allocate(size_t size)
{
    void *p = malloc(size);
    if (!p) {
        fputs("memory allocation failed\n", stderr);
        exit(1);
    }
    return p;
}

canonical_original_code:
static void *allocate(unsigned long size)
{
    void *p = malloc(size);
    if (!p) {
        fputs("memory allocation failed\n", stderr);
        exit(1);
    }
    return p;
}

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *


strallocate

decompiled_code:
void *__fastcall strallocate(void *src)
{
size_t v1; // r12
void *v2; // rax

if ( !src )
return 0LL;
v1 = strlen((const char *)src) + 1;
v2 = allocate(v1);
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func1(void *src)
{
size_t v1; // r12
void *v2; // rax

if ( !src )
return 0LL;
v1 = strlen((const char *)src) + 1;
v2 = func0(v1);
return memcpy(v2, src, v1);
}

original_code:
static char *strallocate(char const *str)
{
    size_t size;
    if (!str)
        return ((void *)0);
    size = strlen(str) + 1;
    return memcpy(allocate(size), str, size);
}

canonical_original_code:
static char *strallocate(char *str)
{
    unsigned long size;
    if (!str)
        return ((void *)0);
    size = strlen(str) + 1;
    return memcpy(allocate(size), str, size);
}


err

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

v1 = thefilename;
if ( a1 )
{
if ( !thefilename )
v1 = (const char *)thisprog;
fprintf(stderr, "%s: ", v1);
va_start(arg, a1);
vfprintf(stderr, a1, arg);
fputc(10, stderr);
}
else
{
perror(thefilename);
}
}

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

v1 = thefilename;
if ( a1 )
{
if ( !thefilename )
v1 = (const char *)thisprog;
fprintf(stderr, "%s: ", v1);
va_start(arg, a1);
vfprintf(stderr, a1, arg);
fputc(10, stderr);
}
else
{
perror(thefilename);
}
}

original_code:
_Bool err(char const *fmt, ...)
{
    va_list args;
    if (!fmt) {
 perror(thefilename);
 return 0;
    }
    fprintf(stderr, "%s: ", thefilename ? thefilename : thisprog);
    __builtin_va_start(args,fmt);
    vfprintf(stderr, fmt, args);
    fputc('\n', stderr);
    __builtin_va_end(args);
    return 0;
}

canonical_original_code:
_Bool err(char *fmt, ...)
{
    __builtin_va_list args;
    if (!fmt) {
 perror(thefilename);
 return 0;
    }
    fprintf(stderr, "%s: ", thefilename ? thefilename : thisprog);
    __builtin_va_start(args,fmt);
    vfprintf(stderr, fmt, args);
    fputc('\n', stderr);
    __builtin_va_end(args);
    return 0;
}

variable_types:
fmt
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r15
char v4; // r14
signed int v5; // er13
unsigned __int8 v6; // r12
const char *v8; // rax
int v9; // eax
char *v11; // rbx
bool v12; // zf
_DWORD *v13; // rax
_DWORD *v14; // rsi
__int64 v15; // rcx
_DWORD *v16; // rdi
_DWORD *v17; // r8
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // rdx
int *i; // rcx
__int64 v22; // rax
const __m128i *v23; // rdx
__m128i *v24; // rax
__m128i v25; // xmm5
int v26; // eax
const char *v27; // rdi
int v28[2]; // [rsp+0h] [rbp-48h] BYREF
void *v29; // [rsp+8h] [rbp-40h]

v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
v8 = *argv;
v28[0] = 0;
thisprog = (__int64)v8;
while ( 2 )
{
v9 = getopt_long(argc, (char *const *)argv, "acf:i:lo:sxz", &options_1, 0LL);
if ( v9 != -1 )
{
switch ( v9 )
{
case 'H':
fwrite(
"Usage: ebfc [OPTIONS] FILENAME\n"
"Compiles Brainfuck source code to an ELF target.\n"
"\n"
" -x Generate a standalone executable file.\n"
" -l Generate a shared library.\n"
" -c Generate the object file for a function.\n"
" -xc Generate the object file for an executable.\n"
" -lc Generate the object file for a shared library.\n"
" -a, --arg Modify the function to take a buffer argument.\n"
" -z, --compressed Read the input file as compressed BF source.\n"
" -o, --output=FILE Write the output to FILE.\n"
" -f, --function=NAME Use NAME as the library's exported function.\n"
" -i, --input=NAME Record the source filename as NAME.\n"
" -s, --strip Omit non-required data from the output file.\n"
" --help Display this help and exit.\n"
" --version Display version information and exit.\n",
1uLL,
0x385uLL,
stdout);
return 0;
case 'V':
fwrite(
"ebfc, version 2.0\n"
"Copyright (C) 1999,2021 by Brian Raiter <breadbox@muppetlabs.com>\n"
"License GPLv2+: GNU GPL version 2 or later.\n"
"This is free software; you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n",
1uLL,
0xF9uLL,
stdout);
return 0;
case 'a':
v4 = 1;
continue;
case 'c':
v3 = 1;
continue;
case 'f':
functionname = (__int64)strallocate(optarg);
continue;
case 'i':
srcfilename = (__int64)strallocate(optarg);
continue;
case 'l':
v5 = 3;
continue;
case 'o':
outfilename = (char *)strallocate(optarg);
continue;
case 's':
addextras = 0;
continue;
case 'x':
v5 = 1;
continue;
case 'z':
v6 = 1;
continue;
default:
err("(try \"--help\" for more information)");
}
}
break;
}
if ( optind + 1 != argc )
{
v27 = "no source file specified.";
if ( optind != argc )
v27 = "multiple source files specified.";
err(v27);
}
v11 = (char *)argv[optind];
if ( v3 )
{
if ( !v5 )
v5 = 2;
v28[0] = 1;
}
else
{
if ( v5 <= 1 )
{
v5 = 1;
v26 = 2;
}
else
{
v26 = 3;
}
v28[0] = v26;
}
if ( v4 )
{
v12 = v5 == 1;
v5 = 4;
if ( v12 )
err("executable file format cannot take an argument");
}
v28[1] = 15;
v29 = allocate(0x5A0uLL);
v13 = memset(v29, 0, 0x5A0uLL);
v14 = &part_ehdr;
v15 = 24LL;
v16 = v13;
v17 = v13;
v18 = v28[0];
while ( v15 )
{
*v16++ = *v14++;
--v15;
}
v19 = 15 * v18;
v20 = partlists[v19];
for ( i = &partlists[v19]; (_DWORD)v20; v24[5] = v25 )
{
v22 = 3 * v20;
v23 = (const __m128i *)*(&parttable + v20);
++i;
v24 = (__m128i *)&v17[8 * v22];
*v24 = _mm_loadu_si128(v23);
v24[1] = _mm_loadu_si128(v23 + 1);
v24[2] = _mm_loadu_si128(v23 + 2);
v24[3] = _mm_loadu_si128(v23 + 3);
v24[4] = _mm_loadu_si128(v23 + 4);
v25 = _mm_loadu_si128(v23 + 5);
v20 = *i;
}
if ( !addextras )
v17[272] = 0;
return (unsigned __int8)compile(v28, v5, v11, v6) ^ 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r15
char v4; // r14
signed int v5; // er13
unsigned __int8 v6; // r12
const char *v8; // rax
int v9; // eax
char *v11; // rbx
bool v12; // zf
_DWORD *v13; // rax
_DWORD *v14; // rsi
__int64 v15; // rcx
_DWORD *v16; // rdi
_DWORD *v17; // r8
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // rdx
int *i; // rcx
__int64 v22; // rax
const __m128i *v23; // rdx
__m128i *v24; // rax
__m128i v25; // xmm5
int v26; // eax
const char *v27; // rdi
int v28[2]; // [rsp+0h] [rbp-48h] BYREF
void *v29; // [rsp+8h] [rbp-40h]

v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
v8 = *argv;
v28[0] = 0;
thisprog = (__int64)v8;
while ( 2 )
{
v9 = getopt_long(argc, (char *const *)argv, "acf:i:lo:sxz", &options_1, 0LL);
if ( v9 != -1 )
{
switch ( v9 )
{
case 'H':
fwrite(
"Usage: ebfc [OPTIONS] FILENAME\n"
"Compiles Brainfuck source code to an ELF target.\n"
"\n"
" -x Generate a standalone executable file.\n"
" -l Generate a shared library.\n"
" -c Generate the object file for a function.\n"
" -xc Generate the object file for an executable.\n"
" -lc Generate the object file for a shared library.\n"
" -a, --arg Modify the function to take a buffer argument.\n"
" -z, --compressed Read the input file as compressed BF source.\n"
" -o, --output=FILE Write the output to FILE.\n"
" -f, --function=NAME Use NAME as the library's exported function.\n"
" -i, --input=NAME Record the source filename as NAME.\n"
" -s, --strip Omit non-required data from the output file.\n"
" --help Display this help and exit.\n"
" --version Display version information and exit.\n",
1uLL,
0x385uLL,
stdout);
return 0;
case 'V':
fwrite(
"ebfc, version 2.0\n"
"Copyright (C) 1999,2021 by Brian Raiter <breadbox@muppetlabs.com>\n"
"License GPLv2+: GNU GPL version 2 or later.\n"
"This is free software; you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n",
1uLL,
0xF9uLL,
stdout);
return 0;
case 'a':
v4 = 1;
continue;
case 'c':
v3 = 1;
continue;
case 'f':
functionname = (__int64)func1(optarg);
continue;
case 'i':
srcfilename = (__int64)func1(optarg);
continue;
case 'l':
v5 = 3;
continue;
case 'o':
outfilename = (char *)func1(optarg);
continue;
case 's':
addextras = 0;
continue;
case 'x':
v5 = 1;
continue;
case 'z':
v6 = 1;
continue;
default:
func4("(try \"--help\" for more information)");
}
}
break;
}
if ( optind + 1 != argc )
{
v27 = "no source file specified.";
if ( optind != argc )
v27 = "multiple source files specified.";
func4(v27);
}
v11 = (char *)argv[optind];
if ( v3 )
{
if ( !v5 )
v5 = 2;
v28[0] = 1;
}
else
{
if ( v5 <= 1 )
{
v5 = 1;
v26 = 2;
}
else
{
v26 = 3;
}
v28[0] = v26;
}
if ( v4 )
{
v12 = v5 == 1;
v5 = 4;
if ( v12 )
func4("executable file format cannot take an argument");
}
v28[1] = 15;
v29 = func0(0x5A0uLL);
v13 = memset(v29, 0, 0x5A0uLL);
v14 = &part_ehdr;
v15 = 24LL;
v16 = v13;
v17 = v13;
v18 = v28[0];
while ( v15 )
{
*v16++ = *v14++;
--v15;
}
v19 = 15 * v18;
v20 = partlists[v19];
for ( i = &partlists[v19]; (_DWORD)v20; v24[5] = v25 )
{
v22 = 3 * v20;
v23 = (const __m128i *)*(&parttable + v20);
++i;
v24 = (__m128i *)&v17[8 * v22];
*v24 = _mm_loadu_si128(v23);
v24[1] = _mm_loadu_si128(v23 + 1);
v24[2] = _mm_loadu_si128(v23 + 2);
v24[3] = _mm_loadu_si128(v23 + 3);
v24[4] = _mm_loadu_si128(v23 + 4);
v25 = _mm_loadu_si128(v23 + 5);
v20 = *i;
}
if ( !addextras )
v17[272] = 0;
return (unsigned __int8)func5(v28, v5, v11, v6) ^ 1;
}

original_code:
int main(int argc, char *argv[])
{
    static char const *optstring = "acf:i:lo:sxz";
    static struct option const options[] = {
        { "output", 1, ((void *)0), 'o' },
        { "input", 1, ((void *)0), 'i' },
        { "function", 1, ((void *)0), 'f' },
        { "arg", 0, ((void *)0), 'a' },
        { "strip", 0, ((void *)0), 's' },
        { "help", 0, ((void *)0), 'H' },
        { "version", 0, ((void *)0), 'V' },
        { 0, 0, 0, 0 }
    };
    blueprint b;
    char const *filename;
    _Bool buildobjfile, bufferarg, compressed;
    int codetype;
    int ch, i;
    thisprog = argv[0];
    b.filetype = 0;
    codetype = CODE_NONE;
    buildobjfile = 0;
    bufferarg = 0;
    compressed = 0;
    while ((ch = getopt_long(argc, argv, optstring, options, ((void *)0))) != (-1)) {
 switch (ch) {
   case 'c': buildobjfile = 1; break;
   case 'x': codetype = CODE_EXEC; break;
   case 'l': codetype = CODE_SOFUNC; break;
   case 'z': compressed = 1; break;
   case 'a': bufferarg = 1; break;
   case 's': addextras = 0; break;
   case 'f': functionname = strallocate(optarg); break;
          case 'i': srcfilename = strallocate(optarg); break;
   case 'o': outfilename = strallocate(optarg); break;
   case 'H': fputs(yowzitch, stdout); return 0;
   case 'V': fputs(vourzhon, stdout); return 0;
   default:
            err("(try \"--help\" for more information)");
            exit(1);
 }
    }
    if (optind + 1 != argc) {
 err(optind == argc ? "no source file specified."
      : "multiple source files specified.");
 exit(1);
    }
    filename = argv[optind];
    if (buildobjfile) {
        if (!codetype)
            codetype = CODE_FUNC;
        b.filetype = 1;
    } else {
 if (!codetype)
     codetype = CODE_EXEC;
 b.filetype = codetype == CODE_EXEC ? 2 : 3;
    }
    if (bufferarg) {
        if (codetype == CODE_EXEC) {
            err("executable file format cannot take an argument");
            exit(1);
        }
        codetype = CODE_FUNCARG;
    }
    b.partcount = P_COUNT;
    b.parts = allocate(P_COUNT * sizeof *b.parts);
    memset(b.parts, 0, b.partcount * sizeof *b.parts);
    b.parts[0] = *parttable[P_EHDR];
    for (i = 0 ; partlists[b.filetype][i] ; ++i) {
        int type = partlists[b.filetype][i];
        b.parts[type] = *parttable[type];
    }
    if (!addextras)
        ((&b.parts[P_COMMENT])->shtype = 0);
    return compile(&b, codetype, filename, compressed) ? 0
             : 1;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    static char *optstring = "acf:i:lo:sxz";
    static struct option options[] = {
        { "output", 1, ((void *)0), 'o' },
        { "input", 1, ((void *)0), 'i' },
        { "function", 1, ((void *)0), 'f' },
        { "arg", 0, ((void *)0), 'a' },
        { "strip", 0, ((void *)0), 's' },
        { "help", 0, ((void *)0), 'H' },
        { "version", 0, ((void *)0), 'V' },
        { 0, 0, 0, 0 }
    };
    struct blueprint b;
    char *filename;
    _Bool buildobjfile; _Bool bufferarg; _Bool compressed;
    int codetype;
    int ch; int i;
    thisprog = argv[0];
    b.filetype = 0;
    codetype = CODE_NONE;
    buildobjfile = 0;
    bufferarg = 0;
    compressed = 0;
    while ((ch = getopt_long(argc, argv, optstring, options, ((void *)0))) != (-1)) {
 switch (ch) {
   case 'c': buildobjfile = 1; break;
   case 'x': codetype = CODE_EXEC; break;
   case 'l': codetype = CODE_SOFUNC; break;
   case 'z': compressed = 1; break;
   case 'a': bufferarg = 1; break;
   case 's': addextras = 0; break;
   case 'f': functionname = strallocate(optarg); break;
          case 'i': srcfilename = strallocate(optarg); break;
   case 'o': outfilename = strallocate(optarg); break;
   case 'H': fputs(yowzitch, stdout); return 0;
   case 'V': fputs(vourzhon, stdout); return 0;
   default:
            err("(try \"--help\" for more information)");
            exit(1);
 }
    }
    if (optind + 1 != argc) {
 err(optind == argc ? "no source file specified."
      : "multiple source files specified.");
 exit(1);
    }
    filename = argv[optind];
    if (buildobjfile) {
        if (!codetype)
            codetype = CODE_FUNC;
        b.filetype = 1;
    } else {
 if (!codetype)
     codetype = CODE_EXEC;
 b.filetype = codetype == CODE_EXEC ? 2 : 3;
    }
    if (bufferarg) {
        if (codetype == CODE_EXEC) {
            err("executable file format cannot take an argument");
            exit(1);
        }
        codetype = CODE_FUNCARG;
    }
    b.partcount = P_COUNT;
    b.parts = allocate(P_COUNT * sizeof *b.parts);
    memset(b.parts, 0, b.partcount * sizeof *b.parts);
    b.parts[0] = *parttable[P_EHDR];
    for (i = 0 ; partlists[b.filetype][i] ; ++i) {
        int type = partlists[b.filetype][i];
        b.parts[type] = *parttable[type];
    }
    if (!addextras)
        ((&b.parts[P_COMMENT])->shtype = 0);
    return compile(&b, codetype, filename, compressed) ? 0
             : 1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
optstring
char *

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: struct blueprint { int filetype; int partcount; struct elfpart { _Bool (*new)(struct elfpart *); _Bool (*init)(struct elfpart *, struct blueprint *); _Bool (*fill)(struct elfpart *, struct blueprint *); _Bool (*complete)(struct elfpart *, struct blueprint *); int shtype; int entsize; int count; int size; void *part; char *shname; unsigned long offset; long addr; struct elfpart *link; int info; short flags; _Bool done; } *parts; }

variable_types: ch int

variable_types: i int

variable_types: type int


verifyident

decompiled_code:
__int64 __fastcall verifyident(__int64 a1, const char *a2)
{
unsigned __int8 *v2; // rbx
__int64 v3; // rsi
__int64 v4; // rsi
__int64 v5; // rsi

if ( filesize <= 15 )
err((int)"file is not an ELF file.", a2);
v2 = (unsigned __int8 *)image;
if ( *(_BYTE *)image != 127
|| *(_BYTE *)(image + 1) != 69
|| *(_BYTE *)(image + 2) != 76
|| *(_BYTE *)(image + 3) != 70 )
{
warn("file does not contain an ELF signature.");
}
v3 = v2[4];
if ( (unsigned __int8)(v3 - 1) <= 1u )
{
v4 = v2[5];
if ( (unsigned __int8)(v4 - 1) <= 1u )
goto LABEL_7;
LABEL_11:
warn("unrecognized ELF data value: %d.", v4);
goto LABEL_8;
}
warn("unrecognized ELF class value: %d.", v3);
v4 = v2[5];
if ( (unsigned __int8)(v4 - 1) > 1u )
goto LABEL_11;
LABEL_7:
if ( (_BYTE)v4 != 1 )
err((int)"not a %s-endian ELF file.", "little");
LABEL_8:
v5 = v2[6];
if ( (_BYTE)v5 != 1 )
warn("unrecognized ELF header version: %d.", v5);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
unsigned __int8 *v2; // rbx
__int64 v3; // rsi
__int64 v4; // rsi
__int64 v5; // rsi

if ( filesize <= 15 )
err((int)"file is not an ELF file.", a2);
v2 = (unsigned __int8 *)image;
if ( *(_BYTE *)image != 127
|| *(_BYTE *)(image + 1) != 69
|| *(_BYTE *)(image + 2) != 76
|| *(_BYTE *)(image + 3) != 70 )
{
warn("file does not contain an ELF signature.");
}
v3 = v2[4];
if ( (unsigned __int8)(v3 - 1) <= 1u )
{
v4 = v2[5];
if ( (unsigned __int8)(v4 - 1) <= 1u )
goto LABEL_7;
LABEL_11:
warn("unrecognized ELF data value: %d.", v4);
goto LABEL_8;
}
warn("unrecognized ELF class value: %d.", v3);
v4 = v2[5];
if ( (unsigned __int8)(v4 - 1) > 1u )
goto LABEL_11;
LABEL_7:
if ( (_BYTE)v4 != 1 )
err((int)"not a %s-endian ELF file.", "little");
LABEL_8:
v5 = v2[6];
if ( (_BYTE)v5 != 1 )
warn("unrecognized ELF header version: %d.", v5);
return 1LL;
}

original_code:
static int verifyident(void)
{
    unsigned char const *ident = image;
    if (filesize < (16))
 return err("file is not an ELF file.");
    if (ident[0] != 0x7f || ident[1] != 'E'
      || ident[2] != 'L'
      || ident[3] != 'F')
 warn("file does not contain an ELF signature.");
    if (ident[4] == 0 || ident[4] >= 3)
 warn("unrecognized ELF class value: %d.", ident[4]);
    if (ident[5] == 0 || ident[5] >= 3) {
 warn("unrecognized ELF data value: %d.", ident[5]);
    } else {
 int be = 1;
 *(char*)&be = 0;
 if (ident[5] != (be ? 2 : 1))
     return err("not a %s-endian ELF file.", (be ? "big" : "little"));
    }
    if (ident[6] == 0 || ident[6] >= 2)
 warn("unrecognized ELF header version: %d.", ident[6]);
    return 1;
}

canonical_original_code:
static int verifyident()
{
    unsigned char *ident = image;
    if (filesize < (16))
 return err("file is not an ELF file.");
    if (ident[0] != 0x7f || ident[1] != 'E'
      || ident[2] != 'L'
      || ident[3] != 'F')
 warn("file does not contain an ELF signature.");
    if (ident[4] == 0 || ident[4] >= 3)
 warn("unrecognized ELF class value: %d.", ident[4]);
    if (ident[5] == 0 || ident[5] >= 3) {
 warn("unrecognized ELF data value: %d.", ident[5]);
    } else {
 int be = 1;
 *(char *)&be = 0;
 if (ident[5] != (be ? 2 : 1))
     return err("not a %s-endian ELF file.", (be ? "big" : "little"));
    }
    if (ident[6] == 0 || ident[6] >= 2)
 warn("unrecognized ELF header version: %d.", ident[6]);
    return 1;
}

variable_types:
ident
unsigned char *

variable_types: be int


copyehdr

original_code:
static void copyehdr(void)
{
    Elf64_Ehdr *eh64;
    eh64 = allocate(sizeof *eh64);
    if (image[4] == 2) {
 if (filesize >= (int)(sizeof *eh64)) {
     memcpy(eh64, image, sizeof *eh64);
     recordpiece(0, sizeof(Elf64_Ehdr), P_EHDR, "ehdr", 0);
 } else {
     memcpy(eh64, image, filesize);
     memset((char*)eh64 + filesize, 0, sizeof *eh64 - filesize);
     warn("file does not have a complete ELF header.");
     recordpiece(0, filesize, P_SECTION, "~ehdr", 0);
 }
 if (eh64->e_version == 0 || eh64->e_version >= 2)
     warn("unrecognized ELF version: %d.", eh64->e_version);
 if (eh64->e_ehsize != sizeof(Elf64_Ehdr))
     warn("unexpected ELF header size: %u instead of %u.",
  eh64->e_ehsize, sizeof(Elf64_Ehdr));
 if (eh64->e_phoff && eh64->e_phentsize != sizeof(Elf64_Phdr))
     warn("unexpected program header entry size: %u instead of %u.",
   eh64->e_phentsize, sizeof(Elf64_Phdr));
 if (eh64->e_shoff && eh64->e_shentsize != sizeof(Elf64_Shdr))
     warn("unexpected section header entry size: %u instead of %u.",
   eh64->e_shentsize, sizeof(Elf64_Shdr));
    } else {
 Elf32_Ehdr eh32;
 if (filesize >= (int)(sizeof eh32)) {
     memcpy(&eh32, image, sizeof eh32);
     recordpiece(0, sizeof(Elf32_Ehdr), P_EHDR, "ehdr", 0);
 } else {
     memcpy(&eh32, image, filesize);
     memset((char*)&eh32 + filesize, 0, sizeof eh32 - filesize);
     warn("file does not have a complete ELF header.");
     recordpiece(0, filesize, P_SECTION, "~ehdr", 0);
 }
 if (eh32.e_version == 0 || eh32.e_version >= 2)
     warn("unrecognized ELF version: %d.", eh32.e_version);
 if (eh32.e_ehsize != sizeof(Elf32_Ehdr))
     warn("unexpected ELF header size: %u instead of %u.",
  eh32.e_ehsize, sizeof(Elf32_Ehdr));
 if (eh32.e_phoff && eh32.e_phentsize != sizeof(Elf32_Phdr))
     warn("unexpected program header entry size: %u instead of %u.",
   eh32.e_phentsize, sizeof(Elf32_Phdr));
 if (eh32.e_shoff && eh32.e_shentsize != sizeof(Elf32_Shdr))
     warn("unexpected section header entry size: %u instead of %u.",
   eh32.e_shentsize, sizeof(Elf32_Shdr));
 memcpy(eh64->e_ident, eh32.e_ident, sizeof eh64->e_ident);
 eh64->e_type = eh32.e_type;
 eh64->e_machine = eh32.e_machine;
 eh64->e_version = eh32.e_version;
 eh64->e_entry = eh32.e_entry;
 eh64->e_phoff = eh32.e_phoff;
 eh64->e_shoff = eh32.e_shoff;
 eh64->e_flags = eh32.e_flags;
 eh64->e_ehsize = eh32.e_ehsize;
 eh64->e_phentsize = eh32.e_phentsize;
 eh64->e_phnum = eh32.e_phnum;
 eh64->e_shentsize = eh32.e_shentsize;
 eh64->e_shnum = eh32.e_shnum;
 eh64->e_shstrndx = eh32.e_shstrndx;
    }
    ehdr = eh64;
}

canonical_original_code:
static void copyehdr()
{
    struct Elf64_Ehdr *eh64;
    eh64 = allocate(sizeof *eh64);
    if (image[4] == 2) {
 if (filesize >= (int)(sizeof *eh64)) {
     memcpy(eh64, image, sizeof *eh64);
     recordpiece(0, sizeof(Elf64_Ehdr), P_EHDR, "ehdr", 0);
 } else {
     memcpy(eh64, image, filesize);
     memset((char *)eh64 + filesize, 0, sizeof *eh64 - filesize);
     warn("file does not have a complete ELF header.");
     recordpiece(0, filesize, P_SECTION, "~ehdr", 0);
 }
 if (eh64->e_version == 0 || eh64->e_version >= 2)
     warn("unrecognized ELF version: %d.", eh64->e_version);
 if (eh64->e_ehsize != sizeof(Elf64_Ehdr))
     warn("unexpected ELF header size: %u instead of %u.",
  eh64->e_ehsize, sizeof(Elf64_Ehdr));
 if (eh64->e_phoff && eh64->e_phentsize != sizeof(Elf64_Phdr))
     warn("unexpected program header entry size: %u instead of %u.",
   eh64->e_phentsize, sizeof(Elf64_Phdr));
 if (eh64->e_shoff && eh64->e_shentsize != sizeof(Elf64_Shdr))
     warn("unexpected section header entry size: %u instead of %u.",
   eh64->e_shentsize, sizeof(Elf64_Shdr));
    } else {
 struct Elf32_Ehdr eh32;
 if (filesize >= (int)(sizeof eh32)) {
     memcpy(&eh32, image, sizeof eh32);
     recordpiece(0, sizeof(Elf32_Ehdr), P_EHDR, "ehdr", 0);
 } else {
     memcpy(&eh32, image, filesize);
     memset((char *)&eh32 + filesize, 0, sizeof eh32 - filesize);
     warn("file does not have a complete ELF header.");
     recordpiece(0, filesize, P_SECTION, "~ehdr", 0);
 }
 if (eh32.e_version == 0 || eh32.e_version >= 2)
     warn("unrecognized ELF version: %d.", eh32.e_version);
 if (eh32.e_ehsize != sizeof(Elf32_Ehdr))
     warn("unexpected ELF header size: %u instead of %u.",
  eh32.e_ehsize, sizeof(Elf32_Ehdr));
 if (eh32.e_phoff && eh32.e_phentsize != sizeof(Elf32_Phdr))
     warn("unexpected program header entry size: %u instead of %u.",
   eh32.e_phentsize, sizeof(Elf32_Phdr));
 if (eh32.e_shoff && eh32.e_shentsize != sizeof(Elf32_Shdr))
     warn("unexpected section header entry size: %u instead of %u.",
   eh32.e_shentsize, sizeof(Elf32_Shdr));
 memcpy(eh64->e_ident, eh32.e_ident, sizeof eh64->e_ident);
 eh64->e_type = eh32.e_type;
 eh64->e_machine = eh32.e_machine;
 eh64->e_version = eh32.e_version;
 eh64->e_entry = eh32.e_entry;
 eh64->e_phoff = eh32.e_phoff;
 eh64->e_shoff = eh32.e_shoff;
 eh64->e_flags = eh32.e_flags;
 eh64->e_ehsize = eh32.e_ehsize;
 eh64->e_phentsize = eh32.e_phentsize;
 eh64->e_phnum = eh32.e_phnum;
 eh64->e_shentsize = eh32.e_shentsize;
 eh64->e_shnum = eh32.e_shnum;
 eh64->e_shstrndx = eh32.e_shstrndx;
    }
    ehdr = eh64;
}

variable_types: struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *

variable_types: struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }


decompiled_code:
_BOOL8 iself64()
{
return *(_BYTE *)(ehdr + 4) == 2;
}

canonical_decompiled_code:
_BOOL8 func2()
{
return *(_BYTE *)(ehdr + 4) == 2;
}

original_code:
int iself64(void)
{
    return ehdr->e_ident[4] == 2;
}

canonical_original_code:
int iself64()
{
    return ehdr->e_ident[4] == 2;
}


iscorefile

decompiled_code:
_BOOL8 iscorefile()
{
return *(_WORD *)(ehdr + 16) == 4;
}

canonical_decompiled_code:
_BOOL8 func3()
{
return *(_WORD *)(ehdr + 16) == 4;
}

original_code:
int iscorefile(void)
{
    return ehdr->e_type == 4;
}

canonical_original_code:
int iscorefile()
{
    return ehdr->e_type == 4;
}


getptrto

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

if ( a1 < 0 || filesize <= a1 || *a2 < 0 )
{
*a2 = 0LL;
result = 0LL;
}
else
{
if ( filesize < a1 + *a2 )
*a2 = filesize - a1;
result = a1 + image;
}
return result;
}

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

if ( a1 < 0 || filesize <= a1 || *a2 < 0 )
{
*a2 = 0LL;
result = 0LL;
}
else
{
if ( filesize < a1 + *a2 )
*a2 = filesize - a1;
result = a1 + image;
}
return result;
}

original_code:
void const *getptrto(long offset, long *size)
{
    if (offset >= filesize || offset < 0 || *size < 0) {
 *size = 0;
 return ((void *)0);
    }
    if (offset + *size > filesize)
 *size = filesize - offset;
    return image + offset;
}

canonical_original_code:
void *getptrto(long offset, long *size)
{
    if (offset >= filesize || offset < 0 || *size < 0) {
 *size = 0;
 return ((void *)0);
    }
    if (offset + *size > filesize)
 *size = filesize - offset;
    return image + offset;
}

variable_types: offset long

variable_types:
size
long *


readelf

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

image = a1;
filesize = (__int64)a2;
result = verifyident(a1, a2);
if ( (_DWORD)result )
{
recordpiece(0LL, filesize, 0LL, "~pad", 0LL);
copyehdr();
setmachinespecific(*(unsigned __int16 *)(ehdr + 18));
dividesegments(*(_QWORD *)(ehdr + 32), *(unsigned __int16 *)(ehdr + 56), *(unsigned __int16 *)(ehdr + 54));
dividesections(
*(_QWORD *)(ehdr + 40),
*(unsigned __int16 *)(ehdr + 60),
*(unsigned __int16 *)(ehdr + 58),
*(unsigned __int16 *)(ehdr + 62));
result = 1LL;
}
return result;
}

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

image = a1;
filesize = (__int64)a2;
result = func0(a1, a2);
if ( (_DWORD)result )
{
recordpiece(0LL, filesize, 0LL, "~pad", 0LL);
func1();
setmachinespecific(*(unsigned __int16 *)(ehdr + 18));
dividesegments(*(_QWORD *)(ehdr + 32), *(unsigned __int16 *)(ehdr + 56), *(unsigned __int16 *)(ehdr + 54));
dividesections(
*(_QWORD *)(ehdr + 40),
*(unsigned __int16 *)(ehdr + 60),
*(unsigned __int16 *)(ehdr + 58),
*(unsigned __int16 *)(ehdr + 62));
result = 1LL;
}
return result;
}

original_code:
int readelf(void const *ptr, size_t size)
{
    image = ptr;
    filesize = size;
    if (!verifyident())
 return 0;
    recordpiece(0, filesize, P_UNCLAIMED, "~pad", 0);
    copyehdr();
    setmachinespecific(ehdr->e_machine);
    dividesegments(ehdr->e_phoff, ehdr->e_phnum, ehdr->e_phentsize);
    dividesections(ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize,
     ehdr->e_shstrndx);
    return 1;
}

canonical_original_code:
int readelf(void *ptr, unsigned long size)
{
    image = ptr;
    filesize = size;
    if (!verifyident())
 return 0;
    recordpiece(0, filesize, P_UNCLAIMED, "~pad", 0);
    copyehdr();
    setmachinespecific(ehdr->e_machine);
    dividesegments(ehdr->e_phoff, ehdr->e_phnum, ehdr->e_phentsize);
    dividesections(ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize,
     ehdr->e_shstrndx);
    return 1;
}

variable_types:
ptr
void *

variable_types:
size
unsigned long


outnothing

decompiled_code:
__int64 outnothing()
{
return out(&unk_B80);
}

canonical_decompiled_code:
__int64 func0()
{
return out(&unk_B80);
}

original_code:
static void outnothing(void const *ptr, long size, int ndx)
{
    (void)ptr;
    (void)size;
    (void)ndx;
    out("{ }");
}

canonical_original_code:
static void outnothing(void *ptr, long size, int ndx)
{
    (void)ptr;
    (void)size;
    (void)ndx;
    out("{ }");
}

variable_types: size long

variable_types: ndx int


outnote

decompiled_code:
__int64 __fastcall outnote(__int64 a1, unsigned __int64 a2, unsigned int a3)
{
signed __int64 v5; // rbx
__int64 v6; // r15
_DWORD *v7; // r13
unsigned int v8; // er14
unsigned int v9; // er12
unsigned int v10; // er14
__int64 v11; // rax
__int64 v12; // rdi
int v13; // er13
__int64 v14; // rax
__int64 v15; // rdi
__int64 v16; // rdi
int v17; // er13
__int64 v18; // rdi
__int64 result; // rax
__int64 v20; // rdi
__int64 v21; // rdi
__int64 v22; // [rsp+0h] [rbp-48h]
int v23; // [rsp+0h] [rbp-48h]

if ( (unsigned int)iscorefile() )
{
if ( (unsigned int)iself64() )
result = outnote64(a1, a2, a3);
else
result = outnote32(a1, a2, a3);
}
else
{
v5 = a2 >> 2;
v6 = 0LL;
beginblock(1LL);
LABEL_3:
while ( v6 < v5 )
{
while ( 1 )
{
v7 = (_DWORD *)(a1 + 4 * v6 + 4);
v22 = 4 * v6;
v8 = *v7 + 3;
v9 = (unsigned int)(*(_DWORD *)(a1 + 4 * v6) + 3) >> 2;
linebreak();
v10 = v8 >> 2;
v11 = strdec(*(unsigned int *)(a1 + 4 * v6));
out(v11);
v12 = (unsigned int)*v7;
v6 += 3LL;
v13 = v6;
v14 = strdec(v12);
out(v14);
v23 = *(_DWORD *)(a1 + v22 + 8);
v15 = strdefint(v23, "NT_GNU_");
out(v15);
if ( v5 > v6 && v9 )
{
do
{
++v6;
v16 = strhex(*(unsigned int *)(a1 + 4 * v6 - 4));
out(v16);
}
while ( v9 + v13 - (_DWORD)v6 && v5 > v6 );
}
if ( !v10 )
break;
if ( v23 == 1 )
{
v20 = *(unsigned int *)(a1 + 4 * v6);
--v10;
++v6;
v21 = strdefint(v20, "ELF_NOTE_OS_");
out(v21);
}
if ( !v10 || v6 >= v5 )
break;
v17 = v10 + v6;
while ( 1 )
{
++v6;
v18 = strhex(*(unsigned int *)(a1 + 4 * v6 - 4));
out(v18);
if ( v17 == (_DWORD)v6 )
break;
if ( v5 <= v6 )
goto LABEL_3;
}
if ( v6 >= v5 )
goto LABEL_18;
}
}
LABEL_18:
result = endblock();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int64 a2, unsigned int a3)
{
signed __int64 v5; // rbx
__int64 v6; // r15
_DWORD *v7; // r13
unsigned int v8; // er14
unsigned int v9; // er12
unsigned int v10; // er14
__int64 v11; // rax
__int64 v12; // rdi
int v13; // er13
__int64 v14; // rax
__int64 v15; // rdi
__int64 v16; // rdi
int v17; // er13
__int64 v18; // rdi
__int64 result; // rax
__int64 v20; // rdi
__int64 v21; // rdi
__int64 v22; // [rsp+0h] [rbp-48h]
int v23; // [rsp+0h] [rbp-48h]

if ( (unsigned int)iscorefile() )
{
if ( (unsigned int)iself64() )
result = outnote64(a1, a2, a3);
else
result = outnote32(a1, a2, a3);
}
else
{
v5 = a2 >> 2;
v6 = 0LL;
beginblock(1LL);
LABEL_3:
while ( v6 < v5 )
{
while ( 1 )
{
v7 = (_DWORD *)(a1 + 4 * v6 + 4);
v22 = 4 * v6;
v8 = *v7 + 3;
v9 = (unsigned int)(*(_DWORD *)(a1 + 4 * v6) + 3) >> 2;
linebreak();
v10 = v8 >> 2;
v11 = strdec(*(unsigned int *)(a1 + 4 * v6));
out(v11);
v12 = (unsigned int)*v7;
v6 += 3LL;
v13 = v6;
v14 = strdec(v12);
out(v14);
v23 = *(_DWORD *)(a1 + v22 + 8);
v15 = strdefint(v23, "NT_GNU_");
out(v15);
if ( v5 > v6 && v9 )
{
do
{
++v6;
v16 = strhex(*(unsigned int *)(a1 + 4 * v6 - 4));
out(v16);
}
while ( v9 + v13 - (_DWORD)v6 && v5 > v6 );
}
if ( !v10 )
break;
if ( v23 == 1 )
{
v20 = *(unsigned int *)(a1 + 4 * v6);
--v10;
++v6;
v21 = strdefint(v20, "ELF_NOTE_OS_");
out(v21);
}
if ( !v10 || v6 >= v5 )
break;
v17 = v10 + v6;
while ( 1 )
{
++v6;
v18 = strhex(*(unsigned int *)(a1 + 4 * v6 - 4));
out(v18);
if ( v17 == (_DWORD)v6 )
break;
if ( v5 <= v6 )
goto LABEL_3;
}
if ( v6 >= v5 )
goto LABEL_18;
}
}
LABEL_18:
result = endblock();
}
return result;
}

original_code:
void outnote(void const *ptr, long size, int ndx)
{
    Elf64_Word const *words = ptr;
    long count = size / sizeof *words;
    long i;
    int namesize, descsize, tag;
    (void)ndx;
    if (iscorefile()) {
 iself64() ? outnote64(ptr, size, ndx)
    : outnote32(ptr, size, ndx);
 return;
    }
    beginblock(1);
    i = 0;
    while (i < count) {
 namesize = (words[i] + 3) / 4;
 descsize = (words[i + 1] + 3) / 4;
 linebreak();
 (out(strdec(words[i++])));
 (out(strdec(words[i++])));
 tag = words[i++];
 (out(strdefint(tag, "NT_GNU_")));
 while (namesize-- && i < count)
     (out(strhex(words[i++])));
 if (descsize == 0)
     continue;
 if (tag == 1) {
     (out(strdefint(words[i++], "ELF_NOTE_OS_")));
     --descsize;
 }
 while (descsize-- && i < count)
     (out(strhex(words[i++])));
    }
    endblock();
}

canonical_original_code:
void outnote(void *ptr, long size, int ndx)
{
    unsigned int *words = ptr;
    long count = size / sizeof *words;
    long i;
    int namesize; int descsize; int tag;
    (void)ndx;
    if (iscorefile()) {
 iself64() ? outnote64(ptr, size, ndx)
    : outnote32(ptr, size, ndx);
 return;
    }
    beginblock(1);
    i = 0;
    while (i < count) {
 namesize = (words[i] + 3) / 4;
 descsize = (words[i + 1] + 3) / 4;
 linebreak();
 (out(strdec(words[i++])));
 (out(strdec(words[i++])));
 tag = words[i++];
 (out(strdefint(tag, "NT_GNU_")));
 while (namesize-- && i < count)
     (out(strhex(words[i++])));
 if (descsize == 0)
     continue;
 if (tag == 1) {
     (out(strdefint(words[i++], "ELF_NOTE_OS_")));
     --descsize;
 }
 while (descsize-- && i < count)
     (out(strhex(words[i++])));
    }
    endblock();
}

variable_types:
words
unsigned int *

variable_types: count long

variable_types: i long

variable_types: namesize int

variable_types: descsize int

variable_types: tag int


outhashtable

original_code:
static void outhashtable(void const *ptr, long size, int ndx)
{
    Elf64_Word const *entries = ptr;
    long count = size / sizeof *entries;
    long i;
    (void)ndx;
    beginblock(1);
    if (count <= 2 || (long)(entries[0] + entries[1] + 2) != count) {
 for (i = 0 ; i < count ; ++i)
     (out(strdec(entries[i])));
    } else {
 (out(strdec(entries[0])));
 (out(strdec(entries[1])));
 linebreak();
 i = 2;
 if (entries[0] > 0) {
     for ( ; i < (long)(2 + entries[0]) ; ++i)
  (out(strdec(entries[i])));
     linebreak();
 }
 for ( ; i < count ; ++i)
     (out(strdec(entries[i])));
    }
    endblock();
}

canonical_original_code:
static void outhashtable(void *ptr, long size, int ndx)
{
    unsigned int *entries = ptr;
    long count = size / sizeof *entries;
    long i;
    (void)ndx;
    beginblock(1);
    if (count <= 2 || (long)(entries[0] + entries[1] + 2) != count) {
 for (i = 0 ; i < count ; ++i)
     (out(strdec(entries[i])));
    } else {
 (out(strdec(entries[0])));
 (out(strdec(entries[1])));
 linebreak();
 i = 2;
 if (entries[0] > 0) {
     for ( ; i < (long)(2 + entries[0]) ; ++i)
  (out(strdec(entries[i])));
     linebreak();
 }
 for ( ; i < count ; ++i)
     (out(strdec(entries[i])));
    }
    endblock();
}

variable_types:
entries
unsigned int *


outxwords

decompiled_code:
__int64 __fastcall outxwords(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rsi
_QWORD *v3; // rdx
__int64 v4; // rax
__int64 v5; // rbp
unsigned __int64 v6; // r12
const char *v7; // r14
__int64 v8; // rax
__int64 v9; // rax
unsigned __int64 v10; // rdx
__int64 v11; // rbx
__int64 v12; // rsi
char v14[56]; // [rsp-38h] [rbp-38h] BYREF

v2 = a2 >> 3;
if ( v2 )
{
v3 = (_QWORD *)(a1 + 8 * v2 - 8);
v4 = 0LL;
v5 = v2;
while ( !*v3 )
{
++v4;
--v3;
if ( v2 == v4 )
return out("{ 0 }");
}
if ( v4 <= 254 || 4 * v4 < v2 )
{
v6 = 0LL;
}
else
{
v6 = v4 & 0xFFFFFFFFFFFFFFFCLL;
v5 = v2 - (v4 & 0xFFFFFFFFFFFFFFFCLL);
if ( v5 <= 0 )
{
v7 = "%lu";
beginblock(1LL);
LABEL_9:
sprintf(v14, "%s x %%ld", v7);
v8 = strf(v14, 0LL, v6);
outcomment(v8);
return endblock();
}
}
v9 = 0LL;
v10 = 0LL;
do
{
if ( v10 < *(_QWORD *)(a1 + 8 * v9) )
v10 = *(_QWORD *)(a1 + 8 * v9);
++v9;
}
while ( v9 < v5 );
if ( v10 <= 0xFFFFFFFF )
{
if ( v10 > 0xFFFF )
{
v7 = "0x%08lX";
}
else if ( v10 > 0xFF )
{
v7 = "0x%04lX";
}
else
{
v7 = "0x%02lX";
if ( v10 <= 0x63 )
{
v7 = "%lu";
beginblock(1LL);
goto LABEL_18;
}
}
}
else
{
v7 = "0x%016lX";
}
beginblock(1LL);
LABEL_18:
v11 = 0LL;
do
{
v12 = *(_QWORD *)(a1 + 8 * v11++);
outf(v7, v12);
}
while ( v11 < v5 );
if ( !v6 )
return endblock();
goto LABEL_9;
}
return out("{ 0 }");
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rsi
_QWORD *v3; // rdx
__int64 v4; // rax
__int64 v5; // rbp
unsigned __int64 v6; // r12
const char *v7; // r14
__int64 v8; // rax
__int64 v9; // rax
unsigned __int64 v10; // rdx
__int64 v11; // rbx
__int64 v12; // rsi
char v14[56]; // [rsp-38h] [rbp-38h] BYREF

v2 = a2 >> 3;
if ( v2 )
{
v3 = (_QWORD *)(a1 + 8 * v2 - 8);
v4 = 0LL;
v5 = v2;
while ( !*v3 )
{
++v4;
--v3;
if ( v2 == v4 )
return out("{ 0 }");
}
if ( v4 <= 254 || 4 * v4 < v2 )
{
v6 = 0LL;
}
else
{
v6 = v4 & 0xFFFFFFFFFFFFFFFCLL;
v5 = v2 - (v4 & 0xFFFFFFFFFFFFFFFCLL);
if ( v5 <= 0 )
{
v7 = "%lu";
beginblock(1LL);
LABEL_9:
sprintf(v14, "%s x %%ld", v7);
v8 = strf(v14, 0LL, v6);
outcomment(v8);
return endblock();
}
}
v9 = 0LL;
v10 = 0LL;
do
{
if ( v10 < *(_QWORD *)(a1 + 8 * v9) )
v10 = *(_QWORD *)(a1 + 8 * v9);
++v9;
}
while ( v9 < v5 );
if ( v10 <= 0xFFFFFFFF )
{
if ( v10 > 0xFFFF )
{
v7 = "0x%08lX";
}
else if ( v10 > 0xFF )
{
v7 = "0x%04lX";
}
else
{
v7 = "0x%02lX";
if ( v10 <= 0x63 )
{
v7 = "%lu";
beginblock(1LL);
goto LABEL_18;
}
}
}
else
{
v7 = "0x%016lX";
}
beginblock(1LL);
LABEL_18:
v11 = 0LL;
do
{
v12 = *(_QWORD *)(a1 + 8 * v11++);
outf(v7, v12);
}
while ( v11 < v5 );
if ( !v6 )
return endblock();
goto LABEL_9;
}
return out("{ 0 }");
}

original_code:
static void outxwords(void const *ptr, long size, int ndx)
{
    Elf64_Xword const *xwords = ptr;
    long count = size / sizeof *xwords;
    char fmtbuf[16];
    char const *fmt = ((void *)0);
    unsigned long max;
    long zeroes, i;
    (void)ndx;
    for (zeroes = 0 ; zeroes < count ; ++zeroes)
 if (xwords[count - zeroes - 1])
     break;
    if (zeroes == count) {
 out("{ 0 }");
 return;
    }
    if (zeroes < 255 || zeroes * 4 < count)
 zeroes = 0;
    zeroes &= ~3;
    count -= zeroes;
    max = 0;
    for (i = 0 ; i < count ; ++i) {
 if (max < xwords[i])
     max = xwords[i];
    }
    fmt = max > 0xFFFFFFFF ? "0x%016lX" :
   max > 0x0000FFFF ? "0x%08lX" :
   max > 0x000000FF ? "0x%04lX" : max > 99 ? "0x%02lX" : "%lu";
    beginblock(1);
    for (i = 0 ; i < count ; ++i)
 outf(fmt, xwords[i]);
    if (zeroes) {
 sprintf(fmtbuf, "%s x %%ld", fmt);
 outcomment(strf(fmtbuf, 0, zeroes));
    }
    endblock();
}

canonical_original_code:
static void outxwords(void *ptr, long size, int ndx)
{
    unsigned long *xwords = ptr;
    long count = size / sizeof *xwords;
    char fmtbuf[16];
    char *fmt = ((void *)0);
    unsigned long max;
    long zeroes; long i;
    (void)ndx;
    for (zeroes = 0 ; zeroes < count ; ++zeroes)
 if (xwords[count - zeroes - 1])
     break;
    if (zeroes == count) {
 out("{ 0 }");
 return;
    }
    if (zeroes < 255 || zeroes * 4 < count)
 zeroes = 0;
    zeroes &= ~3;
    count -= zeroes;
    max = 0;
    for (i = 0 ; i < count ; ++i) {
 if (max < xwords[i])
     max = xwords[i];
    }
    fmt = max > 0xFFFFFFFF ? "0x%016lX" :
   max > 0x0000FFFF ? "0x%08lX" :
   max > 0x000000FF ? "0x%04lX" : max > 99 ? "0x%02lX" : "%lu";
    beginblock(1);
    for (i = 0 ; i < count ; ++i)
 outf(fmt, xwords[i]);
    if (zeroes) {
 sprintf(fmtbuf, "%s x %%ld", fmt);
 outcomment(strf(fmtbuf, 0, zeroes));
    }
    endblock();
}

variable_types:
xwords
unsigned long *

variable_types:
fmtbuf
char [16]

variable_types:
fmt
char *

variable_types:
max
unsigned long

variable_types: zeroes long


outwords

decompiled_code:
__int64 __fastcall outwords(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rsi
_DWORD *v3; // rdx
__int64 v4; // rax
__int64 v5; // rbp
unsigned __int64 v6; // r12
const char *v7; // r14
__int64 v8; // rdi
__int64 v9; // rax
unsigned __int64 v10; // rdx
__int64 v11; // rbx
__int64 v12; // rsi
char v14[56]; // [rsp-38h] [rbp-38h] BYREF

v2 = a2 >> 2;
if ( v2 )
{
v3 = (_DWORD *)(a1 + 4 * v2 - 4);
v4 = 0LL;
v5 = v2;
while ( !*v3 )
{
++v4;
--v3;
if ( v2 == v4 )
return out("{ 0 }");
}
if ( v4 <= 254 || 4 * v4 < v2 )
{
v6 = 0LL;
}
else
{
v6 = v4 & 0xFFFFFFFFFFFFFFFCLL;
v5 = v2 - (v4 & 0xFFFFFFFFFFFFFFFCLL);
if ( v5 <= 0 )
{
v7 = "%u";
beginblock(1LL);
LABEL_9:
sprintf(v14, "%s x %%ld", v7);
v8 = strf(v14, 0LL, v6);
outcomment(v8);
return endblock();
}
}
v9 = 0LL;
v10 = 0LL;
do
{
if ( v10 < *(unsigned int *)(a1 + 4 * v9) )
v10 = *(unsigned int *)(a1 + 4 * v9);
++v9;
}
while ( v9 < v5 );
if ( v10 <= 0xFFFF )
{
if ( v10 > 0xFF )
{
v7 = "0x%04X";
}
else
{
v7 = "0x%02X";
if ( v10 <= 0x63 )
{
v7 = "%u";
beginblock(1LL);
goto LABEL_18;
}
}
}
else
{
v7 = "0x%08X";
}
beginblock(1LL);
LABEL_18:
v11 = 0LL;
do
{
v12 = *(unsigned int *)(a1 + 4 * v11++);
outf(v7, v12);
}
while ( v11 < v5 );
if ( !v6 )
return endblock();
goto LABEL_9;
}
return out("{ 0 }");
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // rsi
_DWORD *v3; // rdx
__int64 v4; // rax
__int64 v5; // rbp
unsigned __int64 v6; // r12
const char *v7; // r14
__int64 v8; // rdi
__int64 v9; // rax
unsigned __int64 v10; // rdx
__int64 v11; // rbx
__int64 v12; // rsi
char v14[56]; // [rsp-38h] [rbp-38h] BYREF

v2 = a2 >> 2;
if ( v2 )
{
v3 = (_DWORD *)(a1 + 4 * v2 - 4);
v4 = 0LL;
v5 = v2;
while ( !*v3 )
{
++v4;
--v3;
if ( v2 == v4 )
return out("{ 0 }");
}
if ( v4 <= 254 || 4 * v4 < v2 )
{
v6 = 0LL;
}
else
{
v6 = v4 & 0xFFFFFFFFFFFFFFFCLL;
v5 = v2 - (v4 & 0xFFFFFFFFFFFFFFFCLL);
if ( v5 <= 0 )
{
v7 = "%u";
beginblock(1LL);
LABEL_9:
sprintf(v14, "%s x %%ld", v7);
v8 = strf(v14, 0LL, v6);
outcomment(v8);
return endblock();
}
}
v9 = 0LL;
v10 = 0LL;
do
{
if ( v10 < *(unsigned int *)(a1 + 4 * v9) )
v10 = *(unsigned int *)(a1 + 4 * v9);
++v9;
}
while ( v9 < v5 );
if ( v10 <= 0xFFFF )
{
if ( v10 > 0xFF )
{
v7 = "0x%04X";
}
else
{
v7 = "0x%02X";
if ( v10 <= 0x63 )
{
v7 = "%u";
beginblock(1LL);
goto LABEL_18;
}
}
}
else
{
v7 = "0x%08X";
}
beginblock(1LL);
LABEL_18:
v11 = 0LL;
do
{
v12 = *(unsigned int *)(a1 + 4 * v11++);
outf(v7, v12);
}
while ( v11 < v5 );
if ( !v6 )
return endblock();
goto LABEL_9;
}
return out("{ 0 }");
}

original_code:
static void outwords(void const *ptr, long size, int ndx)
{
    Elf64_Word const *words = ptr;
    long count = size / sizeof *words;
    char fmtbuf[16];
    char const *fmt = ((void *)0);
    unsigned long max;
    long zeroes, i;
    (void)ndx;
    for (zeroes = 0 ; zeroes < count ; ++zeroes)
 if (words[count - zeroes - 1])
     break;
    if (zeroes == count) {
 out("{ 0 }");
 return;
    }
    if (zeroes < 255 || zeroes * 4 < count)
 zeroes = 0;
    zeroes &= ~3;
    count -= zeroes;
    max = 0;
    for (i = 0 ; i < count ; ++i) {
 if (max < words[i])
     max = words[i];
    }
    fmt = max > 0xFFFF ? "0x%08X" :
   max > 0x00FF ? "0x%04X" : max > 99 ? "0x%02X" : "%u";
    beginblock(1);
    for (i = 0 ; i < count ; ++i)
 outf(fmt, words[i]);
    if (zeroes) {
 sprintf(fmtbuf, "%s x %%ld", fmt);
 outcomment(strf(fmtbuf, 0, zeroes));
    }
    endblock();
}

canonical_original_code:
static void outwords(void *ptr, long size, int ndx)
{
    unsigned int *words = ptr;
    long count = size / sizeof *words;
    char fmtbuf[16];
    char *fmt = ((void *)0);
    unsigned long max;
    long zeroes; long i;
    (void)ndx;
    for (zeroes = 0 ; zeroes < count ; ++zeroes)
 if (words[count - zeroes - 1])
     break;
    if (zeroes == count) {
 out("{ 0 }");
 return;
    }
    if (zeroes < 255 || zeroes * 4 < count)
 zeroes = 0;
    zeroes &= ~3;
    count -= zeroes;
    max = 0;
    for (i = 0 ; i < count ; ++i) {
 if (max < words[i])
     max = words[i];
    }
    fmt = max > 0xFFFF ? "0x%08X" :
   max > 0x00FF ? "0x%04X" : max > 99 ? "0x%02X" : "%u";
    beginblock(1);
    for (i = 0 ; i < count ; ++i)
 outf(fmt, words[i]);
    if (zeroes) {
 sprintf(fmtbuf, "%s x %%ld", fmt);
 outcomment(strf(fmtbuf, 0, zeroes));
    }
    endblock();
}