insertSymClone

decompiled_code:
void __fastcall insertSymClone(__int64 a1, __int64 a2, unsigned int *a3)
{
__int64 v5; // r13
_QWORD *v6; // rbx
__int64 v7; // r14
size_t v8; // r15
char *v9; // rax
__int64 v10; // rdi
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rcx
char *src; // [rsp+8h] [rbp-40h]

if ( !a1 )
_assert_fail("me", "src/libelfu/modelops/reladd.c", 0xECu, "insertSymClone");
if ( !a2 )
_assert_fail("oldmsst", "src/libelfu/modelops/reladd.c", 0xEDu, "insertSymClone");
if ( !a3 )
_assert_fail("oldsym", "src/libelfu/modelops/reladd.c", 0xEEu, "insertSymClone");
v5 = *((_QWORD *)a3 + 3);
if ( !v5 || (v5 = elfu_mScnByOldscn(a1, *((_QWORD *)a3 + 3))) != 0 )
{
elfu_mSymtabAddGlobalDymtabIfNotPresent(a1);
v6 = malloc(0x38uLL);
if ( v6 )
{
src = (char *)(*(_QWORD *)(*(_QWORD *)(a2 + 72) + 64LL) + *a3);
v7 = *(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL);
v8 = *(_QWORD *)(v7 + 32) + strlen(src) + 1;
v9 = (char *)realloc(*(void **)(v7 + 64), v8);
if ( v9 )
{
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL) + 64LL) = v9;
v10 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL) + 32LL);
*(_DWORD *)v6 = v10;
strcpy(&v9[(unsigned int)v10], src);
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL) + 32LL) = v8;
v6[3] = v5;
*((_DWORD *)v6 + 8) = a3[8];
*((_BYTE *)v6 + 20) = *((_BYTE *)a3 + 20);
*((_BYTE *)v6 + 22) = *((_BYTE *)a3 + 22);
*((_DWORD *)v6 + 4) = a3[4];
*((_BYTE *)v6 + 21) = *((_BYTE *)a3 + 21);
v11 = *((_QWORD *)a3 + 1);
v6[1] = v11;
if ( v5 )
v6[1] = *(_QWORD *)(v5 + 16) + v11;
v12 = *(_QWORD *)(a1 + 112);
v6[5] = v12 + 96;
v13 = *(_QWORD *)(v12 + 104);
v6[6] = v13;
if ( v12 + 96 == *(_QWORD *)(v12 + 96) )
*(_QWORD *)(v12 + 96) = v6;
else
*(_QWORD *)(v13 + 40) = v6;
*(_QWORD *)(*(_QWORD *)(a1 + 112) + 104LL) = v6;
}
else
{
fwrite("insertSymClone: realloc() failed for strtab.\n", 1uLL, 0x2DuLL, stderr);
free(v6);
}
}
else
{
fwrite("insertSymClone: malloc() failed for newsym.\n", 1uLL, 0x2CuLL, stderr);
}
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2, unsigned int *a3)
{
__int64 v5; // r13
_QWORD *v6; // rbx
__int64 v7; // r14
size_t v8; // r15
char *v9; // rax
__int64 v10; // rdi
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rcx
char *src; // [rsp+8h] [rbp-40h]

if ( !a1 )
_assert_fail("me", "src/libelfu/modelops/reladd.c", 0xECu, "insertSymClone");
if ( !a2 )
_assert_fail("oldmsst", "src/libelfu/modelops/reladd.c", 0xEDu, "insertSymClone");
if ( !a3 )
_assert_fail("oldsym", "src/libelfu/modelops/reladd.c", 0xEEu, "insertSymClone");
v5 = *((_QWORD *)a3 + 3);
if ( !v5 || (v5 = elfu_mScnByOldscn(a1, *((_QWORD *)a3 + 3))) != 0 )
{
elfu_mSymtabAddGlobalDymtabIfNotPresent(a1);
v6 = malloc(0x38uLL);
if ( v6 )
{
src = (char *)(*(_QWORD *)(*(_QWORD *)(a2 + 72) + 64LL) + *a3);
v7 = *(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL);
v8 = *(_QWORD *)(v7 + 32) + strlen(src) + 1;
v9 = (char *)realloc(*(void **)(v7 + 64), v8);
if ( v9 )
{
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL) + 64LL) = v9;
v10 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL) + 32LL);
*(_DWORD *)v6 = v10;
strcpy(&v9[(unsigned int)v10], src);
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 112) + 72LL) + 32LL) = v8;
v6[3] = v5;
*((_DWORD *)v6 + 8) = a3[8];
*((_BYTE *)v6 + 20) = *((_BYTE *)a3 + 20);
*((_BYTE *)v6 + 22) = *((_BYTE *)a3 + 22);
*((_DWORD *)v6 + 4) = a3[4];
*((_BYTE *)v6 + 21) = *((_BYTE *)a3 + 21);
v11 = *((_QWORD *)a3 + 1);
v6[1] = v11;
if ( v5 )
v6[1] = *(_QWORD *)(v5 + 16) + v11;
v12 = *(_QWORD *)(a1 + 112);
v6[5] = v12 + 96;
v13 = *(_QWORD *)(v12 + 104);
v6[6] = v13;
if ( v12 + 96 == *(_QWORD *)(v12 + 96) )
*(_QWORD *)(v12 + 96) = v6;
else
*(_QWORD *)(v13 + 40) = v6;
*(_QWORD *)(*(_QWORD *)(a1 + 112) + 104LL) = v6;
}
else
{
fwrite("insertSymClone: realloc() failed for strtab.\n", 1uLL, 0x2DuLL, stderr);
free(v6);
}
}
else
{
fwrite("insertSymClone: malloc() failed for newsym.\n", 1uLL, 0x2CuLL, stderr);
}
}
}

