decompiled_code:
__int64 __fastcall obj_unlock(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL) + 48LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL) + 48LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
int obj_unlock(Lock *lock)
{
    if(lock->object->engine->unlock == ((void *)0))
        return 0;
    return lock->object->engine->unlock(lock);
}

canonical_original_code:
int obj_unlock(struct _lock *lock)
{
    if(lock->object->engine->unlock == ((void *)0))
        return 0;
    return lock->object->engine->unlock(lock);
}

variable_types: struct _lock { int id; struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *object; } *


repo: dylan-evans/objspace

decompiled_code:
__int64 __fastcall obj_length(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 56LL);
if ( v1 )
result = v1();
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 56LL);
if ( v1 )
result = v1();
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int obj_length(Object *self)
{
    if(self->engine->length == ((void *)0))
        return -1;
    return self->engine->length(self);
}

canonical_original_code:
int obj_length(struct _object *self)
{
    if(self->engine->length == ((void *)0))
        return -1;
    return self->engine->length(self);
}

variable_types: struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *

return_type: int

user_defined_types: struct _object { int type; struct _object *container; struct _engine *engine; }

user_defined_types: struct _engine { int (*set)(struct _object *, struct _key *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); }


decompiled_code:
__int64 __fastcall obj_string(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 64LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 (*v1)(void); // rax
__int64 result; // rax

v1 = *(__int64 (**)(void))(*(_QWORD *)(a1 + 16) + 64LL);
if ( v1 )
result = v1();
else
result = 0LL;
return result;
}

original_code:
char *obj_string(Object *self)
{
    if(self->engine->string == ((void *)0))
        return ((void *)0);
    return self->engine->string(self);
}

canonical_original_code:
char *obj_string(struct _object *self)
{
    if(self->engine->string == ((void *)0))
        return ((void *)0);
    return self->engine->string(self);
}

return_type: char *

user_defined_types: struct _key { char *str; unsigned int index; }

user_defined_types: struct _iter { struct _object *object; int index; }

user_defined_types: struct _lock { int id; struct _object *object; }


decompiled_code:
void object_lock()
{
;
}

canonical_decompiled_code:
void func10()
{
;
}

original_code:
Lock *object_lock(Object *object, int timeout)
{
    Lock *lock;
}

canonical_original_code:
struct _lock *object_lock(struct _object *object, int timeout)
{
    struct _lock *lock;
}

variable_types:
object
struct _object *

variable_types: timeout int

return_type: struct _lock { int id; struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *object; } *


decompiled_code:
// attributes: thunk
void key_from_int()
{
object_lock();
}

canonical_decompiled_code:
void func11()
{
func10();
}

original_code:
Key *key_from_int(Object *object, int index)
{
}

canonical_original_code:
struct _key *key_from_int(struct _object *object, int index)
{
}

variable_types: struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *

variable_types: index int

return_type: struct _key { char *str; unsigned int index; } *


decompiled_code:
double *__fastcall object_float(double a1)
{
double *result; // rax

result = (double *)number_create(18);
result[3] = a1;
return result;
}

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

result = (double *)number_create(18);
result[3] = a1;
return result;
}

original_code:
Object *object_float(double value)
{
    Number *num;
    num = number_create(0x12);
    num->value.num_float = value;
    return (Object*)num;
}

canonical_original_code:
struct _object *object_float(double value)
{
    struct Number *num;
    num = number_create(0x12);
    num->value.num_float = value;
    return (struct _object *)num;
}

variable_types: value double

variable_types: struct Number { struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } object; union { int num_int; double num_float; } value; } *

return_type: struct _object { int type; struct _object *container; struct _engine { int (*set)(struct _object *, struct _key { char *str; unsigned int index; } *, struct _object *); struct _object *(*get)(struct _object *, struct _key *); int (*destroy)(struct _object *, struct _key *); struct _iter { struct _object *object; int index; } *(*iter)(struct _object *); struct _object *(*next)(struct _iter *); struct _lock { int id; struct _object *object; } *(*lock)(struct _object *, int); int (*unlock)(struct _lock *); int (*length)(struct _object *); char *(*string)(struct _object *); } *engine; } *

user_defined_types: struct Number { struct _object object; union { int num_int; double num_float; } value; }


decompiled_code:
__int64 init_suite_object()
{
return 0LL;
}

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

original_code:
int init_suite_object()
{
    return 0;
}

canonical_original_code:
int init_suite_object()
{
    return 0;
}


decompiled_code:
__int64 clean_suite_object()
{
return init_suite_object();
}

canonical_decompiled_code:
__int64 func1()
{
return func0();
}

original_code:
int clean_suite_object()
{
    return 0;
}

canonical_original_code:
int clean_suite_object()
{
    return 0;
}


decompiled_code:
__int64 init_suite_num()
{
return init_suite_object();
}

canonical_decompiled_code:
__int64 func2()
{
return func0();
}

original_code:
int init_suite_num()
{
    return 0;
}

canonical_original_code:
int init_suite_num()
{
    return 0;
}


decompiled_code:
__int64 clean_suite_num()
{
return init_suite_object();
}

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

original_code:
int clean_suite_num()
{
    return 0;
}

canonical_original_code:
int clean_suite_num()
{
    return 0;
}


decompiled_code:
__int64 test_object1()
{
__int64 v0; // rbx
char v2[27]; // [rsp+Dh] [rbp-1Bh] BYREF

strcpy(v2, "42");
v0 = object_string(v2);
CU_assertImplementation(v0 != 0, 32LL, "obj != NULL", "tests/test_object.c", &unk_2B0, 0LL);
CU_assertImplementation(*(_DWORD *)v0 == 129, 34LL, "obj->type == STRING_TYPE", "tests/test_object.c", &unk_2B0, 0LL);
return CU_assertImplementation(
*(_QWORD *)(v0 + 24) == (_QWORD)v2,
36LL,
"((String*)obj)->ptr == buf",
"tests/test_object.c",
&unk_2B0,
0LL);
}

canonical_decompiled_code:
__int64 func4()
{
__int64 v0; // rbx
char v2[27]; // [rsp+Dh] [rbp-1Bh] BYREF

strcpy(v2, "42");
v0 = object_string(v2);
CU_assertImplementation(v0 != 0, 32LL, "obj != NULL", "tests/test_object.c", &unk_2B0, 0LL);
CU_assertImplementation(*(_DWORD *)v0 == 129, 34LL, "obj->type == STRING_TYPE", "tests/test_object.c", &unk_2B0, 0LL);
return CU_assertImplementation(
*(_QWORD *)(v0 + 24) == (_QWORD)v2,
36LL,
"((String*)obj)->ptr == buf",
"tests/test_object.c",
&unk_2B0,
0LL);
}

