decompiled_code:
__int64 __fastcall eval_op(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rbp
int *v4; // rsi
__int64 v5; // rdx
__int64 v6; // rbp
__int64 v7; // rbx
int v8; // er13
int *v9; // r12
int v10; // esi
__int64 result; // rax
int v12; // er12
int v13; // ebp
__int64 v14; // rdx
unsigned int v15; // er12
int v16; // ebx
int v17; // eax
int v18; // er13
__int64 v19; // rsi
__int64 v20; // rdx
__int64 v21; // rax
int v22; // edx
int v23; // esi
int v24; // ebx
__int64 v25; // rsi
__int64 v26; // rdx
int v27; // ecx
__int64 v28; // rsi
int v29; // edx
__int64 v30; // rsi
int v31; // edx
__int64 v32; // [rsp+0h] [rbp-48h] BYREF
int v33; // [rsp+8h] [rbp-40h]
void *ptr; // [rsp+10h] [rbp-38h]

v32 = 0LL;
v33 = 0;
ptr = 0LL;
if ( seq_stack_p <= 1 )
{
fwrite("not enough operands\n", 1uLL, 0x14uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v2 = (unsigned int)(a1 + 6);
if ( (unsigned int)a1 < 0xFFFFFFFA )
{
fwrite("unrecognized operation\n", 1uLL, 0x17uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
switch ( (int)a1 )
{
case -6:
pop_seq_stack(a1, a2, (unsigned int)seq_stack[6 * seq_stack_p - 6]);
v19 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(a1, v19, v20);
v21 = seq_stack_p;
v23 = v19 - v22;
v24 = seq_stack_p;
goto LABEL_14;
case -5:
v30 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(a1, v30, v2);
pop_seq_stack(a1, v30, (unsigned int)seq_stack[6 * seq_stack_p - 6]);
v21 = seq_stack_p;
v23 = v31 + v30;
v24 = seq_stack_p;
goto LABEL_14;
case -4:
if ( seq_stack[6 * seq_stack_p - 6] )
{
pop_seq_stack(a1, a2, v2);
v25 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(a1, v25, v26);
v24 = seq_stack_p;
v23 = (int)v25 / v27;
v21 = seq_stack_p;
LABEL_14:
addto_seq((__int64)&seq_stack[6 * v21], v23);
seq_stack_p = v24 + 1;
result = 0LL;
}
else
{
fwrite("attempted to divide by 0\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
break;
case -3:
v28 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(a1, v28, v2);
pop_seq_stack(a1, v28, (unsigned int)seq_stack[6 * seq_stack_p - 6]);
v21 = seq_stack_p;
v23 = v29 * v28;
v24 = seq_stack_p;
goto LABEL_14;
case -2:
v3 = seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(&v32, a2, v2);
v4 = &seq_stack[6 * seq_stack_p - 6];
if ( (int)copyn_seq((__int64)&v32, (__int64)v4, v3) < 0 )
return 0xFFFFFFFFLL;
pop_seq_stack(&v32, v4, v5);
if ( (int)v3 <= 0 )
{
v8 = seq_stack_p;
}
else
{
v6 = 4 * v3;
v7 = 0LL;
v8 = seq_stack_p;
v9 = &seq_stack[6 * seq_stack_p];
do
{
v10 = *(_DWORD *)((char *)ptr + v7);
v7 += 4LL;
addto_seq((__int64)v9, v10);
}
while ( v7 != v6 );
}
seq_stack_p = v8 + 1;
free(ptr);
return 0LL;
case -1:
v12 = seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(a1, a2, v2);
v13 = seq_stack[6 * seq_stack_p - 6];
pop_seq_stack(a1, a2, v14);
if ( v12 )
{
v15 = v12 + 1;
v16 = 0;
if ( v13 <= 0 )
{
v18 = seq_stack_p;
}
else
{
do
{
++v16;
v17 = random_range(1LL, v15);
v18 = seq_stack_p;
addto_seq((__int64)&seq_stack[6 * seq_stack_p], v17);
}
while ( v13 != v16 );
}
result = 0LL;
seq_stack_p = v18 + 1;
}
else
{
fwrite("cannot roll a zero-sided die\n", 1uLL, 0x1DuLL, stderr);
result = (unsigned int)a1;
}
return result;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rbp
int *v4; // rsi
__int64 v5; // rdx
__int64 v6; // rbp
__int64 v7; // rbx
int v8; // er13
int *v9; // r12
int v10; // esi
__int64 result; // rax
int v12; // er12
int v13; // ebp
__int64 v14; // rdx
unsigned int v15; // er12
int v16; // ebx
int v17; // eax
int v18; // er13
__int64 v19; // rsi
__int64 v20; // rdx
__int64 v21; // rax
int v22; // edx
int v23; // esi
int v24; // ebx
__int64 v25; // rsi
__int64 v26; // rdx
int v27; // ecx
__int64 v28; // rsi
int v29; // edx
__int64 v30; // rsi
int v31; // edx
__int64 v32; // [rsp+0h] [rbp-48h] BYREF
int v33; // [rsp+8h] [rbp-40h]
void *ptr; // [rsp+10h] [rbp-38h]

v32 = 0LL;
v33 = 0;
ptr = 0LL;
if ( seq_stack_p <= 1 )
{
fwrite("not enough operands\n", 1uLL, 0x14uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v2 = (unsigned int)(a1 + 6);
if ( (unsigned int)a1 < 0xFFFFFFFA )
{
fwrite("unrecognized operation\n", 1uLL, 0x17uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
switch ( (int)a1 )
{
case -6:
func1(a1, a2, (unsigned int)seq_stack[6 * seq_stack_p - 6]);
v19 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
func1(a1, v19, v20);
v21 = seq_stack_p;
v23 = v19 - v22;
v24 = seq_stack_p;
goto LABEL_14;
case -5:
v30 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
func1(a1, v30, v2);
func1(a1, v30, (unsigned int)seq_stack[6 * seq_stack_p - 6]);
v21 = seq_stack_p;
v23 = v31 + v30;
v24 = seq_stack_p;
goto LABEL_14;
case -4:
if ( seq_stack[6 * seq_stack_p - 6] )
{
func1(a1, a2, v2);
v25 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
func1(a1, v25, v26);
v24 = seq_stack_p;
v23 = (int)v25 / v27;
v21 = seq_stack_p;
LABEL_14:
func2((__int64)&seq_stack[6 * v21], v23);
seq_stack_p = v24 + 1;
result = 0LL;
}
else
{
fwrite("attempted to divide by 0\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
break;
case -3:
v28 = (unsigned int)seq_stack[6 * seq_stack_p - 6];
func1(a1, v28, v2);
func1(a1, v28, (unsigned int)seq_stack[6 * seq_stack_p - 6]);
v21 = seq_stack_p;
v23 = v29 * v28;
v24 = seq_stack_p;
goto LABEL_14;
case -2:
v3 = seq_stack[6 * seq_stack_p - 6];
func1(&v32, a2, v2);
v4 = &seq_stack[6 * seq_stack_p - 6];
if ( (int)func4((__int64)&v32, (__int64)v4, v3) < 0 )
return 0xFFFFFFFFLL;
func1(&v32, v4, v5);
if ( (int)v3 <= 0 )
{
v8 = seq_stack_p;
}
else
{
v6 = 4 * v3;
v7 = 0LL;
v8 = seq_stack_p;
v9 = &seq_stack[6 * seq_stack_p];
do
{
v10 = *(_DWORD *)((char *)ptr + v7);
v7 += 4LL;
func2((__int64)v9, v10);
}
while ( v7 != v6 );
}
seq_stack_p = v8 + 1;
free(ptr);
return 0LL;
case -1:
v12 = seq_stack[6 * seq_stack_p - 6];
func1(a1, a2, v2);
v13 = seq_stack[6 * seq_stack_p - 6];
func1(a1, a2, v14);
if ( v12 )
{
v15 = v12 + 1;
v16 = 0;
if ( v13 <= 0 )
{
v18 = seq_stack_p;
}
else
{
do
{
++v16;
v17 = random_range(1LL, v15);
v18 = seq_stack_p;
func2((__int64)&seq_stack[6 * seq_stack_p], v17);
}
while ( v13 != v16 );
}
result = 0LL;
seq_stack_p = v18 + 1;
}
else
{
fwrite("cannot roll a zero-sided die\n", 1uLL, 0x1DuLL, stderr);
result = (unsigned int)a1;
}
return result;
}
}
}
return result;
}

original_code:
static int
eval_op(int op)
{
 int i, n1, n2;
 seq_t *s1, s2;
 s2.sum = s2.cap = s2.size = 0;
 s2.vals = ((void *)0);
 if (seq_stack_p < 2) {
  fprintf(stderr, "not enough operands\n");
  return -1;
 }
 switch (op) {
 case -1:
  n2 = seq_stack[seq_stack_p - 1].sum;
  pop_seq_stack();
  n1 = seq_stack[seq_stack_p - 1].sum;
  pop_seq_stack();
  if (n2 == 0) {
   fprintf(stderr, "cannot roll a zero-sided die\n");
   return -1;
  }
  for (i = 0; i < n1; i++) {
   addto_seq(&seq_stack[seq_stack_p], random_range(1, n2+1));
  }
  seq_stack_p++;
  break;
 case -2:
  n1 = seq_stack[seq_stack_p - 1].sum;
  pop_seq_stack();
  s1 = &seq_stack[seq_stack_p - 1];
  if (copyn_seq(&s2, s1, n1) < 0) {
   return -1;
  }
  pop_seq_stack();
  for (i = 0; i < n1; i++) {
   addto_seq(&seq_stack[seq_stack_p], s2.vals[i]);
  }
  seq_stack_p++;
  free(s2.vals);
  break;
 case -3:
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 * n2); seq_stack_p++; } while (0);
  break;
 case -4:
  if (seq_stack[seq_stack_p - 1].sum == 0) {
   fprintf(stderr, "attempted to divide by 0\n");
   return -1;
  }
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 / n2); seq_stack_p++; } while (0);
  break;
 case -5:
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 + n2); seq_stack_p++; } while (0);
  break;
 case -6:
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 - n2); seq_stack_p++; } while (0);
  break;
 default:
  fprintf(stderr, "unrecognized operation\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
static int eval_op(int op)
{
 int i; int n1; int n2;
 struct seq *s1; struct seq s2;
 s2.sum = s2.cap = s2.size = 0;
 s2.vals = ((void *)0);
 if (seq_stack_p < 2) {
  fprintf(stderr, "not enough operands\n");
  return -1;
 }
 switch (op) {
 case -1:
  n2 = seq_stack[seq_stack_p - 1].sum;
  pop_seq_stack();
  n1 = seq_stack[seq_stack_p - 1].sum;
  pop_seq_stack();
  if (n2 == 0) {
   fprintf(stderr, "cannot roll a zero-sided die\n");
   return -1;
  }
  for (i = 0; i < n1; i++) {
   addto_seq(&seq_stack[seq_stack_p], random_range(1, n2+1));
  }
  seq_stack_p++;
  break;
 case -2:
  n1 = seq_stack[seq_stack_p - 1].sum;
  pop_seq_stack();
  s1 = &seq_stack[seq_stack_p - 1];
  if (copyn_seq(&s2, s1, n1) < 0) {
   return -1;
  }
  pop_seq_stack();
  for (i = 0; i < n1; i++) {
   addto_seq(&seq_stack[seq_stack_p], s2.vals[i]);
  }
  seq_stack_p++;
  free(s2.vals);
  break;
 case -3:
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 * n2); seq_stack_p++; } while (0);
  break;
 case -4:
  if (seq_stack[seq_stack_p - 1].sum == 0) {
   fprintf(stderr, "attempted to divide by 0\n");
   return -1;
  }
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 / n2); seq_stack_p++; } while (0);
  break;
 case -5:
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 + n2); seq_stack_p++; } while (0);
  break;
 case -6:
  do { n2 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); n1 = seq_stack[seq_stack_p - 1].sum; pop_seq_stack(); addto_seq(&seq_stack[seq_stack_p], n1 - n2); seq_stack_p++; } while (0);
  break;
 default:
  fprintf(stderr, "unrecognized operation\n");
  return -1;
 }
 return 0;
}

variable_types: op int

variable_types: struct seq { int sum; int cap; int size; int *vals; }


repo: ingramj/dice

decompiled_code:
void cleanup_evaluator()
{
clean_seq_stack();
op_stack_p = 0;
}

canonical_decompiled_code:
void func6()
{
func3();
op_stack_p = 0;
}

original_code:
void
cleanup_evaluator(void)
{
 clean_seq_stack();
 op_stack_p = 0;
}

return_type: void


evaluate

