decompiled_code:
__int64 __fastcall lst_size(__int64 a1)
{
__int64 v1; // rax
unsigned int i; // er8

if ( !*(_QWORD *)a1 )
return 0LL;
v1 = *(_QWORD *)(*(_QWORD *)a1 + 8LL);
for ( i = 1; v1; ++i )
v1 = *(_QWORD *)(v1 + 8);
return i;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rax
unsigned int i; // er8

if ( !*(_QWORD *)a1 )
return 0LL;
v1 = *(_QWORD *)(*(_QWORD *)a1 + 8LL);
for ( i = 1; v1; ++i )
v1 = *(_QWORD *)(v1 + 8);
return i;
}

original_code:
int lst_size(list *l) {
 int siz = 1;
 node *n = l->head;
 if (!n) return 0;
 while ((n = n->next)) siz++;
 return siz;
}

canonical_original_code:
int lst_size(struct list *l) {
 int siz = 1;
 struct _node *n = l->head;
 if (!n) return 0;
 while ((n = n->next)) siz++;
 return siz;
}

variable_types: siz int


repo: mcsf/postman

decompiled_code:
__int64 __fastcall lst_pop(void **a1)
{
void *v1; // r8
unsigned int v2; // er12

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)v1;
*a1 = (void *)*((_QWORD *)v1 + 1);
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func11(void **a1)
{
void *v1; // r8
unsigned int v2; // er12

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)v1;
*a1 = (void *)*((_QWORD *)v1 + 1);
free(v1);
return v2;
}

original_code:
int lst_pop(list *l) {
 int res;
 node *n = l->head;
 if (!n) return -1;
 res = n->value;
 l->head = n->next;
 free(n);
 return res;
}

canonical_original_code:
int lst_pop(struct list *l) {
 int res;
 struct _node *n = l->head;
 if (!n) return -1;
 res = n->value;
 l->head = n->next;
 free(n);
 return res;
}

variable_types: struct list { struct _node { int value; struct _node *next; } *head; } *

variable_types: res int

return_type: int


decompiled_code:
__int64 __fastcall lst_first(_QWORD *a1)
{
__int64 result; // rax

if ( *a1 )
result = *(unsigned int *)*a1;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
__int64 result; // rax

if ( *a1 )
result = *(unsigned int *)*a1;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int lst_first(list *l) {
 if (!l->head) return -1;
 return l->head->value;
}

canonical_original_code:
int lst_first(struct list *l) {
 if (!l->head) return -1;
 return l->head->value;
}

user_defined_types: struct _node { int value; struct _node *next; }


decompiled_code:
void __fastcall lst_reverse(void **a1)
{
__int64 *v1; // rax
_QWORD *v2; // rbx
__int64 *v3; // r12
int v4; // eax

v1 = lst_new();
v2 = *a1;
if ( *a1 )
{
v3 = v1;
do
{
v2 = (_QWORD *)v2[1];
v4 = lst_pop(a1);
lst_push(v3, v4);
}
while ( v2 );
if ( !lst_empty(a1) )
puts("L IS NOT EMPTY");
*a1 = (void *)*v3;
free(v3);
}
}

canonical_decompiled_code:
void __fastcall func13(void **a1)
{
__int64 *v1; // rax
_QWORD *v2; // rbx
__int64 *v3; // r12
int v4; // eax

v1 = func7();
v2 = *a1;
if ( *a1 )
{
v3 = v1;
do
{
v2 = (_QWORD *)v2[1];
v4 = func11(a1);
lst_push(v3, v4);
}
while ( v2 );
if ( !func9(a1) )
puts("L IS NOT EMPTY");
*a1 = (void *)*v3;
free(v3);
}
}

original_code:
void lst_reverse(list *l) {
 list *new = lst_new();
 node *n = l->head;
 node *next;
 if (!n) return;
 do {
  next = n->next;
  lst_push(new, lst_pop(l));
 } while ((n = next));
 if (!lst_empty(l)) printf("L IS NOT EMPTY\n");
 l->head = new->head;
 free(new);
}

canonical_original_code:
void lst_reverse(struct list *l) {
 struct list *new = lst_new();
 struct _node *n = l->head;
 struct _node *next;
 if (!n) return;
 do {
  next = n->next;
  lst_push(new, lst_pop(l));
 } while ((n = next));
 if (!lst_empty(l)) printf("L IS NOT EMPTY\n");
 l->head = new->head;
 free(new);
}

variable_types: struct _node { int value; struct _node *next; } *

return_type: void


decompiled_code:
__int64 __fastcall lst_subst(_QWORD *a1, int a2, __int64 *a3)
{
_QWORD *v3; // rdi
__int64 v4; // rcx
__int64 v5; // rax
__int64 v6; // rdx
_QWORD *v7; // rax
_QWORD *v8; // rdi

v3 = (_QWORD *)*a1;
if ( !v3 )
return 0LL;
v4 = *a3;
if ( !*a3 )
return 0LL;
v5 = *a3;
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
}
while ( v5 );
v7 = v3;
v8 = (_QWORD *)v3[1];
if ( !v8 )
return 0LL;
while ( *(_DWORD *)v8 != a2 )
{
v7 = v8;
v8 = (_QWORD *)v8[1];
if ( !v8 )
return 0LL;
}
v7[1] = v4;
*(_QWORD *)(v6 + 8) = v8[1];
free(v8);
return 1LL;
}

original_code:
int lst_subst(list *l, int old, list *new) {
 node *pre, *cur, *new_head, *new_tail;
 if (!l->head || !new->head) return 0;
 pre = cur = new_head = new->head;
 while ((cur = cur->next)) pre = cur;
 new_tail = pre;
 pre = cur = l->head;
 while ((cur = cur->next)) {
  if (cur->value == old) {
   pre->next = new_head;
   new_tail->next = cur->next;
   free(cur);
   return 1;
  }
  pre = cur;
 }
 return 0;
}

canonical_original_code:
int lst_subst(struct list *l, int old, struct list *new) {
 struct _node *pre; struct _node *cur; struct _node *new_head; struct _node *new_tail;
 if (!l->head || !new->head) return 0;
 pre = cur = new_head = new->head;
 while ((cur = cur->next)) pre = cur;
 new_tail = pre;
 pre = cur = l->head;
 while ((cur = cur->next)) {
  if (cur->value == old) {
   pre->next = new_head;
   new_tail->next = cur->next;
   free(cur);
   return 1;
  }
  pre = cur;
 }
 return 0;
}

variable_types: old int

variable_types:
new
struct list *


