decompiled_code:
int my_segment_init()
{
void *v0; // rax
const void *v1; // rbx
__int64 v2; // rbp
unsigned int v3; // eax

segment_size = sysconf(30);
v0 = mmap(0LL, segment_size, 3, 34, 0, 0LL);
segment_start = (__int64)v0;
if ( v0 == (void *)-1LL )
{
perror("mmap");
exit(1);
}
v1 = v0;
v2 = segment_size;
v3 = barrier_threadid();
return printf("%x : initialized %ld bytes at %p\n", v3, v2, v1);
}

canonical_decompiled_code:
int func0()
{
void *v0; // rax
const void *v1; // rbx
__int64 v2; // rbp
unsigned int v3; // eax

segment_size = sysconf(30);
v0 = mmap(0LL, segment_size, 3, 34, 0, 0LL);
segment_start = (__int64)v0;
if ( v0 == (void *)-1LL )
{
perror("mmap");
exit(1);
}
v1 = v0;
v2 = segment_size;
v3 = barrier_threadid();
return printf("%x : initialized %ld bytes at %p\n", v3, v2, v1);
}

original_code:
void my_segment_init()
{
    segment_size = sysconf(_SC_PAGESIZE);
    segment_start = mmap(((void *)0),
            segment_size,
            0x1 | 0x2,
            0x02 | 0x20,
            0, 0);
    if(segment_start == ((void *) -1)) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mmap"); exit(1); } while (0);
    }
    printf("%x : initialized %ld bytes at %p\n", barrier_threadid(), segment_size, segment_start);
}


decompiled_code:
__int64 my_barrier_init()
{
whitelist = pthread_self();
barrier = segment_start;
*(&barrier + 1) = segment_size;
*(&barrier + 8) = (__int64)&whitelist;
*((_DWORD *)&barrier + 15) = 3;
return barrier_init(&barrier);
}

canonical_decompiled_code:
__int64 func1()
{
whitelist = pthread_self();
barrier = segment_start;
*(&barrier + 1) = segment_size;
*(&barrier + 8) = (__int64)&whitelist;
*((_DWORD *)&barrier + 15) = 3;
return barrier_init(&barrier);
}

original_code:
void my_barrier_init()
{
    whitelist[0] = pthread_self();
    barrier.start = (void*)segment_start;
    barrier.size = segment_size;
    barrier.whitelist = whitelist;
    barrier.original_prot = 0x1 | 0x2;
    barrier_init(&barrier);
}

canonical_original_code:
void my_barrier_init()
{
    whitelist[0] = pthread_self();
    barrier.start = (void *)segment_start;
    barrier.size = segment_size;
    barrier.whitelist = whitelist;
    barrier.original_prot = 0x1 | 0x2;
    barrier_init(&barrier);
}

return_type: void


decompiled_code:
int increment_counter()
{
unsigned int *v0; // roff
unsigned int v1; // ebx
unsigned int v2; // eax

v0 = (unsigned int *)segment_start;
_InterlockedAdd((volatile signed __int32 *)segment_start, 1u);
v1 = *v0;
v2 = barrier_threadid();
return printf("%x : incremented counter to %d\n", v2, v1);
}

canonical_decompiled_code:
int func2()
{
unsigned int *v0; // roff
unsigned int v1; // ebx
unsigned int v2; // eax

v0 = (unsigned int *)segment_start;
_InterlockedAdd((volatile signed __int32 *)segment_start, 1u);
v1 = *v0;
v2 = barrier_threadid();
return printf("%x : incremented counter to %d\n", v2, v1);
}

original_code:
void increment_counter()
{
    volatile sig_atomic_t* counter = (volatile sig_atomic_t*)segment_start;
 __sync_fetch_and_add(counter, 1);
    printf("%x : incremented counter to %d\n", barrier_threadid(), *counter);
}

canonical_original_code:
void increment_counter()
{
    int *counter = (volatile sig_atomic_t*)segment_start;
 __sync_fetch_and_add(counter, 1);
    printf("%x : incremented counter to %d\n", barrier_threadid(), *counter);
}

variable_types:
counter
int *


decompiled_code:
int peek_counter()
{
unsigned int v0; // ebx
unsigned int v1; // eax

v0 = *(_DWORD *)segment_start;
v1 = barrier_threadid();
return printf("%x : it looks like the counter is %d\n", v1, v0);
}

canonical_decompiled_code:
int func3()
{
unsigned int v0; // ebx
unsigned int v1; // eax

v0 = *(_DWORD *)segment_start;
v1 = barrier_threadid();
return printf("%x : it looks like the counter is %d\n", v1, v0);
}

original_code:
void peek_counter()
{
    printf("%x : it looks like the counter is %d\n", barrier_threadid(), *((sig_atomic_t*)segment_start));
}

canonical_original_code:
void peek_counter()
{
    printf("%x : it looks like the counter is %d\n", barrier_threadid(), *((int *)segment_start));
}


decompiled_code:
unsigned int rand_sleep()
{
int v0; // eax

v0 = rand();
return sleep(v0 % 10);
}

canonical_decompiled_code:
unsigned int func4()
{
int v0; // eax

v0 = rand();
return sleep(v0 % 10);
}

original_code:
void rand_sleep()
{
    int sleep_for = rand() % 10;
    sleep(sleep_for);
}


decompiled_code:
void *__fastcall worker_main(void *a1)
{
unsigned int v1; // eax

v1 = barrier_threadid(a1);
printf("%x : started\n", v1);
while ( running )
{
rand_sleep();
increment_counter();
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
unsigned int v1; // eax

v1 = barrier_threadid(a1);
printf("%x : started\n", v1);
while ( running )
{
func4();
func2();
}
return 0LL;
}

original_code:
void* worker_main(void *arg)
{
    printf("%x : started\n", barrier_threadid());
    while(running) {
        rand_sleep();
        increment_counter();
    }
    return 0;
}

canonical_original_code:
void *worker_main(void *arg)
{
    printf("%x : started\n", barrier_threadid());
    while(running) {
        rand_sleep();
        increment_counter();
    }
    return 0;
}

variable_types:
arg
void *

return_type: void *


decompiled_code:
int create_workers()
{
pthread_t *v0; // rbx
int result; // eax

srand(1u);
v0 = &thread;
do
{
result = pthread_create(v0, 0LL, worker_main, 0LL);
if ( result )
{
*_errno_location() = result;
perror("pthread_create");
exit(1);
}
++v0;
}
while ( v0 != (pthread_t *)(unsigned int)((_DWORD)&thread + 80) );
return result;
}

canonical_decompiled_code:
int func6()
{
pthread_t *v0; // rbx
int result; // eax

srand(1u);
v0 = &thread;
do
{
result = pthread_create(v0, 0LL, worker_main, 0LL);
if ( result )
{
*_errno_location() = result;
perror("pthread_create");
exit(1);
}
++v0;
}
while ( v0 != (pthread_t *)(unsigned int)((_DWORD)&thread + 80) );
return result;
}

original_code:
void create_workers()
{
    srand(1);
    int i, code;
    for (i = 0; i < 10; i++) {
        code = pthread_create(&thread[i], ((void *)0), &worker_main, ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_create"); exit(1); } while (0);
    }
}

canonical_original_code:
void create_workers()
{
    srand(1);
    int i; int code;
    for (i = 0; i < 10; i++) {
        code = pthread_create(&thread[i], ((void *)0), &worker_main, ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_create"); exit(1); } while (0);
    }
}

variable_types: i int

variable_types: code int


decompiled_code:
int join_workers()
{
pthread_t *v0; // rbx
pthread_t v1; // rdi
int v2; // ebp
pthread_t v3; // rbp
unsigned int v4; // eax
int result; // eax

v0 = &thread;
do
{
v1 = *v0;
v2 = pthread_join(*v0, 0LL);
if ( v2 )
{
*_errno_location() = v2;
perror("pthread_join");
exit(1);
}
v3 = *v0;
v4 = barrier_threadid(v1);
result = printf("%x : waiting for %x\n", v4, (unsigned int)v3);
++v0;
}
while ( v0 != (pthread_t *)(unsigned int)((_DWORD)&thread + 80) );
return result;
}

canonical_decompiled_code:
int func7()
{
pthread_t *v0; // rbx
pthread_t v1; // rdi
int v2; // ebp
pthread_t v3; // rbp
unsigned int v4; // eax
int result; // eax

v0 = &thread;
do
{
v1 = *v0;
v2 = pthread_join(*v0, 0LL);
if ( v2 )
{
*_errno_location() = v2;
perror("pthread_join");
exit(1);
}
v3 = *v0;
v4 = barrier_threadid(v1);
result = printf("%x : waiting for %x\n", v4, (unsigned int)v3);
++v0;
}
while ( v0 != (pthread_t *)(unsigned int)((_DWORD)&thread + 80) );
return result;
}

original_code:
void join_workers()
{
    int i, code;
    for (i = 0; i < 10; i++) {
        code = pthread_join(thread[i], ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_join"); exit(1); } while (0);
        printf("%x : waiting for %x\n", barrier_threadid(), (unsigned int)thread[i]);
    }
}

canonical_original_code:
void join_workers()
{
    int i; int code;
    for (i = 0; i < 10; i++) {
        code = pthread_join(thread[i], ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_join"); exit(1); } while (0);
        printf("%x : waiting for %x\n", barrier_threadid(), (unsigned int)thread[i]);
    }
}


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

for ( result = (unsigned int)running; running; result = (unsigned int)running )
{
sleep(6u);
barrier_activate(&barrier);
sleep(2u);
peek_counter();
barrier_deactivate(&barrier);
}
return result;
}

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

for ( result = (unsigned int)running; running; result = (unsigned int)running )
{
sleep(6u);
barrier_activate(&barrier);
sleep(2u);
func3();
barrier_deactivate(&barrier);
}
return result;
}

original_code:
void main_loop()
{
    while(running) {
        sleep(6);
        barrier_activate(&barrier);
        sleep(2);
        peek_counter();
        barrier_deactivate(&barrier);
    }
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

my_segment_init();
my_barrier_init(argc, argv);
create_workers();
main_loop();
join_workers();
barrier_destroy(&barrier);
v3 = barrier_threadid(&barrier);
printf("%x : exiting", v3);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

func0();
func1(argc, argv);
func6();
func8();
func7();
barrier_destroy(&barrier);
v3 = barrier_threadid(&barrier);
printf("%x : exiting", v3);
exit(0);
}

original_code:
int main(int argc, char *argv[])
{
    my_segment_init();
    my_barrier_init();
    create_workers();
    main_loop();
    join_workers();
    barrier_destroy(&barrier);
    printf("%x : exiting", barrier_threadid());
    exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


decompiled_code:
int barrierlist_init()
{
init = 1;
return sem_init(&lock, 0, 1u);
}

canonical_decompiled_code:
int func0()
{
init = 1;
return sem_init(&lock, 0, 1u);
}

original_code:
void barrierlist_init()
{
    init = 1;
    sem_init(&lock, 0, 1);
}


decompiled_code:
int __fastcall barrierlist_add(__int64 a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbx
_QWORD *v3; // rax

if ( !init )
barrierlist_init();
sem_wait(&lock);
v1 = &barrierlist;
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v3 = malloc(0x10uLL);
*v3 = a1;
v2[1] = v3;
return sem_post(&lock);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbx
_QWORD *v3; // rax

if ( !init )
func0();
sem_wait(&lock);
v1 = &barrierlist;
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v3 = malloc(0x10uLL);
*v3 = a1;
v2[1] = v3;
return sem_post(&lock);
}

original_code:
void barrierlist_add(struct barrier* barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist* list = &barrierlist;
    while(list->next != 0) {
        list = list->next;
    }
    struct barrierlist* new = (struct barrierlist*)malloc(sizeof(struct barrierlist));
    new->item = barrier;
    list->next = new;
    sem_post(&lock);
}

canonical_original_code:
void barrierlist_add(struct barrier *barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist *list = &barrierlist;
    while(list->next != 0) {
        list = list->next;
    }
    struct barrierlist *new = (struct barrierlist*)malloc(sizeof(struct barrierlist));
    new->item = barrier;
    list->next = new;
    sem_post(&lock);
}

variable_types: struct barrierlist { struct barrier { char *start; unsigned long size; int locked; union sem_t { char __size[32]; long __align; } wait_sem; int waiting; int original_prot; unsigned long *whitelist; } *item; struct barrierlist *next; } *

user_defined_types: struct barrier { char *start; unsigned long size; int locked; union sem_t wait_sem; int waiting; int original_prot; unsigned long *whitelist; }

user_defined_types: struct barrierlist { struct barrier *item; struct barrierlist *next; }


decompiled_code:
int __fastcall barrierlist_remove(__int64 a1)
{
_QWORD *v2; // rdi

if ( !init )
barrierlist_init();
sem_wait(&lock);
v2 = (_QWORD *)*((_QWORD *)&barrierlist + 1);
if ( v2 )
{
while ( *v2 != a1 )
{
v2 = (_QWORD *)v2[1];
if ( !v2 )
return sem_post(&lock);
}
*((_QWORD *)&barrierlist + 1) = v2[1];
free(v2);
}
return sem_post(&lock);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
_QWORD *v2; // rdi

if ( !init )
func0();
sem_wait(&lock);
v2 = (_QWORD *)*((_QWORD *)&barrierlist + 1);
if ( v2 )
{
while ( *v2 != a1 )
{
v2 = (_QWORD *)v2[1];
if ( !v2 )
return sem_post(&lock);
}
*((_QWORD *)&barrierlist + 1) = v2[1];
free(v2);
}
return sem_post(&lock);
}

original_code:
void barrierlist_remove(struct barrier* barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist* list = barrierlist.next;
    struct barrierlist* prev = &barrierlist;
    while(list != 0) {
        if(list->item == barrier) {
            prev->next = list->next;
            free(list);
            break;
        }
        list = list->next;
    }
    sem_post(&lock);
}

canonical_original_code:
void barrierlist_remove(struct barrier *barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist *list = barrierlist.next;
    struct barrierlist *prev = &barrierlist;
    while(list != 0) {
        if(list->item == barrier) {
            prev->next = list->next;
            free(list);
            break;
        }
        list = list->next;
    }
    sem_post(&lock);
}

variable_types: struct barrier { char *start; unsigned long size; int locked; union sem_t { char __size[32]; long __align; } wait_sem; int waiting; int original_prot; unsigned long *whitelist; } *


decompiled_code:
int __fastcall handle_segv(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
pthread_t v4; // rax
const void *v5; // rbx
unsigned int v6; // eax
const void *v7; // rbp
unsigned int v8; // eax
unsigned int v9; // eax

v2 = barrierlist_match(*(_QWORD *)(a2 + 16));
if ( v2 )
{
v3 = v2;
v4 = pthread_self();
if ( (unsigned int)barrier_iswhitelisted(v3, v4) )
{
v5 = *(const void **)v3;
v6 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p; whitelisted\n", v6, v5);
}
else
{
v7 = *(const void **)v3;
v8 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p\n", v8, v7);
++*(_DWORD *)(v3 + 56);
for ( ; *(_DWORD *)(v3 + 16); LODWORD(v2) = printf("%x : woken up by interrupt", v9) )
{
LODWORD(v2) = sem_wait((sem_t *)(v3 + 24));
if ( !(_DWORD)v2 )
break;
if ( *_errno_location() != 4 )
{
perror("sem_wait");
exit(1);
}
v9 = barrier_threadid();
}
}
}
return v2;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
pthread_t v4; // rax
const void *v5; // rbx
unsigned int v6; // eax
const void *v7; // rbp
unsigned int v8; // eax
unsigned int v9; // eax

v2 = barrierlist_match(*(_QWORD *)(a2 + 16));
if ( v2 )
{
v3 = v2;
v4 = pthread_self();
if ( (unsigned int)barrier_iswhitelisted(v3, v4) )
{
v5 = *(const void **)v3;
v6 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p; whitelisted\n", v6, v5);
}
else
{
v7 = *(const void **)v3;
v8 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p\n", v8, v7);
++*(_DWORD *)(v3 + 56);
for ( ; *(_DWORD *)(v3 + 16); LODWORD(v2) = printf("%x : woken up by interrupt", v9) )
{
LODWORD(v2) = sem_wait((sem_t *)(v3 + 24));
if ( !(_DWORD)v2 )
break;
if ( *_errno_location() != 4 )
{
perror("sem_wait");
exit(1);
}
v9 = barrier_threadid();
}
}
}
return v2;
}

original_code:
void handle_segv(int sig, siginfo_t* siginfo, void* context)
{
    struct barrier* barrier = (struct barrier*) barrierlist_match(siginfo->_sifields._sigfault.si_addr);
    if(barrier != 0) {
        if(barrier_iswhitelisted(barrier, pthread_self())) {
            printf("%x : hit memory barrier %p; whitelisted\n", barrier_threadid(), barrier->start);
        } else {
            printf("%x : hit memory barrier %p\n", barrier_threadid(), barrier->start);
            barrier->waiting++;
            while(barrier->locked && sem_wait(&barrier->wait_sem) != 0) {
                if((*__errno_location ()) != 4) {
                    do { (*__errno_location ()) = (*__errno_location ()); perror("sem_wait"); exit(1); } while (0);
                }
                printf("%x : woken up by interrupt", barrier_threadid());
            }
        }
    }
}

canonical_original_code:
void handle_segv(int sig, struct siginfo_t *siginfo, void *context)
{
    struct barrier *barrier = (struct barrier*) barrierlist_match(siginfo->_sifields._sigfault.si_addr);
    if(barrier != 0) {
        if(barrier_iswhitelisted(barrier, pthread_self())) {
            printf("%x : hit memory barrier %p; whitelisted\n", barrier_threadid(), barrier->start);
        } else {
            printf("%x : hit memory barrier %p\n", barrier_threadid(), barrier->start);
            barrier->waiting++;
            while(barrier->locked && sem_wait(&barrier->wait_sem) != 0) {
                if((*__errno_location ()) != 4) {
                    do { (*__errno_location ()) = (*__errno_location ()); perror("sem_wait"); exit(1); } while (0);
                }
                printf("%x : woken up by interrupt", barrier_threadid());
            }
        }
    }
}

variable_types: sig int

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

variable_types:
context
void *

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

user_defined_types: union sem_t { char __size[32]; long __align; }


decompiled_code:
int segvhandler_attach()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

result = attached;
if ( !attached )
{
attached = 1;
v1.sa_flags = 4;
v1.sa_handler = (__sighandler_t)handle_segv;
result = sigaction(11, &v1, 0LL);
if ( result )
{
perror("sigaction");
exit(1);
}
}
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

result = attached;
if ( !attached )
{
attached = 1;
v1.sa_flags = 4;
v1.sa_handler = (__sighandler_t)handle_segv;
result = sigaction(11, &v1, 0LL);
if ( result )
{
perror("sigaction");
exit(1);
}
}
return result;
}

original_code:
void segvhandler_attach()
{
    if(attached)
        return;
    attached = 1;
    struct sigaction newact;
    newact.sa_flags = 4;
    newact.__sigaction_handler.sa_sigaction = &handle_segv;
    if(sigaction(11, &newact, ((void *)0)) != 0) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("sigaction"); exit(1); } while (0);
    }
}

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

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


decompiled_code:
void segvhandler_detatch()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void segvhandler_detatch()
{
}

canonical_original_code:
void segvhandler_detatch()
{
}


decompiled_code:
__int64 segvhandler_isattached()
{
return (unsigned int)attached;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)attached;
}