decompiled_code:
__int64 __fastcall evaluate(__int64 a1, __int64 a2)
{
int v2; // ebp
int v3; // ebx
int v4; // ebx
__int64 v5; // rdi
__int64 result; // rax
__int64 v7; // rdi
int v8; // ecx
__int64 v9; // rdi
__int64 v10; // rsi
int v11; // eax

v2 = get_token();
if ( (unsigned int)(v2 + 10) <= 1 )
{
LABEL_23:
while ( op_stack_p > 0 )
{
v9 = (unsigned int)op_stack[op_stack_p - 1];
if ( (unsigned int)(v9 + 8) <= 1 )
goto LABEL_15;
--op_stack_p;
if ( (int)eval_op(v9, a2) < 0 )
goto LABEL_16;
}
if ( seq_stack_p > 1 )
{
fprintf(stderr, "%d values left on the sequence stack; should be 1\n", (unsigned int)seq_stack_p);
result = 0xFFFFFFFFLL;
}
else if ( seq_stack_p )
{
v10 = (unsigned int)seq_stack[0];
printf("%d\n", (unsigned int)seq_stack[0]);
pop_seq_stack("%d\n", v10);
result = (unsigned int)(v2 + 9);
}
else
{
result = 0xFFFFFFFFLL;
}
return result;
}
while ( v2 >= 0 )
{
v3 = seq_stack_p;
if ( seq_stack_p > 1023 )
{
fprintf(stderr, "%s\n", "sequence stack overflow");
goto LABEL_16;
}
a2 = (unsigned int)v2;
addto_seq((__int64)&seq_stack[6 * seq_stack_p], v2);
seq_stack_p = v3 + 1;
LABEL_5:
v2 = get_token();
if ( (unsigned int)(v2 + 10) <= 1 )
goto LABEL_23;
}
if ( v2 >= -6 )
{
while ( 1 )
{
v8 = op_stack_p;
if ( op_stack_p <= 0 )
goto LABEL_20;
a2 = (unsigned int)(op_stack_p - 1);
v7 = (unsigned int)op_stack[(int)a2];
if ( precedence[~v2] > precedence[~op_stack[(int)a2]] )
break;
--op_stack_p;
if ( (int)eval_op(v7, a2) < 0 )
goto LABEL_16;
}
if ( op_stack_p <= 511 )
{
LABEL_20:
op_stack[op_stack_p] = v2;
op_stack_p = v8 + 1;
goto LABEL_5;
}
LABEL_30:
fprintf(stderr, "%s\n", "operator stack overflow");
goto LABEL_16;
}
if ( v2 == -7 )
{
v11 = op_stack_p;
if ( op_stack_p > 511 )
goto LABEL_30;
op_stack[op_stack_p] = -7;
op_stack_p = v11 + 1;
goto LABEL_5;
}
if ( v2 != -8 )
goto LABEL_5;
v4 = 0;
while ( 1 )
{
if ( op_stack_p <= 0 )
{
if ( v4 )
{
LABEL_15:
fprintf(stderr, "%s\n", "mismatched parentheses");
goto LABEL_16;
}
goto LABEL_33;
}
v5 = (unsigned int)op_stack[op_stack_p - 1];
if ( (_DWORD)v5 == -7 )
break;
++v4;
--op_stack_p;
if ( (int)eval_op(v5, a2) < 0 )
goto LABEL_16;
}
--op_stack_p;
if ( v4 )
goto LABEL_5;
LABEL_33:
fprintf(stderr, "%s\n", "missing operator");
LABEL_16:
cleanup_evaluator();
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
int v2; // ebp
int v3; // ebx
int v4; // ebx
__int64 v5; // rdi
__int64 result; // rax
__int64 v7; // rdi
int v8; // ecx
__int64 v9; // rdi
__int64 v10; // rsi
int v11; // eax

v2 = get_token();
if ( (unsigned int)(v2 + 10) <= 1 )
{
LABEL_23:
while ( op_stack_p > 0 )
{
v9 = (unsigned int)op_stack[op_stack_p - 1];
if ( (unsigned int)(v9 + 8) <= 1 )
goto LABEL_15;
--op_stack_p;
if ( (int)func5(v9, a2) < 0 )
goto LABEL_16;
}
if ( seq_stack_p > 1 )
{
fprintf(stderr, "%d values left on the sequence stack; should be 1\n", (unsigned int)seq_stack_p);
result = 0xFFFFFFFFLL;
}
else if ( seq_stack_p )
{
v10 = (unsigned int)seq_stack[0];
printf("%d\n", (unsigned int)seq_stack[0]);
func1("%d\n", v10);
result = (unsigned int)(v2 + 9);
}
else
{
result = 0xFFFFFFFFLL;
}
return result;
}
while ( v2 >= 0 )
{
v3 = seq_stack_p;
if ( seq_stack_p > 1023 )
{
fprintf(stderr, "%s\n", "sequence stack overflow");
goto LABEL_16;
}
a2 = (unsigned int)v2;
func2((__int64)&seq_stack[6 * seq_stack_p], v2);
seq_stack_p = v3 + 1;
LABEL_5:
v2 = get_token();
if ( (unsigned int)(v2 + 10) <= 1 )
goto LABEL_23;
}
if ( v2 >= -6 )
{
while ( 1 )
{
v8 = op_stack_p;
if ( op_stack_p <= 0 )
goto LABEL_20;
a2 = (unsigned int)(op_stack_p - 1);
v7 = (unsigned int)op_stack[(int)a2];
if ( precedence[~v2] > precedence[~op_stack[(int)a2]] )
break;
--op_stack_p;
if ( (int)func5(v7, a2) < 0 )
goto LABEL_16;
}
if ( op_stack_p <= 511 )
{
LABEL_20:
op_stack[op_stack_p] = v2;
op_stack_p = v8 + 1;
goto LABEL_5;
}
LABEL_30:
fprintf(stderr, "%s\n", "operator stack overflow");
goto LABEL_16;
}
if ( v2 == -7 )
{
v11 = op_stack_p;
if ( op_stack_p > 511 )
goto LABEL_30;
op_stack[op_stack_p] = -7;
op_stack_p = v11 + 1;
goto LABEL_5;
}
if ( v2 != -8 )
goto LABEL_5;
v4 = 0;
while ( 1 )
{
if ( op_stack_p <= 0 )
{
if ( v4 )
{
LABEL_15:
fprintf(stderr, "%s\n", "mismatched parentheses");
goto LABEL_16;
}
goto LABEL_33;
}
v5 = (unsigned int)op_stack[op_stack_p - 1];
if ( (_DWORD)v5 == -7 )
break;
++v4;
--op_stack_p;
if ( (int)func5(v5, a2) < 0 )
goto LABEL_16;
}
--op_stack_p;
if ( v4 )
goto LABEL_5;
LABEL_33:
fprintf(stderr, "%s\n", "missing operator");
LABEL_16:
func6();
return 1LL;
}

original_code:
int
evaluate(void)
{
 int tok;
 tok = get_token();
 while (tok != -9 && tok != -10) {
  if (tok >= 0) {
   do { if (!(seq_stack_p < 1024)) { fprintf(stderr, "%s\n", "sequence stack overflow"); goto error; }} while (0);
   addto_seq(&seq_stack[seq_stack_p], tok);
   seq_stack_p++;
  } else if (tok >= -6) {
   while (op_stack_p > 0) {
    int cur_prec = precedence[-tok - 1];
    int top_op = op_stack[op_stack_p - 1];
    int top_prec = precedence[-top_op - 1];
    if (top_prec < cur_prec) {
     break;
    }
    op_stack_p--;
    if (eval_op(top_op) < 0) {
     goto error;
    }
   }
   do { if (!(op_stack_p < 512)) { fprintf(stderr, "%s\n", "operator stack overflow"); goto error; }} while (0);
   op_stack[op_stack_p++] = tok;
  } else if (tok == -7) {
   do { if (!(op_stack_p < 512)) { fprintf(stderr, "%s\n", "operator stack overflow"); goto error; }} while (0);
   op_stack[op_stack_p++] = tok;
  } else if (tok == -8) {
   int matched = 0;
   int op_count = 0;
   while (op_stack_p > 0) {
    int top_op = op_stack[op_stack_p - 1];
    if (top_op == -7) {
     matched = 1;
     op_stack_p--;
     break;
    }
    op_count++;
    op_stack_p--;
    if (eval_op(top_op) < 0) {
     goto error;
    }
   }
   do { if (!(op_count > 0)) { fprintf(stderr, "%s\n", "missing operator"); goto error; }} while (0);
   do { if (!(matched)) { fprintf(stderr, "%s\n", "mismatched parentheses"); goto error; }} while (0);
  }
  tok = get_token();
 }
 while (op_stack_p > 0) {
  int top_op = op_stack[op_stack_p - 1];
  do { if (!(top_op != -7 && top_op != -8)) { fprintf(stderr, "%s\n", "mismatched parentheses"); goto error; }} while (0);
  op_stack_p--;
  if (eval_op(top_op) < 0) {
   goto error;
  }
 }
 if (seq_stack_p > 1) {
  fprintf(stderr, "%d values left on the sequence stack; should be 1\n",
   seq_stack_p);
  return -1;
 } else if (seq_stack_p == 0) {
  return -1;
 }
 printf("%d\n", seq_stack[0].sum);
 pop_seq_stack();
 if (tok == -9) {
  return 0;
 }
 return -1;
error:
 cleanup_evaluator();
 return 1;
}

canonical_original_code:
int evaluate()
{
 int tok;
 tok = get_token();
 while (tok != -9 && tok != -10) {
  if (tok >= 0) {
   do { if (!(seq_stack_p < 1024)) { fprintf(stderr, "%s\n", "sequence stack overflow"); goto error; }} while (0);
   addto_seq(&seq_stack[seq_stack_p], tok);
   seq_stack_p++;
  } else if (tok >= -6) {
   while (op_stack_p > 0) {
    int cur_prec = precedence[-tok - 1];
    int top_op = op_stack[op_stack_p - 1];
    int top_prec = precedence[-top_op - 1];
    if (top_prec < cur_prec) {
     break;
    }
    op_stack_p--;
    if (eval_op(top_op) < 0) {
     goto error;
    }
   }
   do { if (!(op_stack_p < 512)) { fprintf(stderr, "%s\n", "operator stack overflow"); goto error; }} while (0);
   op_stack[op_stack_p++] = tok;
  } else if (tok == -7) {
   do { if (!(op_stack_p < 512)) { fprintf(stderr, "%s\n", "operator stack overflow"); goto error; }} while (0);
   op_stack[op_stack_p++] = tok;
  } else if (tok == -8) {
   int matched = 0;
   int op_count = 0;
   while (op_stack_p > 0) {
    int top_op = op_stack[op_stack_p - 1];
    if (top_op == -7) {
     matched = 1;
     op_stack_p--;
     break;
    }
    op_count++;
    op_stack_p--;
    if (eval_op(top_op) < 0) {
     goto error;
    }
   }
   do { if (!(op_count > 0)) { fprintf(stderr, "%s\n", "missing operator"); goto error; }} while (0);
   do { if (!(matched)) { fprintf(stderr, "%s\n", "mismatched parentheses"); goto error; }} while (0);
  }
  tok = get_token();
 }
 while (op_stack_p > 0) {
  int top_op = op_stack[op_stack_p - 1];
  do { if (!(top_op != -7 && top_op != -8)) { fprintf(stderr, "%s\n", "mismatched parentheses"); goto error; }} while (0);
  op_stack_p--;
  if (eval_op(top_op) < 0) {
   goto error;
  }
 }
 if (seq_stack_p > 1) {
  fprintf(stderr, "%d values left on the sequence stack; should be 1\n",
   seq_stack_p);
  return -1;
 } else if (seq_stack_p == 0) {
  return -1;
 }
 printf("%d\n", seq_stack[0].sum);
 pop_seq_stack();
 if (tok == -9) {
  return 0;
 }
 return -1;
error:
 cleanup_evaluator();
 return 1;
}

variable_types: tok int

variable_types: matched int

return_type: int


digit

decompiled_code:
_BOOL8 __fastcall digit(int a1)
{
return (unsigned int)(a1 - 48) <= 9;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int a1)
{
return (unsigned int)(a1 - 48) <= 9;
}

variable_types: c int


decompiled_code:
int __fastcall read_int(char a1)
{
__int64 v1; // rbx
int v2; // eax
int v3; // er8
int v4; // eax
char nptr[35]; // [rsp+5h] [rbp-23h] BYREF

v1 = 1LL;
nptr[0] = a1;
nptr[10] = 0;
do
{
v2 = getc(stdin);
if ( !digit(v2) )
{
nptr[(int)v1] = 0;
goto LABEL_5;
}
nptr[v1++] = v3;
}
while ( v1 != 10 );
v4 = getc(stdin);
if ( !digit(v4) )
{
LABEL_5:
ungetc(v3, stdin);
return atoi(nptr);
}
fwrite("integer constant has too many digits\n", 1uLL, 0x25uLL, stderr);
return -10;
}

canonical_decompiled_code:
int __fastcall func1(char a1)
{
__int64 v1; // rbx
int v2; // eax
int v3; // er8
int v4; // eax
char nptr[35]; // [rsp+5h] [rbp-23h] BYREF

v1 = 1LL;
nptr[0] = a1;
nptr[10] = 0;
do
{
v2 = getc(stdin);
if ( !func0(v2) )
{
nptr[(int)v1] = 0;
goto LABEL_5;
}
nptr[v1++] = v3;
}
while ( v1 != 10 );
v4 = getc(stdin);
if ( !func0(v4) )
{
LABEL_5:
ungetc(v3, stdin);
return atoi(nptr);
}
fwrite("integer constant has too many digits\n", 1uLL, 0x25uLL, stderr);
return -10;
}

original_code:
static int
read_int(char first)
{
 char buf[11];
 int peek, i;
 buf[0] = first;
 buf[10] = '\0';
 for (i = 1; i < 10; i++) {
  peek = getc(stdin);
  if (digit(peek)) {
   buf[i] = peek;
  } else {
   buf[i] = '\0';
   break;
  }
 }
 if (i == 10) {
  peek = getc(stdin);
  if (digit(peek)) {
   fprintf(stderr, "integer constant has too many digits\n");
   return -10;
  }
 }
 ungetc(peek, stdin);
 return atoi(buf);
}

canonical_original_code:
static int read_int(char first)
{
 char buf[11];
 int peek; int i;
 buf[0] = first;
 buf[10] = '\0';
 for (i = 1; i < 10; i++) {
  peek = getc(stdin);
  if (digit(peek)) {
   buf[i] = peek;
  } else {
   buf[i] = '\0';
   break;
  }
 }
 if (i == 10) {
  peek = getc(stdin);
  if (digit(peek)) {
   fprintf(stderr, "integer constant has too many digits\n");
   return -10;
  }
 }
 ungetc(peek, stdin);
 return atoi(buf);
}

variable_types: first char

variable_types:
buf
char [11]

variable_types: peek int

variable_types: i int


decompiled_code:
int get_token()
{
int v0; // eax
int result; // eax

do
{
do
v0 = getc(stdin);
while ( v0 == 32 );
}
while ( v0 == 9 );
switch ( v0 )
{
case -1:
goto LABEL_5;
case 10:
result = -9;
break;
case 37:
result = 100;
break;
case 40:
result = -7;
break;
case 41:
result = -8;
break;
case 42:
result = -3;
break;
case 43:
result = -5;
break;
case 45:
result = -6;
break;
case 47:
result = -4;
break;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
result = read_int(v0);
break;
case 68:
case 100:
result = -1;
break;
case 75:
case 107:
result = -2;
break;
default:
fwrite("unrecognized character\n", 1uLL, 0x17uLL, stderr);
LABEL_5:
result = -10;
break;
}
return result;
}

