decompiled_code:
__int64 __fastcall mem_pool_fill_free_list(unsigned __int64 a1, __int64 a2)
{
unsigned __int64 v2; // rbp
__int64 v3; // r13
__int64 result; // rax
_QWORD *v5; // r14
__int64 v6; // rbp
__int64 *v7; // r12
__int64 *v8; // rbx
__int64 v9; // [rsp+0h] [rbp-40h]

if ( a1 > 0x3E )
return 0LL;
v2 = a1 + 1;
v3 = 16 * (a1 + 5);
if ( !list_empty((_QWORD *)(a2 + v3)) )
goto LABEL_5;
if ( (int)list_count((_QWORD **)(a2 + v3)) != *(_QWORD *)(a2 + 8 * v2 + 1088) )
_assert_fail(
"list_count(&pool->free_list[n + 1]) == pool->count[n + 1]",
"mempool.c",
0x6Bu,
"mem_pool_fill_free_list");
result = mem_pool_fill_free_list(v2, a2);
if ( (_DWORD)result )
{
LABEL_5:
v5 = *(_QWORD **)(16 * v2 + a2 + 64);
list_del_init((__int64)v5);
v6 = a2 + 8 * a1;
--*(_QWORD *)(v6 + 1096);
v9 = ut_2_exp(a1);
v7 = (_QWORD *)((char *)v5 + v9 - 8);
ut_memset(v7, 24LL);
mem_area_set_size(v7, v9);
mem_area_set_free((unsigned __int64 *)v7, 1);
v8 = (__int64 *)(a2 + v3 - 16);
list_add((_QWORD *)((char *)v5 + v9), v8);
++*(_QWORD *)(v6 + 1088);
mem_area_set_size(v5 - 1, v9);
list_add(v5, v8);
++*(_QWORD *)(v6 + 1088);
result = 1LL;
}
return result;
}

original_code:
static int mem_pool_fill_free_list(ulong n, mem_pool_t* pool){
 mem_area_t* area;
 mem_area_t* area2;
 mem_area_t* tmp;
 int ret;
 if(n >= 64 - 1)
  return 0;
 if (list_empty(&pool->free_list[n + 1])) {
  ((void) sizeof ((list_count(&pool->free_list[n + 1]) == pool->count[n + 1]) ? 1 : 0), __extension__ ({ if (list_count(&pool->free_list[n + 1]) == pool->count[n + 1]) ; else __assert_fail ("list_count(&pool->free_list[n + 1]) == pool->count[n + 1]", "mempool.c", 107, __extension__ __PRETTY_FUNCTION__); }));
  if( pool->count[n + 1] > 0 ){
  }
  ret = mem_pool_fill_free_list(n + 1, pool);
  if(ret == 0)
   return 0;
 }
 area = ((mem_area_t *)((char *)(pool->free_list[n + 1].next)-(unsigned long)((&((mem_area_t *)0)->free_list))));
 if ( pool->count[n + 1] == 0){
 }
 list_del_init(&area->free_list);
 pool->count[n + 1]--;
 area2 = (mem_area_t*)((byte*)area + ut_2_exp(n));
 ut_memset((void*)area2, sizeof(struct mem_area_struct));
 mem_area_set_size(area2, ut_2_exp(n));
 mem_area_set_free(area2, 1);
 list_add(&area2->free_list, &pool->free_list[n]);
 pool->count[n]++;
 mem_area_set_size(area, ut_2_exp(n));
 list_add(&area->free_list, &pool->free_list[n]);
 pool->count[n]++;
 return 1;
}

canonical_original_code:
static int mem_pool_fill_free_list(unsigned long n, struct mem_pool_struct *pool){
 struct mem_area_struct *area;
 struct mem_area_struct *area2;
 struct mem_area_struct *tmp;
 int ret;
 if(n >= 64 - 1)
  return 0;
 if (list_empty(&pool->free_list[n + 1])) {
  ((void) sizeof ((list_count(&pool->free_list[n + 1]) == pool->count[n + 1]) ? 1 : 0), __extension__ ({ if (list_count(&pool->free_list[n + 1]) == pool->count[n + 1]) ; else __assert_fail ("list_count(&pool->free_list[n + 1]) == pool->count[n + 1]", "mempool.c", 107, __extension__ __PRETTY_FUNCTION__); }));
  if( pool->count[n + 1] > 0 ){
  }
  ret = mem_pool_fill_free_list(n + 1, pool);
  if(ret == 0)
   return 0;
 }
 area = ((struct mem_area_struct *)((char *)(pool->free_list[n + 1].next)-(unsigned long)((&((struct mem_area_struct *)0)->free_list))));
 if ( pool->count[n + 1] == 0){
 }
 list_del_init(&area->free_list);
 pool->count[n + 1]--;
 area2 = (struct mem_area_struct *)((unsigned char *)area + ut_2_exp(n));
 ut_memset((void *)area2, sizeof(struct mem_area_struct));
 mem_area_set_size(area2, ut_2_exp(n));
 mem_area_set_free(area2, 1);
 list_add(&area2->free_list, &pool->free_list[n]);
 pool->count[n]++;
 mem_area_set_size(area, ut_2_exp(n));
 list_add(&area->free_list, &pool->free_list[n]);
 pool->count[n]++;
 return 1;
}

variable_types: struct mem_pool_struct { unsigned char *buf; unsigned long size; unsigned long reserved; struct mutex_struct { 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; } os_mutex; } mutex; struct list_head { struct list_head *next; } free_list[64]; unsigned long count[64]; } *

variable_types: ret int


repo: chaozh/c-tools

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

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

original_code:
void mem_pool_free(mem_pool_t *pool){
 ut_free(pool->buf);
 ut_free(pool);
}

canonical_original_code:
void mem_pool_free(struct mem_pool_struct *pool){
 ut_free(pool->buf);
 ut_free(pool);
}

return_type: void

user_defined_types: struct mem_pool_struct { unsigned char *buf; unsigned long size; unsigned long reserved; struct mutex_struct mutex; struct list_head free_list[64]; unsigned long count[64]; }

user_defined_types: struct mutex_struct { union pthread_mutex_t os_mutex; }

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

user_defined_types: struct list_head { struct list_head *next; }

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

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall mem_area_alloc(__int64 *a1, __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rax
__int64 v4; // rbp
__int64 v5; // r12
__int64 v6; // r12
unsigned __int64 v8; // [rsp+0h] [rbp-48h]
__int64 v9; // [rsp+8h] [rbp-40h]

v2 = *a1;
v3 = ut_max(*a1 + 24, 0x30uLL);
v4 = ut_2_log(v3);
mutex_enter(a2 + 24);
if ( !list_empty((_QWORD *)(a2 + 16 * (v4 + 4))) || (unsigned int)mem_pool_fill_free_list(v4, a2) )
{
v5 = *(_QWORD *)(16 * v4 + a2 + 64);
v8 = mem_area_get_size((_QWORD *)(v5 - 8));
v9 = ut_2_exp(v4);
if ( v8 != v9 )
_assert_fail("mem_area_get_size(area) == ut_2_exp(n)", "mempool.c", 0xAAu, "mem_area_alloc");
mem_area_set_free((unsigned __int64 *)(v5 - 8), 0);
list_del_init(v5);
--*(_QWORD *)(a2 + 8 * v4 + 1088);
*(_QWORD *)(a2 + 16) += mem_area_get_size((_QWORD *)(v5 - 8));
mutex_exit(a2 + 24);
*a1 = v9 - 24;
v6 = v5 + 16;
ut_memset(v6, v9 - 24);
}
else
{
mutex_exit(a2 + 24);
v6 = ut_malloc(v2);
}
return v6;
}

original_code:
void* mem_area_alloc(ulong* psize, mem_pool_t *pool){
 mem_area_t* area;
 ulong size, n;
 int ret;
 size = *psize;
 n = ut_2_log(ut_max(size + sizeof(struct mem_area_struct), 2 * sizeof(struct mem_area_struct)));
 mutex_enter(&pool->mutex);
 if(list_empty(&pool->free_list[n])){
  ret = mem_pool_fill_free_list(n, pool);
  if(ret == 0){
   mutex_exit(&pool->mutex);
   return ut_malloc(size);
  }
 }
 area = ((mem_area_t *)((char *)(pool->free_list[n].next)-(unsigned long)((&((mem_area_t *)0)->free_list))));
 if(!mem_area_get_free(area)){
 }
 ((void) sizeof ((mem_area_get_size(area) == ut_2_exp(n)) ? 1 : 0), __extension__ ({ if (mem_area_get_size(area) == ut_2_exp(n)) ; else __assert_fail ("mem_area_get_size(area) == ut_2_exp(n)", "mempool.c", 170, __extension__ __PRETTY_FUNCTION__); }));
 mem_area_set_free(area,0);
 list_del_init(&area->free_list);
 pool->count[n]--;
 pool->reserved += mem_area_get_size(area);
 mutex_exit(&pool->mutex);
 *psize = ut_2_exp(n) - sizeof(struct mem_area_struct);
 ut_memset((sizeof(struct mem_area_struct) + (byte*)area), *psize);
 return (void*)(sizeof(struct mem_area_struct) + (byte*)area);
}

canonical_original_code:
void *mem_area_alloc(unsigned long *psize, struct mem_pool_struct *pool){
 struct mem_area_struct *area;
 unsigned long size; unsigned long n;
 int ret;
 size = *psize;
 n = ut_2_log(ut_max(size + sizeof(struct mem_area_struct), 2 * sizeof(struct mem_area_struct)));
 mutex_enter(&pool->mutex);
 if(list_empty(&pool->free_list[n])){
  ret = mem_pool_fill_free_list(n, pool);
  if(ret == 0){
   mutex_exit(&pool->mutex);
   return ut_malloc(size);
  }
 }
 area = ((struct mem_area_struct *)((char *)(pool->free_list[n].next)-(unsigned long)((&((struct mem_area_struct *)0)->free_list))));
 if(!mem_area_get_free(area)){
 }
 ((void) sizeof ((mem_area_get_size(area) == ut_2_exp(n)) ? 1 : 0), __extension__ ({ if (mem_area_get_size(area) == ut_2_exp(n)) ; else __assert_fail ("mem_area_get_size(area) == ut_2_exp(n)", "mempool.c", 170, __extension__ __PRETTY_FUNCTION__); }));
 mem_area_set_free(area,0);
 list_del_init(&area->free_list);
 pool->count[n]--;
 pool->reserved += mem_area_get_size(area);
 mutex_exit(&pool->mutex);
 *psize = ut_2_exp(n) - sizeof(struct mem_area_struct);
 ut_memset((sizeof(struct mem_area_struct) + (unsigned char *)area), *psize);
 return (void *)(sizeof(struct mem_area_struct) + (unsigned char *)area);
}

variable_types:
psize
unsigned long *

variable_types: struct mem_area_struct { unsigned long size_and_free; struct list_head { struct list_head *next; } free_list; } *

variable_types:
size
unsigned long

variable_types:
n
unsigned long

return_type: void *

user_defined_types: struct mem_area_struct { unsigned long size_and_free; struct list_head free_list; }


decompiled_code:
__int64 __fastcall mem_area_free(__int64 *a1, _QWORD *a2)
{
__int64 *v2; // rbp
__int64 result; // rax
__int64 *v4; // r14
unsigned __int64 v5; // r12
__int64 v6; // r13
unsigned __int64 v7; // rax
__int64 *v8; // [rsp+8h] [rbp-40h]

v2 = a1;
if ( *a2 > (unsigned __int64)a1 || (unsigned __int64)a1 >= a2[1] + *a2 )
return ut_free(a1);
v4 = a1 - 3;
v5 = mem_area_get_size(a1 - 3);
ut_memset(a1, v5 - 24);
v8 = (__int64 *)mem_area_get_buddy((__int64)(a1 - 3), v5, a2);
v6 = ut_2_log(v5);
mutex_enter(a2 + 3);
if ( v8 && (unsigned int)mem_area_get_free(v8) && mem_area_get_size(v8) == v5 )
{
if ( v4 <= v8 )
{
mem_area_set_size(v4, 2 * v5);
}
else
{
v2 = v8 + 3;
mem_area_set_size(v8, 2 * v5);
mem_area_set_free((unsigned __int64 *)v8, 0);
}
list_del_init((__int64)(v8 + 1));
--a2[v6 + 136];
a2[2] += ut_2_exp(v6);
mutex_exit(a2 + 3);
result = mem_area_free(v2, a2);
}
else
{
list_add(a1 - 2, &a2[2 * v6 + 8]);
++a2[v6 + 136];
mem_area_set_free((unsigned __int64 *)v4, 1);
v7 = a2[2];
if ( v7 < v5 )
_assert_fail("pool->reserved >= size", "mempool.c", 0xFAu, "mem_area_free");
a2[2] = v7 - v5;
result = mutex_exit(a2 + 3);
}
return result;
}

original_code:
void mem_area_free(void* ptr, mem_pool_t* pool){
 mem_area_t* area;
 mem_area_t* buddy;
 void* new_ptr;
 ulong size;
 ulong n;
 if( (byte*)ptr < pool->buf || (byte*)ptr >= pool->buf + pool->size ){
  ut_free(ptr);
  return;
 }
 area = (mem_area_t*)((byte*)ptr - sizeof(struct mem_area_struct));
 if(mem_area_get_free(area)){
 }
 size = mem_area_get_size(area);
 ut_memset(ptr, size - sizeof(struct mem_area_struct));
 if(size == 0){
 }
 buddy = mem_area_get_buddy(area, size, pool);
 n = ut_2_log(size);
 mutex_enter(&pool->mutex);
 if(buddy && mem_area_get_free(buddy) &&
  size == mem_area_get_size(buddy)){
  if((byte*)buddy < (byte*)area){
   new_ptr = ((byte*)buddy) + sizeof(struct mem_area_struct);
   mem_area_set_size(buddy, size * 2);
   mem_area_set_free(buddy, 0);
  }else {
   new_ptr = ptr;
   mem_area_set_size(area, size * 2);
  }
  list_del_init(&buddy->free_list);
  pool->count[n]--;
  pool->reserved += ut_2_exp(n);
  mutex_exit(&pool->mutex);
  mem_area_free(new_ptr, pool);
  return;
 }else{
  list_add(&area->free_list,&pool->free_list[n]);
  pool->count[n]++;
  mem_area_set_free(area, 1);
  ((void) sizeof ((pool->reserved >= size) ? 1 : 0), __extension__ ({ if (pool->reserved >= size) ; else __assert_fail ("pool->reserved >= size", "mempool.c", 250, __extension__ __PRETTY_FUNCTION__); }));
  pool->reserved -= size;
 }
 mutex_exit(&pool->mutex);
}

