bstRemove

decompiled_code:
void __fastcall bstRemove(int a1, __int64 a2)
{
_DWORD *v2; // rax

if ( *(_DWORD *)a2 > a1 )
{
v2 = *(_DWORD **)(a2 + 8);
if ( v2 )
{
if ( *v2 == a1 )
*(_QWORD *)(a2 + 8) = getReplacementNode(*(_QWORD *)(a2 + 8));
}
}
}

original_code:
int bstRemove(int v, bst *t)
{
    bst * replacement;
    bst * temp;
    if(t->value == v)
    {
    }
    if(v < t->value)
    {
        if(t->left == ((void *)0))
            return 1;
        else if(t->left->value == v)
        {
            replacement = getReplacementNode(t->left);
            temp = t->left;
            t->left = replacement;
        }
        else
        {
        }
    }
    else
    {
        if(t->right == ((void *)0))
            return 1;
        else if(t->right->value == v)
        {
            replacement = getReplacementNode(t->right);
        }
        else
        {
        }
    }
}

canonical_original_code:
int bstRemove(int v, struct subtree *t)
{
    struct subtree *replacement;
    struct subtree *temp;
    if(t->value == v)
    {
    }
    if(v < t->value)
    {
        if(t->left == ((void *)0))
            return 1;
        else if(t->left->value == v)
        {
            replacement = getReplacementNode(t->left);
            temp = t->left;
            t->left = replacement;
        }
        else
        {
        }
    }
    else
    {
        if(t->right == ((void *)0))
            return 1;
        else if(t->right->value == v)
        {
            replacement = getReplacementNode(t->right);
        }
        else
        {
        }
    }
}


maxdigit

repo: vslinko-personal/diofant

decompiled_code:
__int64 __fastcall maxdigit(const char *a1)
{
size_t v1; // rax
const char *v2; // rdx
char *v3; // rdi
int v4; // ecx

v1 = strlen(a1);
v2 = a1;
v3 = (char *)&a1[v1];
v4 = 0;
while ( v2 != v3 )
{
if ( v4 < *v2 - 48 )
v4 = *v2 - 48;
++v2;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
size_t v1; // rax
const char *v2; // rdx
char *v3; // rdi
int v4; // ecx

v1 = strlen(a1);
v2 = a1;
v3 = (char *)&a1[v1];
v4 = 0;
while ( v2 != v3 )
{
if ( v4 < *v2 - 48 )
v4 = *v2 - 48;
++v2;
}
return (unsigned int)v4;
}

original_code:
int maxdigit(const char * str) {
  int maxdigit = 0;
  for (int i = 0; i < strlen(str); i++) {
    int c = str[i] - '0';
    if (c > maxdigit) {
      maxdigit = c;
    }
  }
  return maxdigit;
}

canonical_original_code:
int maxdigit(char *str) {
  int maxdigit = 0;
  for (int i = 0; i < strlen(str); i++) {
    int c = str[i] - '0';
    if (c > maxdigit) {
      maxdigit = c;
    }
  }
  return maxdigit;
}

variable_types:
str
char *

variable_types: maxdigit int

variable_types: i int

variable_types: c int

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er14
size_t i; // rbp
char *v6; // r13
char *v7; // r12
int v8; // eax

v3 = (char *)malloc(0x300uLL);
strcpy(v3, "11");
v4 = 8;
do
{
v6 = (char *)malloc(2uLL);
v7 = v3;
v3 = (char *)malloc(0x300uLL);
v8 = maxdigit(v7);
sprintf(v6, "%d", (unsigned int)(v8 + 1));
*v3 = 0;
for ( i = 0LL; strlen(v7) > i; ++i )
{
strcat(v3, v6);
*(_WORD *)&v3[(int)strlen(v3)] = (unsigned __int8)v7[i];
}
strcat(v3, v6);
free(v6);
free(v7);
--v4;
}
while ( v4 );
printf("%c%c\n", (unsigned int)v3[299], (unsigned int)v3[300]);
free(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er14
size_t i; // rbp
char *v6; // r13
char *v7; // r12
int v8; // eax

v3 = (char *)malloc(0x300uLL);
strcpy(v3, "11");
v4 = 8;
do
{
v6 = (char *)malloc(2uLL);
v7 = v3;
v3 = (char *)malloc(0x300uLL);
v8 = func7(v7);
sprintf(v6, "%d", (unsigned int)(v8 + 1));
*v3 = 0;
for ( i = 0LL; strlen(v7) > i; ++i )
{
strcat(v3, v6);
*(_WORD *)&v3[(int)strlen(v3)] = (unsigned __int8)v7[i];
}
strcat(v3, v6);
free(v6);
free(v7);
--v4;
}
while ( v4 );
printf("%c%c\n", (unsigned int)v3[299], (unsigned int)v3[300]);
free(v3);
return 0;
}

original_code:
int main() {
  char * str = malloc(MAXSTRSIZE);
  strcpy(str, "11");
  for (int i = 0; i < 8; i++) {
    char *m = malloc(sizeof(char) * 2);
    char *newstr = malloc(MAXSTRSIZE);
    sprintf(m, "%d", maxdigit(str) + 1);
    newstr[0] = '\0';
    for (int i = 0; i < strlen(str); i++) {
      strcat(newstr, m);
      int l = strlen(newstr);
      newstr[l] = str[i];
      newstr[l + 1] = '\0';
    }
    strcat(newstr, m);
    free(m);
    free(str);
    str = newstr;
  }
  printf("%c%c\n", str[299], str[300]);
  free(str);
  return 0;
}

canonical_original_code:
int main() {
  char *str = malloc(MAXSTRSIZE);
  strcpy(str, "11");
  for (int i = 0; i < 8; i++) {
    char *m = malloc(sizeof(char) * 2);
    char *newstr = malloc(MAXSTRSIZE);
    sprintf(m, "%d", maxdigit(str) + 1);
    newstr[0] = '\0';
    for (int i = 0; i < strlen(str); i++) {
      strcat(newstr, m);
      int l = strlen(newstr);
      newstr[l] = str[i];
      newstr[l + 1] = '\0';
    }
    strcat(newstr, m);
    free(m);
    free(str);
    str = newstr;
  }
  printf("%c%c\n", str[299], str[300]);
  free(str);
  return 0;
}

variable_types:
m
char *

variable_types:
newstr
char *

variable_types: l int


repo: vvy/wmake

decompiled_code:
__int64 __fastcall hash_default_func(char *a1, unsigned int a2)
{
char v2; // dl
unsigned int v3; // eax

v2 = *a1;
if ( *a1 )
{
v3 = 0;
do
{
v3 += v2;
v2 = *++a1;
}
while ( *a1 );
}
else
{
v3 = 0;
}
return v3 % a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, unsigned int a2)
{
char v2; // dl
unsigned int v3; // eax

v2 = *a1;
if ( *a1 )
{
v3 = 0;
do
{
v3 += v2;
v2 = *++a1;
}
while ( *a1 );
}
else
{
v3 = 0;
}
return v3 % a2;
}

original_code:
unsigned hash_default_func(char *s,unsigned size)
{
    unsigned h = 0;
    while(*s != '\0')
    {
        h += *s;
        s++;
    }
    return h%size;
}

canonical_original_code:
unsigned int hash_default_func(char *s, unsigned int size)
{
    unsigned int h = 0;
    while(*s != '\0')
    {
        h += *s;
        s++;
    }
    return h%size;
}

variable_types:
s
char *

variable_types:
size
unsigned int

variable_types:
h
unsigned int

return_type: unsigned int


decompiled_code:
void __fastcall hmap_set_default_node(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 24) = a2;
}

original_code:
void hmap_set_default_node(HMAP* hptr,void *node)
{
    hptr->default_node = node;
    return;
}

canonical_original_code:
void hmap_set_default_node(struct hashmap *hptr, void *node)
{
    hptr->default_node = node;
    return;
}

variable_types: struct hashmap { int size; int num; double factor; struct hash_node { struct hash_node *next; char *key; void *val; unsigned int hval; } **entry; void *default_node; unsigned int (*hash_f)(char *, unsigned int); void (*free_f)(struct hash_node *); } *

variable_types:
node
void *

return_type: void

user_defined_types: struct hashmap { int size; int num; double factor; struct hash_node **entry; void *default_node; unsigned int (*hash_f)(char *, unsigned int); void (*free_f)(struct hash_node *); }


decompiled_code:
__int64 __fastcall hmap_num(__int64 a1)
{
if ( a1 )
return *(unsigned int *)(a1 + 4);
perror("Error: hashmap is NULL.\n");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
if ( a1 )
return *(unsigned int *)(a1 + 4);
perror("Error: hashmap is NULL.\n");
return 0LL;
}

original_code:
int hmap_num(HMAP* hptr)
{
    if(hptr == ((void *)0))
    {
        perror("Error: hashmap is NULL.\n");
        return 0;
    }
    return hptr->num;
}

canonical_original_code:
int hmap_num(struct hashmap *hptr)
{
    if(hptr == ((void *)0))
    {
        perror("Error: hashmap is NULL.\n");
        return 0;
    }
    return hptr->num;
}

user_defined_types: struct hash_node { struct hash_node *next; char *key; void *val; unsigned int hval; }


