decompiled_code:
__int64 __fastcall pool_add_worker(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
pthread_mutex_t *v4; // rdi
__int64 v5; // rax

v2 = malloc(0x18uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
*v2 = a1;
v2[1] = a2;
v2[2] = 0LL;
pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v4 = (pthread_mutex_t *)pool;
v5 = *((_QWORD *)pool + 1);
if ( v5 )
{
*(_QWORD *)(v5 + 16) = v3;
*(&v4->__align + 1) = (__int64)v3;
if ( !v4->__align )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x8Bu, "pool_add_worker");
}
else
{
*((_QWORD *)pool + 1) = v3;
v4->__align = (__int64)v3;
}
pthread_mutex_unlock(v4 + 1);
pthread_cond_signal((pthread_cond_t *)((char *)pool + 80));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
pthread_mutex_t *v4; // rdi
__int64 v5; // rax

v2 = malloc(0x18uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
*v2 = a1;
v2[1] = a2;
v2[2] = 0LL;
pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v4 = (pthread_mutex_t *)pool;
v5 = *((_QWORD *)pool + 1);
if ( v5 )
{
*(_QWORD *)(v5 + 16) = v3;
*(&v4->__align + 1) = (__int64)v3;
if ( !v4->__align )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x8Bu, "pool_add_worker");
}
else
{
*((_QWORD *)pool + 1) = v3;
v4->__align = (__int64)v3;
}
pthread_mutex_unlock(v4 + 1);
pthread_cond_signal((pthread_cond_t *)((char *)pool + 80));
return 0LL;
}

original_code:
int pool_add_worker(void *(*process)(void *arg), void *arg )
{
    CThread_worker *newworker = (CThread_worker *)malloc(sizeof(CThread_worker));
    if (newworker == ((void *)0))
    {
        return -1;
    }
    newworker->process = process;
    newworker->arg = arg;
    newworker->next = ((void *)0);
    pthread_mutex_lock(&(pool->queue_lock));
    if (pool->work_quequ_rear == ((void *)0))
    {
        pool->work_queue_head = pool->work_quequ_rear = newworker;
    }
    else
    {
        pool->work_quequ_rear->next = newworker;
        pool->work_quequ_rear = newworker;
    }
    ((pool->work_queue_head != ((void *)0)) ? (void) (0) : __assert_fail ("pool->work_queue_head != NULL", "thread_pool.c", 139, __extension__ __PRETTY_FUNCTION__));
    pthread_mutex_unlock(&(pool->queue_lock));
    pthread_cond_signal(&(pool->queue_ready));
    return 0;
}

canonical_original_code:
int pool_add_worker(void *(*process)(void *arg), void *arg)
{
    struct worker *newworker = (CThread_worker *)malloc(sizeof(CThread_worker));
    if (newworker == ((void *)0))
    {
        return -1;
    }
    newworker->process = process;
    newworker->arg = arg;
    newworker->next = ((void *)0);
    pthread_mutex_lock(&(pool->queue_lock));
    if (pool->work_quequ_rear == ((void *)0))
    {
        pool->work_queue_head = pool->work_quequ_rear = newworker;
    }
    else
    {
        pool->work_quequ_rear->next = newworker;
        pool->work_quequ_rear = newworker;
    }
    ((pool->work_queue_head != ((void *)0)) ? (void) (0) : __assert_fail ("pool->work_queue_head != NULL", "thread_pool.c", 139, __extension__ __PRETTY_FUNCTION__));
    pthread_mutex_unlock(&(pool->queue_lock));
    pthread_cond_signal(&(pool->queue_ready));
    return 0;
}

variable_types:
process
void *(*)(void *arg)


repo: bill-lonely/sshscan

decompiled_code:
__int64 pool_destroy()
{
char *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx

v0 = (char *)pool;
if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast((pthread_cond_t *)(v0 + 80));
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
pthread_join(*(_QWORD *)(*((_QWORD *)v1 + 3) + 16 * v2), 0LL);
v1 = (char *)pool;
++v2;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
pool = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
char *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx

v0 = (char *)pool;
if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast((pthread_cond_t *)(v0 + 80));
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
pthread_join(*(_QWORD *)(*((_QWORD *)v1 + 3) + 16 * v2), 0LL);
v1 = (char *)pool;
++v2;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
pool = 0LL;
return 0LL;
}

original_code:
int pool_destroy(void)
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}

canonical_original_code:
int pool_destroy()
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}

variable_types: i int

return_type: int


decompiled_code:
__int64 pool_check_state()
{
int v0; // edx
__int64 v1; // rcx
_DWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v0 = *((_DWORD *)pool + 8);
if ( v0 <= 0 )
{
LABEL_5:
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 0LL;
}
else
{
v1 = *((_QWORD *)pool + 3);
v2 = (_DWORD *)(v1 + 8);
v3 = v1 + 16LL * (unsigned int)(v0 - 1) + 24;
while ( *v2 )
{
v2 += 4;
if ( v2 == (_DWORD *)v3 )
goto LABEL_5;
}
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // edx
__int64 v1; // rcx
_DWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v0 = *((_DWORD *)pool + 8);
if ( v0 <= 0 )
{
LABEL_5:
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 0LL;
}
else
{
v1 = *((_QWORD *)pool + 3);
v2 = (_DWORD *)(v1 + 8);
v3 = v1 + 16LL * (unsigned int)(v0 - 1) + 24;
while ( *v2 )
{
v2 += 4;
if ( v2 == (_DWORD *)v3 )
goto LABEL_5;
}
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 1LL;
}
return result;
}

original_code:
int pool_check_state(void)
{
 int i;
 pthread_mutex_lock(&(pool->queue_lock));
 for (i = 0; i < pool->max_thread_num; ++i)
 {
  if (pool->thread_unite_list[i].state == 0)
  {
   pthread_mutex_unlock(&(pool->queue_lock));
   return 1;
  }
 }
 pthread_mutex_unlock(&(pool->queue_lock));
 return 0;
}

canonical_original_code:
int pool_check_state()
{
 int i;
 pthread_mutex_lock(&(pool->queue_lock));
 for (i = 0; i < pool->max_thread_num; ++i)
 {
  if (pool->thread_unite_list[i].state == 0)
  {
   pthread_mutex_unlock(&(pool->queue_lock));
   return 1;
  }
 }
 pthread_mutex_unlock(&(pool->queue_lock));
 return 0;
}


decompiled_code:
__int64 pool_destroy_force()
{
char *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx

v0 = (char *)pool;
if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast((pthread_cond_t *)(v0 + 80));
sleep(2u);
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
pthread_cancel(*(_QWORD *)(*((_QWORD *)v1 + 3) + 16 * v2));
pthread_join(*(_QWORD *)(*((_QWORD *)pool + 3) + 16 * v2), 0LL);
v1 = (char *)pool;
++v2;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
pool = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
char *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx

v0 = (char *)pool;
if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast((pthread_cond_t *)(v0 + 80));
sleep(2u);
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
pthread_cancel(*(_QWORD *)(*((_QWORD *)v1 + 3) + 16 * v2));
pthread_join(*(_QWORD *)(*((_QWORD *)pool + 3) + 16 * v2), 0LL);
v1 = (char *)pool;
++v2;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
pool = 0LL;
return 0LL;
}

original_code:
int pool_destroy_force(void)
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    sleep(2);
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_cancel(pool->thread_unite_list[i].thread_id);
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}

canonical_original_code:
int pool_destroy_force()
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    sleep(2);
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_cancel(pool->thread_unite_list[i].thread_id);
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}


ouch

decompiled_code:
void __noreturn ouch()
{
recive = 1;
_sysv_signal(2, 0LL);
puts("Recived SIGINT");
pool_destroy_force();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
sleep(2u);
puts("END! BYE.......");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
recive = 1;
_sysv_signal(2, 0LL);
puts("Recived SIGINT");
pool_destroy_force();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
sleep(2u);
puts("END! BYE.......");
exit(0);
}

original_code:
void ouch(int sig)
{
    recive = 1;
    (void) signal(2, ((__sighandler_t) 0));
    printf("Recived SIGINT\n");
    pool_destroy_force();
    libssh2_exit();
    free_ip_list(&IP_List);
    free_user_list(&User_List);
    free_password_list(&Pwd_List);
    free_setting(&setting);
    sleep(2);
    printf("END! BYE.......\n");
    exit(0);
}

canonical_original_code:
void ouch(int sig)
{
    recive = 1;
    (void) signal(2, ((void (*)(int)) 0));
    printf("Recived SIGINT\n");
    pool_destroy_force();
    libssh2_exit();
    free_ip_list(&IP_List);
    free_user_list(&User_List);
    free_password_list(&Pwd_List);
    free_setting(&setting);
    sleep(2);
    printf("END! BYE.......\n");
    exit(0);
}

variable_types: sig int

return_type: void


decompiled_code:
int show_help()
{
puts(aHIp);
puts(aHIp_0);
puts(aU);
puts(aU_0);
puts(aP);
puts(aP_0);
puts(aT);
puts(aD);
puts(aT_0);
return puts(aN);
}

canonical_decompiled_code:
int func1()
{
puts(aHIp);
puts(aHIp_0);
puts(aU);
puts(aU_0);
puts(aP);
puts(aP_0);
puts(aT);
puts(aD);
puts(aT_0);
return puts(aN);
}

original_code:
void show_help(void)
{
 printf("-h [ip地址]\n");
 printf("-H [ip列表文件]\n");
 printf("-u [用户名]\n");
 printf("-U [用户名列表文件]\n");
 printf("-p [密码]\n");
 printf("-P [密码列表文件]\n");
 printf("-t [端口]  默认22\n");
 printf("-D [日志文件目录] 默认在HOME\n");
 printf("-T [线程数]  默认是6\n");
 printf("-N [密码分组数] 默认是4\n");
}