canonical_original_code:
void mem_area_free(void *ptr, struct mem_pool_struct *pool){
 struct mem_area_struct *area;
 struct mem_area_struct *buddy;
 void *new_ptr;
 unsigned long size;
 unsigned long n;
 if( (unsigned char *)ptr < pool->buf || (unsigned char *)ptr >= pool->buf + pool->size ){
  ut_free(ptr);
  return;
 }
 area = (struct mem_area_struct *)((unsigned char *)ptr - sizeof(struct mem_area_struct));
 if(mem_area_get_free(area)){
 }
 size = mem_area_get_size(area);
 ut_memset(ptr, size - sizeof(struct mem_area_struct));
 if(size == 0){
 }
 buddy = mem_area_get_buddy(area, size, pool);
 n = ut_2_log(size);
 mutex_enter(&pool->mutex);
 if(buddy && mem_area_get_free(buddy) &&
  size == mem_area_get_size(buddy)){
  if((unsigned char *)buddy < (unsigned char *)area){
   new_ptr = ((unsigned char *)buddy) + sizeof(struct mem_area_struct);
   mem_area_set_size(buddy, size * 2);
   mem_area_set_free(buddy, 0);
  }else {
   new_ptr = ptr;
   mem_area_set_size(area, size * 2);
  }
  list_del_init(&buddy->free_list);
  pool->count[n]--;
  pool->reserved += ut_2_exp(n);
  mutex_exit(&pool->mutex);
  mem_area_free(new_ptr, pool);
  return;
 }else{
  list_add(&area->free_list,&pool->free_list[n]);
  pool->count[n]++;
  mem_area_set_free(area, 1);
  ((void) sizeof ((pool->reserved >= size) ? 1 : 0), __extension__ ({ if (pool->reserved >= size) ; else __assert_fail ("pool->reserved >= size", "mempool.c", 250, __extension__ __PRETTY_FUNCTION__); }));
  pool->reserved -= size;
 }
 mutex_exit(&pool->mutex);
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall mem_pool_get_reserved(__int64 a1)
{
__int64 v1; // rbx

mutex_enter(a1 + 24);
v1 = *(_QWORD *)(a1 + 16);
mutex_exit(a1 + 24);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1)
{
__int64 v1; // rbx

mutex_enter(a1 + 24);
v1 = *(_QWORD *)(a1 + 16);
mutex_exit(a1 + 24);
return v1;
}

original_code:
ulong mem_pool_get_reserved(mem_pool_t* pool){
 ulong reserved;
 mutex_enter(&pool->mutex);
 reserved = pool->reserved;
 mutex_exit(&pool->mutex);
 return reserved;
}

canonical_original_code:
unsigned long mem_pool_get_reserved(struct mem_pool_struct *pool){
 unsigned long reserved;
 mutex_enter(&pool->mutex);
 reserved = pool->reserved;
 mutex_exit(&pool->mutex);
 return reserved;
}

variable_types:
reserved
unsigned long

return_type: unsigned long


original_code:
void mem_pool_print_info(mem_pool_t* pool){
 mem_area_t* area;
 int i;
 mem_pool_validate(pool);
 printf("------------------------------------------------\n");
 mutex_enter(&pool->mutex);
 printf("Mempool:total size %lu MB; used size %lu B; \n",pool->size / 1048576, pool->reserved );
 for(i = 0; i < 64; i++){
  if( pool->count[i] > 0 ){
   area = ((mem_area_t *)((char *)(pool->free_list[i].next)-(unsigned long)((&((mem_area_t *)0)->free_list))));
   printf("Memarea %d:area size %lu/%lu B; free count %lu; ", i, ut_2_exp(i), mem_area_get_size(area), pool->count[i] );
   printf("\n");
  }
 }
 mutex_exit(&pool->mutex);
 printf("------------------------------------------------\n");
}

canonical_original_code:
void mem_pool_print_info(struct mem_pool_struct *pool){
 struct mem_area_struct *area;
 int i;
 mem_pool_validate(pool);
 printf("------------------------------------------------\n");
 mutex_enter(&pool->mutex);
 printf("Mempool:total size %lu MB; used size %lu B; \n",pool->size / 1048576, pool->reserved );
 for(i = 0; i < 64; i++){
  if( pool->count[i] > 0 ){
   area = ((struct mem_area_struct *)((char *)(pool->free_list[i].next)-(unsigned long)((&((struct mem_area_struct *)0)->free_list))));
   printf("Memarea %d:area size %lu/%lu B; free count %lu; ", i, ut_2_exp(i), mem_area_get_size(area), pool->count[i] );
   printf("\n");
  }
 }
 mutex_exit(&pool->mutex);
 printf("------------------------------------------------\n");
}

variable_types: i int


decompiled_code:
int __fastcall ut_usectime(struct timeval *a1)
{
int result; // eax
struct timeval v2; // [rsp+0h] [rbp-28h] BYREF

result = gettimeofday(&v2, 0LL);
if ( result != -1 )
*a1 = v2;
return result;
}

canonical_decompiled_code:
int __fastcall func0(struct timeval *a1)
{
int result; // eax
struct timeval v2; // [rsp+0h] [rbp-28h] BYREF

result = gettimeofday(&v2, 0LL);
if ( result != -1 )
*a1 = v2;
return result;
}

original_code:
int ut_usectime(ulong* sec, ulong* ms){
 struct timeval tv;
 int ret;
 ret = gettimeofday(&tv, ((void *)0));
 if(ret != -1){
  *sec = (ulong)tv.tv_sec;
  *ms = (ulong)tv.tv_usec;
 }
 return ret;
}

canonical_original_code:
int ut_usectime(unsigned long *sec, unsigned long *ms){
 struct timeval tv;
 int ret;
 ret = gettimeofday(&tv, ((void *)0));
 if(ret != -1){
  *sec = (unsigned long)tv.tv_sec;
  *ms = (unsigned long)tv.tv_usec;
 }
 return ret;
}

variable_types:
sec
unsigned long *

variable_types:
ms
unsigned long *

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

return_type: int


decompiled_code:
int __fastcall mutex_create(pthread_mutex_t *a1)
{
return pthread_mutex_init(a1, 0LL);
}

canonical_decompiled_code:
int __fastcall func1(pthread_mutex_t *a1)
{
return pthread_mutex_init(a1, 0LL);
}

original_code:
void mutex_create(
 mutex_t* mutex
 )
{
 pthread_mutex_init(&mutex->os_mutex,((void *)0));
}

canonical_original_code:
void mutex_create(struct mutex_struct *mutex)
{
 pthread_mutex_init(&mutex->os_mutex,((void *)0));
}

variable_types: struct mutex_struct { 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; } os_mutex; } *


decompiled_code:
int __fastcall mutex_destroy(pthread_mutex_t *a1)
{
return pthread_mutex_destroy(a1);
}

canonical_decompiled_code:
int __fastcall func2(pthread_mutex_t *a1)
{
return pthread_mutex_destroy(a1);
}

original_code:
void mutex_destroy(mutex_t* mutex){
 pthread_mutex_destroy(&mutex->os_mutex);
}

canonical_original_code:
void mutex_destroy(struct mutex_struct *mutex){
 pthread_mutex_destroy(&mutex->os_mutex);
}


decompiled_code:
int __fastcall mutex_enter(pthread_mutex_t *mutex)
{
int result; // eax

result = pthread_mutex_trylock(mutex);
if ( result )
result = pthread_mutex_lock(mutex);
return result;
}

canonical_decompiled_code:
int __fastcall func3(pthread_mutex_t *mutex)
{
int result; // eax

result = pthread_mutex_trylock(mutex);
if ( result )
result = pthread_mutex_lock(mutex);
return result;
}

original_code:
void mutex_enter(mutex_t* mutex){
 int ret;
 ;
 ret = pthread_mutex_trylock(&mutex->os_mutex);
 if(ret == 0){
  return;
 }else{
  pthread_mutex_lock(&mutex->os_mutex);
 }
}

canonical_original_code:
void mutex_enter(struct mutex_struct *mutex){
 int ret;
 ;
 ret = pthread_mutex_trylock(&mutex->os_mutex);
 if(ret == 0){
  return;
 }else{
  pthread_mutex_lock(&mutex->os_mutex);
 }
}


decompiled_code:
int __fastcall mutex_exit(pthread_mutex_t *a1)
{
return pthread_mutex_unlock(a1);
}

canonical_decompiled_code:
int __fastcall func4(pthread_mutex_t *a1)
{
return pthread_mutex_unlock(a1);
}

original_code:
void mutex_exit(mutex_t* mutex){
 pthread_mutex_unlock(&mutex->os_mutex);
}

canonical_original_code:
void mutex_exit(struct mutex_struct *mutex){
 pthread_mutex_unlock(&mutex->os_mutex);
}


decompiled_code:
void *__fastcall ut_memset(void *a1, size_t a2)
{
return memset(a1, 0, a2);
}

canonical_decompiled_code:
void *__fastcall func5(void *a1, size_t a2)
{
return memset(a1, 0, a2);
}

original_code:
void ut_memset(byte* ptr, ulong n){
 memset(ptr, '\0', n);
}

canonical_original_code:
void ut_memset(unsigned char *ptr, unsigned long n){
 memset(ptr, '\0', n);
}

variable_types:
ptr
unsigned char *


decompiled_code:
void *__fastcall ut_malloc_low(size_t n, int a2)
{
void *v2; // rax
void *v3; // rbx

v2 = malloc(n);
if ( !v2 )
_assert_fail("ret", "util_os.c", 0x56u, "ut_malloc_low");
v3 = v2;
if ( a2 )
memset(v2, 0, n);
return v3;
}

canonical_decompiled_code:
void *__fastcall func6(size_t n, int a2)
{
void *v2; // rax
void *v3; // rbx

v2 = malloc(n);
if ( !v2 )
_assert_fail("ret", "util_os.c", 0x56u, "ut_malloc_low");
v3 = v2;
if ( a2 )
memset(v2, 0, n);
return v3;
}

original_code:
void* ut_malloc_low(ulong n, int set_to_zero){
 void* ret;
 ret = malloc(n);
 ((void) sizeof ((ret) ? 1 : 0), __extension__ ({ if (ret) ; else __assert_fail ("ret", "util_os.c", 86, __extension__ __PRETTY_FUNCTION__); }));
 if(set_to_zero)
  memset(ret, '\0', n);
 return ret;
}

canonical_original_code:
void *ut_malloc_low(unsigned long n, int set_to_zero){
 void *ret;
 ret = malloc(n);
 ((void) sizeof ((ret) ? 1 : 0), __extension__ ({ if (ret) ; else __assert_fail ("ret", "util_os.c", 86, __extension__ __PRETTY_FUNCTION__); }));
 if(set_to_zero)
  memset(ret, '\0', n);
 return ret;
}

variable_types:
ret
void *


decompiled_code:
void *__fastcall ut_malloc(size_t a1)
{
return ut_malloc_low(a1, 0);
}

canonical_decompiled_code:
void *__fastcall func7(size_t a1)
{
return func6(a1, 0);
}

original_code:
void* ut_malloc(ulong n){
 return ut_malloc_low(n, 0);
}

canonical_original_code:
void *ut_malloc(unsigned long n){
 return ut_malloc_low(n, 0);
}


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

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

original_code:
void ut_free(void *ptr){
 if(ptr == ((void *)0))
  return;
 else
     free(ptr);
  return;
}


repo: chapmajs/vtserver