original_code:
int segvhandler_isattached()
{
    return attached;
}


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

if ( *(_DWORD *)(a1 + 60) )
{
if ( *(_QWORD *)(a1 + 8) )
{
if ( *(_QWORD *)a1 )
{
sem_init((sem_t *)(a1 + 24), 0, 0);
barrierlist_add(a1);
segvhandler_attach();
result = 0LL;
}
else
{
perror("start not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("size not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("original_prot not set");
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( *(_DWORD *)(a1 + 60) )
{
if ( *(_QWORD *)(a1 + 8) )
{
if ( *(_QWORD *)a1 )
{
sem_init((sem_t *)(a1 + 24), 0, 0);
barrierlist_add(a1);
segvhandler_attach();
result = 0LL;
}
else
{
perror("start not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("size not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("original_prot not set");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int barrier_init(struct barrier* barrier)
{
    if(barrier->original_prot == 0) {
        perror("original_prot not set");
        return -1;
    }
    if(barrier->size == 0) {
        perror("size not set");
        return -1;
    }
    if(barrier->start == 0) {
        perror("start not set");
        return -1;
    }
    sem_init(&barrier->wait_sem, 0, 0);
    barrierlist_add(barrier);
    segvhandler_attach();
 return 0;
}

canonical_original_code:
int barrier_init(struct barrier *barrier)
{
    if(barrier->original_prot == 0) {
        perror("original_prot not set");
        return -1;
    }
    if(barrier->size == 0) {
        perror("size not set");
        return -1;
    }
    if(barrier->start == 0) {
        perror("start not set");
        return -1;
    }
    sem_init(&barrier->wait_sem, 0, 0);
    barrierlist_add(barrier);
    segvhandler_attach();
 return 0;
}


decompiled_code:
__int64 __fastcall barrier_iswhitelisted(__int64 a1, __int64 a2)
{
__int64 *v2; // rax
__int64 v3; // rdx
_QWORD *v4; // rax

v2 = *(__int64 **)(a1 + 64);
v3 = *v2;
if ( !*v2 )
return 0LL;
v4 = v2 + 1;
while ( a2 != v3 )
{
v3 = *v4++;
if ( !v3 )
return 0LL;
}
return 1LL;
}

original_code:
int barrier_iswhitelisted(struct barrier* barrier, pthread_t thread)
{
    int i = 0, result = 0;
    pthread_t* whitelist = barrier->whitelist;
    while(whitelist[i] != 0) {
        if(whitelist[i] == thread) {
            result = 1;
            break;
        }
        i++;
    }
    return result;
}

canonical_original_code:
int barrier_iswhitelisted(struct barrier *barrier, unsigned long thread)
{
    int i = 0; int result = 0;
    unsigned long *whitelist = barrier->whitelist;
    while(whitelist[i] != 0) {
        if(whitelist[i] == thread) {
            result = 1;
            break;
        }
        i++;
    }
    return result;
}

variable_types:
thread
unsigned long

variable_types: result int

variable_types:
whitelist
unsigned long *


decompiled_code:
pthread_t barrier_threadid()
{
return pthread_self();
}

canonical_decompiled_code:
pthread_t func2()
{
return pthread_self();
}

original_code:
unsigned int barrier_threadid()
{
    return (unsigned int)pthread_self();
}

return_type: unsigned int


decompiled_code:
int __fastcall barrier_deactivate(__int64 a1)
{
__int64 v1; // r13
unsigned int v2; // ebx
int result; // eax

v1 = (unsigned int)barrier_threadid();
printf("%x : deactivating memory barrier\n", v1);
if ( mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, *(_DWORD *)(a1 + 60)) )
{
perror("mprotect");
exit(1);
}
v2 = 0;
for ( *(_DWORD *)(a1 + 16) = 0; *(_DWORD *)(a1 + 56) > (signed int)v2; ++v2 )
sem_post((sem_t *)(a1 + 24));
result = printf("%x : woke up %d threads\n", (unsigned int)v1, v2);
*(_DWORD *)(a1 + 56) = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 v1; // r13
unsigned int v2; // ebx
int result; // eax

v1 = (unsigned int)func2();
printf("%x : deactivating memory barrier\n", v1);
if ( mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, *(_DWORD *)(a1 + 60)) )
{
perror("mprotect");
exit(1);
}
v2 = 0;
for ( *(_DWORD *)(a1 + 16) = 0; *(_DWORD *)(a1 + 56) > (signed int)v2; ++v2 )
sem_post((sem_t *)(a1 + 24));
result = printf("%x : woke up %d threads\n", (unsigned int)v1, v2);
*(_DWORD *)(a1 + 56) = 0;
return result;
}

original_code:
void barrier_deactivate(struct barrier* barrier)
{
    printf("%x : deactivating memory barrier\n", barrier_threadid());
    if(mprotect(barrier->start, barrier->size-1, barrier->original_prot) != 0) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
    barrier->locked = 0;
    int i;
    for(i = 0; i < barrier->waiting; i++) {
        sem_post(&barrier->wait_sem);
    }
    printf("%x : woke up %d threads\n", barrier_threadid(), i);
    barrier->waiting = 0;
}

canonical_original_code:
void barrier_deactivate(struct barrier *barrier)
{
    printf("%x : deactivating memory barrier\n", barrier_threadid());
    if(mprotect(barrier->start, barrier->size-1, barrier->original_prot) != 0) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
    barrier->locked = 0;
    int i;
    for(i = 0; i < barrier->waiting; i++) {
        sem_post(&barrier->wait_sem);
    }
    printf("%x : woke up %d threads\n", barrier_threadid(), i);
    barrier->waiting = 0;
}


decompiled_code:
__int64 __fastcall barrier_destroy(__int64 a1)
{
if ( *(_DWORD *)(a1 + 16) )
barrier_deactivate(a1);
segvhandler_detatch();
return barrierlist_remove(a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
if ( *(_DWORD *)(a1 + 16) )
func3(a1);
segvhandler_detatch();
return barrierlist_remove(a1);
}

original_code:
void barrier_destroy(struct barrier* barrier)
{
    if(barrier->locked != 0) {
        barrier_deactivate(barrier);
    }
    segvhandler_detatch();
    barrierlist_remove(barrier);
}

canonical_original_code:
void barrier_destroy(struct barrier *barrier)
{
    if(barrier->locked != 0) {
        barrier_deactivate(barrier);
    }
    segvhandler_detatch();
    barrierlist_remove(barrier);
}


decompiled_code:
int __fastcall barrier_activate(__int64 a1)
{
unsigned int v1; // eax
int result; // eax

*(_DWORD *)(a1 + 16) = 1;
v1 = barrier_threadid();
printf("%x : activating memory barrier for %p\n", v1, *(const void **)a1);
result = mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, 1);
if ( result )
{
perror("mprotect");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
unsigned int v1; // eax
int result; // eax

*(_DWORD *)(a1 + 16) = 1;
v1 = func2();
printf("%x : activating memory barrier for %p\n", v1, *(const void **)a1);
result = mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, 1);
if ( result )
{
perror("mprotect");
exit(1);
}
return result;
}

original_code:
void barrier_activate(struct barrier* barrier)
{
    barrier->locked = 1;
    printf("%x : activating memory barrier for %p\n", barrier_threadid(), barrier->start);
    if(mprotect(barrier->start, barrier->size - 1, 0x1)) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
}

canonical_original_code:
void barrier_activate(struct barrier *barrier)
{
    barrier->locked = 1;
    printf("%x : activating memory barrier for %p\n", barrier_threadid(), barrier->start);
    if(mprotect(barrier->start, barrier->size - 1, 0x1)) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
}


unlock

repo: jonhoo/cryptsetup-gui

decompiled_code:
__int64 __fastcall unlock(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
unsigned int v4; // eax

LOBYTE(v1) = decrypt(name, (const char *)device, (const char *)options, a1);
v2 = v1;
if ( (_BYTE)v1 )
{
if ( do_mount )
{
LOBYTE(v4) = mount(mountpoint);
v2 = v4;
if ( !(_BYTE)v4 )
fprintf(stderr, "Failed to mount device %s\n", name);
}
}
else
{
fprintf(stderr, "Failed to decrypt device %s\n", name);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
unsigned int v4; // eax

LOBYTE(v1) = func7(name, (const char *)device, (const char *)options, a1);
v2 = v1;
if ( (_BYTE)v1 )
{
if ( do_mount )
{
LOBYTE(v4) = func8(mountpoint);
v2 = v4;
if ( !(_BYTE)v4 )
fprintf(stderr, "Failed to mount device %s\n", name);
}
}
else
{
fprintf(stderr, "Failed to decrypt device %s\n", name);
}
return v2;
}

original_code:
_Bool unlock(char* password) {
  if (!decrypt(name, device, options, password)) {
    fprintf(stderr, "Failed to decrypt device %s\n", name);
    return 0;
  }
  if (do_mount) {
    if (!mount(mountpoint)) {
      fprintf(stderr, "Failed to mount device %s\n", name);
      return 0;
    }
  }
  return 1;
}

canonical_original_code:
_Bool unlock(char *password) {
  if (!decrypt(name, device, options, password)) {
    fprintf(stderr, "Failed to decrypt device %s\n", name);
    return 0;
  }
  if (do_mount) {
    if (!mount(mountpoint)) {
      fprintf(stderr, "Failed to mount device %s\n", name);
      return 0;
    }
  }
  return 1;
}

variable_types:
password
char *


decompiled_code:
__int64 __fastcall is_space(int a1, __int64 a2, int a3)
{
unsigned int v3; // eax

v3 = a1 - 9;
LOBYTE(v3) = (unsigned __int8)(a1 - 9) <= 1u;
LOBYTE(a3) = (_BYTE)a1 == 32;
return a3 | v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, int a3)
{
unsigned int v3; // eax

v3 = a1 - 9;
LOBYTE(v3) = (unsigned __int8)(a1 - 9) <= 1u;
LOBYTE(a3) = (_BYTE)a1 == 32;
return a3 | v3;
}

original_code:
_Bool is_space(char s) {
  return s == ' ' || s == '\t' || s == '\n';
}

variable_types: s char


strstrip

decompiled_code:
const char *__fastcall strstrip(const char *a1, __int64 a2)
{
const char *v2; // rbx
size_t v3; // rax
const char *v4; // rdx
const char *i; // rbp
char v6; // di

v2 = a1;
v3 = strlen(a1);
v4 = a1;
if ( v3 )
{
for ( i = &a1[v3 - 1]; a1 <= i; --i )
{
if ( !(unsigned __int8)is_space(*i, a2, (int)v4) )
break;
}
*((_BYTE *)i + 1) = 0;
v6 = *a1;
v4 = v2;
if ( *v2 )
{
while ( (unsigned __int8)is_space(v6, a2, (int)v4) )
{
v6 = *++v2;
if ( !*v2 )
return v2;
}
v4 = v2;
}
}
return v4;
}

canonical_decompiled_code:
const char *__fastcall func11(const char *a1, __int64 a2)
{
const char *v2; // rbx
size_t v3; // rax
const char *v4; // rdx
const char *i; // rbp
char v6; // di

v2 = a1;
v3 = strlen(a1);
v4 = a1;
if ( v3 )
{
for ( i = &a1[v3 - 1]; a1 <= i; --i )
{
if ( !(unsigned __int8)func10(*i, a2, (int)v4) )
break;
}
*((_BYTE *)i + 1) = 0;
v6 = *a1;
v4 = v2;
if ( *v2 )
{
while ( (unsigned __int8)func10(v6, a2, (int)v4) )
{
v6 = *++v2;
if ( !*v2 )
return v2;
}
v4 = v2;
}
}
return v4;
}

original_code:
char *strstrip(char *s) {
    size_t size;
    char *end;
    size = strlen(s);
    if (!size)
        return s;
    end = s + size - 1;
    while (end >= s && is_space(*end))
        end--;
    *(end + 1) = '\0';
    while (*s && is_space(*s))
        s++;
    return s;
}

canonical_original_code:
char *strstrip(char *s) {
    unsigned long size;
    char *end;
    size = strlen(s);
    if (!size)
        return s;
    end = s + size - 1;
    while (end >= s && is_space(*end))
        end--;
    *(end + 1) = '\0';
    while (*s && is_space(*s))
        s++;
    return s;
}

variable_types:
s
char *

variable_types:
size
unsigned long

variable_types:
end
char *

return_type: char *


usage

decompiled_code:
int usage()
{
return printf("Usage: %s [-m] cryptpoint\n", (const char *)arg0);
}

canonical_decompiled_code:
int func13()
{
return printf("Usage: %s [-m] cryptpoint\n", (const char *)arg0);
}

original_code:
void usage() {
  printf("Usage: %s [-m] cryptpoint\n", arg0);
}


decompiled_code:
__int64 __fastcall write_output(char a1)
{
char v1; // bl
__int64 result; // rax

v1 = a1 >> 4;
fprintf(stderr, "DEBUG %s:%s:%d: writing %x\n", "lcd.c", "write_output", 8LL, (unsigned int)(a1 >> 4));
if ( ((a1 >> 4) & 1) != 0 )
*(_DWORD *)(gpio + 28LL) = 128;
else
*(_DWORD *)(gpio + 40LL) = 128;
if ( (v1 & 2) != 0 )
*(_DWORD *)(gpio + 28LL) = 0x1000000;
else
*(_DWORD *)(gpio + 40LL) = 0x1000000;
if ( (v1 & 4) != 0 )
*(_DWORD *)(gpio + 28LL) = 512;
else
*(_DWORD *)(gpio + 40LL) = 512;
result = gpio;
if ( (v1 & 8) != 0 )
*(_DWORD *)(gpio + 28LL) = 1024;
else
*(_DWORD *)(gpio + 40LL) = 1024;
return result;
}

variable_types: i char


nybble

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

*(_DWORD *)(gpio + 28LL) = 0x2000000;
usleep(0x3E8u);
result = gpio;
*(_DWORD *)(gpio + 40LL) = 0x2000000;
return result;
}

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

*(_DWORD *)(gpio + 28LL) = 0x2000000;
usleep(0x3E8u);
result = gpio;
*(_DWORD *)(gpio + 40LL) = 0x2000000;
return result;
}


command

decompiled_code:
__int64 __fastcall command(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: command 0x%x\n", "lcd.c", "command", 25LL, (unsigned int)a1);
*(_DWORD *)(gpio + 40LL) = 2048;
write_output(a1);
nybble();
write_output(16 * a1);
return nybble();
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: command 0x%x\n", "lcd.c", "command", 25LL, (unsigned int)a1);
*(_DWORD *)(gpio + 40LL) = 2048;
func0(a1);
func1();
func0(16 * a1);
return func1();
}

original_code:
void command(char i)
{
 fprintf(stderr, "DEBUG %s:%s:%d: " "command 0x%x" "\n", "lcd.c", __func__, 25, i);
 if (0) { *(gpio+7) = (1 << (11)); } else { *(gpio+10) = (1 << (11)); };
 write_output(i);
 nybble();
 i <<= 4;
 write_output(i);
 nybble();
}


decompiled_code:
__int64 __fastcall lcd_write(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: writing character %c\n", "lcd.c", "lcd_write", 36LL, (unsigned int)a1);
*(_DWORD *)(gpio + 28LL) = 2048;
write_output(a1);
nybble();
write_output(16 * a1);
return nybble();
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: writing character %c\n", "lcd.c", "lcd_write", 36LL, (unsigned int)a1);
*(_DWORD *)(gpio + 28LL) = 2048;
func0(a1);
func1();
func0(16 * a1);
return func1();
}

original_code:
void lcd_write(char i)
{
 fprintf(stderr, "DEBUG %s:%s:%d: " "writing character %c" "\n", "lcd.c", __func__, 36, i);
 if (1) { *(gpio+7) = (1 << (11)); } else { *(gpio+10) = (1 << (11)); };
 write_output(i);
 nybble();
 i <<= 4;
 write_output(i);
 nybble();
}


decompiled_code:
__int64 __fastcall lcd_print(char *a1, __int16 a2)
{
char v3; // di
__int64 result; // rax
char i; // di

v3 = -64;
if ( a2 != 2 )
v3 = 0x80;
result = command(v3);
for ( i = *a1; *a1; i = *a1 )
{
++a1;
result = lcd_write(i);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, __int16 a2)
{
char v3; // di
__int64 result; // rax
char i; // di

v3 = -64;
if ( a2 != 2 )
v3 = 0x80;
result = func2(v3);
for ( i = *a1; *a1; i = *a1 )
{
++a1;
result = func3(i);
}
return result;
}

original_code:
void lcd_print(char* str, short line)
{
 command(0x80 | (line == 2 ? 0x40 : 0));
 while (*str) {
  lcd_write(*str++);
 }
}

canonical_original_code:
void lcd_print(char *str, short line)
{
 command(0x80 | (line == 2 ? 0x40 : 0));
 while (*str) {
  lcd_write(*str++);
 }
}

variable_types:
str
char *

variable_types: line short


decompiled_code:
__int64 init_lcd()
{
_DWORD *v0; // rax
int v1; // edx
int v2; // edx
__int64 v3; // rax
int v4; // edx

*gpio &= 0xFF1FFFFF;
*gpio |= 0x200000u;
v0 = (_DWORD *)gpio;
v1 = *(_DWORD *)(gpio + 8LL);
BYTE1(v1) &= 0x8Fu;
*(_DWORD *)(gpio + 8LL) = v1;
v2 = v0[2];
BYTE1(v2) |= 0x10u;
v0[2] = v2;
*v0 &= 0xC7FFFFFF;
*gpio |= 0x8000000u;
*(_DWORD *)(gpio + 4LL) &= 0xFFFFFFF8;
*(_DWORD *)(gpio + 4LL) |= 1u;
*(_DWORD *)(gpio + 4LL) &= 0xFFFFFFC7;
*(_DWORD *)(gpio + 4LL) |= 8u;
v3 = gpio;
*(_DWORD *)(gpio + 8LL) &= 0xFFFC7FFF;
v4 = *(_DWORD *)(v3 + 8);
BYTE1(v4) |= 0x80u;
*(_DWORD *)(v3 + 8) = v4;
*(_DWORD *)(v3 + 40) = 0x2000000;
*(_DWORD *)(v3 + 40) = 2048;
write_output(0);
usleep(0x186A0u);
write_output(48);
usleep(0x7530u);
nybble();
usleep(0x2710u);
nybble();
usleep(0x2710u);
nybble();
usleep(0x2710u);
sleep(1u);
nybble();
write_output(32);
nybble();
command(40);
command(16);
command(12);
command(1);
usleep(0x7D0u);
command(2);
usleep(0x7D0u);
return command(6);
}


safe

repo: joodicator/utility

decompiled_code:
__int64 __fastcall safe(__int64 a1)
{
if ( !a1 )
{
perror("perror");
exit(1);
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
if ( !a1 )
{
perror("perror");
exit(1);
}
return a1;
}

original_code:
void* safe(void* ptr) {
    if (ptr != ((void *)0)) return ptr;
    perror("perror");
    exit(1);
}

canonical_original_code:
void *safe(void *ptr) {
    if (ptr != ((void *)0)) return ptr;
    perror("perror");
    exit(1);
}

variable_types:
ptr
void *


decompiled_code:
int __fastcall read_line(const char **a1, _DWORD *a2, size_t *a3)
{
size_t v5; // rax
size_t v6; // rsi
void *v7; // rax

*a3 = 0LL;
**a1 = 0;
while ( 1 )
{
fgets((char *)&(*a1)[*a3], *a2 - *a3, stdin);
if ( ferror(stdin) )
safe(0LL);
*a3 = strlen(*a1);
LODWORD(v5) = feof(stdin);
if ( (_DWORD)v5 )
break;
v5 = *a3;
if ( (*a1)[*a3 - 1] == 10 )
break;
v6 = *(_QWORD *)a2 + 1024LL;
*(_QWORD *)a2 = v6;
v7 = realloc((void *)*a1, v6);
safe((__int64)v7);
}
return v5;
}

canonical_decompiled_code:
int __fastcall func8(const char **a1, _DWORD *a2, size_t *a3)
{
size_t v5; // rax
size_t v6; // rsi
void *v7; // rax

*a3 = 0LL;
**a1 = 0;
while ( 1 )
{
fgets((char *)&(*a1)[*a3], *a2 - *a3, stdin);
if ( ferror(stdin) )
func7(0LL);
*a3 = strlen(*a1);
LODWORD(v5) = feof(stdin);
if ( (_DWORD)v5 )
break;
v5 = *a3;
if ( (*a1)[*a3 - 1] == 10 )
break;
v6 = *(_QWORD *)a2 + 1024LL;
*(_QWORD *)a2 = v6;
v7 = realloc((void *)*a1, v6);
func7((__int64)v7);
}
return v5;
}

original_code:
void read_line(char** buf, size_t* buf_s, size_t* len) {
    *len = 0;
    **buf = '\0';
    for (;;) {
        fgets((*buf)+(*len), (*buf_s)-(*len), stdin);
        if (ferror(stdin)) safe(((void *)0));
        *len = strlen(*buf);
        if (feof(stdin) || (*buf)[(*len)-1] == '\n') break;
        *buf_s += BLOCK_SIZE;
        safe(realloc(*buf, *buf_s));
    }
}

canonical_original_code:
void read_line(char **buf, unsigned long *buf_s, unsigned long *len) {
    *len = 0;
    **buf = '\0';
    for (;;) {
        fgets((*buf)+(*len), (*buf_s)-(*len), stdin);
        if (ferror(stdin)) safe(((void *)0));
        *len = strlen(*buf);
        if (feof(stdin) || (*buf)[(*len)-1] == '\n') break;
        *buf_s += BLOCK_SIZE;
        safe(realloc(*buf, *buf_s));
    }
}

variable_types:
buf
char **

variable_types:
len
unsigned long *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
int i; // ebp
char *v5; // rax
char *s; // [rsp+8h] [rbp-30h] BYREF
size_t v8; // [rsp+10h] [rbp-28h] BYREF
__int64 v9[4]; // [rsp+18h] [rbp-20h] BYREF

v9[0] = 1024LL;
v3 = malloc(0x400uLL);
s = (char *)safe((__int64)v3);
for ( i = 1; !feof(stdin); i = 0 )
{
read_line((const char **)&s, v9, &v8);
if ( !v8 )
break;
v5 = &s[v8 - 1];
if ( *v5 == 10 )
*v5 = 0;
if ( !i )
fputc(10, _bss_start);
fputs(s, _bss_start);
}
fflush(_bss_start);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rax
int i; // ebp
char *v5; // rax
char *s; // [rsp+8h] [rbp-30h] BYREF
size_t v8; // [rsp+10h] [rbp-28h] BYREF
__int64 v9[4]; // [rsp+18h] [rbp-20h] BYREF

v9[0] = 1024LL;
v3 = malloc(0x400uLL);
s = (char *)func7((__int64)v3);
for ( i = 1; !feof(stdin); i = 0 )
{
func8((const char **)&s, v9, &v8);
if ( !v8 )
break;
v5 = &s[v8 - 1];
if ( *v5 == 10 )
*v5 = 0;
if ( !i )
fputc(10, _bss_start);
fputs(s, _bss_start);
}
fflush(_bss_start);
return 0;
}

original_code:
int main(void) {
    size_t buf_s = BLOCK_SIZE;
    size_t len;
    char* buf = safe(malloc(buf_s));
    int first = 1;
    while (!feof(stdin)) {
        read_line(&buf, &buf_s, &len);
        if (!len) break;
        if (buf[len-1] == '\n') buf[len-1] = '\0';
        if (first) first = 0;
        else fputc('\n', stdout);
        fputs(buf, stdout);
    }
    fflush(stdout);
    return 0;
}

canonical_original_code:
int main() {
    unsigned long buf_s = BLOCK_SIZE;
    unsigned long len;
    char *buf = safe(malloc(buf_s));
    int first = 1;
    while (!feof(stdin)) {
        read_line(&buf, &buf_s, &len);
        if (!len) break;
        if (buf[len-1] == '\n') buf[len-1] = '\0';
        if (first) first = 0;
        else fputc('\n', stdout);
        fputs(buf, stdout);
    }
    fflush(stdout);
    return 0;
}

variable_types:
len
unsigned long

variable_types:
buf
char *

variable_types: first int


repo: jordan-dinwiddy/Somix

decompiled_code:
__int64 __fastcall alloc_zone(unsigned __int16 a1)
{
int v1; // ebx

v1 = alloc_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
if ( v1 < 0 )
panic("alloc_zone(%d): no free zones available", a1);
return (unsigned int)*((unsigned __int16 *)&sb + 4) + v1 - 1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
int v1; // ebx

v1 = alloc_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
if ( v1 < 0 )
panic("alloc_zone(%d): no free zones available", a1);
return (unsigned int)*((unsigned __int16 *)&sb + 4) + v1 - 1;
}

original_code:
zone_nr alloc_zone(zone_nr near_zone)
{
 int bit = near_zone - (sb.s_firstdatazone - 1);
 int b;
 zone_nr z;
 ;
 if((b = alloc_bit(sb.zmap, bit)) < 0)
  panic("alloc_zone(%d): no free zones available", near_zone);
 z = b + (sb.s_firstdatazone - 1);
 return z;
}

canonical_original_code:
unsigned short alloc_zone(unsigned short near_zone)
{
 int bit = near_zone - (sb.s_firstdatazone - 1);
 int b;
 unsigned short z;
 ;
 if((b = alloc_bit(sb.zmap, bit)) < 0)
  panic("alloc_zone(%d): no free zones available", near_zone);
 z = b + (sb.s_firstdatazone - 1);
 return z;
}

variable_types: bit int

variable_types: b int

variable_types:
z
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall free_zone(unsigned __int16 a1)
{
__int64 result; // rax

if ( a1 )
result = free_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
return result;
}

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

if ( a1 )
result = free_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
return result;
}

original_code:
void free_zone(zone_nr z)
{
 int bit = z - (sb.s_firstdatazone - 1);
 if(z == 0) return;
 ;
 free_bit(sb.zmap, bit);
}

canonical_original_code:
void free_zone(unsigned short z)
{
 int bit = z - (sb.s_firstdatazone - 1);
 if(z == 0) return;
 ;
 free_bit(sb.zmap, bit);
}


original_code:
int write_map(struct minix_inode *inode, int pos, zone_nr new_zone)
{
 zone_nr zone = pos / 1024;
 int excess, index;
 zone_nr *zp;
 zone_nr *dbl_zp;
 char new_ind = 0;
 char new_dbl = 0;
 struct minix_block *blk = ((void *)0);
 if(zone < 7) {
  inode->i_zone[zone] = new_zone;
  inode->i_dirty = 1;
  return 1;
 }
 excess = zone - 7;
 if(excess < (1024/2)) {
  zp = &inode->i_zone[7];
 }
 else {
  dbl_zp = &inode->i_zone[7 +1];
  if(*dbl_zp == 0) {
   *dbl_zp = alloc_zone(inode->i_zone[0]);
   if(*dbl_zp == 0)
    return -28;
   inode->i_dirty = 1;
   new_dbl = 1;
  }
  excess -= (1024/2);
  index = excess / (1024/2);
  excess = excess % (1024/2);
  if(index >= (1024/2)) return -27;
  blk = get_block(*dbl_zp, new_dbl == 1 ? 0 : 1);
  if(new_dbl == 1) zero_block(blk);
  zp = &((zone_nr *)blk->blk_data)[index];
 }
 if(*zp == 0) {
  *zp = alloc_zone(inode->i_zone[0]);
  if(blk != ((void *)0)) blk->blk_dirty = 1;
  else inode->i_dirty = 1;
  if(*zp == 0) {
   if(blk != ((void *)0)) put_block(blk, 2);
   return -28;
  }
  new_ind = 1;
 }
 if(blk != ((void *)0)) put_block(blk, 2);
 blk = get_block(*zp, new_ind == 1 ? 0 : 1);
 if(new_ind == 1) zero_block(blk);
 ;
 ((zone_nr *)blk->blk_data)[excess] = new_zone;
 blk->blk_dirty = 1;
 put_block(blk, 2);
 return 1;
}

canonical_original_code:
int write_map(struct minix_inode *inode, int pos, unsigned short new_zone)
{
 unsigned short zone = pos / 1024;
 int excess; int index;
 unsigned short *zp;
 unsigned short *dbl_zp;
 char new_ind = 0;
 char new_dbl = 0;
 struct minix_block *blk = ((void *)0);
 if(zone < 7) {
  inode->i_zone[zone] = new_zone;
  inode->i_dirty = 1;
  return 1;
 }
 excess = zone - 7;
 if(excess < (1024/2)) {
  zp = &inode->i_zone[7];
 }
 else {
  dbl_zp = &inode->i_zone[7 +1];
  if(*dbl_zp == 0) {
   *dbl_zp = alloc_zone(inode->i_zone[0]);
   if(*dbl_zp == 0)
    return -28;
   inode->i_dirty = 1;
   new_dbl = 1;
  }
  excess -= (1024/2);
  index = excess / (1024/2);
  excess = excess % (1024/2);
  if(index >= (1024/2)) return -27;
  blk = get_block(*dbl_zp, new_dbl == 1 ? 0 : 1);
  if(new_dbl == 1) zero_block(blk);
  zp = &((unsigned short *)blk->blk_data)[index];
 }
 if(*zp == 0) {
  *zp = alloc_zone(inode->i_zone[0]);
  if(blk != ((void *)0)) blk->blk_dirty = 1;
  else inode->i_dirty = 1;
  if(*zp == 0) {
   if(blk != ((void *)0)) put_block(blk, 2);
   return -28;
  }
  new_ind = 1;
 }
 if(blk != ((void *)0)) put_block(blk, 2);
 blk = get_block(*zp, new_ind == 1 ? 0 : 1);
 if(new_ind == 1) zero_block(blk);
 ;
 ((unsigned short *)blk->blk_data)[excess] = new_zone;
 blk->blk_dirty = 1;
 put_block(blk, 2);
 return 1;
}

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *

variable_types: pos int

variable_types:
zone
unsigned short

variable_types: excess int

variable_types: index int

variable_types:
zp
unsigned short *

variable_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *


decompiled_code:
__int64 __fastcall new_block(__int64 a1, int a2)
{
unsigned __int16 v3; // bx
unsigned __int16 v4; // di
__int64 v5; // rbx

v3 = read_map();
if ( v3 )
goto LABEL_5;
v4 = sb[4];
if ( *(_DWORD *)(a1 + 4) )
v4 = *(_WORD *)(a1 + 14);
v3 = alloc_zone(v4);
if ( (int)write_map(a1, a2, v3) < 0 )
return 0LL;
LABEL_5:
v5 = get_block(v3, 0LL);
zero_block(v5);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
unsigned __int16 v3; // bx
unsigned __int16 v4; // di
__int64 v5; // rbx

v3 = read_map();
if ( v3 )
goto LABEL_5;
v4 = sb[4];
if ( *(_DWORD *)(a1 + 4) )
v4 = *(_WORD *)(a1 + 14);
v3 = func1(v4);
if ( (int)func3(a1, a2, v3) < 0 )
return 0LL;
LABEL_5:
v5 = get_block(v3, 0LL);
func0(v5);
return v5;
}

original_code:
struct minix_block *new_block(struct minix_inode *inode, int pos)
{
 zone_nr z, near_z;
 struct minix_block *retval;
 char new_zone = 0;
 ;
 if((z = read_map(inode, pos)) == 0) {
  if(inode->i_size == 0)
   near_z = sb.s_firstdatazone;
  else
   near_z = inode->i_zone[0];
  z = alloc_zone(near_z);
  new_zone = 1;
  if(write_map(inode, pos, z) < 0) {
   ;
   return ((void *)0);
  }
 }
 retval = get_block(z, 0);
 zero_block(retval);
 ;
 return retval;
}

canonical_original_code:
struct minix_block *new_block(struct minix_inode *inode, int pos)
{
 unsigned short z; unsigned short near_z;
 struct minix_block *retval;
 char new_zone = 0;
 ;
 if((z = read_map(inode, pos)) == 0) {
  if(inode->i_size == 0)
   near_z = sb.s_firstdatazone;
  else
   near_z = inode->i_zone[0];
  z = alloc_zone(near_z);
  new_zone = 1;
  if(write_map(inode, pos, z) < 0) {
   ;
   return ((void *)0);
  }
 }
 retval = get_block(z, 0);
 zero_block(retval);
 ;
 return retval;
}

return_type: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *

user_defined_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; }


original_code:
struct minix_inode *new_node(struct minix_inode *parent, const char *filename,
 mode_t mode)
{
 struct minix_inode *new_inode;
 new_inode = alloc_inode();
 new_inode->i_mode = (u16) mode;
 new_inode->i_uid = 500;
 new_inode->i_gid = 0;
 new_inode->i_time = time(((void *)0));
 new_inode->i_nlinks++;
 new_inode->i_dirty = 1;
 ;
 dir_add(parent, filename, new_inode->i_num);
 if(((((mode)) & 0170000) == (0040000))) {
  ;
  init_dir(new_inode, parent);
 }
 else {
  ;
 }
 return new_inode;
}

canonical_original_code:
struct minix_inode *new_node(struct minix_inode *parent, char *filename, unsigned int mode)
{
 struct minix_inode *new_inode;
 new_inode = alloc_inode();
 new_inode->i_mode = (unsigned short) mode;
 new_inode->i_uid = 500;
 new_inode->i_gid = 0;
 new_inode->i_time = time(((void *)0));
 new_inode->i_nlinks++;
 new_inode->i_dirty = 1;
 ;
 dir_add(parent, filename, new_inode->i_num);
 if(((((mode)) & 0170000) == (0040000))) {
  ;
  init_dir(new_inode, parent);
 }
 else {
  ;
 }
 return new_inode;
}

variable_types:
parent
struct minix_inode *

variable_types:
filename
char *

variable_types:
mode
unsigned int

return_type: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *


decompiled_code:
__int64 __fastcall write_chunk(__int64 a1, __int64 a2, int a3, const void *a4)
{
unsigned __int16 v6; // ax
__int64 v7; // rbp

v6 = read_map(a1, a2);
if ( v6 )
{
v7 = get_block(v6, a3 != 1024);
}
else
{
v7 = new_block(a1, a2);
if ( !v7 )
return 4294967268LL;
}
memcpy((void *)(*(_QWORD *)(v7 + 40) + (int)a2 % 1024), a4, a3);
*(_BYTE *)(v7 + 32) = 1;
put_block(v7, 6LL);
return 1LL;
}

original_code:
static int write_chunk(struct minix_inode *inode, int pos, int chunk,
 const char *buf)
{
 int zone = pos / 1024;
 int off = pos % 1024;
 int b_num = 0;
 struct minix_block *blk;
 ;
 b_num = read_map(inode, pos);
 if(b_num == 0) {
  ;
  blk = new_block(inode, pos);
  if(blk == ((void *)0)) {
   return -28;
  }
 }
 else {
  blk = get_block(b_num, chunk == 1024 ? 0 : 1);
 }
 memcpy(blk->blk_data+off, buf, chunk);
 blk->blk_dirty = 1;
 put_block(blk, 6);
 return 1;
}

canonical_original_code:
static int write_chunk(struct minix_inode *inode, int pos, int chunk, char *buf)
{
 int zone = pos / 1024;
 int off = pos % 1024;
 int b_num = 0;
 struct minix_block *blk;
 ;
 b_num = read_map(inode, pos);
 if(b_num == 0) {
  ;
  blk = new_block(inode, pos);
  if(blk == ((void *)0)) {
   return -28;
  }
 }
 else {
  blk = get_block(b_num, chunk == 1024 ? 0 : 1);
 }
 memcpy(blk->blk_data+off, buf, chunk);
 blk->blk_dirty = 1;
 put_block(blk, 6);
 return 1;
}

variable_types: chunk int

variable_types: zone int

variable_types: off int

user_defined_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; }


decompiled_code:
__int64 __fastcall write_buf(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // er13
int v7; // ebx
__int64 result; // rax

if ( !a3 )
return 0xFFFFFFFFLL;
v4 = a3;
v5 = a4;
if ( (unsigned __int64)sb[3] - a3 < a4 )
return 4294967269LL;
v6 = 0;
if ( (int)a3 <= 0 )
{
LABEL_11:
if ( *(_DWORD *)(a1 + 4) < v5 )
*(_DWORD *)(a1 + 4) = v5;
*(_DWORD *)(a1 + 8) = time(0LL);
*(_BYTE *)(a1 + 34) = 1;
result = v6;
}
else
{
v7 = ((unsigned int)((int)a4 >> 31) >> 22) - ((((unsigned int)((int)a4 >> 31) >> 22) + (_WORD)a4) & 0x3FF) + 1024;
if ( v7 > (int)a3 )
v7 = a3;
v6 = 0;
while ( 1 )
{
result = write_chunk(a1, v5, v7, (const void *)(a2 + (int)v6));
if ( (int)result < 0 )
break;
v4 -= v7;
v5 += v7;
v6 += v7;
if ( v4 <= 0 )
goto LABEL_11;
v7 = 1024 - (int)v5 % 1024;
if ( v7 > v4 )
v7 = v4;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // er13
int v7; // ebx
__int64 result; // rax

if ( !a3 )
return 0xFFFFFFFFLL;
v4 = a3;
v5 = a4;
if ( (unsigned __int64)sb[3] - a3 < a4 )
return 4294967269LL;
v6 = 0;
if ( (int)a3 <= 0 )
{
LABEL_11:
if ( *(_DWORD *)(a1 + 4) < v5 )
*(_DWORD *)(a1 + 4) = v5;
*(_DWORD *)(a1 + 8) = time(0LL);
*(_BYTE *)(a1 + 34) = 1;
result = v6;
}
else
{
v7 = ((unsigned int)((int)a4 >> 31) >> 22) - ((((unsigned int)((int)a4 >> 31) >> 22) + (_WORD)a4) & 0x3FF) + 1024;
if ( v7 > (int)a3 )
v7 = a3;
v6 = 0;
while ( 1 )
{
result = func8(a1, v5, v7, (const void *)(a2 + (int)v6));
if ( (int)result < 0 )
break;
v4 -= v7;
v5 += v7;
v6 += v7;
if ( v4 <= 0 )
goto LABEL_11;
v7 = 1024 - (int)v5 % 1024;
if ( v7 > v4 )
v7 = v4;
}
}
return result;
}

original_code:
int write_buf(struct minix_inode *inode, const char *buf, size_t size, off_t offset)
{
 int nbytes = size;
 int off;
 int chunk;
 int pos = offset;
 int sbytes = 0;
 int ret;
 ;
 if(size == 0) return -1;
 if(offset > sb.s_max_size - size)
  return -27;
 while(nbytes > 0) {
  off = pos % 1024;
  chunk = ((nbytes) < ((1024 - off)) ? (nbytes) : ((1024 - off)));
  if(chunk < 0)
   panic("write_buf(%d, buf, %d, %d): why is chunk < 0?",
    inode->i_num, (int) size, (int) offset);
  ret = write_chunk(inode, pos, chunk, buf + sbytes);
  if(ret < 0) {
   ;
   return ret;
  }
  nbytes -= chunk;
  pos += chunk;
  sbytes += chunk;
 }
 if(pos > inode->i_size) {
  inode->i_size = pos;
 }
 inode->i_time = time(((void *)0));
 inode->i_dirty = 1;
 return sbytes;
}

canonical_original_code:
int write_buf(struct minix_inode *inode, char *buf, unsigned long size, long offset)
{
 int nbytes = size;
 int off;
 int chunk;
 int pos = offset;
 int sbytes = 0;
 int ret;
 ;
 if(size == 0) return -1;
 if(offset > sb.s_max_size - size)
  return -27;
 while(nbytes > 0) {
  off = pos % 1024;
  chunk = ((nbytes) < ((1024 - off)) ? (nbytes) : ((1024 - off)));
  if(chunk < 0)
   panic("write_buf(%d, buf, %d, %d): why is chunk < 0?",
    inode->i_num, (int) size, (int) offset);
  ret = write_chunk(inode, pos, chunk, buf + sbytes);
  if(ret < 0) {
   ;
   return ret;
  }
  nbytes -= chunk;
  pos += chunk;
  sbytes += chunk;
 }
 if(pos > inode->i_size) {
  inode->i_size = pos;
 }
 inode->i_time = time(((void *)0));
 inode->i_dirty = 1;
 return sbytes;
}

variable_types:
buf
char *

variable_types:
size
unsigned long

variable_types: offset long

variable_types: nbytes int

variable_types: sbytes int

variable_types: ret int


decompiled_code:
__int64 __fastcall dir_delete(__int64 a1, const char *a2)
{
unsigned __int16 v2; // ax
__int64 v3; // r13
__int64 v4; // r15
__int64 v5; // rbx
unsigned int v7; // [rsp+Ch] [rbp-3Ch]

v7 = 0;
LABEL_2:
v2 = read_map(a1, v7);
if ( !v2 )
return 0LL;
v3 = get_block(v2, 1LL);
v4 = *(_QWORD *)(v3 + 40);
v5 = 0LL;
while ( strcmp((const char *)(v4 + 2 + v5), a2) )
{
v5 += 32LL;
if ( v5 == 1024 )
{
put_block(v3, 1LL);
v7 += 1024;
goto LABEL_2;
}
}
*(_WORD *)(v4 + v5) = 0;
*(_BYTE *)(v3 + 32) = 1;
*(_DWORD *)(a1 + 8) = time(0LL);
*(_BYTE *)(a1 + 34) = 1;
put_block(v3, 1LL);
return 1LL;
}

original_code:
int dir_delete(struct minix_inode *p_dir, const char *file)
{
 struct minix_block *blk;
 zone_nr z;
 int c_pos = 0;
 int i;
 ;
 while((z = read_map(p_dir, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    ;
    *((inode_nr *)(blk->blk_data + i)) = 0;
    blk->blk_dirty = 1;
    p_dir->i_time = time(((void *)0));
    p_dir->i_dirty = 1;
    put_block(blk, 1);
    return 1;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 return 0;
}

canonical_original_code:
int dir_delete(struct minix_inode *p_dir, char *file)
{
 struct minix_block *blk;
 unsigned short z;
 int c_pos = 0;
 int i;
 ;
 while((z = read_map(p_dir, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    ;
    *((unsigned short *)(blk->blk_data + i)) = 0;
    blk->blk_dirty = 1;
    p_dir->i_time = time(((void *)0));
    p_dir->i_dirty = 1;
    put_block(blk, 1);
    return 1;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 return 0;
}

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *

variable_types:
file
char *

variable_types:
z
unsigned short

variable_types: i int


rename

decompiled_code:
__int64 __fastcall rename(const char *a1, const char *a2)
{
__int64 v2; // rax
const char *v3; // rbp
const char *v4; // rbx
__int64 v5; // rax
__int64 v6; // r13
__int64 v7; // rax
__int64 v8; // r12
char v10[32]; // [rsp+0h] [rbp-78h] BYREF
char expbuf[88]; // [rsp+20h] [rbp-58h] BYREF

v2 = last_dir(a1, expbuf);
if ( !v2 )
return 4294967294LL;
v3 = (const char *)v2;
v4 = (const char *)last_dir(a2, v10);
if ( !v4 )
return 4294967294LL;
LODWORD(v5) = advance(v3, expbuf);
v6 = v5;
if ( !v5 )
return 4294967294LL;
LODWORD(v7) = advance(v4, v10);
v8 = v7;
if ( v7 )
{
dir_delete((__int64)v4, v10);
--*(_BYTE *)(v8 + 13);
*(_BYTE *)(v8 + 34) = 1;
put_inode(v8);
}
dir_add((__int64)v4, v10, *(_WORD *)(v6 + 32));
if ( (unsigned int)dir_delete((__int64)v3, expbuf) != 1 )
panic("rename(\"%s\", \"%s\"): unable to delete old entry", a1, a2);
put_inode(v3);
put_inode(v4);
put_inode(v6);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(const char *a1, const char *a2)
{
__int64 v2; // rax
const char *v3; // rbp
const char *v4; // rbx
__int64 v5; // rax
__int64 v6; // r13
__int64 v7; // rax
__int64 v8; // r12
char v10[32]; // [rsp+0h] [rbp-78h] BYREF
char expbuf[88]; // [rsp+20h] [rbp-58h] BYREF

v2 = last_dir(a1, expbuf);
if ( !v2 )
return 4294967294LL;
v3 = (const char *)v2;
v4 = (const char *)last_dir(a2, v10);
if ( !v4 )
return 4294967294LL;
LODWORD(v5) = advance(v3, expbuf);
v6 = v5;
if ( !v5 )
return 4294967294LL;
LODWORD(v7) = advance(v4, v10);
v8 = v7;
if ( v7 )
{
func10((__int64)v4, v10);
--*(_BYTE *)(v8 + 13);
*(_BYTE *)(v8 + 34) = 1;
put_inode(v8);
}
func5((__int64)v4, v10, *(_WORD *)(v6 + 32));
if ( (unsigned int)func10((__int64)v3, expbuf) != 1 )
panic("rename(\"%s\", \"%s\"): unable to delete old entry", a1, a2);
put_inode(v3);
put_inode(v4);
put_inode(v6);
return 1LL;
}

original_code:
int rename(const char *src_path, const char *dest_path)
{
 struct minix_inode *s_dir, *d_dir, *i, *di;
 char old_name[30];
 char new_name[30];
 ;
 if((s_dir = last_dir(src_path, old_name)) == ((void *)0))
  return -2;
 if((d_dir = last_dir(dest_path, new_name)) == ((void *)0))
  return -2;
 if((i = advance(s_dir, old_name)) == ((void *)0))
  return -2;
 if((di = advance(d_dir, new_name)) != ((void *)0)) {
  dir_delete(d_dir, new_name);
  di->i_nlinks--;
  di->i_dirty = 1;
  put_inode(di);
 }
 dir_add(d_dir, new_name, i->i_num);
 if(dir_delete(s_dir, old_name) != 1) {
  panic("rename(\"%s\", \"%s\"): unable to delete old entry",
   src_path, dest_path);
 }
 put_inode(s_dir);
 put_inode(d_dir);
 put_inode(i);
 ;
 return 1;
}

canonical_original_code:
int rename(char *src_path, char *dest_path)
{
 struct minix_inode *s_dir; struct minix_inode *d_dir; struct minix_inode *i; struct minix_inode *di;
 char old_name[30];
 char new_name[30];
 ;
 if((s_dir = last_dir(src_path, old_name)) == ((void *)0))
  return -2;
 if((d_dir = last_dir(dest_path, new_name)) == ((void *)0))
  return -2;
 if((i = advance(s_dir, old_name)) == ((void *)0))
  return -2;
 if((di = advance(d_dir, new_name)) != ((void *)0)) {
  dir_delete(d_dir, new_name);
  di->i_nlinks--;
  di->i_dirty = 1;
  put_inode(di);
 }
 dir_add(d_dir, new_name, i->i_num);
 if(dir_delete(s_dir, old_name) != 1) {
  panic("rename(\"%s\", \"%s\"): unable to delete old entry",
   src_path, dest_path);
 }
 put_inode(s_dir);
 put_inode(d_dir);
 put_inode(i);
 ;
 return 1;
}


unlink

decompiled_code:
__int64 __fastcall unlink(const char *a1)
{
const char *v1; // rbp
__int64 v2; // rax
__int64 v3; // rbx
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

v1 = (const char *)last_dir(a1, v5);
if ( !v1 )
panic("unlink(\"%s\"): failed to resolve final dir in path", a1);
LODWORD(v2) = advance(v1, v5);
v3 = v2;
if ( !v2 )
{
put_inode(v1);
panic("unlink(\"%s\"): failed to resolve final component", a1);
}
dir_delete((__int64)v1, v5);
--*(_BYTE *)(v3 + 13);
*(_BYTE *)(v3 + 34) = 1;
put_inode(v3);
put_inode(v1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char *a1)
{
const char *v1; // rbp
__int64 v2; // rax
__int64 v3; // rbx
char v5[56]; // [rsp+0h] [rbp-38h] BYREF

v1 = (const char *)last_dir(a1, v5);
if ( !v1 )
panic("unlink(\"%s\"): failed to resolve final dir in path", a1);
LODWORD(v2) = advance(v1, v5);
v3 = v2;
if ( !v2 )
{
put_inode(v1);
panic("unlink(\"%s\"): failed to resolve final component", a1);
}
func10((__int64)v1, v5);
--*(_BYTE *)(v3 + 13);
*(_BYTE *)(v3 + 34) = 1;
put_inode(v3);
put_inode(v1);
return 1LL;
}

original_code:
int unlink(const char *path)
{
 struct minix_inode *p_dir;
 struct minix_inode *i;
 char filename[30];
 ;
 if((p_dir = last_dir(path, filename)) == ((void *)0))
  panic("unlink(\"%s\"): failed to resolve final dir in path",
   path);
 ;
 if((i = advance(p_dir, filename)) == ((void *)0)) {
  put_inode(p_dir);
  panic("unlink(\"%s\"): failed to resolve final component",
   path);
 }

 dir_delete(p_dir, filename);
 i->i_nlinks--;
 i->i_dirty = 1;
 put_inode(i);
 put_inode(p_dir);
 return 1;
}

canonical_original_code:
int unlink(char *path)
{
 struct minix_inode *p_dir;
 struct minix_inode *i;
 char filename[30];
 ;
 if((p_dir = last_dir(path, filename)) == ((void *)0))
  panic("unlink(\"%s\"): failed to resolve final dir in path",
   path);
 ;
 if((i = advance(p_dir, filename)) == ((void *)0)) {
  put_inode(p_dir);
  panic("unlink(\"%s\"): failed to resolve final component",
   path);
 }

 dir_delete(p_dir, filename);
 i->i_nlinks--;
 i->i_dirty = 1;
 put_inode(i);
 put_inode(p_dir);
 return 1;
}

variable_types:
path
char *

variable_types:
filename
char [30]


original_code:
static void rw_inode(struct minix_inode *i, int rw_flag)
{
 int i_block;
 int i_block_offset;
 struct minix_block *blk;
 i_block = 2 + sb.s_imap_blocks + sb.s_zmap_blocks;
 i_block += ((i->i_num - 1) * sizeof(struct minix_inode_disk)) / 1024;
 i_block_offset = ((i->i_num - 1) * sizeof(struct minix_inode_disk)) % 1024;
 ;
 blk = get_block(i_block, 1);
 if(rw_flag == 1) {
  memcpy((void *)i, (void *) (blk->blk_data + i_block_offset),
   sizeof(struct minix_inode_disk));
 }
 else {
  memcpy((void *) (blk->blk_data + i_block_offset), (void *) i,
   sizeof(struct minix_inode_disk));
  blk->blk_dirty = 1;
 }
 put_block(blk, 0);
 i->i_dirty = 0;
}

variable_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *

user_defined_types: struct minix_inode_disk { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; }


decompiled_code:
char *__fastcall get_inode(unsigned __int16 a1)
{
__m128i *v1; // rbx
char *result; // rax
int v3; // edx

v1 = 0LL;
result = inode_table;
do
{
v3 = *((_DWORD *)result + 9);
if ( v3 <= 0 )
{
v1 = (__m128i *)result;
}
else if ( *((_WORD *)result + 16) == a1 )
{
*((_DWORD *)result + 9) = v3 + 1;
return result;
}
result += 40;
}
while ( result != &inode_table[1280] );
if ( !v1 )
panic("get_inode(%d): no free slots in inode table to read in read in inode", a1);
v1[2].m128i_i16[0] = a1;
v1[2].m128i_i32[1] = 1;
rw_inode(v1, 1);
return (char *)v1;
}

canonical_decompiled_code:
char *__fastcall func1(unsigned __int16 a1)
{
__m128i *v1; // rbx
char *result; // rax
int v3; // edx

v1 = 0LL;
result = inode_table;
do
{
v3 = *((_DWORD *)result + 9);
if ( v3 <= 0 )
{
v1 = (__m128i *)result;
}
else if ( *((_WORD *)result + 16) == a1 )
{
*((_DWORD *)result + 9) = v3 + 1;
return result;
}
result += 40;
}
while ( result != &inode_table[1280] );
if ( !v1 )
panic("get_inode(%d): no free slots in inode table to read in read in inode", a1);
v1[2].m128i_i16[0] = a1;
v1[2].m128i_i32[1] = 1;
func0(v1, 1);
return (char *)v1;
}

original_code:
struct minix_inode *get_inode(inode_nr i_num)
{
 struct minix_inode *i, *free_slot;
 free_slot = 0;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_count > 0) {
   if(i->i_num == i_num) {
    ;
    i->i_count++;
    return i;
   }
  }
  else {
   free_slot = i;
  }
 }
 if(free_slot == 0)
  panic("get_inode(%d): no free slots in inode table to read in "
   "read in inode", i_num);
 ;
 free_slot->i_num = i_num;
 free_slot->i_count = 1;
 rw_inode(free_slot, 1);
 ;
 return free_slot;
}

canonical_original_code:
struct minix_inode *get_inode(unsigned short i_num)
{
 struct minix_inode *i; struct minix_inode *free_slot;
 free_slot = 0;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_count > 0) {
   if(i->i_num == i_num) {
    ;
    i->i_count++;
    return i;
   }
  }
  else {
   free_slot = i;
  }
 }
 if(free_slot == 0)
  panic("get_inode(%d): no free slots in inode table to read in "
   "read in inode", i_num);
 ;
 free_slot->i_num = i_num;
 free_slot->i_count = 1;
 rw_inode(free_slot, 1);
 ;
 return free_slot;
}


decompiled_code:
int print_inode_table()
{
char *v0; // rbx
const char *v1; // rcx
int result; // eax

v0 = inode_table;
do
{
v1 = "No";
if ( v0[34] == 1 )
v1 = "Yes";
result = printf(
" inode %d: count=%d, dirty = %s xxyyzz\n",
*((unsigned __int16 *)v0 + 16),
*((unsigned int *)v0 + 9),
v1);
v0 += 40;
}
while ( v0 != &inode_table[1280] );
return result;
}

canonical_decompiled_code:
int func2()
{
char *v0; // rbx
const char *v1; // rcx
int result; // eax

v0 = inode_table;
do
{
v1 = "No";
if ( v0[34] == 1 )
v1 = "Yes";
result = printf(
" inode %d: count=%d, dirty = %s xxyyzz\n",
*((unsigned __int16 *)v0 + 16),
*((unsigned int *)v0 + 9),
v1);
v0 += 40;
}
while ( v0 != &inode_table[1280] );
return result;
}

original_code:
void print_inode_table(void)
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  printf("    inode %d: count=%d, dirty = %s xxyyzz\n", i->i_num,
   i->i_count, i->i_dirty == 1 ? "Yes" : "No");
 }
}

canonical_original_code:
void print_inode_table()
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  printf("    inode %d: count=%d, dirty = %s xxyyzz\n", i->i_num,
   i->i_count, i->i_dirty == 1 ? "Yes" : "No");
 }
}


decompiled_code:
char *alloc_inode()
{
unsigned __int16 v0; // bp
char *v1; // rbx

v0 = alloc_bit(*(_QWORD *)&sb[16], 0LL);
v1 = get_inode(v0);
wipe_inode((__int64)v1);
*((_WORD *)v1 + 16) = v0;
v1[34] = 1;
return v1;
}

canonical_decompiled_code:
char *func3()
{
unsigned __int16 v0; // bp
char *v1; // rbx

v0 = alloc_bit(*(_QWORD *)&sb[16], 0LL);
v1 = func1(v0);
wipe_inode((__int64)v1);
*((_WORD *)v1 + 16) = v0;
v1[34] = 1;
return v1;
}

original_code:
struct minix_inode *alloc_inode(void)
{
 struct minix_inode *inode;
 inode_nr i_num;
 ;
 i_num = (inode_nr) alloc_bit(sb.imap, 0);
 if(i_num < 0)
  panic("alloc_inode(): no free inodes available");
 inode = get_inode(i_num);
 wipe_inode(inode);
 inode->i_num = i_num;
 inode->i_dirty = 1;
 return inode;
}

canonical_original_code:
struct minix_inode *alloc_inode()
{
 struct minix_inode *inode;
 unsigned short i_num;
 ;
 i_num = (unsigned short) alloc_bit(sb.imap, 0);
 if(i_num < 0)
  panic("alloc_inode(): no free inodes available");
 inode = get_inode(i_num);
 wipe_inode(inode);
 inode->i_num = i_num;
 inode->i_dirty = 1;
 return inode;
}


decompiled_code:
__int64 __fastcall free_inode(unsigned __int16 a1)
{
return free_bit(*(_QWORD *)&sb[16], a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 a1)
{
return free_bit(*(_QWORD *)&sb[16], a1);
}

original_code:
void free_inode(inode_nr i_num)
{
 ;
 free_bit(sb.imap, (int) i_num);
}

canonical_original_code:
void free_inode(unsigned short i_num)
{
 ;
 free_bit(sb.imap, (int) i_num);
}


original_code:
void flush_inode_table(void)
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_dirty == 1) {
   ;
   if(i->i_nlinks == 0) {
    ;
    truncate(i);
    free_inode(i->i_num);
   }
   rw_inode(i, 2);
  }
 }
}

canonical_original_code:
void flush_inode_table()
{
 struct minix_inode *i;
 for(i = &inode_table[0]; i < &inode_table[32]; i++) {
  if(i->i_dirty == 1) {
   ;
   if(i->i_nlinks == 0) {
    ;
    truncate(i);
    free_inode(i->i_num);
   }
   rw_inode(i, 2);
  }
 }
}


decompiled_code:
__int64 __fastcall put_inode(char *file, __off_t a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *((unsigned int *)file + 9);
v3 = (unsigned int)(result - 1);
*((_DWORD *)file + 9) = v3;
if ( (int)v3 < 0 )
{
a2 = *((unsigned __int16 *)file + 16);
result = panic("put_inode(%d): i_count decremeted to %d!", a2, v3);
}
if ( !*((_DWORD *)file + 9) )
{
if ( !file[13] )
{
truncate(file, a2);
result = free_inode(*((_WORD *)file + 16));
}
if ( file[34] == 1 )
result = rw_inode((__m128i *)file, 2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *file, __off_t a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *((unsigned int *)file + 9);
v3 = (unsigned int)(result - 1);
*((_DWORD *)file + 9) = v3;
if ( (int)v3 < 0 )
{
a2 = *((unsigned __int16 *)file + 16);
result = panic("put_inode(%d): i_count decremeted to %d!", a2, v3);
}
if ( !*((_DWORD *)file + 9) )
{
if ( !file[13] )
{
truncate(file, a2);
result = func4(*((_WORD *)file + 16));
}
if ( file[34] == 1 )
result = func0((__m128i *)file, 2);
}
return result;
}

original_code:
void put_inode(struct minix_inode *inode)
{
 inode->i_count--;
 if(inode->i_count < 0) {
  panic("put_inode(%d): i_count decremeted to %d!",
   inode->i_num, inode->i_count);
 }
 if(inode->i_count == 0) {
  if(inode->i_nlinks == 0) {
   ;
   truncate(inode);
   free_inode(inode->i_num);
  }
  if(inode->i_dirty == 1) {
   ;
    rw_inode(inode, 2);
  }
  else {
   ;
  }
 }
 else {
  ;
 }
 ;
}


original_code:
static void read_super(void)
{
 struct minix_block *blk;
 ;
 blk = get_block(1, 1);
 memcpy(&sb, blk->blk_data, sizeof(struct minix_super_block_disk));
 ;
 put_block(blk, 5 + 0200);
}

canonical_original_code:
static void read_super()
{
 struct minix_block *blk;
 ;
 blk = get_block(1, 1);
 memcpy(&sb, blk->blk_data, sizeof(struct minix_super_block_disk));
 ;
 put_block(blk, 5 + 0200);
}

user_defined_types: struct minix_super_block_disk { unsigned short s_ninodes; unsigned short s_nzones; unsigned short s_imap_blocks; unsigned short s_zmap_blocks; unsigned short s_firstdatazone; unsigned short s_log_zone_size; unsigned int s_max_size; unsigned short s_magic; unsigned short s_state; unsigned int s_zones; }


decompiled_code:
__int64 unload_bitmaps()
{
__int64 v0; // rax
__int64 v1; // rbx
__int64 result; // rax
__int64 v3; // rbx

v0 = *((_QWORD *)&sb + 4);
if ( *(int *)v0 > 0 )
{
v1 = 0LL;
do
{
put_block(*(_QWORD *)(*(_QWORD *)(v0 + 8) + 8 * v1), 131LL);
v0 = *((_QWORD *)&sb + 4);
++v1;
}
while ( *(_DWORD *)v0 > (int)v1 );
}
result = *((_QWORD *)&sb + 5);
if ( *(int *)result > 0 )
{
v3 = 0LL;
do
{
put_block(*(_QWORD *)(*(_QWORD *)(result + 8) + 8 * v3), 132LL);
result = *((_QWORD *)&sb + 5);
++v3;
}
while ( *(_DWORD *)result > (int)v3 );
}
return result;
}

original_code:
static void unload_bitmaps(void)
{
 int i;
 for(i = 0; i < sb.imap->num_blocks; i++)
  put_block(sb.imap->blocks[i], 3 + 0200);
 for(i = 0; i < sb.zmap->num_blocks; i++)
  put_block(sb.zmap->blocks[i], 4 + 0200);
}

canonical_original_code:
static void unload_bitmaps()
{
 int i;
 for(i = 0; i < sb.imap->num_blocks; i++)
  put_block(sb.imap->blocks[i], 3 + 0200);
 for(i = 0; i < sb.zmap->num_blocks; i++)
  put_block(sb.zmap->blocks[i], 4 + 0200);
}


decompiled_code:
int minix_print_version()
{
unsigned __int16 v0; // ax

v0 = *((_WORD *)&sb + 8);
if ( v0 == 9320 )
{
puts("identified minix v2 file system");
goto LABEL_9;
}
if ( v0 > 0x2468u )
{
if ( v0 == 9336 )
{
puts("identified minix v2 file system (30 char names");
goto LABEL_9;
}
LABEL_11:
puts("unable to identify minix file system");
goto LABEL_9;
}
if ( v0 == 4991 )
{
puts("identified minix v1 file system");
goto LABEL_9;
}
if ( v0 != 5007 )
goto LABEL_11;
puts("identified minix v1 file system (30 char names");
LABEL_9:
printf("block size = %d bytes\n", 1024LL);
printf("buffer cache size = %dMB\n", 1LL);
printf("buffer cache hash table size = %d\n", 0x4000LL);
return puts("cache write_immed = OFF");
}

canonical_decompiled_code:
int func2()
{
unsigned __int16 v0; // ax

v0 = *((_WORD *)&sb + 8);
if ( v0 == 9320 )
{
puts("identified minix v2 file system");
goto LABEL_9;
}
if ( v0 > 0x2468u )
{
if ( v0 == 9336 )
{
puts("identified minix v2 file system (30 char names");
goto LABEL_9;
}
LABEL_11:
puts("unable to identify minix file system");
goto LABEL_9;
}
if ( v0 == 4991 )
{
puts("identified minix v1 file system");
goto LABEL_9;
}
if ( v0 != 5007 )
goto LABEL_11;
puts("identified minix v1 file system (30 char names");
LABEL_9:
printf("block size = %d bytes\n", 1024LL);
printf("buffer cache size = %dMB\n", 1LL);
printf("buffer cache hash table size = %d\n", 0x4000LL);
return puts("cache write_immed = OFF");
}

original_code:
static void minix_print_version(void)
{
 switch(sb.s_magic) {
  case 0x137F:
   printf("identified minix v1 file system\n");
   break;
  case 0x138F:
   printf("identified minix v1 file system (30 char "
    "names\n");
   break;
  case 0x2468:
   printf("identified minix v2 file system\n");
   break;
  case 0x2478:
   printf("identified minix v2 file system (30 char "
    "names\n");
   break;
  default:
   printf("unable to identify minix file system\n");
 }
 printf("block size = %d bytes\n", 1024);
 printf("buffer cache size = %dMB\n", 1024*1 / 1024);
 printf("buffer cache hash table size = %d\n", 16384);
 printf("cache write_immed = OFF\n");
}

canonical_original_code:
static void minix_print_version()
{
 switch(sb.s_magic) {
  case 0x137F:
   printf("identified minix v1 file system\n");
   break;
  case 0x138F:
   printf("identified minix v1 file system (30 char "
    "names\n");
   break;
  case 0x2468:
   printf("identified minix v2 file system\n");
   break;
  case 0x2478:
   printf("identified minix v2 file system (30 char "
    "names\n");
   break;
  default:
   printf("unable to identify minix file system\n");
 }
 printf("block size = %d bytes\n", 1024);
 printf("buffer cache size = %dMB\n", 1024*1 / 1024);
 printf("buffer cache hash table size = %d\n", 16384);
 printf("cache write_immed = OFF\n");
}


decompiled_code:
_QWORD *get_imap()
{
_QWORD *result; // rax

result = get_bitmap(2u, WORD2(sb));
*((_DWORD *)result + 4) = (unsigned __int16)sb;
return result;
}

canonical_decompiled_code:
_QWORD *func3()
{
_QWORD *result; // rax

result = get_bitmap(2u, WORD2(sb));
*((_DWORD *)result + 4) = (unsigned __int16)sb;
return result;
}

original_code:
static struct generic_bitmap *get_imap()
{
 int imap_offset = 2;
 struct generic_bitmap *bmap;
 bmap = get_bitmap(imap_offset, sb.s_imap_blocks);
 bmap->num_bits = sb.s_ninodes;
 return bmap;
}

variable_types: struct generic_bitmap { int num_blocks; struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } **blocks; int num_bits; } *

user_defined_types: struct generic_bitmap { int num_blocks; struct minix_block **blocks; int num_bits; }


decompiled_code:
_QWORD *get_zmap()
{
_QWORD *result; // rax

result = get_bitmap((unsigned int)WORD2(sb) + 2, WORD3(sb));
*((_DWORD *)result + 4) = WORD1(sb);
return result;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *result; // rax

result = get_bitmap((unsigned int)WORD2(sb) + 2, WORD3(sb));
*((_DWORD *)result + 4) = WORD1(sb);
return result;
}

original_code:
static struct generic_bitmap *get_zmap()
{
 int zmap_offset = 2 +
  sb.s_imap_blocks;
 struct generic_bitmap *bmap;
 bmap = get_bitmap(zmap_offset, sb.s_zmap_blocks);
 bmap->num_bits = sb.s_nzones;
 return bmap;
}

return_type: struct generic_bitmap { int num_blocks; struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } **blocks; int num_bits; } *


decompiled_code:
_QWORD *load_bitmaps()
{
_QWORD *result; // rax

*((_QWORD *)&sb + 4) = get_imap();
result = get_zmap();
*((_QWORD *)&sb + 5) = result;
return result;
}

canonical_decompiled_code:
_QWORD *func5()
{
_QWORD *result; // rax

*((_QWORD *)&sb + 4) = func3();
result = func4();
*((_QWORD *)&sb + 5) = result;
return result;
}

original_code:
static void load_bitmaps(void)
{
 ;
 sb.imap = get_imap();
 sb.zmap = get_zmap();
}

canonical_original_code:
static void load_bitmaps()
{
 ;
 sb.imap = get_imap();
 sb.zmap = get_zmap();
}


decompiled_code:
__int64 __fastcall minix_mount(char *src)
{
size_t v1; // rax
char *v2; // rax
__int64 result; // rax

open_blk_device();
init_cache();
read_super();
minix_print_version();
v1 = strlen(src);
v2 = (char *)malloc(v1);
*((_QWORD *)&sb + 3) = v2;
strcpy(v2, src);
load_bitmaps();
result = get_inode(1LL);
*((_QWORD *)&sb + 6) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src)
{
size_t v1; // rax
char *v2; // rax
__int64 result; // rax

open_blk_device();
init_cache();
func0();
func2();
v1 = strlen(src);
v2 = (char *)malloc(v1);
*((_QWORD *)&sb + 3) = v2;
strcpy(v2, src);
func5();
result = get_inode(1LL);
*((_QWORD *)&sb + 6) = result;
return result;
}

original_code:
void minix_mount(const char *device_name)
{
 open_blk_device(device_name);
 init_cache();
 read_super();
 minix_print_version();
 sb.device_name = malloc(strlen(device_name));
 strcpy(sb.device_name, device_name);
 load_bitmaps();
 sb.root_inode = get_inode((inode_nr) 1);
}

canonical_original_code:
void minix_mount(char *device_name)
{
 open_blk_device(device_name);
 init_cache();
 read_super();
 minix_print_version();
 sb.device_name = malloc(strlen(device_name));
 strcpy(sb.device_name, device_name);
 load_bitmaps();
 sb.root_inode = get_inode((unsigned short) 1);
}


original_code:
int timeval_subtract(struct timeval *result, struct timeval *x,
 struct timeval *y)
{
 if (x->tv_usec < y->tv_usec) {
  int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  y->tv_usec -= 1000000 * nsec;
  y->tv_sec += nsec;
 }
 if (x->tv_usec - y->tv_usec > 1000000) {
  int nsec = (y->tv_usec - x->tv_usec) / 1000000;
  y->tv_usec += 1000000 * nsec;
  y->tv_sec -= nsec;
 }
 result->tv_sec = x->tv_sec - y->tv_sec;
 result->tv_usec = x->tv_usec - y->tv_usec;
 return x->tv_sec < y->tv_sec;
}

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

variable_types:
x
struct timeval *

variable_types:
y
struct timeval *

variable_types: nsec int


decompiled_code:
__int64 minix_unmount()
{
int v0; // ebp
int v1; // ebx
__int64 v3[2]; // [rsp+0h] [rbp-48h] BYREF
struct timeval v4; // [rsp+10h] [rbp-38h] BYREF
struct timeval tv; // [rsp+20h] [rbp-28h] BYREF

v0 = clock();
gettimeofday(&tv, 0LL);
put_inode(*((_QWORD *)&sb + 6));
flush_inode_table();
unload_bitmaps();
sync_cache();
v1 = clock();
gettimeofday(&v4, 0LL);
timeval_subtract(v3, &v4, &tv);
printf("INFO: ");
printf("unmount(): CPU time elapsed: %f seconds\n", (float)((float)(v1 - v0) / 1000000.0));
printf("INFO: ");
printf("umount(): Wall time elapsed: %ld.%06ld seconds\n", v3[0], v3[1]);
printf("INFO: ");
puts("unmount(): destroying cache...");
return cache_destroy();
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // ebp
int v1; // ebx
__int64 v3[2]; // [rsp+0h] [rbp-48h] BYREF
struct timeval v4; // [rsp+10h] [rbp-38h] BYREF
struct timeval tv; // [rsp+20h] [rbp-28h] BYREF

v0 = clock();
gettimeofday(&tv, 0LL);
put_inode(*((_QWORD *)&sb + 6));
flush_inode_table();
func1();
sync_cache();
v1 = clock();
gettimeofday(&v4, 0LL);
func7(v3, &v4, &tv);
printf("INFO: ");
printf("unmount(): CPU time elapsed: %f seconds\n", (float)((float)(v1 - v0) / 1000000.0));
printf("INFO: ");
printf("umount(): Wall time elapsed: %ld.%06ld seconds\n", v3[0], v3[1]);
printf("INFO: ");
puts("unmount(): destroying cache...");
return cache_destroy();
}

original_code:
void minix_unmount(void)
{
 clock_t cpu_start, cpu_end;
 struct timeval wall_start, wall_end, wall_elapsed;
 float cpu_elapsed;
 cpu_start = clock();
 gettimeofday(&wall_start, ((void *)0));
 ;
 put_inode(sb.root_inode);
 ;
 flush_inode_table();
 ;
 unload_bitmaps();
 ;
 sync_cache();
 cpu_end = clock();
 gettimeofday(&wall_end, ((void *)0));
 cpu_elapsed = ((float) (cpu_end - cpu_start)) / ((__clock_t) 1000000);
 timeval_subtract(&wall_elapsed, &wall_end, &wall_start);
 printf("INFO: "); printf("unmount(): CPU time elapsed: %f seconds" "\n", cpu_elapsed);;
 printf("INFO: "); printf("umount(): Wall time elapsed: %ld.%06ld seconds" "\n", (long) wall_elapsed.tv_sec, (long) wall_elapsed.tv_usec);;
 printf("INFO: "); printf("unmount(): destroying cache..." "\n");;
 cache_destroy();
}

canonical_original_code:
void minix_unmount()
{
 long cpu_start; long cpu_end;
 struct timeval wall_start; struct timeval wall_end; struct timeval wall_elapsed;
 float cpu_elapsed;
 cpu_start = clock();
 gettimeofday(&wall_start, ((void *)0));
 ;
 put_inode(sb.root_inode);
 ;
 flush_inode_table();
 ;
 unload_bitmaps();
 ;
 sync_cache();
 cpu_end = clock();
 gettimeofday(&wall_end, ((void *)0));
 cpu_elapsed = ((float) (cpu_end - cpu_start)) / ((long) 1000000);
 timeval_subtract(&wall_elapsed, &wall_end, &wall_start);
 printf("INFO: "); printf("unmount(): CPU time elapsed: %f seconds" "\n", cpu_elapsed);;
 printf("INFO: "); printf("umount(): Wall time elapsed: %ld.%06ld seconds" "\n", (long) wall_elapsed.tv_sec, (long) wall_elapsed.tv_usec);;
 printf("INFO: "); printf("unmount(): destroying cache..." "\n");;
 cache_destroy();
}

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


decompiled_code:
__int64 __fastcall short_array_add(__int16 a1, __int64 a2)
{
__int64 result; // rax
int v3; // eax

if ( *(_DWORD *)a2 == *(_DWORD *)(a2 + 4) )
{
v3 = 2 * *(_DWORD *)a2;
*(_DWORD *)a2 = v3;
*(_QWORD *)(a2 + 8) = realloc(*(void **)(a2 + 8), 2LL * v3);
}
result = *(_QWORD *)(a2 + 8);
*(_WORD *)(result + 2LL * (int)(*(_DWORD *)(a2 + 4))++) = a1;
return result;
}

original_code:
void short_array_add(short s, struct short_array *array)
{
 if(array->capacity == array->size) {
  array->capacity *= 2;
  array->data = (short *) realloc(array->data,
   array->capacity * sizeof(short));
 }
 array->data[array->size] = s;
 array->size++;
}

variable_types: s short

variable_types: struct short_array { int capacity; int size; short *data; } *


decompiled_code:
void __fastcall short_array_destroy(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func22(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void short_array_destroy(struct short_array *array)
{
 free(array->data);
 free(array);
}

user_defined_types: struct short_array { int capacity; int size; short *data; }


original_code:
static int path_get_nth_cmpo(const char *path, char *buf, int n)
{
 int offset = 0;
 while(*(path + offset) == '/')
  offset++;
 for( ; *(path+offset) != '\0' && n != 0; ) {
  if(*(path+offset) == '/') {
   while(*(path+offset)=='/') offset++;
   n--;
   continue;
  }
  offset++;
 }
 if(n != 0) return 0;
 if(*(path+offset) == '\0') return 0;
 while(*(path+offset) != '/' && *(path+offset) != '\0') {
  *buf = *(path+offset);
  offset++;
  buf++;
 }
 *buf = '\0';
 return 1;
}

canonical_original_code:
static int path_get_nth_cmpo(char *path, char *buf, int n)
{
 int offset = 0;
 while(*(path + offset) == '/')
  offset++;
 for( ; *(path+offset) != '\0' && n != 0; ) {
  if(*(path+offset) == '/') {
   while(*(path+offset)=='/') offset++;
   n--;
   continue;
  }
  offset++;
 }
 if(n != 0) return 0;
 if(*(path+offset) == '\0') return 0;
 while(*(path+offset) != '/' && *(path+offset) != '\0') {
  *buf = *(path+offset);
  offset++;
  buf++;
 }
 *buf = '\0';
 return 1;
}

variable_types:
buf
char *

variable_types: n int

variable_types: offset int


decompiled_code:
__int64 __fastcall path_cnt_cmpos(char *a1)
{
unsigned int v1; // edx
char i; // al

v1 = 0;
while ( *a1 )
{
for ( i = *a1; *a1 == 47; i = *a1 )
++a1;
for ( v1 -= (i == 0) - 1; *a1 != 47 && *a1; ++a1 )
;
}
return v1;
}

original_code:
int path_cnt_cmpos(const char *path)
{
 int i = 0;
 while(*path != '\0') {
  while(*path == '/' && *path != '\0')
   path++;
  if(*path != '\0') i++;
  while(*path != '/' && *path != '\0')
   path++;
 }
 return i;
}

canonical_original_code:
int path_cnt_cmpos(char *path)
{
 int i = 0;
 while(*path != '\0') {
  while(*path == '/' && *path != '\0')
   path++;
  if(*path != '\0') i++;
  while(*path != '/' && *path != '\0')
   path++;
 }
 return i;
}


decompiled_code:
__int64 __fastcall path_get_last_cmpo(char *a1, _BYTE *a2)
{
int v2; // eax
unsigned int v3; // edx

v2 = path_cnt_cmpos(a1);
v3 = 0;
if ( v2 > 0 )
{
path_get_nth_cmpo(a1, a2, v2 - 1);
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, _BYTE *a2)
{
int v2; // eax
unsigned int v3; // edx

v2 = func1(a1);
v3 = 0;
if ( v2 > 0 )
{
func0(a1, a2, v2 - 1);
v3 = 1;
}
return v3;
}

original_code:
int path_get_last_cmpo(const char *path, char *buf)
{
 int cnt = 0;
 if((cnt = path_cnt_cmpos(path)) > 0) {
  path_get_nth_cmpo(path, buf, cnt-1);
  return 1;
 }
 return 0;
}

canonical_original_code:
int path_get_last_cmpo(char *path, char *buf)
{
 int cnt = 0;
 if((cnt = path_cnt_cmpos(path)) > 0) {
  path_get_nth_cmpo(path, buf, cnt-1);
  return 1;
 }
 return 0;
}

variable_types: cnt int


decompiled_code:
__int64 __fastcall resolve_path(__int64 a1, char *a2, int a3)
{
int v3; // er12
__int64 v4; // r14
unsigned int v5; // ebp
unsigned __int16 v7; // bx
_BYTE v8[72]; // [rsp+0h] [rbp-48h] BYREF

v3 = a3;
if ( a3 == -1 )
v3 = path_cnt_cmpos(a2);
if ( !v3 )
return get_inode(*(unsigned __int16 *)(a1 + 32));
v4 = get_inode(*(unsigned __int16 *)(a1 + 32));
if ( v3 > 0 )
{
v5 = 0;
while ( 1 )
{
if ( !(unsigned int)path_get_nth_cmpo(a2, v8, v5) )
panic("resolve_path(): couldn't get component %d", v5);
v7 = dir_search(v4, v8);
if ( !v7 )
break;
put_inode(v4);
v4 = get_inode(v7);
if ( v3 == ++v5 )
return v4;
}
put_inode(v4);
v4 = 0LL;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char *a2, int a3)
{
int v3; // er12
__int64 v4; // r14
unsigned int v5; // ebp
unsigned __int16 v7; // bx
_BYTE v8[72]; // [rsp+0h] [rbp-48h] BYREF

v3 = a3;
if ( a3 == -1 )
v3 = func1(a2);
if ( !v3 )
return get_inode(*(unsigned __int16 *)(a1 + 32));
v4 = get_inode(*(unsigned __int16 *)(a1 + 32));
if ( v3 > 0 )
{
v5 = 0;
while ( 1 )
{
if ( !(unsigned int)func0(a2, v8, v5) )
panic("resolve_path(): couldn't get component %d", v5);
v7 = dir_search(v4, v8);
if ( !v7 )
break;
put_inode(v4);
v4 = get_inode(v7);
if ( v3 == ++v5 )
return v4;
}
put_inode(v4);
v4 = 0LL;
}
return v4;
}

original_code:
struct minix_inode *resolve_path(struct minix_inode *root, const char *path,
 int levels)
{
 char file[30];
 int i;
 struct minix_inode *inode;
 inode_nr i_num;
 if(levels == -1)
  levels = path_cnt_cmpos(path);
 if(levels == 0)
  return get_inode(root->i_num);
 inode = get_inode(root->i_num);
 for(i = 0; i < levels; i++) {
  if(!path_get_nth_cmpo(path, file, i)) {
   panic("resolve_path(): couldn't get component %d", i);
  }
  i_num = dir_search(inode, file);
  if(i_num == 0) {
   ;
   put_inode(inode);
   return ((void *)0);
  }
  put_inode(inode);
  inode = get_inode(i_num);
 }
 ;
 return inode;
}

canonical_original_code:
struct minix_inode *resolve_path(struct minix_inode *root, char *path, int levels)
{
 char file[30];
 int i;
 struct minix_inode *inode;
 unsigned short i_num;
 if(levels == -1)
  levels = path_cnt_cmpos(path);
 if(levels == 0)
  return get_inode(root->i_num);
 inode = get_inode(root->i_num);
 for(i = 0; i < levels; i++) {
  if(!path_get_nth_cmpo(path, file, i)) {
   panic("resolve_path(): couldn't get component %d", i);
  }
  i_num = dir_search(inode, file);
  if(i_num == 0) {
   ;
   put_inode(inode);
   return ((void *)0);
  }
  put_inode(inode);
  inode = get_inode(i_num);
 }
 ;
 return inode;
}

variable_types:
root
struct minix_inode *

variable_types: levels int

variable_types:
file
char [30]

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *


decompiled_code:
__int64 __fastcall last_dir(char *a1, _BYTE *a2)
{
int v2; // ebp
int v3; // edx
__int64 result; // rax

v2 = path_cnt_cmpos(a1);
v3 = path_get_last_cmpo(a1, a2);
result = 0LL;
if ( v3 )
result = resolve_path(sb[6], a1, v2 - 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, _BYTE *a2)
{
int v2; // ebp
int v3; // edx
__int64 result; // rax

v2 = func1(a1);
v3 = func2(a1, a2);
result = 0LL;
if ( v3 )
result = func3(sb[6], a1, v2 - 1);
return result;
}

original_code:
struct minix_inode *last_dir(const char *path, char *buf)
{
 int n = path_cnt_cmpos(path);
 struct minix_inode *p_dir;
 if(path_get_last_cmpo(path, buf) == 0)
  return ((void *)0);
 p_dir = resolve_path(sb.root_inode, path, n - 1);
 return p_dir;
}

canonical_original_code:
struct minix_inode *last_dir(char *path, char *buf)
{
 int n = path_cnt_cmpos(path);
 struct minix_inode *p_dir;
 if(path_get_last_cmpo(path, buf) == 0)
  return ((void *)0);
 p_dir = resolve_path(sb.root_inode, path, n - 1);
 return p_dir;
}

variable_types:
path
char *


advance

decompiled_code:
__int64 __fastcall advance(__int64 a1, __int64 a2)
{
unsigned __int16 v2; // ax
__int64 v3; // rdx

v2 = dir_search(a1, a2);
v3 = 0LL;
if ( v2 )
v3 = get_inode(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
unsigned __int16 v2; // ax
__int64 v3; // rdx

v2 = dir_search(a1, a2);
v3 = 0LL;
if ( v2 )
v3 = get_inode(v2);
return v3;
}

original_code:
struct minix_inode *advance(struct minix_inode *p_dir, const char *filename)
{
 inode_nr i_num;
 struct minix_inode *retval;
 ;
 if((i_num = dir_search(p_dir, filename)) == 0) {
  ;
  return ((void *)0);
 }
 retval = get_inode(i_num);
 return retval;
}

canonical_original_code:
struct minix_inode *advance(struct minix_inode *p_dir, char *filename)
{
 unsigned short i_num;
 struct minix_inode *retval;
 ;
 if((i_num = dir_search(p_dir, filename)) == 0) {
  ;
  return ((void *)0);
 }
 retval = get_inode(i_num);
 return retval;
}

variable_types:
filename
char *


original_code:
zone_nr read_map(struct minix_inode *inode, int byte_offset)
{
 int rel_z = byte_offset / 1024;
 zone_nr z;
 struct minix_block *blk;
 int excess;
 if(rel_z < 7) {
  if((z = inode->i_zone[rel_z]) == 0)
   return 0;
  return z;
 }
 excess = rel_z - 7;
 if(excess < (1024/2)) {
  z = inode->i_zone[7];
 }
 else {
  if((z = inode->i_zone[7 +1]) == 0)
   return 0;
  excess -= (1024/2);
  blk = get_block(z, 1);
  z = ((u16 *)blk->blk_data)[excess/(1024/2)];
  excess = excess % (1024/2);
  put_block(blk, 2);
 }
 if(z == 0) return 0;
 blk = get_block(z, 1);
 z = ((u16 *)blk->blk_data)[excess];
 put_block(blk, 2);
 return z;
}

canonical_original_code:
unsigned short read_map(struct minix_inode *inode, int byte_offset)
{
 int rel_z = byte_offset / 1024;
 unsigned short z;
 struct minix_block *blk;
 int excess;
 if(rel_z < 7) {
  if((z = inode->i_zone[rel_z]) == 0)
   return 0;
  return z;
 }
 excess = rel_z - 7;
 if(excess < (1024/2)) {
  z = inode->i_zone[7];
 }
 else {
  if((z = inode->i_zone[7 +1]) == 0)
   return 0;
  excess -= (1024/2);
  blk = get_block(z, 1);
  z = ((unsigned short *)blk->blk_data)[excess/(1024/2)];
  excess = excess % (1024/2);
  put_block(blk, 2);
 }
 if(z == 0) return 0;
 blk = get_block(z, 1);
 z = ((unsigned short *)blk->blk_data)[excess];
 put_block(blk, 2);
 return z;
}

variable_types:
z
unsigned short

variable_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *

variable_types: excess int


decompiled_code:
__int64 __fastcall dir_search(__int64 a1, const char *a2)
{
int v2; // er15
unsigned int v3; // eax
unsigned int v4; // ebx
__int64 v5; // r13
__int64 v6; // r14
__int64 v7; // rbx

v2 = 0;
LABEL_2:
v3 = read_map(a1, v2);
v4 = v3;
if ( (_WORD)v3 )
{
v5 = get_block((unsigned __int16)v3, 1LL);
v6 = *(_QWORD *)(v5 + 40);
v7 = 0LL;
while ( strcmp((const char *)(v6 + 2 + v7), a2) )
{
v7 += 32LL;
if ( v7 == 1024 )
{
put_block(v5, 1LL);
v2 += 1024;
goto LABEL_2;
}
}
v4 = *(unsigned __int16 *)(v6 + v7);
put_block(v5, 1LL);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
int v2; // er15
unsigned int v3; // eax
unsigned int v4; // ebx
__int64 v5; // r13
__int64 v6; // r14
__int64 v7; // rbx

v2 = 0;
LABEL_2:
v3 = func0(a1, v2);
v4 = v3;
if ( (_WORD)v3 )
{
v5 = get_block((unsigned __int16)v3, 1LL);
v6 = *(_QWORD *)(v5 + 40);
v7 = 0LL;
while ( strcmp((const char *)(v6 + 2 + v7), a2) )
{
v7 += 32LL;
if ( v7 == 1024 )
{
put_block(v5, 1LL);
v2 += 1024;
goto LABEL_2;
}
}
v4 = *(unsigned __int16 *)(v6 + v7);
put_block(v5, 1LL);
}
return v4;
}

original_code:
inode_nr dir_search(struct minix_inode *inode, const char *file)
{
 struct minix_block *blk;
 zone_nr z;
 int c_pos = 0;
 int i;
 inode_nr retval;
 ;
 while((z = read_map(inode, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    retval = *((inode_nr *)(blk->blk_data + i));
    put_block(blk, 1);
    ;
    return retval;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 ;
 return 0;
}

canonical_original_code:
unsigned short dir_search(struct minix_inode *inode, char *file)
{
 struct minix_block *blk;
 unsigned short z;
 int c_pos = 0;
 int i;
 unsigned short retval;
 ;
 while((z = read_map(inode, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    retval = *((unsigned short *)(blk->blk_data + i));
    put_block(blk, 1);
    ;
    return retval;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 ;
 return 0;
}

variable_types:
file
char *

variable_types: i int

variable_types:
retval
unsigned short


original_code:
int minix_read(struct minix_inode *inode, char *buf, size_t size, off_t offset)
{
 int sbytes = 0;
 int nbytes = size;
 int c_pos = offset;
 zone_nr z = 0;
 zone_nr z_data = 0;
 int z_offset = 0;
 int chunk = 0;
 struct minix_block *blk;
 if(nbytes > inode->i_size)
  nbytes = inode->i_size;
 while(nbytes > 0) {
  z = c_pos / 1024;
  z_offset = c_pos % 1024;
  chunk = ((nbytes) < ((1024 - z_offset)) ? (nbytes) : ((1024 - z_offset)));
  if((z_data = read_map(inode, z * 1024)) == 0)
   return sbytes;
  blk = get_block(z_data, 1);
  memcpy(buf + sbytes, blk->blk_data + z_offset, chunk);
  put_block(blk, 6);
  sbytes += chunk;
  nbytes -= chunk;
  c_pos += chunk;
 }
 return sbytes;
}

canonical_original_code:
int minix_read(struct minix_inode *inode, char *buf, unsigned long size, long offset)
{
 int sbytes = 0;
 int nbytes = size;
 int c_pos = offset;
 unsigned short z = 0;
 unsigned short z_data = 0;
 int z_offset = 0;
 int chunk = 0;
 struct minix_block *blk;
 if(nbytes > inode->i_size)
  nbytes = inode->i_size;
 while(nbytes > 0) {
  z = c_pos / 1024;
  z_offset = c_pos % 1024;
  chunk = ((nbytes) < ((1024 - z_offset)) ? (nbytes) : ((1024 - z_offset)));
  if((z_data = read_map(inode, z * 1024)) == 0)
   return sbytes;
  blk = get_block(z_data, 1);
  memcpy(buf + sbytes, blk->blk_data + z_offset, chunk);
  put_block(blk, 6);
  sbytes += chunk;
  nbytes -= chunk;
  c_pos += chunk;
 }
 return sbytes;
}

variable_types:
size
unsigned long

variable_types: offset long

variable_types: sbytes int

variable_types: nbytes int

variable_types: chunk int