original_code:
void test_object1()
{
    char buf[] = "42";
    Object *obj;
    obj = object_string(buf);
    { CU_assertImplementation((obj != ((void *)0)), 32, "obj != NULL", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((obj->type == 0x81), 34, "obj->type == STRING_TYPE", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((((String*)obj)->ptr == buf), 36, "((String*)obj)->ptr == buf", "tests/test_object.c", "", 0); };
}

canonical_original_code:
void test_object1()
{
    char buf[] = "42";
    struct _object *obj;
    obj = object_string(buf);
    { CU_assertImplementation((obj != ((void *)0)), 32, "obj != NULL", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((obj->type == 0x81), 34, "obj->type == STRING_TYPE", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((((struct _string *)obj)->ptr == buf), 36, "((String*)obj)->ptr == buf", "tests/test_object.c", "", 0); };
}

variable_types:
buf
char []

return_type: void

user_defined_types: struct _string { struct _object object; char *ptr; }


decompiled_code:
__int64 test_num1()
{
__int64 v0; // rbx

v0 = object_float(0.6);
CU_assertImplementation(v0 != 0, 59LL, "obj != NULL", "tests/test_object.c", &unk_2B0, 0LL);
CU_assertImplementation(*(_DWORD *)v0 == 18, 61LL, "obj->type == NUMBER_FLOAT", "tests/test_object.c", &unk_2B0, 0LL);
return CU_assertImplementation(
0.6 == *(double *)(v0 + 24),
63LL,
"((Number*)obj)->value.num_float == test_val",
"tests/test_object.c",
&unk_2B0,
0LL);
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rbx

v0 = object_float(0.6);
CU_assertImplementation(v0 != 0, 59LL, "obj != NULL", "tests/test_object.c", &unk_2B0, 0LL);
CU_assertImplementation(*(_DWORD *)v0 == 18, 61LL, "obj->type == NUMBER_FLOAT", "tests/test_object.c", &unk_2B0, 0LL);
return CU_assertImplementation(
0.6 == *(double *)(v0 + 24),
63LL,
"((Number*)obj)->value.num_float == test_val",
"tests/test_object.c",
&unk_2B0,
0LL);
}

original_code:
void test_num1()
{
    double test_val;
    Object *obj;
    test_val = 4.2 / 7;
    obj = object_float(test_val);
    { CU_assertImplementation((obj != ((void *)0)), 59, "obj != NULL", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((obj->type == 0x12), 61, "obj->type == NUMBER_FLOAT", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((((Number*)obj)->value.num_float == test_val), 63, "((Number*)obj)->value.num_float == test_val", "tests/test_object.c", "", 0); };
}

canonical_original_code:
void test_num1()
{
    double test_val;
    struct _object *obj;
    test_val = 4.2 / 7;
    obj = object_float(test_val);
    { CU_assertImplementation((obj != ((void *)0)), 59, "obj != NULL", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((obj->type == 0x12), 61, "obj->type == NUMBER_FLOAT", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((((struct Number *)obj)->value.num_float == test_val), 63, "((Number*)obj)->value.num_float == test_val", "tests/test_object.c", "", 0); };
}


decompiled_code:
__int64 test_num2()
{
_DWORD *v0; // rbx

v0 = (_DWORD *)object_int(42LL);
CU_assertImplementation(v0 != 0LL, 75LL, "obj != NULL", "tests/test_object.c", &unk_2B0, 0LL);
CU_assertImplementation(*v0 == 17, 77LL, "obj->type == NUMBER_INT", "tests/test_object.c", &unk_2B0, 0LL);
return CU_assertImplementation(
v0[6] == 42,
79LL,
"((Number*)obj)->value.num_int == test_val",
"tests/test_object.c",
&unk_2B0,
0LL);
}

canonical_decompiled_code:
__int64 func6()
{
_DWORD *v0; // rbx

v0 = (_DWORD *)object_int(42LL);
CU_assertImplementation(v0 != 0LL, 75LL, "obj != NULL", "tests/test_object.c", &unk_2B0, 0LL);
CU_assertImplementation(*v0 == 17, 77LL, "obj->type == NUMBER_INT", "tests/test_object.c", &unk_2B0, 0LL);
return CU_assertImplementation(
v0[6] == 42,
79LL,
"((Number*)obj)->value.num_int == test_val",
"tests/test_object.c",
&unk_2B0,
0LL);
}

original_code:
void test_num2()
{
    int test_val;
    Object *obj;
    test_val = 42;
    obj = object_int(test_val);
    { CU_assertImplementation((obj != ((void *)0)), 75, "obj != NULL", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((obj->type == 0x11), 77, "obj->type == NUMBER_INT", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((((Number*)obj)->value.num_int == test_val), 79, "((Number*)obj)->value.num_int == test_val", "tests/test_object.c", "", 0); };
}

canonical_original_code:
void test_num2()
{
    int test_val;
    struct _object *obj;
    test_val = 42;
    obj = object_int(test_val);
    { CU_assertImplementation((obj != ((void *)0)), 75, "obj != NULL", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((obj->type == 0x11), 77, "obj->type == NUMBER_INT", "tests/test_object.c", "", 0); };
    { CU_assertImplementation((((struct Number *)obj)->value.num_int == test_val), 79, "((Number*)obj)->value.num_int == test_val", "tests/test_object.c", "", 0); };
}


decompiled_code:
__int64 setup_object_tests()
{
__int64 v0; // rax
__int64 v1; // rdi
__int64 result; // rax

v0 = CU_add_suite("Object Suite", init_suite_object, clean_suite_object);
if ( v0 && CU_add_test(v0, "Object test 1", test_object1) )
{
v1 = CU_add_suite("Number Suite", init_suite_num, clean_suite_num);
result = 0LL;
if ( v1 )
result = CU_add_test(v1, "Number test 1", test_num1) != 0;
}
else
{
CU_cleanup_registry();
result = CU_get_error();
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rax
__int64 v1; // rdi
__int64 result; // rax

v0 = CU_add_suite("Object Suite", init_suite_object, clean_suite_object);
if ( v0 && CU_add_test(v0, "Object test 1", test_object1) )
{
v1 = CU_add_suite("Number Suite", init_suite_num, clean_suite_num);
result = 0LL;
if ( v1 )
result = CU_add_test(v1, "Number test 1", test_num1) != 0;
}
else
{
CU_cleanup_registry();
result = CU_get_error();
}
return result;
}

original_code:
int setup_object_tests()
{
    CU_pSuite pSuite = ((void *)0);
    pSuite = CU_add_suite("Object Suite", init_suite_object,
                          clean_suite_object);
    if(pSuite == ((void *)0)
        || !CU_add_test(pSuite, "Object test 1", test_object1))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    pSuite = CU_add_suite("Number Suite", init_suite_num, clean_suite_num);
    if(!pSuite
        || !CU_add_test(pSuite, "Number test 1", test_num1))
    {
        return 0;
    }
    return 1;
}

canonical_original_code:
int setup_object_tests()
{
    struct CU_Suite *pSuite = ((void *)0);
    pSuite = CU_add_suite("Object Suite", init_suite_object,
                          clean_suite_object);
    if(pSuite == ((void *)0)
        || !CU_add_test(pSuite, "Object test 1", test_object1))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }
    pSuite = CU_add_suite("Number Suite", init_suite_num, clean_suite_num);
    if(!pSuite
        || !CU_add_test(pSuite, "Number test 1", test_num1))
    {
        return 0;
    }
    return 1;
}

variable_types: struct CU_Suite { char *pName; int fActive; struct CU_Test { char *pName; int fActive; void (*pTestFunc)(); struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } (*pJumpBuf)[1]; struct CU_Test *pNext; struct CU_Test *pPrev; } *pTest; int (*pInitializeFunc)(); int (*pCleanupFunc)(); void (*pSetUpFunc)(); void (*pTearDownFunc)(); unsigned int uiNumberOfTests; struct CU_Suite *pNext; struct CU_Suite *pPrev; unsigned int uiNumberOfTestsFailed; unsigned int uiNumberOfTestsSuccess; } *

user_defined_types: struct CU_Suite { char *pName; int fActive; struct CU_Test *pTest; int (*pInitializeFunc)(); int (*pCleanupFunc)(); void (*pSetUpFunc)(); void (*pTearDownFunc)(); unsigned int uiNumberOfTests; struct CU_Suite *pNext; struct CU_Suite *pPrev; unsigned int uiNumberOfTestsFailed; unsigned int uiNumberOfTestsSuccess; }

user_defined_types: struct CU_Test { char *pName; int fActive; void (*pTestFunc)(); struct __jmp_buf_tag (*pJumpBuf)[1]; struct CU_Test *pNext; struct CU_Test *pPrev; }

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }


toprime

repo: dylan-k/Dadadodomax

decompiled_code:
__int64 __fastcall toprime(unsigned __int64 a1)
{
unsigned __int64 *v1; // rax
unsigned __int64 i; // r8

v1 = (unsigned __int64 *)&unk_508;
for ( i = 107LL; a1 > i; i = *v1++ )
{
if ( v1 == (unsigned __int64 *)&unk_6B8 )
return 2411033LL;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1)
{
unsigned __int64 *v1; // rax
unsigned __int64 i; // r8

v1 = (unsigned __int64 *)&unk_508;
for ( i = 107LL; a1 > i; i = *v1++ )
{
if ( v1 == (unsigned __int64 *)&unk_6B8 )
return 2411033LL;
}
return i;
}

original_code:
static unsigned long
toprime (unsigned long size)
{
  unsigned int i;
  static unsigned int s = (sizeof (primes) / sizeof (*primes)) - 1;
  for (i = 0; i < s; i++)
    if (size <= primes[i])
      return primes[i];
  return primes[s-1];
}

canonical_original_code:
static unsigned long toprime(unsigned long size)
{
  unsigned int i;
  static unsigned int s = (sizeof (primes) / sizeof (*primes)) - 1;
  for (i = 0; i < s; i++)
    if (size <= primes[i])
      return primes[i];
  return primes[s-1];
}

variable_types:
size
unsigned long

variable_types:
i
unsigned int

variable_types:
s
unsigned int

return_type: unsigned long


clrhash

decompiled_code:
void *__fastcall clrhash(__int64 *a1)
{
__int64 v1; // rdx

v1 = *a1;
a1[1] = 0LL;
return memset((void *)a1[4], 0, 16 * v1);
}

original_code:
void
clrhash (hash_table *table)
{
  table->count = 0;
  memset (table->buckets, 0, table->size * sizeof(*table->buckets));
}

canonical_original_code:
void clrhash(struct hash_table *table)
{
  table->count = 0;
  memset (table->buckets, 0, table->size * sizeof(*table->buckets));
}

variable_types: struct hash_table { unsigned long size; unsigned long count; long (*hash)(void *); int (*compare)(void *, void *); struct bucket { void *key; void *value; } *buckets; } *

user_defined_types: struct hash_table { unsigned long size; unsigned long count; long (*hash)(void *); int (*compare)(void *, void *); struct bucket *buckets; }


decompiled_code:
void __fastcall free_hash_table(void *ptr)
{
free(*((void **)ptr + 4));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*((void **)ptr + 4));
free(ptr);
}

original_code:
void
free_hash_table (hash_table *table)
{
  free (table->buckets);
  free (table);
}

canonical_original_code:
void free_hash_table(struct hash_table *table)
{
  free (table->buckets);
  free (table);
}

user_defined_types: struct bucket { void *key; void *value; }


puthash

original_code:
int
puthash (hash_table *table, const void *key, void *value)
{
  unsigned long hash = table->hash (key);
  unsigned long size = table->size;
  unsigned long n;
  bucket *b;
  if (size < (1 + ((13 * table->count) / 10)))
    {
      int status;
      status = growhash (table, size + 1);
      if (status < 0) return status;
      size = table->size;
    }
  n = hash % size;
  b = &table->buckets[n];
  if (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0)
    {
      unsigned long h2 = size - 2;
      unsigned long i = (hash % h2) + 1;
      do
 {
   n += i;
   if (n >= size) n -= size;
   b = &table->buckets [n];
 }
      while (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0);
    }
  if (!b->key)
    table->count++;
  b->key = key;
  b->value = value;
  return 0;
}

canonical_original_code:
int puthash(struct hash_table *table, void *key, void *value)
{
  unsigned long hash = table->hash (key);
  unsigned long size = table->size;
  unsigned long n;
  struct bucket *b;
  if (size < (1 + ((13 * table->count) / 10)))
    {
      int status;
      status = growhash (table, size + 1);
      if (status < 0) return status;
      size = table->size;
    }
  n = hash % size;
  b = &table->buckets[n];
  if (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0)
    {
      unsigned long h2 = size - 2;
      unsigned long i = (hash % h2) + 1;
      do
 {
   n += i;
   if (n >= size) n -= size;
   b = &table->buckets [n];
 }
      while (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0);
    }
  if (!b->key)
    table->count++;
  b->key = key;
  b->value = value;
  return 0;
}

variable_types:
key
void *

variable_types:
value
void *

variable_types:
hash
unsigned long

variable_types:
n
unsigned long

variable_types: struct bucket { void *key; void *value; } *

variable_types: status int

variable_types:
i
unsigned long


growhash

decompiled_code:
__int64 __fastcall growhash(unsigned __int64 *a1, unsigned __int64 a2)
{
unsigned __int64 v2; // r13
__int64 *v3; // r14
__int64 v4; // rbx
void *v5; // rax
__int64 *v6; // rbx
__int64 i; // rbp

v2 = *a1;
v3 = (__int64 *)a1[4];
v4 = toprime(a2);
v5 = calloc(v4, 0x10uLL);
if ( !v5 )
return 0xFFFFFFFFLL;
*a1 = v4;
a1[4] = (unsigned __int64)v5;
a1[1] = 0LL;
if ( v2 )
{
v6 = v3;
for ( i = 0LL; i != v2; ++i )
{
if ( *v6 )
puthash(a1, *v6, v6[1]);
v6 += 2;
}
}
free(v3);
return 0LL;
}

original_code:
static int
growhash (hash_table *table, unsigned long new_size)
{
  unsigned long i;
  unsigned long old_size = table->size;
  bucket *old_buckets = table->buckets;
  bucket *new_buckets;
  new_size = toprime (new_size);
  new_buckets = (bucket *) calloc(new_size, sizeof(*table->buckets));
  if (!new_buckets)
    return -1;
  table->size = new_size;
  table->buckets = new_buckets;
  table->count = 0;
  for (i = 0; i < old_size; i++)
    if (old_buckets[i].key)
      puthash (table, old_buckets[i].key, old_buckets[i].value);
  free (old_buckets);
  return 0;
}

canonical_original_code:
static int growhash(struct hash_table *table, unsigned long new_size)
{
  unsigned long i;
  unsigned long old_size = table->size;
  struct bucket *old_buckets = table->buckets;
  struct bucket *new_buckets;
  new_size = toprime (new_size);
  new_buckets = (struct bucket *) calloc(new_size, sizeof(*table->buckets));
  if (!new_buckets)
    return -1;
  table->size = new_size;
  table->buckets = new_buckets;
  table->count = 0;
  for (i = 0; i < old_size; i++)
    if (old_buckets[i].key)
      puthash (table, old_buckets[i].key, old_buckets[i].value);
  free (old_buckets);
  return 0;
}


gethash

original_code:
void *
gethash (hash_table *table, const void *key, void *def)
{
  unsigned long hash = table->hash (key);
  unsigned long size = table->size;
  unsigned long n = hash % size;
  bucket *b = &table->buckets[n];
  if (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0)
    {
      unsigned long h2 = size - 2;
      unsigned long i = (hash % h2) + 1;
      do
 {
   n += i;
   if (n >= size) n -= size;
   b = &table->buckets [n];
 }
      while (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0);
    }
  return b->value;
}

canonical_original_code:
void *gethash(struct hash_table *table, void *key, void *def)
{
  unsigned long hash = table->hash (key);
  unsigned long size = table->size;
  unsigned long n = hash % size;
  struct bucket *b = &table->buckets[n];
  if (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0)
    {
      unsigned long h2 = size - 2;
      unsigned long i = (hash % h2) + 1;
      do
 {
   n += i;
   if (n >= size) n -= size;
   b = &table->buckets [n];
 }
      while (b->key &&
      key != b->key &&
      table->compare (key, b->key) != 0);
    }
  return b->value;
}

variable_types:
def
void *

return_type: void *


maphash

decompiled_code:
__int64 __fastcall maphash(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD, _QWORD, __int64), __int64 a3)
{
__int64 v4; // rbx
_QWORD *v5; // rax
__int64 result; // rax

if ( !a1 )
return 0LL;
if ( !*a1 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = (_QWORD *)(a1[4] + 16 * v4);
if ( *v5 )
{
result = a2(*v5, v5[1], a3);
if ( (_DWORD)result )
break;
}
if ( *a1 <= (unsigned __int64)++v4 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD, _QWORD, __int64), __int64 a3)
{
__int64 v4; // rbx
_QWORD *v5; // rax
__int64 result; // rax

if ( !a1 )
return 0LL;
if ( !*a1 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = (_QWORD *)(a1[4] + 16 * v4);
if ( *v5 )
{
result = a2(*v5, v5[1], a3);
if ( (_DWORD)result )
break;
}
if ( *a1 <= (unsigned __int64)++v4 )
return 0LL;
}
return result;
}

original_code:
int
maphash (hash_table *table,
  int (*mapper) (const void *key, const void *value, void *arg),
  void *arg)
{
  unsigned long i;
  int status;
  if (table)
    for (i = 0; i < table->size; i++)
      if (table->buckets[i].key)
 {
   status = mapper (table->buckets[i].key, table->buckets[i].value,arg);
   if (status != 0) return status;
 }
  return 0;
}

canonical_original_code:
int maphash(struct hash_table *table, int (*mapper)(void *key, void *value, void *arg), void *arg)
{
  unsigned long i;
  int status;
  if (table)
    for (i = 0; i < table->size; i++)
      if (table->buckets[i].key)
 {
   status = mapper (table->buckets[i].key, table->buckets[i].value,arg);
   if (status != 0) return status;
 }
  return 0;
}

variable_types: int (*)(void *key, void *value, void *arg)

variable_types:
arg
void *


decompiled_code:
unsigned __int64 __fastcall string_hash(unsigned __int8 *a1)
{
unsigned __int64 result; // rax
unsigned __int8 *v2; // rdi
__int64 v3; // rdx

if ( !a1 )
return 0LL;
result = *a1;
v2 = a1 + 1;
if ( !(_BYTE)result )
return 0LL;
while ( 1 )
{
v3 = *v2;
if ( !(_BYTE)v3 )
break;
while ( 1 )
{
++v2;
result = v3 + 16 * result;
if ( (result & 0xF0000000) == 0 )
break;
result = ((unsigned __int64)((unsigned int)result & 0xF0000000) >> 24) ^ result & 0xFFFFFFFF0FFFFFFFLL;
v3 = *v2;
if ( !(_BYTE)v3 )
return result;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(unsigned __int8 *a1)
{
unsigned __int64 result; // rax
unsigned __int8 *v2; // rdi
__int64 v3; // rdx

if ( !a1 )
return 0LL;
result = *a1;
v2 = a1 + 1;
if ( !(_BYTE)result )
return 0LL;
while ( 1 )
{
v3 = *v2;
if ( !(_BYTE)v3 )
break;
while ( 1 )
{
++v2;
result = v3 + 16 * result;
if ( (result & 0xF0000000) == 0 )
break;
result = ((unsigned __int64)((unsigned int)result & 0xF0000000) >> 24) ^ result & 0xFFFFFFFF0FFFFFFFLL;
v3 = *v2;
if ( !(_BYTE)v3 )
return result;
}
}
return result;
}

original_code:
unsigned long
string_hash (const unsigned char *x)
{
  unsigned long g, h = 0;
  if (!x) return 0;
  while (*x != 0)
    {
      h = (h << 4) + *x++;
      if ((g = h & 0xf0000000) != 0)
 h = (h ^ (g >> 24)) ^ g;
    }
  return h;
}

canonical_original_code:
unsigned long string_hash(unsigned char *x)
{
  unsigned long g; unsigned long h = 0;
  if (!x) return 0;
  while (*x != 0)
    {
      h = (h << 4) + *x++;
      if ((g = h & 0xf0000000) != 0)
 h = (h ^ (g >> 24)) ^ g;
    }
  return h;
}

variable_types:
x
unsigned char *

variable_types:
g
unsigned long

variable_types:
h
unsigned long


decompiled_code:
unsigned __int64 __fastcall string_case_hash(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbp
__int64 v2; // rbx
const __int32_t *v3; // rcx
unsigned __int64 result; // rax

if ( !a1 )
return 0LL;
v1 = a1;
v2 = *a1;
if ( !(_BYTE)v2 )
return 0LL;
v3 = *_ctype_toupper_loc();
result = 0LL;
do
{
++v1;
result = v3[v2] + 16 * result;
if ( (result & 0xF0000000) != 0 )
result = ((unsigned __int64)((unsigned int)result & 0xF0000000) >> 24) ^ result & 0xFFFFFFFF0FFFFFFFLL;
v2 = *v1;
}
while ( (_BYTE)v2 );
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbp
__int64 v2; // rbx
const __int32_t *v3; // rcx
unsigned __int64 result; // rax

if ( !a1 )
return 0LL;
v1 = a1;
v2 = *a1;
if ( !(_BYTE)v2 )
return 0LL;
v3 = *_ctype_toupper_loc();
result = 0LL;
do
{
++v1;
result = v3[v2] + 16 * result;
if ( (result & 0xF0000000) != 0 )
result = ((unsigned __int64)((unsigned int)result & 0xF0000000) >> 24) ^ result & 0xFFFFFFFF0FFFFFFFLL;
v2 = *v1;
}
while ( (_BYTE)v2 );
return result;
}

original_code:
unsigned long
string_case_hash (const unsigned char *x)
{
  unsigned long g, h = 0;
  if (!x) return 0;
  while (*x != 0)
    {
      h = (h << 4) + toupper(*x++);
      if ((g = h & 0xf0000000) != 0)
 h = (h ^ (g >> 24)) ^ g;
    }
  return h;
}

canonical_original_code:
unsigned long string_case_hash(unsigned char *x)
{
  unsigned long g; unsigned long h = 0;
  if (!x) return 0;
  while (*x != 0)
    {
      h = (h << 4) + toupper(*x++);
      if ((g = h & 0xf0000000) != 0)
 h = (h ^ (g >> 24)) ^ g;
    }
  return h;
}


increment

original_code:
static int
increment (pword *pword, pword_link **listP, int *lengthP, int *sizeP)
{
  int size = *sizeP;
  int length = *lengthP;
  pword_link *list = *listP;
  int bot = 0;
  int top = length;
  while (top > bot)
    {
      int mid = ((top - bot) >> 1) + bot;
      if (pword == list[mid].word)
 {
   list[mid].count++;
   return 0;
 }
      else if (pword < list[mid].word)
 top = mid;
      else if (bot == mid)
 break;
      else
 bot = mid;
    }
  if (size == 0)
    {
      size = 10;
      list = (pword_link *) calloc (size, sizeof(*list));
      if (!list) return 0;
      *listP = list;
      *sizeP = size;
    }
  else if (length+1 >= size)
    {
      size = (((*sizeP + 10) * 13) / 10);
      list = (pword_link *) realloc (list, size * sizeof(*list));
      if (!list) return 0;
      memset (list+length, 0, (size-length) * sizeof(*list));
      *listP = list;
      *sizeP = size;
    }
  if (top != length)
    memmove (list+top+1, list+top, (length - top) * sizeof(*list));
  total_links++;
  (*lengthP)++;
  list[top].word = pword;
  list[top].count = 1;
  return 0;
}

canonical_original_code:
static int increment(struct pword *pword, struct pword_link **listP, int *lengthP, int *sizeP)
{
  int size = *sizeP;
  int length = *lengthP;
  struct pword_link *list = *listP;
  int bot = 0;
  int top = length;
  while (top > bot)
    {
      int mid = ((top - bot) >> 1) + bot;
      if (pword == list[mid].word)
 {
   list[mid].count++;
   return 0;
 }
      else if (pword < list[mid].word)
 top = mid;
      else if (bot == mid)
 break;
      else
 bot = mid;
    }
  if (size == 0)
    {
      size = 10;
      list = (struct pword_link *) calloc (size, sizeof(*list));
      if (!list) return 0;
      *listP = list;
      *sizeP = size;
    }
  else if (length+1 >= size)
    {
      size = (((*sizeP + 10) * 13) / 10);
      list = (struct pword_link *) realloc (list, size * sizeof(*list));
      if (!list) return 0;
      memset (list+length, 0, (size-length) * sizeof(*list));
      *listP = list;
      *sizeP = size;
    }
  if (top != length)
    memmove (list+top+1, list+top, (length - top) * sizeof(*list));
  total_links++;
  (*lengthP)++;
  list[top].word = pword;
  list[top].count = 1;
  return 0;
}

variable_types: struct pword { int id; unsigned char *string; int count; int start; int cap; int comma; int period; int quem; int bang; struct pword_link { int count; struct pword *word; } *succ; int succ_length; int succ_size; } *

variable_types:
listP
struct pword_link **

variable_types:
lengthP
int *

variable_types:
sizeP
int *

variable_types: size int

variable_types: length int

variable_types: struct pword_link { int count; struct pword { int id; unsigned char *string; int count; int start; int cap; int comma; int period; int quem; int bang; struct pword_link *succ; int succ_length; int succ_size; } *word; } *

variable_types: bot int

variable_types: top int

variable_types: mid int


original_code:
unsigned int
ya_random (void)
{
  register int ret = a[i1] + a[i2];
  a[i1] = ret;
  if (++i1 >= 55) i1 = 0;
  if (++i2 >= 55) i2 = 0;
  return ret;
}

canonical_original_code:
unsigned int ya_random()
{
  register int ret = a[i1] + a[i2];
  a[i1] = ret;
  if (++i1 >= 55) i1 = 0;
  if (++i2 >= 55) i2 = 0;
  return ret;
}

variable_types: ret int

return_type: unsigned int


decompiled_code:
__int64 __fastcall ya_rand_init(int a1)
{
unsigned int *v1; // rdx
unsigned int v2; // esi
unsigned int v3; // eax
__int64 result; // rax
int v5; // ebx
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

if ( !a1 )
{
gettimeofday(&tv, &tz);
v5 = 999 * LODWORD(tv.tv_sec) + 1001 * LODWORD(tv.tv_usec);
a1 = v5 + 1003 * getpid();
}
v1 = (unsigned int *)&unk_144;
v2 = a1 + a[0];
a[0] = v2;
v3 = v2;
while ( 1 )
{
a1 = 1001 * v3 + 999 * a1;
v3 = a1 + *v1;
*v1 = v3;
if ( &unk_218 == (_UNKNOWN *)v1 )
break;
++v1;
}
i1 = v2 % 0x37;
result = (unsigned int)((int)(v2 % 0x37 + 20) % 55);
i2 = (int)(v2 % 0x37 + 20) % 55;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
unsigned int *v1; // rdx
unsigned int v2; // esi
unsigned int v3; // eax
__int64 result; // rax
int v5; // ebx
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

if ( !a1 )
{
gettimeofday(&tv, &tz);
v5 = 999 * LODWORD(tv.tv_sec) + 1001 * LODWORD(tv.tv_usec);
a1 = v5 + 1003 * getpid();
}
v1 = (unsigned int *)&unk_144;
v2 = a1 + a[0];
a[0] = v2;
v3 = v2;
while ( 1 )
{
a1 = 1001 * v3 + 999 * a1;
v3 = a1 + *v1;
*v1 = v3;
if ( &unk_218 == (_UNKNOWN *)v1 )
break;
++v1;
}
i1 = v2 % 0x37;
result = (unsigned int)((int)(v2 % 0x37 + 20) % 55);
i2 = (int)(v2 % 0x37 + 20) % 55;
return result;
}

original_code:
void
ya_rand_init(unsigned int seed)
{
  int i;
  if (seed == 0)
    {
      struct timeval tp;
      struct timezone tzp;
      gettimeofday(&tp, &tzp);
      seed = (999*tp.tv_sec) + (1001*tp.tv_usec) + (1003 * getpid());
    }
  a[0] += seed;
  for (i = 1; i < 55; i++)
    {
      seed = a[i-1]*1001 + seed*999;
      a[i] += seed;
    }
  i1 = a[0] % 55;
  i2 = (i1 + 024) % 55;
}

variable_types:
seed
unsigned int

variable_types: i int

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

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }


decompiled_code:
__int64 random_first_word()
{
int *v0; // rbx
int v1; // edx
__int64 v2; // rcx
__int64 result; // rax

v0 = (int *)starters;
v1 = (ya_random() & 0x7FFFFFFFuLL) % (unsigned int)total_starters;
do
{
v2 = *v0++;
result = all_words + 56 * v2;
v1 -= *(_DWORD *)(result + 8);
}
while ( v1 > 0 );
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int *v0; // rbx
int v1; // edx
__int64 v2; // rcx
__int64 result; // rax

v0 = (int *)starters;
v1 = (ya_random() & 0x7FFFFFFFuLL) % (unsigned int)total_starters;
do
{
v2 = *v0++;
result = all_words + 56 * v2;
v1 -= *(_DWORD *)(result + 8);
}
while ( v1 > 0 );
return result;
}

original_code:
static word *
random_first_word (void)
{
  word *w = 0;
  int *s = starters;
  int i = ((ya_random() & 0x7FFFFFFF) % (total_starters));
  do {
    w = &all_words[*s];
    i -= w->start;
    s++;
  } while (i > 0);
  return w;
}

canonical_original_code:
static struct word *random_first_word()
{
  struct word *w = 0;
  int *s = starters;
  int i = ((ya_random() & 0x7FFFFFFF) % (total_starters));
  do {
    w = &all_words[*s];
    i -= w->start;
    s++;
  } while (i > 0);
  return w;
}

variable_types: struct word { int string; int count; int start; int cap; int comma; int period; int quem; int bang; int succ_length; struct word_link { int count; int word; } *succ; } *

variable_types:
s
int *

user_defined_types: struct word { int string; int count; int start; int cap; int comma; int period; int quem; int bang; int succ_length; struct word_link *succ; }


decompiled_code:
__int64 __fastcall random_linked(_DWORD *a1, int a2)
{
_DWORD *v2; // r13
_DWORD *v3; // rax
__int64 v4; // r12
unsigned int v5; // ebp
int v6; // edx
_DWORD *v7; // rax
int i; // edx

if ( a2 <= 0 )
random_linked_cold();
v2 = a1;
v3 = a1;
v4 = (__int64)&a1[2 * (a2 - 1) + 2];
v5 = 0;
do
{
v5 += *v3;
v3 += 2;
}
while ( v3 != (_DWORD *)v4 );
v6 = (ya_random() & 0x7FFFFFFFuLL) % v5;
v7 = a1 + 2;
for ( i = v6 - *a1; i > 0; v7 += 2 )
{
if ( v7 == (_DWORD *)v4 )
JUMPOUT(0xAC5LL);
i -= *v7;
v2 = v7;
}
return all_words + 56LL * (int)v2[1];
}

original_code:
static word *
random_linked (word_link *links, int link_length)
{
  int i;
  int count = 0;
  for (i = 0; i < link_length; i++)
    count += links[i].count;
  count = ((ya_random() & 0x7FFFFFFF) % (count));
  for (i = 0; i < link_length; i++)
    {
      count -= links[i].count;
      if (count <= 0)
 return &all_words [links[i].word];
    }
  abort();
}

canonical_original_code:
static struct word *random_linked(struct word_link *links, int link_length)
{
  int i;
  int count = 0;
  for (i = 0; i < link_length; i++)
    count += links[i].count;
  count = ((ya_random() & 0x7FFFFFFF) % (count));
  for (i = 0; i < link_length; i++)
    {
      count -= links[i].count;
      if (count <= 0)
 return &all_words [links[i].word];
    }
  abort();
}

variable_types:
links
struct word_link *

variable_types: count int

return_type: struct word { int string; int count; int start; int cap; int comma; int period; int quem; int bang; int succ_length; struct word_link { int count; int word; } *succ; } *


decompiled_code:
__int64 __fastcall random_next_word(__int64 a1)
{
int v1; // esi
char v2; // r8
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rax

if ( !*(_DWORD *)(a1 + 36)
|| __ROR4__(-858993459 * (ya_random() & 0x7FFFFFFF), 1) > 0x19999999u
|| (v5 = random_linked(*(_DWORD **)(a1 + 48), *(_DWORD *)(a1 + 36)), result = random_next_word(v5), a1 == result) )
{
v1 = *(_DWORD *)(a1 + 32);
if ( v1 )
{
result = random_linked(*(_DWORD **)(a1 + 40), v1);
}
else
{
v2 = ya_random();
result = 0LL;
if ( (v2 & 3) != 0 )
{
v4 = all_words;
result = v4 + 56 * ((ya_random() & 0x7FFFFFFFuLL) % total_words);
}
}
}
return result;
}

original_code:
static word *
random_next_word (word *w)
{
  if (w->pred_length && !(((ya_random() & 0x7FFFFFFF) % (10))))
    {
      word *w2 = random_next_word (random_linked (w->pred, w->pred_length));
      if (w != w2)
 return w2;
    }
  if (w->succ_length)
    return random_linked (w->succ, w->succ_length);
  else if (ya_random() % 4)
    return &all_words[((ya_random() & 0x7FFFFFFF) % (total_words))];
  else
    return 0;
}

canonical_original_code:
static struct word *random_next_word(struct word *w)
{
  if (w->pred_length && !(((ya_random() & 0x7FFFFFFF) % (10))))
    {
      struct word *w2 = random_next_word (random_linked (w->pred, w->pred_length));
      if (w != w2)
 return w2;
    }
  if (w->succ_length)
    return random_linked (w->succ, w->succ_length);
  else if (ya_random() % 4)
    return &all_words[((ya_random() & 0x7FFFFFFF) % (total_words))];
  else
    return 0;
}

variable_types:
w
struct word *

user_defined_types: struct word_link { int count; int word; }


repo: dynup/kpatch

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

result = a1 & 0xF;
if ( (a1 & 0xC) != 0 )
result = 0LL;
return result;
}

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

result = a1 & 0xF;
if ( (a1 & 0xC) != 0 )
result = 0LL;
return result;
}

original_code:
static inline int inat_last_prefix_id(insn_attr_t attr)
{
 if ((attr & (((1 << 4) - 1) << 0)) > 3)
  return 0;
 else
  return attr & (((1 << 4) - 1) << 0);
}

canonical_original_code:
static inline int inat_last_prefix_id(unsigned int attr)
{
 if ((attr & (((1 << 4) - 1) << 0)) > 3)
  return 0;
 else
  return attr & (((1 << 4) - 1) << 0);
}

variable_types:
attr
unsigned int


decompiled_code:
__int64 __fastcall inat_escape_id(unsigned int a1)
{
return (a1 >> 4) & 3;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return (a1 >> 4) & 3;
}

original_code:
static inline int inat_escape_id(insn_attr_t attr)
{
 return (attr & (((1 << 2) - 1) << (0 + 4))) >> (0 + 4);
}

canonical_original_code:
static inline int inat_escape_id(unsigned int attr)
{
 return (attr & (((1 << 2) - 1) << (0 + 4))) >> (0 + 4);
}


decompiled_code:
__int64 __fastcall inat_is_group(__int16 a1)
{
return a1 & 0x7C0;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
return a1 & 0x7C0;
}

original_code:
static inline int inat_is_group(insn_attr_t attr)
{
 return attr & (((1 << 5) - 1) << ((0 + 4) + 2));
}

canonical_original_code:
static inline int inat_is_group(unsigned int attr)
{
 return attr & (((1 << 5) - 1) << ((0 + 4) + 2));
}


decompiled_code:
__int64 __fastcall inat_group_id(unsigned int a1)
{
return (a1 >> 6) & 0x1F;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
return (a1 >> 6) & 0x1F;
}

original_code:
static inline int inat_group_id(insn_attr_t attr)
{
 return (attr & (((1 << 5) - 1) << ((0 + 4) + 2))) >> ((0 + 4) + 2);
}

canonical_original_code:
static inline int inat_group_id(unsigned int attr)
{
 return (attr & (((1 << 5) - 1) << ((0 + 4) + 2))) >> ((0 + 4) + 2);
}


decompiled_code:
__int64 __fastcall inat_group_common_attribute(int a1)
{
return a1 & 0xFFFFF83F;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
return a1 & 0xFFFFF83F;
}

original_code:
static inline int inat_group_common_attribute(insn_attr_t attr)
{
 return attr & ~(((1 << 5) - 1) << ((0 + 4) + 2));
}

canonical_original_code:
static inline int inat_group_common_attribute(unsigned int attr)
{
 return attr & ~(((1 << 5) - 1) << ((0 + 4) + 2));
}


decompiled_code:
__int64 __fastcall inat_has_variant(int a1)
{
return a1 & 0x40000;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
return a1 & 0x40000;
}

original_code:
static inline int inat_has_variant(insn_attr_t attr)
{
 return attr & (1 << (((((0 + 4) + 2) + 5) + 3) + 4));
}

canonical_original_code:
static inline int inat_has_variant(unsigned int attr)
{
 return attr & (1 << (((((0 + 4) + 2) + 5) + 3) + 4));
}


decompiled_code:
__int64 __fastcall inat_get_opcode_attribute(unsigned __int8 a1)
{
return (unsigned int)inat_primary_table[a1];
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1)
{
return (unsigned int)inat_primary_table[a1];
}

original_code:
insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode)
{
 return inat_primary_table[opcode];
}

canonical_original_code:
unsigned int inat_get_opcode_attribute(unsigned char opcode)
{
 return inat_primary_table[opcode];
}

variable_types:
opcode
unsigned char


decompiled_code:
__int64 __fastcall inat_get_last_prefix_id(unsigned __int8 a1)
{
char v1; // al

v1 = inat_get_opcode_attribute(a1);
return inat_last_prefix_id(v1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1)
{
char v1; // al

v1 = func6(a1);
return func0(v1);
}

original_code:
int inat_get_last_prefix_id(insn_byte_t last_pfx)
{
 insn_attr_t lpfx_attr;
 lpfx_attr = inat_get_opcode_attribute(last_pfx);
 return inat_last_prefix_id(lpfx_attr);
}

canonical_original_code:
int inat_get_last_prefix_id(unsigned char last_pfx)
{
 unsigned int lpfx_attr;
 lpfx_attr = inat_get_opcode_attribute(last_pfx);
 return inat_last_prefix_id(lpfx_attr);
}


decompiled_code:
__int64 __fastcall inat_get_escape_attribute(__int64 a1, int a2, unsigned int a3)
{
__int64 v3; // rax
unsigned __int8 v4; // cl
__int64 v5; // rdx
__int64 v6; // rcx
unsigned int v7; // er8
__int64 result; // rax
__int64 v9; // rax

v3 = inat_escape_tables[4 * (int)inat_escape_id(a3)];
if ( !v3 )
goto LABEL_7;
if ( !(unsigned int)inat_has_variant(*(_DWORD *)(v3 + 4LL * v4)) || !a2 )
return v7;
v9 = inat_escape_tables[4 * v5 + a2];
if ( v9 )
result = *(unsigned int *)(v9 + 4 * v6);
else
LABEL_7:
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2, unsigned int a3)
{
__int64 v3; // rax
unsigned __int8 v4; // cl
__int64 v5; // rdx
__int64 v6; // rcx
unsigned int v7; // er8
__int64 result; // rax
__int64 v9; // rax

v3 = inat_escape_tables[4 * (int)func1(a3)];
if ( !v3 )
goto LABEL_7;
if ( !(unsigned int)func5(*(_DWORD *)(v3 + 4LL * v4)) || !a2 )
return v7;
v9 = inat_escape_tables[4 * v5 + a2];
if ( v9 )
result = *(unsigned int *)(v9 + 4 * v6);
else
LABEL_7:
result = 0LL;
return result;
}

original_code:
insn_attr_t inat_get_escape_attribute(insn_byte_t opcode, int lpfx_id,
          insn_attr_t esc_attr)
{
 const insn_attr_t *table;
 int n;
 n = inat_escape_id(esc_attr);
 table = inat_escape_tables[n][0];
 if (!table)
  return 0;
 if (inat_has_variant(table[opcode]) && lpfx_id) {
  table = inat_escape_tables[n][lpfx_id];
  if (!table)
   return 0;
 }
 return table[opcode];
}

canonical_original_code:
unsigned int inat_get_escape_attribute(unsigned char opcode, int lpfx_id, unsigned int esc_attr)
{
 unsigned int *table;
 int n;
 n = inat_escape_id(esc_attr);
 table = inat_escape_tables[n][0];
 if (!table)
  return 0;
 if (inat_has_variant(table[opcode]) && lpfx_id) {
  table = inat_escape_tables[n][lpfx_id];
  if (!table)
   return 0;
 }
 return table[opcode];
}

variable_types:
table
unsigned int *

variable_types: n int


decompiled_code:
__int64 __fastcall inat_get_group_attribute(__int64 a1, int a2, unsigned int a3)
{
unsigned int v4; // ecx
unsigned int v5; // er8
__int64 v6; // r9
int v7; // edi
__int64 v8; // rdx
__int64 v9; // rcx
__int64 v11; // rax

inat_group_id(a3);
v5 = inat_group_common_attribute(a3);
if ( v6 )
{
v7 = *(_DWORD *)(v6 + 4LL * ((v4 >> 3) & 7));
if ( (unsigned int)inat_has_variant(v7) && a2 )
{
v11 = inat_group_tables[4 * v8 + a2];
if ( !v11 )
return v5;
v7 = *(_DWORD *)(v11 + 4 * v9);
}
v5 |= v7;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, unsigned int a3)
{
unsigned int v4; // ecx
unsigned int v5; // er8
__int64 v6; // r9
int v7; // edi
__int64 v8; // rdx
__int64 v9; // rcx
__int64 v11; // rax

func3(a3);
v5 = func4(a3);
if ( v6 )
{
v7 = *(_DWORD *)(v6 + 4LL * ((v4 >> 3) & 7));
if ( (unsigned int)func5(v7) && a2 )
{
v11 = inat_group_tables[4 * v8 + a2];
if ( !v11 )
return v5;
v7 = *(_DWORD *)(v11 + 4 * v9);
}
v5 |= v7;
}
return v5;
}

original_code:
insn_attr_t inat_get_group_attribute(insn_byte_t modrm, int lpfx_id,
         insn_attr_t grp_attr)
{
 const insn_attr_t *table;
 int n;
 n = inat_group_id(grp_attr);
 table = inat_group_tables[n][0];
 if (!table)
  return inat_group_common_attribute(grp_attr);
 if (inat_has_variant(table[(((modrm) & 0x38) >> 3)]) && lpfx_id) {
  table = inat_group_tables[n][lpfx_id];
  if (!table)
   return inat_group_common_attribute(grp_attr);
 }
 return table[(((modrm) & 0x38) >> 3)] |
        inat_group_common_attribute(grp_attr);
}

canonical_original_code:
unsigned int inat_get_group_attribute(unsigned char modrm, int lpfx_id, unsigned int grp_attr)
{
 unsigned int *table;
 int n;
 n = inat_group_id(grp_attr);
 table = inat_group_tables[n][0];
 if (!table)
  return inat_group_common_attribute(grp_attr);
 if (inat_has_variant(table[(((modrm) & 0x38) >> 3)]) && lpfx_id) {
  table = inat_group_tables[n][lpfx_id];
  if (!table)
   return inat_group_common_attribute(grp_attr);
 }
 return table[(((modrm) & 0x38) >> 3)] |
        inat_group_common_attribute(grp_attr);
}

variable_types:
modrm
unsigned char


decompiled_code:
__int64 __fastcall inat_get_avx_attribute(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 v3; // rax
__int64 v4; // rdx
unsigned __int8 v5; // cl
unsigned int v6; // er8
__int64 result; // rax
__int64 v8; // rax

if ( a2 > 0x1Fu )
goto LABEL_7;
if ( a3 > 3u )
goto LABEL_7;
v3 = inat_avx_tables[4 * a2];
if ( !v3 )
goto LABEL_7;
if ( (unsigned int)inat_is_group(*(_DWORD *)(v3 + 4LL * a1)) || !v5 )
return v6;
v8 = inat_avx_tables[4 * a2 + v5];
if ( v8 )
result = *(unsigned int *)(v8 + 4 * v4);
else
LABEL_7:
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
__int64 v3; // rax
__int64 v4; // rdx
unsigned __int8 v5; // cl
unsigned int v6; // er8
__int64 result; // rax
__int64 v8; // rax

if ( a2 > 0x1Fu )
goto LABEL_7;
if ( a3 > 3u )
goto LABEL_7;
v3 = inat_avx_tables[4 * a2];
if ( !v3 )
goto LABEL_7;
if ( (unsigned int)func2(*(_DWORD *)(v3 + 4LL * a1)) || !v5 )
return v6;
v8 = inat_avx_tables[4 * a2 + v5];
if ( v8 )
result = *(unsigned int *)(v8 + 4 * v4);
else
LABEL_7:
result = 0LL;
return result;
}

original_code:
insn_attr_t inat_get_avx_attribute(insn_byte_t opcode, insn_byte_t vex_m,
       insn_byte_t vex_p)
{
 const insn_attr_t *table;
 if (vex_m > 0x1f || vex_p > 3)
  return 0;
 table = inat_avx_tables[vex_m][0];
 if (!table)
  return 0;
 if (!inat_is_group(table[opcode]) && vex_p) {
  table = inat_avx_tables[vex_m][vex_p];
  if (!table)
   return 0;
 }
 return table[opcode];
}

canonical_original_code:
unsigned int inat_get_avx_attribute(unsigned char opcode, unsigned char vex_m, unsigned char vex_p)
{
 unsigned int *table;
 if (vex_m > 0x1f || vex_p > 3)
  return 0;
 table = inat_avx_tables[vex_m][0];
 if (!table)
  return 0;
 if (!inat_is_group(table[opcode]) && vex_p) {
  table = inat_avx_tables[vex_m][vex_p];
  if (!table)
   return 0;
 }
 return table[opcode];
}


decompiled_code:
void __noreturn qbbs_chat_exit()
{
int v0; // ebx
int v1; // edi
_QWORD *v2; // rbx
void *v3; // rdi

v0 = 0;
loginfo("<qbbschat> shutting down");
do
{
v1 = v0++;
close(v1);
}
while ( v0 != 256 );
v2 = chatlist_top;
chatlist = chatlist_top;
if ( chatlist_top )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[134];
free(v3);
chatlist = v2;
}
while ( v2 );
}
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
int v0; // ebx
int v1; // edi
_QWORD *v2; // rbx
void *v3; // rdi

v0 = 0;
loginfo("<qbbschat> shutting down");
do
{
v1 = v0++;
close(v1);
}
while ( v0 != 256 );
v2 = chatlist_top;
chatlist = chatlist_top;
if ( chatlist_top )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[134];
free(v3);
chatlist = v2;
}
while ( v2 );
}
exit(0);
}

original_code:
void qbbs_chat_exit()
{
    int fd;
    struct chat_list *ptr;
    loginfo("<qbbschat> shutting down");
    for (fd = 0; fd < 256; fd++)
        close(fd);
    chatlist = chatlist_top;
    while(chatlist != ((void *)0))
    {
        ptr = chatlist->next;
        free(chatlist);
        chatlist = ptr;
    }
    exit(0);
}

variable_types: fd int

variable_types: struct chat_list { struct qbbs_chat_list { unsigned char command; char name[26]; long type; char data[1024]; } chat; unsigned char available; struct chat_list *next; } *

user_defined_types: struct chat_list { struct qbbs_chat_list chat; unsigned char available; struct chat_list *next; }


repo: dywisor/diskid

decompiled_code:
void __fastcall set_disk_type_none(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 0;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 0;
}

original_code:
static inline void set_disk_type_none ( struct disk_info* const pnode ) {
   pnode->type = DISK_TYPE_NONE;
}

canonical_original_code:
static inline void set_disk_type_none(struct disk_info *pnode) {
   pnode->type = DISK_TYPE_NONE;
}

variable_types: struct disk_info { char *device; char *name; char *var_name; enum disk_type { DISK_TYPE_NONE=0, DISK_TYPE_ATA=1, DISK_TYPE_SCSI=2, DISK_TYPE_ALL=expr } type; int fd; char *disk_id; } *

user_defined_types: struct disk_info { char *device; char *name; char *var_name; enum disk_type type; int fd; char *disk_id; }


decompiled_code:
void __fastcall set_disk_type_ata(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 1;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
*(_DWORD *)(a1 + 24) = 1;
}

original_code:
static inline void set_disk_type_ata ( struct disk_info* const pnode ) {
   pnode->type = DISK_TYPE_ATA;
}

canonical_original_code:
static inline void set_disk_type_ata(struct disk_info *pnode) {
   pnode->type = DISK_TYPE_ATA;
}

user_defined_types: enum disk_type { DISK_TYPE_NONE=0, DISK_TYPE_ATA=1, DISK_TYPE_SCSI=2, DISK_TYPE_ALL=expr }


decompiled_code:
void __fastcall close_disk_info(void *ptr)
{
int v2; // edi
void *v3; // rdi
void *v4; // rdi

v2 = *((_DWORD *)ptr + 7);
if ( v2 >= 0 )
close(v2);
v3 = (void *)*((_QWORD *)ptr + 4);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
int v2; // edi
void *v3; // rdi
void *v4; // rdi

v2 = *((_DWORD *)ptr + 7);
if ( v2 >= 0 )
close(v2);
v3 = (void *)*((_QWORD *)ptr + 4);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
free(ptr);
}

original_code:
static inline void close_disk_info ( struct disk_info* pnode ) {
   if ( pnode->fd >= 0 ) {
      close ( pnode->fd );
      pnode->fd = -1;
   }
   if ( pnode->disk_id != ((void *)0) ) {
      free ( pnode->disk_id );
      pnode->disk_id = ((void *)0);
   }
   if ( pnode->var_name != ((void *)0) ) {
      free ( pnode->var_name );
   }
   free(pnode);
}

canonical_original_code:
static inline void close_disk_info(struct disk_info *pnode) {
   if ( pnode->fd >= 0 ) {
      close ( pnode->fd );
      pnode->fd = -1;
   }
   if ( pnode->disk_id != ((void *)0) ) {
      free ( pnode->disk_id );
      pnode->disk_id = ((void *)0);
   }
   if ( pnode->var_name != ((void *)0) ) {
      free ( pnode->var_name );
   }
   free(pnode);
}


decompiled_code:
char *__fastcall get_uppercase(char *s)
{
char *v1; // r12
__int64 v2; // rax

v1 = strdup(s);
v2 = strlen(s);
convert_to_uppercase_constprop_0(v1, v2);
return v1;
}

canonical_decompiled_code:
char *__fastcall func5(char *s)
{
char *v1; // r12
__int64 v2; // rax

v1 = strdup(s);
v2 = strlen(s);
func4(v1, v2);
return v1;
}

original_code:
static inline char* get_uppercase ( const char* const str ) {
   char* upstr;
   upstr = strdup ( str );
   convert_to_uppercase ( upstr, 0, strlen ( str ) );
   return upstr;
}

canonical_original_code:
static inline char *get_uppercase(char *str) {
   char *upstr;
   upstr = strdup ( str );
   convert_to_uppercase ( upstr, 0, strlen ( str ) );
   return upstr;
}

variable_types:
str
char *

variable_types:
upstr
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
unsigned int v4; // er13
int v5; // er12
int v6; // eax
int v7; // ebx
char *v9; // rax
int v10; // er15
const char **v11; // r14
_DWORD *v12; // rax
void *v13; // rbp
const char **v14; // [rsp+8h] [rbp-40h]

v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getopt_long(argc, (char *const *)argv, "xhm", &long_options_0, 0LL);
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'm':
v4 = 1;
break;
case 'x':
v5 = 1;
break;
case 'h':
v3 = 1;
v9 = _xpg_basename((char *)*argv);
fprintf(
stdout,
"Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
" -h, --help print this help message and exit\n"
" -x, --export print environment variables\n"
" -m, --mdev print environment variables for mdev\n"
"\n",
v9);
break;
default:
goto LABEL_6;
}
}
if ( v3 != 1 )
{
if ( optind >= argc )
{
fwrite("no device specified\n", 1uLL, 0x14uLL, stderr);
}
else
{
v10 = argc - optind;
v11 = &argv[optind];
v14 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
while ( 1 )
{
v12 = init_disk_info((char *)*v11);
v13 = v12;
if ( !v12 )
break;
v7 = handle_device_constprop_0((__int64)v12, v5, v4, v10);
if ( v7 )
{
v7 = 1;
fflush(stdout);
fflush(stderr);
goto LABEL_18;
}
if ( ++v11 == v14 )
{
fflush(stdout);
fflush(stderr);
LABEL_18:
close_disk_info(v13);
return v7;
}
}
fprintf(stderr, "failed to open device '%s'\n", *v11);
}
LABEL_6:
v7 = 1;
fflush(stdout);
fflush(stderr);
return v7;
}
v7 = 0;
fflush(stdout);
fflush(stderr);
return v7;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
unsigned int v4; // er13
int v5; // er12
int v6; // eax
int v7; // ebx
char *v9; // rax
int v10; // er15
const char **v11; // r14
_DWORD *v12; // rax
void *v13; // rbp
const char **v14; // [rsp+8h] [rbp-40h]

v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = getopt_long(argc, (char *const *)argv, "xhm", &long_options_0, 0LL);
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'm':
v4 = 1;
break;
case 'x':
v5 = 1;
break;
case 'h':
v3 = 1;
v9 = _xpg_basename((char *)*argv);
fprintf(
stdout,
"Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
" -h, --help print this help message and exit\n"
" -x, --export print environment variables\n"
" -m, --mdev print environment variables for mdev\n"
"\n",
v9);
break;
default:
goto LABEL_6;
}
}
if ( v3 != 1 )
{
if ( optind >= argc )
{
fwrite("no device specified\n", 1uLL, 0x14uLL, stderr);
}
else
{
v10 = argc - optind;
v11 = &argv[optind];
v14 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
while ( 1 )
{
v12 = init_disk_info((char *)*v11);
v13 = v12;
if ( !v12 )
break;
v7 = func7((__int64)v12, v5, v4, v10);
if ( v7 )
{
v7 = 1;
fflush(stdout);
fflush(stderr);
goto LABEL_18;
}
if ( ++v11 == v14 )
{
fflush(stdout);
fflush(stderr);
LABEL_18:
func2(v13);
return v7;
}
}
fprintf(stderr, "failed to open device '%s'\n", *v11);
}
LABEL_6:
v7 = 1;
fflush(stdout);
fflush(stderr);
return v7;
}
v7 = 0;
fflush(stdout);
fflush(stderr);
return v7;
}

original_code:
int main ( const int argc, char* const* argv ) {
   int retcode = 0;
   struct disk_info* node = ((void *)0);
   int i;
   unsigned int node_count;
   unsigned int exit_after_getopt;
   unsigned int want_export;
   unsigned int want_mdev_export;
   static const struct option const long_options[] = {
      { "export", 0, ((void *)0), 'x' },
      { "mdev", 0, ((void *)0), 'm' },
      { "help", 0, ((void *)0), 'h' },
      {0}
   };
   exit_after_getopt = 0;
   want_export = 0;
   want_mdev_export = 0;
   while (
      ( i = getopt_long ( argc, argv, "xhm", long_options, ((void *)0) ) ) != -1
   ) {
      switch ( i ) {
         case 'h':
            fprintf ( stdout,
               (
                  "Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
                  "  -h, --help           print this help message and exit\n"
                  "  -x, --export         print environment variables\n"
                  "  -m, --mdev           print environment variables for mdev\n"
                  "\n"
               ), __xpg_basename(argv[0])
            );
            exit_after_getopt = 1;
            break;
         case 'x':
            want_export = 1;
            break;
         case 'm':
            want_mdev_export = 1;
            break;
         default:
            retcode = 1;
            goto main_exit;
      }
   }
   if ( exit_after_getopt == 1 ) {
      goto main_exit;
   } else if ( optind < argc ) {
      node_count = (unsigned int)(argc - optind);
      for ( i = optind; i < argc; i++ ) {
         node = init_disk_info ( argv[i] );
         if ( node == ((void *)0) ) {
            fprintf ( stderr, "failed to open device '%s'\n", argv[i] );
            retcode = 1;
            goto main_exit;
         } else if (
            handle_device (
               node, want_export, want_mdev_export, DISK_TYPE_ALL, node_count
            ) != 0
         ) {
            retcode = 1;
            goto main_exit;
         }
      }
   } else {
      fprintf ( stderr, "no device specified\n" );
      retcode = 1;
      goto main_exit;
   }
main_exit:
   fflush ( stdout );
   fflush ( stderr );
   if ( node != ((void *)0) ) {
      close_disk_info ( node );
      node = ((void *)0);
   }
   return retcode;
}

canonical_original_code:
int main(int argc, char **argv) {
   int retcode = 0;
   struct disk_info *node = ((void *)0);
   int i;
   unsigned int node_count;
   unsigned int exit_after_getopt;
   unsigned int want_export;
   unsigned int want_mdev_export;
   static struct option long_options[] = {
      { "export", 0, ((void *)0), 'x' },
      { "mdev", 0, ((void *)0), 'm' },
      { "help", 0, ((void *)0), 'h' },
      {0}
   };
   exit_after_getopt = 0;
   want_export = 0;
   want_mdev_export = 0;
   while (
      ( i = getopt_long ( argc, argv, "xhm", long_options, ((void *)0) ) ) != -1
   ) {
      switch ( i ) {
         case 'h':
            fprintf ( stdout,
               (
                  "Usage: %s [-h] [-x] [-m] [<DEVICE>...]\n"
                  "  -h, --help           print this help message and exit\n"
                  "  -x, --export         print environment variables\n"
                  "  -m, --mdev           print environment variables for mdev\n"
                  "\n"
               ), __xpg_basename(argv[0])
            );
            exit_after_getopt = 1;
            break;
         case 'x':
            want_export = 1;
            break;
         case 'm':
            want_mdev_export = 1;
            break;
         default:
            retcode = 1;
            goto main_exit;
      }
   }
   if ( exit_after_getopt == 1 ) {
      goto main_exit;
   } else if ( optind < argc ) {
      node_count = (unsigned int)(argc - optind);
      for ( i = optind; i < argc; i++ ) {
         node = init_disk_info ( argv[i] );
         if ( node == ((void *)0) ) {
            fprintf ( stderr, "failed to open device '%s'\n", argv[i] );
            retcode = 1;
            goto main_exit;
         } else if (
            handle_device (
               node, want_export, want_mdev_export, DISK_TYPE_ALL, node_count
            ) != 0
         ) {
            retcode = 1;
            goto main_exit;
         }
      }
   } else {
      fprintf ( stderr, "no device specified\n" );
      retcode = 1;
      goto main_exit;
   }
main_exit:
   fflush ( stdout );
   fflush ( stderr );
   if ( node != ((void *)0) ) {
      close_disk_info ( node );
      node = ((void *)0);
   }
   return retcode;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: retcode int

variable_types: i int

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


decompiled_code:
__int64 __fastcall char_in_range(char a1, char a2, char a3)
{
return (a1 > a3) | (unsigned __int8)(a1 < a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1, char a2, char a3)
{
return (a1 > a3) | (unsigned __int8)(a1 < a2);
}

original_code:
static inline int char_in_range (
   const char c, const char low, const char high
) {
   return ( c >= low && c <= high ) ? 0 : 1;
}

canonical_original_code:
static inline int char_in_range(char c, char low, char high) {
   return ( c >= low && c <= high ) ? 0 : 1;
}

variable_types: c char

variable_types: low char

variable_types: high char


decompiled_code:
_BOOL8 __fastcall is_unicode_valid(unsigned int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 <= 0x10FFFF && (a1 & 0xFFFFF800) != 55296 && a1 - 64976 > 0x1F )
result = (a1 & 0xFFFE) != 65534;
return result;
}

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

result = 0LL;
if ( a1 <= 0x10FFFF && (a1 & 0xFFFFF800) != 55296 && a1 - 64976 > 0x1F )
result = (a1 & 0xFFFE) != 65534;
return result;
}

original_code:
static inline int is_unicode_valid(const uint32_t ch) {
        if (ch >= 0x110000)
                return 0;
        if ((ch & 0xFFFFF800) == 0xD800)
                return 0;
        if ((ch >= 0xFDD0) && (ch <= 0xFDEF))
                return 0;
        if ((ch & 0xFFFE) == 0xFFFE)
                return 0;
        return 1;
}

canonical_original_code:
static inline int is_unicode_valid(unsigned int ch) {
        if (ch >= 0x110000)
                return 0;
        if ((ch & 0xFFFFF800) == 0xD800)
                return 0;
        if ((ch >= 0xFDD0) && (ch <= 0xFDEF))
                return 0;
        if ((ch & 0xFFFE) == 0xFFFE)
                return 0;
        return 1;
}

variable_types:
ch
unsigned int


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

result = 1LL;
if ( a1 > 127 )
{
result = 2LL;
if ( a1 > 2047 )
{
result = 3LL;
if ( a1 > 0xFFFF )
{
result = 4LL;
if ( a1 > 0x1FFFFF )
result = (unsigned int)(a1 > 0x3FFFFFF) + 5;
}
}
}
return result;
}

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