canonical_decompiled_code:
int func2()
{
int v0; // eax
int result; // eax

do
{
do
v0 = getc(stdin);
while ( v0 == 32 );
}
while ( v0 == 9 );
switch ( v0 )
{
case -1:
goto LABEL_5;
case 10:
result = -9;
break;
case 37:
result = 100;
break;
case 40:
result = -7;
break;
case 41:
result = -8;
break;
case 42:
result = -3;
break;
case 43:
result = -5;
break;
case 45:
result = -6;
break;
case 47:
result = -4;
break;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
result = func1(v0);
break;
case 68:
case 100:
result = -1;
break;
case 75:
case 107:
result = -2;
break;
default:
fwrite("unrecognized character\n", 1uLL, 0x17uLL, stderr);
LABEL_5:
result = -10;
break;
}
return result;
}

original_code:
int
get_token(void)
{
 int peek;
 int tok = -10;
 do {
  peek = getc(stdin);
 } while (peek == ' ' || peek == '\t');
 switch (peek) {
 case '0': case '1': case '2': case '3': case '4':
 case '5': case '6': case '7': case '8': case '9':
  tok = read_int(peek);
  break;
 case '%':
  tok = 100;
  break;
 case 'd': case 'D':
  tok = -1;
  break;
 case 'k': case 'K':
  tok = -2;
  break;
 case '*':
  tok = -3;
  break;
 case '/':
  tok = -4;
  break;
 case '+':
  tok = -5;
  break;
 case '-':
  tok = -6;
  break;
 case '(':
  tok = -7;
  break;
 case ')':
  tok = -8;
  break;
 case '\n':
  tok = -9;
  break;
 case (-1):
  tok = -10;
  break;
 default:
  fprintf(stderr, "unrecognized character\n");
 }
 return tok;
}

canonical_original_code:
int get_token()
{
 int peek;
 int tok = -10;
 do {
  peek = getc(stdin);
 } while (peek == ' ' || peek == '\t');
 switch (peek) {
 case '0': case '1': case '2': case '3': case '4':
 case '5': case '6': case '7': case '8': case '9':
  tok = read_int(peek);
  break;
 case '%':
  tok = 100;
  break;
 case 'd': case 'D':
  tok = -1;
  break;
 case 'k': case 'K':
  tok = -2;
  break;
 case '*':
  tok = -3;
  break;
 case '/':
  tok = -4;
  break;
 case '+':
  tok = -5;
  break;
 case '-':
  tok = -6;
  break;
 case '(':
  tok = -7;
  break;
 case ')':
  tok = -8;
  break;
 case '\n':
  tok = -9;
  break;
 case (-1):
  tok = -10;
  break;
 default:
  fprintf(stderr, "unrecognized character\n");
 }
 return tok;
}


decompiled_code:
size_t print_banner()
{
return fwrite(
"Dice Roller v0.1.1\nCopyright (c) 2013 Jim Ingram <ingramj@gmail.com>\nctrl-d to quit\n",
1uLL,
0x54uLL,
stderr);
}

canonical_decompiled_code:
size_t func0()
{
return fwrite(
"Dice Roller v0.1.1\nCopyright (c) 2013 Jim Ingram <ingramj@gmail.com>\nctrl-d to quit\n",
1uLL,
0x54uLL,
stderr);
}

original_code:
static void print_banner(void)
{
 fprintf(stderr, "Dice Roller v0.1.1\n"
         "Copyright (c) 2013 Jim Ingram <ingramj@gmail.com>\n"
         "ctrl-d to quit\n");
}

canonical_original_code:
static void print_banner()
{
 fprintf(stderr, "Dice Roller v0.1.1\n"
         "Copyright (c) 2013 Jim Ingram <ingramj@gmail.com>\n"
         "ctrl-d to quit\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
print_banner();
seed_random(argc, argv);
do
fwrite("> ", 1uLL, 2uLL, stderr);
while ( (int)evaluate() >= 0 );
fputc(10, stderr);
cleanup_evaluator();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func0();
seed_random(argc, argv);
do
fwrite("> ", 1uLL, 2uLL, stderr);
while ( (int)evaluate() >= 0 );
fputc(10, stderr);
cleanup_evaluator();
return 0;
}

original_code:
int
main(void)
{
 print_banner();
 seed_random();
 fprintf(stderr, "> ");
 while (evaluate() >= 0) {
  fprintf(stderr, "> ");
 }
 fprintf(stderr, "\n");
 cleanup_evaluator();
 return 0;
}

canonical_original_code:
int main()
{
 print_banner();
 seed_random();
 fprintf(stderr, "> ");
 while (evaluate() >= 0) {
  fprintf(stderr, "> ");
 }
 fprintf(stderr, "\n");
 cleanup_evaluator();
 return 0;
}


decompiled_code:
void seed_random()
{
unsigned int v0; // ebp
__pid_t v1; // eax
unsigned int v2; // eax
unsigned int v3; // ebp

v0 = time(0LL);
v1 = getpid();
v2 = ((v1 ^ v0 ^ ((v1 ^ v0) << 19)) >> 11) ^ v1 ^ v0 ^ ((v1 ^ v0) << 19);
v3 = v2 ^ (v2 << 9);
fprintf(stderr, "seed = %u\n", v3);
srandom(v3);
}

canonical_decompiled_code:
void func0()
{
unsigned int v0; // ebp
__pid_t v1; // eax
unsigned int v2; // eax
unsigned int v3; // ebp

v0 = time(0LL);
v1 = getpid();
v2 = ((v1 ^ v0 ^ ((v1 ^ v0) << 19)) >> 11) ^ v1 ^ v0 ^ ((v1 ^ v0) << 19);
v3 = v2 ^ (v2 << 9);
fprintf(stderr, "seed = %u\n", v3);
srandom(v3);
}

original_code:
void
seed_random(void)
{
 unsigned int seed = time(((void *)0)) ^ getpid();
 seed ^= (seed << 19);
 seed ^= (seed >> 11);
 seed ^= (seed << 9);
 fprintf(stderr, "seed = %u\n", seed);
 srandom(seed);
}

canonical_original_code:
void seed_random()
{
 unsigned int seed = time(((void *)0)) ^ getpid();
 seed ^= (seed << 19);
 seed ^= (seed >> 11);
 seed ^= (seed << 9);
 fprintf(stderr, "seed = %u\n", seed);
 srandom(seed);
}

variable_types:
seed
unsigned int


decompiled_code:
__int64 __fastcall random_range(int a1, int a2)
{
__int64 result; // rax
int v3; // esi
int v4; // eax

if ( a1 == a2 )
return 0xFFFFFFFFLL;
result = (unsigned int)(a2 - 1);
if ( (_DWORD)result != a1 )
{
v3 = a2 - a1;
do
v4 = random();
while ( v4 > 0x7FFFFFFF - 0x7FFFFFFF % v3 );
result = (unsigned int)(a1 + v4 / (0x7FFFFFFF / v3));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 result; // rax
int v3; // esi
int v4; // eax

if ( a1 == a2 )
return 0xFFFFFFFFLL;
result = (unsigned int)(a2 - 1);
if ( (_DWORD)result != a1 )
{
v3 = a2 - a1;
do
v4 = random();
while ( v4 > 0x7FFFFFFF - 0x7FFFFFFF % v3 );
result = (unsigned int)(a1 + v4 / (0x7FFFFFFF / v3));
}
return result;
}

original_code:
int
random_range(int min, int max)
{
 int range, bucket, remainder, r;
 if (min == max) {
  return -1;
 } else if (min == max - 1) {
  return min;
 }
 range = max - min;
 bucket = 2147483647 / range;
 remainder = 2147483647 % range;
 while ((r = random()) > (2147483647 - remainder))
  ;
 return min + (r / bucket);
}

canonical_original_code:
int random_range(int min, int max)
{
 int range; int bucket; int remainder; int r;
 if (min == max) {
  return -1;
 } else if (min == max - 1) {
  return min;
 }
 range = max - min;
 bucket = 2147483647 / range;
 remainder = 2147483647 % range;
 while ((r = random()) > (2147483647 - remainder))
  ;
 return min + (r / bucket);
}

variable_types: min int

variable_types: max int

variable_types: range int

variable_types: bucket int

variable_types: remainder int

variable_types: r int


repo: innodrivers/mflib

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // eax
int v6; // ebp
char *v7; // rax
char *v8; // r12
const char *v9; // rsi
char *v10; // rbx
char *v11; // rax
const char *v12; // rsi
char *v13; // rax
char *v14; // rax
const char *v15; // rsi
char *v16; // rax
char *v17; // rax
const char *v18; // rsi
char *v19; // rax
char *v20; // rax

progname = (int)basename(*argv);
if ( argc <= 1 )
LABEL_51:
usage(1);
v3 = argv[1];
input_file = (__int64)v3;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "c:m:a:k:h");
if ( v4 != -1 )
{
switch ( v4 )
{
case 'a':
atag_addr = (char *)optarg;
continue;
case 'c':
cmdline = (char *)optarg;
continue;
case 'h':
usage(0);
case 'k':
kernel_addr = (char *)optarg;
continue;
case 'm':
mach_id = (char *)optarg;
continue;
default:
goto LABEL_51;
}
}
break;
}
if ( cmdline && (int)strlen(cmdline) > 256 )
{
fwrite("Command line string too long\n", 1uLL, 0x1DuLL, stderr);
return -1;
}
v5 = open(v3, 2);
v6 = v5;
if ( v5 >= 0 )
{
v7 = (char *)mmap(0LL, 0x4000uLL, 3, 1, v5, 0LL);
v8 = v7;
if ( v7 != (char *)-1LL )
{
v9 = cmdline;
if ( cmdline )
{
v10 = v7 + 0x4000;
while ( *(_QWORD *)v7 != 0x3A454E494C444D43LL )
{
v7 += 4;
if ( v7 == v10 )
{
fwrite("Command line marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_44;
}
}
*((_QWORD *)v7 + 1) = 0LL;
*((_QWORD *)v7 + 32) = 0LL;
memset(
(void *)((unsigned __int64)(v7 + 16) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)v7 + 8 - (((_DWORD)v7 + 16) & 0xFFFFFFF8) + 256) >> 3));
v11 = strcpy(v7 + 8, v9);
msync(v11, 0x100uLL, 6);
v12 = mach_id;
if ( !mach_id )
goto LABEL_47;
}
else
{
LABEL_44:
v12 = mach_id;
if ( !mach_id )
{
LABEL_47:
v15 = atag_addr;
if ( !atag_addr )
{
LABEL_41:
v18 = kernel_addr;
if ( !kernel_addr )
{
LABEL_35:
munmap(v8, 0x4000uLL);
goto LABEL_36;
}
v10 = v8 + 0x4000;
LABEL_29:
v19 = v8;
do
{
if ( *(_QWORD *)v19 == 0x44414C454E52454BLL && *((_WORD *)v19 + 4) == 21060 && v19[10] == 58 )
{
*(_OWORD *)(v19 + 11) = 0LL;
v20 = strcpy(v19 + 11, v18);
msync(v20, 0x10uLL, 6);
goto LABEL_35;
}
v19 += 4;
}
while ( v19 < v10 );
fwrite("Kernel Address marker not found!\n", 1uLL, 0x21uLL, stderr);
goto LABEL_35;
}
v10 = v8 + 0x4000;
LABEL_24:
v16 = v8;
while ( *(_QWORD *)v16 != 0x5244444147415441LL || v16[8] != 58 )
{
v16 += 4;
if ( v16 >= v10 )
{
fwrite("ATAG Address marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_41;
}
}
*(_OWORD *)(v16 + 9) = 0LL;
v17 = strcpy(v16 + 9, v15);
msync(v17, 0x10uLL, 6);
v18 = kernel_addr;
if ( !kernel_addr )
goto LABEL_35;
goto LABEL_29;
}
v10 = v8 + 0x4000;
}
v13 = v8;
while ( *(_DWORD *)v13 != 1212367181 || *((_WORD *)v13 + 2) != 17481 || v13[6] != 58 )
{
v13 += 4;
if ( v13 >= v10 )
{
fwrite("Machine ID marker not found!\n", 1uLL, 0x1DuLL, stderr);
goto LABEL_47;
}
}
*(_OWORD *)(v13 + 7) = 0LL;
v14 = strcpy(v13 + 7, v12);
msync(v14, 0x10uLL, 6);
v15 = atag_addr;
if ( !atag_addr )
goto LABEL_41;
goto LABEL_24;
}
}
fwrite("Could not open input image", 1uLL, 0x1AuLL, stderr);
LABEL_36:
if ( v6 > 0 )
close(v6);
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // eax
int v6; // ebp
char *v7; // rax
char *v8; // r12
const char *v9; // rsi
char *v10; // rbx
char *v11; // rax
const char *v12; // rsi
char *v13; // rax
char *v14; // rax
const char *v15; // rsi
char *v16; // rax
char *v17; // rax
const char *v18; // rsi
char *v19; // rax
char *v20; // rax