canonical_original_code:
void show_help()
{
 printf("-h [ip地址]\n");
 printf("-H [ip列表文件]\n");
 printf("-u [用户名]\n");
 printf("-U [用户名列表文件]\n");
 printf("-p [密码]\n");
 printf("-P [密码列表文件]\n");
 printf("-t [端口]  默认22\n");
 printf("-D [日志文件目录] 默认在HOME\n");
 printf("-T [线程数]  默认是6\n");
 printf("-N [密码分组数] 默认是4\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // zf
int v4; // ebx
unsigned int v5; // ebp
__m128i *v6; // rbx
_DWORD *v7; // rax
_DWORD *v8; // r12
const __m128i *v9; // r12
int v10; // ebp

v3 = strcmp(argv[1], "-help") == 0;
v4 = !v3;
if ( v3 )
{
show_help();
}
else if ( (int)checkSetting((unsigned int)argc, argv, &setting) < 0 )
{
fwrite("Options Error \n", 1uLL, 0xFuLL, stderr);
v4 = -1;
}
else
{
if ( (int)analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0 )
return -1;
if ( *((int *)&IP_List + 2) <= 0 )
goto LABEL_13;
if ( (int)pool_init(*((unsigned int *)&setting + 1065)) < 0 )
{
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
fwrite("Init thread pool error!!\n", 1uLL, 0x19uLL, stderr);
return -2;
}
_sysv_signal(2, (__sighandler_t)ouch);
gcrypt_fix();
v5 = libssh2_init(0LL);
if ( v5 )
{
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
fprintf(stderr, "libssh2 initialization failed (%d)\n", v5);
return -1;
}
v6 = (__m128i *)malloc((__int64)*((int *)&IP_List + 2) << 6);
v7 = malloc(0x18uLL);
v8 = v7;
if ( v7 )
{
v7[2] = 1;
*(_QWORD *)v7 = v6;
pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
*((_QWORD *)v8 + 2) = *((_QWORD *)&setting + 531);
*((_QWORD *)&setting + 531) = v8;
pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
v9 = (const __m128i *)IP_List;
if ( *((int *)&IP_List + 2) > 0 )
{
do
{
v6[3].m128i_i16[2] = *((_WORD *)&setting + 24);
v6[1] = _mm_load_si128((const __m128i *)&User_List);
v6[2] = _mm_load_si128((const __m128i *)&Pwd_List);
*v6 = _mm_loadu_si128(v9);
v6[3].m128i_i64[1] = (__int64)&setting;
pool_add_worker(&ssh_test, v6);
v9 = (const __m128i *)v9->m128i_i64[1];
++v5;
v6 += 4;
}
while ( *((_DWORD *)&IP_List + 2) > (signed int)v5 );
}
sleep(2u);
while ( 1 )
{
v4 = pool_check_state();
if ( !v4 )
break;
sleep(6u);
}
v10 = recive;
if ( !recive )
{
pool_destroy();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
v4 = v10;
}
}
else
{
LABEL_13:
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
v4 = -1;
}
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // zf
int v4; // ebx
unsigned int v5; // ebp
__m128i *v6; // rbx
_DWORD *v7; // rax
_DWORD *v8; // r12
const __m128i *v9; // r12
int v10; // ebp

v3 = strcmp(argv[1], "-help") == 0;
v4 = !v3;
if ( v3 )
{
func1();
}
else if ( (int)checkSetting((unsigned int)argc, argv, &setting) < 0 )
{
fwrite("Options Error \n", 1uLL, 0xFuLL, stderr);
v4 = -1;
}
else
{
if ( (int)analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0 )
return -1;
if ( *((int *)&IP_List + 2) <= 0 )
goto LABEL_13;
if ( (int)pool_init(*((unsigned int *)&setting + 1065)) < 0 )
{
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
fwrite("Init thread pool error!!\n", 1uLL, 0x19uLL, stderr);
return -2;
}
_sysv_signal(2, (__sighandler_t)ouch);
gcrypt_fix();
v5 = libssh2_init(0LL);
if ( v5 )
{
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
fprintf(stderr, "libssh2 initialization failed (%d)\n", v5);
return -1;
}
v6 = (__m128i *)malloc((__int64)*((int *)&IP_List + 2) << 6);
v7 = malloc(0x18uLL);
v8 = v7;
if ( v7 )
{
v7[2] = 1;
*(_QWORD *)v7 = v6;
pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
*((_QWORD *)v8 + 2) = *((_QWORD *)&setting + 531);
*((_QWORD *)&setting + 531) = v8;
pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
v9 = (const __m128i *)IP_List;
if ( *((int *)&IP_List + 2) > 0 )
{
do
{
v6[3].m128i_i16[2] = *((_WORD *)&setting + 24);
v6[1] = _mm_load_si128((const __m128i *)&User_List);
v6[2] = _mm_load_si128((const __m128i *)&Pwd_List);
*v6 = _mm_loadu_si128(v9);
v6[3].m128i_i64[1] = (__int64)&setting;
pool_add_worker(&ssh_test, v6);
v9 = (const __m128i *)v9->m128i_i64[1];
++v5;
v6 += 4;
}
while ( *((_DWORD *)&IP_List + 2) > (signed int)v5 );
}
sleep(2u);
while ( 1 )
{
v4 = pool_check_state();
if ( !v4 )
break;
sleep(6u);
}
v10 = recive;
if ( !recive )
{
pool_destroy();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
v4 = v10;
}
}
else
{
LABEL_13:
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
v4 = -1;
}
}
return v4;
}

original_code:
int main(int argc, char **argv)
{
    int i, rc;
    struct Test_ssh_Arg_by_IP *workarg = ((void *)0);
    struct ip_node *pIP = ((void *)0);
    struct workarg_queue *record_node = ((void *)0);
    if (strcmp(argv[1],"-help") == 0)
    {
 show_help();
 return 0;
    }
    if (checkSetting(argc, argv, &setting) < 0)
    {
        fprintf(stderr, "Options Error \n");
        return -1;
    }
    if (analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0)
    {
        return -1;
    }
    if (IP_List.count <= 0)
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        return -1;
    }
    if (pool_init(setting.thread_num) < 0)
    {
 free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
  fprintf(stderr,"Init thread pool error!!\n");
  return -2;
    }
    (void)signal(2, ouch);
    gcrypt_fix();
    rc = libssh2_init (0);
    if (rc != 0) {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
        return -1;
    }
    workarg = (struct Test_ssh_Arg_by_IP *)malloc(sizeof(struct Test_ssh_Arg_by_IP) * IP_List.count);
    record_node = (struct workarg_queue *)malloc(sizeof(struct workarg_queue));
    if (record_node == ((void *)0))
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);;
        return -1;
    }
    record_node->level = 1;
    record_node->point = (void *)workarg;
    pthread_mutex_lock(&(setting.setting_mutex));
    record_node->next = setting.workarg_list_head;
    setting.workarg_list_head = record_node;
    pthread_mutex_unlock(&(setting.setting_mutex));
    pIP = IP_List.head;
    for (i = 0; i < IP_List.count; ++i)
    {
        workarg[i].port = setting.port;
        workarg[i].users = User_List;
        workarg[i].passwords = Pwd_List;
        workarg[i].ip = *pIP;
        workarg[i].setting = &setting;
        pool_add_worker (ssh_test,(void *)(&workarg[i]));
        pIP = pIP->next;
    }
    sleep(2);
    while (pool_check_state())
    {
        sleep(6);
    }
    if (recive == 0)
    {
        pool_destroy();
        libssh2_exit();
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i; int rc;
    struct Test_ssh_Arg_by_IP *workarg = ((void *)0);
    struct ip_node *pIP = ((void *)0);
    struct workarg_queue *record_node = ((void *)0);
    if (strcmp(argv[1],"-help") == 0)
    {
 show_help();
 return 0;
    }
    if (checkSetting(argc, argv, &setting) < 0)
    {
        fprintf(stderr, "Options Error \n");
        return -1;
    }
    if (analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0)
    {
        return -1;
    }
    if (IP_List.count <= 0)
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        return -1;
    }
    if (pool_init(setting.thread_num) < 0)
    {
 free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
  fprintf(stderr,"Init thread pool error!!\n");
  return -2;
    }
    (void)signal(2, ouch);
    gcrypt_fix();
    rc = libssh2_init (0);
    if (rc != 0) {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
        return -1;
    }
    workarg = (struct Test_ssh_Arg_by_IP *)malloc(sizeof(struct Test_ssh_Arg_by_IP) * IP_List.count);
    record_node = (struct workarg_queue *)malloc(sizeof(struct workarg_queue));
    if (record_node == ((void *)0))
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);;
        return -1;
    }
    record_node->level = 1;
    record_node->point = (void *)workarg;
    pthread_mutex_lock(&(setting.setting_mutex));
    record_node->next = setting.workarg_list_head;
    setting.workarg_list_head = record_node;
    pthread_mutex_unlock(&(setting.setting_mutex));
    pIP = IP_List.head;
    for (i = 0; i < IP_List.count; ++i)
    {
        workarg[i].port = setting.port;
        workarg[i].users = User_List;
        workarg[i].passwords = Pwd_List;
        workarg[i].ip = *pIP;
        workarg[i].setting = &setting;
        pool_add_worker (ssh_test,(void *)(&workarg[i]));
        pIP = pIP->next;
    }
    sleep(2);
    while (pool_check_state())
    {
        sleep(6);
    }
    if (recive == 0)
    {
        pool_destroy();
        libssh2_exit();
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: rc int

variable_types: struct Test_ssh_Arg_by_IP { struct ip_node { char *ip; struct ip_node *next; } ip; struct user_list { struct user_node { char *user; struct user_node *next; } *head; int count; } users; struct password_list { struct password_node { char *password; struct password_node *next; } *head; int count; } passwords; int ret; short port; struct _Setting { char *Host_IP; char *Host_File; char *Username; char *User_File; char *Password; char *Password_File; short port; int connect_test_count; char success_log_filename[2048]; char path_log[2048]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } success_log_mutex; union pthread_mutex_t setting_mutex; struct password_node **pwd_groups; int per_pwd_num; struct workarg_queue { void *point; int level; struct workarg_queue *next; } *workarg_list_head; int pwd_group_num; int thread_num; } *setting; } *

variable_types: struct ip_node { char *ip; struct ip_node *next; } *

user_defined_types: struct Test_ssh_Arg_by_IP { struct ip_node ip; struct user_list users; struct password_list passwords; int ret; short port; struct _Setting *setting; }

user_defined_types: struct user_list { struct user_node *head; int count; }

user_defined_types: struct password_list { struct password_node *head; int count; }

user_defined_types: struct _Setting { char *Host_IP; char *Host_File; char *Username; char *User_File; char *Password; char *Password_File; short port; int connect_test_count; char success_log_filename[2048]; char path_log[2048]; union pthread_mutex_t success_log_mutex; union pthread_mutex_t setting_mutex; struct password_node **pwd_groups; int per_pwd_num; struct workarg_queue *workarg_list_head; int pwd_group_num; int thread_num; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 gcrypt_fix()
{
return gcry_control(47LL, &gcry_threads_pthread);
}

canonical_decompiled_code:
__int64 func0()
{
return gcry_control(47LL, &gcry_threads_pthread);
}

original_code:
void gcrypt_fix() {
   gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
}


repo: billvb/core-flight-executive

decompiled_code:
int __fastcall CFE_ES_CalculateCRC(_BYTE *a1, int a2, int a3, int a4)
{
int result; // eax
_BYTE *v5; // rdx
int v6; // ecx

LOWORD(result) = 0;
if ( a4 == 2 )
{
result = a3;
if ( a2 )
{
v5 = &a1[a2];
do
{
HIWORD(v6) = HIWORD(result);
LOWORD(v6) = CrcTable_0[(unsigned __int8)(*a1 ^ result)] ^ BYTE1(result);
result = v6;
++a1;
}
while ( a1 != v5 );
}
}
return (__int16)result;
}

canonical_decompiled_code:
int __fastcall func7(_BYTE *a1, int a2, int a3, int a4)
{
int result; // eax
_BYTE *v5; // rdx
int v6; // ecx

LOWORD(result) = 0;
if ( a4 == 2 )
{
result = a3;
if ( a2 )
{
v5 = &a1[a2];
do
{
HIWORD(v6) = HIWORD(result);
LOWORD(v6) = CrcTable_0[(unsigned __int8)(*a1 ^ result)] ^ BYTE1(result);
result = v6;
++a1;
}
while ( a1 != v5 );
}
}
return (__int16)result;
}

variable_types:
DataPtr
void *

variable_types:
DataLength
unsigned int

variable_types:
InputCRC
unsigned int

variable_types:
TypeCRC
unsigned int

variable_types: Index short

variable_types: Crc short

variable_types:
BufPtr
unsigned char *

variable_types:
CrcTable
unsigned short [256]

return_type: unsigned int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // eax
int v5; // er14
int v6; // ebp
unsigned int v7; // er13
int v8; // ebx
unsigned int v9; // eax
_BYTE v11[152]; // [rsp+0h] [rbp-98h] BYREF

if ( argc != 2 || (v3 = argv[1], !strcmp(v3, "-help")) )
{
printf("\ncFE TS CRC calculator for LRO files.");
puts("\nUsage: cfe_ts_crc [filename]");
exit(0);
}
v4 = open(v3, 0);
v5 = v4;
if ( v4 < 0 )
{
puts("\ncfe_ts_crc error: can't open input file!");
exit(0);
}
lseek(v4, 116LL, 0);
v6 = 0;
v7 = 0;
do
{
v8 = read(v5, v11, 0x64uLL);
v9 = CFE_ES_CalculateCRC(v11, v8, v6, 2);
v6 = v9;
v7 += v8;
}
while ( v8 == 100 );
printf(
"\nTable File Name: %s\nTable Size: %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n",
argv[1],
v7,
v9);
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // eax
int v5; // er14
int v6; // ebp
unsigned int v7; // er13
int v8; // ebx
unsigned int v9; // eax
_BYTE v11[152]; // [rsp+0h] [rbp-98h] BYREF

if ( argc != 2 || (v3 = argv[1], !strcmp(v3, "-help")) )
{
printf("\ncFE TS CRC calculator for LRO files.");
puts("\nUsage: cfe_ts_crc [filename]");
exit(0);
}
v4 = open(v3, 0);
v5 = v4;
if ( v4 < 0 )
{
puts("\ncfe_ts_crc error: can't open input file!");
exit(0);
}
lseek(v4, 116LL, 0);
v6 = 0;
v7 = 0;
do
{
v8 = read(v5, v11, 0x64uLL);
v9 = func7(v11, v8, v6, 2);
v6 = v9;
v7 += v8;
}
while ( v8 == 100 );
printf(
"\nTable File Name: %s\nTable Size: %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n",
argv[1],
v7,
v9);
return v6;
}

original_code:
int main( int argc, char **argv )
{
 int readSize;
 int skipSize = 0;
 int fileSize = 0;
 uint32 fileCRC = 0;
 int fd;
 int done = 0;
 char buffer[100];
 if ( ( argc != 2 ) || ( strncmp(argv[1], "-help", 100) == 0 ) )
 {
     printf("\ncFE TS CRC calculator for LRO files.");
     printf("\nUsage: cfe_ts_crc [filename]\n");
        exit(0);
    }
 skipSize = 116;
 fd = open( argv[1], 00 );
 if ( fd < 0 )
 {
  printf("\ncfe_ts_crc error: can't open input file!\n");
  exit(0);
 }
 lseek( fd, skipSize, 0 );
 while ( done == 0 )
 {
  readSize = read(fd, buffer, 100);
  fileCRC = CFE_ES_CalculateCRC(buffer, readSize, fileCRC, 2);
  fileSize += readSize;
  if (readSize != 100) done=1;
 }
     printf("\nTable File Name:            %s\nTable Size:                 %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n", argv[1], fileSize, fileCRC);
     return(fileCRC);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int readSize;
 int skipSize = 0;
 int fileSize = 0;
 unsigned int fileCRC = 0;
 int fd;
 int done = 0;
 char buffer[100];
 if ( ( argc != 2 ) || ( strncmp(argv[1], "-help", 100) == 0 ) )
 {
     printf("\ncFE TS CRC calculator for LRO files.");
     printf("\nUsage: cfe_ts_crc [filename]\n");
        exit(0);
    }
 skipSize = 116;
 fd = open( argv[1], 00 );
 if ( fd < 0 )
 {
  printf("\ncfe_ts_crc error: can't open input file!\n");
  exit(0);
 }
 lseek( fd, skipSize, 0 );
 while ( done == 0 )
 {
  readSize = read(fd, buffer, 100);
  fileCRC = CFE_ES_CalculateCRC(buffer, readSize, fileCRC, 2);
  fileSize += readSize;
  if (readSize != 100) done=1;
 }
     printf("\nTable File Name:            %s\nTable Size:                 %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n", argv[1], fileSize, fileCRC);
     return(fileCRC);
}

