showmatch

decompiled_code:
__int64 __fastcall showmatch(__int64 a1, int a2)
{
int v2; // ebx
__int64 result; // rax

if ( a2 <= 0 )
return 1LL;
v2 = 0;
while ( 1 )
{
result = selfinsert(8LL, 1LL);
if ( (_DWORD)result != 1 )
break;
if ( (unsigned int)balance() != 1 )
dobeep();
if ( a2 == ++v2 )
return 1LL;
}
return result;
}

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

if ( a2 <= 0 )
return 1LL;
v2 = 0;
while ( 1 )
{
result = selfinsert(8LL, 1LL);
if ( (_DWORD)result != 1 )
break;
if ( (unsigned int)func1() != 1 )
dobeep();
if ( a2 == ++v2 )
return 1LL;
}
return result;
}

original_code:
int
showmatch(int f, int n)
{
 int i, s;
 for (i = 0; i < n; i++) {
  if ((s = selfinsert(8, 1)) != 1)
   return (s);
  if (balance() != 1)
   dobeep();
 }
 return (1);
}

canonical_original_code:
int showmatch(int f, int n)
{
 int i; int s;
 for (i = 0; i < n; i++) {
  if ((s = selfinsert(8, 1)) != 1)
   return (s);
  if (balance() != 1)
   dobeep();
 }
 return (1);
}

variable_types: f int

variable_types: s int


spawncli

repo: scott-parker/mg-openbsd

decompiled_code:
__int64 spawncli()
{
__int64 result; // rax
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

ttcolor(1LL);
ttnowindow();
ttmove((unsigned int)(nrow - 1), 0LL);
if ( epresf )
{
tteeol();
epresf = 0;
}
result = ttcooked();
if ( (_DWORD)result )
{
tttidy();
ttflush();
sigprocmask(2, 0LL, &v1);
kill(0, 20);
sigprocmask(2, &v1, 0LL);
ttreinit();
sgarbf = 1;
result = ttraw();
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
sigset_t v1; // [rsp+0h] [rbp-88h] BYREF

ttcolor(1LL);
ttnowindow();
ttmove((unsigned int)(nrow - 1), 0LL);
if ( epresf )
{
tteeol();
epresf = 0;
}
result = ttcooked();
if ( (_DWORD)result )
{
tttidy();
ttflush();
sigprocmask(2, 0LL, &v1);
kill(0, 20);
sigprocmask(2, &v1, 0LL);
ttreinit();
sgarbf = 1;
result = ttraw();
}
return result;
}

original_code:
int
spawncli(int f, int n)
{
 sigset_t oset;
 ttcolor(1);
 ttnowindow();
 ttmove(nrow - 1, 0);
 if (epresf != 0) {
  tteeol();
  epresf = 0;
 }
 if (ttcooked() == 0)
  return (0);
 tttidy();
 ttflush();
 (void)sigprocmask(2, ((void *)0), &oset);
 (void)kill(0, 20);
 (void)sigprocmask(2, &oset, ((void *)0));
 ttreinit();
 sgarbf = 1;
 return (ttraw());
}

canonical_original_code:
int spawncli(int f, int n)
{
 struct __sigset_t oset;
 ttcolor(1);
 ttnowindow();
 ttmove(nrow - 1, 0);
 if (epresf != 0) {
  tteeol();
  epresf = 0;
 }
 if (ttcooked() == 0)
  return (0);
 tttidy();
 ttflush();
 (void)sigprocmask(2, ((void *)0), &oset);
 (void)kill(0, 20);
 (void)sigprocmask(2, &oset, ((void *)0));
 ttreinit();
 sgarbf = 1;
 return (ttraw());
}

variable_types: n int

variable_types: struct __sigset_t { unsigned long __val[-1]; }

return_type: int


decompiled_code:
__int64 theo_analyze()
{
const char *v0; // rbx
int v1; // ebp
__int64 v2; // rbp
__int64 v3; // rsi

v0 = (&talk)[(unsigned int)arc4random_uniform(109LL)];
v1 = strlen(v0);
newline(8LL, 2LL);
if ( v1 )
{
v2 = (__int64)&v0[v1 - 1 + 1];
do
{
v3 = (unsigned int)*v0++;
linsert(1LL, v3);
}
while ( v0 != (const char *)v2 );
}
newline(8LL, 2LL);
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
const char *v0; // rbx
int v1; // ebp
__int64 v2; // rbp
__int64 v3; // rsi

v0 = (&talk)[(unsigned int)arc4random_uniform(109LL)];
v1 = strlen(v0);
newline(8LL, 2LL);
if ( v1 )
{
v2 = (__int64)&v0[v1 - 1 + 1];
do
{
v3 = (unsigned int)*v0++;
linsert(1LL, v3);
}
while ( v0 != (const char *)v2 );
}
newline(8LL, 2LL);
return 1LL;
}

original_code:
static int
theo_analyze(int f, int n)
{
 const char *str;
 int len;
 str = talk[arc4random_uniform(ntalk)];
 len = strlen(str);
 newline(8, 2);
 while (len--)
  linsert(1, *str++);
 newline(8, 2);
 return (1);
}

canonical_original_code:
static int theo_analyze(int f, int n)
{
 char *str;
 int len;
 str = talk[arc4random_uniform(ntalk)];
 len = strlen(str);
 newline(8, 2);
 while (len--)
  linsert(1, *str++);
 newline(8, 2);
 return (1);
}

variable_types:
str
char *

variable_types: len int


theo

decompiled_code:
__int64 __fastcall theo(unsigned int a1, unsigned int a2)
{
__int64 v2; // rbx
__int64 v4; // rax

v2 = bfind("theo", 1LL);
if ( (unsigned int)bclear(v2) != 1 )
return 0LL;
*(_QWORD *)(v2 + 48) = name_mode("fundamental");
*(_QWORD *)(v2 + 56) = name_mode("theo");
*(_WORD *)(v2 + 88) = 1;
v4 = popbuf(v2, 0LL);
if ( !v4 )
return 0LL;
curbp = v2;
curwp = v4;
onlywind(a1, a2);
return 1LL;
}

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

v2 = bfind("theo", 1LL);
if ( (unsigned int)bclear(v2) != 1 )
return 0LL;
*(_QWORD *)(v2 + 48) = name_mode("fundamental");
*(_QWORD *)(v2 + 56) = name_mode("theo");
*(_WORD *)(v2 + 88) = 1;
v4 = popbuf(v2, 0LL);
if ( !v4 )
return 0LL;
curbp = v2;
curwp = v4;
onlywind(a1, a2);
return 1LL;
}

original_code:
static int
theo(int f, int n)
{
 struct buffer *bp;
 struct mgwin *wp;
 bp = bfind("theo", 1);
 if (bclear(bp) != 1)
  return (0);
 bp->b_modes[0] = name_mode("fundamental");
 bp->b_modes[1] = name_mode("theo");
 bp->b_nmodes = 1;
 if ((wp = popbuf(bp, 0x00)) == ((void *)0))
  return (0);
 curbp = bp;
 curwp = wp;
 onlywind(f, n);
 return (1);
}

variable_types: struct buffer { struct list { union { struct mgwin { struct list w_list; struct buffer *w_bufp; struct line { struct line *l_fp; struct line *l_bp; int l_size; int l_used; char *l_text; } *w_linep; struct line *w_dotp; struct line *w_markp; int w_doto; int w_marko; int w_toprow; int w_ntrows; int w_frame; char w_rflag; char w_flag; struct line *w_wrapline; int w_dotline; int w_markline; } *l_wp; struct buffer *x_bp; struct list *l_nxt; } l_p; char *l_name; } b_list; struct buffer *b_altb; struct line *b_dotp; struct line *b_markp; struct line *b_headp; struct maps_s { struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; } map_element[1]; } *p_map; char *p_name; struct maps_s *p_next; } *b_modes[4]; int b_doto; int b_marko; short b_nmodes; char b_nwnd; char b_flag; char b_fname[1024]; char b_cwd[1024]; struct fileinfo { unsigned int fi_uid; unsigned int fi_gid; unsigned int fi_mode; struct timespec { long tv_sec; long tv_nsec; } fi_mtime; } b_fi; struct undoq { struct undo_rec { struct { struct undo_rec *tqe_next; struct undo_rec **tqe_prev; } next; enum { INSERT=1, DELETE=2, BOUNDARY=3, MODIFIED=4, DELREG=5 } type; struct region { struct line *r_linep; int r_offset; int r_lineno; int r_size; } region; int pos; char *content; } *tqh_first; struct undo_rec **tqh_last; } b_undo; struct undo_rec *b_undoptr; int b_dotline; int b_markline; int b_lines; } *

variable_types: struct mgwin { struct list { union { struct mgwin *l_wp; struct buffer { struct list b_list; struct buffer *b_altb; struct line { struct line *l_fp; struct line *l_bp; int l_size; int l_used; char *l_text; } *b_dotp; struct line *b_markp; struct line *b_headp; struct maps_s { struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; } map_element[1]; } *p_map; char *p_name; struct maps_s *p_next; } *b_modes[4]; int b_doto; int b_marko; short b_nmodes; char b_nwnd; char b_flag; char b_fname[1024]; char b_cwd[1024]; struct fileinfo { unsigned int fi_uid; unsigned int fi_gid; unsigned int fi_mode; struct timespec { long tv_sec; long tv_nsec; } fi_mtime; } b_fi; struct undoq { struct undo_rec { struct { struct undo_rec *tqe_next; struct undo_rec **tqe_prev; } next; enum { INSERT=1, DELETE=2, BOUNDARY=3, MODIFIED=4, DELREG=5 } type; struct region { struct line *r_linep; int r_offset; int r_lineno; int r_size; } region; int pos; char *content; } *tqh_first; struct undo_rec **tqh_last; } b_undo; struct undo_rec *b_undoptr; int b_dotline; int b_markline; int b_lines; } *x_bp; struct list *l_nxt; } l_p; char *l_name; } w_list; struct buffer *w_bufp; struct line *w_linep; struct line *w_dotp; struct line *w_markp; int w_doto; int w_marko; int w_toprow; int w_ntrows; int w_frame; char w_rflag; char w_flag; struct line *w_wrapline; int w_dotline; int w_markline; } *

user_defined_types: struct buffer { struct list b_list; struct buffer *b_altb; struct line *b_dotp; struct line *b_markp; struct line *b_headp; struct maps_s *b_modes[4]; int b_doto; int b_marko; short b_nmodes; char b_nwnd; char b_flag; char b_fname[1024]; char b_cwd[1024]; struct fileinfo b_fi; struct undoq b_undo; struct undo_rec *b_undoptr; int b_dotline; int b_markline; int b_lines; }

user_defined_types: struct mgwin { struct list w_list; struct buffer *w_bufp; struct line *w_linep; struct line *w_dotp; struct line *w_markp; int w_doto; int w_marko; int w_toprow; int w_ntrows; int w_frame; char w_rflag; char w_flag; struct line *w_wrapline; int w_dotline; int w_markline; }

user_defined_types: struct list { union { struct mgwin *l_wp; struct buffer *x_bp; struct list *l_nxt; } l_p; char *l_name; }

user_defined_types: struct fileinfo { unsigned int fi_uid; unsigned int fi_gid; unsigned int fi_mode; struct timespec fi_mtime; }

user_defined_types: struct undoq { struct undo_rec *tqh_first; struct undo_rec **tqh_last; }

user_defined_types: struct maps_s { struct keymap_s *p_map; char *p_name; struct maps_s *p_next; }

user_defined_types: struct undo_rec { struct { struct undo_rec *tqe_next; struct undo_rec **tqe_prev; } next; enum { INSERT=1, DELETE=2, BOUNDARY=3, MODIFIED=4, DELREG=5 } type; struct region region; int pos; char *content; }

user_defined_types: struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element map_element[1]; }


decompiled_code:
__int64 theo_init()
{
funmap_add(theo, "theo");
return maps_add(&theomap, "theo");
}

canonical_decompiled_code:
__int64 func2()
{
funmap_add(theo, "theo");
return maps_add(&theomap, "theo");
}

original_code:
void
theo_init(void)
{
 funmap_add(theo, "theo");
 maps_add((KEYMAP *)&theomap, "theo");
}