original_code:
static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
{
  GElf_Xword newsize;
  char *newbuf;
  ElfuScn *newscn = ((void *)0);
  ElfuSym *newsym;
  char *oldsymname;
  ((void) sizeof ((me) ? 1 : 0), __extension__ ({ if (me) ; else __assert_fail ("me", "src/libelfu/modelops/reladd.c", 236, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((oldmsst) ? 1 : 0), __extension__ ({ if (oldmsst) ; else __assert_fail ("oldmsst", "src/libelfu/modelops/reladd.c", 237, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((oldsym) ? 1 : 0), __extension__ ({ if (oldsym) ; else __assert_fail ("oldsym", "src/libelfu/modelops/reladd.c", 238, __extension__ __PRETTY_FUNCTION__); }));
  if (oldsym->scnptr) {
    newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
    if (!newscn) {
      return;
    }
  }
  elfu_mSymtabAddGlobalDymtabIfNotPresent(me);
  newsym = malloc(sizeof(*newsym));
  if (!newsym) {
    do { fprintf(stderr, "insertSymClone: malloc() failed for newsym.\n"); } while(0);
    goto ERROR;
  }
  oldsymname = ((oldmsst)->linkptr->databuf + (oldsym->name));
  newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
  newbuf = realloc(me->symtab->linkptr->databuf, newsize);
  if (!newbuf) {
    do { fprintf(stderr, "insertSymClone: realloc() failed for strtab.\n"); } while(0);
    goto ERROR;
  }
  me->symtab->linkptr->databuf = newbuf;
  newsym->name = me->symtab->linkptr->shdr.sh_size;
  strcpy(newbuf + newsym->name, oldsymname);
  me->symtab->linkptr->shdr.sh_size = newsize;
  newsym->scnptr = newscn;
  newsym->shndx = oldsym->shndx;
  newsym->bind = oldsym->bind;
  newsym->other = oldsym->other;
  newsym->size = oldsym->size;
  newsym->type = oldsym->type;
  newsym->value = oldsym->value;
  if (newscn) {
    newsym->value += newscn->shdr.sh_addr;
  }
  do { (newsym)->elem.cqe_next = (void *)(&me->symtab->symtab.syms); (newsym)->elem.cqe_prev = (&me->symtab->symtab.syms)->cqh_last; if ((&me->symtab->symtab.syms)->cqh_first == (void *)(&me->symtab->symtab.syms)) (&me->symtab->symtab.syms)->cqh_first = (newsym); else (&me->symtab->symtab.syms)->cqh_last->elem.cqe_next = (newsym); (&me->symtab->symtab.syms)->cqh_last = (newsym); } while ( 0);
  return;
  ERROR:
  if (newsym) {
    free(newsym);
  }
}

canonical_original_code:
static void insertSymClone(struct ElfuElf *me, struct ElfuScn *oldmsst, struct ElfuSym *oldsym)
{
  unsigned long newsize;
  char *newbuf;
  struct ElfuScn *newscn = ((void *)0);
  struct ElfuSym *newsym;
  char *oldsymname;
  ((void) sizeof ((me) ? 1 : 0), __extension__ ({ if (me) ; else __assert_fail ("me", "src/libelfu/modelops/reladd.c", 236, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((oldmsst) ? 1 : 0), __extension__ ({ if (oldmsst) ; else __assert_fail ("oldmsst", "src/libelfu/modelops/reladd.c", 237, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((oldsym) ? 1 : 0), __extension__ ({ if (oldsym) ; else __assert_fail ("oldsym", "src/libelfu/modelops/reladd.c", 238, __extension__ __PRETTY_FUNCTION__); }));
  if (oldsym->scnptr) {
    newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
    if (!newscn) {
      return;
    }
  }
  elfu_mSymtabAddGlobalDymtabIfNotPresent(me);
  newsym = malloc(sizeof(*newsym));
  if (!newsym) {
    do { fprintf(stderr, "insertSymClone: malloc() failed for newsym.\n"); } while(0);
    goto ERROR;
  }
  oldsymname = ((oldmsst)->linkptr->databuf + (oldsym->name));
  newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
  newbuf = realloc(me->symtab->linkptr->databuf, newsize);
  if (!newbuf) {
    do { fprintf(stderr, "insertSymClone: realloc() failed for strtab.\n"); } while(0);
    goto ERROR;
  }
  me->symtab->linkptr->databuf = newbuf;
  newsym->name = me->symtab->linkptr->shdr.sh_size;
  strcpy(newbuf + newsym->name, oldsymname);
  me->symtab->linkptr->shdr.sh_size = newsize;
  newsym->scnptr = newscn;
  newsym->shndx = oldsym->shndx;
  newsym->bind = oldsym->bind;
  newsym->other = oldsym->other;
  newsym->size = oldsym->size;
  newsym->type = oldsym->type;
  newsym->value = oldsym->value;
  if (newscn) {
    newsym->value += newscn->shdr.sh_addr;
  }
  do { (newsym)->elem.cqe_next = (void *)(&me->symtab->symtab.syms); (newsym)->elem.cqe_prev = (&me->symtab->symtab.syms)->cqh_last; if ((&me->symtab->symtab.syms)->cqh_first == (void *)(&me->symtab->symtab.syms)) (&me->symtab->symtab.syms)->cqh_first = (newsym); else (&me->symtab->symtab.syms)->cqh_last->elem.cqe_next = (newsym); (&me->symtab->symtab.syms)->cqh_last = (newsym); } while ( 0);
  return;
  ERROR:
  if (newsym) {
    free(newsym);
  }
}

variable_types:
oldmsst
struct ElfuScn *

variable_types:
oldsym
struct ElfuSym *

variable_types:
newsize
unsigned long

variable_types:
newbuf
char *

variable_types: struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab { struct Syms { struct ElfuSym *cqh_first; struct ElfuSym *cqh_last; } syms; } symtab; struct ElfuReltab { struct Rels { struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *cqh_first; struct ElfuRel *cqh_last; } rels; } reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; } *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; } *

variable_types:
oldsymname
char *


mergeSymtab

repo: norly/centaur

decompiled_code:
__int64 __fastcall mergeSymtab(__int64 a1, __int64 a2)
{
__int64 v3; // rsi
__int64 i; // rbx

if ( !a1 )
_assert_fail("me", "src/libelfu/modelops/reladd.c", 0x136u, "mergeSymtab");
if ( !a2 )
_assert_fail("mrel", "src/libelfu/modelops/reladd.c", 0x137u, "mergeSymtab");
v3 = *(_QWORD *)(a2 + 112);
for ( i = *(_QWORD *)(v3 + 96); i != v3 + 96; v3 = *(_QWORD *)(a2 + 112) )
{
insertSymClone(a1, v3, (unsigned int *)i);
i = *(_QWORD *)(i + 40);
}
return elfu_mSymtabFlatten(a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v3; // rsi
__int64 i; // rbx

if ( !a1 )
_assert_fail("me", "src/libelfu/modelops/reladd.c", 0x136u, "mergeSymtab");
if ( !a2 )
_assert_fail("mrel", "src/libelfu/modelops/reladd.c", 0x137u, "mergeSymtab");
v3 = *(_QWORD *)(a2 + 112);
for ( i = *(_QWORD *)(v3 + 96); i != v3 + 96; v3 = *(_QWORD *)(a2 + 112) )
{
func2(a1, v3, (unsigned int *)i);
i = *(_QWORD *)(i + 40);
}
return elfu_mSymtabFlatten(a1);
}

original_code:
static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
{
  ElfuSym *sym;
  ((void) sizeof ((me) ? 1 : 0), __extension__ ({ if (me) ; else __assert_fail ("me", "src/libelfu/modelops/reladd.c", 310, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((mrel) ? 1 : 0), __extension__ ({ if (mrel) ; else __assert_fail ("mrel", "src/libelfu/modelops/reladd.c", 311, __extension__ __PRETTY_FUNCTION__); }));
  for ((sym) = ((&mrel->symtab->symtab.syms)->cqh_first); (sym) != (const void *)(&mrel->symtab->symtab.syms); (sym) = ((sym)->elem.cqe_next)) {
    insertSymClone(me, mrel->symtab, sym);
  }
  elfu_mSymtabFlatten(me);
}

canonical_original_code:
static void mergeSymtab(struct ElfuElf *me, struct ElfuElf *mrel)
{
  struct ElfuSym *sym;
  ((void) sizeof ((me) ? 1 : 0), __extension__ ({ if (me) ; else __assert_fail ("me", "src/libelfu/modelops/reladd.c", 310, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((mrel) ? 1 : 0), __extension__ ({ if (mrel) ; else __assert_fail ("mrel", "src/libelfu/modelops/reladd.c", 311, __extension__ __PRETTY_FUNCTION__); }));
  for ((sym) = ((&mrel->symtab->symtab.syms)->cqh_first); (sym) != (void *)(&mrel->symtab->symtab.syms); (sym) = ((sym)->elem.cqe_next)) {
    insertSymClone(me, mrel->symtab, sym);
  }
  elfu_mSymtabFlatten(me);
}

variable_types: struct ElfuElf { int elfclass; 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; } ehdr; struct PhdrList { struct ElfuPhdr { struct Elf64_Phdr { unsigned int p_type; unsigned int p_flags; unsigned long p_offset; unsigned long p_vaddr; unsigned long p_paddr; unsigned long p_filesz; unsigned long p_memsz; unsigned long p_align; } phdr; struct ChildScnList { struct ElfuScn { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab { struct Syms { struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; } *cqh_first; struct ElfuSym *cqh_last; } syms; } symtab; struct ElfuReltab { struct Rels { struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *cqh_first; struct ElfuRel *cqh_last; } rels; } reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; } *cqh_first; struct ElfuScn *cqh_last; } childScnList; struct ChildPhdrList { struct ElfuPhdr *cqh_first; struct ElfuPhdr *cqh_last; } childPhdrList; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elemChildPhdr; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elem; } *cqh_first; struct ElfuPhdr *cqh_last; } phdrList; struct OrphanScnList { struct ElfuScn *cqh_first; struct ElfuScn *cqh_last; } orphanScnList; struct ElfuScn *shstrtab; struct ElfuScn *symtab; } *

variable_types:
mrel
struct ElfuElf *

return_type: void

user_defined_types: struct ElfuElf { int elfclass; struct Elf64_Ehdr ehdr; struct PhdrList phdrList; struct OrphanScnList orphanScnList; struct ElfuScn *shstrtab; struct ElfuScn *symtab; }

user_defined_types: struct PhdrList { struct ElfuPhdr *cqh_first; struct ElfuPhdr *cqh_last; }

user_defined_types: struct ElfuScn { struct Elf64_Shdr shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab symtab; struct ElfuReltab reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; }

user_defined_types: struct ElfuPhdr { struct Elf64_Phdr phdr; struct ChildScnList childScnList; struct ChildPhdrList childPhdrList; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elemChildPhdr; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elem; }

user_defined_types: struct ChildScnList { struct ElfuScn *cqh_first; struct ElfuScn *cqh_last; }

user_defined_types: struct Syms { struct ElfuSym *cqh_first; struct ElfuSym *cqh_last; }

user_defined_types: struct Rels { struct ElfuRel *cqh_first; struct ElfuRel *cqh_last; }


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

if ( !a1 )
_assert_fail("me", "src/libelfu/modelops/reladd.c", 0x144u, "elfu_mReladd");
if ( !a2 )
_assert_fail("mrel", "src/libelfu/modelops/reladd.c", 0x145u, "elfu_mReladd");
elfu_mScnForall(a2, subScnAdd1, a1, 0LL);
mergeSymtab(a1, a2);
if ( elfu_mScnForall(a2, subScnAdd2, a1, 0LL) )
{
fwrite("elfu_mReladd: Reladd aborted. Target model is unclean.\n", 1uLL, 0x37uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
elfu_mLayoutAuto(a1);
result = 0LL;
}
return result;
}

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

if ( !a1 )
_assert_fail("me", "src/libelfu/modelops/reladd.c", 0x144u, "elfu_mReladd");
if ( !a2 )
_assert_fail("mrel", "src/libelfu/modelops/reladd.c", 0x145u, "elfu_mReladd");
elfu_mScnForall(a2, subScnAdd1, a1, 0LL);
func3(a1, a2);
if ( elfu_mScnForall(a2, subScnAdd2, a1, 0LL) )
{
fwrite("elfu_mReladd: Reladd aborted. Target model is unclean.\n", 1uLL, 0x37uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
elfu_mLayoutAuto(a1);
result = 0LL;
}
return result;
}

original_code:
int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
{
  ((void) sizeof ((me) ? 1 : 0), __extension__ ({ if (me) ; else __assert_fail ("me", "src/libelfu/modelops/reladd.c", 324, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((mrel) ? 1 : 0), __extension__ ({ if (mrel) ; else __assert_fail ("mrel", "src/libelfu/modelops/reladd.c", 325, __extension__ __PRETTY_FUNCTION__); }));
  elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, ((void *)0));
  mergeSymtab(me, mrel);
  if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, ((void *)0))) {
    do { fprintf(stderr, "elfu_mReladd: Reladd aborted. Target model is unclean.\n"); } while(0);
    return -1;
  }
  elfu_mLayoutAuto(me);
  return 0;
}

canonical_original_code:
int elfu_mReladd(struct ElfuElf *me, struct ElfuElf *mrel)
{
  ((void) sizeof ((me) ? 1 : 0), __extension__ ({ if (me) ; else __assert_fail ("me", "src/libelfu/modelops/reladd.c", 324, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((mrel) ? 1 : 0), __extension__ ({ if (mrel) ; else __assert_fail ("mrel", "src/libelfu/modelops/reladd.c", 325, __extension__ __PRETTY_FUNCTION__); }));
  elfu_mScnForall((struct ElfuElf *)mrel, subScnAdd1, me, ((void *)0));
  mergeSymtab(me, mrel);
  if (elfu_mScnForall((struct ElfuElf *)mrel, subScnAdd2, me, ((void *)0))) {
    do { fprintf(stderr, "elfu_mReladd: Reladd aborted. Target model is unclean.\n"); } while(0);
    return -1;
  }
  elfu_mLayoutAuto(me);
  return 0;
}

return_type: int

user_defined_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; }

user_defined_types: struct OrphanScnList { struct ElfuScn *cqh_first; struct ElfuScn *cqh_last; }

user_defined_types: struct Elf64_Phdr { unsigned int p_type; unsigned int p_flags; unsigned long p_offset; unsigned long p_vaddr; unsigned long p_paddr; unsigned long p_filesz; unsigned long p_memsz; unsigned long p_align; }

user_defined_types: struct ChildPhdrList { struct ElfuPhdr *cqh_first; struct ElfuPhdr *cqh_last; }

user_defined_types: struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; }

user_defined_types: struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; }

user_defined_types: struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; }


decompiled_code:
int __fastcall puts_alternative(char *s)
{
puts("puts_alternative() has been reached.");
puts(s);
return puts("puts_alternative() is returning.");
}

canonical_decompiled_code:
int __fastcall func0(char *s)
{
puts("puts_alternative() has been reached.");
puts(s);
return puts("puts_alternative() is returning.");
}

original_code:
void puts_alternative(char *arg)
{
  puts("puts_alternative() has been reached.");
  puts(arg);
  puts("puts_alternative() is returning.");
}

variable_types:
arg
char *


decompiled_code:
int puts_noarg()
{
puts("puts_noarg() has been reached.");
return puts("puts_noarg() is returning.");
}

canonical_decompiled_code:
int func0()
{
puts("puts_noarg() has been reached.");
return puts("puts_noarg() is returning.");
}

original_code:
void puts_noarg()
{
  puts("puts_noarg() has been reached.");
  puts("puts_noarg() is returning.");
}


decompiled_code:
__int64 __fastcall elfu_mRelocate(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rbx
int *v7; // r12
__int64 v8; // rax
__int64 v9; // rbp
int v10; // eax
_BOOL8 v11; // rdx
__int64 v12; // rax
int v13; // eax
__int64 v14; // rax
int v15; // esi
unsigned __int8 v16; // al
int v17; // ecx
unsigned __int8 v18; // al
const char *v19; // rax
_DWORD v20[16]; // [rsp+18h] [rbp-40h] BYREF

if ( !a2 )
_assert_fail("mstarget", "src/libelfu/modelops/relocate.c", 0x1Du, "elfu_mRelocate");
if ( !a3 )
_assert_fail("msrt", "src/libelfu/modelops/relocate.c", 0x1Eu, "elfu_mRelocate");
fprintf(stdout, "Relocating in section of type %u size %x\n", *(unsigned int *)(a2 + 4), *(unsigned int *)(a2 + 32));
v5 = *(_QWORD *)(a3 + 112);
if ( v5 == a3 + 112 )
return 0LL;
while ( 1 )
{
v7 = (int *)(*(_QWORD *)(a2 + 64) + *(_QWORD *)v5);
v8 = elfu_mSymtabIndexToSym(*(_QWORD *)(a3 + 72), *(unsigned int *)(v5 + 8));
v9 = v8;
if ( !v8 )
_assert_fail("sym", "src/libelfu/modelops/relocate.c", 0x2Bu, "elfu_mRelocate");
v10 = elfu_mSymtabLookupSymToAddr(a1, *(_QWORD *)(a3 + 72), v8, v20);
v11 = v10 == 0;
if ( v10 )
{
if ( !*(_DWORD *)(v9 + 32) )
{
v12 = elfu_mSymtabSymToName(*(_QWORD *)(a3 + 72), v9, v11);
v13 = elfu_mDynLookupPltAddrByName(a1, v12, v20);
v11 = 1LL;
if ( v13 )
{
v14 = elfu_mSymtabSymToName(*(_QWORD *)(a3 + 72), v9, 1LL);
v11 = (unsigned int)elfu_mDynLookupReldynAddrByName(a1, v14, v20) == 0;
}
}
}
if ( *(_DWORD *)a1 == 1 )
{
if ( *(_DWORD *)(v5 + 16) )
v15 = *(_DWORD *)(v5 + 20);
else
v15 = *v7;
if ( *(_WORD *)(a1 + 26) != 3 )
{
LABEL_29:
fwrite("elfu_mRelocate: Unknown machine type. Aborting.\n", 1uLL, 0x30uLL, stderr);
goto LABEL_10;
}
v16 = *(_BYTE *)(v5 + 12);
switch ( v16 )
{
case 1u:
if ( !v11 )
goto LABEL_48;
*v7 = v15 + v20[0];
break;
case 2u:
if ( !v11 )
goto LABEL_48;
*v7 = v20[0] - *(_DWORD *)(a2 + 16) - *(_DWORD *)v5 + v15;
break;
case 0u:
goto LABEL_24;
default:
fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d\n", v16);
break;
}
goto LABEL_10;
}
if ( *(_DWORD *)a1 != 2 )
goto LABEL_10;
if ( *(_DWORD *)(v5 + 16) )
v17 = *(_DWORD *)(v5 + 20);
else
v17 = *v7;
if ( *(_WORD *)(a1 + 26) != 62 )
goto LABEL_29;
v18 = *(_BYTE *)(v5 + 12);
if ( v18 == 2 )
break;
if ( v18 > 2u )
{
if ( v18 == 10 )
{
if ( !v11 )
goto LABEL_48;
*v7 = v20[0] + v17;
}
else
{
fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d", v18);
}
}
else
{
if ( !v18 )
{
LABEL_24:
fwrite("Skipping relocation: R_386_NONE\n", 1uLL, 0x20uLL, stdout);
goto LABEL_10;
}
if ( !v11 )
goto LABEL_48;
*v7 = v20[0] + v17;
}
LABEL_10:
v5 = *(_QWORD *)(v5 + 24);
if ( v5 == a3 + 112 )
return 0LL;
}
if ( v11 )
{
*v7 = v20[0] - *(_DWORD *)(a2 + 16) - *(_DWORD *)v5 + v17;
goto LABEL_10;
}
LABEL_48:
v19 = (const char *)elfu_mSymtabSymToName(*(_QWORD *)(a3 + 72), v9, v11);
fprintf(stderr, "elfu_mRelocate: Could not resolve symbol %s. Aborting.\n", v19);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rbx
int *v7; // r12
__int64 v8; // rax
__int64 v9; // rbp
int v10; // eax
_BOOL8 v11; // rdx
__int64 v12; // rax
int v13; // eax
__int64 v14; // rax
int v15; // esi
unsigned __int8 v16; // al
int v17; // ecx
unsigned __int8 v18; // al
const char *v19; // rax
_DWORD v20[16]; // [rsp+18h] [rbp-40h] BYREF

if ( !a2 )
_assert_fail("mstarget", "src/libelfu/modelops/relocate.c", 0x1Du, "elfu_mRelocate");
if ( !a3 )
_assert_fail("msrt", "src/libelfu/modelops/relocate.c", 0x1Eu, "elfu_mRelocate");
fprintf(stdout, "Relocating in section of type %u size %x\n", *(unsigned int *)(a2 + 4), *(unsigned int *)(a2 + 32));
v5 = *(_QWORD *)(a3 + 112);
if ( v5 == a3 + 112 )
return 0LL;
while ( 1 )
{
v7 = (int *)(*(_QWORD *)(a2 + 64) + *(_QWORD *)v5);
v8 = elfu_mSymtabIndexToSym(*(_QWORD *)(a3 + 72), *(unsigned int *)(v5 + 8));
v9 = v8;
if ( !v8 )
_assert_fail("sym", "src/libelfu/modelops/relocate.c", 0x2Bu, "elfu_mRelocate");
v10 = elfu_mSymtabLookupSymToAddr(a1, *(_QWORD *)(a3 + 72), v8, v20);
v11 = v10 == 0;
if ( v10 )
{
if ( !*(_DWORD *)(v9 + 32) )
{
v12 = elfu_mSymtabSymToName(*(_QWORD *)(a3 + 72), v9, v11);
v13 = elfu_mDynLookupPltAddrByName(a1, v12, v20);
v11 = 1LL;
if ( v13 )
{
v14 = elfu_mSymtabSymToName(*(_QWORD *)(a3 + 72), v9, 1LL);
v11 = (unsigned int)elfu_mDynLookupReldynAddrByName(a1, v14, v20) == 0;
}
}
}
if ( *(_DWORD *)a1 == 1 )
{
if ( *(_DWORD *)(v5 + 16) )
v15 = *(_DWORD *)(v5 + 20);
else
v15 = *v7;
if ( *(_WORD *)(a1 + 26) != 3 )
{
LABEL_29:
fwrite("elfu_mRelocate: Unknown machine type. Aborting.\n", 1uLL, 0x30uLL, stderr);
goto LABEL_10;
}
v16 = *(_BYTE *)(v5 + 12);
switch ( v16 )
{
case 1u:
if ( !v11 )
goto LABEL_48;
*v7 = v15 + v20[0];
break;
case 2u:
if ( !v11 )
goto LABEL_48;
*v7 = v20[0] - *(_DWORD *)(a2 + 16) - *(_DWORD *)v5 + v15;
break;
case 0u:
goto LABEL_24;
default:
fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d\n", v16);
break;
}
goto LABEL_10;
}
if ( *(_DWORD *)a1 != 2 )
goto LABEL_10;
if ( *(_DWORD *)(v5 + 16) )
v17 = *(_DWORD *)(v5 + 20);
else
v17 = *v7;
if ( *(_WORD *)(a1 + 26) != 62 )
goto LABEL_29;
v18 = *(_BYTE *)(v5 + 12);
if ( v18 == 2 )
break;
if ( v18 > 2u )
{
if ( v18 == 10 )
{
if ( !v11 )
goto LABEL_48;
*v7 = v20[0] + v17;
}
else
{
fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d", v18);
}
}
else
{
if ( !v18 )
{
LABEL_24:
fwrite("Skipping relocation: R_386_NONE\n", 1uLL, 0x20uLL, stdout);
goto LABEL_10;
}
if ( !v11 )
goto LABEL_48;
*v7 = v20[0] + v17;
}
LABEL_10:
v5 = *(_QWORD *)(v5 + 24);
if ( v5 == a3 + 112 )
return 0LL;
}
if ( v11 )
{
*v7 = v20[0] - *(_DWORD *)(a2 + 16) - *(_DWORD *)v5 + v17;
goto LABEL_10;
}
LABEL_48:
v19 = (const char *)elfu_mSymtabSymToName(*(_QWORD *)(a3 + 72), v9, v11);
fprintf(stderr, "elfu_mRelocate: Could not resolve symbol %s. Aborting.\n", v19);
return 0xFFFFFFFFLL;
}

original_code:
int elfu_mRelocate(ElfuElf *metarget, ElfuScn *mstarget, ElfuScn *msrt)
{
  ElfuRel *rel;
  ElfuSym *sym;
  ((void) sizeof ((mstarget) ? 1 : 0), __extension__ ({ if (mstarget) ; else __assert_fail ("mstarget", "src/libelfu/modelops/relocate.c", 29, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((msrt) ? 1 : 0), __extension__ ({ if (msrt) ; else __assert_fail ("msrt", "src/libelfu/modelops/relocate.c", 30, __extension__ __PRETTY_FUNCTION__); }));
  do { fprintf(stdout, "Relocating in section of type %u size %x\n", mstarget->shdr.sh_type, (unsigned)mstarget->shdr.sh_size); } while(0);
  for ((rel) = ((&msrt->reltab.rels)->cqh_first); (rel) != (const void *)(&msrt->reltab.rels); (rel) = ((rel)->elem.cqe_next)) {
    Elf32_Word *dest32 = (Elf32_Word*)(mstarget->databuf + rel->offset);
    Elf64_Word *dest64 = (Elf64_Word*)(mstarget->databuf + rel->offset);
    GElf_Addr s;
    int haveSymval = 0;
    sym = elfu_mSymtabIndexToSym(msrt->linkptr, rel->sym);
    ((void) sizeof ((sym) ? 1 : 0), __extension__ ({ if (sym) ; else __assert_fail ("sym", "src/libelfu/modelops/relocate.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    haveSymval = !elfu_mSymtabLookupSymToAddr(metarget,
                                              msrt->linkptr,
                                              sym,
                                              &s);
    if (!haveSymval) {
      if (sym->shndx == 0) {
        haveSymval = !elfu_mDynLookupPltAddrByName(metarget,
                                     elfu_mSymtabSymToName(msrt->linkptr, sym),
                                     &s);
        if (!haveSymval) {
          haveSymval = !elfu_mDynLookupReldynAddrByName(metarget,
                                       elfu_mSymtabSymToName(msrt->linkptr, sym),
                                       &s);
        }
      }
    }
    if (metarget->elfclass == 1) {
      Elf32_Word a32 = rel->addendUsed ? rel->addend : *dest32;
      Elf32_Addr p32 = mstarget->shdr.sh_addr + rel->offset;
      Elf32_Addr s32 = (Elf32_Addr)s;
      switch(metarget->ehdr.e_machine) {
        case 3:
          switch(rel->type) {
            case 0:
              do { fprintf(stdout, "Skipping relocation: R_386_NONE\n"); } while(0);
              break;
            case 1:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s32 + a32;
              break;
            case 2:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s32 + a32 - p32;
              break;
            default:
              do { fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d\n", rel->type); } while(0);
          }
          break;
        default:
          do { fprintf(stderr, "elfu_mRelocate: Unknown machine type. Aborting.\n"); } while(0);
      }
    } else if (metarget->elfclass == 2) {
      Elf64_Word a64 = rel->addendUsed ? rel->addend : *dest64;
      Elf64_Addr p64 = mstarget->shdr.sh_addr + rel->offset;
      Elf64_Addr s64 = (Elf64_Addr)s;
      switch(metarget->ehdr.e_machine) {
        case 62:
          switch(rel->type) {
            case 0:
              do { fprintf(stdout, "Skipping relocation: R_386_NONE\n"); } while(0);
              break;
            case 1:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest64 = s64 + a64;
              break;
            case 2:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s64 + a64 - p64;
              break;
            case 10:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s64 + a64;
              break;
            default:
              do { fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d", rel->type); } while(0);
          }
          break;
        default:
          do { fprintf(stderr, "elfu_mRelocate: Unknown machine type. Aborting.\n"); } while(0);
      }
    }
  }
  return 0;
  MISSINGSYM:
  do { fprintf(stderr, "elfu_mRelocate: Could not resolve symbol %s. Aborting.\n", elfu_mSymtabSymToName(msrt->linkptr, sym)); } while(0);
  return -1;
}

canonical_original_code:
int elfu_mRelocate(struct ElfuElf *metarget, struct ElfuScn *mstarget, struct ElfuScn *msrt)
{
  struct ElfuRel *rel;
  struct ElfuSym *sym;
  ((void) sizeof ((mstarget) ? 1 : 0), __extension__ ({ if (mstarget) ; else __assert_fail ("mstarget", "src/libelfu/modelops/relocate.c", 29, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((msrt) ? 1 : 0), __extension__ ({ if (msrt) ; else __assert_fail ("msrt", "src/libelfu/modelops/relocate.c", 30, __extension__ __PRETTY_FUNCTION__); }));
  do { fprintf(stdout, "Relocating in section of type %u size %x\n", mstarget->shdr.sh_type, (unsigned int)mstarget->shdr.sh_size); } while(0);
  for ((rel) = ((&msrt->reltab.rels)->cqh_first); (rel) != (void *)(&msrt->reltab.rels); (rel) = ((rel)->elem.cqe_next)) {
    unsigned int *dest32 = (Elf32_Word*)(mstarget->databuf + rel->offset);
    unsigned int *dest64 = (Elf64_Word*)(mstarget->databuf + rel->offset);
    unsigned long s;
    int haveSymval = 0;
    sym = elfu_mSymtabIndexToSym(msrt->linkptr, rel->sym);
    ((void) sizeof ((sym) ? 1 : 0), __extension__ ({ if (sym) ; else __assert_fail ("sym", "src/libelfu/modelops/relocate.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    haveSymval = !elfu_mSymtabLookupSymToAddr(metarget,
                                              msrt->linkptr,
                                              sym,
                                              &s);
    if (!haveSymval) {
      if (sym->shndx == 0) {
        haveSymval = !elfu_mDynLookupPltAddrByName(metarget,
                                     elfu_mSymtabSymToName(msrt->linkptr, sym),
                                     &s);
        if (!haveSymval) {
          haveSymval = !elfu_mDynLookupReldynAddrByName(metarget,
                                       elfu_mSymtabSymToName(msrt->linkptr, sym),
                                       &s);
        }
      }
    }
    if (metarget->elfclass == 1) {
      unsigned int a32 = rel->addendUsed ? rel->addend : *dest32;
      unsigned int p32 = mstarget->shdr.sh_addr + rel->offset;
      unsigned int s32 = (Elf32_Addr)s;
      switch(metarget->ehdr.e_machine) {
        case 3:
          switch(rel->type) {
            case 0:
              do { fprintf(stdout, "Skipping relocation: R_386_NONE\n"); } while(0);
              break;
            case 1:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s32 + a32;
              break;
            case 2:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s32 + a32 - p32;
              break;
            default:
              do { fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d\n", rel->type); } while(0);
          }
          break;
        default:
          do { fprintf(stderr, "elfu_mRelocate: Unknown machine type. Aborting.\n"); } while(0);
      }
    } else if (metarget->elfclass == 2) {
      unsigned int a64 = rel->addendUsed ? rel->addend : *dest64;
      unsigned long p64 = mstarget->shdr.sh_addr + rel->offset;
      unsigned long s64 = (Elf64_Addr)s;
      switch(metarget->ehdr.e_machine) {
        case 62:
          switch(rel->type) {
            case 0:
              do { fprintf(stdout, "Skipping relocation: R_386_NONE\n"); } while(0);
              break;
            case 1:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest64 = s64 + a64;
              break;
            case 2:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s64 + a64 - p64;
              break;
            case 10:
              if (!haveSymval) {
                goto MISSINGSYM;
              }
              *dest32 = s64 + a64;
              break;
            default:
              do { fprintf(stdout, "elfu_mRelocate: Skipping unknown relocation type %d", rel->type); } while(0);
          }
          break;
        default:
          do { fprintf(stderr, "elfu_mRelocate: Unknown machine type. Aborting.\n"); } while(0);
      }
    }
  }
  return 0;
  MISSINGSYM:
  do { fprintf(stderr, "elfu_mRelocate: Could not resolve symbol %s. Aborting.\n", elfu_mSymtabSymToName(msrt->linkptr, sym)); } while(0);
  return -1;
}

variable_types:
mstarget
struct ElfuScn *

variable_types:
msrt
struct ElfuScn *

variable_types: struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *

variable_types:
s
unsigned long

variable_types: haveSymval int


decompiled_code:
size_t sub_with_global()
{
fwrite("sub_with_global() has been reached.\n", 1uLL, 0x24uLL, stdout);
return fwrite("sub_with_global() is returning.\n", 1uLL, 0x20uLL, stdout);
}

canonical_decompiled_code:
size_t func0()
{
fwrite("sub_with_global() has been reached.\n", 1uLL, 0x24uLL, stdout);
return fwrite("sub_with_global() is returning.\n", 1uLL, 0x20uLL, stdout);
}

original_code:
void sub_with_global()
{
  fprintf(stdout, "sub_with_global() has been reached.\n");
  fprintf(stdout, "sub_with_global() is returning.\n");
}


printUsage

decompiled_code:
int __fastcall printUsage(const char *a1)
{
printf("Usage: %s -i <inputfile> [OPTIONS]\n", a1);
return puts(
"\n"
"Options, executed in order given:\n"
" -h, --help Print this help message\n"
"\n"
" -i, --input infile Load new ELF file (must be first command)\n"
"\n"
" -c, --check Do a few sanity checks and print any errors\n"
" -d, --dump Dump current model state (debug only)\n"
" --reladd obj.o Insert object file contents\n"
" --detour from,to Write a jump to <to> at <from>\n"
" -o, --output outfile Where to write the modified ELF file to\n");
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
printf("Usage: %s -i <inputfile> [OPTIONS]\n", a1);
return puts(
"\n"
"Options, executed in order given:\n"
" -h, --help Print this help message\n"
"\n"
" -i, --input infile Load new ELF file (must be first command)\n"
"\n"
" -c, --check Do a few sanity checks and print any errors\n"
" -d, --dump Dump current model state (debug only)\n"
" --reladd obj.o Insert object file contents\n"
" --detour from,to Write a jump to <to> at <from>\n"
" -o, --output outfile Where to write the modified ELF file to\n");
}

original_code:
static void printUsage(char *progname)
{
  printf("Usage: %s -i <inputfile> [OPTIONS]\n", progname);
  printf("\n"
         "Options, executed in order given:\n"
         "  -h, --help                     Print this help message\n"
         "\n"
         "  -i, --input         infile     Load new ELF file (must be first command)\n"
         "\n"
         "  -c, --check                    Do a few sanity checks and print any errors\n"
         "  -d, --dump                     Dump current model state (debug only)\n"
         "      --reladd        obj.o      Insert object file contents\n"
         "      --detour        from,to    Write a jump to <to> at <from>\n"
         "  -o, --output        outfile    Where to write the modified ELF file to\n"
         "\n");
}

variable_types:
progname
char *


decompiled_code:
__int64 __fastcall elfu_mCheck(__int64 a1)
{
__int64 v1; // rax
_QWORD *v2; // rbp
unsigned __int64 v3; // rbx
__int64 v4; // rcx
__int64 v5; // rdx
const char *v6; // r14
const char *v7; // rax
unsigned __int64 v8; // rbx
const char *v9; // rax
unsigned __int64 i; // rbx
const char *v11; // rax
__int64 v12; // rsi
unsigned __int64 v13; // rax
unsigned __int64 v14; // rdx
unsigned __int64 v16[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = elfu_mScnSortedByOffset(a1, v16);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = (_QWORD *)v1;
if ( v16[0] == 1 )
goto LABEL_15;
v3 = 0LL;
do
{
v5 = v2[v3];
v4 = 0LL;
if ( *(_DWORD *)(v5 + 4) != 8 )
v4 = *(_QWORD *)(v5 + 32);
if ( (unsigned __int64)(v4 + *(_QWORD *)(v5 + 24)) > *(_QWORD *)(v2[++v3] + 24LL) )
{
v6 = (const char *)elfu_mScnName(a1);
v7 = (const char *)elfu_mScnName(a1);
fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n", v7, v6);
}
}
while ( v3 < v16[0] - 1 );
if ( v16[0] )
{
LABEL_15:
v8 = 0LL;
do
{
if ( *(_QWORD *)(v2[v8] + 24LL) < (unsigned __int64)*(unsigned __int16 *)(a1 + 60) )
{
v9 = (const char *)elfu_mScnName(a1);
fprintf(stderr, "elfu_check: Found section overlapping with EHDR: %s.\n", v9);
}
++v8;
}
while ( v16[0] > v8 );
if ( v16[0] )
{
for ( i = 0LL; v16[0] > i; ++i )
{
v12 = v2[i];
v13 = *(_QWORD *)(v12 + 24);
v14 = *(_QWORD *)(a1 + 40);
if ( v13 == v14 )
{
if ( *(_DWORD *)(v12 + 4) == 8
|| !*(_QWORD *)(v12 + 32)
|| !(*(unsigned __int16 *)(a1 + 64) * *(unsigned __int16 *)(a1 + 62)) )
{
continue;
}
}
else
{
if ( v13 > v14 )
goto LABEL_27;
if ( *(_DWORD *)(v12 + 4) == 8 )
goto LABEL_29;
}
if ( v14 >= *(_QWORD *)(v12 + 32) + v13 )
{
LABEL_29:
if ( v13 < v14 )
continue;
LABEL_27:
if ( v13 >= *(unsigned __int16 *)(a1 + 64) * *(unsigned __int16 *)(a1 + 62) + v14 )
continue;
}
v11 = (const char *)elfu_mScnName(a1);
fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n", v11);
}
}
}
free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax
_QWORD *v2; // rbp
unsigned __int64 v3; // rbx
__int64 v4; // rcx
__int64 v5; // rdx
const char *v6; // r14
const char *v7; // rax
unsigned __int64 v8; // rbx
const char *v9; // rax
unsigned __int64 i; // rbx
const char *v11; // rax
__int64 v12; // rsi
unsigned __int64 v13; // rax
unsigned __int64 v14; // rdx
unsigned __int64 v16[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = elfu_mScnSortedByOffset(a1, v16);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = (_QWORD *)v1;
if ( v16[0] == 1 )
goto LABEL_15;
v3 = 0LL;
do
{
v5 = v2[v3];
v4 = 0LL;
if ( *(_DWORD *)(v5 + 4) != 8 )
v4 = *(_QWORD *)(v5 + 32);
if ( (unsigned __int64)(v4 + *(_QWORD *)(v5 + 24)) > *(_QWORD *)(v2[++v3] + 24LL) )
{
v6 = (const char *)elfu_mScnName(a1);
v7 = (const char *)elfu_mScnName(a1);
fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n", v7, v6);
}
}
while ( v3 < v16[0] - 1 );
if ( v16[0] )
{
LABEL_15:
v8 = 0LL;
do
{
if ( *(_QWORD *)(v2[v8] + 24LL) < (unsigned __int64)*(unsigned __int16 *)(a1 + 60) )
{
v9 = (const char *)elfu_mScnName(a1);
fprintf(stderr, "elfu_check: Found section overlapping with EHDR: %s.\n", v9);
}
++v8;
}
while ( v16[0] > v8 );
if ( v16[0] )
{
for ( i = 0LL; v16[0] > i; ++i )
{
v12 = v2[i];
v13 = *(_QWORD *)(v12 + 24);
v14 = *(_QWORD *)(a1 + 40);
if ( v13 == v14 )
{
if ( *(_DWORD *)(v12 + 4) == 8
|| !*(_QWORD *)(v12 + 32)
|| !(*(unsigned __int16 *)(a1 + 64) * *(unsigned __int16 *)(a1 + 62)) )
{
continue;
}
}
else
{
if ( v13 > v14 )
goto LABEL_27;
if ( *(_DWORD *)(v12 + 4) == 8 )
goto LABEL_29;
}
if ( v14 >= *(_QWORD *)(v12 + 32) + v13 )
{
LABEL_29:
if ( v13 < v14 )
continue;
LABEL_27:
if ( v13 >= *(unsigned __int16 *)(a1 + 64) * *(unsigned __int16 *)(a1 + 62) + v14 )
continue;
}
v11 = (const char *)elfu_mScnName(a1);
fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n", v11);
}
}
}
free(v2);
return 0LL;
}

original_code:
int elfu_mCheck(ElfuElf *me)
{
  size_t numSecs;
  ElfuScn **sortedSecs;
  size_t i;
  sortedSecs = elfu_mScnSortedByOffset(me, &numSecs);
  if (!sortedSecs) {
    return -1;
  }
  for (i = 0; i < numSecs - 1; i++) {
    if (sortedSecs[i]->shdr.sh_offset + ((&sortedSecs[i]->shdr)->sh_type == 8 ? 0 : (&sortedSecs[i]->shdr)->sh_size)
        > sortedSecs[i+1]->shdr.sh_offset) {
      do { fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n", elfu_mScnName(me, sortedSecs[i]), elfu_mScnName(me, sortedSecs[i+1])); } while(0);
    }
  }
  for (i = 0; i < numSecs; i++) {
    if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
      do { fprintf(stderr, "elfu_check: Found section overlapping with EHDR: %s.\n", elfu_mScnName(me, sortedSecs[i])); } while(0);
    }
  }
  for (i = 0; i < numSecs; i++) {
    if ((!((sortedSecs[i]->shdr.sh_offset) == (me->ehdr.e_phoff) && ((((&sortedSecs[i]->shdr)->sh_type == 8 ? 0 : (&sortedSecs[i]->shdr)->sh_size) == 0) || (me->ehdr.e_phentsize * me->ehdr.e_phnum == 0))) && (((sortedSecs[i]->shdr.sh_offset) <= (me->ehdr.e_phoff) && (me->ehdr.e_phoff) < (((sortedSecs[i]->shdr.sh_offset)) + ((((&sortedSecs[i]->shdr)->sh_type == 8 ? 0 : (&sortedSecs[i]->shdr)->sh_size))))) || ((me->ehdr.e_phoff) <= (sortedSecs[i]->shdr.sh_offset) && (sortedSecs[i]->shdr.sh_offset) < (((me->ehdr.e_phoff)) + ((me->ehdr.e_phentsize * me->ehdr.e_phnum))))) )) {
      do { fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n", elfu_mScnName(me, sortedSecs[i])); } while(0);
    }
  }
  free(sortedSecs);
  return 0;
}

canonical_original_code:
int elfu_mCheck(struct ElfuElf *me)
{
  unsigned long numSecs;
  struct ElfuScn **sortedSecs;
  unsigned long i;
  sortedSecs = elfu_mScnSortedByOffset(me, &numSecs);
  if (!sortedSecs) {
    return -1;
  }
  for (i = 0; i < numSecs - 1; i++) {
    if (sortedSecs[i]->shdr.sh_offset + ((&sortedSecs[i]->shdr)->sh_type == 8 ? 0 : (&sortedSecs[i]->shdr)->sh_size)
        > sortedSecs[i+1]->shdr.sh_offset) {
      do { fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n", elfu_mScnName(me, sortedSecs[i]), elfu_mScnName(me, sortedSecs[i+1])); } while(0);
    }
  }
  for (i = 0; i < numSecs; i++) {
    if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
      do { fprintf(stderr, "elfu_check: Found section overlapping with EHDR: %s.\n", elfu_mScnName(me, sortedSecs[i])); } while(0);
    }
  }
  for (i = 0; i < numSecs; i++) {
    if ((!((sortedSecs[i]->shdr.sh_offset) == (me->ehdr.e_phoff) && ((((&sortedSecs[i]->shdr)->sh_type == 8 ? 0 : (&sortedSecs[i]->shdr)->sh_size) == 0) || (me->ehdr.e_phentsize * me->ehdr.e_phnum == 0))) && (((sortedSecs[i]->shdr.sh_offset) <= (me->ehdr.e_phoff) && (me->ehdr.e_phoff) < (((sortedSecs[i]->shdr.sh_offset)) + ((((&sortedSecs[i]->shdr)->sh_type == 8 ? 0 : (&sortedSecs[i]->shdr)->sh_size))))) || ((me->ehdr.e_phoff) <= (sortedSecs[i]->shdr.sh_offset) && (sortedSecs[i]->shdr.sh_offset) < (((me->ehdr.e_phoff)) + ((me->ehdr.e_phentsize * me->ehdr.e_phnum))))) )) {
      do { fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n", elfu_mScnName(me, sortedSecs[i])); } while(0);
    }
  }
  free(sortedSecs);
  return 0;
}

variable_types:
numSecs
unsigned long

variable_types: struct ElfuScn { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab { struct Syms { struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; } *cqh_first; struct ElfuSym *cqh_last; } syms; } symtab; struct ElfuReltab { struct Rels { struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *cqh_first; struct ElfuRel *cqh_last; } rels; } reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; } **

variable_types:
i
unsigned long


repo: norly/sysstatus

decompiled_code:
size_t __fastcall line_clear(__int64 a1)
{
if ( !*(_QWORD *)(a1 + 16) )
_assert_fail("g->linemax > 0", "src/common/common.c", 0x11u, "line_clear");
**(_BYTE **)a1 = 0;
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 24) = 0;
return line_append_str((size_t *)a1, "[");
}

canonical_decompiled_code:
size_t __fastcall func3(__int64 a1)
{
if ( !*(_QWORD *)(a1 + 16) )
_assert_fail("g->linemax > 0", "src/common/common.c", 0x11u, "line_clear");
**(_BYTE **)a1 = 0;
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 24) = 0;
return func2((size_t *)a1, "[");
}

original_code:
void line_clear(GlobalData *g)
{
  ((void) sizeof ((g->linemax > 0) ? 1 : 0), __extension__ ({ if (g->linemax > 0) ; else __assert_fail ("g->linemax > 0", "src/common/common.c", 17, __extension__ __PRETTY_FUNCTION__); }));
  g->line[0] = '\0';
  g->linelen = 0;
  g->firstItemDone = 0;
  line_append_str(g, "[");
}

canonical_original_code:
void line_clear(struct GlobalData *g)
{
  ((void) sizeof ((g->linemax > 0) ? 1 : 0), __extension__ ({ if (g->linemax > 0) ; else __assert_fail ("g->linemax > 0", "src/common/common.c", 17, __extension__ __PRETTY_FUNCTION__); }));
  g->line[0] = '\0';
  g->linelen = 0;
  g->firstItemDone = 0;
  line_append_str(g, "[");
}

variable_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; } *


decompiled_code:
int __fastcall line_print(size_t *a1)
{
line_append_str(a1, "],");
return puts((const char *)*a1);
}

canonical_decompiled_code:
int __fastcall func5(size_t *a1)
{
func2(a1, "],");
return puts((const char *)*a1);
}

original_code:
void line_print(GlobalData *g)
{
  line_append_str(g, "],");
  puts(g->line);
}

canonical_original_code:
void line_print(struct GlobalData *g)
{
  line_append_str(g, "],");
  puts(g->line);
}

user_defined_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; }


updatestatus

decompiled_code:
int updatestatus()
{
line_clear(&gd);
status_cpuusage(&gd);
status_battery(&gd, "BAT0");
status_battery(&gd, "BAT1");
status_memusage(&gd);
status_netif(&gd, "eno0");
status_netif(&gd, "eth0");
status_netif(&gd, "wlp1s0");
status_netif(&gd, "wlan0");
status_netif(&gd, "usb0");
status_netif(&gd, "usb1");
status_temp(&gd, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon3/temp1_input");
status_temp(&gd, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon5/temp1_input");
status_fan(&gd, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon2/fan1_input");
status_fan(&gd, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon4/fan1_input");
if ( (unsigned int)status_volume_alsa(&gd, "default", "Master", 0LL)
&& (unsigned int)status_volume_alsa(&gd, "default", "DX3 Pro ", 0LL)
&& (unsigned int)status_volume_alsa(&gd, "default", "D10 ", 0LL)
&& (unsigned int)status_volume_alsa(&gd, "default", "PCM", 0LL) )
{
status_volume_alsa(&gd, "default", "Headset", 0LL);
}
status_datetime(&gd);
line_print(&gd);
return fflush(stdout);
}

canonical_decompiled_code:
int func0()
{
line_clear(&gd);
status_cpuusage(&gd);
status_battery(&gd, "BAT0");
status_battery(&gd, "BAT1");
status_memusage(&gd);
status_netif(&gd, "eno0");
status_netif(&gd, "eth0");
status_netif(&gd, "wlp1s0");
status_netif(&gd, "wlan0");
status_netif(&gd, "usb0");
status_netif(&gd, "usb1");
status_temp(&gd, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon3/temp1_input");
status_temp(&gd, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon5/temp1_input");
status_fan(&gd, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon2/fan1_input");
status_fan(&gd, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon4/fan1_input");
if ( (unsigned int)status_volume_alsa(&gd, "default", "Master", 0LL)
&& (unsigned int)status_volume_alsa(&gd, "default", "DX3 Pro ", 0LL)
&& (unsigned int)status_volume_alsa(&gd, "default", "D10 ", 0LL)
&& (unsigned int)status_volume_alsa(&gd, "default", "PCM", 0LL) )
{
status_volume_alsa(&gd, "default", "Headset", 0LL);
}
status_datetime(&gd);
line_print(&gd);
return fflush(stdout);
}

original_code:
void updatestatus()
{
  GlobalData *g = &gd;
  line_clear(g);
  status_cpuusage(g);
  status_battery(g, "BAT0");
  status_battery(g, "BAT1");
  status_memusage(g);
  status_netif(g, "eno0");
  status_netif(g, "eth0");
  status_netif(g, "wlp1s0");
  status_netif(g, "wlan0");
  status_netif(g, "usb0");
  status_netif(g, "usb1");
  status_temp(g, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon3/temp1_input");
  status_temp(g, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon5/temp1_input");
  status_fan(g, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon2/fan1_input");
  status_fan(g, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon4/fan1_input");
  if (status_volume_alsa(g, "default", "Master", 0))
  if (status_volume_alsa(g, "default", "DX3 Pro ", 0))
  if (status_volume_alsa(g, "default", "D10 ", 0))
  if (status_volume_alsa(g, "default", "PCM", 0))
  if (status_volume_alsa(g, "default", "Headset", 0)) {
    ;
  }
  status_datetime(g);
  line_print(g);
  fflush(stdout);
}

canonical_original_code:
void updatestatus()
{
  struct GlobalData *g = &gd;
  line_clear(g);
  status_cpuusage(g);
  status_battery(g, "BAT0");
  status_battery(g, "BAT1");
  status_memusage(g);
  status_netif(g, "eno0");
  status_netif(g, "eth0");
  status_netif(g, "wlp1s0");
  status_netif(g, "wlan0");
  status_netif(g, "usb0");
  status_netif(g, "usb1");
  status_temp(g, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon3/temp1_input");
  status_temp(g, "CPU: ", "/sys/devices/platform/coretemp.0/hwmon/hwmon5/temp1_input");
  status_fan(g, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon2/fan1_input");
  status_fan(g, "Fan: ", "/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon4/fan1_input");
  if (status_volume_alsa(g, "default", "Master", 0))
  if (status_volume_alsa(g, "default", "DX3 Pro ", 0))
  if (status_volume_alsa(g, "default", "D10 ", 0))
  if (status_volume_alsa(g, "default", "PCM", 0))
  if (status_volume_alsa(g, "default", "Headset", 0)) {
    ;
  }
  status_datetime(g);
  line_print(g);
  fflush(stdout);
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

puts("{\"version\":1}\n[");
while ( 1 )
{
updatestatus();
v3.tv_sec = 1LL;
v3.tv_usec = 0LL;
select(0, 0LL, 0LL, 0LL, &v3);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

puts("{\"version\":1}\n[");
while ( 1 )
{
func0();
v3.tv_sec = 1LL;
v3.tv_usec = 0LL;
select(0, 0LL, 0LL, 0LL, &v3);
}
}

original_code:
int main()
{
  struct timeval tv;
  printf("{\"version\":1}\n[\n");
  for(;;)
  {
    updatestatus();
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    select(0, ((void *)0), ((void *)0), ((void *)0), &tv);
  }
  return 0;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


powerToChar

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

switch ( a1 )
{
case 0:
result = 98LL;
break;
case 1:
result = 107LL;
break;
case 2:
result = 77LL;
break;
case 3:
result = 71LL;
break;
case 4:
result = 84LL;
break;
case 5:
result = 80LL;
break;
case 6:
result = 69LL;
break;
default:
result = 63LL;
break;
}
return result;
}

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

switch ( a1 )
{
case 0:
result = 98LL;
break;
case 1:
result = 107LL;
break;
case 2:
result = 77LL;
break;
case 3:
result = 71LL;
break;
case 4:
result = 84LL;
break;
case 5:
result = 80LL;
break;
case 6:
result = 69LL;
break;
default:
result = 63LL;
break;
}
return result;
}

original_code:
char powerToChar(int power)
{
  switch(power)
  {
    case 0:
      return 'b';
    case 1:
      return 'k';
    case 2:
      return 'M';
    case 3:
      return 'G';
    case 4:
      return 'T';
    case 5:
      return 'P';
    case 6:
      return 'E';
  }
  return '?';
}

variable_types: power int

return_type: char


statusError

decompiled_code:
int __fastcall statusError(const char *a1, const char *a2, const char *a3)
{
fprintf(stderr, "%s: %s", a1, a2);
if ( a3 )
fprintf(stderr, " -- %s", a3);
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, const char *a2, const char *a3)
{
fprintf(stderr, "%s: %s", a1, a2);
if ( a3 )
fprintf(stderr, " -- %s", a3);
return fputc(10, stderr);
}

original_code:
void statusError(char *where, char *what, char *extra)
{
  fprintf(stderr, "%s: %s", where, what);
  if (extra) {
    fprintf(stderr, " -- %s", extra);
  }
  fputs("\n", stderr);
}

variable_types:
where
char *

variable_types:
what
char *

variable_types:
extra
char *


fileRead

decompiled_code:
__int64 __fastcall fileRead(void *buf, __int64 a2, const char *a3)
{
int v3; // eax
int v4; // ebp
int v5; // ebx

v3 = open(a3, 0);
if ( v3 < 0 )
return -1LL;
v4 = v3;
v5 = read(v3, buf, a2 - 1);
close(v4);
if ( v5 <= 0 )
*(_BYTE *)buf = 0;
else
*((_BYTE *)buf + v5) = 0;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *buf, __int64 a2, const char *a3)
{
int v3; // eax
int v4; // ebp
int v5; // ebx

v3 = open(a3, 0);
if ( v3 < 0 )
return -1LL;
v4 = v3;
v5 = read(v3, buf, a2 - 1);
close(v4);
if ( v5 <= 0 )
*(_BYTE *)buf = 0;
else
*((_BYTE *)buf + v5) = 0;
return v5;
}

original_code:
ssize_t fileRead(char *buf, size_t bufsize, char *file)
{
  int fd;
  int readbytes;
  fd = open(file, 0);
  if (fd < 0) {
    return -1;
  }
  readbytes = read(fd, buf, bufsize - 1);
  close(fd);
  if (readbytes > 0) {
    buf[readbytes] = '\0';
  } else {
    buf[0] = '\0';
  }
  return readbytes;
}

canonical_original_code:
long fileRead(char *buf, unsigned long bufsize, char *file)
{
  int fd;
  int readbytes;
  fd = open(file, 0);
  if (fd < 0) {
    return -1;
  }
  readbytes = read(fd, buf, bufsize - 1);
  close(fd);
  if (readbytes > 0) {
    buf[readbytes] = '\0';
  } else {
    buf[0] = '\0';
  }
  return readbytes;
}

variable_types:
buf
char *

variable_types:
bufsize
unsigned long

variable_types:
file
char *

variable_types: fd int

variable_types: readbytes int

return_type: long


decompiled_code:
int __fastcall status_netif(__int64 a1, const char *a2)
{
int v2; // ebx
__int64 v3; // rax
unsigned int v4; // er13
char *v5; // rbx
double v6; // r14
__int64 v7; // r9
int v8; // ebx
char v10[16]; // [rsp+0h] [rbp-168h] BYREF
char dest[256]; // [rsp+10h] [rbp-158h] BYREF
char s[8]; // [rsp+110h] [rbp-58h] BYREF
__int64 v13; // [rsp+118h] [rbp-50h]
int v14; // [rsp+120h] [rbp-48h]
__int64 v15[7]; // [rsp+130h] [rbp-38h] BYREF

*(_QWORD *)s = 0LL;
v13 = 0LL;
v14 = 0;
statusitem_init(v15);
v15[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)(v2 + 16 + 21LL) > 0xFF )
{
LODWORD(v3) = statusError("status_netif", "ifpath buffer too small", a2);
return v3;
}
strcpy(dest, "/sys/class/net/");
strcat(dest, a2);
LODWORD(v3) = access(dest, 0);
v4 = v3;
if ( !(_DWORD)v3 )
{
v5 = &dest[v2 + 15];
strcpy(v5, "/carrier");
v3 = fileRead(v10, 16LL, dest);
if ( v3 > 0 && v10[0] == 49 )
{
v15[0] = (__int64)"#FFFF00";
strcpy(v5, "/statistics/rx_bytes");
if ( fileRead(v10, 16LL, dest) <= 0 )
{
strcpy(v5, "/statistics/tx_bytes");
v6 = *(double *)&LC0;
if ( fileRead(v10, 16LL, dest) <= 0 )
goto LABEL_10;
}
else
{
v6 = atof(v10);
strcpy(v5, "/statistics/tx_bytes");
if ( fileRead(v10, 16LL, dest) <= 0 )
goto LABEL_13;
}
v6 = v6 + atof(v10);
LABEL_13:
if ( v6 >= 1024.0 )
{
do
{
v6 = v6 * 0.0009765625;
v8 = v4++;
}
while ( v6 >= 1024.0 );
v7 = (unsigned int)(char)powerToChar(v4);
if ( v4 )
v4 = v8;
goto LABEL_17;
}
LABEL_10:
v7 = (unsigned int)(char)powerToChar(0LL);
LABEL_17:
snprintf(s, 0x14uLL, "%s: %.*f %c", a2, v4, v6, v7);
LODWORD(v3) = line_append_item(a1, v15);
return v3;
}
}
return v3;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, const char *a2)
{
int v2; // ebx
__int64 v3; // rax
unsigned int v4; // er13
char *v5; // rbx
double v6; // r14
__int64 v7; // r9
int v8; // ebx
char v10[16]; // [rsp+0h] [rbp-168h] BYREF
char dest[256]; // [rsp+10h] [rbp-158h] BYREF
char s[8]; // [rsp+110h] [rbp-58h] BYREF
__int64 v13; // [rsp+118h] [rbp-50h]
int v14; // [rsp+120h] [rbp-48h]
__int64 v15[7]; // [rsp+130h] [rbp-38h] BYREF

*(_QWORD *)s = 0LL;
v13 = 0LL;
v14 = 0;
statusitem_init(v15);
v15[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)(v2 + 16 + 21LL) > 0xFF )
{
LODWORD(v3) = statusError("status_netif", "ifpath buffer too small", a2);
return v3;
}
strcpy(dest, "/sys/class/net/");
strcat(dest, a2);
LODWORD(v3) = access(dest, 0);
v4 = v3;
if ( !(_DWORD)v3 )
{
v5 = &dest[v2 + 15];
strcpy(v5, "/carrier");
v3 = fileRead(v10, 16LL, dest);
if ( v3 > 0 && v10[0] == 49 )
{
v15[0] = (__int64)"#FFFF00";
strcpy(v5, "/statistics/rx_bytes");
if ( fileRead(v10, 16LL, dest) <= 0 )
{
strcpy(v5, "/statistics/tx_bytes");
v6 = *(double *)&LC0;
if ( fileRead(v10, 16LL, dest) <= 0 )
goto LABEL_10;
}
else
{
v6 = atof(v10);
strcpy(v5, "/statistics/tx_bytes");
if ( fileRead(v10, 16LL, dest) <= 0 )
goto LABEL_13;
}
v6 = v6 + atof(v10);
LABEL_13:
if ( v6 >= 1024.0 )
{
do
{
v6 = v6 * 0.0009765625;
v8 = v4++;
}
while ( v6 >= 1024.0 );
v7 = (unsigned int)(char)powerToChar(v4);
if ( v4 )
v4 = v8;
goto LABEL_17;
}
LABEL_10:
v7 = (unsigned int)(char)powerToChar(0LL);
LABEL_17:
snprintf(s, 0x14uLL, "%s: %.*f %c", a2, v4, v6, v7);
LODWORD(v3) = line_append_item(a1, v15);
return v3;
}
}
return v3;
}

original_code:
void status_netif(GlobalData *g, char *ifname)
{
  StatusItem s;
  char text[20] = { 0 };
  char ifpath[256];
  int ifpathlen;
  char stline[16];
  ssize_t stlen;
  double ifsum = 0.0;
  int ifsumpower;
  statusitem_init(&s);
  s.text = text;
  ifpathlen = sizeof("/sys/class/net/") - 1 + strlen(ifname);
  if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
    statusError("status_netif",
                "ifpath buffer too small",
                ifname);
    return;
  }
  strcpy(ifpath, "/sys/class/net/");
  strcat(ifpath, ifname);
  if (access(ifpath, 0)) {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/carrier");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    if (stline[0] == '1') {
      s.color = "#FFFF00";
    } else {
      return;
    }
  } else {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum = atof(stline);
  }
  strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum += atof(stline);
  }
  for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
    ifsum = ifsum / 1024;
  }
  snprintf(text, sizeof(text), "%s: %.*f %c",
            ifname,
            ifsumpower ? ifsumpower - 1 : ifsumpower,
            ifsum,
            powerToChar(ifsumpower));
  line_append_item(g, &s);
}

canonical_original_code:
void status_netif(struct GlobalData *g, char *ifname)
{
  struct StatusItem s;
  char text[20] = { 0 };
  char ifpath[256];
  int ifpathlen;
  char stline[16];
  long stlen;
  double ifsum = 0.0;
  int ifsumpower;
  statusitem_init(&s);
  s.text = text;
  ifpathlen = sizeof("/sys/class/net/") - 1 + strlen(ifname);
  if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
    statusError("status_netif",
                "ifpath buffer too small",
                ifname);
    return;
  }
  strcpy(ifpath, "/sys/class/net/");
  strcat(ifpath, ifname);
  if (access(ifpath, 0)) {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/carrier");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    if (stline[0] == '1') {
      s.color = "#FFFF00";
    } else {
      return;
    }
  } else {
    return;
  }
  strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum = atof(stline);
  }
  strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum += atof(stline);
  }
  for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
    ifsum = ifsum / 1024;
  }
  snprintf(text, sizeof(text), "%s: %.*f %c",
            ifname,
            ifsumpower ? ifsumpower - 1 : ifsumpower,
            ifsum,
            powerToChar(ifsumpower));
  line_append_item(g, &s);
}

variable_types:
ifname
char *

variable_types: struct StatusItem { char *color; char *text; }

variable_types:
text
char [20]

variable_types:
ifpath
char [256]

variable_types: ifpathlen int

variable_types:
stline
char [16]

variable_types: stlen long

variable_types: ifsum double

variable_types: ifsumpower int


decompiled_code:
__int64 __fastcall status_temp(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
char v5[4]; // [rsp+0h] [rbp-4Ch]
char v6[16]; // [rsp+4h] [rbp-48h] BYREF
char s[8]; // [rsp+14h] [rbp-38h] BYREF
__int64 v8; // [rsp+1Ch] [rbp-30h]
__int64 v9[5]; // [rsp+24h] [rbp-28h] BYREF

*(_QWORD *)s = 0LL;
v8 = 0LL;
statusitem_init(v9);
v9[1] = (__int64)s;
result = fileRead(v6, 16LL, a3);
if ( result > 0 )
{
if ( (unsigned __int64)(result - 6) <= 1 )
{
v5[result] = 0;
v9[0] = (__int64)"#FF33FF";
snprintf(s, 0x10uLL, "%s%s C", a2, v6);
}
else
{
v9[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
result = line_append_item(a1, v9);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
char v5[4]; // [rsp+0h] [rbp-4Ch]
char v6[16]; // [rsp+4h] [rbp-48h] BYREF
char s[8]; // [rsp+14h] [rbp-38h] BYREF
__int64 v8; // [rsp+1Ch] [rbp-30h]
__int64 v9[5]; // [rsp+24h] [rbp-28h] BYREF

*(_QWORD *)s = 0LL;
v8 = 0LL;
statusitem_init(v9);
v9[1] = (__int64)s;
result = fileRead(v6, 16LL, a3);
if ( result > 0 )
{
if ( (unsigned __int64)(result - 6) <= 1 )
{
v5[result] = 0;
v9[0] = (__int64)"#FF33FF";
snprintf(s, 0x10uLL, "%s%s C", a2, v6);
}
else
{
v9[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
result = line_append_item(a1, v9);
}
return result;
}

original_code:
void status_temp(GlobalData *g, char *title, char *sysfile)
{
  StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  ssize_t stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen < 6 || stlen > 7) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 4] = '\0';
    s.color = "#FF33FF";
    snprintf(text, sizeof(text), "%s%s C", title, stline);
  }
  line_append_item(g, &s);
}

canonical_original_code:
void status_temp(struct GlobalData *g, char *title, char *sysfile)
{
  struct StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  long stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen < 6 || stlen > 7) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 4] = '\0';
    s.color = "#FF33FF";
    snprintf(text, sizeof(text), "%s%s C", title, stline);
  }
  line_append_item(g, &s);
}

variable_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; } *

variable_types:
title
char *

variable_types:
sysfile
char *

variable_types:
text
char [16]

user_defined_types: struct StatusItem { char *color; char *text; }


decompiled_code:
__int64 __fastcall status_uptime(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx
__int64 v3; // rax
int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // er12
unsigned int v7; // edx
char v8[16]; // [rsp+0h] [rbp-48h] BYREF
char s[8]; // [rsp+10h] [rbp-38h] BYREF
__int64 v10; // [rsp+18h] [rbp-30h]
__int64 v11[5]; // [rsp+20h] [rbp-28h] BYREF

*(_QWORD *)s = 0LL;
v10 = 0LL;
statusitem_init(v11);
v11[1] = (__int64)s;
result = fileRead(v8, 16LL, "/proc/uptime");
if ( result >= 0 )
{
v2 = result;
if ( result > 0 )
{
v3 = 0LL;
while ( v8[v3] != 32 )
{
if ( ++v3 == v2 )
goto LABEL_8;
}
v8[(int)v3] = 0;
}
LABEL_8:
v4 = atoi(v8);
v5 = v4 % 86400 / 3600;
v6 = v4 % 86400 % 3600 / 60;
v11[0] = (__int64)"#AAAAAA";
strcpy(s, "up: ");
v7 = 4;
if ( v4 > 86399 )
v7 = snprintf(&s[4], 0xCuLL, "%dd ", (unsigned int)(v4 / 86400)) + 4;
snprintf(&s[v7], 16LL - v7, "%d:%.2d", v5, v6);
result = line_append_item(a1, v11);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx
__int64 v3; // rax
int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // er12
unsigned int v7; // edx
char v8[16]; // [rsp+0h] [rbp-48h] BYREF
char s[8]; // [rsp+10h] [rbp-38h] BYREF
__int64 v10; // [rsp+18h] [rbp-30h]
__int64 v11[5]; // [rsp+20h] [rbp-28h] BYREF

*(_QWORD *)s = 0LL;
v10 = 0LL;
statusitem_init(v11);
v11[1] = (__int64)s;
result = fileRead(v8, 16LL, "/proc/uptime");
if ( result >= 0 )
{
v2 = result;
if ( result > 0 )
{
v3 = 0LL;
while ( v8[v3] != 32 )
{
if ( ++v3 == v2 )
goto LABEL_8;
}
v8[(int)v3] = 0;
}
LABEL_8:
v4 = atoi(v8);
v5 = v4 % 86400 / 3600;
v6 = v4 % 86400 % 3600 / 60;
v11[0] = (__int64)"#AAAAAA";
strcpy(s, "up: ");
v7 = 4;
if ( v4 > 86399 )
v7 = snprintf(&s[4], 0xCuLL, "%dd ", (unsigned int)(v4 / 86400)) + 4;
snprintf(&s[v7], 16LL - v7, "%d:%.2d", v5, v6);
result = line_append_item(a1, v11);
}
return result;
}

original_code:
void status_uptime(GlobalData *g)
{
  StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  ssize_t stlen;
  int i;
  int upts, uptm, upth, uptd;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
  if (stlen < 0) {
    s.color = "#FF0000";
    s.text = "up: ERROR";
  } else {
    unsigned textlen = 0;
    for(i = 0; i < stlen; i++) {
      if (stline[i] == ' ') {
        stline[i] = '\0';
        break;
      }
    }
    upts = atoi(stline);
    uptd = upts / (24 * 60 * 60);
    upts -= uptd * (24 * 60 * 60);
    upth = upts / (60 * 60);
    upts -= upth * (60 * 60);
    uptm = upts / (60);
    upts -= uptm * (60);
    s.color = "#AAAAAA";
    textlen = snprintf(text, sizeof(text), "up: ");
    if (uptd > 0) {
      textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
    }
    snprintf(&text[textlen], sizeof(text) - textlen,
              "%d:%.2d"
              ""
              ,upth
              ,uptm
            );
    line_append_item(g, &s);
  }
}

canonical_original_code:
void status_uptime(struct GlobalData *g)
{
  struct StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  long stlen;
  int i;
  int upts; int uptm; int upth; int uptd;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
  if (stlen < 0) {
    s.color = "#FF0000";
    s.text = "up: ERROR";
  } else {
    unsigned int textlen = 0;
    for(i = 0; i < stlen; i++) {
      if (stline[i] == ' ') {
        stline[i] = '\0';
        break;
      }
    }
    upts = atoi(stline);
    uptd = upts / (24 * 60 * 60);
    upts -= uptd * (24 * 60 * 60);
    upth = upts / (60 * 60);
    upts -= upth * (60 * 60);
    uptm = upts / (60);
    upts -= uptm * (60);
    s.color = "#AAAAAA";
    textlen = snprintf(text, sizeof(text), "up: ");
    if (uptd > 0) {
      textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
    }
    snprintf(&text[textlen], sizeof(text) - textlen,
              "%d:%.2d"
              ""
              ,upth
              ,uptm
            );
    line_append_item(g, &s);
  }
}

variable_types: i int

variable_types: upts int

variable_types: uptm int

variable_types: upth int

variable_types: uptd int

variable_types:
textlen
unsigned int


decompiled_code:
int __fastcall status_battery(__int64 a1, const char *a2)
{
int v2; // ebx
int result; // eax
int v4; // er12
char *v5; // rbx
int v6; // er14
int v7; // er15
int v8; // eax
__int64 v9; // r8
bool v10; // cc
const char *v11; // rax
int v12; // [rsp+Ch] [rbp-17Ch]
char nptr[16]; // [rsp+10h] [rbp-178h] BYREF
char dest[256]; // [rsp+20h] [rbp-168h] BYREF
char s[8]; // [rsp+120h] [rbp-68h] BYREF
__int64 v16; // [rsp+128h] [rbp-60h]
__int64 v17; // [rsp+130h] [rbp-58h]
__int64 v18; // [rsp+138h] [rbp-50h]
__int64 v19[9]; // [rsp+140h] [rbp-48h] BYREF

*(_QWORD *)s = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
statusitem_init(v19);
v19[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)(v2 + 25 + 13LL) > 0xFF )
return statusError("status_battery", "batpath buffer too small", a2);
strcpy(dest, "/sys/class/power_supply/");
strcat(dest, a2);
result = access(dest, 0);
v4 = result;
if ( result )
return result;
v5 = &dest[v2 + 24];
strcpy(v5, "/charge_now");
v12 = 0;
if ( fileRead(nptr, 16LL, dest) > 0 )
v12 = atoi(nptr);
strcpy(v5, "/charge_full");
v6 = -1;
if ( fileRead(nptr, 16LL, dest) > 0 )
v6 = atoi(nptr);
strcpy(v5, "/current_now");
v7 = 1;
if ( fileRead(nptr, 16LL, dest) > 0 )
v7 = atoi(nptr);
strcpy(v5, "/voltage_now");
if ( fileRead(nptr, 16LL, dest) > 0 )
v4 = atoi(nptr);
if ( v6 <= 0 )
{
v9 = 0xFFFFFFFFLL;
goto LABEL_23;
}
v8 = v12 / (v6 / 100);
v9 = (unsigned int)v8;
if ( v8 > 40 )
{
v10 = v8 < 71;
v11 = "#22FF22";
if ( !v10 )
v11 = "#FFFFFF";
v19[0] = (__int64)v11;
goto LABEL_15;
}
if ( v8 <= 25 )
{
if ( v8 > 10 )
{
v19[0] = (__int64)"#FFA500";
goto LABEL_15;
}
LABEL_23:
v19[0] = (__int64)"#FF0000";
goto LABEL_15;
}
v19[0] = (__int64)"#FFFF00";
LABEL_15:
if ( v7 )
snprintf(
s,
0x20uLL,
"%s: %d%% %.1fh %.1fW",
a2,
v9,
(float)((float)v12 / (float)v7),
(float)v4 / 1000000.0 * (float)v7 / 1000000.0);
else
snprintf(s, 0x20uLL, "%s: %d%% _ _", a2, v9);
return line_append_item(a1, v19);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, const char *a2)
{
int v2; // ebx
int result; // eax
int v4; // er12
char *v5; // rbx
int v6; // er14
int v7; // er15
int v8; // eax
__int64 v9; // r8
bool v10; // cc
const char *v11; // rax
int v12; // [rsp+Ch] [rbp-17Ch]
char nptr[16]; // [rsp+10h] [rbp-178h] BYREF
char dest[256]; // [rsp+20h] [rbp-168h] BYREF
char s[8]; // [rsp+120h] [rbp-68h] BYREF
__int64 v16; // [rsp+128h] [rbp-60h]
__int64 v17; // [rsp+130h] [rbp-58h]
__int64 v18; // [rsp+138h] [rbp-50h]
__int64 v19[9]; // [rsp+140h] [rbp-48h] BYREF

*(_QWORD *)s = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
statusitem_init(v19);
v19[1] = (__int64)s;
v2 = strlen(a2);
if ( (unsigned __int64)(v2 + 25 + 13LL) > 0xFF )
return statusError("status_battery", "batpath buffer too small", a2);
strcpy(dest, "/sys/class/power_supply/");
strcat(dest, a2);
result = access(dest, 0);
v4 = result;
if ( result )
return result;
v5 = &dest[v2 + 24];
strcpy(v5, "/charge_now");
v12 = 0;
if ( fileRead(nptr, 16LL, dest) > 0 )
v12 = atoi(nptr);
strcpy(v5, "/charge_full");
v6 = -1;
if ( fileRead(nptr, 16LL, dest) > 0 )
v6 = atoi(nptr);
strcpy(v5, "/current_now");
v7 = 1;
if ( fileRead(nptr, 16LL, dest) > 0 )
v7 = atoi(nptr);
strcpy(v5, "/voltage_now");
if ( fileRead(nptr, 16LL, dest) > 0 )
v4 = atoi(nptr);
if ( v6 <= 0 )
{
v9 = 0xFFFFFFFFLL;
goto LABEL_23;
}
v8 = v12 / (v6 / 100);
v9 = (unsigned int)v8;
if ( v8 > 40 )
{
v10 = v8 < 71;
v11 = "#22FF22";
if ( !v10 )
v11 = "#FFFFFF";
v19[0] = (__int64)v11;
goto LABEL_15;
}
if ( v8 <= 25 )
{
if ( v8 > 10 )
{
v19[0] = (__int64)"#FFA500";
goto LABEL_15;
}
LABEL_23:
v19[0] = (__int64)"#FF0000";
goto LABEL_15;
}
v19[0] = (__int64)"#FFFF00";
LABEL_15:
if ( v7 )
snprintf(
s,
0x20uLL,
"%s: %d%% %.1fh %.1fW",
a2,
v9,
(float)((float)v12 / (float)v7),
(float)v4 / 1000000.0 * (float)v7 / 1000000.0);
else
snprintf(s, 0x20uLL, "%s: %d%% _ _", a2, v9);
return line_append_item(a1, v19);
}

original_code:
void status_battery(GlobalData *g, char *batname)
{
  StatusItem s;
  char text[32] = { 0 };
  char batpath[256];
  int batpathlen;
  char stline[16];
  ssize_t stlen;
  int chargeNow = 0;
  int chargeFull = -1;
  int chargePercent = -1;
  int currentNow = 1;
  int voltageNow = 0;
  float battTime = -1;
  statusitem_init(&s);
  s.text = text;
  batpathlen = sizeof("/sys/class/power_supply/") - 1 + strlen(batname);
  if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
    statusError("status_battery",
                "batpath buffer too small",
                batname);
    return;
  }
  strcpy(batpath, "/sys/class/power_supply/");
  strcat(batpath, batname);
  if (access(batpath, 0)) {
    return;
  }
  strcpy(&batpath[batpathlen], "/charge_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/charge_full");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeFull = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/current_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    currentNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/voltage_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    voltageNow = atoi(stline);
  }
  if (chargeFull > 0) {
    chargePercent = chargeNow / (chargeFull / 100);
  }
  if (chargePercent <= 40) {
    if (chargePercent <= 25) {
      if (chargePercent <= 10) {
        s.color = "#FF0000";
      } else {
        s.color = "#FFA500";
      }
    } else {
      s.color = "#FFFF00";
    }
  } else {
    if (chargePercent > 70) {
      s.color = "#FFFFFF";
    } else {
      s.color = "#22FF22";
    }
  }
  battTime = (float)chargeNow / (float)currentNow;
  if (currentNow == 0) {
    snprintf(text, sizeof(text), "%s: %d%% _ _",
              batname, chargePercent);
  } else {
    snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
              batname, chargePercent, battTime, (float)voltageNow / 1000000.0 * (float)currentNow / 1000000.0);
  }
  line_append_item(g, &s);
}

canonical_original_code:
void status_battery(struct GlobalData *g, char *batname)
{
  struct StatusItem s;
  char text[32] = { 0 };
  char batpath[256];
  int batpathlen;
  char stline[16];
  long stlen;
  int chargeNow = 0;
  int chargeFull = -1;
  int chargePercent = -1;
  int currentNow = 1;
  int voltageNow = 0;
  float battTime = -1;
  statusitem_init(&s);
  s.text = text;
  batpathlen = sizeof("/sys/class/power_supply/") - 1 + strlen(batname);
  if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
    statusError("status_battery",
                "batpath buffer too small",
                batname);
    return;
  }
  strcpy(batpath, "/sys/class/power_supply/");
  strcat(batpath, batname);
  if (access(batpath, 0)) {
    return;
  }
  strcpy(&batpath[batpathlen], "/charge_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/charge_full");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    chargeFull = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/current_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    currentNow = atoi(stline);
  }
  strcpy(&batpath[batpathlen], "/voltage_now");
  stlen = fileRead(stline, sizeof(stline), batpath);
  if (stlen > 0) {
    voltageNow = atoi(stline);
  }
  if (chargeFull > 0) {
    chargePercent = chargeNow / (chargeFull / 100);
  }
  if (chargePercent <= 40) {
    if (chargePercent <= 25) {
      if (chargePercent <= 10) {
        s.color = "#FF0000";
      } else {
        s.color = "#FFA500";
      }
    } else {
      s.color = "#FFFF00";
    }
  } else {
    if (chargePercent > 70) {
      s.color = "#FFFFFF";
    } else {
      s.color = "#22FF22";
    }
  }
  battTime = (float)chargeNow / (float)currentNow;
  if (currentNow == 0) {
    snprintf(text, sizeof(text), "%s: %d%% _ _",
              batname, chargePercent);
  } else {
    snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
              batname, chargePercent, battTime, (float)voltageNow / 1000000.0 * (float)currentNow / 1000000.0);
  }
  line_append_item(g, &s);
}

variable_types:
batname
char *

variable_types: struct StatusItem { char *color; char *text; }

variable_types:
text
char [32]

variable_types:
batpath
char [256]

variable_types: batpathlen int

variable_types: chargeNow int

variable_types: chargeFull int

variable_types: chargePercent int

variable_types: currentNow int

variable_types: voltageNow int

variable_types: battTime float


decompiled_code:
__int64 __fastcall status_datetime(__int64 a1)
{
struct tm *v1; // rbx
__int64 result; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
char s[8]; // [rsp+10h] [rbp-48h] BYREF
__int64 v5; // [rsp+18h] [rbp-40h]
__int64 v6; // [rsp+20h] [rbp-38h]
__int64 v7; // [rsp+28h] [rbp-30h]
const char *v8; // [rsp+30h] [rbp-28h] BYREF
char *v9; // [rsp+38h] [rbp-20h]

*(_QWORD *)s = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
statusitem_init(&v8);
v9 = s;
timer = time(0LL);
if ( timer == -1 )
{
v8 = "#FF0000";
v9 = "ERROR: DATETIME";
result = line_append_item(a1, &v8);
}
else
{
v1 = localtime(&timer);
v8 = "#666666";
snprintf(
s,
0x20uLL,
"%d.%d.%d",
(unsigned int)v1->tm_mday,
(unsigned int)(v1->tm_mon + 1),
(unsigned int)(v1->tm_year + 1900));
line_append_item(a1, &v8);
v8 = "#BEBEBE";
snprintf(s, 0x20uLL, "%d:%.2d", (unsigned int)v1->tm_hour, (unsigned int)v1->tm_min);
result = line_append_item(a1, &v8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
struct tm *v1; // rbx
__int64 result; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
char s[8]; // [rsp+10h] [rbp-48h] BYREF
__int64 v5; // [rsp+18h] [rbp-40h]
__int64 v6; // [rsp+20h] [rbp-38h]
__int64 v7; // [rsp+28h] [rbp-30h]
const char *v8; // [rsp+30h] [rbp-28h] BYREF
char *v9; // [rsp+38h] [rbp-20h]

*(_QWORD *)s = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
statusitem_init(&v8);
v9 = s;
timer = time(0LL);
if ( timer == -1 )
{
v8 = "#FF0000";
v9 = "ERROR: DATETIME";
result = line_append_item(a1, &v8);
}
else
{
v1 = localtime(&timer);
v8 = "#666666";
snprintf(
s,
0x20uLL,
"%d.%d.%d",
(unsigned int)v1->tm_mday,
(unsigned int)(v1->tm_mon + 1),
(unsigned int)(v1->tm_year + 1900));
line_append_item(a1, &v8);
v8 = "#BEBEBE";
snprintf(s, 0x20uLL, "%d:%.2d", (unsigned int)v1->tm_hour, (unsigned int)v1->tm_min);
result = line_append_item(a1, &v8);
}
return result;
}

original_code:
void status_datetime(GlobalData *g)
{
  StatusItem s;
  char text[32] = { 0 };
  time_t nows = 0;
  struct tm *nowtm;
  statusitem_init(&s);
  s.text = text;
  nows = time(((void *)0));
  if (nows == ((time_t) -1)) {
    s.color = "#FF0000";
    s.text = "ERROR: DATETIME";
    line_append_item(g, &s);
  } else {
    nowtm = localtime(&nows);
    s.color = "#666666";
    snprintf(text, sizeof(text),
              "%d.%d.%d",
              nowtm->tm_mday,
              (nowtm->tm_mon) + 1,
              (nowtm->tm_year) + 1900
            );
    line_append_item(g, &s);
    s.color = "#BEBEBE";
    snprintf(text, sizeof(text),
              "%d:%.2d"
              ,nowtm->tm_hour,
              nowtm->tm_min
            );
    line_append_item(g, &s);
  }
}

canonical_original_code:
void status_datetime(struct GlobalData *g)
{
  struct StatusItem s;
  char text[32] = { 0 };
  long nows = 0;
  struct tm *nowtm;
  statusitem_init(&s);
  s.text = text;
  nows = time(((void *)0));
  if (nows == ((time_t) -1)) {
    s.color = "#FF0000";
    s.text = "ERROR: DATETIME";
    line_append_item(g, &s);
  } else {
    nowtm = localtime(&nows);
    s.color = "#666666";
    snprintf(text, sizeof(text),
              "%d.%d.%d",
              nowtm->tm_mday,
              (nowtm->tm_mon) + 1,
              (nowtm->tm_year) + 1900
            );
    line_append_item(g, &s);
    s.color = "#BEBEBE";
    snprintf(text, sizeof(text),
              "%d:%.2d"
              ,nowtm->tm_hour,
              nowtm->tm_min
            );
    line_append_item(g, &s);
  }
}

variable_types: struct GlobalData { char *line; unsigned long linelen; unsigned long linemax; int firstItemDone; } *

variable_types: nows long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


decompiled_code:
__int64 __fastcall status_fan(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
char v5[17]; // [rsp+1h] [rbp-49h]
char v6[16]; // [rsp+2h] [rbp-48h] BYREF
char s[8]; // [rsp+12h] [rbp-38h] BYREF
__int64 v8; // [rsp+1Ah] [rbp-30h]
__int64 v9[5]; // [rsp+22h] [rbp-28h] BYREF

*(_QWORD *)s = 0LL;
v8 = 0LL;
statusitem_init(v9);
v9[1] = (__int64)s;
result = fileRead(v6, 16LL, a3);
if ( result > 0 )
{
if ( result <= 5 )
{
v5[result] = 0;
v9[0] = (__int64)"#CCCCCC";
snprintf(s, 0x10uLL, "%s%s rpm", a2, v6);
}
else
{
v9[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
result = line_append_item(a1, v9);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, __int64 a3)
{
__int64 result; // rax
char v5[17]; // [rsp+1h] [rbp-49h]
char v6[16]; // [rsp+2h] [rbp-48h] BYREF
char s[8]; // [rsp+12h] [rbp-38h] BYREF
__int64 v8; // [rsp+1Ah] [rbp-30h]
__int64 v9[5]; // [rsp+22h] [rbp-28h] BYREF

*(_QWORD *)s = 0LL;
v8 = 0LL;
statusitem_init(v9);
v9[1] = (__int64)s;
result = fileRead(v6, 16LL, a3);
if ( result > 0 )
{
if ( result <= 5 )
{
v5[result] = 0;
v9[0] = (__int64)"#CCCCCC";
snprintf(s, 0x10uLL, "%s%s rpm", a2, v6);
}
else
{
v9[0] = (__int64)"#FF0000";
snprintf(s, 0x10uLL, "%sERROR", a2);
}
result = line_append_item(a1, v9);
}
return result;
}

original_code:
void status_fan(GlobalData *g, char *title, char *sysfile)
{
  StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  ssize_t stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen > 5) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 1] = '\0';
    s.color = "#CCCCCC";
    snprintf(text, sizeof(text), "%s%s rpm", title, stline);
  }
  line_append_item(g, &s);
}

canonical_original_code:
void status_fan(struct GlobalData *g, char *title, char *sysfile)
{
  struct StatusItem s;
  char text[16] = { 0 };
  char stline[16];
  long stlen;
  statusitem_init(&s);
  s.text = text;
  stlen = fileRead(stline, sizeof(stline), sysfile);
  if (stlen <= 0) {
    return;
  }
  if (stlen > 5) {
    s.color = "#FF0000";
    snprintf(text, sizeof(text), "%sERROR", title);
  } else {
    stline[stlen - 1] = '\0';
    s.color = "#CCCCCC";
    snprintf(text, sizeof(text), "%s%s rpm", title, stline);
  }
  line_append_item(g, &s);
}

variable_types:
title
char *

variable_types:
sysfile
char *

variable_types:
text
char [16]

variable_types:
stline
char [16]

variable_types: stlen long


repo: notfed/nonce

decompiled_code:
__int64 __fastcall nonce_next(__int64 a1, __int64 a2)
{
unsigned int v2; // ebx
int v3; // eax
int v4; // eax
int v5; // eax
int v6; // eax
char buf[12]; // [rsp+4h] [rbp-84h] BYREF
char v9[8]; // [rsp+10h] [rbp-78h] BYREF
__int64 v10; // [rsp+18h] [rbp-70h]
__m128i v11; // [rsp+30h] [rbp-58h] BYREF
__int64 v12; // [rsp+40h] [rbp-48h]
__m128i v13; // [rsp+50h] [rbp-38h] BYREF
__int64 v14; // [rsp+60h] [rbp-28h]

byte_zero(buf, 12LL);
byte_zero(v9, 24LL);
v10 = 1LL;
while ( 1 )
{
v2 = open_rw(a1);
if ( v2 != -1 )
break;
v3 = *_errno_location();
if ( v3 == error_intr || v3 == error_again )
{
sleep(1u);
}
else if ( v3 == error_noent )
{
nonce_create(a1);
}
else
{
strerr_die(111LL, "trynonce: error opening nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
}
while ( 1 )
{
v4 = read(v2, buf, 0xCuLL);
if ( v4 != -1 )
break;
v5 = *_errno_location();
if ( v5 == error_intr || v5 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error reading nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
if ( v4 != 12 )
strerr_die(111LL, "trynonce: error: short nonce", 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
taia_now(&v11);
taia_unpack(buf, &v13);
if ( (unsigned int)taia_less(&v11, &v13) )
{
v11 = _mm_loadu_si128(&v13);
v12 = v14;
}
taia_add(&v11, &v11, v9);
taia_pack(buf, &v11);
if ( (unsigned int)seek_set(v2, 0LL) == -1 )
strerr_die(111LL, "trynonce: error rewinding nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
while ( (unsigned int)write(v2, buf, 0xCuLL) == -1 )
{
v6 = *_errno_location();
if ( v6 == error_intr || v6 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error writing nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
close(v2);
return byte_copy(a2, 12LL, buf);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned int v2; // ebx
int v3; // eax
int v4; // eax
int v5; // eax
int v6; // eax
char buf[12]; // [rsp+4h] [rbp-84h] BYREF
char v9[8]; // [rsp+10h] [rbp-78h] BYREF
__int64 v10; // [rsp+18h] [rbp-70h]
__m128i v11; // [rsp+30h] [rbp-58h] BYREF
__int64 v12; // [rsp+40h] [rbp-48h]
__m128i v13; // [rsp+50h] [rbp-38h] BYREF
__int64 v14; // [rsp+60h] [rbp-28h]

byte_zero(buf, 12LL);
byte_zero(v9, 24LL);
v10 = 1LL;
while ( 1 )
{
v2 = open_rw(a1);
if ( v2 != -1 )
break;
v3 = *_errno_location();
if ( v3 == error_intr || v3 == error_again )
{
sleep(1u);
}
else if ( v3 == error_noent )
{
nonce_create(a1);
}
else
{
strerr_die(111LL, "trynonce: error opening nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
}
while ( 1 )
{
v4 = read(v2, buf, 0xCuLL);
if ( v4 != -1 )
break;
v5 = *_errno_location();
if ( v5 == error_intr || v5 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error reading nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
if ( v4 != 12 )
strerr_die(111LL, "trynonce: error: short nonce", 0LL, 0LL, 0LL, 0LL, 0LL, 0LL);
taia_now(&v11);
taia_unpack(buf, &v13);
if ( (unsigned int)taia_less(&v11, &v13) )
{
v11 = _mm_loadu_si128(&v13);
v12 = v14;
}
taia_add(&v11, &v11, v9);
taia_pack(buf, &v11);
if ( (unsigned int)seek_set(v2, 0LL) == -1 )
strerr_die(111LL, "trynonce: error rewinding nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
while ( (unsigned int)write(v2, buf, 0xCuLL) == -1 )
{
v6 = *_errno_location();
if ( v6 == error_intr || v6 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error writing nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
close(v2);
return byte_copy(a2, 12LL, buf);
}

original_code:
void nonce_next(const char *filepath, char *nonce)
{
 int fd;
 int i;
 int j;
        struct taia oldtemps;
        struct taia nowtemps;
        struct taia onenanosecond;
        char tmpnonce[12];
        byte_zero(tmpnonce,12);
        byte_zero((char*)&onenanosecond,sizeof(onenanosecond));
        onenanosecond.nano = 1;
 while((fd=open_rw(filepath))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else if((*__errno_location ())==error_noent)
   { nonce_create(filepath); continue; }
   else
   { strerr_die((111),("trynonce: error opening nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=read(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error reading nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 if(i!=12) {
   strerr_die((111),("trynonce: error: short nonce"),0,0,0,0,0,0);
 }
        taia_now(&nowtemps);
        taia_unpack(tmpnonce,&oldtemps);
        if(taia_less(&nowtemps,&oldtemps))
            nowtemps = oldtemps;
        taia_add(&nowtemps,&nowtemps,&onenanosecond);
        taia_pack(&tmpnonce,&nowtemps);
 if((seek_set((fd),(seek_pos) 0))==-1)
   strerr_die((111),("trynonce: error rewinding nonce: "),0,0,0,0,0,&strerr_sys);
 while((i=write(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error writing nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
 byte_copy(nonce,12,tmpnonce);
}

canonical_original_code:
void nonce_next(char *filepath, char *nonce)
{
 int fd;
 int i;
 int j;
        struct taia oldtemps;
        struct taia nowtemps;
        struct taia onenanosecond;
        char tmpnonce[12];
        byte_zero(tmpnonce,12);
        byte_zero((char *)&onenanosecond,sizeof(onenanosecond));
        onenanosecond.nano = 1;
 while((fd=open_rw(filepath))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else if((*__errno_location ())==error_noent)
   { nonce_create(filepath); continue; }
   else
   { strerr_die((111),("trynonce: error opening nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=read(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error reading nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 if(i!=12) {
   strerr_die((111),("trynonce: error: short nonce"),0,0,0,0,0,0);
 }
        taia_now(&nowtemps);
        taia_unpack(tmpnonce,&oldtemps);
        if(taia_less(&nowtemps,&oldtemps))
            nowtemps = oldtemps;
        taia_add(&nowtemps,&nowtemps,&onenanosecond);
        taia_pack(&tmpnonce,&nowtemps);
 if((seek_set((fd),(unsigned long) 0))==-1)
   strerr_die((111),("trynonce: error rewinding nonce: "),0,0,0,0,0,&strerr_sys);
 while((i=write(fd,tmpnonce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
   { sleep(1); continue; }
   else
   { strerr_die((111),("trynonce: error writing nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
 byte_copy(nonce,12,tmpnonce);
}

variable_types:
filepath
char *

variable_types:
nonce
char *

variable_types: fd int

variable_types: i int

variable_types: j int

variable_types: struct taia { struct tai { unsigned long long x; } sec; unsigned long nano; unsigned long atto; }

variable_types:
tmpnonce
char [12]

user_defined_types: struct taia { struct tai sec; unsigned long nano; unsigned long atto; }


decompiled_code:
int __fastcall nonce_create(__int64 a1)
{
int v1; // ebx
int result; // eax
int v3; // eax
char buf[36]; // [rsp+4h] [rbp-24h] BYREF

byte_zero(buf, 12LL);
while ( 1 )
{
v1 = open_excl(a1);
if ( v1 != -1 )
break;
result = *_errno_location();
if ( result == error_intr || result == error_again )
{
sleep(1u);
}
else
{
if ( result == error_exist )
return result;
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
}
while ( (unsigned int)write(v1, buf, 0xCuLL) == -1 )
{
v3 = *_errno_location();
if ( v3 == error_intr || v3 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
return close(v1);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int v1; // ebx
int result; // eax
int v3; // eax
char buf[36]; // [rsp+4h] [rbp-24h] BYREF

byte_zero(buf, 12LL);
while ( 1 )
{
v1 = open_excl(a1);
if ( v1 != -1 )
break;
result = *_errno_location();
if ( result == error_intr || result == error_again )
{
sleep(1u);
}
else
{
if ( result == error_exist )
return result;
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
}
while ( (unsigned int)write(v1, buf, 0xCuLL) == -1 )
{
v3 = *_errno_location();
if ( v3 == error_intr || v3 == error_again )
sleep(1u);
else
strerr_die(111LL, "trynonce: error creating nonce: ", 0LL, 0LL, 0LL, 0LL, 0LL, &strerr_sys);
}
return close(v1);
}

original_code:
void nonce_create(const char *filepath)
{
 int fd;
 int i;
 char zerononce[12];
 byte_zero(zerononce,12);
        while((fd=open_excl(filepath))==-1) {
          if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else if((*__errno_location ())==error_exist)
   { return; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=write(fd,zerononce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
}

canonical_original_code:
void nonce_create(char *filepath)
{
 int fd;
 int i;
 char zerononce[12];
 byte_zero(zerononce,12);
        while((fd=open_excl(filepath))==-1) {
          if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else if((*__errno_location ())==error_exist)
   { return; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 while((i=write(fd,zerononce,12))==-1) {
   if((*__errno_location ())==error_intr || (*__errno_location ())==error_again)
          { sleep(1); continue; }
          else
          { strerr_die((111),("trynonce: error creating nonce: "),0,0,0,0,0,&strerr_sys); }
 }
 close(fd);
}

variable_types:
zerononce
char [12]


decompiled_code:
__int64 __fastcall nonce_show(unsigned __int8 *a1)
{
unsigned int i; // ebx
unsigned int v3; // eax
__int64 v4; // rdx
unsigned int v5; // eax
_BYTE v7[72]; // [rsp+0h] [rbp-48h] BYREF

buffer_put(buffer_1, &unk_DB, 1LL);
for ( i = 1; i == 1; ++i )
{
v5 = fmt_xlong(v7, *a1);
if ( v5 == 1 )
goto LABEL_8;
buffer_put(buffer_1, v7, v5);
LABEL_4:
++a1;
}
buffer_put(buffer_1, &unk_DD, 1LL);
v3 = fmt_xlong(v7, *a1);
v4 = v3;
if ( v3 != 1 )
goto LABEL_3;
LABEL_8:
buffer_put(buffer_1, &unk_DF, 1LL);
v4 = 1LL;
LABEL_3:
buffer_put(buffer_1, v7, v4);
if ( i <= 0xB )
goto LABEL_4;
buffer_put(buffer_1, &unk_E1, 2LL);
return buffer_flush(buffer_1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
unsigned int i; // ebx
unsigned int v3; // eax
__int64 v4; // rdx
unsigned int v5; // eax
_BYTE v7[72]; // [rsp+0h] [rbp-48h] BYREF

buffer_put(buffer_1, &unk_DB, 1LL);
for ( i = 1; i == 1; ++i )
{
v5 = fmt_xlong(v7, *a1);
if ( v5 == 1 )
goto LABEL_8;
buffer_put(buffer_1, v7, v5);
LABEL_4:
++a1;
}
buffer_put(buffer_1, &unk_DD, 1LL);
v3 = fmt_xlong(v7, *a1);
v4 = v3;
if ( v3 != 1 )
goto LABEL_3;
LABEL_8:
buffer_put(buffer_1, &unk_DF, 1LL);
v4 = 1LL;
LABEL_3:
buffer_put(buffer_1, v7, v4);
if ( i <= 0xB )
goto LABEL_4;
buffer_put(buffer_1, &unk_E1, 2LL);
return buffer_flush(buffer_1);
}

original_code:
void nonce_show(const char *nonce)
{
 unsigned int i;
 unsigned int len;
 unsigned char buf[40];
 buffer_put(buffer_1,"{",1);
 for(i=0;i<12;i++)
 {
   if(i!=0) buffer_put(buffer_1,",",1);
   len = fmt_xlong(buf,(unsigned char)nonce[i]);
   if(len==1) buffer_put(buffer_1,"0",1);
   buffer_put(buffer_1,buf,len);
 }
 buffer_put(buffer_1,"}\n",2);
 buffer_flush(buffer_1);
}

canonical_original_code:
void nonce_show(char *nonce)
{
 unsigned int i;
 unsigned int len;
 unsigned char buf[40];
 buffer_put(buffer_1,"{",1);
 for(i=0;i<12;i++)
 {
   if(i!=0) buffer_put(buffer_1,",",1);
   len = fmt_xlong(buf,(unsigned char)nonce[i]);
   if(len==1) buffer_put(buffer_1,"0",1);
   buffer_put(buffer_1,buf,len);
 }
 buffer_put(buffer_1,"}\n",2);
 buffer_flush(buffer_1);
}

variable_types:
i
unsigned int

variable_types:
len
unsigned int

variable_types:
buf
unsigned char [40]


numbits

decompiled_code:
__int64 __fastcall numbits(int a1)
{
unsigned int v1; // edi
__int64 result; // rax
unsigned int v3; // edx

v1 = a1 - 1;
if ( !v1 )
return 0LL;
LODWORD(result) = 0;
do
{
result = (unsigned int)(result + 1);
v3 = v1;
v1 >>= 1;
}
while ( v3 > 1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
unsigned int v1; // edi
__int64 result; // rax
unsigned int v3; // edx

v1 = a1 - 1;
if ( !v1 )
return 0LL;
LODWORD(result) = 0;
do
{
result = (unsigned int)(result + 1);
v3 = v1;
v1 >>= 1;
}
while ( v3 > 1 );
return result;
}

original_code:
static int numbits(unsigned int num)
{
  register unsigned int c;
  register unsigned int n;
  c = 0;
  n = num-1;
  while(n) { ++c; n/=2; }
  return c;
}

variable_types:
num
unsigned int

variable_types:
c
unsigned int

variable_types:
n
unsigned int