decompiled_code:
__int64 __fastcall mg_open_device(char *file, char a2)
{
int v2; // ebx
__int64 result; // rax
int v4; // eax

if ( a2 )
{
while ( 1 )
{
lprintf(4LL, "mod: blocking-open(%s)", file);
v2 = open(file, 258);
if ( v2 >= 0 )
break;
if ( *_errno_location() != 11 )
goto LABEL_4;
}
}
else
{
v4 = open(file, 2306);
v2 = v4;
if ( v4 < 0 )
{
LABEL_4:
lprintf(0LL, "mod: cannot open line %s", file);
return 0xFFFFFFFFLL;
}
fcntl(v4, 4, 2LL);
}
if ( v2 > 0 && (close(0), dup(v2)) )
{
lprintf(0LL, "mod: cannot make %s stdin", file);
result = 0xFFFFFFFFLL;
}
else
{
close(1);
close(2);
if ( dup(0) == 1 )
{
if ( dup(0) == 2 )
{
if ( v2 > 2 )
close(v2);
setbuf(stdin, 0LL);
setbuf(stdout, 0LL);
setbuf(stderr, 0LL);
result = 0LL;
}
else
{
lprintf(0LL, "mod: cannot dup to stderr");
result = 0xFFFFFFFFLL;
}
}
else
{
lprintf(0LL, "mod: cannot dup to stdout");
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *file, char a2)
{
int v2; // ebx
__int64 result; // rax
int v4; // eax

if ( a2 )
{
while ( 1 )
{
lprintf(4LL, "mod: blocking-open(%s)", file);
v2 = open(file, 258);
if ( v2 >= 0 )
break;
if ( *_errno_location() != 11 )
goto LABEL_4;
}
}
else
{
v4 = open(file, 2306);
v2 = v4;
if ( v4 < 0 )
{
LABEL_4:
lprintf(0LL, "mod: cannot open line %s", file);
return 0xFFFFFFFFLL;
}
fcntl(v4, 4, 2LL);
}
if ( v2 > 0 && (close(0), dup(v2)) )
{
lprintf(0LL, "mod: cannot make %s stdin", file);
result = 0xFFFFFFFFLL;
}
else
{
close(1);
close(2);
if ( dup(0) == 1 )
{
if ( dup(0) == 2 )
{
if ( v2 > 2 )
close(v2);
setbuf(stdin, 0LL);
setbuf(stdout, 0LL);
setbuf(stderr, 0LL);
result = 0LL;
}
else
{
lprintf(0LL, "mod: cannot dup to stderr");
result = 0xFFFFFFFFLL;
}
}
else
{
lprintf(0LL, "mod: cannot dup to stdout");
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int mg_open_device (char * devname,boolean blocking)
{
    int fd;
    if ( ! blocking )
    {
 fd = open(devname, 02 | 04000 | 0400 );
 if ( fd < 0 )
 {
     lprintf( 0, "mod: cannot open line %s", devname );
     return -1;
 }
 fcntl( fd, 4, 02);
    }
    else
    {
      again:
 lprintf( 4, "mod: blocking-open(%s)", devname );
 fd = open( devname, 02 | 0400 );
 if ( fd < 0)
 {
     if ( (*__errno_location ()) == 11 ) goto again;
     lprintf( 0, "mod: cannot open line %s", devname );
     return -1;
 }
    }
    if (fd > 0)
    {
 (void) close(0);
 if (dup(fd) != 0)
 {
     lprintf( 0, "mod: cannot make %s stdin", devname );
     return -1;
 }
    }
    (void) close(1);
    (void) close(2);
    if (dup(0) != 1)
    {
 lprintf( 0, "mod: cannot dup to stdout"); return -1;
    }
    if (dup(0) != 2)
    {
 lprintf( 0, "mod: cannot dup to stderr"); return -1;
    }
    if ( fd > 2 ) (void) close(fd);
    setbuf(stdin, (char *) ((void *)0));
    setbuf(stdout, (char *) ((void *)0));
    setbuf(stderr, (char *) ((void *)0));
    return 0;
}

canonical_original_code:
int mg_open_device(char *devname, char blocking)
{
    int fd;
    if ( ! blocking )
    {
 fd = open(devname, 02 | 04000 | 0400 );
 if ( fd < 0 )
 {
     lprintf( 0, "mod: cannot open line %s", devname );
     return -1;
 }
 fcntl( fd, 4, 02);
    }
    else
    {
      again:
 lprintf( 4, "mod: blocking-open(%s)", devname );
 fd = open( devname, 02 | 0400 );
 if ( fd < 0)
 {
     if ( (*__errno_location ()) == 11 ) goto again;
     lprintf( 0, "mod: cannot open line %s", devname );
     return -1;
 }
    }
    if (fd > 0)
    {
 (void) close(0);
 if (dup(fd) != 0)
 {
     lprintf( 0, "mod: cannot make %s stdin", devname );
     return -1;
 }
    }
    (void) close(1);
    (void) close(2);
    if (dup(0) != 1)
    {
 lprintf( 0, "mod: cannot dup to stdout"); return -1;
    }
    if (dup(0) != 2)
    {
 lprintf( 0, "mod: cannot dup to stderr"); return -1;
    }
    if ( fd > 2 ) (void) close(fd);
    setbuf(stdin, (char *) ((void *)0));
    setbuf(stdout, (char *) ((void *)0));
    setbuf(stderr, (char *) ((void *)0));
    return 0;
}

variable_types:
devname
char *

variable_types: blocking char


repo: Distrotech/mgetty

decompiled_code:
__int64 __fastcall mg_init_device(int fd, char a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // ebx
int v8[4]; // [rsp+0h] [rbp-A8h] BYREF
int v9; // [rsp+10h] [rbp-98h]
char v10[88]; // [rsp+50h] [rbp-58h] BYREF

if ( a2 )
{
lprintf(4LL, "lowering DTR to reset Modem");
tio_toggle_dtr((unsigned int)fd, a3);
}
v8[0] = 0;
if ( ioctl(fd, 0x541AuLL, v8) < 0 )
lprintf(1LL, "cannot turn off soft carrier");
v5 = tio_get((unsigned int)fd, v10);
if ( v5 == -1 )
{
lprintf(0LL, "cannot get TIO");
}
else
{
tio_mode_sane(v10, 1LL);
tio_set_speed(v10, a4);
tio_default_cc(v10);
tio_mode_raw(v10);
tio_set_flow_control(0LL, v10, 1LL);
v5 = tio_set(0LL, v10);
if ( v5 == -1 )
{
lprintf(0LL, "cannot set TIO");
}
else
{
v5 = 0;
if ( a4 == 38400 )
{
if ( !ioctl(0, 0x541EuLL, v8) && (v9 & 0x1030) != 0 )
lprintf(3LL, "WARNING: obsolete setserial spd_hi/spd_vhi used, 38400 is not real port speed");
v5 = 0;
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, char a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // ebx
int v8[4]; // [rsp+0h] [rbp-A8h] BYREF
int v9; // [rsp+10h] [rbp-98h]
char v10[88]; // [rsp+50h] [rbp-58h] BYREF

if ( a2 )
{
lprintf(4LL, "lowering DTR to reset Modem");
tio_toggle_dtr((unsigned int)fd, a3);
}
v8[0] = 0;
if ( ioctl(fd, 0x541AuLL, v8) < 0 )
lprintf(1LL, "cannot turn off soft carrier");
v5 = tio_get((unsigned int)fd, v10);
if ( v5 == -1 )
{
lprintf(0LL, "cannot get TIO");
}
else
{
tio_mode_sane(v10, 1LL);
tio_set_speed(v10, a4);
tio_default_cc(v10);
tio_mode_raw(v10);
tio_set_flow_control(0LL, v10, 1LL);
v5 = tio_set(0LL, v10);
if ( v5 == -1 )
{
lprintf(0LL, "cannot set TIO");
}
else
{
v5 = 0;
if ( a4 == 38400 )
{
if ( !ioctl(0, 0x541EuLL, v8) && (v9 & 0x1030) != 0 )
lprintf(3LL, "WARNING: obsolete setserial spd_hi/spd_vhi used, 38400 is not real port speed");
v5 = 0;
}
}
}
return v5;
}

original_code:
int mg_init_device (int fd,boolean toggle_dtr,int toggle_dtr_waittime,unsigned int portspeed)
{
    TIO tio;
    if (toggle_dtr)
    {
 lprintf( 4, "lowering DTR to reset Modem" );
 tio_toggle_dtr( fd, toggle_dtr_waittime );
    }
    { int off = 0;
    if ( ioctl( fd, 0x541A, &off ) < 0 )
 lprintf( 1, "cannot turn off soft carrier" );
    }
    if ( tio_get( fd, &tio ) == -1 )
    {
 lprintf( 0, "cannot get TIO" );
 return -1;
    }
    tio_mode_sane( &tio, (1==1) );
    tio_set_speed( &tio, portspeed );
    tio_default_cc( &tio );
    tio_mode_raw( &tio );
    tio_set_flow_control( 0, &tio, 0x01 );
    if ( tio_set( 0, &tio ) == -1 )
    {
 lprintf( 0, "cannot set TIO" );
 return -1;
    }
    if ( portspeed == 38400 )
    {
 struct serial_struct serinfo;
 if ( ioctl( 0, 0x541E, &serinfo ) == 0 &&
      ( serinfo.flags & ((1U << 4)|(1U << 5)|(1U << 12)) ) != 0 )
 {
     lprintf( 3, "WARNING: obsolete setserial spd_hi/spd_vhi used, 38400 is not real port speed" );
 }
    }
    return 0;
}

canonical_original_code:
int mg_init_device(int fd, char toggle_dtr, int toggle_dtr_waittime, unsigned int portspeed)
{
    struct termios tio;
    if (toggle_dtr)
    {
 lprintf( 4, "lowering DTR to reset Modem" );
 tio_toggle_dtr( fd, toggle_dtr_waittime );
    }
    { int off = 0;
    if ( ioctl( fd, 0x541A, &off ) < 0 )
 lprintf( 1, "cannot turn off soft carrier" );
    }
    if ( tio_get( fd, &tio ) == -1 )
    {
 lprintf( 0, "cannot get TIO" );
 return -1;
    }
    tio_mode_sane( &tio, (1==1) );
    tio_set_speed( &tio, portspeed );
    tio_default_cc( &tio );
    tio_mode_raw( &tio );
    tio_set_flow_control( 0, &tio, 0x01 );
    if ( tio_set( 0, &tio ) == -1 )
    {
 lprintf( 0, "cannot set TIO" );
 return -1;
    }
    if ( portspeed == 38400 )
    {
 struct serial_struct serinfo;
 if ( ioctl( 0, 0x541E, &serinfo ) == 0 &&
      ( serinfo.flags & ((1U << 4)|(1U << 5)|(1U << 12)) ) != 0 )
 {
     lprintf( 3, "WARNING: obsolete setserial spd_hi/spd_vhi used, 38400 is not real port speed" );
 }
    }
    return 0;
}

variable_types: fd int

variable_types:
portspeed
unsigned int

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

variable_types: off int

variable_types: struct serial_struct { int type; int line; unsigned int port; int irq; int flags; int xmit_fifo_size; int custom_divisor; int baud_base; unsigned short close_delay; char io_type; char reserved_char[1]; int hub6; unsigned short closing_wait; unsigned short closing_wait2; unsigned char *iomem_base; unsigned short iomem_reg_shift; unsigned int port_high; unsigned long iomap_base; }

return_type: int


decompiled_code:
__int64 __fastcall mg_get_device(char *file, char a2, char a3, unsigned int a4, unsigned int a5)
{
char i; // r15
unsigned int v8; // ebx
int v9; // eax
char v10; // bh

delay(500LL);
for ( i = 1; ; i = 0 )
{
v8 = mg_open_device(file, a2);
if ( v8 == -1 )
{
lprintf(0LL, "open device %s failed", file);
return v8;
}
v9 = tio_get_rs232_lines(0LL);
v10 = BYTE1(v9);
if ( v9 != -1 )
{
if ( (v9 & 0x40) != 0 )
lprintf(4LL, "WARNING: DCD line still active, check modem settings (AT&Dx)");
if ( (v10 & 1) == 0 )
lprintf(3LL, "WARNING: DSR is off - modem turned off or bad cable?");
}
v8 = mg_init_device(0, a3, a4, a5);
if ( v8 != -1 )
break;
if ( !i )
{
lprintf(0LL, "mg_init_device failed, exiting");
return v8;
}
lprintf(3LL, "mg_init_device failed, trying again");
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *file, char a2, char a3, unsigned int a4, unsigned int a5)
{
char i; // r15
unsigned int v8; // ebx
int v9; // eax
char v10; // bh

delay(500LL);
for ( i = 1; ; i = 0 )
{
v8 = func3(file, a2);
if ( v8 == -1 )
{
lprintf(0LL, "open device %s failed", file);
return v8;
}
v9 = tio_get_rs232_lines(0LL);
v10 = BYTE1(v9);
if ( v9 != -1 )
{
if ( (v9 & 0x40) != 0 )
lprintf(4LL, "WARNING: DCD line still active, check modem settings (AT&Dx)");
if ( (v10 & 1) == 0 )
lprintf(3LL, "WARNING: DSR is off - modem turned off or bad cable?");
}
v8 = func4(0, a3, a4, a5);
if ( v8 != -1 )
break;
if ( !i )
{
lprintf(0LL, "mg_init_device failed, exiting");
return v8;
}
lprintf(3LL, "mg_init_device failed, trying again");
}
return 0;
}

original_code:
int mg_get_device (char * devname,boolean blocking_open,boolean toggle_dtr,int toggle_dtr_waittime,unsigned int portspeed)
{
    boolean first_try = (1==1);
    int rs_lines;
    delay(500);
try_again:
    if ( mg_open_device( devname, blocking_open ) == -1 )
    {
 lprintf( 0, "open device %s failed", devname );
 return -1;
    }
    rs_lines = tio_get_rs232_lines(0);
    if ( rs_lines != -1 )
    {
 if ( rs_lines & 0x040 )
     lprintf( 4, "WARNING: DCD line still active, check modem settings (AT&Dx)" );
 if ( ! (rs_lines & 0x100) )
     lprintf( 3, "WARNING: DSR is off - modem turned off or bad cable?" );
    }
    if ( mg_init_device( 0, toggle_dtr, toggle_dtr_waittime,
    portspeed ) == -1 )
    {
 if ( first_try )
 {
     lprintf( 3, "mg_init_device failed, trying again" );
     first_try = (1==0); goto try_again;
 }
 lprintf( 0, "mg_init_device failed, exiting" );
 return -1;
    }
    return 0;
}

canonical_original_code:
int mg_get_device(char *devname, char blocking_open, char toggle_dtr, int toggle_dtr_waittime, unsigned int portspeed)
{
    char first_try = (1==1);
    int rs_lines;
    delay(500);
try_again:
    if ( mg_open_device( devname, blocking_open ) == -1 )
    {
 lprintf( 0, "open device %s failed", devname );
 return -1;
    }
    rs_lines = tio_get_rs232_lines(0);
    if ( rs_lines != -1 )
    {
 if ( rs_lines & 0x040 )
     lprintf( 4, "WARNING: DCD line still active, check modem settings (AT&Dx)" );
 if ( ! (rs_lines & 0x100) )
     lprintf( 3, "WARNING: DSR is off - modem turned off or bad cable?" );
    }
    if ( mg_init_device( 0, toggle_dtr, toggle_dtr_waittime,
    portspeed ) == -1 )
    {
 if ( first_try )
 {
     lprintf( 3, "mg_init_device failed, trying again" );
     first_try = (1==0); goto try_again;
 }
 lprintf( 0, "mg_init_device failed, exiting" );
 return -1;
    }
    return 0;
}


decompiled_code:
int __fastcall mg_get_ctty(int fd)
{
int result; // eax
__pid_t v2; // edx

if ( setsid() == -1 && *_errno_location() != 1 )
lprintf(1LL, "cannot make myself session leader (setsid)");
result = ioctl(fd, 0x540EuLL, 0LL);
if ( result )
{
lprintf(1LL, "cannot set controlling tty (ioctl)");
v2 = getppid();
result = -1;
if ( v2 != 1 )
{
lprintf(3LL, ">>> this might be caused because you have run mgetty/vgetty");
lprintf(3LL, ">>> from the command line. Don't do that, use /etc/inittab!");
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd)
{
int result; // eax
__pid_t v2; // edx

if ( setsid() == -1 && *_errno_location() != 1 )
lprintf(1LL, "cannot make myself session leader (setsid)");
result = ioctl(fd, 0x540EuLL, 0LL);
if ( result )
{
lprintf(1LL, "cannot set controlling tty (ioctl)");
v2 = getppid();
result = -1;
if ( v2 != 1 )
{
lprintf(3LL, ">>> this might be caused because you have run mgetty/vgetty");
lprintf(3LL, ">>> from the command line. Don't do that, use /etc/inittab!");
result = -1;
}
}
return result;
}

original_code:
int mg_get_ctty (int fd,char * devname)
{
    if ( setsid() == -1 && (*__errno_location ()) != 1 )
    {
 lprintf( 1, "cannot make myself session leader (setsid)" );
    }
    if ( ioctl( fd, 0x540E, ((void *)0) ) != 0 )
    {
 lprintf( 1, "cannot set controlling tty (ioctl)" );
 if ( getppid() != 1 )
 {
     lprintf( 3, ">>> this might be caused because you have run mgetty/vgetty" );
     lprintf( 3, ">>> from the command line.  Don't do that, use /etc/inittab!" );
 }
 return -1;
    }
    return 0;
}

canonical_original_code:
int mg_get_ctty(int fd, char *devname)
{
    if ( setsid() == -1 && (*__errno_location ()) != 1 )
    {
 lprintf( 1, "cannot make myself session leader (setsid)" );
    }
    if ( ioctl( fd, 0x540E, ((void *)0) ) != 0 )
    {
 lprintf( 1, "cannot set controlling tty (ioctl)" );
 if ( getppid() != 1 )
 {
     lprintf( 3, ">>> this might be caused because you have run mgetty/vgetty" );
     lprintf( 3, ">>> from the command line.  Don't do that, use /etc/inittab!" );
 }
 return -1;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall tio_get(int a1, struct termios *a2)
{
if ( tcgetattr(a1, a2) >= 0 )
return 0LL;
lprintf(1LL, "tcgetattr failed");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, struct termios *a2)
{
if ( tcgetattr(a1, a2) >= 0 )
return 0LL;
lprintf(1LL, "tcgetattr failed");
return 0xFFFFFFFFLL;
}

original_code:
int tio_get (int fd,TIO *t)
{
    if ( tcgetattr( fd, t ) < 0 )
    {
 lprintf( 1, "tcgetattr failed" ); return -1;
    }
    return 0;
}

canonical_original_code:
int tio_get(int fd, struct termios *t)
{
    if ( tcgetattr( fd, t ) < 0 )
    {
 lprintf( 1, "tcgetattr failed" ); return -1;
    }
    return 0;
}

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


decompiled_code:
__int64 __fastcall tio_set(int a1, const struct termios *a2)
{
if ( tcsetattr(a1, 0, a2) >= 0 )
return 0LL;
lprintf(1LL, "tcsetattr failed");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, const struct termios *a2)
{
if ( tcsetattr(a1, 0, a2) >= 0 )
return 0LL;
lprintf(1LL, "tcsetattr failed");
return 0xFFFFFFFFLL;
}

original_code:
int tio_set (int fd,TIO * t)
{
    if ( tcsetattr( fd, 0, t ) < 0 )
    {
 lprintf( 1, "tcsetattr failed" ); return -1;
    }
    return 0;
}

canonical_original_code:
int tio_set(int fd, struct termios *t)
{
    if ( tcsetattr( fd, 0, t ) < 0 )
    {
 lprintf( 1, "tcsetattr failed" ); return -1;
    }
    return 0;
}

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


decompiled_code:
__int64 __fastcall tio_check_speed(unsigned int a1)
{
speed_t v1; // edx
int *v2; // rax
__int64 result; // rax

v1 = speedtab;
if ( speedtab )
{
v2 = &dword_724;
while ( *v2 != a1 )
{
v2 += 4;
v1 = *(v2 - 1);
if ( !v1 )
goto LABEL_5;
}
result = v1;
}
else
{
LABEL_5:
lprintf(5LL, "speed %d not found in table", a1);
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
speed_t v1; // edx
int *v2; // rax
__int64 result; // rax

v1 = speedtab;
if ( speedtab )
{
v2 = &dword_724;
while ( *v2 != a1 )
{
v2 += 4;
v1 = *(v2 - 1);
if ( !v1 )
goto LABEL_5;
}
result = v1;
}
else
{
LABEL_5:
lprintf(5LL, "speed %d not found in table", a1);
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int tio_check_speed (int speed)
{
    int i;
    for ( i=0; speedtab[i].cbaud != 0; i++ )
    {
 if ( speedtab[i].nspeed == speed )
 {
     return speedtab[i].cbaud;
 }
    }
    lprintf( 5, "speed %d not found in table", speed );
    (*__errno_location ())=22;
    return -1;
}

canonical_original_code:
int tio_check_speed(int speed)
{
    int i;
    for ( i=0; speedtab[i].cbaud != 0; i++ )
    {
 if ( speedtab[i].nspeed == speed )
 {
     return speedtab[i].cbaud;
 }
    }
    lprintf( 5, "speed %d not found in table", speed );
    (*__errno_location ())=22;
    return -1;
}

variable_types: speed int

variable_types: i int


decompiled_code:
__int64 __fastcall tio_set_speed(struct termios *termios_p, unsigned int a2)
{
speed_t v2; // ebx
int *v3; // rax
__int64 result; // rax

v2 = speedtab;
if ( !speedtab )
goto LABEL_5;
v3 = &dword_724;
while ( *v3 != a2 )
{
v3 += 4;
v2 = *(v3 - 1);
if ( !v2 )
goto LABEL_5;
}
if ( v2 )
{
lprintf(5LL, "tss: set speed to %d (%03o)", a2, v2);
cfsetospeed(termios_p, v2);
cfsetispeed(termios_p, v2);
result = 0LL;
}
else
{
LABEL_5:
*_errno_location() = 22;
lprintf(1LL, "tss: unknown/unsupported bit rate: %d", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(struct termios *termios_p, unsigned int a2)
{
speed_t v2; // ebx
int *v3; // rax
__int64 result; // rax

v2 = speedtab;
if ( !speedtab )
goto LABEL_5;
v3 = &dword_724;
while ( *v3 != a2 )
{
v3 += 4;
v2 = *(v3 - 1);
if ( !v2 )
goto LABEL_5;
}
if ( v2 )
{
lprintf(5LL, "tss: set speed to %d (%03o)", a2, v2);
cfsetospeed(termios_p, v2);
cfsetispeed(termios_p, v2);
result = 0LL;
}
else
{
LABEL_5:
*_errno_location() = 22;
lprintf(1LL, "tss: unknown/unsupported bit rate: %d", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int tio_set_speed (TIO *t,unsigned int speed)
{
    int i, symspeed=0;
    for ( i=0; speedtab[i].cbaud != 0; i++ )
    {
 if ( speedtab[i].nspeed == speed )
  { symspeed = speedtab[i].cbaud; break; }
    }
    if ( symspeed == 0 )
    {
 (*__errno_location ())=22;
 lprintf( 1, "tss: unknown/unsupported bit rate: %d", speed );
 return -1;
    }
    lprintf( 5, "tss: set speed to %d (%03o)", speed, symspeed );
    cfsetospeed( t, symspeed );
    cfsetispeed( t, symspeed );
    return 0;
}

canonical_original_code:
int tio_set_speed(struct termios *t, unsigned int speed)
{
    int i; int symspeed = 0;
    for ( i=0; speedtab[i].cbaud != 0; i++ )
    {
 if ( speedtab[i].nspeed == speed )
  { symspeed = speedtab[i].cbaud; break; }
    }
    if ( symspeed == 0 )
    {
 (*__errno_location ())=22;
 lprintf( 1, "tss: unknown/unsupported bit rate: %d", speed );
 return -1;
    }
    lprintf( 5, "tss: set speed to %d (%03o)", speed, symspeed );
    cfsetospeed( t, symspeed );
    cfsetispeed( t, symspeed );
    return 0;
}

variable_types:
speed
unsigned int

variable_types: symspeed int


decompiled_code:
__int64 __fastcall tio_get_speed(const struct termios *a1)
{
speed_t v1; // eax
unsigned int v2; // edx
speed_t v3; // ecx
speed_t *v4; // rax

v1 = cfgetospeed(a1);
v2 = dword_724;
if ( dword_724 )
{
v3 = v1;
v4 = &speedtab;
do
{
if ( *v4 == v3 )
break;
v4 += 4;
v2 = v4[1];
}
while ( v2 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(const struct termios *a1)
{
speed_t v1; // eax
unsigned int v2; // edx
speed_t v3; // ecx
speed_t *v4; // rax

v1 = cfgetospeed(a1);
v2 = dword_724;
if ( dword_724 )
{
v3 = v1;
v4 = &speedtab;
do
{
if ( *v4 == v3 )
break;
v4 += 4;
v2 = v4[1];
}
while ( v2 );
}
return v2;
}

original_code:
int tio_get_speed (TIO *t)
{
    speed_t cbaud = cfgetospeed( t );
    struct speedtab * p;
    for ( p=speedtab; p->nspeed != 0; p++ )
    {
 if ( p->cbaud == cbaud ) break;
    }
    return p->nspeed;
}

canonical_original_code:
int tio_get_speed(struct termios *t)
{
    unsigned int cbaud = cfgetospeed( t );
    struct speedtab *p;
    for ( p=speedtab; p->nspeed != 0; p++ )
    {
 if ( p->cbaud == cbaud ) break;
    }
    return p->nspeed;
}

variable_types:
cbaud
unsigned int

variable_types: struct speedtab { unsigned int cbaud; int nspeed; char *speed; } *


return_type: void


variable_types: local int


decompiled_code:
__int16 __fastcall tio_carrier(__int64 a1, int a2)
{
int v2; // eax

v2 = *(_DWORD *)(a1 + 8);
if ( a2 )
BYTE1(v2) &= 0xF7u;
else
BYTE1(v2) |= 8u;
*(_DWORD *)(a1 + 8) = v2;
return v2;
}

canonical_decompiled_code:
__int16 __fastcall func11(__int64 a1, int a2)
{
int v2; // eax

v2 = *(_DWORD *)(a1 + 8);
if ( a2 )
BYTE1(v2) &= 0xF7u;
else
BYTE1(v2) |= 8u;
*(_DWORD *)(a1 + 8) = v2;
return v2;
}

original_code:
void tio_carrier (TIO *t,int carrier_sensitive)
{
    if ( carrier_sensitive )
    {
 t->c_cflag &= ~0004000;
    }
    else
    {
 t->c_cflag |= 0004000;
    }
}

canonical_original_code:
void tio_carrier(struct termios *t, int carrier_sensitive)
{
    if ( carrier_sensitive )
    {
 t->c_cflag &= ~0004000;
    }
    else
    {
 t->c_cflag |= 0004000;
    }
}


variable_types: type int


decompiled_code:
__int64 __fastcall tio_set_flow_control2(unsigned int a1, char a2)
{
__int64 result; // rax
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

result = tio_get(a1, &v3);
if ( (_DWORD)result != -1 )
{
tio_set_flow_control(a1, (int *)&v3, a2);
result = tio_set(a1, &v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int a1, char a2)
{
__int64 result; // rax
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

result = func0(a1, &v3);
if ( (_DWORD)result != -1 )
{
func12(a1, (int *)&v3, a2);
result = func1(a1, &v3);
}
return result;
}

original_code:
int tio_set_flow_control2 (int fd,int type)
{
TIO t;
    if ( tio_get( fd, &t ) == -1 ) return -1;
    tio_set_flow_control( fd, &t, type );
    return tio_set( fd, &t );
}

canonical_original_code:
int tio_set_flow_control2(int fd, int type)
{
struct termios t;
    if ( tio_get( fd, &t ) == -1 ) return -1;
    tio_set_flow_control( fd, &t, type );
    return tio_set( fd, &t );
}


decompiled_code:
__int64 __fastcall tio_toggle_dtr(int fd, unsigned int a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3[0] = 2;
if ( ioctl(fd, 0x5417uLL, v3) < 0 )
{
lprintf(1LL, "TIOCMBIC failed");
result = 0xFFFFFFFFLL;
}
else
{
delay(a2);
if ( ioctl(fd, 0x5416uLL, v3) < 0 )
{
lprintf(1LL, "TIOCMBIS failed");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(int fd, unsigned int a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3[0] = 2;
if ( ioctl(fd, 0x5417uLL, v3) < 0 )
{
lprintf(1LL, "TIOCMBIC failed");
result = 0xFFFFFFFFLL;
}
else
{
delay(a2);
if ( ioctl(fd, 0x5416uLL, v3) < 0 )
{
lprintf(1LL, "TIOCMBIS failed");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int tio_toggle_dtr (int fd,int msec_wait)
{
    int mctl = 0x002;
    if ( ioctl( fd, 0x5417, &mctl ) < 0 )
    {
 lprintf( 1, "TIOCMBIC failed" ); return -1;
    }
    delay( msec_wait );
    if ( ioctl( fd, 0x5416, &mctl ) < 0 )
    {
 lprintf( 1, "TIOCMBIS failed" ); return -1;
    }
    return 0;
}

canonical_original_code:
int tio_toggle_dtr(int fd, int msec_wait)
{
    int mctl = 0x002;
    if ( ioctl( fd, 0x5417, &mctl ) < 0 )
    {
 lprintf( 1, "TIOCMBIC failed" ); return -1;
    }
    delay( msec_wait );
    if ( ioctl( fd, 0x5416, &mctl ) < 0 )
    {
 lprintf( 1, "TIOCMBIS failed" ); return -1;
    }
    return 0;
}

variable_types: mctl int


decompiled_code:
__int64 __fastcall tio_flush_queue(int a1, int a2)
{
unsigned int v2; // ebx

switch ( a2 )
{
case 2:
v2 = tcflush(a1, 1);
break;
case 3:
v2 = tcflush(a1, 2);
break;
case 1:
v2 = tcflush(a1, 0);
break;
default:
lprintf(3LL, "tio_flush_queue: invalid ``queue'' argument");
return (unsigned int)-1;
}
if ( v2 )
lprintf(1LL, "tio: cannot flush queue");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1, int a2)
{
unsigned int v2; // ebx

switch ( a2 )
{
case 2:
v2 = tcflush(a1, 1);
break;
case 3:
v2 = tcflush(a1, 2);
break;
case 1:
v2 = tcflush(a1, 0);
break;
default:
lprintf(3LL, "tio_flush_queue: invalid ``queue'' argument");
return (unsigned int)-1;
}
if ( v2 )
lprintf(1LL, "tio: cannot flush queue");
return v2;
}

original_code:
int tio_flush_queue (int fd,int queue)
{
    int r = 0;
    switch( queue )
    {
      case 0x01: r = tcflush( fd, 0 ); break;
      case 0x02: r = tcflush( fd, 1 ); break;
      case ( 0x01 | 0x02 ): r = tcflush( fd, 2 );break;
      default:
 lprintf( 3, "tio_flush_queue: invalid ``queue'' argument" );
 return -1;
    }
    if ( r != 0 ) lprintf( 1, "tio: cannot flush queue" );
    return r;
}

canonical_original_code:
int tio_flush_queue(int fd, int queue)
{
    int r = 0;
    switch( queue )
    {
      case 0x01: r = tcflush( fd, 0 ); break;
      case 0x02: r = tcflush( fd, 1 ); break;
      case ( 0x01 | 0x02 ): r = tcflush( fd, 2 );break;
      default:
 lprintf( 3, "tio_flush_queue: invalid ``queue'' argument" );
 return -1;
    }
    if ( r != 0 ) lprintf( 1, "tio: cannot flush queue" );
    return r;
}

variable_types: queue int

variable_types: r int


decompiled_code:
__int64 __fastcall tio_flow(int a1, int a2)
{
unsigned int v2; // ebx

if ( a2 )
v2 = tcflow(a1, 1);
else
v2 = tcflow(a1, 0);
if ( v2 )
lprintf(1LL, "tio: cannot change flow ctrl state");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1, int a2)
{
unsigned int v2; // ebx

if ( a2 )
v2 = tcflow(a1, 1);
else
v2 = tcflow(a1, 0);
if ( v2 )
lprintf(1LL, "tio: cannot change flow ctrl state");
return v2;
}

original_code:
int tio_flow (int fd,int restart_output)
{
    int r;
    if ( restart_output ) r = tcflow( fd, 1 );
                     else r = tcflow( fd, 0 );
    if ( r != 0 ) lprintf( 1, "tio: cannot change flow ctrl state" );
    return r;
}

canonical_original_code:
int tio_flow(int fd, int restart_output)
{
    int r;
    if ( restart_output ) r = tcflow( fd, 1 );
                     else r = tcflow( fd, 0 );
    if ( r != 0 ) lprintf( 1, "tio: cannot change flow ctrl state" );
    return r;
}

variable_types: fd int


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

if ( tcdrain(a1) != -1 )
return 0;
v1 = -1;
lprintf(1LL, "tio_drain: tcdrain");
return v1;
}

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

if ( tcdrain(a1) != -1 )
return 0;
v1 = -1;
lprintf(1LL, "tio_drain: tcdrain");
return v1;
}

original_code:
int tio_drain_output (int fd)
{
    if ( tcdrain( fd ) == -1 )
    {
 lprintf( 1, "tio_drain: tcdrain" ); return -1;
    }
    return 0;
}

canonical_original_code:
int tio_drain_output(int fd)
{
    if ( tcdrain( fd ) == -1 )
    {
 lprintf( 1, "tio_drain: tcdrain" ); return -1;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall tio_break(int fd)
{
lprintf(5LL, "sending system call break");
if ( tcsendbreak(fd, 0) >= 0 )
return 0LL;
lprintf(1LL, "tcsendbreak() failed");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func18(int fd)
{
lprintf(5LL, "sending system call break");
if ( tcsendbreak(fd, 0) >= 0 )
return 0LL;
lprintf(1LL, "tcsendbreak() failed");
return 0xFFFFFFFFLL;
}

original_code:
int tio_break (int fd)
{
    lprintf( 5, "sending system call break" );
    if ( tcsendbreak( fd, 0 ) < 0 )
    {
     lprintf( 1, "tcsendbreak() failed" );
     return -1;
    }
   return 0;
}

canonical_original_code:
int tio_break(int fd)
{
    lprintf( 5, "sending system call break" );
    if ( tcsendbreak( fd, 0 ) < 0 )
    {
     lprintf( 1, "tcsendbreak() failed" );
     return -1;
    }
   return 0;
}


decompiled_code:
__int64 __fastcall tio_get_rs232_lines(int a1)
{
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

if ( ioctl(a1, 0x5415uLL, &v2) < 0 )
lprintf(1LL, "tio_get_rs232_lines: TIOCMGET failed");
if ( v2 != -1 )
{
lprintf(5LL, "tio_get_rs232_lines: status:");
if ( (v2 & 4) != 0 )
lputs(5LL, " RTS");
if ( (v2 & 0x20) != 0 )
lputs(5LL, " CTS");
if ( (v2 & 0x100) != 0 )
lputs(5LL, " DSR");
if ( (v2 & 2) != 0 )
lputs(5LL, " DTR");
if ( (v2 & 0x40) != 0 )
lputs(5LL, " DCD");
if ( (v2 & 0x80u) != 0 )
lputs(5LL, " RI");
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func19(int a1)
{
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

if ( ioctl(a1, 0x5415uLL, &v2) < 0 )
lprintf(1LL, "tio_get_rs232_lines: TIOCMGET failed");
if ( v2 != -1 )
{
lprintf(5LL, "tio_get_rs232_lines: status:");
if ( (v2 & 4) != 0 )
lputs(5LL, " RTS");
if ( (v2 & 0x20) != 0 )
lputs(5LL, " CTS");
if ( (v2 & 0x100) != 0 )
lputs(5LL, " DSR");
if ( (v2 & 2) != 0 )
lputs(5LL, " DTR");
if ( (v2 & 0x40) != 0 )
lputs(5LL, " DCD");
if ( (v2 & 0x80u) != 0 )
lputs(5LL, " RI");
}
return v2;
}

original_code:
int tio_get_rs232_lines (int fd)
{
    int flags;
    if ( ioctl(fd, 0x5415, &flags ) < 0 )
     lprintf( 1, "tio_get_rs232_lines: TIOCMGET failed" );
    if ( flags != -1 )
    {
        lprintf( 5, "tio_get_rs232_lines: status:" );
        if ( flags & 0x004 ) lputs( 5, " RTS" );
        if ( flags & 0x020 ) lputs( 5, " CTS" );
        if ( flags & 0x100 ) lputs( 5, " DSR" );
        if ( flags & 0x002 ) lputs( 5, " DTR" );
        if ( flags & 0x040 ) lputs( 5, " DCD" );
        if ( flags & 0x080 ) lputs( 5, " RI" );
    }
    return flags;
}

canonical_original_code:
int tio_get_rs232_lines(int fd)
{
    int flags;
    if ( ioctl(fd, 0x5415, &flags ) < 0 )
     lprintf( 1, "tio_get_rs232_lines: TIOCMGET failed" );
    if ( flags != -1 )
    {
        lprintf( 5, "tio_get_rs232_lines: status:" );
        if ( flags & 0x004 ) lputs( 5, " RTS" );
        if ( flags & 0x020 ) lputs( 5, " CTS" );
        if ( flags & 0x100 ) lputs( 5, " DSR" );
        if ( flags & 0x002 ) lputs( 5, " DTR" );
        if ( flags & 0x040 ) lputs( 5, " DCD" );
        if ( flags & 0x080 ) lputs( 5, " RI" );
    }
    return flags;
}

variable_types: flags int


decompiled_code:
__int64 __fastcall tio_set_rs232_lines(int fd, int a2, int a3)
{
unsigned int v4; // er12
const char *v5; // rdx
const char *v6; // rdx
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v8[0] = 2;
v4 = 0;
if ( a2 != -1 )
{
if ( a2 )
{
v4 = 0;
if ( ioctl(fd, 0x5416uLL, v8) >= 0 )
goto LABEL_12;
v5 = "set";
}
else
{
v4 = 0;
if ( ioctl(fd, 0x5417uLL, v8) >= 0 )
goto LABEL_12;
v5 = "clear";
}
lprintf(1LL, "tio_set_rs232_lines: %s DTR failed", v5);
v4 = -1;
}
LABEL_12:
v8[0] = 4;
if ( a3 == -1 )
return v4;
if ( a3 )
{
if ( ioctl(fd, 0x5416uLL, v8) >= 0 )
return v4;
v6 = "set";
}
else
{
if ( ioctl(fd, 0x5417uLL, v8) >= 0 )
return v4;
v6 = "clear";
}
lprintf(1LL, "tio_set_rs232_lines: %s RTS failed", v6);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func20(int fd, int a2, int a3)
{
unsigned int v4; // er12
const char *v5; // rdx
const char *v6; // rdx
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v8[0] = 2;
v4 = 0;
if ( a2 != -1 )
{
if ( a2 )
{
v4 = 0;
if ( ioctl(fd, 0x5416uLL, v8) >= 0 )
goto LABEL_12;
v5 = "set";
}
else
{
v4 = 0;
if ( ioctl(fd, 0x5417uLL, v8) >= 0 )
goto LABEL_12;
v5 = "clear";
}
lprintf(1LL, "tio_set_rs232_lines: %s DTR failed", v5);
v4 = -1;
}
LABEL_12:
v8[0] = 4;
if ( a3 == -1 )
return v4;
if ( a3 )
{
if ( ioctl(fd, 0x5416uLL, v8) >= 0 )
return v4;
v6 = "set";
}
else
{
if ( ioctl(fd, 0x5417uLL, v8) >= 0 )
return v4;
v6 = "clear";
}
lprintf(1LL, "tio_set_rs232_lines: %s RTS failed", v6);
return (unsigned int)-1;
}

original_code:
int tio_set_rs232_lines (int fd,int do_dtr,int do_rts)
{
    int mctl, rc=0;
    mctl = 0x002;
    if ( do_dtr != -1 &&
         ioctl( fd, do_dtr? 0x5416: 0x5417, &mctl ) < 0 )
    {
 lprintf( 1, "tio_set_rs232_lines: %s DTR failed",
   do_dtr? "set": "clear" );
 rc=-1;
    }
    mctl = 0x004;
    if ( do_rts != -1 &&
         ioctl( fd, do_rts? 0x5416: 0x5417, &mctl ) < 0 )
    {
 lprintf( 1, "tio_set_rs232_lines: %s RTS failed",
   do_rts? "set": "clear" );
 rc=-1;
    }
    return rc;
}

canonical_original_code:
int tio_set_rs232_lines(int fd, int do_dtr, int do_rts)
{
    int mctl; int rc = 0;
    mctl = 0x002;
    if ( do_dtr != -1 &&
         ioctl( fd, do_dtr? 0x5416: 0x5417, &mctl ) < 0 )
    {
 lprintf( 1, "tio_set_rs232_lines: %s DTR failed",
   do_dtr? "set": "clear" );
 rc=-1;
    }
    mctl = 0x004;
    if ( do_rts != -1 &&
         ioctl( fd, do_rts? 0x5416: 0x5417, &mctl ) < 0 )
    {
 lprintf( 1, "tio_set_rs232_lines: %s RTS failed",
   do_rts? "set": "clear" );
 rc=-1;
    }
    return rc;
}

variable_types: rc int


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

reset_watchdog();
if ( a1 == 1 )
{
voice_command("AT+VLS=0", &unk_589);
result = 0LL;
}
else if ( a1 == 2 )
{
voice_command("AT+VLS=1", &unk_589);
result = 0LL;
}
else
{
lprintf(3LL, "%s: Unknown output device (%d)", (const char *)*voice_modem, a1);
result = 0xFFFFFFFFLL;
}
return result;
}

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

reset_watchdog();
if ( a1 == 1 )
{
voice_command("AT+VLS=0", &unk_589);
result = 0LL;
}
else if ( a1 == 2 )
{
voice_command("AT+VLS=1", &unk_589);
result = 0LL;
}
else
{
lprintf(3LL, "%s: Unknown output device (%d)", (const char *)*voice_modem, a1);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int Digi_RAS_set_device (int device)
     {
     reset_watchdog();
     switch (device)
          {
          case (0x0001):
               voice_command("AT+VLS=0", "OK");
               return((0));
          case (0x0002):
               voice_command("AT+VLS=1", "OK");
               return((0));
          };
     lprintf(3, "%s: Unknown output device (%d)", voice_modem->name,
      device);
     return((-1));
     }

canonical_original_code:
static int Digi_RAS_set_device(int device)
     {
     reset_watchdog();
     switch (device)
          {
          case (0x0001):
               voice_command("AT+VLS=0", "OK");
               return((0));
          case (0x0002):
               voice_command("AT+VLS=1", "OK");
               return((0));
          };
     lprintf(3, "%s: Unknown output device (%d)", voice_modem->name,
      device);
     return((-1));
     }

variable_types: device int


decompiled_code:
__int64 __fastcall Digi_RAS_set_compression(__int64 *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 v5; // rcx
__int64 result; // rax

reset_watchdog();
if ( !*a1 )
*a1 = 4LL;
v4 = *a2;
if ( *a2 )
{
if ( v4 != 8000 )
{
lprintf(3LL, "%s: Illegal sample rate (%d)", (const char *)*voice_modem, v4);
return 0xFFFFFFFFLL;
}
}
else
{
*a2 = 8000LL;
}
v5 = *a1;
if ( *a1 == 4 )
{
*a3 = 8;
result = (unsigned int)-((unsigned int)voice_command("AT+VSM=4", &unk_589) != 4096);
}
else if ( v5 == 5 )
{
*a3 = 8;
result = (unsigned int)-((unsigned int)voice_command("AT+VSM=5", &unk_589) != 4096);
}
else
{
lprintf(3LL, "%s: Illegal voice compression method (%d)", (const char *)*voice_modem, v5);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 v5; // rcx
__int64 result; // rax

reset_watchdog();
if ( !*a1 )
*a1 = 4LL;
v4 = *a2;
if ( *a2 )
{
if ( v4 != 8000 )
{
lprintf(3LL, "%s: Illegal sample rate (%d)", (const char *)*voice_modem, v4);
return 0xFFFFFFFFLL;
}
}
else
{
*a2 = 8000LL;
}
v5 = *a1;
if ( *a1 == 4 )
{
*a3 = 8;
result = (unsigned int)-((unsigned int)voice_command("AT+VSM=4", &unk_589) != 4096);
}
else if ( v5 == 5 )
{
*a3 = 8;
result = (unsigned int)-((unsigned int)voice_command("AT+VSM=5", &unk_589) != 4096);
}
else
{
lprintf(3LL, "%s: Illegal voice compression method (%d)", (const char *)*voice_modem, v5);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int Digi_RAS_set_compression (p_int *compression, p_int *speed, int *bits)
     {
     reset_watchdog();
     if (*compression == 0)
          *compression = 4;
     if (*speed == 0)
          *speed = 8000;
     if (*speed != 8000)
          {
          lprintf(3, "%s: Illegal sample rate (%d)", voice_modem->name,
           *speed);
          return((-1));
          };
     switch (*compression)
          {
          case 4:
               *bits = 8;
               if (voice_command("AT+VSM=4", "OK") != (0x1000))
                    return((-1));
               break;
          case 5:
               *bits = 8;
               if (voice_command("AT+VSM=5", "OK") != (0x1000))
                    return((-1));
               break;
          default:
               lprintf(3, "%s: Illegal voice compression method (%d)",
                voice_modem->name, *compression);
               return((-1));
          };
     return((0));
     }

canonical_original_code:
static int Digi_RAS_set_compression(long *compression, long *speed, int *bits)
     {
     reset_watchdog();
     if (*compression == 0)
          *compression = 4;
     if (*speed == 0)
          *speed = 8000;
     if (*speed != 8000)
          {
          lprintf(3, "%s: Illegal sample rate (%d)", voice_modem->name,
           *speed);
          return((-1));
          };
     switch (*compression)
          {
          case 4:
               *bits = 8;
               if (voice_command("AT+VSM=4", "OK") != (0x1000))
                    return((-1));
               break;
          case 5:
               *bits = 8;
               if (voice_command("AT+VSM=5", "OK") != (0x1000))
                    return((-1));
               break;
          default:
               lprintf(3, "%s: Illegal voice compression method (%d)",
                voice_modem->name, *compression);
               return((-1));
          };
     return((0));
     }

variable_types:
compression
long *

variable_types:
speed
long *

variable_types:
bits
int *


decompiled_code:
__int64 Digi_RAS_init()
{
char v1[264]; // [rsp+0h] [rbp-108h] BYREF

reset_watchdog();
voice_modem_state = 2;
lprintf(4LL, "initializing Digi_RAS voice modem");
sprintf(v1, "AT+VSD=%d,%d", (unsigned int)((int)*((_QWORD *)&cvd + 58) / 10 + 123), *((unsigned int *)&cvd + 110));
if ( (unsigned int)voice_command(v1, &unk_589) != 4096 )
lprintf(3LL, "setting recording preferences didn't work");
if ( *((_QWORD *)&cvd + 82) == -1LL )
*((_QWORD *)&cvd + 82) = 50LL;
sprintf(v1, "AT+VGT=%d", (unsigned int)(8 * *((_DWORD *)&cvd + 164) / 100 + 124));
if ( (unsigned int)voice_command(v1, &unk_589) != 4096 )
lprintf(3LL, "setting transmit gain didn't work");
if ( *((_QWORD *)&cvd + 79) == -1LL )
*((_QWORD *)&cvd + 79) = 50LL;
sprintf(v1, "AT+VGR=%d", (unsigned int)(8 * *((_DWORD *)&cvd + 158) / 100 + 124));
if ( (unsigned int)voice_command(v1, &unk_589) != 4096 )
lprintf(3LL, "setting receive gain didn't work");
if ( (unsigned int)voice_command("AT+VIT=0", &unk_589) != 4096 )
lprintf(3LL, "can't deactivate inactivity timer");
if ( (unsigned int)voice_command("AT+VPR=0", &unk_589) != 4096 )
lprintf(3LL, "can't select autobauding");
if ( (unsigned int)voice_command("AT+VLS=0", &unk_589) != 4096 )
lprintf(3LL, "can't deselect all input/output devices");
voice_modem_state = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
char v1[264]; // [rsp+0h] [rbp-108h] BYREF

reset_watchdog();
voice_modem_state = 2;
lprintf(4LL, "initializing Digi_RAS voice modem");
sprintf(v1, "AT+VSD=%d,%d", (unsigned int)((int)*((_QWORD *)&cvd + 58) / 10 + 123), *((unsigned int *)&cvd + 110));
if ( (unsigned int)voice_command(v1, &unk_589) != 4096 )
lprintf(3LL, "setting recording preferences didn't work");
if ( *((_QWORD *)&cvd + 82) == -1LL )
*((_QWORD *)&cvd + 82) = 50LL;
sprintf(v1, "AT+VGT=%d", (unsigned int)(8 * *((_DWORD *)&cvd + 164) / 100 + 124));
if ( (unsigned int)voice_command(v1, &unk_589) != 4096 )
lprintf(3LL, "setting transmit gain didn't work");
if ( *((_QWORD *)&cvd + 79) == -1LL )
*((_QWORD *)&cvd + 79) = 50LL;
sprintf(v1, "AT+VGR=%d", (unsigned int)(8 * *((_DWORD *)&cvd + 158) / 100 + 124));
if ( (unsigned int)voice_command(v1, &unk_589) != 4096 )
lprintf(3LL, "setting receive gain didn't work");
if ( (unsigned int)voice_command("AT+VIT=0", &unk_589) != 4096 )
lprintf(3LL, "can't deactivate inactivity timer");
if ( (unsigned int)voice_command("AT+VPR=0", &unk_589) != 4096 )
lprintf(3LL, "can't select autobauding");
if ( (unsigned int)voice_command("AT+VLS=0", &unk_589) != 4096 )
lprintf(3LL, "can't deselect all input/output devices");
voice_modem_state = 1;
return 0LL;
}

original_code:
static int Digi_RAS_init (void)
     {
     char buffer[(256)];
     reset_watchdog();
     voice_modem_state = (0x0002);
     lprintf(4, "initializing Digi_RAS voice modem");
     sprintf(buffer, "AT+VSD=%d,%d", (int) cvd.rec_silence_threshold.d.i *
      10 / 100 + 123, (int) cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting recording preferences didn't work");
     if (cvd.transmit_gain.d.i == -1)
          cvd.transmit_gain.d.i = 50;
     sprintf(buffer, "AT+VGT=%d", (int)cvd.transmit_gain.d.i * 8 / 100 + 124);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting transmit gain didn't work");
     if (cvd.receive_gain.d.i == -1)
          cvd.receive_gain.d.i = 50;
     sprintf(buffer, "AT+VGR=%d", (int)cvd.receive_gain.d.i * 8 / 100 + 124);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting receive gain didn't work");
     if (voice_command("AT+VIT=0", "OK") != (0x1000))
          lprintf(3, "can't deactivate inactivity timer");
     if (voice_command("AT+VPR=0", "OK") != (0x1000))
          lprintf(3, "can't select autobauding");
     if (voice_command("AT+VLS=0", "OK") != (0x1000))
          lprintf(3, "can't deselect all input/output devices");
     voice_modem_state = (0x0001);
     return((0));
     }

canonical_original_code:
static int Digi_RAS_init()
     {
     char buffer[-1];
     reset_watchdog();
     voice_modem_state = (0x0002);
     lprintf(4, "initializing Digi_RAS voice modem");
     sprintf(buffer, "AT+VSD=%d,%d", (int) cvd.rec_silence_threshold.d.i *
      10 / 100 + 123, (int) cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting recording preferences didn't work");
     if (cvd.transmit_gain.d.i == -1)
          cvd.transmit_gain.d.i = 50;
     sprintf(buffer, "AT+VGT=%d", (int)cvd.transmit_gain.d.i * 8 / 100 + 124);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting transmit gain didn't work");
     if (cvd.receive_gain.d.i == -1)
          cvd.receive_gain.d.i = 50;
     sprintf(buffer, "AT+VGR=%d", (int)cvd.receive_gain.d.i * 8 / 100 + 124);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting receive gain didn't work");
     if (voice_command("AT+VIT=0", "OK") != (0x1000))
          lprintf(3, "can't deactivate inactivity timer");
     if (voice_command("AT+VPR=0", "OK") != (0x1000))
          lprintf(3, "can't select autobauding");
     if (voice_command("AT+VLS=0", "OK") != (0x1000))
          lprintf(3, "can't deselect all input/output devices");
     voice_modem_state = (0x0001);
     return((0));
     }

variable_types:
buffer
char [-1]


decompiled_code:
__int64 voice_open_device()
{
const char *v0; // rdx
const char *v1; // rbp
unsigned int v2; // er12
char *v3; // rax
char *v4; // rbx
const char *v5; // rbp
size_t v6; // rax
char *v7; // rbx
char i; // al
char v10[280]; // [rsp+0h] [rbp-118h] BYREF

lprintf(4LL, "opening voice modem device");
v0 = (const char *)cvd[208];
if ( !*v0 )
{
lprintf(3LL, "no voice modem devices configured in config file");
exit(-1);
}
lprintf(5LL, "voice open '%s'", v0);
v1 = (const char *)cvd[208];
while ( 1 )
{
while ( 1 )
{
v3 = strchr(v1, 58);
v4 = v3;
strcpy(v10, "/dev/");
if ( !v3 )
{
strcat(v10, v1);
lprintf(6LL, "trying device '%s'", v10);
if ( (unsigned int)makelock(&v10[5]) )
goto LABEL_13;
v2 = open(v10, 2306);
voice_fd = v2;
if ( v2 == -1 )
{
lprintf(1LL, "error opening %s", v10);
rmlocks(1LL);
voice_fd = -1;
return v2;
}
break;
}
strncat(v10, v1, v3 - v1);
lprintf(6LL, "trying device '%s'", v10);
v1 = v4 + 1;
if ( (unsigned int)makelock(&v10[5]) )
goto LABEL_7;
voice_fd = open(v10, 2306);
if ( voice_fd != -1 )
break;
lprintf(1LL, "error opening %s", v10);
rmlocks(1LL);
voice_fd = -1;
v1 = v4 + 1;
}
lprintf(4LL, "opened voice modem device %s", v10);
v1 = v4 + 1;
if ( !v4 )
break;
LABEL_7:
if ( voice_fd != -1 )
goto LABEL_14;
}
LABEL_13:
v2 = voice_fd;
if ( voice_fd == -1 )
return v2;
LABEL_14:
v5 = v10;
if ( !memcmp(v10, "/dev/", 5uLL) )
v5 = &v10[5];
v6 = strlen(v5);
v7 = (char *)malloc(v6 + 1);
DevID = v7;
strcpy(v7, v5);
for ( i = *v7; *v7; i = *v7 )
{
if ( i == 47 )
*v7 = 45;
++v7;
}
lprintf(4LL, "reading port %s configuration from config file %s", &v10[5], (const char *)&voice_config_file);
get_config(&voice_config_file, cvd, "port", &v10[5]);
return (unsigned int)voice_init(&voice_config_file);
}

canonical_decompiled_code:
__int64 func0()
{
const char *v0; // rdx
const char *v1; // rbp
unsigned int v2; // er12
char *v3; // rax
char *v4; // rbx
const char *v5; // rbp
size_t v6; // rax
char *v7; // rbx
char i; // al
char v10[280]; // [rsp+0h] [rbp-118h] BYREF

lprintf(4LL, "opening voice modem device");
v0 = (const char *)cvd[208];
if ( !*v0 )
{
lprintf(3LL, "no voice modem devices configured in config file");
exit(-1);
}
lprintf(5LL, "voice open '%s'", v0);
v1 = (const char *)cvd[208];
while ( 1 )
{
while ( 1 )
{
v3 = strchr(v1, 58);
v4 = v3;
strcpy(v10, "/dev/");
if ( !v3 )
{
strcat(v10, v1);
lprintf(6LL, "trying device '%s'", v10);
if ( (unsigned int)makelock(&v10[5]) )
goto LABEL_13;
v2 = open(v10, 2306);
voice_fd = v2;
if ( v2 == -1 )
{
lprintf(1LL, "error opening %s", v10);
rmlocks(1LL);
voice_fd = -1;
return v2;
}
break;
}
strncat(v10, v1, v3 - v1);
lprintf(6LL, "trying device '%s'", v10);
v1 = v4 + 1;
if ( (unsigned int)makelock(&v10[5]) )
goto LABEL_7;
voice_fd = open(v10, 2306);
if ( voice_fd != -1 )
break;
lprintf(1LL, "error opening %s", v10);
rmlocks(1LL);
voice_fd = -1;
v1 = v4 + 1;
}
lprintf(4LL, "opened voice modem device %s", v10);
v1 = v4 + 1;
if ( !v4 )
break;
LABEL_7:
if ( voice_fd != -1 )
goto LABEL_14;
}
LABEL_13:
v2 = voice_fd;
if ( voice_fd == -1 )
return v2;
LABEL_14:
v5 = v10;
if ( !memcmp(v10, "/dev/", 5uLL) )
v5 = &v10[5];
v6 = strlen(v5);
v7 = (char *)malloc(v6 + 1);
DevID = v7;
strcpy(v7, v5);
for ( i = *v7; *v7; i = *v7 )
{
if ( i == 47 )
*v7 = 45;
++v7;
}
lprintf(4LL, "reading port %s configuration from config file %s", &v10[5], (const char *)&voice_config_file);
get_config(&voice_config_file, cvd, "port", &v10[5]);
return (unsigned int)voice_init(&voice_config_file);
}

original_code:
int voice_open_device(void)
     {
     char *voice_tty_start;
     char *voice_tty_end;
     char voice_tty[(256)];
     lprintf(4, "opening voice modem device");
     if (strlen(cvd.voice_devices.d.p) == 0)
          {
          lprintf(3,
           "no voice modem devices configured in config file");
          exit((-1));
          };
     lprintf(5, "voice open '%s'", cvd.voice_devices.d.p);
     voice_tty_start = cvd.voice_devices.d.p;
     do
          {
          voice_tty_end = strchr(voice_tty_start, ':');
          sprintf(voice_tty, "/dev/");
          if (voice_tty_end != ((void *)0))
               strncat(voice_tty, voice_tty_start, voice_tty_end -
                voice_tty_start);
          else
               strcat(voice_tty, voice_tty_start);
          lprintf(6, "trying device '%s'", voice_tty);
          if (makelock(&voice_tty[5]) == (0))
               {
               if ((voice_fd = open(voice_tty, 02 | 04000 | 0400)) == (-1))
                    {
                    lprintf(1, "error opening %s", voice_tty);
                    rmlocks();
                    voice_fd = (-1);
                    }
               else
                    lprintf(4, "opened voice modem device %s", voice_tty);
               };
          voice_tty_start = voice_tty_end + 1;
          }
     while ((voice_tty_end != ((void *)0)) && (voice_fd == (-1)));
     if (voice_fd != (-1))
          {
   char * p = voice_tty;
   if ( strncmp( p, "/dev/", 5 ) == 0 ) p+=5;
          DevID = malloc(strlen(p) + 1);
          strcpy(DevID, p);
   for( p=DevID; *p; p++) if (*p == '/') *p='-';
          lprintf(4, "reading port %s configuration from config file %s",
           &voice_tty[5], voice_config_file);
          get_config(voice_config_file, (conf_data *) &cvd, "port",
           &voice_tty[5]);
          return(voice_init());
          };
     return((-1));
     }

canonical_original_code:
int voice_open_device()
     {
     char *voice_tty_start;
     char *voice_tty_end;
     char voice_tty[-1];
     lprintf(4, "opening voice modem device");
     if (strlen(cvd.voice_devices.d.p) == 0)
          {
          lprintf(3,
           "no voice modem devices configured in config file");
          exit((-1));
          };
     lprintf(5, "voice open '%s'", cvd.voice_devices.d.p);
     voice_tty_start = cvd.voice_devices.d.p;
     do
          {
          voice_tty_end = strchr(voice_tty_start, ':');
          sprintf(voice_tty, "/dev/");
          if (voice_tty_end != ((void *)0))
               strncat(voice_tty, voice_tty_start, voice_tty_end -
                voice_tty_start);
          else
               strcat(voice_tty, voice_tty_start);
          lprintf(6, "trying device '%s'", voice_tty);
          if (makelock(&voice_tty[5]) == (0))
               {
               if ((voice_fd = open(voice_tty, 02 | 04000 | 0400)) == (-1))
                    {
                    lprintf(1, "error opening %s", voice_tty);
                    rmlocks();
                    voice_fd = (-1);
                    }
               else
                    lprintf(4, "opened voice modem device %s", voice_tty);
               };
          voice_tty_start = voice_tty_end + 1;
          }
     while ((voice_tty_end != ((void *)0)) && (voice_fd == (-1)));
     if (voice_fd != (-1))
          {
   char *p = voice_tty;
   if ( strncmp( p, "/dev/", 5 ) == 0 ) p+=5;
          DevID = malloc(strlen(p) + 1);
          strcpy(DevID, p);
   for( p=DevID; *p; p++) if (*p == '/') *p='-';
          lprintf(4, "reading port %s configuration from config file %s",
           &voice_tty[5], voice_config_file);
          get_config(voice_config_file, (struct conf_data *) &cvd, "port",
           &voice_tty[5]);
          return(voice_init());
          };
     return((-1));
     }

variable_types:
p
char *

user_defined_types: struct conf_data { char *key; union { long i; void *p; } d; enum { CT_INT=0, CT_STRING=1, CT_CHAT=2, CT_BOOL=3, CT_FLOWL=4, CT_ACTION=5, CT_KEYWORD=6 } type; enum { C_EMPTY=0, C_PRESET=1, C_OVERRIDE=2, C_CONF=3, C_IGNORE=4 } flags; }


decompiled_code:
__int64 Sierra_init()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
int Sierra_init(void)
     {
     return((0));
     }

canonical_original_code:
int Sierra_init()
     {
     return((0));
     }


decompiled_code:
__int64 Sierra_voice_mode_off()
{
reset_watchdog();
if ( (unsigned int)voice_command("AT#VS0", &unk_5FC) != 4096 )
return 0xFFFFFFFFLL;
tio_set(voice_fd, &Sierra_tio_save);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
reset_watchdog();
if ( (unsigned int)voice_command("AT#VS0", &unk_5FC) != 4096 )
return 0xFFFFFFFFLL;
tio_set(voice_fd, &Sierra_tio_save);
return 0LL;
}

original_code:
int Sierra_voice_mode_off(void)
     {
     reset_watchdog();
     if (voice_command("AT#VS0", "OK") != (0x1000))
          return((-1));
     tio_set(voice_fd, &Sierra_tio_save);
     return((0));
     }

canonical_original_code:
int Sierra_voice_mode_off()
     {
     reset_watchdog();
     if (voice_command("AT#VS0", "OK") != (0x1000))
          return((-1));
     tio_set(voice_fd, &Sierra_tio_save);
     return((0));
     }


decompiled_code:
__int64 __fastcall Sierra_beep(int a1, int a2)
{
int v2; // ebx

if ( (unsigned int)voice_command("AT#VSV", "CONNECT") != 4096 )
return 0xFFFFFFFFLL;
if ( 9600 * a2 > 999 )
{
v2 = 0;
do
voice_write_char((unsigned int)(char)(127 * (int)((double)v2++ * (double)a1 / 9600.0)));
while ( 9600 * a2 / 1000 > v2 );
}
voice_write_char(16LL);
voice_write_char(3LL);
return (unsigned int)-((voice_command(&unk_61D, "OK|VCON") & 0x1000) == 0);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
int v2; // ebx

if ( (unsigned int)voice_command("AT#VSV", "CONNECT") != 4096 )
return 0xFFFFFFFFLL;
if ( 9600 * a2 > 999 )
{
v2 = 0;
do
voice_write_char((unsigned int)(char)(127 * (int)((double)v2++ * (double)a1 / 9600.0)));
while ( 9600 * a2 / 1000 > v2 );
}
voice_write_char(16LL);
voice_write_char(3LL);
return (unsigned int)-((voice_command(&unk_61D, "OK|VCON") & 0x1000) == 0);
}

original_code:
int Sierra_beep (int frequency, int length)
     {
     double freq;
     int time, i;
     char d;
     time = (9600 * length) / 1000;
     freq = (double) frequency;
     if (voice_command("AT#VSV", "CONNECT") != (0x1000))
          return((-1));
     for(i = 0; i < time; i++)
          {
          d = (char) (i * freq / 9600) * 0x7f;
          voice_write_char(d);
          }
     voice_write_char((0x10));
     voice_write_char((0x03));
     if ((voice_command("", "OK|VCON") & (0x1000)) != (0x1000))
          return((-1));
     return((0));
     }

canonical_original_code:
int Sierra_beep(int frequency, int length)
     {
     double freq;
     int time; int i;
     char d;
     time = (9600 * length) / 1000;
     freq = (double) frequency;
     if (voice_command("AT#VSV", "CONNECT") != (0x1000))
          return((-1));
     for(i = 0; i < time; i++)
          {
          d = (char) (i * freq / 9600) * 0x7f;
          voice_write_char(d);
          }
     voice_write_char((0x10));
     voice_write_char((0x03));
     if ((voice_command("", "OK|VCON") & (0x1000)) != (0x1000))
          return((-1));
     return((0));
     }

variable_types: frequency int

variable_types: length int

variable_types: freq double

variable_types: time int

variable_types: i int

variable_types: d char


decompiled_code:
__int64 __fastcall Sierra_set_device(unsigned int a1)
{
lprintf(
3LL,
"%s: set_device(%d) called. Doesn't achieve anything in this implementation",
(const char *)*voice_modem,
a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
lprintf(
3LL,
"%s: set_device(%d) called. Doesn't achieve anything in this implementation",
(const char *)*voice_modem,
a1);
return 0LL;
}

original_code:
int Sierra_set_device(int device)
     {
     lprintf(3,
      "%s: set_device(%d) called. Doesn't achieve anything in this implementation",
      voice_modem->name, device);
     return((0));
     }


decompiled_code:
__int64 __fastcall Sierra_set_compression(_QWORD *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 result; // rax

reset_watchdog();
if ( !*a1 )
*a1 = 2LL;
v4 = *a2;
if ( *a2 )
{
if ( v4 != 9600 )
{
lprintf(3LL, "%s: Illegal sample rate (%d)", (const char *)*voice_modem, v4);
return 0xFFFFFFFFLL;
}
}
else
{
*a2 = 9600LL;
}
if ( *a1 == 2LL )
{
*a3 = 8;
result = (unsigned int)-((unsigned int)voice_command("AT#VSM=2", &unk_5FC) != 4096);
}
else
{
lprintf(3LL, "%s: Illegal voice compression method (%d)", (const char *)*voice_modem, *a1);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 result; // rax

reset_watchdog();
if ( !*a1 )
*a1 = 2LL;
v4 = *a2;
if ( *a2 )
{
if ( v4 != 9600 )
{
lprintf(3LL, "%s: Illegal sample rate (%d)", (const char *)*voice_modem, v4);
return 0xFFFFFFFFLL;
}
}
else
{
*a2 = 9600LL;
}
if ( *a1 == 2LL )
{
*a3 = 8;
result = (unsigned int)-((unsigned int)voice_command("AT#VSM=2", &unk_5FC) != 4096);
}
else
{
lprintf(3LL, "%s: Illegal voice compression method (%d)", (const char *)*voice_modem, *a1);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int Sierra_set_compression(p_int *compression, p_int *speed, int *bits)
     {
     reset_watchdog();
     if (*compression == 0)
          *compression = 2;
     if (*speed == 0)
          *speed = 9600;
     if (*speed != 9600)
          {
          lprintf(3, "%s: Illegal sample rate (%d)", voice_modem->name,
           *speed);
          return((-1));
          }
     if (*compression != 2)
          {
          lprintf(3, "%s: Illegal voice compression method (%d)",
           voice_modem->name, *compression);
          return((-1));
          }
     *bits = 8;
     if (voice_command("AT#VSM=2", "OK") != (0x1000))
          return((-1));
     return((0));
     }

canonical_original_code:
static int Sierra_set_compression(long *compression, long *speed, int *bits)
     {
     reset_watchdog();
     if (*compression == 0)
          *compression = 2;
     if (*speed == 0)
          *speed = 9600;
     if (*speed != 9600)
          {
          lprintf(3, "%s: Illegal sample rate (%d)", voice_modem->name,
           *speed);
          return((-1));
          }
     if (*compression != 2)
          {
          lprintf(3, "%s: Illegal voice compression method (%d)",
           voice_modem->name, *compression);
          return((-1));
          }
     *bits = 8;
     if (voice_command("AT#VSM=2", "OK") != (0x1000))
          return((-1));
     return((0));
     }


decompiled_code:
__int64 Sierra_voice_mode_on()
{
__int64 v1[33]; // [rsp+0h] [rbp-108h] BYREF

reset_watchdog();
voice_modem_state = 2;
lprintf(4LL, "initializing Sierra voice modem");
tio_get(voice_fd, &tio);
Sierra_tio_save = (__int128)_mm_load_si128((const __m128i *)&tio);
xmmword_590 = (__int128)_mm_load_si128((const __m128i *)&xmmword_5D0);
xmmword_5A0 = (__int128)_mm_load_si128((const __m128i *)&xmmword_5E0);
qword_5B0 = qword_5F0;
dword_5B8 = dword_5F8;
if ( (unsigned int)voice_command("AT#VS1", &unk_5FC) != 4096 )
lprintf(3LL, "Voice mode didn't work");
tio_set_speed(&tio, 115200LL);
tio_set(voice_fd, &tio);
sprintf((char *)v1, "ATM2L3#VL=0#VSM=2#VSC=0#VSS=3#VSI=%1u", cvd[110]);
if ( (unsigned int)voice_command(v1, &unk_5FC) != 4096 )
lprintf(3LL, "Couldn't set voice mode options");
v1[0] = 0x303D4656235441LL;
if ( (unsigned int)voice_command(v1, &unk_5FC) != 4096 )
lprintf(3LL, "Couldn't set buffer size to %d", 255LL);
voice_modem_state = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v1[33]; // [rsp+0h] [rbp-108h] BYREF

reset_watchdog();
voice_modem_state = 2;
lprintf(4LL, "initializing Sierra voice modem");
tio_get(voice_fd, &tio);
Sierra_tio_save = (__int128)_mm_load_si128((const __m128i *)&tio);
xmmword_590 = (__int128)_mm_load_si128((const __m128i *)&xmmword_5D0);
xmmword_5A0 = (__int128)_mm_load_si128((const __m128i *)&xmmword_5E0);
qword_5B0 = qword_5F0;
dword_5B8 = dword_5F8;
if ( (unsigned int)voice_command("AT#VS1", &unk_5FC) != 4096 )
lprintf(3LL, "Voice mode didn't work");
tio_set_speed(&tio, 115200LL);
tio_set(voice_fd, &tio);
sprintf((char *)v1, "ATM2L3#VL=0#VSM=2#VSC=0#VSS=3#VSI=%1u", cvd[110]);
if ( (unsigned int)voice_command(v1, &unk_5FC) != 4096 )
lprintf(3LL, "Couldn't set voice mode options");
v1[0] = 0x303D4656235441LL;
if ( (unsigned int)voice_command(v1, &unk_5FC) != 4096 )
lprintf(3LL, "Couldn't set buffer size to %d", 255LL);
voice_modem_state = 1;
return 0LL;
}

original_code:
static int Sierra_voice_mode_on(void)
     {
     char buffer[255];
     reset_watchdog();
     voice_modem_state = (0x0002);
     lprintf(4, "initializing Sierra voice modem");
     tio_get(voice_fd, &tio);
     Sierra_tio_save = tio;
     if (voice_command("AT#VS1", "OK") != (0x1000))
          lprintf(3, "Voice mode didn't work");
     tio_set_speed(&tio, 115200);
     tio_set(voice_fd, &tio);
     sprintf(buffer, "ATM2L3#VL=0#VSM=2#VSC=0#VSS=3#VSI=%1u",
      (int)cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "Couldn't set voice mode options");
     sprintf(buffer, "AT#VF=0");
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "Couldn't set buffer size to %d", buffer_size);
     voice_modem_state = (0x0001);
     return((0));
     }

canonical_original_code:
static int Sierra_voice_mode_on()
     {
     char buffer[255];
     reset_watchdog();
     voice_modem_state = (0x0002);
     lprintf(4, "initializing Sierra voice modem");
     tio_get(voice_fd, &tio);
     Sierra_tio_save = tio;
     if (voice_command("AT#VS1", "OK") != (0x1000))
          lprintf(3, "Voice mode didn't work");
     tio_set_speed(&tio, 115200);
     tio_set(voice_fd, &tio);
     sprintf(buffer, "ATM2L3#VL=0#VSM=2#VSC=0#VSS=3#VSI=%1u",
      (int)cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "Couldn't set voice mode options");
     sprintf(buffer, "AT#VF=0");
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "Couldn't set buffer size to %d", buffer_size);
     voice_modem_state = (0x0001);
     return((0));
     }

variable_types:
buffer
char [255]


decompiled_code:
void getlog_timeout()
{
signal(14, getlog_timeout);
lprintf(3LL, "getlogname: timeout");
++timeouts;
}

canonical_decompiled_code:
void func0()
{
signal(14, getlog_timeout);
lprintf(3LL, "getlogname: timeout");
++timeouts;
}

original_code:
static RETSIGTYPE getlog_timeout()
{
    signal( 14, getlog_timeout );
    lprintf( 3, "getlogname: timeout" );
    timeouts++;
}

canonical_original_code:
static void getlog_timeout()
{
    signal( 14, getlog_timeout );
    lprintf( 3, "getlogname: timeout" );
    timeouts++;
}


strappnd

decompiled_code:
size_t __fastcall strappnd(char *s, const char *a2)
{
strcpy(s, a2);
return strlen(s);
}

canonical_decompiled_code:
size_t __fastcall func1(char *s, const char *a2)
{
strcpy(s, a2);
return strlen(s);
}

original_code:
int strappnd (char * s1,char * s2)
{
    strcpy( s1, s2 );
    return strlen( s1 );
}

canonical_original_code:
int strappnd(char *s1, char *s2)
{
    strcpy( s1, s2 );
    return strlen( s1 );
}


decompiled_code:
char *__fastcall ln_escape_prompt(char *a1)
{
char *v1; // r12
char v2; // al
int v3; // ebx
char *v5; // rbp
char v6; // r13
const char *v7; // r13
char *v8; // r12
const char *v9; // r12
unsigned int v10; // eax
unsigned int v11; // eax
struct tm *v12; // rax
char *v13[13]; // [rsp+0h] [rbp-68h] BYREF

v1 = a1;
if ( !p_2 )
{
p_2 = (char *)malloc(0x12CuLL);
if ( !p_2 )
return a1;
}
if ( !un_cached_1 )
{
uname(&un_0);
un_cached_1 = 1;
}
v2 = *a1;
if ( *a1 )
{
v3 = 0;
do
{
if ( v2 == 64 )
{
if ( v3 + strlen(un_0.nodename) + 1 > 0x12C )
break;
v3 += strappnd(&p_2[v3], un_0.nodename);
v5 = v1;
}
else if ( v2 == 92 )
{
v5 = v1 + 1;
v6 = v1[1];
switch ( v6 )
{
case 'C':
v13[0] = (char *)time(0LL);
v8 = ctime((const time_t *)v13);
if ( v3 + strlen(v8) + 1 <= 0x12C )
v3 = v3 + strappnd(&p_2[v3], v8) - 1;
break;
case 'D':
case 'T':
if ( v3 <= 270 )
{
v13[0] = (char *)time(0LL);
v12 = localtime((const time_t *)v13);
if ( v12 )
{
if ( v1[1] == 68 )
sprintf(
&p_2[v3],
"%d/%d/%d",
(unsigned int)(v12->tm_mon + 1),
(unsigned int)v12->tm_mday,
(unsigned int)(v12->tm_year + 1900));
else
sprintf(
&p_2[v3],
"%02d:%02d:%02d",
(unsigned int)v12->tm_hour,
(unsigned int)v12->tm_min,
(unsigned int)v12->tm_sec);
v3 = strlen(p_2);
}
}
else
{
v3 += strappnd(&p_2[v3], "(x)");
}
break;
case 'I':
v9 = Connect;
if ( v3 + strlen(Connect) + 1 <= 0x12C )
v3 += strappnd(&p_2[v3], v9);
break;
case 'L':
case 'P':
v7 = Device;
if ( v3 + strlen(Device) + 1 <= 0x12C )
goto LABEL_32;
break;
case 'N':
case 'U':
v10 = get_current_users();
sprintf(&p_2[v3], "%d", v10);
v3 = strlen(p_2);
break;
case 'R':
if ( v3 + strlen(un_0.release) + 1 <= 0x12C )
v3 += strappnd(&p_2[v3], un_0.release);
break;
case 'S':
tio_get(0LL, v13);
v11 = tio_get_speed(v13);
sprintf(&p_2[v3], "%d", v11);
v3 = strlen(p_2);
break;
case 'V':
if ( v3 + strlen(un_0.version) + 1 <= 0x12C )
v3 += strappnd(&p_2[v3], un_0.version);
break;
case 'Y':
v7 = CallerId;
if ( v3 + strlen(CallerId) + 1 <= 0x12C )
LABEL_32:
v3 += strappnd(&p_2[v3], v7);
break;
case 'b':
p_2[v3++] = 8;
break;
case 'f':
p_2[v3++] = 12;
break;
case 'g':
p_2[v3++] = 7;
break;
case 'm':
if ( v3 + strlen(un_0.machine) + 1 <= 0x12C )
v3 += strappnd(&p_2[v3], un_0.machine);
break;
case 'n':
p_2[v3++] = 10;
break;
case 'r':
p_2[v3++] = 13;
break;
case 's':
if ( v3 + strlen(un_0.sysname) + 1 <= 0x12C )
v3 += strappnd(&p_2[v3], un_0.sysname);
break;
case 't':
p_2[v3++] = 9;
break;
case 'v':
p_2[v3++] = 11;
break;
default:
if ( ((*_ctype_b_loc())[v6] & 0x800) != 0 )
{
p_2[v3] = strtol(v1 + 1, v13, 0);
v5 = v13[0] - 1;
}
else
{
p_2[v3] = v6;
}
++v3;
break;
}
}
else
{
p_2[v3++] = v2;
v5 = v1;
}
v1 = v5 + 1;
v2 = v5[1];
}
while ( v3 <= 295 && v2 );
}
else
{
v3 = 0;
}
p_2[v3] = 0;
if ( *v1 )
lprintf(3LL, "ln_escape_prompt: input line too long - data truncated");
return p_2;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1)
{
char *v1; // r12
char v2; // al
int v3; // ebx
char *v5; // rbp
char v6; // r13
const char *v7; // r13
char *v8; // r12
const char *v9; // r12
unsigned int v10; // eax
unsigned int v11; // eax
struct tm *v12; // rax
char *v13[13]; // [rsp+0h] [rbp-68h] BYREF

v1 = a1;
if ( !p_2 )
{
p_2 = (char *)malloc(0x12CuLL);
if ( !p_2 )
return a1;
}
if ( !un_cached_1 )
{
uname(&un_0);
un_cached_1 = 1;
}
v2 = *a1;
if ( *a1 )
{
v3 = 0;
do
{
if ( v2 == 64 )
{
if ( v3 + strlen(un_0.nodename) + 1 > 0x12C )
break;
v3 += func1(&p_2[v3], un_0.nodename);
v5 = v1;
}
else if ( v2 == 92 )
{
v5 = v1 + 1;
v6 = v1[1];
switch ( v6 )
{
case 'C':
v13[0] = (char *)time(0LL);
v8 = ctime((const time_t *)v13);
if ( v3 + strlen(v8) + 1 <= 0x12C )
v3 = v3 + func1(&p_2[v3], v8) - 1;
break;
case 'D':
case 'T':
if ( v3 <= 270 )
{
v13[0] = (char *)time(0LL);
v12 = localtime((const time_t *)v13);
if ( v12 )
{
if ( v1[1] == 68 )
sprintf(
&p_2[v3],
"%d/%d/%d",
(unsigned int)(v12->tm_mon + 1),
(unsigned int)v12->tm_mday,
(unsigned int)(v12->tm_year + 1900));
else
sprintf(
&p_2[v3],
"%02d:%02d:%02d",
(unsigned int)v12->tm_hour,
(unsigned int)v12->tm_min,
(unsigned int)v12->tm_sec);
v3 = strlen(p_2);
}
}
else
{
v3 += func1(&p_2[v3], "(x)");
}
break;
case 'I':
v9 = Connect;
if ( v3 + strlen(Connect) + 1 <= 0x12C )
v3 += func1(&p_2[v3], v9);
break;
case 'L':
case 'P':
v7 = Device;
if ( v3 + strlen(Device) + 1 <= 0x12C )
goto LABEL_32;
break;
case 'N':
case 'U':
v10 = get_current_users();
sprintf(&p_2[v3], "%d", v10);
v3 = strlen(p_2);
break;
case 'R':
if ( v3 + strlen(un_0.release) + 1 <= 0x12C )
v3 += func1(&p_2[v3], un_0.release);
break;
case 'S':
tio_get(0LL, v13);
v11 = tio_get_speed(v13);
sprintf(&p_2[v3], "%d", v11);
v3 = strlen(p_2);
break;
case 'V':
if ( v3 + strlen(un_0.version) + 1 <= 0x12C )
v3 += func1(&p_2[v3], un_0.version);
break;
case 'Y':
v7 = CallerId;
if ( v3 + strlen(CallerId) + 1 <= 0x12C )
LABEL_32:
v3 += func1(&p_2[v3], v7);
break;
case 'b':
p_2[v3++] = 8;
break;
case 'f':
p_2[v3++] = 12;
break;
case 'g':
p_2[v3++] = 7;
break;
case 'm':
if ( v3 + strlen(un_0.machine) + 1 <= 0x12C )
v3 += func1(&p_2[v3], un_0.machine);
break;
case 'n':
p_2[v3++] = 10;
break;
case 'r':
p_2[v3++] = 13;
break;
case 's':
if ( v3 + strlen(un_0.sysname) + 1 <= 0x12C )
v3 += func1(&p_2[v3], un_0.sysname);
break;
case 't':
p_2[v3++] = 9;
break;
case 'v':
p_2[v3++] = 11;
break;
default:
if ( ((*_ctype_b_loc())[v6] & 0x800) != 0 )
{
p_2[v3] = strtol(v1 + 1, v13, 0);
v5 = v13[0] - 1;
}
else
{
p_2[v3] = v6;
}
++v3;
break;
}
}
else
{
p_2[v3++] = v2;
v5 = v1;
}
v1 = v5 + 1;
v2 = v5[1];
}
while ( v3 <= 295 && v2 );
}
else
{
v3 = 0;
}
p_2[v3] = 0;
if ( *v1 )
lprintf(3LL, "ln_escape_prompt: input line too long - data truncated");
return p_2;
}

original_code:
char * ln_escape_prompt (char * ep)
{
    static char * p = ((void *)0);
    int i;
    static struct utsname un;
    static boolean un_cached = (1==0);
    if ( p == ((void *)0) ) p = malloc( 300 );
    if ( p == ((void *)0) ) return ep;
    if ( ! un_cached )
    {
 uname( &un );
 un_cached = (1==1);
    }
    i = 0;
    while ( *ep != 0 && i < 300 -4 )
    {
 if ( *ep == '@' )
 {
     if ( strlen( un.nodename ) +1 +i > 300 ) break;
     i += strappnd( &p[i], un.nodename );
 }
 else if ( *ep != '\\' ) p[i++] = *ep;
 else
 {
     ep++;
     switch ( *ep )
     {
       case 'n': p[i++] = '\n'; break;
       case 'r': p[i++] = '\r'; break;
       case 'g': p[i++] = '\007'; break;
       case 'b': p[i++] = '\010'; break;
       case 'v': p[i++] = '\013'; break;
       case 'f': p[i++] = '\f'; break;
       case 't': p[i++] = '\t'; break;
       case 's':
      if ( i + strlen(un.sysname) +1 > 300 ) break;
      i += strappnd( &p[i], un.sysname );
      break;
       case 'm':
      if ( i + strlen(un.machine) +1 > 300 ) break;
      i += strappnd( &p[i], un.machine );
      break;
       case 'R':
      if ( i + strlen(un.release) +1 > 300 ) break;
      i += strappnd( &p[i], un.release );
      break;
       case 'V':
      if ( i + strlen(un.version) +1 > 300 ) break;
      i += strappnd( &p[i], un.version );
      break;
       case 'Y':
  {
  extern char * CallerId;
      if ( i + strlen(CallerId) +1 > 300 ) break;
      i += strappnd( &p[i], CallerId );
      break;
  }
       case 'P':
       case 'L':
  {
      if ( i + strlen(Device) +1 > 300 ) break;
      i += strappnd( &p[i], Device );
      break;
  }
       case 'C':
  {
      time_t ti = time(((void *)0));
      char * h = ctime( &ti );
      if ( strlen(h) +1 +i > 300 ) break;
      i += strappnd( &p[i], h ) -1;
      break;
  }
       case 'I':
  {
      if ( strlen(Connect) +1 +i > 300 ) break;
      i += strappnd( &p[i], Connect);
      break;
  }
       case 'N':
       case 'U':
  {
      sprintf( &p[i], "%d", get_current_users() );
      i = strlen(p);
      break;
  }
       case 'S':
  {
      TIO temp_t;
      tio_get( 0, &temp_t );
      sprintf( &p[i], "%d", tio_get_speed( &temp_t ) );
      i = strlen(p);
  }
  break;
       case 'D':
       case 'T':
  if ( i + 30 > 300 )
  {
      i += strappnd( &p[i], "(x)" ); break;
  }
  else
  {
      time_t ti = time( ((void *)0) );
      struct tm * tm = localtime( &ti );
      if ( tm == ((void *)0) ) break;
      if ( *ep == 'D' )
          sprintf( &p[i], "%d/%d/%d", tm->tm_mon+1,
     tm->tm_mday, tm->tm_year + 1900 );
      else
          sprintf( &p[i], "%02d:%02d:%02d", tm->tm_hour,
     tm->tm_min, tm->tm_sec );
      i = strlen(p);
  }
  break;
       default:
  if ( ((*__ctype_b_loc ())[(int) ((*ep))] & (unsigned short int) _ISdigit) )
  {
      char * help;
      p[i++] = strtol( ep, &help, 0 );
      ep = help-1;
  }
  else p[i++] = *ep;
     }
 }
 ep++;
    }
    p[i] = 0;
    if ( *ep != 0 )
    {
 lprintf( 3, "ln_escape_prompt: input line too long - data truncated" );
    }
    return p;
}

canonical_original_code:
char *ln_escape_prompt(char *ep)
{
    static char *p = ((void *)0);
    int i;
    static struct utsname un;
    static char un_cached = (1==0);
    if ( p == ((void *)0) ) p = malloc( 300 );
    if ( p == ((void *)0) ) return ep;
    if ( ! un_cached )
    {
 uname( &un );
 un_cached = (1==1);
    }
    i = 0;
    while ( *ep != 0 && i < 300 -4 )
    {
 if ( *ep == '@' )
 {
     if ( strlen( un.nodename ) +1 +i > 300 ) break;
     i += strappnd( &p[i], un.nodename );
 }
 else if ( *ep != '\\' ) p[i++] = *ep;
 else
 {
     ep++;
     switch ( *ep )
     {
       case 'n': p[i++] = '\n'; break;
       case 'r': p[i++] = '\r'; break;
       case 'g': p[i++] = '\007'; break;
       case 'b': p[i++] = '\010'; break;
       case 'v': p[i++] = '\013'; break;
       case 'f': p[i++] = '\f'; break;
       case 't': p[i++] = '\t'; break;
       case 's':
      if ( i + strlen(un.sysname) +1 > 300 ) break;
      i += strappnd( &p[i], un.sysname );
      break;
       case 'm':
      if ( i + strlen(un.machine) +1 > 300 ) break;
      i += strappnd( &p[i], un.machine );
      break;
       case 'R':
      if ( i + strlen(un.release) +1 > 300 ) break;
      i += strappnd( &p[i], un.release );
      break;
       case 'V':
      if ( i + strlen(un.version) +1 > 300 ) break;
      i += strappnd( &p[i], un.version );
      break;
       case 'Y':
  {
  extern char *CallerId;
      if ( i + strlen(CallerId) +1 > 300 ) break;
      i += strappnd( &p[i], CallerId );
      break;
  }
       case 'P':
       case 'L':
  {
      if ( i + strlen(Device) +1 > 300 ) break;
      i += strappnd( &p[i], Device );
      break;
  }
       case 'C':
  {
      long ti = time(((void *)0));
      char *h = ctime( &ti );
      if ( strlen(h) +1 +i > 300 ) break;
      i += strappnd( &p[i], h ) -1;
      break;
  }
       case 'I':
  {
      if ( strlen(Connect) +1 +i > 300 ) break;
      i += strappnd( &p[i], Connect);
      break;
  }
       case 'N':
       case 'U':
  {
      sprintf( &p[i], "%d", get_current_users() );
      i = strlen(p);
      break;
  }
       case 'S':
  {
      struct termios temp_t;
      tio_get( 0, &temp_t );
      sprintf( &p[i], "%d", tio_get_speed( &temp_t ) );
      i = strlen(p);
  }
  break;
       case 'D':
       case 'T':
  if ( i + 30 > 300 )
  {
      i += strappnd( &p[i], "(x)" ); break;
  }
  else
  {
      long ti = time( ((void *)0) );
      struct tm *tm = localtime( &ti );
      if ( tm == ((void *)0) ) break;
      if ( *ep == 'D' )
          sprintf( &p[i], "%d/%d/%d", tm->tm_mon+1,
     tm->tm_mday, tm->tm_year + 1900 );
      else
          sprintf( &p[i], "%02d:%02d:%02d", tm->tm_hour,
     tm->tm_min, tm->tm_sec );
      i = strlen(p);
  }
  break;
       default:
  if ( ((*__ctype_b_loc ())[(int) ((*ep))] & (unsigned short) _ISdigit) )
  {
      char *help;
      p[i++] = strtol( ep, &help, 0 );
      ep = help-1;
  }
  else p[i++] = *ep;
     }
 }
 ep++;
    }
    p[i] = 0;
    if ( *ep != 0 )
    {
 lprintf( 3, "ln_escape_prompt: input line too long - data truncated" );
    }
    return p;
}

variable_types:
ep
char *

variable_types: struct utsname { char sysname[65]; char nodename[65]; char release[65]; char version[65]; char machine[65]; char __domainname[65]; }

variable_types:
CallerId
char *

variable_types: ti long

variable_types:
h
char *

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

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types:
help
char *

return_type: char *


decompiled_code:
int __fastcall set_env_var(const char *a1, const char *a2)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbx
int result; // eax

v2 = strlen(a1);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 2);
if ( !v4 )
return lprintf(1LL, "set_env_var: cannot malloc");
v5 = v4;
sprintf(v4, "%s=%s", a1, a2);
lprintf(5LL, "setenv: '%s'", v5);
result = putenv(v5);
if ( result )
result = lprintf(1LL, "putenv failed");
return result;
}

canonical_decompiled_code:
int __fastcall func3(const char *a1, const char *a2)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbx
int result; // eax

v2 = strlen(a1);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 2);
if ( !v4 )
return lprintf(1LL, "set_env_var: cannot malloc");
v5 = v4;
sprintf(v4, "%s=%s", a1, a2);
lprintf(5LL, "setenv: '%s'", v5);
result = putenv(v5);
if ( result )
result = lprintf(1LL, "putenv failed");
return result;
}

original_code:
void set_env_var (char * var,char * string)
{
    char * v;
    v = malloc( strlen(var) + strlen(string) + 2 );
    if ( v == ((void *)0) )
        lprintf( 1, "set_env_var: cannot malloc" );
    else
    {
 sprintf( v, "%s=%s", var, string );
 lprintf( 5, "setenv: '%s'", v );
 if ( putenv( v ) != 0 )
     lprintf( 1, "putenv failed" );
    }
}

canonical_original_code:
void set_env_var(char *var, char *string)
{
    char *v;
    v = malloc( strlen(var) + strlen(string) + 2 );
    if ( v == ((void *)0) )
        lprintf( 1, "set_env_var: cannot malloc" );
    else
    {
 sprintf( v, "%s=%s", var, string );
 lprintf( 5, "setenv: '%s'", v );
 if ( putenv( v ) != 0 )
     lprintf( 1, "putenv failed" );
    }
}

variable_types:
var
char *

variable_types:
string
char *

variable_types:
v
char *


getlogname

decompiled_code:
__int64 __fastcall getlogname(char *a1, __m128i *a2, const char *a3, int a4, signed int a5, __int64 a6, char a7)
{
char *v11; // rax
const char *v12; // r13
int v13; // er12
unsigned int v15; // eax
int v16; // eax
unsigned __int8 v17; // al
char v18; // di
int v19; // er12
unsigned int v20; // eax
__m128i v21; // [rsp+0h] [rbp-78h] BYREF
__m128i v22; // [rsp+10h] [rbp-68h] BYREF
__m128i v23; // [rsp+20h] [rbp-58h] BYREF
__int64 v24; // [rsp+30h] [rbp-48h]
int v25; // [rsp+38h] [rbp-40h]
char buf[57]; // [rsp+3Fh] [rbp-39h] BYREF

v21 = _mm_loadu_si128(a2);
v22 = _mm_loadu_si128(a2 + 1);
v23 = _mm_loadu_si128(a2 + 2);
v24 = a2[3].m128i_i64[0];
v25 = a2[3].m128i_i32[2];
tio_mode_raw(a2);
tio_set(0LL, a2);
v11 = ln_escape_prompt(a1);
v12 = v11;
if ( a7 )
{
printf("\r\n%s", v11);
tio_mode_sane(a2, 0LL);
tio_map_cr(a2, 1LL);
tio_set(0LL, a2);
*a3 = 0;
set_env_var("TTYPROMPT", v12);
return 0LL;
}
if ( a5 > 0 )
{
signal(14, (__sighandler_t)getlog_timeout);
alarm(a5);
}
LABEL_4:
printf("\r\n%s", v12);
if ( ferror(stdin) )
lprintf(1LL, "getlogname: error writing prompt");
lprintf(5LL, "getlogname (no opts), read:");
v13 = 0;
do
{
while ( 1 )
{
while ( 1 )
{
v16 = read(0, buf, 1uLL);
if ( v16 != 1 )
{
if ( !v16 )
{
lprintf(4LL, "getlogname: got EOF, exiting");
exit(0);
}
if ( *_errno_location() != 4 )
exit(0);
if ( timeouts > 1 )
{
printf("\r\n\a\r\nYour login time (%d minutes) ran out. Goodbye.\r\n", (unsigned int)(a5 / 60 + 1));
sleep(3u);
v15 = getpid();
lprintf(2LL, "failed dev=%s, pid=%d, login time out", Device, v15);
exit(0);
}
puts("\r\n\a\r\nHey! Please login now. You have one minute left\r");
alarm(0x3Cu);
buf[0] = 21;
}
lputc(5LL, (unsigned int)buf[0]);
v17 = buf[0];
if ( buf[0] <= 1u )
goto LABEL_16;
v18 = buf[0] & 0x7F;
buf[0] &= 0x7Fu;
if ( (v17 & 0x7F) == 28 )
exit(0);
if ( v18 != 4 )
break;
if ( !v13 )
exit(0);
}
if ( v18 == 21 || v18 == 3 )
goto LABEL_4;
if ( (v17 & 0x7D) == 17 )
break;
if ( v18 == 8 || v18 == 127 )
{
if ( v13 > 0 )
{
fwrite("\b \b", 1uLL, 3uLL, stdout);
--v13;
}
break;
}
if ( v18 != 27 )
{
putc(v18, stdout);
if ( v13 >= a4 || buf[0] == 32 && !v13 || buf[0] == 45 && !v13 )
fwrite("\b \b", 1uLL, 3uLL, stdout);
else
a3[v13++] = buf[0];
if ( v13 > 6 && !memcmp(&a3[v13 - 7], "**EMSI_", 7uLL) )
lprintf(4LL, "incoming fido call, but no FIDO support");
break;
}
}
LABEL_16:
;
}
while ( buf[0] != 10 && buf[0] != 13 );
alarm(0);
v19 = v13 - 1;
a3[v19] = 0;
*a2 = _mm_loadu_si128(&v21);
a2[1] = _mm_loadu_si128(&v22);
a2[2] = _mm_loadu_si128(&v23);
a2[3].m128i_i64[0] = v24;
a2[3].m128i_i32[2] = v25;
if ( !strcmp(a3, "NO CARRIER") || !strcmp(a3, "ERROR") )
{
v20 = getpid();
lprintf(2LL, "failed dev=%s, pid=%d, got modem error '%s'", Device, v20, a3);
exit(0);
}
if ( buf[0] == 10 )
{
tio_map_cr(a2, 0LL);
fputc(13, stdout);
}
else
{
tio_map_cr(a2, 1LL);
fputc(10, stdout);
lprintf(5LL, "input finished with '\\r', setting ICRNL ONLCR");
}
tio_drain_output(0LL);
tio_set(0LL, a2);
return (unsigned int)-(v19 == 0);
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, __m128i *a2, const char *a3, int a4, signed int a5, __int64 a6, char a7)
{
char *v11; // rax
const char *v12; // r13
int v13; // er12
unsigned int v15; // eax
int v16; // eax
unsigned __int8 v17; // al
char v18; // di
int v19; // er12
unsigned int v20; // eax
__m128i v21; // [rsp+0h] [rbp-78h] BYREF
__m128i v22; // [rsp+10h] [rbp-68h] BYREF
__m128i v23; // [rsp+20h] [rbp-58h] BYREF
__int64 v24; // [rsp+30h] [rbp-48h]
int v25; // [rsp+38h] [rbp-40h]
char buf[57]; // [rsp+3Fh] [rbp-39h] BYREF

v21 = _mm_loadu_si128(a2);
v22 = _mm_loadu_si128(a2 + 1);
v23 = _mm_loadu_si128(a2 + 2);
v24 = a2[3].m128i_i64[0];
v25 = a2[3].m128i_i32[2];
tio_mode_raw(a2);
tio_set(0LL, a2);
v11 = func2(a1);
v12 = v11;
if ( a7 )
{
printf("\r\n%s", v11);
tio_mode_sane(a2, 0LL);
tio_map_cr(a2, 1LL);
tio_set(0LL, a2);
*a3 = 0;
func3("TTYPROMPT", v12);
return 0LL;
}
if ( a5 > 0 )
{
signal(14, (__sighandler_t)getlog_timeout);
alarm(a5);
}
LABEL_4:
printf("\r\n%s", v12);
if ( ferror(stdin) )
lprintf(1LL, "getlogname: error writing prompt");
lprintf(5LL, "getlogname (no opts), read:");
v13 = 0;
do
{
while ( 1 )
{
while ( 1 )
{
v16 = read(0, buf, 1uLL);
if ( v16 != 1 )
{
if ( !v16 )
{
lprintf(4LL, "getlogname: got EOF, exiting");
exit(0);
}
if ( *_errno_location() != 4 )
exit(0);
if ( timeouts > 1 )
{
printf("\r\n\a\r\nYour login time (%d minutes) ran out. Goodbye.\r\n", (unsigned int)(a5 / 60 + 1));
sleep(3u);
v15 = getpid();
lprintf(2LL, "failed dev=%s, pid=%d, login time out", Device, v15);
exit(0);
}
puts("\r\n\a\r\nHey! Please login now. You have one minute left\r");
alarm(0x3Cu);
buf[0] = 21;
}
lputc(5LL, (unsigned int)buf[0]);
v17 = buf[0];
if ( buf[0] <= 1u )
goto LABEL_16;
v18 = buf[0] & 0x7F;
buf[0] &= 0x7Fu;
if ( (v17 & 0x7F) == 28 )
exit(0);
if ( v18 != 4 )
break;
if ( !v13 )
exit(0);
}
if ( v18 == 21 || v18 == 3 )
goto LABEL_4;
if ( (v17 & 0x7D) == 17 )
break;
if ( v18 == 8 || v18 == 127 )
{
if ( v13 > 0 )
{
fwrite("\b \b", 1uLL, 3uLL, stdout);
--v13;
}
break;
}
if ( v18 != 27 )
{
putc(v18, stdout);
if ( v13 >= a4 || buf[0] == 32 && !v13 || buf[0] == 45 && !v13 )
fwrite("\b \b", 1uLL, 3uLL, stdout);
else
a3[v13++] = buf[0];
if ( v13 > 6 && !memcmp(&a3[v13 - 7], "**EMSI_", 7uLL) )
lprintf(4LL, "incoming fido call, but no FIDO support");
break;
}
}
LABEL_16:
;
}
while ( buf[0] != 10 && buf[0] != 13 );
alarm(0);
v19 = v13 - 1;
a3[v19] = 0;
*a2 = _mm_loadu_si128(&v21);
a2[1] = _mm_loadu_si128(&v22);
a2[2] = _mm_loadu_si128(&v23);
a2[3].m128i_i64[0] = v24;
a2[3].m128i_i32[2] = v25;
if ( !strcmp(a3, "NO CARRIER") || !strcmp(a3, "ERROR") )
{
v20 = getpid();
lprintf(2LL, "failed dev=%s, pid=%d, got modem error '%s'", Device, v20, a3);
exit(0);
}
if ( buf[0] == 10 )
{
tio_map_cr(a2, 0LL);
fputc(13, stdout);
}
else
{
tio_map_cr(a2, 1LL);
fputc(10, stdout);
lprintf(5LL, "input finished with '\\r', setting ICRNL ONLCR");
}
tio_drain_output(0LL);
tio_set(0LL, a2);
return (unsigned int)-(v19 == 0);
}

original_code:
int getlogname (char * prompt,TIO * tio,char * buf,int maxsize,int max_login_time,boolean do_fido,boolean env_ttyprompt)
{
    int i, r;
    char ch;
    TIO tio_save;
    char * final_prompt;
    tio_save = *tio;
    tio_mode_raw( tio );
    tio_set( 0, tio );
    final_prompt = ln_escape_prompt( prompt );
    if ( env_ttyprompt )
    {
 printf( "\r\n%s", final_prompt );
 tio_mode_sane( tio, (1==0) );
 tio_map_cr( tio, (1==1) );
 tio_set( 0, tio );
 buf[0] = 0;
 set_env_var( "TTYPROMPT", final_prompt );
 return 0;
    }
    if ( max_login_time > 0 )
    {
 signal( 14, getlog_timeout );
 alarm( max_login_time );
    }
  newlogin:
    printf( "\r\n%s", final_prompt );
    if ( ferror( stdin ) )
    {
 lprintf( 1, "getlogname: error writing prompt" );
    }
    lprintf( 5, "getlogname (no opts), read:" );
    i = 0;
    do
    {
 if ( ( r = read( 0, &ch, 1 ) ) != 1 )
 {
     if ( r == 0 )
     {
  lprintf( 4, "getlogname: got EOF, exiting" );
  exit(0);
     }
     if ( (*__errno_location ()) != 4 ) exit(0);
     if ( timeouts <= 1 )
     {
  printf( "\r\n\07\r\nHey! Please login now. You have one minute left\r\n" );
  alarm(60);
     }
     else
     {
  printf( "\r\n\07\r\nYour login time (%d minutes) ran out. Goodbye.\r\n",
         (max_login_time / 60)+1 );
  sleep(3);
  lprintf( 2, "failed dev=%s, pid=%d, login time out",
    Device, getpid() );
  exit(0);
     }
     ch = ('u'&037);
 }
 lputc( 5, ch );
 if ( ch == 0 || ch == 1 ) continue;
 ch = ch & 0x7f;
 if ( ch == 034 ) exit(0);
 if ( ch == ('d'&037) )
 {
            if ( i == 0 ) exit(0); else continue;
 }
 if ( ch == ('u'&037) || ch == 03 ) goto newlogin;
 if ( ch == ('q'&037) || ch == ('s'&037) ) continue;
 if ( ch == 0x7f || ch == 0x08 || ch == 0177 )
 {
     if ( i > 0 )
     {
  fputs( "\b \b", stdout );
  i--;
     }
 }
 else
   if ( ch != 27 )
 {
     putc( ch, stdout );
     if ( i >= maxsize ||
   ( ch == ' ' && i == 0 ) ||
   ( ch == '-' && i == 0 ) )
  fputs( "\b \b", stdout );
     else
  buf[i++] = ch;
     if ( i >= 7 && strncmp( &buf[i-7], "**EMSI_", 7 ) == 0 )
     {
  lprintf( 4, "incoming fido call, but no FIDO support" );
     }
 }
    }
    while ( ch != '\n' && ch != '\r' );
    alarm(0);
    buf[--i] = 0;
    *tio = tio_save;
    if( strcmp(buf,"NO CARRIER") == 0 ||
        strcmp(buf,"ERROR") == 0 )
    {
 lprintf( 2, "failed dev=%s, pid=%d, got modem error '%s'",
                 Device, getpid(), buf );
 exit(0);
    }
    if ( ch == '\n' )
    {
 tio_map_cr( tio, (1==0) );
 fputc( '\r', stdout );
    }
    else
    {
 tio_map_cr( tio, (1==1) );
 fputc( '\n', stdout );
 lprintf( 5, "input finished with '\\r', setting ICRNL ONLCR" );
    }
    tio_drain_output( 0 );
    tio_set( 0, tio );
    if ( i == 0 ) return -1;
    else return 0;
}

canonical_original_code:
int getlogname(char *prompt, struct termios *tio, char *buf, int maxsize, int max_login_time, char do_fido, char env_ttyprompt)
{
    int i; int r;
    char ch;
    struct termios tio_save;
    char *final_prompt;
    tio_save = *tio;
    tio_mode_raw( tio );
    tio_set( 0, tio );
    final_prompt = ln_escape_prompt( prompt );
    if ( env_ttyprompt )
    {
 printf( "\r\n%s", final_prompt );
 tio_mode_sane( tio, (1==0) );
 tio_map_cr( tio, (1==1) );
 tio_set( 0, tio );
 buf[0] = 0;
 set_env_var( "TTYPROMPT", final_prompt );
 return 0;
    }
    if ( max_login_time > 0 )
    {
 signal( 14, getlog_timeout );
 alarm( max_login_time );
    }
  newlogin:
    printf( "\r\n%s", final_prompt );
    if ( ferror( stdin ) )
    {
 lprintf( 1, "getlogname: error writing prompt" );
    }
    lprintf( 5, "getlogname (no opts), read:" );
    i = 0;
    do
    {
 if ( ( r = read( 0, &ch, 1 ) ) != 1 )
 {
     if ( r == 0 )
     {
  lprintf( 4, "getlogname: got EOF, exiting" );
  exit(0);
     }
     if ( (*__errno_location ()) != 4 ) exit(0);
     if ( timeouts <= 1 )
     {
  printf( "\r\n\07\r\nHey! Please login now. You have one minute left\r\n" );
  alarm(60);
     }
     else
     {
  printf( "\r\n\07\r\nYour login time (%d minutes) ran out. Goodbye.\r\n",
         (max_login_time / 60)+1 );
  sleep(3);
  lprintf( 2, "failed dev=%s, pid=%d, login time out",
    Device, getpid() );
  exit(0);
     }
     ch = ('u'&037);
 }
 lputc( 5, ch );
 if ( ch == 0 || ch == 1 ) continue;
 ch = ch & 0x7f;
 if ( ch == 034 ) exit(0);
 if ( ch == ('d'&037) )
 {
            if ( i == 0 ) exit(0); else continue;
 }
 if ( ch == ('u'&037) || ch == 03 ) goto newlogin;
 if ( ch == ('q'&037) || ch == ('s'&037) ) continue;
 if ( ch == 0x7f || ch == 0x08 || ch == 0177 )
 {
     if ( i > 0 )
     {
  fputs( "\b \b", stdout );
  i--;
     }
 }
 else
   if ( ch != 27 )
 {
     putc( ch, stdout );
     if ( i >= maxsize ||
   ( ch == ' ' && i == 0 ) ||
   ( ch == '-' && i == 0 ) )
  fputs( "\b \b", stdout );
     else
  buf[i++] = ch;
     if ( i >= 7 && strncmp( &buf[i-7], "**EMSI_", 7 ) == 0 )
     {
  lprintf( 4, "incoming fido call, but no FIDO support" );
     }
 }
    }
    while ( ch != '\n' && ch != '\r' );
    alarm(0);
    buf[--i] = 0;
    *tio = tio_save;
    if( strcmp(buf,"NO CARRIER") == 0 ||
        strcmp(buf,"ERROR") == 0 )
    {
 lprintf( 2, "failed dev=%s, pid=%d, got modem error '%s'",
                 Device, getpid(), buf );
 exit(0);
    }
    if ( ch == '\n' )
    {
 tio_map_cr( tio, (1==0) );
 fputc( '\r', stdout );
    }
    else
    {
 tio_map_cr( tio, (1==1) );
 fputc( '\n', stdout );
 lprintf( 5, "input finished with '\\r', setting ICRNL ONLCR" );
    }
    tio_drain_output( 0 );
    tio_set( 0, tio );
    if ( i == 0 ) return -1;
    else return 0;
}

variable_types:
prompt
char *

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

variable_types:
buf
char *

variable_types: maxsize int

variable_types: r int

variable_types: ch char


decompiled_code:
__int64 __fastcall lock_write_pid(int fd)
{
unsigned int v1; // eax
size_t v2; // rax
ssize_t v3; // rbp
__int64 result; // rax
char v5[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = getpid();
sprintf(v5, "%10d\n", v1);
v2 = strlen(v5);
v3 = write(fd, v5, v2);
if ( v3 == strlen(v5) )
{
close(fd);
result = 0LL;
}
else
{
lprintf(0LL, "cannot write PID to (temp) lock file");
close(fd);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
unsigned int v1; // eax
size_t v2; // rax
ssize_t v3; // rbp
__int64 result; // rax
char v5[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = getpid();
sprintf(v5, "%10d\n", v1);
v2 = strlen(v5);
v3 = write(fd, v5, v2);
if ( v3 == strlen(v5) )
{
close(fd);
result = 0LL;
}
else
{
lprintf(0LL, "cannot write PID to (temp) lock file");
close(fd);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int lock_write_pid (int fd)
{
    char apid[16];
    sprintf( apid, "%10d\n", (int) getpid() );
    if ( write(fd, apid, strlen(apid)) != strlen(apid) )
    {
 lprintf( 0, "cannot write PID to (temp) lock file" );
 close(fd);
 return(-1);
    }
    close(fd);
    return 0;
}

canonical_original_code:
static int lock_write_pid(int fd)
{
    char apid[16];
    sprintf( apid, "%10d\n", (int) getpid() );
    if ( write(fd, apid, strlen(apid)) != strlen(apid) )
    {
 lprintf( 0, "cannot write PID to (temp) lock file" );
 close(fd);
 return(-1);
    }
    close(fd);
    return 0;
}

variable_types: fd int

variable_types:
apid
char [16]


readlock

decompiled_code:
__int64 __fastcall readlock(const char *a1)
{
int v1; // eax
unsigned int v2; // ebx
unsigned int v3; // ebp
int v5[7]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v6; // [rsp+1Ch] [rbp-1Ch] BYREF

v1 = open(a1, 0);
v2 = v1;
if ( v1 != -1 )
{
v3 = read(v1, v5, 0x13uLL);
*((_BYTE *)v5 + (int)v3) = 0;
v6 = 0;
if ( v3 == 4 || (unsigned int)_isoc99_sscanf(v5, "%d", &v6) != 1 || !v6 )
{
v6 = v5[0];
lprintf(3LL, "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!", v3, (unsigned int)v5[0]);
}
close(v2);
v2 = v6;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
int v1; // eax
unsigned int v2; // ebx
unsigned int v3; // ebp
int v5[7]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v6; // [rsp+1Ch] [rbp-1Ch] BYREF

v1 = open(a1, 0);
v2 = v1;
if ( v1 != -1 )
{
v3 = read(v1, v5, 0x13uLL);
*((_BYTE *)v5 + (int)v3) = 0;
v6 = 0;
if ( v3 == 4 || (unsigned int)_isoc99_sscanf(v5, "%d", &v6) != 1 || !v6 )
{
v6 = v5[0];
lprintf(3LL, "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!", v3, (unsigned int)v5[0]);
}
close(v2);
v2 = v6;
}
return v2;
}

original_code:
static int readlock (char * name)
{
 int fd, pid;
 char apid[20];
 int length;
 if ((fd = open(name, 00)) == -1)
  return(-1);
 length = read(fd, apid, sizeof(apid)-1);
 apid[length]=0;
 pid = 0;
 if ( length == sizeof( pid ) || sscanf(apid, "%d", &pid) != 1 ||
      pid == 0 )
 {
     pid = * ( (int *) apid );
     lprintf( 3, "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!", length, pid );
 }
 (void) close(fd);
 return(pid);
}

canonical_original_code:
static int readlock(char *name)
{
 int fd; int pid;
 char apid[20];
 int length;
 if ((fd = open(name, 00)) == -1)
  return(-1);
 length = read(fd, apid, sizeof(apid)-1);
 apid[length]=0;
 pid = 0;
 if ( length == sizeof( pid ) || sscanf(apid, "%d", &pid) != 1 ||
      pid == 0 )
 {
     pid = * ( (int *) apid );
     lprintf( 3, "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!", length, pid );
 }
 (void) close(fd);
 return(pid);
}

variable_types:
name
char *

variable_types: pid int

variable_types:
apid
char [20]

variable_types: length int


decompiled_code:
char *__fastcall get_lock_name(char *s, char *a2)
{
char *v2; // rbx

v2 = a2;
if ( strchr(a2, 47) )
v2 = strrchr(a2, 47) + 1;
sprintf(s, "/var/lock/LCK..%s", v2);
return s;
}

canonical_decompiled_code:
char *__fastcall func2(char *s, char *a2)
{
char *v2; // rbx

v2 = a2;
if ( strchr(a2, 47) )
v2 = strrchr(a2, 47) + 1;
sprintf(s, "/var/lock/LCK..%s", v2);
return s;
}

original_code:
static char * get_lock_name (char * lock_name,char * device)
{
    if ( strchr( device, '/' ) != ((void *)0) )
        device = strrchr( device, '/' ) +1;
    sprintf( lock_name, "/var/lock/LCK..%s", device);
    return lock_name;
}

canonical_original_code:
static char *get_lock_name(char *lock_name, char *device)
{
    if ( strchr( device, '/' ) != ((void *)0) )
        device = strrchr( device, '/' ) +1;
    sprintf( lock_name, "/var/lock/LCK..%s", device);
    return lock_name;
}

variable_types:
device
char *


decompiled_code:
__int64 do_makelock()
{
unsigned int v0; // er12
unsigned int v2; // er13
int *v3; // rbx
unsigned int v4; // ebp
char v5[1080]; // [rsp+0h] [rbp-438h] BYREF

we_have_lock = 0;
lprintf(5LL, "do_makelock: lock='%s'", lock);
sprintf(v5, "/var/lock/LCK..%s", "TM.XXXXXX");
v0 = mkstemp(v5);
if ( v0 == -1 )
{
lprintf(1LL, "cannot create tempfile (%s)", v5);
}
else
{
chmod(v5, 0x1A4u);
v0 = lock_write_pid(v0);
if ( v0 != -1 )
{
while ( 1 )
{
while ( 1 )
{
v0 = link(v5, lock);
if ( v0 != -1 )
goto LABEL_19;
v3 = _errno_location();
if ( *v3 != 17 )
{
lprintf(1LL, "lock not made: link(temp,lock) failed");
if ( *v3 != 17 )
goto LABEL_6;
}
v4 = readlock(lock);
if ( v4 != -1 )
break;
if ( *v3 != 2 )
{
lprintf(5LL, "cannot read lockfile");
unlink(v5);
return v4;
}
}
if ( getpid() == v4 )
{
lprintf(3LL, "we *have* the line!");
LABEL_19:
lprintf(5LL, "lock made");
unlink(v5);
we_have_lock = 1;
return 0;
}
v2 = kill(v4, 0);
if ( v2 != -1 || *v3 != 3 )
break;
lprintf(5LL, "stale lockfile, created by process %d, ignoring", v4);
if ( unlink(lock) < 0 && ((*v3 - 2) & 0xFFFFFFFD) != 0 )
{
lprintf(1LL, "unlink() failed, giving up");
unlink(v5);
return v2;
}
}
lprintf(4LL, "lock not made: lock file exists (pid=%d)", v4);
}
LABEL_6:
unlink(v5);
}
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // er12
unsigned int v2; // er13
int *v3; // rbx
unsigned int v4; // ebp
char v5[1080]; // [rsp+0h] [rbp-438h] BYREF

we_have_lock = 0;
lprintf(5LL, "do_makelock: lock='%s'", lock);
sprintf(v5, "/var/lock/LCK..%s", "TM.XXXXXX");
v0 = mkstemp(v5);
if ( v0 == -1 )
{
lprintf(1LL, "cannot create tempfile (%s)", v5);
}
else
{
chmod(v5, 0x1A4u);
v0 = func0(v0);
if ( v0 != -1 )
{
while ( 1 )
{
while ( 1 )
{
v0 = link(v5, lock);
if ( v0 != -1 )
goto LABEL_19;
v3 = _errno_location();
if ( *v3 != 17 )
{
lprintf(1LL, "lock not made: link(temp,lock) failed");
if ( *v3 != 17 )
goto LABEL_6;
}
v4 = func1(lock);
if ( v4 != -1 )
break;
if ( *v3 != 2 )
{
lprintf(5LL, "cannot read lockfile");
unlink(v5);
return v4;
}
}
if ( getpid() == v4 )
{
lprintf(3LL, "we *have* the line!");
LABEL_19:
lprintf(5LL, "lock made");
unlink(v5);
we_have_lock = 1;
return 0;
}
v2 = kill(v4, 0);
if ( v2 != -1 || *v3 != 3 )
break;
lprintf(5LL, "stale lockfile, created by process %d, ignoring", v4);
if ( unlink(lock) < 0 && ((*v3 - 2) & 0xFFFFFFFD) != 0 )
{
lprintf(1LL, "unlink() failed, giving up");
unlink(v5);
return v2;
}
}
lprintf(4LL, "lock not made: lock file exists (pid=%d)", v4);
}
LABEL_6:
unlink(v5);
}
return v0;
}

original_code:
int do_makelock (void)
{
 int fd, pid;
 char *temp, buf[1024 +1];
 we_have_lock = (1==0);
 lprintf( 5, "do_makelock: lock='%s'", lock );
 temp=buf;
 sprintf(buf, "/var/lock/LCK..%s", "TM.XXXXXX");
 if ((fd = mkstemp(temp)) == -1 ) {
  lprintf(1, "cannot create tempfile (%s)", temp);
  return(-1);
 }
 chmod( temp, 0644 );
 if ( lock_write_pid( fd ) == -1)
                         { unlink(temp); return -1; }
 while (link(temp, lock) == -1)
 {
         if ((*__errno_location ()) != 17 )
  {
      lprintf(1, "lock not made: link(temp,lock) failed" );
  }
  if ((*__errno_location ()) == 17)
  {
      if ((pid = readlock(lock)) == -1)
      {
   if ( (*__errno_location ()) == 2 )
       continue;
   else
   {
       lprintf( 5, "cannot read lockfile" );
       unlink(temp);
       return -1;
   }
      }
      if (pid == getpid())
      {
   lprintf( 3, "we *have* the line!" );
   break;
      }
      if ((kill(pid, 0) == -1) && (*__errno_location ()) == 3)
      {
   lprintf( 5, "stale lockfile, created by process %d, ignoring", pid );
   if ( unlink(lock) < 0 &&
            (*__errno_location ()) != 4 && (*__errno_location ()) != 2 )
   {
       lprintf( 1, "unlink() failed, giving up" );
       unlink(temp);
       return -1;
   }
   continue;
      }
      lprintf(4, "lock not made: lock file exists (pid=%d)", pid);
  }
  (void) unlink(temp);
  return(-1);
 }
 lprintf(5, "lock made");
 (void) unlink(temp);
 we_have_lock = (1==1);
 return(0);
}

canonical_original_code:
int do_makelock()
{
 int fd; int pid;
 char *temp; char buf[-1];
 we_have_lock = (1==0);
 lprintf( 5, "do_makelock: lock='%s'", lock );
 temp=buf;
 sprintf(buf, "/var/lock/LCK..%s", "TM.XXXXXX");
 if ((fd = mkstemp(temp)) == -1 ) {
  lprintf(1, "cannot create tempfile (%s)", temp);
  return(-1);
 }
 chmod( temp, 0644 );
 if ( lock_write_pid( fd ) == -1)
                         { unlink(temp); return -1; }
 while (link(temp, lock) == -1)
 {
         if ((*__errno_location ()) != 17 )
  {
      lprintf(1, "lock not made: link(temp,lock) failed" );
  }
  if ((*__errno_location ()) == 17)
  {
      if ((pid = readlock(lock)) == -1)
      {
   if ( (*__errno_location ()) == 2 )
       continue;
   else
   {
       lprintf( 5, "cannot read lockfile" );
       unlink(temp);
       return -1;
   }
      }
      if (pid == getpid())
      {
   lprintf( 3, "we *have* the line!" );
   break;
      }
      if ((kill(pid, 0) == -1) && (*__errno_location ()) == 3)
      {
   lprintf( 5, "stale lockfile, created by process %d, ignoring", pid );
   if ( unlink(lock) < 0 &&
            (*__errno_location ()) != 4 && (*__errno_location ()) != 2 )
   {
       lprintf( 1, "unlink() failed, giving up" );
       unlink(temp);
       return -1;
   }
   continue;
      }
      lprintf(4, "lock not made: lock file exists (pid=%d)", pid);
  }
  (void) unlink(temp);
  return(-1);
 }
 lprintf(5, "lock made");
 (void) unlink(temp);
 we_have_lock = (1==1);
 return(0);
}

variable_types:
temp
char *

variable_types:
buf
char [-1]