result = 1LL;
if ( a1 > 127 )
{
result = 2LL;
if ( a1 > 2047 )
{
result = 3LL;
if ( a1 > 0xFFFF )
{
result = 4LL;
if ( a1 > 0x1FFFFF )
result = (unsigned int)(a1 > 0x3FFFFFF) + 5;
}
}
}
return result;
}

original_code:
static int utf8_unichar_to_encoded_len(const int unichar) {
        if (unichar < 0x80)
                return 1;
        if (unichar < 0x800)
                return 2;
        if (unichar < 0x10000)
                return 3;
        if (unichar < 0x200000)
                return 4;
        if (unichar < 0x4000000)
                return 5;
        return 6;
}

canonical_original_code:
static int utf8_unichar_to_encoded_len(int unichar) {
        if (unichar < 0x80)
                return 1;
        if (unichar < 0x800)
                return 2;
        if (unichar < 0x10000)
                return 3;
        if (unichar < 0x200000)
                return 4;
        if (unichar < 0x4000000)
                return 5;
        return 6;
}

variable_types: unichar int


decompiled_code:
// attributes: thunk
size_t util_strnlen(const char *string, size_t maxlen)
{
return strnlen(string, maxlen);
}

canonical_decompiled_code:
size_t func3(const char *string, size_t maxlen)
{
return strnlen(string, maxlen);
}

