philosopher

decompiled_code:
__int64 __fastcall philosopher(void *a1)
{
__int64 v1; // r13
__int64 v2; // rax
pthread_mutex_t *v3; // rbx
pthread_mutex_t *v4; // r12
pthread_mutex_t *v5; // rax
struct timeval v7; // [rsp+0h] [rbp-38h] BYREF

gettimeofday(&v7, 0LL);
printf(
"philosopher started eating left_fork:%i right_fork:%i\n",
*((unsigned int *)a1 + 1),
*((unsigned int *)a1 + 2));
v1 = 0LL;
while ( (double)*(int *)a1 >= timediff(*((_QWORD *)a1 + 3), *((_QWORD *)a1 + 4), v7.tv_sec, v7.tv_usec) )
{
v2 = *((_QWORD *)a1 + 2);
v3 = (pthread_mutex_t *)(v2 + 40LL * *((int *)a1 + 1));
v4 = (pthread_mutex_t *)(v2 + 40LL * *((int *)a1 + 2));
if ( (random() & 1) != 0 )
{
v5 = v3;
v3 = v4;
v4 = v5;
}
while ( pthread_mutex_lock(v3) )
;
if ( !pthread_mutex_trylock(v4) )
{
++v1;
pthread_mutex_unlock(v4);
}
pthread_mutex_unlock(v3);
gettimeofday(&v7, 0LL);
}
printf("philosopher %i stopped eating\n", *((unsigned int *)a1 + 1));
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(void *a1)
{
__int64 v1; // r13
__int64 v2; // rax
pthread_mutex_t *v3; // rbx
pthread_mutex_t *v4; // r12
pthread_mutex_t *v5; // rax
struct timeval v7; // [rsp+0h] [rbp-38h] BYREF

gettimeofday(&v7, 0LL);
printf(
"philosopher started eating left_fork:%i right_fork:%i\n",
*((unsigned int *)a1 + 1),
*((unsigned int *)a1 + 2));
v1 = 0LL;
while ( (double)*(int *)a1 >= func7(*((_QWORD *)a1 + 3), *((_QWORD *)a1 + 4), v7.tv_sec, v7.tv_usec) )
{
v2 = *((_QWORD *)a1 + 2);
v3 = (pthread_mutex_t *)(v2 + 40LL * *((int *)a1 + 1));
v4 = (pthread_mutex_t *)(v2 + 40LL * *((int *)a1 + 2));
if ( (random() & 1) != 0 )
{
v5 = v3;
v3 = v4;
v4 = v5;
}
while ( pthread_mutex_lock(v3) )
;
if ( !pthread_mutex_trylock(v4) )
{
++v1;
pthread_mutex_unlock(v4);
}
pthread_mutex_unlock(v3);
gettimeofday(&v7, 0LL);
}
printf("philosopher %i stopped eating\n", *((unsigned int *)a1 + 1));
return v1;
}

original_code:
void * philosopher(void * args)
{
  philosopher_arg_t * philosopher_args = (philosopher_arg_t *) args;
  int64_t counter = 0;
  timeval current;
  gettimeofday(&current, ((void *)0));
  printf("philosopher started eating left_fork:%i right_fork:%i\n",
      philosopher_args->left_fork, philosopher_args->right_fork);
  while (timediff(philosopher_args->start, current) <= philosopher_args->run_time)
  {
    pthread_mutex_t * first_fork = &philosopher_args->forks[philosopher_args->left_fork];
    pthread_mutex_t * second_fork = &philosopher_args->forks[philosopher_args->right_fork];
    long rand = random();
    if (rand % 2)
    {
      pthread_mutex_t * memory = first_fork;
      first_fork = second_fork;
      second_fork = memory;
    }
    while( pthread_mutex_lock(first_fork) );
    if (0 == pthread_mutex_trylock(second_fork))
    {
      ++counter;
      pthread_mutex_unlock(second_fork);
    }
    pthread_mutex_unlock(first_fork);
    gettimeofday(&current, ((void *)0));
  }
  printf("philosopher %i stopped eating\n", philosopher_args->left_fork);
  return (void *) counter;
}

canonical_original_code:
void *philosopher(void *args)
{
  struct philosopher_arg_t *philosopher_args = (philosopher_arg_t *) args;
  long counter = 0;
  struct timeval current;
  gettimeofday(&current, ((void *)0));
  printf("philosopher started eating left_fork:%i right_fork:%i\n",
      philosopher_args->left_fork, philosopher_args->right_fork);
  while (timediff(philosopher_args->start, current) <= philosopher_args->run_time)
  {
    union pthread_mutex_t *first_fork = &philosopher_args->forks[philosopher_args->left_fork];
    union pthread_mutex_t *second_fork = &philosopher_args->forks[philosopher_args->right_fork];
    long rand = random();
    if (rand % 2)
    {
      union pthread_mutex_t *memory = first_fork;
      first_fork = second_fork;
      second_fork = memory;
    }
    while( pthread_mutex_lock(first_fork) );
    if (0 == pthread_mutex_trylock(second_fork))
    {
      ++counter;
      pthread_mutex_unlock(second_fork);
    }
    pthread_mutex_unlock(first_fork);
    gettimeofday(&current, ((void *)0));
  }
  printf("philosopher %i stopped eating\n", philosopher_args->left_fork);
  return (void *) counter;
}

variable_types:
args
void *

variable_types: struct philosopher_arg_t { int run_time; int left_fork; int right_fork; 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; } *forks; struct timeval { long tv_sec; long tv_usec; } start; } *

variable_types: counter long

variable_types: rand long

return_type: void *

user_defined_types: struct philosopher_arg_t { int run_time; int left_fork; int right_fork; union pthread_mutex_t *forks; struct timeval start; }

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

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


main

canonical_name: main

repo: mschneider/parprogassignments

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
size_t v4; // rbx
void **v5; // r14
pthread_mutex_t *v6; // r13
pthread_t *v7; // r15
pthread_mutex_t *v8; // rbx
int v9; // ebp
int v10; // ebp
pthread_t *v11; // r12
int v12; // ebx
char *v13; // rax
void **v14; // rbp
__int64 v15; // rbx
FILE *v16; // rbp
__int64 v17; // rbx
int result; // eax
struct timeval v19; // [rsp+0h] [rbp-58h] BYREF
int v20; // [rsp+18h] [rbp-40h] BYREF
unsigned int v21; // [rsp+1Ch] [rbp-3Ch]

if ( argc == 3 )
{
read_args(argv, &v20, envp);
printf("running with %i philosopher for %i seconds\n", (unsigned int)v20, v21);
v3 = v20;
v4 = v20;
v5 = (void **)calloc(v20, 8uLL);
v6 = (pthread_mutex_t *)calloc(v4, 0x28uLL);
v7 = (pthread_t *)calloc(v4, 8uLL);
if ( v3 <= 0 )
{
LABEL_6:
gettimeofday(&v19, 0LL);
v10 = v20;
if ( v20 > 0 )
{
v11 = v7;
v12 = 0;
do
{
v13 = (char *)malloc(0x28uLL);
*(_DWORD *)v13 = v21;
*((_DWORD *)v13 + 1) = v12++;
*((_DWORD *)v13 + 2) = v12 % v10;
*((_QWORD *)v13 + 2) = v6;
*(__m128i *)(v13 + 24) = _mm_loadu_si128((const __m128i *)&v19);
pthread_create(v11, 0LL, (void *(*)(void *))philosopher, v13);
v10 = v20;
++v11;
}
while ( v12 < v20 );
if ( v20 > 0 )
{
v14 = v5;
v15 = 0LL;
do
pthread_join(v7[v15++], v14++);
while ( v20 > (int)v15 );
}
}
v16 = fopen("output.txt", "w");
v17 = 1LL;
result = 0;
if ( v20 > 0 )
{
while ( 1 )
{
fprintf(v16, "%lli", v5[v17 - 1]);
if ( v20 <= (int)v17 )
break;
if ( (int)v17 > 0 )
fputc(59, v16);
++v17;
}
result = 0;
}
}
else
{
v8 = v6;
v9 = 0;
while ( !pthread_mutex_init(v8, 0LL) )
{
++v9;
++v8;
if ( v20 <= v9 )
goto LABEL_6;
}
puts("could not initialize mutex");
result = -1;
}
}
else
{
puts("wrong arguments. usage: dinner <threads> <run_time>");
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
size_t v4; // rbx
void **v5; // r14
pthread_mutex_t *v6; // r13
pthread_t *v7; // r15
pthread_mutex_t *v8; // rbx
int v9; // ebp
int v10; // ebp
pthread_t *v11; // r12
int v12; // ebx
char *v13; // rax
void **v14; // rbp
__int64 v15; // rbx
FILE *v16; // rbp
__int64 v17; // rbx
int result; // eax
struct timeval v19; // [rsp+0h] [rbp-58h] BYREF
int v20; // [rsp+18h] [rbp-40h] BYREF
unsigned int v21; // [rsp+1Ch] [rbp-3Ch]

if ( argc == 3 )
{
func9(argv, &v20, envp);
printf("running with %i philosopher for %i seconds\n", (unsigned int)v20, v21);
v3 = v20;
v4 = v20;
v5 = (void **)calloc(v20, 8uLL);
v6 = (pthread_mutex_t *)calloc(v4, 0x28uLL);
v7 = (pthread_t *)calloc(v4, 8uLL);
if ( v3 <= 0 )
{
LABEL_6:
gettimeofday(&v19, 0LL);
v10 = v20;
if ( v20 > 0 )
{
v11 = v7;
v12 = 0;
do
{
v13 = (char *)malloc(0x28uLL);
*(_DWORD *)v13 = v21;
*((_DWORD *)v13 + 1) = v12++;
*((_DWORD *)v13 + 2) = v12 % v10;
*((_QWORD *)v13 + 2) = v6;
*(__m128i *)(v13 + 24) = _mm_loadu_si128((const __m128i *)&v19);
pthread_create(v11, 0LL, (void *(*)(void *))philosopher, v13);
v10 = v20;
++v11;
}
while ( v12 < v20 );
if ( v20 > 0 )
{
v14 = v5;
v15 = 0LL;
do
pthread_join(v7[v15++], v14++);
while ( v20 > (int)v15 );
}
}
v16 = fopen("output.txt", "w");
v17 = 1LL;
result = 0;
if ( v20 > 0 )
{
while ( 1 )
{
fprintf(v16, "%lli", v5[v17 - 1]);
if ( v20 <= (int)v17 )
break;
if ( (int)v17 > 0 )
fputc(59, v16);
++v17;
}
result = 0;
}
}
else
{
v8 = v6;
v9 = 0;
while ( !pthread_mutex_init(v8, 0LL) )
{
++v9;
++v8;
if ( v20 <= v9 )
goto LABEL_6;
}
puts("could not initialize mutex");
result = -1;
}
}
else
{
puts("wrong arguments. usage: dinner <threads> <run_time>");
result = -1;
}
return result;
}

original_code:
int main(int argc, const char** argv)
{
  int ret;
  arg_t args;
  read_args(argc, argv, &args);
  printf("args width:%i height:%i rounds:%i hotspots:%s selection:%s\n",
      args.width_field,
      args.height_field,
      args.n_rounds,
      args.hotspot_filename,
      argc >= 6 ? args.selection_filename : "empty");
  hotspot_vector_t hotspots;
  hotspot_vector_t coordinates;
  if (ret = read_hotspots(args.hotspot_filename, &hotspots))
  {
    printf("could not read hotspots from: %s (%d)", args.hotspot_filename, ret);
    return -1;
  }
  field_t field;
  init_field(args, &field);
  int current_round = 0;
  set_hotspots(&field, current_round, &hotspots);
  while (current_round++ < args.n_rounds)
  {
    simulate_round(&field);
    volatile field_value_t * temp = field.old_values;
    field.old_values = field.new_values;
    field.new_values = temp;
    set_hotspots(&field, current_round, &hotspots);
  }
  if (argc >= 6) {
    if (ret = read_coordinates(args.selection_filename, &coordinates))
    {
      printf("could not read coordinates from: %s (%d)", args.selection_filename, ret);
      return -1;
    }
    print_coordinate_values(&field, &coordinates);}
  else
  {
    print_field(&field);
  }
   return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int ret;
  struct arg_t args;
  read_args(argc, argv, &args);
  printf("args width:%i height:%i rounds:%i hotspots:%s selection:%s\n",
      args.width_field,
      args.height_field,
      args.n_rounds,
      args.hotspot_filename,
      argc >= 6 ? args.selection_filename : "empty");
  struct hotspot_vector hotspots;
  struct hotspot_vector coordinates;
  if (ret = read_hotspots(args.hotspot_filename, &hotspots))
  {
    printf("could not read hotspots from: %s (%d)", args.hotspot_filename, ret);
    return -1;
  }
  struct field field;
  init_field(args, &field);
  int current_round = 0;
  set_hotspots(&field, current_round, &hotspots);
  while (current_round++ < args.n_rounds)
  {
    simulate_round(&field);
    double *temp = field.old_values;
    field.old_values = field.new_values;
    field.new_values = temp;
    set_hotspots(&field, current_round, &hotspots);
  }
  if (argc >= 6) {
    if (ret = read_coordinates(args.selection_filename, &coordinates))
    {
      printf("could not read coordinates from: %s (%d)", args.selection_filename, ret);
      return -1;
    }
    print_coordinate_values(&field, &coordinates);}
  else
  {
    print_field(&field);
  }
   return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ret int

variable_types: struct arg_t { int width_field; int height_field; int n_rounds; int act_round; char *hotspot_filename; char *selection_filename; }

variable_types: struct hotspot_vector { struct hotspot { int x; int y; int start; int end; } *elems; unsigned long count; }

variable_types: struct field { double *old_values; double *new_values; int height; int width; int n_threads; int rows_per_thread; 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; } *start_conditions; union pthread_mutex_t *stop_conditions; unsigned long *threads; struct thread_arg_t { int id; void *field; } *thread_args; }

variable_types:
temp
double *

return_type: int