decompiled_code:
int __fastcall lst_dump(unsigned int **a1)
{
unsigned int *i; // rbx
const char *v2; // rdx

for ( i = *a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
{
v2 = "";
if ( *((_QWORD *)i + 1) )
v2 = " ";
printf("%d%s", *i, v2);
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func15(unsigned int **a1)
{
unsigned int *i; // rbx
const char *v2; // rdx

for ( i = *a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
{
v2 = "";
if ( *((_QWORD *)i + 1) )
v2 = " ";
printf("%d%s", *i, v2);
}
return putchar(10);
}

original_code:
void lst_dump(list *l) {
 node *n;
 for (n = l->head; n; n = n->next)
  printf("%d%s", n->value, n->next ? " " : "");
 printf("\n");
}

canonical_original_code:
void lst_dump(struct list *l) {
 struct _node *n;
 for (n = l->head; n; n = n->next)
  printf("%d%s", n->value, n->next ? " " : "");
 printf("\n");
}


setup

decompiled_code:
int setup()
{
int v0; // ebx
char *v1; // rax
__int64 v2; // rbx
char *v3; // rbp
_QWORD *v4; // rax
bool v5; // cc
__int64 v6; // rbp
int v7; // ebx
int result; // eax
unsigned int **v9; // rdi
int v10; // [rsp+8h] [rbp-20h] BYREF
int v11; // [rsp+Ch] [rbp-1Ch] BYREF

scanf("%d\n%d\n%d\n", &n, &m, &init);
v0 = n;
v1 = (char *)malloc(8LL * n);
edges = (__int64)v1;
if ( v0 > 0 )
{
v2 = 0LL;
while ( 1 )
{
v3 = &v1[8 * v2++];
v4 = lst_new();
v5 = n <= (int)v2;
*(_QWORD *)v3 = v4;
if ( v5 )
break;
v1 = (char *)edges;
}
}
while ( scanf("%d %d\n", &v10, &v11) != -1 )
{
lst_insert_ordered(*(__int64 **)(edges + 8LL * v10 - 8), v11);
lst_insert_ordered(*(__int64 **)(edges + 8LL * v11 - 8), v10);
}
v6 = 0LL;
v7 = 0;
puts("Edge lists:");
result = n;
if ( n > 0 )
{
do
{
printf("%d\t: ", (unsigned int)++v7);
v9 = *(unsigned int ***)(edges + v6);
v6 += 8LL;
result = lst_dump(v9);
}
while ( v7 < n );
}
return result;
}

canonical_decompiled_code:
int func16()
{
int v0; // ebx
char *v1; // rax
__int64 v2; // rbx
char *v3; // rbp
_QWORD *v4; // rax
bool v5; // cc
__int64 v6; // rbp
int v7; // ebx
int result; // eax
unsigned int **v9; // rdi
int v10; // [rsp+8h] [rbp-20h] BYREF
int v11; // [rsp+Ch] [rbp-1Ch] BYREF

scanf("%d\n%d\n%d\n", &n, &m, &init);
v0 = n;
v1 = (char *)malloc(8LL * n);
edges = (__int64)v1;
if ( v0 > 0 )
{
v2 = 0LL;
while ( 1 )
{
v3 = &v1[8 * v2++];
v4 = func7();
v5 = n <= (int)v2;
*(_QWORD *)v3 = v4;
if ( v5 )
break;
v1 = (char *)edges;
}
}
while ( scanf("%d %d\n", &v10, &v11) != -1 )
{
lst_insert_ordered(*(__int64 **)(edges + 8LL * v10 - 8), v11);
lst_insert_ordered(*(__int64 **)(edges + 8LL * v11 - 8), v10);
}
v6 = 0LL;
v7 = 0;
puts("Edge lists:");
result = n;
if ( n > 0 )
{
do
{
printf("%d\t: ", (unsigned int)++v7);
v9 = *(unsigned int ***)(edges + v6);
v6 += 8LL;
result = func15(v9);
}
while ( v7 < n );
}
return result;
}

original_code:
void setup() {
 int i, a, b;
 scanf("%d\n%d\n%d\n", &n, &m, &init);
 edges = malloc(sizeof(list *) * n);
 for (i = 0; i < n; i++)
  edges[i] = lst_new();
 while (scanf("%d %d\n", &a, &b) != (-1)) {
  lst_insert_ordered(edges[a-1], b);
  lst_insert_ordered(edges[b-1], a);
 }
 printf("Edge lists:\n");
 for (i = 0; i < n; i++) {
  printf("%d\t: ", i+1);
  lst_dump(edges[i]);
 }
}

canonical_original_code:
void setup() {
 int i; int a; int b;
 scanf("%d\n%d\n%d\n", &n, &m, &init);
 edges = malloc(sizeof(struct list *) * n);
 for (i = 0; i < n; i++)
  edges[i] = lst_new();
 while (scanf("%d %d\n", &a, &b) != (-1)) {
  lst_insert_ordered(edges[a-1], b);
  lst_insert_ordered(edges[b-1], a);
 }
 printf("Edge lists:\n");
 for (i = 0; i < n; i++) {
  printf("%d\t: ", i+1);
  lst_dump(edges[i]);
 }
}

variable_types: i int

variable_types: a int

variable_types: b int


decompiled_code:
__int64 check_solvable()
{
__int64 *v0; // rdx
__int64 v1; // rsi
__int64 result; // rax
__int64 v3; // rdx

if ( n <= 0 )
return 1LL;
v0 = (__int64 *)edges;
v1 = edges + 8LL * (unsigned int)(n - 1) + 8;
while ( 1 )
{
result = lst_size(*v0);
if ( !(_DWORD)result )
break;
if ( (int)result % 2 == 1 )
return 0LL;
v0 = (__int64 *)(v3 + 8);
if ( v0 == (__int64 *)v1 )
return 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func17()
{
__int64 *v0; // rdx
__int64 v1; // rsi
__int64 result; // rax
__int64 v3; // rdx

if ( n <= 0 )
return 1LL;
v0 = (__int64 *)edges;
v1 = edges + 8LL * (unsigned int)(n - 1) + 8;
while ( 1 )
{
result = func10(*v0);
if ( !(_DWORD)result )
break;
if ( (int)result % 2 == 1 )
return 0LL;
v0 = (__int64 *)(v3 + 8);
if ( v0 == (__int64 *)v1 )
return 1LL;
}
return result;
}

original_code:
int check_solvable() {
 int i;
 for (i = 0; i < n; i++) {
  int size = lst_size(edges[i]);
  if (size == 0 || size % 2 == 1)
   return 0;
 }
 return 1;
}

variable_types: size int


decompiled_code:
__int64 __fastcall initial_vertex(int **a1)
{
int *v1; // rdx
__int64 v2; // rcx
__int64 v3; // rdx
unsigned int v4; // er8

v1 = *a1;
if ( !*a1 )
return 0LL;
v2 = edges;
while ( lst_empty(*(_QWORD **)(v2 + 8LL * *v1 - 8)) )
{
v1 = *(int **)(v3 + 8);
if ( !v1 )
return 0LL;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func18(int **a1)
{
int *v1; // rdx
__int64 v2; // rcx
__int64 v3; // rdx
unsigned int v4; // er8

v1 = *a1;
if ( !*a1 )
return 0LL;
v2 = edges;
while ( func9(*(_QWORD **)(v2 + 8LL * *v1 - 8)) )
{
v1 = *(int **)(v3 + 8);
if ( !v1 )
return 0LL;
}
return v4;
}

original_code:
int initial_vertex(list *cycle) {
 node *n;
 for (n = cycle->head; n; n = n->next) {
  int v = n->value;
  if (!lst_empty(edges[v-1]))
   return v;
 }
 return 0;
}

canonical_original_code:
int initial_vertex(struct list *cycle) {
 struct _node *n;
 for (n = cycle->head; n; n = n->next) {
  int v = n->value;
  if (!lst_empty(edges[v-1]))
   return v;
 }
 return 0;
}

variable_types: v int


decompiled_code:
__int64 collect_cycles()
{
_QWORD *v0; // rax
__int64 v1; // rsi
int v2; // ebp
__int64 *v3; // r12
int v4; // esi
void **v5; // rdi
__int64 result; // rax

v0 = lst_new();
v1 = (unsigned int)init;
cycles = (__int64)v0;
do
{
printf("NEW CYCLE, init=%d\n", v1);
v2 = init;
v3 = lst_new();
lst_push(v3, v2);
if ( lst_empty((_QWORD *)cycles) )
c_0 = (__int64)v3;
while ( 1 )
{
v5 = *(void ***)(edges + 8LL * v2 - 8);
if ( lst_empty(v5) )
break;
v4 = v2;
v2 = lst_pop(v5);
lst_remove(*(_QWORD ***)(edges + 8LL * v2 - 8), v4);
lst_push(v3, v2);
}
lst_dump((unsigned int **)v3);
lst_push((__int64 *)cycles, (int)v3);
result = initial_vertex((int **)v3);
init = result;
v1 = (unsigned int)result;
}
while ( (_DWORD)result );
return result;
}

canonical_decompiled_code:
__int64 func19()
{
_QWORD *v0; // rax
__int64 v1; // rsi
int v2; // ebp
__int64 *v3; // r12
int v4; // esi
void **v5; // rdi
__int64 result; // rax

v0 = func7();
v1 = (unsigned int)init;
cycles = (__int64)v0;
do
{
printf("NEW CYCLE, init=%d\n", v1);
v2 = init;
v3 = func7();
lst_push(v3, v2);
if ( func9((_QWORD *)cycles) )
c_0 = (__int64)v3;
while ( 1 )
{
v5 = *(void ***)(edges + 8LL * v2 - 8);
if ( func9(v5) )
break;
v4 = v2;
v2 = func11(v5);
func8(*(_QWORD ***)(edges + 8LL * v2 - 8), v4);
lst_push(v3, v2);
}
func15((unsigned int **)v3);
lst_push((__int64 *)cycles, (int)v3);
result = func18((int **)v3);
init = result;
v1 = (unsigned int)result;
}
while ( (_DWORD)result );
return result;
}

original_code:
void collect_cycles() {
 int cur;
 list *cycle;
 cycles = lst_new();
 do {
  printf("NEW CYCLE, init=%d\n", init);
  cur = init;
  cycle = lst_new();
  lst_push(cycle, cur);
  if (lst_empty(cycles)) c_0 = cycle;
  while (!lst_empty(edges[cur-1])) {
   int next = lst_pop(edges[cur-1]);
   lst_remove(edges[next-1], cur);
   lst_push(cycle, next);
   cur = next;
  }
  lst_dump(cycle);
  lst_push(cycles, (int) cycle);
 } while ((init = initial_vertex(cycle)));
}

canonical_original_code:
void collect_cycles() {
 int cur;
 struct list *cycle;
 cycles = lst_new();
 do {
  printf("NEW CYCLE, init=%d\n", init);
  cur = init;
  cycle = lst_new();
  lst_push(cycle, cur);
  if (lst_empty(cycles)) c_0 = cycle;
  while (!lst_empty(edges[cur-1])) {
   int next = lst_pop(edges[cur-1]);
   lst_remove(edges[next-1], cur);
   lst_push(cycle, next);
   cur = next;
  }
  lst_dump(cycle);
  lst_push(cycles, (int) cycle);
 } while ((init = initial_vertex(cycle)));
}

variable_types: cur int

variable_types: next int


decompiled_code:
void merge_cycles()
{
void **v0; // r15
unsigned int v1; // er12
_QWORD *v2; // r13
int v3; // ebx
__int64 v4; // rbp
__int64 v5; // r12
__int64 *v6; // r15
__int64 v7; // rbp
unsigned int **v8; // r14

v0 = (void **)cycles;
v1 = lst_size(cycles);
v2 = malloc(8LL * (int)v1);
v3 = v1 - 1;
if ( (int)(v1 - 1) < 0 )
{
printf("SIZE: %d\n", v1);
}
else
{
v4 = v3;
while ( 1 )
{
v2[v4--] = (int)lst_pop(v0);
if ( (int)v4 < 0 )
break;
v0 = (void **)cycles;
}
printf("SIZE: %d\n", v1);
if ( v3 > 0 )
{
v5 = (__int64)&v2[(int)v1 - 2];
do
{
v6 = *(__int64 **)(v5 + 8);
v7 = (unsigned int)lst_first(v6);
printf("<-- MERGING AT %d\n", v7);
v8 = *(unsigned int ***)v5;
lst_dump(*(unsigned int ***)v5);
if ( !(unsigned int)lst_subst(v8, v7, v6) )
{
puts("-+-+-+- OOPS");
raise(10);
}
--v3;
v5 -= 8LL;
lst_dump(v8);
printf("--> MERGED INTO %d\n", (unsigned int)v3);
}
while ( v3 );
}
}
free(v2);
}

canonical_decompiled_code:
void func20()
{
void **v0; // r15
unsigned int v1; // er12
_QWORD *v2; // r13
int v3; // ebx
__int64 v4; // rbp
__int64 v5; // r12
__int64 *v6; // r15
__int64 v7; // rbp
unsigned int **v8; // r14

v0 = (void **)cycles;
v1 = func10(cycles);
v2 = malloc(8LL * (int)v1);
v3 = v1 - 1;
if ( (int)(v1 - 1) < 0 )
{
printf("SIZE: %d\n", v1);
}
else
{
v4 = v3;
while ( 1 )
{
v2[v4--] = (int)func11(v0);
if ( (int)v4 < 0 )
break;
v0 = (void **)cycles;
}
printf("SIZE: %d\n", v1);
if ( v3 > 0 )
{
v5 = (__int64)&v2[(int)v1 - 2];
do
{
v6 = *(__int64 **)(v5 + 8);
v7 = (unsigned int)func12(v6);
printf("<-- MERGING AT %d\n", v7);
v8 = *(unsigned int ***)v5;
func15(*(unsigned int ***)v5);
if ( !(unsigned int)func14(v8, v7, v6) )
{
puts("-+-+-+- OOPS");
raise(10);
}
--v3;
v5 -= 8LL;
func15(v8);
printf("--> MERGED INTO %d\n", (unsigned int)v3);
}
while ( v3 );
}
}
free(v2);
}

original_code:
void merge_cycles() {
 int i, n_cycles = lst_size(cycles);
 list **cycles_arr = malloc(sizeof(list *) * n_cycles);
 for (i = n_cycles - 1; i >= 0; i--)
  cycles_arr[i] = (list *) lst_pop(cycles);
 printf("SIZE: %d\n", n_cycles);
 for (i = n_cycles - 1; i > 0; i--) {
  list *c_i = cycles_arr[i];
  int end = lst_first(c_i);
  printf("<-- MERGING AT %d\n", end);
  lst_dump(cycles_arr[i-1]);
  if (!lst_subst(cycles_arr[i-1], end, c_i)) {
   printf("-+-+-+- OOPS\n");
   raise(10);
  }
  lst_dump(cycles_arr[i-1]);
  printf("--> MERGED INTO %d\n", i-1);
 }
 free(cycles_arr);
}

canonical_original_code:
void merge_cycles() {
 int i; int n_cycles = lst_size(cycles);
 struct list **cycles_arr = malloc(sizeof(list *) * n_cycles);
 for (i = n_cycles - 1; i >= 0; i--)
  cycles_arr[i] = (struct list *) lst_pop(cycles);
 printf("SIZE: %d\n", n_cycles);
 for (i = n_cycles - 1; i > 0; i--) {
  struct list *c_i = cycles_arr[i];
  int end = lst_first(c_i);
  printf("<-- MERGING AT %d\n", end);
  lst_dump(cycles_arr[i-1]);
  if (!lst_subst(cycles_arr[i-1], end, c_i)) {
   printf("-+-+-+- OOPS\n");
   raise(10);
  }
  lst_dump(cycles_arr[i-1]);
  printf("--> MERGED INTO %d\n", i-1);
 }
 free(cycles_arr);
}

variable_types: struct list { struct _node { int value; struct _node *next; } *head; } **

variable_types: end int


output

decompiled_code:
int output()
{
lst_reverse((void **)c_0);
return lst_dump((unsigned int **)c_0);
}

canonical_decompiled_code:
int func21()
{
func13((void **)c_0);
return func15((unsigned int **)c_0);
}

original_code:
void output() {
 lst_reverse(c_0);
 lst_dump(c_0);
}


repo: mczoden/hems

decompiled_code:
void __noreturn sig_hdl()
{
pthread_cancel(tid_uld);
pthread_cancel(tid_gen);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
sleep(1u);
uld_deinit();
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
pthread_cancel(tid_uld);
pthread_cancel(tid_gen);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
sleep(1u);
uld_deinit();
exit(0);
}

original_code:
static void sig_hdl(int sig)
{
    pthread_cancel(tid_uld);
    pthread_cancel(tid_gen);
    pthread_mutex_destroy(&mtx_gen);
    pthread_mutex_destroy(&mtx_uld);
    sleep(1);
    uld_deinit();
    exit(0);
}

variable_types: sig int


decompiled_code:
void __fastcall __noreturn gen_thread_hdl(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
pthread_mutex_lock(&mtx_gen);
if ( (unsigned int)gen(a1) )
dbg(2LL, "%s.", "Generate kpi file failed");
else
pthread_mutex_unlock(&mtx_uld);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
pthread_mutex_lock(&mtx_gen);
if ( (unsigned int)gen(a1) )
dbg(2LL, "%s.", "Generate kpi file failed");
else
pthread_mutex_unlock(&mtx_uld);
}
}

original_code:
static void *gen_thread_hdl(void *arg)
{
    const cheer_ctx_t *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_gen);
        if (gen(ctx) != 0) {
            dbg((1 << 1), "%s.", "Generate kpi file failed");
        } else {
            pthread_mutex_unlock(&mtx_uld);
        }
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
static void *gen_thread_hdl(void *arg)
{
    struct cheer_ctx *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_gen);
        if (gen(ctx) != 0) {
            dbg((1 << 1), "%s.", "Generate kpi file failed");
        } else {
            pthread_mutex_unlock(&mtx_uld);
        }
    }
    pthread_exit(((void *)0));
}

variable_types:
arg
void *

variable_types: struct cheer_ctx { long t_beg_pro; char cfg[256]; struct { int h; int m; } tz; int interval; long t_uld_time; char sn[256]; char url[256]; int port; char username[256]; char password[256]; _Bool need_bak; char bak_dir[256]; int dbg_lvl; } *

return_type: void *


decompiled_code:
void __fastcall __noreturn uld_thread_hdl(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
do
pthread_mutex_lock(&mtx_uld);
while ( !(unsigned int)uld(a1) );
dbg(2LL, "%s.", "Upload kpi file failed, retry at next intval");
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
do
pthread_mutex_lock(&mtx_uld);
while ( !(unsigned int)uld(a1) );
dbg(2LL, "%s.", "Upload kpi file failed, retry at next intval");
}
}

original_code:
static void *uld_thread_hdl(void *arg)
{
    const cheer_ctx_t *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_uld);
        if (uld(ctx) != 0) {
            dbg((1 << 1), "%s.", "Upload kpi file failed, retry at next intval");
        }
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
static void *uld_thread_hdl(void *arg)
{
    struct cheer_ctx *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_uld);
        if (uld(ctx) != 0) {
            dbg((1 << 1), "%s.", "Upload kpi file failed, retry at next intval");
        }
    }
    pthread_exit(((void *)0));
}

user_defined_types: struct cheer_ctx { long t_beg_pro; char cfg[256]; struct { int h; int m; } tz; int interval; long t_uld_time; char sn[256]; char url[256]; int port; char username[256]; char password[256]; _Bool need_bak; char bak_dir[256]; int dbg_lvl; }


decompiled_code:
int __fastcall print_usage(const char *a1)
{
printf("Usage: %s [OPTIONS]\n", a1);
puts(" --config CONFIG specify config file path");
return puts(" --help display this help and exit");
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
printf("Usage: %s [OPTIONS]\n", a1);
puts(" --config CONFIG specify config file path");
return puts(" --help display this help and exit");
}

original_code:
static void print_usage(const char *argv0)
{
    printf("Usage: %s [OPTIONS]\n", argv0);
    printf("  --config CONFIG   specify config file path\n");
    printf("  --help            display this help and exit\n");
}

canonical_original_code:
static void print_usage(char *argv0)
{
    printf("Usage: %s [OPTIONS]\n", argv0);
    printf("  --config CONFIG   specify config file path\n");
    printf("  --help            display this help and exit\n");
}


decompiled_code:
__int64 __fastcall parser_para(__int64 a1, int a2, char *const *a3)
{
int v4; // eax
unsigned int v5; // er8
struct option v7; // [rsp+0h] [rbp-78h] BYREF
const char *v8; // [rsp+20h] [rbp-58h]
int v9; // [rsp+28h] [rbp-50h]
__int64 v10; // [rsp+30h] [rbp-48h]
int v11; // [rsp+38h] [rbp-40h]
__int64 v12; // [rsp+40h] [rbp-38h]
int v13; // [rsp+48h] [rbp-30h]
__int64 v14; // [rsp+50h] [rbp-28h]
int v15; // [rsp+58h] [rbp-20h]

v7.name = "help";
v7.has_arg = 0;
v7.flag = 0LL;
v7.val = 104;
v8 = "config";
v9 = 1;
v10 = 0LL;
v11 = 102;
v12 = 0LL;
v13 = 0;
v14 = 0LL;
v15 = 0;
while ( 1 )
{
v4 = getopt_long(a2, a3, "hf:", &v7, 0LL);
if ( v4 != 102 )
break;
strncpy((char *)(a1 + 8), optarg, 0xFFuLL);
}
if ( v4 > 102 )
{
if ( v4 == 104 )
{
print_usage(*(const char **)a3);
exit(0);
}
goto LABEL_9;
}
v5 = 0;
if ( v4 != -1 )
{
if ( v4 == 63 )
{
print_usage(*(const char **)a3);
exit(1);
}
LABEL_9:
v5 = -1;
}
return v5;
}

original_code:
static int parser_para(cheer_ctx_t *ctx, int argc, char *argv[])
{
    int opt = 0;
    struct option long_opts[] = {
        { "help", 0, 0, 'h' },
        { "config", 1, 0, 'f' },
        { 0, 0, 0, 0 }
    };
    do {
        opt = getopt_long(argc, argv, "hf:", long_opts, ((void *)0));
        switch (opt) {
        case 'h':
            print_usage(argv[0]);
            exit(0);
            break;
        case 'f':
            strncpy(ctx->cfg, optarg, sizeof(ctx->cfg) - 1);
            break;
        case '?':
            print_usage(argv[0]);
            exit(1);
            break;
        case -1:
            break;
        default:
            return -1;
        }
    } while (opt != -1);
    return 0;
}

canonical_original_code:
static int parser_para(struct cheer_ctx *ctx, int argc, char *argv[])
{
    int opt = 0;
    struct option long_opts[] = {
        { "help", 0, 0, 'h' },
        { "config", 1, 0, 'f' },
        { 0, 0, 0, 0 }
    };
    do {
        opt = getopt_long(argc, argv, "hf:", long_opts, ((void *)0));
        switch (opt) {
        case 'h':
            print_usage(argv[0]);
            exit(0);
            break;
        case 'f':
            strncpy(ctx->cfg, optarg, sizeof(ctx->cfg) - 1);
            break;
        case '?':
            print_usage(argv[0]);
            exit(1);
            break;
        case -1:
            break;
        default:
            return -1;
        }
    } while (opt != -1);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

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


original_code:
static int time_init(cheer_ctx_t *ctx)
{
    struct tm tm_beg;
    long int gmt_h = 0, gmt_m = 0;
    ctx->t_beg_pro = time(((void *)0));
    memcpy(&tm_beg, localtime(&(ctx->t_beg_pro)), sizeof(tm_beg));
    gmt_h = tm_beg.__tm_gmtoff / 3600;
    gmt_m = abs((tm_beg.__tm_gmtoff - gmt_h * 3600) / 60);
    ctx->tz.h = gmt_h;
    ctx->tz.m = gmt_m;
    return 0;
}

canonical_original_code:
static int time_init(struct cheer_ctx *ctx)
{
    struct tm tm_beg;
    long gmt_h = 0; long gmt_m = 0;
    ctx->t_beg_pro = time(((void *)0));
    memcpy(&tm_beg, localtime(&(ctx->t_beg_pro)), sizeof(tm_beg));
    gmt_h = tm_beg.__tm_gmtoff / 3600;
    gmt_m = abs((tm_beg.__tm_gmtoff - gmt_h * 3600) / 60);
    ctx->tz.h = gmt_h;
    ctx->tz.m = gmt_m;
    return 0;
}

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


decompiled_code:
__int64 __fastcall chk_and_prep_bak_dir(char *path)
{
int v2; // edi
__int64 result; // rax
char *v4; // rax
struct stat v5; // [rsp+0h] [rbp-98h] BYREF

memset(&v5, 0, sizeof(v5));
if ( stat(path, &v5) )
{
v2 = *_errno_location();
if ( v2 == 2 )
{
if ( !mkdir(path, 0x1FFu) )
return 0LL;
dbg(1LL, "Create directory: %s failed.", path);
result = 0xFFFFFFFFLL;
}
else
{
v4 = strerror(v2);
dbg(1LL, "Invoke stat() failed: %s.", v4);
result = 0xFFFFFFFFLL;
}
}
else
{
if ( (v5.st_mode & 0xF000) == 0x4000 )
return 0LL;
dbg(1LL, "%s has been existed but not a directory.", path);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *path)
{
int v2; // edi
__int64 result; // rax
char *v4; // rax
struct stat v5; // [rsp+0h] [rbp-98h] BYREF

memset(&v5, 0, sizeof(v5));
if ( stat(path, &v5) )
{
v2 = *_errno_location();
if ( v2 == 2 )
{
if ( !mkdir(path, 0x1FFu) )
return 0LL;
dbg(1LL, "Create directory: %s failed.", path);
result = 0xFFFFFFFFLL;
}
else
{
v4 = strerror(v2);
dbg(1LL, "Invoke stat() failed: %s.", v4);
result = 0xFFFFFFFFLL;
}
}
else
{
if ( (v5.st_mode & 0xF000) == 0x4000 )
return 0LL;
dbg(1LL, "%s has been existed but not a directory.", path);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int chk_and_prep_bak_dir(const char *dirname)
{
    struct stat st = { 0 };
    if (stat(dirname, &st) != 0) {
        if ((*__errno_location ()) == 2) {
            if (mkdir(dirname, 0777) != 0) {
                dbg((1 << 0), "Create directory: %s failed.", dirname);
                return -1;
            }
        } else {
            dbg((1 << 0), "Invoke stat() failed: %s.", strerror((*__errno_location ())));
            return -1;
        }
    } else {
        if (!((((st.st_mode)) & 0170000) == (0040000))) {
            dbg((1 << 0), "%s has been existed but not a directory.", dirname);
            return -1;
        }
    }
    return 0;
}

canonical_original_code:
static int chk_and_prep_bak_dir(char *dirname)
{
    struct stat st = { 0 };
    if (stat(dirname, &st) != 0) {
        if ((*__errno_location ()) == 2) {
            if (mkdir(dirname, 0777) != 0) {
                dbg((1 << 0), "Create directory: %s failed.", dirname);
                return -1;
            }
        } else {
            dbg((1 << 0), "Invoke stat() failed: %s.", strerror((*__errno_location ())));
            return -1;
        }
    } else {
        if (!((((st.st_mode)) & 0170000) == (0040000))) {
            dbg((1 << 0), "%s has been existed but not a directory.", dirname);
            return -1;
        }
    }
    return 0;
}

variable_types:
dirname
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall permit_to_run(__int64 a1, const char *a2)
{
const char *v2; // rdx
char *v4; // rbp

v2 = "Upload interval too short";
if ( *(int *)(a1 + 272) <= 1 )
goto LABEL_12;
v2 = "Serial number should not be empty";
if ( !*(_BYTE *)(a1 + 288) )
goto LABEL_12;
v2 = "Upload URL should not be empty";
if ( !*(_BYTE *)(a1 + 544) )
goto LABEL_12;
if ( *(_BYTE *)(a1 + 1316) )
{
v4 = (char *)(a1 + 1317);
if ( !*(_BYTE *)(a1 + 1317) )
{
dbg(2LL, "Backup directory is not specified, default: %s.", "./backup/");
a2 = "./backup/";
strncpy(v4, "./backup/", 0xFFuLL);
}
if ( (unsigned int)chk_and_prep_bak_dir(v4) )
{
v2 = "Backup directory can't be used";
LABEL_12:
dbg(1LL, "%s.", v2);
return 0LL;
}
}
if ( !*(_DWORD *)(a1 + 1576) )
{
a2 = "%s.";
dbg(4LL, "%s.", "Debug level is 0, no dbg will be displayed from now");
}
set_dbg_lvl(*(unsigned int *)(a1 + 1576), a2, v2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2)
{
const char *v2; // rdx
char *v4; // rbp

v2 = "Upload interval too short";
if ( *(int *)(a1 + 272) <= 1 )
goto LABEL_12;
v2 = "Serial number should not be empty";
if ( !*(_BYTE *)(a1 + 288) )
goto LABEL_12;
v2 = "Upload URL should not be empty";
if ( !*(_BYTE *)(a1 + 544) )
goto LABEL_12;
if ( *(_BYTE *)(a1 + 1316) )
{
v4 = (char *)(a1 + 1317);
if ( !*(_BYTE *)(a1 + 1317) )
{
dbg(2LL, "Backup directory is not specified, default: %s.", "./backup/");
a2 = "./backup/";
strncpy(v4, "./backup/", 0xFFuLL);
}
if ( (unsigned int)func6(v4) )
{
v2 = "Backup directory can't be used";
LABEL_12:
dbg(1LL, "%s.", v2);
return 0LL;
}
}
if ( !*(_DWORD *)(a1 + 1576) )
{
a2 = "%s.";
dbg(4LL, "%s.", "Debug level is 0, no dbg will be displayed from now");
}
set_dbg_lvl(*(unsigned int *)(a1 + 1576), a2, v2);
return 1LL;
}

original_code:
static _Bool permit_to_run(cheer_ctx_t *ctx)
{
    if (ctx->interval <= 1) {
        dbg((1 << 0), "%s.", "Upload interval too short");
        return 0;
    }
    if (strlen(ctx->sn) == 0) {
        dbg((1 << 0), "%s.", "Serial number should not be empty");
        return 0;
    }
    if (strlen(ctx->url) == 0) {
        dbg((1 << 0), "%s.", "Upload URL should not be empty");
        return 0;
    }
    if (ctx->need_bak) {
        if (strlen(ctx->bak_dir) == 0) {
            dbg((1 << 1), "Backup directory is not specified, default: %s.", "./backup/");
            strncpy(ctx->bak_dir, "./backup/", sizeof(ctx->bak_dir) - 1);
        }
        if (chk_and_prep_bak_dir(ctx->bak_dir) != 0) {
            dbg((1 << 0), "%s.", "Backup directory can't be used");
            return 0;
        }
    }
    if (ctx->dbg_lvl == (0)) {
        dbg((1 << 2), "%s.", "Debug level is 0, no dbg will be displayed from now");
    }
    set_dbg_lvl(ctx->dbg_lvl);
    return 1;
}

canonical_original_code:
static _Bool permit_to_run(struct cheer_ctx *ctx)
{
    if (ctx->interval <= 1) {
        dbg((1 << 0), "%s.", "Upload interval too short");
        return 0;
    }
    if (strlen(ctx->sn) == 0) {
        dbg((1 << 0), "%s.", "Serial number should not be empty");
        return 0;
    }
    if (strlen(ctx->url) == 0) {
        dbg((1 << 0), "%s.", "Upload URL should not be empty");
        return 0;
    }
    if (ctx->need_bak) {
        if (strlen(ctx->bak_dir) == 0) {
            dbg((1 << 1), "Backup directory is not specified, default: %s.", "./backup/");
            strncpy(ctx->bak_dir, "./backup/", sizeof(ctx->bak_dir) - 1);
        }
        if (chk_and_prep_bak_dir(ctx->bak_dir) != 0) {
            dbg((1 << 0), "%s.", "Backup directory can't be used");
            return 0;
        }
    }
    if (ctx->dbg_lvl == (0)) {
        dbg((1 << 2), "%s.", "Debug level is 0, no dbg will be displayed from now");
    }
    set_dbg_lvl(ctx->dbg_lvl);
    return 1;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // bp
int v4; // eax
int v5; // eax
char v6; // bl
char *v7; // rax
char *v9; // rax
const char *v10; // rdx
struct timeval v11; // [rsp+0h] [rbp-698h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-688h] BYREF
time_t arg[201]; // [rsp+50h] [rbp-648h] BYREF

v11 = 0LL;
memset(arg, 0, 0x630uLL);
if ( (unsigned int)parser_para((__int64)arg, argc, (char *const *)argv) )
{
v10 = "Parser parameters failed, exit";
goto LABEL_19;
}
if ( !LOBYTE(arg[1]) )
{
v10 = "Specify the config file with -f";
goto LABEL_19;
}
if ( (unsigned int)read_cfg(arg, &arg[1]) )
{
v10 = "Read config file: %s failed";
goto LABEL_19;
}
if ( (unsigned int)time_init(arg) )
{
v10 = "Initiate time failed, exit";
LABEL_19:
dbg(1LL, "%s.", v10);
goto LABEL_20;
}
if ( (unsigned int)uld_init(arg, &arg[1]) || (v3 = permit_to_run((__int64)arg, (const char *)&arg[1])) == 0 )
LABEL_20:
exit(1);
pthread_mutex_init(&mtx_gen, 0LL);
pthread_mutex_init(&mtx_uld, 0LL);
pthread_mutex_lock(&mtx_gen);
pthread_mutex_lock(&mtx_uld);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v4 = pthread_create(&tid_uld, &attr, (void *(*)(void *))uld_thread_hdl, arg);
if ( v4 )
{
v9 = strerror(v4);
dbg(1LL, "Create upload pthread failed: %s.", v9);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
else
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v5 = pthread_create(&tid_gen, &attr, (void *(*)(void *))gen_thread_hdl, arg);
if ( !v5 )
{
v6 = 0;
pthread_attr_destroy(&attr);
_sysv_signal(2, (__sighandler_t)sig_hdl);
_sysv_signal(15, (__sighandler_t)sig_hdl);
while ( 1 )
{
if ( (time(0LL) - arg[35]) % SLODWORD(arg[34]) )
{
v6 = 0;
}
else if ( !v6 )
{
v6 = v3;
pthread_mutex_unlock(&mtx_gen);
}
v11.tv_sec = 0LL;
v11.tv_usec = 100000LL;
select(0, 0LL, 0LL, 0LL, &v11);
}
}
v7 = strerror(v5);
dbg(1LL, "Create generate pthread failed: %s.", v7);
pthread_cancel(tid_uld);
sleep(1u);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // bp
int v4; // eax
int v5; // eax
char v6; // bl
char *v7; // rax
char *v9; // rax
const char *v10; // rdx
struct timeval v11; // [rsp+0h] [rbp-698h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-688h] BYREF
time_t arg[201]; // [rsp+50h] [rbp-648h] BYREF

v11 = 0LL;
memset(arg, 0, 0x630uLL);
if ( (unsigned int)func4((__int64)arg, argc, (char *const *)argv) )
{
v10 = "Parser parameters failed, exit";
goto LABEL_19;
}
if ( !LOBYTE(arg[1]) )
{
v10 = "Specify the config file with -f";
goto LABEL_19;
}
if ( (unsigned int)read_cfg(arg, &arg[1]) )
{
v10 = "Read config file: %s failed";
goto LABEL_19;
}
if ( (unsigned int)func5(arg) )
{
v10 = "Initiate time failed, exit";
LABEL_19:
dbg(1LL, "%s.", v10);
goto LABEL_20;
}
if ( (unsigned int)uld_init(arg, &arg[1]) || (v3 = func7((__int64)arg, (const char *)&arg[1])) == 0 )
LABEL_20:
exit(1);
pthread_mutex_init(&mtx_gen, 0LL);
pthread_mutex_init(&mtx_uld, 0LL);
pthread_mutex_lock(&mtx_gen);
pthread_mutex_lock(&mtx_uld);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v4 = pthread_create(&tid_uld, &attr, (void *(*)(void *))uld_thread_hdl, arg);
if ( v4 )
{
v9 = strerror(v4);
dbg(1LL, "Create upload pthread failed: %s.", v9);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
else
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v5 = pthread_create(&tid_gen, &attr, (void *(*)(void *))gen_thread_hdl, arg);
if ( !v5 )
{
v6 = 0;
pthread_attr_destroy(&attr);
_sysv_signal(2, (__sighandler_t)sig_hdl);
_sysv_signal(15, (__sighandler_t)sig_hdl);
while ( 1 )
{
if ( (time(0LL) - arg[35]) % SLODWORD(arg[34]) )
{
v6 = 0;
}
else if ( !v6 )
{
v6 = v3;
pthread_mutex_unlock(&mtx_gen);
}
v11.tv_sec = 0LL;
v11.tv_usec = 100000LL;
select(0, 0LL, 0LL, 0LL, &v11);
}
}
v7 = strerror(v5);
dbg(1LL, "Create generate pthread failed: %s.", v7);
pthread_cancel(tid_uld);
sleep(1u);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
return -1;
}

original_code:
int main(int argc, char *argv[])
{
    cheer_ctx_t ctx = { 0 };
    pthread_attr_t attr;
    _Bool has_gen = 0;
    struct timeval tv = { 0 };
    int err;
    if (parser_para(&ctx, argc, argv) != 0) {
        dbg((1 << 0), "%s.", "Parser parameters failed, exit");
        exit(1);
    }
    if (strlen(ctx.cfg) == 0) {
        dbg((1 << 0), "%s.", "Specify the config file with -f");
        exit(1);
    }
    if (read_cfg(&ctx, ctx.cfg) != 0) {
        dbg((1 << 0), "%s.", "Read config file: %s failed");
        exit(1);
    }
    if (time_init(&ctx) != 0) {
        dbg((1 << 0), "%s.", "Initiate time failed, exit");
        exit(1);
    }
    if (uld_init() != 0) {
        exit(1);
    }
    if (!permit_to_run(&ctx)) {
        exit(1);
    }
    pthread_mutex_init(&mtx_gen, ((void *)0));
    pthread_mutex_init(&mtx_uld, ((void *)0));
    pthread_mutex_lock(&mtx_gen);
    pthread_mutex_lock(&mtx_uld);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_uld, &attr, uld_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create upload pthread failed: %s.", strerror(err));
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_gen, &attr, gen_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create generate pthread failed: %s.", strerror(err));
        pthread_cancel(tid_uld);
        sleep(1);
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_destroy(&attr);
    signal(2, sig_hdl);
    signal(15, sig_hdl);
    while (1) {
        if ((time(((void *)0)) - ctx.t_uld_time) % ctx.interval == 0) {
            if (!has_gen) {
                pthread_mutex_unlock(&mtx_gen);
                has_gen = 1;
            }
        } else {
            has_gen = 0;
        }
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        select(0, ((void *)0), ((void *)0), ((void *)0), &tv);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct cheer_ctx ctx = { 0 };
    union pthread_attr_t attr;
    _Bool has_gen = 0;
    struct timeval tv = { 0 };
    int err;
    if (parser_para(&ctx, argc, argv) != 0) {
        dbg((1 << 0), "%s.", "Parser parameters failed, exit");
        exit(1);
    }
    if (strlen(ctx.cfg) == 0) {
        dbg((1 << 0), "%s.", "Specify the config file with -f");
        exit(1);
    }
    if (read_cfg(&ctx, ctx.cfg) != 0) {
        dbg((1 << 0), "%s.", "Read config file: %s failed");
        exit(1);
    }
    if (time_init(&ctx) != 0) {
        dbg((1 << 0), "%s.", "Initiate time failed, exit");
        exit(1);
    }
    if (uld_init() != 0) {
        exit(1);
    }
    if (!permit_to_run(&ctx)) {
        exit(1);
    }
    pthread_mutex_init(&mtx_gen, ((void *)0));
    pthread_mutex_init(&mtx_uld, ((void *)0));
    pthread_mutex_lock(&mtx_gen);
    pthread_mutex_lock(&mtx_uld);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_uld, &attr, uld_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create upload pthread failed: %s.", strerror(err));
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_gen, &attr, gen_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create generate pthread failed: %s.", strerror(err));
        pthread_cancel(tid_uld);
        sleep(1);
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_destroy(&attr);
    signal(2, sig_hdl);
    signal(15, sig_hdl);
    while (1) {
        if ((time(((void *)0)) - ctx.t_uld_time) % ctx.interval == 0) {
            if (!has_gen) {
                pthread_mutex_unlock(&mtx_gen);
                has_gen = 1;
            }
        } else {
            has_gen = 0;
        }
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        select(0, ((void *)0), ((void *)0), ((void *)0), &tv);
    }
    return 0;
}

variable_types: struct cheer_ctx { long t_beg_pro; char cfg[256]; struct { int h; int m; } tz; int interval; long t_uld_time; char sn[256]; char url[256]; int port; char username[256]; char password[256]; _Bool need_bak; char bak_dir[256]; int dbg_lvl; }

variable_types: union pthread_attr_t { char __size[56]; long __align; }

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

variable_types: err int


ltrim

decompiled_code:
const char *__fastcall ltrim(const char *a1)
{
const unsigned __int16 *v1; // rcx
const char *result; // rax

if ( !strlen(a1) )
return a1;
v1 = *_ctype_b_loc();
for ( result = a1; (v1[*result] & 0x2000) != 0; ++result )
;
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(const char *a1)
{
const unsigned __int16 *v1; // rcx
const char *result; // rax

if ( !strlen(a1) )
return a1;
v1 = *_ctype_b_loc();
for ( result = a1; (v1[*result] & 0x2000) != 0; ++result )
;
return result;
}

original_code:
static char *ltrim(char *src)
{
    size_t i = 0;
    char *p = src;
    size_t len = strlen(src);
    for (; (i < len) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace)); p++) {
        ;
    }
    return p;
}

canonical_original_code:
static char *ltrim(char *src)
{
    unsigned long i = 0;
    char *p = src;
    unsigned long len = strlen(src);
    for (; (i < len) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace)); p++) {
        ;
    }
    return p;
}

variable_types:
src
char *

variable_types:
i
unsigned long

variable_types:
p
char *

variable_types:
len
unsigned long

return_type: char *


rtrim

decompiled_code:
const char *__fastcall rtrim(const char *a1)
{
const char *v1; // rbp
const char *v2; // rbx
const unsigned __int16 *v3; // rdx

v1 = a1 - 1;
v2 = &a1[strlen(a1) - 1];
if ( v2 != a1 - 1 )
{
v3 = *_ctype_b_loc();
while ( (v3[*v2] & 0x2000) != 0 )
{
if ( --v2 == v1 )
goto LABEL_6;
}
v1 = v2;
}
LABEL_6:
*((_BYTE *)v1 + 1) = 0;
return a1;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
const char *v1; // rbp
const char *v2; // rbx
const unsigned __int16 *v3; // rdx

v1 = a1 - 1;
v2 = &a1[strlen(a1) - 1];
if ( v2 != a1 - 1 )
{
v3 = *_ctype_b_loc();
while ( (v3[*v2] & 0x2000) != 0 )
{
if ( --v2 == v1 )
goto LABEL_6;
}
v1 = v2;
}
LABEL_6:
*((_BYTE *)v1 + 1) = 0;
return a1;
}

original_code:
static char *rtrim(char *src)
{
    char *p = src + strlen(src) - 1;
    for (; (p != src - 1) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace)); p--) {
        ;
    }
    *(p + 1) = '\0';
    return src;
}

canonical_original_code:
static char *rtrim(char *src)
{
    char *p = src + strlen(src) - 1;
    for (; (p != src - 1) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace)); p--) {
        ;
    }
    *(p + 1) = '\0';
    return src;
}


dbg

original_code:
void dbg(unsigned char lvl, const char *fmt, ...)
{
    va_list arg;
    time_t t_now = 0;
    struct tm tm_now;
    if ((dbg_lvl & lvl) == 0) {
        return;
    }
    t_now = time(((void *)0));
    memcpy(&tm_now, localtime(&t_now), sizeof(tm_now));
    fprintf(stderr, "[%04d%02d%02d %02d:%02d:%02d] ",
            tm_now.tm_year + 1900,
            tm_now.tm_mon + 1,
            tm_now.tm_mday,
            tm_now.tm_hour,
            tm_now.tm_min,
            tm_now.tm_sec);
    switch (lvl) {
    case (1 << 0):
        fprintf(stderr, "[PNC] ");
        break;
    case (1 << 1):
        fprintf(stderr, "[ERR] ");
        break;
    case (1 << 2):
        fprintf(stderr, "[INF] ");
        break;
    case (1 << 3):
        fprintf(stderr, "[DBG] ");
        break;
    default:
        break;
    }
    __builtin_va_start(arg,fmt);
    vfprintf(stderr, fmt, arg);
    __builtin_va_end(arg);
    fprintf(stderr, "\n");
}

canonical_original_code:
void dbg(unsigned char lvl, char *fmt, ...)
{
    __builtin_va_list arg;
    long t_now = 0;
    struct tm tm_now;
    if ((dbg_lvl & lvl) == 0) {
        return;
    }
    t_now = time(((void *)0));
    memcpy(&tm_now, localtime(&t_now), sizeof(tm_now));
    fprintf(stderr, "[%04d%02d%02d %02d:%02d:%02d] ",
            tm_now.tm_year + 1900,
            tm_now.tm_mon + 1,
            tm_now.tm_mday,
            tm_now.tm_hour,
            tm_now.tm_min,
            tm_now.tm_sec);
    switch (lvl) {
    case (1 << 0):
        fprintf(stderr, "[PNC] ");
        break;
    case (1 << 1):
        fprintf(stderr, "[ERR] ");
        break;
    case (1 << 2):
        fprintf(stderr, "[INF] ");
        break;
    case (1 << 3):
        fprintf(stderr, "[DBG] ");
        break;
    default:
        break;
    }
    __builtin_va_start(arg,fmt);
    vfprintf(stderr, fmt, arg);
    __builtin_va_end(arg);
    fprintf(stderr, "\n");
}

variable_types:
lvl
unsigned char

variable_types:
fmt
char *

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long __tm_gmtoff; char *__tm_zone; }


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

if ( a1 < 0 )
{
dbg(2u, "%s.", "Invalid debug level specified, keep default: panic, error & info");
result = 0xFFFFFFFFLL;
}
else
{
if ( a1 > 7 )
LOBYTE(a1) = 7;
dbg_lvl = 255 >> (8 - a1);
result = 0LL;
}
return result;
}

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

if ( a1 < 0 )
{
func0(2u, "%s.", "Invalid debug level specified, keep default: panic, error & info");
result = 0xFFFFFFFFLL;
}
else
{
if ( a1 > 7 )
LOBYTE(a1) = 7;
dbg_lvl = 255 >> (8 - a1);
result = 0LL;
}
return result;
}