variable_types: readSize int

variable_types: skipSize int

variable_types: fileSize int

variable_types:
fileCRC
unsigned int

variable_types: fd int

variable_types: done int

variable_types:
buffer
char [100]


repo: billyauhk/elektroL-decode

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int16 v4; // r13
int v5; // ebp
char *v6; // rax
int v7; // er12
__off_t v8; // rsi
__off_t v9; // rsi
char *v10; // rax
char *v11; // rsi
char v12; // dl
unsigned int v13; // eax
unsigned int v14; // ebx
unsigned int v15; // edx
char v17[3]; // [rsp+1h] [rbp-88Bh] BYREF
char v18[2000]; // [rsp+4h] [rbp-888h] BYREF
struct stat v19; // [rsp+7D4h] [rbp-B8h] BYREF

if ( argc <= 1 )
{
puts("No filename!");
exit(-1);
}
printf("Filename: %s\n", argv[1]);
if ( (unsigned int)stat((char *)argv[1], &v19) == -1 )
{
perror("stat");
exit(-1);
}
printf("File size: %lld bytes\n", v19.st_size);
v3 = argv[1];
v4 = v3[strlen(v3) - 5];
printf("Channel number: %u\n", (unsigned __int16)(v4 - 48));
v5 = open(argv[1], 0);
if ( v5 == -1 )
{
perror("Cannot open input file");
exit(-1);
}
puts("File opened");
strncpy(v18, argv[1], 0x7D0uLL);
if ( v18[1999] )
{
puts("File name too long!");
exit(-1);
}
v6 = &v17[strlen(v18)];
*(_WORD *)v6 = 25200;
v6[2] = 109;
printf("Output file name: %s\n", v18);
v7 = open(v18, 577, 420LL);
if ( v7 == -1 )
perror("Cannot open output file");
if ( (unsigned __int16)(v4 - 48) > 3u )
{
if ( (unsigned __int16)(v4 - 52) > 5u )
{
puts("Unknown channel name...");
exit(-1);
}
}
else if ( v4 != 48 )
{
v8 = lseek(v5, -248020992LL, 2);
if ( (_DWORD)v8 == -1 )
{
printf("Size; %d\n", 248020992LL);
perror("Cannot seek to position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v8);
strcpy(v18, "P5 11136 11136 1023\n");
if ( (unsigned int)write(v7, v18, 0x14uLL) == -1 )
{
perror("Write PBM header error");
exit(-1);
}
goto LABEL_27;
}
v9 = lseek(v5, -15501312LL, 2);
if ( (_DWORD)v9 == -1 )
{
perror("Cannot seek to the position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v9);
strcpy(v18, "P5 2784 2784 1023\n");
if ( (unsigned int)write(v7, v18, 0x12uLL) == -1 )
{
perror("Write PBM header error");
exit(-1);
}
LABEL_27:
while ( 1 )
{
v13 = read(v5, v18, 0x7D0uLL);
v14 = v13;
if ( !v13 )
break;
v15 = v13 >> 1;
if ( v13 >> 1 )
{
v10 = &v18[1];
v11 = &v18[2 * v15 + 1];
do
{
v12 = *(v10 - 1);
*(v10 - 1) = *v10;
*v10 = v12;
v10 += 2;
}
while ( v10 != v11 );
}
if ( v14 != (unsigned int)write(v7, v18, v14) )
perror("Something wrong with output file writing");
}
if ( close(v5) == -1 )
perror("Cannot close input file");
if ( close(v7) == -1 )
perror("Cannot close output file");
puts("Program ends here");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int16 v4; // r13
int v5; // ebp
char *v6; // rax
int v7; // er12
__off_t v8; // rsi
__off_t v9; // rsi
char *v10; // rax
char *v11; // rsi
char v12; // dl
unsigned int v13; // eax
unsigned int v14; // ebx
unsigned int v15; // edx
char v17[3]; // [rsp+1h] [rbp-88Bh] BYREF
char v18[2000]; // [rsp+4h] [rbp-888h] BYREF
struct stat v19; // [rsp+7D4h] [rbp-B8h] BYREF

if ( argc <= 1 )
{
puts("No filename!");
exit(-1);
}
printf("Filename: %s\n", argv[1]);
if ( (unsigned int)func9((char *)argv[1], &v19) == -1 )
{
perror("stat");
exit(-1);
}
printf("File size: %lld bytes\n", v19.st_size);
v3 = argv[1];
v4 = v3[strlen(v3) - 5];
printf("Channel number: %u\n", (unsigned __int16)(v4 - 48));
v5 = open(argv[1], 0);
if ( v5 == -1 )
{
perror("Cannot open input file");
exit(-1);
}
puts("File opened");
strncpy(v18, argv[1], 0x7D0uLL);
if ( v18[1999] )
{
puts("File name too long!");
exit(-1);
}
v6 = &v17[strlen(v18)];
*(_WORD *)v6 = 25200;
v6[2] = 109;
printf("Output file name: %s\n", v18);
v7 = open(v18, 577, 420LL);
if ( v7 == -1 )
perror("Cannot open output file");
if ( (unsigned __int16)(v4 - 48) > 3u )
{
if ( (unsigned __int16)(v4 - 52) > 5u )
{
puts("Unknown channel name...");
exit(-1);
}
}
else if ( v4 != 48 )
{
v8 = lseek(v5, -248020992LL, 2);
if ( (_DWORD)v8 == -1 )
{
printf("Size; %d\n", 248020992LL);
perror("Cannot seek to position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v8);
strcpy(v18, "P5 11136 11136 1023\n");
if ( (unsigned int)write(v7, v18, 0x14uLL) == -1 )
{
perror("Write PBM header error");
exit(-1);
}
goto LABEL_27;
}
v9 = lseek(v5, -15501312LL, 2);
if ( (_DWORD)v9 == -1 )
{
perror("Cannot seek to the position");
exit(-1);
}
printf("Moved to the %d-th byte\n", v9);
strcpy(v18, "P5 2784 2784 1023\n");
if ( (unsigned int)write(v7, v18, 0x12uLL) == -1 )
{
perror("Write PBM header error");
exit(-1);
}
LABEL_27:
while ( 1 )
{
v13 = read(v5, v18, 0x7D0uLL);
v14 = v13;
if ( !v13 )
break;
v15 = v13 >> 1;
if ( v13 >> 1 )
{
v10 = &v18[1];
v11 = &v18[2 * v15 + 1];
do
{
v12 = *(v10 - 1);
*(v10 - 1) = *v10;
*v10 = v12;
v10 += 2;
}
while ( v10 != v11 );
}
if ( v14 != (unsigned int)write(v7, v18, v14) )
perror("Something wrong with output file writing");
}
if ( close(v5) == -1 )
perror("Cannot close input file");
if ( close(v7) == -1 )
perror("Cannot close output file");
puts("Program ends here");
return 0;
}

original_code:
int main(int argc, char* argv[]){
  int retVal;
  int infile, outfile;
  long long filesize;
  unsigned short channel;
  struct stat sb;
  unsigned int readCount, i;
  char buffer[2000];
  char tempByte;
  if(argc>1){
    printf("Filename: %s\n",argv[1]);
  }else{
    printf("No filename!\n");
    exit(-1);
  }
  if((retVal = stat(argv[1],&sb)) == -1){
    perror("stat");exit(-1);
  }else{
    filesize = (long long) sb.st_size;
    printf("File size: %lld bytes\n",filesize);
  }
  channel = argv[1][strlen(argv[1])-5]-'0';
  printf("Channel number: %u\n",channel);
  if((infile = open(argv[1],00)) == -1){
    perror("Cannot open input file");exit(-1);
  }else{
    printf("File opened\n");
  }
  strncpy(buffer,argv[1],2000);
  if(buffer[2000 -1] != '\0'){
    printf("File name too long!\n");exit(-1);
  }
  strncpy(buffer+strlen(buffer)-3,"pbm",3);
  printf("Output file name: %s\n",buffer);
  if((outfile = open(buffer,01|0100|01000,0644)) == -1){
    perror("Cannot open output file");
  }
  switch(channel){
    case 1: case 2: case 3:
      if((retVal = lseek(infile,-11136*11136*2,2)) == -1){
        printf("Size; %d\n",11136*11136*2);
        perror("Cannot seek to position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 11136 11136 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    case 4: case 5: case 6: case 7:
    case 8: case 9: case 0:
      if((retVal = lseek(infile,-2784*2784*2,2)) == -1){
        perror("Cannot seek to the position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 2784 2784 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    default:
      printf("Unknown channel name...\n");exit(-1);
      break;
  }
  while((readCount = read(infile,buffer,2000)) != 0){
    for(i=0;i<(readCount >> 1);i++){
      buffer[2*i+1] ^= buffer[2*i+0];
      buffer[2*i+0] ^= buffer[2*i+1];
      buffer[2*i+1] ^= buffer[2*i+0];
    }
    if((retVal = write(outfile,buffer,readCount)) != readCount){
      perror("Something wrong with output file writing");
    }
  }
  if((retVal = close(infile)) == -1){
    perror("Cannot close input file");
  }
  if((retVal = close(outfile)) == -1){
    perror("Cannot close output file");
  }
  printf("Program ends here\n");
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  int retVal;
  int infile; int outfile;
  long long filesize;
  unsigned short channel;
  struct stat sb;
  unsigned int readCount; unsigned int i;
  char buffer[2000];
  char tempByte;
  if(argc>1){
    printf("Filename: %s\n",argv[1]);
  }else{
    printf("No filename!\n");
    exit(-1);
  }
  if((retVal = stat(argv[1],&sb)) == -1){
    perror("stat");exit(-1);
  }else{
    filesize = (long long) sb.st_size;
    printf("File size: %lld bytes\n",filesize);
  }
  channel = argv[1][strlen(argv[1])-5]-'0';
  printf("Channel number: %u\n",channel);
  if((infile = open(argv[1],00)) == -1){
    perror("Cannot open input file");exit(-1);
  }else{
    printf("File opened\n");
  }
  strncpy(buffer,argv[1],2000);
  if(buffer[2000 -1] != '\0'){
    printf("File name too long!\n");exit(-1);
  }
  strncpy(buffer+strlen(buffer)-3,"pbm",3);
  printf("Output file name: %s\n",buffer);
  if((outfile = open(buffer,01|0100|01000,0644)) == -1){
    perror("Cannot open output file");
  }
  switch(channel){
    case 1: case 2: case 3:
      if((retVal = lseek(infile,-11136*11136*2,2)) == -1){
        printf("Size; %d\n",11136*11136*2);
        perror("Cannot seek to position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 11136 11136 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    case 4: case 5: case 6: case 7:
    case 8: case 9: case 0:
      if((retVal = lseek(infile,-2784*2784*2,2)) == -1){
        perror("Cannot seek to the position");exit(-1);
      }else{
        printf("Moved to the %d-th byte\n",retVal);
      }
      if((retVal = snprintf(buffer,2000,"P5 2784 2784 1023\n")) < 0){
        perror("snprintf error");exit(-1);
      }
      if((retVal = write(outfile,buffer,retVal)) == -1){
        perror("Write PBM header error");exit(-1);
      }
      break;
    default:
      printf("Unknown channel name...\n");exit(-1);
      break;
  }
  while((readCount = read(infile,buffer,2000)) != 0){
    for(i=0;i<(readCount >> 1);i++){
      buffer[2*i+1] ^= buffer[2*i+0];
      buffer[2*i+0] ^= buffer[2*i+1];
      buffer[2*i+1] ^= buffer[2*i+0];
    }
    if((retVal = write(outfile,buffer,readCount)) != readCount){
      perror("Something wrong with output file writing");
    }
  }
  if((retVal = close(infile)) == -1){
    perror("Cannot close input file");
  }
  if((retVal = close(outfile)) == -1){
    perror("Cannot close output file");
  }
  printf("Program ends here\n");
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: retVal int

variable_types: infile int

variable_types: outfile int

variable_types:
filesize
long long

variable_types:
channel
unsigned short

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
readCount
unsigned int

variable_types:
i
unsigned int

variable_types:
buffer
char [2000]

variable_types: tempByte char

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: bioinfo-cnb/bionmf-gpu

decompiled_code:
__int64 __fastcall print_generate_matrix_help(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"Tool to generate (in CPU) a matrix with random values\n"
"\n"
"Usage:\n"
"\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(
0LL,
"\n"
"Note: Some of the previous options are read for compatibility reasons, but they are ignored by "
"the program.\n"
"\n"
"<output_filename>\n"
"\tOutput file (mandatory if 'help' is not requested).\n"
"\n"
"<rows> <columns>\n"
"\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n"
"\n"
"<max_value>\n"
"\tRange for random values: [0.0 ... <max_value>].\n"
"\tValid values are in range (%g ... %g). The default is %g\n"
"\n"
"-h,-H\tPrints this help message.\n"
"\n",
*(double *)&LC4,
*(double *)&LC3,
*(double *)&LC2) == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_generate_matrix_help( execname )");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"Tool to generate (in CPU) a matrix with random values\n"
"\n"
"Usage:\n"
"\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(
0LL,
"\n"
"Note: Some of the previous options are read for compatibility reasons, but they are ignored by "
"the program.\n"
"\n"
"<output_filename>\n"
"\tOutput file (mandatory if 'help' is not requested).\n"
"\n"
"<rows> <columns>\n"
"\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n"
"\n"
"<max_value>\n"
"\tRange for random values: [0.0 ... <max_value>].\n"
"\tValid values are in range (%g ... %g). The default is %g\n"
"\n"
"-h,-H\tPrints this help message.\n"
"\n",
*(double *)&LC4,
*(double *)&LC3,
*(double *)&LC2) == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_generate_matrix_help( execname )");
result = 1LL;
}
return result;
}

original_code:
static int print_generate_matrix_help( char const *restrict const execname )
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_generate_matrix_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "Tool to generate (in CPU) a matrix with random values\n\n"
    "Usage:\n\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
    "\t%s -h\n\n", execname, execname );
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\nNote: Some of the previous options are read for compatibility reasons, "
    "but they are ignored by the program.\n\n"
    "<output_filename>\n\tOutput file (mandatory if 'help' is not requested).\n\n"
    "<rows> <columns>\n\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n\n"
    "<max_value>\n\tRange for random values: [0.0 ... <max_value>].\n"
    "\tValid values are in range (%g ... %g). The default is %g\n\n"
    "-h,-H\tPrints this help message.\n\n", ( ( 1.17549435082228750796873653722224568e-38F ) ), ( 3.40282346638528859811704183484516925e+38F ), ( (1.0f) ) ) != 0 )
  status = 1;
 return status;
}

canonical_original_code:
static int print_generate_matrix_help(char *execname)
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_generate_matrix_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "Tool to generate (in CPU) a matrix with random values\n\n"
    "Usage:\n\t%s <output_filename> <rows> <columns> [ -e <native_format> ] [ <max_value> ]\n"
    "\t%s -h\n\n", execname, execname );
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\nNote: Some of the previous options are read for compatibility reasons, "
    "but they are ignored by the program.\n\n"
    "<output_filename>\n\tOutput file (mandatory if 'help' is not requested).\n\n"
    "<rows> <columns>\n\tOutput matrix dimensions (both mandatory if 'help' is not requested).\n\n"
    "<max_value>\n\tRange for random values: [0.0 ... <max_value>].\n"
    "\tValid values are in range (%g ... %g). The default is %g\n\n"
    "-h,-H\tPrints this help message.\n\n", ( ( 1.17549435082228750796873653722224568e-38F ) ), ( 3.40282346638528859811704183484516925e+38F ), ( (1.0f) ) ) != 0 )
  status = 1;
 return status;
}

variable_types:
execname
char *

variable_types: status int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
int result; // eax
__int64 v5; // r14
unsigned int v6; // er15
int v7; // ecx
unsigned __int64 v8; // rbp
unsigned __int64 v9; // rbx
float *v10; // r13
unsigned int v11; // eax
float *v12; // rbp
unsigned int i; // er12
int *v14; // rax
__int64 j; // rbx
int v16; // ebp
unsigned int v17; // [rsp+Ch] [rbp-BCh]
char v18[52]; // [rsp+10h] [rbp-B8h] BYREF
float v19; // [rsp+44h] [rbp-84h] BYREF
unsigned int v20; // [rsp+48h] [rbp-80h] BYREF
unsigned int v21; // [rsp+4Ch] [rbp-7Ch] BYREF
__int64 v22; // [rsp+50h] [rbp-78h] BYREF
unsigned int v23; // [rsp+60h] [rbp-68h]
unsigned int v24; // [rsp+7Ch] [rbp-4Ch]
char v25[57]; // [rsp+8Fh] [rbp-39h] BYREF

process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v25[0] = 0;
v3 = check_arguments((unsigned int)argc, argv, v25, &v22);
result = 1;
if ( !v3 )
{
if ( v25[0] )
{
result = print_generate_matrix_help(*argv);
}
else
{
v5 = v22;
v6 = v23;
v21 = 0;
v20 = 0;
v19 = 1.0;
v7 = check_additional_arguments(argc, (__int64)argv, v24, &v21, &v20, &v19);
result = 1;
if ( !v7 )
{
v17 = get_padding(v20);
v8 = v21 * (unsigned __int64)v20;
v9 = v17 * (unsigned __int64)v21;
print_message(
0LL,
"Generating a %u-by-%u data matrix (%zu items), with random values in range [0 .. %g]...\n",
v21,
v20,
v8,
v19);
if ( (v20 == 1) + (v21 == 1) )
append_printed_error(
0LL,
"\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n");
v10 = (float *)malloc(4 * v9);
if ( v10 )
{
v11 = get_seed();
srandom(v11);
if ( v21 )
{
v12 = v10;
for ( i = 0; i < v21; ++i )
{
for ( j = 0LL; v20 > (unsigned int)j; ++j )
v12[j] = (float)(v19 * 4.6566129e-10) * (float)(int)random();
v12 += v17;
}
}
new_empty_matrix_tags(v18);
v16 = matrix_save(v5, v6, v10, v21, v20, v17, 0LL, v18, 1LL);
free(v10);
result = v16 != 0;
}
else
{
v14 = _errno_location();
print_errnum(0LL, (unsigned int)*v14, "malloc(matrix, items=%zu, %zu with padding)", v8, v9);
result = 1;
}
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
int result; // eax
__int64 v5; // r14
unsigned int v6; // er15
int v7; // ecx
unsigned __int64 v8; // rbp
unsigned __int64 v9; // rbx
float *v10; // r13
unsigned int v11; // eax
float *v12; // rbp
unsigned int i; // er12
int *v14; // rax
__int64 j; // rbx
int v16; // ebp
unsigned int v17; // [rsp+Ch] [rbp-BCh]
char v18[52]; // [rsp+10h] [rbp-B8h] BYREF
float v19; // [rsp+44h] [rbp-84h] BYREF
unsigned int v20; // [rsp+48h] [rbp-80h] BYREF
unsigned int v21; // [rsp+4Ch] [rbp-7Ch] BYREF
__int64 v22; // [rsp+50h] [rbp-78h] BYREF
unsigned int v23; // [rsp+60h] [rbp-68h]
unsigned int v24; // [rsp+7Ch] [rbp-4Ch]
char v25[57]; // [rsp+8Fh] [rbp-39h] BYREF

process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v25[0] = 0;
v3 = check_arguments((unsigned int)argc, argv, v25, &v22);
result = 1;
if ( !v3 )
{
if ( v25[0] )
{
result = func0(*argv);
}
else
{
v5 = v22;
v6 = v23;
v21 = 0;
v20 = 0;
v19 = 1.0;
v7 = func1(argc, (__int64)argv, v24, &v21, &v20, &v19);
result = 1;
if ( !v7 )
{
v17 = get_padding(v20);
v8 = v21 * (unsigned __int64)v20;
v9 = v17 * (unsigned __int64)v21;
print_message(
0LL,
"Generating a %u-by-%u data matrix (%zu items), with random values in range [0 .. %g]...\n",
v21,
v20,
v8,
v19);
if ( (v20 == 1) + (v21 == 1) )
append_printed_error(
0LL,
"\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n");
v10 = (float *)malloc(4 * v9);
if ( v10 )
{
v11 = get_seed();
srandom(v11);
if ( v21 )
{
v12 = v10;
for ( i = 0; i < v21; ++i )
{
for ( j = 0LL; v20 > (unsigned int)j; ++j )
v12[j] = (float)(v19 * 4.6566129e-10) * (float)(int)random();
v12 += v17;
}
}
new_empty_matrix_tags(v18);
v16 = matrix_save(v5, v6, v10, v21, v20, v17, 0LL, v18, 1LL);
free(v10);
result = v16 != 0;
}
else
{
v14 = _errno_location();
print_errnum(0LL, (unsigned int)*v14, "malloc(matrix, items=%zu, %zu with padding)", v8, v9);
result = 1;
}
}
}
}
return result;
}

original_code:
int main( int argc, char *argv[] )
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char const *restrict *restrict) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_generate_matrix_help( argv[0] );
 char const *restrict const filename = arguments.filename;
 file_fmt_t const output_file_fmt = arguments.output_file_fmt;
 index_t const idx_other_args = arguments.idx_other_args;
 index_t nrows = 0, ncols = 0;
 real max_value = ( (1.0f) );
 status = check_additional_arguments( argc, (char const *restrict *restrict) argv, idx_other_args, &nrows, &ncols, &max_value );
 if ( status != 0 )
  return 1;
 index_t const pitch = get_padding( ncols );
 size_t const nitems = (size_t) nrows * (size_t) ncols;
 size_t const nitems_padding = (size_t) nrows * (size_t) pitch;
 print_message( shown_by_all, "Generating a %" "u" "-by-%" "u" " data matrix (%zu"
   " items), with random values in range [0 .. %g]...\n", nrows, ncols, nitems, max_value );
 if ( (nrows == 1) + (ncols == 1) )
  append_printed_error( shown_by_all,
     "\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n" );
 real *restrict const matrix = (real *restrict) malloc( nitems_padding * sizeof(real) );
 if ( ! matrix ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "malloc(matrix, items=%zu, %zu with padding)", nitems, nitems_padding );
  return 1;
 }
 index_t seed = get_seed();
 srandom( (unsigned int) seed );
 real const denominator = (real) 2147483647;
 real *pmatrix = matrix;
 for ( index_t i=0 ; i < nrows ; i++, pmatrix += pitch ) {
  real *pmatrix_r = pmatrix;
  for ( index_t j=0 ; j < ncols ; j++, pmatrix_r++ ) {
   real const numerator = (real) random();
   real const val0_1 = numerator / denominator;
   real const val = val0_1 * max_value;
   *pmatrix_r = val;
  }
 }
 _Bool const transpose = 0;
 _Bool const verbose = 1;
 struct matrix_tags_t const mt = new_empty_matrix_tags();
 status = matrix_save( filename, output_file_fmt, matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( matrix );
 if ( status != 0 )
  return 1;
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char **) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_generate_matrix_help( argv[0] );
 char *filename = arguments.filename;
 enum file_format output_file_fmt = arguments.output_file_fmt;
 unsigned int idx_other_args = arguments.idx_other_args;
 unsigned int nrows = 0; unsigned int ncols = 0;
 float max_value = ( (1.0f) );
 status = check_additional_arguments( argc, (char **) argv, idx_other_args, &nrows, &ncols, &max_value );
 if ( status != 0 )
  return 1;
 unsigned int pitch = get_padding( ncols );
 unsigned long nitems = (size_t) nrows * (size_t) ncols;
 unsigned long nitems_padding = (size_t) nrows * (size_t) pitch;
 print_message( shown_by_all, "Generating a %" "u" "-by-%" "u" " data matrix (%zu"
   " items), with random values in range [0 .. %g]...\n", nrows, ncols, nitems, max_value );
 if ( (nrows == 1) + (ncols == 1) )
  append_printed_error( shown_by_all,
     "\nNote, however, that (row or column) vectors are not a valid input for the NMF algorithm.\n" );
 float *matrix = (real *restrict) malloc( nitems_padding * sizeof(real) );
 if ( ! matrix ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "malloc(matrix, items=%zu, %zu with padding)", nitems, nitems_padding );
  return 1;
 }
 unsigned int seed = get_seed();
 srandom( (unsigned int) seed );
 float denominator = (real) 2147483647;
 float *pmatrix = matrix;
 for ( unsigned int i = 0; i < nrows ; i++, pmatrix += pitch ) {
  float *pmatrix_r = pmatrix;
  for ( unsigned int j = 0; j < ncols ; j++, pmatrix_r++ ) {
   float numerator = (real) random();
   float val0_1 = numerator / denominator;
   float val = val0_1 * max_value;
   *pmatrix_r = val;
  }
 }
 _Bool transpose = 0;
 _Bool verbose = 1;
 struct matrix_tags_t mt = new_empty_matrix_tags();
 status = matrix_save( filename, output_file_fmt, matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( matrix );
 if ( status != 0 )
  return 1;
 return 0;
}