progname = (int)basename(*argv);
if ( argc <= 1 )
LABEL_51:
func7(1);
v3 = argv[1];
input_file = (__int64)v3;
while ( 2 )
{
v4 = getopt(argc, (char *const *)argv, "c:m:a:k:h");
if ( v4 != -1 )
{
switch ( v4 )
{
case 'a':
atag_addr = (char *)optarg;
continue;
case 'c':
cmdline = (char *)optarg;
continue;
case 'h':
func7(0);
case 'k':
kernel_addr = (char *)optarg;
continue;
case 'm':
mach_id = (char *)optarg;
continue;
default:
goto LABEL_51;
}
}
break;
}
if ( cmdline && (int)strlen(cmdline) > 256 )
{
fwrite("Command line string too long\n", 1uLL, 0x1DuLL, stderr);
return -1;
}
v5 = open(v3, 2);
v6 = v5;
if ( v5 >= 0 )
{
v7 = (char *)mmap(0LL, 0x4000uLL, 3, 1, v5, 0LL);
v8 = v7;
if ( v7 != (char *)-1LL )
{
v9 = cmdline;
if ( cmdline )
{
v10 = v7 + 0x4000;
while ( *(_QWORD *)v7 != 0x3A454E494C444D43LL )
{
v7 += 4;
if ( v7 == v10 )
{
fwrite("Command line marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_44;
}
}
*((_QWORD *)v7 + 1) = 0LL;
*((_QWORD *)v7 + 32) = 0LL;
memset(
(void *)((unsigned __int64)(v7 + 16) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)v7 + 8 - (((_DWORD)v7 + 16) & 0xFFFFFFF8) + 256) >> 3));
v11 = strcpy(v7 + 8, v9);
msync(v11, 0x100uLL, 6);
v12 = mach_id;
if ( !mach_id )
goto LABEL_47;
}
else
{
LABEL_44:
v12 = mach_id;
if ( !mach_id )
{
LABEL_47:
v15 = atag_addr;
if ( !atag_addr )
{
LABEL_41:
v18 = kernel_addr;
if ( !kernel_addr )
{
LABEL_35:
munmap(v8, 0x4000uLL);
goto LABEL_36;
}
v10 = v8 + 0x4000;
LABEL_29:
v19 = v8;
do
{
if ( *(_QWORD *)v19 == 0x44414C454E52454BLL && *((_WORD *)v19 + 4) == 21060 && v19[10] == 58 )
{
*(_OWORD *)(v19 + 11) = 0LL;
v20 = strcpy(v19 + 11, v18);
msync(v20, 0x10uLL, 6);
goto LABEL_35;
}
v19 += 4;
}
while ( v19 < v10 );
fwrite("Kernel Address marker not found!\n", 1uLL, 0x21uLL, stderr);
goto LABEL_35;
}
v10 = v8 + 0x4000;
LABEL_24:
v16 = v8;
while ( *(_QWORD *)v16 != 0x5244444147415441LL || v16[8] != 58 )
{
v16 += 4;
if ( v16 >= v10 )
{
fwrite("ATAG Address marker not found!\n", 1uLL, 0x1FuLL, stderr);
goto LABEL_41;
}
}
*(_OWORD *)(v16 + 9) = 0LL;
v17 = strcpy(v16 + 9, v15);
msync(v17, 0x10uLL, 6);
v18 = kernel_addr;
if ( !kernel_addr )
goto LABEL_35;
goto LABEL_29;
}
v10 = v8 + 0x4000;
}
v13 = v8;
while ( *(_DWORD *)v13 != 1212367181 || *((_WORD *)v13 + 2) != 17481 || v13[6] != 58 )
{
v13 += 4;
if ( v13 >= v10 )
{
fwrite("Machine ID marker not found!\n", 1uLL, 0x1DuLL, stderr);
goto LABEL_47;
}
}
*(_OWORD *)(v13 + 7) = 0LL;
v14 = strcpy(v13 + 7, v12);
msync(v14, 0x10uLL, 6);
v15 = atag_addr;
if ( !atag_addr )
goto LABEL_41;
goto LABEL_24;
}
}
fwrite("Could not open input image", 1uLL, 0x1AuLL, stderr);
LABEL_36:
if ( v6 > 0 )
close(v6);
return -1;
}

original_code:
int main(int argc, char **argv)
{
 int fd, found = 0, len, ret = -1;
 char *ptr, *p;
 progname = (char*)basename(argv[0]);
 if (argc < 2)
  usage(1);
 input_file = argv[1];
 while (1) {
  int c;
  c = getopt(argc, argv, "c:m:a:k:h");
  if (c == -1)
   break;
  switch (c) {
  case 'c':
   cmdline = optarg;
   break;
  case 'm':
   mach_id = optarg;
   break;
  case 'a':
   atag_addr = optarg;
   break;
  case 'k':
   kernel_addr = optarg;
   break;
  case 'h':
   usage(0);
   break;
  default:
   usage(1);
   break;
  }
 }
 if (cmdline != ((void *)0)) {
  len = strlen(cmdline);
  if (len > 256) {
   fprintf(stderr, "Command line string too long\n");
   goto err1;
  }
 }
 if (((fd = open(input_file, 02)) < 0) ||
  (ptr = (char *) mmap(0, (16 * 1024), 0x1|0x2, 0x01, fd, 0)) == (void *) (-1)) {
  fprintf(stderr, "Could not open input image");
  goto err2;
 }
 if (cmdline != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "CMDLINE:", 8) == 0) {
    found = 1;
    p += 8;
    break;
   }
  }
  if (found) {
   memset(p, 0, 256);
   strcpy(p, cmdline);
   msync(p, 256, 4 | 2);
  } else {
   fprintf(stderr, "Command line marker not found!\n");
  }
 }
 if (mach_id != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "MACHID:", 7) == 0) {
    found = 1;
    p += 7;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Machine ID marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, mach_id);
   msync(p, 16, 4 | 2);
  }
 }
 if (atag_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "ATAGADDR:", 9) == 0) {
    found = 1;
    p += 9;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "ATAG Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, atag_addr);
   msync(p, 16, 4 | 2);
  }
 }
 if (kernel_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "KERNELADDR:", 11) == 0) {
    found = 1;
    p += 11;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Kernel Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, kernel_addr);
   msync(p, 16, 4 | 2);
  }
 }
 munmap((void *) ptr, (16 * 1024));
err2:
 if (fd > 0)
  close(fd);
err1:
 return ret;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int fd; int found = 0; int len; int ret = -1;
 char *ptr; char *p;
 progname = (char *)basename(argv[0]);
 if (argc < 2)
  usage(1);
 input_file = argv[1];
 while (1) {
  int c;
  c = getopt(argc, argv, "c:m:a:k:h");
  if (c == -1)
   break;
  switch (c) {
  case 'c':
   cmdline = optarg;
   break;
  case 'm':
   mach_id = optarg;
   break;
  case 'a':
   atag_addr = optarg;
   break;
  case 'k':
   kernel_addr = optarg;
   break;
  case 'h':
   usage(0);
   break;
  default:
   usage(1);
   break;
  }
 }
 if (cmdline != ((void *)0)) {
  len = strlen(cmdline);
  if (len > 256) {
   fprintf(stderr, "Command line string too long\n");
   goto err1;
  }
 }
 if (((fd = open(input_file, 02)) < 0) ||
  (ptr = (char *) mmap(0, (16 * 1024), 0x1|0x2, 0x01, fd, 0)) == (void *) (-1)) {
  fprintf(stderr, "Could not open input image");
  goto err2;
 }
 if (cmdline != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "CMDLINE:", 8) == 0) {
    found = 1;
    p += 8;
    break;
   }
  }
  if (found) {
   memset(p, 0, 256);
   strcpy(p, cmdline);
   msync(p, 256, 4 | 2);
  } else {
   fprintf(stderr, "Command line marker not found!\n");
  }
 }
 if (mach_id != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "MACHID:", 7) == 0) {
    found = 1;
    p += 7;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Machine ID marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, mach_id);
   msync(p, 16, 4 | 2);
  }
 }
 if (atag_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "ATAGADDR:", 9) == 0) {
    found = 1;
    p += 9;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "ATAG Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, atag_addr);
   msync(p, 16, 4 | 2);
  }
 }
 if (kernel_addr != ((void *)0)) {
  found = 0;
  for (p = ptr; p < (ptr + (16 * 1024)); p += 4) {
   if (memcmp(p, "KERNELADDR:", 11) == 0) {
    found = 1;
    p += 11;
    break;
   }
  }
  if (!found) {
   fprintf(stderr, "Kernel Address marker not found!\n");
  } else {
   memset(p, 0, 16);
   strcpy(p, kernel_addr);
   msync(p, 16, 4 | 2);
  }
 }
 munmap((void *) ptr, (16 * 1024));
err2:
 if (fd > 0)
  close(fd);
err1:
 return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: fd int

variable_types: found int

variable_types: len int

variable_types: ret int

variable_types:
ptr
char *

variable_types:
p
char *


repo: inorindesu/mustached-octo-hipster