original_code:
int set_dbg_lvl(int lvl)
{
    if (lvl < 0) {
        dbg((1 << 1), "%s.", "Invalid debug level specified," " keep default: panic, error & info");
        return -1;
    }
    if (lvl > 7) {
        lvl = 7;
    }
    dbg_lvl = 0xff >> (8 - lvl);
    return 0;
}

variable_types: lvl int


decompiled_code:
__int64 __fastcall get_proto_by_url(const void *a1)
{
bool v2; // zf
__int64 result; // rax

v2 = memcmp(a1, "ftp:", 4uLL) == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( memcmp(a1, "http:", 5uLL) )
result = memcmp(a1, "https:", 6uLL) == 0 ? 2 : -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1)
{
bool v2; // zf
__int64 result; // rax

v2 = memcmp(a1, "ftp:", 4uLL) == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( memcmp(a1, "http:", 5uLL) )
result = memcmp(a1, "https:", 6uLL) == 0 ? 2 : -1;
}
return result;
}

original_code:
static int get_proto_by_url(const char *url)
{
    if (strncmp(url, "ftp:", 4) == 0) {
        return PROTO_FTP;
    } else if (strncmp(url, "http:", 5) == 0) {
        return PROTO_HTTP;
    } else if (strncmp(url, "https:", 6) == 0) {
        return PROTO_HTTPS;
    }
    return -1;
}

