Count

decompiled_code:
__int64 __fastcall Count(__int64 a1, int a2)
{
__int64 result; // rax
bool v3; // zf

for ( result = 0LL; a1; result = v3 + (unsigned int)result )
{
v3 = *(_DWORD *)a1 == a2;
a1 = *(_QWORD *)(a1 + 8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
__int64 result; // rax
bool v3; // zf

for ( result = 0LL; a1; result = v3 + (unsigned int)result )
{
v3 = *(_DWORD *)a1 == a2;
a1 = *(_QWORD *)(a1 + 8);
}
return result;
}

original_code:
int Count(struct node* head, int search_for)
{
 int count = 0;
 if( head == ((void *)0))
  return 0;
 struct node* current = head;
 while(current != ((void *)0))
 {
  if(current->data == search_for)
   count++;
  current=current->next;
 }
 return count;
}

canonical_original_code:
int Count(struct node *head, int search_for)
{
 int count = 0;
 if( head == ((void *)0))
  return 0;
 struct node *current = head;
 while(current != ((void *)0))
 {
  if(current->data == search_for)
   count++;
  current=current->next;
 }
 return count;
}


repo: ankitC/rev

decompiled_code:
__int64 __fastcall get_nth(__int64 a1, int a2)
{
unsigned int *v2; // rdx
int v3; // eax
__int64 result; // rax

if ( (int)list_length(a1) <= a2 )
{
puts("Invalid Position.");
result = 0LL;
}
else
{
v3 = 0;
if ( a2 > 0 )
{
do
{
++v3;
v2 = (unsigned int *)*((_QWORD *)v2 + 1);
}
while ( a2 != v3 );
}
result = *v2;
}
return result;
}

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

if ( (int)func8(a1) <= a2 )
{
puts("Invalid Position.");
result = 0LL;
}
else
{
v3 = 0;
if ( a2 > 0 )
{
do
{
++v3;
v2 = (unsigned int *)*((_QWORD *)v2 + 1);
}
while ( a2 != v3 );
}
result = *v2;
}
return result;
}

original_code:
int get_nth(struct node* head, int position)
{
 int i = 0;
 int data;
 struct node* current = head;
 if(position > list_length(head) - 1)
 {
  printf("Invalid Position.\n");
  return 0;
 }
 for(i = 0; i< position; i++)
 {
  current = current->next;
 }
 data = current->data;
 return data;
}

canonical_original_code:
int get_nth(struct node *head, int position)
{
 int i = 0;
 int data;
 struct node *current = head;
 if(position > list_length(head) - 1)
 {
  printf("Invalid Position.\n");
  return 0;
 }
 for(i = 0; i< position; i++)
 {
  current = current->next;
 }
 data = current->data;
 return data;
}

variable_types: struct node { int data; struct node *next; } *

variable_types: position int

variable_types: i int

variable_types: data int

return_type: int


pop

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

v1 = (int *)*a1;
v2 = 0;
if ( *a1 )
{
v2 = *v1;
*a1 = (void *)*((_QWORD *)v1 + 1);
free(v1);
}
return v2;
}

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

v1 = (int *)*a1;
v2 = 0;
if ( *a1 )
{
v2 = *v1;
*a1 = (void *)*((_QWORD *)v1 + 1);
free(v1);
}
return v2;
}

original_code:
int pop(struct node** headRef)
{
 struct node* head = *headRef;
 if(head == ((void *)0))
  return 0;
 int retval = head->data;
 *headRef = (*headRef)->next;
 free(head);
 return retval;
}

canonical_original_code:
int pop(struct node **headRef)
{
 struct node *head = *headRef;
 if(head == ((void *)0))
  return 0;
 int retval = head->data;
 *headRef = (*headRef)->next;
 free(head);
 return retval;
}

variable_types: struct node { int data; struct node *next; } **

variable_types: retval int


decompiled_code:
int __fastcall insert_nth(__int64 *a1, signed int a2)
{
__int64 v2; // rbx
int v3; // edx
int v4; // er13
_QWORD *v6; // rax
_QWORD *v7; // rdx
int v8; // esi
int v9; // eax

v2 = *a1;
if ( (int)(list_length(*a1) + 1) < a2 )
{
LODWORD(v6) = puts("Trying to insert at invalid position!");
}
else
{
v4 = v3;
v6 = malloc(0x10uLL);
*(_DWORD *)v6 = v4;
v7 = v6;
if ( a2 )
{
v8 = a2 - 1;
v9 = 0;
if ( a2 - 1 > 0 )
{
do
{
++v9;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v9 != v8 );
}
v6 = *(_QWORD **)(v2 + 8);
v7[1] = v6;
*(_QWORD *)(v2 + 8) = v7;
}
else
{
v6[1] = v2;
*a1 = (__int64)v6;
}
}
return (int)v6;
}

canonical_decompiled_code:
int __fastcall func14(__int64 *a1, signed int a2)
{
__int64 v2; // rbx
int v3; // edx
int v4; // er13
_QWORD *v6; // rax
_QWORD *v7; // rdx
int v8; // esi
int v9; // eax

v2 = *a1;
if ( (int)(func8(*a1) + 1) < a2 )
{
LODWORD(v6) = puts("Trying to insert at invalid position!");
}
else
{
v4 = v3;
v6 = malloc(0x10uLL);
*(_DWORD *)v6 = v4;
v7 = v6;
if ( a2 )
{
v8 = a2 - 1;
v9 = 0;
if ( a2 - 1 > 0 )
{
do
{
++v9;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v9 != v8 );
}
v6 = *(_QWORD **)(v2 + 8);
v7[1] = v6;
*(_QWORD *)(v2 + 8) = v7;
}
else
{
v6[1] = v2;
*a1 = (__int64)v6;
}
}
return (int)v6;
}

original_code:
void insert_nth(struct node** headRef, int pos, int element)
{
 struct node* head = *headRef;
 struct node* current = head;
 int i = 0;
 if(pos > (list_length(head) + 1))
 {
  printf("Trying to insert at invalid position!\n");
  return;
 }
 struct node* new_node = (struct node*) malloc(sizeof(struct node));
 new_node->data = element;
 if(pos == 0)
 {
  new_node->next = head;
  *headRef = new_node;
  return;
 }
 for(i=0; i<pos-1; i++)
  current = current->next;
 new_node->next = current->next;
 current->next = new_node;
}

canonical_original_code:
void insert_nth(struct node **headRef, int pos, int element)
{
 struct node *head = *headRef;
 struct node *current = head;
 int i = 0;
 if(pos > (list_length(head) + 1))
 {
  printf("Trying to insert at invalid position!\n");
  return;
 }
 struct node *new_node = (struct node*) malloc(sizeof(struct node));
 new_node->data = element;
 if(pos == 0)
 {
  new_node->next = head;
  *headRef = new_node;
  return;
 }
 for(i=0; i<pos-1; i++)
  current = current->next;
 new_node->next = current->next;
 current->next = new_node;
}

variable_types: pos int

variable_types: element int

return_type: void

user_defined_types: struct node { int data; struct node *next; }


decompiled_code:
void __fastcall remove_duplicates(__int64 a1)
{
__int64 v1; // rbp
_QWORD *v2; // rbx
int v3; // eax
void *v4; // rdi

if ( a1 )
{
v1 = a1;
v2 = *(_QWORD **)(a1 + 8);
while ( v2 )
{
while ( 1 )
{
v3 = *(_DWORD *)v2;
v4 = v2;
v2 = (_QWORD *)v2[1];
if ( *(_DWORD *)v1 == v3 )
break;
v1 = *(_QWORD *)(v1 + 8);
if ( !v2 )
return;
}
*(_QWORD *)(v1 + 8) = v2;
free(v4);
}
}
}

original_code:
void remove_duplicates(struct node* head)
{
 if(head == ((void *)0)||head->next == ((void *)0))
  return;
 struct node* current = head;
 struct node* next_node = head->next;
 struct node* to_free = ((void *)0);
 do
 {
  if(current->data == next_node->data)
  {
   to_free = next_node;
   next_node = next_node->next;
   current->next = next_node;
   free(to_free);
   continue;
  }
  current=current->next;
  next_node = next_node->next;
 }while(next_node!=((void *)0));
}

canonical_original_code:
void remove_duplicates(struct node *head)
{
 if(head == ((void *)0)||head->next == ((void *)0))
  return;
 struct node *current = head;
 struct node *next_node = head->next;
 struct node *to_free = ((void *)0);
 do
 {
  if(current->data == next_node->data)
  {
   to_free = next_node;
   next_node = next_node->next;
   current->next = next_node;
   free(to_free);
   continue;
  }
  current=current->next;
  next_node = next_node->next;
 }while(next_node!=((void *)0));
}


