decompiled_code:
__int64 __fastcall conf_set(char *s1, char *a2, char *s, size_t maxlen)
{
int v8; // eax
unsigned int v9; // er12
void *v10; // rdi
char *v12; // rax
int v13; // eax
size_t v14; // r12
void *v15; // rsp
const char *v16; // rax
unsigned __int64 v17; // r14
size_t v18; // r13
void *v19; // rsp
size_t v20; // r13
void *v21; // rsp
const char *v22; // rax
int v23; // eax
int v24; // er13
void *v25; // rsp
const char *v26; // rax
size_t v27; // r12
void *v28; // rsp
const char *v29; // rax
unsigned int v30; // er13
unsigned int v31; // er12
size_t v32; // r14
void *v33; // rsp
char *v34; // rax
char *v35; // rax
int v36; // eax
char *v37; // rcx
const char *v38; // rdx
size_t v39; // r12
void *v40; // rsp
const char *v41; // rax
size_t v42; // rbx
void *v43; // rsp
size_t v44; // rbx
void *v45; // rsp
const char *v46; // rax
unsigned int v47; // eax
size_t v48; // rbx
void *v49; // rsp
size_t v50; // rbx
void *v51; // rsp
const char *v52; // rax
unsigned int v53; // eax
bool v54; // zf
void *v55; // rdi
void *v56; // rdi
const char *v57; // rdi
_BYTE v58[8]; // [rsp+0h] [rbp-E0h] BYREF
size_t v59; // [rsp+8h] [rbp-D8h]
unsigned int v60; // [rsp+18h] [rbp-C8h] BYREF
unsigned int v61; // [rsp+1Ch] [rbp-C4h] BYREF
void *v62; // [rsp+20h] [rbp-C0h] BYREF
size_t v63; // [rsp+28h] [rbp-B8h]
void *src; // [rsp+30h] [rbp-B0h]
size_t n; // [rsp+38h] [rbp-A8h]
void *v66; // [rsp+40h] [rbp-A0h]
size_t v67; // [rsp+48h] [rbp-98h]
void *v68; // [rsp+50h] [rbp-90h]
size_t v69; // [rsp+58h] [rbp-88h]
void *v70; // [rsp+70h] [rbp-70h]
size_t v71; // [rsp+78h] [rbp-68h]
void *v72; // [rsp+90h] [rbp-50h]
size_t v73; // [rsp+98h] [rbp-48h]

if ( !memcmp(s1, "kafka.", 6uLL) )
{
if ( !memcmp(s1 + 6, "topic.", 6uLL) )
{
v8 = rd_kafka_topic_conf_set(*(&conf + 22), s1 + 12, a2, s, maxlen);
if ( !v8 )
return 0;
}
else
{
v8 = rd_kafka_conf_set(*(&conf + 21), s1 + 6, a2, s, maxlen);
if ( !v8 )
return 0;
}
if ( v8 != -2 )
return (unsigned int)-1;
}
if ( !a2 )
{
if ( (unsigned int)ezd_regmatch("^input +(\\[([^\\]+)\\] +)?pipe +(.+)$", s1, &v62, 3LL) != 3 )
{
if ( (int)ezd_regmatch(
"^input +(\\[([^\\]+)\\] +)?kafka +topic +([^ ]+) +partition +([0-9]+)(-([0-9]+))?( +from +(beginning|e"
"nd|stored|[0-9]+))$",
s1,
&v62,
8LL) <= 3 )
{
if ( (unsigned int)ezd_regmatch("^output +pipe +([0-9]+) +(.+)$", s1, &v62, 2LL) != 2 )
{
v36 = ezd_regmatch("^output +file +([0-9]+) +(.+)$", s1, &v62, 2LL);
v37 = s1;
v38 = "Unknown configuration directive \"%s\"";
if ( v36 == 2 )
{
v42 = n;
v43 = alloca(n + 9);
memcpy(v58, src, n);
v58[v42] = 0;
v44 = v63;
v45 = alloca(v63 + 9);
v46 = (const char *)memcpy(v58, v62, v63);
v46[v44] = 0;
v47 = atoi(v46);
v9 = 0;
output_add(1LL, v47, v58);
return v9;
}
goto LABEL_46;
}
v48 = n;
v49 = alloca(n + 9);
memcpy(v58, src, n);
v58[v48] = 0;
v50 = v63;
v51 = alloca(v63 + 9);
v52 = (const char *)memcpy(v58, v62, v63);
v52[v50] = 0;
v53 = atoi(v52);
output_add(0LL, v53, v58);
}
else
{
v60 = 0;
v61 = 0;
if ( src )
{
v14 = n;
v15 = alloca(n + 9);
v16 = (const char *)memcpy(v58, src, n);
v16[v14] = 0;
if ( (unsigned int)input_kvs_parse_constprop_0(v16, (int *)&v60, &v61, s, maxlen) == -1 )
return (unsigned int)-1;
}
v17 = -1000LL;
if ( v72 )
{
v17 = -2LL;
if ( memcmp(v72, "beginning", 9uLL) )
{
if ( *(_BYTE *)v72 != 101 || *((_BYTE *)v72 + 1) != 110 || (v17 = -1LL, *((_BYTE *)v72 + 2) != 100) )
{
v17 = -1000LL;
if ( memcmp(v72, "stored", 6uLL) )
{
v39 = v73;
v40 = alloca(v73 + 9);
v41 = (const char *)memcpy(v58, v72, v73);
v41[v39] = 0;
v17 = strtoull(v41, 0LL, 10);
}
}
}
}
v18 = v67;
v19 = alloca(v67 + 9);
memcpy(v58, v66, v67);
v58[v18] = 0;
v20 = v69;
v21 = alloca(v69 + 9);
v22 = (const char *)memcpy(v58, v68, v69);
v22[v20] = 0;
v23 = atoi(v22);
v24 = v23;
if ( !v70 )
{
LODWORD(v59) = v23;
LABEL_32:
while ( input_add(1LL, v60, v61, v58, (unsigned int)v24, v17, s, maxlen) )
{
if ( ++v24 > (int)v59 )
return 0;
}
return (unsigned int)-1;
}
v59 = v71;
v25 = alloca(v71 + 9);
v26 = (const char *)memcpy(v58, v70, v71);
v26[v59] = 0;
LODWORD(v59) = atoi(v26);
if ( v24 <= (int)v59 )
goto LABEL_32;
}
return 0;
}
v60 = 0;
v61 = 0;
if ( !src )
{
v31 = 0;
v30 = 0;
LABEL_38:
v32 = v67;
v33 = alloca(v67 + 9);
v34 = (char *)memcpy(v58, v66, v67);
v34[v32] = 0;
v35 = ltrim(v34);
if ( !input_add(0LL, v31, v30, v35, 0LL, 0LL, s, maxlen) )
return (unsigned int)-1;
return 0;
}
v27 = n;
v28 = alloca(n + 9);
v29 = (const char *)memcpy(v58, src, n);
v29[v27] = 0;
if ( (unsigned int)input_kvs_parse_constprop_0(v29, (int *)&v60, &v61, s, maxlen) != -1 )
{
v30 = v61;
v31 = v60;
goto LABEL_38;
}
return (unsigned int)-1;
}
v9 = strcmp(s1, "output.format") != 0;
if ( !v9 )
{
v10 = *(&conf + 7);
if ( v10 )
free(v10);
*(&conf + 7) = strdup(a2);
return v9;
}
if ( !strcmp(s1, "output.encoding") )
{
v13 = encoding_parse(a2);
*((_DWORD *)&conf + 4) = v13;
v9 = v13;
if ( v13 == -1 )
{
snprintf(s, maxlen, "Unknown %s value \"%s\"", s1, a2);
return v9;
}
return 0;
}
v9 = strcmp(s1, "output.delimiter") != 0;
if ( !v9 )
{
if ( conf )
free(conf);
v12 = strdup(a2);
conf = str_unescape(v12);
*((_DWORD *)&conf + 2) = strlen((const char *)conf);
return v9;
}
v9 = strcmp(s1, "output.queue.size") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 16) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.level") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 22) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.statistics.file") != 0;
if ( !v9 )
{
free(*(&conf + 14));
*(&conf + 14) = strdup(a2);
return v9;
}
v9 = strcmp(s1, "log.statistics.interval") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 26) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.rate.max") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 23) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.rate.period") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 24) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "daemonize") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 25) = conf_tof(a2);
return v9;
}
v54 = strcmp(s1, "pid.file.path") == 0;
v9 = !v54;
if ( v54 )
{
free(*(&conf + 16));
*(&conf + 16) = strdup(a2);
return v9;
}
v9 = strcmp(s1, "command.init");
if ( !v9 )
{
v55 = *(&conf + 17);
if ( v55 )
free(v55);
*(&conf + 17) = strdup(a2);
return v9;
}
v9 = strcmp(s1, "command.term");
if ( v9 )
{
v9 = strncmp(s1, "env.", 4uLL);
if ( !v9 )
{
v57 = s1 + 4;
if ( *a2 )
setenv(v57, a2, 1);
else
unsetenv(v57);
return v9;
}
v37 = s1;
v38 = "Unknown configuration property \"%s\"\n";
LABEL_46:
v9 = -1;
snprintf(s, maxlen, v38, v37);
return v9;
}
v56 = *(&conf + 18);
if ( v56 )
free(v56);
*(&conf + 18) = strdup(a2);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *s1, char *a2, char *s, size_t maxlen)
{
int v8; // eax
unsigned int v9; // er12
void *v10; // rdi
char *v12; // rax
int v13; // eax
size_t v14; // r12
void *v15; // rsp
const char *v16; // rax
unsigned __int64 v17; // r14
size_t v18; // r13
void *v19; // rsp
size_t v20; // r13
void *v21; // rsp
const char *v22; // rax
int v23; // eax
int v24; // er13
void *v25; // rsp
const char *v26; // rax
size_t v27; // r12
void *v28; // rsp
const char *v29; // rax
unsigned int v30; // er13
unsigned int v31; // er12
size_t v32; // r14
void *v33; // rsp
char *v34; // rax
char *v35; // rax
int v36; // eax
char *v37; // rcx
const char *v38; // rdx
size_t v39; // r12
void *v40; // rsp
const char *v41; // rax
size_t v42; // rbx
void *v43; // rsp
size_t v44; // rbx
void *v45; // rsp
const char *v46; // rax
unsigned int v47; // eax
size_t v48; // rbx
void *v49; // rsp
size_t v50; // rbx
void *v51; // rsp
const char *v52; // rax
unsigned int v53; // eax
bool v54; // zf
void *v55; // rdi
void *v56; // rdi
const char *v57; // rdi
_BYTE v58[8]; // [rsp+0h] [rbp-E0h] BYREF
size_t v59; // [rsp+8h] [rbp-D8h]
unsigned int v60; // [rsp+18h] [rbp-C8h] BYREF
unsigned int v61; // [rsp+1Ch] [rbp-C4h] BYREF
void *v62; // [rsp+20h] [rbp-C0h] BYREF
size_t v63; // [rsp+28h] [rbp-B8h]
void *src; // [rsp+30h] [rbp-B0h]
size_t n; // [rsp+38h] [rbp-A8h]
void *v66; // [rsp+40h] [rbp-A0h]
size_t v67; // [rsp+48h] [rbp-98h]
void *v68; // [rsp+50h] [rbp-90h]
size_t v69; // [rsp+58h] [rbp-88h]
void *v70; // [rsp+70h] [rbp-70h]
size_t v71; // [rsp+78h] [rbp-68h]
void *v72; // [rsp+90h] [rbp-50h]
size_t v73; // [rsp+98h] [rbp-48h]

if ( !memcmp(s1, "kafka.", 6uLL) )
{
if ( !memcmp(s1 + 6, "topic.", 6uLL) )
{
v8 = rd_kafka_topic_conf_set(*(&conf + 22), s1 + 12, a2, s, maxlen);
if ( !v8 )
return 0;
}
else
{
v8 = rd_kafka_conf_set(*(&conf + 21), s1 + 6, a2, s, maxlen);
if ( !v8 )
return 0;
}
if ( v8 != -2 )
return (unsigned int)-1;
}
if ( !a2 )
{
if ( (unsigned int)ezd_regmatch("^input +(\\[([^\\]+)\\] +)?pipe +(.+)$", s1, &v62, 3LL) != 3 )
{
if ( (int)ezd_regmatch(
"^input +(\\[([^\\]+)\\] +)?kafka +topic +([^ ]+) +partition +([0-9]+)(-([0-9]+))?( +from +(beginning|e"
"nd|stored|[0-9]+))$",
s1,
&v62,
8LL) <= 3 )
{
if ( (unsigned int)ezd_regmatch("^output +pipe +([0-9]+) +(.+)$", s1, &v62, 2LL) != 2 )
{
v36 = ezd_regmatch("^output +file +([0-9]+) +(.+)$", s1, &v62, 2LL);
v37 = s1;
v38 = "Unknown configuration directive \"%s\"";
if ( v36 == 2 )
{
v42 = n;
v43 = alloca(n + 9);
memcpy(v58, src, n);
v58[v42] = 0;
v44 = v63;
v45 = alloca(v63 + 9);
v46 = (const char *)memcpy(v58, v62, v63);
v46[v44] = 0;
v47 = atoi(v46);
v9 = 0;
output_add(1LL, v47, v58);
return v9;
}
goto LABEL_46;
}
v48 = n;
v49 = alloca(n + 9);
memcpy(v58, src, n);
v58[v48] = 0;
v50 = v63;
v51 = alloca(v63 + 9);
v52 = (const char *)memcpy(v58, v62, v63);
v52[v50] = 0;
v53 = atoi(v52);
output_add(0LL, v53, v58);
}
else
{
v60 = 0;
v61 = 0;
if ( src )
{
v14 = n;
v15 = alloca(n + 9);
v16 = (const char *)memcpy(v58, src, n);
v16[v14] = 0;
if ( (unsigned int)func5(v16, (int *)&v60, &v61, s, maxlen) == -1 )
return (unsigned int)-1;
}
v17 = -1000LL;
if ( v72 )
{
v17 = -2LL;
if ( memcmp(v72, "beginning", 9uLL) )
{
if ( *(_BYTE *)v72 != 101 || *((_BYTE *)v72 + 1) != 110 || (v17 = -1LL, *((_BYTE *)v72 + 2) != 100) )
{
v17 = -1000LL;
if ( memcmp(v72, "stored", 6uLL) )
{
v39 = v73;
v40 = alloca(v73 + 9);
v41 = (const char *)memcpy(v58, v72, v73);
v41[v39] = 0;
v17 = strtoull(v41, 0LL, 10);
}
}
}
}
v18 = v67;
v19 = alloca(v67 + 9);
memcpy(v58, v66, v67);
v58[v18] = 0;
v20 = v69;
v21 = alloca(v69 + 9);
v22 = (const char *)memcpy(v58, v68, v69);
v22[v20] = 0;
v23 = atoi(v22);
v24 = v23;
if ( !v70 )
{
LODWORD(v59) = v23;
LABEL_32:
while ( input_add(1LL, v60, v61, v58, (unsigned int)v24, v17, s, maxlen) )
{
if ( ++v24 > (int)v59 )
return 0;
}
return (unsigned int)-1;
}
v59 = v71;
v25 = alloca(v71 + 9);
v26 = (const char *)memcpy(v58, v70, v71);
v26[v59] = 0;
LODWORD(v59) = atoi(v26);
if ( v24 <= (int)v59 )
goto LABEL_32;
}
return 0;
}
v60 = 0;
v61 = 0;
if ( !src )
{
v31 = 0;
v30 = 0;
LABEL_38:
v32 = v67;
v33 = alloca(v67 + 9);
v34 = (char *)memcpy(v58, v66, v67);
v34[v32] = 0;
v35 = func0(v34);
if ( !input_add(0LL, v31, v30, v35, 0LL, 0LL, s, maxlen) )
return (unsigned int)-1;
return 0;
}
v27 = n;
v28 = alloca(n + 9);
v29 = (const char *)memcpy(v58, src, n);
v29[v27] = 0;
if ( (unsigned int)func5(v29, (int *)&v60, &v61, s, maxlen) != -1 )
{
v30 = v61;
v31 = v60;
goto LABEL_38;
}
return (unsigned int)-1;
}
v9 = strcmp(s1, "output.format") != 0;
if ( !v9 )
{
v10 = *(&conf + 7);
if ( v10 )
free(v10);
*(&conf + 7) = strdup(a2);
return v9;
}
if ( !strcmp(s1, "output.encoding") )
{
v13 = func2(a2);
*((_DWORD *)&conf + 4) = v13;
v9 = v13;
if ( v13 == -1 )
{
snprintf(s, maxlen, "Unknown %s value \"%s\"", s1, a2);
return v9;
}
return 0;
}
v9 = strcmp(s1, "output.delimiter") != 0;
if ( !v9 )
{
if ( conf )
free(conf);
v12 = strdup(a2);
conf = func3(v12);
*((_DWORD *)&conf + 2) = strlen((const char *)conf);
return v9;
}
v9 = strcmp(s1, "output.queue.size") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 16) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.level") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 22) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.statistics.file") != 0;
if ( !v9 )
{
free(*(&conf + 14));
*(&conf + 14) = strdup(a2);
return v9;
}
v9 = strcmp(s1, "log.statistics.interval") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 26) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.rate.max") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 23) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "log.rate.period") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 24) = atoi(a2);
return v9;
}
v9 = strcmp(s1, "daemonize") != 0;
if ( !v9 )
{
*((_DWORD *)&conf + 25) = func4(a2);
return v9;
}
v54 = strcmp(s1, "pid.file.path") == 0;
v9 = !v54;
if ( v54 )
{
free(*(&conf + 16));
*(&conf + 16) = strdup(a2);
return v9;
}
v9 = strcmp(s1, "command.init");
if ( !v9 )
{
v55 = *(&conf + 17);
if ( v55 )
free(v55);
*(&conf + 17) = strdup(a2);
return v9;
}
v9 = strcmp(s1, "command.term");
if ( v9 )
{
v9 = strncmp(s1, "env.", 4uLL);
if ( !v9 )
{
v57 = s1 + 4;
if ( *a2 )
setenv(v57, a2, 1);
else
unsetenv(v57);
return v9;
}
v37 = s1;
v38 = "Unknown configuration property \"%s\"\n";
LABEL_46:
v9 = -1;
snprintf(s, maxlen, v38, v37);
return v9;
}
v56 = *(&conf + 18);
if ( v56 )
free(v56);
*(&conf + 18) = strdup(a2);
return v9;
}