canonical_original_code:
static int get_proto_by_url(char *url)
{
    if (strncmp(url, "ftp:", 4) == 0) {
        return PROTO_FTP;
    } else if (strncmp(url, "http:", 5) == 0) {
        return PROTO_HTTP;
    } else if (strncmp(url, "https:", 6) == 0) {
        return PROTO_HTTPS;
    }
    return -1;
}

variable_types:
url
char *


decompiled_code:
__int64 uld_init()
{
if ( !(unsigned int)curl_global_init(0LL) )
return 0LL;
dbg(2LL, "%s.", "Initiate global curl failed, retry at next intval");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
if ( !(unsigned int)curl_global_init(0LL) )
return 0LL;
dbg(2LL, "%s.", "Initiate global curl failed, retry at next intval");
return 0xFFFFFFFFLL;
}

original_code:
int uld_init(void)
{
    if (curl_global_init(0) != CURLE_OK) {
        dbg((1 << 1), "%s.", "Initiate global curl failed, retry at next intval");
        return -1;
    }
    return 0;
}

canonical_original_code:
int uld_init()
{
    if (curl_global_init(0) != CURLE_OK) {
        dbg((1 << 1), "%s.", "Initiate global curl failed, retry at next intval");
        return -1;
    }
    return 0;
}