decompiled_code:
_BOOL8 __fastcall hmap_contain(unsigned int *a1, const char *a2)
{
__int64 **i; // rbx
_BOOL8 result; // rax

if ( a1 )
{
for ( i = *(__int64 ***)(*((_QWORD *)a1 + 2) + 8LL * (*((int (__fastcall **)(const char *, _QWORD))a1 + 4))(a2, *a1));
i;
i = (__int64 **)*i )
{
if ( !strcmp((const char *)i[1], a2) )
break;
}
result = i != 0LL;
}
else
{
perror("Error: hashmap is NULL.\n");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(unsigned int *a1, const char *a2)
{
__int64 **i; // rbx
_BOOL8 result; // rax

if ( a1 )
{
for ( i = *(__int64 ***)(*((_QWORD *)a1 + 2) + 8LL * (*((int (__fastcall **)(const char *, _QWORD))a1 + 4))(a2, *a1));
i;
i = (__int64 **)*i )
{
if ( !strcmp((const char *)i[1], a2) )
break;
}
result = i != 0LL;
}
else
{
perror("Error: hashmap is NULL.\n");
result = 0LL;
}
return result;
}

original_code:
int hmap_contain(HMAP* hptr,char *s)
{
    if(hptr == ((void *)0))
    {
        perror("Error: hashmap is NULL.\n");
        return 0;
    }
    int index = (hptr->hash_f)(s,hptr->size);
    HNODE *head = (hptr->entry)[index];
    while(head!= ((void *)0))
    {
        if(strcmp(head->key,s)!= 0)
            head = head->next;
        else
            break;
    }
    if(head == ((void *)0))
        return 0;
    else
        return 1;
}

canonical_original_code:
int hmap_contain(struct hashmap *hptr, char *s)
{
    if(hptr == ((void *)0))
    {
        perror("Error: hashmap is NULL.\n");
        return 0;
    }
    int index = (hptr->hash_f)(s,hptr->size);
    struct hash_node *head = (hptr->entry)[index];
    while(head!= ((void *)0))
    {
        if(strcmp(head->key,s)!= 0)
            head = head->next;
        else
            break;
    }
    if(head == ((void *)0))
        return 0;
    else
        return 1;
}

variable_types: index int

variable_types: struct hash_node { struct hash_node *next; char *key; void *val; unsigned int hval; } *


original_code:
int hmap_put(HMAP* hptr,char *key,void *val)
{
    int index = (hptr->hash_f)(key,hptr->size);
    HNODE *head = (hptr->entry)[index];
    if(head == ((void *)0))
    {
        (hptr->entry)[index] = malloc(sizeof(HNODE));
        HNODE *p = (hptr->entry)[index];
        p->next = ((void *)0);
        p->key = malloc(sizeof(char) * (strlen(key)+1));
        strcpy(p->key,key);
        p->val = val;
        p->hval = index;
        hptr->num++;
        return 1;
    }
    while(head->next != ((void *)0) && strcmp(head->key,key)!=0)
        head = head->next;
    if(strcmp(head->key,key) == 0)
        head->val = val;
    else
    {
        HNODE *p = malloc(sizeof(HNODE));
        p->next = ((void *)0);
        p->key = malloc(sizeof(char) * (strlen(key)+1));
        strcpy(p->key,key);
        p->val = val;
        p->hval = index;
        head->next = p;
        hptr->num++;
    }
    return 0;
}

canonical_original_code:
int hmap_put(struct hashmap *hptr, char *key, void *val)
{
    int index = (hptr->hash_f)(key,hptr->size);
    struct hash_node *head = (hptr->entry)[index];
    if(head == ((void *)0))
    {
        (hptr->entry)[index] = malloc(sizeof(HNODE));
        struct hash_node *p = (hptr->entry)[index];
        p->next = ((void *)0);
        p->key = malloc(sizeof(char) * (strlen(key)+1));
        strcpy(p->key,key);
        p->val = val;
        p->hval = index;
        hptr->num++;
        return 1;
    }
    while(head->next != ((void *)0) && strcmp(head->key,key)!=0)
        head = head->next;
    if(strcmp(head->key,key) == 0)
        head->val = val;
    else
    {
        struct hash_node *p = malloc(sizeof(HNODE));
        p->next = ((void *)0);
        p->key = malloc(sizeof(char) * (strlen(key)+1));
        strcpy(p->key,key);
        p->val = val;
        p->hval = index;
        head->next = p;
        hptr->num++;
    }
    return 0;
}

variable_types:
key
char *

variable_types:
val
void *


decompiled_code:
void __fastcall hmap_remove(unsigned int *a1, const char *a2)
{
void **v2; // r15
const char **v3; // rbx
const char **v4; // r13
char *v5; // rbp
void (__fastcall *v6)(const char **); // rax

v2 = (void **)(*((_QWORD *)a1 + 2) + 8LL * (*((int (__fastcall **)(const char *, _QWORD))a1 + 4))(a2, *a1));
v3 = (const char **)*v2;
v4 = (const char **)*v2;
if ( *v2 )
{
while ( 1 )
{
v5 = (char *)*v3;
if ( !*v3 )
break;
if ( !strcmp(v3[1], a2) )
goto LABEL_6;
v4 = v3;
v3 = (const char **)v5;
}
if ( strcmp(v3[1], a2) )
goto LABEL_12;
LABEL_6:
if ( v3 == v4 )
*v2 = v5;
else
*v4 = v5;
v6 = (void (__fastcall *)(const char **))*((_QWORD *)a1 + 5);
if ( v6 )
v6(v3);
free(v3);
--a1[1];
}
else
{
LABEL_12:
perror("Error: Not exists.\n");
}
}

canonical_decompiled_code:
void __fastcall func6(unsigned int *a1, const char *a2)
{
void **v2; // r15
const char **v3; // rbx
const char **v4; // r13
char *v5; // rbp
void (__fastcall *v6)(const char **); // rax

v2 = (void **)(*((_QWORD *)a1 + 2) + 8LL * (*((int (__fastcall **)(const char *, _QWORD))a1 + 4))(a2, *a1));
v3 = (const char **)*v2;
v4 = (const char **)*v2;
if ( *v2 )
{
while ( 1 )
{
v5 = (char *)*v3;
if ( !*v3 )
break;
if ( !strcmp(v3[1], a2) )
goto LABEL_6;
v4 = v3;
v3 = (const char **)v5;
}
if ( strcmp(v3[1], a2) )
goto LABEL_12;
LABEL_6:
if ( v3 == v4 )
*v2 = v5;
else
*v4 = v5;
v6 = (void (__fastcall *)(const char **))*((_QWORD *)a1 + 5);
if ( v6 )
v6(v3);
free(v3);
--a1[1];
}
else
{
LABEL_12:
perror("Error: Not exists.\n");
}
}

original_code:
int hmap_remove(HMAP* hptr,char *key)
{
    int index = (hptr->hash_f)(key,hptr->size);
    HNODE *p,*prev;
    p = (hptr->entry)[index];
    if(p == ((void *)0))
    {
        perror("Error: Not exists.\n");
        return 0;
    }
    prev = p;
    while(p->next != ((void *)0) && strcmp(p->key,key)!=0)
    {
        prev = p;
        p = p->next;
    }
    if(strcmp(p->key,key) == 0)
    {
        if(p == prev)
            (hptr->entry)[index] = p->next;
        else
            prev->next = p->next;
        if(hptr->free_f != ((void *)0))
            (hptr->free_f)(p);
        free(p);
        hptr->num--;
    }
    else
    {
        perror("Error: Not exists.\n");
        return 0;
    }
}

canonical_original_code:
int hmap_remove(struct hashmap *hptr, char *key)
{
    int index = (hptr->hash_f)(key,hptr->size);
    struct hash_node *p; struct hash_node *prev;
    p = (hptr->entry)[index];
    if(p == ((void *)0))
    {
        perror("Error: Not exists.\n");
        return 0;
    }
    prev = p;
    while(p->next != ((void *)0) && strcmp(p->key,key)!=0)
    {
        prev = p;
        p = p->next;
    }
    if(strcmp(p->key,key) == 0)
    {
        if(p == prev)
            (hptr->entry)[index] = p->next;
        else
            prev->next = p->next;
        if(hptr->free_f != ((void *)0))
            (hptr->free_f)(p);
        free(p);
        hptr->num--;
    }
    else
    {
        perror("Error: Not exists.\n");
        return 0;
    }
}


decompiled_code:
void __fastcall hmap_free(void *ptr)
{
__int64 v1; // r13
_QWORD *v2; // rbp
_QWORD *v3; // rbx
void (__fastcall *v4)(_QWORD); // rax

if ( *(int *)ptr > 0 )
{
v1 = 0LL;
do
{
v3 = *(_QWORD **)(*((_QWORD *)ptr + 2) + 8 * v1);
while ( v3 )
{
v2 = v3;
v3 = (_QWORD *)*v3;
free((void *)v2[1]);
v4 = (void (__fastcall *)(_QWORD))*((_QWORD *)ptr + 5);
if ( v4 )
v4(v2[2]);
free(v2);
}
++v1;
}
while ( *(_DWORD *)ptr > (int)v1 );
}
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
__int64 v1; // r13
_QWORD *v2; // rbp
_QWORD *v3; // rbx
void (__fastcall *v4)(_QWORD); // rax

if ( *(int *)ptr > 0 )
{
v1 = 0LL;
do
{
v3 = *(_QWORD **)(*((_QWORD *)ptr + 2) + 8 * v1);
while ( v3 )
{
v2 = v3;
v3 = (_QWORD *)*v3;
free((void *)v2[1]);
v4 = (void (__fastcall *)(_QWORD))*((_QWORD *)ptr + 5);
if ( v4 )
v4(v2[2]);
free(v2);
}
++v1;
}
while ( *(_DWORD *)ptr > (int)v1 );
}
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
int hmap_free(HMAP* hptr)
{
    int i;
    HNODE *p,*pnext;
    for(i=0;i<hptr->size;i++)
    {
        p = (hptr->entry)[i];
        while(p!=((void *)0))
        {
            pnext = p->next;
            free(p->key);
            if(hptr->free_f != ((void *)0))
                (hptr->free_f)(p->val);
            free(p);
            p = pnext;
        }
    }
    free(hptr->entry);
    free(hptr);
}

canonical_original_code:
int hmap_free(struct hashmap *hptr)
{
    int i;
    struct hash_node *p; struct hash_node *pnext;
    for(i=0;i<hptr->size;i++)
    {
        p = (hptr->entry)[i];
        while(p!=((void *)0))
        {
            pnext = p->next;
            free(p->key);
            if(hptr->free_f != ((void *)0))
                (hptr->free_f)(p->val);
            free(p);
            p = pnext;
        }
    }
    free(hptr->entry);
    free(hptr);
}


decompiled_code:
int __fastcall hmap_display(unsigned int *a1)
{
__int64 v1; // rax
__int64 v2; // rbp
__int64 **v3; // rbx

LODWORD(v1) = printf("size:%d\n", *a1);
if ( (int)*a1 > 0 )
{
v2 = 0LL;
do
{
v1 = *((_QWORD *)a1 + 2);
v3 = *(__int64 ***)(v1 + 8 * v2);
if ( v3 )
{
printf("%d:", (unsigned int)v2);
do
{
printf("%s ", (const char *)v3[1]);
v3 = (__int64 **)*v3;
}
while ( v3 );
LODWORD(v1) = putchar(10);
}
++v2;
}
while ( (int)*a1 > (int)v2 );
}
return v1;
}

canonical_decompiled_code:
int __fastcall func8(unsigned int *a1)
{
__int64 v1; // rax
__int64 v2; // rbp
__int64 **v3; // rbx

LODWORD(v1) = printf("size:%d\n", *a1);
if ( (int)*a1 > 0 )
{
v2 = 0LL;
do
{
v1 = *((_QWORD *)a1 + 2);
v3 = *(__int64 ***)(v1 + 8 * v2);
if ( v3 )
{
printf("%d:", (unsigned int)v2);
do
{
printf("%s ", (const char *)v3[1]);
v3 = (__int64 **)*v3;
}
while ( v3 );
LODWORD(v1) = putchar(10);
}
++v2;
}
while ( (int)*a1 > (int)v2 );
}
return v1;
}

original_code:
void hmap_display(HMAP *hptr)
{
    int i;
    HNODE *p;
 printf("size:%d\n",hptr->size);
    for(i=0;i<hptr->size;i++)
    {
        p = (hptr->entry)[i];
        if(p == ((void *)0))
            continue;
        printf("%d:",i);
        while(p!=((void *)0))
        {
            printf("%s  ",p->key);
            p=p->next;
        }
        printf("\n");
    }
}

canonical_original_code:
void hmap_display(struct hashmap *hptr)
{
    int i;
    struct hash_node *p;
 printf("size:%d\n",hptr->size);
    for(i=0;i<hptr->size;i++)
    {
        p = (hptr->entry)[i];
        if(p == ((void *)0))
            continue;
        printf("%d:",i);
        while(p!=((void *)0))
        {
            printf("%s  ",p->key);
            p=p->next;
        }
        printf("\n");
    }
}


decompiled_code:
__int64 __fastcall hmap_traverse(__int64 a1, __int64 (__fastcall *a2)(_QWORD *), int a3)
{
__int64 v4; // r12
_QWORD *i; // rbx
__int64 result; // rax

if ( *(int *)a1 > 0 )
{
v4 = 0LL;
do
{
result = *(_QWORD *)(a1 + 16);
for ( i = *(_QWORD **)(result + 8 * v4); i; i = (_QWORD *)*i )
{
if ( a3 )
{
if ( a3 == 1 )
result = a2((_QWORD *)i[2]);
}
else
{
result = a2(i);
}
}
++v4;
}
while ( *(_DWORD *)a1 > (int)v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 (__fastcall *a2)(_QWORD *), int a3)
{
__int64 v4; // r12
_QWORD *i; // rbx
__int64 result; // rax

if ( *(int *)a1 > 0 )
{
v4 = 0LL;
do
{
result = *(_QWORD *)(a1 + 16);
for ( i = *(_QWORD **)(result + 8 * v4); i; i = (_QWORD *)*i )
{
if ( a3 )
{
if ( a3 == 1 )
result = a2((_QWORD *)i[2]);
}
else
{
result = a2(i);
}
}
++v4;
}
while ( *(_DWORD *)a1 > (int)v4 );
}
return result;
}

original_code:
void hmap_traverse(HMAP *hptr,HNODE_FUNC func,int usage)
{
    int i;
    HNODE *p;
    for(i=0;i<hptr->size;i++)
    {
        p = (hptr->entry)[i];
        if(p == ((void *)0))
            continue;
        while(p!=((void *)0))
        {
            if(usage == 0)
                func(p);
            else if(usage == 1)
                func(p->val);
            p=p->next;
        }
    }
}

canonical_original_code:
void hmap_traverse(struct hashmap *hptr, void (*func)(void *), int usage)
{
    int i;
    struct hash_node *p;
    for(i=0;i<hptr->size;i++)
    {
        p = (hptr->entry)[i];
        if(p == ((void *)0))
            continue;
        while(p!=((void *)0))
        {
            if(usage == 0)
                func(p);
            else if(usage == 1)
                func(p->val);
            p=p->next;
        }
    }
}

variable_types:
func
void (*)(void *)

variable_types: usage int


repo: vvy/wshell

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("Hello world!");
return 0;
}

original_code:
int main()
{
    printf("Hello world!\n");
    return 0;
}


repo: vyos/libcap

decompiled_code:
void __fastcall parse_args(int a1, const void **a2, __int64 a3)
{
const void **v3; // rbx
const char *v5; // rbp

if ( a1 > 0 )
{
v3 = a2;
do
{
v5 = (const char *)*v3;
if ( !strcmp((const char *)*v3, "debug") )
{
*(_DWORD *)a3 = 1;
}
else if ( !memcmp(*v3, "config=", 7uLL) )
{
*(_QWORD *)(a3 + 16) = v5 + 7;
}
else
{
pam_log(3, "unknown option; %s", v5);
}
++v3;
}
while ( v3 != &a2[a1] );
}
}

canonical_decompiled_code:
void __fastcall func8(int a1, const void **a2, __int64 a3)
{
const void **v3; // rbx
const char *v5; // rbp

if ( a1 > 0 )
{
v3 = a2;
do
{
v5 = (const char *)*v3;
if ( !strcmp((const char *)*v3, "debug") )
{
*(_DWORD *)a3 = 1;
}
else if ( !memcmp(*v3, "config=", 7uLL) )
{
*(_QWORD *)(a3 + 16) = v5 + 7;
}
else
{
func7(3, "unknown option; %s", v5);
}
++v3;
}
while ( v3 != &a2[a1] );
}
}

original_code:
static void parse_args(int argc, const char **argv, struct pam_cap_s *pcs)
{
    int ctrl=0;
    for (ctrl=0; argc-- > 0; ++argv) {
 if (!strcmp(*argv, "debug")) {
     pcs->debug = 1;
 } else if (!memcmp(*argv, "config=", 7)) {
     pcs->conf_filename = 7 + *argv;
 } else {
     _pam_log(3, "unknown option; %s", *argv);
 }
    }
}

canonical_original_code:
static void parse_args(int argc, char **argv, struct pam_cap_s *pcs)
{
    int ctrl = 0;
    for (ctrl=0; argc-- > 0; ++argv) {
 if (!strcmp(*argv, "debug")) {
     pcs->debug = 1;
 } else if (!memcmp(*argv, "config=", 7)) {
     pcs->conf_filename = 7 + *argv;
 } else {
     _pam_log(3, "unknown option; %s", *argv);
 }
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct pam_cap_s { int debug; char *user; char *conf_filename; } *

variable_types: ctrl int


decompiled_code:
__int64 __fastcall is_member_of(char *s2, char *name)
{
unsigned int v3; // er12
char **v4; // rbx
const char *v5; // rdi
char v7[1032]; // [rsp+0h] [rbp-448h] BYREF
struct group *result; // [rsp+408h] [rbp-40h] BYREF
struct group resultbuf; // [rsp+410h] [rbp-38h] BYREF

result = 0LL;
v3 = 0;
if ( !getgrnam_r(name, &resultbuf, v7, 0x400uLL, &result) )
{
v3 = 0;
if ( result )
{
v4 = result->gr_mem;
v5 = *v4;
if ( *v4 )
{
while ( strcmp(v5, s2) )
{
v5 = *++v4;
if ( !*v4 )
return v3;
}
v3 = 1;
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *s2, char *name)
{
unsigned int v3; // er12
char **v4; // rbx
const char *v5; // rdi
char v7[1032]; // [rsp+0h] [rbp-448h] BYREF
struct group *result; // [rsp+408h] [rbp-40h] BYREF
struct group resultbuf; // [rsp+410h] [rbp-38h] BYREF

result = 0LL;
v3 = 0;
if ( !getgrnam_r(name, &resultbuf, v7, 0x400uLL, &result) )
{
v3 = 0;
if ( result )
{
v4 = result->gr_mem;
v5 = *v4;
if ( *v4 )
{
while ( strcmp(v5, s2) )
{
v5 = *++v4;
if ( !*v4 )
return v3;
}
v3 = 1;
}
}
}
return v3;
}

original_code:
static int is_member_of(const char *user, const char *grnam)
{
    struct group gbuf, *gr = ((void *)0);
    char **m;
    char buf[1024];
    if (getgrnam_r(grnam, &gbuf, buf, sizeof(buf), &gr) != 0 ||
 gr == ((void *)0))
 return 0;
    for (m = gr->gr_mem; *m; ++m)
 if (strcmp(*m, user) == 0)
     return 1;
    return 0;
}

canonical_original_code:
static int is_member_of(char *user, char *grnam)
{
    struct group gbuf; struct group *gr = ((void *)0);
    char **m;
    char buf[1024];
    if (getgrnam_r(grnam, &gbuf, buf, sizeof(buf), &gr) != 0 ||
 gr == ((void *)0))
 return 0;
    for (m = gr->gr_mem; *m; ++m)
 if (strcmp(*m, user) == 0)
     return 1;
    return 0;
}

variable_types:
user
char *

variable_types:
grnam
char *

variable_types: struct group { char *gr_name; char *gr_passwd; unsigned int gr_gid; char **gr_mem; } *

variable_types:
m
char **

variable_types:
buf
char [1024]


repo: vyos/netplug

decompiled_code:
__int64 __fastcall has_meta(_BYTE *a1)
{
char v1; // cl
_BYTE *v2; // rsi
const char *v3; // rdx
char v4; // al

v1 = *a1;
v2 = a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
while ( 2 )
{
v3 = "[]*?";
v4 = 91;
do
{
if ( v4 == v1 )
return (unsigned int)((_DWORD)v2 - (_DWORD)a1);
v4 = *++v3;
}
while ( *v3 );
v1 = *++v2;
if ( *v2 )
continue;
break;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
char v1; // cl
_BYTE *v2; // rsi
const char *v3; // rdx
char v4; // al

v1 = *a1;
v2 = a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
while ( 2 )
{
v3 = "[]*?";
v4 = 91;
do
{
if ( v4 == v1 )
return (unsigned int)((_DWORD)v2 - (_DWORD)a1);
v4 = *++v3;
}
while ( *v3 );
v1 = *++v2;
if ( *v2 )
continue;
break;
}
return 0xFFFFFFFFLL;
}

original_code:
static int
has_meta(char *s)
{
    static const char meta[] = "[]*?";
    for (char *x = s; *x != '\0'; x++) {
        for (const char *m = meta; *m != '\0'; m++) {
            if (*x == *m) {
                return x - s;
            }
        }
    }
    return -1;
}

canonical_original_code:
static int has_meta(char *s)
{
    static char meta[] = "[]*?";
    for (char *x = s; *x != '\0'; x++) {
        for (char *m = meta; *m != '\0'; m++) {
            if (*x == *m) {
                return x - s;
            }
        }
    }
    return -1;
}

variable_types:
s
char *

variable_types:
meta
char []

variable_types:
x
char *

variable_types:
m
char *


decompiled_code:
__int64 __fastcall if_match(char *name)
{
int v1; // edx
__int64 result; // rax
__int64 v3; // rbx

if ( !memo || (v1 = fnmatch(*(const char **)memo, name, 0), result = 1LL, v1) )
{
v3 = pats;
if ( pats )
{
while ( fnmatch(*(const char **)v3, name, 0) )
{
v3 = *(_QWORD *)(v3 + 8);
if ( !v3 )
return 0LL;
}
memo = v3;
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *name)
{
int v1; // edx
__int64 result; // rax
__int64 v3; // rbx

if ( !memo || (v1 = fnmatch(*(const char **)memo, name, 0), result = 1LL, v1) )
{
v3 = pats;
if ( pats )
{
while ( fnmatch(*(const char **)v3, name, 0) )
{
v3 = *(_QWORD *)(v3 + 8);
if ( !v3 )
return 0LL;
}
memo = v3;
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int
if_match(char *name)
{
    struct if_pat *pat;
    if (memo && fnmatch(memo->pat, name, 0) == 0) {
        return 1;
    }
    for (pat = pats; pat != ((void *)0); pat = pat->next) {
        if (fnmatch(pat->pat, name, 0) == 0) {
            memo = pat;
            return 1;
        }
    }
    return 0;
}

variable_types:
name
char *

variable_types: struct if_pat { char *pat; struct if_pat *next; } *


decompiled_code:
size_t __fastcall save_pattern(void *src)
{
size_t result; // rax
int v2; // ebx
_QWORD *v3; // r12
size_t v4; // rbx
void *v5; // rax

result = strlen((const char *)src);
if ( (_DWORD)result )
{
v2 = result;
if ( (unsigned int)fnmatch((const char *)src, "eth0", 0) > 1 )
{
result = 0xFFFFFFFFLL;
}
else
{
v3 = (_QWORD *)xmalloc(16LL);
v4 = v2 + 1;
v5 = (void *)xmalloc(v4);
*v3 = v5;
memcpy(v5, src, v4);
v3[1] = pats;
pats = (__int64)v3;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func2(void *src)
{
size_t result; // rax
int v2; // ebx
_QWORD *v3; // r12
size_t v4; // rbx
void *v5; // rax

result = strlen((const char *)src);
if ( (_DWORD)result )
{
v2 = result;
if ( (unsigned int)fnmatch((const char *)src, "eth0", 0) > 1 )
{
result = 0xFFFFFFFFLL;
}
else
{
v3 = (_QWORD *)xmalloc(16LL);
v4 = v2 + 1;
v5 = (void *)xmalloc(v4);
*v3 = v5;
memcpy(v5, src, v4);
v3[1] = pats;
pats = (__int64)v3;
result = 0LL;
}
}
return result;
}

original_code:
int
save_pattern(char *name)
{
    int len = strlen(name);
    if (len == 0) {
        return 0;
    }
    int x = fnmatch(name, "eth0", 0);
    if (x != 0 && x != 1) {
        return -1;
    }
    struct if_pat *pat = xmalloc(sizeof(*pat));
    pat->pat = xmalloc(len + 1);
    memcpy(pat->pat, name, len + 1);
    pat->next = pats;
    pats = pat;
    return 0;
}

variable_types: len int

variable_types: x int

user_defined_types: struct if_pat { char *pat; struct if_pat *next; }


decompiled_code:
_BOOL8 __fastcall try_probe(__int64 a1)
{
return (unsigned int)run_netplug(a1, "probe") == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1)
{
return (unsigned int)run_netplug(a1, "probe") == 0;
}

original_code:
int
try_probe(char *iface)
{
    return run_netplug(iface, "probe") == 0 ? 1 : 0;
}

variable_types:
iface
char *


decompiled_code:
void probe_interfaces()
{
const char **v0; // r12
char *v1; // r13
unsigned int v2; // ebx
char *v3; // rbp
const char *v4; // rbx
int v5; // eax
int v6; // ebp

v0 = (const char **)pats;
if ( pats )
{
do
{
v4 = *v0;
v5 = has_meta(*v0);
v6 = v5;
if ( v5 == -1 )
{
try_probe((__int64)v4);
}
else if ( v5 )
{
v1 = (char *)xmalloc(v5 + 4);
strncpy(v1, *v0, v6);
v2 = 0;
v3 = &v1[v6];
do
{
sprintf(v3, "%d", v2);
if ( !fnmatch(*v0, v1, 0) )
try_probe((__int64)v1);
++v2;
}
while ( v2 != 16 );
free(v1);
}
else
{
do_log(4LL, "Don't know how to probe for interfaces matching %s", v4);
}
v0 = (const char **)v0[1];
}
while ( v0 );
}
}

canonical_decompiled_code:
void func5()
{
const char **v0; // r12
char *v1; // r13
unsigned int v2; // ebx
char *v3; // rbp
const char *v4; // rbx
int v5; // eax
int v6; // ebp

v0 = (const char **)pats;
if ( pats )
{
do
{
v4 = *v0;
v5 = func0(*v0);
v6 = v5;
if ( v5 == -1 )
{
func4((__int64)v4);
}
else if ( v5 )
{
v1 = (char *)xmalloc(v5 + 4);
strncpy(v1, *v0, v6);
v2 = 0;
v3 = &v1[v6];
do
{
sprintf(v3, "%d", v2);
if ( !fnmatch(*v0, v1, 0) )
func4((__int64)v1);
++v2;
}
while ( v2 != 16 );
free(v1);
}
else
{
do_log(4LL, "Don't know how to probe for interfaces matching %s", v4);
}
v0 = (const char **)v0[1];
}
while ( v0 );
}
}

original_code:
void
probe_interfaces(void)
{
    int nmatch = 0;
    for (struct if_pat *p = pats; p != ((void *)0); p = p->next) {
        int m;
        if ((m = has_meta(p->pat)) == -1) {
            nmatch += try_probe(p->pat);
        }
        else if (m == 0) {
            do_log(4, "Don't know how to probe for interfaces "
                   "matching %s", p->pat);
            continue;
        }
        else {
            char *z = xmalloc(m + 4);
            strncpy(z, p->pat, m);
            for (int i = 0; i < 16; i++) {
                sprintf(z + m, "%d", i);
                if (fnmatch(p->pat, z, 0) == 0) {
                    nmatch += try_probe(z);
                }
            }
            free(z);
        }
    }
    if (nmatch == 0) {
    }
}

canonical_original_code:
void probe_interfaces()
{
    int nmatch = 0;
    for (struct if_pat *p = pats; p != ((void *)0); p = p->next) {
        int m;
        if ((m = has_meta(p->pat)) == -1) {
            nmatch += try_probe(p->pat);
        }
        else if (m == 0) {
            do_log(4, "Don't know how to probe for interfaces "
                   "matching %s", p->pat);
            continue;
        }
        else {
            char *z = xmalloc(m + 4);
            strncpy(z, p->pat, m);
            for (int i = 0; i < 16; i++) {
                sprintf(z + m, "%d", i);
                if (fnmatch(p->pat, z, 0) == 0) {
                    nmatch += try_probe(z);
                }
            }
            free(z);
        }
    }
    if (nmatch == 0) {
    }
}

variable_types: nmatch int

variable_types: m int

variable_types:
z
char *

variable_types: i int


decompiled_code:
__int64 __fastcall handle_interface(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebp
__int64 v5; // rcx
__int64 v6; // rdx
__int64 v7; // rcx
const char *v8; // r12
__int64 v9; // rdi
_BYTE v11[24]; // [rsp+0h] [rbp-1B8h] BYREF
__int64 v12; // [rsp+18h] [rbp-1A0h]

v3 = 0;
if ( (unsigned __int16)(*(_WORD *)(a1 + 4) - 16) <= 1u && (*(_BYTE *)(a1 + 24) & 8) == 0 )
{
v5 = (unsigned int)(*(_DWORD *)a1 - 32);
if ( *(_DWORD *)a1 - 32 < 0 )
{
do_log(3LL, "Malformed netlink packet length", a3, v5);
v3 = -1;
}
else
{
parse_rtattrs(v11, 51LL, a1 + 32, v5);
if ( v12 )
{
v8 = (const char *)(v12 + 4);
v3 = if_match(v12 + 4);
if ( v3 )
{
v9 = if_info_get_interface(a1, v11);
v3 = 0;
if ( v9 )
{
ifsm_flagchange(v9, *(unsigned int *)(a1 + 24));
if_info_update_interface(a1, v11);
}
}
else
{
do_log(7LL, "%s: ignoring event", v8);
}
}
else
{
do_log(3LL, "No interface name", v6, v7);
v3 = -1;
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebp
__int64 v5; // rcx
__int64 v6; // rdx
__int64 v7; // rcx
const char *v8; // r12
__int64 v9; // rdi
_BYTE v11[24]; // [rsp+0h] [rbp-1B8h] BYREF
__int64 v12; // [rsp+18h] [rbp-1A0h]

v3 = 0;
if ( (unsigned __int16)(*(_WORD *)(a1 + 4) - 16) <= 1u && (*(_BYTE *)(a1 + 24) & 8) == 0 )
{
v5 = (unsigned int)(*(_DWORD *)a1 - 32);
if ( *(_DWORD *)a1 - 32 < 0 )
{
do_log(3LL, "Malformed netlink packet length", a3, v5);
v3 = -1;
}
else
{
parse_rtattrs(v11, 51LL, a1 + 32, v5);
if ( v12 )
{
v8 = (const char *)(v12 + 4);
v3 = if_match(v12 + 4);
if ( v3 )
{
v9 = if_info_get_interface(a1, v11);
v3 = 0;
if ( v9 )
{
ifsm_flagchange(v9, *(unsigned int *)(a1 + 24));
if_info_update_interface(a1, v11);
}
}
else
{
do_log(7LL, "%s: ignoring event", v8);
}
}
else
{
do_log(3LL, "No interface name", v6, v7);
v3 = -1;
}
}
}
return v3;
}

original_code:
static int
handle_interface(struct nlmsghdr *hdr, void *arg)
{
    if (hdr->nlmsg_type != RTM_NEWLINK && hdr->nlmsg_type != RTM_DELLINK) {
        return 0;
    }
    struct ifinfomsg *info = ((void*)(((char*)hdr) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) )))));
    int len = hdr->nlmsg_len - ((sizeof(*info)) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) )));
    if (info->ifi_flags & IFF_LOOPBACK) {
        return 0;
    }
    if (len < 0) {
        do_log(3, "Malformed netlink packet length");
        return -1;
    }
    struct rtattr *attrs[(__IFLA_MAX - 1) + 1];
    parse_rtattrs(attrs, (__IFLA_MAX - 1), ((struct rtattr*)(((char*)(info)) + ( ((sizeof(struct ifinfomsg))+4U -1) & ~(4U -1) ))), len);
    if (attrs[IFLA_IFNAME] == ((void *)0)) {
        do_log(3, "No interface name");
        return -1;
    }
    char *name = ((void*)(((char*)(attrs[IFLA_IFNAME])) + (( ((sizeof(struct rtattr))+4U -1) & ~(4U -1) ) + (0))));
    if (!if_match(name)) {
        do_log(7, "%s: ignoring event", name);
        return 0;
    }
    struct if_info *i = if_info_get_interface(hdr, attrs);
    if (i == ((void *)0))
        return 0;
    ifsm_flagchange(i, info->ifi_flags);
    if_info_update_interface(hdr, attrs);
    return 0;
}

canonical_original_code:
static int handle_interface(struct nlmsghdr *hdr, void *arg)
{
    if (hdr->nlmsg_type != RTM_NEWLINK && hdr->nlmsg_type != RTM_DELLINK) {
        return 0;
    }
    struct ifinfomsg *info = ((void*)(((char*)hdr) + ((0) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) )))));
    int len = hdr->nlmsg_len - ((sizeof(*info)) + ((int) ( ((sizeof(struct nlmsghdr))+4U -1) & ~(4U -1) )));
    if (info->ifi_flags & IFF_LOOPBACK) {
        return 0;
    }
    if (len < 0) {
        do_log(3, "Malformed netlink packet length");
        return -1;
    }
    struct rtattr *attrs[-1];
    parse_rtattrs(attrs, (__IFLA_MAX - 1), ((struct rtattr *)(((char *)(info)) + ( ((sizeof(struct ifinfomsg))+4U -1) & ~(4U -1) ))), len);
    if (attrs[IFLA_IFNAME] == ((void *)0)) {
        do_log(3, "No interface name");
        return -1;
    }
    char *name = ((void*)(((char*)(attrs[IFLA_IFNAME])) + (( ((sizeof(struct rtattr))+4U -1) & ~(4U -1) ) + (0))));
    if (!if_match(name)) {
        do_log(7, "%s: ignoring event", name);
        return 0;
    }
    struct if_info *i = if_info_get_interface(hdr, attrs);
    if (i == ((void *)0))
        return 0;
    ifsm_flagchange(i, info->ifi_flags);
    if_info_update_interface(hdr, attrs);
    return 0;
}

variable_types: struct nlmsghdr { unsigned int nlmsg_len; unsigned short nlmsg_type; unsigned short nlmsg_flags; unsigned int nlmsg_seq; unsigned int nlmsg_pid; } *

variable_types:
arg
void *

variable_types: struct ifinfomsg { unsigned char ifi_family; unsigned char __ifi_pad; unsigned short ifi_type; int ifi_index; unsigned int ifi_flags; unsigned int ifi_change; } *

variable_types: struct rtattr { unsigned short rta_len; unsigned short rta_type; } *[-1]

variable_types: struct if_info { struct if_info *next; int index; int type; unsigned int flags; int addr_len; unsigned char addr[8]; char name[16]; enum ifstate { ST_DOWN=0, ST_DOWNANDOUT=1, ST_PROBING=2, ST_PROBING_UP=3, ST_INACTIVE=4, ST_INNING=5, ST_WAIT_IN=6, ST_ACTIVE=7, ST_OUTING=8, ST_INSANE=9 } state; int worker; long lastchange; } *

user_defined_types: struct if_info { struct if_info *next; int index; int type; unsigned int flags; int addr_len; unsigned char addr[8]; char name[16]; enum ifstate state; int worker; long lastchange; }


decompiled_code:
void tidy_pid(void)
{
if ( pid_file )
{
unlink(pid_file);
pid_file = 0LL;
}
}

canonical_decompiled_code:
void func2(void)
{
if ( pid_file )
{
unlink(pid_file);
pid_file = 0LL;
}
}

original_code:
static void
tidy_pid(void)
{
    if (pid_file) {
        unlink(pid_file);
        pid_file = ((void *)0);
    }
}

canonical_original_code:
static void tidy_pid()
{
    if (pid_file) {
        unlink(pid_file);
        pid_file = ((void *)0);
    }
}


decompiled_code:
void __fastcall __noreturn exit_handler(unsigned int a1)
{
tidy_pid();
do_log(3LL, "caught signal %d - exiting", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(unsigned int a1)
{
func2();
do_log(3LL, "caught signal %d - exiting", a1);
exit(1);
}

original_code:
static void
exit_handler(int sig)
{
    tidy_pid();
    do_log(3, "caught signal %d - exiting", sig);
    exit(1);
}

variable_types: sig int


decompiled_code:
int __fastcall child_handler(int a1, __int64 a2)
{
ssize_t v2; // rax
__pid_t buf; // [rsp+8h] [rbp-10h] BYREF
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( a1 != 17 )
_assert_fail("sig == SIGCHLD", "main.c", 0x9Fu, "child_handler");
buf = *(_DWORD *)(a2 + 16);
LODWORD(v2) = waitpid(buf, stat_loc, 0);
if ( *(_DWORD *)(a2 + 16) == (_DWORD)v2 )
{
v2 = write(fd, &buf, 8uLL);
if ( v2 != 8 )
LODWORD(v2) = do_log(3LL, "can't write to child pipe: %m");
}
return v2;
}

canonical_decompiled_code:
int __fastcall func4(int a1, __int64 a2)
{
ssize_t v2; // rax
__pid_t buf; // [rsp+8h] [rbp-10h] BYREF
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( a1 != 17 )
_assert_fail("sig == SIGCHLD", "main.c", 0x9Fu, "child_handler");
buf = *(_DWORD *)(a2 + 16);
LODWORD(v2) = waitpid(buf, stat_loc, 0);
if ( *(_DWORD *)(a2 + 16) == (_DWORD)v2 )
{
v2 = write(fd, &buf, 8uLL);
if ( v2 != 8 )
LODWORD(v2) = do_log(3LL, "can't write to child pipe: %m");
}
return v2;
}

original_code:
static void
child_handler(int sig, siginfo_t *info, void *v)
{
    struct child_exit ce;
    int ret;
    ((void) sizeof ((sig == 17) ? 1 : 0), __extension__ ({ if (sig == 17) ; else __assert_fail ("sig == SIGCHLD", "main.c", 159, __extension__ __PRETTY_FUNCTION__); }));
    ce.pid = info->_sifields._kill.si_pid;
    ret = waitpid(info->_sifields._kill.si_pid, &ce.status, 0);
    if (ret == info->_sifields._kill.si_pid)
        if (write(child_handler_pipe[1], &ce, sizeof(ce)) != sizeof(ce))
     do_log(3, "can't write to child pipe: %m");
}

canonical_original_code:
static void child_handler(int sig, struct siginfo_t *info, void *v)
{
    struct child_exit ce;
    int ret;
    ((void) sizeof ((sig == 17) ? 1 : 0), __extension__ ({ if (sig == 17) ; else __assert_fail ("sig == SIGCHLD", "main.c", 159, __extension__ __PRETTY_FUNCTION__); }));
    ce.pid = info->_sifields._kill.si_pid;
    ret = waitpid(info->_sifields._kill.si_pid, &ce.status, 0);
    if (ret == info->_sifields._kill.si_pid)
        if (write(child_handler_pipe[1], &ce, sizeof(ce)) != sizeof(ce))
     do_log(3, "can't write to child pipe: %m");
}

variable_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *

variable_types:
v
void *

variable_types: struct child_exit { int pid; int status; }

variable_types: ret int

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1, int a2)
{
fprintf(stderr, "Usage: %s [-DFP] [-c config-file] [-i interface] [-p pid-file]\n", a1);
fwrite("\t-D\t\tprint extra debugging messages\n", 1uLL, 0x24uLL, stderr);
fwrite("\t-F\t\trun in foreground (don't become a daemon)\n", 1uLL, 0x2FuLL, stderr);
fwrite("\t-P\t\tdo not autoprobe for interfaces (use with care)\n", 1uLL, 0x35uLL, stderr);
fwrite("\t-c config_file\tread interface patterns from this config file\n", 1uLL, 0x3EuLL, stderr);
fwrite("\t-i interface\tonly handle interfaces matching this pattern\n", 1uLL, 0x3BuLL, stderr);
fwrite("\t-p pid_file\twrite daemon process ID to pid_file\n", 1uLL, 0x31uLL, stderr);
exit(a2);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(const char *a1, int a2)
{
fprintf(stderr, "Usage: %s [-DFP] [-c config-file] [-i interface] [-p pid-file]\n", a1);
fwrite("\t-D\t\tprint extra debugging messages\n", 1uLL, 0x24uLL, stderr);
fwrite("\t-F\t\trun in foreground (don't become a daemon)\n", 1uLL, 0x2FuLL, stderr);
fwrite("\t-P\t\tdo not autoprobe for interfaces (use with care)\n", 1uLL, 0x35uLL, stderr);
fwrite("\t-c config_file\tread interface patterns from this config file\n", 1uLL, 0x3EuLL, stderr);
fwrite("\t-i interface\tonly handle interfaces matching this pattern\n", 1uLL, 0x3BuLL, stderr);
fwrite("\t-p pid_file\twrite daemon process ID to pid_file\n", 1uLL, 0x31uLL, stderr);
exit(a2);
}

original_code:
static void
usage(char *progname, int exitcode)
{
    fprintf(stderr, "Usage: %s [-DFP] [-c config-file] [-i interface] [-p pid-file]\n",
            progname);
    fprintf(stderr, "\t-D\t\t"
            "print extra debugging messages\n");
    fprintf(stderr, "\t-F\t\t"
            "run in foreground (don't become a daemon)\n");
    fprintf(stderr, "\t-P\t\t"
            "do not autoprobe for interfaces (use with care)\n");
    fprintf(stderr, "\t-c config_file\t"
            "read interface patterns from this config file\n");
    fprintf(stderr, "\t-i interface\t"
            "only handle interfaces matching this pattern\n");
    fprintf(stderr, "\t-p pid_file\t"
            "write daemon process ID to pid_file\n");
    exit(exitcode);
}

variable_types:
progname
char *

variable_types: exitcode int


decompiled_code:
__int64 poll_interfaces()
{
unsigned int v1; // eax

if ( sockfd_2 == -1 )
{
v1 = socket(2, 2, 0);
sockfd_2 = v1;
if ( v1 == -1 )
{
do_log(3LL, "can't create interface socket: %m");
exit(1);
}
close_on_exec(v1);
}
return for_each_iface(pollflags_1);
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v1; // eax

if ( sockfd_2 == -1 )
{
v1 = socket(2, 2, 0);
sockfd_2 = v1;
if ( v1 == -1 )
{
do_log(3LL, "can't create interface socket: %m");
exit(1);
}
close_on_exec(v1);
}
return for_each_iface(pollflags_1);
}

original_code:
static void
poll_interfaces(void)
{
    static int sockfd = -1;
    if (sockfd == -1) {
        sockfd = socket(2, SOCK_DGRAM, IPPROTO_IP);
        if (sockfd == -1) {
            do_log(3, "can't create interface socket: %m");
            exit(1);
        }
        close_on_exec(sockfd);
    }
    int pollflags(struct if_info *info) {
        struct ifreq ifr;
        if (!if_match(info->name))
            return 0;
        memcpy(ifr.ifr_ifrn.ifrn_name, info->name, sizeof(ifr.ifr_ifrn.ifrn_name));
        if (ioctl(sockfd, 0x8913, &ifr) < 0) {
     if (debug)
              do_log(3, "%s: can't get flags: %m", info->name);
        } else {
            ifsm_flagchange(info, ifr.ifr_ifru.ifru_flags);
            ifsm_flagpoll(info);
        }
        return 0;
    }
    for_each_iface(pollflags);
}

canonical_original_code:
static void poll_interfaces()
{
    static int sockfd = -1;
    if (sockfd == -1) {
        sockfd = socket(2, SOCK_DGRAM, IPPROTO_IP);
        if (sockfd == -1) {
            do_log(3, "can't create interface socket: %m");
            exit(1);
        }
        close_on_exec(sockfd);
    }
    int pollflags(struct if_info *info) {
        struct ifreq ifr;
        if (!if_match(info->name))
            return 0;
        memcpy(ifr.ifr_ifrn.ifrn_name, info->name, sizeof(ifr.ifr_ifrn.ifrn_name));
        if (ioctl(sockfd, 0x8913, &ifr) < 0) {
     if (debug)
              do_log(3, "%s: can't get flags: %m", info->name);
        } else {
            ifsm_flagchange(info, ifr.ifr_ifru.ifru_flags);
            ifsm_flagpoll(info);
        }
        return 0;
    }
    for_each_iface(pollflags);
}

variable_types: sockfd int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er12
int v5; // er13
int v6; // eax
__int64 v7; // rbx
int result; // eax
int v9; // eax
int v10; // eax
int buf[2]; // [rsp+8h] [rbp-190h] BYREF
struct pollfd fds; // [rsp+10h] [rbp-188h] BYREF
int v13; // [rsp+18h] [rbp-180h]
__int16 v14; // [rsp+1Ch] [rbp-17Ch]
__int16 v15; // [rsp+1Eh] [rbp-17Ah]
struct sigaction v16; // [rsp+20h] [rbp-178h] BYREF
struct sigaction v17; // [rsp+C0h] [rbp-D8h] BYREF

v3 = 1;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "DFPc:hi:p:");
if ( v6 == -1 )
break;
switch ( v6 )
{
case '?':
usage(*argv, 1);
case 'D':
debug = 1;
break;
case 'F':
v5 = 1;
break;
case 'P':
v3 = 0;
break;
case 'c':
read_config(optarg);
v4 = 1;
break;
case 'h':
fprintf(stderr, "netplugd version %s\n", "1.2.9.1");
usage(*argv, 0);
case 'i':
if ( (unsigned int)save_pattern(optarg) == -1 )
{
fprintf(stderr, "Bad pattern for '-i %s'\n", optarg);
exit(1);
}
return result;
case 'p':
pid_file = optarg;
break;
default:
continue;
}
}
if ( !v4 )
read_config("/etc/netplug/netplugd.conf");
if ( getuid() )
do_log(4LL, "This daemon will not work properly unless run by root");
if ( v3 )
probe_interfaces();
memset(&v17.sa_mask, 0, 0x90uLL);
v17.sa_handler = (__sighandler_t)exit_handler;
v17.sa_flags = -1073741824;
if ( sigaction(1, &v17, 0LL) == -1 )
{
do_log(3LL, "can't catch hangup signal: %m");
exit(1);
}
if ( sigaction(2, &v17, 0LL) == -1 )
{
do_log(3LL, "can't catch interrupt signal: %m");
exit(1);
}
if ( sigaction(15, &v17, 0LL) == -1 )
{
do_log(3LL, "can't catch termination signal: %m");
exit(1);
}
if ( !v5 )
{
use_syslog = 1;
openlog("netplugd", 1, 24);
}
if ( pipe(child_handler_pipe) == -1 )
{
do_log(3LL, "can't create pipe: %m");
exit(1);
}
close_on_exec((unsigned int)child_handler_pipe[0]);
close_on_exec((unsigned int)fd);
if ( fcntl(child_handler_pipe[0], 4, 2048LL) == -1 )
{
do_log(3LL, "can't set pipe non-blocking: %m");
exit(1);
}
v16.sa_handler = (__sighandler_t)child_handler;
v16.sa_flags = 268435460;
sigfillset(&v16.sa_mask);
if ( sigaction(17, &v16, 0LL) == -1 )
{
do_log(3LL, "can't set SIGCHLD handler: %m");
exit(1);
}
v7 = (unsigned int)netlink_open();
netlink_request_dump(v7);
netlink_receive_dump((unsigned int)v7, &if_info_save_interface, 0LL);
if ( fcntl(v7, 4, 2048LL) == -1 )
{
do_log(3LL, "can't set socket non-blocking: %m");
exit(1);
}
if ( !v5 )
{
if ( daemon(0, 0) == -1 )
{
do_log(3LL, "daemon: %m");
exit(1);
}
if ( pid_file )
{
atexit(tidy_pid);
write_pid();
}
}
fds.fd = v7;
fds.events = 1;
fds.revents = 0;
v13 = child_handler_pipe[0];
v14 = 1;
v15 = 0;
for_each_iface(poll_flags_0);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
poll_interfaces();
v10 = poll(&fds, 2uLL, -1);
if ( v10 != -1 )
break;
if ( *_errno_location() != 4 )
{
do_log(3LL, "poll failed: %m");
exit(1);
}
}
if ( v10 )
break;
sleep(1u);
}
if ( (fds.revents & 1) != 0 && !(unsigned int)netlink_listen((unsigned int)v7, handle_interface, 0LL) )
break;
if ( (v15 & 1) != 0 )
{
while ( 1 )
{
v9 = read(child_handler_pipe[0], buf, 8uLL);
if ( (unsigned int)(v9 + 1) > 1 && v9 != 8 )
_assert_fail("ret == 0 || ret == -1 || ret == sizeof(ce)", "main.c", 0x17Eu, "main");
if ( v9 != 8 )
break;
ifsm_scriptdone((unsigned int)buf[0], (unsigned int)buf[1]);
}
if ( v9 == -1 && *_errno_location() != 11 )
{
do_log(3LL, "pipe read failed: %m");
exit(1);
}
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er12
int v5; // er13
int v6; // eax
__int64 v7; // rbx
int result; // eax
int v9; // eax
int v10; // eax
int buf[2]; // [rsp+8h] [rbp-190h] BYREF
struct pollfd fds; // [rsp+10h] [rbp-188h] BYREF
int v13; // [rsp+18h] [rbp-180h]
__int16 v14; // [rsp+1Ch] [rbp-17Ch]
__int16 v15; // [rsp+1Eh] [rbp-17Ah]
struct sigaction v16; // [rsp+20h] [rbp-178h] BYREF
struct sigaction v17; // [rsp+C0h] [rbp-D8h] BYREF

v3 = 1;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "DFPc:hi:p:");
if ( v6 == -1 )
break;
switch ( v6 )
{
case '?':
func5(*argv, 1);
case 'D':
debug = 1;
break;
case 'F':
v5 = 1;
break;
case 'P':
v3 = 0;
break;
case 'c':
read_config(optarg);
v4 = 1;
break;
case 'h':
fprintf(stderr, "netplugd version %s\n", "1.2.9.1");
func5(*argv, 0);
case 'i':
if ( (unsigned int)save_pattern(optarg) == -1 )
{
fprintf(stderr, "Bad pattern for '-i %s'\n", optarg);
exit(1);
}
return result;
case 'p':
pid_file = optarg;
break;
default:
continue;
}
}
if ( !v4 )
read_config("/etc/netplug/netplugd.conf");
if ( getuid() )
do_log(4LL, "This daemon will not work properly unless run by root");
if ( v3 )
probe_interfaces();
memset(&v17.sa_mask, 0, 0x90uLL);
v17.sa_handler = (__sighandler_t)exit_handler;
v17.sa_flags = -1073741824;
if ( sigaction(1, &v17, 0LL) == -1 )
{
do_log(3LL, "can't catch hangup signal: %m");
exit(1);
}
if ( sigaction(2, &v17, 0LL) == -1 )
{
do_log(3LL, "can't catch interrupt signal: %m");
exit(1);
}
if ( sigaction(15, &v17, 0LL) == -1 )
{
do_log(3LL, "can't catch termination signal: %m");
exit(1);
}
if ( !v5 )
{
use_syslog = 1;
openlog("netplugd", 1, 24);
}
if ( pipe(child_handler_pipe) == -1 )
{
do_log(3LL, "can't create pipe: %m");
exit(1);
}
close_on_exec((unsigned int)child_handler_pipe[0]);
close_on_exec((unsigned int)fd);
if ( fcntl(child_handler_pipe[0], 4, 2048LL) == -1 )
{
do_log(3LL, "can't set pipe non-blocking: %m");
exit(1);
}
v16.sa_handler = (__sighandler_t)child_handler;
v16.sa_flags = 268435460;
sigfillset(&v16.sa_mask);
if ( sigaction(17, &v16, 0LL) == -1 )
{
do_log(3LL, "can't set SIGCHLD handler: %m");
exit(1);
}
v7 = (unsigned int)netlink_open();
netlink_request_dump(v7);
netlink_receive_dump((unsigned int)v7, &if_info_save_interface, 0LL);
if ( fcntl(v7, 4, 2048LL) == -1 )
{
do_log(3LL, "can't set socket non-blocking: %m");
exit(1);
}
if ( !v5 )
{
if ( daemon(0, 0) == -1 )
{
do_log(3LL, "daemon: %m");
exit(1);
}
if ( pid_file )
{
atexit(tidy_pid);
func6();
}
}
fds.fd = v7;
fds.events = 1;
fds.revents = 0;
v13 = child_handler_pipe[0];
v14 = 1;
v15 = 0;
for_each_iface(poll_flags_0);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
func7();
v10 = poll(&fds, 2uLL, -1);
if ( v10 != -1 )
break;
if ( *_errno_location() != 4 )
{
do_log(3LL, "poll failed: %m");
exit(1);
}
}
if ( v10 )
break;
sleep(1u);
}
if ( (fds.revents & 1) != 0 && !(unsigned int)netlink_listen((unsigned int)v7, handle_interface, 0LL) )
break;
if ( (v15 & 1) != 0 )
{
while ( 1 )
{
v9 = read(child_handler_pipe[0], buf, 8uLL);
if ( (unsigned int)(v9 + 1) > 1 && v9 != 8 )
_assert_fail("ret == 0 || ret == -1 || ret == sizeof(ce)", "main.c", 0x17Eu, "main");
if ( v9 != 8 )
break;
ifsm_scriptdone((unsigned int)buf[0], (unsigned int)buf[1]);
}
if ( v9 == -1 && *_errno_location() != 11 )
{
do_log(3LL, "pipe read failed: %m");
exit(1);
}
}
}
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
    int foreground = 0;
    int cfg_read = 0;
    int probe = 1;
    int c;
    while ((c = getopt(argc, argv, "DFPc:hi:p:")) != (-1)) {
        switch (c) {
        case 'D':
            debug = 1;
            break;
        case 'F':
            foreground = 1;
            break;
        case 'P':
            probe = 0;
            break;
        case 'c':
            read_config(optarg);
            cfg_read = 1;
            break;
        case 'h':
            fprintf(stderr, "netplugd version %s\n", "1.2.9.1");
            usage(argv[0], 0);
            break;
        case 'i':
            if (save_pattern(optarg) == -1) {
                fprintf(stderr, "Bad pattern for '-i %s'\n", optarg);
                exit(1);
            }
            break;
        case 'p':
            pid_file = optarg;
            break;
        case '?':
            usage(argv[0], 1);
        }
    }
    if (!cfg_read) {
        read_config("/etc/netplug" "/netplugd.conf");
    }
    if (getuid() != 0) {
        do_log(4, "This daemon will not work properly unless "
               "run by root");
    }
    if (probe) {
        probe_interfaces();
    }
    struct sigaction act = {
        .__sigaction_handler.sa_handler = exit_handler,
        .sa_flags = 0x80000000 | 0x40000000,
    };
    if (sigaction(1, &act, ((void *)0)) == -1) {
        do_log(3, "can't catch hangup signal: %m");
        exit(1);
    }
    if (sigaction(2, &act, ((void *)0)) == -1) {
        do_log(3, "can't catch interrupt signal: %m");
        exit(1);
    }
    if (sigaction(15, &act, ((void *)0)) == -1) {
        do_log(3, "can't catch termination signal: %m");
        exit(1);
    }
    if (!foreground) {
        use_syslog = 1;
        openlog("netplugd", 0x01, (3<<3));
    }
    if (pipe(child_handler_pipe) == -1) {
        do_log(3, "can't create pipe: %m");
        exit(1);
    }
    close_on_exec(child_handler_pipe[0]);
    close_on_exec(child_handler_pipe[1]);
    if (fcntl(child_handler_pipe[0], 4, 04000) == -1) {
        do_log(3, "can't set pipe non-blocking: %m");
        exit(1);
    }
    struct sigaction sa;
    sa.__sigaction_handler.sa_sigaction = child_handler;
    sa.sa_flags = 0x10000000 | 4;
    sigfillset(&sa.sa_mask);
    if (sigaction(17, &sa, ((void *)0)) == -1) {
        do_log(3, "can't set SIGCHLD handler: %m");
        exit(1);
    }
    int fd = netlink_open();
    netlink_request_dump(fd);
    netlink_receive_dump(fd, if_info_save_interface, ((void *)0));
    if (fcntl(fd, 4, 04000) == -1) {
        do_log(3, "can't set socket non-blocking: %m");
        exit(1);
    }
    if (!foreground) {
 if (daemon(0, 0) == -1) {
     do_log(3, "daemon: %m");
     exit(1);
 }
        if (pid_file) {
            atexit(tidy_pid);
            write_pid();
        }
    }
    struct pollfd fds[] = {
        { fd, 0x001, 0 },
        { child_handler_pipe[0], 0x001, 0 },
    };
    {
        int poll_flags(struct if_info *i) {
            if (if_match(i->name))
                ifsm_flagpoll(i);
            return 0;
        }
        for_each_iface(poll_flags);
    }
    for(;;) {
        int ret;
        poll_interfaces();
        ret = poll(fds, sizeof(fds)/sizeof(fds[0]), -1);
        if (ret == -1) {
            if ((*__errno_location ()) == 4)
                continue;
            do_log(3, "poll failed: %m");
            exit(1);
        }
        if (ret == 0) {
            sleep(1);
            continue;
        }
        if (fds[0].revents & 0x001) {
            if (netlink_listen(fd, handle_interface, ((void *)0)) == 0)
                break;
        }
        if (fds[1].revents & 0x001) {
            int ret;
            struct child_exit ce;
            do {
                ret = read(child_handler_pipe[0], &ce, sizeof(ce));
                ((void) sizeof ((ret == 0 || ret == -1 || ret == sizeof(ce)) ? 1 : 0), __extension__ ({ if (ret == 0 || ret == -1 || ret == sizeof(ce)) ; else __assert_fail ("ret == 0 || ret == -1 || ret == sizeof(ce)", "main.c", 382, __extension__ __PRETTY_FUNCTION__); }));
                if (ret == sizeof(ce))
                    ifsm_scriptdone(ce.pid, ce.status);
                else if (ret == -1 && (*__errno_location ()) != 11) {
                    do_log(3, "pipe read failed: %m");
                    exit(1);
                }
            } while(ret == sizeof(ce));
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: foreground int

variable_types: probe int

variable_types: c int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: fd int

variable_types: struct pollfd { int fd; short events; short revents; } []

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct child_exit { int pid; int status; }


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

result = fcntl(a1, 2, 1LL);
if ( result == -1 )
{
do_log(3, "can't set fd %d to close on exec: %m", a1);
exit(1);
}
return result;
}

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

result = fcntl(a1, 2, 1LL);
if ( result == -1 )
{
func0(3, "can't set fd %d to close on exec: %m", a1);
exit(1);
}
return result;
}

original_code:
void
close_on_exec(int fd)
{
    if (fcntl(fd, 2, 1) == -1) {
        do_log(3, "can't set fd %d to close on exec: %m", fd);
        exit(1);
    }
}


decompiled_code:
__pid_t __fastcall run_netplug_bg(const char *a1, const char *a2)
{
__pid_t result; // eax
unsigned int v3; // eax

result = fork();
if ( result == -1 )
{
do_log(3, "fork: %m");
exit(1);
}
if ( !result )
{
setpgrp();
v3 = getpid();
do_log(6, "%s %s %s -> pid %d", "/etc/netplug.d/netplug", a1, a2, v3);
execl("/etc/netplug.d/netplug", "/etc/netplug.d/netplug", a1, a2, 0LL);
do_log(3, "/etc/netplug.d/netplug: %m");
exit(1);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func2(const char *a1, const char *a2)
{
__pid_t result; // eax
unsigned int v3; // eax

result = fork();
if ( result == -1 )
{
func0(3, "fork: %m");
exit(1);
}
if ( !result )
{
setpgrp();
v3 = getpid();
func0(6, "%s %s %s -> pid %d", "/etc/netplug.d/netplug", a1, a2, v3);
execl("/etc/netplug.d/netplug", "/etc/netplug.d/netplug", a1, a2, 0LL);
func0(3, "/etc/netplug.d/netplug: %m");
exit(1);
}
return result;
}

original_code:
pid_t
run_netplug_bg(char *ifname, char *action)
{
    pid_t pid;
    if ((pid = fork()) == -1) {
        do_log(3, "fork: %m");
        exit(1);
    }
    else if (pid != 0) {
        return pid;
    }
    setpgrp();
    do_log(6, "%s %s %s -> pid %d",
           "/etc/netplug.d" "/netplug", ifname, action, getpid());
    execl("/etc/netplug.d" "/netplug", "/etc/netplug.d" "/netplug", ifname, action, ((void *)0));
    do_log(3, "/etc/netplug.d" "/netplug" ": %m");
    exit(1);
}

canonical_original_code:
int run_netplug_bg(char *ifname, char *action)
{
    int pid;
    if ((pid = fork()) == -1) {
        do_log(3, "fork: %m");
        exit(1);
    }
    else if (pid != 0) {
        return pid;
    }
    setpgrp();
    do_log(6, "%s %s %s -> pid %d",
           "/etc/netplug.d" "/netplug", ifname, action, getpid());
    execl("/etc/netplug.d" "/netplug", "/etc/netplug.d" "/netplug", ifname, action, ((void *)0));
    do_log(3, "/etc/netplug.d" "/netplug" ": %m");
    exit(1);
}

variable_types:
ifname
char *

variable_types:
action
char *

variable_types: pid int


decompiled_code:
__int64 __fastcall run_netplug(const char *a1, const char *a2)
{
__pid_t v2; // eax
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

v2 = run_netplug_bg(a1, a2);
if ( waitpid(v2, &stat_loc, 0) == -1 )
{
do_log(3, "waitpid: %m");
exit(1);
}
result = BYTE1(stat_loc);
if ( (stat_loc & 0x7F) != 0 )
result = -(stat_loc & 0x7F);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, const char *a2)
{
__pid_t v2; // eax
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

v2 = func2(a1, a2);
if ( waitpid(v2, &stat_loc, 0) == -1 )
{
func0(3, "waitpid: %m");
exit(1);
}
result = BYTE1(stat_loc);
if ( (stat_loc & 0x7F) != 0 )
result = -(stat_loc & 0x7F);
return result;
}

original_code:
int
run_netplug(char *ifname, char *action)
{
    pid_t pid = run_netplug_bg(ifname, action);
    int status, ret;
    if ((ret = waitpid(pid, &status, 0)) == -1) {
        do_log(3, "waitpid: %m");
        exit(1);
    }
    return (((status) & 0x7f) == 0) ? (((status) & 0xff00) >> 8) : -((status) & 0x7f);
}

canonical_original_code:
int run_netplug(char *ifname, char *action)
{
    int pid = run_netplug_bg(ifname, action);
    int status; int ret;
    if ((ret = waitpid(pid, &status, 0)) == -1) {
        do_log(3, "waitpid: %m");
        exit(1);
    }
    return (((status) & 0x7f) == 0) ? (((status) & 0xff00) >> 8) : -((status) & 0x7f);
}

variable_types: status int


xmalloc

decompiled_code:
void *__fastcall xmalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( a1 )
{
if ( !result )
{
do_log(3, "malloc: %m");
exit(1);
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( a1 )
{
if ( !result )
{
func0(3, "malloc: %m");
exit(1);
}
}
return result;
}

original_code:
void *
xmalloc(size_t n)
{
    void *x = malloc(n);
    if (n > 0 && x == ((void *)0)) {
        do_log(3, "malloc: %m");
        exit(1);
    }
    return x;
}

canonical_original_code:
void *xmalloc(unsigned long n)
{
    void *x = malloc(n);
    if (n > 0 && x == ((void *)0)) {
        do_log(3, "malloc: %m");
        exit(1);
    }
    return x;
}

variable_types:
n
unsigned long

variable_types:
x
void *

return_type: void *


decompiled_code:
void __fastcall kill_script(__pid_t pgrp)
{
__pid_t v1; // eax
sigset_t v2; // [rsp-80h] [rbp-118h] BYREF
sigset_t set; // [rsp+0h] [rbp-98h] BYREF
int stat_loc[3]; // [rsp+8Ch] [rbp-Ch] BYREF

if ( pgrp != -1 )
{
if ( pgrp <= 0 )
_assert_fail("pid > 0", "lib.c", 0x98u, "kill_script");
sigemptyset(&set);
sigaddset(&set, 17);
sigprocmask(0, &set, &v2);
if ( killpg(pgrp, 15) == -1 )
{
do_log(3, "Can't kill script pgrp %d: %m", (unsigned int)pgrp);
}
else
{
sleep(1u);
v1 = waitpid(pgrp, stat_loc, 1);
if ( v1 == -1 )
{
do_log(3, "Failed to wait for %d: %m?!", (unsigned int)pgrp);
}
else
{
if ( v1 )
goto LABEL_6;
if ( killpg(pgrp, 9) != -1 )
{
v1 = waitpid(pgrp, stat_loc, 0);
LABEL_6:
if ( v1 != pgrp )
_assert_fail("ret == pid", "lib.c", 0xB6u, "kill_script");
goto LABEL_7;
}
do_log(3, "2nd kill %d failed: %m?!", (unsigned int)pgrp);
}
}
LABEL_7:
sigprocmask(2, &v2, 0LL);
}
}

canonical_decompiled_code:
void __fastcall func6(__pid_t pgrp)
{
__pid_t v1; // eax
sigset_t v2; // [rsp-80h] [rbp-118h] BYREF
sigset_t set; // [rsp+0h] [rbp-98h] BYREF
int stat_loc[3]; // [rsp+8Ch] [rbp-Ch] BYREF

if ( pgrp != -1 )
{
if ( pgrp <= 0 )
func5("pid > 0", "lib.c", 0x98u, "kill_script");
sigemptyset(&set);
sigaddset(&set, 17);
sigprocmask(0, &set, &v2);
if ( killpg(pgrp, 15) == -1 )
{
func0(3, "Can't kill script pgrp %d: %m", (unsigned int)pgrp);
}
else
{
sleep(1u);
v1 = waitpid(pgrp, stat_loc, 1);
if ( v1 == -1 )
{
func0(3, "Failed to wait for %d: %m?!", (unsigned int)pgrp);
}
else
{
if ( v1 )
goto LABEL_6;
if ( killpg(pgrp, 9) != -1 )
{
v1 = waitpid(pgrp, stat_loc, 0);
LABEL_6:
if ( v1 != pgrp )
func5("ret == pid", "lib.c", 0xB6u, "kill_script");
goto LABEL_7;
}
func0(3, "2nd kill %d failed: %m?!", (unsigned int)pgrp);
}
}
LABEL_7:
sigprocmask(2, &v2, 0LL);
}
}

original_code:
void
kill_script(pid_t pid)
{
    pid_t ret;
    int status;
    sigset_t mask, origmask;
    if (pid == -1)
        return;
    ((void) sizeof ((pid > 0) ? 1 : 0), __extension__ ({ if (pid > 0) ; else __assert_fail ("pid > 0", "lib.c", 152, __extension__ __PRETTY_FUNCTION__); }));
    sigemptyset(&mask);
    sigaddset(&mask, 17);
    sigprocmask(0, &mask, &origmask);
    if (killpg(pid, 15) == -1) {
        do_log(3, "Can't kill script pgrp %d: %m", pid);
        goto done;
    }
    sleep(1);
    ret = waitpid(pid, &status, 1);
    if (ret == -1) {
        do_log(3, "Failed to wait for %d: %m?!", pid);
        goto done;
    } else if (ret == 0) {
        if (killpg(pid, 9) == -1) {
            do_log(3, "2nd kill %d failed: %m?!", pid);
            goto done;
        }
        ret = waitpid(pid, &status, 0);
    }
    ((void) sizeof ((ret == pid) ? 1 : 0), __extension__ ({ if (ret == pid) ; else __assert_fail ("ret == pid", "lib.c", 182, __extension__ __PRETTY_FUNCTION__); }));
 done:
    sigprocmask(2, &origmask, ((void *)0));
}

canonical_original_code:
void kill_script(int pid)
{
    int ret;
    int status;
    struct __sigset_t mask; struct __sigset_t origmask;
    if (pid == -1)
        return;
    ((void) sizeof ((pid > 0) ? 1 : 0), __extension__ ({ if (pid > 0) ; else __assert_fail ("pid > 0", "lib.c", 152, __extension__ __PRETTY_FUNCTION__); }));
    sigemptyset(&mask);
    sigaddset(&mask, 17);
    sigprocmask(0, &mask, &origmask);
    if (killpg(pid, 15) == -1) {
        do_log(3, "Can't kill script pgrp %d: %m", pid);
        goto done;
    }
    sleep(1);
    ret = waitpid(pid, &status, 1);
    if (ret == -1) {
        do_log(3, "Failed to wait for %d: %m?!", pid);
        goto done;
    } else if (ret == 0) {
        if (killpg(pid, 9) == -1) {
            do_log(3, "2nd kill %d failed: %m?!", pid);
            goto done;
        }
        ret = waitpid(pid, &status, 0);
    }
    ((void) sizeof ((ret == pid) ? 1 : 0), __extension__ ({ if (ret == pid) ; else __assert_fail ("ret == pid", "lib.c", 182, __extension__ __PRETTY_FUNCTION__); }));
 done:
    sigprocmask(2, &origmask, ((void *)0));
}

variable_types: ret int

variable_types: struct __sigset_t { unsigned long __val[-1]; }


statename

decompiled_code:
const char *__fastcall statename(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 0:
result = "DOWN";
break;
case 1:
result = "DOWNANDOUT";
break;
case 2:
result = "PROBING";
break;
case 3:
result = "PROBING_UP";
break;
case 4:
result = "INACTIVE";
break;
case 5:
result = "INNING";
break;
case 6:
result = "WAIT_IN";
break;
case 7:
result = "ACTIVE";
break;
case 8:
result = "OUTING";
break;
case 9:
result = "INSANE";
break;
default:
result = (const char *)&unk_980;
break;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 0:
result = "DOWN";
break;
case 1:
result = "DOWNANDOUT";
break;
case 2:
result = "PROBING";
break;
case 3:
result = "PROBING_UP";
break;
case 4:
result = "INACTIVE";
break;
case 5:
result = "INNING";
break;
case 6:
result = "WAIT_IN";
break;
case 7:
result = "ACTIVE";
break;
case 8:
result = "OUTING";
break;
case 9:
result = "INSANE";
break;
default:
result = (const char *)&unk_980;
break;
}
return result;
}

original_code:
static const char *
statename(enum ifstate s)
{
    switch(s) {
        case ST_DOWN: return "DOWN";
        case ST_DOWNANDOUT: return "DOWNANDOUT";
        case ST_PROBING: return "PROBING";
        case ST_PROBING_UP: return "PROBING_UP";
        case ST_INACTIVE: return "INACTIVE";
        case ST_INNING: return "INNING";
        case ST_WAIT_IN: return "WAIT_IN";
        case ST_ACTIVE: return "ACTIVE";
        case ST_OUTING: return "OUTING";
        case ST_INSANE: return "INSANE";
    default: return "???";
    }
}

canonical_original_code:
static char *statename(enum ifstate s)
{
    switch(s) {
        case ST_DOWN: return "DOWN";
        case ST_DOWNANDOUT: return "DOWNANDOUT";
        case ST_PROBING: return "PROBING";
        case ST_PROBING_UP: return "PROBING_UP";
        case ST_INACTIVE: return "INACTIVE";
        case ST_INNING: return "INNING";
        case ST_WAIT_IN: return "WAIT_IN";
        case ST_ACTIVE: return "ACTIVE";
        case ST_OUTING: return "OUTING";
        case ST_INSANE: return "INSANE";
    default: return "???";
    }
}

variable_types: enum ifstate { ST_DOWN=0, ST_DOWNANDOUT=1, ST_PROBING=2, ST_PROBING_UP=3, ST_INACTIVE=4, ST_INNING=5, ST_WAIT_IN=6, ST_ACTIVE=7, ST_OUTING=8, ST_INSANE=9 }

return_type: char *


decompiled_code:
char *__fastcall flags_str(char *s, unsigned int a2)
{
const char *v3; // rbx
char *v4; // r12
int v5; // eax

*s = 0;
v3 = (const char *)&flags_2;
v4 = s;
do
{
v5 = *((_DWORD *)v3 + 2);
if ( (a2 & v5) != 0 )
{
a2 &= ~v5;
v4 += sprintf(v4, "%s,", *(const char **)v3);
}
v3 += 16;
}
while ( v3 != "ifsm_flagchange" );
if ( a2 )
v4 += sprintf(v4, "%x,", a2);
if ( v4 != s )
*(v4 - 1) = 0;
return s;
}

canonical_decompiled_code:
char *__fastcall func2(char *s, unsigned int a2)
{
const char *v3; // rbx
char *v4; // r12
int v5; // eax

*s = 0;
v3 = (const char *)&flags_2;
v4 = s;
do
{
v5 = *((_DWORD *)v3 + 2);
if ( (a2 & v5) != 0 )
{
a2 &= ~v5;
v4 += sprintf(v4, "%s,", *(const char **)v3);
}
v3 += 16;
}
while ( v3 != "ifsm_flagchange" );
if ( a2 )
v4 += sprintf(v4, "%x,", a2);
if ( v4 != s )
*(v4 - 1) = 0;
return s;
}

original_code:
static const char *
flags_str(char *buf, unsigned int fl)
{
    static struct flag {
        const char *name;
        unsigned int flag;
    } flags[] = {
        { "UP", IFF_UP },
        { "BROADCAST", IFF_BROADCAST },
        { "DEBUG", IFF_DEBUG },
        { "LOOPBACK", IFF_LOOPBACK },
        { "POINTOPOINT", IFF_POINTOPOINT },
        { "NOTRAILERS", IFF_NOTRAILERS },
        { "RUNNING", IFF_RUNNING },
        { "NOARP", IFF_NOARP },
        { "PROMISC", IFF_PROMISC },
        { "ALLMULTI", IFF_ALLMULTI },
        { "MASTER", IFF_MASTER },
        { "SLAVE", IFF_SLAVE },
        { "MULTICAST", IFF_MULTICAST },
    };
    char *cp = buf;
    *cp = '\0';
    for(int i = 0; i < sizeof(flags)/sizeof(*flags); i++) {
        if (fl & flags[i].flag) {
            fl &= ~flags[i].flag;
            cp += sprintf(cp, "%s,", flags[i].name);
        }
    }
    if (fl != 0)
        cp += sprintf(cp, "%x,", fl);
    if (cp != buf)
        cp[-1] = '\0';
    return buf;
}

canonical_original_code:
static char *flags_str(char *buf, unsigned int fl)
{
    static struct flag flags[] = {
        { "UP", IFF_UP },
        { "BROADCAST", IFF_BROADCAST },
        { "DEBUG", IFF_DEBUG },
        { "LOOPBACK", IFF_LOOPBACK },
        { "POINTOPOINT", IFF_POINTOPOINT },
        { "NOTRAILERS", IFF_NOTRAILERS },
        { "RUNNING", IFF_RUNNING },
        { "NOARP", IFF_NOARP },
        { "PROMISC", IFF_PROMISC },
        { "ALLMULTI", IFF_ALLMULTI },
        { "MASTER", IFF_MASTER },
        { "SLAVE", IFF_SLAVE },
        { "MULTICAST", IFF_MULTICAST },
    };
    char *cp = buf;
    *cp = '\0';
    for(int i = 0; i < sizeof(flags)/sizeof(*flags); i++) {
        if (fl & flags[i].flag) {
            fl &= ~flags[i].flag;
            cp += sprintf(cp, "%s,", flags[i].name);
        }
    }
    if (fl != 0)
        cp += sprintf(cp, "%x,", fl);
    if (cp != buf)
        cp[-1] = '\0';
    return buf;
}

variable_types:
buf
char *

variable_types:
fl
unsigned int

variable_types: struct flag { char *name; unsigned int flag; } []

variable_types:
cp
char *

variable_types: i int


decompiled_code:
void __fastcall for_each_iface(unsigned int (__fastcall *a1)(_QWORD *))
{
__int64 *v1; // r12
_QWORD *v2; // rbx

v1 = &if_info;
while ( 1 )
{
v2 = (_QWORD *)*v1;
if ( *v1 )
break;
LABEL_5:
if ( ++v1 == (__int64 *)&unk_980 )
return;
}
while ( !a1(v2) )
{
v2 = (_QWORD *)*v2;
if ( !v2 )
goto LABEL_5;
}
}

canonical_decompiled_code:
void __fastcall func3(unsigned int (__fastcall *a1)(_QWORD *))
{
__int64 *v1; // r12
_QWORD *v2; // rbx

v1 = &if_info;
while ( 1 )
{
v2 = (_QWORD *)*v1;
if ( *v1 )
break;
LABEL_5:
if ( ++v1 == (__int64 *)&unk_980 )
return;
}
while ( !a1(v2) )
{
v2 = (_QWORD *)*v2;
if ( !v2 )
goto LABEL_5;
}
}

original_code:
void
for_each_iface(int (*func)(struct if_info *))
{
    for(int i = 0; i < 16; i++) {
        for(struct if_info *info = if_info[i]; info != ((void *)0); info = info->next) {
            if ((*func)(info))
                return;
        }
    }
}

variable_types: int (*)(struct if_info { struct if_info *next; int index; int type; unsigned int flags; int addr_len; unsigned char addr[8]; char name[16]; enum ifstate { ST_DOWN=0, ST_DOWNANDOUT=1, ST_PROBING=2, ST_PROBING_UP=3, ST_INACTIVE=4, ST_INNING=5, ST_WAIT_IN=6, ST_ACTIVE=7, ST_OUTING=8, ST_INSANE=9 } state; int worker; long lastchange; } *)


decompiled_code:
__int64 __fastcall ifsm_flagpoll(__int64 a1)
{
unsigned int v2; // ebp
__int64 result; // rax
int v4; // edi
const char *v5; // r12
const char *v6; // rax

v2 = *(_DWORD *)(a1 + 48);
result = v2;
switch ( v2 )
{
case 0u:
if ( (*(_BYTE *)(a1 + 16) & 0x41) != 0 )
goto LABEL_3;
break;
case 4u:
LABEL_3:
result = *(unsigned int *)(a1 + 16);
if ( (result & 1) == 0 )
{
if ( *(_DWORD *)(a1 + 52) != -1 )
_assert_fail("info->worker == -1", "if_info.c", 0x7Au, "ifsm_flagpoll");
result = run_netplug_bg(a1 + 32, "probe");
*(_DWORD *)(a1 + 52) = result;
*(_DWORD *)(a1 + 48) = 2;
goto LABEL_6;
}
if ( (result & 0x40) != 0 )
{
if ( *(_DWORD *)(a1 + 52) != -1 )
_assert_fail("info->worker == -1", "if_info.c", 0x7Eu, "ifsm_flagpoll");
result = run_netplug_bg(a1 + 32, "in");
*(_DWORD *)(a1 + 52) = result;
*(_DWORD *)(a1 + 48) = 5;
goto LABEL_6;
}
break;
case 5u:
if ( (*(_BYTE *)(a1 + 16) & 0x40) == 0 )
{
*(_DWORD *)(a1 + 48) = 6;
goto LABEL_15;
}
break;
case 7u:
if ( (*(_BYTE *)(a1 + 16) & 0x40) == 0 )
{
if ( *(_DWORD *)(a1 + 52) != -1 )
_assert_fail("info->worker == -1", "if_info.c", 0x91u, "ifsm_flagpoll");
*(_DWORD *)(a1 + 52) = run_netplug_bg(a1 + 32, "out");
*(_DWORD *)(a1 + 48) = 8;
goto LABEL_15;
}
break;
case 8u:
if ( (*(_BYTE *)(a1 + 16) & 1) == 0 )
{
*(_DWORD *)(a1 + 48) = 1;
LABEL_15:
v4 = *(_DWORD *)(a1 + 48);
goto LABEL_16;
}
break;
default:
LABEL_6:
v4 = *(_DWORD *)(a1 + 48);
if ( v4 != v2 )
{
LABEL_16:
v5 = statename(v4);
v6 = statename(v2);
result = do_log(7LL, "ifsm_flagpoll %s: moved from state %s to %s", (const char *)(a1 + 32), v6, v5);
}
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
unsigned int v2; // ebp
__int64 result; // rax
int v4; // edi
const char *v5; // r12
const char *v6; // rax

v2 = *(_DWORD *)(a1 + 48);
result = v2;
switch ( v2 )
{
case 0u:
if ( (*(_BYTE *)(a1 + 16) & 0x41) != 0 )
goto LABEL_3;
break;
case 4u:
LABEL_3:
result = *(unsigned int *)(a1 + 16);
if ( (result & 1) == 0 )
{
if ( *(_DWORD *)(a1 + 52) != -1 )
_assert_fail("info->worker == -1", "if_info.c", 0x7Au, "ifsm_flagpoll");
result = run_netplug_bg(a1 + 32, "probe");
*(_DWORD *)(a1 + 52) = result;
*(_DWORD *)(a1 + 48) = 2;
goto LABEL_6;
}
if ( (result & 0x40) != 0 )
{
if ( *(_DWORD *)(a1 + 52) != -1 )
_assert_fail("info->worker == -1", "if_info.c", 0x7Eu, "ifsm_flagpoll");
result = run_netplug_bg(a1 + 32, "in");
*(_DWORD *)(a1 + 52) = result;
*(_DWORD *)(a1 + 48) = 5;
goto LABEL_6;
}
break;
case 5u:
if ( (*(_BYTE *)(a1 + 16) & 0x40) == 0 )
{
*(_DWORD *)(a1 + 48) = 6;
goto LABEL_15;
}
break;
case 7u:
if ( (*(_BYTE *)(a1 + 16) & 0x40) == 0 )
{
if ( *(_DWORD *)(a1 + 52) != -1 )
_assert_fail("info->worker == -1", "if_info.c", 0x91u, "ifsm_flagpoll");
*(_DWORD *)(a1 + 52) = run_netplug_bg(a1 + 32, "out");
*(_DWORD *)(a1 + 48) = 8;
goto LABEL_15;
}
break;
case 8u:
if ( (*(_BYTE *)(a1 + 16) & 1) == 0 )
{
*(_DWORD *)(a1 + 48) = 1;
LABEL_15:
v4 = *(_DWORD *)(a1 + 48);
goto LABEL_16;
}
break;
default:
LABEL_6:
v4 = *(_DWORD *)(a1 + 48);
if ( v4 != v2 )
{
LABEL_16:
v5 = func0(v4);
v6 = func0(v2);
result = do_log(7LL, "ifsm_flagpoll %s: moved from state %s to %s", (const char *)(a1 + 32), v6, v5);
}
break;
}
return result;
}

original_code:
void
ifsm_flagpoll(struct if_info *info)
{
    enum ifstate state = info->state;
    switch(info->state) {
    case ST_DOWN:
        if ((info->flags & (IFF_UP|IFF_RUNNING)) == 0)
            break;
    case ST_INACTIVE:
        if (!(info->flags & IFF_UP)) {
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 122, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "probe");
            info->state = ST_PROBING;
        } else if (info->flags & IFF_RUNNING) {
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 126, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "in");
            info->state = ST_INNING;
        }
        break;
    case ST_PROBING:
    case ST_PROBING_UP:
    case ST_WAIT_IN:
    case ST_DOWNANDOUT:
        break;
    case ST_INNING:
        if (!(info->flags & IFF_RUNNING))
            info->state = ST_WAIT_IN;
        break;
    case ST_ACTIVE:
        if (!(info->flags & IFF_RUNNING)) {
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 145, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "out");
            info->state = ST_OUTING;
        }
        break;
    case ST_OUTING:
        if (!(info->flags & IFF_UP))
            info->state = ST_DOWNANDOUT;
 break;
    case ST_INSANE:
        break;
    }
    if (info->state != state)
        do_log(7, "ifsm_flagpoll %s: moved from state %s to %s",
               info->name, statename(state), statename(info->state));
}

variable_types: struct if_info { struct if_info *next; int index; int type; unsigned int flags; int addr_len; unsigned char addr[8]; char name[16]; enum ifstate { ST_DOWN=0, ST_DOWNANDOUT=1, ST_PROBING=2, ST_PROBING_UP=3, ST_INACTIVE=4, ST_INNING=5, ST_WAIT_IN=6, ST_ACTIVE=7, ST_OUTING=8, ST_INSANE=9 } state; int worker; long lastchange; } *


original_code:
void
ifsm_flagchange(struct if_info *info, unsigned int newflags)
{
    unsigned int changed = (info->flags ^ newflags) & (IFF_RUNNING | IFF_UP);
    if (changed == 0)
        return;
    char buf1[512], buf2[512];
    do_log(6, "%s: state %s flags 0x%08x %s -> 0x%08x %s", info->name,
           statename(info->state),
           info->flags, flags_str(buf1, info->flags),
           newflags, flags_str(buf2, newflags));
    if (0 ) {
        info->state = ST_INSANE;
    }
    if (changed & IFF_UP) {
        if (newflags & IFF_UP) {
            switch(info->state) {
            case ST_DOWN:
                info->state = ST_INACTIVE;
                break;
            case ST_PROBING:
                info->state = ST_PROBING_UP;
                break;
            default:
                do_log(3, "%s: unexpected state %s for UP", info->name, statename(info->state));
                exit(1);
            }
        } else {
            switch(info->state) {
            case ST_OUTING:
                info->state = ST_DOWNANDOUT;
                break;
            case ST_DOWN:
                break;
     case ST_PROBING:
  break;
     case ST_PROBING_UP:
  info->state = ST_PROBING;
  break;
            default:
                kill_script(info->worker);
                info->state = ST_PROBING;
                info->worker = run_netplug_bg(info->name, "probe");
            }
        }
    }
    if (changed & IFF_RUNNING) {
        switch(info->state) {
        case ST_INACTIVE:
            ((void) sizeof ((!(info->flags & IFF_RUNNING)) ? 1 : 0), __extension__ ({ if (!(info->flags & IFF_RUNNING)) ; else __assert_fail ("!(info->flags & IFF_RUNNING)", "if_info.c", 235, __extension__ __PRETTY_FUNCTION__); }));
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 236, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "in");
            info->state = ST_INNING;
            break;
        case ST_INNING:
            ((void) sizeof ((info->flags & IFF_RUNNING) ? 1 : 0), __extension__ ({ if (info->flags & IFF_RUNNING) ; else __assert_fail ("info->flags & IFF_RUNNING", "if_info.c", 243, __extension__ __PRETTY_FUNCTION__); }));
            info->state = ST_WAIT_IN;
            break;
        case ST_WAIT_IN:
            break;
        case ST_ACTIVE:
            ((void) sizeof ((info->flags & IFF_RUNNING) ? 1 : 0), __extension__ ({ if (info->flags & IFF_RUNNING) ; else __assert_fail ("info->flags & IFF_RUNNING", "if_info.c", 252, __extension__ __PRETTY_FUNCTION__); }));
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 253, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "out");
            info->state = ST_OUTING;
            break;
        case ST_OUTING:
            break;
        case ST_PROBING:
        case ST_PROBING_UP:
            break;
        case ST_INSANE:
            break;
        case ST_DOWN:
        case ST_DOWNANDOUT:
            break;
        }
    }
    do_log(7, "%s: moved to state %s; worker %d",
           info->name, statename(info->state), info->worker);
    info->flags = newflags;
    info->lastchange = time(0);
}

canonical_original_code:
void ifsm_flagchange(struct if_info *info, unsigned int newflags)
{
    unsigned int changed = (info->flags ^ newflags) & (IFF_RUNNING | IFF_UP);
    if (changed == 0)
        return;
    char buf1[512]; char buf2[512];
    do_log(6, "%s: state %s flags 0x%08x %s -> 0x%08x %s", info->name,
           statename(info->state),
           info->flags, flags_str(buf1, info->flags),
           newflags, flags_str(buf2, newflags));
    if (0 ) {
        info->state = ST_INSANE;
    }
    if (changed & IFF_UP) {
        if (newflags & IFF_UP) {
            switch(info->state) {
            case ST_DOWN:
                info->state = ST_INACTIVE;
                break;
            case ST_PROBING:
                info->state = ST_PROBING_UP;
                break;
            default:
                do_log(3, "%s: unexpected state %s for UP", info->name, statename(info->state));
                exit(1);
            }
        } else {
            switch(info->state) {
            case ST_OUTING:
                info->state = ST_DOWNANDOUT;
                break;
            case ST_DOWN:
                break;
     case ST_PROBING:
  break;
     case ST_PROBING_UP:
  info->state = ST_PROBING;
  break;
            default:
                kill_script(info->worker);
                info->state = ST_PROBING;
                info->worker = run_netplug_bg(info->name, "probe");
            }
        }
    }
    if (changed & IFF_RUNNING) {
        switch(info->state) {
        case ST_INACTIVE:
            ((void) sizeof ((!(info->flags & IFF_RUNNING)) ? 1 : 0), __extension__ ({ if (!(info->flags & IFF_RUNNING)) ; else __assert_fail ("!(info->flags & IFF_RUNNING)", "if_info.c", 235, __extension__ __PRETTY_FUNCTION__); }));
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 236, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "in");
            info->state = ST_INNING;
            break;
        case ST_INNING:
            ((void) sizeof ((info->flags & IFF_RUNNING) ? 1 : 0), __extension__ ({ if (info->flags & IFF_RUNNING) ; else __assert_fail ("info->flags & IFF_RUNNING", "if_info.c", 243, __extension__ __PRETTY_FUNCTION__); }));
            info->state = ST_WAIT_IN;
            break;
        case ST_WAIT_IN:
            break;
        case ST_ACTIVE:
            ((void) sizeof ((info->flags & IFF_RUNNING) ? 1 : 0), __extension__ ({ if (info->flags & IFF_RUNNING) ; else __assert_fail ("info->flags & IFF_RUNNING", "if_info.c", 252, __extension__ __PRETTY_FUNCTION__); }));
            ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 253, __extension__ __PRETTY_FUNCTION__); }));
            info->worker = run_netplug_bg(info->name, "out");
            info->state = ST_OUTING;
            break;
        case ST_OUTING:
            break;
        case ST_PROBING:
        case ST_PROBING_UP:
            break;
        case ST_INSANE:
            break;
        case ST_DOWN:
        case ST_DOWNANDOUT:
            break;
        }
    }
    do_log(7, "%s: moved to state %s; worker %d",
           info->name, statename(info->state), info->worker);
    info->flags = newflags;
    info->lastchange = time(0);
}