variable_types: struct input_arguments { char *filename; _Bool numeric_hdrs; _Bool numeric_lbls; enum file_format { ASCII_TEXT_FMT=0, NON_NATIVE_BINARY_FMT=1, NATIVE_BINARY_FMT=2 } input_file_fmt; enum file_format { ASCII_TEXT_FMT=0, NON_NATIVE_BINARY_FMT=1, NATIVE_BINARY_FMT=2 } output_file_fmt; unsigned int k; unsigned int kp; unsigned int nIters; unsigned int niter_test_conv; unsigned int stop_threshold; unsigned int gpu_device; unsigned int idx_other_args; }

variable_types:
filename
char *

variable_types:
nrows
unsigned int

variable_types:
ncols
unsigned int

variable_types:
pitch
unsigned int

variable_types:
nitems
unsigned long

variable_types:
matrix
float *

variable_types:
seed
unsigned int

variable_types: denominator float

variable_types:
pmatrix
float *

variable_types:
j
unsigned int

variable_types: numerator float

variable_types: val float

variable_types: struct matrix_tags_t { char *name; struct tag_t { char *tokens; char **ptokens; } headers; struct tag_t labels; }

user_defined_types: struct input_arguments { char *filename; _Bool numeric_hdrs; _Bool numeric_lbls; enum file_format input_file_fmt; enum file_format output_file_fmt; unsigned int k; unsigned int kp; unsigned int nIters; unsigned int niter_test_conv; unsigned int stop_threshold; unsigned int gpu_device; unsigned int idx_other_args; }