original_code:
static inline size_t util_strnlen ( const char* s, size_t maxlen ) {
   return strnlen ( s, maxlen );
}

canonical_original_code:
static inline unsigned long util_strnlen(char *s, unsigned long maxlen) {
   return strnlen ( s, maxlen );
}

variable_types:
s
char *

variable_types:
maxlen
unsigned long


decompiled_code:
__int64 __fastcall whitelisted_char_for_devnode(char a1, const char *a2)
{
unsigned int v2; // er12

v2 = 1;
if ( (unsigned int)char_in_range(a1, 48, 57) || (unsigned int)char_in_range(a1, 65, 90) )
return v2;
v2 = char_in_range(a1, 97, 122);
if ( !v2 && !strchr("#+-.:=@_", a1) )
{
if ( a2 )
v2 = strchr(a2, a1) != 0LL;
return v2;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1, const char *a2)
{
unsigned int v2; // er12

v2 = 1;
if ( (unsigned int)func0(a1, 48, 57) || (unsigned int)func0(a1, 65, 90) )
return v2;
v2 = func0(a1, 97, 122);
if ( !v2 && !strchr("#+-.:=@_", a1) )
{
if ( a2 )
v2 = strchr(a2, a1) != 0LL;
return v2;
}
return 1LL;
}

original_code:
static inline int whitelisted_char_for_devnode (
   const char c, const char* const white
) {
   return (
      char_in_range ( c, '0', '9' ) ||
      char_in_range ( c, 'A', 'Z' ) ||
      char_in_range ( c, 'a', 'z' ) ||
      strchr("#+-.:=@_", c) ||
      ( white != ((void *)0) && strchr(white, c) != ((void *)0) )
   ) ? 1 : 0;
}

canonical_original_code:
static inline int whitelisted_char_for_devnode(char c, char *white) {
   return (
      char_in_range ( c, '0', '9' ) ||
      char_in_range ( c, 'A', 'Z' ) ||
      char_in_range ( c, 'a', 'z' ) ||
      strchr("#+-.:=@_", c) ||
      ( white != ((void *)0) && strchr(white, c) != ((void *)0) )
   ) ? 1 : 0;
}

variable_types:
white
char *


decompiled_code:
__int64 __fastcall utf8_encoded_to_unichar(char *a1)
{
int v1; // esi
unsigned int v2; // er8
__int64 v3; // r9
__int64 v4; // rdx
char v5; // al

v1 = utf8_encoded_expected_len_isra_0(*a1);
switch ( v1 )
{
case 1:
return v2;
case 2:
v2 &= 0x1Fu;
goto LABEL_3;
case 3:
v2 &= 0xFu;
goto LABEL_3;
case 4:
v2 &= 7u;
goto LABEL_3;
case 5:
v2 &= 3u;
goto LABEL_3;
case 6:
v2 &= 1u;
LABEL_3:
v4 = 1LL;
break;
default:
return (unsigned int)-1;
}
while ( 1 )
{
v5 = *(_BYTE *)(v3 + v4);
if ( (v5 & 0xC0) != 128 )
break;
++v4;
v2 = v5 & 0x3F | (v2 << 6);
if ( v1 <= (int)v4 )
return v2;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1)
{
int v1; // esi
unsigned int v2; // er8
__int64 v3; // r9
__int64 v4; // rdx
char v5; // al

v1 = func5(*a1);
switch ( v1 )
{
case 1:
return v2;
case 2:
v2 &= 0x1Fu;
goto LABEL_3;
case 3:
v2 &= 0xFu;
goto LABEL_3;
case 4:
v2 &= 7u;
goto LABEL_3;
case 5:
v2 &= 3u;
goto LABEL_3;
case 6:
v2 &= 1u;
LABEL_3:
v4 = 1LL;
break;
default:
return (unsigned int)-1;
}
while ( 1 )
{
v5 = *(_BYTE *)(v3 + v4);
if ( (v5 & 0xC0) != 128 )
break;
++v4;
v2 = v5 & 0x3F | (v2 << 6);
if ( v1 <= (int)v4 )
return v2;
}
return (unsigned int)-1;
}

original_code:
static int utf8_encoded_to_unichar(const char* const str) {
        int unichar;
        int len;
        int i;
        len = utf8_encoded_expected_len(str);
        switch (len) {
        case 1:
                return (int)str[0];
        case 2:
                unichar = str[0] & 0x1f;
                break;
        case 3:
                unichar = (int)str[0] & 0x0f;
                break;
        case 4:
                unichar = (int)str[0] & 0x07;
                break;
        case 5:
                unichar = (int)str[0] & 0x03;
                break;
        case 6:
                unichar = (int)str[0] & 0x01;
                break;
        default:
                return -1;
        }
        for (i = 1; i < len; i++) {
                if (((int)str[i] & 0xc0) != 0x80)
                        return -1;
                unichar <<= 6;
                unichar |= (int)str[i] & 0x3f;
        }
        return unichar;
}

canonical_original_code:
static int utf8_encoded_to_unichar(char *str) {
        int unichar;
        int len;
        int i;
        len = utf8_encoded_expected_len(str);
        switch (len) {
        case 1:
                return (int)str[0];
        case 2:
                unichar = str[0] & 0x1f;
                break;
        case 3:
                unichar = (int)str[0] & 0x0f;
                break;
        case 4:
                unichar = (int)str[0] & 0x07;
                break;
        case 5:
                unichar = (int)str[0] & 0x03;
                break;
        case 6:
                unichar = (int)str[0] & 0x01;
                break;
        default:
                return -1;
        }
        for (i = 1; i < len; i++) {
                if (((int)str[i] & 0xc0) != 0x80)
                        return -1;
                unichar <<= 6;
                unichar |= (int)str[i] & 0x3f;
        }
        return unichar;
}

variable_types: len int


decompiled_code:
__int64 __fastcall utf8_encoded_valid_unichar(char *a1)
{
int v1; // eax
char v2; // cl
char *v3; // r8
unsigned int v4; // er10
char *v5; // rax
unsigned int v7; // edi
int v8; // eax
int v9; // er10
unsigned int v10; // er10

v1 = utf8_encoded_expected_len_isra_0(*a1);
v4 = v1;
if ( !v1 )
return (unsigned int)-1;
if ( v1 == 1 )
return v4;
if ( v1 > 0 )
{
v5 = v3;
while ( v2 < 0 )
{
if ( v5 == &v3[v4 - 1] )
goto LABEL_10;
v2 = *++v5;
}
return (unsigned int)-1;
}
LABEL_10:
v7 = utf8_encoded_to_unichar(v3);
v8 = utf8_unichar_to_encoded_len(v7);
if ( v8 != v9 )
return (unsigned int)-1;
if ( !is_unicode_valid(v7) )
v10 = -1;
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1)
{
int v1; // eax
char v2; // cl
char *v3; // r8
unsigned int v4; // er10
char *v5; // rax
unsigned int v7; // edi
int v8; // eax
int v9; // er10
unsigned int v10; // er10

v1 = func5(*a1);
v4 = v1;
if ( !v1 )
return (unsigned int)-1;
if ( v1 == 1 )
return v4;
if ( v1 > 0 )
{
v5 = v3;
while ( v2 < 0 )
{
if ( v5 == &v3[v4 - 1] )
goto LABEL_10;
v2 = *++v5;
}
return (unsigned int)-1;
}
LABEL_10:
v7 = func6(v3);
v8 = func2(v7);
if ( v8 != v9 )
return (unsigned int)-1;
if ( !func1(v7) )
v10 = -1;
return v10;
}

original_code:
static int utf8_encoded_valid_unichar(const char* const str) {
        int len;
        int unichar;
        int i;
        len = utf8_encoded_expected_len(str);
        if (len == 0)
                return -1;
        if (len == 1)
                return 1;
        for (i = 0; i < len; i++)
                if ((str[i] & 0x80) != 0x80)
                        return -1;
        unichar = utf8_encoded_to_unichar(str);
        if (utf8_unichar_to_encoded_len(unichar) != len)
                return -1;
        if (is_unicode_valid(unichar) == 0)
                return -1;
        return len;
}

canonical_original_code:
static int utf8_encoded_valid_unichar(char *str) {
        int len;
        int unichar;
        int i;
        len = utf8_encoded_expected_len(str);
        if (len == 0)
                return -1;
        if (len == 1)
                return 1;
        for (i = 0; i < len; i++)
                if ((str[i] & 0x80) != 0x80)
                        return -1;
        unichar = utf8_encoded_to_unichar(str);
        if (utf8_unichar_to_encoded_len(unichar) != len)
                return -1;
        if (is_unicode_valid(unichar) == 0)
                return -1;
        return len;
}


original_code:
int util_replace_whitespace (
   const char* const str, char* const to, size_t len
) {
        size_t i, j;
        len = util_strnlen(str, len);
        while (len && ((*__ctype_b_loc ())[(int) ((str[len-1]))] & (unsigned short int) _ISspace))
                len--;
        i = 0;
        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace) && (i < len))
                i++;
        j = 0;
        while (i < len) {
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace)) {
                        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace))
                                i++;
                        to[j++] = '_';
                }
                to[j++] = str[i++];
        }
        to[j] = '\0';
        return 0;
}