decompiled_code:
__int64 __fastcall alternating_split(__int64 a1, _QWORD *a2, _QWORD *a3)
{
__int64 result; // rax
_QWORD *v5; // r8
__int64 v6; // [rsp+0h] [rbp-8h] BYREF

*a2 = 0LL;
v6 = a1;
*a3 = 0LL;
while ( v6 )
{
result = move_node(a2, &v6);
if ( !v6 )
break;
result = move_node(v5, &v6);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, _QWORD *a2, _QWORD *a3)
{
__int64 result; // rax
_QWORD *v5; // r8
__int64 v6; // [rsp+0h] [rbp-8h] BYREF

*a2 = 0LL;
v6 = a1;
*a3 = 0LL;
while ( v6 )
{
result = func17(a2, &v6);
if ( !v6 )
break;
result = func17(v5, &v6);
}
return result;
}

original_code:
void alternating_split(struct node* source, struct node** aRef, struct node** bRef)
{
 *aRef = ((void *)0);
 *bRef = ((void *)0);
 while(source!=((void *)0))
 {
  move_node(aRef, &source);
  if(source!=((void *)0))
   move_node(bRef, &source);
 }
}

canonical_original_code:
void alternating_split(struct node *source, struct node **aRef, struct node **bRef)
{
 *aRef = ((void *)0);
 *bRef = ((void *)0);
 while(source!=((void *)0))
 {
  move_node(aRef, &source);
  if(source!=((void *)0))
   move_node(bRef, &source);
 }
}

variable_types:
aRef
struct node **

variable_types:
bRef
struct node **


original_code:
struct node* shuffle_merge(struct node* a, struct node* b)
{
 struct node* returned;
 if(a == ((void *)0) && b == ((void *)0))
  return ((void *)0);
 else if(a == ((void *)0) && b!=((void *)0))
 {
  returned = shuffle_merge(((void *)0), b->next);
  b->next = returned;
  return b;
 }
 else if(a!=((void *)0) && b==((void *)0))
 {
  returned = shuffle_merge(a->next, ((void *)0));
  a->next = returned;
  return a;
 }
 else if(a!=((void *)0) && b!=((void *)0))
 {
  returned = shuffle_merge(a->next, b->next);
  b->next = returned;
  a->next = b;
  return a;
 }
}

canonical_original_code:
struct node *shuffle_merge(struct node *a, struct node *b)
{
 struct node *returned;
 if(a == ((void *)0) && b == ((void *)0))
  return ((void *)0);
 else if(a == ((void *)0) && b!=((void *)0))
 {
  returned = shuffle_merge(((void *)0), b->next);
  b->next = returned;
  return b;
 }
 else if(a!=((void *)0) && b==((void *)0))
 {
  returned = shuffle_merge(a->next, ((void *)0));
  a->next = returned;
  return a;
 }
 else if(a!=((void *)0) && b!=((void *)0))
 {
  returned = shuffle_merge(a->next, b->next);
  b->next = returned;
  a->next = b;
  return a;
 }
}

variable_types:
a
struct node *

variable_types:
b
struct node *

return_type: struct node { int data; struct node *next; } *


original_code:
struct node* sorted_merge(struct node* a, struct node* b)
{
 struct node* returned = ((void *)0);
 if(a==((void *)0) && b==((void *)0))
  return ((void *)0);
 else if(a!=((void *)0) && b==((void *)0))
 {
  returned = sorted_merge(a->next, ((void *)0));
  move_node(&returned, &a);
 }
 else if(a==((void *)0) && b!=((void *)0))
 {
  returned = sorted_merge(((void *)0), b->next);
  move_node(&returned, &b);
 }
 else if(a!=((void *)0) && b!=((void *)0))
 {
  if(b->data <= a->data)
  {
   returned = sorted_merge(a, b->next);
   b->next == ((void *)0);
   move_node(&returned,&b);
  }
  else
  {
   returned = sorted_merge(a->next, b);
   a->next == ((void *)0);
   move_node(&returned,&a);
  }
 }
 return returned;
}

canonical_original_code:
struct node *sorted_merge(struct node *a, struct node *b)
{
 struct node *returned = ((void *)0);
 if(a==((void *)0) && b==((void *)0))
  return ((void *)0);
 else if(a!=((void *)0) && b==((void *)0))
 {
  returned = sorted_merge(a->next, ((void *)0));
  move_node(&returned, &a);
 }
 else if(a==((void *)0) && b!=((void *)0))
 {
  returned = sorted_merge(((void *)0), b->next);
  move_node(&returned, &b);
 }
 else if(a!=((void *)0) && b!=((void *)0))
 {
  if(b->data <= a->data)
  {
   returned = sorted_merge(a, b->next);
   b->next == ((void *)0);
   move_node(&returned,&b);
  }
  else
  {
   returned = sorted_merge(a->next, b);
   a->next == ((void *)0);
   move_node(&returned,&a);
  }
 }
 return returned;
}


decompiled_code:
int __fastcall merge_sort(__int64 *a1)
{
unsigned int *v2; // rdi
int result; // eax
unsigned int *v4; // rdi
unsigned int *v5; // rdi
__int64 v6; // rdx
unsigned int *v7; // rax
__int128 v8; // [rsp+0h] [rbp-18h] BYREF

v2 = (unsigned int *)*a1;
v8 = 0uLL;
result = print_list(v2);
if ( *(_QWORD *)(*a1 + 8) )
{
front_back_split(*a1, (__int64 *)&v8, (__int64 **)&v8 + 1);
v4 = (unsigned int *)v8;
if ( (_QWORD)v8 )
{
merge_sort(&v8);
v4 = (unsigned int *)v8;
}
print_list(v4);
v5 = (unsigned int *)*((_QWORD *)&v8 + 1);
if ( *((_QWORD *)&v8 + 1) )
{
merge_sort((char *)&v8 + 8);
v5 = (unsigned int *)*((_QWORD *)&v8 + 1);
}
print_list(v5);
v7 = (unsigned int *)sorted_merge(v8, v6);
*a1 = (__int64)v7;
result = print_list(v7);
}
return result;
}

canonical_decompiled_code:
int __fastcall func21(__int64 *a1)
{
unsigned int *v2; // rdi
int result; // eax
unsigned int *v4; // rdi
unsigned int *v5; // rdi
__int64 v6; // rdx
unsigned int *v7; // rax
__int128 v8; // [rsp+0h] [rbp-18h] BYREF

v2 = (unsigned int *)*a1;
v8 = 0uLL;
result = func9(v2);
if ( *(_QWORD *)(*a1 + 8) )
{
front_back_split(*a1, (__int64 *)&v8, (__int64 **)&v8 + 1);
v4 = (unsigned int *)v8;
if ( (_QWORD)v8 )
{
func21(&v8);
v4 = (unsigned int *)v8;
}
func9(v4);
v5 = (unsigned int *)*((_QWORD *)&v8 + 1);
if ( *((_QWORD *)&v8 + 1) )
{
func21((char *)&v8 + 8);
v5 = (unsigned int *)*((_QWORD *)&v8 + 1);
}
func9(v5);
v7 = (unsigned int *)func20(v8, v6);
*a1 = (__int64)v7;
result = func9(v7);
}
return result;
}

original_code:
void merge_sort(struct node** headRef)
{
 struct node* part1 = ((void *)0);
 struct node* part2 = ((void *)0);
 print_list(*headRef);
 if((*headRef)->next == ((void *)0))
  return;
 front_back_split(*headRef,&part1,&part2);
 if(part1!=((void *)0))
  merge_sort(&part1);
 print_list(part1);
 if(part2!=((void *)0))
  merge_sort(&part2);
 print_list(part2);
 *headRef = sorted_merge(part1,part2);
 print_list(*headRef);
 return;
}

canonical_original_code:
void merge_sort(struct node **headRef)
{
 struct node *part1 = ((void *)0);
 struct node *part2 = ((void *)0);
 print_list(*headRef);
 if((*headRef)->next == ((void *)0))
  return;
 front_back_split(*headRef,&part1,&part2);
 if(part1!=((void *)0))
  merge_sort(&part1);
 print_list(part1);
 if(part2!=((void *)0))
  merge_sort(&part2);
 print_list(part2);
 *headRef = sorted_merge(part1,part2);
 print_list(*headRef);
 return;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4[2]; // [rsp+8h] [rbp-10h] BYREF

v4[0] = BuildOneTwoThree(argc, argv, envp);
print_list(v4[0]);
insert_nth(v4, 0LL, 10LL);
insert_nth(v4, 1LL, 20LL);
insert_nth(v4, 3LL, 14LL);
insert_nth(v4, 4LL, 78LL);
insert_nth(v4, 1LL, 32LL);
insert_nth(v4, 3LL, 42LL);
print_list(v4[0]);
reverse(v4);
return print_list(v4[0]);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4[2]; // [rsp+8h] [rbp-10h] BYREF

v4[0] = func7(argc, argv, envp);
func9(v4[0]);
func14(v4, 0LL, 10LL);
func14(v4, 1LL, 20LL);
func14(v4, 3LL, 14LL);
func14(v4, 4LL, 78LL);
func14(v4, 1LL, 32LL);
func14(v4, 3LL, 42LL);
func9(v4[0]);
func22(v4);
return func9(v4[0]);
}

original_code:
void main()
{
 struct node* head = BuildOneTwoThree();
 print_list(head);
 insert_nth(&head,0,10);
 insert_nth(&head,1,20);
 insert_nth(&head,3,14);
 insert_nth(&head,4,78);
 insert_nth(&head,1,32);
 insert_nth(&head,3,42);
 print_list(head);
 reverse(&head);
 print_list(head);
}

canonical_original_code:
void main()
{
 struct node *head = BuildOneTwoThree();
 print_list(head);
 insert_nth(&head,0,10);
 insert_nth(&head,1,20);
 insert_nth(&head,3,14);
 insert_nth(&head,4,78);
 insert_nth(&head,1,32);
 insert_nth(&head,3,42);
 print_list(head);
 reverse(&head);
 print_list(head);
}


decompiled_code:
int __fastcall print_inorder(unsigned int *a1)
{
unsigned int *v1; // rbx
int result; // eax

if ( a1 )
{
v1 = a1;
do
{
print_inorder(*((_QWORD *)v1 + 1));
result = printf("%d\t", *v1);
v1 = (unsigned int *)*((_QWORD *)v1 + 2);
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned int *a1)
{
unsigned int *v1; // rbx
int result; // eax

if ( a1 )
{
v1 = a1;
do
{
func2(*((_QWORD *)v1 + 1));
result = printf("%d\t", *v1);
v1 = (unsigned int *)*((_QWORD *)v1 + 2);
}
while ( v1 );
}
return result;
}

original_code:
void print_inorder(struct node* node)
{
 if(node == ((void *)0))
  return;
 print_inorder(node->left);
 printf("%d\t",node->data);
 print_inorder(node->right);
}

canonical_original_code:
void print_inorder(struct node *node)
{
 if(node == ((void *)0))
  return;
 print_inorder(node->left);
 printf("%d\t",node->data);
 print_inorder(node->right);
}

variable_types: struct node { int data; struct node *left; struct node *right; } *


decompiled_code:
int __fastcall print_postorder(unsigned int **a1)
{
int result; // eax

if ( a1 )
{
print_inorder(a1[1]);
print_inorder(a1[2]);
result = printf("%d\t", *(unsigned int *)a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(unsigned int **a1)
{
int result; // eax

if ( a1 )
{
func2(a1[1]);
func2(a1[2]);
result = printf("%d\t", *(unsigned int *)a1);
}
return result;
}

original_code:
void print_postorder(struct node* node)
{
 if(node == ((void *)0))
  return;
 print_inorder(node->left);
 print_inorder(node->right);
 printf("%d\t",node->data);
}

canonical_original_code:
void print_postorder(struct node *node)
{
 if(node == ((void *)0))
  return;
 print_inorder(node->left);
 print_inorder(node->right);
 printf("%d\t",node->data);
}

user_defined_types: struct node { int data; struct node *left; struct node *right; }


decompiled_code:
unsigned int *build123()
{
__int64 v0; // rax
__int64 v1; // rax
unsigned int *v2; // rax
unsigned int *v3; // r12

insert(0LL, 2);
insert(v0, 1);
insert(v1, 3);
v3 = v2;
print_inorder(v2);
putchar(10);
return v3;
}

canonical_decompiled_code:
unsigned int *func4()
{
__int64 v0; // rax
__int64 v1; // rax
unsigned int *v2; // rax
unsigned int *v3; // r12

func1(0LL, 2);
func1(v0, 1);
func1(v1, 3);
v3 = v2;
func2(v2);
putchar(10);
return v3;
}

original_code:
struct node* build123()
{
 struct node* root = ((void *)0);
 root = insert(root, 2);
 root = insert(root, 1);
 root = insert(root, 3);
 print_inorder(root);
 printf("\n");
 return root;
}

canonical_original_code:
struct node *build123()
{
 struct node *root = ((void *)0);
 root = insert(root, 2);
 root = insert(root, 1);
 root = insert(root, 3);
 print_inorder(root);
 printf("\n");
 return root;
}

return_type: struct node { int data; struct node *left; struct node *right; } *


size

original_code:
int size(struct node* node)
{
 if(node == ((void *)0))
  return 0;
 int n = 1 + size(node->left) + size(node->right);
 return n;
}

canonical_original_code:
int size(struct node *node)
{
 if(node == ((void *)0))
  return 0;
 int n = 1 + size(node->left) + size(node->right);
 return n;
}

variable_types: n int


decompiled_code:
__int64 __fastcall max_depth(__int64 a1)
{
int v1; // ebp
int v2; // edx
__int64 result; // rax

if ( !a1 )
return 0LL;
v1 = max_depth(*(_QWORD *)(a1 + 8));
v2 = max_depth(*(_QWORD *)(a1 + 16));
result = (unsigned int)(v1 + 1);
if ( v1 <= v2 )
result = (unsigned int)(v2 + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
int v1; // ebp
int v2; // edx
__int64 result; // rax

if ( !a1 )
return 0LL;
v1 = func7(*(_QWORD *)(a1 + 8));
v2 = func7(*(_QWORD *)(a1 + 16));
result = (unsigned int)(v1 + 1);
if ( v1 <= v2 )
result = (unsigned int)(v2 + 1);
return result;
}

original_code:
int max_depth(struct node* node)
{
 if(node == ((void *)0))
  return 0;
 int left_depth = 1+max_depth(node->left);
 int right_depth = 1+max_depth(node->right);
 if(right_depth >= left_depth)
  return right_depth;
 else
  return left_depth;
}

canonical_original_code:
int max_depth(struct node *node)
{
 if(node == ((void *)0))
  return 0;
 int left_depth = 1+max_depth(node->left);
 int right_depth = 1+max_depth(node->right);
 if(right_depth >= left_depth)
  return right_depth;
 else
  return left_depth;
}


decompiled_code:
__int64 __fastcall min_value(signed int *a1)
{
signed int v1; // er12
signed int v2; // eax
signed int v3; // eax

v1 = *a1;
if ( *((_QWORD *)a1 + 1) )
{
v2 = min_value();
if ( v1 > v2 )
v1 = v2;
}
if ( *((_QWORD *)a1 + 2) )
{
v3 = min_value();
if ( v1 > v3 )
v1 = v3;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(signed int *a1)
{
signed int v1; // er12
signed int v2; // eax
signed int v3; // eax

v1 = *a1;
if ( *((_QWORD *)a1 + 1) )
{
v2 = func8();
if ( v1 > v2 )
v1 = v2;
}
if ( *((_QWORD *)a1 + 2) )
{
v3 = func8();
if ( v1 > v3 )
v1 = v3;
}
return (unsigned int)v1;
}

original_code:
int min_value(struct node* node)
{
 int min_val;
 int left_value, right_value;
 min_val = node->data;
 if(node->left != ((void *)0))
 {
  left_value = min_value(node->left);
  if(min_val > left_value)
   min_val = left_value;
 }
 if(node->right!=((void *)0))
 {
  right_value = min_value(node->right);
  if(min_val > right_value)
   min_val = right_value;
 }
 return min_val;
}

canonical_original_code:
int min_value(struct node *node)
{
 int min_val;
 int left_value; int right_value;
 min_val = node->data;
 if(node->left != ((void *)0))
 {
  left_value = min_value(node->left);
  if(min_val > left_value)
   min_val = left_value;
 }
 if(node->right!=((void *)0))
 {
  right_value = min_value(node->right);
  if(min_val > right_value)
   min_val = right_value;
 }
 return min_val;
}


decompiled_code:
__int64 __fastcall max_value(signed int *a1)
{
signed int v1; // er12
signed int v2; // eax
signed int v3; // eax

v1 = *a1;
if ( *((_QWORD *)a1 + 1) )
{
v2 = max_value();
if ( v1 < v2 )
v1 = v2;
}
if ( *((_QWORD *)a1 + 2) )
{
v3 = max_value();
if ( v1 < v3 )
v1 = v3;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(signed int *a1)
{
signed int v1; // er12
signed int v2; // eax
signed int v3; // eax

v1 = *a1;
if ( *((_QWORD *)a1 + 1) )
{
v2 = func9();
if ( v1 < v2 )
v1 = v2;
}
if ( *((_QWORD *)a1 + 2) )
{
v3 = func9();
if ( v1 < v3 )
v1 = v3;
}
return (unsigned int)v1;
}

original_code:
int max_value(struct node* node)
{
 int max_val;
 int left_value, right_value;
 max_val = node->data;
 if(node->left != ((void *)0))
 {
  left_value = max_value(node->left);
  if(max_val < left_value)
   max_val = left_value;
 }
 if(node->right!=((void *)0))
 {
  right_value = max_value(node->right);
  if(max_val < right_value)
   max_val = right_value;
 }
 return max_val;
}

canonical_original_code:
int max_value(struct node *node)
{
 int max_val;
 int left_value; int right_value;
 max_val = node->data;
 if(node->left != ((void *)0))
 {
  left_value = max_value(node->left);
  if(max_val < left_value)
   max_val = left_value;
 }
 if(node->right!=((void *)0))
 {
  right_value = max_value(node->right);
  if(max_val < right_value)
   max_val = right_value;
 }
 return max_val;
}


decompiled_code:
__int64 __fastcall has_path_sum(__int64 a1, int a2)
{
__int64 v4; // rdi
__int64 result; // rax

while ( *(_DWORD *)a1 != a2 )
{
a2 -= *(_DWORD *)a1;
if ( a2 <= 0 )
return 0LL;
v4 = *(_QWORD *)(a1 + 8);
if ( v4 )
{
result = has_path_sum(v4, (unsigned int)a2);
a1 = *(_QWORD *)(a1 + 16);
if ( (_DWORD)result || !a1 )
return result;
}
else
{
a1 = *(_QWORD *)(a1 + 16);
if ( !a1 )
return 0LL;
}
}
result = 0LL;
if ( !*(_QWORD *)(a1 + 16) )
result = *(_QWORD *)(a1 + 8) == 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
__int64 v4; // rdi
__int64 result; // rax

while ( *(_DWORD *)a1 != a2 )
{
a2 -= *(_DWORD *)a1;
if ( a2 <= 0 )
return 0LL;
v4 = *(_QWORD *)(a1 + 8);
if ( v4 )
{
result = func10(v4, (unsigned int)a2);
a1 = *(_QWORD *)(a1 + 16);
if ( (_DWORD)result || !a1 )
return result;
}
else
{
a1 = *(_QWORD *)(a1 + 16);
if ( !a1 )
return 0LL;
}
}
result = 0LL;
if ( !*(_QWORD *)(a1 + 16) )
result = *(_QWORD *)(a1 + 8) == 0LL;
return result;
}

original_code:
int has_path_sum(struct node* node, int sum)
{
 int chksum = sum;
 int retval = 0;
 if(node->data == sum && node->right == ((void *)0) && node->left == ((void *)0))
  return 1;
 chksum = sum - node->data;
 if(chksum > 0 && node->left!=((void *)0) && !retval)
  retval = has_path_sum(node->left, chksum);
 if(chksum > 0 && node->right!=((void *)0) && !retval)
  retval = has_path_sum(node->right, chksum);
 return retval;
}

canonical_original_code:
int has_path_sum(struct node *node, int sum)
{
 int chksum = sum;
 int retval = 0;
 if(node->data == sum && node->right == ((void *)0) && node->left == ((void *)0))
  return 1;
 chksum = sum - node->data;
 if(chksum > 0 && node->left!=((void *)0) && !retval)
  retval = has_path_sum(node->left, chksum);
 if(chksum > 0 && node->right!=((void *)0) && !retval)
  retval = has_path_sum(node->right, chksum);
 return retval;
}

variable_types: sum int

variable_types: chksum int


mirror

decompiled_code:
__int64 __fastcall mirror(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

if ( a1 )
{
mirror(*(_QWORD *)(a1 + 8));
mirror(*(_QWORD *)(a1 + 16));
result = *(_QWORD *)(a1 + 8);
v2 = *(_QWORD *)(a1 + 16);
*(_QWORD *)(a1 + 16) = result;
*(_QWORD *)(a1 + 8) = v2;
}
return result;
}

original_code:
void mirror(struct node* node)
{
 if(node == ((void *)0))
  return;
 struct node* right_node = ((void *)0);
 struct node* left_node = ((void *)0);
 mirror(node->left);
 mirror(node->right);
 struct node* temp = node->left;
 node->left = node->right;
 node->right = temp;
 return;
}

canonical_original_code:
void mirror(struct node *node)
{
 if(node == ((void *)0))
  return;
 struct node *right_node = ((void *)0);
 struct node *left_node = ((void *)0);
 mirror(node->left);
 mirror(node->right);
 struct node *temp = node->left;
 node->left = node->right;
 node->right = temp;
 return;
}


decompiled_code:
int __fastcall print_paths_recur(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // ebp
_DWORD *i; // r12
int result; // eax
__int64 v8; // rdi
int v9; // eax
int v10; // er14
__int64 v11; // rsi

v4 = a3 + 1;
for ( i = (_DWORD *)(a2 + 4LL * a3); ; ++i )
{
v8 = *(_QWORD *)(a1 + 8);
v9 = *(_DWORD *)a1;
v10 = v4 - 1;
if ( !v8 )
break;
*i = v9;
result = print_paths_recur(v8, a2, v4);
a1 = *(_QWORD *)(a1 + 16);
if ( !a1 )
return result;
LABEL_3:
++v4;
}
a1 = *(_QWORD *)(a1 + 16);
*i = v9;
if ( a1 )
goto LABEL_3;
if ( v10 >= 0 )
{
do
{
v11 = *(unsigned int *)(a2 + 4 * a1++);
printf("%d\t", v11);
}
while ( v10 >= (int)a1 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // ebp
_DWORD *i; // r12
int result; // eax
__int64 v8; // rdi
int v9; // eax
int v10; // er14
__int64 v11; // rsi

v4 = a3 + 1;
for ( i = (_DWORD *)(a2 + 4LL * a3); ; ++i )
{
v8 = *(_QWORD *)(a1 + 8);
v9 = *(_DWORD *)a1;
v10 = v4 - 1;
if ( !v8 )
break;
*i = v9;
result = func12(v8, a2, v4);
a1 = *(_QWORD *)(a1 + 16);
if ( !a1 )
return result;
LABEL_3:
++v4;
}
a1 = *(_QWORD *)(a1 + 16);
*i = v9;
if ( a1 )
goto LABEL_3;
if ( v10 >= 0 )
{
do
{
v11 = *(unsigned int *)(a2 + 4 * a1++);
printf("%d\t", v11);
}
while ( v10 >= (int)a1 );
}
return putchar(10);
}

original_code:
void print_paths_recur(struct node* node, int a[], int position)
{
 int i = 0;
 if(node->left == ((void *)0) && node->right == ((void *)0))
 {
  a[position] = node->data;
  for(i=0; i<=position; i++)
   printf("%d\t",a[i]);
  printf("\n");
  return;
 }
 a[position] = node->data;
 position++;
 if(node->left!=((void *)0))
  print_paths_recur(node->left, a, position);
 if(node->right!=((void *)0))
  print_paths_recur(node->right,a, position);
}

canonical_original_code:
void print_paths_recur(struct node *node, int a[], int position)
{
 int i = 0;
 if(node->left == ((void *)0) && node->right == ((void *)0))
 {
  a[position] = node->data;
  for(i=0; i<=position; i++)
   printf("%d\t",a[i]);
  printf("\n");
  return;
 }
 a[position] = node->data;
 position++;
 if(node->left!=((void *)0))
  print_paths_recur(node->left, a, position);
 if(node->right!=((void *)0))
  print_paths_recur(node->right,a, position);
}

variable_types:
a
int []


decompiled_code:
int __fastcall print_paths(__int64 a1)
{
void *v1; // rsp
__int64 v2; // r8
__int64 savedregs; // [rsp+0h] [rbp+0h] BYREF

v1 = alloca(4LL * (int)max_depth(a1));
return print_paths_recur(v2, (__int64)&savedregs, 0);
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1)
{
void *v1; // rsp
__int64 v2; // r8
__int64 savedregs; // [rsp+0h] [rbp+0h] BYREF

v1 = alloca(4LL * (int)max_depth(a1));
return func12(v2, (__int64)&savedregs, 0);
}

original_code:
void print_paths(struct node* node)
{
 int size = max_depth(node);
 int a[size];
 int position = 0;
 print_paths_recur(node, a, position);
}

canonical_original_code:
void print_paths(struct node *node)
{
 int size = max_depth(node);
 int a[-1];
 int position = 0;
 print_paths_recur(node, a, position);
}

variable_types: size int

variable_types:
a
int [-1]


decompiled_code:
void __fastcall double_tree(__int64 a1)
{
__int64 v1; // rax

if ( a1 )
{
double_tree(*(_QWORD *)(a1 + 8));
double_tree(*(_QWORD *)(a1 + 16));
new_node();
*(_QWORD *)(v1 + 8) = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v1;
}
}

original_code:
void double_tree(struct node* node)
{
 if(node==((void *)0))
  return;
 double_tree(node->left);
 double_tree(node->right);
 struct node* new_node1 = new_node(node->data);
 new_node1->left = node->left;
 node->left = new_node1;
}

canonical_original_code:
void double_tree(struct node *node)
{
 if(node==((void *)0))
  return;
 double_tree(node->left);
 double_tree(node->right);
 struct node *new_node1 = new_node(node->data);
 new_node1->left = node->left;
 node->left = new_node1;
}


original_code:
int same_tree(struct node* a, struct node* b)
{
 int right_value = 0, left_value = 0;
 if((a == ((void *)0) && b!=((void *)0)) || (a!=((void *)0) && b == ((void *)0)))
  return 0;
 if(a->left!=((void *)0) && b->left!=((void *)0))
  left_value = same_tree(a->left, b->left);
 else if(a->left==((void *)0) && b->left==((void *)0))
  left_value = 1;
 if(a->right!=((void *)0) && b->right!=((void *)0))
  right_value = same_tree(a->right, b->right);
 else if(a->right==((void *)0) && b->right==((void *)0))
  right_value = 1;
 if(a->data == b->data)
  return (1 && left_value && right_value);
 else
  return 0;
}

canonical_original_code:
int same_tree(struct node *a, struct node *b)
{
 int right_value = 0; int left_value = 0;
 if((a == ((void *)0) && b!=((void *)0)) || (a!=((void *)0) && b == ((void *)0)))
  return 0;
 if(a->left!=((void *)0) && b->left!=((void *)0))
  left_value = same_tree(a->left, b->left);
 else if(a->left==((void *)0) && b->left==((void *)0))
  left_value = 1;
 if(a->right!=((void *)0) && b->right!=((void *)0))
  right_value = same_tree(a->right, b->right);
 else if(a->right==((void *)0) && b->right==((void *)0))
  right_value = 1;
 if(a->data == b->data)
  return (1 && left_value && right_value);
 else
  return 0;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
unsigned int *v8; // rax
__int64 v9; // rbp
unsigned int *v10; // rax
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rax
__int64 v14; // rax
unsigned int *v15; // rax
__int64 v16; // r12
unsigned int v17; // eax

v3 = build123();
insert((__int64)v3, 5);
insert(v4, 9);
insert(v5, 4);
insert(v6, 6);
insert(v7, 10);
v9 = (__int64)v8;
print_inorder(v8);
putchar(10);
v10 = build123();
insert((__int64)v10, 5);
insert(v11, 10);
insert(v12, 9);
insert(v13, 4);
insert(v14, 6);
v16 = (__int64)v15;
print_inorder(v15);
putchar(10);
v17 = same_tree(v9, v16);
return printf("Same:%d\n", v17);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
unsigned int *v8; // rax
__int64 v9; // rbp
unsigned int *v10; // rax
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rax
__int64 v14; // rax
unsigned int *v15; // rax
__int64 v16; // r12
unsigned int v17; // eax

v3 = func4();
func1((__int64)v3, 5);
func1(v4, 9);
func1(v5, 4);
func1(v6, 6);
func1(v7, 10);
v9 = (__int64)v8;
func2(v8);
putchar(10);
v10 = func4();
func1((__int64)v10, 5);
func1(v11, 10);
func1(v12, 9);
func1(v13, 4);
func1(v14, 6);
v16 = (__int64)v15;
func2(v15);
putchar(10);
v17 = func15(v9, v16);
return printf("Same:%d\n", v17);
}

original_code:
void main()
{
 int n = 0;
 struct node* root = build123();
 root = insert(root, 5);
 root = insert(root, 9);
 root = insert(root, 4);
 root = insert(root , 6);
 root = insert(root , 10);
 print_inorder(root);
 printf("\n");
 struct node* root1 = build123();
 root1 = insert(root1, 5);
 root1 = insert(root1 , 10);
 root1 = insert(root1, 9);
 root1 = insert(root1, 4);
 root1 = insert(root1 , 6);
 print_inorder(root1);
 printf("\n");
 n=same_tree(root, root1);
 printf("Same:%d\n", n);
 return;
}

canonical_original_code:
void main()
{
 int n = 0;
 struct node *root = build123();
 root = insert(root, 5);
 root = insert(root, 9);
 root = insert(root, 4);
 root = insert(root , 6);
 root = insert(root , 10);
 print_inorder(root);
 printf("\n");
 struct node *root1 = build123();
 root1 = insert(root1, 5);
 root1 = insert(root1 , 10);
 root1 = insert(root1, 9);
 root1 = insert(root1, 4);
 root1 = insert(root1 , 6);
 print_inorder(root1);
 printf("\n");
 n=same_tree(root, root1);
 printf("Same:%d\n", n);
 return;
}


decompiled_code:
int __fastcall print_array(unsigned int *a1, int a2)
{
__int64 v2; // rbp
unsigned int *v3; // rbx
__int64 v4; // rsi

if ( a2 > 0 )
{
v2 = (__int64)&a1[a2 - 1 + 1];
v3 = a1;
do
{
v4 = *v3++;
printf("%d ", v4);
}
while ( v3 != (unsigned int *)v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int *a1, int a2)
{
__int64 v2; // rbp
unsigned int *v3; // rbx
__int64 v4; // rsi

if ( a2 > 0 )
{
v2 = (__int64)&a1[a2 - 1 + 1];
v3 = a1;
do
{
v4 = *v3++;
printf("%d ", v4);
}
while ( v3 != (unsigned int *)v2 );
}
return putchar(10);
}

original_code:
void print_array(int a[], int length)
{
 int i = 0;
 for(i = 0; i<length; i++)
  printf("%d ",a[i]);
 printf("\n");
}

variable_types: length int

variable_types: i int


decompiled_code:
void __fastcall generate_array(__int64 a1, int a2)
{
__int64 v2; // rbp
unsigned int v3; // eax

v2 = a1;
v3 = time(0LL);
srand(v3);
if ( a2 > 0 )
{
do
{
v2 += 4LL;
*(_DWORD *)(v2 - 4) = rand() % 100;
}
while ( v2 != a1 + 4LL * (unsigned int)(a2 - 1) + 4 );
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, int a2)
{
__int64 v2; // rbp
unsigned int v3; // eax

v2 = a1;
v3 = time(0LL);
srand(v3);
if ( a2 > 0 )
{
do
{
v2 += 4LL;
*(_DWORD *)(v2 - 4) = rand() % 100;
}
while ( v2 != a1 + 4LL * (unsigned int)(a2 - 1) + 4 );
}
}

original_code:
void generate_array(int a[], int length)
{
 int i = 0;
 srand ( time(((void *)0)) );
 for(i=0;i<length; i++)
  a[i] = rand()%100;
}


original_code:
void selection_sort(int a[], int length)
{
 int i, j, temp;
 int min;
 for(i=0; i<length; i++)
 {
  min = i;
  for(j=i; j<length; j++)
  {
   if(a[j]<a[min])
    min = j;
  }
  temp = a[i];
  a[i] = a[min];
  a[min] = temp;
 }
}

canonical_original_code:
void selection_sort(int a[], int length)
{
 int i; int j; int temp;
 int min;
 for(i=0; i<length; i++)
 {
  min = i;
  for(j=i; j<length; j++)
  {
   if(a[j]<a[min])
    min = j;
  }
  temp = a[i];
  a[i] = a[min];
  a[min] = temp;
 }
}

variable_types: j int

variable_types: temp int

variable_types: min int


original_code:
void bubble_sort(int a[], int length)
{
 int i, j, temp;
 for(i=0; i<length; i++)
 {
  for(j=0; j<length - i - 1; j++)
  {
   if(a[j] > a[j+1])
   {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
   }
  }
 }
}

canonical_original_code:
void bubble_sort(int a[], int length)
{
 int i; int j; int temp;
 for(i=0; i<length; i++)
 {
  for(j=0; j<length - i - 1; j++)
  {
   if(a[j] > a[j+1])
   {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
   }
  }
 }
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int128 v4[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+20h] [rbp-18h]

v4[0] = 0LL;
v4[1] = 0LL;
v5 = 0LL;
generate_array(v4, 10LL, envp);
print_array(v4, 10LL);
puts("sorting...");
selection_sort(v4, 10LL);
return print_array(v4, 10LL);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int128 v4[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+20h] [rbp-18h]

v4[0] = 0LL;
v4[1] = 0LL;
v5 = 0LL;
func8(v4, 10LL, envp);
func7(v4, 10LL);
puts("sorting...");
func9(v4, 10LL);
return func7(v4, 10LL);
}

original_code:
void main()
{
 int a[10] = {0};
 generate_array(a, 10);
 print_array(a,10);
 printf("sorting...\n");
 selection_sort(a, 10);
 print_array(a,10);
 return;
}

variable_types:
a
int [10]


repo: anlif/micropong

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // al
char v4; // al
char v5; // al
char v7; // al

hw_init(argc, argv, envp);
pong_init();
while ( 1 )
{
switch ( (unsigned int)hw_get_input() )
{
case 0u:
v7 = pong_get_paddle_state(0LL);
pong_move_paddle(0LL, (unsigned __int8)(v7 - 2));
break;
case 1u:
v5 = pong_get_paddle_state(0LL);
pong_move_paddle(0LL, (unsigned __int8)(v5 + 2));
break;
case 2u:
v4 = pong_get_paddle_state(1LL);
pong_move_paddle(1LL, (unsigned __int8)(v4 - 2));
break;
case 3u:
v3 = pong_get_paddle_state(1LL);
pong_move_paddle(1LL, (unsigned __int8)(v3 + 2));
break;
case 4u:
if ( (unsigned __int8)pong_move_ball() != 2 )
{
puts("restart");
pong_restart();
}
hw_draw();
draw_swap_buffers();
draw_copy_buffers();
hw_deinit();
return 0;
default:
break;
}
if ( (unsigned __int8)pong_move_ball() != 2 )
{
puts("restart");
pong_restart();
}
hw_draw();
draw_swap_buffers();
draw_copy_buffers();
}
}

original_code:
int main(){
 hw_init();
 pong_init();
 int run = 1;
 _Bool play = 1;
 while(run){
  if(play){
   switch(hw_get_input()){
    case EXIT:
     run = 0;
     break;
    case ONE_UP:
     pong_move_paddle(PONG_PADDLE_LEFT,
       pong_get_paddle_state(PONG_PADDLE_LEFT) - 2);
     break;
    case ONE_DOWN:
     pong_move_paddle(PONG_PADDLE_LEFT,
       pong_get_paddle_state(PONG_PADDLE_LEFT) + 2);
     break;
    case TWO_UP:
     pong_move_paddle(PONG_PADDLE_RIGHT,
       pong_get_paddle_state(PONG_PADDLE_RIGHT) - 2);
     break;
    case TWO_DOWN:
     pong_move_paddle(PONG_PADDLE_RIGHT,
       pong_get_paddle_state(PONG_PADDLE_RIGHT) + 2);
     break;
   }
   int status = pong_move_ball();
   if( status != PONG_NO_ERROR ){
    printf("restart\n");
    pong_restart();
   }
   hw_draw();
   draw_swap_buffers();
   draw_copy_buffers();
  }
 }
 hw_deinit();
 return 0;
}

variable_types: run int

variable_types: status int


decompiled_code:
__int64 __fastcall hw_set_pixel(__int16 a1)
{
return drawRect(screen, 6 * (unsigned int)(unsigned __int8)a1, 6 * (unsigned int)HIBYTE(a1), 6u, 6u, 0x99u);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
return func1(screen, 6 * (unsigned int)(unsigned __int8)a1, 6 * (unsigned int)HIBYTE(a1), 6u, 6u, 0x99u);
}

original_code:
static void hw_set_pixel(point_t point){
 drawRect(screen, point.x*6, point.y*6, 6, 6, (0462 >> 1));
}

canonical_original_code:
static void hw_set_pixel(struct point_t point){
 drawRect(screen, point.x*6, point.y*6, 6, 6, (0462 >> 1));
}

variable_types: struct point_t { unsigned char x; unsigned char y; }


decompiled_code:
__int64 hw_init()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax

if ( (unsigned int)SDL_Init(32LL) )
{
v2 = (const char *)SDL_GetError();
printf("Error in init video: %s\n", v2);
exit(1);
}
screen = SDL_SetVideoMode(768LL, 384LL, 8LL, 0LL);
if ( !screen )
{
v1 = (const char *)SDL_GetError();
printf("Error in getting surface: %s\n", v1);
exit(1);
}
if ( (unsigned int)SDL_EnableKeyRepeat(1LL, 1LL) )
printf("Error in enabling key repeat");
result = SDL_GetTicks();
ticks_last_draw = result;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax

if ( (unsigned int)SDL_Init(32LL) )
{
v2 = (const char *)SDL_GetError();
printf("Error in init video: %s\n", v2);
exit(1);
}
screen = SDL_SetVideoMode(768LL, 384LL, 8LL, 0LL);
if ( !screen )
{
v1 = (const char *)SDL_GetError();
printf("Error in getting surface: %s\n", v1);
exit(1);
}
if ( (unsigned int)SDL_EnableKeyRepeat(1LL, 1LL) )
printf("Error in enabling key repeat");
result = SDL_GetTicks();
ticks_last_draw = result;
return result;
}

original_code:
void hw_init(){
 if(SDL_Init(0x00000020)){
  printf("Error in init video: %s\n", SDL_GetError());
  exit(1);
 }
 screen = SDL_SetVideoMode(128*6, 64*6, 8, 0x00000000);
 if(screen == ((void *)0)){
  printf("Error in getting surface: %s\n", SDL_GetError());
  exit(1);
 }
 if(SDL_EnableKeyRepeat(1, 1)){
  printf("Error in enabling key repeat");
 }
 ticks_last_draw = SDL_GetTicks();
}


decompiled_code:
__int64 hw_draw()
{
unsigned __int8 *v0; // rbp
unsigned __int8 *v1; // rbx
int v2; // eax

drawRect(screen, 0, 0, 0x300u, 0x180u, 0);
v0 = (unsigned __int8 *)draw_next_point();
hw_set_pixel(*(_WORD *)v0);
if ( print_0 )
printf("Point: {%i, %i}\n", *v0, v0[1]);
v1 = (unsigned __int8 *)draw_next_point();
while ( *v1 != *v0 || v1[1] != v0[1] )
{
hw_set_pixel(*(_WORD *)v1);
v1 = (unsigned __int8 *)draw_next_point();
if ( print_0 )
printf("Point: {%i, %i}\n", *v1, v1[1]);
}
print_0 = 0;
SDL_UpdateRect(screen, 0LL, 0LL, 0LL, 0LL);
v2 = SDL_GetTicks();
return SDL_Delay((unsigned int)(50 * ((v2 - ticks_last_draw) / 50) - (v2 - ticks_last_draw) + 50));
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int8 *v0; // rbp
unsigned __int8 *v1; // rbx
int v2; // eax

func1(screen, 0, 0, 0x300u, 0x180u, 0);
v0 = (unsigned __int8 *)draw_next_point();
func2(*(_WORD *)v0);
if ( print_0 )
printf("Point: {%i, %i}\n", *v0, v0[1]);
v1 = (unsigned __int8 *)draw_next_point();
while ( *v1 != *v0 || v1[1] != v0[1] )
{
func2(*(_WORD *)v1);
v1 = (unsigned __int8 *)draw_next_point();
if ( print_0 )
printf("Point: {%i, %i}\n", *v1, v1[1]);
}
print_0 = 0;
SDL_UpdateRect(screen, 0LL, 0LL, 0LL, 0LL);
v2 = SDL_GetTicks();
return SDL_Delay((unsigned int)(50 * ((v2 - ticks_last_draw) / 50) - (v2 - ticks_last_draw) + 50));
}

original_code:
void hw_draw(){
 static int print = 0;
 drawRect(screen, 0,0,128*6,64*6,0);
 point_t *first_point = draw_next_point();
 hw_set_pixel(*first_point);
 if(print) printf("Point: {%i, %i}\n", first_point->x, first_point->y);
 point_t *current_point = draw_next_point();
 while(!(current_point->x == first_point->x && current_point->y == first_point->y)){
  hw_set_pixel(*current_point);
  current_point = draw_next_point();
  if(print) printf("Point: {%i, %i}\n", current_point->x, current_point->y);
 }
 print = 0;
 SDL_UpdateRect(screen, 0, 0, 0, 0);
 int current_ticks = SDL_GetTicks();
 int delta = (current_ticks - ticks_last_draw) % (1000/20);
 int delay = (1000/20) - delta;
 SDL_Delay(delay);
}

canonical_original_code:
void hw_draw(){
 static int print = 0;
 drawRect(screen, 0,0,128*6,64*6,0);
 struct point_t *first_point = draw_next_point();
 hw_set_pixel(*first_point);
 if(print) printf("Point: {%i, %i}\n", first_point->x, first_point->y);
 struct point_t *current_point = draw_next_point();
 while(!(current_point->x == first_point->x && current_point->y == first_point->y)){
  hw_set_pixel(*current_point);
  current_point = draw_next_point();
  if(print) printf("Point: {%i, %i}\n", current_point->x, current_point->y);
 }
 print = 0;
 SDL_UpdateRect(screen, 0, 0, 0, 0);
 int current_ticks = SDL_GetTicks();
 int delta = (current_ticks - ticks_last_draw) % (1000/20);
 int delay = (1000/20) - delta;
 SDL_Delay(delay);
}

variable_types: print int

variable_types: struct point_t { unsigned char x; unsigned char y; } *

variable_types: delta int

variable_types: delay int


decompiled_code:
// attributes: thunk
__int64 hw_deinit()
{
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func7()
{
return SDL_Quit();
}

original_code:
void hw_deinit(){
 SDL_Quit();
}

canonical_original_code:
void hw_deinit(){
 SDL_Quit();
}


repo: anomit/loki

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( geteuid() )
{
puts("You need to be uid 0");
exit(1);
}
check(argc, argv);
exec();
return 0;
}

original_code:
int main()
{
    if(geteuid())
    {
        printf("You need to be uid 0\n");
        exit(1);
    }
    check();
    exec();
    return 0;
}


canonical_original_code:
int calculate_key(unsigned char *box, unsigned char *iv, unsigned char key[16])
{
 unsigned char ctx[16];
 unsigned short ctrl1 = 0;
 unsigned short ctrl2 = 0;
 unsigned int x1 = 0;
 unsigned int x2 = 0;
 unsigned int s[16];
 unsigned int t[8];
 unsigned int x[16];
 unsigned int p[4];
 unsigned int n[4];
 unsigned int state_matrix[16];
 unsigned int *ku = (const uint32_t *)box;
 int round;
 int i; int j;
 memset(key, 0, 16);
 memset(ctx, 0, 16);
 for (i = 0; i < 16; ++i) {
  ctx[i] = T1[i * 256 + iv[i]] ^ box[i];
 }
 for (round = 0; round < 5; ++round, ku += 8) {
  for (i = 0; i < 8; ++i) {
   t[i] = swap32(ku[i]);
  }
   for (i = 0; i < 16; ++i) {
    s[i] = swap32(B[ctx[i] + i * 256 + round * 0x2000]);
   }
  for (i = 0; i < 16; ++i) {
   unsigned short ctrl = phase1_ctrl[i];
   if ((ctrl & (1 << 11)) == (1 << 11)) {
    x[i] = t[(((ctrl) >> 0) & 0x1F)];
   } else if ((ctrl & (1 << 10)) == (1 << 10)) {
    x[i] = s[(((ctrl) >> 0) & 0x1F)] ^ s[(((ctrl) >> 5) & 0x1F)];
   } else {
    x[i] = s[(((ctrl) >> 0) & 0x1F)];
   }
  }
  for (i = 0; i < 4; ++i) {
   ((void) sizeof (((phase2_ctrl1[i] & (1 << 10)) == (1 << 10)) ? 1 : 0), __extension__ ({ if ((phase2_ctrl1[i] & (1 << 10)) == (1 << 10)) ; else __assert_fail ("(phase2_ctrl1[i] & RCTRL_X_BIT) == RCTRL_X_BIT", "crypt.c", 120, __extension__ __PRETTY_FUNCTION__); }));
   ((void) sizeof (((phase2_ctrl2[i] & (1 << 10)) == (1 << 10)) ? 1 : 0), __extension__ ({ if ((phase2_ctrl2[i] & (1 << 10)) == (1 << 10)) ; else __assert_fail ("(phase2_ctrl2[i] & RCTRL_X_BIT) == RCTRL_X_BIT", "crypt.c", 121, __extension__ __PRETTY_FUNCTION__); }));
   ctrl1 = phase2_ctrl1[i];
   ctrl2 = phase2_ctrl2[i];
   x1 = s[(((ctrl1) >> 0) & 0x1F)] ^ x[(((ctrl1) >> 5) & 0x1F)];
   x2 = s[(((ctrl2) >> 0) & 0x1F)] ^ t[(((ctrl2) >> 5) & 0x1F)];
   p[i] = x1 ^ x2;
  }
  for (i = 0; i < 4; ++i) {
   n[i] = swap32(ku[i + 8]);
  }
  if (round < 5 - 1) {
    for (i = 0; i < 4; ++i) {
     for (j = 0; j < 4; ++j) {
      state_matrix[i * 4 + j] = swap32(B[(((p[i]) >> 8 * (4 - (j) - 1)) & 0xFF) + i * 1024 + j * 256 + ((round * 2 + 1) << 12)]);
     }
    }
   for (i = 0; i < 4; ++i) {
    unsigned int x1 = state_matrix[((i + 0) & 3) * 4 + ((0) & 3)];
    unsigned int x2 = state_matrix[((i + 1) & 3) * 4 + ((1) & 3)];
    unsigned int x3 = state_matrix[((i + 2) & 3) * 4 + ((2) & 3)];
    unsigned int x4 = state_matrix[((i + 3) & 3) * 4 + ((3) & 3)];
    unsigned int xxx = x1 ^ x2 ^ x3 ^ x4 ^ n[i];
    for (j = 0; j < 4; ++j) {
     ctx[i * 4 + j] = (((xxx) >> 8 * (4 - (j) - 1)) & 0xFF);
    }
   }
  } else {
   for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
     ctx[i * 4 + j] = (((p[i]) >> 8 * (4 - (j) - 1)) & 0xFF);
    }
   }
  }
 }
 for (i = 0; i < 4; ++i) {
  unsigned int a = swap32(B[ctx[FT[i * 4 + 0]] + FT[i * 4 + 0] * 256 + 0x9000]) & 0xFF000000;
  unsigned int b = swap32(B[ctx[FT[i * 4 + 1]] + FT[i * 4 + 1] * 256 + 0x9000]) & 0xFF0000;
  unsigned int c = swap32(B[ctx[FT[i * 4 + 2]] + FT[i * 4 + 2] * 256 + 0x9000]) & 0xFF00;
  unsigned int d = swap32(B[ctx[FT[i * 4 + 3]] + FT[i * 4 + 3] * 256 + 0x9000]) & 0xFF;
  unsigned int f = n[i] ^ a ^ b ^ c ^ d;
   for (j = 0; j < 4; ++j) {
    key[((i + 1) * 4 + j) & 15] = T2[(((f) >> 8 * (4 - (j) - 1)) & 0xFF) + i * 1024 + j * 256];
   }
 }
 return 0;
}

variable_types:
box
unsigned char *

variable_types:
iv
unsigned char *

variable_types:
key
unsigned char [16]

variable_types:
ctx
unsigned char [16]

variable_types:
s
unsigned int [16]

variable_types:
t
unsigned int [8]

variable_types:
x
unsigned int [16]

variable_types:
p
unsigned int [4]

variable_types:
n
unsigned int [4]

variable_types:
ku
unsigned int *

variable_types: round int

variable_types:
ctrl
unsigned short

variable_types:
xxx
unsigned int

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types:
c
unsigned int

variable_types:
d
unsigned int

variable_types:
f
unsigned int


original_code:
int sfc_process_data(sfc_context_t *ctx, const uint8_t *in, uint8_t *out, uint32_t size) {
 sfc_context_prv_t *real_ctx = ((void *)0);
 uint8_t final_key[16] = {0};
 uint32_t num_blocks = 0;
 uint32_t i, j = 0;
 if (ctx == ((void *)0) || in == ((void *)0) || out == ((void *)0))
  return (-1);
 real_ctx = (sfc_context_prv_t *)ctx;
 num_blocks = (uint32_t)(size / 16);
 for (i = 0; i < num_blocks; ++i) {
  calculate_key(real_ctx->erk, real_ctx->riv, final_key);
  for (j = 0; j < 16; ++j)
   out[j] = in[j] ^ final_key[j];
  for (j = (16 - 1); j > 0; --j) {
   real_ctx->riv[j]++;
   if (real_ctx->riv[j])
    break;
  }
  in += 16;
  out += 16;
 }
 return ( 0);
}

canonical_original_code:
int sfc_process_data(struct sfc_context_t *ctx, unsigned char *in, unsigned char *out, unsigned int size) {
 struct sfc_context_prv_t *real_ctx = ((void *)0);
 unsigned char final_key[16] = {0};
 unsigned int num_blocks = 0;
 unsigned int i; unsigned int j = 0;
 if (ctx == ((void *)0) || in == ((void *)0) || out == ((void *)0))
  return (-1);
 real_ctx = (struct sfc_context_prv_t *)ctx;
 num_blocks = (unsigned int)(size / 16);
 for (i = 0; i < num_blocks; ++i) {
  calculate_key(real_ctx->erk, real_ctx->riv, final_key);
  for (j = 0; j < 16; ++j)
   out[j] = in[j] ^ final_key[j];
  for (j = (16 - 1); j > 0; --j) {
   real_ctx->riv[j]++;
   if (real_ctx->riv[j])
    break;
  }
  in += 16;
  out += 16;
 }
 return ( 0);
}

variable_types: struct sfc_context_t { void *unused; } *

variable_types:
in
unsigned char *

variable_types:
out
unsigned char *

variable_types:
size
unsigned int

variable_types: struct sfc_context_prv_t { unsigned char erk[-1]; unsigned char riv[16]; } *

variable_types:
i
unsigned int

variable_types:
j
unsigned int


decompiled_code:
void __fastcall sfc_destroy_context(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void sfc_destroy_context(sfc_context_t *ctx) {
 sfc_context_prv_t *real_ctx;
 if (ctx != ((void *)0)) {
  real_ctx = (sfc_context_prv_t *)ctx;
  free(real_ctx);
 }
}

canonical_original_code:
void sfc_destroy_context(struct sfc_context_t *ctx) {
 struct sfc_context_prv_t *real_ctx;
 if (ctx != ((void *)0)) {
  real_ctx = (struct sfc_context_prv_t *)ctx;
  free(real_ctx);
 }
}

user_defined_types: struct sfc_context_prv_t { unsigned char erk[-1]; unsigned char riv[16]; }


repo: anre/lielas

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
const char *v4; // rbp
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx
__int64 v9; // rdx
__int64 v10; // rdx

if ( argc <= 1 )
{
print_help(argc, argv, envp);
exit(0);
}
v3 = argv[1];
if ( *(_WORD *)v3 == 25959 && v3[2] == 116 )
{
write_log("get network settings", 2LL, envp);
get_settings();
return 0;
}
if ( strncmp(v3, "set", 3uLL) )
{
if ( strncmp(v3, "restore", 7uLL) )
{
print_help(v3, "restore", v9);
return 0;
}
write_log("restoring network settings", 2LL, v9);
if ( !(unsigned int)restore_backup() )
{
restart_net_interface();
return 0;
}
write_log("failed to restore network settings", 0LL, v10);
LABEL_22:
exit(-1);
}
v4 = argv[2];
if ( !strncmp(v4, "dhcp", 4uLL) )
{
write_log("set network settings dhcp", 2LL, v5);
set_dhcp();
restart_net_interface();
return 0;
}
if ( !strncmp(v4, "static", 6uLL) )
{
write_log(&unk_4022B6, 2LL, v6);
set_static(argv[3], argv[4], argv[5]);
restart_net_interface();
return 0;
}
if ( strncmp(v4, "dns", 3uLL) )
goto LABEL_22;
write_log("set dns server", 2LL, v7);
if ( argc == 4 )
{
set_dns(argv[3], 0LL);
}
else if ( argc == 5 )
{
set_dns(argv[3], argv[4]);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
const char *v4; // rbp
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx
__int64 v9; // rdx
__int64 v10; // rdx

if ( argc <= 1 )
{
func8(argc, argv, envp);
exit(0);
}
v3 = argv[1];
if ( *(_WORD *)v3 == 25959 && v3[2] == 116 )
{
func7("get network settings", 2LL, envp);
func10();
return 0;
}
if ( strncmp(v3, "set", 3uLL) )
{
if ( strncmp(v3, "restore", 7uLL) )
{
func8(v3, "restore", v9);
return 0;
}
func7("restoring network settings", 2LL, v9);
if ( !(unsigned int)func15() )
{
func9();
return 0;
}
func7("failed to restore network settings", 0LL, v10);
LABEL_22:
exit(-1);
}
v4 = argv[2];
if ( !strncmp(v4, "dhcp", 4uLL) )
{
func7("set network settings dhcp", 2LL, v5);
func13();
func9();
return 0;
}
if ( !strncmp(v4, "static", 6uLL) )
{
func7(&unk_4022B6, 2LL, v6);
func14(argv[3], argv[4], argv[5]);
func9();
return 0;
}
if ( strncmp(v4, "dns", 3uLL) )
goto LABEL_22;
func7("set dns server", 2LL, v7);
if ( argc == 4 )
{
func11(argv[3], 0LL);
}
else if ( argc == 5 )
{
func11(argv[3], argv[4]);
}
return 0;
}

original_code:
int main(int argc, char *argv[]){
  if(argc < 2){
    print_help();
    exit(0);
  }
  if(!strncmp(argv[1], "get", 3)){
    write_log("get network settings", 2);
    get_settings();
  }else if(!strncmp(argv[1], "set", 3)){
    if(!strncmp(argv[2], "dhcp", 4)){
      write_log("set network settings dhcp", 2);
      set_dhcp();
      restart_net_interface();
    }else if(!strncmp(argv[2], "static", 6)){
      write_log("set network settings static", 2);
      set_static(argv[3], argv[4], argv[5]);
      restart_net_interface();
    }else if(!strncmp(argv[2], "dns", 3)){
      write_log("set dns server", 2);
      if( argc == 4 ){
        set_dns(argv[3], ((void *)0));
      }else if(argc == 5){
        set_dns(argv[3], argv[4]);
      }
    }else{
      exit(-1);
    }
  }else if(!strncmp(argv[1], "restore", 7)){
    write_log("restoring network settings", 2);
    if(restore_backup()){
      write_log("failed to restore network settings", 0);
      exit(-1);
    }
    restart_net_interface();
  }else{
    print_help();
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
int print_help()
{
printf("Version: %s\n", ver);
puts("Usage:");
puts("ipchanger get");
puts("...returns current ip");
putchar(10);
puts("ipchanger set dhcp");
puts("...sets eth0 to dhcp");
putchar(10);
puts("ipchanger set static <address> <netmask> <gateway>");
puts("...sets eth0 to static ip");
putchar(10);
puts("ipchanger set dns <dns1> <dns2>");
puts("...sets dns1 and if given dns2");
putchar(10);
puts("ipchanger restore");
puts("...restores settings from interfaces.old");
return putchar(10);
}

canonical_decompiled_code:
int func8()
{
printf("Version: %s\n", ver);
puts("Usage:");
puts("ipchanger get");
puts("...returns current ip");
putchar(10);
puts("ipchanger set dhcp");
puts("...sets eth0 to dhcp");
putchar(10);
puts("ipchanger set static <address> <netmask> <gateway>");
puts("...sets eth0 to static ip");
putchar(10);
puts("ipchanger set dns <dns1> <dns2>");
puts("...sets dns1 and if given dns2");
putchar(10);
puts("ipchanger restore");
puts("...restores settings from interfaces.old");
return putchar(10);
}

original_code:
void print_help(){
  printf("Version: %s\n", ver);
  printf("Usage:\n");
  printf("ipchanger get\n");
  printf("...returns current ip\n");
  printf("\n");
  printf("ipchanger set dhcp\n");
  printf("...sets eth0 to dhcp\n");
  printf("\n");
  printf("ipchanger set static <address> <netmask> <gateway>\n");
  printf("...sets eth0 to static ip\n");
  printf("\n");
  printf("ipchanger set dns <dns1> <dns2>\n");
  printf("...sets dns1 and if given dns2\n");
  printf("\n");
  printf("ipchanger restore\n");
  printf("...restores settings from interfaces.old\n");
  printf("\n");
}


decompiled_code:
unsigned int restart_net_interface()
{
write_log("resetting eth0", 2);
sleep(2u);
if ( system("/sbin/ifdown eth0") )
{
write_log("failed to shut down eth0", 0);
exit(-1);
}
sleep(5u);
if ( system("/sbin/ifup eth0") )
{
write_log("failed to bring eth0 up", 0);
exit(-1);
}
return sleep(2u);
}

canonical_decompiled_code:
unsigned int func9()
{
func7("resetting eth0", 2);
sleep(2u);
if ( system("/sbin/ifdown eth0") )
{
func7("failed to shut down eth0", 0);
exit(-1);
}
sleep(5u);
if ( system("/sbin/ifup eth0") )
{
func7("failed to bring eth0 up", 0);
exit(-1);
}
return sleep(2u);
}

original_code:
void restart_net_interface(){
    write_log("resetting eth0", 2);
    sleep(2);
    if(system("/sbin/ifdown eth0")){
      write_log("failed to shut down eth0", 0);
      exit(-1);
    }
    sleep(5);
    if(system("/sbin/ifup eth0")){
      write_log("failed to bring eth0 up", 0);
      exit(-1);
    }
    sleep(2);
}


decompiled_code:
int save_backup()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s %s.bak", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

canonical_decompiled_code:
int func12()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s %s.bak", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

original_code:
int save_backup(){
  char cmd[500];
  sprintf(cmd, "cp %s %s.bak", "/etc/network/interfaces", "/etc/network/interfaces");
  return system(cmd);
}

variable_types:
cmd
char [500]


decompiled_code:
int restore_backup()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s.bak %s", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

canonical_decompiled_code:
int func15()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s.bak %s", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

original_code:
int restore_backup(){
  char cmd[500];
  sprintf(cmd, "cp %s.bak %s", "/etc/network/interfaces", "/etc/network/interfaces");
  return system(cmd);
 }


decompiled_code:
void __fastcall lwp_init_resource(__int64 a1)
{
*(_BYTE *)a1 = 0;
*(_DWORD *)(a1 + 100) = 0;
}

original_code:
void lwp_init_resource(lwp_resource *res){
  res->name[0] = 0;
  res->attributes = 0;
}

canonical_original_code:
void lwp_init_resource(struct lwp_resource_struct *res){
  res->name[0] = 0;
  res->attributes = 0;
}

variable_types: struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } *

user_defined_types: struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct attr[10]; }


decompiled_code:
void __fastcall lwp_init_wkc(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax

lwp_init_resource(a1);
lwp_init_resource(a1 + 1104);
lwp_init_resource(v1 + 2208);
lwp_init_resource(v2 + 3312);
lwp_init_resource(v3 + 4416);
lwp_init_resource(v4 + 5520);
*(_DWORD *)(v5 + 28704) = 0;
}

original_code:
void lwp_init_wkc(lwp_wkc *wkc){
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  wkc->channels = 0;
}

canonical_original_code:
void lwp_init_wkc(struct lwp_wkc_struct *wkc){
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  wkc->channels = 0;
}

variable_types: struct lwp_wkc_struct { struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; } *

user_defined_types: struct lwp_wkc_struct { struct lwp_resource_struct info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; }


decompiled_code:
__int64 __fastcall lwp_get_res_by_name(const char *a1, __int64 a2)
{
__int64 result; // rax

result = a2;
if ( !memcmp(a1, "channel", 7uLL) )
{
result += 1104LL * (*(_DWORD *)(result + 28704) + 1) + 6624;
}
else if ( !strcmp(a1, "database") )
{
result += 4416LL;
}
else if ( !strcmp(a1, "device") )
{
result += 1104LL;
}
else if ( strcmp(a1, "info") )
{
if ( !strcmp(a1, "logger") )
{
result += 5520LL;
}
else if ( !strcmp(a1, "modul") )
{
result += 2208LL;
}
else
{
result += 3312LL;
if ( strcmp(a1, "network") )
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, __int64 a2)
{
__int64 result; // rax

result = a2;
if ( !memcmp(a1, "channel", 7uLL) )
{
result += 1104LL * (*(_DWORD *)(result + 28704) + 1) + 6624;
}
else if ( !strcmp(a1, "database") )
{
result += 4416LL;
}
else if ( !strcmp(a1, "device") )
{
result += 1104LL;
}
else if ( strcmp(a1, "info") )
{
if ( !strcmp(a1, "logger") )
{
result += 5520LL;
}
else if ( !strcmp(a1, "modul") )
{
result += 2208LL;
}
else
{
result += 3312LL;
if ( strcmp(a1, "network") )
result = 0LL;
}
}
return result;
}

original_code:
lwp_resource *lwp_get_res_by_name( char* str, lwp_wkc *wkc){
  lwp_resource *res = 0;
  if(!strncmp(str, "channel", strlen("channel"))){
    res = &wkc->channel[wkc->channels+1];
  }else if(!strcmp(str, "database")){
    res = &wkc->database;
  }else if(!strcmp(str, "device")){
    res = &wkc->device;
  }else if(!strcmp(str, "info")){
    res = &wkc->info;
  }else if(!strcmp(str, "logger")){
    res = &wkc->logger;
  }else if(!strcmp(str, "modul")){
    res = &wkc->modul;
  }else if(!strcmp(str, "network")){
    res = &wkc->network;
  }
  return res;
}

canonical_original_code:
struct lwp_resource_struct *lwp_get_res_by_name(char *str, struct lwp_wkc_struct *wkc){
  struct lwp_resource_struct *res = 0;
  if(!strncmp(str, "channel", strlen("channel"))){
    res = &wkc->channel[wkc->channels+1];
  }else if(!strcmp(str, "database")){
    res = &wkc->database;
  }else if(!strcmp(str, "device")){
    res = &wkc->device;
  }else if(!strcmp(str, "info")){
    res = &wkc->info;
  }else if(!strcmp(str, "logger")){
    res = &wkc->logger;
  }else if(!strcmp(str, "modul")){
    res = &wkc->modul;
  }else if(!strcmp(str, "network")){
    res = &wkc->network;
  }
  return res;
}

variable_types:
str
char *

variable_types: struct lwp_wkc_struct { struct lwp_resource_struct info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; } *

return_type: struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } *


decompiled_code:
__int64 __fastcall lwp_add_attr(char *src, __int64 a2)
{
__int64 v2; // rax
__int64 result; // rax

v2 = *(int *)(a2 + 100);
*(_DWORD *)(a2 + 100) = v2 + 1;
if ( (int)v2 + 1 > 9 )
{
*(_DWORD *)(a2 + 100) = v2;
result = 0xFFFFFFFFLL;
}
else
{
strncpy((char *)(a2 + 100 * v2 + 104), src, 0x64uLL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src, __int64 a2)
{
__int64 v2; // rax
__int64 result; // rax

v2 = *(int *)(a2 + 100);
*(_DWORD *)(a2 + 100) = v2 + 1;
if ( (int)v2 + 1 > 9 )
{
*(_DWORD *)(a2 + 100) = v2;
result = 0xFFFFFFFFLL;
}
else
{
strncpy((char *)(a2 + 100 * v2 + 104), src, 0x64uLL);
result = 0LL;
}
return result;
}

original_code:
int lwp_add_attr(char* name, lwp_resource *res){
  res->attributes += 1;
  if(res->attributes >= 10){
    res->attributes -= 1;
    return -1;
  }
  strncpy(res->attr[res->attributes -1].name, name, 100);
  return 0;
}

canonical_original_code:
int lwp_add_attr(char *name, struct lwp_resource_struct *res){
  res->attributes += 1;
  if(res->attributes >= 10){
    res->attributes -= 1;
    return -1;
  }
  strncpy(res->attr[res->attributes -1].name, name, 100);
  return 0;
}

variable_types:
name
char *

user_defined_types: struct lwp_attr_struct { char name[100]; }


decompiled_code:
__int64 __fastcall lwp_parse_wkc(_BYTE *a1, __int64 a2)
{
__int64 v3; // rax
__int64 v4; // rcx
__int64 v5; // rdx
int v6; // ebx
char *v7; // r13
int v8; // eax
__int64 v9; // rcx
char *v10; // rdx
int v11; // er12
char v12; // al
__int64 result; // rax
__int64 v14; // rax
char v15; // cl
__int64 v16; // rsi
__int64 v17; // rax
char *v18; // r14
int v19; // eax
char *v20; // rdi
char *v21; // rax
int v22; // er15
__int64 v23; // rax
__int64 v24; // rdi
int v25; // ebx
char v26; // dl
int v27; // [rsp+0h] [rbp-508h] BYREF
__int16 v28; // [rsp+4h] [rbp-504h]
char v29; // [rsp+6h] [rbp-502h]
char src[112]; // [rsp+70h] [rbp-498h] BYREF
char s[1064]; // [rsp+E0h] [rbp-428h] BYREF

if ( !*a1 )
{
lielas_log("error parsing wkc, empty wkc string", 1LL);
return 0xFFFFFFFFLL;
}
*(_DWORD *)(a2 + 28704) = 0;
lwp_init_resource(a2);
lwp_init_resource(a2 + 1104);
lwp_init_resource(a2 + 2208);
lwp_init_resource(a2 + 3312);
lwp_init_resource(a2 + 4416);
lwp_init_resource(a2 + 5520);
do
lwp_init_resource(v3);
while ( v3 != v5 );
v6 = 0;
v7 = strchr((const char *)(v4 + 1), 60);
v8 = strlen(v7);
v9 = 0LL;
v10 = v7;
v11 = v8;
v12 = *v7;
if ( *v7 == 60 )
goto LABEL_9;
while ( 1 )
{
do
{
do
{
LABEL_5:
if ( !v12 || v6 > v11 )
return 0LL;
v9 = ++v6;
v10 = &v7[v6];
v12 = *v10;
}
while ( *v10 != 60 );
LABEL_9:
;
}
while ( v7[v9 + 1] != 47 );
v6 += 2;
v14 = 0LL;
while ( 1 )
{
v16 = (int)v14;
if ( v11 <= v6 + (int)v14 )
break;
v15 = v10[v14 + 2];
if ( v15 == 62 )
break;
*((_BYTE *)&v27 + v14++) = v15;
if ( v14 == 100 )
{
v16 = 100LL;
break;
}
}
*((_BYTE *)&v27 + v16) = 0;
v17 = lwp_get_res_by_name((const char *)&v27, a2);
v18 = (char *)v17;
if ( v27 == 1851877475 && v28 == 25966 && v29 == 108 )
break;
if ( v17 )
goto LABEL_21;
LABEL_16:
snprintf(s, 0x3E8uLL, "unknown resource parsing .well-known/core: %s", (const char *)&v27);
lielas_log(s, 1LL);
v12 = v7[v6];
}
v19 = *(_DWORD *)(a2 + 28704) + 1;
if ( v19 > 19 )
goto LABEL_16;
*(_DWORD *)(a2 + 28704) = v19;
if ( !v18 )
goto LABEL_16;
LABEL_21:
strncpy(v18, (const char *)&v27, 0x64uLL);
v20 = strstr(&v7[v6], "title");
if ( v20 )
{
v21 = strchr(v20, 34);
if ( v21 )
{
v22 = (_DWORD)v21 - (_DWORD)v7 + 1;
while ( 2 )
{
v23 = 0LL;
while ( 1 )
{
v24 = (int)v23;
v25 = v23 + 1;
if ( v22 + (int)v23 >= v11 )
break;
v26 = v7[v22 + v23];
if ( v26 == 32 )
break;
if ( v26 == 10 || v26 == 34 || v26 == 124 )
{
src[(int)v23] = 0;
v6 = v22 + v25;
if ( !src[0] || !(unsigned int)lwp_add_attr(src, (__int64)v18) )
{
v12 = v7[v6];
goto LABEL_5;
}
goto LABEL_36;
}
src[v23++] = v26;
if ( v23 == 100 )
{
v25 = 101;
v24 = 100LL;
break;
}
}
src[v24] = 0;
v22 += v25;
if ( !src[0] || !(unsigned int)lwp_add_attr(src, (__int64)v18) )
continue;
break;
}
LABEL_36:
lielas_log("error parsing wkc, can't add attribute", 1LL);
result = 0xFFFFFFFFLL;
}
else
{
lielas_log("error parsing wkc, title value not found", 1LL);
result = 0xFFFFFFFFLL;
}
}
else
{
lielas_log("error parsing wkc, title token not found", 1LL);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int lwp_parse_wkc(char *str, lwp_wkc *wkc){
  char resname[100];
  char attrname[100];
  char *title;
  char log[1000];
  int pos = 0;
  int eof = 0;
  int eoa = 0;
  int len;
  int i;
  lwp_resource *res;
  if(str[0] == 0){
    lielas_log((unsigned char*)"error parsing wkc, empty wkc string", 1);
    return -1;
  }
  wkc->channels = 0;
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  for(i = 0; i < 20; i++){
    lwp_init_resource(&wkc->channel[i]);
  }
  str = strchr(&str[1], '<');
  len = strlen(str);
  while(!eof){
    if(str[pos] == '<' && str[pos+1] == '/'){
      pos += 2;
      for(i = 0; i < 100 && ((pos+i) < len); i++){
        if(str[pos+i] == '>'){
          break;
        }
        resname[i] = str[pos+i];
      }
      resname[i] = 0;
      res = lwp_get_res_by_name(resname, wkc);
      if(strncmp(resname, "channel", strlen("channel")) == 0){
        wkc->channels += 1;
        if( wkc->channels >= 20){
          wkc->channels -= 1;
          res = 0;
        }
      }
      if(res == 0){
        snprintf(log, 1000, "unknown resource parsing .well-known/core: %s", resname);
        lielas_log((unsigned char*)log, 1);
      }else{
        strncpy(res->name, resname, 100);
        title = strstr(&str[pos], "title");
        if(title == ((void *)0)){
          lielas_log((unsigned char*)"error parsing wkc, title token not found", 1);
          return -1;
        }
        title = strchr(title, '"');
        if(title == ((void *)0)){
          lielas_log((unsigned char*)"error parsing wkc, title value not found", 1);
          return -1;
        }
        pos = title - str + 1;
        eoa = 0;
        i = 0;
        while(!eoa){
          for(i = 0; i < 100 && ((pos+i) < len); i++){
            if( str[pos+i] == ' '){
              break;
            }else if(str[pos+i] == '"' || str[pos+i] == ' ' || str[pos+i] == '\n' || str[pos+i] == '|'){
              eoa = 1;
              break;
            }
            attrname[i] = str[pos+i];
          }
          attrname[i] = 0;
          pos += i + 1;
          if( strlen(attrname) > 0){
            if(lwp_add_attr(attrname, res) != 0){
              lielas_log((unsigned char*)"error parsing wkc, can't add attribute", 1);
              return -1;
            }
          }
        }
      }
    }
    if(str[pos] == 0 || pos > len){
      eof = 1;
    }
    pos += 1;
  }
  return 0;
}

canonical_original_code:
int lwp_parse_wkc(char *str, struct lwp_wkc_struct *wkc){
  char resname[100];
  char attrname[100];
  char *title;
  char log[1000];
  int pos = 0;
  int eof = 0;
  int eoa = 0;
  int len;
  int i;
  struct lwp_resource_struct *res;
  if(str[0] == 0){
    lielas_log((unsigned char *)"error parsing wkc, empty wkc string", 1);
    return -1;
  }
  wkc->channels = 0;
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  for(i = 0; i < 20; i++){
    lwp_init_resource(&wkc->channel[i]);
  }
  str = strchr(&str[1], '<');
  len = strlen(str);
  while(!eof){
    if(str[pos] == '<' && str[pos+1] == '/'){
      pos += 2;
      for(i = 0; i < 100 && ((pos+i) < len); i++){
        if(str[pos+i] == '>'){
          break;
        }
        resname[i] = str[pos+i];
      }
      resname[i] = 0;
      res = lwp_get_res_by_name(resname, wkc);
      if(strncmp(resname, "channel", strlen("channel")) == 0){
        wkc->channels += 1;
        if( wkc->channels >= 20){
          wkc->channels -= 1;
          res = 0;
        }
      }
      if(res == 0){
        snprintf(log, 1000, "unknown resource parsing .well-known/core: %s", resname);
        lielas_log((unsigned char *)log, 1);
      }else{
        strncpy(res->name, resname, 100);
        title = strstr(&str[pos], "title");
        if(title == ((void *)0)){
          lielas_log((unsigned char *)"error parsing wkc, title token not found", 1);
          return -1;
        }
        title = strchr(title, '"');
        if(title == ((void *)0)){
          lielas_log((unsigned char *)"error parsing wkc, title value not found", 1);
          return -1;
        }
        pos = title - str + 1;
        eoa = 0;
        i = 0;
        while(!eoa){
          for(i = 0; i < 100 && ((pos+i) < len); i++){
            if( str[pos+i] == ' '){
              break;
            }else if(str[pos+i] == '"' || str[pos+i] == ' ' || str[pos+i] == '\n' || str[pos+i] == '|'){
              eoa = 1;
              break;
            }
            attrname[i] = str[pos+i];
          }
          attrname[i] = 0;
          pos += i + 1;
          if( strlen(attrname) > 0){
            if(lwp_add_attr(attrname, res) != 0){
              lielas_log((unsigned char *)"error parsing wkc, can't add attribute", 1);
              return -1;
            }
          }
        }
      }
    }
    if(str[pos] == 0 || pos > len){
      eof = 1;
    }
    pos += 1;
  }
  return 0;
}

variable_types:
resname
char [100]

variable_types:
attrname
char [100]

variable_types:
title
char *

variable_types:
log
char [1000]

variable_types: pos int

variable_types: eof int

variable_types: eoa int

variable_types: len int

variable_types: i int


original_code:
void lwp_print_wkc(lwp_wkc *wkc, char* str){
  int i, j;
  int pos = 0;
  lwp_resource *res;
  pos += snprintf(&str[pos], 2000 - pos, ".well-known/core:\n");
  res = &wkc->info;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->device;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->modul;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->network;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->database;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  for(j = 1; j <= wkc->channels && j < 20; j++){
    res = &wkc->channel[j];
    pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
    for(i = 0; i < res->attributes; i++){
      pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
    }
  }
  res = &wkc->logger;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
}

canonical_original_code:
void lwp_print_wkc(struct lwp_wkc_struct *wkc, char *str){
  int i; int j;
  int pos = 0;
  struct lwp_resource_struct *res;
  pos += snprintf(&str[pos], 2000 - pos, ".well-known/core:\n");
  res = &wkc->info;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->device;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->modul;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->network;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->database;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  for(j = 1; j <= wkc->channels && j < 20; j++){
    res = &wkc->channel[j];
    pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
    for(i = 0; i < res->attributes; i++){
      pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
    }
  }
  res = &wkc->logger;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
}

variable_types: j int


original_code:
int lwp_get_attr_value(char *str, lwp_resource *res, char *attr_name, char *val, int size){
  int rank;
  int pos =0 ;
  int i;
  for(rank=0; rank < res->attributes && rank < 10; rank++){
    if(strcmp(res->attr[rank].name, attr_name)== 0){
      break;
    }
  }
  if(rank >= 10 || rank >= res->attributes){
    return -1;
  }
  while(rank > 0){
    if(str[pos] == '\t' || str[pos] == '\n' || str[pos] == 0){
      rank -= 1;
    }
    pos += 1;
    if(pos >= size){
      return -1;
    }
  }
  for( i = 0; pos < size; i++){
    if(str[pos+i] == '\t' || str[pos+i] == '\n' || str[pos+i] == 0){
      break;
    }
    val[i] = str[pos + i];
  }
  if((pos+i) >= size){
    val[0] = 0;
    return -1;
  }
  val[i] = 0;
  return 0;
}

canonical_original_code:
int lwp_get_attr_value(char *str, struct lwp_resource_struct *res, char *attr_name, char *val, int size){
  int rank;
  int pos = 0;
  int i;
  for(rank=0; rank < res->attributes && rank < 10; rank++){
    if(strcmp(res->attr[rank].name, attr_name)== 0){
      break;
    }
  }
  if(rank >= 10 || rank >= res->attributes){
    return -1;
  }
  while(rank > 0){
    if(str[pos] == '\t' || str[pos] == '\n' || str[pos] == 0){
      rank -= 1;
    }
    pos += 1;
    if(pos >= size){
      return -1;
    }
  }
  for( i = 0; pos < size; i++){
    if(str[pos+i] == '\t' || str[pos+i] == '\n' || str[pos+i] == 0){
      break;
    }
    val[i] = str[pos + i];
  }
  if((pos+i) >= size){
    val[0] = 0;
    return -1;
  }
  val[i] = 0;
  return 0;
}

variable_types:
val
char *

variable_types: size int

variable_types: rank int


variable_types:
cdt
unsigned char *

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 rtc_get_state()
{
unsigned int v0; // er12

pthread_mutex_lock(&rtcmutex);
v0 = rtc_state;
pthread_mutex_unlock(&rtcmutex);
return v0;
}

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

pthread_mutex_lock(&rtcmutex);
v0 = rtc_state;
pthread_mutex_unlock(&rtcmutex);
return v0;
}

original_code:
int rtc_get_state(){
  int state;
  pthread_mutex_lock(&rtcmutex);
  state = rtc_state;
  pthread_mutex_unlock(&rtcmutex);
  return state;
}

variable_types: state int


decompiled_code:
char *rtc_get_state_text()
{
__int64 v0; // rbx
char *result; // rax

pthread_mutex_lock(&rtcmutex);
v0 = (unsigned int)rtc_state;
pthread_mutex_unlock(&rtcmutex);
result = 0LL;
if ( (unsigned int)v0 <= 2 )
result = CSWTCH_2[v0];
return result;
}

canonical_decompiled_code:
char *func1()
{
__int64 v0; // rbx
char *result; // rax

pthread_mutex_lock(&rtcmutex);
v0 = (unsigned int)rtc_state;
pthread_mutex_unlock(&rtcmutex);
result = 0LL;
if ( (unsigned int)v0 <= 2 )
result = CSWTCH_2[v0];
return result;
}

original_code:
const char *rtc_get_state_text(){
  int state;
  pthread_mutex_lock(&rtcmutex);
  state = rtc_state;
  pthread_mutex_unlock(&rtcmutex);
  switch(state){
    case 1:
      return rtc_state_text_uninitialized;
    case 0:
      return rtc_state_text_ok;
    case 2:
      return rtc_state_text_not_synced;
  }
  return ((void *)0);
}

return_type: char *


testdt

decompiled_code:
__int64 __fastcall testdt(_DWORD *a1)
{
__int64 result; // rax

if ( a1[2] > 0x17u || a1[1] > 0x3Bu || *a1 > 0x3Bu || (unsigned int)(a1[5] - 100) > 0x64 || a1[4] > 0xBu )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-((unsigned int)(a1[3] - 1) > 0x1E);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
__int64 result; // rax

if ( a1[2] > 0x17u || a1[1] > 0x3Bu || *a1 > 0x3Bu || (unsigned int)(a1[5] - 100) > 0x64 || a1[4] > 0xBu )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-((unsigned int)(a1[3] - 1) > 0x1E);
return result;
}

original_code:
int testdt(struct tm *dt){
 if(dt->tm_hour < 0 || dt->tm_hour > 23)
  return -1;
 if(dt->tm_min < 0 || dt->tm_min > 59)
  return -1;
 if(dt->tm_sec < 0 || dt->tm_sec > 59)
  return -1;
 if(dt->tm_year < 100 || dt->tm_year > 200)
  return -1;
 if(dt->tm_mon < 0 || dt->tm_mon > 11)
  return -1;
 if(dt->tm_mday < 1 || dt->tm_mday > 31)
  return -1;
 return 0;
}

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 rtc_init()
{
struct tm *v0; // rax
__int64 result; // rax
pthread_mutexattr_t attr; // [rsp+4h] [rbp-C4h] BYREF
time_t timer; // [rsp+8h] [rbp-C0h] BYREF
int v4[16]; // [rsp+10h] [rbp-B8h] BYREF
char s[120]; // [rsp+50h] [rbp-78h] BYREF

rtc_state = 1;
attr.__align = 0;
pthread_mutexattr_settype(&attr, 1);
if ( pthread_mutex_init(&rtcmutex, &attr) )
{
lielas_log("Couldn't initialize mutex for rtc", 0LL);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)rtc4162_init() )
return 0xFFFFFFFFLL;
if ( !(unsigned int)rtc4162_get(v4) && !(unsigned int)testdt(v4) )
{
lielas_log("rtc: rtc time valid, setting system time", 2LL);
snprintf(
s,
0x64uLL,
"sudo date --set %.4i-%.2i-%.2i",
(unsigned int)(v4[5] + 1900),
(unsigned int)(v4[4] + 1),
(unsigned int)v4[3]);
if ( !system(s) )
{
snprintf(
s,
0x64uLL,
"sudo date --set %.2i:%.2i:%.2i",
(unsigned int)v4[2],
(unsigned int)v4[1],
(unsigned int)v4[0]);
if ( !system(s) )
{
lielas_log("rtc: system time set", 2LL);
result = 0LL;
rtc_state = 0;
return result;
}
}
return 0xFFFFFFFFLL;
}
lielas_log("rtc: rtc-module time not valid, check if system time is synced", 2LL);
if ( (unsigned int)ntp_gettimex(s) )
{
lielas_log("rtc: error executing ntp_gettime()", 1LL);
goto LABEL_16;
}
if ( *_errno_location() == 5 )
{
LABEL_16:
lielas_log("rtc: rtc-module time not valid and ntp not synced", 1LL);
rtc_state = 2;
return 0xFFFFFFFFLL;
}
lielas_log("rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2LL);
time(&timer);
v0 = gmtime(&timer);
rtc4162_set(v0);
sleep(2u);
if ( (unsigned int)rtc4162_init() || (unsigned int)rtc4162_get(v4) )
return 0xFFFFFFFFLL;
return (unsigned int)-((unsigned int)testdt(v4) != 0);
}

canonical_decompiled_code:
__int64 func3()
{
struct tm *v0; // rax
__int64 result; // rax
pthread_mutexattr_t attr; // [rsp+4h] [rbp-C4h] BYREF
time_t timer; // [rsp+8h] [rbp-C0h] BYREF
int v4[16]; // [rsp+10h] [rbp-B8h] BYREF
char s[120]; // [rsp+50h] [rbp-78h] BYREF

rtc_state = 1;
attr.__align = 0;
pthread_mutexattr_settype(&attr, 1);
if ( pthread_mutex_init(&rtcmutex, &attr) )
{
lielas_log("Couldn't initialize mutex for rtc", 0LL);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)rtc4162_init() )
return 0xFFFFFFFFLL;
if ( !(unsigned int)rtc4162_get(v4) && !(unsigned int)func2(v4) )
{
lielas_log("rtc: rtc time valid, setting system time", 2LL);
snprintf(
s,
0x64uLL,
"sudo date --set %.4i-%.2i-%.2i",
(unsigned int)(v4[5] + 1900),
(unsigned int)(v4[4] + 1),
(unsigned int)v4[3]);
if ( !system(s) )
{
snprintf(
s,
0x64uLL,
"sudo date --set %.2i:%.2i:%.2i",
(unsigned int)v4[2],
(unsigned int)v4[1],
(unsigned int)v4[0]);
if ( !system(s) )
{
lielas_log("rtc: system time set", 2LL);
result = 0LL;
rtc_state = 0;
return result;
}
}
return 0xFFFFFFFFLL;
}
lielas_log("rtc: rtc-module time not valid, check if system time is synced", 2LL);
if ( (unsigned int)ntp_gettimex(s) )
{
lielas_log("rtc: error executing ntp_gettime()", 1LL);
goto LABEL_16;
}
if ( *_errno_location() == 5 )
{
LABEL_16:
lielas_log("rtc: rtc-module time not valid and ntp not synced", 1LL);
rtc_state = 2;
return 0xFFFFFFFFLL;
}
lielas_log("rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2LL);
time(&timer);
v0 = gmtime(&timer);
rtc4162_set(v0);
sleep(2u);
if ( (unsigned int)rtc4162_init() || (unsigned int)rtc4162_get(v4) )
return 0xFFFFFFFFLL;
return (unsigned int)-((unsigned int)func2(v4) != 0);
}

original_code:
int rtc_init(){
  struct tm dt;
  struct ntptimeval ntv;
 time_t rawtime;
 struct tm *now;
  int timenotvalid = 0;
  int ntpnotsynced = 0;
  pthread_mutexattr_t mutexAttr = {};
  char cmd[100];
  rtc_state = 1;
  pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  if(pthread_mutex_init(&rtcmutex, &mutexAttr) != 0){
  lielas_log((unsigned char*)"Couldn't initialize mutex for rtc", 0);
  return -1;
  }
  if(rtc4162_init()){
    return -1;;
  }
  timenotvalid = rtc4162_get(&dt);
  if(!timenotvalid){
    timenotvalid = testdt(&dt);
  }
  if(timenotvalid){
    lielas_log((unsigned char*)"rtc: rtc-module time not valid, check if system time is synced", 2);
    if(ntp_gettime(&ntv)){
      lielas_log((unsigned char*)"rtc: error executing ntp_gettime()", 1);
      ntpnotsynced = 1;
    }
    if(ntpnotsynced || (*__errno_location ()) == 5){
      lielas_log((unsigned char*)"rtc: rtc-module time not valid and ntp not synced", 1);
      rtc_state = 2;
      return -1;;
    }else{
      lielas_log((unsigned char*)"rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2);
      time(&rawtime);
      now = gmtime(&rawtime);
      rtc4162_set(now);
      sleep(2);
      if(rtc4162_init()){
        return -1;;
      }
      timenotvalid = rtc4162_get(&dt);
      if(!timenotvalid){
        timenotvalid = testdt(&dt);
      }
      if(timenotvalid){
        return -1;;
      }
      return 0;
    }
    return -1;;
  }
  lielas_log((unsigned char*)"rtc: rtc time valid, setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt.tm_year+1900), (dt.tm_mon+1), dt.tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt.tm_hour, dt.tm_min, dt.tm_sec);
  if(system(cmd)){
    return -1;;
  }
  lielas_log((unsigned char*)"rtc: system time set", 2);
  rtc_state = 0;
  return 0;
}

canonical_original_code:
int rtc_init(){
  struct tm dt;
  struct ntptimeval ntv;
 long rawtime;
 struct tm *now;
  int timenotvalid = 0;
  int ntpnotsynced = 0;
  union pthread_mutexattr_t mutexAttr = {};
  char cmd[100];
  rtc_state = 1;
  pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  if(pthread_mutex_init(&rtcmutex, &mutexAttr) != 0){
  lielas_log((unsigned char *)"Couldn't initialize mutex for rtc", 0);
  return -1;
  }
  if(rtc4162_init()){
    return -1;;
  }
  timenotvalid = rtc4162_get(&dt);
  if(!timenotvalid){
    timenotvalid = testdt(&dt);
  }
  if(timenotvalid){
    lielas_log((unsigned char *)"rtc: rtc-module time not valid, check if system time is synced", 2);
    if(ntp_gettime(&ntv)){
      lielas_log((unsigned char *)"rtc: error executing ntp_gettime()", 1);
      ntpnotsynced = 1;
    }
    if(ntpnotsynced || (*__errno_location ()) == 5){
      lielas_log((unsigned char *)"rtc: rtc-module time not valid and ntp not synced", 1);
      rtc_state = 2;
      return -1;;
    }else{
      lielas_log((unsigned char *)"rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2);
      time(&rawtime);
      now = gmtime(&rawtime);
      rtc4162_set(now);
      sleep(2);
      if(rtc4162_init()){
        return -1;;
      }
      timenotvalid = rtc4162_get(&dt);
      if(!timenotvalid){
        timenotvalid = testdt(&dt);
      }
      if(timenotvalid){
        return -1;;
      }
      return 0;
    }
    return -1;;
  }
  lielas_log((unsigned char *)"rtc: rtc time valid, setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt.tm_year+1900), (dt.tm_mon+1), dt.tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt.tm_hour, dt.tm_min, dt.tm_sec);
  if(system(cmd)){
    return -1;;
  }
  lielas_log((unsigned char *)"rtc: system time set", 2);
  rtc_state = 0;
  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; }

variable_types: struct ntptimeval { struct timeval { long tv_sec; long tv_usec; } time; long maxerror; long esterror; long tai; long __glibc_reserved1; long __glibc_reserved2; long __glibc_reserved3; long __glibc_reserved4; }

variable_types: rawtime long

variable_types: timenotvalid int

variable_types: ntpnotsynced int

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }

variable_types:
cmd
char [100]

user_defined_types: struct ntptimeval { struct timeval time; long maxerror; long esterror; long tai; long __glibc_reserved1; long __glibc_reserved2; long __glibc_reserved3; long __glibc_reserved4; }


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

pthread_mutex_lock(&rtcmutex);
lielas_log("rtc: setting rtc time", 2LL);
rtc4162_set(a1);
v1 = rtc4162_get(a1);
if ( !v1 )
v1 = testdt(a1);
lielas_log("rtc: setting system time", 2LL);
snprintf(v3, 0x64uLL, "sudo date --set %.4i-%.2i-%.2i", a1[5] + 1900, a1[4] + 1, a1[3]);
if ( system(v3) )
return (unsigned int)-1;
snprintf(v3, 0x64uLL, "sudo date --set %.2i:%.2i:%.2i", a1[2], a1[1], *a1);
if ( system(v3) )
return (unsigned int)-1;
rtc_state = 0;
pthread_mutex_unlock(&rtcmutex);
return v1;
}

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

pthread_mutex_lock(&rtcmutex);
lielas_log("rtc: setting rtc time", 2LL);
rtc4162_set(a1);
v1 = rtc4162_get(a1);
if ( !v1 )
v1 = func2(a1);
lielas_log("rtc: setting system time", 2LL);
snprintf(v3, 0x64uLL, "sudo date --set %.4i-%.2i-%.2i", a1[5] + 1900, a1[4] + 1, a1[3]);
if ( system(v3) )
return (unsigned int)-1;
snprintf(v3, 0x64uLL, "sudo date --set %.2i:%.2i:%.2i", a1[2], a1[1], *a1);
if ( system(v3) )
return (unsigned int)-1;
rtc_state = 0;
pthread_mutex_unlock(&rtcmutex);
return v1;
}

original_code:
int rtc_set_dt(struct tm *dt){
  int timenotvalid = 0;
  char cmd[100];
  pthread_mutex_lock(&rtcmutex);
  lielas_log((unsigned char*)"rtc: setting rtc time", 2);
  rtc4162_set(dt);
  timenotvalid = rtc4162_get(dt);
  if(!timenotvalid){
    timenotvalid = testdt(dt);
  }
  lielas_log((unsigned char*)"rtc: setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt->tm_year+1900), (dt->tm_mon+1), dt->tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt->tm_hour, dt->tm_min, dt->tm_sec);
  if(system(cmd)){
    return -1;;
  }
  rtc_state = 0;
  pthread_mutex_unlock(&rtcmutex);
  return timenotvalid;
}

canonical_original_code:
int rtc_set_dt(struct tm *dt){
  int timenotvalid = 0;
  char cmd[100];
  pthread_mutex_lock(&rtcmutex);
  lielas_log((unsigned char *)"rtc: setting rtc time", 2);
  rtc4162_set(dt);
  timenotvalid = rtc4162_get(dt);
  if(!timenotvalid){
    timenotvalid = testdt(dt);
  }
  lielas_log((unsigned char *)"rtc: setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt->tm_year+1900), (dt->tm_mon+1), dt->tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt->tm_hour, dt->tm_min, dt->tm_sec);
  if(system(cmd)){
    return -1;;
  }
  rtc_state = 0;
  pthread_mutex_unlock(&rtcmutex);
  return timenotvalid;
}


LcreateChannel

decompiled_code:
_BYTE *LcreateChannel()
{
_BYTE *result; // rax

result = malloc(0x108uLL);
*(_DWORD *)result = 0;
result[4] = 0;
result[54] = 0;
result[154] = 0;
result[204] = 0;
*((_QWORD *)result + 32) = 0LL;
return result;
}

canonical_decompiled_code:
_BYTE *func0()
{
_BYTE *result; // rax

result = malloc(0x108uLL);
*(_DWORD *)result = 0;
result[4] = 0;
result[54] = 0;
result[154] = 0;
result[204] = 0;
*((_QWORD *)result + 32) = 0LL;
return result;
}

original_code:
Lchannel *LcreateChannel(){
 Lchannel *channel = malloc(sizeof(Lchannel));
 channel->id = 0;
 channel->address[0] = 0;
 channel->type[0] = 0;
 channel->unit[0] = 0;
  channel->class[0] = 0;
  channel->exponent = 0.0;
 return channel;
}

canonical_original_code:
struct Lchannel_struct *LcreateChannel(){
 struct Lchannel_struct *channel = malloc(sizeof(Lchannel));
 channel->id = 0;
 channel->address[0] = 0;
 channel->type[0] = 0;
 channel->unit[0] = 0;
  channel->class[0] = 0;
  channel->exponent = 0.0;
 return channel;
}

variable_types: struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *


LdeleteChannel

decompiled_code:
// attributes: thunk
void LdeleteChannel(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
void LdeleteChannel(Lchannel *c){
 free(c);
}

canonical_original_code:
void LdeleteChannel(struct Lchannel_struct *c){
 free(c);
}

user_defined_types: struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; }


LcreateDevice

original_code:
Ldevice *LcreateDevice(){
 Ldevice *d = malloc(sizeof(Ldevice));
 if(d == ((void *)0)){
  lielas_log((unsigned char*)"Can't allocate memory for new device", 0);
  return ((void *)0);
 }
 d->id = 0;
 d->registered = 0;
 d->address[0] = 0;
  d->mac[0] = 0;
 d->mint[0] = 0;
 d->pint[0] = 0;
 d->aint[0] = 0;
 d->moduls = 0;
 d->modul[0] = 0;
  d->name[0] = 0;
  d->sw_ver[0] = 0;
  d->supply[0] = 0;
 d->event = 0;
  lwp_init_wkc(&d->wkc);
 return d;
}

canonical_original_code:
struct Ldevice_struct *LcreateDevice(){
 struct Ldevice_struct *d = malloc(sizeof(Ldevice));
 if(d == ((void *)0)){
  lielas_log((unsigned char *)"Can't allocate memory for new device", 0);
  return ((void *)0);
 }
 d->id = 0;
 d->registered = 0;
 d->address[0] = 0;
  d->mac[0] = 0;
 d->mint[0] = 0;
 d->pint[0] = 0;
 d->aint[0] = 0;
 d->moduls = 0;
 d->modul[0] = 0;
  d->name[0] = 0;
  d->sw_ver[0] = 0;
  d->supply[0] = 0;
 d->event = 0;
  lwp_init_wkc(&d->wkc);
 return d;
}

variable_types: struct Ldevice_struct { unsigned int id; int registered; char address[50]; char mac[50]; char mint[50]; char pint[50]; char aint[50]; char name[64]; char sw_ver[64]; char supply[64]; int moduls; struct Lmodul_struct { unsigned int id; char address[50]; int channels; char mint[50]; char pint[50]; char aint[50]; struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *channel[20]; } *modul[20]; 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; } lastProcessed; int hasLogger; int event; struct lwp_wkc_struct { struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; } wkc; } *

user_defined_types: struct Ldevice_struct { unsigned int id; int registered; char address[50]; char mac[50]; char mint[50]; char pint[50]; char aint[50]; char name[64]; char sw_ver[64]; char supply[64]; int moduls; struct Lmodul_struct *modul[20]; struct tm lastProcessed; int hasLogger; int event; struct lwp_wkc_struct wkc; }

user_defined_types: struct Lmodul_struct { unsigned int id; char address[50]; int channels; char mint[50]; char pint[50]; char aint[50]; struct Lchannel_struct *channel[20]; }


LdeleteDevice

decompiled_code:
void __fastcall LdeleteDevice(void *ptr)
{
__int64 v2; // rbx
__int64 v3; // rdi

if ( *((int *)ptr + 113) > 0 )
{
v2 = 0LL;
do
{
v3 = *((_QWORD *)ptr + v2++ + 57);
LdeleteModul(v3);
}
while ( *((_DWORD *)ptr + 113) > (int)v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v2; // rbx
__int64 v3; // rdi

if ( *((int *)ptr + 113) > 0 )
{
v2 = 0LL;
do
{
v3 = *((_QWORD *)ptr + v2++ + 57);
LdeleteModul(v3);
}
while ( *((_DWORD *)ptr + 113) > (int)v2 );
}
free(ptr);
}

original_code:
void LdeleteDevice(Ldevice *d){
 int i;
 for(i = 0; i < d->moduls; i++){
  LdeleteModul(d->modul[i]);
 }
 free(d);
 d = ((void *)0);
}

canonical_original_code:
void LdeleteDevice(struct Ldevice_struct *d){
 int i;
 for(i = 0; i < d->moduls; i++){
  LdeleteModul(d->modul[i]);
 }
 free(d);
 d = ((void *)0);
}

variable_types: i int


LaddModul

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

result = *(unsigned int *)(a1 + 452);
if ( (_DWORD)result == 20 )
return lielas_log("Can't add modul, maximum number of moduls reached", 1LL);
*(_QWORD *)(a1 + 8LL * ((int)result + 1) + 456) = a2;
*(_DWORD *)(a1 + 452) = result + 1;
if ( (int)result + 1 <= 20 )
{
result = (int)result + 2;
*(_QWORD *)(a1 + 8 * result + 456) = 0LL;
}
return result;
}

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

result = *(unsigned int *)(a1 + 452);
if ( (_DWORD)result == 20 )
return lielas_log("Can't add modul, maximum number of moduls reached", 1LL);
*(_QWORD *)(a1 + 8LL * ((int)result + 1) + 456) = a2;
*(_DWORD *)(a1 + 452) = result + 1;
if ( (int)result + 1 <= 20 )
{
result = (int)result + 2;
*(_QWORD *)(a1 + 8 * result + 456) = 0LL;
}
return result;
}

original_code:
void LaddModul(Ldevice *device, Lmodul *modul){
 if(device->moduls == 20){
  lielas_log((unsigned char*)"Can't add modul, maximum number of moduls reached", 1);
  return;
 }
 device->modul[device->moduls+1] = modul;
  device->moduls+=1;
 if(device->moduls <= 20){
  device->modul[device->moduls+1] = ((void *)0);
 }
}

canonical_original_code:
void LaddModul(struct Ldevice_struct *device, struct Lmodul_struct *modul){
 if(device->moduls == 20){
  lielas_log((unsigned char *)"Can't add modul, maximum number of moduls reached", 1);
  return;
 }
 device->modul[device->moduls+1] = modul;
  device->moduls+=1;
 if(device->moduls <= 20){
  device->modul[device->moduls+1] = ((void *)0);
 }
}

variable_types:
modul
struct Lmodul_struct *