decompiled_code:
int __fastcall breadth_dump(_DWORD *a1)
{
int v1; // er12
unsigned int *v2; // rbp
int v3; // ebx
double v4; // xmm0_8

v1 = *a1;
if ( (int)*a1 > 0 )
{
v2 = a1 + 1;
v3 = 1;
do
{
printf("%d ", *v2);
v4 = log2((double)++v3);
if ( fmod(v4, 1.0) == 0.0 )
putchar(10);
++v2;
}
while ( v3 <= v1 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(_DWORD *a1)
{
int v1; // er12
unsigned int *v2; // rbp
int v3; // ebx
double v4; // xmm0_8

v1 = *a1;
if ( (int)*a1 > 0 )
{
v2 = a1 + 1;
v3 = 1;
do
{
printf("%d ", *v2);
v4 = log2((double)++v3);
if ( fmod(v4, 1.0) == 0.0 )
putchar(10);
++v2;
}
while ( v3 <= v1 );
}
return putchar(10);
}

original_code:
void breadth_dump(int *pHeap) {
 int i;
 int SIZE = pHeap[0];
 for(i=1;i<=SIZE;i++) {
  printf("%d  ",pHeap[i]);
  if (fmod(log2(i+1),1.0f)==0.0f) {
   printf("\n");
  }
 }
 printf("\n");
}

variable_types:
pHeap
int *

variable_types: SIZE int


repo: netskink/heap

original_code:
int * max_heapify(int *pHeap, int i) {
 int iLeft;
 int iRight;
 int iLargest;
 int iTemp;
 int SIZE = pHeap[0];
 iLeft = left(i);
 iRight = right(i);
 if ( (iLeft <= SIZE) && (pHeap[iLeft] > pHeap[i]) ) {
  iLargest = iLeft;
 } else {
  iLargest = i;
 }
 if ( (iRight <= SIZE) && (pHeap[iRight] > pHeap[iLargest]) ) {
  iLargest = iRight;
 }
 if (iLargest != i) {
  iTemp = pHeap[i];
  pHeap[i] = pHeap[iLargest];
  pHeap[iLargest] = iTemp;
  pHeap = max_heapify(pHeap, iLargest);
 }
 return pHeap;
}

canonical_original_code:
int *max_heapify(int *pHeap, int i) {
 int iLeft;
 int iRight;
 int iLargest;
 int iTemp;
 int SIZE = pHeap[0];
 iLeft = left(i);
 iRight = right(i);
 if ( (iLeft <= SIZE) && (pHeap[iLeft] > pHeap[i]) ) {
  iLargest = iLeft;
 } else {
  iLargest = i;
 }
 if ( (iRight <= SIZE) && (pHeap[iRight] > pHeap[iLargest]) ) {
  iLargest = iRight;
 }
 if (iLargest != i) {
  iTemp = pHeap[i];
  pHeap[i] = pHeap[iLargest];
  pHeap[iLargest] = iTemp;
  pHeap = max_heapify(pHeap, iLargest);
 }
 return pHeap;
}

variable_types: i int

variable_types: iLeft int

variable_types: iRight int

variable_types: iLargest int

variable_types: iTemp int

return_type: int *


decompiled_code:
int *__fastcall build_max_heap(int *a1)
{
int v1; // ebx

v1 = *a1 / 2;
if ( *a1 > 1 )
{
do
a1 = (int *)max_heapify(a1, v1--);
while ( v1 > 0 );
}
return a1;
}

canonical_decompiled_code:
int *__fastcall func5(int *a1)
{
int v1; // ebx

v1 = *a1 / 2;
if ( *a1 > 1 )
{
do
a1 = (int *)func4(a1, v1--);
while ( v1 > 0 );
}
return a1;
}

original_code:
int * build_max_heap(int *pHeap) {
 int i;
 int SIZE = pHeap[0];
 for(i=(SIZE/2); i>0; i--) {
  pHeap = max_heapify(pHeap,i);
 }
 return pHeap;
}

canonical_original_code:
int *build_max_heap(int *pHeap) {
 int i;
 int SIZE = pHeap[0];
 for(i=(SIZE/2); i>0; i--) {
  pHeap = max_heapify(pHeap,i);
 }
 return pHeap;
}


original_code:
int * min_heapify(int *pHeap, int i) {
 int iLeft;
 int iRight;
 int iSmallest;
 int iTemp;
 int SIZE = pHeap[0];
 iLeft = left(i);
 iRight = right(i);
 if ( (iLeft <= SIZE) && (pHeap[iLeft] < pHeap[i]) ) {
  iSmallest = iLeft;
 } else {
  iSmallest = i;
 }
 if ( (iRight <= SIZE) && (pHeap[iRight] < pHeap[iSmallest]) ) {
  iSmallest = iRight;
 }
 if (iSmallest != i) {
  iTemp = pHeap[i];
  pHeap[i] = pHeap[iSmallest];
  pHeap[iSmallest] = iTemp;
  pHeap = min_heapify(pHeap, iSmallest);
 }
 return pHeap;
}

canonical_original_code:
int *min_heapify(int *pHeap, int i) {
 int iLeft;
 int iRight;
 int iSmallest;
 int iTemp;
 int SIZE = pHeap[0];
 iLeft = left(i);
 iRight = right(i);
 if ( (iLeft <= SIZE) && (pHeap[iLeft] < pHeap[i]) ) {
  iSmallest = iLeft;
 } else {
  iSmallest = i;
 }
 if ( (iRight <= SIZE) && (pHeap[iRight] < pHeap[iSmallest]) ) {
  iSmallest = iRight;
 }
 if (iSmallest != i) {
  iTemp = pHeap[i];
  pHeap[i] = pHeap[iSmallest];
  pHeap[iSmallest] = iTemp;
  pHeap = min_heapify(pHeap, iSmallest);
 }
 return pHeap;
}

variable_types: iSmallest int


decompiled_code:
int *__fastcall build_min_heap(int *a1)
{
int v1; // ebx

v1 = *a1 / 2;
if ( *a1 > 1 )
{
do
a1 = (int *)min_heapify(a1, v1--);
while ( v1 > 0 );
}
return a1;
}

canonical_decompiled_code:
int *__fastcall func7(int *a1)
{
int v1; // ebx

v1 = *a1 / 2;
if ( *a1 > 1 )
{
do
a1 = (int *)func6(a1, v1--);
while ( v1 > 0 );
}
return a1;
}

original_code:
int * build_min_heap(int *pHeap) {
 int i;
 int SIZE = pHeap[0];
 for(i=(SIZE/2); i>0; i--) {
  pHeap = min_heapify(pHeap,i);
 }
 return pHeap;
}

canonical_original_code:
int *build_min_heap(int *pHeap) {
 int i;
 int SIZE = pHeap[0];
 for(i=(SIZE/2); i>0; i--) {
  pHeap = min_heapify(pHeap,i);
 }
 return pHeap;
}


repo: netskink/tiles

decompiled_code:
__int64 __fastcall find_name(char *a1, __int64 a2)
{
char v3; // di
unsigned int v4; // ebp
char v7; // si

v3 = *a1;
v4 = 0;
if ( v3 )
{
v4 = 1;
if ( !(unsigned int)exists1((unsigned int)v3) || (push(a2, (unsigned int)*a1), (v4 = find_name(a1 + 1, a2)) != 0) )
{
v7 = a1[1];
if ( !v7 )
{
pop(a2);
return 1;
}
}
else
{
v7 = a1[1];
if ( !v7 )
return v4;
}
if ( (unsigned int)exists2((unsigned int)*a1, (unsigned int)v7) )
{
push(a2, (unsigned int)*a1);
push(a2, (unsigned int)(char)(a1[1] + 32));
v4 = find_name(a1 + 2, a2);
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, __int64 a2)
{
char v3; // di
unsigned int v4; // ebp
char v7; // si

v3 = *a1;
v4 = 0;
if ( v3 )
{
v4 = 1;
if ( !(unsigned int)func14((unsigned int)v3) || (func12(a2, (unsigned int)*a1), (v4 = func7(a1 + 1, a2)) != 0) )
{
v7 = a1[1];
if ( !v7 )
{
func10(a2);
return 1;
}
}
else
{
v7 = a1[1];
if ( !v7 )
return v4;
}
if ( (unsigned int)func15((unsigned int)*a1, (unsigned int)v7) )
{
func12(a2, (unsigned int)*a1);
func12(a2, (unsigned int)(char)(a1[1] + 32));
v4 = func7(a1 + 2, a2);
}
}
return v4;
}

original_code:
int find_name(char *pchName,stack_t *pResult) {
 int iRC = 1;
 if (0 == pchName[0]) {
  return (0);
 }
 if ( exists1(pchName[0]) ) {
  push(pResult,pchName[0]);
  iRC = find_name(((char *)&pchName[1]),pResult);
 }
 if (iRC == 0 && (0 == pchName[1])) {
  return iRC;
 }
 if (0 == pchName[1]) {
  pop(pResult);
  return 1;
 }
 if (exists2(pchName[0],pchName[1] )) {
  push(pResult,pchName[0]);
  push(pResult,pchName[1]+32);
  iRC = find_name(((char *)&pchName[2]),pResult);
 }
 return iRC;
}

canonical_original_code:
int find_name(char *pchName, struct stack *pResult) {
 int iRC = 1;
 if (0 == pchName[0]) {
  return (0);
 }
 if ( exists1(pchName[0]) ) {
  push(pResult,pchName[0]);
  iRC = find_name(((char *)&pchName[1]),pResult);
 }
 if (iRC == 0 && (0 == pchName[1])) {
  return iRC;
 }
 if (0 == pchName[1]) {
  pop(pResult);
  return 1;
 }
 if (exists2(pchName[0],pchName[1] )) {
  push(pResult,pchName[0]);
  push(pResult,pchName[1]+32);
  iRC = find_name(((char *)&pchName[2]),pResult);
 }
 return iRC;
}

variable_types:
pchName
char *

variable_types: struct stack { unsigned long size; char items[10000]; } *

variable_types: iRC int

return_type: int


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

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

original_code:
int stack_not_empty(stack_t *pStack) {
    return (pStack->size);
}

canonical_original_code:
int stack_not_empty(struct stack *pStack) {
    return (pStack->size);
}

user_defined_types: struct stack { unsigned long size; char items[10000]; }


pop

decompiled_code:
__int64 __fastcall pop(__int64 *a1)
{
__int64 v1; // rax

if ( !*a1 )
{
fwrite("Error: stack underflow!!!\n", 1uLL, 0x1AuLL, stderr);
abort();
}
v1 = *a1 - 1;
*a1 = v1;
return *((unsigned __int8 *)a1 + v1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 *a1)
{
__int64 v1; // rax

if ( !*a1 )
{
fwrite("Error: stack underflow!!!\n", 1uLL, 0x1AuLL, stderr);
abort();
}
v1 = *a1 - 1;
*a1 = v1;
return *((unsigned __int8 *)a1 + v1 + 8);
}

original_code:
char pop(stack_t *pStack) {
    if (pStack->size == 0){
        fputs("Error: stack underflow!!!\n", stderr);
        abort();
    } else {
        return pStack->items[--pStack->size];
 }
}

canonical_original_code:
char pop(struct stack *pStack) {
    if (pStack->size == 0){
        fputs("Error: stack underflow!!!\n", stderr);
        abort();
    } else {
        return pStack->items[--pStack->size];
 }
}

return_type: char


decompiled_code:
void __fastcall dump_stack(__int64 *a1)
{
char v1; // al

while ( *a1 )
{
v1 = pop(a1);
printf("%d %c\n", *(unsigned int *)a1, (unsigned int)v1);
}
}

canonical_decompiled_code:
void __fastcall func11(__int64 *a1)
{
char v1; // al

while ( *a1 )
{
v1 = func10(a1);
printf("%d %c\n", *(unsigned int *)a1, (unsigned int)v1);
}
}

original_code:
void dump_stack(stack_t *pStack) {
 while(pStack->size != 0) {
  printf("%d %c\n",(int)pStack->size,pop(pStack));
 }
}

canonical_original_code:
void dump_stack(struct stack *pStack) {
 while(pStack->size != 0) {
  printf("%d %c\n",(int)pStack->size,pop(pStack));
 }
}

return_type: void


push

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

result = *a1;
if ( *a1 == 10000 )
{
fwrite("Error: stack overflow!!!\n", 1uLL, 0x19uLL, stderr);
dump_stack(a1);
abort();
}
*a1 = result + 1;
*((_BYTE *)a1 + result + 8) = a2;
return result;
}

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

result = *a1;
if ( *a1 == 10000 )
{
fwrite("Error: stack overflow!!!\n", 1uLL, 0x19uLL, stderr);
func11(a1);
abort();
}
*a1 = result + 1;
*((_BYTE *)a1 + result + 8) = a2;
return result;
}

original_code:
void push(stack_t *pStack, char chBuffer) {
    if (pStack->size == 10000) {
        fputs("Error: stack overflow!!!\n", stderr);
  dump_stack(pStack);
        abort();
    } else {
  pStack->items[pStack->size++]=chBuffer;
 }
}

canonical_original_code:
void push(struct stack *pStack, char chBuffer) {
    if (pStack->size == 10000) {
        fputs("Error: stack overflow!!!\n", stderr);
  dump_stack(pStack);
        abort();
    } else {
  pStack->items[pStack->size++]=chBuffer;
 }
}

variable_types: chBuffer char


decompiled_code:
__int64 __fastcall exists1(char a1)
{
return (unsigned int)tiles1[a1 - 65];
}

canonical_decompiled_code:
__int64 __fastcall func14(char a1)
{
return (unsigned int)tiles1[a1 - 65];
}

original_code:
int exists1(char chLetter) {
 return tiles1[chLetter-'A'];
}

variable_types: chLetter char


decompiled_code:
__int64 __fastcall exists2(char a1, char a2)
{
return (unsigned int)tiles2[26 * a1 - 1755 + a2];
}

canonical_decompiled_code:
__int64 __fastcall func15(char a1, char a2)
{
return (unsigned int)tiles2[26 * a1 - 1755 + a2];
}

original_code:
int exists2(char chLetter1,char chLetter2) {
 return tiles2[chLetter1-'A'][chLetter2-'A'];
}

canonical_original_code:
int exists2(char chLetter1, char chLetter2) {
 return tiles2[chLetter1-'A'][chLetter2-'A'];
}


decompiled_code:
__int64 test1()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("BOB", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func16()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("BOB", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test1() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("BOB",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test1() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("BOB",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

variable_types: struct stack { unsigned long size; char items[10000]; }


decompiled_code:
__int64 test2()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("HUNYH", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func17()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("HUNYH", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test2() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("HUNYH",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test2() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("HUNYH",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test3()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("SUNNY", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func18()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("SUNNY", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test3() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("SUNNY",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test3() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("SUNNY",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test4()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("PWWN", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func19()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("PWWN", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test4() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("PWWN",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test4() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("PWWN",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test5()
{
int v0; // edx
__int64 result; // rax
_QWORD v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = find_name("JOHN", (__int64)v2);
result = 1LL;
if ( v0 == 1 )
{
empty_stack(v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func20()
{
int v0; // edx
__int64 result; // rax
_QWORD v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = func7("JOHN", (__int64)v2);
result = 1LL;
if ( v0 == 1 )
{
func8(v2);
result = 0LL;
}
return result;
}

original_code:
int test5() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("JOHN",&stResult);
 if (1 == iRC) {
  empty_stack(&stResult);
  iRC = 0;
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test5() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("JOHN",&stResult);
 if (1 == iRC) {
  empty_stack(&stResult);
  iRC = 0;
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test6()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("AL", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func21()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("AL", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test6() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("AL",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test6() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("AL",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test7()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("CONE", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func22()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("CONE", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test7() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("CONE",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test7() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("CONE",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test8()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("OTIS", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func23()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("OTIS", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test8() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("OTIS",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test8() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("OTIS",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test9()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("OATIS", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func24()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("OATIS", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test9() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("OATIS",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  iRC = 0;
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test9() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("OATIS",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  iRC = 0;
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test10()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("OTIAS", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func25()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("OTIAS", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test10() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("OTIAS",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  iRC = 0;
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test10() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("OTIAS",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  iRC = 0;
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
__int64 test11()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)find_name("GEOFF", (__int64)v2) )
{
v0 = 0;
dump_stack(v2);
}
return v0;
}

canonical_decompiled_code:
__int64 func26()
{
unsigned int v0; // ebx
__int64 v2[1253]; // [rsp+0h] [rbp-2728h] BYREF

v0 = 1;
if ( !(unsigned int)func7("GEOFF", (__int64)v2) )
{
v0 = 0;
func11(v2);
}
return v0;
}

original_code:
int test11() {
 int iRC=1;
 stack_t stResult;
 iRC = find_name("GEOFF",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}

canonical_original_code:
int test11() {
 int iRC = 1;
 struct stack stResult;
 iRC = find_name("GEOFF",&stResult);
 if (0 == iRC) {
  dump_stack(&stResult);
  return (0);
 } else {
  iRC = 1;
 }
 return (iRC);
}


decompiled_code:
int unit_tests()
{
if ( (unsigned int)test1() )
return fwrite("Test 1 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test2() )
return fwrite("Test 2 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test3() )
return fwrite("Test 3 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test4() )
return fwrite("Test 4 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test5() )
return fwrite("Test 5 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test6() )
return fwrite("Test 6 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test7() )
return fwrite("Test 7 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test8() )
return fwrite("Test 8 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test9() )
return fwrite("Test 9 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)test10() )
return fwrite("Test 10 failed.\n", 1uLL, 0x10uLL, stderr);
puts("---------------");
if ( (unsigned int)test11() )
return fwrite("Test 11 failed.\n", 1uLL, 0x10uLL, stderr);
puts("---------------");
return puts("All tests passed.");
}

canonical_decompiled_code:
int func27()
{
if ( (unsigned int)func16() )
return fwrite("Test 1 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func17() )
return fwrite("Test 2 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func18() )
return fwrite("Test 3 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func19() )
return fwrite("Test 4 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func20() )
return fwrite("Test 5 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func21() )
return fwrite("Test 6 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func22() )
return fwrite("Test 7 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func23() )
return fwrite("Test 8 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func24() )
return fwrite("Test 9 failed.\n", 1uLL, 0xFuLL, stderr);
puts("---------------");
if ( (unsigned int)func25() )
return fwrite("Test 10 failed.\n", 1uLL, 0x10uLL, stderr);
puts("---------------");
if ( (unsigned int)func26() )
return fwrite("Test 11 failed.\n", 1uLL, 0x10uLL, stderr);
puts("---------------");
return puts("All tests passed.");
}

original_code:
void unit_tests(void) {
 if ( test1() ) {
  fprintf(stderr,"Test 1 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test2() ) {
  fprintf(stderr,"Test 2 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test3() ) {
  fprintf(stderr,"Test 3 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test4() ) {
  fprintf(stderr,"Test 4 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test5() ) {
  fprintf(stderr,"Test 5 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test6() ) {
  fprintf(stderr,"Test 6 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test7() ) {
  fprintf(stderr,"Test 7 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test8() ) {
  fprintf(stderr,"Test 8 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test9() ) {
  fprintf(stderr,"Test 9 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test10() ) {
  fprintf(stderr,"Test 10 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test11() ) {
  fprintf(stderr,"Test 11 failed.\n");
  return;
 }
 printf("---------------\n");
 printf("All tests passed.\n");
}

canonical_original_code:
void unit_tests() {
 if ( test1() ) {
  fprintf(stderr,"Test 1 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test2() ) {
  fprintf(stderr,"Test 2 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test3() ) {
  fprintf(stderr,"Test 3 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test4() ) {
  fprintf(stderr,"Test 4 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test5() ) {
  fprintf(stderr,"Test 5 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test6() ) {
  fprintf(stderr,"Test 6 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test7() ) {
  fprintf(stderr,"Test 7 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test8() ) {
  fprintf(stderr,"Test 8 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test9() ) {
  fprintf(stderr,"Test 9 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test10() ) {
  fprintf(stderr,"Test 10 failed.\n");
  return;
 }
 printf("---------------\n");
 if ( test11() ) {
  fprintf(stderr,"Test 11 failed.\n");
  return;
 }
 printf("---------------\n");
 printf("All tests passed.\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
if ( argc != 1 )
{
fprintf(stderr, "Usage: %s \n", *argv);
exit(1);
}
build_tiles2();
unit_tests();
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
if ( argc != 1 )
{
fprintf(stderr, "Usage: %s \n", *argv);
exit(1);
}
func13();
func27();
exit(0);
}

original_code:
int main(int argc, char *argv[]) {
 int iRC;
 iRC=0;
 if (argc != 1) {
  fprintf(stderr, "Usage: %s \n", argv[0]);
  iRC=1;
  exit(iRC);
 }
 build_tiles2();
 unit_tests();
 exit(iRC);
}

variable_types: argc int

variable_types:
argv
char *[]


stringnum

repo: netsoc/hybserv-to-anope

decompiled_code:
__int64 __fastcall stringnum(char *s2)
{
int v1; // ebp
__int64 v2; // r13
__int64 v3; // rbx
unsigned int v4; // er14

v1 = numstrings;
if ( numstrings <= 0 )
return (unsigned int)-1;
v2 = stringnames;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( !strcmp(*(const char **)(v2 + 8 * v3), s2) )
break;
if ( ++v3 == v1 )
return (unsigned int)-1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s2)
{
int v1; // ebp
__int64 v2; // r13
__int64 v3; // rbx
unsigned int v4; // er14

v1 = numstrings;
if ( numstrings <= 0 )
return (unsigned int)-1;
v2 = stringnames;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( !strcmp(*(const char **)(v2 + 8 * v3), s2) )
break;
if ( ++v3 == v1 )
return (unsigned int)-1;
}
return v4;
}

original_code:
int stringnum(const char *name)
{
    int i;
    for (i = 0; i < numstrings; i++) {
 if (strcmp(stringnames[i], name) == 0)
     return i;
    }
    return -1;
}

canonical_original_code:
int stringnum(char *name)
{
    int i;
    for (i = 0; i < numstrings; i++) {
 if (strcmp(stringnames[i], name) == 0)
     return i;
    }
    return -1;
}

variable_types:
name
char *


anopeStrDup

decompiled_code:
char *__fastcall anopeStrDup(char *src)
{
char *v1; // rbp
size_t v2; // rax
char *v3; // rax

v1 = src;
if ( src )
{
v2 = strlen(src);
v3 = (char *)malloc(v2 + 1);
v1 = v3;
if ( v3 )
strcpy(v3, src);
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func10(char *src)
{
char *v1; // rbp
size_t v2; // rax
char *v3; // rax

v1 = src;
if ( src )
{
v2 = strlen(src);
v3 = (char *)malloc(v2 + 1);
v1 = v3;
if ( v3 )
strcpy(v3, src);
}
return v1;
}

original_code:
char *anopeStrDup(const char *src) {
    char *ret=((void *)0);
    if(src) {
        if( (ret = (char *)malloc(strlen(src)+1)) ) {;
            strcpy(ret,src);
        }
    }
    return ret;
}

canonical_original_code:
char *anopeStrDup(char *src) {
    char *ret = ((void *)0);
    if(src) {
        if( (ret = (char *)malloc(strlen(src)+1)) ) {;
            strcpy(ret,src);
        }
    }
    return ret;
}

variable_types:
src
char *

variable_types:
ret
char *

return_type: char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
char *v4; // rax
signed __int64 v5; // rbx
FILE *v6; // r15
FILE *v7; // r14
int v8; // er12
int v9; // ebp
char *v11; // rbx
int v12; // eax
size_t v13; // rax
const char *v14; // rax
char *v15; // rax
const char *v16; // rbx
int v17; // eax
void *v18; // rax
int v19; // ebp
__int64 v20; // rbx
const char *v21; // rdi
__int64 v22; // rbx
_BYTE *v23; // rbp
const char *v24; // rdi
__int64 c; // [rsp+0h] [rbp-268h]
_QWORD *ca; // [rsp+0h] [rbp-268h]
int cb; // [rsp+0h] [rbp-268h]
int v28; // [rsp+Ch] [rbp-25Ch]
char *v29; // [rsp+10h] [rbp-258h]
char *v30; // [rsp+10h] [rbp-258h]
int v31; // [rsp+18h] [rbp-250h]
int v32; // [rsp+1Ch] [rbp-24Ch]
const char **v33; // [rsp+20h] [rbp-248h]
char s[256]; // [rsp+30h] [rbp-238h] BYREF
char dest[312]; // [rsp+130h] [rbp-138h] BYREF

v32 = argc;
v31 = 0;
if ( argc > 1 && !strcmp(argv[1], "-w") )
{
argv[1] = argv[2];
v32 = argc - 1;
v31 = 1;
}
if ( v32 == 2 )
{
v3 = argv[1];
v4 = strrchr(v3, 46);
if ( !v4 )
v4 = (char *)&v3[strlen(v3)];
if ( (unsigned __int64)(v4 - v3) > 0xFB )
v4 = (char *)(v3 + 253);
v5 = v4 - v3;
strncpy(dest, v3, v4 - v3);
dest[v5] = 0;
snprintf(s, 0x100uLL, "%s", dest);
v28 = 1;
if ( (int)read_index_file() >= 0 )
{
v6 = fopen(v3, "rb");
if ( v6 )
{
v7 = fopen(s, "wb");
if ( v7 )
{
v8 = 50;
v9 = -2;
v28 = 0;
do
{
v15 = ano_getline(v6);
v16 = v15;
if ( !v15 )
break;
if ( *v15 == 9 )
{
if ( v9 == -2 )
{
fprintf(stderr, "%s:%d: Junk at beginning of file\n", v3, (unsigned int)linenum);
v28 = 1;
}
else if ( v9 >= 0 )
{
v29 = v15 + 1;
v33 = (const char **)(strings + 8LL * v9);
v11 = (char *)*v33;
v12 = 0;
if ( *v33 )
v12 = strlen(*v33);
c = v12;
v13 = strlen(v29);
v14 = (const char *)realloc(v11, c + v13 + 2);
*v33 = v14;
if ( !v14 )
{
LABEL_24:
fprintf(stderr, "%s:%d: Out of memory!\n", v3, (unsigned int)linenum);
return 2;
}
sprintf((char *)(*(_QWORD *)(strings + 8LL * v9) + c), "%s\n", v29);
}
}
else
{
v17 = stringnum(v15);
v9 = v17;
if ( v17 < 0 )
{
fprintf(stderr, "%s:%d: Unknown string name `%s'\n", v3, (unsigned int)linenum, v16);
--v8;
v28 = 1;
}
else
{
v30 = (char *)v17;
ca = (_QWORD *)(strings + 8LL * v17);
if ( *ca )
{
fprintf(stderr, "%s:%d: Duplicate occurrence of string `%s'\n", v3, (unsigned int)linenum, v16);
--v8;
v28 = 1;
}
else
{
v18 = malloc(1uLL);
*ca = v18;
if ( !v18 )
goto LABEL_24;
**(_BYTE **)(strings + 8LL * (_QWORD)v30) = 0;
}
}
if ( !v8 )
{
fprintf(stderr, "%s:%d: Too many errors!\n", v3, (unsigned int)linenum);
break;
}
}
}
while ( v8 > 0 );
fput32(numstrings, v7);
v19 = 8 * numstrings + 4;
if ( numstrings > 0 )
{
v20 = 0LL;
do
{
v21 = *(const char **)(strings + 8 * v20);
cb = 0;
if ( v21 && *v21 )
cb = strlen(v21) - 1;
fput32(v19, v7);
fput32(cb, v7);
v19 += cb;
++v20;
}
while ( numstrings > (int)v20 );
if ( numstrings > 0 )
{
v22 = 0LL;
do
{
v23 = *(_BYTE **)(strings + 8 * v22);
if ( v23 )
{
if ( *v23 )
{
v23[strlen(*(const char **)(strings + 8 * v22)) - 1] = 0;
v24 = *(const char **)(strings + 8 * v22);
if ( *v24 )
fputs(v24, v7);
}
}
else if ( v31 )
{
fprintf(stderr, "%s: String `%s' missing\n", v3, *(const char **)(stringnames + 8 * v22));
}
++v22;
}
while ( numstrings > (int)v22 );
}
}
fclose(v6);
fclose(v7);
}
else
{
perror(s);
fclose(v6);
v28 = 1;
}
}
else
{
perror(v3);
v28 = 1;
}
}
}
else
{
fprintf(stderr, "Usage: %s [-w] <lang-file>\n", *argv);
v28 = 1;
}
return v28;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
char *v4; // rax
signed __int64 v5; // rbx
FILE *v6; // r15
FILE *v7; // r14
int v8; // er12
int v9; // ebp
char *v11; // rbx
int v12; // eax
size_t v13; // rax
const char *v14; // rax
char *v15; // rax
const char *v16; // rbx
int v17; // eax
void *v18; // rax
int v19; // ebp
__int64 v20; // rbx
const char *v21; // rdi
__int64 v22; // rbx
_BYTE *v23; // rbp
const char *v24; // rdi
__int64 c; // [rsp+0h] [rbp-268h]
_QWORD *ca; // [rsp+0h] [rbp-268h]
int cb; // [rsp+0h] [rbp-268h]
int v28; // [rsp+Ch] [rbp-25Ch]
char *v29; // [rsp+10h] [rbp-258h]
char *v30; // [rsp+10h] [rbp-258h]
int v31; // [rsp+18h] [rbp-250h]
int v32; // [rsp+1Ch] [rbp-24Ch]
const char **v33; // [rsp+20h] [rbp-248h]
char s[256]; // [rsp+30h] [rbp-238h] BYREF
char dest[312]; // [rsp+130h] [rbp-138h] BYREF

v32 = argc;
v31 = 0;
if ( argc > 1 && !strcmp(argv[1], "-w") )
{
argv[1] = argv[2];
v32 = argc - 1;
v31 = 1;
}
if ( v32 == 2 )
{
v3 = argv[1];
v4 = strrchr(v3, 46);
if ( !v4 )
v4 = (char *)&v3[strlen(v3)];
if ( (unsigned __int64)(v4 - v3) > 0xFB )
v4 = (char *)(v3 + 253);
v5 = v4 - v3;
strncpy(dest, v3, v4 - v3);
dest[v5] = 0;
snprintf(s, 0x100uLL, "%s", dest);
v28 = 1;
if ( (int)func11() >= 0 )
{
v6 = fopen(v3, "rb");
if ( v6 )
{
v7 = fopen(s, "wb");
if ( v7 )
{
v8 = 50;
v9 = -2;
v28 = 0;
do
{
v15 = func8(v6);
v16 = v15;
if ( !v15 )
break;
if ( *v15 == 9 )
{
if ( v9 == -2 )
{
fprintf(stderr, "%s:%d: Junk at beginning of file\n", v3, (unsigned int)linenum);
v28 = 1;
}
else if ( v9 >= 0 )
{
v29 = v15 + 1;
v33 = (const char **)(strings + 8LL * v9);
v11 = (char *)*v33;
v12 = 0;
if ( *v33 )
v12 = strlen(*v33);
c = v12;
v13 = strlen(v29);
v14 = (const char *)realloc(v11, c + v13 + 2);
*v33 = v14;
if ( !v14 )
{
LABEL_24:
fprintf(stderr, "%s:%d: Out of memory!\n", v3, (unsigned int)linenum);
return 2;
}
sprintf((char *)(*(_QWORD *)(strings + 8LL * v9) + c), "%s\n", v29);
}
}
else
{
v17 = func7(v15);
v9 = v17;
if ( v17 < 0 )
{
fprintf(stderr, "%s:%d: Unknown string name `%s'\n", v3, (unsigned int)linenum, v16);
--v8;
v28 = 1;
}
else
{
v30 = (char *)v17;
ca = (_QWORD *)(strings + 8LL * v17);
if ( *ca )
{
fprintf(stderr, "%s:%d: Duplicate occurrence of string `%s'\n", v3, (unsigned int)linenum, v16);
--v8;
v28 = 1;
}
else
{
v18 = malloc(1uLL);
*ca = v18;
if ( !v18 )
goto LABEL_24;
**(_BYTE **)(strings + 8LL * (_QWORD)v30) = 0;
}
}
if ( !v8 )
{
fprintf(stderr, "%s:%d: Too many errors!\n", v3, (unsigned int)linenum);
break;
}
}
}
while ( v8 > 0 );
func9(numstrings, v7);
v19 = 8 * numstrings + 4;
if ( numstrings > 0 )
{
v20 = 0LL;
do
{
v21 = *(const char **)(strings + 8 * v20);
cb = 0;
if ( v21 && *v21 )
cb = strlen(v21) - 1;
func9(v19, v7);
func9(cb, v7);
v19 += cb;
++v20;
}
while ( numstrings > (int)v20 );
if ( numstrings > 0 )
{
v22 = 0LL;
do
{
v23 = *(_BYTE **)(strings + 8 * v22);
if ( v23 )
{
if ( *v23 )
{
v23[strlen(*(const char **)(strings + 8 * v22)) - 1] = 0;
v24 = *(const char **)(strings + 8 * v22);
if ( *v24 )
fputs(v24, v7);
}
}
else if ( v31 )
{
fprintf(stderr, "%s: String `%s' missing\n", v3, *(const char **)(stringnames + 8 * v22));
}
++v22;
}
while ( numstrings > (int)v22 );
}
}
fclose(v6);
fclose(v7);
}
else
{
perror(s);
fclose(v6);
v28 = 1;
}
}
else
{
perror(v3);
v28 = 1;
}
}
}
else
{
fprintf(stderr, "Usage: %s [-w] <lang-file>\n", *argv);
v28 = 1;
}
return v28;
}

original_code:
int main(int argc, char *argv[])
{
    char buf[8192];
    int headers_done = 0;
    char *server, *aport;
    short port;
    if (argc == 1)
        return 0;
    server = strtok(argv[1], ":");
    if ((aport = strtok(((void *)0), ""))) {
        port = atoi(aport);
    } else {
        port = 25;
    }
   if (!server) {
        alog("No Server");
        return 0;
    } else {
        alog("SMTP: server %s port %d",server,port);
    }
    memset(&mail, 0, sizeof(mail));
    while (fgets(buf, 8192, stdin)) {
        if (smtp_is_header(buf) && !headers_done) {
            char *header, *value;
            smtp_add_header(buf);
            smtp_parse_header(buf, &header, &value);
            if (!strcasecmp(header, "from")) {
          alog("SMTP: from: %s",value);
                smtp_set_from(value);
            } else if (!strcasecmp(header, "to")) {
          alog("SMTP: to: %s",value);
                smtp_set_to(value);
            } else if (smtp_is_end(buf)) {
                break;
            } else {
                headers_done = 1;
                smtp_add_body_line(buf);
            }
        } else {
                smtp_add_body_line(buf);
        }
    }
    if (!smtp_connect(server, port)) {
     alog("SMTP: failed to connect to %s:%d",server, port);
  mail_cleanup();
        return 0;
    }
    if (!smtp_send_email()) {
     alog("SMTP: error during sending of mail");
  mail_cleanup();
        return 0;
    }
    smtp_disconnect();
 mail_cleanup();
    return 1;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char buf[8192];
    int headers_done = 0;
    char *server; char *aport;
    short port;
    if (argc == 1)
        return 0;
    server = strtok(argv[1], ":");
    if ((aport = strtok(((void *)0), ""))) {
        port = atoi(aport);
    } else {
        port = 25;
    }
   if (!server) {
        alog("No Server");
        return 0;
    } else {
        alog("SMTP: server %s port %d",server,port);
    }
    memset(&mail, 0, sizeof(mail));
    while (fgets(buf, 8192, stdin)) {
        if (smtp_is_header(buf) && !headers_done) {
            char *header; char *value;
            smtp_add_header(buf);
            smtp_parse_header(buf, &header, &value);
            if (!strcasecmp(header, "from")) {
          alog("SMTP: from: %s",value);
                smtp_set_from(value);
            } else if (!strcasecmp(header, "to")) {
          alog("SMTP: to: %s",value);
                smtp_set_to(value);
            } else if (smtp_is_end(buf)) {
                break;
            } else {
                headers_done = 1;
                smtp_add_body_line(buf);
            }
        } else {
                smtp_add_body_line(buf);
        }
    }
    if (!smtp_connect(server, port)) {
     alog("SMTP: failed to connect to %s:%d",server, port);
  mail_cleanup();
        return 0;
    }
    if (!smtp_send_email()) {
     alog("SMTP: error during sending of mail");
  mail_cleanup();
        return 0;
    }
    smtp_disconnect();
 mail_cleanup();
    return 1;
}

variable_types:
buf
char [8192]

variable_types:
server
char *

variable_types:
aport
char *

variable_types: port short

variable_types:
header
char *

variable_types:
value
char *


repo: neub/wr-switch-sw

decompiled_code:
__int64 __fastcall shw_pwm_update_timeout(int a1)
{
fan_update_timeout = 100000 * a1;
return trace_printf("fan.c", 85LL, 16LL, "Fan tick timeout is =%d", (unsigned int)(100000 * a1));
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
fan_update_timeout = 100000 * a1;
return trace_printf("fan.c", 85LL, 16LL, "Fan tick timeout is =%d", (unsigned int)(100000 * a1));
}

original_code:
void shw_pwm_update_timeout(int tout_100ms)
{
 fan_update_timeout=tout_100ms*100000;
 trace_printf("fan.c", 85, 0x10,"Fan tick timeout is =%d",fan_update_timeout);;
}


decompiled_code:
__int64 __fastcall pwm_configure_fpga(char a1, float a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 < 1.0 )
result = (unsigned int)(int)(a2 * 255.0);
if ( (a1 & 1) != 0 )
*(_DWORD *)(spwm_wbr + 8) = (unsigned __int8)result;
if ( (a1 & 2) != 0 )
{
result = (unsigned __int8)result;
*(_DWORD *)(spwm_wbr + 12) = (unsigned __int8)result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1, float a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 < 1.0 )
result = (unsigned int)(int)(a2 * 255.0);
if ( (a1 & 1) != 0 )
*(_DWORD *)(spwm_wbr + 8) = (unsigned __int8)result;
if ( (a1 & 2) != 0 )
{
result = (unsigned __int8)result;
*(_DWORD *)(spwm_wbr + 12) = (unsigned __int8)result;
}
return result;
}

original_code:
void pwm_configure_fpga(int enmask, float rate)
{
 uint8_t u8speed=(uint8_t)((rate>=1)?0xff:(rate*255.0));
 if((enmask & 0x1)>0) spwm_wbr->DR0=u8speed;
 if((enmask & 0x2)>0) spwm_wbr->DR1=u8speed;
}

canonical_original_code:
void pwm_configure_fpga(int enmask, float rate)
{
 unsigned char u8speed = (uint8_t)((rate>=1)?0xff:(rate*255.0));
 if((enmask & 0x1)>0) spwm_wbr->DR0=u8speed;
 if((enmask & 0x2)>0) spwm_wbr->DR1=u8speed;
}

variable_types: enmask int

variable_types: rate float


decompiled_code:
int __fastcall shw_pwm_speed(char a1, float a2)
{
_DWORD *v2; // rax

if ( !is_cpu_pwn )
return pwm_configure_fpga(a1, a2);
v2 = (_DWORD *)get_pio_pin(2LL);
return pwm_configure_pin(v2, a1, (int)(float)(a2 * 1000.0));
}

canonical_decompiled_code:
int __fastcall func5(char a1, float a2)
{
_DWORD *v2; // rax

if ( !is_cpu_pwn )
return func4(a1, a2);
v2 = (_DWORD *)get_pio_pin(2LL);
return func3(v2, a1, (int)(float)(a2 * 1000.0));
}

original_code:
void shw_pwm_speed(int enmask, float rate)
{
 if(is_cpu_pwn)
 {
  pwm_configure_pin(get_pio_pin(shw_io_box_fan_en), enmask, rate*1000);
 }
 else
 {
  pwm_configure_fpga(enmask,rate);
 }
}


decompiled_code:
__int64 __fastcall tmp100_read_reg(unsigned int a1, char a2, unsigned int a3)
{
unsigned int v4; // ebx
char *v5; // rax
char v7[32]; // [rsp+8h] [rbp-20h] BYREF

v7[0] = a2;
i2c_write(&fpga_sensors_i2c, a1, 1LL, v7);
v4 = a3;
i2c_read(&fpga_sensors_i2c, a1, a3, v7);
if ( a3 )
{
v5 = v7;
v4 = 0;
do
v4 = (unsigned __int8)*v5++ | (v4 << 8);
while ( v5 != &v7[a3] );
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, char a2, unsigned int a3)
{
unsigned int v4; // ebx
char *v5; // rax
char v7[32]; // [rsp+8h] [rbp-20h] BYREF

v7[0] = a2;
i2c_write(&fpga_sensors_i2c, a1, 1LL, v7);
v4 = a3;
i2c_read(&fpga_sensors_i2c, a1, a3, v7);
if ( a3 )
{
v5 = v7;
v4 = 0;
do
v4 = (unsigned __int8)*v5++ | (v4 << 8);
while ( v5 != &v7[a3] );
}
return v4;
}

original_code:
uint32_t tmp100_read_reg(int dev_addr, uint8_t reg_addr, int n_bytes)
{
 uint8_t data[8];
 uint32_t rv=0, i;
 data[0] = reg_addr;
 i2c_write(&fpga_sensors_i2c, dev_addr, 1, data);
 i2c_read(&fpga_sensors_i2c, dev_addr, n_bytes, data);
 for(i=0; i<n_bytes;i++)
 {
  rv <<= 8;
  rv |= data[i];
 }
 return rv;
}

canonical_original_code:
unsigned int tmp100_read_reg(int dev_addr, unsigned char reg_addr, int n_bytes)
{
 unsigned char data[8];
 unsigned int rv = 0; unsigned int i;
 data[0] = reg_addr;
 i2c_write(&fpga_sensors_i2c, dev_addr, 1, data);
 i2c_read(&fpga_sensors_i2c, dev_addr, n_bytes, data);
 for(i=0; i<n_bytes;i++)
 {
  rv <<= 8;
  rv |= data[i];
 }
 return rv;
}

variable_types:
data
unsigned char [8]

variable_types:
rv
unsigned int

variable_types:
i
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall tmp100_write_reg(unsigned int a1, char a2, char a3)
{
char v4[10]; // [rsp+Eh] [rbp-Ah] BYREF

v4[0] = a2;
v4[1] = a3;
return i2c_write(&fpga_sensors_i2c, a1, 2LL, v4);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, char a2, char a3)
{
char v4[10]; // [rsp+Eh] [rbp-Ah] BYREF

v4[0] = a2;
v4[1] = a3;
return i2c_write(&fpga_sensors_i2c, a1, 2LL, v4);
}

original_code:
void tmp100_write_reg(int dev_addr, uint8_t reg_addr, uint8_t value)
{
 uint8_t data[2];
 data[0] = reg_addr;
 data[1] = value;
 i2c_write(&fpga_sensors_i2c, dev_addr, 2, data);
}

canonical_original_code:
void tmp100_write_reg(int dev_addr, unsigned char reg_addr, unsigned char value)
{
 unsigned char data[2];
 data[0] = reg_addr;
 data[1] = value;
 i2c_write(&fpga_sensors_i2c, dev_addr, 2, data);
}

variable_types:
value
unsigned char

variable_types:
data
unsigned char [2]


decompiled_code:
float __fastcall tmp100_read_temp(unsigned int a1)
{
return (float)((int)tmp100_read_reg(a1, 0, 2u) >> 4) * 0.0625;
}

canonical_decompiled_code:
float __fastcall func8(unsigned int a1)
{
return (float)((int)func6(a1, 0, 2u) >> 4) * 0.0625;
}

original_code:
float tmp100_read_temp(int dev_addr)
{
 int temp = tmp100_read_reg(dev_addr, 0, 2);
 return ((float) (temp >> 4)) / 16.0;
}

variable_types: temp int

return_type: float


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

result = i2c_init_bus(&fpga_sensors_i2c);
if ( (int)result < 0 )
{
trace_printf("fan.c", 203LL, 3LL, "can't initialize temperature sensors I2C bus.\n");
result = 0xFFFFFFFFLL;
}
return result;
}

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

result = i2c_init_bus(&fpga_sensors_i2c);
if ( (int)result < 0 )
{
trace_printf("fan.c", 203LL, 3LL, "can't initialize temperature sensors I2C bus.\n");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int shw_init_i2c_sensors()
{
 if (i2c_init_bus(&fpga_sensors_i2c) < 0) {
  trace_printf("fan.c", 203, 3, "can't initialize temperature sensors I2C bus.\n");;
  return -1;
 }
}


decompiled_code:
__int64 shw_init_fans()
{
int v0; // eax
const char *v1; // r8

v0 = shw_get_hw_ver();
is_cpu_pwn = v0 <= 329;
v1 = "CPU";
if ( v0 >= 330 )
v1 = "FPGA";
trace_printf(
"fan.c",
218LL,
16LL,
"Configuring %s PWMs for fans (desired temperature = %.1f degC)...",
v1,
*(double *)&LC10);
if ( !is_cpu_pwn )
{
spwm_wbr = fpga_base_virt + 356352LL;
*(_DWORD *)(fpga_base_virt + 356352LL) = 16711710;
fan_pi = 1065353216;
dword_524 = 1082130432;
dword_534 = 1137180672;
dword_52C = 1128792064;
dword_538 = 1148846080;
LABEL_5:
shw_init_i2c_sensors();
tmp100_write_reg(0x4Cu, 1, 96);
pi_init((__int64)&fan_pi);
shw_pwm_update_timeout(5);
return 0LL;
}
pwm_fd = open("/dev/at91_softpwm", 2);
if ( pwm_fd >= 0 )
{
fan_pi = 1065353216;
dword_524 = 1082130432;
dword_534 = LC15;
dword_52C = LC15;
dword_538 = 1145569280;
goto LABEL_5;
}
trace_printf("fan.c", 225LL, 3LL, "at91_softpwm driver not installed or device not created.\n");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // eax
const char *v1; // r8

v0 = shw_get_hw_ver();
is_cpu_pwn = v0 <= 329;
v1 = "CPU";
if ( v0 >= 330 )
v1 = "FPGA";
trace_printf(
"fan.c",
218LL,
16LL,
"Configuring %s PWMs for fans (desired temperature = %.1f degC)...",
v1,
*(double *)&LC10);
if ( !is_cpu_pwn )
{
spwm_wbr = fpga_base_virt + 356352LL;
*(_DWORD *)(fpga_base_virt + 356352LL) = 16711710;
fan_pi = 1065353216;
dword_524 = 1082130432;
dword_534 = 1137180672;
dword_52C = 1128792064;
dword_538 = 1148846080;
LABEL_5:
func9();
func7(0x4Cu, 1, 96);
func1((__int64)&fan_pi);
func2(5);
return 0LL;
}
pwm_fd = open("/dev/at91_softpwm", 2);
if ( pwm_fd >= 0 )
{
fan_pi = 1065353216;
dword_524 = 1082130432;
dword_534 = LC15;
dword_52C = LC15;
dword_538 = 1145569280;
goto LABEL_5;
}
trace_printf("fan.c", 225LL, 3LL, "at91_softpwm driver not installed or device not created.\n");
return 0xFFFFFFFFLL;
}

original_code:
int shw_init_fans()
{
 uint8_t dev_map[128];
 uint32_t val=0;
 int detect, i;
 if(shw_get_hw_ver()<330) is_cpu_pwn=1;
 else is_cpu_pwn=0;
 trace_printf("fan.c", 218, 0x10, "Configuring %s PWMs for fans (desired temperature = %.1f degC)...",is_cpu_pwn?"CPU":"FPGA",55.0);;
 if(is_cpu_pwn)
 {
  pwm_fd = open("/dev/at91_softpwm", 02);
  if(pwm_fd < 0)
  {
   trace_printf("fan.c", 225, 3, "at91_softpwm driver not installed or device not created.\n");;
   return -1;
  }
  fan_pi.ki = 1.0;
  fan_pi.kp = 4.0;
  fan_pi.y_min = 200;
  fan_pi.bias = 200;
  fan_pi.y_max = 800;
 }
 else
 {
  spwm_wbr= (volatile struct SPWM_WB *) (_fpga_base_virt + 0x57000);
  val= (((30) & ((1<<(16))-1)) << (0)) | (((255) & ((1<<(16))-1)) << (16));
  spwm_wbr->CR=val;
  fan_pi.ki = 1.0;
  fan_pi.kp = 4.0;
  fan_pi.y_min = 400;
  fan_pi.bias = 200;
  fan_pi.y_max = 1000;
 }
 shw_init_i2c_sensors();
 tmp100_write_reg(0x4c, 1, 0x60);
 pi_init(&fan_pi);
 shw_pwm_update_timeout(5);
 return 0;
}

canonical_original_code:
int shw_init_fans()
{
 unsigned char dev_map[128];
 unsigned int val = 0;
 int detect; int i;
 if(shw_get_hw_ver()<330) is_cpu_pwn=1;
 else is_cpu_pwn=0;
 trace_printf("fan.c", 218, 0x10, "Configuring %s PWMs for fans (desired temperature = %.1f degC)...",is_cpu_pwn?"CPU":"FPGA",55.0);;
 if(is_cpu_pwn)
 {
  pwm_fd = open("/dev/at91_softpwm", 02);
  if(pwm_fd < 0)
  {
   trace_printf("fan.c", 225, 3, "at91_softpwm driver not installed or device not created.\n");;
   return -1;
  }
  fan_pi.ki = 1.0;
  fan_pi.kp = 4.0;
  fan_pi.y_min = 200;
  fan_pi.bias = 200;
  fan_pi.y_max = 800;
 }
 else
 {
  spwm_wbr= (struct SPWM_WB *) (_fpga_base_virt + 0x57000);
  val= (((30) & ((1<<(16))-1)) << (0)) | (((255) & ((1<<(16))-1)) << (16));
  spwm_wbr->CR=val;
  fan_pi.ki = 1.0;
  fan_pi.kp = 4.0;
  fan_pi.y_min = 400;
  fan_pi.bias = 200;
  fan_pi.y_max = 1000;
 }
 shw_init_i2c_sensors();
 tmp100_write_reg(0x4c, 1, 0x60);
 pi_init(&fan_pi);
 shw_pwm_update_timeout(5);
 return 0;
}

variable_types:
val
unsigned int

variable_types: detect int

variable_types: i int

user_defined_types: struct SPWM_WB { unsigned int CR; unsigned int SR; unsigned int DR0; unsigned int DR1; unsigned int DR2; unsigned int DR3; unsigned int DR4; unsigned int DR5; unsigned int DR6; unsigned int DR7; }


decompiled_code:
int __fastcall shw_update_fans(__m128 a1)
{
int result; // eax
__int64 v2; // rbx
float v3; // xmm0_4

result = shw_get_tics();
if ( fan_update_timeout > 0 )
{
v2 = result;
result = last_tics_0;
if ( last_tics_0 < 0 || v2 - last_tics_0 > fan_update_timeout )
{
a1.m128_f32[0] = tmp100_read_temp(0x4Cu) - 55.0;
v3 = pi_update((__int64)&fan_pi, a1).m128_f32[0];
result = shw_pwm_speed(255, v3 / 1000.0);
last_tics_0 = v2;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(__m128 a1)
{
int result; // eax
__int64 v2; // rbx
float v3; // xmm0_4

result = shw_get_tics();
if ( fan_update_timeout > 0 )
{
v2 = result;
result = last_tics_0;
if ( last_tics_0 < 0 || v2 - last_tics_0 > fan_update_timeout )
{
a1.m128_f32[0] = func8(0x4Cu) - 55.0;
v3 = func0((__int64)&fan_pi, a1).m128_f32[0];
result = func5(255, v3 / 1000.0);
last_tics_0 = v2;
}
}
return result;
}

original_code:
void shw_update_fans()
{
 static int64_t last_tics = -1;
 int64_t cur_tics = shw_get_tics();
 if(fan_update_timeout>0 && (last_tics < 0 || (cur_tics - last_tics) > fan_update_timeout))
 {
  float t_cur = tmp100_read_temp(0x4c);
  float drive = pi_update(&fan_pi, t_cur - 55.0);
  shw_pwm_speed(0xFF, drive/1000);
  last_tics = cur_tics;
 }
}

canonical_original_code:
void shw_update_fans()
{
 static long last_tics = -1;
 long cur_tics = shw_get_tics();
 if(fan_update_timeout>0 && (last_tics < 0 || (cur_tics - last_tics) > fan_update_timeout))
 {
  float t_cur = tmp100_read_temp(0x4c);
  float drive = pi_update(&fan_pi, t_cur - 55.0);
  shw_pwm_speed(0xFF, drive/1000);
  last_tics = cur_tics;
 }
}

variable_types: drive float


decompiled_code:
__int64 call_cleanup_cbs()
{
__int64 result; // rax
int *v1; // rbx

result = trace_printf("hal_main.c", 46LL, 16LL, "Cleaning up...");
v1 = (int *)&cleanup_cb;
do
{
if ( *(_QWORD *)v1 )
result = (*(__int64 (**)(void))v1)();
v1 += 2;
}
while ( v1 != &daemon_mode );
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
int *v1; // rbx

result = trace_printf("hal_main.c", 46LL, 16LL, "Cleaning up...");
v1 = (int *)&cleanup_cb;
do
{
if ( *(_QWORD *)v1 )
result = (*(__int64 (**)(void))v1)();
v1 += 2;
}
while ( v1 != &daemon_mode );
return result;
}

original_code:
static void call_cleanup_cbs()
{
  int i;
 trace_printf("hal_main.c", 46, 0x10, "Cleaning up...");;
 for(i=0;i<16;i++)
  if(cleanup_cb[i]) cleanup_cb[i]();
}


decompiled_code:
__int64 unload_kernel_modules()
{
__int64 i; // rsi
__int64 result; // rax
char v2[88]; // [rsp+0h] [rbp-58h] BYREF

for ( i = 0LL; ; i = (unsigned int)(i + 1) )
{
result = hal_config_iterate("global.modules", i, v2, 80LL);
if ( !(_DWORD)result )
break;
load_unload_kmod(v2, 0);
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 i; // rsi
__int64 result; // rax
char v2[88]; // [rsp+0h] [rbp-58h] BYREF

for ( i = 0LL; ; i = (unsigned int)(i + 1) )
{
result = hal_config_iterate("global.modules", i, v2, 80LL);
if ( !(_DWORD)result )
break;
func1(v2, 0);
}
return result;
}

original_code:
static void unload_kernel_modules()
{
 char module_name[80];
 int index = 0;
 for(;;)
 {
  if(!hal_config_iterate("global.modules", index++, module_name, sizeof(module_name)))
   break;
  load_unload_kmod(module_name, 0);
 }
}

variable_types: index int


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

v1 = 0LL;
while ( cleanup_cb[v1] )
{
if ( ++v1 == 16 )
return 0xFFFFFFFFLL;
}
cleanup_cb[(int)v1] = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( cleanup_cb[v1] )
{
if ( ++v1 == 16 )
return 0xFFFFFFFFLL;
}
cleanup_cb[(int)v1] = a1;
return 0LL;
}

original_code:
int hal_add_cleanup_callback(hal_cleanup_callback_t cb)
{
 int i;
 for(i=0;i<16;i++)
  if(!cleanup_cb[i])
  {
    cleanup_cb[i] = cb;
    return 0;
  }
 return -1;
}

canonical_original_code:
int hal_add_cleanup_callback(void (*cb)())
{
 int i;
 for(i=0;i<16;i++)
  if(!cleanup_cb[i])
  {
    cleanup_cb[i] = cb;
    return 0;
  }
 return -1;
}

variable_types:
cb
void (*)()


decompiled_code:
__int64 hal_setup_fpga_images()
{
__int64 v1; // [rsp+0h] [rbp-88h] BYREF

return (unsigned int)((int)hal_config_get_string("global.hal_firmware_path", &v1, 128LL) >> 31);
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v1; // [rsp+0h] [rbp-88h] BYREF

return (unsigned int)((int)hal_config_get_string("global.hal_firmware_path", &v1, 128LL) >> 31);
}

original_code:
int hal_setup_fpga_images()
{
 char fpga_dir[128];
  if( hal_config_get_string("global.hal_firmware_path", fpga_dir, sizeof(fpga_dir)) < 0)
   return -1;
  return 0;
}


decompiled_code:
__int64 hal_load_kernel_modules()
{
unsigned int v0; // ebp
__int64 v1; // rsi
int v2; // ebx
char v4[104]; // [rsp+0h] [rbp-68h] BYREF

trace_printf("hal_main.c", 123LL, 16LL, "Loading kernel modules...");
v0 = 0;
while ( 1 )
{
v1 = v0++;
v2 = hal_config_iterate("global.modules", v1, v4, 80LL);
if ( !v2 )
break;
v2 = load_unload_kmod(v4, 1);
if ( v2 < 0 )
return (unsigned int)v2;
}
hal_add_cleanup_callback((__int64)unload_kernel_modules);
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // ebp
__int64 v1; // rsi
int v2; // ebx
char v4[104]; // [rsp+0h] [rbp-68h] BYREF

trace_printf("hal_main.c", 123LL, 16LL, "Loading kernel modules...");
v0 = 0;
while ( 1 )
{
v1 = v0++;
v2 = hal_config_iterate("global.modules", v1, v4, 80LL);
if ( !v2 )
break;
v2 = func1(v4, 1);
if ( v2 < 0 )
return (unsigned int)v2;
}
func5((__int64)unload_kernel_modules);
return (unsigned int)v2;
}

original_code:
int hal_load_kernel_modules()
{
 char module_name[80];
 int index = 0;
 trace_printf("hal_main.c", 123, 0x10, "Loading kernel modules...");;
 for(;;)
 {
  if(!hal_config_iterate("global.modules", index++, module_name, sizeof(module_name)))
   break;
  { int ret; if((ret = load_unload_kmod(module_name, 1)) < 0) return ret; };
 }
 hal_add_cleanup_callback(unload_kernel_modules);
 return 0;
}

variable_types: ret int


decompiled_code:
__int64 hal_shutdown()
{
call_cleanup_cbs();
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
func0();
return 0LL;
}

original_code:
int hal_shutdown()
{
 call_cleanup_cbs();
 return 0;
}


decompiled_code:
__int64 hal_update()
{
hal_update_wripc();
hal_update_ports();
return shw_update_fans();
}

canonical_decompiled_code:
__int64 func9()
{
hal_update_wripc();
hal_update_ports();
return shw_update_fans();
}

original_code:
void hal_update()
{
 hal_update_wripc();
 hal_update_ports();
 shw_update_fans();
}


decompiled_code:
__pid_t hal_deamonize()
{
__pid_t result; // eax
__pid_t v1; // eax

result = getppid();
if ( result != 1 )
{
v1 = fork();
if ( v1 < 0 )
exit(1);
if ( v1 > 0 )
exit(0);
umask(0);
if ( setsid() < 0 )
exit(1);
if ( chdir("/") < 0 )
exit(1);
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
result = (unsigned int)freopen("/dev/null", "w", stderr);
}
return result;
}

canonical_decompiled_code:
__pid_t func10()
{
__pid_t result; // eax
__pid_t v1; // eax

result = getppid();
if ( result != 1 )
{
v1 = fork();
if ( v1 < 0 )
exit(1);
if ( v1 > 0 )
exit(0);
umask(0);
if ( setsid() < 0 )
exit(1);
if ( chdir("/") < 0 )
exit(1);
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
result = (unsigned int)freopen("/dev/null", "w", stderr);
}
return result;
}

original_code:
void hal_deamonize()
{
 pid_t pid, sid;
    if ( getppid() == 1 ) return;
    pid = fork();
    if (pid < 0) {
        exit(1);
    }
    if (pid > 0) {
        exit(0);
    }
    umask(0);
    sid = setsid();
    if (sid < 0) {
        exit(1);
    }
    if ((chdir("/")) < 0) {
        exit(1);
    }
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/dev/null", "w", stderr);
}

canonical_original_code:
void hal_deamonize()
{
 int pid; int sid;
    if ( getppid() == 1 ) return;
    pid = fork();
    if (pid < 0) {
        exit(1);
    }
    if (pid > 0) {
        exit(0);
    }
    umask(0);
    sid = setsid();
    if (sid < 0) {
        exit(1);
    }
    if ((chdir("/")) < 0) {
        exit(1);
    }
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/dev/null", "w", stderr);
}

variable_types: pid int

variable_types: sid int


decompiled_code:
__int64 hal_init()
{
__int64 result; // rax
char v1[1032]; // [rsp+0h] [rbp-408h] BYREF

trace_printf("hal_main.c", 166LL, 16LL, "HAL initializing...");
memset(cleanup_cb, 0, sizeof(cleanup_cb));
signal(11, (__sighandler_t)sighandler);
signal(2, (__sighandler_t)sighandler);
signal(15, (__sighandler_t)sighandler);
signal(4, (__sighandler_t)sighandler);
result = hal_parse_config();
if ( (int)result >= 0 )
{
if ( !(unsigned int)hal_config_get_string("global.sfp_database_path", v1, 1024LL) )
{
if ( (int)shw_sfp_read_db(v1) < 0 )
trace_printf("hal_main.c", 184LL, 2LL, "Can't read SFP database (%s)", v1);
else
trace_printf("hal_main.c", 186LL, 16LL, "Loaded SFP database (%s)", v1);
}
result = shw_init();
if ( (int)result >= 0 )
{
result = hal_init_timing();
if ( (int)result >= 0 )
{
result = hal_init_ports();
if ( (int)result >= 0 )
{
result = hal_init_wripc();
if ( (int)result >= 0 )
{
result = (unsigned int)daemon_mode;
if ( daemon_mode )
{
hal_deamonize();
result = 0LL;
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 result; // rax
char v1[1032]; // [rsp+0h] [rbp-408h] BYREF

trace_printf("hal_main.c", 166LL, 16LL, "HAL initializing...");
memset(cleanup_cb, 0, sizeof(cleanup_cb));
signal(11, (__sighandler_t)sighandler);
signal(2, (__sighandler_t)sighandler);
signal(15, (__sighandler_t)sighandler);
signal(4, (__sighandler_t)sighandler);
result = hal_parse_config();
if ( (int)result >= 0 )
{
if ( !(unsigned int)hal_config_get_string("global.sfp_database_path", v1, 1024LL) )
{
if ( (int)shw_sfp_read_db(v1) < 0 )
trace_printf("hal_main.c", 184LL, 2LL, "Can't read SFP database (%s)", v1);
else
trace_printf("hal_main.c", 186LL, 16LL, "Loaded SFP database (%s)", v1);
}
result = shw_init();
if ( (int)result >= 0 )
{
result = hal_init_timing();
if ( (int)result >= 0 )
{
result = hal_init_ports();
if ( (int)result >= 0 )
{
result = hal_init_wripc();
if ( (int)result >= 0 )
{
result = (unsigned int)daemon_mode;
if ( daemon_mode )
{
func10();
result = 0LL;
}
}
}
}
}
}
return result;
}

original_code:
int hal_init()
{
 char sfp_db_path[1024];
 trace_printf("hal_main.c", 166, 0x10,"HAL initializing...");;
 memset(cleanup_cb, 0, sizeof(cleanup_cb));
 signal(11, sighandler);
 signal(2, sighandler);
 signal(15, sighandler);
 signal(4, sighandler);
 { int ret; if((ret = hal_parse_config()) < 0) return ret; };
  if(!hal_config_get_string("global.sfp_database_path", sfp_db_path, sizeof(sfp_db_path)))
  {
   if(shw_sfp_read_db(sfp_db_path) < 0) {
   trace_printf("hal_main.c", 184, 2, "Can't read SFP database (%s)", sfp_db_path);
  } else {
   trace_printf("hal_main.c", 186, 0x10, "Loaded SFP database (%s)", sfp_db_path);;
  }
 }
 { int ret; if((ret = shw_init()) < 0) return ret; };
 { int ret; if((ret = hal_init_timing()) < 0) return ret; };
 { int ret; if((ret = hal_init_ports()) < 0) return ret; };
 { int ret; if((ret = hal_init_wripc()) < 0) return ret; };
 if(daemon_mode)
  hal_deamonize();
 return 0;
}


decompiled_code:
int __fastcall hal_parse_cmdline(int argc, char **argv)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "dfhx:c:");
if ( result == -1 )
return result;
if ( result == 104 )
{
show_help();
exit(0);
}
if ( result > 104 )
{
if ( result == 120 )
hal_config_extra_cmdline(optarg);
else
LABEL_11:
fprintf(stderr, "Unrecognized option. Call %s -h for help.\n", *argv);
}
else if ( result == 99 )
{
hal_config_set_config_file(optarg);
}
else
{
if ( result != 100 )
goto LABEL_11;
daemon_mode = 1;
}
}
}

canonical_decompiled_code:
int __fastcall func12(int argc, char **argv)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "dfhx:c:");
if ( result == -1 )
return result;
if ( result == 104 )
{
func4();
exit(0);
}
if ( result > 104 )
{
if ( result == 120 )
hal_config_extra_cmdline(optarg);
else
LABEL_11:
fprintf(stderr, "Unrecognized option. Call %s -h for help.\n", *argv);
}
else if ( result == 99 )
{
hal_config_set_config_file(optarg);
}
else
{
if ( result != 100 )
goto LABEL_11;
daemon_mode = 1;
}
}
}

original_code:
void hal_parse_cmdline(int argc, char *argv[])
{
 int opt;
 while((opt=getopt(argc, argv, "dfhx:c:")) != -1)
 {
  switch(opt)
  {
   case 'd':
    daemon_mode = 1;
    break;
   case 'x':
    hal_config_extra_cmdline(optarg);
    break;
   case 'c':
    hal_config_set_config_file(optarg);
    break;
   case 'h':
    show_help();
    exit(0);
    break;
   default:
    fprintf(stderr,"Unrecognized option. Call %s -h for help.\n", argv[0]);
    break;
  }
 }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int


sighandler

decompiled_code:
void __fastcall __noreturn sighandler(unsigned int a1)
{
trace_printf("hal_main.c", 141LL, 2LL, "signal caught (%d)!", a1);
shw_io_write(9LL, 1LL);
shw_io_write(8LL, 0LL);
call_cleanup_cbs();
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(unsigned int a1)
{
trace_printf("hal_main.c", 141LL, 2LL, "signal caught (%d)!", a1);
shw_io_write(9LL, 1LL);
shw_io_write(8LL, 0LL);
func0();
exit(0);
}

original_code:
void sighandler(int sig)
{
 trace_printf("hal_main.c", 141, 2, "signal caught (%d)!", sig);;
 shw_io_write(shw_io_led_state_o,1);
 shw_io_write(shw_io_led_state_g,0);
 call_cleanup_cbs();
 exit(0);
}

variable_types: sig int


decompiled_code:
int show_help()
{
return puts(
"WR Switch Hardware Abstraction Layer daemon (wrsw_hal)\n"
"Usage: wrsw_hal [options], where [options] can be:\n"
"-f : force FPGA firmware reload\n"
"-d : fork into background (daemon mode)\n"
"-x [code]: execute arbitrary Lua [code] before loading configuration file\n"
"-c [file]: specify your own config file\n");
}

canonical_decompiled_code:
int func4()
{
return puts(
"WR Switch Hardware Abstraction Layer daemon (wrsw_hal)\n"
"Usage: wrsw_hal [options], where [options] can be:\n"
"-f : force FPGA firmware reload\n"
"-d : fork into background (daemon mode)\n"
"-x [code]: execute arbitrary Lua [code] before loading configuration file\n"
"-c [file]: specify your own config file\n");
}

original_code:
static void show_help()
{
printf("WR Switch Hardware Abstraction Layer daemon (wrsw_hal)\nUsage: wrsw_hal [options], where [options] can be:\n-f       : force FPGA firmware reload\n-d       : fork into background (daemon mode)\n-x [code]: execute arbitrary Lua [code] before loading configuration file\n-c [file]: specify your own config file\n\n");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
trace_log_file("/dev/kmsg", argv, envp);
if ( !(unsigned int)hal_check_running() )
{
hal_parse_cmdline(argc, (char **)argv);
hal_init();
while ( 1 )
hal_update();
}
fwrite("Fatal: There is another WR HAL instance running. We can't work together.\n\n", 1uLL, 0x4AuLL, stderr);
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
trace_log_file("/dev/kmsg", argv, envp);
if ( !(unsigned int)hal_check_running() )
{
func12(argc, (char **)argv);
func11();
while ( 1 )
func9();
}
fwrite("Fatal: There is another WR HAL instance running. We can't work together.\n\n", 1uLL, 0x4AuLL, stderr);
return -1;
}

original_code:
int main(int argc, char *argv[])
{
 trace_log_file("/dev/kmsg");
 if(hal_check_running())
 {
  fprintf(stderr, "Fatal: There is another WR HAL instance running. We can't work together.\n\n");
  return -1;
 }
 hal_parse_cmdline(argc, argv);
 hal_init();
 for(;;) hal_update();
 hal_shutdown();
 return 0;
}


decompiled_code:
__int64 nmea_resync_ppsgen()
{
__int64 result; // rax
__int64 v1; // [rsp+8h] [rbp-20h] BYREF
__int64 v2; // [rsp+10h] [rbp-18h] BYREF
char v3[12]; // [rsp+1Ch] [rbp-Ch] BYREF

nmea_read_tai((__int64)"/dev/ttyS2", &v1);
shw_pps_gen_read_time(&v2, v3);
shw_pps_gen_adjust(1LL, v1 - v2);
while ( 1 )
{
result = shw_pps_gen_busy();
if ( (_DWORD)result )
break;
usleep(0x186A0u);
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
__int64 v1; // [rsp+8h] [rbp-20h] BYREF
__int64 v2; // [rsp+10h] [rbp-18h] BYREF
char v3[12]; // [rsp+1Ch] [rbp-Ch] BYREF

func1((__int64)"/dev/ttyS2", &v1);
shw_pps_gen_read_time(&v2, v3);
shw_pps_gen_adjust(1LL, v1 - v2);
while ( 1 )
{
result = shw_pps_gen_busy();
if ( (_DWORD)result )
break;
usleep(0x186A0u);
}
return result;
}

original_code:
void nmea_resync_ppsgen(const char *dev_name)
{
 uint32_t nsec;
 uint64_t cur_tai, new_tai;
 nmea_read_tai("/dev/ttyS2", (int64_t *)&new_tai);
 shw_pps_gen_read_time( &cur_tai, &nsec);
 shw_pps_gen_adjust(0x1, (int64_t) new_tai - (int64_t) cur_tai);
 while(!shw_pps_gen_busy()) usleep(100000);
}

canonical_original_code:
void nmea_resync_ppsgen(char *dev_name)
{
 unsigned int nsec;
 unsigned long cur_tai; unsigned long new_tai;
 nmea_read_tai("/dev/ttyS2", (long *)&new_tai);
 shw_pps_gen_read_time( &cur_tai, &nsec);
 shw_pps_gen_adjust(0x1, (long) new_tai - (long) cur_tai);
 while(!shw_pps_gen_busy()) usleep(100000);
}

variable_types:
nsec
unsigned int


decompiled_code:
__int64 __fastcall htab_count_buckets(int a1)
{
__int64 v1; // rcx
int *v2; // rax
int *v3; // rcx
unsigned int v4; // edx

v1 = 88LL * a1;
v2 = &rtu_htab[v1];
v3 = (int *)((char *)&unk_14CE0 + v1 * 4);
v4 = 0;
do
{
v4 -= (v2[2] == 0) - 1;
v2 += 22;
}
while ( v2 != v3 );
return v4;
}

original_code:
static int htab_count_buckets(struct rtu_addr addr)
{
 int n = 0, i;
 for(i=0; i<4; i++)
  if(rtu_htab[addr.hash][i].valid)
   n++;
 return n;
}

canonical_original_code:
static int htab_count_buckets(struct rtu_addr addr)
{
 int n = 0; int i;
 for(i=0; i<4; i++)
  if(rtu_htab[addr.hash][i].valid)
   n++;
 return n;
}

variable_types: struct rtu_addr { int hash; int bucket; }

variable_types: n int


tail

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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 24);
}
while ( a1 );
return result;
}

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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 24);
}
while ( a1 );
return result;
}

original_code:
static struct hw_req *tail(struct hw_req *head){
    struct hw_req *ptr;
    for(ptr = head; ptr->next; ptr = ptr->next);
    return ptr;
}

variable_types:
head
struct hw_req *

variable_types: struct hw_req { int type; struct fd_handle { struct rtu_addr { int hash; int bucket; } addr; struct filtering_entry { struct rtu_addr addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; } *entry_ptr; } handle; struct hw_req *next; } *

user_defined_types: struct hw_req { int type; struct fd_handle handle; struct hw_req *next; }

user_defined_types: struct fd_handle { struct rtu_addr addr; struct filtering_entry *entry_ptr; }


decompiled_code:
__int64 __fastcall to_mem_addr(__int64 a1)
{
return (unsigned __int16)(32 * a1) | (8 * BYTE4(a1)) & 0x18u;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return (unsigned __int16)(32 * a1) | (8 * BYTE4(a1)) & 0x18u;
}

original_code:
static inline int to_mem_addr(struct rtu_addr addr)
{
    return (( 0x07FF & addr.hash ) << 5 ) | ((0x0003 & addr.bucket) << 3);
}

user_defined_types: struct rtu_addr { int hash; int bucket; }


decompiled_code:
__int64 clean_fd()
{
memset(rtu_htab, 0, (size_t)&unk_2C000);
return rtu_clean_htab();
}

canonical_decompiled_code:
__int64 func3()
{
memset(rtu_htab, 0, (size_t)&unk_2C000);
return rtu_clean_htab();
}

original_code:
static void clean_fd(void)
{
 memset(&rtu_htab, 0, sizeof(rtu_htab));
    rtu_clean_htab();
}

canonical_original_code:
static void clean_fd()
{
 memset(&rtu_htab, 0, sizeof(rtu_htab));
    rtu_clean_htab();
}


decompiled_code:
__int64 clean_vd()
{
_DWORD *v0; // rax

rtu_clean_vlan();
v0 = &unk_BA4;
do
{
*v0 = 1;
v0 += 5;
}
while ( v0 != (_DWORD *)&unk_14B90 );
vlan_tab = -1;
dword_B90 = 0;
byte_B84 = 0;
dword_B88 = 0;
dword_B8C = 0;
byte_B85 = 0;
return rtu_write_vlan_entry(0LL, &vlan_tab, &unk_14B90);
}

canonical_decompiled_code:
__int64 func4()
{
_DWORD *v0; // rax

rtu_clean_vlan();
v0 = &unk_BA4;
do
{
*v0 = 1;
v0 += 5;
}
while ( v0 != (_DWORD *)&unk_14B90 );
vlan_tab = -1;
dword_B90 = 0;
byte_B84 = 0;
dword_B88 = 0;
dword_B8C = 0;
byte_B85 = 0;
return rtu_write_vlan_entry(0LL, &vlan_tab, &unk_14B90);
}

original_code:
static void clean_vd(void)
{
    int i;
    rtu_clean_vlan();
    for(i = 1; i < 4096; i++) {
        vlan_tab[i].drop = 1;
    }
    vlan_tab[0].port_mask = 0xffffffff;
    vlan_tab[0].drop = 0;
    vlan_tab[0].fid = 0;
    vlan_tab[0].has_prio = 0;
    vlan_tab[0].prio_override = 0;
    vlan_tab[0].prio = 0;
    rtu_write_vlan_entry(0, &vlan_tab[0]);
}

canonical_original_code:
static void clean_vd()
{
    int i;
    rtu_clean_vlan();
    for(i = 1; i < 4096; i++) {
        vlan_tab[i].drop = 1;
    }
    vlan_tab[0].port_mask = 0xffffffff;
    vlan_tab[0].drop = 0;
    vlan_tab[0].fid = 0;
    vlan_tab[0].has_prio = 0;
    vlan_tab[0].prio_override = 0;
    vlan_tab[0].prio = 0;
    rtu_write_vlan_entry(0, &vlan_tab[0]);
}


decompiled_code:
_BOOL8 __fastcall mac_equal(const void *a1, const void *a2)
{
return memcmp(a1, a2, 6uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(const void *a1, const void *a2)
{
return memcmp(a1, a2, 6uLL) == 0;
}

original_code:
static inline int mac_equal(uint8_t a[6], uint8_t b[6])
{
    return !memcmp(a, b, 6);
}

canonical_original_code:
static inline int mac_equal(unsigned char a[6], unsigned char b[6])
{
    return !memcmp(a, b, 6);
}

variable_types:
a
unsigned char [6]

variable_types:
b
unsigned char [6]


original_code:
static int htab_search (
        uint8_t mac[6],
        uint8_t fid,
        struct filtering_entry **ent)
{
 int i, j;
 for(i=0; i<((2048)/(4)); i++)
  for(j=0; j<4; j++)
  {
   struct filtering_entry *tmp = &rtu_htab[i][j];
   if(!tmp->valid)
    continue;
   if(mac_equal(tmp->mac, mac) && tmp->fid == fid)
   {
    *ent = tmp;
    return 1;
   }
  }
 return 0;
}

canonical_original_code:
static int htab_search(unsigned char mac[6], unsigned char fid, struct filtering_entry **ent)
{
 int i; int j;
 for(i=0; i<((2048)/(4)); i++)
  for(j=0; j<4; j++)
  {
   struct filtering_entry *tmp = &rtu_htab[i][j];
   if(!tmp->valid)
    continue;
   if(mac_equal(tmp->mac, mac) && tmp->fid == fid)
   {
    *ent = tmp;
    return 1;
   }
  }
 return 0;
}

variable_types:
mac
unsigned char [6]

variable_types:
fid
unsigned char

variable_types: struct filtering_entry { struct rtu_addr { int hash; int bucket; } addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; } **

variable_types: i int

variable_types: j int

user_defined_types: struct filtering_entry { struct rtu_addr addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; }


now

decompiled_code:
time_t now()
{
return time(0LL);
}

canonical_decompiled_code:
time_t func7()
{
return time(0LL);
}

original_code:
static inline
unsigned long now()
{
    return (unsigned long) time(((void *)0));
}

return_type: unsigned long


decompiled_code:
char *rtu_fd_age_update()
{
char *v0; // r14
int i; // er15
const char *v2; // rax
__int64 v3; // rcx
__int64 v4; // rax
char *result; // rax
time_t v6; // [rsp+8h] [rbp-160h]
time_t v7; // [rsp+10h] [rbp-158h]
int v8; // [rsp+2Ch] [rbp-13Ch]
char v9[256]; // [rsp+30h] [rbp-138h] BYREF
char v10[56]; // [rsp+130h] [rbp-38h] BYREF

rtu_read_aging_bitmap(v9);
v6 = now();
v0 = v9;
v8 = 0;
do
{
for ( i = 0; i != 32; ++i )
{
if ( (*(_DWORD *)v0 & (1 << i)) != 0 )
{
if ( dword_14BD0[88 * (unsigned __int16)((i | v8) >> 2) + 22 * ((i | v8) & 3)] )
{
v7 = v6
- (unsigned int)dword_14BB0[88 * (unsigned __int16)((i | v8) >> 2)
+ 22 * (((unsigned __int8)i | (unsigned __int8)v8) & 3)];
v2 = (const char *)mac_to_string(
(char *)&unk_14B94
+ 352 * (unsigned __int16)((i | v8) >> 2)
+ 88 * (((unsigned __int8)i | (unsigned __int8)v8) & 3));
trace_printf(
"rtu_fd.c",
460LL,
16LL,
"Updated htab entry age: mac = %s, hash = %03x:%d, delta_t = %d",
v2,
(unsigned __int16)((i | v8) >> 2),
((unsigned __int8)i | (unsigned __int8)v8) & 3,
v7);
v3 = 11LL * (((unsigned __int8)i | (unsigned __int8)v8) & 3);
v4 = 88LL * (unsigned __int16)((i | v8) >> 2);
dword_14BD4[2 * v3 + v4] = v6 - dword_14BB0[2 * v3 + v4];
dword_14BB0[2 * v3 + v4] = v6;
}
}
}
v8 += 32;
v0 += 4;
result = v10;
}
while ( v0 != v10 );
return result;
}

canonical_decompiled_code:
char *func8()
{
char *v0; // r14
int i; // er15
const char *v2; // rax
__int64 v3; // rcx
__int64 v4; // rax
char *result; // rax
time_t v6; // [rsp+8h] [rbp-160h]
time_t v7; // [rsp+10h] [rbp-158h]
int v8; // [rsp+2Ch] [rbp-13Ch]
char v9[256]; // [rsp+30h] [rbp-138h] BYREF
char v10[56]; // [rsp+130h] [rbp-38h] BYREF

rtu_read_aging_bitmap(v9);
v6 = func7();
v0 = v9;
v8 = 0;
do
{
for ( i = 0; i != 32; ++i )
{
if ( (*(_DWORD *)v0 & (1 << i)) != 0 )
{
if ( dword_14BD0[88 * (unsigned __int16)((i | v8) >> 2) + 22 * ((i | v8) & 3)] )
{
v7 = v6
- (unsigned int)dword_14BB0[88 * (unsigned __int16)((i | v8) >> 2)
+ 22 * (((unsigned __int8)i | (unsigned __int8)v8) & 3)];
v2 = (const char *)mac_to_string(
(char *)&unk_14B94
+ 352 * (unsigned __int16)((i | v8) >> 2)
+ 88 * (((unsigned __int8)i | (unsigned __int8)v8) & 3));
trace_printf(
"rtu_fd.c",
460LL,
16LL,
"Updated htab entry age: mac = %s, hash = %03x:%d, delta_t = %d",
v2,
(unsigned __int16)((i | v8) >> 2),
((unsigned __int8)i | (unsigned __int8)v8) & 3,
v7);
v3 = 11LL * (((unsigned __int8)i | (unsigned __int8)v8) & 3);
v4 = 88LL * (unsigned __int16)((i | v8) >> 2);
dword_14BD4[2 * v3 + v4] = v6 - dword_14BB0[2 * v3 + v4];
dword_14BB0[2 * v3 + v4] = v6;
}
}
}
v8 += 32;
v0 += 4;
result = v10;
}
while ( v0 != v10 );
return result;
}

original_code:
static void rtu_fd_age_update(void)
{
    int i;
    int j;
    uint32_t agr_word;
    uint16_t hash;
    int bucket;
    int bit_cnt;
    unsigned long t;
  uint32_t bitmap[2048 / 32];
  rtu_read_aging_bitmap(bitmap);
    t = now();
    for(i = 0; i < 2048 / 32; i++)
     for(j = 0; j < 32; j++) {
        agr_word = bitmap[i];
    if(agr_word & (1 << j)) {
         bit_cnt = (i << 5) | j;
          hash = bit_cnt >> 2;
          bucket = bit_cnt & 0x03;
      if(!rtu_htab[hash][bucket].dynamic)
       continue;
          trace_printf("rtu_fd.c", 460, 0x10, "Updated htab entry age: mac = %s, hash = %03x:%d, delta_t = %d", mac_to_string(rtu_htab[hash][bucket].mac), hash, bucket, t - rtu_htab[hash][bucket].last_access_t);;
     rtu_htab[hash][bucket].age = t - rtu_htab[hash][bucket].last_access_t;
          rtu_htab[hash][bucket].last_access_t = t;
    }
      }
}

canonical_original_code:
static void rtu_fd_age_update()
{
    int i;
    int j;
    unsigned int agr_word;
    unsigned short hash;
    int bucket;
    int bit_cnt;
    unsigned long t;
  unsigned int bitmap[-1];
  rtu_read_aging_bitmap(bitmap);
    t = now();
    for(i = 0; i < 2048 / 32; i++)
     for(j = 0; j < 32; j++) {
        agr_word = bitmap[i];
    if(agr_word & (1 << j)) {
         bit_cnt = (i << 5) | j;
          hash = bit_cnt >> 2;
          bucket = bit_cnt & 0x03;
      if(!rtu_htab[hash][bucket].dynamic)
       continue;
          trace_printf("rtu_fd.c", 460, 0x10, "Updated htab entry age: mac = %s, hash = %03x:%d, delta_t = %d", mac_to_string(rtu_htab[hash][bucket].mac), hash, bucket, t - rtu_htab[hash][bucket].last_access_t);;
     rtu_htab[hash][bucket].age = t - rtu_htab[hash][bucket].last_access_t;
          rtu_htab[hash][bucket].last_access_t = t;
    }
      }
}

variable_types:
hash
unsigned short

variable_types: bucket int

variable_types:
t
unsigned long

variable_types:
bitmap
unsigned int [-1]


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

result = a1;
*(_DWORD *)a1 = *(_DWORD *)a2;
*(_WORD *)(a1 + 4) = *(_WORD *)(a2 + 4);
return result;
}

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

result = a1;
*(_DWORD *)a1 = *(_DWORD *)a2;
*(_WORD *)(a1 + 4) = *(_WORD *)(a2 + 4);
return result;
}

original_code:
static inline uint8_t* mac_copy(uint8_t dst[6], uint8_t src[6])
{
    return memcpy(dst, src, 6);
}

canonical_original_code:
static inline unsigned char *mac_copy(unsigned char dst[6], unsigned char src[6])
{
    return memcpy(dst, src, 6);
}

variable_types:
dst
unsigned char [6]

variable_types:
src
unsigned char [6]

return_type: unsigned char *


decompiled_code:
__int64 __fastcall hw_request(int a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax

v4 = malloc(0x20uLL);
if ( !v4 )
return 4294967284LL;
*(_DWORD *)v4 = a1;
v4[1] = a2;
v4[2] = a3;
v4[3] = 0LL;
if ( hw_req_list )
*(_QWORD *)(tail((__int64)hw_req_list) + 24) = v4;
else
hw_req_list = v4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax

v4 = malloc(0x20uLL);
if ( !v4 )
return 4294967284LL;
*(_DWORD *)v4 = a1;
v4[1] = a2;
v4[2] = a3;
v4[3] = 0LL;
if ( hw_req_list )
*(_QWORD *)(func1((__int64)hw_req_list) + 24) = v4;
else
hw_req_list = v4;
return 0LL;
}

original_code:
static int hw_request(int type, struct rtu_addr addr, struct filtering_entry *ent)
{
    struct hw_req *req;
    req = (struct hw_req*) malloc(sizeof(struct hw_req));
    if(!req)
        return -12;
    req->type = type;
    req->handle.addr = addr;
    req->handle.entry_ptr = ent;
    req->next = ((void *)0);
    if(!hw_req_list)
        hw_req_list = req;
    else
        tail(hw_req_list)->next = req;
    return 0;
}

canonical_original_code:
static int hw_request(int type, struct rtu_addr addr, struct filtering_entry *ent)
{
    struct hw_req *req;
    req = (struct hw_req *) malloc(sizeof(struct hw_req));
    if(!req)
        return -12;
    req->type = type;
    req->handle.addr = addr;
    req->handle.entry_ptr = ent;
    req->next = ((void *)0);
    if(!hw_req_list)
        hw_req_list = req;
    else
        tail(hw_req_list)->next = req;
    return 0;
}

variable_types: type int

variable_types: struct rtu_addr { int hash; int bucket; }

variable_types: struct filtering_entry { struct rtu_addr addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; } *


decompiled_code:
void *__fastcall delete_htab_entry(__int64 a1)
{
int v1; // ebp
unsigned __int64 v2; // rbx
unsigned __int64 v3; // r13
const char *v4; // rax
char *v5; // r13
void *result; // rax
int *v7; // rbx
__int64 v8; // r13
unsigned __int16 v9; // ax

v1 = htab_count_buckets(a1);
v2 = 352LL * (int)a1;
v3 = v2 + 88 * (a1 >> 32);
v4 = (const char *)mac_to_string((char *)&unk_14B94 + v3);
trace_printf("rtu_fd.c", 540LL, 16LL, "Deleted entry for MAC %s : hash %03x:%d.", v4, (unsigned int)a1, a1 >> 32);
v5 = (char *)rtu_htab + v3;
result = 0LL;
memset(v5, 0, 0x58uLL);
if ( v1 - 1 > SHIDWORD(a1) )
result = memmove(v5, (char *)&unk_14BD8 + 88 * (a1 >> 32) + v2, 88LL * (v1 - HIDWORD(a1) - 1));
if ( v1 > 0 )
{
v7 = &rtu_htab[v2 / 4];
v8 = 0LL;
do
{
v9 = to_mem_addr((v8 << 32) | (unsigned int)a1);
result = (void *)rtu_write_htab_entry(v9, v7, v1 - 1 == (_DWORD)v8);
v8 = (unsigned int)(v8 + 1);
v7 += 22;
}
while ( v1 != (_DWORD)v8 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func11(__int64 a1)
{
int v1; // ebp
unsigned __int64 v2; // rbx
unsigned __int64 v3; // r13
const char *v4; // rax
char *v5; // r13
void *result; // rax
int *v7; // rbx
__int64 v8; // r13
unsigned __int16 v9; // ax

v1 = func0(a1);
v2 = 352LL * (int)a1;
v3 = v2 + 88 * (a1 >> 32);
v4 = (const char *)mac_to_string((char *)&unk_14B94 + v3);
trace_printf("rtu_fd.c", 540LL, 16LL, "Deleted entry for MAC %s : hash %03x:%d.", v4, (unsigned int)a1, a1 >> 32);
v5 = (char *)rtu_htab + v3;
result = 0LL;
memset(v5, 0, 0x58uLL);
if ( v1 - 1 > SHIDWORD(a1) )
result = memmove(v5, (char *)&unk_14BD8 + 88 * (a1 >> 32) + v2, 88LL * (v1 - HIDWORD(a1) - 1));
if ( v1 > 0 )
{
v7 = &rtu_htab[v2 / 4];
v8 = 0LL;
do
{
v9 = func2((v8 << 32) | (unsigned int)a1);
result = (void *)rtu_write_htab_entry(v9, v7, v1 - 1 == (_DWORD)v8);
v8 = (unsigned int)(v8 + 1);
v7 += 22;
}
while ( v1 != (_DWORD)v8 );
}
return result;
}

original_code:
static void delete_htab_entry(struct rtu_addr addr)
{
 int i, n_buckets = htab_count_buckets(addr);
 trace_printf("rtu_fd.c", 540, 0x10, "Deleted entry for MAC %s : hash %03x:%d.", mac_to_string(rtu_htab[addr.hash][addr.bucket].mac), addr.hash, addr.bucket);;
 memset(&rtu_htab[addr.hash][addr.bucket], 0, sizeof(struct filtering_entry));
 if(addr.bucket < n_buckets-1)
  memmove(&rtu_htab[addr.hash][addr.bucket],
      &rtu_htab[addr.hash][addr.bucket+1],
      (n_buckets-addr.bucket-1) * sizeof(struct filtering_entry));
 for(i=0; i<n_buckets; i++)
 {
  struct rtu_addr a;
  a.hash = addr.hash;
  a.bucket = i;
  rtu_write_htab_entry(to_mem_addr(a), &rtu_htab[a.hash][a.bucket], (i == n_buckets-1) ? 1 : 0);
 }
}

canonical_original_code:
static void delete_htab_entry(struct rtu_addr addr)
{
 int i; int n_buckets = htab_count_buckets(addr);
 trace_printf("rtu_fd.c", 540, 0x10, "Deleted entry for MAC %s : hash %03x:%d.", mac_to_string(rtu_htab[addr.hash][addr.bucket].mac), addr.hash, addr.bucket);;
 memset(&rtu_htab[addr.hash][addr.bucket], 0, sizeof(struct filtering_entry));
 if(addr.bucket < n_buckets-1)
  memmove(&rtu_htab[addr.hash][addr.bucket],
      &rtu_htab[addr.hash][addr.bucket+1],
      (n_buckets-addr.bucket-1) * sizeof(struct filtering_entry));
 for(i=0; i<n_buckets; i++)
 {
  struct rtu_addr a;
  a.hash = addr.hash;
  a.bucket = i;
  rtu_write_htab_entry(to_mem_addr(a), &rtu_htab[a.hash][a.bucket], (i == n_buckets-1) ? 1 : 0);
 }
}


decompiled_code:
void rtu_hw_commit()
{
_QWORD *v0; // rbx
unsigned __int16 v1; // ax

v0 = hw_req_list;
if ( hw_req_list )
{
do
{
if ( *(_DWORD *)v0 )
{
if ( *(_DWORD *)v0 == 1 )
delete_htab_entry(v0[1]);
}
else
{
v1 = to_mem_addr(v0[1]);
rtu_write_htab_entry(v1, v0[2], 1LL);
}
v0 = (_QWORD *)v0[3];
}
while ( v0 );
}
}

canonical_decompiled_code:
void func12()
{
_QWORD *v0; // rbx
unsigned __int16 v1; // ax

v0 = hw_req_list;
if ( hw_req_list )
{
do
{
if ( *(_DWORD *)v0 )
{
if ( *(_DWORD *)v0 == 1 )
func11(v0[1]);
}
else
{
v1 = func2(v0[1]);
rtu_write_htab_entry(v1, v0[2], 1LL);
}
v0 = (_QWORD *)v0[3];
}
while ( v0 );
}
}

original_code:
static void rtu_hw_commit(void)
{
    struct hw_req *req;
    for(req = hw_req_list; req; req = req->next){
        switch(req->type){
        case 0:
            rtu_write_htab_entry(to_mem_addr(req->handle.addr), req->handle.entry_ptr, 1);
            break;
        case 1:
            delete_htab_entry(req->handle.addr);
            break;
        }
    }
}

canonical_original_code:
static void rtu_hw_commit()
{
    struct hw_req *req;
    for(req = hw_req_list; req; req = req->next){
        switch(req->type){
        case 0:
            rtu_write_htab_entry(to_mem_addr(req->handle.addr), req->handle.entry_ptr, 1);
            break;
        case 1:
            delete_htab_entry(req->handle.addr);
            break;
        }
    }
}

variable_types: struct hw_req { int type; struct fd_handle { struct rtu_addr { int hash; int bucket; } addr; struct filtering_entry { struct rtu_addr addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; } *entry_ptr; } handle; struct hw_req *next; } *


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

v1 = ptr;
while ( v1 )
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 3);
free(v2);
}
}

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

v1 = ptr;
while ( v1 )
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 3);
free(v2);
}
}

original_code:
static void clean_list(struct hw_req *head){
    struct hw_req *ptr;
    while(head) {
        ptr = head;
        head = head->next;
        free(ptr);
    }
}


decompiled_code:
void rtu_fd_commit()
{
if ( hw_req_list )
{
rtu_hw_commit();
clean_list(hw_req_list);
hw_req_list = 0LL;
}
}

canonical_decompiled_code:
void func14()
{
if ( hw_req_list )
{
func12();
func13(hw_req_list);
hw_req_list = 0LL;
}
}

original_code:
static void rtu_fd_commit(void)
{
    if(!hw_req_list)
        return;
    rtu_hw_commit();
    clean_list(hw_req_list);
    hw_req_list = ((void *)0);
}

canonical_original_code:
static void rtu_fd_commit()
{
    if(!hw_req_list)
        return;
    rtu_hw_commit();
    clean_list(hw_req_list);
    hw_req_list = ((void *)0);
}


decompiled_code:
void rtu_fd_age_out()
{
char *v0; // r13
unsigned int i; // er14
__int64 v2; // r15
const char *v3; // rax
__int64 v4; // rbp
_DWORD *v5; // rbx
char *v6; // r12
time_t v7; // [rsp+8h] [rbp-40h]

v7 = now() - aging_time;
v0 = (char *)&unk_2BEA0;
for ( i = 511; i != -1; --i )
{
v5 = (int *)((char *)rtu_htab + (_QWORD)v0);
v6 = (char *)&unk_14C9C + (_QWORD)v0;
v4 = 3LL;
do
{
if ( v5[68] && v5[86] && ((unsigned int)v5[78] - v7 < 0 || v5[79]) )
{
v2 = (unsigned int)v5[79];
v3 = (const char *)mac_to_string(v6);
trace_printf(
"rtu_fd.c",
516LL,
16LL,
"Deleting htab entry: mac = %s, hash = 0x%x, bucket = %d, forced=%d\n",
v3,
i,
v4,
v2);
hw_request(1, *(_QWORD *)(v6 - 20), (__int64)(v6 - 20));
}
v4 = (unsigned int)(v4 - 1);
v5 -= 22;
v6 -= 88;
}
while ( (_DWORD)v4 != -1 );
v0 -= 352;
}
rtu_fd_commit();
}

canonical_decompiled_code:
void func15()
{
char *v0; // r13
unsigned int i; // er14
__int64 v2; // r15
const char *v3; // rax
__int64 v4; // rbp
_DWORD *v5; // rbx
char *v6; // r12
time_t v7; // [rsp+8h] [rbp-40h]

v7 = func7() - aging_time;
v0 = (char *)&unk_2BEA0;
for ( i = 511; i != -1; --i )
{
v5 = (int *)((char *)rtu_htab + (_QWORD)v0);
v6 = (char *)&unk_14C9C + (_QWORD)v0;
v4 = 3LL;
do
{
if ( v5[68] && v5[86] && ((unsigned int)v5[78] - v7 < 0 || v5[79]) )
{
v2 = (unsigned int)v5[79];
v3 = (const char *)mac_to_string(v6);
trace_printf(
"rtu_fd.c",
516LL,
16LL,
"Deleting htab entry: mac = %s, hash = 0x%x, bucket = %d, forced=%d\n",
v3,
i,
v4,
v2);
func10(1, *(_QWORD *)(v6 - 20), (__int64)(v6 - 20));
}
v4 = (unsigned int)(v4 - 1);
v5 -= 22;
v6 -= 88;
}
while ( (_DWORD)v4 != -1 );
v0 -= 352;
}
func14();
}

original_code:
static void rtu_fd_age_out(void)
{
    int i;
    int j;
    struct filtering_entry *ent;
    unsigned long t;
    t = now() - aging_time;
    for (i = ((2048)/(4)); i-- > 0;) {
        for (j = 4; j-- > 0;) {
            ent = &rtu_htab[i][j];
            if(ent->valid && ent->dynamic && (((long)(ent->last_access_t) - (long)(t) < 0) || ent->force_remove)){
                trace_printf("rtu_fd.c", 516, 0x10, "Deleting htab entry: mac = %s, hash = 0x%x, bucket = %d, forced=%d\n", mac_to_string(ent->mac), i, j, ent->force_remove);;
                hw_request(1, ent->addr, ent);
            }
        }
    }
    rtu_fd_commit();
}

canonical_original_code:
static void rtu_fd_age_out()
{
    int i;
    int j;
    struct filtering_entry *ent;
    unsigned long t;
    t = now() - aging_time;
    for (i = ((2048)/(4)); i-- > 0;) {
        for (j = 4; j-- > 0;) {
            ent = &rtu_htab[i][j];
            if(ent->valid && ent->dynamic && (((long)(ent->last_access_t) - (long)(t) < 0) || ent->force_remove)){
                trace_printf("rtu_fd.c", 516, 0x10, "Deleting htab entry: mac = %s, hash = 0x%x, bucket = %d, forced=%d\n", mac_to_string(ent->mac), i, j, ent->force_remove);;
                hw_request(1, ent->addr, ent);
            }
        }
    }
    rtu_fd_commit();
}

variable_types: struct filtering_entry { struct rtu_addr { int hash; int bucket; } addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; } *


original_code:
int rtu_fd_create_entry(uint8_t mac[6], uint16_t vid, uint32_t port_mask, int dynamic)
{
    struct filtering_entry *ent;
    uint8_t fid;
    int ret = 0;
    uint32_t mask_src, mask_dst;
  struct rtu_addr eaddr;
    pthread_mutex_lock(&fd_mutex);
    if (! vlan_tab[vid].drop) {
        fid = vlan_tab[vid].fid;
    if(htab_search(mac, fid, &ent))
    {
      ;
            mask_dst = port_mask;
            mask_src = 0xFFFFFFFF;
            if ((ent->port_mask_dst != mask_dst) ||
                (ent->port_mask_src != mask_src)) {
                ent->port_mask_dst = mask_dst;
                ent->port_mask_src = mask_src;
                ent->last_access_t = now();
                hw_request(0, ent->addr, ent);
            }
    } else {
      int n_buckets;
      eaddr.hash = rtu_hash(mac, fid);
      n_buckets = htab_count_buckets(eaddr);
      if(n_buckets == 4)
      {
       trace_printf("rtu_fd.c", 237, 2, "Hash %03x has no buckets left.", eaddr.hash);;
       return -12;
      }
      eaddr.bucket = n_buckets;
      ent = &rtu_htab[eaddr.hash][eaddr.bucket];
      ent->addr = eaddr;
      trace_printf("rtu_fd.c", 246, 0x10, "Created new entry for MAC %s : hash %03x:%d.", mac_to_string(mac), eaddr.hash, eaddr.bucket);;
            ent->valid = 1;
            ent->fid = fid;
            ent->port_mask_dst = port_mask;
            ent->port_mask_src = 0xFFFFFFFF;
            ent->dynamic = dynamic;
            ent->last_access_t = now();
            mac_copy(ent->mac, mac);
            hw_request(0, eaddr, ent);
    }
    }
    rtu_fd_commit();
    pthread_mutex_unlock(&fd_mutex);
    return ret;
}

canonical_original_code:
int rtu_fd_create_entry(unsigned char mac[6], unsigned short vid, unsigned int port_mask, int dynamic)
{
    struct filtering_entry *ent;
    unsigned char fid;
    int ret = 0;
    unsigned int mask_src; unsigned int mask_dst;
  struct rtu_addr eaddr;
    pthread_mutex_lock(&fd_mutex);
    if (! vlan_tab[vid].drop) {
        fid = vlan_tab[vid].fid;
    if(htab_search(mac, fid, &ent))
    {
      ;
            mask_dst = port_mask;
            mask_src = 0xFFFFFFFF;
            if ((ent->port_mask_dst != mask_dst) ||
                (ent->port_mask_src != mask_src)) {
                ent->port_mask_dst = mask_dst;
                ent->port_mask_src = mask_src;
                ent->last_access_t = now();
                hw_request(0, ent->addr, ent);
            }
    } else {
      int n_buckets;
      eaddr.hash = rtu_hash(mac, fid);
      n_buckets = htab_count_buckets(eaddr);
      if(n_buckets == 4)
      {
       trace_printf("rtu_fd.c", 237, 2, "Hash %03x has no buckets left.", eaddr.hash);;
       return -12;
      }
      eaddr.bucket = n_buckets;
      ent = &rtu_htab[eaddr.hash][eaddr.bucket];
      ent->addr = eaddr;
      trace_printf("rtu_fd.c", 246, 0x10, "Created new entry for MAC %s : hash %03x:%d.", mac_to_string(mac), eaddr.hash, eaddr.bucket);;
            ent->valid = 1;
            ent->fid = fid;
            ent->port_mask_dst = port_mask;
            ent->port_mask_src = 0xFFFFFFFF;
            ent->dynamic = dynamic;
            ent->last_access_t = now();
            mac_copy(ent->mac, mac);
            hw_request(0, eaddr, ent);
    }
    }
    rtu_fd_commit();
    pthread_mutex_unlock(&fd_mutex);
    return ret;
}

variable_types:
vid
unsigned short

variable_types: dynamic int

variable_types: ret int


decompiled_code:
int __fastcall rtu_fd_set_hash_poly(unsigned __int16 a1)
{
pthread_mutex_lock(&fd_mutex);
rtu_write_hash_poly(a1);
rtu_hash_set_poly(a1);
return pthread_mutex_unlock(&fd_mutex);
}

canonical_decompiled_code:
int __fastcall func17(unsigned __int16 a1)
{
pthread_mutex_lock(&fd_mutex);
rtu_write_hash_poly(a1);
rtu_hash_set_poly(a1);
return pthread_mutex_unlock(&fd_mutex);
}

original_code:
void rtu_fd_set_hash_poly(uint16_t poly)
{
    pthread_mutex_lock(&fd_mutex);
    rtu_write_hash_poly(poly);
    rtu_hash_set_poly(poly);
    pthread_mutex_unlock(&fd_mutex);
}

canonical_original_code:
void rtu_fd_set_hash_poly(unsigned short poly)
{
    pthread_mutex_lock(&fd_mutex);
    rtu_write_hash_poly(poly);
    rtu_hash_set_poly(poly);
    pthread_mutex_unlock(&fd_mutex);
}

variable_types:
poly
unsigned short


decompiled_code:
__int64 __fastcall rtu_fd_init(unsigned __int16 a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // [rsp+0h] [rbp-118h] BYREF

trace_printf("rtu_fd.c", 132LL, 16LL, "clean filtering database.");
clean_fd();
trace_printf("rtu_fd.c", 134LL, 16LL, "clean vlan database.");
clean_vd();
trace_printf("rtu_fd.c", 136LL, 16LL, "clean aging map.");
rtu_read_aging_bitmap(&v4);
trace_printf("rtu_fd.c", 138LL, 16LL, "set aging time [%d].", a2);
aging_time = a2;
v2 = pthread_mutex_init(&fd_mutex, 0LL);
if ( !v2 )
{
trace_printf("rtu_fd.c", 145LL, 16LL, "set hash poly.");
rtu_fd_set_hash_poly(a1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int16 a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // [rsp+0h] [rbp-118h] BYREF

trace_printf("rtu_fd.c", 132LL, 16LL, "clean filtering database.");
func3();
trace_printf("rtu_fd.c", 134LL, 16LL, "clean vlan database.");
func4();
trace_printf("rtu_fd.c", 136LL, 16LL, "clean aging map.");
rtu_read_aging_bitmap(&v4);
trace_printf("rtu_fd.c", 138LL, 16LL, "set aging time [%d].", a2);
aging_time = a2;
v2 = pthread_mutex_init(&fd_mutex, 0LL);
if ( !v2 )
{
trace_printf("rtu_fd.c", 145LL, 16LL, "set hash poly.");
func17(a1);
}
return v2;
}

original_code:
int rtu_fd_init(uint16_t poly, unsigned long aging)
{
    uint32_t bitmap[2048 / 32];
    int err;
    trace_printf("rtu_fd.c", 132, 0x10, "clean filtering database.");;
    clean_fd();
    trace_printf("rtu_fd.c", 134, 0x10, "clean vlan database.");;
    clean_vd();
    trace_printf("rtu_fd.c", 136, 0x10, "clean aging map.");;
    rtu_read_aging_bitmap(bitmap);
    trace_printf("rtu_fd.c", 138, 0x10, "set aging time [%d].", aging);;
    aging_time = aging;
    err = pthread_mutex_init(&fd_mutex, ((void *)0));
    if (err)
        return err;
    trace_printf("rtu_fd.c", 145, 0x10, "set hash poly.");;
    rtu_fd_set_hash_poly(poly);
    return 0;
}

canonical_original_code:
int rtu_fd_init(unsigned short poly, unsigned long aging)
{
    unsigned int bitmap[-1];
    int err;
    trace_printf("rtu_fd.c", 132, 0x10, "clean filtering database.");;
    clean_fd();
    trace_printf("rtu_fd.c", 134, 0x10, "clean vlan database.");;
    clean_vd();
    trace_printf("rtu_fd.c", 136, 0x10, "clean aging map.");;
    rtu_read_aging_bitmap(bitmap);
    trace_printf("rtu_fd.c", 138, 0x10, "set aging time [%d].", aging);;
    aging_time = aging;
    err = pthread_mutex_init(&fd_mutex, ((void *)0));
    if (err)
        return err;
    trace_printf("rtu_fd.c", 145, 0x10, "set hash poly.");;
    rtu_fd_set_hash_poly(poly);
    return 0;
}

variable_types:
aging
unsigned long

variable_types: err int


decompiled_code:
__int64 __fastcall rtu_fd_set_aging_time(__int64 a1)
{
if ( (unsigned __int64)(a1 - 10) > 0xF4236 )
return 4294967274LL;
aging_time = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1)
{
if ( (unsigned __int64)(a1 - 10) > 0xF4236 )
return 4294967274LL;
aging_time = a1;
return 0LL;
}

original_code:
int rtu_fd_set_aging_time(unsigned long t)
{
    if ((t < 10) || (t > 1000000))
        return -22;
    aging_time = t;
    return 0;
}


decompiled_code:
int rtu_fd_flush()
{
rtu_fd_age_update();
pthread_mutex_lock(&fd_mutex);
rtu_fd_age_out();
return pthread_mutex_unlock(&fd_mutex);
}

canonical_decompiled_code:
int func20()
{
func8();
pthread_mutex_lock(&fd_mutex);
func15();
return pthread_mutex_unlock(&fd_mutex);
}

original_code:
void rtu_fd_flush(void)
{
    rtu_fd_age_update();
    pthread_mutex_lock(&fd_mutex);
    rtu_fd_age_out();
    pthread_mutex_unlock(&fd_mutex);
}

canonical_original_code:
void rtu_fd_flush()
{
    rtu_fd_age_update();
    pthread_mutex_lock(&fd_mutex);
    rtu_fd_age_out();
    pthread_mutex_unlock(&fd_mutex);
}


decompiled_code:
void __fastcall rtu_fd_clear_entries_for_port(char a1)
{
__int64 *v1; // r12
_DWORD *v2; // rbx
__int64 *v3; // rbp

v1 = (__int64 *)&unk_409C8;
do
{
v2 = v1 + 11;
v3 = v1 + 44;
do
{
if ( v2[68] && v2[86] )
{
if ( v2[74] == 1 << a1 )
hw_request(1, *v3, (__int64)v3);
else
trace_printf("rtu_fd.c", 489LL, 2LL, "cleaning multicast entries not supported yet...\n");
}
v2 -= 22;
v3 -= 11;
}
while ( v3 != v1 );
v1 -= 44;
}
while ( v1 != (__int64 *)&unk_149C8 );
rtu_fd_commit();
}

original_code:
void rtu_fd_clear_entries_for_port(int dest_port)
{
    int i;
    int j;
    struct filtering_entry *ent;
    for (i = ((2048)/(4)); i-- > 0;) {
        for (j = 4; j-- > 0;) {
            ent = &rtu_htab[i][j];
            if(ent->valid && ent->dynamic) {
       if(ent->port_mask_dst == (1<<dest_port))
                hw_request(1, ent->addr, ent);
              else {
        trace_printf("rtu_fd.c", 489, 2, "cleaning multicast entries not supported yet...\n");;
              }
            }
        }
    }
    rtu_fd_commit();
}

variable_types: i int

variable_types: j int


decompiled_code:
void __fastcall rtu_hash_set_poly(unsigned __int16 a1)
{
hash_poly = (8 * a1) | 0x80000;
}

canonical_decompiled_code:
void __fastcall func1(unsigned __int16 a1)
{
hash_poly = (8 * a1) | 0x80000;
}

original_code:
void rtu_hash_set_poly(uint16_t poly)
{
    hash_poly = ((0x10000 | poly) << 3 );
}

canonical_original_code:
void rtu_hash_set_poly(unsigned short poly)
{
    hash_poly = ((0x10000 | poly) << 3 );
}