user_defined_types: struct hotspot_vector { struct hotspot *elems; unsigned long count; }

user_defined_types: struct field { double *old_values; double *new_values; int height; int width; int n_threads; int rows_per_thread; union pthread_mutex_t *start_conditions; union pthread_mutex_t *stop_conditions; unsigned long *threads; struct thread_arg_t *thread_args; }


decompiled_code:
unsigned __int64 __fastcall time_less(__int64 a1, __int64 a2)
{
return (unsigned __int64)(a1 - a2) >> 63;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, __int64 a2)
{
return (unsigned __int64)(a1 - a2) >> 63;
}

original_code:
__attribute__ ((unused))
static int time_less(time_val x, time_val y) {
  return (stime_val)(x - y) < 0;
}

canonical_original_code:
static int time_less(unsigned long x, unsigned long y) {
  return (stime_val)(x - y) < 0;
}

variable_types:
x
unsigned long

variable_types:
y
unsigned long


decompiled_code:
__int64 __fastcall timer_compare(_QWORD *a1, _QWORD *a2)
{
return (*a1 - *a2) >> 63;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, _QWORD *a2)
{
return (*a1 - *a2) >> 63;
}

original_code:
static int timer_compare(void* px, void *py) {
  return (int64_t)(((struct timer_info*)px)->time -
                   ((struct timer_info*)py)->time) < 0;
}

canonical_original_code:
static int timer_compare(void *px, void *py) {
  return (long)(((struct timer_info *)px)->time -
                   ((struct timer_info *)py)->time) < 0;
}

variable_types:
px
void *

variable_types:
py
void *

user_defined_types: struct timer_info { unsigned long time; unsigned long heap_id; void (*timer_expired)(struct timer_info *, unsigned long); }


decompiled_code:
__int64 __fastcall timer_get_heap_id(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
static size_t timer_get_heap_id(void* x) {
  return ((struct timer_info*)x)->heap_id;
}

canonical_original_code:
static unsigned long timer_get_heap_id(void *x) {
  return ((struct timer_info *)x)->heap_id;
}

variable_types:
x
void *

return_type: unsigned long


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

canonical_decompiled_code:
void __fastcall func4(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
}

original_code:
static void timer_set_heap_id(void* x, size_t id) {
  ((struct timer_info*)x)->heap_id = id;
}

canonical_original_code:
static void timer_set_heap_id(void *x, unsigned long id) {
  ((struct timer_info *)x)->heap_id = id;
}

variable_types:
id
unsigned long

return_type: void


decompiled_code:
char *mstream_daemon_create()
{
char *v0; // rbx
int v1; // eax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

v0 = (char *)calloc(1uLL, 0x78uLL);
*((_DWORD *)v0 + 12) = epoll_create(10);
v1 = mstream_timerfd_create(1LL, 2048LL);
*((_DWORD *)v0 + 13) = v1;
event.data.u64 = 0LL;
event.events = 1;
epoll_ctl(*((_DWORD *)v0 + 12), 1, v1, &event);
pthread_mutex_init((pthread_mutex_t *)(v0 + 64), 0LL);
mstream_heap_init(v0, timer_compare, timer_get_heap_id, timer_set_heap_id);
return v0;
}

canonical_decompiled_code:
char *func9()
{
char *v0; // rbx
int v1; // eax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

v0 = (char *)calloc(1uLL, 0x78uLL);
*((_DWORD *)v0 + 12) = epoll_create(10);
v1 = mstream_timerfd_create(1LL, 2048LL);
*((_DWORD *)v0 + 13) = v1;
event.data.u64 = 0LL;
event.events = 1;
epoll_ctl(*((_DWORD *)v0 + 12), 1, v1, &event);
pthread_mutex_init((pthread_mutex_t *)(v0 + 64), 0LL);
mstream_heap_init(v0, timer_compare, timer_get_heap_id, timer_set_heap_id);
return v0;
}

original_code:
struct mdaemon* mstream_daemon_create() {
  struct mdaemon* daemon = (struct mdaemon*)calloc(1, sizeof(struct mdaemon));
  daemon->epollfd = epoll_create(10);
  daemon->timerfd = _mstream_timerfd_create(1, 04000);
  struct epoll_event ev;
  memset(&ev, 0, sizeof(ev));
  ev.events = EPOLLIN;
  epoll_ctl(daemon->epollfd, 1, daemon->timerfd, &ev);
  pthread_mutex_init(&daemon->lock, ((void *)0));
  _mstream_heap_init(&daemon->timer_heap, timer_compare,
                     timer_get_heap_id, timer_set_heap_id);
  return daemon;
}

canonical_original_code:
struct mdaemon *mstream_daemon_create() {
  struct mdaemon *daemon = (struct mdaemon*)calloc(1, sizeof(struct mdaemon));
  daemon->epollfd = epoll_create(10);
  daemon->timerfd = _mstream_timerfd_create(1, 04000);
  struct epoll_event ev;
  memset(&ev, 0, sizeof(ev));
  ev.events = EPOLLIN;
  epoll_ctl(daemon->epollfd, 1, daemon->timerfd, &ev);
  pthread_mutex_init(&daemon->lock, ((void *)0));
  _mstream_heap_init(&daemon->timer_heap, timer_compare,
                     timer_get_heap_id, timer_set_heap_id);
  return daemon;
}

variable_types: struct mdaemon { struct heap { int (*compare)(void *, void *); unsigned long (*to_id)(void *); void (*set_id)(void *, unsigned long); unsigned long size; unsigned long cap; void **heap; } timer_heap; int epollfd; int timerfd; unsigned long cur_timer; 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; } lock; int thread_shutdown; unsigned long thread; } *

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct mdaemon { struct heap timer_heap; int epollfd; int timerfd; unsigned long cur_timer; union pthread_mutex_t lock; int thread_shutdown; unsigned long thread; }


decompiled_code:
int __fastcall mstream_daemon_start(void *arg)
{
return pthread_create((pthread_t *)arg + 14, 0LL, daemon_thread, arg);
}

canonical_decompiled_code:
int __fastcall func10(void *arg)
{
return pthread_create((pthread_t *)arg + 14, 0LL, daemon_thread, arg);
}

original_code:
void mstream_daemon_start(struct mdaemon* daemon) {
  pthread_create(&daemon->thread, ((void *)0), daemon_thread, daemon);
}

canonical_original_code:
void mstream_daemon_start(struct mdaemon *daemon) {
  pthread_create(&daemon->thread, ((void *)0), daemon_thread, daemon);
}

user_defined_types: struct heap { int (*compare)(void *, void *); unsigned long (*to_id)(void *); void (*set_id)(void *, unsigned long); unsigned long size; unsigned long cap; void **heap; }

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


decompiled_code:
int __fastcall mstream_daemon_stop(__int64 a1)
{
int result; // eax

*(_DWORD *)(a1 + 104) = 1;
mstream_daemon_adjust_timer(a1);
result = pthread_join(*(_QWORD *)(a1 + 112), 0LL);
*(_DWORD *)(a1 + 104) = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1)
{
int result; // eax

*(_DWORD *)(a1 + 104) = 1;
func7(a1);
result = pthread_join(*(_QWORD *)(a1 + 112), 0LL);
*(_DWORD *)(a1 + 104) = 0;
return result;
}

original_code:
void mstream_daemon_stop(struct mdaemon* daemon) {
  daemon->thread_shutdown = 1;
  _mstream_daemon_adjust_timer(daemon);
  pthread_join(daemon->thread, ((void *)0));
  daemon->thread_shutdown = 0;
}

canonical_original_code:
void mstream_daemon_stop(struct mdaemon *daemon) {
  daemon->thread_shutdown = 1;
  _mstream_daemon_adjust_timer(daemon);
  pthread_join(daemon->thread, ((void *)0));
  daemon->thread_shutdown = 0;
}


decompiled_code:
void __fastcall mstream_daemon_destroy(void *ptr)
{
mstream_daemon_stop((__int64)ptr);
close(*((_DWORD *)ptr + 12));
close(*((_DWORD *)ptr + 13));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 64));
mstream_heap_destroy(ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
func11((__int64)ptr);
close(*((_DWORD *)ptr + 12));
close(*((_DWORD *)ptr + 13));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 64));
mstream_heap_destroy(ptr);
free(ptr);
}

original_code:
void mstream_daemon_destroy(struct mdaemon* daemon) {
  mstream_daemon_stop(daemon);
  close(daemon->epollfd);
  close(daemon->timerfd);
  pthread_mutex_destroy(&daemon->lock);
  _mstream_heap_destroy(&daemon->timer_heap);
  free(daemon);
}

canonical_original_code:
void mstream_daemon_destroy(struct mdaemon *daemon) {
  mstream_daemon_stop(daemon);
  close(daemon->epollfd);
  close(daemon->timerfd);
  pthread_mutex_destroy(&daemon->lock);
  _mstream_heap_destroy(&daemon->timer_heap);
  free(daemon);
}


adjust

original_code:
static void adjust(struct heap* h, size_t i) {
  void* tmp = h->heap[i];
  for(; i; ) {
    size_t p = (i - 1) >> 1;
    if(!h->compare(tmp, h->heap[p])) {
      break;
    }
    h->heap[i] = h->heap[p];
    h->set_id(h->heap[i], i);
    i = p;
  }
  while((i << 1) + 1 < h->size) {
    size_t c1 = (i << 1) + 1;
    size_t c2 = c1 + 1;
    if(c2 < h->size && h->compare(h->heap[c2], h->heap[c1])) {
      c1 = c2;
    }
    if(!h->compare(h->heap[c1], tmp)) {
      break;
    }
    h->heap[i] = h->heap[c1];
    h->set_id(h->heap[i], i);
    i = c1;
  }
  h->heap[i] = tmp;
  h->set_id(h->heap[i], i);
}

canonical_original_code:
static void adjust(struct heap *h, unsigned long i) {
  void *tmp = h->heap[i];
  for(; i; ) {
    unsigned long p = (i - 1) >> 1;
    if(!h->compare(tmp, h->heap[p])) {
      break;
    }
    h->heap[i] = h->heap[p];
    h->set_id(h->heap[i], i);
    i = p;
  }
  while((i << 1) + 1 < h->size) {
    unsigned long c1 = (i << 1) + 1;
    unsigned long c2 = c1 + 1;
    if(c2 < h->size && h->compare(h->heap[c2], h->heap[c1])) {
      c1 = c2;
    }
    if(!h->compare(h->heap[c1], tmp)) {
      break;
    }
    h->heap[i] = h->heap[c1];
    h->set_id(h->heap[i], i);
    i = c1;
  }
  h->heap[i] = tmp;
  h->set_id(h->heap[i], i);
}

variable_types: struct heap { int (*compare)(void *, void *); unsigned long (*to_id)(void *); void (*set_id)(void *, unsigned long); unsigned long size; unsigned long cap; void **heap; } *

variable_types:
i
unsigned long

variable_types:
tmp
void *

variable_types:
p
unsigned long


decompiled_code:
__int64 __fastcall min_time(__int64 a1, __int64 a2)
{
__int64 v2; // rbx

v2 = a1;
if ( !(unsigned int)time_less(a1, a2) )
v2 = a2;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v2; // rbx

v2 = a1;
if ( !(unsigned int)func0(a1, a2) )
v2 = a2;
return v2;
}

original_code:
__attribute__ ((unused))
static time_val min_time(time_val x, time_val y) {
  return time_less(x, y) ? x : y;
}

canonical_original_code:
static unsigned long min_time(unsigned long x, unsigned long y) {
  return time_less(x, y) ? x : y;
}


outb

repo: msiniavine/CherryOS

decompiled_code:
__int64 __fastcall outb(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

original_code:
static inline void outb(u16 port, u8 value)
{
 asm volatile ("outb %1, %0" :: "dN"(port), "a"(value));
}

canonical_original_code:
static inline void outb(unsigned short port, unsigned char value)
{
 asm volatile ("outb %1, %0" :: "dN"(port), "a"(value));
}

variable_types:
port
unsigned short

variable_types:
value
unsigned char


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

result = (unsigned int)(50 * (tick / 50));
if ( tick == (_DWORD)result )
result = printk("Tick: %d\n", (unsigned int)(tick / 50));
++tick;
return result;
}

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

result = (unsigned int)(50 * (tick / 50));
if ( tick == (_DWORD)result )
result = printk("Tick: %d\n", (unsigned int)(tick / 50));
++tick;
return result;
}

original_code:
void timer_interrupt()
{
 if(tick % 50 == 0)
  printk("Tick: %d\n", tick/50);
 tick++;
}


decompiled_code:
__int64 init_timer()
{
outb(0x43u, 0x36u);
outb(0x40u, 0x37u);
outb(0x40u, 0x5Du);
return register_interrupt_handler(timer_interrupt, 32LL);
}

canonical_decompiled_code:
__int64 func2()
{
func0(0x43u, 0x36u);
func0(0x40u, 0x37u);
func0(0x40u, 0x5Du);
return register_interrupt_handler(timer_interrupt, 32LL);
}

original_code:
void init_timer()
{
 u32 divisor = 1193180/50;
 u8 l = divisor & 0xff;
 u8 h = (divisor >> 8) & 0xff;
 outb(0x43, 0x36);
 outb(0x40, l);
 outb(0x40, h);
 register_interrupt_handler(timer_interrupt, 32);
}

canonical_original_code:
void init_timer()
{
 unsigned int divisor = 1193180/50;
 unsigned char l = divisor & 0xff;
 unsigned char h = (divisor >> 8) & 0xff;
 outb(0x43, 0x36);
 outb(0x40, l);
 outb(0x40, h);
 register_interrupt_handler(timer_interrupt, 32);
}

variable_types:
divisor
unsigned int

variable_types:
l
unsigned char

variable_types:
h
unsigned char


decompiled_code:
__int64 init_pic()
{
outb(0x20u, 0x11u);
outb(0xA0u, 0x11u);
outb(0x21u, 0x20u);
outb(0xA1u, 0x28u);
outb(0x21u, 4u);
outb(0xA1u, 2u);
outb(0x21u, 1u);
outb(0xA1u, 1u);
outb(0x21u, 0);
return outb(0xA1u, 0);
}