variable_types:
newflags
unsigned int

variable_types:
changed
unsigned int

user_defined_types: enum ifstate { ST_DOWN=0, ST_DOWNANDOUT=1, ST_PROBING=2, ST_PROBING_UP=3, ST_INACTIVE=4, ST_INNING=5, ST_WAIT_IN=6, ST_ACTIVE=7, ST_OUTING=8, ST_INSANE=9 }


decompiled_code:
__int64 __fastcall ifsm_scriptdone(unsigned int a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
int v7; // er12
__int64 v8; // rbp
const char *v9; // rax
__int64 v10; // rbx
int v11; // edi
__int64 v13; // rbx
const char *v14; // rax
int v15; // eax
int v16; // edx
__int64 v17; // rax
const char *v18; // rax
__int64 v19; // [rsp+0h] [rbp-48h] BYREF
unsigned int v20; // [rsp+8h] [rbp-40h]
__int16 v21; // [rsp+Ch] [rbp-3Ch] BYREF
int v22; // [rsp+Eh] [rbp-3Ah]
__int16 v23; // [rsp+12h] [rbp-36h]
__int64 *v24; // [rsp+14h] [rbp-34h]
int v25; // [rsp+1Ch] [rbp-2Ch]
char *v26; // [rsp+28h] [rbp-20h]

v26 = &a7;
v20 = a1;
v21 = -17599;
v22 = (int)find_pid_0;
v23 = -17847;
v24 = &v19;
v25 = -1864106167;
v7 = a2 & 0x7F;
if ( v7 | BYTE1(a2) )
{
if ( (a2 & 0x7F) != 0 )
{
if ( __OFSUB__((a2 & 0x7F) + 1, 1) || (a2 & 0x7F) == 0 )
_assert_fail("WIFEXITED(exitstatus) || WIFSIGNALED(exitstatus)", "if_info.c", 0x124u, "ifsm_scriptdone");
v7 = 0;
}
}
else
{
v7 = 1;
}
v19 = 0LL;
for_each_iface((unsigned int (__fastcall *)(_QWORD *))&v21);
v8 = v19;
if ( !v19 )
return do_log(6LL, "Unexpected child %d exited with status %d", v20, a2);
v9 = statename(*(_DWORD *)(v19 + 48));
do_log(6LL, "%s: state %s pid %d exited status %d", (const char *)(v8 + 32), v9, v20, a2);
v10 = v19;
*(_DWORD *)(v19 + 52) = -1;
v11 = *(_DWORD *)(v10 + 48);
switch ( v11 )
{
case 0:
case 4:
case 7:
case 9:
v18 = statename(v11);
do_log(3LL, "ifsm_scriptdone: %s: bad state %s for script termination", (const char *)(v10 + 32), v18);
exit(1);
case 1:
*(_DWORD *)(v10 + 48) = 2;
v15 = run_netplug_bg(v10 + 32, "probe");
*(_DWORD *)(v19 + 52) = v15;
break;
case 2:
if ( !v7 )
do_log(4LL, "Could not bring %s back up", (const char *)(v10 + 32));
*(_DWORD *)(v19 + 48) = 0;
break;
case 3:
*(_DWORD *)(v10 + 48) = 4;
break;
case 5:
*(_DWORD *)(v10 + 48) = v7 == 0 ? 9 : 7;
break;
case 6:
v16 = run_netplug_bg(v10 + 32, "out");
v17 = v19;
*(_DWORD *)(v19 + 52) = v16;
*(_DWORD *)(v17 + 48) = 8;
break;
case 8:
*(_DWORD *)(v10 + 48) = 4;
break;
default:
break;
}
v13 = v19;
v14 = statename(*(_DWORD *)(v19 + 48));
return do_log(7LL, "%s: moved to state %s", (const char *)(v13 + 32), v14);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
int v7; // er12
__int64 v8; // rbp
const char *v9; // rax
__int64 v10; // rbx
int v11; // edi
__int64 v13; // rbx
const char *v14; // rax
int v15; // eax
int v16; // edx
__int64 v17; // rax
const char *v18; // rax
__int64 v19; // [rsp+0h] [rbp-48h] BYREF
unsigned int v20; // [rsp+8h] [rbp-40h]
__int16 v21; // [rsp+Ch] [rbp-3Ch] BYREF
int v22; // [rsp+Eh] [rbp-3Ah]
__int16 v23; // [rsp+12h] [rbp-36h]
__int64 *v24; // [rsp+14h] [rbp-34h]
int v25; // [rsp+1Ch] [rbp-2Ch]
char *v26; // [rsp+28h] [rbp-20h]

v26 = &a7;
v20 = a1;
v21 = -17599;
v22 = (int)find_pid_0;
v23 = -17847;
v24 = &v19;
v25 = -1864106167;
v7 = a2 & 0x7F;
if ( v7 | BYTE1(a2) )
{
if ( (a2 & 0x7F) != 0 )
{
if ( __OFSUB__((a2 & 0x7F) + 1, 1) || (a2 & 0x7F) == 0 )
_assert_fail("WIFEXITED(exitstatus) || WIFSIGNALED(exitstatus)", "if_info.c", 0x124u, "ifsm_scriptdone");
v7 = 0;
}
}
else
{
v7 = 1;
}
v19 = 0LL;
func3((unsigned int (__fastcall *)(_QWORD *))&v21);
v8 = v19;
if ( !v19 )
return do_log(6LL, "Unexpected child %d exited with status %d", v20, a2);
v9 = func0(*(_DWORD *)(v19 + 48));
do_log(6LL, "%s: state %s pid %d exited status %d", (const char *)(v8 + 32), v9, v20, a2);
v10 = v19;
*(_DWORD *)(v19 + 52) = -1;
v11 = *(_DWORD *)(v10 + 48);
switch ( v11 )
{
case 0:
case 4:
case 7:
case 9:
v18 = func0(v11);
do_log(3LL, "ifsm_scriptdone: %s: bad state %s for script termination", (const char *)(v10 + 32), v18);
exit(1);
case 1:
*(_DWORD *)(v10 + 48) = 2;
v15 = run_netplug_bg(v10 + 32, "probe");
*(_DWORD *)(v19 + 52) = v15;
break;
case 2:
if ( !v7 )
do_log(4LL, "Could not bring %s back up", (const char *)(v10 + 32));
*(_DWORD *)(v19 + 48) = 0;
break;
case 3:
*(_DWORD *)(v10 + 48) = 4;
break;
case 5:
*(_DWORD *)(v10 + 48) = v7 == 0 ? 9 : 7;
break;
case 6:
v16 = run_netplug_bg(v10 + 32, "out");
v17 = v19;
*(_DWORD *)(v19 + 52) = v16;
*(_DWORD *)(v17 + 48) = 8;
break;
case 8:
*(_DWORD *)(v10 + 48) = 4;
break;
default:
break;
}
v13 = v19;
v14 = func0(*(_DWORD *)(v19 + 48));
return do_log(7LL, "%s: moved to state %s", (const char *)(v13 + 32), v14);
}

