decompiled_code:
int __fastcall dsp_open(const char *a1)
{
int result; // eax

result = open(a1, 2);
fd = result;
if ( result < 0 )
{
fprintf(stderr, "open of %s failed\n", a1);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int result; // eax

result = open(a1, 2);
fd = result;
if ( result < 0 )
{
fprintf(stderr, "open of %s failed\n", a1);
exit(1);
}
return result;
}

original_code:
void dsp_open(char *dev)
{
    fd = open(dev, 02);
    if (fd < 0) {
        fprintf(stderr, "open of %s failed\n", dev);
        exit(1);
    }
}


repo: adilinden/repeater-controller

decompiled_code:
int __fastcall dsp_setup(int a1)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 16;
if ( ioctl(fd, 0xC0045005uLL, v2) < -1 )
fwrite("SNDCTL_DSP_SETFMT ioctl failed\n", 1uLL, 0x1FuLL, stderr);
v2[0] = 1;
if ( ioctl(fd, 0xC0045006uLL, v2) < -1 )
fwrite("SNDCTL_DSP_CHANNELS ioctl failed\n", 1uLL, 0x21uLL, stderr);
if ( v2[0] != 1 )
fwrite("unable to set channels\n", 1uLL, 0x17uLL, stderr);
v2[0] = a1;
result = ioctl(fd, 0xC0045002uLL, v2);
if ( result < -1 )
result = fwrite("SNDCTL_DSP_SPEED ioctl failed\n", 1uLL, 0x1EuLL, stderr);
if ( v2[0] != a1 )
result = fprintf(stderr, "unable to set sample rate, using %d\n", (unsigned int)v2[0]);
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 16;
if ( ioctl(fd, 0xC0045005uLL, v2) < -1 )
fwrite("SNDCTL_DSP_SETFMT ioctl failed\n", 1uLL, 0x1FuLL, stderr);
v2[0] = 1;
if ( ioctl(fd, 0xC0045006uLL, v2) < -1 )
fwrite("SNDCTL_DSP_CHANNELS ioctl failed\n", 1uLL, 0x21uLL, stderr);
if ( v2[0] != 1 )
fwrite("unable to set channels\n", 1uLL, 0x17uLL, stderr);
v2[0] = a1;
result = ioctl(fd, 0xC0045002uLL, v2);
if ( result < -1 )
result = fwrite("SNDCTL_DSP_SPEED ioctl failed\n", 1uLL, 0x1EuLL, stderr);
if ( v2[0] != a1 )
result = fprintf(stderr, "unable to set sample rate, using %d\n", (unsigned int)v2[0]);
return result;
}

variable_types: rate int

variable_types: a int

variable_types: r int

return_type: void


decompiled_code:
ssize_t __fastcall dsp_write(const void **a1, int *a2)
{
return write(fd, *a1, *a2);
}

canonical_decompiled_code:
ssize_t __fastcall func2(const void **a1, int *a2)
{
return write(fd, *a1, *a2);
}

original_code:
int dsp_write(int16_t **bf, int *nbf)
{
    return write(fd, *bf, *nbf);
}

canonical_original_code:
int dsp_write(short **bf, int *nbf)
{
    return write(fd, *bf, *nbf);
}

variable_types:
bf
short **

variable_types:
nbf
int *

return_type: int


decompiled_code:
int dsp_close()
{
return close(fd);
}

canonical_decompiled_code:
int func3()
{
return close(fd);
}

original_code:
void dsp_close()
{
    close(fd);
}

canonical_original_code:
void dsp_close()
{
    close(fd);
}


portctl

decompiled_code:
__int64 __fastcall portctl(char a1, unsigned int a2, const char *a3)
{
char v4[269]; // [rsp+0h] [rbp-128h] BYREF
char v5; // [rsp+10Dh] [rbp-1Bh] BYREF
char v6; // [rsp+10Eh] [rbp-1Ah]
char v7[25]; // [rsp+10Fh] [rbp-19h] BYREF

sprintf(v4, "Doing: %s", a3);
do_log(v4);
if ( (int)irlpdev_open() < 0 )
{
fwrite("Can't open parallel port", 1uLL, 0x18uLL, stderr);
}
else if ( (unsigned int)read_irlpdev(&v5, 2LL) == 2 )
{
if ( a2 == 1 )
{
v7[0] = v6 | a1;
}
else if ( !a2 )
{
v7[0] = v6 & ~a1;
}
write_irlpdev(v7, 1LL);
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1, unsigned int a2, const char *a3)
{
char v4[269]; // [rsp+0h] [rbp-128h] BYREF
char v5; // [rsp+10Dh] [rbp-1Bh] BYREF
char v6; // [rsp+10Eh] [rbp-1Ah]
char v7[25]; // [rsp+10Fh] [rbp-19h] BYREF

sprintf(v4, "Doing: %s", a3);
func29(v4);
if ( (int)func23() < 0 )
{
fwrite("Can't open parallel port", 1uLL, 0x18uLL, stderr);
}
else if ( (unsigned int)func24(&v5, 2LL) == 2 )
{
if ( a2 == 1 )
{
v7[0] = v6 | a1;
}
else if ( !a2 )
{
v7[0] = v6 & ~a1;
}
func25(v7, 1LL);
}
return a2;
}

original_code:
int portctl(char mask, int pin, char *name)
{
    unsigned char out;
    unsigned char c[2];
    char str[255];
    sprintf(str, "Doing: %s", name);
    do_log(str);
    if ( irlpdev_open() < 0 ) {
        fprintf(stderr, "Can't open parallel port");
        return pin;
    }
    if (read_irlpdev(c, 2) != 2) return pin;
    if (pin == 1) out = (c[1] | mask);
    if (pin == 0) out = (c[1] & ~mask);
    if (write_irlpdev(&out, 1) != 1) return pin;
    return pin;
}

variable_types: mask char

variable_types: pin int

variable_types:
name
char *

variable_types:
out
unsigned char

variable_types:
c
unsigned char [2]

variable_types:
str
char [255]


unkey

decompiled_code:
__int64 unkey()
{
portctl(4, 0, "unkey");
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
func7(4, 0, "unkey");
return 0LL;
}

original_code:
int unkey()
{
    int pin = 0;
    char mask = 0x04;
    portctl(mask, pin, "unkey");
    return 0;
}


key

decompiled_code:
__int64 key()
{
portctl(4, 1u, "key");
return 1LL;
}

canonical_decompiled_code:
__int64 func9()
{
func7(4, 1u, "key");
return 1LL;
}

original_code:
int key()
{
    int pin = 1;
    char mask = 0x04;
    portctl(mask, pin, "key");
    return 1;
}


keyup

decompiled_code:
__int64 keyup()
{
portctl(4, 1u, "keyup");
return 1LL;
}

canonical_decompiled_code:
__int64 func10()
{
func7(4, 1u, "keyup");
return 1LL;
}

original_code:
int keyup()
{
    int pin = 1;
    char mask = 0x04;
    portctl(mask, pin, "keyup");
    return 1;
}


unmute

decompiled_code:
__int64 unmute()
{
portctl(8, 1u, "unmute");
return 0LL;
}

canonical_decompiled_code:
__int64 func11()
{
func7(8, 1u, "unmute");
return 0LL;
}

original_code:
int unmute()
{
    int pin = 1;
    char mask = 0x08;
    portctl(mask, pin, "unmute");
    return 0;
}


mute

decompiled_code:
__int64 mute()
{
portctl(8, 0, "mute");
return 1LL;
}

canonical_decompiled_code:
__int64 func12()
{
func7(8, 0, "mute");
return 1LL;
}

original_code:
int mute()
{
    int pin = 0;
    char mask = 0x08;
    portctl(mask, pin, "mute");
    return 1;
}


ctcssoff

decompiled_code:
__int64 ctcssoff()
{
portctl(16, 1u, "ctcssoff");
return 0LL;
}

canonical_decompiled_code:
__int64 func13()
{
func7(16, 1u, "ctcssoff");
return 0LL;
}

original_code:
int ctcssoff()
{
    int pin = 1;
    char mask = 0x10;
    portctl(mask, pin, "ctcssoff");
    return 0;
}


ctcsson

decompiled_code:
__int64 ctcsson()
{
portctl(16, 0, "ctcsson");
return 1LL;
}

canonical_decompiled_code:
__int64 func14()
{
func7(16, 0, "ctcsson");
return 1LL;
}

original_code:
int ctcsson()
{
    int pin = 0;
    char mask = 0x10;
    portctl(mask, pin, "ctcsson");
    return 1;
}


fanoff

decompiled_code:
__int64 fanoff()
{
portctl(32, 0, "fanoff");
return 0LL;
}

canonical_decompiled_code:
__int64 func15()
{
func7(32, 0, "fanoff");
return 0LL;
}

original_code:
int fanoff()
{
    int pin = 0;
    char mask = 0x20;
    portctl(mask, pin, "fanoff");
    return 0;
}


decompiled_code:
__int64 aux4off()
{
portctl(32, 0, "aux4off");
return 0LL;
}

canonical_decompiled_code:
__int64 func16()
{
func7(32, 0, "aux4off");
return 0LL;
}

original_code:
int aux4off()
{
    int pin = 0;
    char mask = 0x20;
    portctl(mask, pin, "aux4off");
    return 0;
}


fanon

decompiled_code:
__int64 fanon()
{
portctl(32, 1u, "fanon");
return 1LL;
}

canonical_decompiled_code:
__int64 func17()
{
func7(32, 1u, "fanon");
return 1LL;
}

original_code:
int fanon()
{
    int pin = 1;
    char mask = 0x20;
    portctl(mask, pin, "fanon");
    return 1;
}


decompiled_code:
__int64 aux4on()
{
portctl(32, 1u, "aux4on");
return 1LL;
}

canonical_decompiled_code:
__int64 func18()
{
func7(32, 1u, "aux4on");
return 1LL;
}

original_code:
int aux4on()
{
    int pin = 1;
    char mask = 0x20;
    portctl(mask, pin, "aux4on");
    return 1;
}


decompiled_code:
__int64 aux5off()
{
portctl(64, 0, "aux5off");
return 0LL;
}

canonical_decompiled_code:
__int64 func19()
{
func7(64, 0, "aux5off");
return 0LL;
}

original_code:
int aux5off()
{
    int pin = 0;
    char mask = 0x40;
    portctl(mask, pin, "aux5off");
    return 0;
}


decompiled_code:
__int64 aux5on()
{
portctl(64, 1u, "aux5on");
return 1LL;
}

canonical_decompiled_code:
__int64 func20()
{
func7(64, 1u, "aux5on");
return 1LL;
}

original_code:
int aux5on()
{
    int pin = 1;
    char mask = 0x40;
    portctl(mask, pin, "aux5on");
    return 1;
}


ppclaim

decompiled_code:
int ppclaim()
{
int result; // eax
int *v1; // rax
int *v2; // rax

if ( fd < 0 )
return -1;
if ( lockfilefd < 0 )
{
lockfilefd = open("/tmp/irlp-lockfile-parport0", 65, 436LL);
if ( lockfilefd < 0 )
{
perror("Can't open lock file /tmp/irlp-lockfile-parport0");
v1 = __errno_location();
exit(*v1);
}
}
if ( flock(lockfilefd, 2) < 0 )
{
perror("Can't optain exclusive lock on /tmp/irlp-lockfile-parport0");
v2 = __errno_location();
exit(*v2);
}
result = ioctl(fd, 0x708BuLL);
if ( result )
{
perror("PPCLAIM");
flock(lockfilefd, 8);
result = -1;
}
return result;
}

canonical_decompiled_code:
int func21()
{
int result; // eax
int *v1; // rax
int *v2; // rax

if ( fd < 0 )
return -1;
if ( lockfilefd < 0 )
{
lockfilefd = open("/tmp/irlp-lockfile-parport0", 65, 436LL);
if ( lockfilefd < 0 )
{
perror("Can't open lock file /tmp/irlp-lockfile-parport0");
v1 = __errno_location();
exit(*v1);
}
}
if ( flock(lockfilefd, 2) < 0 )
{
perror("Can't optain exclusive lock on /tmp/irlp-lockfile-parport0");
v2 = __errno_location();
exit(*v2);
}
result = ioctl(fd, 0x708BuLL);
if ( result )
{
perror("PPCLAIM");
flock(lockfilefd, 8);
result = -1;
}
return result;
}

original_code:
int ppclaim() {
    if( fd < 0 )
        return -1;
    if( lockfilefd < 0 ) {
        lockfilefd = open("/tmp/irlp-lockfile-parport0", 01|0100, 0664);
        if( lockfilefd < 0 ) {
            perror("Can't open lock file ""/tmp/irlp-lockfile-parport0");
            exit((*__errno_location ()));
        }
    }
    if( flock(lockfilefd, 2) < 0 ) {
        perror("Can't optain exclusive lock on ""/tmp/irlp-lockfile-parport0");
        exit((*__errno_location ()));
    }
    if( ioctl(fd, (((0U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x8b)) << 0) | ((0) << ((0 +8)+8)))) ) {
        perror("PPCLAIM");
           flock(lockfilefd, 8);
        return -1;
    }
    return 0;
}


pprelease

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

if ( fd < 0 )
{
v0 = -1;
}
else
{
v0 = ioctl(fd, 0x708CuLL);
if ( v0 )
{
perror("PPRELEASE");
v0 = -1;
}
}
flock(lockfilefd, 8);
return v0;
}

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

if ( fd < 0 )
{
v0 = -1;
}
else
{
v0 = ioctl(fd, 0x708CuLL);
if ( v0 )
{
perror("PPRELEASE");
v0 = -1;
}
}
flock(lockfilefd, 8);
return v0;
}

variable_types: ret int


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

result = (unsigned int)fd;
if ( fd < 0 )
{
fd = open("/dev/parport0", 2);
if ( fd < 0 )
{
perror("open(\"/dev/parport0\") failed.");
result = 0xFFFFFFFFLL;
}
else if ( ppclaim() < 0 )
{
perror("Trial PPCLAIM failed on /dev/parport0");
close(fd);
fd = -1;
result = 0xFFFFFFFFLL;
}
else
{
pprelease();
result = (unsigned int)fd;
}
}
return result;
}

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

result = (unsigned int)fd;
if ( fd < 0 )
{
fd = open("/dev/parport0", 2);
if ( fd < 0 )
{
perror("open(\"/dev/parport0\") failed.");
result = 0xFFFFFFFFLL;
}
else if ( func21() < 0 )
{
perror("Trial PPCLAIM failed on /dev/parport0");
close(fd);
fd = -1;
result = 0xFFFFFFFFLL;
}
else
{
func22();
result = (unsigned int)fd;
}
}
return result;
}