canonical_original_code:
void theo_init()
{
 funmap_add(theo, "theo");
 maps_add((struct keymap_s *)&theomap, "theo");
}

return_type: void

user_defined_types: struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; }


strtonum

decompiled_code:
__int64 __fastcall strtonum(char *nptr, __int64 a2, __int64 a3, _QWORD *a4)
{
int *v6; // rax
int v7; // er15
int *v8; // rbx
int v9; // edx
__int64 v10; // rax
__int64 v11; // r8
char *endptr[16]; // [rsp+8h] [rbp-80h] BYREF

endptr[3] = "invalid";
endptr[5] = "too small";
endptr[7] = "too large";
v6 = _errno_location();
v7 = *v6;
v8 = v6;
*v6 = 0;
if ( a2 > a3 || (v11 = strtoll(nptr, endptr, 10), endptr[0] == nptr) || *endptr[0] )
{
v9 = 22;
v10 = 1LL;
goto LABEL_3;
}
if ( v11 == 0x8000000000000000LL )
{
v9 = *v8;
if ( *v8 == 34 )
{
LABEL_19:
v10 = 2LL;
goto LABEL_3;
}
if ( a2 == 0x8000000000000000LL )
goto LABEL_12;
LABEL_18:
v9 = 34;
goto LABEL_19;
}
if ( a2 > v11 )
goto LABEL_18;
if ( v11 == 0x7FFFFFFFFFFFFFFFLL )
{
v9 = *v8;
if ( *v8 == 34 )
{
v10 = 3LL;
LABEL_3:
if ( a4 )
{
v11 = 0LL;
*a4 = endptr[2 * v10 + 1];
*v8 = v9;
}
else
{
*v8 = v9;
v11 = 0LL;
}
return v11;
}
}
v9 = 34;
v10 = 3LL;
if ( a3 < v11 )
goto LABEL_3;
LABEL_12:
if ( a4 )
*a4 = 0LL;
*v8 = v7;
return v11;
}

original_code:
long long
strtonum(const char *numstr, long long minval, long long maxval,
    const char **errstrp)
{
 long long ll = 0;
 int error = 0;
 char *ep;
 struct errval {
  const char *errstr;
  int err;
 } ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval) {
  error = 1;
 } else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

canonical_original_code:
long long strtonum(char *numstr, long long minval, long long maxval, char **errstrp)
{
 long long ll = 0;
 int error = 0;
 char *ep;
 struct errval ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval) {
  error = 1;
 } else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

variable_types:
numstr
char *

variable_types:
minval
long long

variable_types:
maxval
long long

variable_types:
errstrp
char **

variable_types:
ll
long long

variable_types: error int

variable_types:
ep
char *

variable_types: struct errval { char *errstr; int err; } [4]

return_type: long long


isescaped

decompiled_code:
__int64 __fastcall isescaped(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
result = 0LL;
v5 = a2 - a1;
if ( a1 <= (unsigned __int64)v4 )
{
do
{
if ( *v4 != a3 )
return result & 1;
++result;
--v4;
}
while ( v5 != result );
result = v5 & 1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
result = 0LL;
v5 = a2 - a1;
if ( a1 <= (unsigned __int64)v4 )
{
do
{
if ( *v4 != a3 )
return result & 1;
++result;
--v4;
}
while ( v5 != result );
result = v5 & 1;
}
}
return result;
}

original_code:
static int
isescaped(const char *sp, const char *p, int esc)
{
 const char *cp;
 size_t ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

canonical_original_code:
static int isescaped(char *sp, char *p, int esc)
{
 char *cp;
 unsigned long ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

variable_types:
sp
char *

variable_types:
p
char *

variable_types: esc int

variable_types:
cp
char *

variable_types:
ne
unsigned long


reallocarray

decompiled_code:
void *__fastcall reallocarray(void *a1, unsigned __int64 a2, unsigned __int64 a3)
{
if ( a3 <= 0xFFFFFFFF && a2 <= 0xFFFFFFFF || !a2 || is_mul_ok(a2, a3) )
return realloc(a1, a3 * a2);
*_errno_location() = 12;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1, unsigned __int64 a2, unsigned __int64 a3)
{
if ( a3 <= 0xFFFFFFFF && a2 <= 0xFFFFFFFF || !a2 || is_mul_ok(a2, a3) )
return realloc(a1, a3 * a2);
*_errno_location() = 12;
return 0LL;
}

original_code:
void *
reallocarray(void *optr, size_t nmemb, size_t size)
{
 if ((nmemb >= ((size_t)1 << (sizeof(size_t) * 4)) || size >= ((size_t)1 << (sizeof(size_t) * 4))) &&
     nmemb > 0 && (18446744073709551615UL) / nmemb < size) {
  (*__errno_location ()) = 12;
  return ((void *)0);
 }
 return realloc(optr, size * nmemb);
}

canonical_original_code:
void *reallocarray(void *optr, unsigned long nmemb, unsigned long size)
{
 if ((nmemb >= ((unsigned long)1 << (sizeof(unsigned long) * 4)) || size >= ((unsigned long)1 << (sizeof(unsigned long) * 4))) &&
     nmemb > 0 && (18446744073709551615UL) / nmemb < size) {
  (*__errno_location ()) = 12;
  return ((void *)0);
 }
 return realloc(optr, size * nmemb);
}

variable_types:
optr
void *

variable_types:
nmemb
unsigned long

variable_types:
size
unsigned long

return_type: void *


decompiled_code:
void maps_init()
{
void **v0; // rax
__int16 **v1; // rdx
__int16 **v2; // rcx

maps = (__int64)&fundamental_mode;
if ( off_108 )
{
v0 = &map_table;
v1 = &fundamental_mode;
do
{
v2 = v1;
v1 = (__int16 **)v0;
v0 += 3;
*(v0 - 1) = v2;
}
while ( v0[1] );
maps = (__int64)v1;
}
}

canonical_decompiled_code:
void func0()
{
void **v0; // rax
__int16 **v1; // rdx
__int16 **v2; // rcx

maps = (__int64)&fundamental_mode;
if ( off_108 )
{
v0 = &map_table;
v1 = &fundamental_mode;
do
{
v2 = v1;
v1 = (__int16 **)v0;
v0 += 3;
*(v0 - 1) = v2;
}
while ( v0[1] );
maps = (__int64)v1;
}
}

original_code:
void
maps_init(void)
{
 int i;
 struct maps_s *mp;
 maps = &fundamental_mode;
 for (i = 0; map_table[i].p_name != ((void *)0); i++) {
  mp = &map_table[i];
  mp->p_next = maps;
  maps = mp;
 }
}

canonical_original_code:
void maps_init()
{
 int i;
 struct maps_s *mp;
 maps = &fundamental_mode;
 for (i = 0; map_table[i].p_name != ((void *)0); i++) {
  mp = &map_table[i];
  mp->p_next = maps;
  maps = mp;
 }
}

variable_types: i int

variable_types: struct maps_s { struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; } map_element[1]; } *p_map; char *p_name; struct maps_s *p_next; } *


decompiled_code:
__int64 __fastcall maps_add(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx

v2 = malloc(0x18uLL);
if ( !v2 )
return 0LL;
v3 = maps;
v2[1] = a2;
*v2 = a1;
v2[2] = v3;
maps = (__int64)v2;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx

v2 = malloc(0x18uLL);
if ( !v2 )
return 0LL;
v3 = maps;
v2[1] = a2;
*v2 = a1;
v2[2] = v3;
maps = (__int64)v2;
return 1LL;
}

original_code:
int
maps_add(KEYMAP *map, const char *name)
{
 struct maps_s *mp;
 if ((mp = malloc(sizeof(*mp))) == ((void *)0))
  return (0);
 mp->p_name = name;
 mp->p_map = map;
 mp->p_next = maps;
 maps = mp;
 return (1);
}

canonical_original_code:
int maps_add(struct keymap_s *map, char *name)
{
 struct maps_s *mp;
 if ((mp = malloc(sizeof(*mp))) == ((void *)0))
  return (0);
 mp->p_name = name;
 mp->p_map = map;
 mp->p_next = maps;
 maps = mp;
 return (1);
}

variable_types: struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; } map_element[1]; } *

variable_types:
name
char *


decompiled_code:
__int64 __fastcall name_mode(char *s2)
{
__int64 i; // r12

for ( i = maps; i; i = *(_QWORD *)(i + 16) )
{
if ( !strcmp(*(const char **)(i + 8), s2) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s2)
{
__int64 i; // r12

for ( i = maps; i; i = *(_QWORD *)(i + 16) )
{
if ( !strcmp(*(const char **)(i + 8), s2) )
break;
}
return i;
}

original_code:
struct maps_s *
name_mode(const char *name)
{
 struct maps_s *mp;
 for (mp = maps; mp != ((void *)0); mp = mp->p_next)
  if (strcmp(mp->p_name, name) == 0)
   return (mp);
 return (((void *)0));
}

canonical_original_code:
struct maps_s *name_mode(char *name)
{
 struct maps_s *mp;
 for (mp = maps; mp != ((void *)0); mp = mp->p_next)
  if (strcmp(mp->p_name, name) == 0)
   return (mp);
 return (((void *)0));
}

return_type: struct maps_s { struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; } map_element[1]; } *p_map; char *p_name; struct maps_s *p_next; } *


decompiled_code:
void __fastcall explicit_bzero(void *a1, size_t a2)
{
memset(a1, 0, a2);
_explicit_bzero_hook();
}

canonical_decompiled_code:
void __fastcall func1(void *a1, size_t a2)
{
memset(a1, 0, a2);
func0();
}

original_code:
void
explicit_bzero(void *buf, size_t len)
{
 memset(buf, 0, len);
 __explicit_bzero_hook(buf, len);
}

canonical_original_code:
void explicit_bzero(void *buf, unsigned long len)
{
 memset(buf, 0, len);
 __explicit_bzero_hook(buf, len);
}

variable_types:
buf
void *