original_code:
int conf_set (const char *name, const char *val,
       char *errstr, size_t errstr_size,
       void *opaque) {
 rd_kafka_conf_res_t res;
 if (!strncmp(name, "kafka.", strlen("kafka."))) {
  name += strlen("kafka.");
  if (!strncmp(name, "topic.", strlen("topic.")))
   res = rd_kafka_topic_conf_set(conf.rkt_conf,
            name+strlen("topic."),
            val,
            errstr, errstr_size);
  else
   res = rd_kafka_conf_set(conf.rk_conf, name,
      val, errstr, errstr_size);
  if (res == RD_KAFKA_CONF_OK)
   return 0;
  else if (res != RD_KAFKA_CONF_UNKNOWN)
   return -1;
  name -= strlen("kafka.");
 }
 if (!val) {
  struct iovec arg[8+1];
  if (ezd_regmatch("^input +(\\[([^\\]+)\\] +)?pipe +(.+)$",
     name, arg, 3) == 3) {
   encoding_t enc = ENC_STRING;
   int flags = 0;
   if (arg[1].iov_base) {
    if (input_kvs_parse(({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }),
          &enc, &flags,
          errstr, errstr_size) == -1)
     return -1;
   }
   if (!input_add(INPUT_PIPE, enc, flags,
           ltrim(({ struct iovec *_v = (&arg[2]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; })), 0, 0,
           errstr, errstr_size))
    return -1;
  } else if (ezd_regmatch("^input +(\\[([^\\]+)\\] +)?kafka +"
     "topic +([^ ]+) +"
     "partition +([0-9]+)(-([0-9]+))?"
     "( +from "
     "+(beginning|end|stored|[0-9]+))"
     "$",
     name, arg, 8) >= 4) {
   int part_lo, part_hi;
   char *topic;
   int64_t offset = -1000;
   encoding_t enc = ENC_STRING;
   int flags = 0;
   if (arg[1].iov_base) {
    if (input_kvs_parse(({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }),
          &enc, &flags,
          errstr, errstr_size) == -1)
     return -1;
   }
   if (arg[7].iov_base) {
    if (!strncmp(arg[7].iov_base, "beginning", 9))
     offset = -2;
    else if (!strncmp(arg[7].iov_base, "end", 3))
     offset = -1;
    else if (!strncmp(arg[7].iov_base, "stored", 6))
     offset = -1000;
    else
     offset = strtoull(({ struct iovec *_v = (&arg[7]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }),
         ((void *)0), 10);
   }
   topic = ({ struct iovec *_v = (&arg[2]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; });
   part_lo = atoi(({ struct iovec *_v = (&arg[3]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
   if (arg[5].iov_base)
    part_hi = atoi(({ struct iovec *_v = (&arg[5]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
   else
    part_hi = part_lo;
   for ( ; part_lo <= part_hi ; part_lo++)
    if (!input_add(INPUT_KAFKA, enc, flags,
            topic, part_lo,
            offset, errstr, errstr_size))
     return -1;
  } else if (ezd_regmatch("^output +pipe +([0-9]+) +(.+)$",
     name, arg, 2) == 2) {
   output_add(OUTPUT_PIPE, atoi(({ struct iovec *_v = (&arg[0]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; })),
       ({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
  } else if (ezd_regmatch("^output +file +([0-9]+) +(.+)$",
     name, arg, 2) == 2) {
   output_add(OUTPUT_FILE, atoi(({ struct iovec *_v = (&arg[0]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; })),
       ({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1) ; memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
  } else {
   snprintf(errstr, errstr_size,
     "Unknown configuration directive \"%s\"",
     name);
   return -1;
  }
  return 0;
 }
 if (!strcmp(name, "output.format")) {
  if (conf.fconf.format)
   free(conf.fconf.format);
  conf.fconf.format = strdup(val);
 } else if (!strcmp(name, "output.encoding")) {
  if ((conf.fconf.encoding =
       encoding_parse(val)) == ENC_ERROR) {
   snprintf(errstr, errstr_size,
     "Unknown %s value \"%s\"", name, val);
   return -1;
  }
 } else if (!strcmp(name, "output.delimiter")) {
  if (conf.output_delimiter)
   free(conf.output_delimiter);
  conf.output_delimiter = str_unescape(strdup(val));
  conf.output_delimiter_len = strlen(conf.output_delimiter);
 } else if (!strcmp(name, "output.queue.size")) {
  conf.output_queue_size = atoi(val);
 } else if (!strcmp(name, "log.level"))
  conf.log_level = atoi(val);
 else if (!strcmp(name, "log.statistics.file")) {
  free(conf.stats_file);
  conf.stats_file = strdup(val);
 } else if (!strcmp(name, "log.statistics.interval"))
  conf.stats_interval = atoi(val);
 else if (!strcmp(name, "log.rate.max"))
  conf.log_rate = atoi(val);
 else if (!strcmp(name, "log.rate.period"))
  conf.log_rate_period = atoi(val);
 else if (!strcmp(name, "daemonize"))
  conf.daemonize = conf_tof(val);
 else if (!strcmp(name, "pid.file.path")) {
  free(conf.pid_file_path);
  conf.pid_file_path = strdup(val);
 } else if (!strcmp(name, "command.init")) {
  if (conf.cmd_init)
   free(conf.cmd_init);
  conf.cmd_init = strdup(val);
 } else if (!strcmp(name, "command.term")) {
  if (conf.cmd_term)
   free(conf.cmd_term);
  conf.cmd_term = strdup(val);
 } else if (!strncmp(name, "env.", strlen("env."))) {
  if (*val)
   setenv(name+strlen("env."), val, 1);
  else
   unsetenv(name+strlen("env."));
 } else {
  snprintf(errstr, errstr_size,
    "Unknown configuration property \"%s\"\n", name);
  return -1;
 }
 return 0;
}

canonical_original_code:
int conf_set(char *name, char *val, char *errstr, unsigned long errstr_size, void *opaque) {
 enum rd_kafka_conf_res_t res;
 if (!strncmp(name, "kafka.", strlen("kafka."))) {
  name += strlen("kafka.");
  if (!strncmp(name, "topic.", strlen("topic.")))
   res = rd_kafka_topic_conf_set(conf.rkt_conf,
            name+strlen("topic."),
            val,
            errstr, errstr_size);
  else
   res = rd_kafka_conf_set(conf.rk_conf, name,
      val, errstr, errstr_size);
  if (res == RD_KAFKA_CONF_OK)
   return 0;
  else if (res != RD_KAFKA_CONF_UNKNOWN)
   return -1;
  name -= strlen("kafka.");
 }
 if (!val) {
  struct iovec arg[-1];
  if (ezd_regmatch("^input +(\\[([^\\]+)\\] +)?pipe +(.+)$",
     name, arg, 3) == 3) {
   enum encoding_t enc = ENC_STRING;
   int flags = 0;
   if (arg[1].iov_base) {
    if (input_kvs_parse(({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }),
          &enc, &flags,
          errstr, errstr_size) == -1)
     return -1;
   }
   if (!input_add(INPUT_PIPE, enc, flags,
           ltrim(({ struct iovec *_v = (&arg[2]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; })), 0, 0,
           errstr, errstr_size))
    return -1;
  } else if (ezd_regmatch("^input +(\\[([^\\]+)\\] +)?kafka +"
     "topic +([^ ]+) +"
     "partition +([0-9]+)(-([0-9]+))?"
     "( +from "
     "+(beginning|end|stored|[0-9]+))"
     "$",
     name, arg, 8) >= 4) {
   int part_lo; int part_hi;
   char *topic;
   long offset = -1000;
   enum encoding_t enc = ENC_STRING;
   int flags = 0;
   if (arg[1].iov_base) {
    if (input_kvs_parse(({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }),
          &enc, &flags,
          errstr, errstr_size) == -1)
     return -1;
   }
   if (arg[7].iov_base) {
    if (!strncmp(arg[7].iov_base, "beginning", 9))
     offset = -2;
    else if (!strncmp(arg[7].iov_base, "end", 3))
     offset = -1;
    else if (!strncmp(arg[7].iov_base, "stored", 6))
     offset = -1000;
    else
     offset = strtoull(({ struct iovec *_v = (&arg[7]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }),
         ((void *)0), 10);
   }
   topic = ({ struct iovec *_v = (&arg[2]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; });
   part_lo = atoi(({ struct iovec *_v = (&arg[3]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
   if (arg[5].iov_base)
    part_hi = atoi(({ struct iovec *_v = (&arg[5]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
   else
    part_hi = part_lo;
   for ( ; part_lo <= part_hi ; part_lo++)
    if (!input_add(INPUT_KAFKA, enc, flags,
            topic, part_lo,
            offset, errstr, errstr_size))
     return -1;
  } else if (ezd_regmatch("^output +pipe +([0-9]+) +(.+)$",
     name, arg, 2) == 2) {
   output_add(OUTPUT_PIPE, atoi(({ struct iovec *_v = (&arg[0]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; })),
       ({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
  } else if (ezd_regmatch("^output +file +([0-9]+) +(.+)$",
     name, arg, 2) == 2) {
   output_add(OUTPUT_FILE, atoi(({ struct iovec *_v = (&arg[0]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; })),
       ({ struct iovec *_v = (&arg[1]); char *_s = __builtin_alloca (_v->iov_len+1); memcpy(_s, _v->iov_base, _v->iov_len); _s[_v->iov_len] = '\0'; _s; }));
  } else {
   snprintf(errstr, errstr_size,
     "Unknown configuration directive \"%s\"",
     name);
   return -1;
  }
  return 0;
 }
 if (!strcmp(name, "output.format")) {
  if (conf.fconf.format)
   free(conf.fconf.format);
  conf.fconf.format = strdup(val);
 } else if (!strcmp(name, "output.encoding")) {
  if ((conf.fconf.encoding =
       encoding_parse(val)) == ENC_ERROR) {
   snprintf(errstr, errstr_size,
     "Unknown %s value \"%s\"", name, val);
   return -1;
  }
 } else if (!strcmp(name, "output.delimiter")) {
  if (conf.output_delimiter)
   free(conf.output_delimiter);
  conf.output_delimiter = str_unescape(strdup(val));
  conf.output_delimiter_len = strlen(conf.output_delimiter);
 } else if (!strcmp(name, "output.queue.size")) {
  conf.output_queue_size = atoi(val);
 } else if (!strcmp(name, "log.level"))
  conf.log_level = atoi(val);
 else if (!strcmp(name, "log.statistics.file")) {
  free(conf.stats_file);
  conf.stats_file = strdup(val);
 } else if (!strcmp(name, "log.statistics.interval"))
  conf.stats_interval = atoi(val);
 else if (!strcmp(name, "log.rate.max"))
  conf.log_rate = atoi(val);
 else if (!strcmp(name, "log.rate.period"))
  conf.log_rate_period = atoi(val);
 else if (!strcmp(name, "daemonize"))
  conf.daemonize = conf_tof(val);
 else if (!strcmp(name, "pid.file.path")) {
  free(conf.pid_file_path);
  conf.pid_file_path = strdup(val);
 } else if (!strcmp(name, "command.init")) {
  if (conf.cmd_init)
   free(conf.cmd_init);
  conf.cmd_init = strdup(val);
 } else if (!strcmp(name, "command.term")) {
  if (conf.cmd_term)
   free(conf.cmd_term);
  conf.cmd_term = strdup(val);
 } else if (!strncmp(name, "env.", strlen("env."))) {
  if (*val)
   setenv(name+strlen("env."), val, 1);
  else
   unsetenv(name+strlen("env."));
 } else {
  snprintf(errstr, errstr_size,
    "Unknown configuration property \"%s\"\n", name);
  return -1;
 }
 return 0;
}

variable_types:
name
char *

variable_types:
errstr
char *

variable_types:
opaque
void *

variable_types: enum rd_kafka_conf_res_t { RD_KAFKA_CONF_UNKNOWN=-2, RD_KAFKA_CONF_INVALID=-1, RD_KAFKA_CONF_OK=0 }

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } [-1]

variable_types: enum encoding_t { ENC_ERROR=-1, ENC_STRING=0, ENC_JSON=1 }

variable_types: flags int

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } *

variable_types:
topic
char *

variable_types: offset long


repo: wikimedia/analytics-kafkatee

decompiled_code:
int outputs_term()
{
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

return pthread_join(outputs_thread, &thread_return);
}

canonical_decompiled_code:
int func10()
{
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

return pthread_join(outputs_thread, &thread_return);
}

original_code:
void outputs_term (void) {
 void *ignore;
 pthread_join(outputs_thread, &ignore);
}

canonical_original_code:
void outputs_term() {
 void *ignore;
 pthread_join(outputs_thread, &ignore);
}

variable_types:
ignore
void *

return_type: void


decompiled_code:
int outputs_start()
{
int result; // eax
int *v1; // rax
char *v2; // rdx
int *v3; // rax
char *v4; // rdx

outputs_epoll_fd = epoll_create1(0x80000);
if ( outputs_epoll_fd == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
syslog(2, "Failed to create epoll fd: %s", v2);
goto LABEL_5;
}
result = pthread_create(&outputs_thread, 0LL, outputs_main, 0LL);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(2, "Failed to create outputs thread: %s", v4);
LABEL_5:
exit(1);
}
return result;
}

canonical_decompiled_code:
int func11()
{
int result; // eax
int *v1; // rax
char *v2; // rdx
int *v3; // rax
char *v4; // rdx

outputs_epoll_fd = epoll_create1(0x80000);
if ( outputs_epoll_fd == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
syslog(2, "Failed to create epoll fd: %s", v2);
goto LABEL_5;
}
result = pthread_create(&outputs_thread, 0LL, outputs_main, 0LL);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(2, "Failed to create outputs thread: %s", v4);
LABEL_5:
exit(1);
}
return result;
}

original_code:
void outputs_start (void) {
 outputs_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 if (outputs_epoll_fd == -1) {
  syslog(2, "Failed to create epoll fd: %s", strerror((*__errno_location ())));
  exit(1);
 }
 if (pthread_create(&outputs_thread, ((void *)0), outputs_main, ((void *)0)) == -1) {
  syslog(2, "Failed to create outputs thread: %s", strerror((*__errno_location ())));
  exit(1);
 }
}

canonical_original_code:
void outputs_start() {
 outputs_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 if (outputs_epoll_fd == -1) {
  syslog(2, "Failed to create epoll fd: %s", strerror((*__errno_location ())));
  exit(1);
 }
 if (pthread_create(&outputs_thread, ((void *)0), outputs_main, ((void *)0)) == -1) {
  syslog(2, "Failed to create outputs thread: %s", strerror((*__errno_location ())));
  exit(1);
 }
}


sighup

decompiled_code:
void sighup()
{
conf[21] = ++rotate_version_0;
}

canonical_decompiled_code:
void func0()
{
conf[21] = ++rotate_version_0;
}

original_code:
static void sighup (int sig) {
 static int rotate_version = 0;
 conf.rotate = ++rotate_version;
}

canonical_original_code:
static void sighup(int sig) {
 static int rotate_version = 0;
 conf.rotate = ++rotate_version;
}

variable_types: sig int


term

decompiled_code:
void __fastcall term(unsigned int a1)
{
syslog(5, "Received signal %i: terminating", a1);
if ( !conf[19] )
{
syslog(4, "Forced termination");
exit(0);
}
_InterlockedExchange(&conf[19], 0);
}

canonical_decompiled_code:
void __fastcall func2(unsigned int a1)
{
syslog(5, "Received signal %i: terminating", a1);
if ( !conf[19] )
{
syslog(4, "Forced termination");
exit(0);
}
_InterlockedExchange(&conf[19], 0);
}

original_code:
static void term (int sig) {
 syslog(5, "Received signal %i: terminating", sig);
 if (!conf.run) {
  syslog(4, "Forced termination");
  exit(0);
 }
 __sync_lock_test_and_set(&conf.run,0);
}

canonical_original_code:
static void term(int sig) {
 syslog(5, "Received signal %i: terminating", sig);
 if (!conf.run) {
  syslog(4, "Forced termination");
  exit(0);
 }
 __sync_lock_test_and_set(&conf.run,0);
}


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(
stderr,
"kafkatee version %s\n"
"Kafka consumer with multiple inputs and outputs\n"
"\n"
"Usage: %s [options]\n"
"\n"
"Options:\n"
" -c <path> Configuration file path (%s)\n"
" -p <path> Pid file path (%s)\n"
" -d Enable debugging\n"
" -D Do not daemonize\n"
" -e Exit on EOF:\n"
" Exit when all inputs have reached their\n"
" EOF and all output queues are empty.\n"
" -x Exit on input or output failure.\n"
" (exit code 2)\n"
"\n",
"978c5d",
a1,
"/etc/kafkatee.conf",
"/run/kafkatee.pid");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(const char *a1)
{
fprintf(
stderr,
"kafkatee version %s\n"
"Kafka consumer with multiple inputs and outputs\n"
"\n"
"Usage: %s [options]\n"
"\n"
"Options:\n"
" -c <path> Configuration file path (%s)\n"
" -p <path> Pid file path (%s)\n"
" -d Enable debugging\n"
" -D Do not daemonize\n"
" -e Exit on EOF:\n"
" Exit when all inputs have reached their\n"
" EOF and all output queues are empty.\n"
" -x Exit on input or output failure.\n"
" (exit code 2)\n"
"\n",
"978c5d",
a1,
"/etc/kafkatee.conf",
"/run/kafkatee.pid");
exit(1);
}

original_code:
static void usage (const char *argv0) {
 fprintf(stderr,
  "kafkatee version %s\n"
  "Kafka consumer with multiple inputs and outputs\n"
  "\n"
  "Usage: %s [options]\n"
  "\n"
  "Options:\n"
  "  -c <path>          Configuration file path (%s)\n"
  "  -p <path>          Pid file path (%s)\n"
  "  -d                 Enable debugging\n"
  "  -D                 Do not daemonize\n"
  "  -e                 Exit on EOF:\n"
  "                     Exit when all inputs have reached their\n"
  "                     EOF and all output queues are empty.\n"
  "  -x                 Exit on input or output failure.\n"
  "                     (exit code 2)\n"
  "\n",
  "978c5d",
  argv0,
  "/etc/kafkatee.conf",
  "/run/kafkatee.pid");
 exit(1);
}

canonical_original_code:
static void usage(char *argv0) {
 fprintf(stderr,
  "kafkatee version %s\n"
  "Kafka consumer with multiple inputs and outputs\n"
  "\n"
  "Usage: %s [options]\n"
  "\n"
  "Options:\n"
  "  -c <path>          Configuration file path (%s)\n"
  "  -p <path>          Pid file path (%s)\n"
  "  -d                 Enable debugging\n"
  "  -D                 Do not daemonize\n"
  "  -e                 Exit on EOF:\n"
  "                     Exit when all inputs have reached their\n"
  "                     EOF and all output queues are empty.\n"
  "  -x                 Exit on input or output failure.\n"
  "                     (exit code 2)\n"
  "\n",
  "978c5d",
  argv0,
  "/etc/kafkatee.conf",
  "/run/kafkatee.pid");
 exit(1);
}


decompiled_code:
int stats_close()
{
int result; // eax

result = fclose(*(FILE **)&conf[30]);
*(_QWORD *)&conf[30] = 0LL;
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

result = fclose(*(FILE **)&conf[30]);
*(_QWORD *)&conf[30] = 0LL;
return result;
}

original_code:
static void stats_close (void) {
 stats_print();
 fclose(conf.stats_fp);
 conf.stats_fp = ((void *)0);
}

canonical_original_code:
static void stats_close() {
 stats_print();
 fclose(conf.stats_fp);
 conf.stats_fp = ((void *)0);
}


decompiled_code:
__int64 stats_open()
{
FILE *v0; // rax
int *v2; // rax
char *v3; // rax

if ( *(_QWORD *)&conf[30] )
stats_close();
v0 = fopen(*(const char **)&conf[28], "a");
*(_QWORD *)&conf[30] = v0;
if ( v0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
syslog(3, "Failed to open statistics log file %s: %s", *(const char **)&conf[28], v3);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func6()
{
FILE *v0; // rax
int *v2; // rax
char *v3; // rax

if ( *(_QWORD *)&conf[30] )
func5();
v0 = fopen(*(const char **)&conf[28], "a");
*(_QWORD *)&conf[30] = v0;
if ( v0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
syslog(3, "Failed to open statistics log file %s: %s", *(const char **)&conf[28], v3);
return 0xFFFFFFFFLL;
}

original_code:
static int stats_open (void) {
 if (conf.stats_fp)
  stats_close();
 if (!(conf.stats_fp = fopen(conf.stats_file, "a"))) {
  syslog(3, "Failed to open statistics log file %s: %s", conf.stats_file, strerror((*__errno_location ())));
  return -1;
 }
 return 0;
}

canonical_original_code:
static int stats_open() {
 if (conf.stats_fp)
  stats_close();
 if (!(conf.stats_fp = fopen(conf.stats_file, "a"))) {
  syslog(3, "Failed to open statistics log file %s: %s", conf.stats_file, strerror((*__errno_location ())));
  return -1;
 }
 return 0;
}

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
char *v4; // rax
int v5; // eax
__int64 v6; // rax
char v7; // al
__int64 v8; // rsi
__int64 v9; // rax
const char *v10; // rdi
bool v11; // zf
const char *v12; // rdi
int *v13; // rax
char *v14; // rax
char v16[32]; // [rsp+0h] [rbp-238h] BYREF
char v17[536]; // [rsp+20h] [rbp-218h] BYREF

v3 = "/etc/kafkatee.conf";
v4 = strdup("/run/kafkatee.pid");
conf[39] = 0;
*(_QWORD *)&conf[32] = v4;
conf[22] = 6;
*(_QWORD *)&conf[25] = 0x3C00000001LL;
*(_QWORD *)&conf[28] = strdup("/tmp/kafkatee.stats.json");
*(_QWORD *)&conf[18] = 0x100002800LL;
*(_QWORD *)conf = strdup("\n");
v5 = strlen(*(const char **)conf);
conf[16] = 100000;
conf[2] = v5;
v6 = rd_kafka_conf_new();
*(_QWORD *)&conf[42] = v6;
rd_kafka_conf_set(v6, "client.id", "kafkatee", 0LL, 0LL);
((void (__fastcall *)(_QWORD, void (__fastcall *)(__int64, unsigned int, const char *)))rd_kafka_conf_set_error_cb)(
*(_QWORD *)&conf[42],
kafka_error_cb);
*(_QWORD *)&conf[44] = ((__int64 (*)(void))rd_kafka_topic_conf_new)();
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "hc:p:dDex");
if ( v7 == -1 )
break;
if ( v7 == 68 )
{
conf[25] = 0;
}
else
{
switch ( v7 )
{
case 'c':
v3 = optarg;
break;
case 'd':
conf[22] = 7;
break;
case 'e':
conf[38] |= 1u;
break;
case 'p':
*(_QWORD *)&conf[32] = strdup(optarg);
break;
case 'x':
conf[38] |= 2u;
break;
default:
usage(*argv);
}
}
}
openlog("kafkatee", 33, 24);
if ( (unsigned int)ezd_conf_file_read(v3, &conf_set, v17, 512LL, 0LL) == -1
|| conf[25]
&& ((unsigned int)ezd_daemon(10LL, v17, 512LL) == -1
|| (unsigned int)ezd_pidfile_open(*(_QWORD *)&conf[32], v17, 512LL) == -1) )
{
syslog(3, "%s", v17);
exit(1);
}
v8 = *(_QWORD *)&conf[14];
if ( v8 )
{
if ( conf[4] )
{
syslog(3, "Output formatting only supported for output.encoding = string");
if ( !conf[25] )
goto LABEL_19;
goto LABEL_33;
}
if ( (unsigned int)format_parse((unsigned int)((_DWORD)conf + 16), v8, v17, 512LL) == -1 )
{
syslog(3, "Failed to parse format string: %s\n%s", *(_QWORD *)&conf[14], v17);
goto LABEL_18;
}
}
if ( conf[26] )
{
if ( (unsigned int)stats_open() == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
syslog(3, "Failed to open statistics log file %s: %s", *(_QWORD *)&conf[28], v14);
LABEL_18:
if ( !conf[25] )
goto LABEL_19;
goto LABEL_33;
}
snprintf(v16, 0x1EuLL, "%i", (unsigned int)(1000 * conf[26]));
rd_kafka_conf_set_stats_cb(*(_QWORD *)&conf[42], kafka_stats_cb);
rd_kafka_conf_set(*(_QWORD *)&conf[42], "statistics.interval.ms", v16, 0LL, 0LL);
}
v9 = rd_kafka_new(1LL, *(_QWORD *)&conf[42], v17, 512LL);
*(_QWORD *)&conf[40] = v9;
if ( v9 )
{
rd_kafka_set_log_level(v9, (unsigned int)conf[22]);
exec_init();
if ( conf[25] )
ezd_daemon_started();
v10 = *(const char **)&conf[34];
if ( v10 && system(v10) )
syslog(3, "\"command.init\" execution of \"%s\" failed with exit code %i", *(const char **)&conf[34], 1LL);
ezd_thread_sigmask(0LL, 0LL, 0xFFFFFFFFLL);
outputs_start();
inputs_start();
ezd_thread_sigmask(1LL, 1LL, 2LL);
signal(1, (__sighandler_t)sighup);
signal(2, (__sighandler_t)term);
signal(15, (__sighandler_t)term);
syslog(6, "kafkatee starting");
while ( conf[19] )
{
rd_kafka_poll(*(_QWORD *)&conf[40], 1000LL);
if ( conf[21] != our_rotate_version_1 )
{
v11 = conf[26] == 0;
our_rotate_version_1 = conf[21];
if ( !v11 )
stats_open();
}
}
inputs_term();
outputs_term();
exec_term();
rd_kafka_destroy(*(_QWORD *)&conf[40]);
rd_kafka_wait_destroyed(5000LL);
if ( *(_QWORD *)&conf[30] )
stats_close();
free(*(void **)&conf[28]);
v12 = *(const char **)&conf[36];
if ( v12 && system(v12) )
syslog(3, "\"command.term\" execution of \"%s\" failed with exit code %i", *(const char **)&conf[36], 1LL);
if ( conf[25] )
ezd_pidfile_close();
syslog(6, "kafkatee exiting");
exit(conf[39]);
}
syslog(3, "Failed to create kafka handle: %s", v17);
if ( !conf[25] )
LABEL_19:
exit(1);
LABEL_33:
ezd_pidfile_close();
goto LABEL_19;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
char *v4; // rax
int v5; // eax
__int64 v6; // rax
char v7; // al
__int64 v8; // rsi
__int64 v9; // rax
const char *v10; // rdi
bool v11; // zf
const char *v12; // rdi
int *v13; // rax
char *v14; // rax
char v16[32]; // [rsp+0h] [rbp-238h] BYREF
char v17[536]; // [rsp+20h] [rbp-218h] BYREF

v3 = "/etc/kafkatee.conf";
v4 = strdup("/run/kafkatee.pid");
conf[39] = 0;
*(_QWORD *)&conf[32] = v4;
conf[22] = 6;
*(_QWORD *)&conf[25] = 0x3C00000001LL;
*(_QWORD *)&conf[28] = strdup("/tmp/kafkatee.stats.json");
*(_QWORD *)&conf[18] = 0x100002800LL;
*(_QWORD *)conf = strdup("\n");
v5 = strlen(*(const char **)conf);
conf[16] = 100000;
conf[2] = v5;
v6 = rd_kafka_conf_new();
*(_QWORD *)&conf[42] = v6;
rd_kafka_conf_set(v6, "client.id", "kafkatee", 0LL, 0LL);
((void (__fastcall *)(_QWORD, void (__fastcall *)(__int64, unsigned int, const char *)))rd_kafka_conf_set_error_cb)(
*(_QWORD *)&conf[42],
kafka_error_cb);
*(_QWORD *)&conf[44] = ((__int64 (*)(void))rd_kafka_topic_conf_new)();
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "hc:p:dDex");
if ( v7 == -1 )
break;
if ( v7 == 68 )
{
conf[25] = 0;
}
else
{
switch ( v7 )
{
case 'c':
v3 = optarg;
break;
case 'd':
conf[22] = 7;
break;
case 'e':
conf[38] |= 1u;
break;
case 'p':
*(_QWORD *)&conf[32] = strdup(optarg);
break;
case 'x':
conf[38] |= 2u;
break;
default:
func3(*argv);
}
}
}
openlog("kafkatee", 33, 24);
if ( (unsigned int)ezd_conf_file_read(v3, &conf_set, v17, 512LL, 0LL) == -1
|| conf[25]
&& ((unsigned int)ezd_daemon(10LL, v17, 512LL) == -1
|| (unsigned int)ezd_pidfile_open(*(_QWORD *)&conf[32], v17, 512LL) == -1) )
{
syslog(3, "%s", v17);
exit(1);
}
v8 = *(_QWORD *)&conf[14];
if ( v8 )
{
if ( conf[4] )
{
syslog(3, "Output formatting only supported for output.encoding = string");
if ( !conf[25] )
goto LABEL_19;
goto LABEL_33;
}
if ( (unsigned int)format_parse((unsigned int)((_DWORD)conf + 16), v8, v17, 512LL) == -1 )
{
syslog(3, "Failed to parse format string: %s\n%s", *(_QWORD *)&conf[14], v17);
goto LABEL_18;
}
}
if ( conf[26] )
{
if ( (unsigned int)func6() == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
syslog(3, "Failed to open statistics log file %s: %s", *(_QWORD *)&conf[28], v14);
LABEL_18:
if ( !conf[25] )
goto LABEL_19;
goto LABEL_33;
}
snprintf(v16, 0x1EuLL, "%i", (unsigned int)(1000 * conf[26]));
rd_kafka_conf_set_stats_cb(*(_QWORD *)&conf[42], kafka_stats_cb);
rd_kafka_conf_set(*(_QWORD *)&conf[42], "statistics.interval.ms", v16, 0LL, 0LL);
}
v9 = rd_kafka_new(1LL, *(_QWORD *)&conf[42], v17, 512LL);
*(_QWORD *)&conf[40] = v9;
if ( v9 )
{
rd_kafka_set_log_level(v9, (unsigned int)conf[22]);
exec_init();
if ( conf[25] )
ezd_daemon_started();
v10 = *(const char **)&conf[34];
if ( v10 && system(v10) )
syslog(3, "\"command.init\" execution of \"%s\" failed with exit code %i", *(const char **)&conf[34], 1LL);
ezd_thread_sigmask(0LL, 0LL, 0xFFFFFFFFLL);
outputs_start();
inputs_start();
ezd_thread_sigmask(1LL, 1LL, 2LL);
signal(1, (__sighandler_t)sighup);
signal(2, (__sighandler_t)term);
signal(15, (__sighandler_t)term);
syslog(6, "kafkatee starting");
while ( conf[19] )
{
rd_kafka_poll(*(_QWORD *)&conf[40], 1000LL);
if ( conf[21] != our_rotate_version_1 )
{
v11 = conf[26] == 0;
our_rotate_version_1 = conf[21];
if ( !v11 )
func6();
}
}
inputs_term();
outputs_term();
exec_term();
rd_kafka_destroy(*(_QWORD *)&conf[40]);
rd_kafka_wait_destroyed(5000LL);
if ( *(_QWORD *)&conf[30] )
func5();
free(*(void **)&conf[28]);
v12 = *(const char **)&conf[36];
if ( v12 && system(v12) )
syslog(3, "\"command.term\" execution of \"%s\" failed with exit code %i", *(const char **)&conf[36], 1LL);
if ( conf[25] )
ezd_pidfile_close();
syslog(6, "kafkatee exiting");
exit(conf[39]);
}
syslog(3, "Failed to create kafka handle: %s", v17);
if ( !conf[25] )
LABEL_19:
exit(1);
LABEL_33:
ezd_pidfile_close();
goto LABEL_19;
}

original_code:
int main (int argc, char **argv) {
 const char *conf_file_path = "/etc/kafkatee.conf";
 char errstr[512];
 char c;
 int r;
 static int our_rotate_version = 0;
 conf.pid_file_path = strdup("/run/kafkatee.pid");
 conf.run = 1;
 conf.exit_code = 0;
 conf.log_level = 6;
 conf.daemonize = 1;
 conf.stats_interval = 60;
 conf.stats_file = strdup("/tmp/kafkatee.stats.json");
 conf.input_buf_size = 1024 * 10;
 conf.output_delimiter = strdup("\n");
 conf.output_delimiter_len = strlen(conf.output_delimiter);
 conf.output_queue_size = 100000;
 conf.rk_conf = rd_kafka_conf_new();
 rd_kafka_conf_set(conf.rk_conf, "client.id", "kafkatee", ((void *)0), 0);
 rd_kafka_conf_set_error_cb(conf.rk_conf, kafka_error_cb);
 conf.rkt_conf = rd_kafka_topic_conf_new();
 while ((c = getopt(argc, argv, "hc:p:dDex")) != -1) {
  switch (c) {
  case 'h':
   usage(argv[0]);
   break;
  case 'c':
   conf_file_path = optarg;
   break;
  case 'p':
   conf.pid_file_path = strdup(optarg);
   break;
  case 'd':
   conf.log_level = 7;
   break;
  case 'D':
   conf.daemonize = 0;
   break;
  case 'e':
   conf.flags |= 0x1;
   break;
  case 'x':
   conf.flags |= 0x2;
   break;
  default:
   usage(argv[0]);
   break;
  }
 }
 openlog("kafkatee", 0x01|0x20, (3<<3));
 if (ezd_conf_file_read(conf_file_path, conf_set,
          errstr, sizeof(errstr), ((void *)0)) == -1) {
  syslog(3, "%s", errstr);
  exit(1);
 }
 if (conf.daemonize) {
  if (ezd_daemon(10, errstr, sizeof(errstr)) == -1) {
   syslog(3, "%s", errstr);
   exit(1);
  }
  if (ezd_pidfile_open(conf.pid_file_path,
         errstr, sizeof(errstr)) == -1) {
   syslog(3, "%s", errstr);
   exit(1);
  }
 }
 if (conf.fconf.format) {
  if (conf.fconf.encoding != ENC_STRING) {
   syslog(3, "Output formatting only supported for " "output.encoding = string");
   if (conf.daemonize)
    ezd_pidfile_close();
   exit(1);
  }
  if (format_parse(&conf.fconf, conf.fconf.format,
     errstr, sizeof(errstr)) == -1) {
   syslog(3, "Failed to parse format string: %s\n%s", conf.fconf.format, errstr);
   if (conf.daemonize)
    ezd_pidfile_close();
   exit(1);
  }
 }
 if (conf.stats_interval) {
  char tmp[30];
  if (stats_open() == -1) {
   syslog(3, "Failed to open statistics log file %s: %s", conf.stats_file, strerror((*__errno_location ())));
   if (conf.daemonize)
    ezd_pidfile_close();
   exit(1);
  }
  snprintf(tmp, sizeof(tmp), "%i", conf.stats_interval*1000);
  rd_kafka_conf_set_stats_cb(conf.rk_conf, kafka_stats_cb);
  rd_kafka_conf_set(conf.rk_conf, "statistics.interval.ms", tmp,
      ((void *)0), 0);
 }
 if (!(conf.rk = rd_kafka_new(RD_KAFKA_CONSUMER, conf.rk_conf,
         errstr, sizeof(errstr)))) {
  syslog(3, "Failed to create kafka handle: %s", errstr);
  if (conf.daemonize)
   ezd_pidfile_close();
  exit(1);
 }
 rd_kafka_set_log_level(conf.rk, conf.log_level);
 exec_init();
 if (conf.daemonize)
  ezd_daemon_started();
 if (conf.cmd_init) {
  if ((r = system(conf.cmd_init) != 0))
   syslog(3, "\"command.init\" execution of \"%s\" failed " "with exit code %i", conf.cmd_init, r);
 }
 ezd_thread_sigmask(0, 0 , -1 );
 outputs_start();
 inputs_start();
 ezd_thread_sigmask(1, 1, 2, 15, -1);
 signal(1, sighup);
 signal(2, term);
 signal(15, term);
 syslog(6, "kafkatee starting");
 while (conf.run) {
  rd_kafka_poll(conf.rk, 1000);
  if (__builtin_expect((conf.rotate != our_rotate_version),0)) {
   our_rotate_version = conf.rotate;
   if (conf.stats_interval)
    stats_open();
  }
 }
 inputs_term();
 outputs_term();
 exec_term();
 rd_kafka_destroy(conf.rk);
 rd_kafka_wait_destroyed(5000);
 if (conf.stats_fp)
  stats_close();
 free(conf.stats_file);
 if (conf.cmd_term) {
  if ((r = system(conf.cmd_term) != 0))
   syslog(3, "\"command.term\" execution of \"%s\" failed " "with exit code %i", conf.cmd_term, r);
 }
 if (conf.daemonize)
  ezd_pidfile_close();
 syslog(6, "kafkatee exiting");
 exit(conf.exit_code);
}

canonical_original_code:
int main(int argc, char **argv) {
 char *conf_file_path = "/etc/kafkatee.conf";
 char errstr[512];
 char c;
 int r;
 static int our_rotate_version = 0;
 conf.pid_file_path = strdup("/run/kafkatee.pid");
 conf.run = 1;
 conf.exit_code = 0;
 conf.log_level = 6;
 conf.daemonize = 1;
 conf.stats_interval = 60;
 conf.stats_file = strdup("/tmp/kafkatee.stats.json");
 conf.input_buf_size = 1024 * 10;
 conf.output_delimiter = strdup("\n");
 conf.output_delimiter_len = strlen(conf.output_delimiter);
 conf.output_queue_size = 100000;
 conf.rk_conf = rd_kafka_conf_new();
 rd_kafka_conf_set(conf.rk_conf, "client.id", "kafkatee", ((void *)0), 0);
 rd_kafka_conf_set_error_cb(conf.rk_conf, kafka_error_cb);
 conf.rkt_conf = rd_kafka_topic_conf_new();
 while ((c = getopt(argc, argv, "hc:p:dDex")) != -1) {
  switch (c) {
  case 'h':
   usage(argv[0]);
   break;
  case 'c':
   conf_file_path = optarg;
   break;
  case 'p':
   conf.pid_file_path = strdup(optarg);
   break;
  case 'd':
   conf.log_level = 7;
   break;
  case 'D':
   conf.daemonize = 0;
   break;
  case 'e':
   conf.flags |= 0x1;
   break;
  case 'x':
   conf.flags |= 0x2;
   break;
  default:
   usage(argv[0]);
   break;
  }
 }
 openlog("kafkatee", 0x01|0x20, (3<<3));
 if (ezd_conf_file_read(conf_file_path, conf_set,
          errstr, sizeof(errstr), ((void *)0)) == -1) {
  syslog(3, "%s", errstr);
  exit(1);
 }
 if (conf.daemonize) {
  if (ezd_daemon(10, errstr, sizeof(errstr)) == -1) {
   syslog(3, "%s", errstr);
   exit(1);
  }
  if (ezd_pidfile_open(conf.pid_file_path,
         errstr, sizeof(errstr)) == -1) {
   syslog(3, "%s", errstr);
   exit(1);
  }
 }
 if (conf.fconf.format) {
  if (conf.fconf.encoding != ENC_STRING) {
   syslog(3, "Output formatting only supported for " "output.encoding = string");
   if (conf.daemonize)
    ezd_pidfile_close();
   exit(1);
  }
  if (format_parse(&conf.fconf, conf.fconf.format,
     errstr, sizeof(errstr)) == -1) {
   syslog(3, "Failed to parse format string: %s\n%s", conf.fconf.format, errstr);
   if (conf.daemonize)
    ezd_pidfile_close();
   exit(1);
  }
 }
 if (conf.stats_interval) {
  char tmp[30];
  if (stats_open() == -1) {
   syslog(3, "Failed to open statistics log file %s: %s", conf.stats_file, strerror((*__errno_location ())));
   if (conf.daemonize)
    ezd_pidfile_close();
   exit(1);
  }
  snprintf(tmp, sizeof(tmp), "%i", conf.stats_interval*1000);
  rd_kafka_conf_set_stats_cb(conf.rk_conf, kafka_stats_cb);
  rd_kafka_conf_set(conf.rk_conf, "statistics.interval.ms", tmp,
      ((void *)0), 0);
 }
 if (!(conf.rk = rd_kafka_new(RD_KAFKA_CONSUMER, conf.rk_conf,
         errstr, sizeof(errstr)))) {
  syslog(3, "Failed to create kafka handle: %s", errstr);
  if (conf.daemonize)
   ezd_pidfile_close();
  exit(1);
 }
 rd_kafka_set_log_level(conf.rk, conf.log_level);
 exec_init();
 if (conf.daemonize)
  ezd_daemon_started();
 if (conf.cmd_init) {
  if ((r = system(conf.cmd_init) != 0))
   syslog(3, "\"command.init\" execution of \"%s\" failed " "with exit code %i", conf.cmd_init, r);
 }
 ezd_thread_sigmask(0, 0 , -1 );
 outputs_start();
 inputs_start();
 ezd_thread_sigmask(1, 1, 2, 15, -1);
 signal(1, sighup);
 signal(2, term);
 signal(15, term);
 syslog(6, "kafkatee starting");
 while (conf.run) {
  rd_kafka_poll(conf.rk, 1000);
  if (__builtin_expect((conf.rotate != our_rotate_version),0)) {
   our_rotate_version = conf.rotate;
   if (conf.stats_interval)
    stats_open();
  }
 }
 inputs_term();
 outputs_term();
 exec_term();
 rd_kafka_destroy(conf.rk);
 rd_kafka_wait_destroyed(5000);
 if (conf.stats_fp)
  stats_close();
 free(conf.stats_file);
 if (conf.cmd_term) {
  if ((r = system(conf.cmd_term) != 0))
   syslog(3, "\"command.term\" execution of \"%s\" failed " "with exit code %i", conf.cmd_term, r);
 }
 if (conf.daemonize)
  ezd_pidfile_close();
 syslog(6, "kafkatee exiting");
 exit(conf.exit_code);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
errstr
char [512]

variable_types: c char

variable_types: r int

variable_types:
tmp
char [30]


decompiled_code:
void do_timeout()
{
run = 0;
fclose(stdin);
}

canonical_decompiled_code:
void func7()
{
run = 0;
fclose(stdin);
}

original_code:
static void do_timeout (int sig) {
 run = 0;
 fclose(stdin);
}

canonical_original_code:
static void do_timeout(int sig) {
 run = 0;
 fclose(stdin);
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // er12
int v5; // eax
unsigned int v6; // ebx
int v7; // ebp
char v8[4136]; // [rsp+0h] [rbp-1028h] BYREF

if ( argc <= 2 )
{
printf("Usage: %s <expected-number-of-lines-to-read> <timeout> [<sleep-time-after-expected-reached>]\n", *argv);
exit(1);
}
v3 = 5;
v4 = atoi(argv[1]);
v5 = atoi(argv[2]);
alarm(v5);
signal(14, do_timeout);
if ( argc == 4 )
v3 = atoi(argv[3]);
v6 = run;
if ( run )
{
v7 = 0;
v6 = 0;
while ( fgets(v8, 4095, stdin) )
{
if ( v4 <= (int)++v6 )
{
if ( v7 )
{
if ( time(0LL) >= v7 )
break;
}
else
{
v7 = v3 + time(0LL);
}
}
if ( !run )
break;
}
}
printf("%i lines read, %i expected\n", v6, (unsigned int)v4);
if ( v6 != v4 )
exit(2);
exit(0);
}

original_code:
int main (int argc, char **argv) {
 int cnt = 0;
 int expected;
 int sleeptime = 5;
 int endtime = 0;
 char buf[4096];
 if (argc < 3) {
  printf("Usage: %s <expected-number-of-lines-to-read> "
         " <timeout> "
         " [<sleep-time-after-expected-reached>]\n", argv[0]);
  exit(1);
 }
 expected = atoi(argv[1]);
 alarm(atoi(argv[2]));
 signal(14, do_timeout);
 if (argc == 4)
  sleeptime = atoi(argv[3]);
 while (run && fgets(buf, sizeof(buf)-1, stdin)) {
  cnt++;
  if (cnt >= expected) {
   if (!endtime)
    endtime = time(((void *)0)) + sleeptime;
   else if (time(((void *)0)) >= endtime)
    break;
  }
 }
 printf("%i lines read, %i expected\n", cnt, expected);
 if (cnt == expected)
  exit(0);
 else
  exit(2);
}

canonical_original_code:
int main(int argc, char **argv) {
 int cnt = 0;
 int expected;
 int sleeptime = 5;
 int endtime = 0;
 char buf[4096];
 if (argc < 3) {
  printf("Usage: %s <expected-number-of-lines-to-read> "
         " <timeout> "
         " [<sleep-time-after-expected-reached>]\n", argv[0]);
  exit(1);
 }
 expected = atoi(argv[1]);
 alarm(atoi(argv[2]));
 signal(14, do_timeout);
 if (argc == 4)
  sleeptime = atoi(argv[3]);
 while (run && fgets(buf, sizeof(buf)-1, stdin)) {
  cnt++;
  if (cnt >= expected) {
   if (!endtime)
    endtime = time(((void *)0)) + sleeptime;
   else if (time(((void *)0)) >= endtime)
    break;
  }
 }
 printf("%i lines read, %i expected\n", cnt, expected);
 if (cnt == expected)
  exit(0);
 else
  exit(2);
}

variable_types: cnt int

variable_types: expected int

variable_types: sleeptime int

variable_types: endtime int

variable_types:
buf
char [4096]


repo: willcast/nsboot

decompiled_code:
__int64 __fastcall ts_open(const char *a1)
{
int *v2; // rax
char *v3; // rax

ts_fd = open(a1, 0);
if ( ts_fd >= 0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "error opening touchscreen event device", v3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
int *v2; // rax
char *v3; // rax

ts_fd = open(a1, 0);
if ( ts_fd >= 0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "error opening touchscreen event device", v3);
return 1LL;
}

original_code:
int ts_open(char *dev_file) {
 ts_fd = open(dev_file, 00);
 if (ts_fd < 0) {
  logprintf("2%s: %s", "error opening touchscreen event device", strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall gpio_keys_open(const char *a1)
{
int *v2; // rax
char *v3; // rax

gpio_keys_fd = open(a1, 0);
if ( gpio_keys_fd >= 0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "error opening GPIO key event device", v3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
int *v2; // rax
char *v3; // rax

gpio_keys_fd = open(a1, 0);
if ( gpio_keys_fd >= 0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "error opening GPIO key event device", v3);
return 1LL;
}

original_code:
int gpio_keys_open(char *dev_file) {
 gpio_keys_fd = open(dev_file, 00);
 if (gpio_keys_fd < 0) {
  logprintf("2%s: %s", "error opening GPIO key event device", strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall pmic_keys_open(const char *a1)
{
int *v2; // rax
char *v3; // rax

pmic_keys_fd = open(a1, 1);
if ( pmic_keys_fd >= 0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "error opening PMIC key event device", v3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
int *v2; // rax
char *v3; // rax

pmic_keys_fd = open(a1, 1);
if ( pmic_keys_fd >= 0 )
return 0LL;
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "error opening PMIC key event device", v3);
return 1LL;
}

original_code:
int pmic_keys_open(char *dev_file) {
 pmic_keys_fd = open(dev_file, 01);
 if (pmic_keys_fd < 0) {
  logprintf("2%s: %s", "error opening PMIC key event device", strerror((*__errno_location ())));
  return 1;
 }
 return 0;
}


decompiled_code:
int __fastcall vibrator_open(const char *a1)
{
int result; // eax
int *v2; // rax
char *v3; // rax

result = open(a1, 1);
vibrator_fd = result;
if ( result < 0 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = logprintf("2%s: %s", "error opening vibrator sysfs file", v3);
}
else
{
vibrator_usable = 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(const char *a1)
{
int result; // eax
int *v2; // rax
char *v3; // rax

result = open(a1, 1);
vibrator_fd = result;
if ( result < 0 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = logprintf("2%s: %s", "error opening vibrator sysfs file", v3);
}
else
{
vibrator_usable = 1;
}
return result;
}

original_code:
void vibrator_open(char *dev_file) {
 vibrator_fd = open(dev_file, 01);
 if (vibrator_fd < 0)
  logprintf("2%s: %s", "error opening vibrator sysfs file", strerror((*__errno_location ())));
 else
  vibrator_usable = 1;
}


decompiled_code:
int ts_close()
{
int result; // eax

result = ts_usable;
if ( ts_usable )
{
result = close(ts_fd);
ts_usable = 0;
}
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

result = ts_usable;
if ( ts_usable )
{
result = close(ts_fd);
ts_usable = 0;
}
return result;
}

original_code:
void ts_close(void) {
 if (ts_usable) {
  close(ts_fd);
  ts_usable = 0;
 }
}

canonical_original_code:
void ts_close() {
 if (ts_usable) {
  close(ts_fd);
  ts_usable = 0;
 }
}


decompiled_code:
int gpio_keys_close()
{
int result; // eax

result = gpio_keys_usable;
if ( gpio_keys_usable )
{
result = close(gpio_keys_fd);
gpio_keys_usable = 0;
}
return result;
}

canonical_decompiled_code:
int func6()
{
int result; // eax

result = gpio_keys_usable;
if ( gpio_keys_usable )
{
result = close(gpio_keys_fd);
gpio_keys_usable = 0;
}
return result;
}

original_code:
void gpio_keys_close(void) {
 if (gpio_keys_usable) {
  close(gpio_keys_fd);
  gpio_keys_usable = 0;
 }
}

canonical_original_code:
void gpio_keys_close() {
 if (gpio_keys_usable) {
  close(gpio_keys_fd);
  gpio_keys_usable = 0;
 }
}


decompiled_code:
int pmic_keys_close()
{
int result; // eax

result = pmic_keys_usable;
if ( pmic_keys_usable )
{
result = close(pmic_keys_fd);
pmic_keys_usable = 0;
}
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax

result = pmic_keys_usable;
if ( pmic_keys_usable )
{
result = close(pmic_keys_fd);
pmic_keys_usable = 0;
}
return result;
}

original_code:
void pmic_keys_close(void) {
 if (pmic_keys_usable) {
  close(pmic_keys_fd);
  pmic_keys_usable = 0;
 }
}

canonical_original_code:
void pmic_keys_close() {
 if (pmic_keys_usable) {
  close(pmic_keys_fd);
  pmic_keys_usable = 0;
 }
}


decompiled_code:
__int64 input_close()
{
ts_close();
gpio_keys_close();
pmic_keys_close();
return qfprintf("/sys/devices/platform/cy8ctma395/vdd", "0");
}

canonical_decompiled_code:
__int64 func8()
{
func5();
func6();
func7();
return qfprintf("/sys/devices/platform/cy8ctma395/vdd", "0");
}

original_code:
void input_close(void) {
 ts_close();
 gpio_keys_close();
 pmic_keys_close();
 qfprintf("/sys/devices/platform/cy8ctma395/vdd", "0");
}

canonical_original_code:
void input_close() {
 ts_close();
 gpio_keys_close();
 pmic_keys_close();
 qfprintf("/sys/devices/platform/cy8ctma395/vdd", "0");
}


decompiled_code:
__int64 __fastcall input_open(const char *a1)
{
unsigned int v1; // ebx
char *v2; // rax
char *v3; // rbp
char *v4; // rax
char *v5; // rbp
char *v6; // rax
char *v7; // rbp
__int64 result; // rax
char v9[4120]; // [rsp+0h] [rbp-1018h] BYREF

v1 = 0;
while ( 1 )
{
++v1;
usleep(0x51615u);
logprintf("0locating touch screen (attempt %d)", v1);
v2 = name_to_event_dev("HPTouchpad");
v3 = v2;
if ( v2 )
break;
if ( v1 == 5 )
{
logprintf("3unable to detect touch screen!");
goto LABEL_5;
}
}
snprintf(v9, 0x1000uLL, "%s/%s", a1, v2);
free(v3);
logprintf("0trying to open touch screen at %s", v9);
if ( !(unsigned int)ts_open(v9) )
ts_usable = 1;
LABEL_5:
v4 = name_to_event_dev("gpio-keys");
v5 = v4;
if ( v4 )
{
snprintf(v9, 0x1000uLL, "%s/%s", a1, v4);
free(v5);
logprintf("0trying to open GPIO keys at %s", v9);
if ( !(unsigned int)gpio_keys_open(v9) )
gpio_keys_usable = 1;
}
else
{
logprintf("3unable to detect GPIO keys!");
}
v6 = name_to_event_dev("pmic8058_pwrkey");
v7 = v6;
if ( v6 )
{
snprintf(v9, 0x1000uLL, "%s/%s", a1, v6);
free(v7);
logprintf("0trying to open PMIC keys at %s", v9);
if ( !(unsigned int)pmic_keys_open(v9) )
pmic_keys_usable = 1;
}
else
{
logprintf("3unable to detect PMIC keys!");
}
if ( ts_usable || gpio_keys_usable && pmic_keys_usable )
{
result = 0LL;
last_input = 0LL;
*(&last_input + 1) = 0LL;
cur_input = 0LL;
*(&cur_input + 1) = 0LL;
diff_input = 0LL;
*(&diff_input + 1) = 0LL;
}
else
{
logprintf("3no usable input devices!");
input_close();
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
unsigned int v1; // ebx
char *v2; // rax
char *v3; // rbp
char *v4; // rax
char *v5; // rbp
char *v6; // rax
char *v7; // rbp
__int64 result; // rax
char v9[4120]; // [rsp+0h] [rbp-1018h] BYREF

v1 = 0;
while ( 1 )
{
++v1;
usleep(0x51615u);
logprintf("0locating touch screen (attempt %d)", v1);
v2 = func0("HPTouchpad");
v3 = v2;
if ( v2 )
break;
if ( v1 == 5 )
{
logprintf("3unable to detect touch screen!");
goto LABEL_5;
}
}
snprintf(v9, 0x1000uLL, "%s/%s", a1, v2);
free(v3);
logprintf("0trying to open touch screen at %s", v9);
if ( !(unsigned int)func1(v9) )
ts_usable = 1;
LABEL_5:
v4 = func0("gpio-keys");
v5 = v4;
if ( v4 )
{
snprintf(v9, 0x1000uLL, "%s/%s", a1, v4);
free(v5);
logprintf("0trying to open GPIO keys at %s", v9);
if ( !(unsigned int)func2(v9) )
gpio_keys_usable = 1;
}
else
{
logprintf("3unable to detect GPIO keys!");
}
v6 = func0("pmic8058_pwrkey");
v7 = v6;
if ( v6 )
{
snprintf(v9, 0x1000uLL, "%s/%s", a1, v6);
free(v7);
logprintf("0trying to open PMIC keys at %s", v9);
if ( !(unsigned int)func3(v9) )
pmic_keys_usable = 1;
}
else
{
logprintf("3unable to detect PMIC keys!");
}
if ( ts_usable || gpio_keys_usable && pmic_keys_usable )
{
result = 0LL;
last_input = 0LL;
*(&last_input + 1) = 0LL;
cur_input = 0LL;
*(&cur_input + 1) = 0LL;
diff_input = 0LL;
*(&diff_input + 1) = 0LL;
}
else
{
logprintf("3no usable input devices!");
func8();
result = 1LL;
}
return result;
}

original_code:
int input_open(char *uinput_dir) {
 char dev_path[4096];
 char *dev_name = ((void *)0);
 int attempt;
 for (attempt = 0; attempt < 5; ++attempt) {
  usleep(333333);
  logprintf("0locating touch screen (attempt %d)", attempt + 1);
  dev_name = name_to_event_dev("HPTouchpad");
  if (dev_name != ((void *)0))
   break;
 }
 if (attempt == 5) {
  logprintf("3unable to detect touch screen!");
 } else {
  snprintf(dev_path, sizeof(dev_path), "%s/%s", uinput_dir, dev_name);
  free(dev_name);
  logprintf("0trying to open touch screen at %s", dev_path);
  if (!ts_open(dev_path))
   ts_usable = 1;
 }
 dev_name = name_to_event_dev("gpio-keys");
 if (dev_name == ((void *)0)) {
  logprintf("3unable to detect GPIO keys!");
 } else {
  snprintf(dev_path, sizeof(dev_path), "%s/%s", uinput_dir, dev_name);
  free(dev_name);
  logprintf("0trying to open GPIO keys at %s", dev_path);
  if (!gpio_keys_open(dev_path))
   gpio_keys_usable = 1;
 }
 dev_name = name_to_event_dev("pmic8058_pwrkey");
 if (dev_name == ((void *)0)) {
  logprintf("3unable to detect PMIC keys!");
 } else {
  snprintf(dev_path, sizeof(dev_path), "%s/%s", uinput_dir, dev_name);
  free(dev_name);
  logprintf("0trying to open PMIC keys at %s", dev_path);
  if (!pmic_keys_open(dev_path))
   pmic_keys_usable = 1;
 }
 if (!ts_usable && !(gpio_keys_usable && pmic_keys_usable)) {
  logprintf("3no usable input devices!");
  input_close();
  return 1;
 }
 memset(&last_input, 0, sizeof(last_input));
 memset(&cur_input, 0, sizeof(last_input));
 memset(&diff_input, 0, sizeof(last_input));
 return 0;
}

variable_types: attempt int


decompiled_code:
int vibrator_close()
{
int result; // eax

result = vibrator_usable;
if ( vibrator_usable )
{
result = close(vibrator_fd);
vibrator_usable = 0;
}
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax

result = vibrator_usable;
if ( vibrator_usable )
{
result = close(vibrator_fd);
vibrator_usable = 0;
}
return result;
}

original_code:
void vibrator_close(void) {
 if (vibrator_usable) {
  close(vibrator_fd);
  vibrator_usable = 0;
 }
}

canonical_original_code:
void vibrator_close() {
 if (vibrator_usable) {
  close(vibrator_fd);
  vibrator_usable = 0;
 }
}


vibrate

decompiled_code:
ssize_t __fastcall vibrate(unsigned int a1)
{
ssize_t result; // rax
int *v2; // rax
char *v3; // rax
char s[16]; // [rsp+0h] [rbp-10h] BYREF

result = (unsigned int)vibrator_usable;
if ( vibrator_usable )
{
snprintf(s, 8uLL, "%d\n", a1);
result = write(vibrator_fd, s, 8uLL);
if ( result <= 1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = logprintf("2%s: %s", "can't write to vibrator", v3);
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func14(unsigned int a1)
{
ssize_t result; // rax
int *v2; // rax
char *v3; // rax
char s[16]; // [rsp+0h] [rbp-10h] BYREF

result = (unsigned int)vibrator_usable;
if ( vibrator_usable )
{
snprintf(s, 8uLL, "%d\n", a1);
result = write(vibrator_fd, s, 8uLL);
if ( result <= 1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = logprintf("2%s: %s", "can't write to vibrator", v3);
}
}
return result;
}

original_code:
void vibrate(int ms) {
 char str_ms[8];
 if (!vibrator_usable)
  return;
 snprintf(str_ms, sizeof(str_ms), "%d\n", ms);
 if (write(vibrator_fd, str_ms, 8) < 2)
  logprintf("2%s: %s", "can't write to vibrator", strerror((*__errno_location ())));
}

variable_types: ms int


decompiled_code:
unsigned int input_read()
{
int v0; // edi
int v1; // edx
int v2; // edx
int v3; // eax
unsigned int result; // eax
int v5; // ecx
_BOOL4 v6; // edx
int v7; // er10
_BOOL4 v8; // edx
int v9; // er8
_BOOL4 v10; // edx
int v11; // esi
int *v12; // rax
char *v13; // rax
struct timeval v14; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

memset(&readfds, 0, sizeof(readfds));
v0 = ts_usable;
last_input = (__int128)_mm_load_si128((const __m128i *)&cur_input);
*(&last_input + 1) = (__int128)_mm_load_si128((const __m128i *)&cur_input + 1);
if ( v0 )
{
v0 = ts_fd;
readfds.fds_bits[ts_fd / 64] |= 1LL << (ts_fd % 64);
if ( v0 < 0 )
v0 = 0;
}
if ( gpio_keys_usable )
{
v1 = gpio_keys_fd;
readfds.fds_bits[gpio_keys_fd / 64] |= 1LL << (gpio_keys_fd % 64);
if ( v0 < v1 )
v0 = v1;
}
if ( pmic_keys_usable )
{
v2 = pmic_keys_fd;
readfds.fds_bits[pmic_keys_fd / 64] |= 1LL << (pmic_keys_fd % 64);
if ( v0 < v2 )
v0 = v2;
}
v14.tv_sec = 0LL;
v14.tv_usec = 50000LL;
v3 = select(v0, &readfds, 0LL, 0LL, &v14);
if ( v3 )
{
if ( v3 == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
logprintf("2%s: %s", "select", v13);
return sleep(1u);
}
*((_DWORD *)&cur_input + 7) = 0;
}
else
{
*((_DWORD *)&cur_input + 7) = 1;
}
if ( (readfds.fds_bits[ts_fd / 64] & (1LL << (ts_fd % 64))) != 0 )
ts_read(
(unsigned int *)&cur_input,
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 4),
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 8));
if ( (readfds.fds_bits[gpio_keys_fd / 64] & (1LL << (gpio_keys_fd % 64))) != 0 )
gpio_keys_read(
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 12),
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 16),
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 20));
if ( (readfds.fds_bits[pmic_keys_fd / 64] & (1LL << (pmic_keys_fd % 64))) != 0 )
pmic_keys_read((unsigned int *)(unsigned int)((_DWORD)&cur_input + 24));
result = DWORD2(last_input);
if ( result )
result = DWORD2(cur_input) == 0;
v5 = HIDWORD(last_input);
DWORD2(diff_input) = result;
v6 = 0;
if ( !v5 )
v6 = HIDWORD(cur_input) != 0;
v7 = *((_DWORD *)&last_input + 4);
HIDWORD(diff_input) = v6;
v8 = 0;
if ( !v7 )
v8 = *((_DWORD *)&cur_input + 4) != 0;
v9 = *((_DWORD *)&last_input + 5);
*((_DWORD *)&diff_input + 4) = v8;
v10 = 0;
if ( !v9 )
v10 = *((_DWORD *)&cur_input + 5) != 0;
v11 = *((_DWORD *)&last_input + 6);
*((_DWORD *)&diff_input + 5) = v10;
if ( v11 )
{
*((_DWORD *)&diff_input + 6) = 0;
if ( !result )
return result;
return vibrate(0x1Eu);
}
*((_DWORD *)&diff_input + 6) = *((_DWORD *)&cur_input + 6) != 0;
if ( result )
return vibrate(0x1Eu);
return result;
}

canonical_decompiled_code:
unsigned int func15()
{
int v0; // edi
int v1; // edx
int v2; // edx
int v3; // eax
unsigned int result; // eax
int v5; // ecx
_BOOL4 v6; // edx
int v7; // er10
_BOOL4 v8; // edx
int v9; // er8
_BOOL4 v10; // edx
int v11; // esi
int *v12; // rax
char *v13; // rax
struct timeval v14; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

memset(&readfds, 0, sizeof(readfds));
v0 = ts_usable;
last_input = (__int128)_mm_load_si128((const __m128i *)&cur_input);
*(&last_input + 1) = (__int128)_mm_load_si128((const __m128i *)&cur_input + 1);
if ( v0 )
{
v0 = ts_fd;
readfds.fds_bits[ts_fd / 64] |= 1LL << (ts_fd % 64);
if ( v0 < 0 )
v0 = 0;
}
if ( gpio_keys_usable )
{
v1 = gpio_keys_fd;
readfds.fds_bits[gpio_keys_fd / 64] |= 1LL << (gpio_keys_fd % 64);
if ( v0 < v1 )
v0 = v1;
}
if ( pmic_keys_usable )
{
v2 = pmic_keys_fd;
readfds.fds_bits[pmic_keys_fd / 64] |= 1LL << (pmic_keys_fd % 64);
if ( v0 < v2 )
v0 = v2;
}
v14.tv_sec = 0LL;
v14.tv_usec = 50000LL;
v3 = select(v0, &readfds, 0LL, 0LL, &v14);
if ( v3 )
{
if ( v3 == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
logprintf("2%s: %s", "select", v13);
return sleep(1u);
}
*((_DWORD *)&cur_input + 7) = 0;
}
else
{
*((_DWORD *)&cur_input + 7) = 1;
}
if ( (readfds.fds_bits[ts_fd / 64] & (1LL << (ts_fd % 64))) != 0 )
func11(
(unsigned int *)&cur_input,
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 4),
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 8));
if ( (readfds.fds_bits[gpio_keys_fd / 64] & (1LL << (gpio_keys_fd % 64))) != 0 )
func12(
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 12),
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 16),
(unsigned int *)(unsigned int)((_DWORD)&cur_input + 20));
if ( (readfds.fds_bits[pmic_keys_fd / 64] & (1LL << (pmic_keys_fd % 64))) != 0 )
func13((unsigned int *)(unsigned int)((_DWORD)&cur_input + 24));
result = DWORD2(last_input);
if ( result )
result = DWORD2(cur_input) == 0;
v5 = HIDWORD(last_input);
DWORD2(diff_input) = result;
v6 = 0;
if ( !v5 )
v6 = HIDWORD(cur_input) != 0;
v7 = *((_DWORD *)&last_input + 4);
HIDWORD(diff_input) = v6;
v8 = 0;
if ( !v7 )
v8 = *((_DWORD *)&cur_input + 4) != 0;
v9 = *((_DWORD *)&last_input + 5);
*((_DWORD *)&diff_input + 4) = v8;
v10 = 0;
if ( !v9 )
v10 = *((_DWORD *)&cur_input + 5) != 0;
v11 = *((_DWORD *)&last_input + 6);
*((_DWORD *)&diff_input + 5) = v10;
if ( v11 )
{
*((_DWORD *)&diff_input + 6) = 0;
if ( !result )
return result;
return func14(0x1Eu);
}
*((_DWORD *)&diff_input + 6) = *((_DWORD *)&cur_input + 6) != 0;
if ( result )
return func14(0x1Eu);
return result;
}

original_code:
void input_read(void) {
 fd_set input_fds;
 struct timeval tv;
 int num_fds = 0;
 int status;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&input_fds)->fds_bits)[0]) : "memory"); } while (0);
 last_input = cur_input;
 if (ts_usable) {
  ((void) (((&input_fds)->fds_bits)[((ts_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts_fd) % (8 * (int) sizeof (__fd_mask)))))));
  num_fds = (((num_fds) > (ts_fd)) ? (num_fds) : (ts_fd));
 }
 if (gpio_keys_usable) {
  ((void) (((&input_fds)->fds_bits)[((gpio_keys_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((gpio_keys_fd) % (8 * (int) sizeof (__fd_mask)))))));
  num_fds = (((num_fds) > (gpio_keys_fd)) ? (num_fds) : (gpio_keys_fd));
 }
 if (pmic_keys_usable) {
  ((void) (((&input_fds)->fds_bits)[((pmic_keys_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pmic_keys_fd) % (8 * (int) sizeof (__fd_mask)))))));
  num_fds = (((num_fds) > (pmic_keys_fd)) ? (num_fds) : (pmic_keys_fd));
 }
 tv.tv_sec = 0;
 tv.tv_usec = 50000;
 status = select(num_fds, &input_fds, ((void *)0), ((void *)0), &tv);
 if (!status) {
  cur_input.timed_out = 1;
 } else if (status == -1) {
  logprintf("2%s: %s", "select", strerror((*__errno_location ())));
  sleep(1);
  return;
 } else {
  cur_input.timed_out = 0;
 }
 if (((((&input_fds)->fds_bits)[((ts_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  ts_read(&cur_input.ts_x, &cur_input.ts_y, &cur_input.touch);
 if (((((&input_fds)->fds_bits)[((gpio_keys_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((gpio_keys_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  gpio_keys_read(&cur_input.vol_up, &cur_input.vol_down,
   &cur_input.center);
 if (((((&input_fds)->fds_bits)[((pmic_keys_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pmic_keys_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  pmic_keys_read(&cur_input.power);
 diff_input.touch = last_input.touch && !cur_input.touch;
 diff_input.vol_up = !last_input.vol_up && cur_input.vol_up;
 diff_input.vol_down = !last_input.vol_down && cur_input.vol_down;
 diff_input.center = !last_input.center && cur_input.center;
 diff_input.power = !last_input.power && cur_input.power;
 if (diff_input.touch)
  vibrate(30);
}

canonical_original_code:
void input_read() {
 struct fd_set input_fds;
 struct timeval tv;
 int num_fds = 0;
 int status;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&input_fds)->fds_bits)[0]) : "memory"); } while (0);
 last_input = cur_input;
 if (ts_usable) {
  ((void) (((&input_fds)->fds_bits)[((ts_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts_fd) % (8 * (int) sizeof (__fd_mask)))))));
  num_fds = (((num_fds) > (ts_fd)) ? (num_fds) : (ts_fd));
 }
 if (gpio_keys_usable) {
  ((void) (((&input_fds)->fds_bits)[((gpio_keys_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((gpio_keys_fd) % (8 * (int) sizeof (__fd_mask)))))));
  num_fds = (((num_fds) > (gpio_keys_fd)) ? (num_fds) : (gpio_keys_fd));
 }
 if (pmic_keys_usable) {
  ((void) (((&input_fds)->fds_bits)[((pmic_keys_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pmic_keys_fd) % (8 * (int) sizeof (__fd_mask)))))));
  num_fds = (((num_fds) > (pmic_keys_fd)) ? (num_fds) : (pmic_keys_fd));
 }
 tv.tv_sec = 0;
 tv.tv_usec = 50000;
 status = select(num_fds, &input_fds, ((void *)0), ((void *)0), &tv);
 if (!status) {
  cur_input.timed_out = 1;
 } else if (status == -1) {
  logprintf("2%s: %s", "select", strerror((*__errno_location ())));
  sleep(1);
  return;
 } else {
  cur_input.timed_out = 0;
 }
 if (((((&input_fds)->fds_bits)[((ts_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  ts_read(&cur_input.ts_x, &cur_input.ts_y, &cur_input.touch);
 if (((((&input_fds)->fds_bits)[((gpio_keys_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((gpio_keys_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  gpio_keys_read(&cur_input.vol_up, &cur_input.vol_down,
   &cur_input.center);
 if (((((&input_fds)->fds_bits)[((pmic_keys_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pmic_keys_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  pmic_keys_read(&cur_input.power);
 diff_input.touch = last_input.touch && !cur_input.touch;
 diff_input.vol_up = !last_input.vol_up && cur_input.vol_up;
 diff_input.vol_down = !last_input.vol_down && cur_input.vol_down;
 diff_input.center = !last_input.center && cur_input.center;
 diff_input.power = !last_input.power && cur_input.power;
 if (diff_input.touch)
  vibrate(30);
}

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

variable_types: status int


decompiled_code:
void do_cleanup(void)
{
if ( (unsigned int)is_fb_available() )
fb_close();
display_wholelog();
}

canonical_decompiled_code:
void func0(void)
{
if ( (unsigned int)is_fb_available() )
fb_close();
display_wholelog();
}

original_code:
void do_cleanup(void) {
 if (is_fb_available()) fb_close();
 display_wholelog();
}

canonical_original_code:
void do_cleanup() {
 if (is_fb_available()) fb_close();
 display_wholelog();
}


decompiled_code:
void do_shutdown()
{
logprintf("0stopping tssrv");
system_logged("killall tssrv");
system_logged("killall adbd");
qfprintf("/sys/class/usb_composite/adb/enable", "0");
qfprintf("/sys/class/usb_composite/rndis/enable", "0");
logprintf("0unmounting base volumes");
umount_lv("media");
umount_lv("root");
logprintf("0unmounting base partitions");
logprintf("0 - /mnt/boot");
umount("/mnt/boot");
logprintf("0 - /mnt/firmware");
umount("/mnt/firmware");
logprintf("0disabling framebuffer");
if ( (unsigned int)is_fb_available() )
fb_close();
logprintf("0disabling input");
input_close();
logprintf("0unmounting all volumes");
chdir("/");
sync();
qfprintf("/proc/sysrq-trigger", "u");
logprintf("0syncing");
sync();
}

canonical_decompiled_code:
void func1()
{
logprintf("0stopping tssrv");
system_logged("killall tssrv");
system_logged("killall adbd");
qfprintf("/sys/class/usb_composite/adb/enable", "0");
qfprintf("/sys/class/usb_composite/rndis/enable", "0");
logprintf("0unmounting base volumes");
umount_lv("media");
umount_lv("root");
logprintf("0unmounting base partitions");
logprintf("0 - /mnt/boot");
umount("/mnt/boot");
logprintf("0 - /mnt/firmware");
umount("/mnt/firmware");
logprintf("0disabling framebuffer");
if ( (unsigned int)is_fb_available() )
fb_close();
logprintf("0disabling input");
input_close();
logprintf("0unmounting all volumes");
chdir("/");
sync();
qfprintf("/proc/sysrq-trigger", "u");
logprintf("0syncing");
sync();
}

original_code:
void do_shutdown(void) {
 logprintf("0stopping tssrv");
 system_logged("killall tssrv");
 system_logged("killall adbd");
 qfprintf("/sys/class/usb_composite/adb/enable", "0");
 qfprintf("/sys/class/usb_composite/rndis/enable", "0");
 logprintf("0unmounting base volumes");
 umount_lv("media");
 umount_lv("root");
 logprintf("0unmounting base partitions");
 logprintf("0 - /mnt/boot");
 umount("/mnt/boot");
 logprintf("0 - /mnt/firmware");
 umount("/mnt/firmware");
 logprintf("0disabling framebuffer");
 if (is_fb_available()) fb_close();
 logprintf("0disabling input");
 input_close();
 logprintf("0unmounting all volumes");
 (void)chdir("/");
 sync();
 qfprintf("/proc/sysrq-trigger", "u");
 logprintf("0syncing");
 sync();
}

canonical_original_code:
void do_shutdown() {
 logprintf("0stopping tssrv");
 system_logged("killall tssrv");
 system_logged("killall adbd");
 qfprintf("/sys/class/usb_composite/adb/enable", "0");
 qfprintf("/sys/class/usb_composite/rndis/enable", "0");
 logprintf("0unmounting base volumes");
 umount_lv("media");
 umount_lv("root");
 logprintf("0unmounting base partitions");
 logprintf("0 - /mnt/boot");
 umount("/mnt/boot");
 logprintf("0 - /mnt/firmware");
 umount("/mnt/firmware");
 logprintf("0disabling framebuffer");
 if (is_fb_available()) fb_close();
 logprintf("0disabling input");
 input_close();
 logprintf("0unmounting all volumes");
 (void)chdir("/");
 sync();
 qfprintf("/proc/sysrq-trigger", "u");
 logprintf("0syncing");
 sync();
}


decompiled_code:
int symlink_binaries()
{
int result; // eax
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax

if ( symlink("/mnt/root/usr/lib", "/usr/lib") == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
logprintf("2%s: %s", "symlink /usr/lib failed", v2);
}
if ( symlink("/mnt/root/lib", "/lib") == -1 )
{
v15 = _errno_location();
v16 = strerror(*v15);
logprintf("2%s: %s", "symlink /lib failed", v16);
}
unlink("/bin/resizefat");
if ( symlink("/mnt/root/bin/resizefat", "/bin/resizefat") == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
logprintf("2%s: %s", "symlink resizefat failed", v14);
}
unlink("/bin/mke2fs");
if ( symlink("/mnt/root/sbin/mke2fs.e2fsprogs", "/bin/mke2fs") == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
logprintf("2%s: %s", "symlink mke2fs failed", v12);
}
unlink("/bin/e2fsck");
if ( symlink("/mnt/root/sbin/e2fsck.e2fsprogs", "/bin/e2fsck") == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
logprintf("2%s: %s", "symlink e2fsck failed", v10);
}
unlink("/bin/resize2fs");
if ( symlink("/mnt/root/sbin/resize2fs", "/bin/resize2fs") == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
logprintf("2%s: %s", "symlink resize2fs failed", v8);
}
unlink("/bin/dosfsck");
if ( symlink("/mnt/root/usr/sbin/dosfsck", "/bin/dosfsck") == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
logprintf("2%s: %s", "symlink dosfsck failed", v6);
}
unlink("/bin/gzip");
result = symlink("/bin/pigz", "/bin/gzip");
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
result = logprintf("2%s: %s", "symlink gzip failed", v4);
}
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax

if ( symlink("/mnt/root/usr/lib", "/usr/lib") == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
logprintf("2%s: %s", "symlink /usr/lib failed", v2);
}
if ( symlink("/mnt/root/lib", "/lib") == -1 )
{
v15 = _errno_location();
v16 = strerror(*v15);
logprintf("2%s: %s", "symlink /lib failed", v16);
}
unlink("/bin/resizefat");
if ( symlink("/mnt/root/bin/resizefat", "/bin/resizefat") == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
logprintf("2%s: %s", "symlink resizefat failed", v14);
}
unlink("/bin/mke2fs");
if ( symlink("/mnt/root/sbin/mke2fs.e2fsprogs", "/bin/mke2fs") == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
logprintf("2%s: %s", "symlink mke2fs failed", v12);
}
unlink("/bin/e2fsck");
if ( symlink("/mnt/root/sbin/e2fsck.e2fsprogs", "/bin/e2fsck") == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
logprintf("2%s: %s", "symlink e2fsck failed", v10);
}
unlink("/bin/resize2fs");
if ( symlink("/mnt/root/sbin/resize2fs", "/bin/resize2fs") == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
logprintf("2%s: %s", "symlink resize2fs failed", v8);
}
unlink("/bin/dosfsck");
if ( symlink("/mnt/root/usr/sbin/dosfsck", "/bin/dosfsck") == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
logprintf("2%s: %s", "symlink dosfsck failed", v6);
}
unlink("/bin/gzip");
result = symlink("/bin/pigz", "/bin/gzip");
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
result = logprintf("2%s: %s", "symlink gzip failed", v4);
}
return result;
}

original_code:
void symlink_binaries(void) {
 if (symlink("/mnt/root/usr/lib", "/usr/lib") == -1)
  logprintf("2%s: %s", "symlink /usr/lib failed", strerror((*__errno_location ())));
 if (symlink("/mnt/root/lib", "/lib") == -1)
  logprintf("2%s: %s", "symlink /lib failed", strerror((*__errno_location ())));
 unlink("/bin/resizefat");
 if (symlink("/mnt/root/bin/resizefat", "/bin/resizefat") == -1)
  logprintf("2%s: %s", "symlink resizefat failed", strerror((*__errno_location ())));
 unlink("/bin/mke2fs");
 if (symlink("/mnt/root/sbin/mke2fs.e2fsprogs", "/bin/mke2fs") == -1)
  logprintf("2%s: %s", "symlink mke2fs failed", strerror((*__errno_location ())));
 unlink("/bin/e2fsck");
 if (symlink("/mnt/root/sbin/e2fsck.e2fsprogs", "/bin/e2fsck") == -1)
  logprintf("2%s: %s", "symlink e2fsck failed", strerror((*__errno_location ())));
 unlink("/bin/resize2fs");
 if (symlink("/mnt/root/sbin/resize2fs", "/bin/resize2fs") == -1)
  logprintf("2%s: %s", "symlink resize2fs failed", strerror((*__errno_location ())));
 unlink("/bin/dosfsck");
 if (symlink("/mnt/root/usr/sbin/dosfsck", "/bin/dosfsck") == -1)
  logprintf("2%s: %s", "symlink dosfsck failed", strerror((*__errno_location ())));
 unlink("/bin/gzip");
 if (symlink("/bin/pigz", "/bin/gzip") == -1)
  logprintf("2%s: %s", "symlink gzip failed", strerror((*__errno_location ())));
}

canonical_original_code:
void symlink_binaries() {
 if (symlink("/mnt/root/usr/lib", "/usr/lib") == -1)
  logprintf("2%s: %s", "symlink /usr/lib failed", strerror((*__errno_location ())));
 if (symlink("/mnt/root/lib", "/lib") == -1)
  logprintf("2%s: %s", "symlink /lib failed", strerror((*__errno_location ())));
 unlink("/bin/resizefat");
 if (symlink("/mnt/root/bin/resizefat", "/bin/resizefat") == -1)
  logprintf("2%s: %s", "symlink resizefat failed", strerror((*__errno_location ())));
 unlink("/bin/mke2fs");
 if (symlink("/mnt/root/sbin/mke2fs.e2fsprogs", "/bin/mke2fs") == -1)
  logprintf("2%s: %s", "symlink mke2fs failed", strerror((*__errno_location ())));
 unlink("/bin/e2fsck");
 if (symlink("/mnt/root/sbin/e2fsck.e2fsprogs", "/bin/e2fsck") == -1)
  logprintf("2%s: %s", "symlink e2fsck failed", strerror((*__errno_location ())));
 unlink("/bin/resize2fs");
 if (symlink("/mnt/root/sbin/resize2fs", "/bin/resize2fs") == -1)
  logprintf("2%s: %s", "symlink resize2fs failed", strerror((*__errno_location ())));
 unlink("/bin/dosfsck");
 if (symlink("/mnt/root/usr/sbin/dosfsck", "/bin/dosfsck") == -1)
  logprintf("2%s: %s", "symlink dosfsck failed", strerror((*__errno_location ())));
 unlink("/bin/gzip");
 if (symlink("/bin/pigz", "/bin/gzip") == -1)
  logprintf("2%s: %s", "symlink gzip failed", strerror((*__errno_location ())));
}


decompiled_code:
int rebuild_fs()
{
int result; // eax
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax

mkdir("/dev", 0x1EDu);
mkdir("/etc", 0x1EDu);
mkdir("/mnt", 0x1EDu);
mkdir("/mnt/boot", 0x1EDu);
mkdir("/mnt/firmware", 0x1EDu);
mkdir("/proc", 0x1EDu);
mkdir("/sys", 0x1EDu);
mkdir("/tmp", 0x1EDu);
mkdir("/usr", 0x1EDu);
mkdir("/var", 0x1EDu);
if ( symlink("/", "/system") == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
logprintf("2%s: %s", "symlink /system failed", v2);
}
if ( symlink("/bin", "/sbin") == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
logprintf("2%s: %s", "symlink /sbin failed", v8);
}
if ( symlink("/bin", "/usr/bin") == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
logprintf("2%s: %s", "symlink /usr/bin failed", v6);
}
result = symlink("/sbin", "/usr/sbin");
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
result = logprintf("2%s: %s", "symlink /usr/sbin failed", v4);
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax

mkdir("/dev", 0x1EDu);
mkdir("/etc", 0x1EDu);
mkdir("/mnt", 0x1EDu);
mkdir("/mnt/boot", 0x1EDu);
mkdir("/mnt/firmware", 0x1EDu);
mkdir("/proc", 0x1EDu);
mkdir("/sys", 0x1EDu);
mkdir("/tmp", 0x1EDu);
mkdir("/usr", 0x1EDu);
mkdir("/var", 0x1EDu);
if ( symlink("/", "/system") == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
logprintf("2%s: %s", "symlink /system failed", v2);
}
if ( symlink("/bin", "/sbin") == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
logprintf("2%s: %s", "symlink /sbin failed", v8);
}
if ( symlink("/bin", "/usr/bin") == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
logprintf("2%s: %s", "symlink /usr/bin failed", v6);
}
result = symlink("/sbin", "/usr/sbin");
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
result = logprintf("2%s: %s", "symlink /usr/sbin failed", v4);
}
return result;
}

original_code:
void rebuild_fs(void) {
 mkdir("/dev", 0755);
 mkdir("/etc", 0755);
 mkdir("/mnt", 0755);
 mkdir("/mnt/boot", 0755);
 mkdir("/mnt/firmware", 0755);
 mkdir("/proc", 0755);
 mkdir("/sys", 0755);
 mkdir("/tmp", 0755);
 mkdir("/usr", 0755);
 mkdir("/var", 0755);
 if (symlink("/", "/system") == -1)
  logprintf("2%s: %s", "symlink /system failed", strerror((*__errno_location ())));
 if (symlink("/bin", "/sbin") == -1)
  logprintf("2%s: %s", "symlink /sbin failed", strerror((*__errno_location ())));
 if (symlink("/bin", "/usr/bin") == -1)
  logprintf("2%s: %s", "symlink /usr/bin failed", strerror((*__errno_location ())));
 if (symlink("/sbin", "/usr/sbin") == -1)
  logprintf("2%s: %s", "symlink /usr/sbin failed", strerror((*__errno_location ())));
}

canonical_original_code:
void rebuild_fs() {
 mkdir("/dev", 0755);
 mkdir("/etc", 0755);
 mkdir("/mnt", 0755);
 mkdir("/mnt/boot", 0755);
 mkdir("/mnt/firmware", 0755);
 mkdir("/proc", 0755);
 mkdir("/sys", 0755);
 mkdir("/tmp", 0755);
 mkdir("/usr", 0755);
 mkdir("/var", 0755);
 if (symlink("/", "/system") == -1)
  logprintf("2%s: %s", "symlink /system failed", strerror((*__errno_location ())));
 if (symlink("/bin", "/sbin") == -1)
  logprintf("2%s: %s", "symlink /sbin failed", strerror((*__errno_location ())));
 if (symlink("/bin", "/usr/bin") == -1)
  logprintf("2%s: %s", "symlink /usr/bin failed", strerror((*__errno_location ())));
 if (symlink("/sbin", "/usr/sbin") == -1)
  logprintf("2%s: %s", "symlink /usr/sbin failed", strerror((*__errno_location ())));
}


decompiled_code:
int enable_coredumps()
{
struct rlimit v1; // [rsp+0h] [rbp-18h] BYREF

v1.rlim_cur = -1LL;
v1.rlim_max = -1LL;
qfprintf("/proc/sys/kernel/core_pattern", "/var/core-%%e");
return setrlimit(RLIMIT_CORE, &v1);
}

canonical_decompiled_code:
int func4()
{
struct rlimit v1; // [rsp+0h] [rbp-18h] BYREF

v1.rlim_cur = -1LL;
v1.rlim_max = -1LL;
qfprintf("/proc/sys/kernel/core_pattern", "/var/core-%%e");
return setrlimit(RLIMIT_CORE, &v1);
}

original_code:
void enable_coredumps(void) {
 struct rlimit unlim;
 unlim.rlim_cur = ((__rlim_t) -1);
 unlim.rlim_max = ((__rlim_t) -1);
 qfprintf("/proc/sys/kernel/core_pattern", "/var/core-%%e");
 setrlimit(RLIMIT_CORE, &unlim);
}

canonical_original_code:
void enable_coredumps() {
 struct rlimit unlim;
 unlim.rlim_cur = ((__rlim_t) -1);
 unlim.rlim_max = ((__rlim_t) -1);
 qfprintf("/proc/sys/kernel/core_pattern", "/var/core-%%e");
 setrlimit(RLIMIT_CORE, &unlim);
}

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }


decompiled_code:
__int64 stop_adbd()
{
logprintf("0stopping adbd");
return system_logged("killall adbd");
}

canonical_decompiled_code:
__int64 func5()
{
logprintf("0stopping adbd");
return system_logged("killall adbd");
}

original_code:
void stop_adbd(void) {
 logprintf("0stopping adbd");
 system_logged("killall adbd");
}

canonical_original_code:
void stop_adbd() {
 logprintf("0stopping adbd");
 system_logged("killall adbd");
}


decompiled_code:
__int64 start_adbd()
{
logprintf("0starting adbd");
qfprintf("/sys/class/usb_composite/adb/enable", "1");
qfprintf("/sys/class/usb_composite/rndis/enable", "1");
chmod("/dev/android_adb", 0x1FFu);
return system_logged("adbd >/var/adbd.log 2>/var/adbd.err &");
}

canonical_decompiled_code:
__int64 func6()
{
logprintf("0starting adbd");
qfprintf("/sys/class/usb_composite/adb/enable", "1");
qfprintf("/sys/class/usb_composite/rndis/enable", "1");
chmod("/dev/android_adb", 0x1FFu);
return system_logged("adbd >/var/adbd.log 2>/var/adbd.err &");
}

original_code:
void start_adbd(void) {
 logprintf("0starting adbd");
 qfprintf("/sys/class/usb_composite/adb/enable", "1");
 qfprintf("/sys/class/usb_composite/rndis/enable", "1");
 chmod("/dev/android_adb", 0777);
 system_logged("adbd >/var/adbd.log 2>/var/adbd.err &");
}

canonical_original_code:
void start_adbd() {
 logprintf("0starting adbd");
 qfprintf("/sys/class/usb_composite/adb/enable", "1");
 qfprintf("/sys/class/usb_composite/rndis/enable", "1");
 chmod("/dev/android_adb", 0777);
 system_logged("adbd >/var/adbd.log 2>/var/adbd.err &");
}