user_defined_types: struct matrix_tags_t { char *name; struct tag_t headers; struct tag_t labels; }


decompiled_code:
__int64 __fastcall print_file_converter_help(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"\t<< ASCII-Binary file converter >>\n"
"\n"
"Usage:\n"
"\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)append_printed_message(
0LL,
"\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n") )
v1 = 0;
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(0LL, "\n-h,-H\tPrints this help message.\n\n") == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_file_converter_help( execname )");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // ebx
bool v2; // zf
__int64 result; // rax

if ( a1 )
{
v1 = print_message(
0LL,
"\t<< ASCII-Binary file converter >>\n"
"\n"
"Usage:\n"
"\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n"
"\t%s -h\n"
"\n",
a1,
a1);
if ( (unsigned int)append_printed_message(
0LL,
"\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n") )
v1 = 0;
if ( (unsigned int)help_file_formats() )
v1 = 1;
v2 = (unsigned int)append_printed_message(0LL, "\n-h,-H\tPrints this help message.\n\n") == 0;
result = 1LL;
if ( v2 )
result = v1;
}
else
{
print_errnum(0LL, 14LL, "print_file_converter_help( execname )");
result = 1LL;
}
return result;
}

original_code:
static int print_file_converter_help( char const *restrict const execname )
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_file_converter_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "\t<< ASCII-Binary file converter >>\n\n"
    "Usage:\n\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n\t%s -h\n\n",
    execname, execname );
 if ( append_printed_message( shown_by_all,
    "\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n" ) != 0 )
  status = 0;
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\n-h,-H\tPrints this help message.\n\n" ) != 0 )
  status = 1;
 return status;
}