variable_types:
len
unsigned long


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(_BYTE *a1, _BYTE *a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
char v5; // dl
_BYTE *v6; // rdx
__int64 result; // rax

if ( a3 )
{
v3 = (__int64)&a1[a3 - 1];
v4 = a2;
while ( 1 )
{
v6 = v4++;
if ( a1 == (_BYTE *)v3 )
break;
v5 = *(v4 - 1);
*a1++ = v5;
if ( !v5 )
goto LABEL_6;
}
*a1 = 0;
v4 = v6 + 1;
if ( *v6 )
goto LABEL_8;
LABEL_6:
result = v4 - a2 - 1;
}
else
{
v4 = a2;
do
LABEL_8:
++v4;
while ( *(v4 - 1) );
result = v4 - a2 - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, _BYTE *a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
char v5; // dl
_BYTE *v6; // rdx
__int64 result; // rax

if ( a3 )
{
v3 = (__int64)&a1[a3 - 1];
v4 = a2;
while ( 1 )
{
v6 = v4++;
if ( a1 == (_BYTE *)v3 )
break;
v5 = *(v4 - 1);
*a1++ = v5;
if ( !v5 )
goto LABEL_6;
}
*a1 = 0;
v4 = v6 + 1;
if ( *v6 )
goto LABEL_8;
LABEL_6:
result = v4 - a2 - 1;
}
else
{
v4 = a2;
do
LABEL_8:
++v4;
while ( *(v4 - 1) );
result = v4 - a2 - 1;
}
return result;
}

original_code:
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
 const char *osrc = src;
 size_t nleft = dsize;
 if (nleft != 0) {
  while (--nleft != 0) {
   if ((*dst++ = *src++) == '\0')
    break;
  }
 }
 if (nleft == 0) {
  if (dsize != 0)
   *dst = '\0';
  while (*src++)
   ;
 }
 return(src - osrc - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long dsize)
{
 char *osrc = src;
 unsigned long nleft = dsize;
 if (nleft != 0) {
  while (--nleft != 0) {
   if ((*dst++ = *src++) == '\0')
    break;
  }
 }
 if (nleft == 0) {
  if (dsize != 0)
   *dst = '\0';
  while (*src++)
   ;
 }
 return(src - osrc - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
dsize
unsigned long

variable_types:
osrc
char *

variable_types:
nleft
unsigned long

return_type: unsigned long


repo: scottellis/overo-blinkm

decompiled_code:
int __fastcall read_error(unsigned __int8 a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
return fprintf(stderr, "Read failed for device 0x%02X: %s\n", a1, v2);
}

canonical_decompiled_code:
int __fastcall func0(unsigned __int8 a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
return fprintf(stderr, "Read failed for device 0x%02X: %s\n", a1, v2);
}

original_code:
void read_error(uint8_t led)
{
 fprintf(stderr, "Read failed for device 0x%02X: %s\n", led, strerror((*__errno_location ())));
}

canonical_original_code:
void read_error(unsigned char led)
{
 fprintf(stderr, "Read failed for device 0x%02X: %s\n", led, strerror((*__errno_location ())));
}

variable_types:
led
unsigned char


decompiled_code:
int __fastcall write_error(unsigned __int8 a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
return fprintf(stderr, "Write failed for device 0x%02X: %s\n", a1, v2);
}

canonical_decompiled_code:
int __fastcall func1(unsigned __int8 a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
return fprintf(stderr, "Write failed for device 0x%02X: %s\n", a1, v2);
}

original_code:
void write_error(uint8_t led)
{
 fprintf(stderr, "Write failed for device 0x%02X: %s\n", led, strerror((*__errno_location ())));
}

canonical_original_code:
void write_error(unsigned char led)
{
 fprintf(stderr, "Write failed for device 0x%02X: %s\n", led, strerror((*__errno_location ())));
}


decompiled_code:
__int64 __fastcall blinkm_get_address(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er13
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = i2c_start_transaction(a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
buf[0] = 97;
if ( (unsigned int)write(v1, buf, 1uLL) == 1 )
{
buf[0] = 0;
v3 = read(v2, buf, 1uLL);
if ( v3 != 1 )
{
v3 = -1;
read_error(a1);
}
}
else
{
v3 = -1;
write_error(a1);
}
i2c_end_transaction(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er13
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = i2c_start_transaction(a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
buf[0] = 97;
if ( (unsigned int)write(v1, buf, 1uLL) == 1 )
{
buf[0] = 0;
v3 = read(v2, buf, 1uLL);
if ( v3 != 1 )
{
v3 = -1;
func0(a1);
}
}
else
{
v3 = -1;
func1(a1);
}
i2c_end_transaction(v2);
return v3;
}

original_code:
int blinkm_get_address(uint8_t led)
{
 int fh, result;
 uint8_t data;
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data = 0x61;
 result = write(fh, &data, 1);
 if (result == 1) {
  data = 0;
  result = read(fh, &data, 1);
  if (result != 1) {
   read_error(led);
   result = -1;
  }
 } else {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_get_address(unsigned char led)
{
 int fh; int result;
 unsigned char data;
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data = 0x61;
 result = write(fh, &data, 1);
 if (result == 1) {
  data = 0;
  result = read(fh, &data, 1);
  if (result != 1) {
   read_error(led);
   result = -1;
  }
 } else {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

variable_types: fh int

variable_types: result int

variable_types:
data
unsigned char


decompiled_code:
__int64 __fastcall blinkm_set_address(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er12
char buf[2]; // [rsp+8h] [rbp-20h] BYREF
__int16 v6; // [rsp+Ah] [rbp-1Eh]
unsigned __int8 v7; // [rsp+Ch] [rbp-1Ch]

v1 = i2c_start_transaction(0LL);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
buf[0] = 65;
buf[1] = a1;
v6 = 3536;
v7 = a1;
if ( (unsigned int)write(v1, buf, 5uLL) == 5 )
{
v3 = a1;
fprintf(stdout, "Set new blinkm address to 0x%02X\n", a1);
}
else
{
v3 = -1;
write_error(0);
}
i2c_end_transaction(v2);
msleep(100LL);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er12
char buf[2]; // [rsp+8h] [rbp-20h] BYREF
__int16 v6; // [rsp+Ah] [rbp-1Eh]
unsigned __int8 v7; // [rsp+Ch] [rbp-1Ch]

v1 = i2c_start_transaction(0LL);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
buf[0] = 65;
buf[1] = a1;
v6 = 3536;
v7 = a1;
if ( (unsigned int)write(v1, buf, 5uLL) == 5 )
{
v3 = a1;
fprintf(stdout, "Set new blinkm address to 0x%02X\n", a1);
}
else
{
v3 = -1;
func1(0);
}
i2c_end_transaction(v2);
msleep(100LL);
return v3;
}

original_code:
int blinkm_set_address(uint8_t new_addr)
{
 int fh, result;
 uint8_t data[8];
 fh = i2c_start_transaction(0x00);
 if (fh < 0)
  return -1;
 data[0] = 0x41;
 data[1] = new_addr;
 data[2] = 0xd0;
 data[3] = 0x0d;
 data[4] = new_addr;
 result = write(fh, &data, 5);
 if (result == 5) {
  fprintf(stdout, "Set new blinkm address to 0x%02X\n", new_addr);
  result = new_addr;
 } else {
  write_error(0x00);
  result = -1;
 }
 i2c_end_transaction(fh);
 msleep(100);
 return result;
}

canonical_original_code:
int blinkm_set_address(unsigned char new_addr)
{
 int fh; int result;
 unsigned char data[8];
 fh = i2c_start_transaction(0x00);
 if (fh < 0)
  return -1;
 data[0] = 0x41;
 data[1] = new_addr;
 data[2] = 0xd0;
 data[3] = 0x0d;
 data[4] = new_addr;
 result = write(fh, &data, 5);
 if (result == 5) {
  fprintf(stdout, "Set new blinkm address to 0x%02X\n", new_addr);
  result = new_addr;
 } else {
  write_error(0x00);
  result = -1;
 }
 i2c_end_transaction(fh);
 msleep(100);
 return result;
}

variable_types:
data
unsigned char [8]


decompiled_code:
__int64 __fastcall blinkm_set_rgb_color_now(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 110;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
write_error(a1);
}
i2c_end_transaction(v7);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 110;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
func1(a1);
}
i2c_end_transaction(v7);
return v8;
}

original_code:
int blinkm_set_rgb_color_now(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x6E;
 data[1] = r;
 data[2] = g;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_set_rgb_color_now(unsigned char led, unsigned char r, unsigned char g, unsigned char b)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x6E;
 data[1] = r;
 data[2] = g;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char

variable_types:
data
unsigned char [4]


decompiled_code:
__int64 __fastcall blinkm_fade_to_rgb_color(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 99;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
write_error(a1);
}
i2c_end_transaction(v7);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 99;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
func1(a1);
}
i2c_end_transaction(v7);
return v8;
}

original_code:
int blinkm_fade_to_rgb_color(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x63;
 data[1] = r;
 data[2] = g;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_fade_to_rgb_color(unsigned char led, unsigned char r, unsigned char g, unsigned char b)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x63;
 data[1] = r;
 data[2] = g;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}


decompiled_code:
__int64 __fastcall blinkm_fade_to_hsb_color(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 104;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
write_error(a1);
}
i2c_end_transaction(v7);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 104;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
func1(a1);
}
i2c_end_transaction(v7);
return v8;
}

original_code:
int blinkm_fade_to_hsb_color(uint8_t led, uint8_t h, uint8_t s, uint8_t b)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x68;
 data[1] = h;
 data[2] = s;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_fade_to_hsb_color(unsigned char led, unsigned char h, unsigned char s, unsigned char b)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x68;
 data[1] = h;
 data[2] = s;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

variable_types:
h
unsigned char

variable_types:
s
unsigned char


decompiled_code:
__int64 __fastcall blinkm_fade_to_random_rgb_color(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 67;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
write_error(a1);
}
i2c_end_transaction(v7);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 67;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
func1(a1);
}
i2c_end_transaction(v7);
return v8;
}

original_code:
int blinkm_fade_to_random_rgb_color(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x43;
 data[1] = r;
 data[2] = g;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_fade_to_random_rgb_color(unsigned char led, unsigned char r, unsigned char g, unsigned char b)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x43;
 data[1] = r;
 data[2] = g;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}


decompiled_code:
__int64 __fastcall blinkm_fade_to_random_hsb_color(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 72;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
write_error(a1);
}
i2c_end_transaction(v7);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 a1, char a2, char a3, char a4)
{
int v6; // eax
unsigned int v7; // ebp
unsigned int v8; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = i2c_start_transaction(a1);
if ( v6 < 0 )
return (unsigned int)-1;
buf[3] = a4;
buf[2] = a3;
v7 = v6;
buf[0] = 72;
buf[1] = a2;
v8 = write(v6, buf, 4uLL);
if ( v8 != 4 )
{
v8 = -1;
func1(a1);
}
i2c_end_transaction(v7);
return v8;
}

original_code:
int blinkm_fade_to_random_hsb_color(uint8_t led, uint8_t h, uint8_t s, uint8_t b)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x48;
 data[1] = h;
 data[2] = s;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_fade_to_random_hsb_color(unsigned char led, unsigned char h, unsigned char s, unsigned char b)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x48;
 data[1] = h;
 data[2] = s;
 data[3] = b;
 result = write(fh, data, 4);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}


decompiled_code:
__int64 __fastcall blinkm_get_current_rgb_color(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er12
int buf[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = i2c_start_transaction(a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
LOBYTE(buf[0]) = 103;
if ( (unsigned int)write(v1, buf, 1uLL) == 1 )
{
buf[0] = 0;
if ( (unsigned int)read(v2, buf, 3uLL) == 3 )
{
v3 = BYTE2(buf[0]) + (LOBYTE(buf[0]) << 16) + (BYTE1(buf[0]) << 8);
}
else
{
v3 = -1;
read_error(a1);
}
}
else
{
v3 = -1;
write_error(a1);
}
i2c_end_transaction(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er12
int buf[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = i2c_start_transaction(a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
LOBYTE(buf[0]) = 103;
if ( (unsigned int)write(v1, buf, 1uLL) == 1 )
{
buf[0] = 0;
if ( (unsigned int)read(v2, buf, 3uLL) == 3 )
{
v3 = BYTE2(buf[0]) + (LOBYTE(buf[0]) << 16) + (BYTE1(buf[0]) << 8);
}
else
{
v3 = -1;
func0(a1);
}
}
else
{
v3 = -1;
func1(a1);
}
i2c_end_transaction(v2);
return v3;
}

original_code:
int blinkm_get_current_rgb_color(uint8_t led)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x67;
 result = write(fh, data, 1);
 if (result == 1) {
  bzero(data, sizeof(data));
  result = read(fh, data, 3);
  if (result == 3) {
   result = (data[0] << 16) + (data[1] << 8) + data[2];
  } else {
   read_error(led);
   result = -1;
  }
 } else {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_get_current_rgb_color(unsigned char led)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x67;
 result = write(fh, data, 1);
 if (result == 1) {
  bzero(data, sizeof(data));
  result = read(fh, data, 3);
  if (result == 3) {
   result = (data[0] << 16) + (data[1] << 8) + data[2];
  } else {
   read_error(led);
   result = -1;
  }
 } else {
  write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}


decompiled_code:
__int64 __fastcall blinkm_stop_script(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er13
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v1 = i2c_start_transaction(a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
buf[0] = 111;
v3 = write(v1, buf, 1uLL);
i2c_end_transaction(v2);
if ( v3 != 1 )
{
v3 = -1;
write_error(a1);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // er13
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v1 = i2c_start_transaction(a1);
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
buf[0] = 111;
v3 = write(v1, buf, 1uLL);
i2c_end_transaction(v2);
if ( v3 != 1 )
{
v3 = -1;
func1(a1);
}
return v3;
}

original_code:
int blinkm_stop_script(uint8_t led)
{
 int fh, result;
 uint8_t data;
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data = 0x6F;
 result = write(fh, &data, 1);
 i2c_end_transaction(fh);
 if (result != 1) {
  write_error(led);
  result = -1;
 }
 return result;
}

canonical_original_code:
int blinkm_stop_script(unsigned char led)
{
 int fh; int result;
 unsigned char data;
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data = 0x6F;
 result = write(fh, &data, 1);
 i2c_end_transaction(fh);
 if (result != 1) {
  write_error(led);
  result = -1;
 }
 return result;
}


decompiled_code:
__int64 __fastcall blinkm_play_script(unsigned __int8 a1, char a2, char a3)
{
int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = i2c_start_transaction(a1);
if ( v4 < 0 )
return (unsigned int)-1;
buf[2] = a3;
buf[1] = a2;
v5 = v4;
buf[0] = 112;
buf[3] = 0;
v6 = write(v4, buf, 4uLL);
i2c_end_transaction(v5);
if ( v6 != 4 )
{
v6 = -1;
write_error(a1);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int8 a1, char a2, char a3)
{
int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = i2c_start_transaction(a1);
if ( v4 < 0 )
return (unsigned int)-1;
buf[2] = a3;
buf[1] = a2;
v5 = v4;
buf[0] = 112;
buf[3] = 0;
v6 = write(v4, buf, 4uLL);
i2c_end_transaction(v5);
if ( v6 != 4 )
{
v6 = -1;
func1(a1);
}
return v6;
}

original_code:
int blinkm_play_script(uint8_t led, uint8_t script_id, uint8_t num_repeats)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x70;
 data[1] = script_id;
 data[2] = num_repeats;
 data[3] = 0;
 result = write(fh, data, 4);
 i2c_end_transaction(fh);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 return result;
}

canonical_original_code:
int blinkm_play_script(unsigned char led, unsigned char script_id, unsigned char num_repeats)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x70;
 data[1] = script_id;
 data[2] = num_repeats;
 data[3] = 0;
 result = write(fh, data, 4);
 i2c_end_transaction(fh);
 if (result != 4) {
  write_error(led);
  result = -1;
 }
 return result;
}


decompiled_code:
__int64 __fastcall blinkm_set_fade_speed(unsigned __int8 a1, char a2)
{
int v2; // eax
unsigned int v3; // ebp
unsigned int v4; // er13
char buf[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = i2c_start_transaction(a1);
if ( v2 < 0 )
return (unsigned int)-1;
buf[1] = a2;
v3 = v2;
buf[0] = 102;
v4 = write(v2, buf, 2uLL);
i2c_end_transaction(v3);
if ( v4 != 2 )
{
v4 = -1;
write_error(a1);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned __int8 a1, char a2)
{
int v2; // eax
unsigned int v3; // ebp
unsigned int v4; // er13
char buf[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = i2c_start_transaction(a1);
if ( v2 < 0 )
return (unsigned int)-1;
buf[1] = a2;
v3 = v2;
buf[0] = 102;
v4 = write(v2, buf, 2uLL);
i2c_end_transaction(v3);
if ( v4 != 2 )
{
v4 = -1;
func1(a1);
}
return v4;
}

original_code:
int blinkm_set_fade_speed(uint8_t led, uint8_t speed)
{
 int fh, result;
 uint8_t data[2];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x66;
 data[1] = speed;
 result = write(fh, data, 2);
 i2c_end_transaction(fh);
 if (result != 2) {
  write_error(led);
  result = -1;
 }
 return result;
}

canonical_original_code:
int blinkm_set_fade_speed(unsigned char led, unsigned char speed)
{
 int fh; int result;
 unsigned char data[2];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x66;
 data[1] = speed;
 result = write(fh, data, 2);
 i2c_end_transaction(fh);
 if (result != 2) {
  write_error(led);
  result = -1;
 }
 return result;
}

variable_types:
speed
unsigned char

variable_types:
data
unsigned char [2]


decompiled_code:
__int64 __fastcall blinkm_set_time_adjust(unsigned __int8 a1, char a2)
{
int v2; // eax
unsigned int v3; // ebp
unsigned int v4; // er13
char buf[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = i2c_start_transaction(a1);
if ( v2 < 0 )
return (unsigned int)-1;
buf[1] = a2;
v3 = v2;
buf[0] = 116;
v4 = write(v2, buf, 2uLL);
i2c_end_transaction(v3);
if ( v4 != 2 )
{
v4 = -1;
write_error(a1);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int8 a1, char a2)
{
int v2; // eax
unsigned int v3; // ebp
unsigned int v4; // er13
char buf[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = i2c_start_transaction(a1);
if ( v2 < 0 )
return (unsigned int)-1;
buf[1] = a2;
v3 = v2;
buf[0] = 116;
v4 = write(v2, buf, 2uLL);
i2c_end_transaction(v3);
if ( v4 != 2 )
{
v4 = -1;
func1(a1);
}
return v4;
}

original_code:
int blinkm_set_time_adjust(uint8_t led, int8_t adjust)
{
 int fh, result;
 uint8_t data[2];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x74;
 data[1] = adjust;
 result = write(fh, data, 2);
 i2c_end_transaction(fh);
 if (result != 2) {
  write_error(led);
  result = -1;
 }
 return result;
}

canonical_original_code:
int blinkm_set_time_adjust(unsigned char led, char adjust)
{
 int fh; int result;
 unsigned char data[2];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x74;
 data[1] = adjust;
 result = write(fh, data, 2);
 i2c_end_transaction(fh);
 if (result != 2) {
  write_error(led);
  result = -1;
 }
 return result;
}

variable_types: adjust char


decompiled_code:
__int64 __fastcall blinkm_read_script_line(unsigned __int8 a1, char a2, __int64 a3)
{
int v4; // er12
unsigned int v5; // er13
__int64 buf[6]; // [rsp+8h] [rbp-30h] BYREF

if ( !a3 )
return (unsigned int)-1;
v4 = i2c_start_transaction(a1);
if ( v4 < 0 )
return (unsigned int)-1;
LOWORD(buf[0]) = 82;
BYTE2(buf[0]) = a2;
if ( (unsigned int)write(v4, buf, 3uLL) == 3 )
{
buf[0] = 0LL;
v5 = read(v4, buf, 5uLL);
if ( v5 == 5 )
{
*(_DWORD *)a3 = buf[0];
*(_BYTE *)(a3 + 4) = BYTE4(buf[0]);
}
else
{
v5 = -1;
read_error(a1);
}
}
else
{
v5 = -1;
write_error(a1);
}
i2c_end_transaction((unsigned int)v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int8 a1, char a2, __int64 a3)
{
int v4; // er12
unsigned int v5; // er13
__int64 buf[6]; // [rsp+8h] [rbp-30h] BYREF

if ( !a3 )
return (unsigned int)-1;
v4 = i2c_start_transaction(a1);
if ( v4 < 0 )
return (unsigned int)-1;
LOWORD(buf[0]) = 82;
BYTE2(buf[0]) = a2;
if ( (unsigned int)write(v4, buf, 3uLL) == 3 )
{
buf[0] = 0LL;
v5 = read(v4, buf, 5uLL);
if ( v5 == 5 )
{
*(_DWORD *)a3 = buf[0];
*(_BYTE *)(a3 + 4) = BYTE4(buf[0]);
}
else
{
v5 = -1;
func0(a1);
}
}
else
{
v5 = -1;
func1(a1);
}
i2c_end_transaction((unsigned int)v4);
return v5;
}

original_code:
int blinkm_read_script_line(uint8_t led, uint8_t line_no, struct script_line *s)
{
 int fh, result;
 uint8_t data[8];
 if (!s)
  return -1;
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x52;
 data[1] = 0x00;
 data[2] = line_no;
 result = write(fh, data, 3);
 if (result != 3) {
  write_error(led);
  result = -1;
 } else {
  bzero(data, sizeof(data));
  result = read(fh, data, 5);
  if (result != 5) {
   read_error(led);
   result = -1;
  } else {
   s->_ticks = data[0];
   s->_cmd = data[1];
   s->_arg[0] = data[2];
   s->_arg[1] = data[3];
   s->_arg[2] = data[4];
  }
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_read_script_line(unsigned char led, unsigned char line_no, struct script_line *s)
{
 int fh; int result;
 unsigned char data[8];
 if (!s)
  return -1;
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x52;
 data[1] = 0x00;
 data[2] = line_no;
 result = write(fh, data, 3);
 if (result != 3) {
  write_error(led);
  result = -1;
 } else {
  bzero(data, sizeof(data));
  result = read(fh, data, 5);
  if (result != 5) {
   read_error(led);
   result = -1;
  } else {
   s->_ticks = data[0];
   s->_cmd = data[1];
   s->_arg[0] = data[2];
   s->_arg[1] = data[3];
   s->_arg[2] = data[4];
  }
 }
 i2c_end_transaction(fh);
 return result;
}

variable_types: struct script_line { unsigned char _ticks; unsigned char _cmd; unsigned char _arg[3]; } *


decompiled_code:
__int64 __fastcall blinkm_write_script_line(unsigned __int8 a1, unsigned __int8 a2, __int64 a3)
{
__int64 v4; // rdx
int v6; // eax
unsigned int v7; // er12
int v8; // edx
int v9; // edi
char v10; // al
unsigned int v11; // er13
__int16 buf; // [rsp+8h] [rbp-30h] BYREF
unsigned __int8 v14; // [rsp+Ah] [rbp-2Eh]
int v15; // [rsp+Bh] [rbp-2Dh]
char v16; // [rsp+Fh] [rbp-29h]

if ( !a3 )
return (unsigned int)-1;
if ( a2 > 0x31u )
{
fprintf(stderr, "Invalid script line number %d\n", a2);
return (unsigned int)-1;
}
v4 = *(unsigned __int8 *)(a3 + 1);
switch ( (char)v4 )
{
case 'C':
case 'H':
case 'c':
case 'f':
case 'h':
case 'n':
case 't':
v6 = i2c_start_transaction(a1);
v7 = v6;
if ( v6 < 0 )
return (unsigned int)-1;
v8 = *(_DWORD *)a3;
v9 = v6;
buf = 87;
v10 = *(_BYTE *)(a3 + 4);
v15 = v8;
v14 = a2;
v16 = v10;
v11 = write(v9, &buf, 8uLL);
if ( v11 != 8 )
{
v11 = -1;
write_error(a1);
}
msleep(100LL);
i2c_end_transaction(v7);
break;
default:
v11 = -1;
fprintf(stderr, "Invalid script command 0x%02X\n", v4);
break;
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned __int8 a1, unsigned __int8 a2, __int64 a3)
{
__int64 v4; // rdx
int v6; // eax
unsigned int v7; // er12
int v8; // edx
int v9; // edi
char v10; // al
unsigned int v11; // er13
__int16 buf; // [rsp+8h] [rbp-30h] BYREF
unsigned __int8 v14; // [rsp+Ah] [rbp-2Eh]
int v15; // [rsp+Bh] [rbp-2Dh]
char v16; // [rsp+Fh] [rbp-29h]

if ( !a3 )
return (unsigned int)-1;
if ( a2 > 0x31u )
{
fprintf(stderr, "Invalid script line number %d\n", a2);
return (unsigned int)-1;
}
v4 = *(unsigned __int8 *)(a3 + 1);
switch ( (char)v4 )
{
case 'C':
case 'H':
case 'c':
case 'f':
case 'h':
case 'n':
case 't':
v6 = i2c_start_transaction(a1);
v7 = v6;
if ( v6 < 0 )
return (unsigned int)-1;
v8 = *(_DWORD *)a3;
v9 = v6;
buf = 87;
v10 = *(_BYTE *)(a3 + 4);
v15 = v8;
v14 = a2;
v16 = v10;
v11 = write(v9, &buf, 8uLL);
if ( v11 != 8 )
{
v11 = -1;
func1(a1);
}
msleep(100LL);
i2c_end_transaction(v7);
break;
default:
v11 = -1;
fprintf(stderr, "Invalid script command 0x%02X\n", v4);
break;
}
return v11;
}

original_code:
int blinkm_write_script_line(uint8_t led, uint8_t line_no, struct script_line *s)
{
 int fh, result;
 uint8_t data[8];
 if (!s)
  return -1;
 if (line_no > 49) {
  fprintf(stderr, "Invalid script line number %d\n", line_no);
  return -1;
 }
 switch (s->_cmd) {
 case 0x6E:
 case 0x63:
 case 0x68:
 case 0x43:
 case 0x48:
 case 0x66:
 case 0x74:
  break;
 default:
  fprintf(stderr, "Invalid script command 0x%02X\n", s->_cmd);
  return -1;
 }
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x57;
 data[1] = 0x00;
 data[2] = line_no;
 data[3] = s->_ticks;
 data[4] = s->_cmd;
 data[5] = s->_arg[0];
 data[6] = s->_arg[1];
 data[7] = s->_arg[2];
 result = write(fh, data, 8);
 if (result != 8) {
  write_error(led);
  result = -1;
 }
 msleep(100);
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_write_script_line(unsigned char led, unsigned char line_no, struct script_line *s)
{
 int fh; int result;
 unsigned char data[8];
 if (!s)
  return -1;
 if (line_no > 49) {
  fprintf(stderr, "Invalid script line number %d\n", line_no);
  return -1;
 }
 switch (s->_cmd) {
 case 0x6E:
 case 0x63:
 case 0x68:
 case 0x43:
 case 0x48:
 case 0x66:
 case 0x74:
  break;
 default:
  fprintf(stderr, "Invalid script command 0x%02X\n", s->_cmd);
  return -1;
 }
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x57;
 data[1] = 0x00;
 data[2] = line_no;
 data[3] = s->_ticks;
 data[4] = s->_cmd;
 data[5] = s->_arg[0];
 data[6] = s->_arg[1];
 data[7] = s->_arg[2];
 result = write(fh, data, 8);
 if (result != 8) {
  write_error(led);
  result = -1;
 }
 msleep(100);
 i2c_end_transaction(fh);
 return result;
}

user_defined_types: struct script_line { unsigned char _ticks; unsigned char _cmd; unsigned char _arg[3]; }


decompiled_code:
__int64 __fastcall blinkm_set_script_length_and_repeats(unsigned __int8 a1, char a2, char a3)
{
int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = i2c_start_transaction(a1);
if ( v4 < 0 )
return (unsigned int)-1;
buf[2] = a3;
buf[1] = a2;
v5 = v4;
buf[0] = 76;
v6 = write(v4, buf, 3uLL);
if ( v6 != 3 )
{
v6 = -1;
write_error(a1);
}
msleep(50LL);
i2c_end_transaction(v5);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned __int8 a1, char a2, char a3)
{
int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er13
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

v4 = i2c_start_transaction(a1);
if ( v4 < 0 )
return (unsigned int)-1;
buf[2] = a3;
buf[1] = a2;
v5 = v4;
buf[0] = 76;
v6 = write(v4, buf, 3uLL);
if ( v6 != 3 )
{
v6 = -1;
func1(a1);
}
msleep(50LL);
i2c_end_transaction(v5);
return v6;
}

original_code:
int blinkm_set_script_length_and_repeats(uint8_t led, uint8_t length, uint8_t repeats)
{
 int fh, result;
 uint8_t data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x4C;
 data[1] = length;
 data[2] = repeats;
 result = write(fh, data, 3);
 if (result != 3) {
  write_error(led);
  result = -1;
 }
 msleep(50);
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_set_script_length_and_repeats(unsigned char led, unsigned char length, unsigned char repeats)
{
 int fh; int result;
 unsigned char data[4];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x4C;
 data[1] = length;
 data[2] = repeats;
 result = write(fh, data, 3);
 if (result != 3) {
  write_error(led);
  result = -1;
 }
 msleep(50);
 i2c_end_transaction(fh);
 return result;
}

variable_types:
length
unsigned char

variable_types:
repeats
unsigned char


decompiled_code:
__int64 __fastcall blinkm_get_firmware_version(unsigned __int8 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebp
unsigned int v4; // er12
__int16 buf[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v2 = i2c_start_transaction(a1);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
LOBYTE(buf[0]) = 90;
if ( (unsigned int)write(v2, buf, 1uLL) == 1 )
{
buf[0] = 0;
if ( (unsigned int)read(v3, buf, 2uLL) == 2 )
{
v4 = (unsigned __int16)__ROL2__(buf[0], 8);
goto LABEL_6;
}
if ( a2 )
{
v4 = -1;
read_error(a1);
goto LABEL_6;
}
}
else if ( a2 )
{
write_error(a1);
}
v4 = -1;
LABEL_6:
i2c_end_transaction(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned __int8 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebp
unsigned int v4; // er12
__int16 buf[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v2 = i2c_start_transaction(a1);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
LOBYTE(buf[0]) = 90;
if ( (unsigned int)write(v2, buf, 1uLL) == 1 )
{
buf[0] = 0;
if ( (unsigned int)read(v3, buf, 2uLL) == 2 )
{
v4 = (unsigned __int16)__ROL2__(buf[0], 8);
goto LABEL_6;
}
if ( a2 )
{
v4 = -1;
func0(a1);
goto LABEL_6;
}
}
else if ( a2 )
{
func1(a1);
}
v4 = -1;
LABEL_6:
i2c_end_transaction(v3);
return v4;
}

original_code:
int blinkm_get_firmware_version(uint8_t led, int verbose)
{
 int fh, result;
 uint8_t data[2];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x5A;
 result = write(fh, data, 1);
 if (result == 1) {
  data[0] = 0;
  data[1] = 0;
  result = read(fh, data, 2);
  if (result != 2) {
   if (verbose)
    read_error(led);
   result = -1;
  } else {
   result = data[0];
   result <<= 8;
   result |= data[1];
  }
 } else {
  if (verbose)
   write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

canonical_original_code:
int blinkm_get_firmware_version(unsigned char led, int verbose)
{
 int fh; int result;
 unsigned char data[2];
 fh = i2c_start_transaction(led);
 if (fh < 0)
  return -1;
 data[0] = 0x5A;
 result = write(fh, data, 1);
 if (result == 1) {
  data[0] = 0;
  data[1] = 0;
  result = read(fh, data, 2);
  if (result != 2) {
   if (verbose)
    read_error(led);
   result = -1;
  } else {
   result = data[0];
   result <<= 8;
   result |= data[1];
  }
 } else {
  if (verbose)
   write_error(led);
  result = -1;
 }
 i2c_end_transaction(fh);
 return result;
}

variable_types:
led
unsigned char

variable_types: verbose int


decompiled_code:
__int64 i2c_open_device()
{
int v0; // er12
int *v2; // rax
char *v3; // rax

v0 = open(i2c_bus, 2);
if ( v0 < 0 )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "Error: Could not open file %s: %s\n", i2c_bus, v3);
}
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // er12
int *v2; // rax
char *v3; // rax

v0 = open(i2c_bus, 2);
if ( v0 < 0 )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "Error: Could not open file %s: %s\n", i2c_bus, v3);
}
return (unsigned int)v0;
}

original_code:
int i2c_open_device()
{
 int fh = -1;
 fh = open(i2c_bus, 02);
 if (fh < 0) {
  fprintf(stderr, "Error: Could not open file %s: %s\n",
    i2c_bus, strerror((*__errno_location ())));
 }
 return fh;
}

variable_types: fh int


decompiled_code:
__int64 __fastcall i2c_set_slave_address(int a1, unsigned __int8 a2)
{
__int64 result; // rax
int v3; // er8
int v4; // edi
char *v5; // rax

result = 0xFFFFFFFFLL;
if ( a1 >= 0 )
{
v3 = ioctl(a1, 0x703uLL, a2);
result = 1LL;
if ( v3 < 0 )
{
v4 = *_errno_location();
if ( v4 == 16 )
{
fprintf(stderr, "Device %d is busy!\n", a2);
}
else
{
v5 = strerror(v4);
fprintf(stderr, "Could not set slave address to 0x%02x: %s\n", a2, v5);
}
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, unsigned __int8 a2)
{
__int64 result; // rax
int v3; // er8
int v4; // edi
char *v5; // rax

result = 0xFFFFFFFFLL;
if ( a1 >= 0 )
{
v3 = ioctl(a1, 0x703uLL, a2);
result = 1LL;
if ( v3 < 0 )
{
v4 = *_errno_location();
if ( v4 == 16 )
{
fprintf(stderr, "Device %d is busy!\n", a2);
}
else
{
v5 = strerror(v4);
fprintf(stderr, "Could not set slave address to 0x%02x: %s\n", a2, v5);
}
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int i2c_set_slave_address(int fh, uint8_t address)
{
 if (fh < 0)
  return -1;
 if (ioctl(fh, 0x0703, address) < 0) {
  if ((*__errno_location ()) == 16)
   fprintf(stderr, "Device %d is busy!\n", address);
  else
   fprintf(stderr, "Could not set slave address to 0x%02x: %s\n",
    address, strerror((*__errno_location ())));
  return -1;
 }
 return 1;
}

canonical_original_code:
int i2c_set_slave_address(int fh, unsigned char address)
{
 if (fh < 0)
  return -1;
 if (ioctl(fh, 0x0703, address) < 0) {
  if ((*__errno_location ()) == 16)
   fprintf(stderr, "Device %d is busy!\n", address);
  else
   fprintf(stderr, "Could not set slave address to 0x%02x: %s\n",
    address, strerror((*__errno_location ())));
  return -1;
 }
 return 1;
}

variable_types:
address
unsigned char


decompiled_code:
__int64 __fastcall i2c_start_transaction(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // er12
int v3; // eax
int v5; // edi
unsigned int v6; // er12

v1 = i2c_open_device();
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
v3 = i2c_set_slave_address(v1, a1);
if ( v3 >= 0 )
return v2;
v5 = v2;
v6 = v3;
close(v5);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
int v1; // eax
unsigned int v2; // er12
int v3; // eax
int v5; // edi
unsigned int v6; // er12

v1 = func0();
if ( v1 < 0 )
return (unsigned int)-1;
v2 = v1;
v3 = func1(v1, a1);
if ( v3 >= 0 )
return v2;
v5 = v2;
v6 = v3;
close(v5);
return v6;
}

original_code:
int i2c_start_transaction(uint8_t slave_address)
{
 int fh, result;
 fh = i2c_open_device();
 if (fh < 0)
  return -1;
 result = i2c_set_slave_address(fh, slave_address);
 if (result < 0) {
  close(fh);
  return result;
 }
 return fh;
}

canonical_original_code:
int i2c_start_transaction(unsigned char slave_address)
{
 int fh; int result;
 fh = i2c_open_device();
 if (fh < 0)
  return -1;
 result = i2c_set_slave_address(fh, slave_address);
 if (result < 0) {
  close(fh);
  return result;
 }
 return fh;
}

variable_types: result int


decompiled_code:
__int64 __fastcall i2c_end_transaction(int a1)
{
if ( a1 > 0 )
close(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
if ( a1 > 0 )
close(a1);
return 1LL;
}

original_code:
int i2c_end_transaction(int fh)
{
 if (fh > 0)
  close(fh);
 return 1;
}


msleep

decompiled_code:
int __fastcall msleep(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

if ( a1 <= 0 )
return -2;
v2.tv_sec = a1 / 1000;
v2.tv_nsec = 1000000 * (a1 % 1000);
return nanosleep(&v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

if ( a1 <= 0 )
return -2;
v2.tv_sec = a1 / 1000;
v2.tv_nsec = 1000000 * (a1 % 1000);
return nanosleep(&v2, 0LL);
}

original_code:
int msleep(int milliseconds)
{
 struct timespec ts;
 if (milliseconds < 1) {
  return -2;
 }
 ts.tv_sec = milliseconds / 1000;
 ts.tv_nsec = 1000000 * (milliseconds % 1000);
 return nanosleep(&ts, ((void *)0));
}

variable_types: milliseconds int

variable_types: struct timespec { long tv_sec; long tv_nsec; }


main

canonical_name: main

repo: scottellis/polladc

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int v5; // eax
int v6; // eax
const char **v7; // r12
const char **v8; // r13
unsigned int v9; // eax
__int64 v10; // rsi
signed int v11; // eax
unsigned int v12; // er12
int result; // eax
struct timeval v14; // [rsp+0h] [rbp-68h] BYREF
struct timeval tv; // [rsp+10h] [rbp-58h] BYREF
__int128 v16; // [rsp+20h] [rbp-48h] BYREF
__int64 v17; // [rsp+30h] [rbp-38h]

v3 = 50;
register_sig_handler(argc, argv, envp);
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "d:h");
if ( v6 == -1 )
break;
if ( v6 != 100 )
goto LABEL_21;
v5 = atoi(optarg);
v3 = v5;
if ( v5 <= 0 )
{
printf("Invalid delay %d. Must be greater then zero.\n", (unsigned int)v5);
usage(*argv);
}
}
if ( optind == argc )
{
puts("A list of ADCs is required");
LABEL_21:
usage(*argv);
}
v17 = 0LL;
v16 = 0LL;
if ( optind < argc )
{
v7 = &argv[optind];
v8 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
v9 = atoi(*v7);
v10 = v9;
v11 = v9 - 2;
if ( (unsigned int)v11 > 5 )
{
printf("adc %d is out of range\n", v10);
usage(*argv);
}
if ( *((_DWORD *)&v16 + v11) )
{
printf("adc %d listed more then once\n", v10);
usage(*argv);
}
++v7;
*((_DWORD *)&v16 + v11) = 1;
}
while ( v7 != v8 );
}
if ( gettimeofday(&v14, 0LL) < 0 )
{
perror("gettimeofday: start");
result = 1;
}
else
{
v12 = loop(v3, &v16);
if ( gettimeofday(&tv, 0LL) < 0 )
perror("gettimeofday: end");
else
show_elapsed(&v14, &tv, v12);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int v5; // eax
int v6; // eax
const char **v7; // r12
const char **v8; // r13
unsigned int v9; // eax
__int64 v10; // rsi
signed int v11; // eax
unsigned int v12; // er12
int result; // eax
struct timeval v14; // [rsp+0h] [rbp-68h] BYREF
struct timeval tv; // [rsp+10h] [rbp-58h] BYREF
__int128 v16; // [rsp+20h] [rbp-48h] BYREF
__int64 v17; // [rsp+30h] [rbp-38h]

v3 = 50;
func12(argc, argv, envp);
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "d:h");
if ( v6 == -1 )
break;
if ( v6 != 100 )
goto LABEL_21;
v5 = atoi(optarg);
v3 = v5;
if ( v5 <= 0 )
{
printf("Invalid delay %d. Must be greater then zero.\n", (unsigned int)v5);
func8(*argv);
}
}
if ( optind == argc )
{
puts("A list of ADCs is required");
LABEL_21:
func8(*argv);
}
v17 = 0LL;
v16 = 0LL;
if ( optind < argc )
{
v7 = &argv[optind];
v8 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
v9 = atoi(*v7);
v10 = v9;
v11 = v9 - 2;
if ( (unsigned int)v11 > 5 )
{
printf("adc %d is out of range\n", v10);
func8(*argv);
}
if ( *((_DWORD *)&v16 + v11) )
{
printf("adc %d listed more then once\n", v10);
func8(*argv);
}
++v7;
*((_DWORD *)&v16 + v11) = 1;
}
while ( v7 != v8 );
}
if ( gettimeofday(&v14, 0LL) < 0 )
{
perror("gettimeofday: start");
result = 1;
}
else
{
v12 = func14(v3, &v16);
if ( gettimeofday(&tv, 0LL) < 0 )
perror("gettimeofday: end");
else
func9(&v14, &tv, v12);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
    int opt, delay_ms, adc, i;
    struct timeval start, end;
    int adc_list[6];
    register_sig_handler();
    delay_ms = 50;
    while ((opt = getopt(argc, argv, "d:h")) != -1) {
        switch (opt) {
        case 'd':
            delay_ms = atoi(optarg);
            if (delay_ms < 1) {
                printf("Invalid delay %d. Must be greater then zero.\n", delay_ms);
                usage(argv[0]);
            }
   break;
        case 'h':
        default:
            usage(argv[0]);
            break;
        }
    }
    if (optind == argc) {
        printf("A list of ADCs is required\n");
        usage(argv[0]);
    }
    memset(adc_list, 0, sizeof(adc_list));
    for (i = optind; i < argc; i++) {
        adc = atoi(argv[i]);
        if (adc < 2 || adc > 7) {
            printf("adc %d is out of range\n", adc);
            usage(argv[0]);
        }
        adc -= 2;
        if (adc_list[adc]) {
            printf("adc %d listed more then once\n", adc + 2);
            usage(argv[0]);
        }
        adc_list[adc] = 1;
    }
    if (gettimeofday(&start, ((void *)0)) < 0) {
        perror("gettimeofday: start");
        return 1;
    }
    int count = loop(delay_ms, adc_list);
    if (gettimeofday(&end, ((void *)0)) < 0)
        perror("gettimeofday: end");
    else
        show_elapsed(&start, &end, count);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int opt; int delay_ms; int adc; int i;
    struct timeval start; struct timeval end;
    int adc_list[6];
    register_sig_handler();
    delay_ms = 50;
    while ((opt = getopt(argc, argv, "d:h")) != -1) {
        switch (opt) {
        case 'd':
            delay_ms = atoi(optarg);
            if (delay_ms < 1) {
                printf("Invalid delay %d. Must be greater then zero.\n", delay_ms);
                usage(argv[0]);
            }
   break;
        case 'h':
        default:
            usage(argv[0]);
            break;
        }
    }
    if (optind == argc) {
        printf("A list of ADCs is required\n");
        usage(argv[0]);
    }
    memset(adc_list, 0, sizeof(adc_list));
    for (i = optind; i < argc; i++) {
        adc = atoi(argv[i]);
        if (adc < 2 || adc > 7) {
            printf("adc %d is out of range\n", adc);
            usage(argv[0]);
        }
        adc -= 2;
        if (adc_list[adc]) {
            printf("adc %d listed more then once\n", adc + 2);
            usage(argv[0]);
        }
        adc_list[adc] = 1;
    }
    if (gettimeofday(&start, ((void *)0)) < 0) {
        perror("gettimeofday: start");
        return 1;
    }
    int count = loop(delay_ms, adc_list);
    if (gettimeofday(&end, ((void *)0)) < 0)
        perror("gettimeofday: end");
    else
        show_elapsed(&start, &end, count);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types: adc int

variable_types: i int

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

variable_types: count int


decompiled_code:
void sigint_handler()
{
abort_read = 1;
}

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

original_code:
void sigint_handler(int sig)
{
    abort_read = 1;
}

variable_types: sig int


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
printf("\nUsage: %s <options> [adc-list]\n", a1);
puts(" -d<delay-ms> Delay between reads, default 50, min 1");
puts(" adc-list Space separated list of ADCs to monitor, 2-7");
printf("\nExample:\n\t%s -d100 2 3 5\n", a1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
printf("\nUsage: %s <options> [adc-list]\n", a1);
puts(" -d<delay-ms> Delay between reads, default 50, min 1");
puts(" adc-list Space separated list of ADCs to monitor, 2-7");
printf("\nExample:\n\t%s -d100 2 3 5\n", a1);
exit(0);
}

original_code:
void usage(char *argv_0)
{
    printf("\nUsage: %s <options> [adc-list]\n", argv_0);
    printf("  -d<delay-ms>       Delay between reads, default 50, min 1\n");
    printf("  adc-list           Space separated list of ADCs to monitor, 2-7\n");
    printf("\nExample:\n\t%s -d100 2 3 5\n", argv_0);
    exit(0);
}


decompiled_code:
int __fastcall show_elapsed(__int64 a1, __int64 *a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rcx
__int64 v6; // rdx
int v7; // eax
double v8; // xmm0_8
double v9; // xmm1_8

v3 = a2[1];
v4 = *(_QWORD *)(a1 + 8);
v6 = *a2;
if ( v3 <= v4 )
{
--v6;
v7 = v3 + 1000000 - v4;
*a2 = v6;
}
else
{
v7 = v3 - v4;
}
v8 = (double)((int)v6 - *(_DWORD *)a1) + (double)v7 / 1000000.0;
v9 = 0.0;
if ( v8 > 0.0 )
v9 = (double)a3 / v8;
return printf("Summary\n Elapsed: %0.2lf seconds\n Reads: %d\n Rate: %0.2lf Hz\n\n", v8, (unsigned int)a3, v9);
}

original_code:
void show_elapsed(struct timeval *start, struct timeval *end, int count)
{
    double diff;
    double rate;
    if (end->tv_usec > start->tv_usec) {
        diff = (double) (end->tv_usec - start->tv_usec);
    }
    else {
        diff = (double) ((1000000 + end->tv_usec) - start->tv_usec);
        end->tv_sec--;
    }
    diff /= 1000000.0;
    diff += (double)(end->tv_sec - start->tv_sec);
    if (diff > 0.0)
        rate = count / diff;
    else
        rate = 0.0;
    printf("Summary\n  Elapsed: %0.2lf seconds\n    Reads: %d\n     Rate: %0.2lf Hz\n\n",
        diff, count, rate);
}

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

variable_types:
end
struct timeval *

variable_types: diff double

variable_types: rate double


decompiled_code:
int __fastcall read_adc(int a1)
{
__int64 buf[2]; // [rsp+8h] [rbp-10h] BYREF

buf[0] = 0LL;
if ( read(a1, buf, 8uLL) >= 0 )
return atoi((const char *)buf);
perror("read()");
return -100000;
}

canonical_decompiled_code:
int __fastcall func10(int a1)
{
__int64 buf[2]; // [rsp+8h] [rbp-10h] BYREF

buf[0] = 0LL;
if ( read(a1, buf, 8uLL) >= 0 )
return atoi((const char *)buf);
perror("read()");
return -100000;
}

original_code:
int read_adc(int fd)
{
    char buff[8];
    int val = -100000;
    memset(buff, 0, sizeof(buff));
    if (read(fd, buff, 8) < 0)
        perror("read()");
    else
        val = atoi(buff);
    return val;
}

variable_types: fd int

variable_types:
buff
char [8]

variable_types: val int


decompiled_code:
__int64 __fastcall open_adc(unsigned int a1)
{
int v1; // er12
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

sprintf(v3, "%sin%d_input", hwmonsyspath, a1);
v1 = open(v3, 0);
if ( v1 < 0 )
{
perror("open()");
puts(v3);
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1)
{
int v1; // er12
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

sprintf(v3, "%sin%d_input", hwmonsyspath, a1);
v1 = open(v3, 0);
if ( v1 < 0 )
{
perror("open()");
puts(v3);
}
return (unsigned int)v1;
}

original_code:
int open_adc(int adc)
{
    char path[128];
    sprintf(path, "%sin%d_input", hwmonsyspath, adc);
    int fd = open(path, 00);
    if (fd < 0) {
        perror("open()");
        printf("%s\n", path);
    }
    return fd;
}

variable_types:
path
char [128]


decompiled_code:
int register_sig_handler()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigint_handler;
memset(&v1.sa_mask, 0, 0x90uLL);
result = sigaction(2, &v1, 0LL);
if ( result < 0 )
{
perror("sigaction(SIGINT)");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func12()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigint_handler;
memset(&v1.sa_mask, 0, 0x90uLL);
result = sigaction(2, &v1, 0LL);
if ( result < 0 )
{
perror("sigaction(SIGINT)");
exit(1);
}
return result;
}

original_code:
void register_sig_handler()
{
    struct sigaction sia;
    bzero(&sia, sizeof sia);
    sia.__sigaction_handler.sa_handler = sigint_handler;
    if (sigaction(2, &sia, ((void *)0)) < 0) {
        perror("sigaction(SIGINT)");
        exit(1);
    }
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


msleep

decompiled_code:
int __fastcall msleep(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = a1 / 1000;
v2.tv_nsec = 1000000 * (a1 % 1000);
return nanosleep(&v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func13(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = a1 / 1000;
v2.tv_nsec = 1000000 * (a1 % 1000);
return nanosleep(&v2, 0LL);
}

original_code:
void msleep(int ms)
{
    struct timespec ts;
    ts.tv_sec = ms / 1000;
    ts.tv_nsec = (ms % 1000) * 1000000;
    nanosleep(&ts, ((void *)0));
}

variable_types: ms int

variable_types: struct timespec { long tv_sec; long tv_nsec; }


loop

decompiled_code:
__int64 __fastcall loop(int a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rdx
unsigned int v4; // er13
__int64 i; // rbx
int v6; // eax
int v7; // ebp
int v8; // er15
__int64 v10; // rbx
__int64 v11; // rdx
__int128 v12; // [rsp+0h] [rbp-58h]
__int64 v13; // [rsp+10h] [rbp-48h]

v2 = 0LL;
v12 = 0LL;
v13 = 0LL;
fwrite("\n(use ctrl-c to stop)\n\n", 1uLL, 0x17uLL, _bss_start);
fwrite("ADC ", 1uLL, 0xDuLL, _bss_start);
do
{
while ( !*(_DWORD *)(a2 + 4 * v2) )
{
if ( ++v2 == 6 )
goto LABEL_5;
}
v3 = (unsigned int)(v2++ + 2);
fprintf(_bss_start, " %d", v3);
}
while ( v2 != 6 );
LABEL_5:
fputc(10, _bss_start);
v4 = abort_read;
if ( !abort_read )
{
while ( 1 )
{
for ( i = 0LL; i != 6; ++i )
{
if ( *(_DWORD *)(a2 + 4 * i) )
{
v6 = open_adc((int)i + 2);
v7 = v6;
if ( v6 < 0 )
break;
v8 = read_adc(v6);
*((_DWORD *)&v12 + i) = v8;
close(v7);
if ( v8 == -100000 )
break;
}
}
if ( a1 > 9 )
{
if ( a1 > 49 || (v4 & 7) == 0 )
{
LABEL_17:
v10 = 0LL;
fprintf(_bss_start, "\rRead %8d: ", v4);
do
{
while ( !*(_DWORD *)(a2 + v10) )
{
v10 += 4LL;
if ( v10 == 24 )
goto LABEL_21;
}
v11 = *(unsigned int *)((char *)&v12 + v10);
v10 += 4LL;
fprintf(_bss_start, " %4d ", v11);
}
while ( v10 != 24 );
LABEL_21:
fflush(_bss_start);
goto LABEL_13;
}
}
else if ( (v4 & 0x1F) == 0 )
{
goto LABEL_17;
}
LABEL_13:
++v4;
msleep(a1);
if ( abort_read )
goto LABEL_14;
}
}
v4 = 0;
LABEL_14:
fwrite("\n\n", 1uLL, 2uLL, _bss_start);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rdx
unsigned int v4; // er13
__int64 i; // rbx
int v6; // eax
int v7; // ebp
int v8; // er15
__int64 v10; // rbx
__int64 v11; // rdx
__int128 v12; // [rsp+0h] [rbp-58h]
__int64 v13; // [rsp+10h] [rbp-48h]

v2 = 0LL;
v12 = 0LL;
v13 = 0LL;
fwrite("\n(use ctrl-c to stop)\n\n", 1uLL, 0x17uLL, _bss_start);
fwrite("ADC ", 1uLL, 0xDuLL, _bss_start);
do
{
while ( !*(_DWORD *)(a2 + 4 * v2) )
{
if ( ++v2 == 6 )
goto LABEL_5;
}
v3 = (unsigned int)(v2++ + 2);
fprintf(_bss_start, " %d", v3);
}
while ( v2 != 6 );
LABEL_5:
fputc(10, _bss_start);
v4 = abort_read;
if ( !abort_read )
{
while ( 1 )
{
for ( i = 0LL; i != 6; ++i )
{
if ( *(_DWORD *)(a2 + 4 * i) )
{
v6 = func11((int)i + 2);
v7 = v6;
if ( v6 < 0 )
break;
v8 = func10(v6);
*((_DWORD *)&v12 + i) = v8;
close(v7);
if ( v8 == -100000 )
break;
}
}
if ( a1 > 9 )
{
if ( a1 > 49 || (v4 & 7) == 0 )
{
LABEL_17:
v10 = 0LL;
fprintf(_bss_start, "\rRead %8d: ", v4);
do
{
while ( !*(_DWORD *)(a2 + v10) )
{
v10 += 4LL;
if ( v10 == 24 )
goto LABEL_21;
}
v11 = *(unsigned int *)((char *)&v12 + v10);
v10 += 4LL;
fprintf(_bss_start, " %4d ", v11);
}
while ( v10 != 24 );
LABEL_21:
fflush(_bss_start);
goto LABEL_13;
}
}
else if ( (v4 & 0x1F) == 0 )
{
goto LABEL_17;
}
LABEL_13:
++v4;
func13(a1);
if ( abort_read )
goto LABEL_14;
}
}
v4 = 0;
LABEL_14:
fwrite("\n\n", 1uLL, 2uLL, _bss_start);
return v4;
}

original_code:
int loop(int delay_ms, int *list)
{
    int fd, count, i, update;
    int val[6];
    count = 0;
    memset(val, 0, sizeof(val));
    fprintf(stdout, "\n(use ctrl-c to stop)\n\n");
    fprintf(stdout, "ADC          ");
    for (i = 0; i < 6; i++) {
        if (list[i])
            fprintf(stdout, "      %d", i + 2);
    }
    fprintf(stdout, "\n");
    while (!abort_read) {
        for (i = 0; i < 6; i++) {
            if (!list[i])
                continue;
            fd = open_adc(i + 2);
            if (fd < 0)
                break;;
            val[i] = read_adc(fd);
            close(fd);
            if (val[i] == -100000)
                break;
        }
        update = 0;
        if (delay_ms < 10) {
            if ((count & 0x1f) == 0)
                update = 1;
        }
        else if (delay_ms < 50) {
            if ((count & 0x07) == 0)
                update = 1;
        }
        else {
            update = 1;
        }
        if (update) {
            fprintf(stdout, "\rRead %8d: ", count);
            for (i = 0; i < 6; i++) {
                if (list[i])
                    fprintf(stdout, " %4d  ", val[i]);
            }
            fflush(stdout);
        }
        count++;
        msleep(delay_ms);
    }
    fprintf(stdout, "\n\n");
    return count;
}

canonical_original_code:
int loop(int delay_ms, int *list)
{
    int fd; int count; int i; int update;
    int val[6];
    count = 0;
    memset(val, 0, sizeof(val));
    fprintf(stdout, "\n(use ctrl-c to stop)\n\n");
    fprintf(stdout, "ADC          ");
    for (i = 0; i < 6; i++) {
        if (list[i])
            fprintf(stdout, "      %d", i + 2);
    }
    fprintf(stdout, "\n");
    while (!abort_read) {
        for (i = 0; i < 6; i++) {
            if (!list[i])
                continue;
            fd = open_adc(i + 2);
            if (fd < 0)
                break;;
            val[i] = read_adc(fd);
            close(fd);
            if (val[i] == -100000)
                break;
        }
        update = 0;
        if (delay_ms < 10) {
            if ((count & 0x1f) == 0)
                update = 1;
        }
        else if (delay_ms < 50) {
            if ((count & 0x07) == 0)
                update = 1;
        }
        else {
            update = 1;
        }
        if (update) {
            fprintf(stdout, "\rRead %8d: ", count);
            for (i = 0; i < 6; i++) {
                if (list[i])
                    fprintf(stdout, " %4d  ", val[i]);
            }
            fflush(stdout);
        }
        count++;
        msleep(delay_ms);
    }
    fprintf(stdout, "\n\n");
    return count;
}

variable_types:
list
int *

variable_types: update int

variable_types:
val
int [6]


repo: scottgw/quicksilver

decompiled_code:
int __fastcall join_executor(__int64 a1)
{
return pthread_join(*(_QWORD *)(a1 + 24), 0LL);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
return pthread_join(*(_QWORD *)(a1 + 24), 0LL);
}

original_code:
static
void
join_executor(void* elem, void* user)
{
  executor_t exec = (executor_t)elem;
  pthread_join(exec->thread, ((void *)0));
}

canonical_original_code:
static void join_executor(void *elem, void *user)
{
  struct executor *exec = (executor_t)elem;
  pthread_join(exec->thread, ((void *)0));
}

variable_types:
elem
void *

variable_types:
user
void *

variable_types: struct executor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct list { void **data; int start; int end; int size; } *work; struct processor { struct task *task; struct executor *executor; int id; } *current_proc; unsigned long thread; } *

user_defined_types: struct executor { struct task *task; struct list *work; struct processor *current_proc; unsigned long thread; }

user_defined_types: struct task { void *base; struct ucontext_t ctx; enum task_state state; struct task *next; }

user_defined_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t uc_stack; struct mcontext_t uc_mcontext; struct __sigset_t uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

user_defined_types: struct mcontext_t { long long gregs[23]; struct _libc_fpstate *fpregs; unsigned long long __reserved1[8]; }

user_defined_types: struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg _st[8]; struct _libc_xmmreg _xmm[16]; unsigned int __glibc_reserved1[24]; }


decompiled_code:
void *__fastcall executor_run(void *a1)
{
void *result; // rax
__int64 v2; // rdi
ucontext_t ucp; // [rsp+20h] [rbp-3D8h] BYREF

getcontext(&ucp);
v2 = *(_QWORD *)a1;
*(_QWORD *)(v2 + 16) = &ucp;
task_set_func(v2, executor_loop, a1);
task_run(*(_QWORD *)a1);
puts("end exec loop");
result = 0LL;
notifier_done = 1;
return result;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
void *result; // rax
__int64 v2; // rdi
ucontext_t ucp; // [rsp+20h] [rbp-3D8h] BYREF

getcontext(&ucp);
v2 = *(_QWORD *)a1;
*(_QWORD *)(v2 + 16) = &ucp;
task_set_func(v2, executor_loop, a1);
task_run(*(_QWORD *)a1);
puts("end exec loop");
result = 0LL;
notifier_done = 1;
return result;
}

original_code:
static
void*
executor_run(void* data)
{
  executor_t exec = data;
  ucontext_t loop_ctx;
  int volatile flag = 0;
  getcontext(&loop_ctx);
  if (flag == 0)
    {
      flag = 1;
      exec->task->ctx.uc_link = &loop_ctx;
      task_set_func(exec->task, executor_loop, exec);
      task_run(exec->task);
    }
  printf("end exec loop\n");
  notifier_done = 1;
  return ((void *)0);
}

canonical_original_code:
static void *executor_run(void *data)
{
  struct executor *exec = data;
  struct ucontext_t loop_ctx;
  int flag = 0;
  getcontext(&loop_ctx);
  if (flag == 0)
    {
      flag = 1;
      exec->task->ctx.uc_link = &loop_ctx;
      task_set_func(exec->task, executor_loop, exec);
      task_run(exec->task);
    }
  printf("end exec loop\n");
  notifier_done = 1;
  return ((void *)0);
}

variable_types:
data
void *

variable_types: struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; }

variable_types: flag int

user_defined_types: struct list { void **data; int start; int end; int size; }

user_defined_types: struct processor { struct task *task; struct executor *executor; int id; }

user_defined_types: enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 }


decompiled_code:
__int64 __fastcall switch_to_next_processor(__int64 *a1)
{
_QWORD *v2; // rax
__int64 v3; // rdi
__int64 v4; // rsi
_QWORD *v5; // rbp
__int64 result; // rax

if ( (int)list_size(a1[1]) <= 0 )
_assert_fail("list_size(exec->work) > 0", "executor.c", 0x15u, "switch_to_next_processor");
v2 = (_QWORD *)list_take(a1[1]);
v3 = *a1;
v4 = *v2;
v2[1] = a1;
v5 = v2;
a1[2] = (__int64)v2;
*(_QWORD *)(v4 + 984) = v3;
yield_to();
result = *(unsigned int *)(*v5 + 976LL);
if ( (_DWORD)result != 3 )
result = list_add(a1[1], v5);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1)
{
_QWORD *v2; // rax
__int64 v3; // rdi
__int64 v4; // rsi
_QWORD *v5; // rbp
__int64 result; // rax

if ( (int)list_size(a1[1]) <= 0 )
_assert_fail("list_size(exec->work) > 0", "executor.c", 0x15u, "switch_to_next_processor");
v2 = (_QWORD *)list_take(a1[1]);
v3 = *a1;
v4 = *v2;
v2[1] = a1;
v5 = v2;
a1[2] = (__int64)v2;
*(_QWORD *)(v4 + 984) = v3;
yield_to();
result = *(unsigned int *)(*v5 + 976LL);
if ( (_DWORD)result != 3 )
result = list_add(a1[1], v5);
return result;
}

original_code:
static
void
switch_to_next_processor(executor_t exec)
{
  ((void) sizeof ((list_size(exec->work) > 0) ? 1 : 0), __extension__ ({ if (list_size(exec->work) > 0) ; else __assert_fail ("list_size(exec->work) > 0", "executor.c", 21, __extension__ __PRETTY_FUNCTION__); }));
  processor_t proc = list_take(exec->work);
  proc->executor = exec;
  exec->current_proc = proc;
  proc->task->next = exec->task;
  yield_to(exec->task, proc->task);
  if(proc->task->state != TASK_FINISHED)
    list_add(exec->work, proc);
}

canonical_original_code:
static void switch_to_next_processor(struct executor *exec)
{
  ((void) sizeof ((list_size(exec->work) > 0) ? 1 : 0), __extension__ ({ if (list_size(exec->work) > 0) ; else __assert_fail ("list_size(exec->work) > 0", "executor.c", 21, __extension__ __PRETTY_FUNCTION__); }));
  struct processor *proc = list_take(exec->work);
  proc->executor = exec;
  exec->current_proc = proc;
  proc->task->next = exec->task;
  yield_to(exec->task, proc->task);
  if(proc->task->state != TASK_FINISHED)
    list_add(exec->work, proc);
}

variable_types: struct processor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct executor { struct task *task; struct list { void **data; int start; int end; int size; } *work; struct processor *current_proc; unsigned long thread; } *executor; int id; } *


decompiled_code:
__int64 __fastcall executor_loop(__int64 *a1)
{
__int64 result; // rax
unsigned int v2; // [rsp+Ch] [rbp-Ch]

v2 = 0;
puts("Executor running");
while ( 1 )
{
result = v2;
if ( v2 )
break;
while ( (int)list_size(a1[1]) > 0 )
switch_to_next_processor(a1);
puts("no workers");
v2 = 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1)
{
__int64 result; // rax
unsigned int v2; // [rsp+Ch] [rbp-Ch]

v2 = 0;
puts("Executor running");
while ( 1 )
{
result = v2;
if ( v2 )
break;
while ( (int)list_size(a1[1]) > 0 )
func2(a1);
puts("no workers");
v2 = 1;
}
return result;
}

original_code:
static
void
executor_loop(void* data)
{
  executor_t exec = (executor_t) data;
  int volatile done = 0;
  printf("Executor running\n");
  while(done == 0)
    {
      if (list_size(exec->work) > 0)
        {
          switch_to_next_processor(exec);
        }
      else
        {
          printf("no workers\n");
          done = 1;
        }
    }
}

canonical_original_code:
static void executor_loop(void *data)
{
  struct executor *exec = (executor_t) data;
  int done = 0;
  printf("Executor running\n");
  while(done == 0)
    {
      if (list_size(exec->work) > 0)
        {
          switch_to_next_processor(exec);
        }
      else
        {
          printf("no workers\n");
          done = 1;
        }
    }
}

variable_types: done int

user_defined_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; }

user_defined_types: struct _libc_xmmreg { unsigned int element[4]; }


decompiled_code:
_QWORD *make_executor()
{
_QWORD *v0; // r12
__int64 v1; // rax

v0 = malloc(8uLL);
v1 = task_make();
v0[2] = 0LL;
*v0 = v1;
return v0;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *v0; // r12
__int64 v1; // rax

v0 = malloc(8uLL);
v1 = task_make();
v0[2] = 0LL;
*v0 = v1;
return v0;
}

original_code:
executor_t
make_executor()
{
  executor_t exec = malloc(sizeof(executor_t));
  exec->task = task_make();
  exec->current_proc = ((void *)0);
  return exec;
}

canonical_original_code:
struct executor *make_executor()
{
  struct executor *exec = malloc(sizeof(executor_t));
  exec->task = task_make();
  exec->current_proc = ((void *)0);
  return exec;
}

return_type: struct executor { struct task { void *base; struct ucontext_t { unsigned long uc_flags; struct ucontext_t *uc_link; struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct mcontext_t { long long gregs[23]; struct _libc_fpstate { unsigned short cwd; unsigned short swd; unsigned short ftw; unsigned short fop; unsigned long rip; unsigned long rdp; unsigned int mxcsr; unsigned int mxcr_mask; struct _libc_fpxreg { unsigned short significand[4]; unsigned short exponent; unsigned short __glibc_reserved1[3]; } _st[8]; struct _libc_xmmreg { unsigned int element[4]; } _xmm[16]; unsigned int __glibc_reserved1[24]; } *fpregs; unsigned long long __reserved1[8]; } uc_mcontext; struct __sigset_t { unsigned long __val[-1]; } uc_sigmask; struct _libc_fpstate __fpregs_mem; unsigned long long __ssp[4]; } ctx; enum task_state { TASK_UNINIT=0, TASK_RUNNING=1, TASK_RUNNABLE=2, TASK_FINISHED=3 } state; struct task *next; } *task; struct list { void **data; int start; int end; int size; } *work; struct processor { struct task *task; struct executor *executor; int id; } *current_proc; unsigned long thread; } *


decompiled_code:
__int64 join_executors()
{
return list_foreach(executors, join_executor, 0LL);
}

canonical_decompiled_code:
__int64 func5()
{
return list_foreach(executors, join_executor, 0LL);
}

original_code:
void
join_executors()
{
  list_foreach(executors, join_executor, ((void *)0));
}


decompiled_code:
int __fastcall create_executors(pthread_t a1, int a2)
{
__int64 v3; // rax
int v4; // ebx
pthread_t *v5; // rax
__int64 v6; // rdi
pthread_t *v7; // r12

v3 = list_make();
executors = v3;
if ( a2 > 0 )
{
v4 = 0;
do
{
++v4;
v5 = make_executor();
v6 = executors;
v5[1] = a1;
v7 = v5;
list_add(v6, v5);
LODWORD(v3) = pthread_create(v7 + 3, 0LL, executor_run, v7);
}
while ( a2 != v4 );
}
return v3;
}

canonical_decompiled_code:
int __fastcall func6(pthread_t a1, int a2)
{
__int64 v3; // rax
int v4; // ebx
pthread_t *v5; // rax
__int64 v6; // rdi
pthread_t *v7; // r12

v3 = list_make();
executors = v3;
if ( a2 > 0 )
{
v4 = 0;
do
{
++v4;
v5 = func4();
v6 = executors;
v5[1] = a1;
v7 = v5;
list_add(v6, v5);
LODWORD(v3) = pthread_create(v7 + 3, 0LL, executor_run, v7);
}
while ( a2 != v4 );
}
return v3;
}

original_code:
void
create_executors(list_t work, int n)
{
  executors = list_make();
  for(int i = 0; i < n; i++)
    {
      executor_t exec = make_executor();
      exec->work = work;
      list_add(executors, exec);
      pthread_create(&exec->thread, ((void *)0), executor_run, exec);
    }
}

canonical_original_code:
void create_executors(struct list *work, int n)
{
  executors = list_make();
  for(int i = 0; i < n; i++)
    {
      struct executor *exec = make_executor();
      exec->work = work;
      list_add(executors, exec);
      pthread_create(&exec->thread, ((void *)0), executor_run, exec);
    }
}

variable_types: struct list { void **data; int start; int end; int size; } *

variable_types: n int

variable_types: i int


decompiled_code:
_QWORD *list_make()
{
_QWORD *v0; // r12
void *v1; // rax
_QWORD *result; // rax

v0 = malloc(0x18uLL);
v1 = malloc(0x2000uLL);
v0[1] = 0LL;
*v0 = v1;
result = v0;
*((_DWORD *)v0 + 4) = 0;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // r12
void *v1; // rax
_QWORD *result; // rax

v0 = malloc(0x18uLL);
v1 = malloc(0x2000uLL);
v0[1] = 0LL;
*v0 = v1;
result = v0;
*((_DWORD *)v0 + 4) = 0;
return result;
}

original_code:
list_t
list_make()
{
  list_t list = (list_t)malloc(sizeof(struct list));
  list->data = malloc(1024*sizeof(void*));
  list->start = 0;
  list->end = 0;
  list->size = 0;
  return list;
}

canonical_original_code:
struct list *list_make()
{
  struct list *list = (list_t)malloc(sizeof(struct list));
  list->data = malloc(1024*sizeof(void *));
  list->start = 0;
  list->end = 0;
  list->size = 0;
  return list;
}

return_type: struct list { void **data; int start; int end; int size; } *


original_code:
void*
list_take(list_t list)
{
  void* data;
  ((void) sizeof ((list->size != 0) ? 1 : 0), __extension__ ({ if (list->size != 0) ; else __assert_fail ("list->size != 0", "list.c", 23, __extension__ __PRETTY_FUNCTION__); }));
  data = list->data[list->start];
  list->start = (list->start + 1) % 1024;
  list->size--;
  return data;
}

canonical_original_code:
void *list_take(struct list *list)
{
  void *data;
  ((void) sizeof ((list->size != 0) ? 1 : 0), __extension__ ({ if (list->size != 0) ; else __assert_fail ("list->size != 0", "list.c", 23, __extension__ __PRETTY_FUNCTION__); }));
  data = list->data[list->start];
  list->start = (list->start + 1) % 1024;
  list->size--;
  return data;
}


decompiled_code:
__int64 __fastcall list_foreach(__int64 a1, __int64 (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 result; // rax
int v5; // edx
__int64 v7; // rbx
unsigned __int64 v8; // r13
__int64 v9; // rdi

result = *(unsigned int *)(a1 + 12);
v5 = *(_DWORD *)(a1 + 8);
if ( (int)result > v5 )
{
v7 = 8LL * v5;
v8 = 8 * (v5 + (unsigned __int64)(unsigned int)(result - 1 - v5) + 1);
do
{
v9 = *(_QWORD *)(*(_QWORD *)a1 + v7);
v7 += 8LL;
result = a2(v9, a3);
}
while ( v7 != v8 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 result; // rax
int v5; // edx
__int64 v7; // rbx
unsigned __int64 v8; // r13
__int64 v9; // rdi

result = *(unsigned int *)(a1 + 12);
v5 = *(_DWORD *)(a1 + 8);
if ( (int)result > v5 )
{
v7 = 8LL * v5;
v8 = 8 * (v5 + (unsigned __int64)(unsigned int)(result - 1 - v5) + 1);
do
{
v9 = *(_QWORD *)(*(_QWORD *)a1 + v7);
v7 += 8LL;
result = a2(v9, a3);
}
while ( v7 != v8 );
}
return result;
}

original_code:
void
list_foreach(list_t list, void (*func)(void*, void*), void* user_data)
{
  int end = list->end;
  for(int i = list->start; i < end; i++)
    {
      func(list->data[i], user_data);
    }
}

canonical_original_code:
void list_foreach(struct list *list, void (*func)(void *, void *), void *user_data)
{
  int end = list->end;
  for(int i = list->start; i < end; i++)
    {
      func(list->data[i], user_data);
    }
}

variable_types:
func
void (*)(void *, void *)

variable_types: end int


decompiled_code:
__int64 __fastcall list_add(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 12);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v2) = a2;
++*(_DWORD *)(a1 + 16);
result = (unsigned int)((v2 + 1) % 1024);
*(_DWORD *)(a1 + 12) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 12);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v2) = a2;
++*(_DWORD *)(a1 + 16);
result = (unsigned int)((v2 + 1) % 1024);
*(_DWORD *)(a1 + 12) = result;
return result;
}

original_code:
void
list_add(list_t list, void* data)
{
  *(list->data + list->end) = data;
  list->size++;
  list->end = (list->end + 1) % 1024;
}

canonical_original_code:
void list_add(struct list *list, void *data)
{
  *(list->data + list->end) = data;
  list->size++;
  list->end = (list->end + 1) % 1024;
}


decompiled_code:
__int64 __fastcall list_size(__int64 a1)
{
if ( !a1 )
_assert_fail("list != NULL", "list.c", 0x33u, "list_size");
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
if ( !a1 )
_assert_fail("list != NULL", "list.c", 0x33u, "list_size");
return *(unsigned int *)(a1 + 16);
}

original_code:
int
list_size(list_t list)
{
  ((void) sizeof ((list != ((void *)0)) ? 1 : 0), __extension__ ({ if (list != ((void *)0)) ; else __assert_fail ("list != NULL", "list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
  return list->size;
}

canonical_original_code:
int list_size(struct list *list)
{
  ((void) sizeof ((list != ((void *)0)) ? 1 : 0), __extension__ ({ if (list != ((void *)0)) ; else __assert_fail ("list != NULL", "list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
  return list->size;
}


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

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

original_code:
void
list_free(list_t list)
{
  free(list->data);
  free(list);
}

canonical_original_code:
void list_free(struct list *list)
{
  free(list->data);
  free(list);
}