decompiled_code:
// attributes: thunk
__int64 uld_deinit()
{
return curl_global_cleanup();
}

canonical_decompiled_code:
__int64 func3()
{
return curl_global_cleanup();
}

original_code:
void uld_deinit(void)
{
    curl_global_cleanup();
}

canonical_original_code:
void uld_deinit()
{
    curl_global_cleanup();
}


decompiled_code:
__int64 __fastcall mgostring_add(__int64 a1, const char *a2)
{
char *v2; // r13
size_t v3; // rbp
size_t v4; // rbp
void *v5; // rax

v2 = *(char **)a1;
v3 = strlen(*(const char **)a1);
v4 = strlen(a2) + v3;
if ( *(_QWORD *)(a1 + 8) >= v4 )
goto LABEL_4;
v5 = realloc(v2, v4 + 1);
if ( v5 )
{
*(_QWORD *)a1 = v5;
*(_QWORD *)(a1 + 8) = v4;
printf("*** realloc, str->length is now %lu ***\n", v4);
v2 = *(char **)a1;
LABEL_4:
strcat(v2, a2);
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2)
{
char *v2; // r13
size_t v3; // rbp
size_t v4; // rbp
void *v5; // rax

v2 = *(char **)a1;
v3 = strlen(*(const char **)a1);
v4 = strlen(a2) + v3;
if ( *(_QWORD *)(a1 + 8) >= v4 )
goto LABEL_4;
v5 = realloc(v2, v4 + 1);
if ( v5 )
{
*(_QWORD *)a1 = v5;
*(_QWORD *)(a1 + 8) = v4;
printf("*** realloc, str->length is now %lu ***\n", v4);
v2 = *(char **)a1;
LABEL_4:
strcat(v2, a2);
return 1LL;
}
return 0LL;
}

original_code:
_Bool mgostring_add(struct MgoString *str, const char *new){
    unsigned long new_len = strlen(str->data) + strlen(new);
    if (new_len > str->length) {
        char *new_data = realloc(str->data, new_len + 1);
        if (new_data == ((void *)0)) {
            return 0;
        }
        str->data = new_data;
        str->length = new_len;
            printf("*** realloc, str->length is now %lu ***\n", str->length);
    }
    strcat(str->data, new);
    return 1;
}

canonical_original_code:
_Bool mgostring_add(struct MgoString *str, char *new){
    unsigned long new_len = strlen(str->data) + strlen(new);
    if (new_len > str->length) {
        char *new_data = realloc(str->data, new_len + 1);
        if (new_data == ((void *)0)) {
            return 0;
        }
        str->data = new_data;
        str->length = new_len;
            printf("*** realloc, str->length is now %lu ***\n", str->length);
    }
    strcat(str->data, new);
    return 1;
}

variable_types: struct MgoString { char *data; unsigned long length; } *

variable_types:
new
char *


decompiled_code:
__int64 __fastcall mgostring_c_str(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
char *mgostring_c_str(struct MgoString *str){
    return str->data;
}

user_defined_types: struct MgoString { char *data; unsigned long length; }


decompiled_code:
const unsigned __int16 *__fastcall mgostring_rtrim(const char **a1)
{
const char *v1; // rbx
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
const unsigned __int16 *result; // rax

v1 = *a1;
v2 = &v1[strlen(*a1) - 1];
v3 = __ctype_b_loc();
while ( 1 )
{
result = *v3;
if ( ((*v3)[*v2] & 0x2000) == 0 )
break;
*v2-- = 0;
}
return result;
}

canonical_decompiled_code:
const unsigned __int16 *__fastcall func9(const char **a1)
{
const char *v1; // rbx
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
const unsigned __int16 *result; // rax

v1 = *a1;
v2 = &v1[strlen(*a1) - 1];
v3 = __ctype_b_loc();
while ( 1 )
{
result = *v3;
if ( ((*v3)[*v2] & 0x2000) == 0 )
break;
*v2-- = 0;
}
return result;
}

original_code:
void mgostring_rtrim(struct MgoString *str){
    char *non_space = str->data + (strlen(str->data) - 1);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short int) _ISspace)) {
        *non_space = '\0';
        non_space--;
    }
}

canonical_original_code:
void mgostring_rtrim(struct MgoString *str){
    char *non_space = str->data + (strlen(str->data) - 1);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short) _ISspace)) {
        *non_space = '\0';
        non_space--;
    }
}


decompiled_code:
void *__fastcall mgostring_ltrim(const char **a1)
{
char *v1; // rbp
size_t v2; // rbx
char *v3; // rsi
const unsigned __int16 *v4; // rdx

v1 = (char *)*a1;
v2 = strlen(*a1);
v3 = v1;
v4 = *__ctype_b_loc();
while ( (v4[*v3] & 0x2000) != 0 )
++v3;
return memmove(v1, v3, v2 + 1);
}