canonical_original_code:
int util_replace_whitespace(char *str, char *to, unsigned long len) {
        unsigned long i; unsigned long j;
        len = util_strnlen(str, len);
        while (len && ((*__ctype_b_loc ())[(int) ((str[len-1]))] & (unsigned short) _ISspace))
                len--;
        i = 0;
        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace) && (i < len))
                i++;
        j = 0;
        while (i < len) {
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace)) {
                        while (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace))
                                i++;
                        to[j++] = '_';
                }
                to[j++] = str[i++];
        }
        to[j] = '\0';
        return 0;
}

variable_types:
to
char *

variable_types:
len
unsigned long

variable_types:
i
unsigned long

variable_types:
j
unsigned long


decompiled_code:
__int64 __fastcall util_replace_chars(char *a1, const char *a2)
{
__int64 v2; // rbx
char *v3; // r12
unsigned int v4; // er15
__int64 v5; // rbp
int v6; // eax

v2 = *a1;
if ( !(_BYTE)v2 )
return 0;
v3 = a1;
v4 = 0;
v5 = 0LL;
do
{
while ( (unsigned int)whitelisted_char_for_devnode(v2, a2) )
{
v3 = &a1[++v5];
v2 = a1[v5];
if ( !(_BYTE)v2 )
return v4;
}
if ( (_BYTE)v2 == 92 && a1[v5 + 1] == 120 )
{
v5 += 2LL;
}
else
{
v6 = utf8_encoded_valid_unichar(v3);
if ( v6 > 1 )
{
v5 += v6;
}
else
{
++v5;
++v4;
if ( ((*_ctype_b_loc())[v2] & 0x2000) != 0 && a2 && strchr(a2, 32) )
*v3 = 32;
else
*v3 = 95;
}
}
v3 = &a1[v5];
v2 = a1[v5];
}
while ( (_BYTE)v2 );
return v4;
}