original_code:
void ifsm_scriptdone(pid_t pid, int exitstatus)
{
    int exitok = (((exitstatus) & 0x7f) == 0) && (((exitstatus) & 0xff00) >> 8) == 0;
    struct if_info *info;
    ((void) sizeof (((((exitstatus) & 0x7f) == 0) || (((signed char) (((exitstatus) & 0x7f) + 1) >> 1) > 0)) ? 1 : 0), __extension__ ({ if ((((exitstatus) & 0x7f) == 0) || (((signed char) (((exitstatus) & 0x7f) + 1) >> 1) > 0)) ; else __assert_fail ("WIFEXITED(exitstatus) || WIFSIGNALED(exitstatus)", "if_info.c", 292, __extension__ __PRETTY_FUNCTION__); }));
    int find_pid(struct if_info *i) {
        if (i->worker == pid) {
            info = i;
            return 1;
        }
        return 0;
    }
    info = ((void *)0);
    for_each_iface(find_pid);
    if (info == ((void *)0)) {
        do_log(6, "Unexpected child %d exited with status %d",
               pid, exitstatus);
        return;
    }
    do_log(6, "%s: state %s pid %d exited status %d",
           info->name, statename(info->state), pid, exitstatus);
    info->worker = -1;
    switch(info->state) {
    case ST_PROBING:
        if (!exitok)
            do_log(4, "Could not bring %s back up", info->name);
        info->state = ST_DOWN;
        break;
    case ST_PROBING_UP:
        info->state = ST_INACTIVE;
        break;
    case ST_DOWNANDOUT:
        info->state = ST_PROBING;
        ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 338, __extension__ __PRETTY_FUNCTION__); }));
        info->worker = run_netplug_bg(info->name, "probe");
        break;
    case ST_INNING:
        if (exitok)
            info->state = ST_ACTIVE;
        else
            info->state = ST_INSANE;
        break;
    case ST_OUTING:
        info->state = ST_INACTIVE;
        break;
    case ST_WAIT_IN:
        ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 355, __extension__ __PRETTY_FUNCTION__); }));
        info->worker = run_netplug_bg(info->name, "out");
        info->state = ST_OUTING;
        break;
    case ST_INACTIVE:
    case ST_ACTIVE:
    case ST_INSANE:
    case ST_DOWN:
        do_log(3, "ifsm_scriptdone: %s: bad state %s for script termination",
               info->name, statename(info->state));
        exit(1);
    }
    do_log(7, "%s: moved to state %s", info->name, statename(info->state));
}