canonical_decompiled_code:
void *__fastcall func10(const char **a1)
{
char *v1; // rbp
size_t v2; // rbx
char *v3; // rsi
const unsigned __int16 *v4; // rdx

v1 = (char *)*a1;
v2 = strlen(*a1);
v3 = v1;
v4 = *__ctype_b_loc();
while ( (v4[*v3] & 0x2000) != 0 )
++v3;
return memmove(v1, v3, v2 + 1);
}

original_code:
void mgostring_ltrim(struct MgoString *str){
    char *non_space = str->data;
    unsigned long len = strlen(str->data);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short int) _ISspace)) non_space++;
    memmove(str->data, non_space, len + 1);
}

canonical_original_code:
void mgostring_ltrim(struct MgoString *str){
    char *non_space = str->data;
    unsigned long len = strlen(str->data);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short) _ISspace)) non_space++;
    memmove(str->data, non_space, len + 1);
}


decompiled_code:
const unsigned __int16 *__fastcall mgostring_trim(const char **a1)
{
mgostring_ltrim(a1);
return mgostring_rtrim(a1);
}

canonical_decompiled_code:
const unsigned __int16 *__fastcall func11(const char **a1)
{
func10(a1);
return func9(a1);
}

original_code:
void mgostring_trim(struct MgoString *str){
    mgostring_ltrim(str);
    mgostring_rtrim(str);
}


decompiled_code:
__int64 __fastcall mgostring_replace(const char **a1, const char *a2, const char *a3)
{
const char *v4; // rbx
char *v5; // rax
const char *v6; // r15
unsigned int v7; // er13
char *v8; // rax
char *v9; // rbx
char *ptr; // [rsp+8h] [rbp-40h]

v4 = *a1;
if ( !strstr(*a1, a2) )
return 0;
v5 = strdup(v4);
if ( (ptr = v5) == 0LL )
return 0;
*v4 = 0;
v6 = v5;
v7 = 0;
while ( 1 )
{
v8 = strstr(v6, a2);
v9 = v8;
if ( !v8 )
break;
*v8 = 0;
++v7;
mgostring_add((__int64)a1, v6);
mgostring_add((__int64)a1, a3);
v6 = &v9[strlen(a2)];
}
mgostring_add((__int64)a1, v6);
free(ptr);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char **a1, const char *a2, const char *a3)
{
const char *v4; // rbx
char *v5; // rax
const char *v6; // r15
unsigned int v7; // er13
char *v8; // rax
char *v9; // rbx
char *ptr; // [rsp+8h] [rbp-40h]

v4 = *a1;
if ( !strstr(*a1, a2) )
return 0;
v5 = strdup(v4);
if ( (ptr = v5) == 0LL )
return 0;
*v4 = 0;
v6 = v5;
v7 = 0;
while ( 1 )
{
v8 = strstr(v6, a2);
v9 = v8;
if ( !v8 )
break;
*v8 = 0;
++v7;
func7((__int64)a1, v6);
func7((__int64)a1, a3);
v6 = &v9[strlen(a2)];
}
func7((__int64)a1, v6);
free(ptr);
return v7;
}

original_code:
int mgostring_replace(struct MgoString *str, const char *from, const char *to) {
    if (strstr(str->data, from) == ((void *)0)) return 0;
    char *copy = strdup(str->data);
    if (copy == ((void *)0)) return 0;
    int num_changes = 0;
    strcpy(str->data,"");
    char *copy_from = copy;
    char *up_to = ((void *)0);
    do {
        up_to = strstr(copy_from, from);
        if (up_to == ((void *)0)) break;
        num_changes++;
        *up_to = '\0';
        mgostring_add(str, copy_from);
        mgostring_add(str, to);
        copy_from = up_to + strlen(from);
    } while(1);
    mgostring_add(str, copy_from);
    free(copy);
    return num_changes;
}

canonical_original_code:
int mgostring_replace(struct MgoString *str, char *from, char *to) {
    if (strstr(str->data, from) == ((void *)0)) return 0;
    char *copy = strdup(str->data);
    if (copy == ((void *)0)) return 0;
    int num_changes = 0;
    strcpy(str->data,"");
    char *copy_from = copy;
    char *up_to = ((void *)0);
    do {
        up_to = strstr(copy_from, from);
        if (up_to == ((void *)0)) break;
        num_changes++;
        *up_to = '\0';
        mgostring_add(str, copy_from);
        mgostring_add(str, to);
        copy_from = up_to + strlen(from);
    } while(1);
    mgostring_add(str, copy_from);
    free(copy);
    return num_changes;
}

variable_types:
from
char *

variable_types:
to
char *

variable_types:
copy
char *


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

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

original_code:
void mgostring_destruct(struct MgoString *str){
    free(str->data);
    free(str);
}


callback