original_code:
int util_replace_chars ( char* const str, const char* const white ) {
        size_t i = 0;
        int replaced = 0;
        while (str[i] != '\0') {
                int len;
                if (whitelisted_char_for_devnode(str[i], white)) {
                        i++;
                        continue;
                }
                if (str[i] == '\\' && str[i+1] == 'x') {
                        i += 2;
                        continue;
                }
                len = utf8_encoded_valid_unichar(&str[i]);
                if (len > 1) {
                        i += len;
                        continue;
                }
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace) && white != ((void *)0) && strchr(white, ' ') != ((void *)0)) {
                        str[i] = ' ';
                        i++;
                        replaced++;
                        continue;
                }
                str[i] = '_';
                i++;
                replaced++;
        }
        return replaced;
}

canonical_original_code:
int util_replace_chars(char *str, char *white) {
        unsigned long i = 0;
        int replaced = 0;
        while (str[i] != '\0') {
                int len;
                if (whitelisted_char_for_devnode(str[i], white)) {
                        i++;
                        continue;
                }
                if (str[i] == '\\' && str[i+1] == 'x') {
                        i += 2;
                        continue;
                }
                len = utf8_encoded_valid_unichar(&str[i]);
                if (len > 1) {
                        i += len;
                        continue;
                }
                if (((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace) && white != ((void *)0) && strchr(white, ' ') != ((void *)0)) {
                        str[i] = ' ';
                        i++;
                        replaced++;
                        continue;
                }
                str[i] = '_';
                i++;
                replaced++;
        }
        return replaced;
}

variable_types:
str
char *

variable_types: replaced int


decompiled_code:
__int64 __fastcall encode_devnode_name(unsigned __int8 *a1, __int64 a2, __int64 a3)
{
unsigned __int8 v3; // r15
char *v4; // r11
__int64 v5; // r12
__int64 v6; // rbp
__int64 v7; // r15
int v8; // eax
const void *v9; // r11
int v10; // ebx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v3 = *a1;
if ( *a1 )
{
v4 = (char *)a1;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v8 = utf8_encoded_valid_unichar(v4);
v10 = v8;
if ( v8 > 1 )
{
v7 = v8;
if ( a3 - v5 < (unsigned __int64)v8 )
return 0xFFFFFFFFLL;
memcpy((void *)(a2 + v5), v9, v8);
v5 += v7;
v6 += v10 - 1;
}
else if ( v3 == 92 || !(unsigned int)whitelisted_char_for_devnode(v3, 0LL) )
{
if ( (unsigned __int64)(a3 - v5) <= 3 )
return 0xFFFFFFFFLL;
sprintf((char *)(a2 + v5), "\\x%02x", v3);
v5 += 4LL;
}
else
{
if ( a3 == v5 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v5++) = v3;
}
v4 = (char *)&a1[++v6];
v3 = a1[v6];
if ( !v3 )
goto LABEL_16;
}
}
v5 = 0LL;
LABEL_16:
if ( a3 == v5 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v5) = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 *a1, __int64 a2, __int64 a3)
{
unsigned __int8 v3; // r15
char *v4; // r11
__int64 v5; // r12
__int64 v6; // rbp
__int64 v7; // r15
int v8; // eax
const void *v9; // r11
int v10; // ebx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v3 = *a1;
if ( *a1 )
{
v4 = (char *)a1;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v8 = func7(v4);
v10 = v8;
if ( v8 > 1 )
{
v7 = v8;
if ( a3 - v5 < (unsigned __int64)v8 )
return 0xFFFFFFFFLL;
memcpy((void *)(a2 + v5), v9, v8);
v5 += v7;
v6 += v10 - 1;
}
else if ( v3 == 92 || !(unsigned int)func4(v3, 0LL) )
{
if ( (unsigned __int64)(a3 - v5) <= 3 )
return 0xFFFFFFFFLL;
sprintf((char *)(a2 + v5), "\\x%02x", v3);
v5 += 4LL;
}
else
{
if ( a3 == v5 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v5++) = v3;
}
v4 = (char *)&a1[++v6];
v3 = a1[v6];
if ( !v3 )
goto LABEL_16;
}
}
v5 = 0LL;
LABEL_16:
if ( a3 == v5 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a2 + v5) = 0;
return 0LL;
}