decompiled_code:
__int64 __fastcall print_error(unsigned int *a1)
{
printk("\n");
printk("FATAL ERROR!!!\n");
printk("Unhandled Exception: %x Error Code %x\n", a1[9], a1[10]);
printk("EIP %p\n", (const void *)a1[11]);
printk("EFLAGS %p\n", (const void *)a1[13]);
printk("eax: %x ebx: %x ecx: %x edx: %x\n", a1[8], a1[5], a1[7], a1[6]);
printk("esi: %x edi: %x ebp: %x esp: %x\n", a1[2], a1[1], a1[3], a1[4]);
printk("ds: %x cs: %x ss: %x\n", *a1, a1[12], a1[15]);
return printk("user sp: %x\n", a1[14]);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
printk("\n");
printk("FATAL ERROR!!!\n");
printk("Unhandled Exception: %x Error Code %x\n", a1[9], a1[10]);
printk("EIP %p\n", (const void *)a1[11]);
printk("EFLAGS %p\n", (const void *)a1[13]);
printk("eax: %x ebx: %x ecx: %x edx: %x\n", a1[8], a1[5], a1[7], a1[6]);
printk("esi: %x edi: %x ebp: %x esp: %x\n", a1[2], a1[1], a1[3], a1[4]);
printk("ds: %x cs: %x ss: %x\n", *a1, a1[12], a1[15]);
return printk("user sp: %x\n", a1[14]);
}

original_code:
static void print_error(struct regs* regs)
{
 printk("\n");
 printk("FATAL ERROR!!!\n");
 printk("Unhandled Exception: %x Error Code %x\n", regs->int_no, regs->err);
 printk("EIP %p\n", regs->eip);
 printk("EFLAGS %p\n", regs->eflags);
 printk("eax: %x ebx: %x ecx: %x edx: %x\n", regs->eax, regs->ebx, regs->ecx, regs->edx);
 printk("esi: %x edi: %x ebp: %x esp: %x\n", regs->esi, regs->edi, regs->ebp, regs->esp);
 printk("ds: %x cs: %x ss: %x\n", regs->ds, regs->cs, regs->ss);
 printk("user sp: %x\n", regs->useresp);
}

canonical_original_code:
static void print_error(struct regs *regs)
{
 printk("\n");
 printk("FATAL ERROR!!!\n");
 printk("Unhandled Exception: %x Error Code %x\n", regs->int_no, regs->err);
 printk("EIP %p\n", regs->eip);
 printk("EFLAGS %p\n", regs->eflags);
 printk("eax: %x ebx: %x ecx: %x edx: %x\n", regs->eax, regs->ebx, regs->ecx, regs->edx);
 printk("esi: %x edi: %x ebp: %x esp: %x\n", regs->esi, regs->edi, regs->ebp, regs->esp);
 printk("ds: %x cs: %x ss: %x\n", regs->ds, regs->cs, regs->ss);
 printk("user sp: %x\n", regs->useresp);
}

variable_types: struct regs { unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; unsigned int int_no; unsigned int err; unsigned int eip; unsigned int cs; unsigned int eflags; unsigned int useresp; unsigned int ss; } *


decompiled_code:
void __fastcall register_interrupt_handler(__int64 a1, int a2)
{
interrupt_handlers[a2] = a1;
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
interrupt_handlers[a2] = a1;
}

original_code:
void register_interrupt_handler(handler_func handler, int num)
{
 interrupt_handlers[num] = handler;
}

canonical_original_code:
void register_interrupt_handler(void (*handler)(), int num)
{
 interrupt_handlers[num] = handler;
}

variable_types:
handler
void (*)()

variable_types: num int


original_code:
void set_up_idt()
{
 memset(&idt[0], 0, sizeof(idt));
 idtr.address = (u32)&idt[0];
 idtr.limit = sizeof(idt)-1;
 fill_idt_entry(&idt[0], isr0, 0x8, 0xf);
 fill_idt_entry(&idt[2], isr2, 0x8, 0xf);
 fill_idt_entry(&idt[3], isr3, 0x8, 0xf);
 fill_idt_entry(&idt[4], isr4, 0x8, 0xf);
 fill_idt_entry(&idt[5], isr5, 0x8, 0xf);
 fill_idt_entry(&idt[6], isr6, 0x8, 0xf);
 fill_idt_entry(&idt[7], isr7, 0x8, 0xf);
 fill_idt_entry(&idt[8], isr8, 0x8, 0xf);
 fill_idt_entry(&idt[10], isr10, 0x8, 0xf);
 fill_idt_entry(&idt[11], isr11, 0x8, 0xf);
 fill_idt_entry(&idt[12], isr12, 0x8, 0xf);
 fill_idt_entry(&idt[13], isr13, 0x8, 0xf);
 fill_idt_entry(&idt[14], isr14, 0x8, 0xf);
 fill_idt_entry(&idt[16], isr16, 0x8, 0xf);
 fill_idt_entry(&idt[17], isr17, 0x8, 0xf);
 fill_idt_entry(&idt[18], isr18, 0x8, 0xf);
 fill_idt_entry(&idt[19], isr19, 0x8, 0xf);
 fill_idt_entry(&idt[32], irq0, 0x8, 0xe);
 fill_idt_entry(&idt[33], irq1, 0x8, 0xe);
 fill_idt_entry(&idt[34], irq2, 0x8, 0xe);
 fill_idt_entry(&idt[35], irq3, 0x8, 0xe);
 fill_idt_entry(&idt[36], irq4, 0x8, 0xe);
 fill_idt_entry(&idt[37], irq5, 0x8, 0xe);
 fill_idt_entry(&idt[38], irq6, 0x8, 0xe);
 fill_idt_entry(&idt[39], irq7, 0x8, 0xe);
 fill_idt_entry(&idt[40], irq8, 0x8, 0xe);
 fill_idt_entry(&idt[41], irq9, 0x8, 0xe);
 fill_idt_entry(&idt[42], irq10, 0x8, 0xe);
 fill_idt_entry(&idt[43], irq11, 0x8, 0xe);
 fill_idt_entry(&idt[44], irq12, 0x8, 0xe);
 fill_idt_entry(&idt[45], irq13, 0x8, 0xe);
 fill_idt_entry(&idt[46], irq14, 0x8, 0xe);
 fill_idt_entry(&idt[47], irq15, 0x8, 0xe);
 init_pic();
 memset(interrupt_handlers, 0, sizeof(interrupt_handlers));
}


decompiled_code:
__int64 __fastcall isr_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_error((unsigned int *)&a7);
return cpu_halt();
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func4((unsigned int *)&a7);
return cpu_halt();
}

original_code:
void isr_handler(struct regs regs)
{
 print_error(&regs);
 cpu_halt();
}

variable_types: struct regs { unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; unsigned int int_no; unsigned int err; unsigned int eip; unsigned int cs; unsigned int eflags; unsigned int useresp; unsigned int ss; }


decompiled_code:
__int64 __fastcall irq_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7, int a8, int a9, int a10, __int64 a11)
{
void (*v11)(void); // rdx

v11 = (void (*)(void))interrupt_handlers[HIDWORD(a11)];
if ( v11 )
v11();
return acknowledge_interrupt((__int64)&a7);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7, int a8, int a9, int a10, __int64 a11)
{
void (*v11)(void); // rdx

v11 = (void (*)(void))interrupt_handlers[HIDWORD(a11)];
if ( v11 )
v11();
return func3((__int64)&a7);
}

original_code:
void irq_handler(struct regs regs)
{
 if(interrupt_handlers[regs.int_no])
  interrupt_handlers[regs.int_no]();
 acknowledge_interrupt(&regs);
}

user_defined_types: struct regs { unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; unsigned int int_no; unsigned int err; unsigned int eip; unsigned int cs; unsigned int eflags; unsigned int useresp; unsigned int ss; }


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
__int64 result; // rax

if ( !*a1 )
return 0LL;
result = 0LL;
do
++result;
while ( a1[result] );
return result;
}

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

if ( !*a1 )
return 0LL;
result = 0LL;
do
++result;
while ( a1[result] );
return result;
}

original_code:
size_t strlen(const char* str)
{
 size_t len = 0;
 for(len = 0; str[len] != 0; len++);
 return len;
}

canonical_original_code:
unsigned long strlen(char *str)
{
 unsigned long len = 0;
 for(len = 0; str[len] != 0; len++);
 return len;
}

variable_types:
str
char *

variable_types:
len
unsigned long


memcpy

original_code:
void* memcpy(void* dest, void* src, size_t num)
{
 char* d = dest;
 char* s = src;
 size_t i;
 for(i = 0; i<num; i++)
 {
  d[i] = s[i];
 }
 return dest;
}

canonical_original_code:
void *memcpy(void *dest, void *src, unsigned long num)
{
 char *d = dest;
 char *s = src;
 unsigned long i;
 for(i = 0; i<num; i++)
 {
  d[i] = s[i];
 }
 return dest;
}

variable_types:
dest
void *

variable_types:
src
void *

variable_types:
num
unsigned long

variable_types:
d
char *

variable_types:
s
char *


scroll

decompiled_code:
void *scroll()
{
__int64 i; // rbx
void *v1; // rdi
void *result; // rax

for ( i = 753664LL; i != 757504; result = memcpy(v1, (const void *)i, 0xA0uLL) )
{
v1 = (void *)i;
i += 160LL;
}
do
{
*(_WORD *)i = 3872;
i += 2LL;
}
while ( i != 757664 );
dword_124 = 0;
cy = 24;
return result;
}

canonical_decompiled_code:
void *func0()
{
__int64 i; // rbx
void *v1; // rdi
void *result; // rax

for ( i = 753664LL; i != 757504; result = memcpy(v1, (const void *)i, 0xA0uLL) )
{
v1 = (void *)i;
i += 160LL;
}
do
{
*(_WORD *)i = 3872;
i += 2LL;
}
while ( i != 757664 );
dword_124 = 0;
cy = 24;
return result;
}

original_code:
static void scroll()
{
 u8 space_attr = (0 << 4) | (15 & 0x0f);
 u16 space = 0x20 | (space_attr << 8);
 int row;
 int i;
 for(row = 1; row < 25; row++)
 {
  u16* src = &vram[row*80];
  u16* dest = &vram[(row-1)*80];
  memcpy(dest, src, sizeof(u16)*80);
 }
 for(i = 24*80; i<25*80;i++)
 {
  vram[i] = space;
 }
 cx = 0;
 cy = 25 -1;
}

canonical_original_code:
static void scroll()
{
 unsigned char space_attr = (0 << 4) | (15 & 0x0f);
 unsigned short space = 0x20 | (space_attr << 8);
 int row;
 int i;
 for(row = 1; row < 25; row++)
 {
  unsigned short *src = &vram[row*80];
  unsigned short *dest = &vram[(row-1)*80];
  memcpy(dest, src, sizeof(u16)*80);
 }
 for(i = 24*80; i<25*80;i++)
 {
  vram[i] = space;
 }
 cx = 0;
 cy = 25 -1;
}

variable_types:
space
unsigned short

variable_types: row int

variable_types: i int

variable_types:
src
unsigned short *

variable_types:
dest
unsigned short *


putchar