canonical_original_code:
void ifsm_scriptdone(int pid, int exitstatus)
{
    int exitok = (((exitstatus) & 0x7f) == 0) && (((exitstatus) & 0xff00) >> 8) == 0;
    struct if_info *info;
    ((void) sizeof (((((exitstatus) & 0x7f) == 0) || (((char) (((exitstatus) & 0x7f) + 1) >> 1) > 0)) ? 1 : 0), __extension__ ({ if ((((exitstatus) & 0x7f) == 0) || (((char) (((exitstatus) & 0x7f) + 1) >> 1) > 0)) ; else __assert_fail ("WIFEXITED(exitstatus) || WIFSIGNALED(exitstatus)", "if_info.c", 292, __extension__ __PRETTY_FUNCTION__); }));
    int find_pid(struct if_info *i) {
        if (i->worker == pid) {
            info = i;
            return 1;
        }
        return 0;
    }
    info = ((void *)0);
    for_each_iface(find_pid);
    if (info == ((void *)0)) {
        do_log(6, "Unexpected child %d exited with status %d",
               pid, exitstatus);
        return;
    }
    do_log(6, "%s: state %s pid %d exited status %d",
           info->name, statename(info->state), pid, exitstatus);
    info->worker = -1;
    switch(info->state) {
    case ST_PROBING:
        if (!exitok)
            do_log(4, "Could not bring %s back up", info->name);
        info->state = ST_DOWN;
        break;
    case ST_PROBING_UP:
        info->state = ST_INACTIVE;
        break;
    case ST_DOWNANDOUT:
        info->state = ST_PROBING;
        ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 338, __extension__ __PRETTY_FUNCTION__); }));
        info->worker = run_netplug_bg(info->name, "probe");
        break;
    case ST_INNING:
        if (exitok)
            info->state = ST_ACTIVE;
        else
            info->state = ST_INSANE;
        break;
    case ST_OUTING:
        info->state = ST_INACTIVE;
        break;
    case ST_WAIT_IN:
        ((void) sizeof ((info->worker == -1) ? 1 : 0), __extension__ ({ if (info->worker == -1) ; else __assert_fail ("info->worker == -1", "if_info.c", 355, __extension__ __PRETTY_FUNCTION__); }));
        info->worker = run_netplug_bg(info->name, "out");
        info->state = ST_OUTING;
        break;
    case ST_INACTIVE:
    case ST_ACTIVE:
    case ST_INSANE:
    case ST_DOWN:
        do_log(3, "ifsm_scriptdone: %s: bad state %s for script termination",
               info->name, statename(info->state));
        exit(1);
    }
    do_log(7, "%s: moved to state %s", info->name, statename(info->state));
}

variable_types: exitstatus int

variable_types: exitok int