decompiled_code:
__int64 __fastcall get_command(void *buf)
{
unsigned int v1; // er12
unsigned int *v3; // rbp
unsigned int i; // ebx
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
char v8; // al
char v9; // r12
char v10; // bp
char *v11; // rbx
char v12[47]; // [rsp+0h] [rbp-58h] BYREF
char bufa[41]; // [rsp+2Fh] [rbp-29h] BYREF

read(portfd, buf, 1uLL);
v1 = havesentbootcode;
if ( !havesentbootcode && *(_BYTE *)buf == 64 )
{
v3 = (unsigned int *)&bootcode;
for ( i = 49152; i != 49230; i += 2 )
{
sprintf(v12, "%06o/", i);
v5 = strlen(v12);
write(portfd, v12, v5);
do
read(portfd, bufa, 1uLL);
while ( bufa[0] != 32 );
sprintf(v12, "%06o\r", *v3);
v6 = strlen(v12);
write(portfd, v12, v6);
do
read(portfd, bufa, 1uLL);
while ( bufa[0] != 64 );
++v3;
}
sprintf(v12, "%06oG", 49152LL);
v7 = strlen(v12);
write(portfd, v12, v7);
do
read(portfd, bufa, 1uLL);
while ( bufa[0] != 71 );
havesentbootcode = 1;
}
else if ( *(_BYTE *)buf == 31 )
{
read(portfd, (char *)buf + 1, 1uLL);
if ( *((_BYTE *)buf + 1) == 42 )
{
read(portfd, (char *)buf + 2, 1uLL);
read(portfd, (char *)buf + 3, 1uLL);
read(portfd, (char *)buf + 4, 1uLL);
read(portfd, (char *)buf + 5, 1uLL);
v8 = *((_BYTE *)buf + 2);
v1 = 1;
if ( v8 )
{
v9 = *((_BYTE *)buf + 4) ^ v8 ^ 0x1F;
v10 = *((_BYTE *)buf + 5) ^ *((_BYTE *)buf + 3) ^ 0x2A;
if ( v8 == 4 )
{
v11 = &inbuf;
do
{
read(portfd, v11, 1uLL);
v9 ^= *v11;
read(portfd, v11 + 1, 1uLL);
v10 ^= v11[1];
v11 += 2;
}
while ( v11 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
}
read(portfd, (char *)buf + 6, 1uLL);
read(portfd, (char *)buf + 7, 1uLL);
if ( *((_BYTE *)buf + 6) != v9 || (v1 = 1, *((_BYTE *)buf + 7) != v10) )
{
fputc(101, stderr);
v1 = 0;
}
}
}
else
{
write(1, (char *)buf + 1, 1uLL);
v1 = 0;
}
}
else
{
write(1, buf, 1uLL);
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *buf)
{
unsigned int v1; // er12
unsigned int *v3; // rbp
unsigned int i; // ebx
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
char v8; // al
char v9; // r12
char v10; // bp
char *v11; // rbx
char v12[47]; // [rsp+0h] [rbp-58h] BYREF
char bufa[41]; // [rsp+2Fh] [rbp-29h] BYREF

read(portfd, buf, 1uLL);
v1 = havesentbootcode;
if ( !havesentbootcode && *(_BYTE *)buf == 64 )
{
v3 = (unsigned int *)&bootcode;
for ( i = 49152; i != 49230; i += 2 )
{
sprintf(v12, "%06o/", i);
v5 = strlen(v12);
write(portfd, v12, v5);
do
read(portfd, bufa, 1uLL);
while ( bufa[0] != 32 );
sprintf(v12, "%06o\r", *v3);
v6 = strlen(v12);
write(portfd, v12, v6);
do
read(portfd, bufa, 1uLL);
while ( bufa[0] != 64 );
++v3;
}
sprintf(v12, "%06oG", 49152LL);
v7 = strlen(v12);
write(portfd, v12, v7);
do
read(portfd, bufa, 1uLL);
while ( bufa[0] != 71 );
havesentbootcode = 1;
}
else if ( *(_BYTE *)buf == 31 )
{
read(portfd, (char *)buf + 1, 1uLL);
if ( *((_BYTE *)buf + 1) == 42 )
{
read(portfd, (char *)buf + 2, 1uLL);
read(portfd, (char *)buf + 3, 1uLL);
read(portfd, (char *)buf + 4, 1uLL);
read(portfd, (char *)buf + 5, 1uLL);
v8 = *((_BYTE *)buf + 2);
v1 = 1;
if ( v8 )
{
v9 = *((_BYTE *)buf + 4) ^ v8 ^ 0x1F;
v10 = *((_BYTE *)buf + 5) ^ *((_BYTE *)buf + 3) ^ 0x2A;
if ( v8 == 4 )
{
v11 = &inbuf;
do
{
read(portfd, v11, 1uLL);
v9 ^= *v11;
read(portfd, v11 + 1, 1uLL);
v10 ^= v11[1];
v11 += 2;
}
while ( v11 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
}
read(portfd, (char *)buf + 6, 1uLL);
read(portfd, (char *)buf + 7, 1uLL);
if ( *((_BYTE *)buf + 6) != v9 || (v1 = 1, *((_BYTE *)buf + 7) != v10) )
{
fputc(101, stderr);
v1 = 0;
}
}
}
else
{
write(1, (char *)buf + 1, 1uLL);
v1 = 0;
}
}
else
{
write(1, buf, 1uLL);
v1 = 0;
}
return v1;
}

original_code:
int get_command(struct vtcmd *v)
{
  int i,loc;
  unsigned char sum0, sum1;
  char ch,bootbuf[40];
  read(portfd, &v->hdr1, 1);
  if ((havesentbootcode==0) && (v->hdr1 == '@')) {
    for (i=0,loc=0140000;i<(sizeof(bootcode)/sizeof(int));i++,loc+=2) {
 sprintf(bootbuf, "%06o/", loc);
 write(portfd, bootbuf, strlen(bootbuf));
 while (1) { read(portfd, &ch, 1); if (ch==' ') break; }
 sprintf(bootbuf, "%06o\r", bootcode[i]);
 write(portfd, bootbuf, strlen(bootbuf));
 while (1) { read(portfd, &ch, 1); if (ch=='@') break; }
    }
    sprintf(bootbuf, "%06oG", 0140000);
    write(portfd, bootbuf, strlen(bootbuf));
    while (1) { read(portfd, &ch, 1); if (ch=='G') break; }
    havesentbootcode=1; return(0);
  }
  if (v->hdr1 != 31) { write(1,&v->hdr1, 1); return(0); }
  read(portfd, &v->hdr2, 1);
  if (v->hdr2 != 42) { write(1,&v->hdr2, 1); return(0); }
  read(portfd, &v->cmd, 1); read(portfd, &v->record, 1);
  read(portfd, &v->blklo, 1); read(portfd, &v->blkhi, 1);
  sum0 = 31 ^ v->cmd ^ v->blklo;
  sum1 = 42 ^ v->record ^ v->blkhi;
  if (v->cmd == 0) return(1);
  if (v->cmd == 4) {
    for (i = 0; i < 512; i++) {
      read(portfd, &inbuf[i], 1); sum0 ^= inbuf[i]; i++;
      read(portfd, &inbuf[i], 1); sum1 ^= inbuf[i];
    }
  }
  read(portfd, &(v->sum0), 1);
  read(portfd, &(v->sum1), 1);
  if ((sum0 != v->sum0) || (sum1 != v->sum1))
    { fputc('e',stderr); return(0); }
  return(1);
}

canonical_original_code:
int get_command(struct vtcmd *v)
{
  int i; int loc;
  unsigned char sum0; unsigned char sum1;
  char ch; char bootbuf[40];
  read(portfd, &v->hdr1, 1);
  if ((havesentbootcode==0) && (v->hdr1 == '@')) {
    for (i=0,loc=0140000;i<(sizeof(bootcode)/sizeof(int));i++,loc+=2) {
 sprintf(bootbuf, "%06o/", loc);
 write(portfd, bootbuf, strlen(bootbuf));
 while (1) { read(portfd, &ch, 1); if (ch==' ') break; }
 sprintf(bootbuf, "%06o\r", bootcode[i]);
 write(portfd, bootbuf, strlen(bootbuf));
 while (1) { read(portfd, &ch, 1); if (ch=='@') break; }
    }
    sprintf(bootbuf, "%06oG", 0140000);
    write(portfd, bootbuf, strlen(bootbuf));
    while (1) { read(portfd, &ch, 1); if (ch=='G') break; }
    havesentbootcode=1; return(0);
  }
  if (v->hdr1 != 31) { write(1,&v->hdr1, 1); return(0); }
  read(portfd, &v->hdr2, 1);
  if (v->hdr2 != 42) { write(1,&v->hdr2, 1); return(0); }
  read(portfd, &v->cmd, 1); read(portfd, &v->record, 1);
  read(portfd, &v->blklo, 1); read(portfd, &v->blkhi, 1);
  sum0 = 31 ^ v->cmd ^ v->blklo;
  sum1 = 42 ^ v->record ^ v->blkhi;
  if (v->cmd == 0) return(1);
  if (v->cmd == 4) {
    for (i = 0; i < 512; i++) {
      read(portfd, &inbuf[i], 1); sum0 ^= inbuf[i]; i++;
      read(portfd, &inbuf[i], 1); sum1 ^= inbuf[i];
    }
  }
  read(portfd, &(v->sum0), 1);
  read(portfd, &(v->sum1), 1);
  if ((sum0 != v->sum0) || (sum1 != v->sum1))
    { fputc('e',stderr); return(0); }
  return(1);
}

variable_types: struct vtcmd { unsigned char hdr1; unsigned char hdr2; unsigned char cmd; unsigned char record; unsigned char blklo; unsigned char blkhi; unsigned char sum0; unsigned char sum1; } *

variable_types: loc int

variable_types: ch char

variable_types:
bootbuf
char [40]


decompiled_code:
ssize_t send_reply()
{
char v0; // al
char v1; // cl
char v2; // dl
ssize_t result; // rax
char *v4; // rbx
char *v5; // rax
char *v6; // rbx

if ( BYTE2(vtcmd) )
{
v0 = BYTE2(vtreply);
v1 = BYTE4(vtreply) ^ v0 ^ 0x1F;
BYTE6(vtreply) = v1;
v2 = BYTE5(vtreply) ^ BYTE3(vtreply) ^ 0x2A;
HIBYTE(vtreply) = v2;
if ( v0 == 3 )
{
v5 = &inbuf;
do
{
v1 ^= *v5;
v2 ^= v5[1];
v5 += 2;
}
while ( v5 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
BYTE6(vtreply) = v1;
HIBYTE(vtreply) = v2;
}
write(portfd, &vtreply, 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 1), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 2), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 3), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 4), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 5), 1uLL);
if ( BYTE2(vtreply) == 3 )
{
v6 = &inbuf;
do
write(portfd, v6++, 1uLL);
while ( v6 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
}
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 6), 1uLL);
result = write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 7), 1uLL);
}
else
{
result = write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 2), 1uLL);
if ( !BYTE2(vtreply) )
{
v4 = &inbuf;
do
result = write(portfd, v4++, 1uLL);
while ( v4 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
}
}
return result;
}

canonical_decompiled_code:
ssize_t func1()
{
char v0; // al
char v1; // cl
char v2; // dl
ssize_t result; // rax
char *v4; // rbx
char *v5; // rax
char *v6; // rbx

if ( BYTE2(vtcmd) )
{
v0 = BYTE2(vtreply);
v1 = BYTE4(vtreply) ^ v0 ^ 0x1F;
BYTE6(vtreply) = v1;
v2 = BYTE5(vtreply) ^ BYTE3(vtreply) ^ 0x2A;
HIBYTE(vtreply) = v2;
if ( v0 == 3 )
{
v5 = &inbuf;
do
{
v1 ^= *v5;
v2 ^= v5[1];
v5 += 2;
}
while ( v5 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
BYTE6(vtreply) = v1;
HIBYTE(vtreply) = v2;
}
write(portfd, &vtreply, 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 1), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 2), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 3), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 4), 1uLL);
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 5), 1uLL);
if ( BYTE2(vtreply) == 3 )
{
v6 = &inbuf;
do
write(portfd, v6++, 1uLL);
while ( v6 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
}
write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 6), 1uLL);
result = write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 7), 1uLL);
}
else
{
result = write(portfd, (const void *)(unsigned int)((_DWORD)&vtreply + 2), 1uLL);
if ( !BYTE2(vtreply) )
{
v4 = &inbuf;
do
result = write(portfd, v4++, 1uLL);
while ( v4 != (char *)(unsigned int)((_DWORD)&inbuf + 512) );
}
}
return result;
}

original_code:
void send_reply()
{
  int i;
  if ((vtcmd.cmd)==0) {
    write(portfd, &vtreply.cmd, 1);
    if (vtreply.cmd!=0) return;
    for (i=0; i< 512; i++) write(portfd, &inbuf[i], 1);
    return;
  }
  vtreply.sum0 = 31 ^ vtreply.cmd ^ vtreply.blklo;
  vtreply.sum1 = 42 ^ vtreply.record ^ vtreply.blkhi;
  if (vtreply.cmd == 3) {
    for (i = 0; i < 512; i++) {
      vtreply.sum0 ^= inbuf[i]; i++;
      vtreply.sum1 ^= inbuf[i];
    }
  }
  write(portfd, &vtreply.hdr1, 1);
  write(portfd, &vtreply.hdr2, 1);
  write(portfd, &vtreply.cmd, 1);
  write(portfd, &vtreply.record, 1);
  write(portfd, &vtreply.blklo, 1);
  write(portfd, &vtreply.blkhi, 1);
  if (vtreply.cmd == 3) {
    for (i = 0; i < 512; i++) write(portfd, &inbuf[i], 1);
  }
  write(portfd, &vtreply.sum0, 1);
  write(portfd, &vtreply.sum1, 1);
}


decompiled_code:
int do_command()
{
int v0; // ebp
int v1; // ebx
const char *v2; // rdi
int v3; // er12
int result; // eax
int *v5; // rax
char *v6; // rax
__int64 i; // rax
int v8; // edx

vtreply = vtcmd;
v0 = BYTE3(vtcmd);
v1 = (BYTE4(vtcmd) + (BYTE5(vtcmd) << 8)) << 9;
if ( lastrec != v0 )
{
v2 = (const char *)recname[BYTE3(vtcmd)];
if ( !v2 )
{
result = fprintf(stderr, "No such tape record %d\r\n", BYTE3(vtcmd));
BYTE2(vtreply) |= 0x10u;
return result;
}
v3 = open(v2, 2);
if ( v3 >= 0 )
{
fprintf(stderr, "\nOpened %s read-write\r\n ", (const char *)recname[v0]);
}
else
{
v3 = open((const char *)recname[v0], 0);
if ( v3 >= 0 )
{
fprintf(stderr, "\nOpened %s read-only\r\n ", (const char *)recname[v0]);
}
else
{
v3 = open((const char *)recname[v0], 578, 384LL);
if ( v3 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
result = fprintf(stderr, "Cannot open %s: %s\r\n", (const char *)recname[v0], v6);
BYTE2(vtreply) |= 0x10u;
return result;
}
fprintf(stderr, "\nOpened %s as a new file\r\n ", (const char *)recname[v0]);
}
}
if ( lastrec != v0 )
close(recfd);
recfd = v3;
lastrec = v0;
}
switch ( BYTE2(vtcmd) )
{
case 0:
BYTE2(vtreply) = 0;
goto LABEL_18;
case 1:
goto LABEL_16;
case 2:
close(recfd);
lastrec = -1;
goto LABEL_16;
case 3:
case 6:
LABEL_18:
if ( (unsigned int)lseek(recfd, v1, 0) == -1 || (result = read(recfd, inbuf, 0x200uLL)) == 0 )
{
result = fwrite(" EOF\r\n", 1uLL, 6uLL, stderr);
BYTE2(vtreply) |= 0x70u;
return result;
}
if ( result == -1 )
{
BYTE2(vtreply) |= 0x40u;
return result;
}
if ( BYTE2(vtcmd) == 6 )
{
for ( i = 0LL; i != 512; ++i )
{
v8 = i;
if ( inbuf[i] )
break;
v8 = i + 1;
}
BYTE2(vtreply) = 3 * (v8 == 512) + 3;
}
if ( v1 )
{
if ( v1 == 102400 * ((int)((unsigned __int64)(1374389535LL * v1) >> 32) >> 15) )
fprintf(stderr, "\r\n%dK sent\r\n", (unsigned int)v1 >> 10);
}
fputc(114, stderr);
goto LABEL_16;
case 4:
if ( (unsigned int)lseek(recfd, v1, 0) == -1 )
{
result = fwrite(" seek error\r\n", 1uLL, 0xDuLL, stderr);
BYTE2(vtreply) |= 0x50u;
}
else
{
result = write(recfd, inbuf, 0x200uLL);
if ( result <= 0 )
{
BYTE2(vtreply) |= 0x50u;
}
else
{
if ( v1 && v1 == 102400 * ((int)((unsigned __int64)(1374389535LL * v1) >> 32) >> 15) )
fprintf(stderr, "\r\n%dK received\r\n", (unsigned int)v1 >> 10);
fputc(119, stderr);
LABEL_16:
result = fflush(stderr);
}
}
break;
default:
fputc(63, stderr);
BYTE2(vtreply) |= 0x60u;
goto LABEL_16;
}
return result;
}