decompiled_code:
int __fastcall callback(const char *a1, const char *a2)
{
return printf("%s = \"%s\"\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1, const char *a2)
{
return printf("%s = \"%s\"\n", a1, a2);
}

original_code:
void callback(char*k, char*v){
    printf("%s = \"%s\"\n", k, v);
}

canonical_original_code:
void callback(char *k, char *v){
    printf("%s = \"%s\"\n", k, v);
}

variable_types:
k
char *

variable_types:
v
char *


trimwhitespace

decompiled_code:
char *__fastcall trimwhitespace(char *s)
{
const unsigned __int16 *v2; // rbx
__int64 v3; // rdx
char *v4; // rax

v2 = *__ctype_b_loc();
while ( 1 )
{
v3 = (unsigned __int8)*s;
if ( (v2[v3] & 0x2000) == 0 )
break;
++s;
}
if ( (_BYTE)v3 )
{
v4 = &s[strlen(s) - 1];
if ( s < v4 )
{
do
{
if ( (v2[(unsigned __int8)*v4] & 0x2000) == 0 )
break;
--v4;
}
while ( s != v4 );
}
v4[1] = 0;
}
return s;
}

canonical_decompiled_code:
char *__fastcall func8(char *s)
{
const unsigned __int16 *v2; // rbx
__int64 v3; // rdx
char *v4; // rax

v2 = *__ctype_b_loc();
while ( 1 )
{
v3 = (unsigned __int8)*s;
if ( (v2[v3] & 0x2000) == 0 )
break;
++s;
}
if ( (_BYTE)v3 )
{
v4 = &s[strlen(s) - 1];
if ( s < v4 )
{
do
{
if ( (v2[(unsigned __int8)*v4] & 0x2000) == 0 )
break;
--v4;
}
while ( s != v4 );
}
v4[1] = 0;
}
return s;
}

original_code:
static char *trimwhitespace(char *str)
{
    char *end;
    while(((*__ctype_b_loc ())[(int) (((unsigned char) *str))] & (unsigned short int) _ISspace)) {
        str++;
    }
    if(*str == 0) {
        return str;
    }
    end = str + strlen(str) - 1;
    while(end > str && ((*__ctype_b_loc ())[(int) (((unsigned char) *end))] & (unsigned short int) _ISspace)) {
        end--;
    }
    *(end+1) = 0;
    return str;
}

canonical_original_code:
static char *trimwhitespace(char *str)
{
    char *end;
    while(((*__ctype_b_loc ())[(int) (((unsigned char) *str))] & (unsigned short) _ISspace)) {
        str++;
    }
    if(*str == 0) {
        return str;
    }
    end = str + strlen(str) - 1;
    while(end > str && ((*__ctype_b_loc ())[(int) (((unsigned char) *end))] & (unsigned short) _ISspace)) {
        end--;
    }
    *(end+1) = 0;
    return str;
}

variable_types:
str
char *

variable_types:
end
char *


decompiled_code:
const char *__fastcall cfg_get_string(const char **a1, const char *a2, __int64 a3)
{
const char **v3; // rbp
char *v5; // rax
unsigned __int8 v6; // bl
char *v7; // r13
const __int32_t *v8; // rcx
unsigned __int8 *v9; // rax
const char *v10; // r14

v3 = a1;
if ( !a1 )
return 0LL;
v5 = strdup(a2);
v6 = *v5;
v7 = v5;
if ( !*v5 )
goto LABEL_7;
v8 = *__ctype_toupper_loc();
v9 = (unsigned __int8 *)v7;
do
{
*v9++ = v8[v6];
v6 = *v9;
}
while ( *v9 );
if ( !strcmp(*a1, v7) )
{
LABEL_8:
v10 = v3[1];
free(v7);
if ( v10 )
return v10;
}
else
{
while ( 1 )
{
v3 = (const char **)v3[2];
if ( !v3 )
break;
LABEL_7:
if ( !strcmp(*v3, v7) )
goto LABEL_8;
}
free(v7);
}
return (const char *)a3;
}

canonical_decompiled_code:
const char *__fastcall func12(const char **a1, const char *a2, __int64 a3)
{
const char **v3; // rbp
char *v5; // rax
unsigned __int8 v6; // bl
char *v7; // r13
const __int32_t *v8; // rcx
unsigned __int8 *v9; // rax
const char *v10; // r14

v3 = a1;
if ( !a1 )
return 0LL;
v5 = strdup(a2);
v6 = *v5;
v7 = v5;
if ( !*v5 )
goto LABEL_7;
v8 = *__ctype_toupper_loc();
v9 = (unsigned __int8 *)v7;
do
{
*v9++ = v8[v6];
v6 = *v9;
}
while ( *v9 );
if ( !strcmp(*a1, v7) )
{
LABEL_8:
v10 = v3[1];
free(v7);
if ( v10 )
return v10;
}
else
{
while ( 1 )
{
v3 = (const char **)v3[2];
if ( !v3 )
break;
LABEL_7:
if ( !strcmp(*v3, v7) )
goto LABEL_8;
}
free(v7);
}
return (const char *)a3;
}

original_code:
char *
cfg_get_string(struct Config *cfg, char *key, char *default_value)
{
    if (cfg == ((void *)0)) {
        return ((void *)0);
    }
    char *upper_key = strdup(key);
    unsigned char *t = (unsigned char *)upper_key;
    while (*t) {
        *t = toupper(*t);
        t++;
    }
    struct Config *current_record = cfg;
    char *rc = ((void *)0);
    do {
        if (strcmp(current_record->key, upper_key) == 0) {
            rc = current_record->value;
            break;
        }
        if (current_record->next == ((void *)0)) {
            break;
        }
        current_record = current_record->next;
    } while (1);
    free(upper_key);
    if (rc == ((void *)0)) {
        return default_value;
    } else {
        return rc;
    }
}

canonical_original_code:
char *cfg_get_string(struct Config *cfg, char *key, char *default_value)
{
    if (cfg == ((void *)0)) {
        return ((void *)0);
    }
    char *upper_key = strdup(key);
    unsigned char *t = (unsigned char *)upper_key;
    while (*t) {
        *t = toupper(*t);
        t++;
    }
    struct Config *current_record = cfg;
    char *rc = ((void *)0);
    do {
        if (strcmp(current_record->key, upper_key) == 0) {
            rc = current_record->value;
            break;
        }
        if (current_record->next == ((void *)0)) {
            break;
        }
        current_record = current_record->next;
    } while (1);
    free(upper_key);
    if (rc == ((void *)0)) {
        return default_value;
    } else {
        return rc;
    }
}

variable_types: struct Config { char *key; char *value; struct Config *next; } *

variable_types:
key
char *

variable_types:
t
unsigned char *

variable_types:
rc
char *


decompiled_code:
__int64 __fastcall cfg_get_long(const char **a1, const char *a2, int a3)
{
const char *v4; // rdi
__int64 result; // rax

if ( a1 && (v4 = cfg_get_string(a1, a2, 4202510LL)) != 0LL )
result = strtol(v4, 0LL, 0);
else
result = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(const char **a1, const char *a2, int a3)
{
const char *v4; // rdi
__int64 result; // rax

if ( a1 && (v4 = func12(a1, a2, 4202510LL)) != 0LL )
result = strtol(v4, 0LL, 0);
else
result = a3;
return result;
}

original_code:
long
cfg_get_long(struct Config *cfg, char *key, int default_value)
{
    if (cfg == ((void *)0)) {
        return default_value;
    }
    char *value = cfg_get_string(cfg, key, "");
    if (!value) {
        return default_value;
    }
    return strtol(value, ((void *)0), 0);
}

variable_types:
value
char *

return_type: long

user_defined_types: struct Config { char *key; char *value; struct Config *next; }


decompiled_code:
void __fastcall cfg_free(void *ptr)
{
void **v1; // rbx
void **v2; // rdi

if ( ptr )
{
v1 = (void **)ptr;
do
{
free(*v1);
free(v1[1]);
v2 = v1;
v1 = (void **)v1[2];
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
void **v1; // rbx
void **v2; // rdi

if ( ptr )
{
v1 = (void **)ptr;
do
{
free(*v1);
free(v1[1]);
v2 = v1;
v1 = (void **)v1[2];
free(v2);
}
while ( v1 );
}
}

original_code:
void
cfg_free(struct Config *cfg)
{
    if (cfg == ((void *)0)) return;
    struct Config *next = ((void *)0);
    do {
        free(cfg->key);
        free(cfg->value);
        next = cfg->next;
        free(cfg);
        if (next == ((void *)0)) break;
        cfg = next;
    } while(1);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
const char *v4; // rax
const char *v5; // rax
__int64 v6; // rax

v3 = (void *)cfg_init("settings.cfg");
v4 = (const char *)cfg_get_string(v3, "barney", "The default value");
printf("\"%s\"\n", v4);
v5 = (const char *)cfg_get_string(v3, "non-existent", "A default value");
printf("\"%s\"\n", v5);
v6 = cfg_get_long(v3, "NuMbEr", 69LL);
printf("Number is %ld\n", v6);
puts("DEBUG dump all:");
cfg_foreach(v3, callback);
cfg_free(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
const char *v4; // rax
const char *v5; // rax
__int64 v6; // rax

v3 = (void *)func11("settings.cfg");
v4 = (const char *)func12(v3, "barney", "The default value");
printf("\"%s\"\n", v4);
v5 = (const char *)func12(v3, "non-existent", "A default value");
printf("\"%s\"\n", v5);
v6 = func13(v3, "NuMbEr", 69LL);
printf("Number is %ld\n", v6);
puts("DEBUG dump all:");
func15(v3, callback);
func14(v3);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    struct Config *cfg = cfg_init("settings.cfg");
    char *value = cfg_get_string(cfg, "barney", "The default value");
    printf("\"%s\"\n", value);
    char *value2 = cfg_get_string(cfg, "non-existent", "A default value");
    printf("\"%s\"\n", value2);
    long num = cfg_get_long(cfg, "NuMbEr", 69);
    printf("Number is %ld\n", num);
    printf("DEBUG dump all:\n");
    cfg_foreach(cfg, callback);
    cfg_free(cfg);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: num long


testDone

repo: mdavidsaver/avr

decompiled_code:
_BOOL8 testDone()
{
__int64 v0; // rsi

putchar(10);
if ( nplan && nplan != npass + nfail )
printf("Planned %lu tests but ran %lu\n", nplan, npass + nfail);
printf("%lu test pass\n", npass);
v0 = nfail;
if ( nfail )
{
printf("%lu tests failed!\n", nfail);
v0 = nfail;
}
return v0 != 0;
}

canonical_decompiled_code:
_BOOL8 func0()
{
__int64 v0; // rsi

putchar(10);
if ( nplan && nplan != npass + nfail )
printf("Planned %lu tests but ran %lu\n", nplan, npass + nfail);
printf("%lu test pass\n", npass);
v0 = nfail;
if ( nfail )
{
printf("%lu tests failed!\n", nfail);
v0 = nfail;
}
return v0 != 0;
}

original_code:
static int testDone(void)
{
    printf("\n");
    if(nplan && npass+nfail!=nplan) {
        printf("Planned %lu tests but ran %lu\n",
               (unsigned long)nplan,
               (unsigned long)(npass+nfail));
    }
    printf("%lu test pass\n",
           (unsigned long)(npass));
    if(nfail) {
        printf("%lu tests failed!\n",
               (unsigned long)nfail);
    }
    return nfail!=0;
}

canonical_original_code:
static int testDone()
{
    printf("\n");
    if(nplan && npass+nfail!=nplan) {
        printf("Planned %lu tests but ran %lu\n",
               (unsigned long)nplan,
               (unsigned long)(npass+nfail));
    }
    printf("%lu test pass\n",
           (unsigned long)(npass));
    if(nfail) {
        printf("%lu tests failed!\n",
               (unsigned long)nfail);
    }
    return nfail!=0;
}


decompiled_code:
_BOOL8 __fastcall modbus_in_all(char *a1, __int64 a2)
{
char *v2; // rbx
char v3; // al

v2 = a1;
do
{
if ( (mbus_status & 6) != 0 )
return 1LL;
v3 = *v2++;
mbus_in_byte = v3;
mbus_status |= 2u;
mbus_process();
}
while ( &a1[a2] != v2 );
return (mbus_status & 6) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(char *a1, __int64 a2)
{
char *v2; // rbx
char v3; // al

v2 = a1;
do
{
if ( (mbus_status & 6) != 0 )
return 1LL;
v3 = *v2++;
mbus_in_byte = v3;
mbus_status |= 2u;
mbus_process();
}
while ( &a1[a2] != v2 );
return (mbus_status & 6) != 0;
}

original_code:
static
int modbus_in_all(uint8_t* data, size_t dsize)
{
    uint8_t *end=data+dsize;
    while(data!=end && !(mbus_status&(0x02|0x04)) ) {
        mbus_in_byte = *data++;
        mbus_status |= 0x02;
        mbus_process();
    }
    return data!=end || mbus_status&(0x02|0x04);
}

canonical_original_code:
static int modbus_in_all(unsigned char *data, unsigned long dsize)
{
    unsigned char *end = data+dsize;
    while(data!=end && !(mbus_status&(0x02|0x04)) ) {
        mbus_in_byte = *data++;
        mbus_status |= 0x02;
        mbus_process();
    }
    return data!=end || mbus_status&(0x02|0x04);
}

variable_types:
data
unsigned char *

variable_types:
dsize
unsigned long

variable_types:
end
unsigned char *


testDiag

decompiled_code:
int testDiag(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("# ");
vprintf(format, arg);
return putchar(10);
}

canonical_decompiled_code:
int func4(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("# ");
vprintf(format, arg);
return putchar(10);
}

original_code:
void testDiag(const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    printf("# ");
    vprintf(fmt, args);
    printf("\n");
    __builtin_va_end(args);
}

canonical_original_code:
void testDiag(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    printf("# ");
    vprintf(fmt, args);
    printf("\n");
    __builtin_va_end(args);
}

variable_types:
fmt
char *


testPassV

decompiled_code:
int __fastcall testPassV(char *format, __gnuc_va_list arg)
{
++npass;
printf("ok - ");
vprintf(format, arg);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func5(char *format, __gnuc_va_list arg)
{
++npass;
printf("ok - ");
vprintf(format, arg);
return putchar(10);
}

original_code:
void testPassV(const char* fmt, va_list args)
{
    npass++;
    printf("ok - ");
    vprintf(fmt, args);
    printf("\n");
}

canonical_original_code:
void testPassV(char *fmt, __builtin_va_list args)
{
    npass++;
    printf("ok - ");
    vprintf(fmt, args);
    printf("\n");
}


testFailV

decompiled_code:
int __fastcall testFailV(char *format, __gnuc_va_list arg)
{
++nfail;
printf("fail - ");
vprintf(format, arg);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func6(char *format, __gnuc_va_list arg)
{
++nfail;
printf("fail - ");
vprintf(format, arg);
return putchar(10);
}

original_code:
void testFailV(const char* fmt, va_list args)
{
    nfail++;
    printf("fail - ");
    vprintf(fmt, args);
    printf("\n");
}

canonical_original_code:
void testFailV(char *fmt, __builtin_va_list args)
{
    nfail++;
    printf("fail - ");
    vprintf(fmt, args);
    printf("\n");
}


testPass

decompiled_code:
int testPass(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return testPassV(a1, arg);
}

canonical_decompiled_code:
int func7(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return func5(a1, arg);
}

original_code:
void testPass(const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    testPassV(fmt, args);
    __builtin_va_end(args);
}

canonical_original_code:
void testPass(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    testPassV(fmt, args);
    __builtin_va_end(args);
}


testFail

decompiled_code:
int testFail(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return testFailV(a1, arg);
}

canonical_decompiled_code:
int func8(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return func6(a1, arg);
}

original_code:
void testFail(const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    testFailV(fmt, args);
    __builtin_va_end(args);
}

canonical_original_code:
void testFail(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    testFailV(fmt, args);
    __builtin_va_end(args);
}


testOk

decompiled_code:
__int64 testOk(unsigned int a1, char *a2, ...)
{
__int64 result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( a1 )
{
testPassV(a2, arg);
result = a1;
}
else
{
testFailV(a2, arg);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func9(unsigned int a1, char *a2, ...)
{
__int64 result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( a1 )
{
func5(a2, arg);
result = a1;
}
else
{
func6(a2, arg);
result = 0LL;
}
return result;
}

original_code:
int testOk(int v, const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    if(v)
        testPassV(fmt, args);
    else
        testFailV(fmt, args);
    return v;
}

canonical_original_code:
int testOk(int v, char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    if(v)
        testPassV(fmt, args);
    else
        testFailV(fmt, args);
    return v;
}

variable_types: v int


testRead

decompiled_code:
int testRead()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

testDiag("Testing read holding registers (command 3)");
v0 = modbus_in_all(cmd_16, 8LL);
testOk(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
testOk(read_counter == 1, "read_counter==1");
testOk(write_counter == 0, "write_counter==0");
testOk(0x1234u, "read_addr=0x1234");
testOk(read_count == 4, "read_count==4");
v1 = modbus_out_all_constprop_0(rep_15);
if ( !(unsigned int)testOk(v1 == 13, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
return testFail("Sizes don't match", "modbus_out_all(rep, sizeof(rep))==sizeof(expect)");
v2 = memcmp(rep_15, &expect_14, 0xDuLL);
return testOk(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}

canonical_decompiled_code:
int func10()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

func4("Testing read holding registers (command 3)");
v0 = func1(cmd_16, 8LL);
func9(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
func9(read_counter == 1, "read_counter==1");
func9(write_counter == 0, "write_counter==0");
func9(0x1234u, "read_addr=0x1234");
func9(read_count == 4, "read_count==4");
v1 = func2(rep_15);
if ( !(unsigned int)func9(v1 == 13, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
return func8("Sizes don't match", "modbus_out_all(rep, sizeof(rep))==sizeof(expect)");
v2 = memcmp(rep_15, &expect_14, 0xDuLL);
return func9(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}

original_code:
static void testRead(void)
{
    static uint8_t cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x3, 0x8, 0x0, 0x1, 0x2, 0x3,
                               0x4, 0x5, 0x6, 0x7, 0xA6, 0x93};
    testDiag("Testing read holding registers (command 3)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    testOk(read_addr=0x1234, "read_addr=0x1234");
    testOk(read_count==4, "read_count==4");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
}

canonical_original_code:
static void testRead()
{
    static unsigned char cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x3, 0x8, 0x0, 0x1, 0x2, 0x3,
                               0x4, 0x5, 0x6, 0x7, 0xA6, 0x93};
    testDiag("Testing read holding registers (command 3)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    testOk(read_addr=0x1234, "read_addr=0x1234");
    testOk(read_count==4, "read_count==4");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
}

variable_types:
cmd
unsigned char []

variable_types:
rep
unsigned char [20]

variable_types:
expect
unsigned char []


testWrite

decompiled_code:
int testWrite()
{
_BOOL4 v0; // eax
int v1; // eax
int result; // eax

testDiag("Testing write single holding register (command 6)");
v0 = modbus_in_all((char *)&cmd_13, 8LL);
testOk(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 1, "write_counter==1");
testOk(0x2143u, "write_addr=0x2143");
testOk(write_data == 22136, "write_data==0x5678");
v1 = modbus_out_all_constprop_0(&rep_12);
if ( (unsigned int)testOk(v1 == 8, "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)") )
result = testOk(rep_12 == cmd_13, "memcmp(rep, cmd, sizeof(cmd))==0");
else
result = testFail("Sizes don't match", "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)");
return result;
}

canonical_decompiled_code:
int func11()
{
_BOOL4 v0; // eax
int v1; // eax
int result; // eax

func4("Testing write single holding register (command 6)");
v0 = func1((char *)&cmd_13, 8LL);
func9(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 1, "write_counter==1");
func9(0x2143u, "write_addr=0x2143");
func9(write_data == 22136, "write_data==0x5678");
v1 = func2(&rep_12);
if ( (unsigned int)func9(v1 == 8, "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)") )
result = func9(rep_12 == cmd_13, "memcmp(rep, cmd, sizeof(cmd))==0");
else
result = func8("Sizes don't match", "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)");
return result;
}

original_code:
static void testWrite(void)
{
    static uint8_t cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static uint8_t rep[20];
    testDiag("Testing write single holding register (command 6)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    testOk(write_addr=0x2143, "write_addr=0x2143");
    testOk(write_data==0x5678, "write_data==0x5678");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(cmd), "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)")) {
        testOk(memcmp(rep, cmd, sizeof(cmd))==0, "memcmp(rep, cmd, sizeof(cmd))==0");
    } else
        testFail("Sizes don't match");
}

canonical_original_code:
static void testWrite()
{
    static unsigned char cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static unsigned char rep[20];
    testDiag("Testing write single holding register (command 6)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    testOk(write_addr=0x2143, "write_addr=0x2143");
    testOk(write_data==0x5678, "write_data==0x5678");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(cmd), "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)")) {
        testOk(memcmp(rep, cmd, sizeof(cmd))==0, "memcmp(rep, cmd, sizeof(cmd))==0");
    } else
        testFail("Sizes don't match");
}


testInvalidFunc

decompiled_code:
int testInvalidFunc()
{
_BOOL4 v0; // eax
int v1; // eax

testDiag("Testing reception of an invalid function code");
v0 = modbus_in_all(cmd_11, 2LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 0, "write_counter==0");
v1 = modbus_out_all_constprop_0(&rep_10);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
testOk(rep_10 == expect_9, "memcmp(rep, expect, sizeof(expect))==0");
else
testFail("Sizes don't match");
testDiag("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
testOk(mbus_status == 0, "mbus_status==0");
return testWrite();
}

canonical_decompiled_code:
int func12()
{
_BOOL4 v0; // eax
int v1; // eax

func4("Testing reception of an invalid function code");
v0 = func1(cmd_11, 2LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 0, "write_counter==0");
v1 = func2(&rep_10);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
func9(rep_10 == expect_9, "memcmp(rep, expect, sizeof(expect))==0");
else
func8("Sizes don't match");
func4("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
func9(mbus_status == 0, "mbus_status==0");
return func11();
}

original_code:
static void testInvalidFunc(void)
{
    static uint8_t cmd[] = {0x1, 0x08};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x88, 0x1, 0x76};
    testDiag("Testing reception of an invalid function code");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}

canonical_original_code:
static void testInvalidFunc()
{
    static unsigned char cmd[] = {0x1, 0x08};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x88, 0x1, 0x76};
    testDiag("Testing reception of an invalid function code");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}


testBadCRC

decompiled_code:
int testBadCRC()
{
_BOOL4 v0; // eax
int v1; // eax

testDiag("Testing reception of an message with bad CRC");
v0 = modbus_in_all(cmd_8, 8LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 0, "write_counter==0");
v1 = modbus_out_all_constprop_0(&rep_7);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
testOk(rep_7 == expect_6, "memcmp(rep, expect, sizeof(expect))==0");
else
testFail("Sizes don't match");
testDiag("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
testOk(mbus_status == 0, "mbus_status==0");
return testWrite();
}

canonical_decompiled_code:
int func13()
{
_BOOL4 v0; // eax
int v1; // eax

func4("Testing reception of an message with bad CRC");
v0 = func1(cmd_8, 8LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 0, "write_counter==0");
v1 = func2(&rep_7);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
func9(rep_7 == expect_6, "memcmp(rep, expect, sizeof(expect))==0");
else
func8("Sizes don't match");
func4("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
func9(mbus_status == 0, "mbus_status==0");
return func11();
}

original_code:
static void testBadCRC(void)
{
    static uint8_t cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xFF, 0xFF};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x83, 0x4, 0x78};
    testDiag("Testing reception of an message with bad CRC");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}

canonical_original_code:
static void testBadCRC()
{
    static unsigned char cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xFF, 0xFF};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x83, 0x4, 0x78};
    testDiag("Testing reception of an message with bad CRC");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}


testUserReadError

decompiled_code:
__int64 testUserReadError()
{
_BOOL4 v0; // eax
int v1; // eax

read_fail = 1;
testDiag("Testing user error on read");
v0 = modbus_in_all(cmd_5, 8LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
read_fail = 0;
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 1, "read_counter==1");
testOk(write_counter == 0, "write_counter==0");
v1 = modbus_out_all_constprop_0(&rep_4);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
testOk(rep_4 == expect_3, "memcmp(rep, expect, sizeof(expect))==0");
else
testFail("Sizes don't match");
mbus_status &= 0xFBu;
return testOk(mbus_status == 0, "mbus_status==0");
}

canonical_decompiled_code:
__int64 func14()
{
_BOOL4 v0; // eax
int v1; // eax

read_fail = 1;
func4("Testing user error on read");
v0 = func1(cmd_5, 8LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
read_fail = 0;
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 1, "read_counter==1");
func9(write_counter == 0, "write_counter==0");
v1 = func2(&rep_4);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
func9(rep_4 == expect_3, "memcmp(rep, expect, sizeof(expect))==0");
else
func8("Sizes don't match");
mbus_status &= 0xFBu;
return func9(mbus_status == 0, "mbus_status==0");
}

original_code:
static void testUserReadError(void)
{
    static uint8_t cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x83, 0x2, 0x0};
    read_fail = 1;
    testDiag("Testing user error on read");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    read_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}

canonical_original_code:
static void testUserReadError()
{
    static unsigned char cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x83, 0x2, 0x0};
    read_fail = 1;
    testDiag("Testing user error on read");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    read_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}


testUserWriteError

decompiled_code:
__int64 testUserWriteError()
{
_BOOL4 v0; // eax
int v1; // eax

write_fail = 1;
testDiag("Testing user error on write");
v0 = modbus_in_all(cmd_2, 8LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
write_fail = 0;
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 1, "write_counter==1");
v1 = modbus_out_all_constprop_0(&rep_1);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
testOk(rep_1 == expect_0, "memcmp(rep, expect, sizeof(expect))==0");
else
testFail("Sizes don't match");
mbus_status &= 0xFBu;
return testOk(mbus_status == 0, "mbus_status==0");
}

canonical_decompiled_code:
__int64 func15()
{
_BOOL4 v0; // eax
int v1; // eax

write_fail = 1;
func4("Testing user error on write");
v0 = func1(cmd_2, 8LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
write_fail = 0;
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 1, "write_counter==1");
v1 = func2(&rep_1);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
func9(rep_1 == expect_0, "memcmp(rep, expect, sizeof(expect))==0");
else
func8("Sizes don't match");
mbus_status &= 0xFBu;
return func9(mbus_status == 0, "mbus_status==0");
}

original_code:
static void testUserWriteError(void)
{
    static uint8_t cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x86, 0x3, 0x76};
    write_fail = 1;
    testDiag("Testing user error on write");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    write_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}

canonical_original_code:
static void testUserWriteError()
{
    static unsigned char cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x86, 0x3, 0x76};
    write_fail = 1;
    testDiag("Testing user error on write");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    write_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}


runtests

decompiled_code:
__int64 __fastcall runtests(int a1)
{
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testRead();
if ( a1 )
{
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testWrite();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testInvalidFunc();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testBadCRC();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testUserReadError();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
}
else
{
write_counter = 0LL;
read_counter = 0LL;
testWrite();
write_counter = 0LL;
read_counter = 0LL;
testInvalidFunc();
write_counter = 0LL;
read_counter = 0LL;
testBadCRC();
write_counter = 0LL;
read_counter = 0LL;
testUserReadError();
}
write_counter = 0LL;
read_counter = 0LL;
return testUserWriteError();
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func10();
if ( a1 )
{
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func11();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func12();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func13();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func14();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
}
else
{
write_counter = 0LL;
read_counter = 0LL;
func11();
write_counter = 0LL;
read_counter = 0LL;
func12();
write_counter = 0LL;
read_counter = 0LL;
func13();
write_counter = 0LL;
read_counter = 0LL;
func14();
}
write_counter = 0LL;
read_counter = 0LL;
return func15();
}

original_code:
static void runtests(int reset)
{
    mbus_reset();
    testOk(mbus_status==0, "mbus_status==0");
    read_counter = write_counter = 0;
    testRead();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testWrite();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testInvalidFunc();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testBadCRC();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testUserReadError();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testUserWriteError();
}

variable_types: reset int


decompiled_code:
__int64 __fastcall mbus_read_holding(__int64 a1, unsigned __int8 a2, __int64 a3)
{
__int16 v3; // cx
__int64 v4; // rdi
__int16 v5; // si
__int16 v6; // ax
__int16 v7; // ax
__int64 result; // rax

++read_counter;
read_count = a2;
if ( a2 )
{
v3 = 0;
v4 = a3 + 2LL * ((unsigned int)a2 - 1) + 2;
v5 = 1 - a3;
do
{
v6 = v5 + a3;
a3 += 2LL;
v7 = v3 | v6;
v3 += 512;
*(_WORD *)(a3 - 2) = v7;
}
while ( a3 != v4 );
}
result = (unsigned int)read_fail;
if ( read_fail )
result = mbus_exception(2LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, unsigned __int8 a2, __int64 a3)
{
__int16 v3; // cx
__int64 v4; // rdi
__int16 v5; // si
__int16 v6; // ax
__int16 v7; // ax
__int64 result; // rax

++read_counter;
read_count = a2;
if ( a2 )
{
v3 = 0;
v4 = a3 + 2LL * ((unsigned int)a2 - 1) + 2;
v5 = 1 - a3;
do
{
v6 = v5 + a3;
a3 += 2LL;
v7 = v3 | v6;
v3 += 512;
*(_WORD *)(a3 - 2) = v7;
}
while ( a3 != v4 );
}
result = (unsigned int)read_fail;
if ( read_fail )
result = mbus_exception(2LL);
return result;
}

original_code:
void mbus_read_holding(uint16_t addr, uint8_t count, uint16_t * restrict result)
{
    int i;
    read_counter++;
    read_addr = addr;
    read_count = count;
    for(i=0; i<count; i++) {
        result[i]=(2*i+1)|(2*i<<8);
    }
    if(read_fail)
        mbus_exception(2);
}

canonical_original_code:
void mbus_read_holding(unsigned short addr, unsigned char count, unsigned short *result)
{
    int i;
    read_counter++;
    read_addr = addr;
    read_count = count;
    for(i=0; i<count; i++) {
        result[i]=(2*i+1)|(2*i<<8);
    }
    if(read_fail)
        mbus_exception(2);
}

variable_types:
addr
unsigned short

variable_types:
count
unsigned char

variable_types:
result
unsigned short *

variable_types: i int


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

result = (unsigned int)write_fail;
++write_counter;
write_data = a2;
if ( write_fail )
result = mbus_exception(3LL);
return result;
}

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

result = (unsigned int)write_fail;
++write_counter;
write_data = a2;
if ( write_fail )
result = mbus_exception(3LL);
return result;
}

original_code:
void mbus_write_holding(uint16_t addr, uint16_t value)
{
    write_counter++;
    write_addr = addr;
    write_data = value;
    if(write_fail)
        mbus_exception(3);
}

canonical_original_code:
void mbus_write_holding(unsigned short addr, unsigned short value)
{
    write_counter++;
    write_addr = addr;
    write_data = value;
    if(write_fail)
        mbus_exception(3);
}

variable_types:
value
unsigned short


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
testPlan_constprop_0();
testDiag("run and reset state between tests", argv);
runtests(1);
testDiag("run tests w/o resets");
runtests(0);
return testDone();
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func3();
func4("run and reset state between tests", argv);
func16(1);
func4("run tests w/o resets");
func16(0);
return func0();
}

original_code:
int main(int argc, char** argv)
{
    testPlan(119);
    testDiag("run and reset state between tests");
    runtests(1);
    testDiag("run tests w/o resets");
    runtests(0);
    return testDone();
}

canonical_original_code:
int main(int argc, char **argv)
{
    testPlan(119);
    testDiag("run and reset state between tests");
    runtests(1);
    testDiag("run tests w/o resets");
    runtests(0);
    return testDone();
}

variable_types: argc int

variable_types:
argv
char **