canonical_original_code:
static int print_file_converter_help(char *execname)
{
 if ( ! execname ) {
  print_errnum( error_shown_by_all, 14, "print_file_converter_help( execname )" );
  return 1;
 }
 int status = 0;
 status = print_message( shown_by_all, "\t<< ASCII-Binary file converter >>\n\n"
    "Usage:\n\t%s <filename> [ -b <native_format> ] [ -cr ] [ -e <native_format> ]\n\t%s -h\n\n",
    execname, execname );
 if ( append_printed_message( shown_by_all,
    "\n<filename>\n\tInput data matrix (mandatory if 'help' is not requested).\n\n" ) != 0 )
  status = 0;
 if ( help_file_formats() != 0 )
  status = 1;
 if ( append_printed_message( shown_by_all, "\n-h,-H\tPrints this help message.\n\n" ) != 0 )
  status = 1;
 return status;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
char *v4; // rbp
unsigned __int8 v5; // bl
unsigned __int8 v6; // r15
unsigned int v7; // er14
unsigned int v8; // er13
const char *v9; // r15
size_t v10; // rbx
size_t v11; // rax
char *v12; // rbx
int *v13; // r14
__int64 v14; // rcx
char *v15; // rdi
char *v16; // rsi
int *v18; // rax
char *v19; // rsi
__int64 v20; // rcx
_DWORD *v21; // rdi
__int64 v22; // rcx
_DWORD *v23; // rdi
_BYTE v24[48]; // [rsp-10h] [rbp-E8h] BYREF
char v25[44]; // [rsp+20h] [rbp-B8h] BYREF
unsigned int v26; // [rsp+4Ch] [rbp-8Ch] BYREF
unsigned int v27; // [rsp+50h] [rbp-88h] BYREF
unsigned int v28; // [rsp+54h] [rbp-84h] BYREF
__int64 v29; // [rsp+58h] [rbp-80h] BYREF
char *s; // [rsp+60h] [rbp-78h] BYREF
unsigned __int8 v31; // [rsp+68h] [rbp-70h]
unsigned __int8 v32; // [rsp+69h] [rbp-6Fh]
unsigned int v33; // [rsp+6Ch] [rbp-6Ch]
unsigned int v34; // [rsp+70h] [rbp-68h]
__int64 v35; // [rsp+88h] [rbp-50h]
char v36[57]; // [rsp+9Fh] [rbp-39h] BYREF

process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v36[0] = 0;
v3 = 1;
if ( !(unsigned int)check_arguments((unsigned int)argc, argv, v36, &s) )
{
if ( v36[0] )
return print_file_converter_help(*argv);
v4 = s;
v5 = v31;
v6 = v32;
v7 = v33;
v8 = v34;
v29 = 0LL;
v28 = 0;
v27 = 0;
v26 = 0;
new_empty_matrix_tags(v25);
if ( (unsigned int)matrix_load(v4, v5, v6, v7, &v29, &v28, &v27, &v26, v25) )
{
print_error(0LL, "Error reading '%s'\n", v4);
return 1;
}
v9 = (const char *)file_extension[v8];
v10 = strlen(v4);
v11 = strlen(v9);
v12 = (char *)malloc(v10 + v11);
if ( v12 )
{
v13 = _errno_location();
*v13 = 0;
if ( sprintf(v12, "%s%s", v4, v9) > 0 )
{
print_message(0LL, "Output file: %s\n", v12);
v3 = matrix_save(v12, v8, v29, v28, v27, v26, 0LL, v25, 1LL);
free(v12);
v14 = 10LL;
v15 = v25;
v16 = v25;
while ( v14 )
{
*(_DWORD *)v15 = *(_DWORD *)v16;
v16 += 4;
v15 += 4;
--v14;
}
matrix_clean(v35, v16);
return v3;
}
print_errnum(0LL, (unsigned int)*v13, "Error setting output filename (sprintf)");
free(v12);
v19 = v25;
v22 = 10LL;
v23 = v24;
while ( v22 )
{
*v23 = *(_DWORD *)v19;
v19 += 4;
++v23;
--v22;
}
}
else
{
v18 = _errno_location();
print_errnum(0LL, (unsigned int)*v18, "Error setting output filename: malloc(filename_str)");
v19 = v25;
v20 = 10LL;
v21 = v24;
while ( v20 )
{
*v21 = *(_DWORD *)v19;
v19 += 4;
++v21;
--v20;
}
}
matrix_clean(v29, v19);
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
char *v4; // rbp
unsigned __int8 v5; // bl
unsigned __int8 v6; // r15
unsigned int v7; // er14
unsigned int v8; // er13
const char *v9; // r15
size_t v10; // rbx
size_t v11; // rax
char *v12; // rbx
int *v13; // r14
__int64 v14; // rcx
char *v15; // rdi
char *v16; // rsi
int *v18; // rax
char *v19; // rsi
__int64 v20; // rcx
_DWORD *v21; // rdi
__int64 v22; // rcx
_DWORD *v23; // rdi
_BYTE v24[48]; // [rsp-10h] [rbp-E8h] BYREF
char v25[44]; // [rsp+20h] [rbp-B8h] BYREF
unsigned int v26; // [rsp+4Ch] [rbp-8Ch] BYREF
unsigned int v27; // [rsp+50h] [rbp-88h] BYREF
unsigned int v28; // [rsp+54h] [rbp-84h] BYREF
__int64 v29; // [rsp+58h] [rbp-80h] BYREF
char *s; // [rsp+60h] [rbp-78h] BYREF
unsigned __int8 v31; // [rsp+68h] [rbp-70h]
unsigned __int8 v32; // [rsp+69h] [rbp-6Fh]
unsigned int v33; // [rsp+6Ch] [rbp-6Ch]
unsigned int v34; // [rsp+70h] [rbp-68h]
__int64 v35; // [rsp+88h] [rbp-50h]
char v36[57]; // [rsp+9Fh] [rbp-39h] BYREF

process_id = 0;
num_processes = 1;
num_act_processes = 1;
set_default_matrix_limits(argc, argv, envp);
v36[0] = 0;
v3 = 1;
if ( !(unsigned int)check_arguments((unsigned int)argc, argv, v36, &s) )
{
if ( v36[0] )
return func0(*argv);
v4 = s;
v5 = v31;
v6 = v32;
v7 = v33;
v8 = v34;
v29 = 0LL;
v28 = 0;
v27 = 0;
v26 = 0;
new_empty_matrix_tags(v25);
if ( (unsigned int)matrix_load(v4, v5, v6, v7, &v29, &v28, &v27, &v26, v25) )
{
print_error(0LL, "Error reading '%s'\n", v4);
return 1;
}
v9 = (const char *)file_extension[v8];
v10 = strlen(v4);
v11 = strlen(v9);
v12 = (char *)malloc(v10 + v11);
if ( v12 )
{
v13 = _errno_location();
*v13 = 0;
if ( sprintf(v12, "%s%s", v4, v9) > 0 )
{
print_message(0LL, "Output file: %s\n", v12);
v3 = matrix_save(v12, v8, v29, v28, v27, v26, 0LL, v25, 1LL);
free(v12);
v14 = 10LL;
v15 = v25;
v16 = v25;
while ( v14 )
{
*(_DWORD *)v15 = *(_DWORD *)v16;
v16 += 4;
v15 += 4;
--v14;
}
matrix_clean(v35, v16);
return v3;
}
print_errnum(0LL, (unsigned int)*v13, "Error setting output filename (sprintf)");
free(v12);
v19 = v25;
v22 = 10LL;
v23 = v24;
while ( v22 )
{
*v23 = *(_DWORD *)v19;
v19 += 4;
++v23;
--v22;
}
}
else
{
v18 = _errno_location();
print_errnum(0LL, (unsigned int)*v18, "Error setting output filename: malloc(filename_str)");
v19 = v25;
v20 = 10LL;
v21 = v24;
while ( v20 )
{
*v21 = *(_DWORD *)v19;
v19 += 4;
++v21;
--v20;
}
}
matrix_clean(v29, v19);
v3 = 1;
}
return v3;
}

original_code:
int main( int argc, char *argv[] )
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char const *restrict *restrict) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_file_converter_help( argv[0] );
 char const *restrict const filename = arguments.filename;
 _Bool const numeric_hdrs = arguments.numeric_hdrs;
 _Bool const numeric_lbls = arguments.numeric_lbls;
 file_fmt_t const input_file_fmt = arguments.input_file_fmt;
 file_fmt_t const output_file_fmt = arguments.output_file_fmt;
 real *restrict matrix = ((void *)0);
 index_t nrows = 0, ncols = 0, pitch = 0;
 struct matrix_tags_t mt = new_empty_matrix_tags();
 _Bool const transpose = 0;
 status = matrix_load( filename, numeric_hdrs, numeric_lbls, input_file_fmt, &matrix, &nrows, &ncols, &pitch, &mt );
 if ( status != 0 ) {
  print_error( error_shown_by_all, "Error reading '%s'\n", filename );
  return 1;
 }
 char const *restrict const p_file_ext = file_extension[ output_file_fmt ];
 char *restrict const filename_str = malloc( (strlen(filename) + strlen(p_file_ext)) * sizeof(char) );
 if ( ! filename_str ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename: malloc(filename_str)" );
  matrix_clean( matrix, mt );
  return 1;
 }
 (*__errno_location ()) = 0;
 int const conv = sprintf( filename_str, "%s%s", filename, p_file_ext );
 if ( conv <= 0 ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename (sprintf)" );
  free( filename_str ); matrix_clean( matrix, mt );
  return 1;
 }
 print_message( shown_by_all, "Output file: %s\n", filename_str );
 _Bool const verbose = 1;
 status = matrix_save( filename_str, output_file_fmt, (real const *restrict) matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( filename_str );
 matrix_clean( matrix, mt );
 return status;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 process_id = 0;
 num_act_processes = num_processes = 1;
 set_default_matrix_limits();
 int status = 0;
 _Bool help = 0;
 struct input_arguments arguments;
 if ( check_arguments( argc, (char **) argv, &help, &arguments ) != 0 )
  return 1;
 if ( help )
  return print_file_converter_help( argv[0] );
 char *filename = arguments.filename;
 _Bool numeric_hdrs = arguments.numeric_hdrs;
 _Bool numeric_lbls = arguments.numeric_lbls;
 enum file_format input_file_fmt = arguments.input_file_fmt;
 enum file_format output_file_fmt = arguments.output_file_fmt;
 float *matrix = ((void *)0);
 unsigned int nrows = 0; unsigned int ncols = 0; unsigned int pitch = 0;
 struct matrix_tags_t mt = new_empty_matrix_tags();
 _Bool transpose = 0;
 status = matrix_load( filename, numeric_hdrs, numeric_lbls, input_file_fmt, &matrix, &nrows, &ncols, &pitch, &mt );
 if ( status != 0 ) {
  print_error( error_shown_by_all, "Error reading '%s'\n", filename );
  return 1;
 }
 char *p_file_ext = file_extension[ output_file_fmt ];
 char *filename_str = malloc( (strlen(filename) + strlen(p_file_ext)) * sizeof(char) );
 if ( ! filename_str ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename: malloc(filename_str)" );
  matrix_clean( matrix, mt );
  return 1;
 }
 (*__errno_location ()) = 0;
 int conv = sprintf( filename_str, "%s%s", filename, p_file_ext );
 if ( conv <= 0 ) {
  print_errnum( sys_error_shown_by_all, (*__errno_location ()), "Error setting output filename (sprintf)" );
  free( filename_str ); matrix_clean( matrix, mt );
  return 1;
 }
 print_message( shown_by_all, "Output file: %s\n", filename_str );
 _Bool verbose = 1;
 status = matrix_save( filename_str, output_file_fmt, (float *) matrix, nrows, ncols, pitch, transpose, &mt, verbose );
 free( filename_str );
 matrix_clean( matrix, mt );
 return status;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: enum file_format { ASCII_TEXT_FMT=0, NON_NATIVE_BINARY_FMT=1, NATIVE_BINARY_FMT=2 }

variable_types: conv int

user_defined_types: struct tag_t { char *tokens; char **ptokens; }


bail

repo: bitbanger/roommate

decompiled_code:
void __fastcall __noreturn bail(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = __errno_location();
v2 = strerror(*v1);
fprintf(stderr, "%s: %s\n", a1, v2);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = __errno_location();
v2 = strerror(*v1);
fprintf(stderr, "%s: %s\n", a1, v2);
exit(1);
}

original_code:
void bail(const char *problem) {
        fprintf(stderr, "%s: %s\n", problem, strerror((*__errno_location ())));
        exit(1);
}

canonical_original_code:
void bail(char *problem) {
        fprintf(stderr, "%s: %s\n", problem, strerror((*__errno_location ())));
        exit(1);
}

variable_types:
problem
char *


sendpkt

decompiled_code:
int __fastcall sendpkt(void *buf)
{
int v1; // eax
int v2; // ebx
struct sockaddr v4[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0 )
bail("socket()");
v2 = v1;
v4[0].sa_family = 2;
*(_WORD *)v4[0].sa_data = _bswap_16(1030);
inet_pton(2, "127.0.0.1", &v4[0].sa_data[2]);
if ( sendto(v2, buf, 4uLL, 0, v4, 0x10u) < 0 )
bail("sendto()");
return close(v2);
}

canonical_decompiled_code:
int __fastcall func10(void *buf)
{
int v1; // eax
int v2; // ebx
struct sockaddr v4[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0 )
func9("socket()");
v2 = v1;
v4[0].sa_family = 2;
*(_WORD *)v4[0].sa_data = func7(1030);
inet_pton(2, "127.0.0.1", &v4[0].sa_data[2]);
if ( sendto(v2, buf, 4uLL, 0, v4, 0x10u) < 0 )
func9("sendto()");
return close(v2);
}

original_code:
void sendpkt(const uint8_t *pkt) {
 int sfd = -1;
 if((sfd = socket(2, SOCK_DGRAM, 0)) < 0)
  bail("socket()");
 struct sockaddr_in addr;
 addr.sin_family = 2;
 addr.sin_port = htons(PORT);
 inet_pton(2, "127.0.0.1", &addr.sin_addr);
 if(sendto(sfd, pkt, PCKT_LEN, 0, (struct sockaddr *)&addr, sizeof addr) < 0)
  bail("sendto()");
 close(sfd);
}

canonical_original_code:
void sendpkt(unsigned char *pkt) {
 int sfd = -1;
 if((sfd = socket(2, SOCK_DGRAM, 0)) < 0)
  bail("socket()");
 struct sockaddr_in addr;
 addr.sin_family = 2;
 addr.sin_port = htons(PORT);
 inet_pton(2, "127.0.0.1", &addr.sin_addr);
 if(sendto(sfd, pkt, PCKT_LEN, 0, (struct sockaddr *)&addr, sizeof addr) < 0)
  bail("sendto()");
 close(sfd);
}

variable_types:
pkt
unsigned char *

variable_types: sfd int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


cmdWren

repo: bitcraze/crazyradio-firmware

decompiled_code:
__int64 cmdWren()
{
unsigned int v0; // ebx

buffer = 6;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

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

buffer = 6;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

original_code:
int cmdWren() {
  int i=0;
  int ret;
  buffer[i++] = 0x06;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdWren() {
  int i = 0;
  int ret;
  buffer[i++] = 0x06;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

variable_types: i int

variable_types: ret int


cmdWrdis

decompiled_code:
__int64 cmdWrdis()
{
unsigned int v0; // ebx

buffer = 4;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx

buffer = 4;
spiSetCS(0LL);
v0 = spiSend(&buffer, 1LL);
spiSetCS(1LL);
return v0;
}

original_code:
int cmdWrdis() {
  int i=0;
  int ret;
  buffer[i++] = 0x04;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdWrdis() {
  int i = 0;
  int ret;
  buffer[i++] = 0x04;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}


cmdRdsr

decompiled_code:
__int64 __fastcall cmdRdsr(_BYTE *a1)
{
unsigned int v1; // ebx

buffer = 5;
spiSetCS(0LL);
spiSend(&buffer, 1LL);
v1 = spiReceive(&buffer, 1LL);
spiSetCS(1LL);
*a1 = buffer;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1)
{
unsigned int v1; // ebx

buffer = 5;
spiSetCS(0LL);
spiSend(&buffer, 1LL);
v1 = spiReceive(&buffer, 1LL);
spiSetCS(1LL);
*a1 = buffer;
return v1;
}

original_code:
int cmdRdsr(char *fsr) {
  int i=0;
  int ret;
  buffer[i++] = 0x05;
  spiSetCS(0);
  spiSend(buffer, i);
  ret = spiReceive(buffer, 1);
  spiSetCS(1);
  *fsr = buffer[0];
  return ret;
}

canonical_original_code:
int cmdRdsr(char *fsr) {
  int i = 0;
  int ret;
  buffer[i++] = 0x05;
  spiSetCS(0);
  spiSend(buffer, i);
  ret = spiReceive(buffer, 1);
  spiSetCS(1);
  *fsr = buffer[0];
  return ret;
}

variable_types:
fsr
char *


cmdWrsr

decompiled_code:
__int64 __fastcall cmdWrsr(char a1)
{
unsigned int v1; // ebx

buffer = 1;
byte_6E1 = a1;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
unsigned int v1; // ebx

buffer = 1;
byte_6E1 = a1;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

original_code:
int cmdWrsr(char fsr) {
  int i=0;
  int ret;
  buffer[i++] = 0x01;
  buffer[i++] = fsr;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdWrsr(char fsr) {
  int i = 0;
  int ret;
  buffer[i++] = 0x01;
  buffer[i++] = fsr;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

variable_types: fsr char


cmdRead

decompiled_code:
__int64 __fastcall cmdRead(__int16 a1, __int64 a2, int a3)
{
unsigned int v5; // er15
int v6; // er12
unsigned int v7; // ebx
__int64 v8; // rsi
unsigned int v9; // ebx
int v11; // [rsp+Ch] [rbp-3Ch]

if ( a3 <= 0 )
{
v9 = 0;
}
else
{
v11 = a3 - 1;
v5 = ((a3 - 1) & 0xFFFFFF80) + 128;
v6 = 0;
v7 = 0;
do
{
printf("\rRead byte %d/%d (%3d%%)", v7, (unsigned int)a3, (unsigned int)(v6 / a3));
fflush(stdout);
buffer = 3;
byte_6E1 = (unsigned __int16)(a1 + v7) >> 8;
byte_6E2 = a1 + v7;
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v8 = a3 - v7;
if ( (int)v8 > 128 )
v8 = 128LL;
spiReceive(a2, v8);
spiSetCS(1LL);
v7 += 128;
v6 += 12800;
a2 += 128LL;
}
while ( v7 != v5 );
v9 = (v11 & 0xFFFFFF80) + 128;
}
printf("\rRead byte %d/%d (%3d%%)\n", v9, (unsigned int)a3, 100LL);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1, __int64 a2, int a3)
{
unsigned int v5; // er15
int v6; // er12
unsigned int v7; // ebx
__int64 v8; // rsi
unsigned int v9; // ebx
int v11; // [rsp+Ch] [rbp-3Ch]

if ( a3 <= 0 )
{
v9 = 0;
}
else
{
v11 = a3 - 1;
v5 = ((a3 - 1) & 0xFFFFFF80) + 128;
v6 = 0;
v7 = 0;
do
{
printf("\rRead byte %d/%d (%3d%%)", v7, (unsigned int)a3, (unsigned int)(v6 / a3));
fflush(stdout);
buffer = 3;
byte_6E1 = (unsigned __int16)(a1 + v7) >> 8;
byte_6E2 = a1 + v7;
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v8 = a3 - v7;
if ( (int)v8 > 128 )
v8 = 128LL;
spiReceive(a2, v8);
spiSetCS(1LL);
v7 += 128;
v6 += 12800;
a2 += 128LL;
}
while ( v7 != v5 );
v9 = (v11 & 0xFFFFFF80) + 128;
}
printf("\rRead byte %d/%d (%3d%%)\n", v9, (unsigned int)a3, 100LL);
return v9;
}

original_code:
int cmdRead(int addr, char *data, int len)
{
  int i=0;
  for(i=0; i<len; i+=128) {
    printf("\rRead byte %d/%d (%3d%%)", i, len, (100*i)/len);
    fflush(stdout);
    buffer[0] = 0x03;
    buffer[1] = ((addr+i)>>8)&0x0FF;
    buffer[2] = (addr+i)&0x0FF;
    spiSetCS(0);
    spiSend(buffer, 3);
    spiReceive(data+i, ((len-i)>128)?128:(len-i) );
    spiSetCS(1);
  }
  printf("\rRead byte %d/%d (%3d%%)\n", i, len, 100);
  return i;
}

canonical_original_code:
int cmdRead(int addr, char *data, int len)
{
  int i = 0;
  for(i=0; i<len; i+=128) {
    printf("\rRead byte %d/%d (%3d%%)", i, len, (100*i)/len);
    fflush(stdout);
    buffer[0] = 0x03;
    buffer[1] = ((addr+i)>>8)&0x0FF;
    buffer[2] = (addr+i)&0x0FF;
    spiSetCS(0);
    spiSend(buffer, 3);
    spiReceive(data+i, ((len-i)>128)?128:(len-i) );
    spiSetCS(1);
  }
  printf("\rRead byte %d/%d (%3d%%)\n", i, len, 100);
  return i;
}

variable_types: addr int

variable_types:
data
char *

variable_types: len int


cmdErasePage

decompiled_code:
__int64 __fastcall cmdErasePage(char a1)
{
unsigned int v1; // ebx

buffer = 82;
byte_6E1 = a1;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(char a1)
{
unsigned int v1; // ebx

buffer = 82;
byte_6E1 = a1;
spiSetCS(0LL);
v1 = spiSend(&buffer, 2LL);
spiSetCS(1LL);
return v1;
}

original_code:
int cmdErasePage(int pageNo) {
  int i=0;
  int ret;
  buffer[i++] = 0x52;
  buffer[i++] = pageNo;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

canonical_original_code:
int cmdErasePage(int pageNo) {
  int i = 0;
  int ret;
  buffer[i++] = 0x52;
  buffer[i++] = pageNo;
  spiSetCS(0);
  ret = spiSend(buffer, i);
  spiSetCS(1);
  return ret;
}

variable_types: pageNo int


cmdProgram

decompiled_code:
__int64 __fastcall cmdProgram(int a1, __int64 a2, signed int a3)
{
signed int v5; // ebx
int v6; // er13
int v7; // er12
unsigned int v8; // er15
signed int v9; // eax
int v10; // er12
__int64 result; // rax
__int64 v12; // rdx
char *v13; // rax
__int64 v14; // rsi
struct _IO_FILE *v15; // rdi
__int16 v16; // r15
char *v17; // [rsp+8h] [rbp-60h]
unsigned int v18; // [rsp+14h] [rbp-54h]
unsigned int v19; // [rsp+18h] [rbp-50h]
int v20; // [rsp+1Ch] [rbp-4Ch]
char v21[57]; // [rsp+2Fh] [rbp-39h] BYREF

if ( a1 > 30720 )
{
puts("ERROR: Writing above 30K is disabled!");
result = 0xFFFFFFFFLL;
}
else
{
v5 = a3;
v6 = a1 + a3;
if ( a1 + a3 > 30720 )
{
v5 = 30720 - a1;
printf("WARNING Write above 30K disabled! The new write length is %d\n", (unsigned int)(30720 - a1));
}
v7 = a1 / 512;
v18 = a1 / 512;
v8 = (a1 / 512) << 9;
if ( (v5 & 0x1FF) != 0 )
{
v9 = v5 + 511;
if ( v5 >= 0 )
v9 = v5;
v5 = (v9 & 0xFFFFFE00) + 512;
}
printf("Write the device from 0x%04X, with a length of %d bytes\n", v8, (unsigned int)v5);
v20 = (int)(v5 + v8) / 512;
if ( v7 >= v20 )
{
puts(" Done!");
}
else
{
v19 = v5 / 512 - 1;
do
{
printf("\rErasing page %d/%d ...", (unsigned int)v7, v19);
fflush(stdout);
((void (*)(void))cmdWren)();
cmdErasePage(v7);
cmdRdsr(v21);
while ( v21[0] )
cmdRdsr(v21);
++v7;
}
while ( v7 != v20 );
puts(" Done!");
v10 = 0;
do
{
v14 = v18;
printf("\rWriting page %d/%d ...", v18, v19);
v15 = stdout;
fflush(stdout);
v16 = (_WORD)v18 << 9;
v17 = (char *)&page_0;
v13 = (char *)&page_0;
LODWORD(v12) = v18 << 9;
do
{
if ( (int)v12 >= a1 )
{
if ( v6 >= (int)v12 )
*v13 = *(_BYTE *)(a2 + v10++);
else
*v13 = -1;
}
else
{
*v13 = -1;
}
v12 = (unsigned int)(v12 + 1);
++v13;
}
while ( &buffer != v13 );
do
{
cmdWren(v15, v14, v12);
buffer = 2;
byte_6E1 = HIBYTE(v16);
byte_6E2 = v16;
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v14 = 128LL;
spiSend(v17, 128LL);
spiSetCS(1LL);
v15 = (struct _IO_FILE *)v21;
cmdRdsr(v21);
while ( v21[0] )
{
v15 = (struct _IO_FILE *)v21;
cmdRdsr(v21);
}
v16 += 128;
v17 += 128;
}
while ( &buffer != v17 );
++v18;
}
while ( v18 != v20 );
}
puts("Done!");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 a2, signed int a3)
{
signed int v5; // ebx
int v6; // er13
int v7; // er12
unsigned int v8; // er15
signed int v9; // eax
int v10; // er12
__int64 result; // rax
__int64 v12; // rdx
char *v13; // rax
__int64 v14; // rsi
struct _IO_FILE *v15; // rdi
__int16 v16; // r15
char *v17; // [rsp+8h] [rbp-60h]
unsigned int v18; // [rsp+14h] [rbp-54h]
unsigned int v19; // [rsp+18h] [rbp-50h]
int v20; // [rsp+1Ch] [rbp-4Ch]
char v21[57]; // [rsp+2Fh] [rbp-39h] BYREF

if ( a1 > 30720 )
{
puts("ERROR: Writing above 30K is disabled!");
result = 0xFFFFFFFFLL;
}
else
{
v5 = a3;
v6 = a1 + a3;
if ( a1 + a3 > 30720 )
{
v5 = 30720 - a1;
printf("WARNING Write above 30K disabled! The new write length is %d\n", (unsigned int)(30720 - a1));
}
v7 = a1 / 512;
v18 = a1 / 512;
v8 = (a1 / 512) << 9;
if ( (v5 & 0x1FF) != 0 )
{
v9 = v5 + 511;
if ( v5 >= 0 )
v9 = v5;
v5 = (v9 & 0xFFFFFE00) + 512;
}
printf("Write the device from 0x%04X, with a length of %d bytes\n", v8, (unsigned int)v5);
v20 = (int)(v5 + v8) / 512;
if ( v7 >= v20 )
{
puts(" Done!");
}
else
{
v19 = v5 / 512 - 1;
do
{
printf("\rErasing page %d/%d ...", (unsigned int)v7, v19);
fflush(stdout);
((void (*)(void))cmdWren)();
func5(v7);
func2(v21);
while ( v21[0] )
func2(v21);
++v7;
}
while ( v7 != v20 );
puts(" Done!");
v10 = 0;
do
{
v14 = v18;
printf("\rWriting page %d/%d ...", v18, v19);
v15 = stdout;
fflush(stdout);
v16 = (_WORD)v18 << 9;
v17 = (char *)&page_0;
v13 = (char *)&page_0;
LODWORD(v12) = v18 << 9;
do
{
if ( (int)v12 >= a1 )
{
if ( v6 >= (int)v12 )
*v13 = *(_BYTE *)(a2 + v10++);
else
*v13 = -1;
}
else
{
*v13 = -1;
}
v12 = (unsigned int)(v12 + 1);
++v13;
}
while ( &buffer != v13 );
do
{
func0(v15, v14, v12);
buffer = 2;
byte_6E1 = HIBYTE(v16);
byte_6E2 = v16;
spiSetCS(0LL);
spiSend(&buffer, 3LL);
v14 = 128LL;
spiSend(v17, 128LL);
spiSetCS(1LL);
v15 = (struct _IO_FILE *)v21;
func2(v21);
while ( v21[0] )
{
v15 = (struct _IO_FILE *)v21;
func2(v21);
}
v16 += 128;
v17 += 128;
}
while ( &buffer != v17 );
++v18;
}
while ( v18 != v20 );
}
puts("Done!");
result = 0LL;
}
return result;
}

original_code:
int cmdProgram(int addr, char *data, int len) {
  static char page[512];
  char fsr;
  int i, j;
  int oaddr = addr;
  int olen = len;
  int ptr=0;
  if (addr>30*1024) {
    printf("ERROR: Writing above 30K is disabled!\n");
    return -1;
  }
  if((addr+len)>30*1024) {
    len = (30*1024)-addr;
    printf("WARNING Write above 30K disabled! The new write length is %d\n", len);
  }
  addr = (addr/512)*512;
  if(len%512) len = ((len/512)+1)*512;
  printf("Write the device from 0x%04X, with a length of %d bytes\n", addr, len);
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rErasing page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    cmdWren();
    cmdErasePage(i);
    cmdRdsr(&fsr);
    while(fsr)
      cmdRdsr(&fsr);
  }
  printf(" Done!\n");
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rWriting page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    for(j=0;j<512;j++) {
      if(((i*512)+j)<oaddr) page[j]=0xFF;
      else if (((i*512)+j)>(oaddr+olen)) page[j]=0xFF;
      else page[j] = data[ptr++];
    }
    for(j=0;j<4;j++) {
      cmdWren();
      buffer[0] = 0x02;
      buffer[1] = (((i*512)+(j*128))>>8)&0x0FF;
      buffer[2] = ((i*512)+(j*128))&0x0FF;
      spiSetCS(0);
      spiSend(buffer, 3);
      spiSend(page+(j*128), 128);
      spiSetCS(1);
      cmdRdsr(&fsr);
      while(fsr) cmdRdsr(&fsr);
    }
  }
  printf("Done!\n");
  return 0;
}

canonical_original_code:
int cmdProgram(int addr, char *data, int len) {
  static char page[512];
  char fsr;
  int i; int j;
  int oaddr = addr;
  int olen = len;
  int ptr = 0;
  if (addr>30*1024) {
    printf("ERROR: Writing above 30K is disabled!\n");
    return -1;
  }
  if((addr+len)>30*1024) {
    len = (30*1024)-addr;
    printf("WARNING Write above 30K disabled! The new write length is %d\n", len);
  }
  addr = (addr/512)*512;
  if(len%512) len = ((len/512)+1)*512;
  printf("Write the device from 0x%04X, with a length of %d bytes\n", addr, len);
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rErasing page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    cmdWren();
    cmdErasePage(i);
    cmdRdsr(&fsr);
    while(fsr)
      cmdRdsr(&fsr);
  }
  printf(" Done!\n");
  for(i=(addr/512);i<((addr+len)/512); i++) {
    printf("\rWriting page %d/%d ...", i, (len/512)-1);
    fflush(stdout);
    for(j=0;j<512;j++) {
      if(((i*512)+j)<oaddr) page[j]=0xFF;
      else if (((i*512)+j)>(oaddr+olen)) page[j]=0xFF;
      else page[j] = data[ptr++];
    }
    for(j=0;j<4;j++) {
      cmdWren();
      buffer[0] = 0x02;
      buffer[1] = (((i*512)+(j*128))>>8)&0x0FF;
      buffer[2] = ((i*512)+(j*128))&0x0FF;
      spiSetCS(0);
      spiSend(buffer, 3);
      spiSend(page+(j*128), 128);
      spiSetCS(1);
      cmdRdsr(&fsr);
      while(fsr) cmdRdsr(&fsr);
    }
  }
  printf("Done!\n");
  return 0;
}

variable_types:
page
char [512]

variable_types: j int

variable_types: oaddr int

variable_types: olen int

variable_types: ptr int


holdReset

decompiled_code:
__int64 holdReset()
{
puts("\nHold the reset");
return spiSetResetProg(0LL, 0LL);
}

canonical_decompiled_code:
__int64 func0()
{
puts("\nHold the reset");
return spiSetResetProg(0LL, 0LL);
}

original_code:
void holdReset()
{
  printf("\nHold the reset\n");
  spiSetResetProg(0, 0);
}


activateSPI

decompiled_code:
__int64 activateSPI()
{
spiSetSpiOE(0LL);
return spiSetCS(1LL);
}

canonical_decompiled_code:
__int64 func1()
{
spiSetSpiOE(0LL);
return spiSetCS(1LL);
}

original_code:
void activateSPI()
{
  spiSetSpiOE(0);
  spiSetCS(1);
}


resetProg

decompiled_code:
__int64 resetProg()
{
spiSetResetProg(0LL, 0LL);
usleep(0x1388u);
spiSetResetProg(1LL, 1LL);
usleep(0x1388u);
return activateSPI();
}

canonical_decompiled_code:
__int64 func2()
{
spiSetResetProg(0LL, 0LL);
usleep(0x1388u);
spiSetResetProg(1LL, 1LL);
usleep(0x1388u);
return func1();
}

original_code:
void resetProg()
{
  spiSetResetProg(0, 0);
  usleep(5000);
  spiSetResetProg(1, 1);
  usleep(5000);
  activateSPI();
}


deactivateSPI

decompiled_code:
__int64 deactivateSPI()
{
return spiSetSpiOE(1LL);
}

canonical_decompiled_code:
__int64 func3()
{
return spiSetSpiOE(1LL);
}

original_code:
void deactivateSPI()
{
  spiSetSpiOE(1);
}


resetRun

decompiled_code:
__int64 resetRun()
{
deactivateSPI();
puts("\nReset the nRF24LU01 in run mode");
spiSetResetProg(0LL, 0LL);
usleep(0xC350u);
return spiSetResetProg(1LL, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
func3();
puts("\nReset the nRF24LU01 in run mode");
spiSetResetProg(0LL, 0LL);
usleep(0xC350u);
return spiSetResetProg(1LL, 0LL);
}

original_code:
void resetRun()
{
  deactivateSPI();
  printf("\nReset the nRF24LU01 in run mode\n");
  spiSetResetProg(0, 0);
  usleep(50000);
  spiSetResetProg(1, 0);
}


help

decompiled_code:
int __fastcall help(const char *a1)
{
return printf(
"nrfProg V0.01 - nRF24LU1 programmer using the Amontec JTAG Key\n"
"\n"
"Usage: %s <COMMAND> <PARAMETERS>\n"
"Commands:\n"
" help:\n"
" Display this message\n"
" read <filename> [addr] [len]:\n"
" read the memory and write a dump in filename\n"
" write <filename> [addr]:\n"
" write the content of the binary <filename> into the nRF24LU flash.\n"
" hexdump [addr] [len]:\n"
" Display a hex dump of the memory\n"
" reset:\n"
" Reset the nRF24LU1+\n"
" holdreset:\n"
" Hold the reset (ie. stop) the nRF24LU1+\n",
a1);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
return printf(
"nrfProg V0.01 - nRF24LU1 programmer using the Amontec JTAG Key\n"
"\n"
"Usage: %s <COMMAND> <PARAMETERS>\n"
"Commands:\n"
" help:\n"
" Display this message\n"
" read <filename> [addr] [len]:\n"
" read the memory and write a dump in filename\n"
" write <filename> [addr]:\n"
" write the content of the binary <filename> into the nRF24LU flash.\n"
" hexdump [addr] [len]:\n"
" Display a hex dump of the memory\n"
" reset:\n"
" Reset the nRF24LU1+\n"
" holdreset:\n"
" Hold the reset (ie. stop) the nRF24LU1+\n",
a1);
}

original_code:
void help(char *prog)
{
  printf("nrfProg V0.01 - nRF24LU1 programmer using the Amontec JTAG Key\n"
         "\n"
         "Usage: %s <COMMAND> <PARAMETERS>\n"
         "Commands:\n"
         "  help:\n"
         "      Display this message\n"
         "  read <filename> [addr] [len]:\n"
         "      read the memory and write a dump in filename\n"
         "  write <filename> [addr]:\n"
         "      write the content of the binary <filename> into the nRF24LU flash.\n"
         "  hexdump [addr] [len]:\n"
         "      Display a hex dump of the memory\n"
         "  reset:\n"
         "      Reset the nRF24LU1+\n"
         "  holdreset:\n"
         "      Hold the reset (ie. stop) the nRF24LU1+\n", prog);
}

variable_types:
prog
char *


hexdump

decompiled_code:
__int64 __fastcall hexdump(int a1, _QWORD *a2)
{
__int64 v2; // rbp
__int64 i; // rbx
const unsigned __int16 **v4; // rax
char v5; // di
char v7[24]; // [rsp+0h] [rbp-38h]
unsigned int v8; // [rsp+18h] [rbp-20h] BYREF
unsigned int v9; // [rsp+1Ch] [rbp-1Ch] BYREF

v9 = 0;
v8 = 0x8000;
resetProg();
if ( a1 > 0 )
_isoc99_sscanf(*a2, "%d", &v9);
if ( a1 > 1 )
_isoc99_sscanf(a2[1], "%d", &v8);
if ( (int)v9 > 0x7FFF )
v9 = 0x7FFF;
if ( (int)(v8 + v9) > 0x7FFF )
v8 = 0x8000 - v9;
printf("hexdump, display %d bytes from 0x%04X\n", v8, v9);
cmdRead(v9, buffer, v8);
if ( (int)v8 > 0 )
{
v2 = 0LL;
do
{
if ( (v2 & 0xF) == 0 )
printf("0x%04X :", v9 + (unsigned int)v2);
if ( (v2 & 7) == 0 )
putchar(32);
printf("%02X", (unsigned __int8)buffer[v2]);
v7[(int)v2 % 16] = buffer[v2];
if ( (((_BYTE)v2 + 1) & 0xF) == 0 )
{
for ( i = 0LL; i != 16; ++i )
{
if ( (i & 7) == 0 )
putchar(32);
v4 = _ctype_b_loc();
v5 = v7[i];
if ( ((*v4)[v5] & 0x4000) != 0 )
putchar(v5);
else
putchar(46);
}
putchar(10);
}
++v2;
}
while ( (int)v8 > (int)v2 );
}
return resetRun();
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, _QWORD *a2)
{
__int64 v2; // rbp
__int64 i; // rbx
const unsigned __int16 **v4; // rax
char v5; // di
char v7[24]; // [rsp+0h] [rbp-38h]
unsigned int v8; // [rsp+18h] [rbp-20h] BYREF
unsigned int v9; // [rsp+1Ch] [rbp-1Ch] BYREF

v9 = 0;
v8 = 0x8000;
func2();
if ( a1 > 0 )
_isoc99_sscanf(*a2, "%d", &v9);
if ( a1 > 1 )
_isoc99_sscanf(a2[1], "%d", &v8);
if ( (int)v9 > 0x7FFF )
v9 = 0x7FFF;
if ( (int)(v8 + v9) > 0x7FFF )
v8 = 0x8000 - v9;
printf("hexdump, display %d bytes from 0x%04X\n", v8, v9);
cmdRead(v9, buffer, v8);
if ( (int)v8 > 0 )
{
v2 = 0LL;
do
{
if ( (v2 & 0xF) == 0 )
printf("0x%04X :", v9 + (unsigned int)v2);
if ( (v2 & 7) == 0 )
putchar(32);
printf("%02X", (unsigned __int8)buffer[v2]);
v7[(int)v2 % 16] = buffer[v2];
if ( (((_BYTE)v2 + 1) & 0xF) == 0 )
{
for ( i = 0LL; i != 16; ++i )
{
if ( (i & 7) == 0 )
putchar(32);
v4 = _ctype_b_loc();
v5 = v7[i];
if ( ((*v4)[v5] & 0x4000) != 0 )
putchar(v5);
else
putchar(46);
}
putchar(10);
}
++v2;
}
while ( (int)v8 > (int)v2 );
}
return func4();
}

original_code:
void hexdump(int argc, char ** argv) {
  int address = 0;
  int len = 32*1024;
  int i,j;
  char line[16];
  resetProg();
  if(argc>0) sscanf(argv[0], "%d", &address);
  if(argc>1) sscanf(argv[1], "%d", &len);
  if(address>0x7FFF) address=0x7FFF;
  if((address+len)>0x7fff) len=0x7FFF-address+1;
  printf("hexdump, display %d bytes from 0x%04X\n", len, address);
  cmdRead(address, buffer, len);
  for(i=0;i<len;i++) {
    if(!(i%16)) printf("0x%04X :", (unsigned) address+i);
    if(!(i%8)) printf(" ");
    printf("%02X", (unsigned char)buffer[i]);
    line[i%16] = buffer[i];
    if(!((i+1)%16)) {
      for(j=0;j<16;j++) {
        if(!(j%8)) putchar(' ');
        if(((*__ctype_b_loc ())[(int) ((line[j]))] & (unsigned short int) _ISprint)) putchar(line[j]);
        else putchar('.');
      }
      putchar('\n');
    }
  }
  resetRun();
  return;
}

canonical_original_code:
void hexdump(int argc, char **argv) {
  int address = 0;
  int len = 32*1024;
  int i; int j;
  char line[16];
  resetProg();
  if(argc>0) sscanf(argv[0], "%d", &address);
  if(argc>1) sscanf(argv[1], "%d", &len);
  if(address>0x7FFF) address=0x7FFF;
  if((address+len)>0x7fff) len=0x7FFF-address+1;
  printf("hexdump, display %d bytes from 0x%04X\n", len, address);
  cmdRead(address, buffer, len);
  for(i=0;i<len;i++) {
    if(!(i%16)) printf("0x%04X :", (unsigned int) address+i);
    if(!(i%8)) printf(" ");
    printf("%02X", (unsigned char)buffer[i]);
    line[i%16] = buffer[i];
    if(!((i+1)%16)) {
      for(j=0;j<16;j++) {
        if(!(j%8)) putchar(' ');
        if(((*__ctype_b_loc ())[(int) ((line[j]))] & (unsigned short) _ISprint)) putchar(line[j]);
        else putchar('.');
      }
      putchar('\n');
    }
  }
  resetRun();
  return;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: address int

variable_types:
line
char [16]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v4; // cf
bool v5; // zf
const char *v7; // rsi
const char *v8; // rdi
__int64 v9; // rcx

v4 = argc == 0;
v5 = argc == 1;
if ( argc <= 1 )
goto LABEL_6;
v7 = argv[1];
v8 = "help";
v9 = 5LL;
do
{
if ( !v9 )
break;
v4 = *v7 < (unsigned int)*v8;
v5 = *v7++ == *v8++;
--v9;
}
while ( v5 );
if ( (!v4 && !v5) == v4 )
{
LABEL_6:
help(*argv);
}
else
{
if ( (int)spiInit(v8, v7, envp) < 0 )
exit(1);
puts("Ftdi dongle found");
if ( !strcmp(argv[1], "hexdump") )
hexdump(argc - 2, argv + 2);
if ( !strcmp(argv[1], "read") )
nrfread(argc - 2, argv + 2);
if ( !strcmp(argv[1], "write") )
nrfwrite(argc - 2, argv + 2);
if ( !strcmp(argv[1], "reset") )
resetRun();
if ( !strcmp(argv[1], "holdreset") )
holdReset();
spiDeinit();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v4; // cf
bool v5; // zf
const char *v7; // rsi
const char *v8; // rdi
__int64 v9; // rcx

v4 = argc == 0;
v5 = argc == 1;
if ( argc <= 1 )
goto LABEL_6;
v7 = argv[1];
v8 = "help";
v9 = 5LL;
do
{
if ( !v9 )
break;
v4 = *v7 < (unsigned int)*v8;
v5 = *v7++ == *v8++;
--v9;
}
while ( v5 );
if ( (!v4 && !v5) == v4 )
{
LABEL_6:
func5(*argv);
}
else
{
if ( (int)spiInit(v8, v7, envp) < 0 )
exit(1);
puts("Ftdi dongle found");
if ( !strcmp(argv[1], "hexdump") )
func6(argc - 2, argv + 2);
if ( !strcmp(argv[1], "read") )
func7(argc - 2, argv + 2);
if ( !strcmp(argv[1], "write") )
func8(argc - 2, argv + 2);
if ( !strcmp(argv[1], "reset") )
func4();
if ( !strcmp(argv[1], "holdreset") )
func0();
spiDeinit();
}
return 0;
}

original_code:
int main (int argc, char *argv[])
{
  if(argc<2 || !strcmp(argv[1], "help"))
  {
    help(argv[0]);
    return 0;
  }
  if(spiInit()<0)
    exit(1);
  printf("Ftdi dongle found\n");
  if (!strcmp(argv[1], "hexdump"))
    hexdump(argc-2, argv+2);
  if (!strcmp(argv[1], "read"))
    nrfread(argc-2, argv+2);
  if (!strcmp(argv[1], "write"))
    nrfwrite(argc-2, argv+2);
  if (!strcmp(argv[1], "reset"))
    resetRun();
  if (!strcmp(argv[1], "holdreset"))
    holdReset();
  spiDeinit();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  if(argc<2 || !strcmp(argv[1], "help"))
  {
    help(argv[0]);
    return 0;
  }
  if(spiInit()<0)
    exit(1);
  printf("Ftdi dongle found\n");
  if (!strcmp(argv[1], "hexdump"))
    hexdump(argc-2, argv+2);
  if (!strcmp(argv[1], "read"))
    nrfread(argc-2, argv+2);
  if (!strcmp(argv[1], "write"))
    nrfwrite(argc-2, argv+2);
  if (!strcmp(argv[1], "reset"))
    resetRun();
  if (!strcmp(argv[1], "holdreset"))
    holdReset();
  spiDeinit();
  return 0;
}

variable_types:
argv
char *[]


ftdiSendBuffer

decompiled_code:
__int64 __fastcall ftdiSendBuffer(__int64 a1, unsigned int a2)
{
int v2; // er12
unsigned int v3; // ebp
int v4; // ebx
const char *v5; // rax

v2 = 11;
v3 = 0;
while ( (int)v3 < (int)a2 )
{
if ( !--v2 )
break;
v4 = ftdi_write_data(&ftdic, a1 + (int)v3, a2);
v3 += v4;
if ( v4 < 0 )
{
v5 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", (unsigned int)v4, v5);
return (unsigned int)-1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
int v2; // er12
unsigned int v3; // ebp
int v4; // ebx
const char *v5; // rax

v2 = 11;
v3 = 0;
while ( (int)v3 < (int)a2 )
{
if ( !--v2 )
break;
v4 = ftdi_write_data(&ftdic, a1 + (int)v3, a2);
v3 += v4;
if ( v4 < 0 )
{
v5 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", (unsigned int)v4, v5);
return (unsigned int)-1;
}
}
return v3;
}

original_code:
int ftdiSendBuffer(char *buff, int len)
{
  int i=0, ret, try=0;
  while((i<len) && ((try++)<10)) {
    ret = ftdi_write_data(&ftdic, (unsigned char*)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

canonical_original_code:
int ftdiSendBuffer(char *buff, int len)
{
  int i = 0; int ret; int try = 0;
  while((i<len) && ((try++)<10)) {
    ret = ftdi_write_data(&ftdic, (unsigned char *)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to send to the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

variable_types:
buff
char *

variable_types: i int

variable_types: ret int

variable_types: try int


ftdiRecvBuffer

decompiled_code:
__int64 __fastcall ftdiRecvBuffer(__int64 a1, unsigned int a2)
{
int v2; // er12
unsigned int v3; // ebp
int v4; // ebx
const char *v5; // rax

v2 = 101;
v3 = 0;
while ( (int)v3 < (int)a2 )
{
if ( !--v2 )
break;
v4 = ftdi_read_data(&ftdic, a1 + (int)v3, a2);
v3 += v4;
if ( v4 < 0 )
{
v5 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", (unsigned int)v4, v5);
return (unsigned int)-1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
int v2; // er12
unsigned int v3; // ebp
int v4; // ebx
const char *v5; // rax

v2 = 101;
v3 = 0;
while ( (int)v3 < (int)a2 )
{
if ( !--v2 )
break;
v4 = ftdi_read_data(&ftdic, a1 + (int)v3, a2);
v3 += v4;
if ( v4 < 0 )
{
v5 = (const char *)ftdi_get_error_string(&ftdic);
fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", (unsigned int)v4, v5);
return (unsigned int)-1;
}
}
return v3;
}

original_code:
int ftdiRecvBuffer(char* buff, int len)
{
  int i=0, ret, try=0;
  while((i<len) && ((try++)<100)) {
    ret = ftdi_read_data(&ftdic, (unsigned char*)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

canonical_original_code:
int ftdiRecvBuffer(char *buff, int len)
{
  int i = 0; int ret; int try = 0;
  while((i<len) && ((try++)<100)) {
    ret = ftdi_read_data(&ftdic, (unsigned char *)buff+i, len);
    i+=ret;
    if (ret<0) {
        fprintf(stderr, "Unable to receive from the FTDI chip (%d): %s\n", ret,
                ftdi_get_error_string(&ftdic));
        return -1;
    }
  }
  return i;
}

variable_types: len int