canonical_decompiled_code:
int func2()
{
int v0; // ebp
int v1; // ebx
const char *v2; // rdi
int v3; // er12
int result; // eax
int *v5; // rax
char *v6; // rax
__int64 i; // rax
int v8; // edx

vtreply = vtcmd;
v0 = BYTE3(vtcmd);
v1 = (BYTE4(vtcmd) + (BYTE5(vtcmd) << 8)) << 9;
if ( lastrec != v0 )
{
v2 = (const char *)recname[BYTE3(vtcmd)];
if ( !v2 )
{
result = fprintf(stderr, "No such tape record %d\r\n", BYTE3(vtcmd));
BYTE2(vtreply) |= 0x10u;
return result;
}
v3 = open(v2, 2);
if ( v3 >= 0 )
{
fprintf(stderr, "\nOpened %s read-write\r\n ", (const char *)recname[v0]);
}
else
{
v3 = open((const char *)recname[v0], 0);
if ( v3 >= 0 )
{
fprintf(stderr, "\nOpened %s read-only\r\n ", (const char *)recname[v0]);
}
else
{
v3 = open((const char *)recname[v0], 578, 384LL);
if ( v3 < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
result = fprintf(stderr, "Cannot open %s: %s\r\n", (const char *)recname[v0], v6);
BYTE2(vtreply) |= 0x10u;
return result;
}
fprintf(stderr, "\nOpened %s as a new file\r\n ", (const char *)recname[v0]);
}
}
if ( lastrec != v0 )
close(recfd);
recfd = v3;
lastrec = v0;
}
switch ( BYTE2(vtcmd) )
{
case 0:
BYTE2(vtreply) = 0;
goto LABEL_18;
case 1:
goto LABEL_16;
case 2:
close(recfd);
lastrec = -1;
goto LABEL_16;
case 3:
case 6:
LABEL_18:
if ( (unsigned int)lseek(recfd, v1, 0) == -1 || (result = read(recfd, inbuf, 0x200uLL)) == 0 )
{
result = fwrite(" EOF\r\n", 1uLL, 6uLL, stderr);
BYTE2(vtreply) |= 0x70u;
return result;
}
if ( result == -1 )
{
BYTE2(vtreply) |= 0x40u;
return result;
}
if ( BYTE2(vtcmd) == 6 )
{
for ( i = 0LL; i != 512; ++i )
{
v8 = i;
if ( inbuf[i] )
break;
v8 = i + 1;
}
BYTE2(vtreply) = 3 * (v8 == 512) + 3;
}
if ( v1 )
{
if ( v1 == 102400 * ((int)((unsigned __int64)(1374389535LL * v1) >> 32) >> 15) )
fprintf(stderr, "\r\n%dK sent\r\n", (unsigned int)v1 >> 10);
}
fputc(114, stderr);
goto LABEL_16;
case 4:
if ( (unsigned int)lseek(recfd, v1, 0) == -1 )
{
result = fwrite(" seek error\r\n", 1uLL, 0xDuLL, stderr);
BYTE2(vtreply) |= 0x50u;
}
else
{
result = write(recfd, inbuf, 0x200uLL);
if ( result <= 0 )
{
BYTE2(vtreply) |= 0x50u;
}
else
{
if ( v1 && v1 == 102400 * ((int)((unsigned __int64)(1374389535LL * v1) >> 32) >> 15) )
fprintf(stderr, "\r\n%dK received\r\n", (unsigned int)v1 >> 10);
fputc(119, stderr);
LABEL_16:
result = fflush(stderr);
}
}
break;
default:
fputc(63, stderr);
BYTE2(vtreply) |= 0x60u;
goto LABEL_16;
}
return result;
}

original_code:
void do_command()
{
  int record, block, i, offset;
  memcpy(&vtreply, &vtcmd, sizeof(vtcmd));
  record = vtcmd.record; block = (vtcmd.blkhi << 8) + vtcmd.blklo;
  offset = block * 512;
  if (record != lastrec) {
    if (recname[record] == ((void *)0)) {
 fprintf(stderr,"No such tape record %d\r\n",record);
 vtreply.cmd |= (1<<4);; return;
    }
    i = open(recname[record], 02);
    if (i>=0) {
       fprintf(stderr,"\nOpened %s read-write\r\n ", recname[record]);
       goto afteropen;
    }
    i = open(recname[record], 00);
    if (i>=0) {
       fprintf(stderr,"\nOpened %s read-only\r\n ", recname[record]);
       goto afteropen;
    }
    i = open(recname[record], 02|0100|01000, 0600);
    if (i>=0) {
       fprintf(stderr,"\nOpened %s as a new file\r\n ", recname[record]);
       goto afteropen;
    }
    fprintf(stderr,"Cannot open %s: %s\r\n",recname[record], strerror((*__errno_location ())));
    vtreply.cmd |= (1<<4);; return;
afteropen:
    if (record != lastrec) close(recfd);
    recfd = i; lastrec = record;
  }
  switch (vtcmd.cmd) {
    case 1: break;
    case 2: close(recfd); lastrec = -1; break;
    case 0: vtreply.cmd=0;
    case 6:
    case 3:
      i= lseek(recfd, offset, 0);
            if (i==-1)
              { fprintf(stderr," EOF\r\n"); vtreply.cmd |= (7<<4);; return; }
         i = read(recfd, &inbuf, 512);
            if (i == 0)
              { fprintf(stderr," EOF\r\n"); vtreply.cmd |= (7<<4);; return; }
            if (i == -1) { vtreply.cmd |= (4<<4);; return; }
      if (vtcmd.cmd==6) {
        for (i=0;i<512;i++) if (inbuf[i]!=0) break;
        if (i==512) vtreply.cmd=6;
        else vtreply.cmd=3;
      }
      if (offset && (offset % 102400) == 0)
   fprintf(stderr,"\r\n%dK sent\r\n", offset/1024);
      fputc('r',stderr);
            break;
    case 4: i= lseek(recfd, offset, 0);
            if (i==-1)
              { fprintf(stderr," seek error\r\n");
   vtreply.cmd |= (5<<4);; return;
        }
      i = write(recfd, &inbuf, 512);
            if (i < 1) { vtreply.cmd |= (5<<4);; return; }
      if (offset && (offset % 102400) == 0)
   fprintf(stderr,"\r\n%dK received\r\n", offset/1024);
      fputc('w',stderr);
             break;
    default: fputc('?',stderr);
         vtreply.cmd |= (6<<4);;
  }
  fflush(stderr);
}

canonical_original_code:
void do_command()
{
  int record; int block; int i; int offset;
  memcpy(&vtreply, &vtcmd, sizeof(vtcmd));
  record = vtcmd.record; block = (vtcmd.blkhi << 8) + vtcmd.blklo;
  offset = block * 512;
  if (record != lastrec) {
    if (recname[record] == ((void *)0)) {
 fprintf(stderr,"No such tape record %d\r\n",record);
 vtreply.cmd |= (1<<4);; return;
    }
    i = open(recname[record], 02);
    if (i>=0) {
       fprintf(stderr,"\nOpened %s read-write\r\n ", recname[record]);
       goto afteropen;
    }
    i = open(recname[record], 00);
    if (i>=0) {
       fprintf(stderr,"\nOpened %s read-only\r\n ", recname[record]);
       goto afteropen;
    }
    i = open(recname[record], 02|0100|01000, 0600);
    if (i>=0) {
       fprintf(stderr,"\nOpened %s as a new file\r\n ", recname[record]);
       goto afteropen;
    }
    fprintf(stderr,"Cannot open %s: %s\r\n",recname[record], strerror((*__errno_location ())));
    vtreply.cmd |= (1<<4);; return;
afteropen:
    if (record != lastrec) close(recfd);
    recfd = i; lastrec = record;
  }
  switch (vtcmd.cmd) {
    case 1: break;
    case 2: close(recfd); lastrec = -1; break;
    case 0: vtreply.cmd=0;
    case 6:
    case 3:
      i= lseek(recfd, offset, 0);
            if (i==-1)
              { fprintf(stderr," EOF\r\n"); vtreply.cmd |= (7<<4);; return; }
         i = read(recfd, &inbuf, 512);
            if (i == 0)
              { fprintf(stderr," EOF\r\n"); vtreply.cmd |= (7<<4);; return; }
            if (i == -1) { vtreply.cmd |= (4<<4);; return; }
      if (vtcmd.cmd==6) {
        for (i=0;i<512;i++) if (inbuf[i]!=0) break;
        if (i==512) vtreply.cmd=6;
        else vtreply.cmd=3;
      }
      if (offset && (offset % 102400) == 0)
   fprintf(stderr,"\r\n%dK sent\r\n", offset/1024);
      fputc('r',stderr);
            break;
    case 4: i= lseek(recfd, offset, 0);
            if (i==-1)
              { fprintf(stderr," seek error\r\n");
   vtreply.cmd |= (5<<4);; return;
        }
      i = write(recfd, &inbuf, 512);
            if (i < 1) { vtreply.cmd |= (5<<4);; return; }
      if (offset && (offset % 102400) == 0)
   fprintf(stderr,"\r\n%dK received\r\n", offset/1024);
      fputc('w',stderr);
             break;
    default: fputc('?',stderr);
         vtreply.cmd |= (6<<4);;
  }
  fflush(stderr);
}

variable_types: record int

variable_types: block int

variable_types: offset int


setraw

decompiled_code:
int __fastcall setraw(int fd, const char *a2, int a3)
{
tcflag_t v4; // eax
int result; // eax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
struct termios v10; // [rsp+0h] [rbp-58h] BYREF

if ( tcgetattr(fd, &v10) == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "Error getting %s attributes: %s\n", a2, v7);
exit(1);
}
if ( a3 )
{
*(__m128i *)&oldterm.c_iflag = _mm_loadu_si128((const __m128i *)&v10);
*(__m128i *)&oldterm.c_line = _mm_loadu_si128((const __m128i *)&v10.c_line);
*(__m128i *)&oldterm.c_cc[15] = _mm_loadu_si128((const __m128i *)&v10.c_cc[15]);
*(_QWORD *)&oldterm.c_cc[31] = *(_QWORD *)&v10.c_cc[31];
oldterm.c_ospeed = v10.c_ospeed;
}
v10.c_iflag = v10.c_iflag & 0xFFFFCA01 | 1;
v10.c_oflag &= 0xFFFFFFFE;
v10.c_lflag &= 0xFFFF3E04;
v4 = v10.c_cflag & 0xFFFFFECF;
LOBYTE(v4) = v10.c_cflag & 0xCF | 0xB0;
v10.c_cflag = v4;
*(_WORD *)&v10.c_cc[5] = 256;
result = tcsetattr(fd, 0, &v10);
if ( result == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "Error setting %s attributes: %s\n", a2, v9);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int fd, const char *a2, int a3)
{
tcflag_t v4; // eax
int result; // eax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
struct termios v10; // [rsp+0h] [rbp-58h] BYREF

if ( tcgetattr(fd, &v10) == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "Error getting %s attributes: %s\n", a2, v7);
exit(1);
}
if ( a3 )
{
*(__m128i *)&oldterm.c_iflag = _mm_loadu_si128((const __m128i *)&v10);
*(__m128i *)&oldterm.c_line = _mm_loadu_si128((const __m128i *)&v10.c_line);
*(__m128i *)&oldterm.c_cc[15] = _mm_loadu_si128((const __m128i *)&v10.c_cc[15]);
*(_QWORD *)&oldterm.c_cc[31] = *(_QWORD *)&v10.c_cc[31];
oldterm.c_ospeed = v10.c_ospeed;
}
v10.c_iflag = v10.c_iflag & 0xFFFFCA01 | 1;
v10.c_oflag &= 0xFFFFFFFE;
v10.c_lflag &= 0xFFFF3E04;
v4 = v10.c_cflag & 0xFFFFFECF;
LOBYTE(v4) = v10.c_cflag & 0xCF | 0xB0;
v10.c_cflag = v4;
*(_WORD *)&v10.c_cc[5] = 256;
result = tcsetattr(fd, 0, &v10);
if ( result == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "Error setting %s attributes: %s\n", a2, v9);
exit(1);
}
return result;
}

variable_types: fd int

variable_types:
portname
char *

variable_types: dosave int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


termexit