original_code:
int irlpdev_open() {
    if( fd >= 0 )
        return fd;
    if( (fd = open("/dev/parport0", 02)) < 0 ) {
        perror("open(\"/dev/parport0\") failed.");
        return -1;
    }
    if( ppclaim() < 0 ) {
        perror("Trial PPCLAIM failed on /dev/parport0");
        close(fd);
        fd = -1;
        return -1;
    }
    pprelease();
    return fd;
}


decompiled_code:
__int64 __fastcall read_irlpdev(__int64 a1, int a2)
{
unsigned int v2; // er12

if ( ppclaim() < 0 )
return (unsigned int)-1;
v2 = 0;
if ( a2 > 0 )
{
if ( ioctl(fd, 0x80017081uLL, a1) )
{
perror("PPRSTATUS");
pprelease();
return (unsigned int)-1;
}
v2 = 1;
}
if ( a2 > 1 )
{
if ( ioctl(fd, 0x80017085uLL, a1 + 1) )
{
perror("PPRDATA");
pprelease();
return (unsigned int)-1;
}
++v2;
}
pprelease();
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func24(__int64 a1, int a2)
{
unsigned int v2; // er12

if ( func21() < 0 )
return (unsigned int)-1;
v2 = 0;
if ( a2 > 0 )
{
if ( ioctl(fd, 0x80017081uLL, a1) )
{
perror("PPRSTATUS");
func22();
return (unsigned int)-1;
}
v2 = 1;
}
if ( a2 > 1 )
{
if ( ioctl(fd, 0x80017085uLL, a1 + 1) )
{
perror("PPRDATA");
func22();
return (unsigned int)-1;
}
++v2;
}
func22();
return v2;
}

variable_types:
buff
unsigned char *

variable_types: n int

variable_types: k int


decompiled_code:
__int64 __fastcall write_irlpdev(__int64 a1, int a2)
{
unsigned int v2; // er12

v2 = -1;
if ( ppclaim() >= 0 )
{
if ( a2 <= 0 )
{
v2 = 0;
}
else
{
v2 = 1;
if ( ioctl(fd, 0x40017086uLL, a1) )
{
perror("PPWDATA");
pprelease();
return (unsigned int)-1;
}
}
pprelease();
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func25(__int64 a1, int a2)
{
unsigned int v2; // er12

v2 = -1;
if ( func21() >= 0 )
{
if ( a2 <= 0 )
{
v2 = 0;
}
else
{
v2 = 1;
if ( ioctl(fd, 0x40017086uLL, a1) )
{
perror("PPWDATA");
func22();
return (unsigned int)-1;
}
}
func22();
}
return v2;
}

original_code:
int write_irlpdev(unsigned char *buff, int n) {
    int k;
    if( ppclaim() < 0 )
        return -1;
    k = 0;
    if( n > 0 ) {
        if( ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x86)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), buff) ) {
            perror("PPWDATA");
            pprelease();
            return -1;
        }
        k++;
    }
    pprelease();
    return k;
}


timestamp

decompiled_code:
size_t __fastcall timestamp(char *s, int a2)
{
struct tm *v2; // rax
struct timeval v4; // [rsp+0h] [rbp-28h] BYREF

if ( gettimeofday(&v4, 0LL) < 0 )
*s = 0;
v2 = localtime(&v4.tv_sec);
return strftime(s, a2, "%b %d %H:%M:%S", v2);
}

canonical_decompiled_code:
size_t __fastcall func26(char *s, int a2)
{
struct tm *v2; // rax
struct timeval v4; // [rsp+0h] [rbp-28h] BYREF

if ( gettimeofday(&v4, 0LL) < 0 )
*s = 0;
v2 = localtime(&v4.tv_sec);
return strftime(s, a2, "%b %d %H:%M:%S", v2);
}

original_code:
void timestamp(char *buf, int sz)
{
    struct timeval tv;
    if( gettimeofday(&tv, ((void *)0)) < 0 ) buf[0] = 0;
    strftime(buf, sz, "%b %d %H:%M:%S", localtime(&tv.tv_sec));
}

variable_types:
buf
char *

variable_types: sz int

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


decompiled_code:
void __fastcall open_syslog(const char *a1)
{
openlog(a1, 9, 24);
}

canonical_decompiled_code:
void __fastcall func27(const char *a1)
{
openlog(a1, 9, 24);
}

original_code:
void open_syslog(char *nam)
{
    openlog(nam, 0x08 | 0x01, (3<<3));
}

variable_types:
nam
char *


decompiled_code:
void __fastcall write_syslog(char *fmt)
{
syslog(6, fmt);
}

canonical_decompiled_code:
void __fastcall func28(char *fmt)
{
syslog(6, fmt);
}

original_code:
void write_syslog(char *str)
{
    syslog(6, str);
}

variable_types:
str
char *


decompiled_code:
void __fastcall do_log(char *fmt)
{
char v1[56]; // [rsp+0h] [rbp-38h] BYREF

if ( verbose )
{
timestamp(v1, 40);
fprintf(stdout, "[%s] %s\n", v1, fmt);
}
if ( logging )
write_syslog(fmt);
}

canonical_decompiled_code:
void __fastcall func29(char *fmt)
{
char v1[56]; // [rsp+0h] [rbp-38h] BYREF

if ( verbose )
{
func26(v1, 40);
fprintf(stdout, "[%s] %s\n", v1, fmt);
}
if ( logging )
func28(fmt);
}

original_code:
void do_log(char *str)
{
    int sztime = 40;
    char time[sztime];
    if (verbose) {
        timestamp(time, sztime);
        fprintf(stdout, "[%s] %s\n", time, str);
    }
    if (logging) {
        write_syslog(str);
    }
}

canonical_original_code:
void do_log(char *str)
{
    int sztime = 40;
    char time[-1];
    if (verbose) {
        timestamp(time, sztime);
        fprintf(stdout, "[%s] %s\n", time, str);
    }
    if (logging) {
        write_syslog(str);
    }
}

variable_types: sztime int

variable_types:
time
char [-1]


mktones