original_code:
int encode_devnode_name (
   const char* const str, char* const str_enc, const size_t len
) {
        size_t i, j;
        if (str == ((void *)0) || str_enc == ((void *)0))
                return -1;
        for (i = 0, j = 0; str[i] != '\0'; i++) {
                int seqlen;
                seqlen = utf8_encoded_valid_unichar(&str[i]);
                if (seqlen > 1) {
                        if (len-j < (size_t)seqlen)
                                goto err;
                        memcpy(&str_enc[j], &str[i], seqlen);
                        j += seqlen;
                        i += (seqlen-1);
                } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i],((void *)0))) {
                        if (len-j < 4)
                                goto err;
                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
                        j += 4;
                } else {
                        if (len-j < 1)
                                goto err;
                        str_enc[j] = str[i];
                        j++;
                }
        }
        if (len-j < 1)
                goto err;
        str_enc[j] = '\0';
        return 0;
err:
        return -1;
}

canonical_original_code:
int encode_devnode_name(char *str, char *str_enc, unsigned long len) {
        unsigned long i; unsigned long j;
        if (str == ((void *)0) || str_enc == ((void *)0))
                return -1;
        for (i = 0, j = 0; str[i] != '\0'; i++) {
                int seqlen;
                seqlen = utf8_encoded_valid_unichar(&str[i]);
                if (seqlen > 1) {
                        if (len-j < (unsigned long)seqlen)
                                goto err;
                        memcpy(&str_enc[j], &str[i], seqlen);
                        j += seqlen;
                        i += (seqlen-1);
                } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i],((void *)0))) {
                        if (len-j < 4)
                                goto err;
                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
                        j += 4;
                } else {
                        if (len-j < 1)
                                goto err;
                        str_enc[j] = str[i];
                        j++;
                }
        }
        if (len-j < 1)
                goto err;
        str_enc[j] = '\0';
        return 0;
err:
        return -1;
}

variable_types: seqlen int