original_code:
int move_to(const FT_Vector* to, void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "MOVE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" M %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int move_to(struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "MOVE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" M %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}

variable_types:
to
struct FT_Vector_ { long x; long y; } *

variable_types:
user
void *

variable_types:
vec
struct VecData_ { int xLow; } *

user_defined_types:
struct FT_Vector_ { long x; long y; }
struct VecData_ { int xLow; }


original_code:
int line_to(const FT_Vector* to, void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "LINE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" L %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int line_to(struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "LINE_TO: (%d, %d)\n", to->x, vec->yHigh - to->y);
  printf(" L %d %d ", to->x, vec->yHigh - to->y);
  return 0;
}


original_code:
int conic_to(const FT_Vector* ctrl, const FT_Vector* to, void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "CONIC_TO: *(%d, %d) (%d, %d)\n", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  printf(" Q %d %d %d %d ", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int conic_to(struct FT_Vector_ *ctrl, struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "CONIC_TO: *(%d, %d) (%d, %d)\n", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  printf(" Q %d %d %d %d ", ctrl->x, vec->yHigh - ctrl->y, to->x, vec->yHigh - to->y);
  return 0;
}

variable_types:
ctrl
struct FT_Vector_ { long x; long y; } *

variable_types:
to
struct FT_Vector_ *


original_code:
int cubic_to(const FT_Vector* ctrl1, const FT_Vector* ctrl2, const FT_Vector* to,
             void* user)
{
  VecData* vec = (VecData*)user;
  fprintf(stderr, "CUBIC_TO: *(%d, %d)*(%d, %d) (%d, %d)\n", ctrl1->x, vec->yHigh - ctrl1->y
         , ctrl2->x, vec->yHigh - ctrl2->y, to->x, vec->yHigh - to->y);
  printf(" C %d %d %d %d ", ctrl1->x, vec->yHigh - ctrl1->y, ctrl2->x, vec->yHigh - ctrl2->y, to->x
         , vec->yHigh - to->y);
  return 0;
}

canonical_original_code:
int cubic_to(struct FT_Vector_ *ctrl1, struct FT_Vector_ *ctrl2, struct FT_Vector_ *to, void *user)
{
  struct VecData_ *vec = (VecData*)user;
  fprintf(stderr, "CUBIC_TO: *(%d, %d)*(%d, %d) (%d, %d)\n", ctrl1->x, vec->yHigh - ctrl1->y
         , ctrl2->x, vec->yHigh - ctrl2->y, to->x, vec->yHigh - to->y);
  printf(" C %d %d %d %d ", ctrl1->x, vec->yHigh - ctrl1->y, ctrl2->x, vec->yHigh - ctrl2->y, to->x
         , vec->yHigh - to->y);
  return 0;
}


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

v2 = *a1;
if ( *a1 > a2[1] )
a2[1] = v2;
if ( v2 < *a2 )
*a2 = v2;
result = a1[1];
if ( result > a2[3] )
a2[3] = result;
if ( result < a2[2] )
a2[2] = result;
return result;
}

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

v2 = *a1;
if ( *a1 > a2[1] )
a2[1] = v2;
if ( v2 < *a2 )
*a2 = v2;
result = a1[1];
if ( result > a2[3] )
a2[3] = result;
if ( result < a2[2] )
a2[2] = result;
return result;
}

original_code:
void update_vector(const FT_Vector* src, VecData* data)
{
  if (src->x > data->xHigh)
    data->xHigh = src->x;
  if (src->x < data->xLow)
    data->xLow = src->x;
  if (src->y > data->yHigh)
    data->yHigh = src->y;
  if (src->y < data->yLow)
    data->yLow = src->y;
}

canonical_original_code:
void update_vector(struct FT_Vector_ *src, struct VecData_ *data)
{
  if (src->x > data->xHigh)
    data->xHigh = src->x;
  if (src->x < data->xLow)
    data->xLow = src->x;
  if (src->y > data->yHigh)
    data->yHigh = src->y;
  if (src->y < data->yLow)
    data->yLow = src->y;
}

variable_types:
src
struct FT_Vector_ { long x; long y; } *

variable_types:
data
struct VecData_ { int xLow; } *


decompiled_code:
__int64 __fastcall test_move_to(__int64 *a1, int *a2)
{
update_vector(a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 *a1, int *a2)
{
func11(a1, a2);
return 0LL;
}

original_code:
int test_move_to(const FT_Vector* to, void* user)
{
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_move_to(struct FT_Vector_ *to, void *user)
{
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


decompiled_code:
// attributes: thunk
__int64 __fastcall test_line_to(__int64 *a1, int *a2)
{
return test_move_to(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 *a1, int *a2)
{
return func12(a1, a2);
}

original_code:
int test_line_to(const FT_Vector* to, void* user)
{
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_line_to(struct FT_Vector_ *to, void *user)
{
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


decompiled_code:
__int64 __fastcall test_conic_to(__int64 *a1, __int64 a2, int *a3)
{
__int64 *v4; // r8

update_vector(a1, a3);
update_vector(v4, a3);
return 0LL;
}

original_code:
int test_conic_to(const FT_Vector* ctrl, const FT_Vector* to, void* user)
{
  update_vector(ctrl, (VecData*)user);
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_conic_to(struct FT_Vector_ *ctrl, struct FT_Vector_ *to, void *user)
{
  update_vector(ctrl, (struct VecData_ *)user);
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


decompiled_code:
__int64 __fastcall test_cubic_to(__int64 *a1, __int64 a2, __int64 a3, int *a4)
{
__int64 *v5; // r9
__int64 *v6; // r8

update_vector(a1, a4);
update_vector(v5, a4);
update_vector(v6, a4);
return 0LL;
}

original_code:
int test_cubic_to(const FT_Vector* ctrl1, const FT_Vector* ctrl2, const FT_Vector* to,
             void* user)
{
  update_vector(ctrl1, (VecData*)user);
  update_vector(ctrl2, (VecData*)user);
  update_vector(to, (VecData*)user);
  return 0;
}

canonical_original_code:
int test_cubic_to(struct FT_Vector_ *ctrl1, struct FT_Vector_ *ctrl2, struct FT_Vector_ *to, void *user)
{
  update_vector(ctrl1, (struct VecData_ *)user);
  update_vector(ctrl2, (struct VecData_ *)user);
  update_vector(to, (struct VecData_ *)user);
  return 0;
}


original_code:
unsigned int utf8_to_utf32(char* str, char** next)
{
  char leader = str[0];
  unsigned int ret = 0;
  char* nextBuf = ((void *)0);
  if ((leader & 0xF0) == 0xF0)
    {
      ret += (str[0] & 0x07) << 18;
      ret += (str[1] & 0x3F) << 12;
      ret += (str[2] & 0x3F) << 6;
      ret += (str[3] & 0x3F);
      nextBuf = &str[4];
    }
  else if ((leader & 0xE0) == 0xE0)
    {
      ret += (str[0] & 0x0F) << 12;
      ret += (str[1] & 0x3F) << 6;
      ret += (str[2] & 0x3F);
      nextBuf = &str[3];
    }
  else if ((leader & 0xC0) == 0xC0)
    {
      ret += (str[0] & 0x1F) << 6;
      ret += (str[1] & 0x3F);
      nextBuf = &str[2];
    }
  else
    {
      ret = str[0];
      nextBuf = &str[1];
    }
  if (next != ((void *)0))
    *next = nextBuf;
  return ret;
}

canonical_original_code:
unsigned int utf8_to_utf32(char *str, char **next)
{
  char leader = str[0];
  unsigned int ret = 0;
  char *nextBuf = ((void *)0);
  if ((leader & 0xF0) == 0xF0)
    {
      ret += (str[0] & 0x07) << 18;
      ret += (str[1] & 0x3F) << 12;
      ret += (str[2] & 0x3F) << 6;
      ret += (str[3] & 0x3F);
      nextBuf = &str[4];
    }
  else if ((leader & 0xE0) == 0xE0)
    {
      ret += (str[0] & 0x0F) << 12;
      ret += (str[1] & 0x3F) << 6;
      ret += (str[2] & 0x3F);
      nextBuf = &str[3];
    }
  else if ((leader & 0xC0) == 0xC0)
    {
      ret += (str[0] & 0x1F) << 6;
      ret += (str[1] & 0x3F);
      nextBuf = &str[2];
    }
  else
    {
      ret = str[0];
      nextBuf = &str[1];
    }
  if (next != ((void *)0))
    *next = nextBuf;
  return ret;
}

variable_types:
str
char *

variable_types:
next
char **

variable_types: leader char

variable_types:
ret
unsigned int

variable_types:
nextBuf
char *

return_type: unsigned int


repo: inorindesu/pango-itemizer

decompiled_code:
void print_header()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code:
void print_header()
{
}

canonical_original_code:
void print_header()
{
}


repo: inovex/vnc-reflector

decompiled_code:
size_t __fastcall report_usage(const char *a1)
{
fprintf(stderr, "VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n", "1.2.4");
fprintf(stderr, "Usage: %s [OPTIONS...] HOST_INFO_FILE\n\n", a1);
fwrite(
"Options:\n"
" -i PID_FILE - write pid file, appending listening port to the filename\n"
" -p PASSWD_FILE - read a plaintext client password file [default: passwd]\n"
" -a ACTIVE_FILE - create file during times when a host is connected\n"
" -c ACTIONS_FILE - on events, execute commands specified in a file\n"
" -l LISTEN_PORT - port to listen for client connections [default: 5999]\n"
" -b IP_ADDRESS - bind listening sockets to a specific IP [default: any]\n",
1uLL,
0x1C1uLL,
stderr);
fwrite(
" -s FBS_PREFIX - save host sessions in rfbproxy-compatible files\n"
" (optionally appending 3-digit session IDs to the\n"
" filename prefix, only if used without the -j option)\n"
" -j - join saved sessions (see -s option) in one session file\n"
" -t - use Tight encoding for host communications if possible\n"
" -T COMPR_LEVEL - like -t, but use the specified compression level (1..9)\n",
1uLL,
0x1B5uLL,
stderr);
fprintf(
stderr,
" -g LOG_FILE - write logs to the specified file [default: reflector.log]\n"
" -v LOG_LEVEL - set verbosity level for the log file (0..%d) [default: %d]\n"
" -f LOG_LEVEL - run in foreground, show logs on stderr at the specified\n"
" verbosity level (0..%d) [note: use %d for normal output]\n"
" -q - suppress printing copyright banner at startup\n"
" -h - print this help message\n"
"\n",
6LL,
4LL,
6LL,
3LL);
return fwrite(
"Please refer to the README file for a description of the file formats for\n"
" HOST_INFO_FILE and PASSWD_FILE files mentioned above in this help text.\n"
"\n",
1uLL,
0x95uLL,
stderr);
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
fprintf(stderr, "VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n", "1.2.4");
fprintf(stderr, "Usage: %s [OPTIONS...] HOST_INFO_FILE\n\n", a1);
fwrite(
"Options:\n"
" -i PID_FILE - write pid file, appending listening port to the filename\n"
" -p PASSWD_FILE - read a plaintext client password file [default: passwd]\n"
" -a ACTIVE_FILE - create file during times when a host is connected\n"
" -c ACTIONS_FILE - on events, execute commands specified in a file\n"
" -l LISTEN_PORT - port to listen for client connections [default: 5999]\n"
" -b IP_ADDRESS - bind listening sockets to a specific IP [default: any]\n",
1uLL,
0x1C1uLL,
stderr);
fwrite(
" -s FBS_PREFIX - save host sessions in rfbproxy-compatible files\n"
" (optionally appending 3-digit session IDs to the\n"
" filename prefix, only if used without the -j option)\n"
" -j - join saved sessions (see -s option) in one session file\n"
" -t - use Tight encoding for host communications if possible\n"
" -T COMPR_LEVEL - like -t, but use the specified compression level (1..9)\n",
1uLL,
0x1B5uLL,
stderr);
fprintf(
stderr,
" -g LOG_FILE - write logs to the specified file [default: reflector.log]\n"
" -v LOG_LEVEL - set verbosity level for the log file (0..%d) [default: %d]\n"
" -f LOG_LEVEL - run in foreground, show logs on stderr at the specified\n"
" verbosity level (0..%d) [note: use %d for normal output]\n"
" -q - suppress printing copyright banner at startup\n"
" -h - print this help message\n"
"\n",
6LL,
4LL,
6LL,
3LL);
return fwrite(
"Please refer to the README file for a description of the file formats for\n"
" HOST_INFO_FILE and PASSWD_FILE files mentioned above in this help text.\n"
"\n",
1uLL,
0x95uLL,
stderr);
}

original_code:
static void report_usage(char *program_name)
{
  fprintf(stderr,
          "VNC Reflector %s.  Copyright (C) 2001-2003 HorizonLive.com, Inc."
          "\n\n",
          "1.2.4");
  fprintf(stderr, "Usage: %s [OPTIONS...] HOST_INFO_FILE\n\n",
          program_name);
  fprintf(stderr,
          "Options:\n"
          "  -i PID_FILE     - write pid file, appending listening port"
          " to the filename\n"
          "  -p PASSWD_FILE  - read a plaintext client password file"
          " [default: passwd]\n"
          "  -a ACTIVE_FILE  - create file during times when a host is"
          " connected\n"
          "  -c ACTIONS_FILE - on events, execute commands specified in"
          " a file\n"
          "  -l LISTEN_PORT  - port to listen for client connections"
          " [default: 5999]\n"
          "  -b IP_ADDRESS   - bind listening sockets to a specific IP"
          " [default: any]\n");
  fprintf(stderr,
          "  -s FBS_PREFIX   - save host sessions in rfbproxy-compatible"
          " files\n"
          "                    (optionally appending 3-digit session IDs"
          " to the\n"
          "                    filename prefix, only if used without the"
          " -j option)\n"
          "  -j              - join saved sessions (see -s option) in one"
          " session file\n"
          "  -t              - use Tight encoding for host communications"
          " if possible\n"
          "  -T COMPR_LEVEL  - like -t, but use the specified compression"
          " level (1..9)\n");
  fprintf(stderr,
          "  -g LOG_FILE     - write logs to the specified file"
          " [default: reflector.log]\n"
          "  -v LOG_LEVEL    - set verbosity level for the log file (0..%d)"
          " [default: %d]\n"
          "  -f LOG_LEVEL    - run in foreground, show logs on stderr"
          " at the specified\n"
          "                    verbosity level (0..%d) [note: use %d for"
          " normal output]\n"
          "  -q              - suppress printing copyright banner at startup\n"
          "  -h              - print this help message\n\n",
          6, 4, 6, 3);
  fprintf(stderr,
          "Please refer to the README file for a description of the file"
          " formats for\n"
          "  HOST_INFO_FILE and PASSWD_FILE files mentioned above in this"
          " help text.\n\n");
}


decompiled_code:
char *__fastcall parse_args(int argc, char **argv)
{
char *v2; // r12
int v3; // eax
int v4; // eax
char *result; // rax
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

v2 = 0LL;
opt_pid_file = 0;
opt_no_banner = 0;
opt_foreground = 0;
opt_stderr_loglevel = -1;
opt_file_loglevel = -1;
opt_passwd_filename = 0LL;
opt_log_filename = 0LL;
opt_active_filename = 0LL;
opt_cl_listen_port = -1;
opt_fbs_prefix = 0LL;
opt_join_sessions = 0;
opt_bind_ip = 0LL;
opt_request_tight = 0;
opt_tight_level = -1;
while ( 1 )
{
v3 = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'T':
if ( opt_request_tight )
goto LABEL_15;
opt_request_tight = 1;
opt_tight_level = atoi(optarg);
if ( (unsigned int)(opt_tight_level - 1) > 8 )
goto LABEL_15;
continue;
case 'a':
if ( opt_active_filename )
goto LABEL_15;
opt_active_filename = (__int64)optarg;
break;
case 'b':
if ( opt_bind_ip )
goto LABEL_15;
opt_bind_ip = (__int64)optarg;
break;
case 'c':
if ( opt_actions_filename )
goto LABEL_15;
opt_actions_filename = (struct aioinit *)optarg;
break;
case 'f':
opt_foreground = 1;
if ( opt_stderr_loglevel != -1 )
goto LABEL_15;
opt_stderr_loglevel = atoi(optarg);
break;
case 'g':
if ( opt_log_filename )
goto LABEL_15;
opt_log_filename = (__int64)optarg;
break;
case 'i':
if ( v2 )
goto LABEL_15;
v2 = optarg;
break;
case 'j':
opt_join_sessions = 1;
continue;
case 'l':
if ( opt_cl_listen_port != -1 )
goto LABEL_15;
opt_cl_listen_port = atoi(optarg);
if ( opt_cl_listen_port <= 0 )
goto LABEL_15;
continue;
case 'p':
if ( opt_passwd_filename )
goto LABEL_15;
opt_passwd_filename = optarg;
break;
case 'q':
opt_no_banner = 1;
continue;
case 's':
if ( opt_fbs_prefix )
goto LABEL_15;
opt_fbs_prefix = (__int64)optarg;
break;
case 't':
if ( opt_request_tight )
goto LABEL_15;
opt_request_tight = 1;
break;
case 'v':
if ( opt_file_loglevel != -1 )
goto LABEL_15;
opt_file_loglevel = atoi(optarg);
break;
default:
goto LABEL_15;
}
}
if ( argc - 1 != optind )
{
LABEL_15:
report_usage(*argv);
exit(1);
}
if ( opt_file_loglevel == -1 )
opt_file_loglevel = 4;
if ( !opt_passwd_filename )
opt_passwd_filename = "passwd";
if ( !opt_log_filename )
opt_log_filename = (__int64)"reflector.log";
if ( opt_cl_listen_port == -1 )
opt_cl_listen_port = 5999;
if ( v2 )
{
sprintf(v6, "%d", (unsigned int)opt_cl_listen_port);
v4 = strlen(v6);
sprintf(&opt_pid_file, "%.*s.%s", 254 - v4, v2, v6);
}
result = argv[optind];
opt_host_info_file = (__int64)result;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(int argc, char **argv)
{
char *v2; // r12
int v3; // eax
int v4; // eax
char *result; // rax
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

v2 = 0LL;
opt_pid_file = 0;
opt_no_banner = 0;
opt_foreground = 0;
opt_stderr_loglevel = -1;
opt_file_loglevel = -1;
opt_passwd_filename = 0LL;
opt_log_filename = 0LL;
opt_active_filename = 0LL;
opt_cl_listen_port = -1;
opt_fbs_prefix = 0LL;
opt_join_sessions = 0;
opt_bind_ip = 0LL;
opt_request_tight = 0;
opt_tight_level = -1;
while ( 1 )
{
v3 = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'T':
if ( opt_request_tight )
goto LABEL_15;
opt_request_tight = 1;
opt_tight_level = atoi(optarg);
if ( (unsigned int)(opt_tight_level - 1) > 8 )
goto LABEL_15;
continue;
case 'a':
if ( opt_active_filename )
goto LABEL_15;
opt_active_filename = (__int64)optarg;
break;
case 'b':
if ( opt_bind_ip )
goto LABEL_15;
opt_bind_ip = (__int64)optarg;
break;
case 'c':
if ( opt_actions_filename )
goto LABEL_15;
opt_actions_filename = (struct aioinit *)optarg;
break;
case 'f':
opt_foreground = 1;
if ( opt_stderr_loglevel != -1 )
goto LABEL_15;
opt_stderr_loglevel = atoi(optarg);
break;
case 'g':
if ( opt_log_filename )
goto LABEL_15;
opt_log_filename = (__int64)optarg;
break;
case 'i':
if ( v2 )
goto LABEL_15;
v2 = optarg;
break;
case 'j':
opt_join_sessions = 1;
continue;
case 'l':
if ( opt_cl_listen_port != -1 )
goto LABEL_15;
opt_cl_listen_port = atoi(optarg);
if ( opt_cl_listen_port <= 0 )
goto LABEL_15;
continue;
case 'p':
if ( opt_passwd_filename )
goto LABEL_15;
opt_passwd_filename = optarg;
break;
case 'q':
opt_no_banner = 1;
continue;
case 's':
if ( opt_fbs_prefix )
goto LABEL_15;
opt_fbs_prefix = (__int64)optarg;
break;
case 't':
if ( opt_request_tight )
goto LABEL_15;
opt_request_tight = 1;
break;
case 'v':
if ( opt_file_loglevel != -1 )
goto LABEL_15;
opt_file_loglevel = atoi(optarg);
break;
default:
goto LABEL_15;
}
}
if ( argc - 1 != optind )
{
LABEL_15:
func0(*argv);
exit(1);
}
if ( opt_file_loglevel == -1 )
opt_file_loglevel = 4;
if ( !opt_passwd_filename )
opt_passwd_filename = "passwd";
if ( !opt_log_filename )
opt_log_filename = (__int64)"reflector.log";
if ( opt_cl_listen_port == -1 )
opt_cl_listen_port = 5999;
if ( v2 )
{
sprintf(v6, "%d", (unsigned int)opt_cl_listen_port);
v4 = strlen(v6);
sprintf(&opt_pid_file, "%.*s.%s", 254 - v4, v2, v6);
}
result = argv[optind];
opt_host_info_file = (__int64)result;
return result;
}

original_code:
static void parse_args(int argc, char **argv)
{
  int err = 0;
  int c;
  char *temp_pid_file = ((void *)0);
  char temp_buf[32];
  opt_no_banner = 0;
  opt_foreground = 0;
  opt_stderr_loglevel = -1;
  opt_file_loglevel = -1;
  opt_passwd_filename = ((void *)0);
  opt_log_filename = ((void *)0);
  opt_active_filename = ((void *)0);
  opt_cl_listen_port = -1;
  opt_pid_file[0] = '\0';
  opt_fbs_prefix = ((void *)0);
  opt_join_sessions = 0;
  opt_bind_ip = ((void *)0);
  opt_request_tight = 0;
  opt_tight_level = -1;
  while (!err &&
         (c = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:")) != -1) {
    switch (c) {
    case 'h':
      err = 1;
      break;
    case 'q':
      opt_no_banner = 1;
      break;
    case 'j':
      opt_join_sessions = 1;
      break;
    case 'v':
      if (opt_file_loglevel != -1)
        err = 1;
      else
        opt_file_loglevel = atoi(optarg);
      break;
    case 'f':
      opt_foreground = 1;
      if (opt_stderr_loglevel != -1)
        err = 1;
      else
        opt_stderr_loglevel = atoi(optarg);
      break;
    case 'p':
      if (opt_passwd_filename != ((void *)0))
        err = 1;
      else
        opt_passwd_filename = optarg;
      break;
    case 'g':
      if (opt_log_filename != ((void *)0))
        err = 1;
      else
        opt_log_filename = optarg;
      break;
    case 'a':
      if (opt_active_filename != ((void *)0))
        err = 1;
      else
        opt_active_filename = optarg;
      break;
    case 'c':
      if (opt_actions_filename != ((void *)0))
        err = 1;
      else
        opt_actions_filename = optarg;
      break;
    case 'l':
      if (opt_cl_listen_port != -1)
        err = 1;
      else {
        opt_cl_listen_port = atoi(optarg);
        if (opt_cl_listen_port <= 0)
          err = 1;
      }
      break;
    case 'i':
      if (temp_pid_file != ((void *)0))
        err = 1;
      else
        temp_pid_file = optarg;
      break;
    case 's':
      if (opt_fbs_prefix != ((void *)0))
        err = 1;
      else
        opt_fbs_prefix = optarg;
      break;
    case 'b':
      if (opt_bind_ip != ((void *)0))
        err = 1;
      else
        opt_bind_ip = optarg;
      break;
    case 't':
      if (opt_request_tight)
        err = 1;
      else
        opt_request_tight = 1;
      break;
    case 'T':
      if (opt_request_tight) {
        err = 1;
      } else {
        opt_request_tight = 1;
        opt_tight_level = atoi(optarg);
        if (opt_tight_level <= 0 || opt_tight_level > 9)
          err = 1;
      }
      break;
    default:
      err = 1;
    }
  }
  if (err || optind != argc - 1) {
    report_usage(argv[0]);
    exit(1);
  }
  if (opt_file_loglevel == -1)
    opt_file_loglevel = 4;
  if (opt_passwd_filename == ((void *)0))
    opt_passwd_filename = "passwd";
  if (opt_log_filename == ((void *)0))
    opt_log_filename = "reflector.log";
  if (opt_cl_listen_port == -1)
    opt_cl_listen_port = 5999;
  if (temp_pid_file != ((void *)0)) {
    sprintf(temp_buf, "%d", opt_cl_listen_port);
    sprintf(opt_pid_file, "%.*s.%s", (int)(255 - strlen(temp_buf) - 1),
            temp_pid_file, temp_buf);
  }
  opt_host_info_file = argv[optind];
}

variable_types: argc int

variable_types:
argv
char **

variable_types: err int

variable_types: c int


decompiled_code:
__int64 write_pid_file()
{
unsigned int v0; // er12
int v2; // ebp
unsigned int v3; // eax
size_t v4; // rbx
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

v0 = 1;
if ( opt_pid_file )
{
v2 = open(&opt_pid_file, 193, 420LL);
if ( v2 == -1 )
{
v0 = 0;
log_write(1LL, "Pid file exists, another instance may be running");
}
else
{
v3 = getpid();
sprintf(v5, "%d\n", v3);
v4 = strlen(v5);
if ( write(v2, v5, v4) == v4 )
{
log_write(6LL, "Wrote pid file: %s", &opt_pid_file);
close(v2);
}
else
{
v0 = 0;
close(v2);
log_write(1LL, "Error writing to pid file");
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // er12
int v2; // ebp
unsigned int v3; // eax
size_t v4; // rbx
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

v0 = 1;
if ( opt_pid_file )
{
v2 = open(&opt_pid_file, 193, 420LL);
if ( v2 == -1 )
{
v0 = 0;
log_write(1LL, "Pid file exists, another instance may be running");
}
else
{
v3 = getpid();
sprintf(v5, "%d\n", v3);
v4 = strlen(v5);
if ( write(v2, v5, v4) == v4 )
{
log_write(6LL, "Wrote pid file: %s", &opt_pid_file);
close(v2);
}
else
{
v0 = 0;
close(v2);
log_write(1LL, "Error writing to pid file");
}
}
}
return v0;
}

original_code:
static int write_pid_file(void)
{
  int pid_fd, len;
  char buf[32];
  if (opt_pid_file[0] == '\0')
    return 1;
  pid_fd = open(opt_pid_file, 01 | 0100 | 0200, 0644);
  if (pid_fd == -1) {
    log_write(1, "Pid file exists, another instance may be running");
    return 0;
  }
  sprintf(buf, "%d\n", (int)getpid());
  len = strlen(buf);
  if (write(pid_fd, buf, len) != len) {
    close(pid_fd);
    log_write(1, "Error writing to pid file");
    return 0;
  }
  log_write(6, "Wrote pid file: %s", opt_pid_file);
  close(pid_fd);
  return 1;
}

canonical_original_code:
static int write_pid_file()
{
  int pid_fd; int len;
  char buf[32];
  if (opt_pid_file[0] == '\0')
    return 1;
  pid_fd = open(opt_pid_file, 01 | 0100 | 0200, 0644);
  if (pid_fd == -1) {
    log_write(1, "Pid file exists, another instance may be running");
    return 0;
  }
  sprintf(buf, "%d\n", (int)getpid());
  len = strlen(buf);
  if (write(pid_fd, buf, len) != len) {
    close(pid_fd);
    log_write(1, "Error writing to pid file");
    return 0;
  }
  log_write(6, "Wrote pid file: %s", opt_pid_file);
  close(pid_fd);
  return 1;
}

variable_types: len int

variable_types:
buf
char [32]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // rdx
const struct aioinit *v6; // rdi
__int64 v7; // rsi
__int64 v8; // rdi
void *v9; // rdi
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
__int64 v13; // r9
__int64 v14; // [rsp+0h] [rbp-18h] BYREF
__int64 v15; // [rsp+8h] [rbp-10h] BYREF

parse_args(argc, (char **)argv);
if ( !opt_no_banner )
fprintf(
stderr,
"VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n"
"\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment. For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n"
"\n",
"1.2.4");
v4 = 0xFFFFFFFFLL;
if ( opt_foreground )
v4 = (unsigned int)opt_stderr_loglevel;
if ( !(unsigned int)log_open(opt_log_filename, (unsigned int)opt_file_loglevel, v4) )
fprintf(stderr, "%s: error opening log file (ignoring this error)\n", *argv);
log_write(3LL, "Starting VNC Reflector %s", "1.2.4");
if ( opt_foreground )
{
LABEL_15:
if ( (unsigned int)init_screen_info() )
{
read_password_file_isra_0();
set_host_encodings((unsigned int)opt_request_tight, (unsigned int)opt_tight_level);
set_client_passwords(opt_client_password, opt_client_ro_password);
fbs_set_prefix(opt_fbs_prefix, (unsigned int)opt_join_sessions);
set_active_file(opt_active_filename);
v6 = opt_actions_filename;
set_actions_file(opt_actions_filename);
aio_init(v6);
if ( opt_bind_ip )
{
if ( (unsigned int)aio_set_bind_address() )
log_write(4LL, "Would bind listening sockets to address %s", (const char *)opt_bind_ip);
else
log_write(2LL, "Illegal address to bind listening sockets to: %s", (const char *)opt_bind_ip);
}
v7 = (unsigned int)opt_cl_listen_port;
v8 = opt_host_info_file;
if ( (unsigned int)connect_to_host(opt_host_info_file, (unsigned int)opt_cl_listen_port)
&& (unsigned int)write_pid_file() )
{
set_control_signals();
aio_mainloop();
remove_pid_file_isra_0(v8, v7, v10, v11, v12, v13);
}
if ( g_framebuffer )
{
log_write(5LL, "Freeing framebuffer and associated structures");
free(g_framebuffer);
free_enc_cache();
}
v9 = (void *)*((_QWORD *)&g_screen_info + 3);
if ( v9 )
free(v9);
get_hextile_caching_stats(&v14, &v15);
if ( v15 + v14 )
log_write(
4LL,
"Hextile BGR233 caching efficiency: %d%%",
(unsigned int)((100 * v14 + (v15 + v14) / 2) / (v15 + v14)));
}
log_write(3LL, "Terminating");
if ( !(unsigned int)log_close() && opt_foreground )
fprintf(stderr, "%s: error closing log file (ignoring this error)\n", *argv);
exit(1);
}
if ( !opt_no_banner )
fwrite("Starting in the background, see the log file for errors and other messages.\n", 1uLL, 0x4CuLL, stderr);
if ( getpid() == 1 )
{
LABEL_14:
close(0);
close(1);
close(2);
log_write(4LL, "Switched to the background mode");
goto LABEL_15;
}
signal(21, (__sighandler_t)((char *)report_usage + 1));
signal(22, (__sighandler_t)((char *)report_usage + 1));
signal(20, (__sighandler_t)((char *)report_usage + 1));
if ( !fork() )
{
setsid();
goto LABEL_14;
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // rdx
const struct aioinit *v6; // rdi
__int64 v7; // rsi
__int64 v8; // rdi
void *v9; // rdi
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
__int64 v13; // r9
__int64 v14; // [rsp+0h] [rbp-18h] BYREF
__int64 v15; // [rsp+8h] [rbp-10h] BYREF

func1(argc, (char **)argv);
if ( !opt_no_banner )
fprintf(
stderr,
"VNC Reflector %s. Copyright (C) 2001-2003 HorizonLive.com, Inc.\n"
"\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment. For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n"
"\n",
"1.2.4");
v4 = 0xFFFFFFFFLL;
if ( opt_foreground )
v4 = (unsigned int)opt_stderr_loglevel;
if ( !(unsigned int)log_open(opt_log_filename, (unsigned int)opt_file_loglevel, v4) )
fprintf(stderr, "%s: error opening log file (ignoring this error)\n", *argv);
log_write(3LL, "Starting VNC Reflector %s", "1.2.4");
if ( opt_foreground )
{
LABEL_15:
if ( (unsigned int)func2() )
{
func5();
set_host_encodings((unsigned int)opt_request_tight, (unsigned int)opt_tight_level);
set_client_passwords(opt_client_password, opt_client_ro_password);
fbs_set_prefix(opt_fbs_prefix, (unsigned int)opt_join_sessions);
set_active_file(opt_active_filename);
v6 = opt_actions_filename;
set_actions_file(opt_actions_filename);
aio_init(v6);
if ( opt_bind_ip )
{
if ( (unsigned int)aio_set_bind_address() )
log_write(4LL, "Would bind listening sockets to address %s", (const char *)opt_bind_ip);
else
log_write(2LL, "Illegal address to bind listening sockets to: %s", (const char *)opt_bind_ip);
}
v7 = (unsigned int)opt_cl_listen_port;
v8 = opt_host_info_file;
if ( (unsigned int)connect_to_host(opt_host_info_file, (unsigned int)opt_cl_listen_port)
&& (unsigned int)func3() )
{
set_control_signals();
aio_mainloop();
func4(v8, v7, v10, v11, v12, v13);
}
if ( g_framebuffer )
{
log_write(5LL, "Freeing framebuffer and associated structures");
free(g_framebuffer);
free_enc_cache();
}
v9 = (void *)*((_QWORD *)&g_screen_info + 3);
if ( v9 )
free(v9);
get_hextile_caching_stats(&v14, &v15);
if ( v15 + v14 )
log_write(
4LL,
"Hextile BGR233 caching efficiency: %d%%",
(unsigned int)((100 * v14 + (v15 + v14) / 2) / (v15 + v14)));
}
log_write(3LL, "Terminating");
if ( !(unsigned int)log_close() && opt_foreground )
fprintf(stderr, "%s: error closing log file (ignoring this error)\n", *argv);
exit(1);
}
if ( !opt_no_banner )
fwrite("Starting in the background, see the log file for errors and other messages.\n", 1uLL, 0x4CuLL, stderr);
if ( getpid() == 1 )
{
LABEL_14:
close(0);
close(1);
close(2);
log_write(4LL, "Switched to the background mode");
goto LABEL_15;
}
signal(21, (__sighandler_t)((char *)report_usage + 1));
signal(22, (__sighandler_t)((char *)report_usage + 1));
signal(20, (__sighandler_t)((char *)report_usage + 1));
if ( !fork() )
{
setsid();
goto LABEL_14;
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
  long cache_hits, cache_misses;
  parse_args(argc, argv);
  if (!opt_no_banner) {
    fprintf(stderr,
"VNC Reflector %s.  Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment.  For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n\n",
            "1.2.4");
  }
  if (!log_open(opt_log_filename, opt_file_loglevel,
                (opt_foreground) ? opt_stderr_loglevel : -1)) {
    fprintf(stderr, "%s: error opening log file (ignoring this error)\n",
            argv[0]);
  }
  log_write(3, "Starting VNC Reflector %s", "1.2.4");
  if (!opt_foreground) {
    if (!opt_no_banner) {
      fprintf(stderr, "Starting in the background, "
              "see the log file for errors and other messages.\n");
    }
    if (getpid() != 1) {
      signal(21, ((__sighandler_t) 1));
      signal(22, ((__sighandler_t) 1));
      signal(20, ((__sighandler_t) 1));
      if (fork ())
        return 0;
      setsid();
    }
    close(0);
    close(1);
    close(2);
    log_write(4, "Switched to the background mode");
  }
  if (init_screen_info()) {
    read_password_file();
    set_host_encodings(opt_request_tight, opt_tight_level);
    set_client_passwords(opt_client_password, opt_client_ro_password);
    fbs_set_prefix(opt_fbs_prefix, opt_join_sessions);
    set_active_file(opt_active_filename);
    set_actions_file(opt_actions_filename);
    aio_init();
    if (opt_bind_ip != ((void *)0)) {
      if (aio_set_bind_address(opt_bind_ip)) {
        log_write(4, "Would bind listening sockets to address %s",
                  opt_bind_ip);
      } else {
        log_write(2, "Illegal address to bind listening sockets to: %s",
                  opt_bind_ip);
      }
    }
    if (connect_to_host(opt_host_info_file, opt_cl_listen_port)) {
      if (write_pid_file()) {
        set_control_signals();
        aio_mainloop();
        remove_pid_file();
      }
    }
    if (g_framebuffer != ((void *)0)) {
      log_write(5, "Freeing framebuffer and associated structures");
      free(g_framebuffer);
      free_enc_cache();
    }
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    get_hextile_caching_stats(&cache_hits, &cache_misses);
    if (cache_hits + cache_misses != 0) {
      log_write(4, "Hextile BGR233 caching efficiency: %d%%",
                (int)((cache_hits * 100 + (cache_hits + cache_misses) / 2)
                      / (cache_hits + cache_misses)));
    }
  }
  log_write(3, "Terminating");
  if (!log_close() && opt_foreground) {
    fprintf(stderr, "%s: error closing log file (ignoring this error)\n",
            argv[0]);
  }
  exit(1);
}

canonical_original_code:
int main(int argc, char **argv)
{
  long cache_hits; long cache_misses;
  parse_args(argc, argv);
  if (!opt_no_banner) {
    fprintf(stderr,
"VNC Reflector %s.  Copyright (C) 2001-2003 HorizonLive.com, Inc.\n\n"
"HorizonLive provides e-Learning and collaborative synchronous presentation\n"
"solutions in a totally Web-based environment.  For more information about\n"
"HorizonLive, please see our website at http://www.horizonlive.com/\n\n",
            "1.2.4");
  }
  if (!log_open(opt_log_filename, opt_file_loglevel,
                (opt_foreground) ? opt_stderr_loglevel : -1)) {
    fprintf(stderr, "%s: error opening log file (ignoring this error)\n",
            argv[0]);
  }
  log_write(3, "Starting VNC Reflector %s", "1.2.4");
  if (!opt_foreground) {
    if (!opt_no_banner) {
      fprintf(stderr, "Starting in the background, "
              "see the log file for errors and other messages.\n");
    }
    if (getpid() != 1) {
      signal(21, ((void (*)(int)) 1));
      signal(22, ((void (*)(int)) 1));
      signal(20, ((void (*)(int)) 1));
      if (fork ())
        return 0;
      setsid();
    }
    close(0);
    close(1);
    close(2);
    log_write(4, "Switched to the background mode");
  }
  if (init_screen_info()) {
    read_password_file();
    set_host_encodings(opt_request_tight, opt_tight_level);
    set_client_passwords(opt_client_password, opt_client_ro_password);
    fbs_set_prefix(opt_fbs_prefix, opt_join_sessions);
    set_active_file(opt_active_filename);
    set_actions_file(opt_actions_filename);
    aio_init();
    if (opt_bind_ip != ((void *)0)) {
      if (aio_set_bind_address(opt_bind_ip)) {
        log_write(4, "Would bind listening sockets to address %s",
                  opt_bind_ip);
      } else {
        log_write(2, "Illegal address to bind listening sockets to: %s",
                  opt_bind_ip);
      }
    }
    if (connect_to_host(opt_host_info_file, opt_cl_listen_port)) {
      if (write_pid_file()) {
        set_control_signals();
        aio_mainloop();
        remove_pid_file();
      }
    }
    if (g_framebuffer != ((void *)0)) {
      log_write(5, "Freeing framebuffer and associated structures");
      free(g_framebuffer);
      free_enc_cache();
    }
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    get_hextile_caching_stats(&cache_hits, &cache_misses);
    if (cache_hits + cache_misses != 0) {
      log_write(4, "Hextile BGR233 caching efficiency: %d%%",
                (int)((cache_hits * 100 + (cache_hits + cache_misses) / 2)
                      / (cache_hits + cache_misses)));
    }
  }
  log_write(3, "Terminating");
  if (!log_close() && opt_foreground) {
    fprintf(stderr, "%s: error closing log file (ignoring this error)\n",
            argv[0]);
  }
  exit(1);
}


decompiled_code:
__int64 log_reopen()
{
unsigned int v0; // er12
__int64 result; // rax

if ( !log_fname )
return 0LL;
v0 = 0;
if ( !log_fp )
return v0;
if ( fclose(log_fp) != -1 )
{
v0 = 1;
log_fp = fopen(log_fname, "a");
if ( !log_fp )
{
v0 = 0;
free(log_fname);
log_fname = 0LL;
}
return v0;
}
free(log_fname);
result = 0LL;
log_fname = 0LL;
log_fp = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
__int64 result; // rax

if ( !log_fname )
return 0LL;
v0 = 0;
if ( !log_fp )
return v0;
if ( fclose(log_fp) != -1 )
{
v0 = 1;
log_fp = fopen(log_fname, "a");
if ( !log_fp )
{
v0 = 0;
free(log_fname);
log_fname = 0LL;
}
return v0;
}
free(log_fname);
result = 0LL;
log_fname = 0LL;
log_fp = 0LL;
return result;
}

original_code:
int log_reopen(void)
{
  if (log_fname == ((void *)0) || log_fp == ((void *)0))
    return 0;
  if (fclose(log_fp) == (-1)) {
    free(log_fname);
    log_fname = ((void *)0);
    log_fp = ((void *)0);
    return 0;
  }
  log_fp = fopen(log_fname, "a");
  if (log_fp == ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
    return 0;
  }
  return 1;
}

canonical_original_code:
int log_reopen()
{
  if (log_fname == ((void *)0) || log_fp == ((void *)0))
    return 0;
  if (fclose(log_fp) == (-1)) {
    free(log_fname);
    log_fname = ((void *)0);
    log_fp = ((void *)0);
    return 0;
  }
  log_fp = fopen(log_fname, "a");
  if (log_fp == ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
    return 0;
  }
  return 1;
}


decompiled_code:
_BOOL8 log_close()
{
_BOOL8 result; // rax
int v1; // eax

if ( log_fname )
{
free(log_fname);
log_fname = 0LL;
}
result = 1LL;
if ( log_fp )
{
v1 = fclose(log_fp);
log_fp = 0LL;
result = v1 != -1;
}
log_file_level = -1;
log_stderr_level = -1;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
_BOOL8 result; // rax
int v1; // eax

if ( log_fname )
{
free(log_fname);
log_fname = 0LL;
}
result = 1LL;
if ( log_fp )
{
v1 = fclose(log_fp);
log_fp = 0LL;
result = v1 != -1;
}
log_file_level = -1;
log_stderr_level = -1;
return result;
}

original_code:
int log_close(void)
{
  int success = 1;
  if (log_fname != ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
  }
  if (log_fp != ((void *)0)) {
    success = (fclose(log_fp) != (-1));
    log_fp = ((void *)0);
  }
  log_file_level = -1;
  log_stderr_level = -1;
  return success;
}

canonical_original_code:
int log_close()
{
  int success = 1;
  if (log_fname != ((void *)0)) {
    free(log_fname);
    log_fname = ((void *)0);
  }
  if (log_fp != ((void *)0)) {
    success = (fclose(log_fp) != (-1));
    log_fp = ((void *)0);
  }
  log_file_level = -1;
  log_stderr_level = -1;
  return success;
}

variable_types: success int


decompiled_code:
void log_write(signed int a1, const char *a2, ...)
{
FILE *v2; // rbp
struct tm *v3; // rax
char v4; // r14
__int64 v5; // rcx
struct tm *v6; // rax
time_t v7; // [rsp+0h] [rbp-118h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-110h] BYREF
char s[32]; // [rsp+20h] [rbp-F8h] BYREF

v2 = log_fp;
if ( log_fp && log_file_level >= a1 )
{
v7 = time(0LL);
v3 = localtime(&v7);
strftime(s, 0x1FuLL, "%d/%m/%y %H:%M:%S", v3);
if ( (unsigned int)a1 > 6 )
{
v5 = 32LL;
v4 = 32;
goto LABEL_14;
}
v4 = log_lchar[a1];
}
else
{
if ( log_stderr_level < a1 )
return;
v7 = time(0LL);
v6 = localtime(&v7);
strftime(s, 0x1FuLL, "%d/%m/%y %H:%M:%S", v6);
if ( (unsigned int)a1 > 6 )
{
v4 = 32;
if ( a1 > log_file_level )
goto LABEL_12;
}
else
{
v4 = log_lchar[a1];
if ( a1 > log_file_level )
goto LABEL_9;
}
}
v5 = (unsigned int)v4;
LABEL_14:
fprintf(v2, "%s %c ", s, v5);
va_start(arg, a2);
vfprintf(log_fp, a2, arg);
fputc(10, log_fp);
fflush(log_fp);
LABEL_9:
if ( log_stderr_level >= a1 )
{
LABEL_12:
fprintf(stderr, "%s %c ", s, (unsigned int)v4);
va_end(arg);
va_start(arg, a2);
vfprintf(stderr, a2, arg);
fputc(10, stderr);
fflush(stderr);
}
}

canonical_decompiled_code:
void func2(signed int a1, const char *a2, ...)
{
FILE *v2; // rbp
struct tm *v3; // rax
char v4; // r14
__int64 v5; // rcx
struct tm *v6; // rax
time_t v7; // [rsp+0h] [rbp-118h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-110h] BYREF
char s[32]; // [rsp+20h] [rbp-F8h] BYREF

v2 = log_fp;
if ( log_fp && log_file_level >= a1 )
{
v7 = time(0LL);
v3 = localtime(&v7);
strftime(s, 0x1FuLL, "%d/%m/%y %H:%M:%S", v3);
if ( (unsigned int)a1 > 6 )
{
v5 = 32LL;
v4 = 32;
goto LABEL_14;
}
v4 = log_lchar[a1];
}
else
{
if ( log_stderr_level < a1 )
return;
v7 = time(0LL);
v6 = localtime(&v7);
strftime(s, 0x1FuLL, "%d/%m/%y %H:%M:%S", v6);
if ( (unsigned int)a1 > 6 )
{
v4 = 32;
if ( a1 > log_file_level )
goto LABEL_12;
}
else
{
v4 = log_lchar[a1];
if ( a1 > log_file_level )
goto LABEL_9;
}
}
v5 = (unsigned int)v4;
LABEL_14:
fprintf(v2, "%s %c ", s, v5);
va_start(arg, a2);
vfprintf(log_fp, a2, arg);
fputc(10, log_fp);
fflush(log_fp);
LABEL_9:
if ( log_stderr_level >= a1 )
{
LABEL_12:
fprintf(stderr, "%s %c ", s, (unsigned int)v4);
va_end(arg);
va_start(arg, a2);
vfprintf(stderr, a2, arg);
fputc(10, stderr);
fflush(stderr);
}
}

original_code:
void log_write(int level, char *format, ...)
{
  va_list arg_list;
  time_t now;
  char time_buf[32];
  char level_char = ' ';
  if ( (log_fp != ((void *)0) && level <= log_file_level) ||
       level <= log_stderr_level ) {
    now = time(((void *)0));
    strftime(time_buf, 31, "%d/%m/%y %H:%M:%S", localtime(&now));
    if (level >= 0 && level < sizeof(log_lchar) - 1)
      level_char = log_lchar[level];
    if (level <= log_file_level) {
      fprintf(log_fp, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(log_fp, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(log_fp, "\n");
      fflush(log_fp);
    }
    if (level <= log_stderr_level) {
      fprintf(stderr, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(stderr, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(stderr, "\n");
      fflush(stderr);
    }
  }
}

canonical_original_code:
void log_write(int level, char *format, ...)
{
  __builtin_va_list arg_list;
  long now;
  char time_buf[32];
  char level_char = ' ';
  if ( (log_fp != ((void *)0) && level <= log_file_level) ||
       level <= log_stderr_level ) {
    now = time(((void *)0));
    strftime(time_buf, 31, "%d/%m/%y %H:%M:%S", localtime(&now));
    if (level >= 0 && level < sizeof(log_lchar) - 1)
      level_char = log_lchar[level];
    if (level <= log_file_level) {
      fprintf(log_fp, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(log_fp, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(log_fp, "\n");
      fflush(log_fp);
    }
    if (level <= log_stderr_level) {
      fprintf(stderr, "%s %c ", time_buf, (int)level_char);
      __builtin_va_start(arg_list,format);
      vfprintf(stderr, format, arg_list);
      __builtin_va_end(arg_list);
      fprintf(stderr, "\n");
      fflush(stderr);
    }
  }
}

variable_types: level int

variable_types:
format
char *

variable_types: now long


decompiled_code:
__int64 __fastcall log_open(void *src, int a2, int a3)
{
FILE *v4; // r13
size_t v5; // r14

if ( log_fname || log_fp )
{
log_write(0, "Trying to open already opened log");
return 0LL;
}
log_stderr_level = a3;
log_fp = fopen((const char *)src, "a");
v4 = log_fp;
if ( !log_fp )
return 0LL;
v5 = strlen((const char *)src) + 1;
log_fname = (char *)malloc(v5);
if ( !log_fname )
{
fclose(v4);
log_fp = 0LL;
return 0LL;
}
memcpy(log_fname, src, v5);
log_file_level = a2;
fputc(10, v4);
fflush(log_fp);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *src, int a2, int a3)
{
FILE *v4; // r13
size_t v5; // r14

if ( log_fname || log_fp )
{
func2(0, "Trying to open already opened log");
return 0LL;
}
log_stderr_level = a3;
log_fp = fopen((const char *)src, "a");
v4 = log_fp;
if ( !log_fp )
return 0LL;
v5 = strlen((const char *)src) + 1;
log_fname = (char *)malloc(v5);
if ( !log_fname )
{
fclose(v4);
log_fp = 0LL;
return 0LL;
}
memcpy(log_fname, src, v5);
log_file_level = a2;
fputc(10, v4);
fflush(log_fp);
return 1LL;
}

original_code:
int log_open(char *filename, int file_level, int stderr_level)
{
  if (log_fname != ((void *)0) || log_fp != ((void *)0)) {
    log_write(0, "Trying to open already opened log");
    return 0;
  }
  log_stderr_level = stderr_level;
  log_fp = fopen(filename, "a");
  if (log_fp == ((void *)0))
    return 0;
  log_fname = malloc(strlen(filename) + 1);
  if (log_fname == ((void *)0)) {
    fclose(log_fp);
    log_fp = ((void *)0);
    return 0;
  }
  strcpy(log_fname, filename);
  log_file_level = file_level;
  fprintf(log_fp, "\n");
  fflush(log_fp);
  return 1;
}

variable_types:
filename
char *


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

result = cur_slot;
*cur_slot = 2;
return result;
}

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

result = cur_slot;
*cur_slot = 2;
return result;
}

original_code:
static void host_listen_init_hook(void)
{
  cur_slot->type = 2;
}

canonical_original_code:
static void host_listen_init_hook()
{
  cur_slot->type = 2;
}


decompiled_code:
__int64 host_init_hook()
{
*cur_slot = 3;
aio_setclose(&host_close_hook);
return aio_setread(rf_host_ver, 0LL, 12LL);
}

canonical_decompiled_code:
__int64 func1()
{
*cur_slot = 3;
aio_setclose(&host_close_hook);
return aio_setread(rf_host_ver, 0LL, 12LL);
}

original_code:
static void host_init_hook(void)
{
  cur_slot->type = 3;
  aio_setclose(host_close_hook);
  aio_setread(rf_host_ver, ((void *)0), 12);
}

canonical_original_code:
static void host_init_hook()
{
  cur_slot->type = 3;
  aio_setclose(host_close_hook);
  aio_setread(rf_host_ver, ((void *)0), 12);
}


decompiled_code:
__int64 host_accept_hook()
{
log_write(3LL, "Accepted host connection from %s", *(const char **)(cur_slot + 16LL));
return host_init_hook();
}

canonical_decompiled_code:
__int64 func2()
{
log_write(3LL, "Accepted host connection from %s", *(const char **)(cur_slot + 16LL));
return func1();
}

original_code:
static void host_accept_hook(void)
{
  log_write(3, "Accepted host connection from %s", cur_slot->name);
  host_init_hook();
}

canonical_original_code:
static void host_accept_hook()
{
  log_write(3, "Accepted host connection from %s", cur_slot->name);
  host_init_hook();
}


decompiled_code:
__int64 rf_host_initmsg()
{
__int64 v0; // rbx
_WORD *v1; // rax
unsigned __int16 v2; // dx
unsigned __int16 v3; // cx
__int64 v4; // rdx

v0 = cur_slot;
v1 = *(_WORD **)(cur_slot + 32LL);
v2 = __ROL2__(*v1, 8);
*(_WORD *)(cur_slot + 372LL) = v2;
v3 = __ROL2__(v1[1], 8);
*(_WORD *)(v0 + 374) = v3;
log_write(3LL, "Remote desktop geometry is %dx%d", v2, v3);
v4 = _byteswap_ulong(*(_DWORD *)(*(_QWORD *)(cur_slot + 32LL) + 20LL));
*(_DWORD *)(v0 + 368) = v4;
return aio_setread(rf_host_set_formats, 0LL, v4);
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbx
_WORD *v1; // rax
unsigned __int16 v2; // dx
unsigned __int16 v3; // cx
__int64 v4; // rdx

v0 = cur_slot;
v1 = *(_WORD **)(cur_slot + 32LL);
v2 = __ROL2__(*v1, 8);
*(_WORD *)(cur_slot + 372LL) = v2;
v3 = __ROL2__(v1[1], 8);
*(_WORD *)(v0 + 374) = v3;
log_write(3LL, "Remote desktop geometry is %dx%d", v2, v3);
v4 = _byteswap_ulong(*(_DWORD *)(*(_QWORD *)(cur_slot + 32LL) + 20LL));
*(_DWORD *)(v0 + 368) = v4;
return aio_setread(rf_host_set_formats, 0LL, v4);
}

original_code:
static void rf_host_initmsg(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  hs->fb_width = ((u_int16_t)((u_int8_t *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((u_int8_t *)cur_slot->readbuf)[1]);
  hs->fb_height = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[1]);
  log_write(3, "Remote desktop geometry is %dx%d",
            (int)hs->fb_width, (int)hs->fb_height);
  hs->temp_len = ((u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[0] << 24 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[1] << 16 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[2] << 8 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[20])[3]);;
  aio_setread(rf_host_set_formats, ((void *)0), hs->temp_len);
}

canonical_original_code:
static void rf_host_initmsg()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  hs->fb_width = ((u_int16_t)((unsigned int *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((unsigned int *)cur_slot->readbuf)[1]);
  hs->fb_height = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[1]);
  log_write(3, "Remote desktop geometry is %dx%d",
            (int)hs->fb_width, (int)hs->fb_height);
  hs->temp_len = ((u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[0] << 24 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[1] << 16 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[2] << 8 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[20])[3]);;
  aio_setread(rf_host_set_formats, ((void *)0), hs->temp_len);
}

variable_types: struct _HOST_SLOT { struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; } *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; } s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; } *

user_defined_types: struct _HOST_SLOT { struct _AIO_SLOT s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; }

user_defined_types: struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; }


decompiled_code:
__int64 rf_host_conn_failed()
{
log_write(1LL, "VNC connection failed: %.*s", *(unsigned int *)(cur_slot + 368LL), *(_QWORD *)(cur_slot + 32LL));
return aio_close(0LL);
}

canonical_decompiled_code:
__int64 func4()
{
log_write(1LL, "VNC connection failed: %.*s", *(unsigned int *)(cur_slot + 368LL), *(_QWORD *)(cur_slot + 32LL));
return aio_close(0LL);
}

original_code:
static void rf_host_conn_failed(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  log_write(1, "VNC connection failed: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  aio_close(0);
}

canonical_original_code:
static void rf_host_conn_failed()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  log_write(1, "VNC connection failed: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  aio_close(0);
}

user_defined_types: struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; }


decompiled_code:
__int64 rf_host_ver()
{
__int64 v0; // rbx
const unsigned __int16 *v1; // rax
unsigned int v3; // ebp
__int64 v4; // rbx
char v5[40]; // [rsp+0h] [rbp-28h] BYREF

v0 = *(_QWORD *)(cur_slot + 32LL);
if ( memcmp((const void *)v0, "RFB ", 4uLL)
|| (v1 = *_ctype_b_loc(), (v1[*(char *)(v0 + 4)] & 0x800) == 0)
|| (v1[*(char *)(v0 + 5)] & 0x800) == 0
|| (v1[*(char *)(v0 + 6)] & 0x800) == 0
|| *(_BYTE *)(v0 + 7) != 46
|| (v1[*(char *)(v0 + 8)] & 0x800) == 0
|| (v1[*(char *)(v0 + 9)] & 0x800) == 0
|| (v1[*(char *)(v0 + 10)] & 0x800) == 0
|| *(_BYTE *)(v0 + 11) != 10 )
{
log_write(1LL, "Wrong greeting data received from host");
return aio_close(0LL);
}
v3 = atoi((const char *)(v0 + 4));
v4 = (unsigned int)atoi((const char *)(v0 + 8));
log_write(4LL, "Remote RFB Protocol version is %d.%d", v3, v4);
if ( v3 != 3 )
{
log_write(1LL, "Wrong protocol version, expected %d.x", 3LL);
return aio_close(0LL);
}
if ( (_DWORD)v4 != 3 )
log_write(2LL, "Protocol sub-version does not match (ignoring)");
sprintf(v5, "RFB %03d.%03d\n", 3LL, 3LL);
aio_write(0LL);
return aio_setread(rf_host_auth, 0LL, 4LL);
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbx
const unsigned __int16 *v1; // rax
unsigned int v3; // ebp
__int64 v4; // rbx
char v5[40]; // [rsp+0h] [rbp-28h] BYREF

v0 = *(_QWORD *)(cur_slot + 32LL);
if ( memcmp((const void *)v0, "RFB ", 4uLL)
|| (v1 = *_ctype_b_loc(), (v1[*(char *)(v0 + 4)] & 0x800) == 0)
|| (v1[*(char *)(v0 + 5)] & 0x800) == 0
|| (v1[*(char *)(v0 + 6)] & 0x800) == 0
|| *(_BYTE *)(v0 + 7) != 46
|| (v1[*(char *)(v0 + 8)] & 0x800) == 0
|| (v1[*(char *)(v0 + 9)] & 0x800) == 0
|| (v1[*(char *)(v0 + 10)] & 0x800) == 0
|| *(_BYTE *)(v0 + 11) != 10 )
{
log_write(1LL, "Wrong greeting data received from host");
return aio_close(0LL);
}
v3 = atoi((const char *)(v0 + 4));
v4 = (unsigned int)atoi((const char *)(v0 + 8));
log_write(4LL, "Remote RFB Protocol version is %d.%d", v3, v4);
if ( v3 != 3 )
{
log_write(1LL, "Wrong protocol version, expected %d.x", 3LL);
return aio_close(0LL);
}
if ( (_DWORD)v4 != 3 )
log_write(2LL, "Protocol sub-version does not match (ignoring)");
sprintf(v5, "RFB %03d.%03d\n", 3LL, 3LL);
aio_write(0LL);
return aio_setread(rf_host_auth, 0LL, 4LL);
}

original_code:
static void rf_host_ver(void)
{
  char *buf = (char *)cur_slot->readbuf;
  int major = 3, minor = 3;
  int remote_major, remote_minor;
  char ver_msg[16];
  if ( strncmp(buf, "RFB ", 4) != 0 || !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short int) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short int) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[5]))] & (unsigned short int) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[6]))] & (unsigned short int) _ISdigit) ||
       buf[7] != '.' || !((*__ctype_b_loc ())[(int) ((buf[8]))] & (unsigned short int) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[9]))] & (unsigned short int) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[10]))] & (unsigned short int) _ISdigit) || buf[11] != '\n' ) {
    log_write(1, "Wrong greeting data received from host");
    aio_close(0);
    return;
  }
  remote_major = atoi(&buf[4]);
  remote_minor = atoi(&buf[8]);
  log_write(4, "Remote RFB Protocol version is %d.%d",
            remote_major, remote_minor);
  if (remote_major != major) {
    log_write(1, "Wrong protocol version, expected %d.x", major);
    aio_close(0);
    return;
  } else if (remote_minor != minor) {
    log_write(2, "Protocol sub-version does not match (ignoring)");
  }
  sprintf(ver_msg, "RFB %03d.%03d\n", abs(major) % 999, abs(minor) % 999);
  aio_write(((void *)0), ver_msg, 12);
  aio_setread(rf_host_auth, ((void *)0), 4);
}