decompiled_code:
unsigned __int64 __fastcall putchar(char a1)
{
int v1; // ecx
int v2; // edx
unsigned __int64 result; // rax
int v4; // edx

if ( a1 > 31 )
{
v1 = cy;
v2 = dword_124;
result = dword_124 + 80 * cy;
*(_WORD *)(2 * result + 0xB8000) = a1 | 0x700;
v4 = v2 + 1;
if ( v4 > 79 )
{
dword_124 = 0;
cy = v1 + 1;
}
else
{
dword_124 = v4;
}
if ( cy > 24 )
return (unsigned __int64)scroll();
}
else if ( a1 == 10 )
{
dword_124 = 0;
result = (unsigned int)(cy + 1);
cy = result;
if ( (int)result > 24 )
return (unsigned __int64)scroll();
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(char a1)
{
int v1; // ecx
int v2; // edx
unsigned __int64 result; // rax
int v4; // edx

if ( a1 > 31 )
{
v1 = cy;
v2 = dword_124;
result = dword_124 + 80 * cy;
*(_WORD *)(2 * result + 0xB8000) = a1 | 0x700;
v4 = v2 + 1;
if ( v4 > 79 )
{
dword_124 = 0;
cy = v1 + 1;
}
else
{
dword_124 = v4;
}
if ( cy > 24 )
return (unsigned __int64)func0();
}
else if ( a1 == 10 )
{
dword_124 = 0;
result = (unsigned int)(cy + 1);
cy = result;
if ( (int)result > 24 )
return (unsigned __int64)func0();
}
return result;
}

original_code:
static void putchar(char c)
{
 if(c >= 0x20)
 {
  u16 glyph = (0x07 << 8) | c;
  vram[cy*80 + cx] = glyph;
  cx++;
  if(cx >= 80)
  {
   cx = 0;
   cy++;
  }
  if(cy >= 25)
  {
   scroll();
  }
  return;
 }
 if(c == '\n')
 {
  cx = 0;
  cy++;
  if(cy >= 25)
  {
   scroll();
  }
 }
}

canonical_original_code:
static void putchar(char c)
{
 if(c >= 0x20)
 {
  unsigned short glyph = (0x07 << 8) | c;
  vram[cy*80 + cx] = glyph;
  cx++;
  if(cx >= 80)
  {
   cx = 0;
   cy++;
  }
  if(cy >= 25)
  {
   scroll();
  }
  return;
 }
 if(c == '\n')
 {
  cx = 0;
  cy++;
  if(cy >= 25)
  {
   scroll();
  }
 }
}

variable_types: c char

variable_types:
glyph
unsigned short


decompiled_code:
unsigned __int64 __fastcall console_write(char *a1, __int64 a2)
{
char *v2; // rbx
unsigned __int64 result; // rax

if ( a2 )
{
v2 = a1;
do
result = putchar(*v2++);
while ( v2 != &a1[a2] );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(char *a1, __int64 a2)
{
char *v2; // rbx
unsigned __int64 result; // rax

if ( a2 )
{
v2 = a1;
do
result = func1(*v2++);
while ( v2 != &a1[a2] );
}
return result;
}

original_code:
void console_write(const char* str, size_t len)
{
 size_t i;
 for(i=0; i<len; i++)
 {
  putchar(str[i]);
 }
}

canonical_original_code:
void console_write(char *str, unsigned long len)
{
 unsigned long i;
 for(i=0; i<len; i++)
 {
  putchar(str[i]);
 }
}

variable_types:
i
unsigned long


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

result = (a1 >> 5) & 0x3F;
bitmap[result] |= 1 << a1;
return result;
}

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

result = (a1 >> 5) & 0x3F;
bitmap[result] |= 1 << a1;
return result;
}

original_code:
static void mark_page_used(u32 pfn)
{
 bitmap[(((pfn) >> 5) & 0x3f)] |= (1 << ((pfn) & 0x1f));
}

canonical_original_code:
static void mark_page_used(unsigned int pfn)
{
 bitmap[(((pfn) >> 5) & 0x3f)] |= (1 << ((pfn) & 0x1f));
}

variable_types:
pfn
unsigned int


decompiled_code:
__int64 __fastcall in_use(unsigned int a1)
{
return bitmap[(a1 >> 5) & 0x3F] & (unsigned int)(1 << a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return bitmap[(a1 >> 5) & 0x3F] & (unsigned int)(1 << a1);
}

original_code:
static int in_use(u32 pfn)
{
 return bitmap[(((pfn) >> 5) & 0x3f)] & (1 << ((pfn) & 0x1f));
}

canonical_original_code:
static int in_use(unsigned int pfn)
{
 return bitmap[(((pfn) >> 5) & 0x3f)] & (1 << ((pfn) & 0x1f));
}


decompiled_code:
void *__fastcall debug_allocate(void *s)
{
return memset((void *)(unsigned int)s, 165, 0x1000uLL);
}

canonical_decompiled_code:
void *__fastcall func2(void *s)
{
return memset((void *)(unsigned int)s, 165, 0x1000uLL);
}

original_code:
static void debug_allocate(u32 address)
{
 memset((void*)address, 0xa5, (4*1024));
}

canonical_original_code:
static void debug_allocate(unsigned int address)
{
 memset((void *)address, 0xa5, (4*1024));
}

variable_types:
address
unsigned int


decompiled_code:
void *__fastcall debug_free(void *s)
{
return memset((void *)(unsigned int)s, 182, 0x1000uLL);
}

canonical_decompiled_code:
void *__fastcall func3(void *s)
{
return memset((void *)(unsigned int)s, 182, 0x1000uLL);
}

original_code:
static void debug_free(u32 address)
{
 memset((void*)address, 0xb6, (4*1024));
}

canonical_original_code:
static void debug_free(unsigned int address)
{
 memset((void *)address, 0xb6, (4*1024));
}


decompiled_code:
__int64 init_memory()
{
unsigned int i; // ebx
__int64 result; // rax

memset(bitmap, 0, 0x100uLL);
for ( i = 0; i != 512; ++i )
result = mark_page_used(i);
return result;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int i; // ebx
__int64 result; // rax

memset(bitmap, 0, 0x100uLL);
for ( i = 0; i != 512; ++i )
result = func0(i);
return result;
}

original_code:
void init_memory()
{
 int i;
 memset(bitmap, 0, sizeof(bitmap));
 for(i = ((0) >> 12); i < (((2*1024*1024)) >> 12); i++)
 {
  mark_page_used(i);
 }
}


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

v0 = 512;
while ( (unsigned int)in_use(v0) )
{
if ( ++v0 == 2048 )
return 0;
}
mark_page_used(v0);
v1 = v0 << 12;
debug_allocate((void *)v1);
return v1;
}

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

v0 = 512;
while ( (unsigned int)func1(v0) )
{
if ( ++v0 == 2048 )
return 0;
}
func0(v0);
v1 = v0 << 12;
func2((void *)v1);
return v1;
}

return_type: unsigned int


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

debug_free(a1);
result = (unsigned int)~(1 << ((unsigned int)a1 >> 12));
bitmap[((unsigned int)a1 >> 17) & 0x3F] &= result;
return result;
}

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

func3(a1);
result = (unsigned int)~(1 << ((unsigned int)a1 >> 12));
bitmap[((unsigned int)a1 >> 17) & 0x3F] &= result;
return result;
}

original_code:
void free_page(u32 address)
{
 debug_free(address);
 bitmap[(((((address) >> 12)) >> 5) & 0x3f)] &= ~((1 << ((((address) >> 12)) & 0x1f)));
}

canonical_original_code:
void free_page(unsigned int address)
{
 debug_free(address);
 bitmap[(((((address) >> 12)) >> 5) & 0x3f)] &= ~((1 << ((((address) >> 12)) & 0x1f)));
}


sti

decompiled_code:
void sti()
{
_enable();
}

canonical_decompiled_code:
void func0()
{
_enable();
}

original_code:
static inline void sti()
{
 asm volatile ("sti");
}


decompiled_code:
void __noreturn cpu_relax()
{
__halt();
}

canonical_decompiled_code:
void __noreturn func1()
{
__halt();
}

original_code:
static inline void cpu_relax()
{
 asm volatile ("hlt");
}