decompiled_code:
__int64 __fastcall set_disk_id(__int64 a1, _BYTE *a2, char *a3)
{
void *v5; // rdi
__int64 result; // rax
char *v7; // rax

v5 = *(void **)(a1 + 32);
if ( v5 )
{
free(v5);
*(_QWORD *)(a1 + 32) = 0LL;
}
result = 1LL;
if ( a2 && *a2 )
{
if ( a3 && *a3 )
{
v7 = join_str_triple_constprop_0(a2, a3);
*(_QWORD *)(a1 + 32) = v7;
}
else
{
v7 = strdup(a2);
*(_QWORD *)(a1 + 32) = v7;
}
result = 2 * (unsigned int)(v7 == 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _BYTE *a2, char *a3)
{
void *v5; // rdi
__int64 result; // rax
char *v7; // rax

v5 = *(void **)(a1 + 32);
if ( v5 )
{
free(v5);
*(_QWORD *)(a1 + 32) = 0LL;
}
result = 1LL;
if ( a2 && *a2 )
{
if ( a3 && *a3 )
{
v7 = func1(a2, a3);
*(_QWORD *)(a1 + 32) = v7;
}
else
{
v7 = strdup(a2);
*(_QWORD *)(a1 + 32) = v7;
}
result = 2 * (unsigned int)(v7 == 0LL);
}
return result;
}

original_code:
int set_disk_id (
   struct disk_info* const node,
   const char* const model, const char* const serial
) {
   if ( node->disk_id != ((void *)0) ) {
      free ( node->disk_id );
      node->disk_id = ((void *)0);
   }
   if ( model == ((void *)0) || model[0] == '\0' ) {
      return 1;
   } else if ( serial == ((void *)0) || serial[0] == '\0' ) {
      node->disk_id = strdup ( model );
   } else {
      node->disk_id = join_str_triple ( model, "_", serial );
   }
   return ( node->disk_id != ((void *)0) ) ? 0 : 2;
}

canonical_original_code:
int set_disk_id(struct disk_info *node, char *model, char *serial) {
   if ( node->disk_id != ((void *)0) ) {
      free ( node->disk_id );
      node->disk_id = ((void *)0);
   }
   if ( model == ((void *)0) || model[0] == '\0' ) {
      return 1;
   } else if ( serial == ((void *)0) || serial[0] == '\0' ) {
      node->disk_id = strdup ( model );
   } else {
      node->disk_id = join_str_triple ( model, "_", serial );
   }
   return ( node->disk_id != ((void *)0) ) ? 0 : 2;
}

variable_types: struct disk_info { char *device; char *name; char *var_name; enum disk_type { DISK_TYPE_NONE=0, DISK_TYPE_ATA=1, DISK_TYPE_SCSI=2, DISK_TYPE_ALL=expr } type; int fd; char *disk_id; } *

variable_types:
model
char *

variable_types:
serial
char *


repo: dzeban/keymon

decompiled_code:
void __fastcall __noreturn keymon_connect(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_connect();
pthread_mutex_unlock(&sk_mutex);
sleep(5u);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_connect();
pthread_mutex_unlock(&sk_mutex);
sleep(5u);
}
}

original_code:
void *keymon_connect()
{
    while(1)
    {
        pthread_mutex_lock( &sk_mutex );
        nl_sock_connect();
        pthread_mutex_unlock( &sk_mutex );
        sleep(5);
    }
    pthread_exit( ((void *)0) );
}


decompiled_code:
void __fastcall __noreturn keymon_receive(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_receive();
pthread_mutex_unlock(&sk_mutex);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
while ( 1 )
{
pthread_mutex_lock(&sk_mutex);
nl_sock_receive();
pthread_mutex_unlock(&sk_mutex);
}
}

original_code:
void *keymon_receive()
{
    while(1)
    {
        pthread_mutex_lock( &sk_mutex );
        nl_sock_receive();
        pthread_mutex_unlock( &sk_mutex );
    }
    pthread_exit( ((void *)0) );
}


decompiled_code:
__int64 thread_init()
{
int v0; // eax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // er12
pthread_attr_t v7; // [rsp+0h] [rbp-148h] BYREF
sigset_t set; // [rsp+40h] [rbp-108h] BYREF
__sigset_t oldmask; // [rsp+C0h] [rbp-88h] BYREF

v0 = pthread_attr_init(&v7);
if ( v0 < 0 )
{
syslog(3, "pthread_attr_init failed, rc %d\n", (unsigned int)v0);
goto LABEL_10;
}
pthread_attr_setdetachstate(&v7, 0);
pthread_mutex_init(&sk_mutex, 0LL);
sigemptyset(&set);
sigaddset(&set, 10);
v1 = pthread_sigmask(0, &set, &oldmask);
if ( v1 )
goto LABEL_9;
v2 = pthread_create(&connector_thread, &v7, (void *(*)(void *))keymon_connect, 0LL);
if ( v2 )
{
syslog(3, "pthread_create for connector thread failed, rc %d\n", v2);
goto LABEL_10;
}
v3 = pthread_create(&receiver_thread, &v7, (void *(*)(void *))keymon_receive, 0LL);
if ( v3 )
{
syslog(3, "pthread_create for receiver thread failed, rc %d\n", v3);
goto LABEL_10;
}
v1 = pthread_sigmask(2, &oldmask, 0LL);
if ( v1 )
{
LABEL_9:
syslog(3, "pthread_sigmask failed, rc %d\n", v1);
LABEL_10:
v5 = -1;
nl_sock_cleanup();
return v5;
}
v4 = pthread_attr_destroy(&v7);
v5 = v4;
if ( v4 )
{
syslog(3, "pthread_attr_destroy failed, rc %d\n", v4);
goto LABEL_10;
}
pthread_join(connector_thread, 0LL);
pthread_join(receiver_thread, 0LL);
return v5;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // er12
pthread_attr_t v7; // [rsp+0h] [rbp-148h] BYREF
sigset_t set; // [rsp+40h] [rbp-108h] BYREF
__sigset_t oldmask; // [rsp+C0h] [rbp-88h] BYREF

v0 = pthread_attr_init(&v7);
if ( v0 < 0 )
{
syslog(3, "pthread_attr_init failed, rc %d\n", (unsigned int)v0);
goto LABEL_10;
}
pthread_attr_setdetachstate(&v7, 0);
pthread_mutex_init(&sk_mutex, 0LL);
sigemptyset(&set);
sigaddset(&set, 10);
v1 = pthread_sigmask(0, &set, &oldmask);
if ( v1 )
goto LABEL_9;
v2 = pthread_create(&connector_thread, &v7, (void *(*)(void *))keymon_connect, 0LL);
if ( v2 )
{
syslog(3, "pthread_create for connector thread failed, rc %d\n", v2);
goto LABEL_10;
}
v3 = pthread_create(&receiver_thread, &v7, (void *(*)(void *))keymon_receive, 0LL);
if ( v3 )
{
syslog(3, "pthread_create for receiver thread failed, rc %d\n", v3);
goto LABEL_10;
}
v1 = pthread_sigmask(2, &oldmask, 0LL);
if ( v1 )
{
LABEL_9:
syslog(3, "pthread_sigmask failed, rc %d\n", v1);
LABEL_10:
v5 = -1;
nl_sock_cleanup();
return v5;
}
v4 = pthread_attr_destroy(&v7);
v5 = v4;
if ( v4 )
{
syslog(3, "pthread_attr_destroy failed, rc %d\n", v4);
goto LABEL_10;
}
pthread_join(connector_thread, 0LL);
pthread_join(receiver_thread, 0LL);
return v5;
}

original_code:
int thread_init()
{
    int rc = 0;
    sigset_t sigset, oldset;
    pthread_attr_t attr;
    rc = pthread_attr_init( &attr );
    if( rc < 0 )
    {
        syslog(3, "pthread_attr_init failed, rc %d\n", rc);
        goto fail;
    }
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
    pthread_mutex_init( &sk_mutex, ((void *)0) );
    sigemptyset(&sigset);
    sigaddset(&sigset, 10);
    rc = pthread_sigmask(0, &sigset, &oldset);
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &connector_thread, &attr, keymon_connect, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for connector thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &receiver_thread, &attr, keymon_receive, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for receiver thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_sigmask(2, &oldset, ((void *)0));
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_attr_destroy( &attr );
    if( rc )
    {
        syslog(3, "pthread_attr_destroy failed, rc %d\n", rc);
        goto fail;
    }
    pthread_join(connector_thread, ((void *)0));
    pthread_join(receiver_thread, ((void *)0));
    return 0;
fail:
    nl_sock_cleanup();
    return -1;
}

canonical_original_code:
int thread_init()
{
    int rc = 0;
    struct __sigset_t sigset; struct __sigset_t oldset;
    union pthread_attr_t attr;
    rc = pthread_attr_init( &attr );
    if( rc < 0 )
    {
        syslog(3, "pthread_attr_init failed, rc %d\n", rc);
        goto fail;
    }
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
    pthread_mutex_init( &sk_mutex, ((void *)0) );
    sigemptyset(&sigset);
    sigaddset(&sigset, 10);
    rc = pthread_sigmask(0, &sigset, &oldset);
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &connector_thread, &attr, keymon_connect, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for connector thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_create( &receiver_thread, &attr, keymon_receive, ((void *)0) );
    if( rc )
    {
        syslog(3, "pthread_create for receiver thread failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_sigmask(2, &oldset, ((void *)0));
    if( rc )
    {
        syslog(3, "pthread_sigmask failed, rc %d\n", rc);
        goto fail;
    }
    rc = pthread_attr_destroy( &attr );
    if( rc )
    {
        syslog(3, "pthread_attr_destroy failed, rc %d\n", rc);
        goto fail;
    }
    pthread_join(connector_thread, ((void *)0));
    pthread_join(receiver_thread, ((void *)0));
    return 0;
fail:
    nl_sock_cleanup();
    return -1;
}

variable_types: rc int

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

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


decompiled_code:
int __fastcall signal_handler(unsigned int a1)
{
KM_DBG = ~KM_DBG;
return printf("Caught signal %d. KM_DBG %d\n", a1, (unsigned int)KM_DBG);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1)
{
KM_DBG = ~KM_DBG;
return printf("Caught signal %d. KM_DBG %d\n", a1, (unsigned int)KM_DBG);
}

original_code:
void signal_handler(int signo)
{
    KM_DBG = ~KM_DBG;
    printf("Caught signal %d. KM_DBG %d\n", signo, KM_DBG);
}

variable_types: signo int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct sigaction v4; // [rsp+0h] [rbp-A8h] BYREF

openlog("keymon", 0, 24);
v4.sa_handler = (__sighandler_t)signal_handler;
if ( sigaction(10, &v4, 0LL) )
{
syslog(4, "sigaction");
}
else if ( (int)db_init() < 0 )
{
syslog(3, "db_init");
}
else if ( (int)nl_sock_init() < 0 )
{
syslog(3, "nl_sock_init");
}
else
{
daemon(0, 0);
if ( (int)thread_init() >= 0 )
pthread_exit(0LL);
syslog(3, "thread_init");
}
return 1;
}

original_code:
int main(int argc, const char *argv[])
{
    int rc = 0;
    openlog("keymon", 0, (3<<3));
    struct sigaction sa_act;
    sa_act.__sigaction_handler.sa_handler = signal_handler;
    rc = sigaction(10, &sa_act, ((void *)0));
    if( rc )
    {
        syslog(4, "sigaction");
        return 1;
    }
    if( db_init() < 0 )
    {
        syslog(3, "db_init" );
        return 1;
    }
 if( nl_sock_init() < 0 )
 {
  syslog(3, "nl_sock_init" );
  return 1;
 }
    daemon(0,0);
    if( thread_init() < 0 )
    {
        syslog(3, "thread_init" );
        return 1;
    }
    pthread_exit( ((void *)0) );
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int rc = 0;
    openlog("keymon", 0, (3<<3));
    struct sigaction sa_act;
    sa_act.__sigaction_handler.sa_handler = signal_handler;
    rc = sigaction(10, &sa_act, ((void *)0));
    if( rc )
    {
        syslog(4, "sigaction");
        return 1;
    }
    if( db_init() < 0 )
    {
        syslog(3, "db_init" );
        return 1;
    }
 if( nl_sock_init() < 0 )
 {
  syslog(3, "nl_sock_init" );
  return 1;
 }
    daemon(0,0);
    if( thread_init() < 0 )
    {
        syslog(3, "thread_init" );
        return 1;
    }
    pthread_exit( ((void *)0) );
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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)(); }

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 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; }


decompiled_code:
void __fastcall keymon_db_store(__int64 a1, unsigned int a2)
{
unsigned int v2; // eax
const char *v3; // rax
int v4; // ecx

v2 = rec(db, a1, a2, 0);
if ( v2 == -30988 )
{
v4 = 1;
}
else
{
if ( v2 )
{
v3 = (const char *)db_strerror(v2);
syslog(3, "Failed to store key %d: %s\n", (unsigned int)a1, v3);
return;
}
v4 = 2;
}
rec(db, a1, a2, v4);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2)
{
unsigned int v2; // eax
const char *v3; // rax
int v4; // ecx

v2 = func1(db, a1, a2, 0);
if ( v2 == -30988 )
{
v4 = 1;
}
else
{
if ( v2 )
{
v3 = (const char *)db_strerror(v2);
syslog(3, "Failed to store key %d: %s\n", (unsigned int)a1, v3);
return;
}
v4 = 2;
}
func1(db, a1, a2, v4);
}

original_code:
void keymon_db_store( struct keymon_event event )
{
    int rc = 0;
    rc = rec(db, event, REC_CHECK);
    switch(rc)
    {
        case (-30988):
            rec(db, event, REC_CREATE);
            break;
        case 0:
            rec(db, event, REC_UPDATE);
            break;
        default:
            syslog(3, "Failed to store key %d: %s\n", event.value, db_strerror(rc));
            break;
    }
    return;
}

canonical_original_code:
void keymon_db_store(struct keymon_event event)
{
    int rc = 0;
    rc = rec(db, event, REC_CHECK);
    switch(rc)
    {
        case (-30988):
            rec(db, event, REC_CREATE);
            break;
        case 0:
            rec(db, event, REC_UPDATE);
            break;
        default:
            syslog(3, "Failed to store key %d: %s\n", event.value, db_strerror(rc));
            break;
    }
    return;
}

variable_types: struct keymon_event { int value; int down; int shift; }


decompiled_code:
void db_cleanup()
{
if ( db )
(*(void (__fastcall **)(__int64, _QWORD))(db + 568))(db, 0LL);
syslog(6, "Successfully closed DB.\n");
}

canonical_decompiled_code:
void func3()
{
if ( db )
(*(void (__fastcall **)(__int64, _QWORD))(db + 568))(db, 0LL);
syslog(6, "Successfully closed DB.\n");
}

original_code:
void db_cleanup()
{
 if( db )
 {
  db->close( db, 0 );
 }
 syslog(6, "Successfully closed DB.\n" );
}