decompiled_code:
__int64 __fastcall mktones(int a1, unsigned int a2, unsigned int a3, unsigned int a4)
{
__int64 v6; // r14
__int64 result; // rax

v6 = (unsigned int)(int)(1200.0 / (double)a1);
sditbf = mkwave(a2, a3, a4, v6);
if ( sditbf < 0 )
{
fwrite("Dit waveform generation failed\n", 1uLL, 0x1FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
sdahbf = mkwave(a2, a3, a4, (unsigned int)(3 * v6));
if ( sditbf < 0 )
{
fwrite("dah waveform generation failed\n", 1uLL, 0x1FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
ssgapbf = mksilence(a3, (unsigned int)v6, &sgapbf, &nsgapbf);
if ( sditbf < 0 )
{
fwrite("short gap waveform generation failed\n", 1uLL, 0x25uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
slgapbf = mksilence(a3, (unsigned int)(3 * v6), &lgapbf, &nlgapbf);
if ( sditbf < 0 )
{
fwrite("Long gap waveform generation failed\n", 1uLL, 0x24uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned int a2, unsigned int a3, unsigned int a4)
{
__int64 v6; // r14
__int64 result; // rax

v6 = (unsigned int)(int)(1200.0 / (double)a1);
sditbf = mkwave(a2, a3, a4, v6);
if ( sditbf < 0 )
{
fwrite("Dit waveform generation failed\n", 1uLL, 0x1FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
sdahbf = mkwave(a2, a3, a4, (unsigned int)(3 * v6));
if ( sditbf < 0 )
{
fwrite("dah waveform generation failed\n", 1uLL, 0x1FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
ssgapbf = mksilence(a3, (unsigned int)v6, &sgapbf, &nsgapbf);
if ( sditbf < 0 )
{
fwrite("short gap waveform generation failed\n", 1uLL, 0x25uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
slgapbf = mksilence(a3, (unsigned int)(3 * v6), &lgapbf, &nlgapbf);
if ( sditbf < 0 )
{
fwrite("Long gap waveform generation failed\n", 1uLL, 0x24uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
}
return result;
}

original_code:
int mktones(int wpm, int freq, int rate, int ampl, int atta, int deca)
{
    int ditlen;
    ditlen = (double) 1200 / wpm;
    sditbf = mkwave(freq, rate, ampl, ditlen, atta, deca, &ditbf, &nditbf);
    if (sditbf < 0) {
        fprintf(stderr, "Dit waveform generation failed\n");
        return -1;
    }
    sdahbf = mkwave(freq, rate, ampl, ditlen * 3, atta, deca, &dahbf, &ndahbf);
    if (sditbf < 0) {
        fprintf(stderr, "dah waveform generation failed\n");
        return -1;
    }
    ssgapbf = mksilence(rate, ditlen, &sgapbf, &nsgapbf);
    if (sditbf < 0) {
        fprintf(stderr, "short gap waveform generation failed\n");
        return -1;
    }
    slgapbf = mksilence(rate, ditlen * 3, &lgapbf, &nlgapbf);
    if (sditbf < 0) {
        fprintf(stderr, "Long gap waveform generation failed\n");
        return -1;
    }
    return 0;
}

variable_types: wpm int

variable_types: freq int

variable_types: rate int

variable_types: ampl int

variable_types: atta int

variable_types: deca int

variable_types: ditlen int


decompiled_code:
int __fastcall code2snd(char *a1)
{
_BYTE *v1; // rbx
char i; // al
int result; // eax

setbuf(stdout, 0LL);
v1 = a1 + 1;
for ( i = *a1; i; i = *(v1 - 1) )
{
switch ( i )
{
case '.':
sound_write(&ditbf, &nditbf, (unsigned int)outp);
if ( verbose )
fputc(46, stderr);
break;
case '-':
sound_write(&dahbf, &ndahbf, (unsigned int)outp);
if ( verbose )
fputc(45, stderr);
break;
case 'S':
sound_write(&lgapbf, &nlgapbf, (unsigned int)outp);
if ( verbose )
fwrite(" ", 1uLL, 2uLL, stderr);
break;
}
sound_write(&sgapbf, &nsgapbf, (unsigned int)outp);
++v1;
}
result = sound_write(&lgapbf, &nlgapbf, (unsigned int)outp);
if ( verbose )
result = fputc(32, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func1(char *a1)
{
_BYTE *v1; // rbx
char i; // al
int result; // eax

setbuf(stdout, 0LL);
v1 = a1 + 1;
for ( i = *a1; i; i = *(v1 - 1) )
{
switch ( i )
{
case '.':
sound_write(&ditbf, &nditbf, (unsigned int)outp);
if ( verbose )
fputc(46, stderr);
break;
case '-':
sound_write(&dahbf, &ndahbf, (unsigned int)outp);
if ( verbose )
fputc(45, stderr);
break;
case 'S':
sound_write(&lgapbf, &nlgapbf, (unsigned int)outp);
if ( verbose )
fwrite(" ", 1uLL, 2uLL, stderr);
break;
}
sound_write(&sgapbf, &nsgapbf, (unsigned int)outp);
++v1;
}
result = sound_write(&lgapbf, &nlgapbf, (unsigned int)outp);
if ( verbose )
result = fputc(32, stderr);
return result;
}

original_code:
void code2snd(char *cd)
{
    char ch, w;
    setbuf(stdout, ((void *)0));
    while ((ch = *cd++) != '\0') {
        if (ch == '.') {
            w = sound_write(&ditbf, &nditbf, outp);
            if (verbose) fprintf(stderr, ".");
        }
        if (ch == '-') {
            w = sound_write(&dahbf, &ndahbf, outp);
            if (verbose) fprintf(stderr, "-");
        }
        if (ch == 'S') {
            w = sound_write(&lgapbf, &nlgapbf, outp);
            if (verbose) fprintf(stderr, "  ");
        }
        w = sound_write(&sgapbf, &nsgapbf, outp);
    }
    w = sound_write(&lgapbf, &nlgapbf, outp);
    if (verbose) fprintf(stderr, " ");
}

canonical_original_code:
void code2snd(char *cd)
{
    char ch; char w;
    setbuf(stdout, ((void *)0));
    while ((ch = *cd++) != '\0') {
        if (ch == '.') {
            w = sound_write(&ditbf, &nditbf, outp);
            if (verbose) fprintf(stderr, ".");
        }
        if (ch == '-') {
            w = sound_write(&dahbf, &ndahbf, outp);
            if (verbose) fprintf(stderr, "-");
        }
        if (ch == 'S') {
            w = sound_write(&lgapbf, &nlgapbf, outp);
            if (verbose) fprintf(stderr, "  ");
        }
        w = sound_write(&sgapbf, &nsgapbf, outp);
    }
    w = sound_write(&lgapbf, &nlgapbf, outp);
    if (verbose) fprintf(stderr, " ");
}

variable_types:
cd
char *

variable_types: ch char

variable_types: w char


decompiled_code:
int __fastcall text2code(char *a1)
{
char v1; // bp
int v2; // ebx
char *v3; // r12
const unsigned __int16 **v4; // r13
int v5; // ebx

v1 = *a1;
v2 = *a1;
if ( *a1 )
{
v3 = a1 + 1;
v4 = _ctype_b_loc();
do
{
if ( ((*v4)[v1] & 0x400) != 0 )
v2 = toupper(v2);
if ( (unsigned int)(v2 - 65) > 0x19 )
{
if ( (unsigned int)(v2 - 48) > 9 )
{
switch ( v2 )
{
case '?':
v5 = 36;
break;
case '/':
v5 = 37;
break;
case ' ':
v5 = 38;
break;
default:
goto LABEL_6;
}
}
else
{
v5 = v2 - 22;
}
}
else
{
v5 = v2 - 65;
}
code2snd(morse[v5]);
LABEL_6:
v1 = *v3++;
v2 = v1;
}
while ( v1 );
}
return code2snd("S");
}

canonical_decompiled_code:
int __fastcall func2(char *a1)
{
char v1; // bp
int v2; // ebx
char *v3; // r12
const unsigned __int16 **v4; // r13
int v5; // ebx

v1 = *a1;
v2 = *a1;
if ( *a1 )
{
v3 = a1 + 1;
v4 = _ctype_b_loc();
do
{
if ( ((*v4)[v1] & 0x400) != 0 )
v2 = toupper(v2);
if ( (unsigned int)(v2 - 65) > 0x19 )
{
if ( (unsigned int)(v2 - 48) > 9 )
{
switch ( v2 )
{
case '?':
v5 = 36;
break;
case '/':
v5 = 37;
break;
case ' ':
v5 = 38;
break;
default:
goto LABEL_6;
}
}
else
{
v5 = v2 - 22;
}
}
else
{
v5 = v2 - 65;
}
func1(morse[v5]);
LABEL_6:
v1 = *v3++;
v2 = v1;
}
while ( v1 );
}
return func1("S");
}

original_code:
void text2code(char *tx)
{
    int ch;
    char *cd;
    while ((ch = *tx++) != '\0') {
        if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISalpha))
            ch = toupper(ch);
        if ((ch >= 'A') && (ch <= 'Z'))
            ch = ch - 65;
        else if ((ch >= '0') && (ch <= '9'))
            ch = ch - 22;
        else if (ch == '?')
            ch = 36;
        else if (ch == '/')
            ch = 37;
        else if (ch == ' ')
            ch = 38;
        else
            continue;
        cd = morse[ch];
        code2snd(cd);
    }
    cd = morse[38];
    code2snd(cd);
}

canonical_original_code:
void text2code(char *tx)
{
    int ch;
    char *cd;
    while ((ch = *tx++) != '\0') {
        if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISalpha))
            ch = toupper(ch);
        if ((ch >= 'A') && (ch <= 'Z'))
            ch = ch - 65;
        else if ((ch >= '0') && (ch <= '9'))
            ch = ch - 22;
        else if (ch == '?')
            ch = 36;
        else if (ch == '/')
            ch = 37;
        else if (ch == ' ')
            ch = 38;
        else
            continue;
        cd = morse[ch];
        code2snd(cd);
    }
    cd = morse[38];
    code2snd(cd);
}

variable_types:
tx
char *

variable_types: ch int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v5; // er13
int v6; // er14
int v7; // er15
const char *v8; // rbx
int v9; // er14
const char **v10; // rbx
unsigned int v12; // [rsp+4h] [rbp-44h]

if ( argc <= 1 )
{
LABEL_40:
fwrite("No text given\n", 1uLL, 0xEuLL, stderr);
v9 = -1;
}
else
{
v3 = argc;
v12 = 100;
v5 = 22500;
v6 = 660;
v7 = 10;
while ( 1 )
{
v8 = argv[1];
if ( *v8 != 45 )
break;
if ( !strcmp(argv[1], "-w") )
v7 = atoi(argv[2]);
if ( !strcmp(v8, "-o") )
{
if ( !strcmp(argv[2], "alsa") )
outp = 1;
else
outp = 2 * (strcmp(argv[2], "dsp") == 0);
}
if ( !strcmp(v8, "-f") )
v6 = atoi(argv[2]);
if ( !strcmp(v8, "-r") )
v5 = atoi(argv[2]);
if ( !strcmp(v8, "-a") )
v12 = atoi(argv[2]);
if ( !strcmp(v8, "-da") )
atoi(argv[2]);
if ( !strcmp(v8, "-dd") )
atoi(argv[2]);
if ( !strcmp(v8, "-h") )
{
fprintf(
stderr,
"Usage: cw [OPTION] [TEXT ...]\n"
"Play morse code from command line.\n"
" -o output method [alsa|dsp|stdout]\n"
" -w word per minute\n"
" -f frequency in hertz\n"
" -r sample rate in samples per second\n"
" -a amplitude in %\n"
" -da attack in milliseconds\n"
" -dd decay in milliseconds\n"
" -v clutter the screen\n"
" -h display this help and exit\n"
"Copyright (c) 2011, Adi Linden <adi@adis.ca>\n",
10LL);
return -1;
}
if ( !strcmp(v8, "-v") )
{
verbose = 1;
++v3;
--argv;
}
v3 -= 2;
argv += 2;
if ( v3 <= 1 )
goto LABEL_40;
}
if ( (unsigned int)(v7 - 2) > 0x62 )
{
fprintf(stderr, "Support %d to %d word per minute range\n", 2LL, 100LL);
v9 = -1;
}
else if ( outp <= 0 )
{
fwrite("Output format needs to be alsa, dsp, or stdout\n", 1uLL, 0x2FuLL, stderr);
v9 = -1;
}
else if ( 2 * v6 > v5 )
{
fwrite("Sample rate must be more then twice the frequency\n", 1uLL, 0x32uLL, stderr);
v9 = -1;
}
else if ( (unsigned int)(v6 - 10) > 0x57DA )
{
fprintf(stderr, "Support %d to %d Hz frequency range\n", 10LL, 22500LL);
v9 = -1;
}
else if ( (unsigned int)(v5 - 8000) > 0x8D04 )
{
fprintf(stderr, "Support %d to %d samples per second\n", 8000LL, 44100LL);
v9 = -1;
}
else if ( v12 > 0x64 )
{
fprintf(stderr, "Support %d to %d amplitude\n", 0LL, 100LL);
v9 = -1;
}
else
{
v9 = mktones(v7, v6, v5, v12);
if ( v9 )
{
v9 = -1;
}
else
{
sound_open((unsigned int)outp);
sound_setup((unsigned int)v5, (unsigned int)outp);
if ( verbose )
fwrite("Morse code: ", 1uLL, 0xCuLL, stderr);
v10 = &argv[(unsigned int)(v3 - 2) + 1];
do
{
text2code((char *)argv[1]);
++argv;
}
while ( argv != v10 );
if ( verbose )
fputc(10, stderr);
free(ditbf);
free(dahbf);
free(sgapbf);
free(lgapbf);
sound_close((unsigned int)outp);
}
}
}
return v9;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v5; // er13
int v6; // er14
int v7; // er15
const char *v8; // rbx
int v9; // er14
const char **v10; // rbx
unsigned int v12; // [rsp+4h] [rbp-44h]

if ( argc <= 1 )
{
LABEL_40:
fwrite("No text given\n", 1uLL, 0xEuLL, stderr);
v9 = -1;
}
else
{
v3 = argc;
v12 = 100;
v5 = 22500;
v6 = 660;
v7 = 10;
while ( 1 )
{
v8 = argv[1];
if ( *v8 != 45 )
break;
if ( !strcmp(argv[1], "-w") )
v7 = atoi(argv[2]);
if ( !strcmp(v8, "-o") )
{
if ( !strcmp(argv[2], "alsa") )
outp = 1;
else
outp = 2 * (strcmp(argv[2], "dsp") == 0);
}
if ( !strcmp(v8, "-f") )
v6 = atoi(argv[2]);
if ( !strcmp(v8, "-r") )
v5 = atoi(argv[2]);
if ( !strcmp(v8, "-a") )
v12 = atoi(argv[2]);
if ( !strcmp(v8, "-da") )
atoi(argv[2]);
if ( !strcmp(v8, "-dd") )
atoi(argv[2]);
if ( !strcmp(v8, "-h") )
{
fprintf(
stderr,
"Usage: cw [OPTION] [TEXT ...]\n"
"Play morse code from command line.\n"
" -o output method [alsa|dsp|stdout]\n"
" -w word per minute\n"
" -f frequency in hertz\n"
" -r sample rate in samples per second\n"
" -a amplitude in %\n"
" -da attack in milliseconds\n"
" -dd decay in milliseconds\n"
" -v clutter the screen\n"
" -h display this help and exit\n"
"Copyright (c) 2011, Adi Linden <adi@adis.ca>\n",
10LL);
return -1;
}
if ( !strcmp(v8, "-v") )
{
verbose = 1;
++v3;
--argv;
}
v3 -= 2;
argv += 2;
if ( v3 <= 1 )
goto LABEL_40;
}
if ( (unsigned int)(v7 - 2) > 0x62 )
{
fprintf(stderr, "Support %d to %d word per minute range\n", 2LL, 100LL);
v9 = -1;
}
else if ( outp <= 0 )
{
fwrite("Output format needs to be alsa, dsp, or stdout\n", 1uLL, 0x2FuLL, stderr);
v9 = -1;
}
else if ( 2 * v6 > v5 )
{
fwrite("Sample rate must be more then twice the frequency\n", 1uLL, 0x32uLL, stderr);
v9 = -1;
}
else if ( (unsigned int)(v6 - 10) > 0x57DA )
{
fprintf(stderr, "Support %d to %d Hz frequency range\n", 10LL, 22500LL);
v9 = -1;
}
else if ( (unsigned int)(v5 - 8000) > 0x8D04 )
{
fprintf(stderr, "Support %d to %d samples per second\n", 8000LL, 44100LL);
v9 = -1;
}
else if ( v12 > 0x64 )
{
fprintf(stderr, "Support %d to %d amplitude\n", 0LL, 100LL);
v9 = -1;
}
else
{
v9 = func0(v7, v6, v5, v12);
if ( v9 )
{
v9 = -1;
}
else
{
sound_open((unsigned int)outp);
sound_setup((unsigned int)v5, (unsigned int)outp);
if ( verbose )
fwrite("Morse code: ", 1uLL, 0xCuLL, stderr);
v10 = &argv[(unsigned int)(v3 - 2) + 1];
do
{
func2((char *)argv[1]);
++argv;
}
while ( argv != v10 );
if ( verbose )
fputc(10, stderr);
free(ditbf);
free(dahbf);
free(sgapbf);
free(lgapbf);
sound_close((unsigned int)outp);
}
}
}
return v9;
}

original_code:
int main(int argc, char *argv[])
{
    int wpm = 10;
    int freq = 660;
    int rate = 22500;
    int ampl = 100;
    int atta = 6;
    int deca = 12;
    while (argc > 1 && *argv[1] == '-') {
        if (!strcmp(argv[1], "-w")) {
            wpm = atoi(argv[2]);
        }
        if (!strcmp(argv[1], "-o")) {
            if (!strcmp(argv[2], "alsa"))
                outp = 1;
            else if (!strcmp(argv[2], "dsp"))
                outp = 2;
            else if (!strcmp(argv[2], "alsa"))
                outp = 3;
            else
                outp = 0;
        }
        if (!strcmp(argv[1], "-f")) {
            freq = atoi(argv[2]);
        }
        if (!strcmp(argv[1], "-r")) {
            rate = atoi(argv[2]);
        }
        if (!strcmp(argv[1], "-a")) {
            ampl = atoi(argv[2]);
        }
        if (!strcmp(argv[1], "-da")) {
            atta = atoi(argv[2]);
        }
        if (!strcmp(argv[1], "-dd")) {
            deca = atoi(argv[2]);
        }
        if (!strcmp(argv[1], "-h")) {
            fprintf(stderr, usage, 10);
            return -1;
        }
        if (!strcmp(argv[1], "-v")) {
            verbose = 1;
            ++argc;
            --argv;
        }
        argc -= 2;
        argv += 2;
    }
    if (!(argc > 1)) {
        fprintf(stderr, "No text given\n");
        return -1;
    }
    if (wpm < 2 || wpm > 100) {
        fprintf(stderr, "Support %d to %d word per minute range\n",
                2, 100);
        return -1;
    }
    if (outp < 1) {
        fprintf(stderr, "Output format needs to be alsa, dsp, or stdout\n");
        return -1;
    }
    if (rate < 2 * freq) {
        fprintf(stderr, "Sample rate must be more then twice the frequency\n");
        return -1;
    }
    if (freq < 10 || freq > 22500) {
        fprintf(stderr, "Support %d to %d Hz frequency range\n",
                10, 22500);
        return -1;
    }
    if (rate < 8000 || rate > 44100) {
        fprintf(stderr, "Support %d to %d samples per second\n",
                8000, 44100);
        return -1;
    }
    if (ampl < 0 || ampl > 100) {
        fprintf(stderr, "Support %d to %d amplitude\n", 0, 100);
        return -1;
    }
    if (mktones(wpm, freq, rate, ampl, atta, deca)) {
        return -1;
    }
    sound_open(outp);
    sound_setup(rate, outp);
    if (verbose) fprintf(stderr, "Morse code: ");
    while (argc > 1) {
        text2code(argv[1]);
        argc--;
        argv++;
    }
    if (verbose) fprintf(stderr, "\n");
    free(ditbf);
    free(dahbf);
    free(sgapbf);
    free(lgapbf);
    sound_close(outp);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
size_t __fastcall sound_open(int a1)
{
switch ( a1 )
{
case 2:
return dsp_open("/dev/dsp");
case 3:
return stdout_open("/dev/dsp");
case 1:
return alsa_open("default");
}
return fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func0(int a1)
{
switch ( a1 )
{
case 2:
return dsp_open("/dev/dsp");
case 3:
return stdout_open("/dev/dsp");
case 1:
return alsa_open("default");
}
return fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
}

original_code:
void sound_open(int outp)
{
    switch (outp) {
        case 1:
            alsa_open("default");
            break;
        case 2:
            dsp_open("/dev/dsp");
            break;
        case 3:
            stdout_open("/dev/dsp");
            break;
        default:
            fprintf(stderr, "Unknown output method\n");
    }
}

variable_types: outp int


decompiled_code:
size_t __fastcall sound_setup(__int64 a1, int a2)
{
switch ( a2 )
{
case 2:
return dsp_setup();
case 3:
return stdout_setup();
case 1:
return alsa_setup();
}
return fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func1(__int64 a1, int a2)
{
switch ( a2 )
{
case 2:
return dsp_setup();
case 3:
return stdout_setup();
case 1:
return alsa_setup();
}
return fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
}

original_code:
void sound_setup(int rate, int outp)
{
    switch (outp) {
        case 1:
            alsa_setup(rate);
            break;
        case 2:
            dsp_setup(rate);
            break;
        case 3:
            stdout_setup(rate);
            break;
        default:
            fprintf(stderr, "Unknown output method\n");
    }
}


decompiled_code:
__int64 __fastcall sound_write(__int64 a1, __int64 a2, int a3)
{
switch ( a3 )
{
case 2:
return dsp_write();
case 3:
return stdout_write();
case 1:
return alsa_write();
}
fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, int a3)
{
switch ( a3 )
{
case 2:
return dsp_write();
case 3:
return stdout_write();
case 1:
return alsa_write();
}
fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int sound_write(int16_t **bf, int *nbf, int outp)
{
    switch (outp) {
        case 1:
            return alsa_write(bf, nbf);
            break;
        case 2:
            return dsp_write(bf, nbf);
            break;
        case 3:
            return stdout_write(bf, nbf);
            break;
        default:
            fprintf(stderr, "Unknown output method\n");
    }
    return -1;
}

canonical_original_code:
int sound_write(short **bf, int *nbf, int outp)
{
    switch (outp) {
        case 1:
            return alsa_write(bf, nbf);
            break;
        case 2:
            return dsp_write(bf, nbf);
            break;
        case 3:
            return stdout_write(bf, nbf);
            break;
        default:
            fprintf(stderr, "Unknown output method\n");
    }
    return -1;
}

variable_types:
bf
short **

variable_types:
nbf
int *


decompiled_code:
size_t __fastcall sound_close(int a1)
{
switch ( a1 )
{
case 2:
return dsp_close();
case 3:
return stdout_close();
case 1:
return alsa_close();
}
return fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func3(int a1)
{
switch ( a1 )
{
case 2:
return dsp_close();
case 3:
return stdout_close();
case 1:
return alsa_close();
}
return fwrite("Unknown output method\n", 1uLL, 0x16uLL, stderr);
}

original_code:
void sound_close(int outp)
{
    switch (outp) {
        case 1:
            alsa_close();
            break;
        case 2:
            dsp_close();
            break;
        case 3:
            stdout_close();
            break;
        default:
            fprintf(stderr, "Unknown output method\n");
    }
}


decompiled_code:
__pid_t __fastcall fork_script(__pid_t *a1, const char *a2)
{
__pid_t result; // eax

result = fork();
*a1 = result;
if ( !result )
{
usleep(0x186A0u);
system(a2);
usleep(0x186A0u);
exit(0);
}
if ( result < 0 )
exit(-1);
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func0(__pid_t *a1, const char *a2)
{
__pid_t result; // eax

result = fork();
*a1 = result;
if ( !result )
{
usleep(0x186A0u);
system(a2);
usleep(0x186A0u);
exit(0);
}
if ( result < 0 )
exit(-1);
return result;
}

original_code:
void fork_script(pid_t *pid, const char *script)
{
    *pid = fork();
    if (*pid == 0) {
        usleep(100 * 1000);
        system(script);
        usleep(100 * 1000);
        exit(0);
    }
    if (*pid < 0) {
        exit(-1);
    }
}

canonical_original_code:
void fork_script(int *pid, char *script)
{
    *pid = fork();
    if (*pid == 0) {
        usleep(100 * 1000);
        system(script);
        usleep(100 * 1000);
        exit(0);
    }
    if (*pid < 0) {
        exit(-1);
    }
}

variable_types:
pid
int *

variable_types:
script
char *


decompiled_code:
__int64 __fastcall do_ct(__pid_t *a1)
{
char v2[40]; // [rsp+0h] [rbp-28h] BYREF

if ( *a1 )
return do_log("Failed: courtesy");
fork_script(a1, "courtesy");
sprintf(v2, "Script: [%d] courtesy", (unsigned int)*a1);
return do_log(v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__pid_t *a1)
{
char v2[40]; // [rsp+0h] [rbp-28h] BYREF

if ( *a1 )
return do_log("Failed: courtesy");
func0(a1, "courtesy");
sprintf(v2, "Script: [%d] courtesy", (unsigned int)*a1);
return do_log(v2);
}

original_code:
void do_ct(pid_t *pid)
{
    char m[30];
    if (!*pid) {
        fork_script(pid, "courtesy");
        sprintf(m, "Script: [%d] " "courtesy", *pid);
        do_log(m);
    }
    else {
        do_log("Failed: " "courtesy");
    }
}

canonical_original_code:
void do_ct(int *pid)
{
    char m[30];
    if (!*pid) {
        fork_script(pid, "courtesy");
        sprintf(m, "Script: [%d] " "courtesy", *pid);
        do_log(m);
    }
    else {
        do_log("Failed: " "courtesy");
    }
}

variable_types:
m
char [30]


decompiled_code:
__int64 __fastcall do_id(__pid_t *a1)
{
char v2[40]; // [rsp+0h] [rbp-28h] BYREF

if ( *a1 )
return do_log("Failed: ider");
fork_script(a1, "ider");
sprintf(v2, "Script: [%d] ider", (unsigned int)*a1);
return do_log(v2);
}

canonical_decompiled_code:
__int64 __fastcall func2(__pid_t *a1)
{
char v2[40]; // [rsp+0h] [rbp-28h] BYREF

if ( *a1 )
return do_log("Failed: ider");
func0(a1, "ider");
sprintf(v2, "Script: [%d] ider", (unsigned int)*a1);
return do_log(v2);
}

original_code:
void do_id(pid_t *pid)
{
    char m[30];
    if (!*pid) {
        fork_script(pid, "ider");
        sprintf(m, "Script: [%d] " "ider", *pid);
        do_log(m);
    }
    else {
        do_log("Failed: " "ider");
    }
}

canonical_original_code:
void do_id(int *pid)
{
    char m[30];
    if (!*pid) {
        fork_script(pid, "ider");
        sprintf(m, "Script: [%d] " "ider", *pid);
        do_log(m);
    }
    else {
        do_log("Failed: " "ider");
    }
}


dnow

decompiled_code:
double dnow()
{
double result; // xmm0_8
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = 0.0;
if ( gettimeofday(&v1, 0LL) >= 0 )
result = ((double)SLODWORD(v1.tv_usec) * 0.000001 + (double)SLODWORD(v1.tv_sec)) * 1000.0;
return result;
}

canonical_decompiled_code:
double func4()
{
double result; // xmm0_8
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

result = 0.0;
if ( gettimeofday(&v1, 0LL) >= 0 )
result = ((double)SLODWORD(v1.tv_usec) * 0.000001 + (double)SLODWORD(v1.tv_sec)) * 1000.0;
return result;
}

original_code:
double dnow()
{
    struct timeval tv;
    if( gettimeofday(&tv, ((void *)0)) < 0 ) return (0);
    else return(1000*((double)tv.tv_sec + 1.e-6 * (double)tv.tv_usec));
}

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

return_type: double


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r8
char *v4; // rax
bool v5; // cl
bool v6; // cf
bool v7; // zf
const char *v8; // rdi
__int64 v9; // rcx
int v10; // er13
int v11; // er12
int v12; // er15
int v13; // er14
int v14; // ebp
char v15; // bl
__pid_t v16; // eax
bool v17; // dl
int v18; // eax
bool v19; // dl
int v20; // eax
unsigned __int8 v21; // bp
int v23; // [rsp+0h] [rbp-A8h]
int v24; // [rsp+4h] [rbp-A4h]
double v25; // [rsp+8h] [rbp-A0h]
int v26; // [rsp+10h] [rbp-98h]
int v27; // [rsp+14h] [rbp-94h]
double v28; // [rsp+18h] [rbp-90h]
double v29; // [rsp+20h] [rbp-88h]
int v30; // [rsp+28h] [rbp-80h]
bool v31; // [rsp+2Dh] [rbp-7Bh]
char v32; // [rsp+2Eh] [rbp-7Ah]
bool v33; // [rsp+2Fh] [rbp-79h]
int v34; // [rsp+30h] [rbp-78h]
int v35; // [rsp+34h] [rbp-74h]
double v36; // [rsp+38h] [rbp-70h]
double v37; // [rsp+40h] [rbp-68h]
double v38; // [rsp+48h] [rbp-60h]
struct timespec requested_time; // [rsp+50h] [rbp-58h] BYREF
__pid_t v40; // [rsp+64h] [rbp-44h] BYREF
__pid_t v41; // [rsp+68h] [rbp-40h] BYREF
char v42[58]; // [rsp+6Eh] [rbp-3Ah] BYREF

v41 = 0;
v40 = 0;
if ( argc <= 1 )
{
LABEL_12:
if ( logging )
open_syslog("repeater", argv, envp);
do_log("Starting: repeater, version 20130112");
if ( (int)irlpdev_open("Starting: repeater, version 20130112", argv) < 0 )
{
fwrite("Can't access parallel port", 1uLL, 0x1AuLL, stderr);
exit(-1);
}
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 5000000LL;
v10 = unkey(0LL);
v30 = mute(0LL);
v37 = 0.0;
v36 = 0.0;
v28 = 0.0;
v29 = 0.0;
v25 = 0.0;
v38 = 0.0;
v34 = 0;
v26 = 0;
v11 = 0;
v27 = 0;
v23 = 1;
v24 = 1;
v12 = 0;
v35 = 0;
v13 = 0;
while ( 1 )
{
if ( (unsigned int)read_irlpdev(v42, 2LL) != 2 )
fwrite("Can't read parallel port", 1uLL, 0x18uLL, stderr);
v21 = v42[0];
v15 = (unsigned __int8)v42[0] >> 7;
v32 = v42[1] & 2;
if ( v10 )
{
if ( !v26 )
break;
}
v31 = v10 == 0;
v33 = v10 == 0 && v26 != 0;
if ( v33 )
{
if ( dnow() - v37 > 300000.0 )
v26 = fanoff(0LL);
v31 = v33;
}
else if ( v10 )
{
goto LABEL_18;
}
LABEL_106:
if ( v15 )
{
if ( (unsigned __int8)(((v21 >> 3) & 0xF) - 1) > 0x10u )
{
if ( v30 && dnow() - v38 > 1000.0 )
v30 = unmute(0LL);
}
else
{
if ( !v30 )
v30 = mute(0LL);
v38 = dnow();
}
if ( v31 )
{
v10 = keyup(0LL);
v36 = dnow();
}
v25 = dnow();
v29 = dnow();
v23 = 0;
v24 = 0;
}
else if ( !v30 )
{
v30 = mute(0LL);
}
if ( !v32 )
{
if ( !v11 )
{
if ( !v15 )
goto LABEL_34;
goto LABEL_33;
}
LABEL_28:
if ( !v10 )
{
v10 = keyup(0LL);
v36 = dnow();
}
goto LABEL_32;
}
if ( !v10 )
{
v14 = keyup(0LL);
v36 = dnow();
v25 = dnow();
v29 = dnow();
if ( !v11 )
{
v10 = v14;
LABEL_117:
v15 |= v32;
v34 = 1;
v23 = 0;
goto LABEL_33;
}
v34 = v11;
v23 = 0;
v10 = v14;
goto LABEL_28;
}
v25 = dnow();
v29 = dnow();
if ( !v11 )
goto LABEL_117;
v34 = v11;
v23 = 0;
LABEL_32:
v25 = dnow();
v15 |= v32;
LABEL_33:
if ( !v27 && dnow() - v36 > 10.0 )
{
do_log("Shortkey exceeded");
v27 = 1;
}
LABEL_34:
if ( !v12 && !v15 )
{
if ( !(v41 | v24) && v34 )
{
if ( dnow() - v29 <= 300.0 )
goto LABEL_36;
LABEL_65:
do_ct(&v41);
v11 = 1;
v35 = 1;
goto LABEL_36;
}
if ( !v24 && !(v41 | v34) && dnow() - v29 > 1000.0 )
goto LABEL_65;
}
LABEL_36:
check_script(&v41);
v16 = v41;
if ( !v41 && v35 )
{
v17 = v11 != 0;
if ( v11 )
v11 = 0;
if ( !v17 )
v16 = v35;
v35 = v16;
v18 = 1;
if ( !v17 )
v18 = v24;
v24 = v18;
}
if ( v13 | v23 )
{
if ( v13 == 3 && !v23 )
{
do_log("ID: delayed");
LABEL_49:
if ( !v40 )
{
if ( !v15 && v10 && v24 && dnow() - v28 > 1200000.0 )
{
do_id(&v40);
v28 = dnow();
v11 = 1;
v12 = 1;
}
if ( dnow() - v28 > 1680000.0 )
{
do_id(&v40);
v28 = dnow();
v11 = 1;
v12 = 1;
}
}
v13 = 2;
goto LABEL_81;
}
if ( !v40 && v13 == 1 )
{
LABEL_74:
if ( !v15 && v10 && v24 )
{
do_id(&v40);
v28 = dnow();
v11 = 1;
v12 = 1;
}
if ( dnow() - v28 > 480000.0 )
{
do_id(&v40);
v28 = dnow();
v11 = 1;
v12 = 1;
}
v13 = 1;
goto LABEL_81;
}
if ( v13 == 2 )
goto LABEL_49;
}
else
{
v28 = dnow();
do_log("ID: immediate");
v13 = 1;
if ( !v40 )
goto LABEL_74;
}
if ( v13 == 3 && dnow() - v28 > 1680000.0 )
{
do_log("ID: reset");
v13 = 0;
}
LABEL_81:
check_script(&v40);
if ( !v40 && v12 )
{
v19 = v11 != 0;
if ( v11 )
v11 = 0;
if ( v19 )
v12 = 0;
v20 = 1;
if ( !v19 )
v20 = v23;
v23 = v20;
if ( v19 )
v13 = 3;
}
if ( !v15 )
{
if ( !(v11 | v27) && v10 )
{
v10 = unkey(0LL);
v34 = 0;
v27 = 0;
v23 = 1;
v24 = 1;
}
if ( !v11 && v10 && dnow() - v25 > 3000.0 )
{
v10 = unkey(0LL);
v34 = 0;
v27 = 0;
}
}
fflush(stdout);
fflush(stderr);
nanosleep(&requested_time, 0LL);
}
v26 = fanon(0LL);
v31 = 0;
LABEL_18:
v37 = dnow();
goto LABEL_106;
}
envp = argv;
v3 = &argv[argc - 1];
while ( 1 )
{
v4 = (char *)envp[1];
if ( !strcmp(v4, "-h") )
break;
v5 = strcmp(envp[1], "-v") != 0;
v6 = 0;
v7 = !v5;
if ( !v5 )
verbose = 1;
v8 = "-l";
v9 = 3LL;
argv = (const char **)v4;
do
{
if ( !v9 )
break;
v6 = *(_BYTE *)argv < *v8;
v7 = *(_BYTE *)argv == *v8;
argv = (const char **)((char *)argv + 1);
++v8;
--v9;
}
while ( v7 );
if ( (!v6 && !v7) == v6 )
logging = 1;
if ( v3 == ++envp )
goto LABEL_12;
}
fwrite(
"Usage: repeater [OPTION]\n"
"The repeater controller.\n"
" -l log to syslog\n"
" -v clutter the screen\n"
" -h display this help and exit\n"
"Copyright (c) 2013, Adi Linden <adi@adis.ca>\n",
1uLL,
0xBCuLL,
stderr);
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r8
char *v4; // rax
bool v5; // cl
bool v6; // cf
bool v7; // zf
const char *v8; // rdi
__int64 v9; // rcx
int v10; // er13
int v11; // er12
int v12; // er15
int v13; // er14
int v14; // ebp
char v15; // bl
__pid_t v16; // eax
bool v17; // dl
int v18; // eax
bool v19; // dl
int v20; // eax
unsigned __int8 v21; // bp
int v23; // [rsp+0h] [rbp-A8h]
int v24; // [rsp+4h] [rbp-A4h]
double v25; // [rsp+8h] [rbp-A0h]
int v26; // [rsp+10h] [rbp-98h]
int v27; // [rsp+14h] [rbp-94h]
double v28; // [rsp+18h] [rbp-90h]
double v29; // [rsp+20h] [rbp-88h]
int v30; // [rsp+28h] [rbp-80h]
bool v31; // [rsp+2Dh] [rbp-7Bh]
char v32; // [rsp+2Eh] [rbp-7Ah]
bool v33; // [rsp+2Fh] [rbp-79h]
int v34; // [rsp+30h] [rbp-78h]
int v35; // [rsp+34h] [rbp-74h]
double v36; // [rsp+38h] [rbp-70h]
double v37; // [rsp+40h] [rbp-68h]
double v38; // [rsp+48h] [rbp-60h]
struct timespec requested_time; // [rsp+50h] [rbp-58h] BYREF
__pid_t v40; // [rsp+64h] [rbp-44h] BYREF
__pid_t v41; // [rsp+68h] [rbp-40h] BYREF
char v42[58]; // [rsp+6Eh] [rbp-3Ah] BYREF

v41 = 0;
v40 = 0;
if ( argc <= 1 )
{
LABEL_12:
if ( logging )
open_syslog("repeater", argv, envp);
do_log("Starting: repeater, version 20130112");
if ( (int)irlpdev_open("Starting: repeater, version 20130112", argv) < 0 )
{
fwrite("Can't access parallel port", 1uLL, 0x1AuLL, stderr);
exit(-1);
}
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 5000000LL;
v10 = unkey(0LL);
v30 = mute(0LL);
v37 = 0.0;
v36 = 0.0;
v28 = 0.0;
v29 = 0.0;
v25 = 0.0;
v38 = 0.0;
v34 = 0;
v26 = 0;
v11 = 0;
v27 = 0;
v23 = 1;
v24 = 1;
v12 = 0;
v35 = 0;
v13 = 0;
while ( 1 )
{
if ( (unsigned int)read_irlpdev(v42, 2LL) != 2 )
fwrite("Can't read parallel port", 1uLL, 0x18uLL, stderr);
v21 = v42[0];
v15 = (unsigned __int8)v42[0] >> 7;
v32 = v42[1] & 2;
if ( v10 )
{
if ( !v26 )
break;
}
v31 = v10 == 0;
v33 = v10 == 0 && v26 != 0;
if ( v33 )
{
if ( func4() - v37 > 300000.0 )
v26 = fanoff(0LL);
v31 = v33;
}
else if ( v10 )
{
goto LABEL_18;
}
LABEL_106:
if ( v15 )
{
if ( (unsigned __int8)(((v21 >> 3) & 0xF) - 1) > 0x10u )
{
if ( v30 && func4() - v38 > 1000.0 )
v30 = unmute(0LL);
}
else
{
if ( !v30 )
v30 = mute(0LL);
v38 = func4();
}
if ( v31 )
{
v10 = keyup(0LL);
v36 = func4();
}
v25 = func4();
v29 = func4();
v23 = 0;
v24 = 0;
}
else if ( !v30 )
{
v30 = mute(0LL);
}
if ( !v32 )
{
if ( !v11 )
{
if ( !v15 )
goto LABEL_34;
goto LABEL_33;
}
LABEL_28:
if ( !v10 )
{
v10 = keyup(0LL);
v36 = func4();
}
goto LABEL_32;
}
if ( !v10 )
{
v14 = keyup(0LL);
v36 = func4();
v25 = func4();
v29 = func4();
if ( !v11 )
{
v10 = v14;
LABEL_117:
v15 |= v32;
v34 = 1;
v23 = 0;
goto LABEL_33;
}
v34 = v11;
v23 = 0;
v10 = v14;
goto LABEL_28;
}
v25 = func4();
v29 = func4();
if ( !v11 )
goto LABEL_117;
v34 = v11;
v23 = 0;
LABEL_32:
v25 = func4();
v15 |= v32;
LABEL_33:
if ( !v27 && func4() - v36 > 10.0 )
{
do_log("Shortkey exceeded");
v27 = 1;
}
LABEL_34:
if ( !v12 && !v15 )
{
if ( !(v41 | v24) && v34 )
{
if ( func4() - v29 <= 300.0 )
goto LABEL_36;
LABEL_65:
func1(&v41);
v11 = 1;
v35 = 1;
goto LABEL_36;
}
if ( !v24 && !(v41 | v34) && func4() - v29 > 1000.0 )
goto LABEL_65;
}
LABEL_36:
func3(&v41);
v16 = v41;
if ( !v41 && v35 )
{
v17 = v11 != 0;
if ( v11 )
v11 = 0;
if ( !v17 )
v16 = v35;
v35 = v16;
v18 = 1;
if ( !v17 )
v18 = v24;
v24 = v18;
}
if ( v13 | v23 )
{
if ( v13 == 3 && !v23 )
{
do_log("ID: delayed");
LABEL_49:
if ( !v40 )
{
if ( !v15 && v10 && v24 && func4() - v28 > 1200000.0 )
{
func2(&v40);
v28 = func4();
v11 = 1;
v12 = 1;
}
if ( func4() - v28 > 1680000.0 )
{
func2(&v40);
v28 = func4();
v11 = 1;
v12 = 1;
}
}
v13 = 2;
goto LABEL_81;
}
if ( !v40 && v13 == 1 )
{
LABEL_74:
if ( !v15 && v10 && v24 )
{
func2(&v40);
v28 = func4();
v11 = 1;
v12 = 1;
}
if ( func4() - v28 > 480000.0 )
{
func2(&v40);
v28 = func4();
v11 = 1;
v12 = 1;
}
v13 = 1;
goto LABEL_81;
}
if ( v13 == 2 )
goto LABEL_49;
}
else
{
v28 = func4();
do_log("ID: immediate");
v13 = 1;
if ( !v40 )
goto LABEL_74;
}
if ( v13 == 3 && func4() - v28 > 1680000.0 )
{
do_log("ID: reset");
v13 = 0;
}
LABEL_81:
func3(&v40);
if ( !v40 && v12 )
{
v19 = v11 != 0;
if ( v11 )
v11 = 0;
if ( v19 )
v12 = 0;
v20 = 1;
if ( !v19 )
v20 = v23;
v23 = v20;
if ( v19 )
v13 = 3;
}
if ( !v15 )
{
if ( !(v11 | v27) && v10 )
{
v10 = unkey(0LL);
v34 = 0;
v27 = 0;
v23 = 1;
v24 = 1;
}
if ( !v11 && v10 && func4() - v25 > 3000.0 )
{
v10 = unkey(0LL);
v34 = 0;
v27 = 0;
}
}
fflush(stdout);
fflush(stderr);
nanosleep(&requested_time, 0LL);
}
v26 = fanon(0LL);
v31 = 0;
LABEL_18:
v37 = func4();
goto LABEL_106;
}
envp = argv;
v3 = &argv[argc - 1];
while ( 1 )
{
v4 = (char *)envp[1];
if ( !strcmp(v4, "-h") )
break;
v5 = strcmp(envp[1], "-v") != 0;
v6 = 0;
v7 = !v5;
if ( !v5 )
verbose = 1;
v8 = "-l";
v9 = 3LL;
argv = (const char **)v4;
do
{
if ( !v9 )
break;
v6 = *(_BYTE *)argv < *v8;
v7 = *(_BYTE *)argv == *v8;
argv = (const char **)((char *)argv + 1);
++v8;
--v9;
}
while ( v7 );
if ( (!v6 && !v7) == v6 )
logging = 1;
if ( v3 == ++envp )
goto LABEL_12;
}
fwrite(
"Usage: repeater [OPTION]\n"
"The repeater controller.\n"
" -l log to syslog\n"
" -v clutter the screen\n"
" -h display this help and exit\n"
"Copyright (c) 2013, Adi Linden <adi@adis.ca>\n",
1uLL,
0xBCuLL,
stderr);
return -1;
}

original_code:
int main(int argc, char *argv[])
  {
    int irlpdev = 0;
    unsigned char c[2];
    pid_t ctpid = 0;
    pid_t idpid = 0;
    int idstate = 0;
    int ctbusy = 0;
    int idbusy = 0;
    int ctflag = 1;
    int idflag = 1;
    int muteflag = 0;
    int keyflag = 0;
    int shortkeyflag = 0;
    int forcekeyflag = 0;
    int fanflag = 0;
    int irlpflag = 0;
    unsigned char COS = '1';
    unsigned char dtmf = '0';
    unsigned char irlpkey;
    struct timespec tim;
    double mutetimer = 0;
    double hangtimer = 0;
    double cttimer = 0;
    double idtimer = 0;
    double shortkeytimer = 0;
    double fantimer = 0;
    while (argc > 1) {
        if (!strcmp(argv[1], "-h")) {
            fprintf(stderr, usage);
            return -1;
        }
        if (!strcmp(argv[1], "-v")) {
            verbose = 1;
        }
        if (!strcmp(argv[1], "-l")) {
            logging = 1;
        }
        --argc;
        ++argv;
    }
    if (logging)
        open_syslog("repeater");
    do_log("Starting: " "repeater" ", version " "20130112");
    if(irlpdev_open() < 0 ) {
        fprintf(stderr, "Can't access parallel port");
        exit(-1);
    }
    tim.tv_sec = 0;
    tim.tv_nsec = 5000000;
    keyflag = unkey(irlpdev);
    muteflag = mute(irlpdev);
    while (1) {
        if (read_irlpdev(c, 2) != 2)
            fprintf(stderr, "Can't read parallel port");
        COS = (c[0] >> 7) & 0x01;
        dtmf = (c[0] >> 3) & 0x0f;
        irlpkey = c[1] & 0x02;
        if (keyflag && !fanflag) {
            fanflag = fanon(irlpdev);
        }
        if (!keyflag && fanflag && dnow() - fantimer > 300000) {
            fanflag = fanoff(irlpdev);
        }
        if (keyflag) {
            fantimer = dnow();
        }
        if (COS) {
            if (dtmf >= 1 && dtmf <= 17) {
                if (!muteflag)
                    muteflag = mute(irlpdev);
                mutetimer = dnow();
            } else {
                if (muteflag) {
                    if (((dnow() - mutetimer) > 1000) && muteflag)
                        muteflag = unmute(irlpdev);
                }
            }
        }
        if (!COS && !muteflag) {
            muteflag = mute(irlpdev);
        }
        if (COS) {
            if (!keyflag) {
                keyflag = keyup(irlpdev);
                shortkeytimer = dnow();
            }
            hangtimer = dnow();
            cttimer = dnow();
            ctflag = 0;
            idflag = 0;
        }
        if (irlpkey) {
            if (!keyflag) {
                keyflag = keyup(irlpdev);
                shortkeytimer = dnow();
            }
            hangtimer = dnow();
            cttimer = dnow();
            irlpflag = 1;
            idflag = 0;
        }
        if(forcekeyflag) {
            if (!keyflag) {
                keyflag = keyup(irlpdev);
                shortkeytimer = dnow();
            }
            hangtimer = dnow();
        }
        if (COS || irlpkey || forcekeyflag) {
            if (!shortkeyflag && dnow() - shortkeytimer > 10) {
                do_log("Shortkey exceeded");
                shortkeyflag = 1;
            }
        }
        if (!COS && !irlpkey && !idbusy) {
            if (!ctflag && !ctpid && irlpflag && dnow() - cttimer > 300) {
                do_ct(&ctpid);
                ctbusy = 1;
                forcekeyflag = 1;
            }
            if (!ctflag && !ctpid && !irlpflag && dnow() - cttimer > 1000) {
                do_ct(&ctpid);
                ctbusy = 1;
                forcekeyflag = 1;
            }
        }
        check_script(&ctpid);
        if (!ctpid && ctbusy && forcekeyflag) {
            ctbusy = 0;
            ctflag = 1;
            forcekeyflag = 0;
        }
        if (idstate == 0 && !idflag) {
            idstate = 1;
            idtimer = dnow();
            do_log("ID: immediate");
        }
        if (idstate == 3 && !idflag) {
            idstate = 2;
            do_log("ID: delayed");
        }
        if (idstate == 1 && !idpid) {
            if (!COS && !irlpkey && keyflag && ctflag) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
            if (dnow() - idtimer > 480000) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
        }
        if (idstate == 2 && !idpid) {
            if (!COS && !irlpkey && keyflag && ctflag &&
                    dnow() - idtimer > 1200000) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
            if (dnow() - idtimer > 1200000 + 480000) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
        }
        if (idstate == 3 && dnow() - idtimer > 1200000 + 480000) {
            idstate = 0;
            do_log("ID: reset");
        }
        check_script(&idpid);
        if (!idpid && idbusy && forcekeyflag) {
            idbusy = 0;
            idflag = 1;
            idstate = 3;
            forcekeyflag = 0;
        }
        if (!COS && !irlpkey && !forcekeyflag && keyflag && !shortkeyflag) {
            keyflag = unkey(irlpdev);
            ctflag = 1;
            idflag = 1;
            irlpflag = 0;
            shortkeyflag = 0;
        }
        if (!COS && !irlpkey && !forcekeyflag &&
                keyflag && (dnow() - hangtimer > 3000)) {
            keyflag = unkey(irlpdev);
            irlpflag = 0;
            shortkeyflag = 0;
        }
        fflush(stdout);
        fflush(stderr);
        nanosleep(&tim, ((void *)0));
    }
}

canonical_original_code:
int main(int argc, char *argv[])
  {
    int irlpdev = 0;
    unsigned char c[2];
    int ctpid = 0;
    int idpid = 0;
    int idstate = 0;
    int ctbusy = 0;
    int idbusy = 0;
    int ctflag = 1;
    int idflag = 1;
    int muteflag = 0;
    int keyflag = 0;
    int shortkeyflag = 0;
    int forcekeyflag = 0;
    int fanflag = 0;
    int irlpflag = 0;
    unsigned char COS = '1';
    unsigned char dtmf = '0';
    unsigned char irlpkey;
    struct timespec tim;
    double mutetimer = 0;
    double hangtimer = 0;
    double cttimer = 0;
    double idtimer = 0;
    double shortkeytimer = 0;
    double fantimer = 0;
    while (argc > 1) {
        if (!strcmp(argv[1], "-h")) {
            fprintf(stderr, usage);
            return -1;
        }
        if (!strcmp(argv[1], "-v")) {
            verbose = 1;
        }
        if (!strcmp(argv[1], "-l")) {
            logging = 1;
        }
        --argc;
        ++argv;
    }
    if (logging)
        open_syslog("repeater");
    do_log("Starting: " "repeater" ", version " "20130112");
    if(irlpdev_open() < 0 ) {
        fprintf(stderr, "Can't access parallel port");
        exit(-1);
    }
    tim.tv_sec = 0;
    tim.tv_nsec = 5000000;
    keyflag = unkey(irlpdev);
    muteflag = mute(irlpdev);
    while (1) {
        if (read_irlpdev(c, 2) != 2)
            fprintf(stderr, "Can't read parallel port");
        COS = (c[0] >> 7) & 0x01;
        dtmf = (c[0] >> 3) & 0x0f;
        irlpkey = c[1] & 0x02;
        if (keyflag && !fanflag) {
            fanflag = fanon(irlpdev);
        }
        if (!keyflag && fanflag && dnow() - fantimer > 300000) {
            fanflag = fanoff(irlpdev);
        }
        if (keyflag) {
            fantimer = dnow();
        }
        if (COS) {
            if (dtmf >= 1 && dtmf <= 17) {
                if (!muteflag)
                    muteflag = mute(irlpdev);
                mutetimer = dnow();
            } else {
                if (muteflag) {
                    if (((dnow() - mutetimer) > 1000) && muteflag)
                        muteflag = unmute(irlpdev);
                }
            }
        }
        if (!COS && !muteflag) {
            muteflag = mute(irlpdev);
        }
        if (COS) {
            if (!keyflag) {
                keyflag = keyup(irlpdev);
                shortkeytimer = dnow();
            }
            hangtimer = dnow();
            cttimer = dnow();
            ctflag = 0;
            idflag = 0;
        }
        if (irlpkey) {
            if (!keyflag) {
                keyflag = keyup(irlpdev);
                shortkeytimer = dnow();
            }
            hangtimer = dnow();
            cttimer = dnow();
            irlpflag = 1;
            idflag = 0;
        }
        if(forcekeyflag) {
            if (!keyflag) {
                keyflag = keyup(irlpdev);
                shortkeytimer = dnow();
            }
            hangtimer = dnow();
        }
        if (COS || irlpkey || forcekeyflag) {
            if (!shortkeyflag && dnow() - shortkeytimer > 10) {
                do_log("Shortkey exceeded");
                shortkeyflag = 1;
            }
        }
        if (!COS && !irlpkey && !idbusy) {
            if (!ctflag && !ctpid && irlpflag && dnow() - cttimer > 300) {
                do_ct(&ctpid);
                ctbusy = 1;
                forcekeyflag = 1;
            }
            if (!ctflag && !ctpid && !irlpflag && dnow() - cttimer > 1000) {
                do_ct(&ctpid);
                ctbusy = 1;
                forcekeyflag = 1;
            }
        }
        check_script(&ctpid);
        if (!ctpid && ctbusy && forcekeyflag) {
            ctbusy = 0;
            ctflag = 1;
            forcekeyflag = 0;
        }
        if (idstate == 0 && !idflag) {
            idstate = 1;
            idtimer = dnow();
            do_log("ID: immediate");
        }
        if (idstate == 3 && !idflag) {
            idstate = 2;
            do_log("ID: delayed");
        }
        if (idstate == 1 && !idpid) {
            if (!COS && !irlpkey && keyflag && ctflag) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
            if (dnow() - idtimer > 480000) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
        }
        if (idstate == 2 && !idpid) {
            if (!COS && !irlpkey && keyflag && ctflag &&
                    dnow() - idtimer > 1200000) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
            if (dnow() - idtimer > 1200000 + 480000) {
                do_id(&idpid);
                idbusy = 1;
                forcekeyflag = 1;
                idtimer = dnow();
            }
        }
        if (idstate == 3 && dnow() - idtimer > 1200000 + 480000) {
            idstate = 0;
            do_log("ID: reset");
        }
        check_script(&idpid);
        if (!idpid && idbusy && forcekeyflag) {
            idbusy = 0;
            idflag = 1;
            idstate = 3;
            forcekeyflag = 0;
        }
        if (!COS && !irlpkey && !forcekeyflag && keyflag && !shortkeyflag) {
            keyflag = unkey(irlpdev);
            ctflag = 1;
            idflag = 1;
            irlpflag = 0;
            shortkeyflag = 0;
        }
        if (!COS && !irlpkey && !forcekeyflag &&
                keyflag && (dnow() - hangtimer > 3000)) {
            keyflag = unkey(irlpdev);
            irlpflag = 0;
            shortkeyflag = 0;
        }
        fflush(stdout);
        fflush(stderr);
        nanosleep(&tim, ((void *)0));
    }
}

variable_types: irlpdev int

variable_types:
c
unsigned char [2]

variable_types: ctpid int

variable_types: idpid int

variable_types: idstate int

variable_types: ctbusy int

variable_types: idbusy int

variable_types: ctflag int

variable_types: idflag int

variable_types: muteflag int

variable_types: keyflag int

variable_types: shortkeyflag int

variable_types: forcekeyflag int

variable_types: fanflag int

variable_types: irlpflag int

variable_types:
COS
unsigned char

variable_types:
dtmf
unsigned char

variable_types:
irlpkey
unsigned char

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: mutetimer double

variable_types: hangtimer double

variable_types: cttimer double

variable_types: idtimer double

variable_types: shortkeytimer double

variable_types: fantimer double


decompiled_code:
__int64 __fastcall alsa_open(__int64 a1)
{
__int64 result; // rax
const char *v2; // rax

result = snd_pcm_open(&ph, a1, 0LL, 0LL);
if ( (int)result < 0 )
{
v2 = (const char *)snd_strerror((unsigned int)result);
fprintf(stderr, "open of pcm device %s failed\n", v2);
exit(1);
}
return result;
}

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

result = snd_pcm_open(&ph, a1, 0LL, 0LL);
if ( (int)result < 0 )
{
v2 = (const char *)snd_strerror((unsigned int)result);
fprintf(stderr, "open of pcm device %s failed\n", v2);
exit(1);
}
return result;
}

original_code:
void alsa_open(char *ident)
{
    int rc;
    rc = snd_pcm_open(&ph, ident, SND_PCM_STREAM_PLAYBACK, 0);
    if (rc < 0) {
        fprintf(stderr, "open of pcm device %s failed\n", snd_strerror(rc));
        exit(1);
    }
}

variable_types:
ident
char *

variable_types: rc int


decompiled_code:
__int64 __fastcall alsa_write(_QWORD *a1, _DWORD *a2)
{
__int64 v2; // rax
unsigned int v3; // ebx
const char *v5; // rax

v2 = snd_pcm_writei(ph, *a1, *a2 / 2);
v3 = v2;
if ( v2 < 0 )
{
v5 = (const char *)snd_strerror((unsigned int)v2);
fprintf(stderr, "write error: %s\n", v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, _DWORD *a2)
{
__int64 v2; // rax
unsigned int v3; // ebx
const char *v5; // rax

v2 = snd_pcm_writei(ph, *a1, *a2 / 2);
v3 = v2;
if ( v2 < 0 )
{
v5 = (const char *)snd_strerror((unsigned int)v2);
fprintf(stderr, "write error: %s\n", v5);
}
return v3;
}

original_code:
int alsa_write(int16_t **bf, int *nbf)
{
    snd_pcm_sframes_t rc;
    int n;
    n = *nbf / 2;
    rc = snd_pcm_writei(ph, *bf, n);
    if (rc < 0) {
        fprintf(stderr, "write error: %s\n", snd_strerror(rc));
    }
    return rc;
}

canonical_original_code:
int alsa_write(short **bf, int *nbf)
{
    long rc;
    int n;
    n = *nbf / 2;
    rc = snd_pcm_writei(ph, *bf, n);
    if (rc < 0) {
        fprintf(stderr, "write error: %s\n", snd_strerror(rc));
    }
    return rc;
}

variable_types:
bf
short **

variable_types:
nbf
int *

variable_types: rc long

variable_types: n int


decompiled_code:
__int64 alsa_close()
{
snd_pcm_drain(ph);
return snd_pcm_close(ph);
}

canonical_decompiled_code:
__int64 func3()
{
snd_pcm_drain(ph);
return snd_pcm_close(ph);
}

original_code:
void alsa_close()
{
    snd_pcm_drain(ph);
    snd_pcm_close(ph);
}


sinval

decompiled_code:
int __fastcall sinval(int a1, int a2, int a3, int a4)
{
return (__int16)(int)((double)a3 * 32767.0 / 100.0 * sin((double)a4 / (double)a2 * (double)a1 * 6.283185307179586));
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, int a3, int a4)
{
return (__int16)(int)((double)a3 * 32767.0 / 100.0 * sin((double)a4 / (double)a2 * (double)a1 * 6.283185307179586));
}

original_code:
int sinval(int freq, int rate, int ampl, int p)
{
    int16_t v;
    v = ((double)ampl * 0x7fff / 100) * sin((double)p / rate * freq * (2 * 3.141592653589793));
    return v;
}

canonical_original_code:
int sinval(int freq, int rate, int ampl, int p)
{
    short v;
    v = ((double)ampl * 0x7fff / 100) * sin((double)p / rate * freq * (2 * 3.141592653589793));
    return v;
}

variable_types: freq int

variable_types: rate int

variable_types: ampl int

variable_types: p int

variable_types: v short


mkwave

original_code:
int mkwave(int freq, int rate, int ampl, int dura, int atta, int deca,
           int16_t **bf, int *nbf)
{
    int st, sa, sd, ss;
    int16_t *tbf;
    int ntbf;
    int i;
    int a;
    st = (double)rate * dura / 1000;
    sa = (double)rate * atta / 1000;
    sd = (double)rate * deca / 1000;
    ss = st - sa - sd;
    ntbf = st * 2;
    tbf = malloc(ntbf);
    if (tbf == ((void *)0)) {
        return -1;
    }
    for (i = 0; i < sa; ++i) {
        a = ((double)ampl / sa) * i;
        tbf[i] = sinval(freq, rate, a, i);
    }
    for (i = sa; i < sa + ss; ++i) {
        tbf[i] = sinval(freq, rate, ampl, i);
    }
    for (i = sa + ss; i < st; ++i) {
        a = ((double)ampl / sd) * (st - i);
        tbf[i] = sinval(freq, rate, a, i);
    }
    *bf = tbf;
    *nbf = ntbf;
    return st;
}

canonical_original_code:
int mkwave(int freq, int rate, int ampl, int dura, int atta, int deca, short **bf, int *nbf)
{
    int st; int sa; int sd; int ss;
    short *tbf;
    int ntbf;
    int i;
    int a;
    st = (double)rate * dura / 1000;
    sa = (double)rate * atta / 1000;
    sd = (double)rate * deca / 1000;
    ss = st - sa - sd;
    ntbf = st * 2;
    tbf = malloc(ntbf);
    if (tbf == ((void *)0)) {
        return -1;
    }
    for (i = 0; i < sa; ++i) {
        a = ((double)ampl / sa) * i;
        tbf[i] = sinval(freq, rate, a, i);
    }
    for (i = sa; i < sa + ss; ++i) {
        tbf[i] = sinval(freq, rate, ampl, i);
    }
    for (i = sa + ss; i < st; ++i) {
        a = ((double)ampl / sd) * (st - i);
        tbf[i] = sinval(freq, rate, a, i);
    }
    *bf = tbf;
    *nbf = ntbf;
    return st;
}

variable_types: dura int

variable_types: atta int

variable_types: deca int

variable_types: st int

variable_types: sa int

variable_types: sd int

variable_types: ss int

variable_types:
tbf
short *

variable_types: ntbf int

variable_types: i int

variable_types: a int


mksilence

decompiled_code:
__int64 __fastcall mksilence(int a1, int a2, _QWORD *a3, _DWORD *a4)
{
int v6; // ebx
_WORD *v7; // rax
_WORD *v8; // rcx
_WORD *v9; // rdx

v6 = (int)((double)a1 * (double)a2 / 1000.0);
v7 = malloc(2 * v6);
if ( v7 )
{
v8 = v7;
if ( v6 > 0 )
{
v9 = &v7[v6];
do
*v7++ = 0;
while ( v7 != v9 );
}
*a3 = v8;
*a4 = 2 * v6;
}
else
{
v6 = -1;
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, _QWORD *a3, _DWORD *a4)
{
int v6; // ebx
_WORD *v7; // rax
_WORD *v8; // rcx
_WORD *v9; // rdx

v6 = (int)((double)a1 * (double)a2 / 1000.0);
v7 = malloc(2 * v6);
if ( v7 )
{
v8 = v7;
if ( v6 > 0 )
{
v9 = &v7[v6];
do
*v7++ = 0;
while ( v7 != v9 );
}
*a3 = v8;
*a4 = 2 * v6;
}
else
{
v6 = -1;
}
return (unsigned int)v6;
}

original_code:
int mksilence(int rate, int dura, int16_t **bf, int *nbf)
{
    int16_t *tbf;
    int ntbf;
    int i, st;
    st = (double)rate * dura / 1000;
    ntbf = st * 2;
    tbf = malloc(ntbf);
    if (tbf == ((void *)0)) {
        return -1;
    }
    for (i = 0; i < st; ++i) {
        tbf[i] = 0;
    }
    *bf = tbf;
    *nbf = ntbf;
    return st;
}

canonical_original_code:
int mksilence(int rate, int dura, short **bf, int *nbf)
{
    short *tbf;
    int ntbf;
    int i; int st;
    st = (double)rate * dura / 1000;
    ntbf = st * 2;
    tbf = malloc(ntbf);
    if (tbf == ((void *)0)) {
        return -1;
    }
    for (i = 0; i < st; ++i) {
        tbf[i] = 0;
    }
    *bf = tbf;
    *nbf = ntbf;
    return st;
}

variable_types:
bf
short **

variable_types:
nbf
int *


repo: aditibarbhai/buxfer

decompiled_code:
int __fastcall list_groups(const char **a1)
{
const char **i; // rbx

if ( !a1 )
return puts(" ");
for ( i = a1; i[3]; i = (const char **)i[3] )
printf("%s \n", *i);
return printf("%s \n", *i);
}

canonical_decompiled_code:
int __fastcall func0(const char **a1)
{
const char **i; // rbx

if ( !a1 )
return puts(" ");
for ( i = a1; i[3]; i = (const char **)i[3] )
printf("%s \n", *i);
return printf("%s \n", *i);
}

original_code:
void list_groups(Group *group_list) {
    Group *currentGrp = group_list;
    if ( currentGrp ) {
        while ( currentGrp->next ) {
            printf("%s \n", currentGrp->name);
            currentGrp = currentGrp->next;
        }
        printf("%s \n", currentGrp->name);
    } else printf(" \n");
}

canonical_original_code:
void list_groups(struct group *group_list) {
    struct group *currentGrp = group_list;
    if ( currentGrp ) {
        while ( currentGrp->next ) {
            printf("%s \n", currentGrp->name);
            currentGrp = currentGrp->next;
        }
        printf("%s \n", currentGrp->name);
    } else printf(" \n");
}

variable_types: struct group { char *name; struct user { char *name; double balance; struct user *next; } *users; struct xct { char *name; double amount; struct xct *next; } *xcts; struct group *next; } *

user_defined_types: struct group { char *name; struct user *users; struct xct *xcts; struct group *next; }


decompiled_code:
const char **__fastcall find_group(__int64 a1, const char *a2)
{
__int64 v2; // rbx
const char **v3; // rbp

v2 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
v3 = (const char **)v2;
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
break;
if ( !strcmp(*v3, a2) )
return v3;
}
if ( strcmp(*v3, a2) )
v3 = 0LL;
return v3;
}

canonical_decompiled_code:
const char **__fastcall func1(__int64 a1, const char *a2)
{
__int64 v2; // rbx
const char **v3; // rbp

v2 = a1;
if ( !a1 )
return 0LL;
while ( 1 )
{
v3 = (const char **)v2;
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
break;
if ( !strcmp(*v3, a2) )
return v3;
}
if ( strcmp(*v3, a2) )
v3 = 0LL;
return v3;
}

original_code:
Group *find_group(Group *group_list, const char *group_name) {
    Group *currentGrp = group_list;
    if ( currentGrp == ((void *)0) ) return ((void *)0);
    while( currentGrp->next ) {
        if ( strcmp(currentGrp->name, group_name) == 0 ) {
            return currentGrp;
        }
        currentGrp = currentGrp->next;
    }
    if( strcmp( currentGrp->name, group_name ) == 0 ) {
        return currentGrp;
    } else return ((void *)0);
}

canonical_original_code:
struct group *find_group(struct group *group_list, char *group_name) {
    struct group *currentGrp = group_list;
    if ( currentGrp == ((void *)0) ) return ((void *)0);
    while( currentGrp->next ) {
        if ( strcmp(currentGrp->name, group_name) == 0 ) {
            return currentGrp;
        }
        currentGrp = currentGrp->next;
    }
    if( strcmp( currentGrp->name, group_name ) == 0 ) {
        return currentGrp;
    } else return ((void *)0);
}

return_type: struct group { char *name; struct user { char *name; double balance; struct user *next; } *users; struct xct { char *name; double amount; struct xct *next; } *xcts; struct group *next; } *


decompiled_code:
__int64 __fastcall add_group(__int64 *a1, const char *a2)
{
__int64 v2; // rbx
_QWORD *v3; // r12
size_t v4; // rbp
char *v5; // rax
char *v6; // r13
__int64 v7; // rax
__int64 result; // rax

v2 = *a1;
if ( find_group(*a1, a2) )
return 0xFFFFFFFFLL;
v3 = malloc(0x20uLL);
if ( !v3 )
{
puts("Error when creating group pointer. Program will now exit. ");
exit(0);
}
v4 = (int)(strlen(a2) + 1);
v5 = (char *)malloc(v4);
v6 = v5;
*v3 = v5;
if ( !v5 )
{
puts("Error when adding group name. Program will now exit ");
exit(0);
}
strncpy(v5, a2, v4);
v6[v4] = 0;
v3[3] = 0LL;
if ( v2 )
{
do
{
v7 = v2;
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
*(_QWORD *)(v7 + 24) = v3;
result = 0LL;
}
else
{
*a1 = (__int64)v3;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1, const char *a2)
{
__int64 v2; // rbx
_QWORD *v3; // r12
size_t v4; // rbp
char *v5; // rax
char *v6; // r13
__int64 v7; // rax
__int64 result; // rax

v2 = *a1;
if ( func1(*a1, a2) )
return 0xFFFFFFFFLL;
v3 = malloc(0x20uLL);
if ( !v3 )
{
puts("Error when creating group pointer. Program will now exit. ");
exit(0);
}
v4 = (int)(strlen(a2) + 1);
v5 = (char *)malloc(v4);
v6 = v5;
*v3 = v5;
if ( !v5 )
{
puts("Error when adding group name. Program will now exit ");
exit(0);
}
strncpy(v5, a2, v4);
v6[v4] = 0;
v3[3] = 0LL;
if ( v2 )
{
do
{
v7 = v2;
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
*(_QWORD *)(v7 + 24) = v3;
result = 0LL;
}
else
{
*a1 = (__int64)v3;
result = 0LL;
}
return result;
}

original_code:
int add_group(Group **group_list_ptr, const char *group_name) {
    if ( find_group(*group_list_ptr, group_name) ) {
        return -1;
    } else {
        Group *newGrp = malloc(sizeof(Group));
        if ( newGrp == ((void *)0) ) {
            printf("Error when creating group pointer. Program will now exit. \n");
            exit(0);
        } else {
            int LENGTH = strlen(group_name) + 1;
            newGrp->name = malloc(LENGTH);
            if ( newGrp->name == ((void *)0) ) {
                printf("Error when adding group name. Program will now exit \n");
                exit(0);
            } else {
                strncpy(newGrp->name, group_name, LENGTH );
                newGrp->name[LENGTH] = '\0';
                newGrp->next = ((void *)0);
            }
        }
        if ( *group_list_ptr == ((void *)0) ) {
            *group_list_ptr = newGrp;
            return 0;
        } else {
            Group *currentGrp = *group_list_ptr;
            while ( currentGrp->next ) {
                currentGrp = currentGrp->next;
            }
            currentGrp->next = newGrp;
            return 0;
        }
    }
}

canonical_original_code:
int add_group(struct group **group_list_ptr, char *group_name) {
    if ( find_group(*group_list_ptr, group_name) ) {
        return -1;
    } else {
        struct group *newGrp = malloc(sizeof(Group));
        if ( newGrp == ((void *)0) ) {
            printf("Error when creating group pointer. Program will now exit. \n");
            exit(0);
        } else {
            int LENGTH = strlen(group_name) + 1;
            newGrp->name = malloc(LENGTH);
            if ( newGrp->name == ((void *)0) ) {
                printf("Error when adding group name. Program will now exit \n");
                exit(0);
            } else {
                strncpy(newGrp->name, group_name, LENGTH );
                newGrp->name[LENGTH] = '\0';
                newGrp->next = ((void *)0);
            }
        }
        if ( *group_list_ptr == ((void *)0) ) {
            *group_list_ptr = newGrp;
            return 0;
        } else {
            struct group *currentGrp = *group_list_ptr;
            while ( currentGrp->next ) {
                currentGrp = currentGrp->next;
            }
            currentGrp->next = newGrp;
            return 0;
        }
    }
}

variable_types: struct group { char *name; struct user { char *name; double balance; struct user *next; } *users; struct xct { char *name; double amount; struct xct *next; } *xcts; struct group *next; } **

variable_types: LENGTH int


decompiled_code:
int __fastcall list_users(__int64 a1)
{
const char **v1; // rbx

v1 = *(const char ***)(a1 + 8);
if ( !v1 )
return puts(" ");
for ( ; v1[2]; v1 = (const char **)v1[2] )
printf("%s \n", *v1);
return printf("%s \n", *v1);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
const char **v1; // rbx

v1 = *(const char ***)(a1 + 8);
if ( !v1 )
return puts(" ");
for ( ; v1[2]; v1 = (const char **)v1[2] )
printf("%s \n", *v1);
return printf("%s \n", *v1);
}

original_code:
void list_users(Group *group) {
    User *userPtr = group->users;
    if ( userPtr ) {
        while ( userPtr->next ) {
            printf("%s \n", userPtr->name);
            userPtr = userPtr->next;
        }
        printf("%s \n", userPtr->name);
    } else printf(" \n");
}

canonical_original_code:
void list_users(struct group *group) {
    struct user *userPtr = group->users;
    if ( userPtr ) {
        while ( userPtr->next ) {
            printf("%s \n", userPtr->name);
            userPtr = userPtr->next;
        }
        printf("%s \n", userPtr->name);
    } else printf(" \n");
}

variable_types: struct user { char *name; double balance; struct user *next; } *

user_defined_types: struct xct { char *name; double amount; struct xct *next; }


decompiled_code:
__int64 __fastcall under_paid(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rbx
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 8);
if ( !v1 )
return 0xFFFFFFFFLL;
if ( *(_QWORD *)(v1 + 16) )
{
v2 = *(_QWORD *)(a1 + 8);
do
{
if ( *(double *)(v1 + 8) >= *(double *)(v2 + 8) )
puts(*(const char **)v2);
v2 = *(_QWORD *)(v2 + 16);
}
while ( *(_QWORD *)(v2 + 16) );
}
else
{
v2 = *(_QWORD *)(a1 + 8);
}
result = 0LL;
if ( *(double *)(v1 + 8) >= *(double *)(v2 + 8) )
{
puts(*(const char **)v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rbx
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 8);
if ( !v1 )
return 0xFFFFFFFFLL;
if ( *(_QWORD *)(v1 + 16) )
{
v2 = *(_QWORD *)(a1 + 8);
do
{
if ( *(double *)(v1 + 8) >= *(double *)(v2 + 8) )
puts(*(const char **)v2);
v2 = *(_QWORD *)(v2 + 16);
}
while ( *(_QWORD *)(v2 + 16) );
}
else
{
v2 = *(_QWORD *)(a1 + 8);
}
result = 0LL;
if ( *(double *)(v1 + 8) >= *(double *)(v2 + 8) )
{
puts(*(const char **)v2);
result = 0LL;
}
return result;
}

original_code:
int under_paid(Group *group) {
    User *currentUser = group->users;
    if ( currentUser ) {
        User *underPaid = currentUser;
        while ( currentUser->next ) {
            if ( currentUser->balance <= underPaid->balance ) {
                printf("%s\n", currentUser->name);
            }
            currentUser = currentUser->next;
        }
        if ( currentUser->balance <= underPaid->balance ) {
            printf("%s\n", currentUser->name );
        }
        return 0;
    } else return -1;
}

canonical_original_code:
int under_paid(struct group *group) {
    struct user *currentUser = group->users;
    if ( currentUser ) {
        struct user *underPaid = currentUser;
        while ( currentUser->next ) {
            if ( currentUser->balance <= underPaid->balance ) {
                printf("%s\n", currentUser->name);
            }
            currentUser = currentUser->next;
        }
        if ( currentUser->balance <= underPaid->balance ) {
            printf("%s\n", currentUser->name );
        }
        return 0;
    } else return -1;
}

user_defined_types: struct user { char *name; double balance; struct user *next; }


decompiled_code:
const char **__fastcall find_prev_user(__int64 a1, const char *a2)
{
const char **v2; // r12
char *v3; // rbx
const char **v4; // rbp

v2 = *(const char ***)(a1 + 8);
if ( v2 )
{
v3 = (char *)v2[2];
if ( v3 )
{
if ( strcmp(*v2, a2) )
{
while ( 1 )
{
v4 = (const char **)v3;
v3 = (char *)*((_QWORD *)v3 + 2);
if ( !v3 )
break;
if ( !strcmp(*v4, a2) )
return v2;
v2 = v4;
}
if ( strcmp(*v4, a2) )
v2 = 0LL;
}
}
else if ( strcmp(*v2, a2) )
{
v2 = 0LL;
}
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func5(__int64 a1, const char *a2)
{
const char **v2; // r12
char *v3; // rbx
const char **v4; // rbp

v2 = *(const char ***)(a1 + 8);
if ( v2 )
{
v3 = (char *)v2[2];
if ( v3 )
{
if ( strcmp(*v2, a2) )
{
while ( 1 )
{
v4 = (const char **)v3;
v3 = (char *)*((_QWORD *)v3 + 2);
if ( !v3 )
break;
if ( !strcmp(*v4, a2) )
return v2;
v2 = v4;
}
if ( strcmp(*v4, a2) )
v2 = 0LL;
}
}
else if ( strcmp(*v2, a2) )
{
v2 = 0LL;
}
}
return v2;
}

original_code:
User *find_prev_user(Group *group, const char *user_name) {
    User *previous, *current;
    if ( group->users ) {
        current = group->users;
        if( current->next == ((void *)0) ){
            if ( strcmp(current->name, user_name) == 0 ) {
                return current;
            } else return ((void *)0);
        } else {
            if ( strcmp(current->name, user_name) == 0 ) {
                return current;
            } else {
                previous = current;
                current = current->next;
                while ( current->next ) {
                    if ( strcmp(current->name, user_name) == 0 ) {
                        return previous;
                    }
                    previous = current;
                    current = current->next;
                }
                if(strcmp(current->name, user_name) == 0) {
                    return previous;
                }
            }
            return ((void *)0);
        }
    } else return ((void *)0);
}

canonical_original_code:
struct user *find_prev_user(struct group *group, char *user_name) {
    struct user *previous; struct user *current;
    if ( group->users ) {
        current = group->users;
        if( current->next == ((void *)0) ){
            if ( strcmp(current->name, user_name) == 0 ) {
                return current;
            } else return ((void *)0);
        } else {
            if ( strcmp(current->name, user_name) == 0 ) {
                return current;
            } else {
                previous = current;
                current = current->next;
                while ( current->next ) {
                    if ( strcmp(current->name, user_name) == 0 ) {
                        return previous;
                    }
                    previous = current;
                    current = current->next;
                }
                if(strcmp(current->name, user_name) == 0) {
                    return previous;
                }
            }
            return ((void *)0);
        }
    } else return ((void *)0);
}

variable_types: struct group { char *name; struct user *users; struct xct { char *name; double amount; struct xct *next; } *xcts; struct group *next; } *

return_type: struct user { char *name; double balance; struct user *next; } *


decompiled_code:
__int64 __fastcall add_user(__int64 a1, const char *a2)
{
_QWORD *v2; // rbx
size_t v3; // rbp
char *v4; // rax
char *v5; // r14
__int64 result; // rax

if ( find_prev_user(a1, a2) )
return 0xFFFFFFFFLL;
v2 = malloc(0x18uLL);
if ( !v2 )
{
puts("Error while creating a new user. Program will now exit. ");
exit(0);
}
v3 = (int)(strlen(a2) + 1);
v4 = (char *)malloc(v3);
v5 = v4;
*v2 = v4;
if ( !v4 )
{
puts("Error while creating user name. Program will now exit. ");
exit(0);
}
strncpy(v4, a2, v3);
v5[v3] = 0;
v2[1] = 0LL;
v2[2] = 0LL;
result = *(_QWORD *)(a1 + 8);
if ( result )
{
v2[2] = result;
*(_QWORD *)(a1 + 8) = v2;
result = 0LL;
}
else
{
*(_QWORD *)(a1 + 8) = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, const char *a2)
{
_QWORD *v2; // rbx
size_t v3; // rbp
char *v4; // rax
char *v5; // r14
__int64 result; // rax

if ( func5(a1, a2) )
return 0xFFFFFFFFLL;
v2 = malloc(0x18uLL);
if ( !v2 )
{
puts("Error while creating a new user. Program will now exit. ");
exit(0);
}
v3 = (int)(strlen(a2) + 1);
v4 = (char *)malloc(v3);
v5 = v4;
*v2 = v4;
if ( !v4 )
{
puts("Error while creating user name. Program will now exit. ");
exit(0);
}
strncpy(v4, a2, v3);
v5[v3] = 0;
v2[1] = 0LL;
v2[2] = 0LL;
result = *(_QWORD *)(a1 + 8);
if ( result )
{
v2[2] = result;
*(_QWORD *)(a1 + 8) = v2;
result = 0LL;
}
else
{
*(_QWORD *)(a1 + 8) = v2;
}
return result;
}

original_code:
int add_user(Group *group, const char *user_name) {
    if ( find_prev_user(group, user_name) != ((void *)0) ) {
        return -1;
    } else {
        User *newUsr = malloc(sizeof(User));
        if ( newUsr == ((void *)0) ) {
            printf("Error while creating a new user. Program will now exit. \n");
            exit(0);
        } else {
            int LENGTH = strlen(user_name) + 1;
            newUsr->name = malloc(LENGTH);
            if ( newUsr->name == ((void *)0) ) {
                printf("Error while creating user name. Program will now exit. \n");
                exit(0);
            } else {
                strncpy(newUsr->name, user_name, LENGTH );
                newUsr->name[LENGTH] = '\0';
                newUsr->balance = 0;
                newUsr->next = ((void *)0);
            }
        }
        if ( group->users == ((void *)0) ) {
            group->users = newUsr;
        } else {
            newUsr->next = group->users;
            group->users = newUsr;
        }
        return 0;
    }
}

canonical_original_code:
int add_user(struct group *group, char *user_name) {
    if ( find_prev_user(group, user_name) != ((void *)0) ) {
        return -1;
    } else {
        struct user *newUsr = malloc(sizeof(User));
        if ( newUsr == ((void *)0) ) {
            printf("Error while creating a new user. Program will now exit. \n");
            exit(0);
        } else {
            int LENGTH = strlen(user_name) + 1;
            newUsr->name = malloc(LENGTH);
            if ( newUsr->name == ((void *)0) ) {
                printf("Error while creating user name. Program will now exit. \n");
                exit(0);
            } else {
                strncpy(newUsr->name, user_name, LENGTH );
                newUsr->name[LENGTH] = '\0';
                newUsr->balance = 0;
                newUsr->next = ((void *)0);
            }
        }
        if ( group->users == ((void *)0) ) {
            group->users = newUsr;
        } else {
            newUsr->next = group->users;
            group->users = newUsr;
        }
        return 0;
    }
}


decompiled_code:
__int64 __fastcall user_balance(__int64 a1, const char *a2)
{
const char **v2; // rax
const char **v3; // rbx
unsigned int v4; // ebp

if ( !*(_QWORD *)(a1 + 8) )
return (unsigned int)-1;
v2 = find_prev_user(a1, a2);
v3 = v2;
if ( !v2 )
return (unsigned int)-1;
v4 = strcmp(*v2, a2);
if ( v4 )
{
printf("$%.2f\n", *((double *)v3[2] + 1));
v4 = 0;
}
else
{
printf("$%.2f\n", *((double *)v3 + 1));
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2)
{
const char **v2; // rax
const char **v3; // rbx
unsigned int v4; // ebp

if ( !*(_QWORD *)(a1 + 8) )
return (unsigned int)-1;
v2 = func5(a1, a2);
v3 = v2;
if ( !v2 )
return (unsigned int)-1;
v4 = strcmp(*v2, a2);
if ( v4 )
{
printf("$%.2f\n", *((double *)v3[2] + 1));
v4 = 0;
}
else
{
printf("$%.2f\n", *((double *)v3 + 1));
}
return v4;
}

original_code:
int user_balance(Group *group, const char *user_name) {
    User *currentUser = group->users;
    User *prevUser = ((void *)0);
    if( currentUser ) {
        prevUser = find_prev_user(group, user_name);
        if ( prevUser ) {
            if ( strcmp(prevUser->name, user_name) == 0 ) {
                printf("$%.2f\n", prevUser->balance);
                return 0;
            } else {
                printf("$%.2f\n", prevUser->next->balance);
                return 0;
            }
        } else return -1;
    } else return -1;
}

canonical_original_code:
int user_balance(struct group *group, char *user_name) {
    struct user *currentUser = group->users;
    struct user *prevUser = ((void *)0);
    if( currentUser ) {
        prevUser = find_prev_user(group, user_name);
        if ( prevUser ) {
            if ( strcmp(prevUser->name, user_name) == 0 ) {
                printf("$%.2f\n", prevUser->balance);
                return 0;
            } else {
                printf("$%.2f\n", prevUser->next->balance);
                return 0;
            }
        } else return -1;
    } else return -1;
}


decompiled_code:
__int64 __fastcall add_xct(__int64 a1, const char *a2, double a3)
{
const char **v3; // rax
__int64 v4; // rbx

v3 = find_prev_user(a1, a2);
if ( !v3 )
return 0xFFFFFFFFLL;
v4 = (__int64)v3;
if ( strcmp(*v3, a2) )
v4 = *(_QWORD *)(v4 + 16);
xct_helper(a1, a2, a3);
*(double *)(v4 + 8) = a3 + *(double *)(v4 + 8);
update_user_position(a1, v4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, const char *a2, double a3)
{
const char **v3; // rax
__int64 v4; // rbx

v3 = func5(a1, a2);
if ( !v3 )
return 0xFFFFFFFFLL;
v4 = (__int64)v3;
if ( strcmp(*v3, a2) )
v4 = *(_QWORD *)(v4 + 16);
func8(a1, a2, a3);
*(double *)(v4 + 8) = a3 + *(double *)(v4 + 8);
func9(a1, v4);
return 0LL;
}

original_code:
int add_xct(Group *group, const char *user_name, double amount) {
    User *prevUser = ((void *)0);
    User *user = ((void *)0);
    prevUser = find_prev_user(group, user_name);
    if ( prevUser ) {
        if ( strcmp(prevUser->name, user_name) == 0 ) {
            user = prevUser;
        } else {
            user = prevUser->next;
        }
        _xct_helper(group, user_name, amount);
        user->balance = user->balance + amount;
        _update_user_position(group, user);
        return 0;
    } else return -1;
}

canonical_original_code:
int add_xct(struct group *group, char *user_name, double amount) {
    struct user *prevUser = ((void *)0);
    struct user *user = ((void *)0);
    prevUser = find_prev_user(group, user_name);
    if ( prevUser ) {
        if ( strcmp(prevUser->name, user_name) == 0 ) {
            user = prevUser;
        } else {
            user = prevUser->next;
        }
        _xct_helper(group, user_name, amount);
        user->balance = user->balance + amount;
        _update_user_position(group, user);
        return 0;
    } else return -1;
}

variable_types: amount double


decompiled_code:
int __fastcall recent_xct(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
int result; // eax

v2 = *(_QWORD *)(a1 + 16);
if ( !v2 )
return puts(" ");
v3 = a2;
if ( a2 > 0 )
{
do
{
result = printf("Transaction #%s; Amount: %.2f.\n", *(const char **)v2, *(double *)(v2 + 8));
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
break;
--v3;
}
while ( v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
int result; // eax

v2 = *(_QWORD *)(a1 + 16);
if ( !v2 )
return puts(" ");
v3 = a2;
if ( a2 > 0 )
{
do
{
result = printf("Transaction #%s; Amount: %.2f.\n", *(const char **)v2, *(double *)(v2 + 8));
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
break;
--v3;
}
while ( v3 );
}
return result;
}

original_code:
void recent_xct(Group *group, long nu_xct) {
    Xct *xctPtr = group->xcts;
    if( xctPtr == ((void *)0) ) {
        printf(" \n");
    } else {
        long num;
        for ( num = nu_xct ; num > 0; num-- ) {
            printf("Transaction #%s; Amount: %.2f.\n", xctPtr->name, xctPtr->amount);
            if( xctPtr->next == ((void *)0) ) {
                break;
            }
            xctPtr = xctPtr->next;
         }
    }
}

canonical_original_code:
void recent_xct(struct group *group, long nu_xct) {
    struct xct *xctPtr = group->xcts;
    if( xctPtr == ((void *)0) ) {
        printf(" \n");
    } else {
        long num;
        for ( num = nu_xct ; num > 0; num-- ) {
            printf("Transaction #%s; Amount: %.2f.\n", xctPtr->name, xctPtr->amount);
            if( xctPtr->next == ((void *)0) ) {
                break;
            }
            xctPtr = xctPtr->next;
         }
    }
}

variable_types: struct xct { char *name; double amount; struct xct *next; } *

variable_types: num long


decompiled_code:
void __fastcall remove_xct(__int64 a1, const char *a2)
{
const char **v2; // rbx
const char *v3; // r12
const char **v4; // r13
const char **v5; // rbp

v2 = *(const char ***)(a1 + 16);
if ( v2 )
{
v3 = v2[2];
if ( v3 )
{
v4 = 0LL;
while ( 1 )
{
if ( !strcmp(*v2, a2) )
{
if ( v4 )
v4[2] = v3;
else
*(_QWORD *)(a1 + 16) = v3;
free(v2);
}
v5 = (const char **)v2[2];
v3 = v5[2];
v4 = v2;
if ( !v3 )
break;
v2 = (const char **)v2[2];
}
if ( !strcmp(*v5, a2) )
{
free(v5);
v2[2] = 0LL;
}
}
else if ( !strcmp(*v2, a2) )
{
*(_QWORD *)(a1 + 16) = 0LL;
}
}
}

canonical_decompiled_code:
void __fastcall func12(__int64 a1, const char *a2)
{
const char **v2; // rbx
const char *v3; // r12
const char **v4; // r13
const char **v5; // rbp

v2 = *(const char ***)(a1 + 16);
if ( v2 )
{
v3 = v2[2];
if ( v3 )
{
v4 = 0LL;
while ( 1 )
{
if ( !strcmp(*v2, a2) )
{
if ( v4 )
v4[2] = v3;
else
*(_QWORD *)(a1 + 16) = v3;
free(v2);
}
v5 = (const char **)v2[2];
v3 = v5[2];
v4 = v2;
if ( !v3 )
break;
v2 = (const char **)v2[2];
}
if ( !strcmp(*v5, a2) )
{
free(v5);
v2[2] = 0LL;
}
}
else if ( !strcmp(*v2, a2) )
{
*(_QWORD *)(a1 + 16) = 0LL;
}
}
}

original_code:
void remove_xct(Group *group, const char *user_name) {
    Xct *currentXct = group->xcts;
    Xct *prevXct = ((void *)0);
    if ( currentXct ) {
        while ( currentXct->next ) {
            if ( strcmp(currentXct->name, user_name) == 0 ) {
                if ( prevXct == ((void *)0) ) {
                    group->xcts = currentXct->next;
                } else {
                    prevXct->next = currentXct->next;
                }
                free(currentXct);
            }
            prevXct = currentXct;
            currentXct = currentXct->next;
        }
        if ( strcmp(currentXct->name, user_name) == 0 ) {
            if ( prevXct == ((void *)0) ) {
                group->xcts = ((void *)0);
            } else {
                free(currentXct);
                prevXct->next = ((void *)0);
            }
        }
    }
}

canonical_original_code:
void remove_xct(struct group *group, char *user_name) {
    struct xct *currentXct = group->xcts;
    struct xct *prevXct = ((void *)0);
    if ( currentXct ) {
        while ( currentXct->next ) {
            if ( strcmp(currentXct->name, user_name) == 0 ) {
                if ( prevXct == ((void *)0) ) {
                    group->xcts = currentXct->next;
                } else {
                    prevXct->next = currentXct->next;
                }
                free(currentXct);
            }
            prevXct = currentXct;
            currentXct = currentXct->next;
        }
        if ( strcmp(currentXct->name, user_name) == 0 ) {
            if ( prevXct == ((void *)0) ) {
                group->xcts = ((void *)0);
            } else {
                free(currentXct);
                prevXct->next = ((void *)0);
            }
        }
    }
}


decompiled_code:
__int64 __fastcall remove_user(__int64 a1, const char *a2)
{
const char **v2; // rax
const char **v3; // rbx
const char **v5; // rax

if ( !*(_QWORD *)(a1 + 8) )
return 0xFFFFFFFFLL;
v2 = find_prev_user(a1, a2);
v3 = v2;
if ( !v2 )
return 0xFFFFFFFFLL;
if ( !strcmp(*v2, a2) )
{
*(_QWORD *)(a1 + 8) = v3[2];
}
else
{
v5 = (const char **)v3[2];
v3[2] = v5[2];
v3 = v5;
}
free(v3);
remove_xct(a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, const char *a2)
{
const char **v2; // rax
const char **v3; // rbx
const char **v5; // rax

if ( !*(_QWORD *)(a1 + 8) )
return 0xFFFFFFFFLL;
v2 = func5(a1, a2);
v3 = v2;
if ( !v2 )
return 0xFFFFFFFFLL;
if ( !strcmp(*v2, a2) )
{
*(_QWORD *)(a1 + 8) = v3[2];
}
else
{
v5 = (const char **)v3[2];
v3[2] = v5[2];
v3 = v5;
}
free(v3);
func12(a1, a2);
return 0LL;
}

original_code:
int remove_user(Group *group, const char *user_name) {
    User *currentUser = group->users;
    User *prevUser = ((void *)0);
    if ( currentUser ) {
        prevUser = find_prev_user(group, user_name);
        if ( prevUser ) {
            if ( strcmp(prevUser->name, user_name) == 0 ) {
                currentUser = prevUser;
                if ( currentUser->next != ((void *)0) ) {
                    group->users = currentUser->next;
                } else {
                    group->users = ((void *)0);
                }
            } else {
                currentUser = prevUser->next;
                prevUser->next = currentUser->next;
            }
            currentUser->next = ((void *)0);
            free(currentUser);
            remove_xct(group, user_name);
            return 0;
        } else return -1;
    } else return -1;
}

canonical_original_code:
int remove_user(struct group *group, char *user_name) {
    struct user *currentUser = group->users;
    struct user *prevUser = ((void *)0);
    if ( currentUser ) {
        prevUser = find_prev_user(group, user_name);
        if ( prevUser ) {
            if ( strcmp(prevUser->name, user_name) == 0 ) {
                currentUser = prevUser;
                if ( currentUser->next != ((void *)0) ) {
                    group->users = currentUser->next;
                } else {
                    group->users = ((void *)0);
                }
            } else {
                currentUser = prevUser->next;
                prevUser->next = currentUser->next;
            }
            currentUser->next = ((void *)0);
            free(currentUser);
            remove_xct(group, user_name);
            return 0;
        } else return -1;
    } else return -1;
}


error

decompiled_code:
int __fastcall error(const char *a1)
{
return fprintf(stderr, "Error: %s\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(stderr, "Error: %s\n", a1);
}

original_code:
void error(const char *msg) {
    fprintf(stderr, "Error: %s\n", msg);
}

canonical_original_code:
void error(char *msg) {
    fprintf(stderr, "Error: %s\n", msg);
}

variable_types:
msg
char *