decompiled_code:
__int64 __fastcall print_mb_info(unsigned int *a1)
{
unsigned int v1; // ebx
__int64 result; // rax
unsigned int v3; // ebp
unsigned int *v4; // rbx
unsigned int v5; // ebp

v1 = *a1;
result = printk("Flags %x\n", *a1);
if ( (v1 & 1) != 0 )
result = printk("mem_lowerp %uk mem_upper %uk\n", a1[1], a1[2]);
if ( (v1 & 0x40) != 0 )
{
printk("mmap_length %u, mmap_addr %p\n", a1[12], (const void *)a1[13]);
v3 = a1[13];
v4 = (unsigned int *)v3;
for ( result = a1[12] + v3; v3 < (unsigned int)result; result = a1[13] + a1[12] )
{
printk(
" size=%x, base_addr=%p%p, length=%p%p, type=%x\n",
*v4,
(const void *)v4[2],
(const void *)v4[1],
(const void *)v4[4],
(const void *)v4[3],
v4[5]);
v5 = *v4 + v3;
v4 = (unsigned int *)(v5 + 4LL);
v3 = v5 + 4;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1)
{
unsigned int v1; // ebx
__int64 result; // rax
unsigned int v3; // ebp
unsigned int *v4; // rbx
unsigned int v5; // ebp

v1 = *a1;
result = printk("Flags %x\n", *a1);
if ( (v1 & 1) != 0 )
result = printk("mem_lowerp %uk mem_upper %uk\n", a1[1], a1[2]);
if ( (v1 & 0x40) != 0 )
{
printk("mmap_length %u, mmap_addr %p\n", a1[12], (const void *)a1[13]);
v3 = a1[13];
v4 = (unsigned int *)v3;
for ( result = a1[12] + v3; v3 < (unsigned int)result; result = a1[13] + a1[12] )
{
printk(
" size=%x, base_addr=%p%p, length=%p%p, type=%x\n",
*v4,
(const void *)v4[2],
(const void *)v4[1],
(const void *)v4[4],
(const void *)v4[3],
v4[5]);
v5 = *v4 + v3;
v4 = (unsigned int *)(v5 + 4LL);
v3 = v5 + 4;
}
}
return result;
}

original_code:
static void print_mb_info(struct mb_info* info)
{
 u32 flags = info->flags;
 printk("Flags %x\n", flags);
 if(flags & (1<<0))
  printk("mem_lowerp %uk mem_upper %uk\n", info->mem_lower, info->mem_upper);
 if(flags & (1<<6))
 {
  struct mb_mmap_entry* mmap;
  printk("mmap_length %u, mmap_addr %p\n", info->mmap_length, info->mmap_addr);
  for(mmap = (struct mb_mmap_entry*)info->mmap_addr;
      (u32)mmap < info->mmap_addr+info->mmap_length;
      mmap = (struct mb_mmap_entry*)((u32)mmap+mmap->size + sizeof(mmap->size)))
  {
   printk(" size=%x, base_addr=%p%p,"
          " length=%p%p, type=%x\n",
          mmap->size,
          mmap->addr_high, mmap->addr_low,
          mmap->len_high, mmap->len_low,
          mmap->type
    );
  }
 }
}

canonical_original_code:
static void print_mb_info(struct mb_info *info)
{
 unsigned int flags = info->flags;
 printk("Flags %x\n", flags);
 if(flags & (1<<0))
  printk("mem_lowerp %uk mem_upper %uk\n", info->mem_lower, info->mem_upper);
 if(flags & (1<<6))
 {
  struct mb_mmap_entry *mmap;
  printk("mmap_length %u, mmap_addr %p\n", info->mmap_length, info->mmap_addr);
  for(mmap = (struct mb_mmap_entry *)info->mmap_addr;
      (unsigned int)mmap < info->mmap_addr+info->mmap_length;
      mmap = (struct mb_mmap_entry *)((unsigned int)mmap+mmap->size + sizeof(mmap->size)))
  {
   printk(" size=%x, base_addr=%p%p,"
          " length=%p%p, type=%x\n",
          mmap->size,
          mmap->addr_high, mmap->addr_low,
          mmap->len_high, mmap->len_low,
          mmap->type
    );
  }
 }
}

variable_types: struct mb_info { unsigned int flags; unsigned int mem_lower; unsigned int mem_upper; unsigned int boot_device; char *cmdline; unsigned int mods_count; unsigned int mods_addr; unsigned int syms[4]; unsigned int mmap_length; unsigned int mmap_addr; } *

variable_types:
flags
unsigned int

variable_types: struct mb_mmap_entry { unsigned int size; unsigned int addr_low; unsigned int len_low; unsigned int type; } *


panic

decompiled_code:
__int64 __fastcall panic(const char *a1)
{
printk("panic - %s", a1);
return cpu_halt();
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
printk("panic - %s", a1);
return cpu_halt();
}

original_code:
void panic(const char* msg)
{
 printk("panic - %s", msg);
 cpu_halt();
}

canonical_original_code:
void panic(char *msg)
{
 printk("panic - %s", msg);
 cpu_halt();
}

variable_types:
msg
char *


kmain

decompiled_code:
void __noreturn kmain()
{
unsigned int v0; // er13
unsigned __int64 v1; // rbx
__int64 v2; // r12
unsigned __int64 v3; // rbp
int v4; // eax
size_t v5; // rax

printk("Welcome to CherryOS\n");
print_mb_info((unsigned int *)mbd);
init_mm();
v0 = get_free_page();
if ( !v0 )
panic("Could not allocate test page\n");
printk("Pages %p\n", (const void *)v0);
v1 = v0;
v2 = v0 + 4096LL;
v3 = v0;
while ( 1 )
{
v4 = get_free_page();
*(_DWORD *)v3 = v4;
if ( !v4 )
panic("Failed to test\n");
v3 += 4LL;
if ( v3 == v2 )
{
printk("Now attempt to free\n");
do
{
free_page(*(unsigned int *)v1);
v1 += 4LL;
}
while ( v1 != v2 );
free_page(v0);
init_timer();
init_serial();
sti();
v5 = strlen("hello world serial");
serial_write("hello world serial", v5);
cpu_relax();
}
}
}

canonical_decompiled_code:
void __noreturn func4()
{
unsigned int v0; // er13
unsigned __int64 v1; // rbx
__int64 v2; // r12
unsigned __int64 v3; // rbp
int v4; // eax
size_t v5; // rax

printk("Welcome to CherryOS\n");
func2((unsigned int *)mbd);
init_mm();
v0 = get_free_page();
if ( !v0 )
func3("Could not allocate test page\n");
printk("Pages %p\n", (const void *)v0);
v1 = v0;
v2 = v0 + 4096LL;
v3 = v0;
while ( 1 )
{
v4 = get_free_page();
*(_DWORD *)v3 = v4;
if ( !v4 )
func3("Failed to test\n");
v3 += 4LL;
if ( v3 == v2 )
{
printk("Now attempt to free\n");
do
{
free_page(*(unsigned int *)v1);
v1 += 4LL;
}
while ( v1 != v2 );
free_page(v0);
init_timer();
init_serial();
func0();
v5 = strlen("hello world serial");
serial_write("hello world serial", v5);
func1();
}
}
}

original_code:
void kmain()
{
 u32* pages;
 int i;
 printk("Welcome to CherryOS\n");
 print_mb_info(mbd);
 init_mm();
 pages = (u32*)get_free_page();
 if(!pages)
 {
  panic("Could not allocate test page\n");
 }
 printk("Pages %p\n", pages);
 for(i=0; i<1024; i++)
 {
  pages[i] = get_free_page();
  if(!pages[i])
   panic("Failed to test\n");
 }
 printk("Now attempt to free\n");
 for(i=0; i<1024; i++)
 {
  free_page(pages[i]);
 }
 free_page((u32)pages);
 init_timer();
 init_serial();
 sti();
 serial_write("hello world serial", strlen("hello world serial"));
 for(;;) cpu_relax();
 cpu_halt();
}

canonical_original_code:
void kmain()
{
 unsigned int *pages;
 int i;
 printk("Welcome to CherryOS\n");
 print_mb_info(mbd);
 init_mm();
 pages = (unsigned int *)get_free_page();
 if(!pages)
 {
  panic("Could not allocate test page\n");
 }
 printk("Pages %p\n", pages);
 for(i=0; i<1024; i++)
 {
  pages[i] = get_free_page();
  if(!pages[i])
   panic("Failed to test\n");
 }
 printk("Now attempt to free\n");
 for(i=0; i<1024; i++)
 {
  free_page(pages[i]);
 }
 free_page((unsigned int)pages);
 init_timer();
 init_serial();
 sti();
 serial_write("hello world serial", strlen("hello world serial"));
 for(;;) cpu_relax();
 cpu_halt();
}

variable_types:
pages
unsigned int *


outb

decompiled_code:
__int64 __fastcall outb(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

original_code:
static inline void outb(u16 port, u8 value)
{
 asm volatile ("outb %1, %0" :: "dN"(port), "a"(value));
}

canonical_original_code:
static inline void outb(unsigned short port, unsigned char value)
{
 asm volatile ("outb %1, %0" :: "dN"(port), "a"(value));
}

variable_types:
port
unsigned short

variable_types:
value
unsigned char


decompiled_code:
__int64 is_transmit_empty()
{
return inb(0x3FDu) & 0x20;
}

canonical_decompiled_code:
__int64 func2()
{
return func1(0x3FDu) & 0x20;
}

original_code:
static int is_transmit_empty()
{
 return inb(0x3f8 +5) & 0x20;
}


decompiled_code:
__int64 serial_interrupt()
{
return printk("Serial interrupt\n");
}

canonical_decompiled_code:
__int64 func3()
{
return printk("Serial interrupt\n");
}

original_code:
static void serial_interrupt()
{
 printk("Serial interrupt\n");
}


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

outb(0x3F9u, 0);
outb(0x3FBu, 0x80u);
outb(0x3F8u, 1u);
outb(0x3F9u, 0);
outb(0x3FBu, 3u);
outb(0x3FAu, 0xC7u);
outb(0x3FCu, 0xBu);
register_interrupt_handler(serial_interrupt, 35LL);
result = register_interrupt_handler(serial_interrupt, 36LL);
is_init = 1;
return result;
}

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

func0(0x3F9u, 0);
func0(0x3FBu, 0x80u);
func0(0x3F8u, 1u);
func0(0x3F9u, 0);
func0(0x3FBu, 3u);
func0(0x3FAu, 0xC7u);
func0(0x3FCu, 0xBu);
register_interrupt_handler(serial_interrupt, 35LL);
result = register_interrupt_handler(serial_interrupt, 36LL);
is_init = 1;
return result;
}

original_code:
void init_serial()
{
 outb(0x3f8 +1, 0);
 outb(0x3f8 +3, 0x80);
 outb(0x3f8 +0, 0x01);
 outb(0x3f8 +1, 0x00);
 outb(0x3f8 +3, 0x03);
 outb(0x3f8 +2, 0xc7);
 outb(0x3f8 +4, 0x0b);
 register_interrupt_handler(serial_interrupt, 35);
 register_interrupt_handler(serial_interrupt, 36);
 is_init = 1;
}


decompiled_code:
void __fastcall serial_write(unsigned __int8 *a1, __int64 a2)
{
unsigned __int8 *v2; // rbx

if ( is_init && a2 )
{
v2 = a1;
do
{
while ( !(unsigned int)is_transmit_empty() )
;
outb(0x3F8u, *v2++);
}
while ( v2 != &a1[a2] );
}
}

canonical_decompiled_code:
void __fastcall func5(unsigned __int8 *a1, __int64 a2)
{
unsigned __int8 *v2; // rbx

if ( is_init && a2 )
{
v2 = a1;
do
{
while ( !(unsigned int)func2() )
;
func0(0x3F8u, *v2++);
}
while ( v2 != &a1[a2] );
}
}

original_code:
void serial_write(const void* buff, size_t num)
{
 const char* data = buff;
 size_t i;
 if(!is_init) return;
 for(i=0; i<num; i++)
 {
  while(is_transmit_empty() == 0);
  outb(0x3f8, data[i]);
 }
}

canonical_original_code:
void serial_write(void *buff, unsigned long num)
{
 char *data = buff;
 unsigned long i;
 if(!is_init) return;
 for(i=0; i<num; i++)
 {
  while(is_transmit_empty() == 0);
  outb(0x3f8, data[i]);
 }
}

variable_types:
buff
void *

variable_types:
num
unsigned long

variable_types:
data
char *


decompiled_code:
void *set_up_gdt()
{
void *result; // rax

memset(&gdt, 0, 0x18uLL);
fill_gdt_entry(word_E8, 0, -1, 10);
fill_gdt_entry(word_F0, 0, -1, 2);
result = &gdt;
*(_DWORD *)(&gdtr + 1) = (unsigned int)&gdt;
gdtr = 23;
return result;
}

canonical_decompiled_code:
void *func1()
{
void *result; // rax

memset(&gdt, 0, 0x18uLL);
func0(word_E8, 0, -1, 10);
func0(word_F0, 0, -1, 2);
result = &gdt;
*(_DWORD *)(&gdtr + 1) = (unsigned int)&gdt;
gdtr = 23;
return result;
}

original_code:
void set_up_gdt()
{
 memset(&gdt[0], 0, sizeof(gdt));
 fill_gdt_entry(&gdt[1], 0, 0xffffffff, 10);
 fill_gdt_entry(&gdt[2], 0, 0xffffffff, 2);
 gdtr.address = (u32)&gdt[0];
 gdtr.limit = sizeof(gdt)-1;
}


decompiled_code:
__int64 enable_paging()
{
unsigned __int64 v0; // rax
__int64 result; // rax

v0 = __readcr0();
result = (unsigned int)v0 | 0x80000000;
__writecr0((unsigned int)result);
return result;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned __int64 v0; // rax
__int64 result; // rax

v0 = __readcr0();
result = (unsigned int)v0 | 0x80000000;
__writecr0((unsigned int)result);
return result;
}

canonical_original_code:
static void enable_paging()
{
 unsigned int cr0;
 __asm__("mov %%cr0, %0\n"
  "or $0x80000000, %0\n"
  "mov %0, %%cr0\n" :: "r"(cr0));
}


itoak

original_code:
int itoak(char* buffer, size_t size, long n, int base, int flags)
{
 char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
 char tmp[66];
 int i = 0;
 size_t copied = 0;
 char* str = buffer;
 char sign = 0;
 if(flags & 1)
 {
  if(n < 0)
  {
   n=-n;
   sign = '-';
  }
  else
  {
   sign = 0;
  }
 }
 if(flags & 2)
 {
  memset(tmp, ((char)(flags >> 2)), 8);
 }
 do
 {
  tmp[i] = digits[(unsigned long)n%(unsigned)base];
  n = (unsigned long)n/(unsigned) base;
  i++;
       } while(n);
 if(sign)
 {
  tmp[i] = sign;
  i++;
 }
 if(flags & 2 && i < 8)
 {
  i = 8;
 }
 while(i-- > 0)
 {
  *str++ = tmp[i];
  copied+=1;
  if(copied == size - 1)
   break;
 }
 buffer[copied] = '\0';
 return copied;
}

canonical_original_code:
int itoak(char *buffer, unsigned long size, long n, int base, int flags)
{
 char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
 char tmp[66];
 int i = 0;
 unsigned long copied = 0;
 char *str = buffer;
 char sign = 0;
 if(flags & 1)
 {
  if(n < 0)
  {
   n=-n;
   sign = '-';
  }
  else
  {
   sign = 0;
  }
 }
 if(flags & 2)
 {
  memset(tmp, ((char)(flags >> 2)), 8);
 }
 do
 {
  tmp[i] = digits[(unsigned long)n%(unsigned int)base];
  n = (unsigned long)n/(unsigned int) base;
  i++;
       } while(n);
 if(sign)
 {
  tmp[i] = sign;
  i++;
 }
 if(flags & 2 && i < 8)
 {
  i = 8;
 }
 while(i-- > 0)
 {
  *str++ = tmp[i];
  copied+=1;
  if(copied == size - 1)
   break;
 }
 buffer[copied] = '\0';
 return copied;
}

variable_types:
buffer
char *

variable_types:
size
unsigned long

variable_types: n long

variable_types: base int

variable_types: flags int

variable_types:
digits
char []

variable_types:
tmp
char [66]

variable_types: i int

variable_types:
copied
unsigned long

variable_types:
str
char *

variable_types: sign char


help

repo: msteinert/cairo-demo

decompiled_code:
int __fastcall help(const char *a1)
{
return fprintf(
stdout,
"usage: %s [options]\n"
" -h, --help display this help\n"
" -o <op>, --operator=<op>\n"
" draw the specified operator demonstration\n"
" -p <file>, --png=<file>\n"
" draw the demonstration to a PNG file\n"
" -v, --version output version information and exit\n",
a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(
stdout,
"usage: %s [options]\n"
" -h, --help display this help\n"
" -o <op>, --operator=<op>\n"
" draw the specified operator demonstration\n"
" -p <file>, --png=<file>\n"
" draw the demonstration to a PNG file\n"
" -v, --version output version information and exit\n",
a1);
}

original_code:
void
help(const char *name)
{
 fprintf(stdout,
  "usage: %s [options]\n"
  " -h, --help     display this help\n"
  " -o <op>, --operator=<op>\n"
  "                draw the specified operator demonstration\n"
  " -p <file>, --png=<file>\n"
  "                draw the demonstration to a PNG file\n"
  " -v, --version  output version information and exit\n",
  name);
}

canonical_original_code:
void help(char *name)
{
 fprintf(stdout,
  "usage: %s [options]\n"
  " -h, --help     display this help\n"
  " -o <op>, --operator=<op>\n"
  "                draw the specified operator demonstration\n"
  " -p <file>, --png=<file>\n"
  "                draw the demonstration to a PNG file\n"
  " -v, --version  output version information and exit\n",
  name);
}

variable_types:
name
char *


myToupper

repo: mtentukov/LameWavEncoder

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

result = a1;
if ( a1 - 97 < 0x1A )
result = a1 - 32;
return result;
}

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

result = a1;
if ( a1 - 97 < 0x1A )
result = a1 - 32;
return result;
}

original_code:
static inline
int myToupper (int ch)
{
   if ( (ch >='a')&&(ch <= 'z') )
   {
      return ch - ('a' - 'A');
   }
   return ch;
}

canonical_original_code:
static inline int myToupper(int ch)
{
   if ( (ch >='a')&&(ch <= 'z') )
   {
      return ch - ('a' - 'A');
   }
   return ch;
}

variable_types: ch int


getCpuNumber

decompiled_code:
__int64 getCpuNumber()
{
return sysconf(84);
}

canonical_decompiled_code:
__int64 func4()
{
return sysconf(84);
}

original_code:
static inline
int getCpuNumber(void)
{
   return sysconf( _SC_NPROCESSORS_ONLN );
}

canonical_original_code:
static inline int getCpuNumber()
{
   return sysconf( _SC_NPROCESSORS_ONLN );
}


scanDirectory