canonical_original_code:
static void rf_host_ver()
{
  char *buf = (char *)cur_slot->readbuf;
  int major = 3; int minor = 3;
  int remote_major; int remote_minor;
  char ver_msg[16];
  if ( strncmp(buf, "RFB ", 4) != 0 || !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[4]))] & (unsigned short) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[5]))] & (unsigned short) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[6]))] & (unsigned short) _ISdigit) ||
       buf[7] != '.' || !((*__ctype_b_loc ())[(int) ((buf[8]))] & (unsigned short) _ISdigit) || !((*__ctype_b_loc ())[(int) ((buf[9]))] & (unsigned short) _ISdigit) ||
       !((*__ctype_b_loc ())[(int) ((buf[10]))] & (unsigned short) _ISdigit) || buf[11] != '\n' ) {
    log_write(1, "Wrong greeting data received from host");
    aio_close(0);
    return;
  }
  remote_major = atoi(&buf[4]);
  remote_minor = atoi(&buf[8]);
  log_write(4, "Remote RFB Protocol version is %d.%d",
            remote_major, remote_minor);
  if (remote_major != major) {
    log_write(1, "Wrong protocol version, expected %d.x", major);
    aio_close(0);
    return;
  } else if (remote_minor != minor) {
    log_write(2, "Protocol sub-version does not match (ignoring)");
  }
  sprintf(ver_msg, "RFB %03d.%03d\n", abs(major) % 999, abs(minor) % 999);
  aio_write(((void *)0), ver_msg, 12);
  aio_setread(rf_host_auth, ((void *)0), 4);
}

variable_types:
buf
char *

variable_types: major int

variable_types: minor int


decompiled_code:
__int64 send_client_initmsg()
{
log_write(5LL, "Requesting %s session", "non-shared");
aio_write(0LL);
return aio_setread(rf_host_initmsg, 0LL, 24LL);
}

canonical_decompiled_code:
__int64 func6()
{
log_write(5LL, "Requesting %s session", "non-shared");
aio_write(0LL);
return aio_setread(rf_host_initmsg, 0LL, 24LL);
}

original_code:
static void send_client_initmsg(void)
{
  u_int8_t shared_session = 1;
  log_write(5, "Requesting %s session",
            (shared_session != 0) ? "non-shared" : "shared");
  aio_write(((void *)0), &shared_session, 1);
  aio_setread(rf_host_initmsg, ((void *)0), 24);
}

canonical_original_code:
static void send_client_initmsg()
{
  unsigned int shared_session = 1;
  log_write(5, "Requesting %s session",
            (shared_session != 0) ? "non-shared" : "shared");
  aio_write(((void *)0), &shared_session, 1);
  aio_setread(rf_host_initmsg, ((void *)0), 24);
}