decompiled_code:
void __fastcall __noreturn termexit(int status)
{
tcsetattr(ttyfd, 0, &oldterm);
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(int status)
{
tcsetattr(ttyfd, 0, &oldterm);
exit(status);
}

original_code:
void termexit(int how)
{
  tcsetattr(ttyfd, 0, &oldterm);
  exit(how);
}

variable_types: how int


decompiled_code:
int open_port()
{
int *v1; // rax
char *v2; // rax

fprintf(stderr, "Opening port %s .... ", port);
fflush(stderr);
portfd = open(port, 2);
if ( portfd == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
fprintf(stderr, "Error opening device %s: %s\n", port, v2);
exit(1);
}
fwrite("Port open\n", 1uLL, 0xAuLL, stderr);
return setraw(portfd, port, 0);
}

canonical_decompiled_code:
int func6()
{
int *v1; // rax
char *v2; // rax

fprintf(stderr, "Opening port %s .... ", port);
fflush(stderr);
portfd = open(port, 2);
if ( portfd == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
fprintf(stderr, "Error opening device %s: %s\n", port, v2);
exit(1);
}
fwrite("Port open\n", 1uLL, 0xAuLL, stderr);
return func4(portfd, port, 0);
}

original_code:
void open_port()
{
  fprintf(stderr,"Opening port %s .... ", port); fflush(stderr);
  portfd = open(port, 02);
  if (portfd == -1) {
    fprintf(stderr, "Error opening device %s: %s\n", port, strerror((*__errno_location ())));
    exit(1);
  }
  fprintf(stderr,"Port open\n");
  setraw(portfd,port,0);
}


decompiled_code:
void __noreturn server_loop()
{
int v0; // ebp
int v1; // er12
int v2; // edi
fd_set v3; // [rsp+0h] [rbp-A8h] BYREF
char buf[25]; // [rsp+8Fh] [rbp-19h] BYREF

buf[0] = 13;
write(portfd, buf, 1uLL);
memset(&v3, 0, sizeof(v3));
v0 = 0;
v1 = 0;
while ( 1 )
{
do
{
v3.fds_bits[ttyfd / 64] |= 1LL << (ttyfd % 64);
v2 = portfd;
v3.fds_bits[portfd / 64] |= 1LL << (portfd % 64);
}
while ( select(v2 + 1, &v3, 0LL, 0LL, 0LL) <= 0 );
if ( (v3.fds_bits[ttyfd / 64] & (1LL << (ttyfd % 64))) != 0 )
{
read(ttyfd, buf, 1uLL);
if ( !v0 )
{
if ( buf[0] == 27 )
{
if ( ++v1 == 2 )
termexit(0);
}
else
{
v1 = 0;
}
write(portfd, buf, 1uLL);
}
}
if ( (v3.fds_bits[portfd / 64] & (1LL << (portfd % 64))) != 0 )
{
v0 = get_command(&vtcmd);
if ( v0 )
{
do_command();
send_reply();
v0 = 1;
}
}
}
}

canonical_decompiled_code:
void __noreturn func7()
{
int v0; // ebp
int v1; // er12
int v2; // edi
fd_set v3; // [rsp+0h] [rbp-A8h] BYREF
char buf[25]; // [rsp+8Fh] [rbp-19h] BYREF

buf[0] = 13;
write(portfd, buf, 1uLL);
memset(&v3, 0, sizeof(v3));
v0 = 0;
v1 = 0;
while ( 1 )
{
do
{
v3.fds_bits[ttyfd / 64] |= 1LL << (ttyfd % 64);
v2 = portfd;
v3.fds_bits[portfd / 64] |= 1LL << (portfd % 64);
}
while ( select(v2 + 1, &v3, 0LL, 0LL, 0LL) <= 0 );
if ( (v3.fds_bits[ttyfd / 64] & (1LL << (ttyfd % 64))) != 0 )
{
read(ttyfd, buf, 1uLL);
if ( !v0 )
{
if ( buf[0] == 27 )
{
if ( ++v1 == 2 )
func5(0);
}
else
{
v1 = 0;
}
write(portfd, buf, 1uLL);
}
}
if ( (v3.fds_bits[portfd / 64] & (1LL << (portfd % 64))) != 0 )
{
v0 = func0(&vtcmd);
if ( v0 )
{
func2();
func1();
v0 = 1;
}
}
}
}

original_code:
void server_loop()
{
  char ch;
  int i;
  int esc_num=0;
  int in_tape_mode=0;
  fd_set fdset;
  ch='\r'; write(portfd,&ch,1);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdset)->__fds_bits)[0]) : "memory"); } while (0);
  while (1) {
    ((void) (((&fdset)->__fds_bits)[((ttyfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ttyfd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fdset)->__fds_bits)[((portfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((portfd) % (8 * (int) sizeof (__fd_mask)))))));
    i=select(portfd+1, &fdset, ((void *)0), ((void *)0), ((void *)0));
    if (i<1) continue;
    if (((((&fdset)->__fds_bits)[((ttyfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ttyfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 read(ttyfd,&ch,1);
 if (!in_tape_mode) {
   if (ch=='') esc_num++;
   else esc_num=0;
   if (esc_num==2) termexit(0);
   write(portfd,&ch,1);
 }
    }
    if (((((&fdset)->__fds_bits)[((portfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((portfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if (get_command(&vtcmd)==0) { in_tape_mode=0; continue; }
      in_tape_mode=1;
      do_command();
      send_reply();
    }
  }
}

canonical_original_code:
void server_loop()
{
  char ch;
  int i;
  int esc_num = 0;
  int in_tape_mode = 0;
  struct fd_set fdset;
  ch='\r'; write(portfd,&ch,1);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdset)->__fds_bits)[0]) : "memory"); } while (0);
  while (1) {
    ((void) (((&fdset)->__fds_bits)[((ttyfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ttyfd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fdset)->__fds_bits)[((portfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((portfd) % (8 * (int) sizeof (__fd_mask)))))));
    i=select(portfd+1, &fdset, ((void *)0), ((void *)0), ((void *)0));
    if (i<1) continue;
    if (((((&fdset)->__fds_bits)[((ttyfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ttyfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
 read(ttyfd,&ch,1);
 if (!in_tape_mode) {
   if (ch=='') esc_num++;
   else esc_num=0;
   if (esc_num==2) termexit(0);
   write(portfd,&ch,1);
 }
    }
    if (((((&fdset)->__fds_bits)[((portfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((portfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if (get_command(&vtcmd)==0) { in_tape_mode=0; continue; }
      in_tape_mode=1;
      do_command();
      send_reply();
    }
  }
}

variable_types: i int

variable_types:
fdset
struct fd_set { long __fds_bits[-1]; }


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
fwrite("Virtual tape server, $Revision: 2.3.1.5 $ \n", 1uLL, 0x2BuLL, stderr);
if ( argc == 2 && !strcmp(argv[1], "-odt") )
havesentbootcode = 0;
read_config();
open_port();
setraw(ttyfd, "standard input", 1);
server_loop();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
fwrite("Virtual tape server, $Revision: 2.3.1.5 $ \n", 1uLL, 0x2BuLL, stderr);
if ( argc == 2 && !strcmp(argv[1], "-odt") )
havesentbootcode = 0;
func3();
func6();
func4(ttyfd, "standard input", 1);
func7();
}

original_code:
int main(int argc, char *argv[])
{
  fprintf(stderr,"Virtual tape server, $Revision: 2.3.1.5 $ \n");
  if ((argc==2) && (!strcmp(argv[1], "-odt"))) havesentbootcode=0;
  read_config(); open_port();
  setraw(ttyfd,"standard input",1);
  server_loop();
  exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]


repo: charlag/coursework

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
const char *v4; // rdi
__int64 v5; // rdx
int v6; // ebp
double *v7; // rsi
__int64 v8; // rcx
_DWORD *v9; // rdi
double *v10; // rsi
__int64 v11; // rcx
_DWORD *v12; // rdi
void *v13; // rdx
double *v14; // rsi
__int64 v15; // rcx
_DWORD *v16; // rdi
_BYTE v18[72]; // [rsp-40h] [rbp-D8h] BYREF
double v19; // [rsp+8h] [rbp-90h] BYREF
double v20; // [rsp+10h] [rbp-88h] BYREF
void *v21; // [rsp+18h] [rbp-80h] BYREF
void *v22; // [rsp+20h] [rbp-78h] BYREF
void *ptr; // [rsp+28h] [rbp-70h] BYREF
double v24; // [rsp+30h] [rbp-68h] BYREF
double v25; // [rsp+38h] [rbp-60h]
double v26; // [rsp+40h] [rbp-58h] BYREF
double v27; // [rsp+48h] [rbp-50h]
unsigned int v28; // [rsp+70h] [rbp-28h]

v3 = menu(argc, argv, envp);
ptr = malloc(0x50uLL);
v22 = malloc(0x50uLL);
v4 = (_BYTE *)&loc_50;
v21 = malloc(0x50uLL);
if ( v3 != 8 )
{
v6 = 0;
do
{
switch ( v3 )
{
case 1:
argv = (const char **)&v24;
if ( (int)file_read_params(&v26, &v24) > 8 )
{
v4 = "succes!";
puts("succes!");
getint();
v6 = v3;
}
else
{
v4 = "Something went wrong";
puts("Something went wrong");
getint();
v6 = 0;
}
break;
case 2:
argv = (const char **)&v24;
v6 = manual(&v26, &v24);
if ( v6 )
{
v4 = "succes!";
puts("succes!");
getint();
v6 = 1;
}
else
{
v4 = "Cannnot evalute if less then 2 points";
puts("Cannnot evalute if less then 2 points");
getint();
}
break;
case 3:
if ( v6 <= 1 )
goto LABEL_14;
v7 = &v26;
v8 = 14LL;
v9 = v18;
while ( v8 )
{
*v9 = *(_DWORD *)v7;
v7 = (double *)((char *)v7 + 4);
++v9;
--v8;
}
argv = (const char **)ptr;
v4 = (const char *)stdout;
print_signal(stdout, ptr, v22, v21, v24, v25, v20, v19);
getint();
break;
case 4:
if ( v6 > 1 )
{
v10 = &v26;
v11 = 14LL;
v12 = v18;
while ( v11 )
{
*v12 = *(_DWORD *)v10;
v10 = (double *)((char *)v10 + 4);
++v12;
--v11;
}
argv = (const char **)v22;
if ( (unsigned int)file_print(ptr, v22, v21, v24, v25, v20, v19) )
{
v4 = "Success!";
puts("Success!");
}
else
{
v4 = "Something went wrong";
puts("Something went wrong");
}
goto LABEL_21;
}
LABEL_14:
v4 = "Signal is not generated";
puts("Signal is not generated");
getint();
break;
case 5:
if ( v6 <= 0 )
{
v4 = "Signal is not inserted";
puts("Signal is not inserted");
getint();
}
else
{
ptr = realloc(ptr, 8LL * (int)(v28 + 1));
v22 = realloc(v22, 8LL * (int)(v28 + 1));
v13 = realloc(v21, 8LL * (int)(v28 + 1));
v21 = v13;
v14 = &v26;
v15 = 14LL;
v16 = v18;
while ( v15 )
{
*v16 = *(_DWORD *)v14;
v14 = (double *)((char *)v14 + 4);
++v16;
--v15;
}
sig_gen(ptr, v22, v13, v24, v25);
v20 = sig_duration(v22, v28, v26, v27);
argv = (const char **)v28;
v19 = sig_duration(v21, v28, v26, v27);
v4 = "Success!";
puts("Success!");
getint();
v6 = 2;
}
break;
case 6:
argv = (const char **)&v24;
v6 = file_read_result(&v26, &v24, &v22, &v21, &ptr, &v20, &v19);
if ( v6 )
{
v4 = "succes!";
puts("succes!");
getint();
v6 = 2;
}
else
{
v4 = "Something went wrong";
puts("Something went wrong");
getint();
}
break;
case 7:
if ( v6 <= 1 )
{
v4 = "Signal is not generated";
puts("Signal is not generated");
}
else
{
argv = (const char **)ptr;
maxima(v28, ptr, v22, v21);
v4 = "Generated files. Open mxm.wxm with WxMaxima";
puts("Generated files. Open mxm.wxm with WxMaxima");
}
LABEL_21:
getint();
break;
case 8:
v4 = "Bye";
puts("Bye");
break;
default:
break;
}
v3 = menu(v4, argv, v5);
}
while ( v3 != 8 );
}
return 0;
}

original_code:
int main() {
 IN_SIGNAL in_sig;
 OUT_SIGNAL out_sig;
 int choice = menu();
 int ready = 0;
 double* time = (double*) malloc(sizeof(double) * 10);
 double* u_in = (double*) malloc(sizeof(double) * 10);
 double* u_out = (double*) malloc(sizeof(double) * 10);
 double in_durat, out_durat;
 while (choice != 8) {
  switch(choice) {
  case 1:
   if(file_read_params(&in_sig, &out_sig) < 9) {
    ready = 0;
    puts("Something went wrong");
    getint();
   } else {
    puts("succes!");
    ready = 1;
    getint();
   }
   break;
  case 2:
   if(!manual(&in_sig, &out_sig)) {
    puts("Cannnot evalute if less then 2 points");
    ready = 0;
    getint();
    break;
   }
   puts("succes!");
   ready = 1;
   getint();
   break;
  case 3:
   if (ready < 2) {
    puts("Signal is not generated");
    getint();
    break;
   }
   print_signal(stdout, in_sig, out_sig, time, u_in, u_out,
         in_durat, out_durat);
   getint();
   break;
  case 4:
   if (ready < 2) {
    puts("Signal is not generated");
    getint();
   } else if ( !file_print(in_sig, out_sig, time, u_in, u_out, in_durat, out_durat)) {
    puts("Something went wrong");
    getint();
   } else {
    puts("Success!");
    getint();
   }
   break;
  case 5:
   if (ready < 1) {
    puts("Signal is not inserted");
    getint();
   } else {
     time = realloc(time, sizeof(double) * (in_sig.n+1));
     u_in = realloc(u_in, sizeof(double) * (in_sig.n+1));
     u_out = realloc(u_out, sizeof(double) * (in_sig.n+1));
    sig_gen(in_sig, out_sig, time, u_in, u_out);
    ready = 2;
    in_durat = sig_duration(u_in, in_sig.n, in_sig.tn, in_sig.tk);
    out_durat = sig_duration(u_out, in_sig.n, in_sig.tn, in_sig.tk);
    puts("Success!");
    getint();
   }
   break;
  case 6:
   if (!file_read_result(&in_sig, &out_sig, &u_in, &u_out, &time, &in_durat, &out_durat)) {
    ready = 0;
    puts("Something went wrong");
    getint();
   } else {
    puts("succes!");
    ready = 2;
    getint();
   }
   break;
  case 7:
   if(ready < 2) {
    puts("Signal is not generated");
    getint();
   } else {
    maxima(in_sig.n, time, u_in, u_out);
    puts("Generated files. Open mxm.wxm with WxMaxima");
    getint();
   }
   break;
  case 8:
   puts("Bye");
   break;
  }
  choice = menu();
 }
 return 0;
}

canonical_original_code:
int main() {
 struct IN_SIGNAL in_sig;
 struct OUT_SIGNAL out_sig;
 int choice = menu();
 int ready = 0;
 double *time = (double*) malloc(sizeof(double) * 10);
 double *u_in = (double*) malloc(sizeof(double) * 10);
 double *u_out = (double*) malloc(sizeof(double) * 10);
 double in_durat; double out_durat;
 while (choice != 8) {
  switch(choice) {
  case 1:
   if(file_read_params(&in_sig, &out_sig) < 9) {
    ready = 0;
    puts("Something went wrong");
    getint();
   } else {
    puts("succes!");
    ready = 1;
    getint();
   }
   break;
  case 2:
   if(!manual(&in_sig, &out_sig)) {
    puts("Cannnot evalute if less then 2 points");
    ready = 0;
    getint();
    break;
   }
   puts("succes!");
   ready = 1;
   getint();
   break;
  case 3:
   if (ready < 2) {
    puts("Signal is not generated");
    getint();
    break;
   }
   print_signal(stdout, in_sig, out_sig, time, u_in, u_out,
         in_durat, out_durat);
   getint();
   break;
  case 4:
   if (ready < 2) {
    puts("Signal is not generated");
    getint();
   } else if ( !file_print(in_sig, out_sig, time, u_in, u_out, in_durat, out_durat)) {
    puts("Something went wrong");
    getint();
   } else {
    puts("Success!");
    getint();
   }
   break;
  case 5:
   if (ready < 1) {
    puts("Signal is not inserted");
    getint();
   } else {
     time = realloc(time, sizeof(double) * (in_sig.n+1));
     u_in = realloc(u_in, sizeof(double) * (in_sig.n+1));
     u_out = realloc(u_out, sizeof(double) * (in_sig.n+1));
    sig_gen(in_sig, out_sig, time, u_in, u_out);
    ready = 2;
    in_durat = sig_duration(u_in, in_sig.n, in_sig.tn, in_sig.tk);
    out_durat = sig_duration(u_out, in_sig.n, in_sig.tn, in_sig.tk);
    puts("Success!");
    getint();
   }
   break;
  case 6:
   if (!file_read_result(&in_sig, &out_sig, &u_in, &u_out, &time, &in_durat, &out_durat)) {
    ready = 0;
    puts("Something went wrong");
    getint();
   } else {
    puts("succes!");
    ready = 2;
    getint();
   }
   break;
  case 7:
   if(ready < 2) {
    puts("Signal is not generated");
    getint();
   } else {
    maxima(in_sig.n, time, u_in, u_out);
    puts("Generated files. Open mxm.wxm with WxMaxima");
    getint();
   }
   break;
  case 8:
   puts("Bye");
   break;
  }
  choice = menu();
 }
 return 0;
}

variable_types: struct IN_SIGNAL { double tn; double tk; double tm; double u; double a; double b; int n; }

variable_types: struct OUT_SIGNAL { double u1; double u2; }

variable_types: choice int

variable_types: ready int

variable_types:
time
double *


readline

decompiled_code:
char *readline()
{
__ssize_t v0; // rax
char *v1; // rax
char *v3; // [rsp+0h] [rbp-18h] BYREF
size_t n[2]; // [rsp+8h] [rbp-10h] BYREF

n[0] = 2LL;
v3 = 0LL;
v0 = getline(&v3, n, stdin);
v1 = &v3[v0 - 1];
if ( *v1 == 10 )
*v1 = 0;
return v3;
}

canonical_decompiled_code:
char *func0()
{
__ssize_t v0; // rax
char *v1; // rax
char *v3; // [rsp+0h] [rbp-18h] BYREF
size_t n[2]; // [rsp+8h] [rbp-10h] BYREF

n[0] = 2LL;
v3 = 0LL;
v0 = getline(&v3, n, stdin);
v1 = &v3[v0 - 1];
if ( *v1 == 10 )
*v1 = 0;
return v3;
}

original_code:
char* readline() {
 size_t nbytes = 2;
 char* line = ((void *)0);
 ssize_t read = getline(&line, &nbytes, stdin);
 if (line[read - 1] == '\n') {
  line[read - 1] = '\0';
 }
 return line;
}

canonical_original_code:
char *readline() {
 unsigned long nbytes = 2;
 char *line = ((void *)0);
 long read = getline(&line, &nbytes, stdin);
 if (line[read - 1] == '\n') {
  line[read - 1] = '\0';
 }
 return line;
}

variable_types:
nbytes
unsigned long

variable_types:
line
char *

variable_types: read long

return_type: char *


getint

decompiled_code:
__int64 getint()
{
char *v0; // rbx
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

v2 = 0;
v0 = readline();
if ( !sscanf(v0, "%d", &v2) )
puts("No numer");
free(v0);
return v2;
}

canonical_decompiled_code:
__int64 func1()
{
char *v0; // rbx
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

v2 = 0;
v0 = func0();
if ( !sscanf(v0, "%d", &v2) )
puts("No numer");
free(v0);
return v2;
}

original_code:
int getint() {
 int num = 0;
 char* line = readline();
 if (!sscanf(line, "%d", &num)) {
  puts("No numer");
 }
 free(line);
 return num;
}

canonical_original_code:
int getint() {
 int num = 0;
 char *line = readline();
 if (!sscanf(line, "%d", &num)) {
  puts("No numer");
 }
 free(line);
 return num;
}

variable_types: num int


getdouble

decompiled_code:
double getdouble()
{
char *v0; // rbx
double v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0.0;
v0 = readline();
if ( !sscanf(v0, "%lf", &v2) )
puts("No number");
free(v0);
return v2;
}

canonical_decompiled_code:
double func2()
{
char *v0; // rbx
double v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0.0;
v0 = func0();
if ( !sscanf(v0, "%lf", &v2) )
puts("No number");
free(v0);
return v2;
}

original_code:
double getdouble() {
 double num = 0;
 char *line = readline();
 if (!sscanf(line, "%lf", &num)) {
  puts("No number");
 }
 free(line);
 return num;
}

variable_types: num double

return_type: double


getstring

decompiled_code:
void *getstring()
{
char *v0; // rbx
size_t v1; // rax
void *v2; // rbp

v0 = readline();
v1 = strlen(v0);
v2 = malloc(v1 + 1);
sscanf(v0, "%s", v2);
free(v0);
return v2;
}

canonical_decompiled_code:
void *func3()
{
char *v0; // rbx
size_t v1; // rax
void *v2; // rbp

v0 = func0();
v1 = strlen(v0);
v2 = malloc(v1 + 1);
sscanf(v0, "%s", v2);
free(v0);
return v2;
}

original_code:
char* getstring() {
 char* rawline = readline();
 char* newline = malloc(strlen(rawline) + 1);
 sscanf(rawline, "%s", newline);
 if(rawline) {
  free(rawline);
 }
 return newline;
}

canonical_original_code:
char *getstring() {
 char *rawline = readline();
 char *newline = malloc(strlen(rawline) + 1);
 sscanf(rawline, "%s", newline);
 if(rawline) {
  free(rawline);
 }
 return newline;
}

variable_types:
rawline
char *

variable_types:
newline
char *


menu

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

do
{
printf("\x1B[1;1H\x1B[2J");
puts("\n\n");
puts(" 1: Input signal parametrs from file");
puts(" 2: Insert signal parametrs manually");
puts(" 3: Display signal");
puts(" 4: Print signal to file");
puts(" 5: Generate signal");
puts(" 6: Input results from file");
puts(" 7: Plot the graphics in Maxima");
puts(" 8: Exit");
putchar(62);
result = getint();
}
while ( (unsigned int)result > 8 );
return result;
}

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

do
{
printf("\x1B[1;1H\x1B[2J");
puts("\n\n");
puts(" 1: Input signal parametrs from file");
puts(" 2: Insert signal parametrs manually");
puts(" 3: Display signal");
puts(" 4: Print signal to file");
puts(" 5: Generate signal");
puts(" 6: Input results from file");
puts(" 7: Plot the graphics in Maxima");
puts(" 8: Exit");
putchar(62);
result = getint();
}
while ( (unsigned int)result > 8 );
return result;
}

original_code:
int menu(void) {
 int option = 0 ;
 do {
  printf("\e[1;1H\e[2J");
  printf("\n\n\n");
  puts(" 1: Input signal parametrs from file");
  puts(" 2: Insert signal parametrs manually");
  puts(" 3: Display signal");
  puts(" 4: Print signal to file");
  puts(" 5: Generate signal");
  puts(" 6: Input results from file");
  puts(" 7: Plot the graphics in Maxima");
  puts(" 8: Exit");
  printf(">");
  option = getint();
 } while ((option < 0) || (option > 8));
 return option;
}

canonical_original_code:
int menu() {
 int option = 0;
 do {
  printf("\e[1;1H\e[2J");
  printf("\n\n\n");
  puts(" 1: Input signal parametrs from file");
  puts(" 2: Insert signal parametrs manually");
  puts(" 3: Display signal");
  puts(" 4: Print signal to file");
  puts(" 5: Generate signal");
  puts(" 6: Input results from file");
  puts(" 7: Plot the graphics in Maxima");
  puts(" 8: Exit");
  printf(">");
  option = getint();
 } while ((option < 0) || (option > 8));
 return option;
}

variable_types: option int


manual

decompiled_code:
_BOOL8 __fastcall manual(__int64 a1, double *a2)
{
puts("Not inserted values will be set to 0");
puts("Insert u:");
*(double *)(a1 + 24) = getdouble();
puts("Insert tn:");
*(double *)a1 = getdouble();
puts("Insert t1:");
*(double *)(a1 + 16) = getdouble();
puts("Insert tk:");
*(double *)(a1 + 8) = getdouble();
puts("Insert a:");
*(double *)(a1 + 32) = getdouble();
puts("Insert b:");
*(double *)(a1 + 40) = getdouble();
puts("Insert muber of point to calcute, > 1");
*(_DWORD *)(a1 + 48) = getint();
puts("Insert u1:");
*a2 = getdouble();
puts("Insert u2:");
a2[1] = getdouble();
return *(_DWORD *)(a1 + 48) > 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, double *a2)
{
puts("Not inserted values will be set to 0");
puts("Insert u:");
*(double *)(a1 + 24) = getdouble();
puts("Insert tn:");
*(double *)a1 = getdouble();
puts("Insert t1:");
*(double *)(a1 + 16) = getdouble();
puts("Insert tk:");
*(double *)(a1 + 8) = getdouble();
puts("Insert a:");
*(double *)(a1 + 32) = getdouble();
puts("Insert b:");
*(double *)(a1 + 40) = getdouble();
puts("Insert muber of point to calcute, > 1");
*(_DWORD *)(a1 + 48) = getint();
puts("Insert u1:");
*a2 = getdouble();
puts("Insert u2:");
a2[1] = getdouble();
return *(_DWORD *)(a1 + 48) > 1;
}

original_code:
int manual(IN_SIGNAL *in_sig, OUT_SIGNAL *out_sig) {
 puts("Not inserted values will be set to 0");
 puts("Insert u:");
 in_sig->u = getdouble();
 puts("Insert tn:");
 in_sig->tn = getdouble();
 puts("Insert t1:");
 in_sig->tm = getdouble();
 puts("Insert tk:");
 in_sig->tk = getdouble();
 puts("Insert a:");
 in_sig->a = getdouble();
        puts("Insert b:");
 in_sig->b = getdouble();
 puts("Insert muber of point to calcute, > 1");
 in_sig->n = getint();
 puts("Insert u1:");
 out_sig->u1 = getdouble();
 puts("Insert u2:");
 out_sig->u2 = getdouble();
 if (in_sig->n <= 1) {
  return 0;
 }
 return 1;
}

canonical_original_code:
int manual(struct IN_SIGNAL *in_sig, struct OUT_SIGNAL *out_sig) {
 puts("Not inserted values will be set to 0");
 puts("Insert u:");
 in_sig->u = getdouble();
 puts("Insert tn:");
 in_sig->tn = getdouble();
 puts("Insert t1:");
 in_sig->tm = getdouble();
 puts("Insert tk:");
 in_sig->tk = getdouble();
 puts("Insert a:");
 in_sig->a = getdouble();
        puts("Insert b:");
 in_sig->b = getdouble();
 puts("Insert muber of point to calcute, > 1");
 in_sig->n = getint();
 puts("Insert u1:");
 out_sig->u1 = getdouble();
 puts("Insert u2:");
 out_sig->u2 = getdouble();
 if (in_sig->n <= 1) {
  return 0;
 }
 return 1;
}

variable_types: struct IN_SIGNAL { double tn; double tk; double tm; double u; double a; double b; int n; } *

variable_types: struct OUT_SIGNAL { double u1; double u2; } *


original_code:
void sig_gen(const IN_SIGNAL in_sig, const OUT_SIGNAL out_sig, double *time, double *u_in, double *u_out) {
 double t = in_sig.tn;
 double dt = (in_sig.tk - in_sig.tn)/(in_sig.n-1);
 double mult = out_sig.u2/out_sig.u1;
 for(int i = 0; i < in_sig.n; i++) {
  time[i] = t;
  if (t < in_sig.tm) {
   u_in[i] = in_sig.u*(1-exp(-in_sig.a*(t - in_sig.tn)));
  } else {
   u_in[i] = in_sig.u*(1-exp(-in_sig.a*(in_sig.tm - in_sig.tn))) * exp(-in_sig.b * (t - in_sig.tm));
  }
  if (u_in[i] < out_sig.u1) {
   u_out[i] = u_in[i]*mult;
  } else {
   u_out[i] = out_sig.u2;
  }
  t+= dt;
 }
}

canonical_original_code:
void sig_gen(struct IN_SIGNAL in_sig, struct OUT_SIGNAL out_sig, double *time, double *u_in, double *u_out) {
 double t = in_sig.tn;
 double dt = (in_sig.tk - in_sig.tn)/(in_sig.n-1);
 double mult = out_sig.u2/out_sig.u1;
 for(int i = 0; i < in_sig.n; i++) {
  time[i] = t;
  if (t < in_sig.tm) {
   u_in[i] = in_sig.u*(1-exp(-in_sig.a*(t - in_sig.tn)));
  } else {
   u_in[i] = in_sig.u*(1-exp(-in_sig.a*(in_sig.tm - in_sig.tn))) * exp(-in_sig.b * (t - in_sig.tm));
  }
  if (u_in[i] < out_sig.u1) {
   u_out[i] = u_in[i]*mult;
  } else {
   u_out[i] = out_sig.u2;
  }
  t+= dt;
 }
}

variable_types: t double

variable_types: dt double

variable_types: mult double

variable_types: i int

user_defined_types: struct IN_SIGNAL { double tn; double tk; double tm; double u; double a; double b; int n; }

user_defined_types: struct OUT_SIGNAL { double u1; double u2; }


clear

repo: charlesUnixPro/Tempest-Source-Code

decompiled_code:
char *clear()
{
char *v0; // rdx
char *result; // rax

v0 = (char *)&unk_404480;
do
{
result = v0 - 1024;
do
*result++ = 32;
while ( result != v0 );
v0 += 1024;
}
while ( v0 != (char *)5260416 );
return result;
}

canonical_decompiled_code:
char *func7()
{
char *v0; // rdx
char *result; // rax

v0 = (char *)&unk_404480;
do
{
result = v0 - 1024;
do
*result++ = 32;
while ( result != v0 );
v0 += 1024;
}
while ( v0 != (char *)5260416 );
return result;
}

original_code:
static void clear (void)
  {
    int x, y;
    for (y = 0; y < 1024; y ++)
      for (x = 0; x < 1024; x ++)
        image [y] [x] = ' ';
  }

canonical_original_code:
static void clear()
  {
    int x; int y;
    for (y = 0; y < 1024; y ++)
      for (x = 0; x < 1024; x ++)
        image [y] [x] = ' ';
  }

variable_types: x int

variable_types: y int


putpixel

decompiled_code:
int __fastcall putpixel(unsigned int a1, unsigned int a2, unsigned int a3)
{
int result; // eax

if ( a2 + 512 > 0x3FF )
return printf("%d %d %d\n", a1, a2, a3);
result = a1 + 512;
if ( a1 + 512 > 0x3FF )
return printf("%d %d %d\n", a1, a2, a3);
image[1024 * (__int64)(int)(a2 + 512) + result] = 42;
return result;
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, unsigned int a2, unsigned int a3)
{
int result; // eax

if ( a2 + 512 > 0x3FF )
return printf("%d %d %d\n", a1, a2, a3);
result = a1 + 512;
if ( a1 + 512 > 0x3FF )
return printf("%d %d %d\n", a1, a2, a3);
image[1024 * (__int64)(int)(a2 + 512) + result] = 42;
return result;
}

original_code:
static void putpixel (int x, int y, int c)
  {
    int xp = x + 1024 / 2;
    int yp = y + 1024 / 2;
    if (xp < 0 || xp >= 1024 || yp < 0 || yp >= 1024)
      printf ("%d %d %d\n", x, y, c);
    else
      image [yp] [xp] = '*';
  }

canonical_original_code:
static void putpixel(int x, int y, int c)
  {
    int xp = x + 1024 / 2;
    int yp = y + 1024 / 2;
    if (xp < 0 || xp >= 1024 || yp < 0 || yp >= 1024)
      printf ("%d %d %d\n", x, y, c);
    else
      image [yp] [xp] = '*';
  }

variable_types: c int

variable_types: xp int

variable_types: yp int


draw

decompiled_code:
int __fastcall draw(int a1, int a2, int a3, int a4, unsigned int a5)
{
float v6; // xmm2_4
int v7; // ecx
int v8; // ebp
signed int v9; // eax
int result; // eax
int i; // ebx
float v12; // [rsp+0h] [rbp-28h]
float v13; // [rsp+4h] [rbp-24h]
float v14; // [rsp+Ch] [rbp-1Ch]

v6 = (float)(a3 - a1);
v7 = a4 - a2;
v8 = abs32((int)v6);
v9 = abs32((int)(float)v7);
if ( v8 < v9 )
v8 = v9;
v14 = (float)v7 / (float)v8;
v12 = (float)a1;
v13 = (float)a2;
result = putpixel((int)(float)a1, (int)(float)a2, a5);
if ( v8 > 0 )
{
for ( i = 1; i <= v8; ++i )
{
v12 = v12 + (float)(v6 / (float)v8);
v13 = v13 + v14;
result = putpixel((int)v12, (int)v13, a5);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int a1, int a2, int a3, int a4, unsigned int a5)
{
float v6; // xmm2_4
int v7; // ecx
int v8; // ebp
signed int v9; // eax
int result; // eax
int i; // ebx
float v12; // [rsp+0h] [rbp-28h]
float v13; // [rsp+4h] [rbp-24h]
float v14; // [rsp+Ch] [rbp-1Ch]

v6 = (float)(a3 - a1);
v7 = a4 - a2;
v8 = abs32((int)v6);
v9 = abs32((int)(float)v7);
if ( v8 < v9 )
v8 = v9;
v14 = (float)v7 / (float)v8;
v12 = (float)a1;
v13 = (float)a2;
result = func8((int)(float)a1, (int)(float)a2, a5);
if ( v8 > 0 )
{
for ( i = 1; i <= v8; ++i )
{
v12 = v12 + (float)(v6 / (float)v8);
v13 = v13 + v14;
result = func8((int)v12, (int)v13, a5);
}
}
return result;
}

original_code:
static void draw (int x1, int y1, int x2, int y2, int c)
  {
    float x, y, xinc, yinc, dx, dy;
    int k;
    int step;
    dx = x2 - x1;
    dy = y2 - y1;
    if (abs (dx) > abs (dy))
      step = abs (dx);
    else
      step = abs (dy);
    xinc = dx / step;
    yinc = dy / step;
    x = x1;
    y = y1;
    putpixel (x, y, c);
    for (k = 1; k <= step; k++)
      {
        x = x + xinc;
        y = y + yinc;
        putpixel (x, y, c);
      }
  }

canonical_original_code:
static void draw(int x1, int y1, int x2, int y2, int c)
  {
    float x; float y; float xinc; float yinc; float dx; float dy;
    int k;
    int step;
    dx = x2 - x1;
    dy = y2 - y1;
    if (abs (dx) > abs (dy))
      step = abs (dx);
    else
      step = abs (dy);
    xinc = dx / step;
    yinc = dy / step;
    x = x1;
    y = y1;
    putpixel (x, y, c);
    for (k = 1; k <= step; k++)
      {
        x = x + xinc;
        y = y + yinc;
        putpixel (x, y, c);
      }
  }

variable_types: x float

variable_types: y float

variable_types: xinc float

variable_types: yinc float

variable_types: dx float

variable_types: dy float

variable_types: k int

variable_types: step int


print

original_code:
static void print (void)
  {
    int x, y;
    int minx = 1024, miny = 1024, maxx = -1, maxy = -1;
    for (y = 0; y < 1024; y ++)
      for (x = 0; x < 1024; x ++)
        if (image [y] [x] != ' ')
          {
            if (x < minx)
              minx = x;
            if (y < miny)
              miny = y;
            if (x > maxx)
              maxx = x;
            if (y > maxy)
              maxy = y;
          }
    for (y = maxy; y >= miny; y --)
      {
        printf ("; ");
        for (x = minx; x <= maxx; x ++)
          printf ("%c", image [y] [x]);
        printf ("\n");
      }
  }

canonical_original_code:
static void print()
  {
    int x; int y;
    int minx = 1024; int miny = 1024; int maxx = -1; int maxy = -1;
    for (y = 0; y < 1024; y ++)
      for (x = 0; x < 1024; x ++)
        if (image [y] [x] != ' ')
          {
            if (x < minx)
              minx = x;
            if (y < miny)
              miny = y;
            if (x > maxx)
              maxx = x;
            if (y > maxy)
              maxy = y;
          }
    for (y = maxy; y >= miny; y --)
      {
        printf ("; ");
        for (x = minx; x <= maxx; x ++)
          printf ("%c", image [y] [x]);
        printf ("\n");
      }
  }

variable_types: minx int

variable_types: miny int

variable_types: maxx int

variable_types: maxy int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // ebp
int v5; // ebx
char *v6; // rax
unsigned int v8; // [rsp+4h] [rbp-234h]
int v9; // [rsp+8h] [rbp-230h]
int v10; // [rsp+Ch] [rbp-22Ch]
char v11[256]; // [rsp+10h] [rbp-228h] BYREF
char s[296]; // [rsp+110h] [rbp-128h] BYREF

v3 = 1;
if ( argc == 2 )
v3 = atoi(argv[1]);
printf("%d\n", (unsigned int)v3);
clear();
v4 = 0;
v5 = 0;
while ( !feof(_bss_start) )
{
fgets(s, 256, _bss_start);
v6 = strstr(s, "stat");
if ( v6 )
*(_DWORD *)v6 = 840966176;
__isoc99_sscanf();
if ( !strcmp(v11, "vsdraw") || !strcmp(v11, "vldraw") )
{
if ( v8 )
draw(v5 / v3, v4 / v3, (v10 + v5) / v3, (v9 + v4) / v3, v8);
v5 += v10;
v4 += v9;
}
}
print();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // ebp
int v5; // ebx
char *v6; // rax
unsigned int v8; // [rsp+4h] [rbp-234h]
int v9; // [rsp+8h] [rbp-230h]
int v10; // [rsp+Ch] [rbp-22Ch]
char v11[256]; // [rsp+10h] [rbp-228h] BYREF
char s[296]; // [rsp+110h] [rbp-128h] BYREF

v3 = 1;
if ( argc == 2 )
v3 = atoi(argv[1]);
printf("%d\n", (unsigned int)v3);
func7();
v4 = 0;
v5 = 0;
while ( !feof(_bss_start) )
{
fgets(s, 256, _bss_start);
v6 = strstr(s, "stat");
if ( v6 )
*(_DWORD *)v6 = 840966176;
__isoc99_sscanf();
if ( !strcmp(v11, "vsdraw") || !strcmp(v11, "vldraw") )
{
if ( v8 )
func9(v5 / v3, v4 / v3, (v10 + v5) / v3, (v9 + v4) / v3, v8);
v5 += v10;
v4 += v9;
}
}
func10();
return 0;
}

original_code:
int main (int argc, char * argv [])
  {
    char buf [256], cmd [256];
    int p1, p2, p3;
    int x = 0, y = 0;
    int scale = 1;
    char * cp;
    if (argc == 2)
      scale = atoi (argv [1]);
    printf ("%d\n", scale);
    clear ();
    while (! feof (stdin))
      {
        fgets (buf, 256, stdin);
        if ((cp = strstr (buf, "stat")))
          strncpy (cp, "   2", 4);
        sscanf (buf, "%s%d,%d,%d", cmd, & p1, & p2, &p3);
        if (strcmp (cmd, "vsdraw") == 0 || strcmp (cmd, "vldraw") == 0)
          {
            if (p3)
              draw (x/scale, y/scale, (x + p1)/scale, (y + p2)/scale, p3);
            x = x + p1;
            y = y + p2;
          }
      }
    print ();
    return 0;
  }

canonical_original_code:
int main(int argc, char *argv[])
  {
    char buf[256]; char cmd[256];
    int p1; int p2; int p3;
    int x = 0; int y = 0;
    int scale = 1;
    char *cp;
    if (argc == 2)
      scale = atoi (argv [1]);
    printf ("%d\n", scale);
    clear ();
    while (! feof (stdin))
      {
        fgets (buf, 256, stdin);
        if ((cp = strstr (buf, "stat")))
          strncpy (cp, "   2", 4);
        sscanf (buf, "%s%d,%d,%d", cmd, & p1, & p2, &p3);
        if (strcmp (cmd, "vsdraw") == 0 || strcmp (cmd, "vldraw") == 0)
          {
            if (p3)
              draw (x/scale, y/scale, (x + p1)/scale, (y + p2)/scale, p3);
            x = x + p1;
            y = y + p2;
          }
      }
    print ();
    return 0;
  }

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
buf
char [256]

variable_types:
cmd
char [256]

variable_types: x int

variable_types: scale int

variable_types:
cp
char *


repo: charlesgarvin/projecteuler

decompiled_code:
__int64 __fastcall fib_recur(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = a1;
if ( a1 && a1 != 1 )
{
v2 = fib_recur(a1 - 1);
v1 = v2 + fib_recur(a1 - 2);
}
return v1;
}

original_code:
static unsigned long long fib_recur(unsigned long long n)
{
  if (n == 0) {
    return 0;
  }
  else if (n == 1) {
    return 1;
  }
  return fib_recur(n - 1) + fib_recur(n - 2);
}

variable_types:
n
unsigned long long

return_type: unsigned long long


recursive

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

if ( !a1 )
return 0LL;
v1 = 0LL;
v2 = 0LL;
do
{
v3 = fib_recur(v2);
if ( (v3 & 1) == 0 )
v1 += v3;
++v2;
}
while ( a1 > v3 );
return v1;
}

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

if ( !a1 )
return 0LL;
v1 = 0LL;
v2 = 0LL;
do
{
v3 = func7(v2);
if ( (v3 & 1) == 0 )
v1 += v3;
++v2;
}
while ( a1 > v3 );
return v1;
}

original_code:
static unsigned long long recursive(unsigned long long max)
{
  unsigned long long i = 0;
  unsigned long long n = 0;
  unsigned long long sum = 0;
  for (i = 0; n < max; i++) {
    n = fib_recur(i);
    if (n % 2 == 0) {
      sum += n;
    }
  }
  return sum;
}

variable_types:
max
unsigned long long

variable_types:
i
unsigned long long

variable_types:
sum
unsigned long long


generative

decompiled_code:
__int64 __fastcall generative(unsigned __int64 a1)
{
__int64 v1; // rsi
unsigned __int64 v2; // rax
__int64 v3; // rcx
unsigned __int64 v4; // rdx
__int64 v5; // rsi

v1 = 1LL;
v2 = 1LL;
v3 = 0LL;
if ( !a1 )
return 0LL;
while ( a1 > v2 )
{
v4 = v2 + v1;
v5 = v3 + v2 + v1;
if ( (v4 & 1) == 0 )
v3 = v5;
v1 = v2;
v2 = v4;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int64 a1)
{
__int64 v1; // rsi
unsigned __int64 v2; // rax
__int64 v3; // rcx
unsigned __int64 v4; // rdx
__int64 v5; // rsi

v1 = 1LL;
v2 = 1LL;
v3 = 0LL;
if ( !a1 )
return 0LL;
while ( a1 > v2 )
{
v4 = v2 + v1;
v5 = v3 + v2 + v1;
if ( (v4 & 1) == 0 )
v3 = v5;
v1 = v2;
v2 = v4;
}
return v3;
}

original_code:
static unsigned long long generative(unsigned long long max)
{
  unsigned long long n = 0;
  unsigned long long prev_1 = 1;
  unsigned long long prev_2 = 0;
  unsigned long long sum = 0;
  while (n < max) {
    n = prev_1 + prev_2;
    if (n % 2 == 0) {
      sum += n;
    }
    prev_2 = prev_1;
    prev_1 = n;
  }
  return sum;
}


decompiled_code:
int __fastcall print_usage(const char *a1)
{
__int64 *v1; // rbx
const char *v2; // rdi
int result; // eax
__int64 v4[10]; // [rsp+8h] [rbp-50h] BYREF

v4[0] = (__int64)"";
v4[1] = (__int64)" method is the computation method you want to use. Valid choices are";
v4[2] = (__int64)" recursive, generative. The default is recursive.";
v4[3] = (__int64)"";
v4[4] = (__int64)" max is an integer representing the maximum number (i.e. sum of all";
v4[5] = (__int64)" even Fibonacci numbers below <max>). The value must be between 2";
v4[6] = (__int64)" and 1000000000. The default is 4000000.";
v4[7] = 0LL;
printf("%s", a1);
v1 = v4;
v2 = " [--method=<method>] [--max=<max>]";
do
{
result = puts(v2);
v2 = (const char *)*v1++;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
__int64 *v1; // rbx
const char *v2; // rdi
int result; // eax
__int64 v4[10]; // [rsp+8h] [rbp-50h] BYREF

v4[0] = (__int64)"";
v4[1] = (__int64)" method is the computation method you want to use. Valid choices are";
v4[2] = (__int64)" recursive, generative. The default is recursive.";
v4[3] = (__int64)"";
v4[4] = (__int64)" max is an integer representing the maximum number (i.e. sum of all";
v4[5] = (__int64)" even Fibonacci numbers below <max>). The value must be between 2";
v4[6] = (__int64)" and 1000000000. The default is 4000000.";
v4[7] = 0LL;
printf("%s", a1);
v1 = v4;
v2 = " [--method=<method>] [--max=<max>]";
do
{
result = puts(v2);
v2 = (const char *)*v1++;
}
while ( v2 );
return result;
}

original_code:
static void print_usage(const char *program)
{
  int i;
  const char *usage[] = {
    " [--method=<method>] [--max=<max>]",
    "",
    "    method is the computation method you want to use.  Valid choices are",
    "    recursive, generative.  The default is recursive.",
    "",
    "    max is an integer representing the maximum number (i.e. sum of all",
    "    even Fibonacci numbers below <max>).  The value must be between 2",
    "    and 1000000000.  The default is 4000000.",
    ((void *)0)
  };
  printf("%s", program);
  for (i = 0; usage[i]; i++) {
    printf("%s\n", usage[i]);
  }
}

canonical_original_code:
static void print_usage(char *program)
{
  int i;
  char *usage[] = {
    " [--method=<method>] [--max=<max>]",
    "",
    "    method is the computation method you want to use.  Valid choices are",
    "    recursive, generative.  The default is recursive.",
    "",
    "    max is an integer representing the maximum number (i.e. sum of all",
    "    even Fibonacci numbers below <max>).  The value must be between 2",
    "    and 1000000000.  The default is 4000000.",
    ((void *)0)
  };
  printf("%s", program);
  for (i = 0; usage[i]; i++) {
    printf("%s\n", usage[i]);
  }
}

variable_types:
program
char *

variable_types: i int

variable_types:
usage
char *[]


decompiled_code:
__int64 __fastcall parse_opts(int argc, char **argv, __int64 (__fastcall **a3)(unsigned __int64 a1))
{
unsigned int v4; // er12
__int64 (__fastcall *v5)(unsigned __int64); // rax
int *v6; // r14
unsigned __int64 v7; // rax
int v8; // eax
char *v10; // [rsp+0h] [rbp-38h] BYREF
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

*a3 = recursive;
a3[1] = (__int64 (__fastcall *)(unsigned __int64))4000000;
v4 = 0;
while ( 1 )
{
v8 = getopt_long(argc, argv, "", &long_opts_0, longind);
if ( v8 < 0 )
break;
if ( v8 == 1 )
{
v5 = method_to_func(_bss_start);
*a3 = v5;
if ( !v5 )
{
puts("Invalid method");
v4 = -1;
}
}
else if ( v8 == 2 )
{
v6 = __errno_location();
*v6 = 0;
v7 = strtoull(_bss_start, &v10, 10);
if ( *v6 || *v10 || v7 - 2 > 0x3B9AC9FE )
{
puts("Invalid maximum number");
v4 = -1;
}
else
{
a3[1] = (__int64 (__fastcall *)(unsigned __int64))v7;
}
}
else
{
v4 = -1;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func11(int argc, char **argv, __int64 (__fastcall **a3)(unsigned __int64 a1))
{
unsigned int v4; // er12
__int64 (__fastcall *v5)(unsigned __int64); // rax
int *v6; // r14
unsigned __int64 v7; // rax
int v8; // eax
char *v10; // [rsp+0h] [rbp-38h] BYREF
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

*a3 = recursive;
a3[1] = (__int64 (__fastcall *)(unsigned __int64))4000000;
v4 = 0;
while ( 1 )
{
v8 = getopt_long(argc, argv, "", &long_opts_0, longind);
if ( v8 < 0 )
break;
if ( v8 == 1 )
{
v5 = method_to_func(_bss_start);
*a3 = v5;
if ( !v5 )
{
puts("Invalid method");
v4 = -1;
}
}
else if ( v8 == 2 )
{
v6 = __errno_location();
*v6 = 0;
v7 = strtoull(_bss_start, &v10, 10);
if ( *v6 || *v10 || v7 - 2 > 0x3B9AC9FE )
{
puts("Invalid maximum number");
v4 = -1;
}
else
{
a3[1] = (__int64 (__fastcall *)(unsigned __int64))v7;
}
}
else
{
v4 = -1;
}
}
return v4;
}

original_code:
static int parse_opts(int argc, char **argv, struct my_opts *opts)
{
  int rv = 0;
  int opt;
  int opt_ind;
  static struct option long_opts[] = {
    {"method", 1, 0, 1},
    {"max", 1, 0, 2},
    {0, 0, 0, 0}
  };
  long val;
  char *endp;
  opts->func = recursive;
  opts->max = 4000000;
  while ((opt = getopt_long(argc, argv, "", long_opts, &opt_ind)) >= 0) {
    switch (opt) {
      case 1:
        opts->func = method_to_func(optarg);
        if (opts->func == ((void *)0)) {
          printf("Invalid method\n");
          rv = -1;
        }
        break;
      case 2:
        (*__errno_location ()) = 0;
        val = strtoull(optarg, &endp, 10);
        if ((*__errno_location ()) || *endp || val < 2 || val > 1000000000ULL) {
          printf("Invalid maximum number\n");
          rv = -1;
        } else {
          opts->max = val;
        }
        break;
      default:
        rv = -1;
        break;
    }
  }
  return rv;
}

variable_types:
argv
char **

variable_types: struct my_opts { unsigned long long (*func)(unsigned long long); unsigned long long max; } *

variable_types: rv int

variable_types: opt int

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

variable_types: val long

variable_types:
endp
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
int result; // eax
__int64 (__fastcall *v5[3])(unsigned __int64); // [rsp+0h] [rbp-18h] BYREF

if ( (int)parse_opts(argc, (char **)argv, v5) < 0 )
{
print_usage(*argv);
result = 1;
}
else
{
v3 = v5[0](v5[1]);
printf("sum = %llu\n", v3);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
int result; // eax
__int64 (__fastcall *v5[3])(unsigned __int64); // [rsp+0h] [rbp-18h] BYREF

if ( (int)func11(argc, (char **)argv, v5) < 0 )
{
func10(*argv);
result = 1;
}
else
{
v3 = v5[0](v5[1]);
printf("sum = %llu\n", v3);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
  unsigned long long sum = 0;
  struct my_opts opts;
  if (parse_opts(argc, argv, &opts) < 0) {
    print_usage(argv[0]);
    return 1;
  }
  sum = opts.func(opts.max);
  printf("sum = %llu\n", sum);
  return 0;
}

variable_types: argc int

variable_types: struct my_opts { unsigned long long (*func)(unsigned long long); unsigned long long max; }


decompiled_code:
__int64 __fastcall brute_force(unsigned __int64 a1)
{
__int64 v1; // rsi
unsigned __int64 i; // rcx

if ( a1 <= 1 )
return 0LL;
v1 = 0LL;
for ( i = 1LL; i != a1; ++i )
{
if ( i == i / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)i) >> 64) & 0xFFFFFFFFFFFFFFFELL)
|| i == i / 5 + (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)i) >> 64) & 0xFFFFFFFFFFFFFFFCLL) )
{
v1 += i;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int64 a1)
{
__int64 v1; // rsi
unsigned __int64 i; // rcx

if ( a1 <= 1 )
return 0LL;
v1 = 0LL;
for ( i = 1LL; i != a1; ++i )
{
if ( i == i / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)i) >> 64) & 0xFFFFFFFFFFFFFFFELL)
|| i == i / 5 + (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)i) >> 64) & 0xFFFFFFFFFFFFFFFCLL) )
{
v1 += i;
}
}
return v1;
}

original_code:
static unsigned long long brute_force(unsigned long long max)
{
  unsigned long long i;
  unsigned long long sum = 0;
  for (i = 1; i < max; i++) {
    if (i % 3 == 0 || i % 5 == 0) {
      sum += i;
    }
  }
  return sum;
}


generate

original_code:
static unsigned long long generate(unsigned long long max)
{
  unsigned long long sum = 0;
  unsigned long long three = 3;
  unsigned long long five = 5;
  unsigned long long prev_three = 0;
  unsigned long long prev_five = 0;
  while (three < max || five < max) {
    if (three < max && three != five && three != prev_three) {
      sum += three;
    }
    if (five < max && three != five && five != prev_five) {
      sum += five;
    }
    prev_three = three;
    prev_five = five;
    if (three < five) {
      three += 3;
    } else if (five < three) {
      five += 5;
    } else {
      three += 3;
      five += 5;
    }
  }
  return sum;
}

variable_types:
three
unsigned long long

variable_types:
five
unsigned long long


decompiled_code:
unsigned __int64 __fastcall mult_sum(unsigned __int64 a1, unsigned __int64 a2)
{
return ((a2 / a1 + 1) * a2 / a1 * a1) >> 1;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(unsigned __int64 a1, unsigned __int64 a2)
{
return ((a2 / a1 + 1) * a2 / a1 * a1) >> 1;
}

original_code:
static unsigned long long mult_sum(unsigned long long mult, unsigned long long max)
{
  unsigned long long div;
  div = max / mult;
  return mult * (div * div + div) / 2;
}

variable_types:
mult
unsigned long long

variable_types:
max
unsigned long long

variable_types:
div
unsigned long long


decompiled_code:
unsigned __int64 __fastcall const_time(__int64 a1)
{
unsigned __int64 v1; // rbx
unsigned __int64 v2; // r12

v1 = mult_sum(3uLL, a1 - 1);
v2 = mult_sum(5uLL, a1 - 1);
return v1 + v2 - mult_sum(0xFuLL, a1 - 1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(__int64 a1)
{
unsigned __int64 v1; // rbx
unsigned __int64 v2; // r12

v1 = func9(3uLL, a1 - 1);
v2 = func9(5uLL, a1 - 1);
return v1 + v2 - func9(0xFuLL, a1 - 1);
}

original_code:
static unsigned long long const_time(unsigned long long max)
{
  unsigned long long three_sum;
  unsigned long long five_sum;
  unsigned long long common_sum;
  max -= 1;
  three_sum = mult_sum(3, max);
  five_sum = mult_sum(5, max);
  common_sum = mult_sum(3 * 5, max);
  return three_sum + five_sum - common_sum;
}


decompiled_code:
int __fastcall print_usage(const char *a1)
{
__int64 *v1; // rbx
const char *v2; // rdi
int result; // eax
__int64 v4[10]; // [rsp+8h] [rbp-50h] BYREF

v4[0] = (__int64)"";
v4[1] = (__int64)" <method> is the computation method you wish to use,";
v4[2] = (__int64)" brute_force, generate or const_time. The default is brute_force.";
v4[3] = (__int64)"";
v4[4] = (__int64)" max is an integer representing the maximum number (i.e. sum of all";
v4[5] = (__int64)" multiples below <max>). The value must be between 1 and 1000000000.";
v4[6] = (__int64)" The default is 1000";
v4[7] = 0LL;
printf("%s", a1);
v1 = v4;
v2 = " [--method=<method>] [--max=<max>]";
do
{
result = puts(v2);
v2 = (const char *)*v1++;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
int __fastcall func11(const char *a1)
{
__int64 *v1; // rbx
const char *v2; // rdi
int result; // eax
__int64 v4[10]; // [rsp+8h] [rbp-50h] BYREF

v4[0] = (__int64)"";
v4[1] = (__int64)" <method> is the computation method you wish to use,";
v4[2] = (__int64)" brute_force, generate or const_time. The default is brute_force.";
v4[3] = (__int64)"";
v4[4] = (__int64)" max is an integer representing the maximum number (i.e. sum of all";
v4[5] = (__int64)" multiples below <max>). The value must be between 1 and 1000000000.";
v4[6] = (__int64)" The default is 1000";
v4[7] = 0LL;
printf("%s", a1);
v1 = v4;
v2 = " [--method=<method>] [--max=<max>]";
do
{
result = puts(v2);
v2 = (const char *)*v1++;
}
while ( v2 );
return result;
}

original_code:
static void print_usage(const char *program)
{
  int i;
  const char *usage[] = {
    " [--method=<method>] [--max=<max>]",
    "",
    "    <method> is the computation method you wish to use,",
    "    brute_force, generate or const_time.  The default is brute_force.",
    "",
    "    max is an integer representing the maximum number (i.e. sum of all",
    "    multiples below <max>).  The value must be between 1 and 1000000000.",
    "    The default is 1000",
    ((void *)0)
  };
  printf("%s", program);
  for (i = 0; usage[i]; i++) {
    printf("%s\n", usage[i]);
  }
}

canonical_original_code:
static void print_usage(char *program)
{
  int i;
  char *usage[] = {
    " [--method=<method>] [--max=<max>]",
    "",
    "    <method> is the computation method you wish to use,",
    "    brute_force, generate or const_time.  The default is brute_force.",
    "",
    "    max is an integer representing the maximum number (i.e. sum of all",
    "    multiples below <max>).  The value must be between 1 and 1000000000.",
    "    The default is 1000",
    ((void *)0)
  };
  printf("%s", program);
  for (i = 0; usage[i]; i++) {
    printf("%s\n", usage[i]);
  }
}

variable_types:
program
char *


decompiled_code:
__int64 __fastcall parse_opts(int argc, char **argv, __int64 (__fastcall **a3)(unsigned __int64 a1))
{
unsigned int v4; // er12
__int64 (__fastcall *v5)(unsigned __int64); // rax
int *v6; // r14
unsigned __int64 v7; // rax
int v8; // eax
char *v10; // [rsp+0h] [rbp-38h] BYREF
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

*a3 = brute_force;
a3[1] = (__int64 (__fastcall *)(unsigned __int64))1000;
v4 = 0;
while ( 1 )
{
v8 = getopt_long(argc, argv, "", &long_opts_0, longind);
if ( v8 < 0 )
break;
if ( v8 == 1 )
{
v5 = (__int64 (__fastcall *)(unsigned __int64))method_to_func(_bss_start);
*a3 = v5;
if ( !v5 )
{
puts("Invalid method name");
v4 = -1;
}
}
else if ( v8 == 2 )
{
v6 = __errno_location();
*v6 = 0;
v7 = strtoull(_bss_start, &v10, 10);
if ( *v6 || *v10 || v7 - 1 > 0x3B9AC9FF )
{
puts("Invalid maximum number");
v4 = -1;
}
else
{
a3[1] = (__int64 (__fastcall *)(unsigned __int64))v7;
}
}
else
{
v4 = -1;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func13(int argc, char **argv, __int64 (__fastcall **a3)(unsigned __int64 a1))
{
unsigned int v4; // er12
__int64 (__fastcall *v5)(unsigned __int64); // rax
int *v6; // r14
unsigned __int64 v7; // rax
int v8; // eax
char *v10; // [rsp+0h] [rbp-38h] BYREF
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

*a3 = brute_force;
a3[1] = (__int64 (__fastcall *)(unsigned __int64))1000;
v4 = 0;
while ( 1 )
{
v8 = getopt_long(argc, argv, "", &long_opts_0, longind);
if ( v8 < 0 )
break;
if ( v8 == 1 )
{
v5 = (__int64 (__fastcall *)(unsigned __int64))func12(_bss_start);
*a3 = v5;
if ( !v5 )
{
puts("Invalid method name");
v4 = -1;
}
}
else if ( v8 == 2 )
{
v6 = __errno_location();
*v6 = 0;
v7 = strtoull(_bss_start, &v10, 10);
if ( *v6 || *v10 || v7 - 1 > 0x3B9AC9FF )
{
puts("Invalid maximum number");
v4 = -1;
}
else
{
a3[1] = (__int64 (__fastcall *)(unsigned __int64))v7;
}
}
else
{
v4 = -1;
}
}
return v4;
}

original_code:
static int parse_opts(int argc, char **argv, struct my_opts *opts)
{
  int rv = 0;
  int opt;
  int opt_ind;
  static struct option long_opts[] = {
    {"method", 1, 0, 1},
    {"max", 1, 0, 2},
    {0, 0, 0, 0}
  };
  long val;
  char *endp;
  opts->func = brute_force;
  opts->max = 1000;
  while ((opt = getopt_long(argc, argv, "", long_opts, &opt_ind)) >= 0) {
    switch (opt) {
      case 1:
        opts->func = method_to_func(optarg);
        if (opts->func == ((void *)0)) {
          printf("Invalid method name\n");
          rv = -1;
        }
        break;
      case 2:
        (*__errno_location ()) = 0;
        val = strtoull(optarg, &endp, 10);
        if ((*__errno_location ()) || *endp || val < 1 || val > 1000000000ULL) {
          printf("Invalid maximum number\n");
          rv = -1;
        } else {
          opts->max = val;
        }
        break;
      default:
        rv = -1;
        break;
    }
  }
  return rv;
}

user_defined_types: struct my_opts { unsigned long long (*func)(unsigned long long); unsigned long long max; }

user_defined_types: struct option { char *name; int has_arg; int *flag; int val; }


main

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
int result; // eax
__int64 (__fastcall *v5[3])(unsigned __int64); // [rsp+0h] [rbp-18h] BYREF

if ( (int)func13(argc, (char **)argv, v5) < 0 )
{
func11(*argv);
result = 1;
}
else
{
v3 = v5[0](v5[1]);
printf("sum = %llu\n", v3);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
  unsigned long long sum;
  struct my_opts opts;
  if (parse_opts(argc, argv, &opts) < 0) {
    print_usage(argv[0]);
    return 1;
  }
  sum = opts.func(opts.max);
  printf("sum = %llu\n", sum);
  return 0;
}

variable_types:
sum
unsigned long long


ignore

decompiled_code:
void ignore()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void ignore(int signal)
{
}

canonical_original_code:
void ignore(int signal)
{
}

variable_types: signal int