decompiled_code:
__int64 __fastcall scanDirectory(__int64 a1)
{
__int64 result; // rax
int v2; // ebx
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = 0;
result = listDir(a1, &doScanDirectory, v3);
if ( (int)result >= 0 )
{
pthread_mutex_lock(&g_mFileName);
if ( g_nWorkers > 0 )
{
v2 = 0;
do
{
txt_qFLPushFifo((__int64)&g_qFileName, (__int64)&unk_1670);
sem_post(&g_sFileName);
++v2;
}
while ( g_nWorkers > v2 );
}
pthread_mutex_unlock(&g_mFileName);
if ( !v3[0] )
message("No files found!\n");
result = v3[0];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 result; // rax
int v2; // ebx
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = 0;
result = listDir(a1, &doScanDirectory, v3);
if ( (int)result >= 0 )
{
pthread_mutex_lock(&g_mFileName);
if ( g_nWorkers > 0 )
{
v2 = 0;
do
{
func6((__int64)&g_qFileName, (__int64)&unk_1670);
sem_post(&g_sFileName);
++v2;
}
while ( g_nWorkers > v2 );
}
pthread_mutex_unlock(&g_mFileName);
if ( !v3[0] )
message("No files found!\n");
result = v3[0];
}
return result;
}

original_code:
static int scanDirectory (char *pathName)
{
   int foundItems = 0;
   int i;
   int tot = listDir(pathName, doScanDirectory, (void*)&foundItems);
   if (tot < 0)
   {
      return tot;
   }
   pthread_mutex_lock(&g_mFileName);
   for (i = 0; i < g_nWorkers; ++i)
   {
      txt_qFLPushFifo(&g_qFileName, "");
      sem_post(&g_sFileName);
   }
   pthread_mutex_unlock(&g_mFileName);
   if (0 == foundItems)
   {
      message("No files found!\n");
   }
   return foundItems;
}

canonical_original_code:
static int scanDirectory(char *pathName)
{
   int foundItems = 0;
   int i;
   int tot = listDir(pathName, doScanDirectory, (void*)&foundItems);
   if (tot < 0)
   {
      return tot;
   }
   pthread_mutex_lock(&g_mFileName);
   for (i = 0; i < g_nWorkers; ++i)
   {
      txt_qFLPushFifo(&g_qFileName, "");
      sem_post(&g_sFileName);
   }
   pthread_mutex_unlock(&g_mFileName);
   if (0 == foundItems)
   {
      message("No files found!\n");
   }
   return foundItems;
}

variable_types:
pathName
char *

variable_types: foundItems int

variable_types: tot int


doScanDirectory

decompiled_code:
__int64 __fastcall doScanDirectory(__int64 a1, const char *a2, int a3, _DWORD *a4)
{
size_t v5; // rbp
const char *v6; // r13
char *v8; // rax
char *v9; // rax

if ( a3 )
return 0LL;
v5 = strlen(a2);
v6 = &a2[v5 - 4];
if ( *v6 == 46
&& (unsigned int)myToupper(v6[1]) == 87
&& (unsigned int)myToupper(v6[2]) == 65
&& (unsigned int)myToupper(v6[3]) == 86 )
{
pthread_mutex_lock(&g_mFileName);
v8 = (char *)malloc(v5 + 1);
v9 = strcpy(v8, a2);
txt_qFLPushFifo((__int64)&g_qFileName, (__int64)v9);
sem_post(&g_sFileName);
pthread_mutex_unlock(&g_mFileName);
++*a4;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, const char *a2, int a3, _DWORD *a4)
{
size_t v5; // rbp
const char *v6; // r13
char *v8; // rax
char *v9; // rax

if ( a3 )
return 0LL;
v5 = strlen(a2);
v6 = &a2[v5 - 4];
if ( *v6 == 46
&& (unsigned int)func0(v6[1]) == 87
&& (unsigned int)func0(v6[2]) == 65
&& (unsigned int)func0(v6[3]) == 86 )
{
pthread_mutex_lock(&g_mFileName);
v8 = (char *)malloc(v5 + 1);
v9 = strcpy(v8, a2);
func6((__int64)&g_qFileName, (__int64)v9);
sem_post(&g_sFileName);
pthread_mutex_unlock(&g_mFileName);
++*a4;
}
return 0LL;
}

original_code:
static int doScanDirectory( int i, char *dirEntry, int isDirectory, void *data)
{
   int *n = (int*)data;
   if (!isDirectory)
   {
      size_t l = strlen(dirEntry);
      char *ch = dirEntry + (l - 4);
      if (
            ('.' == ch[0]) &&
            ('W' == myToupper(ch[1])) &&
            ('A' == myToupper(ch[2])) &&
            ('V' == myToupper(ch[3]))
         )
      {
         pthread_mutex_lock(&g_mFileName);
         txt_qFLPushFifo(&g_qFileName, strcpy(malloc(l+1),dirEntry));
         sem_post(&g_sFileName);
         pthread_mutex_unlock(&g_mFileName);
         ++(*n);
      }
   }
   return 0;
}

canonical_original_code:
static int doScanDirectory(int i, char *dirEntry, int isDirectory, void *data)
{
   int *n = (int*)data;
   if (!isDirectory)
   {
      unsigned long l = strlen(dirEntry);
      char *ch = dirEntry + (l - 4);
      if (
            ('.' == ch[0]) &&
            ('W' == myToupper(ch[1])) &&
            ('A' == myToupper(ch[2])) &&
            ('V' == myToupper(ch[3]))
         )
      {
         pthread_mutex_lock(&g_mFileName);
         txt_qFLPushFifo(&g_qFileName, strcpy(malloc(l+1),dirEntry));
         sem_post(&g_sFileName);
         pthread_mutex_unlock(&g_mFileName);
         ++(*n);
      }
   }
   return 0;
}

variable_types:
dirEntry
char *

variable_types: isDirectory int

variable_types:
data
void *

variable_types:
n
int *

variable_types:
l
unsigned long

variable_types:
ch
char *


prnPcmHeader

decompiled_code:
__int64 __fastcall prnPcmHeader(__int64 a1)
{
unblockedMessage("RIFF header :%s\n", (const char *)a1);
unblockedMessage("Chunk size :%d\n", *(unsigned int *)(a1 + 8));
unblockedMessage("WAVE header :%s\n", (const char *)(a1 + 12));
unblockedMessage("FMT :%s\n", (const char *)(a1 + 17));
unblockedMessage("Subchunk1 size :%d\n", *(unsigned int *)(a1 + 24));
unblockedMessage("Audio format :%d\n", (unsigned int)*(__int16 *)(a1 + 28));
unblockedMessage("Num of chans :%d\n", (unsigned int)*(__int16 *)(a1 + 30));
unblockedMessage("Samples per sec :%d\n", *(unsigned int *)(a1 + 32));
unblockedMessage("Bytes per sec :%d\n", *(unsigned int *)(a1 + 36));
unblockedMessage("Block align :%d\n", (unsigned int)*(__int16 *)(a1 + 40));
unblockedMessage("Bits per sample :%d\n", (unsigned int)*(__int16 *)(a1 + 42));
unblockedMessage("Subchunk2 ID :%s\n", (const char *)(a1 + 44));
return unblockedMessage("Subchunk2 size :%d\n", *(unsigned int *)(a1 + 52));
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
unblockedMessage("RIFF header :%s\n", (const char *)a1);
unblockedMessage("Chunk size :%d\n", *(unsigned int *)(a1 + 8));
unblockedMessage("WAVE header :%s\n", (const char *)(a1 + 12));
unblockedMessage("FMT :%s\n", (const char *)(a1 + 17));
unblockedMessage("Subchunk1 size :%d\n", *(unsigned int *)(a1 + 24));
unblockedMessage("Audio format :%d\n", (unsigned int)*(__int16 *)(a1 + 28));
unblockedMessage("Num of chans :%d\n", (unsigned int)*(__int16 *)(a1 + 30));
unblockedMessage("Samples per sec :%d\n", *(unsigned int *)(a1 + 32));
unblockedMessage("Bytes per sec :%d\n", *(unsigned int *)(a1 + 36));
unblockedMessage("Block align :%d\n", (unsigned int)*(__int16 *)(a1 + 40));
unblockedMessage("Bits per sample :%d\n", (unsigned int)*(__int16 *)(a1 + 42));
unblockedMessage("Subchunk2 ID :%s\n", (const char *)(a1 + 44));
return unblockedMessage("Subchunk2 size :%d\n", *(unsigned int *)(a1 + 52));
}

original_code:
void prnPcmHeader(wav_hdr_t *hdr)
{
   unblockedMessage("RIFF header     :%s\n", hdr->RIFF);
   unblockedMessage("Chunk size      :%" "d" "\n", hdr->chunkSize);
   unblockedMessage("WAVE header     :%s\n", hdr->WAVE);
   unblockedMessage("FMT             :%s\n", hdr->FMT);
   unblockedMessage("Subchunk1 size  :%" "d" "\n", hdr->subchunk1Size);
   unblockedMessage("Audio format    :%" "d" "\n", hdr->AudioFormat);
   unblockedMessage("Num of chans    :%" "d" "\n", hdr->NumChannels);
   unblockedMessage("Samples per sec :%" "d" "\n", hdr->sampleRate);
   unblockedMessage("Bytes per sec   :%" "d" "\n", hdr->byteRate);
   unblockedMessage("Block align     :%" "d" "\n", hdr->blockAlign);
   unblockedMessage("Bits per sample :%" "d" "\n", hdr->bitsPerSample);
   unblockedMessage("Subchunk2 ID    :%s\n", hdr->subchunk2ID);
   unblockedMessage("Subchunk2 size  :%" "d" "\n", hdr->subchunk2Size);
}

canonical_original_code:
void prnPcmHeader(struct wav_hdr_t *hdr)
{
   unblockedMessage("RIFF header     :%s\n", hdr->RIFF);
   unblockedMessage("Chunk size      :%" "d" "\n", hdr->chunkSize);
   unblockedMessage("WAVE header     :%s\n", hdr->WAVE);
   unblockedMessage("FMT             :%s\n", hdr->FMT);
   unblockedMessage("Subchunk1 size  :%" "d" "\n", hdr->subchunk1Size);
   unblockedMessage("Audio format    :%" "d" "\n", hdr->AudioFormat);
   unblockedMessage("Num of chans    :%" "d" "\n", hdr->NumChannels);
   unblockedMessage("Samples per sec :%" "d" "\n", hdr->sampleRate);
   unblockedMessage("Bytes per sec   :%" "d" "\n", hdr->byteRate);
   unblockedMessage("Block align     :%" "d" "\n", hdr->blockAlign);
   unblockedMessage("Bits per sample :%" "d" "\n", hdr->bitsPerSample);
   unblockedMessage("Subchunk2 ID    :%s\n", hdr->subchunk2ID);
   unblockedMessage("Subchunk2 size  :%" "d" "\n", hdr->subchunk2Size);
}

variable_types: struct wav_hdr_t { char RIFF[5]; int chunkSize; char WAVE[5]; char FMT[5]; int subchunk1Size; short AudioFormat; short NumChannels; int sampleRate; int byteRate; short blockAlign; short bitsPerSample; char subchunk2ID[5]; int subchunk2Size; } *


startWorker

decompiled_code:
void *__fastcall startWorker(void *a1)
{
__int64 v1; // rbx

v1 = ((__int64)a1 - g_pool) >> 3;
message(" started worker %d\n", (unsigned int)v1);
sem_post(&g_sAllThreadsReady);
theWorker(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func13(void *a1)
{
__int64 v1; // rbx

v1 = ((__int64)a1 - g_pool) >> 3;
message(" started worker %d\n", (unsigned int)v1);
sem_post(&g_sAllThreadsReady);
func12(v1);
return 0LL;
}

original_code:
static void *startWorker (void *ptr)
{
   int id;
   {
      pool_t *p = (pool_t*)ptr;
      id = p - g_pool;
   }
   message(" started worker %d\n", id);
   sem_post(&g_sAllThreadsReady);
   theWorker(id);
   return ((void *)0);
}

canonical_original_code:
static void *startWorker(void *ptr)
{
   int id;
   {
      unsigned long *p = (pool_t*)ptr;
      id = p - g_pool;
   }
   message(" started worker %d\n", id);
   sem_post(&g_sAllThreadsReady);
   theWorker(id);
   return ((void *)0);
}

variable_types:
ptr
void *

variable_types: id int

variable_types:
p
unsigned long *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
__int64 v4; // rdx
int v5; // eax
__int64 v6; // rbx
int v7; // ebx
int v8; // ebx
__int64 v9; // rbx

if ( argc != 2 )
halt(10LL, "usage: %s pathname\n", *argv);
v3 = 0LL;
LODWORD(v4) = 0;
if ( *argv[1] )
{
while ( 1 )
{
g_pathname[v3] = argv[1][v3];
v4 = (unsigned int)++v3;
if ( !argv[1][v3] )
break;
if ( (int)v3 > 2046 )
halt(15LL, "Too long path name\n", v4);
}
}
g_pathnameLength = v4;
if ( g_pathname[(int)v4 - 1] != 47 )
{
g_pathnameLength = v4 + 1;
g_pathname[(int)v4] = 47;
}
g_pathname[g_pathnameLength] = 0;
v5 = getCpuNumber();
if ( !g_nWorkers )
g_nWorkers = v5 + v5 / 2;
if ( sem_init(&g_sFileName, 0, 0) == -1 || sem_init(&g_sAllThreadsReady, 0, 0) == -1 )
halt(10LL, "Error initialising global semaphores\n");
pthread_mutex_lock(&g_mEndScan);
txt_qFLInit(0LL, 0, g_qFileName, 0);
g_pool = (__int64)malloc(8LL * g_nWorkers);
if ( !g_pool )
halt(15LL, "malloc fails\n");
if ( g_nWorkers > 0 )
{
v6 = 0LL;
do
{
if ( pthread_create((pthread_t *)(g_pool + 8 * v6), 0LL, startWorker, (void *)(g_pool + 8 * v6)) )
halt(20LL, "Can't start worker %d of %d\n", (unsigned int)v6, (unsigned int)g_nWorkers);
++v6;
}
while ( g_nWorkers > (int)v6 );
}
message("Waiting init\n");
v7 = g_nWorkers;
if ( g_nWorkers > 0 )
{
do
{
sem_wait(&g_sAllThreadsReady);
--v7;
}
while ( v7 );
}
message("Everybody is ready, start scanner\n");
v8 = scanDirectory((__int64)argv[1]);
if ( v8 < 0 )
halt(2LL, "Internal error\n");
message("%d .wav files found\n", (unsigned int)v8);
pthread_mutex_unlock(&g_mEndScan);
if ( g_nWorkers > 0 )
{
v9 = 0LL;
do
pthread_join(*(_QWORD *)(g_pool + 8 * v9++), 0LL);
while ( g_nWorkers > (int)v9 );
}
message("\n%u files converted\n", (unsigned int)g_totalConverted);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
__int64 v4; // rdx
int v5; // eax
__int64 v6; // rbx
int v7; // ebx
int v8; // ebx
__int64 v9; // rbx

if ( argc != 2 )
halt(10LL, "usage: %s pathname\n", *argv);
v3 = 0LL;
LODWORD(v4) = 0;
if ( *argv[1] )
{
while ( 1 )
{
g_pathname[v3] = argv[1][v3];
v4 = (unsigned int)++v3;
if ( !argv[1][v3] )
break;
if ( (int)v3 > 2046 )
halt(15LL, "Too long path name\n", v4);
}
}
g_pathnameLength = v4;
if ( g_pathname[(int)v4 - 1] != 47 )
{
g_pathnameLength = v4 + 1;
g_pathname[(int)v4] = 47;
}
g_pathname[g_pathnameLength] = 0;
v5 = func4();
if ( !g_nWorkers )
g_nWorkers = v5 + v5 / 2;
if ( sem_init(&g_sFileName, 0, 0) == -1 || sem_init(&g_sAllThreadsReady, 0, 0) == -1 )
halt(10LL, "Error initialising global semaphores\n");
pthread_mutex_lock(&g_mEndScan);
txt_qFLInit(0LL, 0, g_qFileName, 0);
g_pool = (__int64)malloc(8LL * g_nWorkers);
if ( !g_pool )
halt(15LL, "malloc fails\n");
if ( g_nWorkers > 0 )
{
v6 = 0LL;
do
{
if ( pthread_create((pthread_t *)(g_pool + 8 * v6), 0LL, startWorker, (void *)(g_pool + 8 * v6)) )
halt(20LL, "Can't start worker %d of %d\n", (unsigned int)v6, (unsigned int)g_nWorkers);
++v6;
}
while ( g_nWorkers > (int)v6 );
}
message("Waiting init\n");
v7 = g_nWorkers;
if ( g_nWorkers > 0 )
{
do
{
sem_wait(&g_sAllThreadsReady);
--v7;
}
while ( v7 );
}
message("Everybody is ready, start scanner\n");
v8 = func7((__int64)argv[1]);
if ( v8 < 0 )
halt(2LL, "Internal error\n");
message("%d .wav files found\n", (unsigned int)v8);
pthread_mutex_unlock(&g_mEndScan);
if ( g_nWorkers > 0 )
{
v9 = 0LL;
do
pthread_join(*(_QWORD *)(g_pool + 8 * v9++), 0LL);
while ( g_nWorkers > (int)v9 );
}
message("\n%u files converted\n", (unsigned int)g_totalConverted);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
   int i;
   if (2 != argc)
   {
      halt(10, "usage: %s pathname\n", argv[0]);
   }
   for (i = 0; '\0' != argv[1][i]; ++i)
   {
      if (i >= 2048 - 1)
      {
         halt(15, "Too long path name\n");
      }
      g_pathname[i] = argv[1][i];
   }
   g_pathnameLength = i;
   if ('/' != g_pathname[g_pathnameLength - 1])
   {
      g_pathname[g_pathnameLength++] = '/';
   }
   g_pathname[g_pathnameLength] = '\0';
   g_cpuNumber = getCpuNumber();
   if (0 == g_nWorkers)
   {
      g_nWorkers = g_cpuNumber + g_cpuNumber / 2;
   }
   if (
        (sem_init(&g_sFileName, 0, 0) == -1)||
        (sem_init(&g_sAllThreadsReady, 0, 0) == -1)
      )
   {
      halt(10, "Error initialising global semaphores\n");
   }
   pthread_mutex_lock(&g_mEndScan);
   txt_qFLInit(((void *)0), 0, &g_qFileName, 0);
   g_pool = malloc( g_nWorkers * sizeof(pool_t) );
   if (((void *)0) == g_pool )
   {
       halt(15, "malloc fails\n");
   }
   for(i = 0; i < g_nWorkers; ++i)
   {
      if(
            pthread_create (g_pool + i,
                         ((void *)0), startWorker,
                        (void*)(g_pool + i))
        )
      {
         halt(20, "Can't start worker %d of %d\n", i, g_nWorkers);
      }
   }
   message("Waiting init\n");
   for(i = g_nWorkers; i > 0; --i)
   {
      sem_wait(&g_sAllThreadsReady);
   }
   message("Everybody is ready, start scanner\n");
   i = scanDirectory( argv[1]);
   if ( i < 0 )
   {
      halt(2, "Internal error\n");
   }
   message("%d .wav files found\n", i);
   pthread_mutex_unlock(&g_mEndScan);
   for(i = 0; i < g_nWorkers; ++i)
   {
      pthread_join(g_pool[i], ((void *)0));
   }
   message("\n%u files converted\n", g_totalConverted);
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
   int i;
   if (2 != argc)
   {
      halt(10, "usage: %s pathname\n", argv[0]);
   }
   for (i = 0; '\0' != argv[1][i]; ++i)
   {
      if (i >= 2048 - 1)
      {
         halt(15, "Too long path name\n");
      }
      g_pathname[i] = argv[1][i];
   }
   g_pathnameLength = i;
   if ('/' != g_pathname[g_pathnameLength - 1])
   {
      g_pathname[g_pathnameLength++] = '/';
   }
   g_pathname[g_pathnameLength] = '\0';
   g_cpuNumber = getCpuNumber();
   if (0 == g_nWorkers)
   {
      g_nWorkers = g_cpuNumber + g_cpuNumber / 2;
   }
   if (
        (sem_init(&g_sFileName, 0, 0) == -1)||
        (sem_init(&g_sAllThreadsReady, 0, 0) == -1)
      )
   {
      halt(10, "Error initialising global semaphores\n");
   }
   pthread_mutex_lock(&g_mEndScan);
   txt_qFLInit(((void *)0), 0, &g_qFileName, 0);
   g_pool = malloc( g_nWorkers * sizeof(pool_t) );
   if (((void *)0) == g_pool )
   {
       halt(15, "malloc fails\n");
   }
   for(i = 0; i < g_nWorkers; ++i)
   {
      if(
            pthread_create (g_pool + i,
                         ((void *)0), startWorker,
                        (void *)(g_pool + i))
        )
      {
         halt(20, "Can't start worker %d of %d\n", i, g_nWorkers);
      }
   }
   message("Waiting init\n");
   for(i = g_nWorkers; i > 0; --i)
   {
      sem_wait(&g_sAllThreadsReady);
   }
   message("Everybody is ready, start scanner\n");
   i = scanDirectory( argv[1]);
   if ( i < 0 )
   {
      halt(2, "Internal error\n");
   }
   message("%d .wav files found\n", i);
   pthread_mutex_unlock(&g_mEndScan);
   for(i = 0; i < g_nWorkers; ++i)
   {
      pthread_join(g_pool[i], ((void *)0));
   }
   message("\n%u files converted\n", g_totalConverted);
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int


isLogOpen

decompiled_code:
_BOOL8 isLogOpen()
{
return l_logFile != 0LL;
}

canonical_decompiled_code:
_BOOL8 func0()
{
return l_logFile != 0LL;
}

original_code:
int isLogOpen(void)
{
   return (((void *)0) != l_logFile);
}

canonical_original_code:
int isLogOpen()
{
   return (((void *)0) != l_logFile);
}


openLog

decompiled_code:
_BOOL8 __fastcall openLog(char *filename)
{
_BOOL8 result; // rax
FILE *v2; // rax
FILE *v3; // rbx

if ( l_logFile )
{
fclose(l_logFile);
l_logFile = 0LL;
}
if ( !filename )
return 0LL;
result = 0LL;
if ( *filename )
{
v2 = fopen(filename, "w");
v3 = v2;
l_logFile = v2;
if ( v2 )
setlinebuf(v2);
result = v3 == 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(char *filename)
{
_BOOL8 result; // rax
FILE *v2; // rax
FILE *v3; // rbx

if ( l_logFile )
{
fclose(l_logFile);
l_logFile = 0LL;
}
if ( !filename )
return 0LL;
result = 0LL;
if ( *filename )
{
v2 = fopen(filename, "w");
v3 = v2;
l_logFile = v2;
if ( v2 )
setlinebuf(v2);
result = v3 == 0LL;
}
return result;
}

original_code:
int openLog (char *logFileName)
{
   if (((void *)0) != l_logFile)
   {
      fclose (l_logFile);
      l_logFile = ((void *)0);
   }
   if ( (((void *)0) == logFileName) || ('\0' == *logFileName) )
   {
      return 0;
   }
   l_logFile = fopen (logFileName, "w");
   if (((void *)0) != l_logFile)
   {
      setlinebuf(l_logFile);
   }
   return (((void *)0) == l_logFile);
}

canonical_original_code:
int openLog(char *logFileName)
{
   if (((void *)0) != l_logFile)
   {
      fclose (l_logFile);
      l_logFile = ((void *)0);
   }
   if ( (((void *)0) == logFileName) || ('\0' == *logFileName) )
   {
      return 0;
   }
   l_logFile = fopen (logFileName, "w");
   if (((void *)0) != l_logFile)
   {
      setlinebuf(l_logFile);
   }
   return (((void *)0) == l_logFile);
}

variable_types:
logFileName
char *


errorMsg

decompiled_code:
int errorMsg(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
pthread_mutex_lock(&g_mIO);
vfprintf(stderr, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
vfprintf(l_logFile, format, arg);
}
return pthread_mutex_unlock(&g_mIO);
}

canonical_decompiled_code:
int func2(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
pthread_mutex_lock(&g_mIO);
vfprintf(stderr, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
vfprintf(l_logFile, format, arg);
}
return pthread_mutex_unlock(&g_mIO);
}

original_code:
void errorMsg(char *fmt, ...)
{
  va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  pthread_mutex_lock( &g_mIO);
  vfprintf(stderr,fmt,arg_ptr);
  if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  pthread_mutex_unlock( &g_mIO);
  __builtin_va_end(arg_ptr);
}

canonical_original_code:
void errorMsg(char *fmt, ...)
{
  __builtin_va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  pthread_mutex_lock( &g_mIO);
  vfprintf(stderr,fmt,arg_ptr);
  if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  pthread_mutex_unlock( &g_mIO);
  __builtin_va_end(arg_ptr);
}

variable_types:
fmt
char *


unblockedMessage

decompiled_code:
int unblockedMessage(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
result = vfprintf(stdout, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
result = vfprintf(l_logFile, format, arg);
}
return result;
}

canonical_decompiled_code:
int func3(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
result = vfprintf(stdout, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
result = vfprintf(l_logFile, format, arg);
}
return result;
}

original_code:
void unblockedMessage(char *fmt, ...)
{
  va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  vfprintf(stdout,fmt,arg_ptr);
  if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  __builtin_va_end(arg_ptr);
}

canonical_original_code:
void unblockedMessage(char *fmt, ...)
{
  __builtin_va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  vfprintf(stdout,fmt,arg_ptr);
  if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  __builtin_va_end(arg_ptr);
}


message

decompiled_code:
int message(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
pthread_mutex_lock(&g_mIO);
vfprintf(stdout, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
vfprintf(l_logFile, format, arg);
}
return pthread_mutex_unlock(&g_mIO);
}

canonical_decompiled_code:
int func4(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
pthread_mutex_lock(&g_mIO);
vfprintf(stdout, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
vfprintf(l_logFile, format, arg);
}
return pthread_mutex_unlock(&g_mIO);
}

original_code:
void message(char *fmt, ...)
{
  va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  pthread_mutex_lock( &g_mIO);
  vfprintf(stdout,fmt,arg_ptr);
  if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  pthread_mutex_unlock( &g_mIO);
  __builtin_va_end(arg_ptr);
}

canonical_original_code:
void message(char *fmt, ...)
{
  __builtin_va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  pthread_mutex_lock( &g_mIO);
  vfprintf(stdout,fmt,arg_ptr);
  if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  pthread_mutex_unlock( &g_mIO);
  __builtin_va_end(arg_ptr);
}


blockMessage

decompiled_code:
int blockMessage()
{
return pthread_mutex_lock(&g_mIO);
}

canonical_decompiled_code:
int func5()
{
return pthread_mutex_lock(&g_mIO);
}

original_code:
void blockMessage(void)
{
  pthread_mutex_lock (&g_mIO);
}

canonical_original_code:
void blockMessage()
{
  pthread_mutex_lock (&g_mIO);
}


unblockMessage

decompiled_code:
int unblockMessage()
{
return pthread_mutex_unlock(&g_mIO);
}

canonical_decompiled_code:
int func6()
{
return pthread_mutex_unlock(&g_mIO);
}

original_code:
void unblockMessage(void)
{
  pthread_mutex_unlock (&g_mIO);
}

canonical_original_code:
void unblockMessage()
{
  pthread_mutex_unlock (&g_mIO);
}


halt

decompiled_code:
void __noreturn halt(int status, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
pthread_mutex_lock(&g_mIO);
vfprintf(stderr, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
vfprintf(l_logFile, format, arg);
}
pthread_mutex_unlock(&g_mIO);
exit(status);
}

canonical_decompiled_code:
void __noreturn func7(int status, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
pthread_mutex_lock(&g_mIO);
vfprintf(stderr, format, arg);
if ( l_logFile )
{
va_end(arg);
va_start(arg, format);
vfprintf(l_logFile, format, arg);
}
pthread_mutex_unlock(&g_mIO);
exit(status);
}

original_code:
void halt(int retval,char *fmt, ...)
{
  va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  pthread_mutex_lock( &g_mIO);
  vfprintf(stderr,fmt,arg_ptr);
 if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  __builtin_va_end(arg_ptr);
  pthread_mutex_unlock( &g_mIO);
  exit(retval);
}

canonical_original_code:
void halt(int retval, char *fmt, ...)
{
  __builtin_va_list arg_ptr;
  __builtin_va_start(arg_ptr,fmt);
  pthread_mutex_lock( &g_mIO);
  vfprintf(stderr,fmt,arg_ptr);
 if (((void *)0) != l_logFile)
  {
     __builtin_va_start(arg_ptr,fmt);
     vfprintf(l_logFile,fmt,arg_ptr);
  }
  __builtin_va_end(arg_ptr);
  pthread_mutex_unlock( &g_mIO);
  exit(retval);
}

variable_types: retval int


main

repo: mtippett/LEDSign

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r14
int v4; // ebp
int v5; // eax
unsigned int v6; // ebx
const char *v7; // rbx
size_t v8; // rax
int v9; // ecx
int v10; // er15
__int64 v11; // rax
unsigned int v12; // er12
__int64 v13; // rbx
const char *v14; // rbp
int v16; // [rsp+Ch] [rbp-BCh]
int longind; // [rsp+14h] [rbp-B4h] BYREF
time_t timer; // [rsp+18h] [rbp-B0h] BYREF
char *s[6]; // [rsp+20h] [rbp-A8h] BYREF
struct termios termios_p; // [rsp+50h] [rbp-78h] BYREF

v16 = 0;
v3 = 0LL;
while ( 1 )
{
v4 = _bss_start;
if ( !_bss_start )
v4 = 1;
longind = 0;
v5 = getopt_long(argc, (char *const *)argv, "t:d:0:1:2:3:4:5:6:", &long_options_0, &longind);
v6 = v5;
if ( v5 == -1 )
break;
if ( v5 == 100 )
{
printf("option device with option %s\n", optarg);
v7 = optarg;
v8 = strlen(optarg);
v3 = (char *)malloc(v8);
strcpy(v3, v7);
}
else if ( v5 > 100 )
{
if ( v5 == 116 )
{
if ( optarg )
{
date = (__int64)getdate(optarg);
if ( !date )
{
printf("Invalid date string \"%s\" code %d\n", optarg, (unsigned int)getdate_err);
exit(1);
}
}
else
{
timer = time(0LL);
date = (__int64)localtime(&timer);
}
printf("Time f %s\n", optarg);
}
else
{
LABEL_10:
printf("?? getopt returned character code 0%o ??\n", (unsigned int)v5);
}
}
else if ( v5 > 54 )
{
if ( v5 != 63 )
goto LABEL_10;
}
else
{
if ( v5 <= 47 )
goto LABEL_10;
if ( v16 )
{
if ( v16 != v4 )
puts("digits occur in two different argv-elements.");
}
printf("option %c,%s\n", v6, optarg);
v16 = v4;
}
}
v9 = _bss_start;
v10 = 0;
if ( _bss_start < argc )
{
v11 = _bss_start + 1;
do
{
(&s[v11])[-v9 - 1] = (char *)argv[v11 - 1];
_bss_start = v11++;
}
while ( v11 != v9 + (unsigned __int64)(unsigned int)(argc + ~v9) + 2 );
v10 = argc - v9;
printf("message_count %d\n", (unsigned int)(argc - v9));
}
if ( !v3 )
{
puts("Device name must be specified with -d or --device");
exit(-1);
}
printf("Opening file %s\n", v3);
v12 = open(v3, 3394);
if ( v3 == strstr(v3, "/dev/") )
{
puts("Assuming tty semantics");
tcgetattr(v12, &termios_p);
cfsetspeed(&termios_p, 0xFu);
tcsetattr(v12, 0, &termios_p);
}
puts("Attention");
sign_proto_attention(v12);
usleep(0x186A0u);
if ( v10 <= 0 )
{
v10 = 0;
}
else
{
v13 = 0LL;
do
{
v14 = s[v13];
printf("Write buffer %d with %s\n", (unsigned int)v13, v14);
strlen(v14);
sign_proto_write_buffer(v12);
usleep(0x15F90u);
++v13;
}
while ( v10 > (int)v13 );
}
puts("Activate");
sign_proto_activate(v12, (unsigned int)v10);
usleep(0x15F90u);
if ( date )
{
puts("Set Time");
sign_proto_set_time(v12);
usleep(0x15F90u);
}
puts("Close");
close(v12);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r14
int v4; // ebp
int v5; // eax
unsigned int v6; // ebx
const char *v7; // rbx
size_t v8; // rax
int v9; // ecx
int v10; // er15
__int64 v11; // rax
unsigned int v12; // er12
__int64 v13; // rbx
const char *v14; // rbp
int v16; // [rsp+Ch] [rbp-BCh]
int longind; // [rsp+14h] [rbp-B4h] BYREF
time_t timer; // [rsp+18h] [rbp-B0h] BYREF
char *s[6]; // [rsp+20h] [rbp-A8h] BYREF
struct termios termios_p; // [rsp+50h] [rbp-78h] BYREF

v16 = 0;
v3 = 0LL;
while ( 1 )
{
v4 = _bss_start;
if ( !_bss_start )
v4 = 1;
longind = 0;
v5 = getopt_long(argc, (char *const *)argv, "t:d:0:1:2:3:4:5:6:", &long_options_0, &longind);
v6 = v5;
if ( v5 == -1 )
break;
if ( v5 == 100 )
{
printf("option device with option %s\n", optarg);
v7 = optarg;
v8 = strlen(optarg);
v3 = (char *)malloc(v8);
strcpy(v3, v7);
}
else if ( v5 > 100 )
{
if ( v5 == 116 )
{
if ( optarg )
{
date = (__int64)getdate(optarg);
if ( !date )
{
printf("Invalid date string \"%s\" code %d\n", optarg, (unsigned int)getdate_err);
exit(1);
}
}
else
{
timer = time(0LL);
date = (__int64)localtime(&timer);
}
printf("Time f %s\n", optarg);
}
else
{
LABEL_10:
printf("?? getopt returned character code 0%o ??\n", (unsigned int)v5);
}
}
else if ( v5 > 54 )
{
if ( v5 != 63 )
goto LABEL_10;
}
else
{
if ( v5 <= 47 )
goto LABEL_10;
if ( v16 )
{
if ( v16 != v4 )
puts("digits occur in two different argv-elements.");
}
printf("option %c,%s\n", v6, optarg);
v16 = v4;
}
}
v9 = _bss_start;
v10 = 0;
if ( _bss_start < argc )
{
v11 = _bss_start + 1;
do
{
(&s[v11])[-v9 - 1] = (char *)argv[v11 - 1];
_bss_start = v11++;
}
while ( v11 != v9 + (unsigned __int64)(unsigned int)(argc + ~v9) + 2 );
v10 = argc - v9;
printf("message_count %d\n", (unsigned int)(argc - v9));
}
if ( !v3 )
{
puts("Device name must be specified with -d or --device");
exit(-1);
}
printf("Opening file %s\n", v3);
v12 = open(v3, 3394);
if ( v3 == strstr(v3, "/dev/") )
{
puts("Assuming tty semantics");
tcgetattr(v12, &termios_p);
cfsetspeed(&termios_p, 0xFu);
tcsetattr(v12, 0, &termios_p);
}
puts("Attention");
func8(v12);
usleep(0x186A0u);
if ( v10 <= 0 )
{
v10 = 0;
}
else
{
v13 = 0LL;
do
{
v14 = s[v13];
printf("Write buffer %d with %s\n", (unsigned int)v13, v14);
strlen(v14);
func11(v12);
usleep(0x15F90u);
++v13;
}
while ( v10 > (int)v13 );
}
puts("Activate");
func9(v12, (unsigned int)v10);
usleep(0x15F90u);
if ( date )
{
puts("Set Time");
func12(v12);
usleep(0x15F90u);
}
puts("Close");
close(v12);
return 0;
}

original_code:
int main(int argc, char **argv) {
    int testfile;
    struct termios term;
    char *device_name = ((void *)0);
    char *my_string;
    char *messages[6];
    int message_count = 0;
    int c;
    int digit_optind = 0;
    my_string = (char *) malloc(250);
    while (1) {
               int this_option_optind = optind ? optind : 1;
               int option_index = 0;
               static struct option long_options[] = {
                   {"device", 1, ((void *)0), 'd'},
                   {"time", 2, ((void *)0), 't'},
                   {0, 0, 0, 0}
               };
               c = getopt_long(argc, argv, "t:d:0:1:2:3:4:5:6:",
                        long_options, &option_index);
               if (c == -1)
                   break;
               switch (c) {
               case '0':
               case '1':
               case '2':
               case '3':
               case '4':
               case '5':
               case '6':
                   if (digit_optind != 0 && digit_optind != this_option_optind)
                     printf("digits occur in two different argv-elements.\n");
                   digit_optind = this_option_optind;
                   printf("option %c,%s\n", c, optarg);
                   break;
               case 'd':
                   printf("option device with option %s\n", optarg);
            device_name = malloc(strlen(optarg));
     strcpy(device_name, optarg);
                   break;
               case 't':
     if (optarg != ((void *)0)) {
   date = getdate(optarg);
   if (date == ((void *)0)) {
    printf("Invalid date string \"%s\" code %d\n",optarg,getdate_err);
    exit(1);
   }
     } else {
          time_t current_time;
          current_time = time(((void *)0));
          date = localtime(&current_time);
            }
                   printf("Time f %s\n", optarg);
                   break;
               case '?':
                   break;
               default:
                   printf("?? getopt returned character code 0%o ??\n", c);
               }
           }
    if (optind < argc) {
        while (optind < argc) {
  messages[message_count] = argv[optind];
  message_count++;
  optind++;
 }
        printf("message_count %d\n", message_count);
    }
    if(device_name == ((void *)0)) {
 printf("Device name must be specified with -d or --device\n");
 exit(-1);
    }
    printf("Opening file %s\n", device_name);
    testfile = open(device_name, 02 | 0400 | 0100| 04000|02000);
    if(strstr(device_name, "/dev/") == device_name) {
     printf("Assuming tty semantics\n");
     tcgetattr(testfile, &term);
 cfsetspeed(&term,0000017);
 tcsetattr(testfile,0,&term);
    }
    printf("Attention\n");
    sign_proto_attention(testfile);
    usleep(100000);
    for( c= 0; c < message_count; c++ ) {
     printf("Write buffer %d with %s\n", c, messages[c]);
        sign_proto_write_buffer(testfile, c, messages[c], strlen(messages[c]), 0x35, 0x42);
        usleep(90000);
    }
    printf("Activate\n");
    sign_proto_activate(testfile,c);
    usleep(90000);
    if (date != ((void *)0)) {
 printf("Set Time\n");
 sign_proto_set_time(testfile, date);
     usleep(90000);
    }
    printf("Close\n");
    close(testfile);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    int testfile;
    struct termios term;
    char *device_name = ((void *)0);
    char *my_string;
    char *messages[6];
    int message_count = 0;
    int c;
    int digit_optind = 0;
    my_string = (char *) malloc(250);
    while (1) {
               int this_option_optind = optind ? optind : 1;
               int option_index = 0;
               static struct option long_options[] = {
                   {"device", 1, ((void *)0), 'd'},
                   {"time", 2, ((void *)0), 't'},
                   {0, 0, 0, 0}
               };
               c = getopt_long(argc, argv, "t:d:0:1:2:3:4:5:6:",
                        long_options, &option_index);
               if (c == -1)
                   break;
               switch (c) {
               case '0':
               case '1':
               case '2':
               case '3':
               case '4':
               case '5':
               case '6':
                   if (digit_optind != 0 && digit_optind != this_option_optind)
                     printf("digits occur in two different argv-elements.\n");
                   digit_optind = this_option_optind;
                   printf("option %c,%s\n", c, optarg);
                   break;
               case 'd':
                   printf("option device with option %s\n", optarg);
            device_name = malloc(strlen(optarg));
     strcpy(device_name, optarg);
                   break;
               case 't':
     if (optarg != ((void *)0)) {
   date = getdate(optarg);
   if (date == ((void *)0)) {
    printf("Invalid date string \"%s\" code %d\n",optarg,getdate_err);
    exit(1);
   }
     } else {
          long current_time;
          current_time = time(((void *)0));
          date = localtime(&current_time);
            }
                   printf("Time f %s\n", optarg);
                   break;
               case '?':
                   break;
               default:
                   printf("?? getopt returned character code 0%o ??\n", c);
               }
           }
    if (optind < argc) {
        while (optind < argc) {
  messages[message_count] = argv[optind];
  message_count++;
  optind++;
 }
        printf("message_count %d\n", message_count);
    }
    if(device_name == ((void *)0)) {
 printf("Device name must be specified with -d or --device\n");
 exit(-1);
    }
    printf("Opening file %s\n", device_name);
    testfile = open(device_name, 02 | 0400 | 0100| 04000|02000);
    if(strstr(device_name, "/dev/") == device_name) {
     printf("Assuming tty semantics\n");
     tcgetattr(testfile, &term);
 cfsetspeed(&term,0000017);
 tcsetattr(testfile,0,&term);
    }
    printf("Attention\n");
    sign_proto_attention(testfile);
    usleep(100000);
    for( c= 0; c < message_count; c++ ) {
     printf("Write buffer %d with %s\n", c, messages[c]);
        sign_proto_write_buffer(testfile, c, messages[c], strlen(messages[c]), 0x35, 0x42);
        usleep(90000);
    }
    printf("Activate\n");
    sign_proto_activate(testfile,c);
    usleep(90000);
    if (date != ((void *)0)) {
 printf("Set Time\n");
 sign_proto_set_time(testfile, date);
     usleep(90000);
    }
    printf("Close\n");
    close(testfile);
    return 0;
}

variable_types:
argv
char **

variable_types: testfile 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; }

variable_types:
messages
char *[6]

variable_types: c int

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


repo: mtottenh/IMPS

decompiled_code:
void __fastcall symbol_table_free_aux(void ***a1)
{
void **v2; // rdi

v2 = *a1;
if ( v2 )
{
symbol_table_free_aux(v2 + 2);
free(**a1);
**a1 = 0LL;
free(*a1);
*a1 = 0LL;
}
}

original_code:
static void symbol_table_free_aux(Symbol_Table_Entry **entry_ptr) {
 if(*entry_ptr != ((void *)0)) {
  symbol_table_free_aux(&((*entry_ptr)->next));
  free((*entry_ptr)->key);
  (*entry_ptr)->key = ((void *)0);
  free(*entry_ptr);
  *entry_ptr = ((void *)0);
 }
}

canonical_original_code:
static void symbol_table_free_aux(struct Symbol_Table_Entry **entry_ptr) {
 if(*entry_ptr != ((void *)0)) {
  symbol_table_free_aux(&((*entry_ptr)->next));
  free((*entry_ptr)->key);
  (*entry_ptr)->key = ((void *)0);
  free(*entry_ptr);
  *entry_ptr = ((void *)0);
 }
}

variable_types: struct Symbol_Table_Entry { char *key; unsigned short value; struct Symbol_Table_Entry *next; } **


decompiled_code:
void __fastcall symbol_table_free(void ****a1)
{
symbol_table_free_aux(*a1);
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func2(void ****a1)
{
func0(*a1);
free(*a1);
*a1 = 0LL;
}

original_code:
void symbol_table_free(Symbol_Table **entry_ptr) {
 symbol_table_free_aux(&((*entry_ptr)->head));
 free(*entry_ptr);
 *entry_ptr = ((void *)0);
}

canonical_original_code:
void symbol_table_free(struct Symbol_Table **entry_ptr) {
 symbol_table_free_aux(&((*entry_ptr)->head));
 free(*entry_ptr);
 *entry_ptr = ((void *)0);
}

variable_types: struct Symbol_Table { struct Symbol_Table_Entry { char *key; unsigned short value; struct Symbol_Table_Entry *next; } *head; } **

user_defined_types: struct Symbol_Table { struct Symbol_Table_Entry *head; }


decompiled_code:
__int64 __fastcall symbol_table_getLength(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx

if ( !a1 )
return -1LL;
v1 = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
if ( !v1 )
return 0LL;
v2 = 0LL;
do
{
++v2;
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx

if ( !a1 )
return -1LL;
v1 = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
if ( !v1 )
return 0LL;
v2 = 0LL;
do
{
++v2;
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
return v2;
}

original_code:
size_t symbol_table_getLength(Symbol_Table *table) {
 if (table == ((void *)0)) {
  return -1;
 }
 size_t counter = 0;
 for(Symbol_Table_Entry *i = table->head; i->next != ((void *)0); i=i->next) {
  counter++;
 }
 return counter;
}

canonical_original_code:
unsigned long symbol_table_getLength(struct Symbol_Table *table) {
 if (table == ((void *)0)) {
  return -1;
 }
 unsigned long counter = 0;
 for(struct Symbol_Table_Entry *i = table->head; i->next != ((void *)0); i=i->next) {
  counter++;
 }
 return counter;
}

variable_types: struct Symbol_Table { struct Symbol_Table_Entry { char *key; unsigned short value; struct Symbol_Table_Entry *next; } *head; } *

variable_types:
counter
unsigned long