decompiled_code:
__int64 __fastcall Dolphin_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 = 2;
result = (unsigned int)-((unsigned int)voice_command("AT+VSM=2", "OK") != 4096);
}
else
{
lprintf(3LL, "%s: Illegal voice compression method (%d)", (const char *)*voice_modem, *a1);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_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 = 2;
result = (unsigned int)-((unsigned int)voice_command("AT+VSM=2", "OK") != 4096);
}
else
{
lprintf(3LL, "%s: Illegal voice compression method (%d)", (const char *)*voice_modem, *a1);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int Dolphin_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 = 2;
     if (voice_command("AT+VSM=2", "OK") != (0x1000))
          return((-1));
     return((0));
     }

canonical_original_code:
static int Dolphin_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 = 2;
     if (voice_command("AT+VSM=2", "OK") != (0x1000))
          return((-1));
     return((0));
     }

variable_types:
compression
long *

variable_types:
speed
long *

variable_types:
bits
int *


repo: Distrotech/mgetty

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

reset_watchdog();
voice_modem_state = 2;
lprintf(4LL, "initializing Dolphin voice modem");
if ( (unsigned int)voice_command("AT+VNH=1", "OK") != 4096 )
lprintf(3LL, "disabling automatic hangup didn't work");
sprintf(v1, "AT+VSD=%d,%d", (unsigned int)((int)(31 * *((_QWORD *)&cvd + 58)) / 100), *((unsigned int *)&cvd + 110));
if ( (unsigned int)voice_command(v1, "OK") != 4096 )
lprintf(3LL, "setting recording preferences didn't work");
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 Dolphin voice modem");
if ( (unsigned int)voice_command("AT+VNH=1", "OK") != 4096 )
lprintf(3LL, "disabling automatic hangup didn't work");
sprintf(v1, "AT+VSD=%d,%d", (unsigned int)((int)(31 * *((_QWORD *)&cvd + 58)) / 100), *((unsigned int *)&cvd + 110));
if ( (unsigned int)voice_command(v1, "OK") != 4096 )
lprintf(3LL, "setting recording preferences didn't work");
voice_modem_state = 1;
return 0LL;
}

original_code:
static int Dolphin_init (void)
     {
     char buffer[(256)];
     reset_watchdog();
     voice_modem_state = (0x0002);
     lprintf(4, "initializing Dolphin voice modem");
     if (voice_command("AT+VNH=1", "OK") != (0x1000))
          lprintf(3, "disabling automatic hangup didn't work");
     sprintf(buffer, "AT+VSD=%d,%d", (int)cvd.rec_silence_threshold.d.i * 31 / 100,
      (int)cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting recording preferences didn't work");
     voice_modem_state = (0x0001);
     return((0));
     }

canonical_original_code:
static int Dolphin_init()
     {
     char buffer[-1];
     reset_watchdog();
     voice_modem_state = (0x0002);
     lprintf(4, "initializing Dolphin voice modem");
     if (voice_command("AT+VNH=1", "OK") != (0x1000))
          lprintf(3, "disabling automatic hangup didn't work");
     sprintf(buffer, "AT+VSD=%d,%d", (int)cvd.rec_silence_threshold.d.i * 31 / 100,
      (int)cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3, "setting recording preferences didn't work");
     voice_modem_state = (0x0001);
     return((0));
     }

variable_types:
buffer
char [-1]

return_type: int


variable_types: sample int

variable_types: sign int

variable_types: exponent int

variable_types: mantissa int

variable_types:
ulawbyte
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall ulaw2linear(char a1)
{
char v1; // di
unsigned __int8 v2; // dl
__int64 result; // rax
unsigned int v4; // edx

v1 = ~a1;
v2 = ((unsigned __int8)v1 >> 4) & 7;
result = exp_lut_2[v2] + ((unsigned __int8)(v1 & 0xF) << (v2 + 3));
v4 = -(exp_lut_2[v2] + ((v1 & 0xF) << (v2 + 3)));
if ( v1 < 0 )
result = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
char v1; // di
unsigned __int8 v2; // dl
__int64 result; // rax
unsigned int v4; // edx

v1 = ~a1;
v2 = ((unsigned __int8)v1 >> 4) & 7;
result = exp_lut_2[v2] + ((unsigned __int8)(v1 & 0xF) << (v2 + 3));
v4 = -(exp_lut_2[v2] + ((v1 & 0xF) << (v2 + 3)));
if ( v1 < 0 )
result = v4;
return result;
}

original_code:
int ulaw2linear (unsigned char ulawbyte)
     {
     static int exp_lut[8] = {0, 132, 396, 924, 1980, 4092, 8316, 16764};
     int sign;
     int exponent;
     int mantissa;
     int sample;
     ulawbyte = ~ulawbyte;
     sign = (ulawbyte & 0x80);
     exponent = (ulawbyte >> 4) & 0x07;
     mantissa = ulawbyte & 0x0f;
     sample = exp_lut[exponent] + (mantissa << (exponent + 3));
     if (sign != 0)
          sample = -sample;
     return(sample);
     }

canonical_original_code:
int ulaw2linear(unsigned char ulawbyte)
     {
     static int exp_lut[8] = {0, 132, 396, 924, 1980, 4092, 8316, 16764};
     int sign;
     int exponent;
     int mantissa;
     int sample;
     ulawbyte = ~ulawbyte;
     sign = (ulawbyte & 0x80);
     exponent = (ulawbyte >> 4) & 0x07;
     mantissa = ulawbyte & 0x0f;
     sample = exp_lut[exponent] + (mantissa << (exponent + 3));
     if (sign != 0)
          sample = -sample;
     return(sample);
     }


decompiled_code:
__int64 __fastcall ulaw2alaw(unsigned __int8 a1)
{
return (unsigned __int8)ulaw_to_alaw_1[a1];
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
return (unsigned __int8)ulaw_to_alaw_1[a1];
}


decompiled_code:
__int64 __fastcall alaw2ulaw(unsigned __int8 a1)
{
return (unsigned __int8)alaw_to_ulaw_0[a1];
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1)
{
return (unsigned __int8)alaw_to_ulaw_0[a1];
}

variable_types:
alawbyte
unsigned char


decompiled_code:
__int64 voice_mode_on()
{
lprintf(5LL, "%s: entering voice mode", program_name);
tio_set(voice_fd, &voice_tio);
voice_install_signal_handler();
(*(void (**)(void))(voice_modem + 432LL))();
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
lprintf(5LL, "%s: entering voice mode", program_name);
tio_set(voice_fd, &voice_tio);
voice_install_signal_handler();
(*(void (**)(void))(voice_modem + 432LL))();
return 0LL;
}

original_code:
int voice_mode_on(void)
     {
     lprintf(5, "%s: entering voice mode", program_name);
     tio_set(voice_fd, &voice_tio);
     voice_install_signal_handler();
     voice_modem->voice_mode_on();
     return((0));
     }

canonical_original_code:
int voice_mode_on()
     {
     lprintf(5, "%s: entering voice mode", program_name);
     tio_set(voice_fd, &voice_tio);
     voice_install_signal_handler();
     voice_modem->voice_mode_on();
     return((0));
     }


decompiled_code:
__int64 voice_mode_off()
{
lprintf(5LL, "%s: leaving voice mode", program_name);
(*(void (**)(void))(voice_modem + 424LL))();
voice_restore_signal_handler();
tio_set(voice_fd, &tio_save);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
lprintf(5LL, "%s: leaving voice mode", program_name);
(*(void (**)(void))(voice_modem + 424LL))();
voice_restore_signal_handler();
tio_set(voice_fd, &tio_save);
return 0LL;
}

original_code:
int voice_mode_off(void)
     {
     lprintf(5, "%s: leaving voice mode", program_name);
     voice_modem->voice_mode_off();
     voice_restore_signal_handler();
     tio_set(voice_fd, &tio_save);
     return((0));
     }

canonical_original_code:
int voice_mode_off()
     {
     lprintf(5, "%s: leaving voice mode", program_name);
     voice_modem->voice_mode_off();
     voice_restore_signal_handler();
     tio_set(voice_fd, &tio_save);
     return((0));
     }


decompiled_code:
__int64 __fastcall enter_data_fax_mode(char a1)
{
const char *v1; // r9
int v2; // ebp
int v3; // ebx
const char *v4; // rcx
const char *v5; // rbx
unsigned int v6; // er12
__int64 result; // rax

v1 = "FAX";
if ( (a1 & 2) == 0 )
v1 = (const char *)&unk_3BD;
v2 = a1 & 3;
if ( v2 == 3 )
{
v3 = a1 & 1;
if ( (a1 & 1) != 0 )
lprintf(6LL, "%s: asked %s%s%s", program_name, "DATA", "/", v1);
else
lprintf(6LL, "%s: asked %s%s%s", program_name, (const char *)&unk_3BD, "/", v1);
}
else
{
v3 = a1 & 1;
v4 = (const char *)&unk_3BD;
if ( (a1 & 1) != 0 )
v4 = "DATA";
lprintf(6LL, "%s: asked %s%s%s", program_name, v4, (const char *)&unk_3BD, v1);
if ( (a1 & 3) == 0 )
{
lprintf(5LL, "%s: answer mode is set to not accept data or fax connections", program_name);
return 0xFFFFFFFFLL;
}
}
if ( modem_type == 4 )
{
v5 = "2";
v6 = 0;
}
else
{
if ( modem_type != 5 )
{
if ( !v3 )
{
lprintf(5LL, "%s: modem does not support wanted mode", program_name);
return 0xFFFFFFFFLL;
}
goto LABEL_12;
}
v5 = "2.0";
v6 = 1;
}
if ( v2 != 2 )
{
if ( v2 == 3 )
{
lprintf(6LL, "%s: trying data and fax connection", program_name);
if ( modem_type == 4 )
{
v5 = "2";
if ( (modem_quirks & 1) == 0 )
v5 = "0";
}
result = (*(__int64 (__fastcall **)(const char *))(voice_modem + 416LL))(v5);
if ( (_DWORD)result != -1 )
{
if ( (unsigned int)voice_command("AT+FAA=1", "OK") == 4096 )
goto LABEL_24;
result = 0xFFFFFFFFLL;
}
return result;
}
if ( v2 != 1 )
return 0LL;
LABEL_12:
lprintf(6LL, "%s: trying data connection", program_name);
result = (*(__int64 (__fastcall **)(const char *))(voice_modem + 416LL))("0");
if ( (_DWORD)result != -1 )
{
if ( (unsigned int)voice_command("AT+FAA=0", "OK") == 4096 )
{
tio_set(voice_fd, &tio_save);
voice_restore_signal_handler();
result = 0LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}
lprintf(6LL, "%s: trying fax connection", program_name);
result = (*(__int64 (__fastcall **)(const char *))(voice_modem + 416LL))(v5);
if ( (_DWORD)result != -1 )
{
if ( (unsigned int)voice_command("AT+FAA=0", "OK") == 4096 )
{
LABEL_24:
tio_set(voice_fd, &tio_save);
voice_restore_signal_handler();
fax_set_bor(voice_fd, v6);
return 0LL;
}
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
const char *v1; // r9
int v2; // ebp
int v3; // ebx
const char *v4; // rcx
const char *v5; // rbx
unsigned int v6; // er12
__int64 result; // rax

v1 = "FAX";
if ( (a1 & 2) == 0 )
v1 = (const char *)&unk_3BD;
v2 = a1 & 3;
if ( v2 == 3 )
{
v3 = a1 & 1;
if ( (a1 & 1) != 0 )
lprintf(6LL, "%s: asked %s%s%s", program_name, "DATA", "/", v1);
else
lprintf(6LL, "%s: asked %s%s%s", program_name, (const char *)&unk_3BD, "/", v1);
}
else
{
v3 = a1 & 1;
v4 = (const char *)&unk_3BD;
if ( (a1 & 1) != 0 )
v4 = "DATA";
lprintf(6LL, "%s: asked %s%s%s", program_name, v4, (const char *)&unk_3BD, v1);
if ( (a1 & 3) == 0 )
{
lprintf(5LL, "%s: answer mode is set to not accept data or fax connections", program_name);
return 0xFFFFFFFFLL;
}
}
if ( modem_type == 4 )
{
v5 = "2";
v6 = 0;
}
else
{
if ( modem_type != 5 )
{
if ( !v3 )
{
lprintf(5LL, "%s: modem does not support wanted mode", program_name);
return 0xFFFFFFFFLL;
}
goto LABEL_12;
}
v5 = "2.0";
v6 = 1;
}
if ( v2 != 2 )
{
if ( v2 == 3 )
{
lprintf(6LL, "%s: trying data and fax connection", program_name);
if ( modem_type == 4 )
{
v5 = "2";
if ( (modem_quirks & 1) == 0 )
v5 = "0";
}
result = (*(__int64 (__fastcall **)(const char *))(voice_modem + 416LL))(v5);
if ( (_DWORD)result != -1 )
{
if ( (unsigned int)voice_command("AT+FAA=1", "OK") == 4096 )
goto LABEL_24;
result = 0xFFFFFFFFLL;
}
return result;
}
if ( v2 != 1 )
return 0LL;
LABEL_12:
lprintf(6LL, "%s: trying data connection", program_name);
result = (*(__int64 (__fastcall **)(const char *))(voice_modem + 416LL))("0");
if ( (_DWORD)result != -1 )
{
if ( (unsigned int)voice_command("AT+FAA=0", "OK") == 4096 )
{
tio_set(voice_fd, &tio_save);
voice_restore_signal_handler();
result = 0LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}
lprintf(6LL, "%s: trying fax connection", program_name);
result = (*(__int64 (__fastcall **)(const char *))(voice_modem + 416LL))(v5);
if ( (_DWORD)result != -1 )
{
if ( (unsigned int)voice_command("AT+FAA=0", "OK") == 4096 )
{
LABEL_24:
tio_set(voice_fd, &tio_save);
voice_restore_signal_handler();
fax_set_bor(voice_fd, v6);
return 0LL;
}
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int enter_data_fax_mode(int answer_mode)
     {
     int bit_order = 0;
     char *fax_mode = ((void *)0);
     lprintf(6, "%s: asked %s%s%s",
             program_name,
             (answer_mode & (1)) ? "DATA" : "",
             ((answer_mode & ((1)|(2)))
              == ((1)|(2))) ? "/" : "",
             (answer_mode & (2)) ? "FAX" : "");
     answer_mode &= ((1) | (2));
     if (answer_mode == 0)
          {
          lprintf(5,
           "%s: answer mode is set to not accept data or fax connections",
           program_name);
          return ((-1));
          };
     if (modem_type == Mt_class2)
          {
          bit_order = 0;
          fax_mode = "2";
          };
     if (modem_type == Mt_class2_0)
          {
          bit_order = 1;
          fax_mode = "2.0";
          };
     if ((modem_type == Mt_data) || (fax_mode == ((void *)0)))
          answer_mode &= (1);
     if (answer_mode == 0)
          {
          lprintf(5, "%s: modem does not support wanted mode",
           program_name);
          return((-1));
          };
     switch (answer_mode)
          {
          case (1) | (2):
               lprintf(6, "%s: trying data and fax connection",
                program_name);
               if (modem_type == Mt_class2)
                    fax_mode = ( modem_quirks & 0x01 ) ? "2": "0";
               if (voice_modem->switch_to_data_fax(fax_mode) == (-1))
                    return((-1));
               if (voice_command("AT+FAA=1", "OK") != (0x1000))
                    return((-1));
               tio_set(voice_fd, &tio_save);
               voice_restore_signal_handler();
               fax_set_bor(voice_fd, bit_order);
               break;
          case (1):
               lprintf(6, "%s: trying data connection", program_name);
               if (voice_modem->switch_to_data_fax("0") == (-1))
                    return((-1));
               if (voice_command("AT+FAA=0", "OK") != (0x1000))
                    return((-1));
               tio_set(voice_fd, &tio_save);
               voice_restore_signal_handler();
               break;
          case (2):
               lprintf(6, "%s: trying fax connection", program_name);
               if (voice_modem->switch_to_data_fax(fax_mode) == (-1))
                    return((-1));
               if (voice_command("AT+FAA=0", "OK") != (0x1000))
                    return((-1));
               tio_set(voice_fd, &tio_save);
               voice_restore_signal_handler();
               fax_set_bor(voice_fd, bit_order);
               break;
          };
     return((0));
     }


decompiled_code:
__int64 get_current_users()
{
unsigned int i; // ebx
struct utmp *v1; // rax

setutent();
for ( i = 0; ; i += v1->ut_type == 7 )
{
v1 = getutent();
if ( !v1 )
break;
}
endutent();
return i;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int i; // ebx
struct utmp *v1; // rax

setutent();
for ( i = 0; ; i += v1->ut_type == 7 )
{
v1 = getutent();
if ( !v1 )
break;
}
endutent();
return i;
}

original_code:
int get_current_users (void)
{
    struct utmp * utmp;
    int Nusers = 0;
    setutent();
    while ((utmp = getutent()) != (struct utmp *) ((void *)0))
    {
 if (utmp->ut_type == 7)
 {
     Nusers++;
 }
    }
    endutent();
    return Nusers;
}

canonical_original_code:
int get_current_users()
{
    struct utmp *utmp;
    int Nusers = 0;
    setutent();
    while ((utmp = getutent()) != (struct utmp *) ((void *)0))
    {
 if (utmp->ut_type == 7)
 {
     Nusers++;
 }
    }
    endutent();
    return Nusers;
}

variable_types: struct utmp { short ut_type; int ut_pid; char ut_line[32]; char ut_id[4]; char ut_user[32]; char ut_host[256]; struct exit_status { short e_termination; short e_exit; } ut_exit; int ut_session; struct { int tv_sec; int tv_usec; } ut_tv; int ut_addr_v6[4]; char __glibc_reserved[20]; } *

variable_types: Nusers int

user_defined_types: struct utmp { short ut_type; int ut_pid; char ut_line[32]; char ut_id[4]; char ut_user[32]; char ut_host[256]; struct exit_status ut_exit; int ut_session; struct { int tv_sec; int tv_usec; } ut_tv; int ut_addr_v6[4]; char __glibc_reserved[20]; }


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

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

original_code: int fax1_set_fdcc( int fd, int fine, int max, int min ) { return 0; }

canonical_original_code: int fax1_set_fdcc(int fd, int fine, int max, int min) { return 0; }

variable_types: fd int

variable_types: fine int

variable_types: max int

variable_types: min int


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

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

original_code: int fax1_set_l_id( int fd, char * fax_id ) { return 0; }

canonical_original_code: int fax1_set_l_id(int fd, char *fax_id) { return 0; }


lputc

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

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

original_code: int lputc( int level, char ch ) { return 0; }

canonical_original_code: int lputc(int level, char ch) { return 0; }

variable_types: level int

variable_types: ch char


lputs

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

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

original_code: int lputs( int level, char * s ) { return 0; }

canonical_original_code: int lputs(int level, char *s) { return 0; }

variable_types:
s
char *


lprintf

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

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

original_code:
int lprintf( int level, const char * format, ...)
    { return 0; }

canonical_original_code:
int lprintf(int level, char *format, ...)
    { return 0; }

variable_types:
format
char *


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

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

original_code: char * fax_strerror( int h ) { return ((void *)0); }

canonical_original_code: char *fax_strerror(int h) { return ((void *)0); }

variable_types: h int

return_type: char *


main

canonical_name: main

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

v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "p:r");
if ( v5 == -1 )
break;
switch ( v5 )
{
case 'p':
v4 = atoi(optarg);
break;
case 'r':
v3 = 1;
break;
case '?':
fwrite("options: -p <padbytes>, -r\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
}
fax_init_swaptable(v3 ^ 1u, &fax_send_swaptable);
for ( i = optind; i < argc; optind = i )
{
if ( (int)g3_open_read(argv[i]) < 0 )
{
perror("g3_open");
exit(2);
}
if ( (unsigned int)g3_send_file(&g3_rf_chunk, 1LL, 0LL, 0LL, v4, 0xFFFFFFFFLL) )
{
perror("g3_send_file");
exit(3);
}
i = optind + 1;
}
exit(0);
}

original_code:
int main( int argc, char ** argv )
{
int opt;
int pad_bytes = 0;
int reverse = (1==0);
    while( (opt = getopt(argc, argv, "p:r" )) != (-1) )
    {
 switch(opt)
 {
   case 'p':
     pad_bytes = atoi(optarg);
     break;
   case 'r':
     reverse = (1==1);
     break;
   case '?':
     fprintf( stderr, "options: -p <padbytes>, -r\n" );
     exit(1);
 }
    }
    fax_init_swaptable( !reverse, fax_send_swaptable );
    while( optind < argc )
    {
        if ( g3_open_read( argv[optind] ) < 0 )
 {
     perror( "g3_open" ); exit(2);
 }
 if ( g3_send_file( g3_rf_chunk, 1, (1==0),
                       (1==0), pad_bytes, -1 ) )
 {
     perror( "g3_send_file" ); exit(3);
 }
 optind++;
    }
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
int opt;
int pad_bytes = 0;
int reverse = (1==0);
    while( (opt = getopt(argc, argv, "p:r" )) != (-1) )
    {
 switch(opt)
 {
   case 'p':
     pad_bytes = atoi(optarg);
     break;
   case 'r':
     reverse = (1==1);
     break;
   case '?':
     fprintf( stderr, "options: -p <padbytes>, -r\n" );
     exit(1);
 }
    }
    fax_init_swaptable( !reverse, fax_send_swaptable );
    while( optind < argc )
    {
        if ( g3_open_read( argv[optind] ) < 0 )
 {
     perror( "g3_open" ); exit(2);
 }
 if ( g3_send_file( g3_rf_chunk, 1, (1==0),
                       (1==0), pad_bytes, -1 ) )
 {
     perror( "g3_send_file" ); exit(3);
 }
 optind++;
    }
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types: reverse int


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

result = 3LL;
if ( a1 <= 99 )
{
result = (unsigned int)a1;
if ( a1 )
result = (unsigned int)((3 * a1 / 10 + 10) / 10);
}
return result;
}

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

result = 3LL;
if ( a1 <= 99 )
{
result = (unsigned int)a1;
if ( a1 )
result = (unsigned int)((3 * a1 / 10 + 10) / 10);
}
return result;
}

original_code:
static int silence_threshold_compute(int threshold) {
   if (threshold >= 100) {
      return 3;
   }
   else if (threshold) {
      return (((threshold * 3 / 10) + 10) / 10);
   }
   else {
      return 0;
   }
}

variable_types: threshold int


decompiled_code:
__int64 USR_voice_mode_on()
{
unsigned int v0; // ebx
unsigned int v2; // eax

v0 = IS_101_voice_mode_on();
if ( !v0 )
{
v2 = silence_threshold_compute(cvd[116]);
sprintf(buffer_1, "AT#VTD=3F,3F,3F#VSD=1#VSS=%d#VSP=%d#VRA=%d#VRN=%d", v2, cvd[110], cvd[398], cvd[404]);
if ( (unsigned int)voice_command(buffer_1, "OK") != 4096 )
lprintf(3LL, "setting voice preferences didn't work");
}
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx
unsigned int v2; // eax

v0 = IS_101_voice_mode_on();
if ( !v0 )
{
v2 = func0(cvd[116]);
sprintf(buffer_1, "AT#VTD=3F,3F,3F#VSD=1#VSS=%d#VSP=%d#VRA=%d#VRN=%d", v2, cvd[110], cvd[398], cvd[404]);
if ( (unsigned int)voice_command(buffer_1, "OK") != 4096 )
lprintf(3LL, "setting voice preferences didn't work");
}
return v0;
}

original_code:
static int USR_voice_mode_on(void)
     {
     int ret;
     static char buffer[(256)];
     if( (ret=IS_101_voice_mode_on( )) != (0) )
          return( ret );
     sprintf(buffer, "AT#VTD=3F,3F,3F#VSD=1#VSS=%d#VSP=%d#VRA=%d#VRN=%d",
             silence_threshold_compute(cvd.rec_silence_threshold.d.i),
             (int)cvd.rec_silence_len.d.i,
             (int)cvd.ringback_goes_away.d.i, (int)cvd.ringback_never_came.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3,"setting voice preferences didn't work");
     return((0));
     }

canonical_original_code:
static int USR_voice_mode_on()
     {
     int ret;
     static char buffer[-1];
     if( (ret=IS_101_voice_mode_on( )) != (0) )
          return( ret );
     sprintf(buffer, "AT#VTD=3F,3F,3F#VSD=1#VSS=%d#VSP=%d#VRA=%d#VRN=%d",
             silence_threshold_compute(cvd.rec_silence_threshold.d.i),
             (int)cvd.rec_silence_len.d.i,
             (int)cvd.ringback_goes_away.d.i, (int)cvd.ringback_never_came.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3,"setting voice preferences didn't work");
     return((0));
     }

variable_types: ret int

variable_types:
buffer
char [-1]


decompiled_code:
__int64 __fastcall USR_set_compression(__int64 *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 result; // rax
int v6; // eax
__int64 v7; // rdx
char v8[280]; // [rsp+0h] [rbp-118h] BYREF

if ( !*a1 )
*a1 = 1LL;
v4 = *a2;
if ( *a2 )
{
if ( v4 != 8000 )
{
lprintf(3LL, "%s: Illegal sample rate (%d)", (const char *)*voice_modem, v4);
return 0xFFFFFFFFLL;
}
}
else
{
*a2 = 8000LL;
}
if ( !probed_for_adpcm )
{
if ( *a1 == 1 )
{
LABEL_7:
*a3 = 8;
result = (unsigned int)supports_adpcm;
if ( supports_adpcm )
{
result = (unsigned int)in_adpcm_mode;
if ( in_adpcm_mode )
{
result = voice_command("AT#VSM=128,8000", "OK");
if ( (_DWORD)result != 0x4000 )
{
in_adpcm_mode = 0;
result = 0LL;
}
}
}
return result;
}
v6 = voice_command("AT#VSM=129,8000", "OK");
supports_adpcm = v6 == 4096;
if ( v6 == 4096 )
in_adpcm_mode = 1;
else
lprintf(3LL, "%s: Ignore the above error!", program_name);
probed_for_adpcm = 1;
}
if ( *a1 )
{
v7 = *a1;
if ( *a1 == 1 )
goto LABEL_7;
if ( (unsigned __int64)(v7 - 2) > 2 )
{
lprintf(3LL, "Illegal voice compression method (%d)", v7);
return 0xFFFFFFFFLL;
}
}
else
{
if ( !supports_adpcm )
{
*a1 = 1LL;
goto LABEL_7;
}
*a1 = 2LL;
LODWORD(v7) = 2;
}
*a3 = v7;
if ( supports_adpcm && !in_adpcm_mode )
{
result = voice_command("AT#VSM=129,8000", "OK");
if ( (_DWORD)result == 0x4000 )
return result;
in_adpcm_mode = 1;
}
sprintf(v8, "AT#VBS=%1d", *a1);
result = voice_command(v8, "OK");
if ( (_DWORD)result != 0x4000 )
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 result; // rax
int v6; // eax
__int64 v7; // rdx
char v8[280]; // [rsp+0h] [rbp-118h] BYREF

if ( !*a1 )
*a1 = 1LL;
v4 = *a2;
if ( *a2 )
{
if ( v4 != 8000 )
{
lprintf(3LL, "%s: Illegal sample rate (%d)", (const char *)*voice_modem, v4);
return 0xFFFFFFFFLL;
}
}
else
{
*a2 = 8000LL;
}
if ( !probed_for_adpcm )
{
if ( *a1 == 1 )
{
LABEL_7:
*a3 = 8;
result = (unsigned int)supports_adpcm;
if ( supports_adpcm )
{
result = (unsigned int)in_adpcm_mode;
if ( in_adpcm_mode )
{
result = voice_command("AT#VSM=128,8000", "OK");
if ( (_DWORD)result != 0x4000 )
{
in_adpcm_mode = 0;
result = 0LL;
}
}
}
return result;
}
v6 = voice_command("AT#VSM=129,8000", "OK");
supports_adpcm = v6 == 4096;
if ( v6 == 4096 )
in_adpcm_mode = 1;
else
lprintf(3LL, "%s: Ignore the above error!", program_name);
probed_for_adpcm = 1;
}
if ( *a1 )
{
v7 = *a1;
if ( *a1 == 1 )
goto LABEL_7;
if ( (unsigned __int64)(v7 - 2) > 2 )
{
lprintf(3LL, "Illegal voice compression method (%d)", v7);
return 0xFFFFFFFFLL;
}
}
else
{
if ( !supports_adpcm )
{
*a1 = 1LL;
goto LABEL_7;
}
*a1 = 2LL;
LODWORD(v7) = 2;
}
*a3 = v7;
if ( supports_adpcm && !in_adpcm_mode )
{
result = voice_command("AT#VSM=129,8000", "OK");
if ( (_DWORD)result == 0x4000 )
return result;
in_adpcm_mode = 1;
}
sprintf(v8, "AT#VBS=%1d", *a1);
result = voice_command(v8, "OK");
if ( (_DWORD)result != 0x4000 )
result = 0LL;
return result;
}

original_code:
static int USR_set_compression(p_int *compression, p_int *speed, int *bits)
     {
     char buffer[(256)];
     if (*compression == 0)
          *compression = 1;
     if (*speed == 0)
          *speed = 8000;
     if (*speed != 8000)
          {
          lprintf(3, "%s: Illegal sample rate (%d)",
           voice_modem->name, (int)*speed);
          return((-1));
          };
     if ((!probed_for_adpcm) && (*compression != 1))
          {
          if (!(supports_adpcm =
           (voice_command("AT#VSM=129,8000", "OK") == (0x1000))))
               lprintf(3,"%s: Ignore the above error!", program_name);
          else
               in_adpcm_mode = (0==0);
          probed_for_adpcm = (0==0);
          }
     if (*compression == 0)
          {
          if (supports_adpcm)
               *compression = 2;
          else
               *compression = 1;
          }
     switch (*compression)
          {
          case 1:
               *bits = 8;
               if ((supports_adpcm) && (in_adpcm_mode))
                    {
                    if (voice_command("AT#VSM=128,8000", "OK") != (0x4000))
                         {
                         in_adpcm_mode = (0==1);
                         return((0));
                         }
                    else
                         return((0x4000));
                    }
               return((0));
          case 2:
          case 3:
          case 4:
               *bits = *compression;
               if ((supports_adpcm) && (!in_adpcm_mode))
                    {
                    if (voice_command("AT#VSM=129,8000", "OK") == (0x4000))
                         return((0x4000));
                    in_adpcm_mode = (0==0);
                    }
               sprintf(buffer,"AT#VBS=%1d", (int)*compression);
               if (voice_command(buffer, "OK") != (0x4000))
                    return((0));
               else
                    return((0x4000));
          }
     lprintf(3,"Illegal voice compression method (%d)", *compression);
     return((-1));
     }

canonical_original_code:
static int USR_set_compression(long *compression, long *speed, int *bits)
     {
     char buffer[-1];
     if (*compression == 0)
          *compression = 1;
     if (*speed == 0)
          *speed = 8000;
     if (*speed != 8000)
          {
          lprintf(3, "%s: Illegal sample rate (%d)",
           voice_modem->name, (int)*speed);
          return((-1));
          };
     if ((!probed_for_adpcm) && (*compression != 1))
          {
          if (!(supports_adpcm =
           (voice_command("AT#VSM=129,8000", "OK") == (0x1000))))
               lprintf(3,"%s: Ignore the above error!", program_name);
          else
               in_adpcm_mode = (0==0);
          probed_for_adpcm = (0==0);
          }
     if (*compression == 0)
          {
          if (supports_adpcm)
               *compression = 2;
          else
               *compression = 1;
          }
     switch (*compression)
          {
          case 1:
               *bits = 8;
               if ((supports_adpcm) && (in_adpcm_mode))
                    {
                    if (voice_command("AT#VSM=128,8000", "OK") != (0x4000))
                         {
                         in_adpcm_mode = (0==1);
                         return((0));
                         }
                    else
                         return((0x4000));
                    }
               return((0));
          case 2:
          case 3:
          case 4:
               *bits = *compression;
               if ((supports_adpcm) && (!in_adpcm_mode))
                    {
                    if (voice_command("AT#VSM=129,8000", "OK") == (0x4000))
                         return((0x4000));
                    in_adpcm_mode = (0==0);
                    }
               sprintf(buffer,"AT#VBS=%1d", (int)*compression);
               if (voice_command(buffer, "OK") != (0x4000))
                    return((0));
               else
                    return((0x4000));
          }
     lprintf(3,"Illegal voice compression method (%d)", *compression);
     return((-1));
     }

variable_types:
compression
long *

variable_types:
speed
long *

variable_types:
bits
int *


decompiled_code:
__int64 __fastcall USR_switch_to_data_fax(const char *a1)
{
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

reset_watchdog();
(*(void (**)(void))(voice_modem + 424LL))();
sprintf(v2, "AT+FCLASS=%s", a1);
return (unsigned int)-((unsigned int)voice_command(v2, "OK") != 4096);
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

reset_watchdog();
(*(void (**)(void))(voice_modem + 424LL))();
sprintf(v2, "AT+FCLASS=%s", a1);
return (unsigned int)-((unsigned int)voice_command(v2, "OK") != 4096);
}

original_code:
static int USR_switch_to_data_fax (char *mode)
     {
     char buffer[(256)];
     reset_watchdog();
     voice_modem->voice_mode_off();
     sprintf(buffer, "AT+FCLASS=%s", mode);
     if (voice_command(buffer, "OK") != (0x1000))
          return((-1));
     return((0));
     }

canonical_original_code:
static int USR_switch_to_data_fax(char *mode)
     {
     char buffer[-1];
     reset_watchdog();
     voice_modem->voice_mode_off();
     sprintf(buffer, "AT+FCLASS=%s", mode);
     if (voice_command(buffer, "OK") != (0x1000))
          return((-1));
     return((0));
     }

variable_types:
mode
char *


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

reset_watchdog();
internal_speaker_used = 0;
switch ( a1 )
{
case 1u:
voice_command("AT#VLS=0H0", "OK|VCON");
result = 0LL;
break;
case 2u:
voice_command("AT#VLS=0", "OK|VCON");
result = 0LL;
break;
case 3u:
case 7u:
voice_command("AT#VLS=1", "OK|VCON");
result = 0LL;
break;
case 4u:
voice_command("AT#VLS=3", "OK|VCON");
result = 0LL;
break;
case 5u:
internal_speaker_used = 1;
voice_command("AT#VLS=2", "OK|VCON");
result = 0LL;
break;
case 6u:
internal_speaker_used = 1;
voice_command("AT#VLS=4", "OK|VCON");
result = 0LL;
break;
default:
lprintf(3LL, "USR: Unknown output device (%d)", a1);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

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

reset_watchdog();
internal_speaker_used = 0;
switch ( a1 )
{
case 1u:
voice_command("AT#VLS=0H0", "OK|VCON");
result = 0LL;
break;
case 2u:
voice_command("AT#VLS=0", "OK|VCON");
result = 0LL;
break;
case 3u:
case 7u:
voice_command("AT#VLS=1", "OK|VCON");
result = 0LL;
break;
case 4u:
voice_command("AT#VLS=3", "OK|VCON");
result = 0LL;
break;
case 5u:
internal_speaker_used = 1;
voice_command("AT#VLS=2", "OK|VCON");
result = 0LL;
break;
case 6u:
internal_speaker_used = 1;
voice_command("AT#VLS=4", "OK|VCON");
result = 0LL;
break;
default:
lprintf(3LL, "USR: Unknown output device (%d)", a1);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
static int USR_set_device(int device)
     {
     reset_watchdog();
     internal_speaker_used = (0==1);
     switch (device)
          {
          case (0x0001):
               voice_command("AT#VLS=0H0","OK|VCON");
               return((0));
          case (0x0002):
               voice_command("AT#VLS=0","OK|VCON");
               return((0));
   case (0x0007):
       voice_command("AT#VLS=1","OK|VCON");
       return((0));
          case (0x0003):
               voice_command("AT#VLS=1","OK|VCON");
               return((0));
          case (0x0006):
               internal_speaker_used = (0==0);
               voice_command("AT#VLS=4","OK|VCON");
               return((0));
          case (0x0005):
               internal_speaker_used = (0==0);
               voice_command("AT#VLS=2","OK|VCON");
               return((0));
          case (0x0004):
               voice_command("AT#VLS=3","OK|VCON");
               return((0));
          default:
               lprintf(3,"USR: Unknown output device (%d)",device);
               return((-1));
          };
     }

variable_types: device int


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

v0 = IS_101_stop_play_file();
if ( *(_QWORD *)&cvd[170] )
{
if ( (unsigned int)voice_write_raw("ATE1\r", 5LL) )
return (unsigned int)-1;
if ( (voice_command(&unk_DE6, "OK|VCON") & 0x1000) == 0 )
return (unsigned int)-1;
}
if ( (unsigned int)voice_command("AT", "OK") != 4096 )
return (unsigned int)-1;
voice_modem_state = 1;
voice_check_events();
if ( v0 )
v0 = 19712;
return v0;
}

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

v0 = IS_101_stop_play_file();
if ( *(_QWORD *)&cvd[170] )
{
if ( (unsigned int)voice_write_raw("ATE1\r", 5LL) )
return (unsigned int)-1;
if ( (voice_command(&unk_DE6, "OK|VCON") & 0x1000) == 0 )
return (unsigned int)-1;
}
if ( (unsigned int)voice_command("AT", "OK") != 4096 )
return (unsigned int)-1;
voice_modem_state = 1;
voice_check_events();
if ( v0 )
v0 = 19712;
return v0;
}

original_code:
static int USR_stop_play_file(void)
     {
     int stop_playing = IS_101_stop_play_file();
     if (cvd.enable_command_echo.d.i)
          {
          if (voice_write_raw("ATE1\r",5) != (0))
               return((-1));
          if ((voice_command("", "OK|VCON") & (0x1000)) != (0x1000))
               return((-1));
          }
     if (voice_command("AT", "OK") != (0x1000))
          return((-1));
     voice_modem_state = (0x0001);
     voice_check_events();
     if (stop_playing)
          return((0x4d00));
     return((0));
     }

canonical_original_code:
static int USR_stop_play_file()
     {
     int stop_playing = IS_101_stop_play_file();
     if (cvd.enable_command_echo.d.i)
          {
          if (voice_write_raw("ATE1\r",5) != (0))
               return((-1));
          if ((voice_command("", "OK|VCON") & (0x1000)) != (0x1000))
               return((-1));
          }
     if (voice_command("AT", "OK") != (0x1000))
          return((-1));
     voice_modem_state = (0x0001);
     voice_check_events();
     if (stop_playing)
          return((0x4d00));
     return((0));
     }


decompiled_code:
__int64 USR_init()
{
unsigned int v0; // eax
_BYTE v2[72]; // [rsp+0h] [rbp-48h] BYREF

reset_watchdog();
lprintf(4LL, "US Robotics voice modem");
lprintf(3LL, "This is a driver beta version. V0.4.b3");
voice_modem_state = 2;
if ( (unsigned int)voice_command("AT&H1&R2&I0", "OK") == 4096 )
{
tio_get(voice_fd, v2);
tio_set_flow_control(voice_fd, v2, 1LL);
tio_set(voice_fd, v2);
}
else
{
lprintf(3LL, "can't turn on hardware flow control");
}
if ( (unsigned int)voice_command("AT#VTD=3F,3F,3F", "OK") == 4096 )
lprintf(3LL, "VTD setup successful");
else
lprintf(3LL, "can't set DLE responses");
v0 = silence_threshold_compute(cvd[116]);
sprintf(buffer_0, "AT#VSD=1#VSS=%d#VSP=%d", v0, cvd[110]);
if ( (unsigned int)voice_command(buffer_0, "OK") != 4096 )
lprintf(3LL, "setting recording preferences didn't work");
sprintf(buffer_0, "AT#VRA=%d#VRN=%d", cvd[398], cvd[404]);
if ( (unsigned int)voice_command(buffer_0, "OK") != 4096 )
lprintf(3LL, "setting ringback delay didn't work");
voice_modem_state = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int v0; // eax
_BYTE v2[72]; // [rsp+0h] [rbp-48h] BYREF

reset_watchdog();
lprintf(4LL, "US Robotics voice modem");
lprintf(3LL, "This is a driver beta version. V0.4.b3");
voice_modem_state = 2;
if ( (unsigned int)voice_command("AT&H1&R2&I0", "OK") == 4096 )
{
tio_get(voice_fd, v2);
tio_set_flow_control(voice_fd, v2, 1LL);
tio_set(voice_fd, v2);
}
else
{
lprintf(3LL, "can't turn on hardware flow control");
}
if ( (unsigned int)voice_command("AT#VTD=3F,3F,3F", "OK") == 4096 )
lprintf(3LL, "VTD setup successful");
else
lprintf(3LL, "can't set DLE responses");
v0 = func0(cvd[116]);
sprintf(buffer_0, "AT#VSD=1#VSS=%d#VSP=%d", v0, cvd[110]);
if ( (unsigned int)voice_command(buffer_0, "OK") != 4096 )
lprintf(3LL, "setting recording preferences didn't work");
sprintf(buffer_0, "AT#VRA=%d#VRN=%d", cvd[398], cvd[404]);
if ( (unsigned int)voice_command(buffer_0, "OK") != 4096 )
lprintf(3LL, "setting ringback delay didn't work");
voice_modem_state = 1;
return 0LL;
}

original_code:
static int USR_init(void)
     {
     static char buffer[(256)];
     reset_watchdog();
     lprintf(4, "US Robotics voice modem");
     lprintf(3, "This is a driver beta version. V0.4.b3");
     voice_modem_state = (0x0002);
     if (voice_command("AT&H1&R2&I0", "OK") == (0x1000))
          {
          TIO tio;
          tio_get(voice_fd, &tio);
          tio_set_flow_control(voice_fd, &tio, 0x01);
          tio_set(voice_fd, &tio);
          }
     else
          lprintf(3,"can't turn on hardware flow control");
     if (voice_command("AT#VTD=3F,3F,3F", "OK") != (0x1000))
          lprintf(3, "can't set DLE responses");
     else
          lprintf(3, "VTD setup successful");
     sprintf(buffer, "AT#VSD=1#VSS=%d#VSP=%d",
             silence_threshold_compute(cvd.rec_silence_threshold.d.i),
             (int)cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3,"setting recording preferences didn't work");
     sprintf(buffer, "AT#VRA=%d#VRN=%d",
      (int)cvd.ringback_goes_away.d.i, (int)cvd.ringback_never_came.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3,"setting ringback delay didn't work");
     voice_modem_state = (0x0001);
     return((0));
     }

canonical_original_code:
static int USR_init()
     {
     static char buffer[-1];
     reset_watchdog();
     lprintf(4, "US Robotics voice modem");
     lprintf(3, "This is a driver beta version. V0.4.b3");
     voice_modem_state = (0x0002);
     if (voice_command("AT&H1&R2&I0", "OK") == (0x1000))
          {
          struct termios tio;
          tio_get(voice_fd, &tio);
          tio_set_flow_control(voice_fd, &tio, 0x01);
          tio_set(voice_fd, &tio);
          }
     else
          lprintf(3,"can't turn on hardware flow control");
     if (voice_command("AT#VTD=3F,3F,3F", "OK") != (0x1000))
          lprintf(3, "can't set DLE responses");
     else
          lprintf(3, "VTD setup successful");
     sprintf(buffer, "AT#VSD=1#VSS=%d#VSP=%d",
             silence_threshold_compute(cvd.rec_silence_threshold.d.i),
             (int)cvd.rec_silence_len.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3,"setting recording preferences didn't work");
     sprintf(buffer, "AT#VRA=%d#VRN=%d",
      (int)cvd.ringback_goes_away.d.i, (int)cvd.ringback_never_came.d.i);
     if (voice_command(buffer, "OK") != (0x1000))
          lprintf(3,"setting ringback delay didn't work");
     voice_modem_state = (0x0001);
     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 USR_handle_dle(char a1)
{
__int64 result; // rax

if ( a1 != 100 && a1 != 105 )
return IS_101_handle_dle((unsigned int)a1);
if ( voice_modem_state == 5 )
{
lprintf(6LL, "USR_handle_dle: Ignoring <DLE> <%c> because the modem is PLAYING.", (unsigned int)a1);
result = 0LL;
}
else
{
if ( voice_modem_state != 6 )
return IS_101_handle_dle((unsigned int)a1);
lprintf(6LL, "USR_handle_dle: Ignoring <DLE> <%c> because the modem is RECORDING.", (unsigned int)a1);
result = 0LL;
}
return result;
}

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

if ( a1 != 100 && a1 != 105 )
return IS_101_handle_dle((unsigned int)a1);
if ( voice_modem_state == 5 )
{
lprintf(6LL, "USR_handle_dle: Ignoring <DLE> <%c> because the modem is PLAYING.", (unsigned int)a1);
result = 0LL;
}
else
{
if ( voice_modem_state != 6 )
return IS_101_handle_dle((unsigned int)a1);
lprintf(6LL, "USR_handle_dle: Ignoring <DLE> <%c> because the modem is RECORDING.", (unsigned int)a1);
result = 0LL;
}
return result;
}

original_code:
static int USR_handle_dle(char data)
   {
   if (data == 'd' || data == 'i')
      {
      switch (voice_modem_state)
           {
           case (0x0005):
                lprintf(6, "USR_handle_dle: Ignoring <DLE> <%c> because the modem is PLAYING.",data);
                return((0));
           case (0x0006):
                lprintf(6, "USR_handle_dle: Ignoring <DLE> <%c> because the modem is RECORDING.",data);
                return((0));
           }
      }
   return(IS_101_handle_dle(data));
   }

variable_types: data char


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

result = IS_101_beep();
if ( (_DWORD)result )
return 0xFFFFFFFFLL;
if ( !internal_speaker_used )
result = (unsigned int)-((unsigned int)IS_101_answer_phone() != 8202);
return result;
}

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

result = IS_101_beep();
if ( (_DWORD)result )
return 0xFFFFFFFFLL;
if ( !internal_speaker_used )
result = (unsigned int)-((unsigned int)IS_101_answer_phone() != 8202);
return result;
}

original_code:
static int USR_beep(int frequency, int length)
     {
     if (IS_101_beep(frequency, length) != (0))
          return((-1));
     if (internal_speaker_used) return((0));
     if (IS_101_answer_phone() != (0x200a))
          return((-1));
     return((0));
}

variable_types: frequency int

variable_types: length int


original_code:
void g3_init_lr_bittable (void)
{
signed int i,j;
    if ( bits_lr_init ) return;
    bits_left[0] = bits_right[0] = 8;
    for( i=0; i<=7; i++ )
    {
 for( j=(1<<(7-i))-1;j>=0; j--)
 {
     bits_left[ (1<<(7-i)) + j ] = i;
     bits_right[ ((j<<1)+1) << i ] = i;
 }
    }
    bits_lr_init = 1;
}

canonical_original_code:
void g3_init_lr_bittable()
{
int i; int j;
    if ( bits_lr_init ) return;
    bits_left[0] = bits_right[0] = 8;
    for( i=0; i<=7; i++ )
    {
 for( j=(1<<(7-i))-1;j>=0; j--)
 {
     bits_left[ (1<<(7-i)) + j ] = i;
     bits_right[ ((j<<1)+1) << i ] = i;
 }
    }
    bits_lr_init = 1;
}

variable_types: i int

variable_types: j int

return_type: void


decompiled_code:
__int64 __fastcall g3_open_read(char *file)
{
int v1; // eax
int v2; // eax
__int64 result; // rax

g3_init_lr_bittable();
v1 = open(file, 0);
g3_read_fd = v1;
if ( v1 < 0 )
{
lprintf(1LL, "g3_open_read: can't open '%s'", file);
result = 0xFFFFFFFFLL;
}
else
{
g3_rf_fine = -1;
g3_rf_idx = 0;
v2 = read(v1, &g3_rf_buf, 0x40uLL);
g3_rf_num = v2;
if ( v2 < 0 )
{
lprintf(1LL, "g3_open_read: can't read from '%s'", file);
close(g3_read_fd);
result = 0xFFFFFFFFLL;
}
else
{
if ( v2 > 63 && !strcmp(byte_7E1, "PC Research, Inc") )
{
lprintf(4LL, "skipping over DigiFax header");
g3_rf_idx = 64;
g3_rf_fine = byte_7FD != 0;
}
result = (unsigned int)g3_read_fd;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *file)
{
int v1; // eax
int v2; // eax
__int64 result; // rax

func0();
v1 = open(file, 0);
g3_read_fd = v1;
if ( v1 < 0 )
{
lprintf(1LL, "g3_open_read: can't open '%s'", file);
result = 0xFFFFFFFFLL;
}
else
{
g3_rf_fine = -1;
g3_rf_idx = 0;
v2 = read(v1, &g3_rf_buf, 0x40uLL);
g3_rf_num = v2;
if ( v2 < 0 )
{
lprintf(1LL, "g3_open_read: can't read from '%s'", file);
close(g3_read_fd);
result = 0xFFFFFFFFLL;
}
else
{
if ( v2 > 63 && !strcmp(byte_7E1, "PC Research, Inc") )
{
lprintf(4LL, "skipping over DigiFax header");
g3_rf_idx = 64;
g3_rf_fine = byte_7FD != 0;
}
result = (unsigned int)g3_read_fd;
}
}
return result;
}

original_code:
int g3_open_read (char * filename)
{
    g3_init_lr_bittable();
    g3_read_fd = open( filename, 00 );
    if ( g3_read_fd < 0 )
    {
 lprintf( 1, "g3_open_read: can't open '%s'", filename );
 return -1;
    }
    g3_rf_fine = -1;
    g3_rf_idx = 0;
    g3_rf_num = read( g3_read_fd, g3_rf_buf, sizeof(g3_rf_buf) );
    if ( g3_rf_num < 0 )
    {
 lprintf( 1, "g3_open_read: can't read from '%s'", filename );
 close( g3_read_fd );
 return -1;
    }
    if ( g3_rf_num >= 64 &&
  strcmp( g3_rf_buf+1, "PC Research, Inc" ) == 0 )
    {
 lprintf( 4, "skipping over DigiFax header" );
 g3_rf_idx = 64;
 g3_rf_fine = ( g3_rf_buf[29] != 0 );
    }
    return g3_read_fd;
}

canonical_original_code:
int g3_open_read(char *filename)
{
    g3_init_lr_bittable();
    g3_read_fd = open( filename, 00 );
    if ( g3_read_fd < 0 )
    {
 lprintf( 1, "g3_open_read: can't open '%s'", filename );
 return -1;
    }
    g3_rf_fine = -1;
    g3_rf_idx = 0;
    g3_rf_num = read( g3_read_fd, g3_rf_buf, sizeof(g3_rf_buf) );
    if ( g3_rf_num < 0 )
    {
 lprintf( 1, "g3_open_read: can't read from '%s'", filename );
 close( g3_read_fd );
 return -1;
    }
    if ( g3_rf_num >= 64 &&
  strcmp( g3_rf_buf+1, "PC Research, Inc" ) == 0 )
    {
 lprintf( 4, "skipping over DigiFax header" );
 g3_rf_idx = 64;
 g3_rf_fine = ( g3_rf_buf[29] != 0 );
    }
    return g3_read_fd;
}

variable_types:
filename
char *


decompiled_code:
int g3_close()
{
int result; // eax

if ( g3_read_fd >= 0 )
{
result = close(g3_read_fd);
g3_read_fd = -1;
g3_rf_idx = 0;
g3_rf_num = 0;
g3_rf_fine = -1;
}
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax

if ( g3_read_fd >= 0 )
{
result = close(g3_read_fd);
g3_read_fd = -1;
g3_rf_idx = 0;
g3_rf_num = 0;
g3_rf_fine = -1;
}
return result;
}

original_code:
void g3_close (void)
{
    if (g3_read_fd < 0 ) return;
    close(g3_read_fd);
    g3_read_fd = -1;
    g3_rf_idx = 0;
    g3_rf_num = 0;
    g3_rf_fine = -1;
}

canonical_original_code:
void g3_close()
{
    if (g3_read_fd < 0 ) return;
    close(g3_read_fd);
    g3_read_fd = -1;
    g3_rf_idx = 0;
    g3_rf_num = 0;
    g3_rf_fine = -1;
}


decompiled_code:
__int64 __fastcall g3_rf_chunk(char *a1, int a2)
{
int v2; // er14
int v3; // er13
unsigned int v4; // ebp
int v5; // ebx

v2 = g3_rf_idx;
if ( g3_rf_idx >= g3_rf_num )
{
v4 = 0;
LABEL_7:
v5 = read(g3_read_fd, &a1[v4], (int)(a2 - v4));
if ( v5 < 0 )
lprintf(1LL, "g3_read");
lprintf(6LL, "rp=%d, r=%d", v4, (unsigned int)v5);
v4 += v5;
return v4;
}
v3 = g3_rf_num - g3_rf_idx;
v4 = a2;
if ( g3_rf_num - g3_rf_idx <= a2 )
v4 = g3_rf_num - g3_rf_idx;
memcpy(a1, &g3_rf_buf, v3);
g3_rf_idx = v2 + v4;
if ( v3 < a2 )
goto LABEL_7;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, int a2)
{
int v2; // er14
int v3; // er13
unsigned int v4; // ebp
int v5; // ebx

v2 = g3_rf_idx;
if ( g3_rf_idx >= g3_rf_num )
{
v4 = 0;
LABEL_7:
v5 = read(g3_read_fd, &a1[v4], (int)(a2 - v4));
if ( v5 < 0 )
lprintf(1LL, "g3_read");
lprintf(6LL, "rp=%d, r=%d", v4, (unsigned int)v5);
v4 += v5;
return v4;
}
v3 = g3_rf_num - g3_rf_idx;
v4 = a2;
if ( g3_rf_num - g3_rf_idx <= a2 )
v4 = g3_rf_num - g3_rf_idx;
memcpy(a1, &g3_rf_buf, v3);
g3_rf_idx = v2 + v4;
if ( v3 < a2 )
goto LABEL_7;
return v4;
}

original_code:
int g3_rf_chunk (char * buf,int max)
{
int r, rp;
    rp = 0;
    if ( g3_rf_idx < g3_rf_num )
    {
 r = g3_rf_num - g3_rf_idx;
 if ( r > max ) { r=max; }
 memcpy( buf, g3_rf_buf, g3_rf_num-g3_rf_idx );
 g3_rf_idx += r;
 if ( r == max ) return r;
 rp = r;
    }
    r = read( g3_read_fd, buf+rp, max-rp );
    if ( r < 0 )
 { lprintf( 1, "g3_read" ); }
    lprintf( 6, "rp=%d, r=%d", rp, r );
    return r+rp;
}

canonical_original_code:
int g3_rf_chunk(char *buf, int max)
{
int r; int rp;
    rp = 0;
    if ( g3_rf_idx < g3_rf_num )
    {
 r = g3_rf_num - g3_rf_idx;
 if ( r > max ) { r=max; }
 memcpy( buf, g3_rf_buf, g3_rf_num-g3_rf_idx );
 g3_rf_idx += r;
 if ( r == max ) return r;
 rp = r;
    }
    r = read( g3_read_fd, buf+rp, max-rp );
    if ( r < 0 )
 { lprintf( 1, "g3_read" ); }
    lprintf( 6, "rp=%d, r=%d", rp, r );
    return r+rp;
}

variable_types:
buf
char *

variable_types: max int

variable_types: r int

variable_types: rp int


decompiled_code:
__int64 __fastcall g3_send_file(__int64 (__fastcall *a1)(char *, __int64), unsigned int a2, unsigned int a3, int a4, unsigned int a5, int a6)
{
const char *v8; // rcx
const char *v9; // rdx
int v10; // er12
signed int v11; // eax
signed int v12; // er14
int v13; // er13
unsigned int v14; // ebx
int v15; // ebp
int v17; // er8
char *v18; // rdx
unsigned __int8 v19; // al
char v20; // al
unsigned __int64 v21; // [rsp+0h] [rbp-878h]
unsigned int v23; // [rsp+18h] [rbp-860h]
signed int v24; // [rsp+1Ch] [rbp-85Ch]
unsigned __int8 v25; // [rsp+2Fh] [rbp-849h] BYREF
char buf[1040]; // [rsp+30h] [rbp-848h] BYREF
char v27[1080]; // [rsp+440h] [rbp-438h] BYREF

if ( g3_rf_fine != a6 && g3_rf_fine != -1 )
{
v8 = "fine";
if ( !g3_rf_fine )
v8 = "normal";
v9 = "fine";
if ( !a6 )
v9 = "normal";
fprintf(stderr, "WARNING: sending in %s mode, fax data is %s mode\n", v9, v8);
lprintf(3LL, "resolution mismatch");
}
if ( a5 > 0x201 )
{
*_errno_location() = 22;
lprintf(1LL, "g3_send_file: too much padding requested, pad_bytes=%d, CHUNK=%d", a5, 1024LL);
fwrite("g3_send_file: internal error (pad_bytes)\n", 1uLL, 0x29uLL, stderr);
return 0xFFFFFFFFLL;
}
v24 = a5;
v10 = a1(v27, 1024LL);
v11 = a5;
v12 = a5;
v23 = -1;
v13 = 0;
v14 = 0;
v15 = 0;
v21 = 1024LL - v11;
while ( 1 )
{
v19 = v27[v15];
v25 = v19;
++v12;
if ( v19 )
{
if ( (unsigned __int8)bits_left[v19] + v13 > 10 )
{
if ( v24 > v12 )
{
v17 = v24 - v12;
if ( v24 - v12 > 0 )
{
v18 = &buf[v14];
do
*v18++ = 0;
while ( v18 != &buf[v14 + 1 + v17 - 1] );
v14 += v17;
}
}
++v23;
v12 = 0;
}
v13 = (unsigned __int8)bits_right[v19];
}
else
{
v13 += 8;
}
v20 = *((_BYTE *)&fax_send_swaptable + v19);
buf[v14] = v20;
if ( v20 == 16 && a4 )
buf[++v14] = 16;
++v14;
if ( v10 > v15 + 1 )
{
++v15;
}
else
{
v10 = a1(v27, 1024LL);
if ( v10 < 0 )
goto LABEL_34;
v15 = 0;
}
if ( (int)v14 >= v21 || !v10 )
break;
LABEL_24:
if ( v10 <= 0 )
goto LABEL_34;
}
if ( v14 == write(a2, buf, (int)v14) )
{
lprintf(6LL, "write %d", v14);
v14 = a3;
if ( a3 )
{
v14 = 0;
if ( (unsigned __int8)check_for_input(a2) )
{
lprintf(5LL, "input: got ");
while ( read(a2, &v25, 1uLL) == 1 )
{
lputc(5LL, (unsigned int)(char)v25);
if ( !(unsigned __int8)check_for_input(a2) )
{
v14 = 0;
goto LABEL_24;
}
}
lprintf(1LL, "read failed");
v14 = 0;
}
}
goto LABEL_24;
}
lprintf(1LL, "fax1_send_page: can't write %d bytes", v14);
LABEL_34:
g3_close();
lprintf(5LL, "end of page, %d lines sent", v23);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 (__fastcall *a1)(char *, __int64), unsigned int a2, unsigned int a3, int a4, unsigned int a5, int a6)
{
const char *v8; // rcx
const char *v9; // rdx
int v10; // er12
signed int v11; // eax
signed int v12; // er14
int v13; // er13
unsigned int v14; // ebx
int v15; // ebp
int v17; // er8
char *v18; // rdx
unsigned __int8 v19; // al
char v20; // al
unsigned __int64 v21; // [rsp+0h] [rbp-878h]
unsigned int v23; // [rsp+18h] [rbp-860h]
signed int v24; // [rsp+1Ch] [rbp-85Ch]
unsigned __int8 v25; // [rsp+2Fh] [rbp-849h] BYREF
char buf[1040]; // [rsp+30h] [rbp-848h] BYREF
char v27[1080]; // [rsp+440h] [rbp-438h] BYREF

if ( g3_rf_fine != a6 && g3_rf_fine != -1 )
{
v8 = "fine";
if ( !g3_rf_fine )
v8 = "normal";
v9 = "fine";
if ( !a6 )
v9 = "normal";
fprintf(stderr, "WARNING: sending in %s mode, fax data is %s mode\n", v9, v8);
lprintf(3LL, "resolution mismatch");
}
if ( a5 > 0x201 )
{
*_errno_location() = 22;
lprintf(1LL, "g3_send_file: too much padding requested, pad_bytes=%d, CHUNK=%d", a5, 1024LL);
fwrite("g3_send_file: internal error (pad_bytes)\n", 1uLL, 0x29uLL, stderr);
return 0xFFFFFFFFLL;
}
v24 = a5;
v10 = a1(v27, 1024LL);
v11 = a5;
v12 = a5;
v23 = -1;
v13 = 0;
v14 = 0;
v15 = 0;
v21 = 1024LL - v11;
while ( 1 )
{
v19 = v27[v15];
v25 = v19;
++v12;
if ( v19 )
{
if ( (unsigned __int8)bits_left[v19] + v13 > 10 )
{
if ( v24 > v12 )
{
v17 = v24 - v12;
if ( v24 - v12 > 0 )
{
v18 = &buf[v14];
do
*v18++ = 0;
while ( v18 != &buf[v14 + 1 + v17 - 1] );
v14 += v17;
}
}
++v23;
v12 = 0;
}
v13 = (unsigned __int8)bits_right[v19];
}
else
{
v13 += 8;
}
v20 = *((_BYTE *)&fax_send_swaptable + v19);
buf[v14] = v20;
if ( v20 == 16 && a4 )
buf[++v14] = 16;
++v14;
if ( v10 > v15 + 1 )
{
++v15;
}
else
{
v10 = a1(v27, 1024LL);
if ( v10 < 0 )
goto LABEL_34;
v15 = 0;
}
if ( (int)v14 >= v21 || !v10 )
break;
LABEL_24:
if ( v10 <= 0 )
goto LABEL_34;
}
if ( v14 == write(a2, buf, (int)v14) )
{
lprintf(6LL, "write %d", v14);
v14 = a3;
if ( a3 )
{
v14 = 0;
if ( (unsigned __int8)check_for_input(a2) )
{
lprintf(5LL, "input: got ");
while ( read(a2, &v25, 1uLL) == 1 )
{
lputc(5LL, (unsigned int)(char)v25);
if ( !(unsigned __int8)check_for_input(a2) )
{
v14 = 0;
goto LABEL_24;
}
}
lprintf(1LL, "read failed");
v14 = 0;
}
}
goto LABEL_24;
}
lprintf(1LL, "fax1_send_page: can't write %d bytes", v14);
LABEL_34:
func2();
lprintf(5LL, "end of page, %d lines sent", v23);
return 0LL;
}

original_code:
int g3_send_file (in_func_t * in_func,int out_fd,int is_device,int escape_dle,int pad_bytes,int fax_res)
{
uch rbuf[1024], wbuf[1024 +2];
int r, w, r_num;
uch r_ch;
int bytes_cur;
int have_0bits;
int lines_seen=-1;
    if ( g3_rf_fine != -1 && fax_res != g3_rf_fine )
    {
 fprintf( stderr, "WARNING: sending in %s mode, fax data is %s mode\n",
   fax_res? "fine" : "normal",
   g3_rf_fine? "fine" : "normal" );
 lprintf( 3, "resolution mismatch" );
    }
    if ( pad_bytes > sizeof(wbuf)/2 )
    {
 (*__errno_location ())=22;
 lprintf( 1, "g3_send_file: too much padding requested, pad_bytes=%d, CHUNK=%d", pad_bytes, 1024 );
 fprintf( stderr, "g3_send_file: internal error (pad_bytes)\n" );
 return -1;
    }
    if ( pad_bytes < 0 ) pad_bytes = 0;
    r_num = in_func( rbuf, sizeof(rbuf) );
    r=0;
    w=0;
    bytes_cur=pad_bytes;
    have_0bits=0;
    do
    {
 r_ch = (uch)rbuf[r++];
 bytes_cur++;
 if ( r_ch == 0 )
     { have_0bits += 8; }
 else
 {
     if ( have_0bits + bits_left[r_ch] >= 11 )
     {
  int need = (bytes_cur<pad_bytes)? pad_bytes-bytes_cur: 0;
  while( need>0 )
      { wbuf[w++] = 0; need--; }
  bytes_cur=0;
  lines_seen++;
     }
     have_0bits = bits_right[r_ch];
 }
 wbuf[w] = fax_send_swaptable[ r_ch ];
 if ( escape_dle && wbuf[w] == 020 ) wbuf[++w] = 020;
 w++;
        if ( r >= r_num )
 {
     r_num = in_func( rbuf, sizeof(rbuf) );
     if ( r_num < 0 )
  { break; }
     r = 0;
 }
 if ( ( w >= (sizeof(wbuf)-2 - pad_bytes ) ) ||
      (r_num == 0) )
 {
     if ( w != write( out_fd, wbuf, w ) )
     {
         lprintf( 1, "fax1_send_page: can't write %d bytes", w );
  break;
     }
     lprintf( 6, "write %d", w );
     w=0;
     if ( is_device && check_for_input( out_fd ) )
     {
  lprintf( 5, "input: got " );
  do
  {
      if ( read( out_fd, &r_ch, 1 ) != 1 )
      {
   lprintf( 1, "read failed" );
   break;
      }
      else
   lputc( 5, r_ch );
  }
  while ( check_for_input( out_fd ) );
     }
 }
    }
    while(r_num>0);
    g3_close();
    lprintf( 5, "end of page, %d lines sent", lines_seen );
    return 0;
}

canonical_original_code:
int g3_send_file(int (*in_func)(char *, int), int out_fd, int is_device, int escape_dle, int pad_bytes, int fax_res)
{
unsigned char rbuf[1024]; unsigned char wbuf[-1];
int r; int w; int r_num;
unsigned char r_ch;
int bytes_cur;
int have_0bits;
int lines_seen = -1;
    if ( g3_rf_fine != -1 && fax_res != g3_rf_fine )
    {
 fprintf( stderr, "WARNING: sending in %s mode, fax data is %s mode\n",
   fax_res? "fine" : "normal",
   g3_rf_fine? "fine" : "normal" );
 lprintf( 3, "resolution mismatch" );
    }
    if ( pad_bytes > sizeof(wbuf)/2 )
    {
 (*__errno_location ())=22;
 lprintf( 1, "g3_send_file: too much padding requested, pad_bytes=%d, CHUNK=%d", pad_bytes, 1024 );
 fprintf( stderr, "g3_send_file: internal error (pad_bytes)\n" );
 return -1;
    }
    if ( pad_bytes < 0 ) pad_bytes = 0;
    r_num = in_func( rbuf, sizeof(rbuf) );
    r=0;
    w=0;
    bytes_cur=pad_bytes;
    have_0bits=0;
    do
    {
 r_ch = (unsigned char)rbuf[r++];
 bytes_cur++;
 if ( r_ch == 0 )
     { have_0bits += 8; }
 else
 {
     if ( have_0bits + bits_left[r_ch] >= 11 )
     {
  int need = (bytes_cur<pad_bytes)? pad_bytes-bytes_cur: 0;
  while( need>0 )
      { wbuf[w++] = 0; need--; }
  bytes_cur=0;
  lines_seen++;
     }
     have_0bits = bits_right[r_ch];
 }
 wbuf[w] = fax_send_swaptable[ r_ch ];
 if ( escape_dle && wbuf[w] == 020 ) wbuf[++w] = 020;
 w++;
        if ( r >= r_num )
 {
     r_num = in_func( rbuf, sizeof(rbuf) );
     if ( r_num < 0 )
  { break; }
     r = 0;
 }
 if ( ( w >= (sizeof(wbuf)-2 - pad_bytes ) ) ||
      (r_num == 0) )
 {
     if ( w != write( out_fd, wbuf, w ) )
     {
         lprintf( 1, "fax1_send_page: can't write %d bytes", w );
  break;
     }
     lprintf( 6, "write %d", w );
     w=0;
     if ( is_device && check_for_input( out_fd ) )
     {
  lprintf( 5, "input: got " );
  do
  {
      if ( read( out_fd, &r_ch, 1 ) != 1 )
      {
   lprintf( 1, "read failed" );
   break;
      }
      else
   lputc( 5, r_ch );
  }
  while ( check_for_input( out_fd ) );
     }
 }
    }
    while(r_num>0);
    g3_close();
    lprintf( 5, "end of page, %d lines sent", lines_seen );
    return 0;
}

variable_types:
rbuf
unsigned char [1024]

variable_types:
wbuf
unsigned char [-1]

variable_types: w int

variable_types: need int


decompiled_code:
const char *__fastcall get_basename(const char *a1)
{
char *v2; // rax
char *v3; // rdi
bool v4; // zf
const char *result; // rax

if ( !a1 )
return 0LL;
v2 = strrchr(a1, 47);
v3 = v2 + 1;
v4 = v2 == 0LL;
result = a1;
if ( !v4 )
result = v3;
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(const char *a1)
{
char *v2; // rax
char *v3; // rdi
bool v4; // zf
const char *result; // rax

if ( !a1 )
return 0LL;
v2 = strrchr(a1, 47);
v3 = v2 + 1;
v4 = v2 == 0LL;
result = a1;
if ( !v4 )
result = v3;
return result;
}

original_code:
char * get_basename (char * s)
{
char * p;
    if ( s == ((void *)0) ) return ((void *)0);
    p = strrchr( s, '/' );
    return ( p == ((void *)0) ) ? s: p+1;
}

canonical_original_code:
char *get_basename(char *s)
{
char *p;
    if ( s == ((void *)0) ) return ((void *)0);
    p = strrchr( s, '/' );
    return ( p == ((void *)0) ) ? s: p+1;
}

variable_types:
s
char *

variable_types:
p
char *


decompiled_code:
void __fastcall get_ugid(__int64 a1, __int64 a2, __uid_t *a3, __gid_t *a4)
{
const unsigned __int16 **v7; // rax
const char *v8; // rdi
int v9; // eax
struct passwd *v10; // rax
const unsigned __int16 **v11; // rax
const char *v12; // rdi
int v13; // eax
struct group *v14; // rax

*a4 = 0;
*a3 = 0;
if ( *(_DWORD *)(a1 + 20) )
{
v7 = _ctype_b_loc();
v8 = *(const char **)(a1 + 8);
if ( ((*v7)[*v8] & 0x800) != 0 )
{
v9 = atoi(v8);
v10 = getpwuid(v9);
}
else
{
v10 = getpwnam(v8);
}
if ( v10 )
{
*a3 = v10->pw_uid;
*a4 = v10->pw_gid;
}
else
{
lprintf(1LL, "can't get user id for '%s'", *(const char **)(a1 + 8));
}
endpwent();
}
if ( *(_DWORD *)(a2 + 20) )
{
v11 = _ctype_b_loc();
v12 = *(const char **)(a2 + 8);
if ( ((*v11)[*v12] & 0x800) != 0 )
{
v13 = atoi(v12);
v14 = getgrgid(v13);
}
else
{
v14 = getgrnam(v12);
}
if ( v14 )
*a4 = v14->gr_gid;
else
lprintf(1LL, "can't get group '%s'", *(const char **)(a2 + 8));
endgrent();
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, __int64 a2, __uid_t *a3, __gid_t *a4)
{
const unsigned __int16 **v7; // rax
const char *v8; // rdi
int v9; // eax
struct passwd *v10; // rax
const unsigned __int16 **v11; // rax
const char *v12; // rdi
int v13; // eax
struct group *v14; // rax

*a4 = 0;
*a3 = 0;
if ( *(_DWORD *)(a1 + 20) )
{
v7 = _ctype_b_loc();
v8 = *(const char **)(a1 + 8);
if ( ((*v7)[*v8] & 0x800) != 0 )
{
v9 = atoi(v8);
v10 = getpwuid(v9);
}
else
{
v10 = getpwnam(v8);
}
if ( v10 )
{
*a3 = v10->pw_uid;
*a4 = v10->pw_gid;
}
else
{
lprintf(1LL, "can't get user id for '%s'", *(const char **)(a1 + 8));
}
endpwent();
}
if ( *(_DWORD *)(a2 + 20) )
{
v11 = _ctype_b_loc();
v12 = *(const char **)(a2 + 8);
if ( ((*v11)[*v12] & 0x800) != 0 )
{
v13 = atoi(v12);
v14 = getgrgid(v13);
}
else
{
v14 = getgrnam(v12);
}
if ( v14 )
*a4 = v14->gr_gid;
else
lprintf(1LL, "can't get group '%s'", *(const char **)(a2 + 8));
endgrent();
}
}

original_code:
void get_ugid (conf_data * user,conf_data * group,uid_t * uid,gid_t * gid)
{
    *uid = *gid = 0;
    if ( user->flags != C_EMPTY )
    {
 struct passwd *pwd;
 if ( ((*__ctype_b_loc ())[(int) ((*(char*)(user->d.p)))] & (unsigned short int) _ISdigit))
     pwd = getpwuid( atoi( (char*) (user->d.p) ));
 else
     pwd = getpwnam( (char*)(user->d.p) );
 if ( pwd == ((void *)0) )
     lprintf( 1, "can't get user id for '%s'", user->d.p );
 else
 {
     *uid = pwd->pw_uid;
     *gid = pwd->pw_gid;
 }
 endpwent();
    }
    if ( group->flags != C_EMPTY )
    {
 struct group * grp;
 if ( ((*__ctype_b_loc ())[(int) ((*(char*)(group->d.p)))] & (unsigned short int) _ISdigit))
     grp = getgrgid( atoi( (char*)(group->d.p)) );
 else
     grp = getgrnam( (char*) (group->d.p) );
 if ( grp == ((void *)0) )
     lprintf( 1, "can't get group '%s'", group->d.p );
 else
     *gid = grp->gr_gid;
 endgrent();
    }
}

canonical_original_code:
void get_ugid(struct conf_data *user, struct conf_data *group, unsigned int *uid, unsigned int *gid)
{
    *uid = *gid = 0;
    if ( user->flags != C_EMPTY )
    {
 struct passwd *pwd;
 if ( ((*__ctype_b_loc ())[(int) ((*(char *)(user->d.p)))] & (unsigned short) _ISdigit))
     pwd = getpwuid( atoi( (char *) (user->d.p) ));
 else
     pwd = getpwnam( (char *)(user->d.p) );
 if ( pwd == ((void *)0) )
     lprintf( 1, "can't get user id for '%s'", user->d.p );
 else
 {
     *uid = pwd->pw_uid;
     *gid = pwd->pw_gid;
 }
 endpwent();
    }
    if ( group->flags != C_EMPTY )
    {
 struct group *grp;
 if ( ((*__ctype_b_loc ())[(int) ((*(char *)(group->d.p)))] & (unsigned short) _ISdigit))
     grp = getgrgid( atoi( (char *)(group->d.p)) );
 else
     grp = getgrnam( (char *) (group->d.p) );
 if ( grp == ((void *)0) )
     lprintf( 1, "can't get group '%s'", group->d.p );
 else
     *gid = grp->gr_gid;
 endgrent();
    }
}

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

variable_types:
group
struct conf_data *

variable_types:
uid
unsigned int *

variable_types:
gid
unsigned int *

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types: struct group { char *gr_name; char *gr_passwd; unsigned int gr_gid; char **gr_mem; } *


decompiled_code:
char *__fastcall get_ps_args(unsigned int a1)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
__int64 i; // rax
char *result; // rax
const unsigned __int16 **v6; // rsi
__int64 v7; // rax
char v8[56]; // [rsp+0h] [rbp-38h] BYREF

sprintf(v8, "/proc/%d/cmdline", a1);
v1 = open(v8, 0);
if ( v1 < 0 )
{
lprintf(1LL, "cannot open %s", v8);
result = 0LL;
}
else
{
v2 = v1;
v3 = read(v1, psinfo_0, 0x3BuLL);
if ( v3 < 0 )
{
lprintf(1LL, "reading %s failed", v8);
close(v2);
result = 0LL;
}
else
{
close(v2);
psinfo_0[v3] = 0;
if ( v3 <= 0 )
{
result = psinfo_0;
}
else
{
for ( i = 0LL; ; ++i )
{
if ( !psinfo_0[i] )
psinfo_0[i] = 32;
if ( i == v3 - 1 )
break;
}
v6 = _ctype_b_loc();
v7 = v3;
while ( ((*v6)[*(char *)(v7 + 543)] & 0x2000) != 0 )
{
*(_BYTE *)(v7 + 543) = 0;
if ( (int)--v7 <= 0 )
return psinfo_0;
}
result = psinfo_0;
}
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(unsigned int a1)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
__int64 i; // rax
char *result; // rax
const unsigned __int16 **v6; // rsi
__int64 v7; // rax
char v8[56]; // [rsp+0h] [rbp-38h] BYREF

sprintf(v8, "/proc/%d/cmdline", a1);
v1 = open(v8, 0);
if ( v1 < 0 )
{
lprintf(1LL, "cannot open %s", v8);
result = 0LL;
}
else
{
v2 = v1;
v3 = read(v1, psinfo_0, 0x3BuLL);
if ( v3 < 0 )
{
lprintf(1LL, "reading %s failed", v8);
close(v2);
result = 0LL;
}
else
{
close(v2);
psinfo_0[v3] = 0;
if ( v3 <= 0 )
{
result = psinfo_0;
}
else
{
for ( i = 0LL; ; ++i )
{
if ( !psinfo_0[i] )
psinfo_0[i] = 32;
if ( i == v3 - 1 )
break;
}
v6 = _ctype_b_loc();
v7 = v3;
while ( ((*v6)[*(char *)(v7 + 543)] & 0x2000) != 0 )
{
*(_BYTE *)(v7 + 543) = 0;
if ( (int)--v7 <= 0 )
return psinfo_0;
}
result = psinfo_0;
}
}
}
return result;
}

original_code:
char * get_ps_args (int pid)
{
    char procfn[30];
    int procfd;
    int i,l;
    static char psinfo[60];
    sprintf( procfn, "/proc/%d/cmdline", pid );
    procfd = open( procfn, 00 );
    if ( procfd < 0 )
    {
 lprintf( 1, "cannot open %s", procfn );
 return ((void *)0);
    }
    l = read( procfd, psinfo, sizeof( psinfo ) -1 );
    if ( l < 0 )
    {
 lprintf( 1, "reading %s failed", procfn );
 close( procfd );
 return ((void *)0);
    }
    close( procfd );
    psinfo[l] = 0;
    for ( i=0; i<l; i++ )
 if ( psinfo[i] == 0 ) psinfo[i] = ' ';
    while( l>0 && ((*__ctype_b_loc ())[(int) ((psinfo[l-1]))] & (unsigned short int) _ISspace) ) psinfo[--l]='\0';
    return psinfo;
}

canonical_original_code:
char *get_ps_args(int pid)
{
    char procfn[30];
    int procfd;
    int i; int l;
    static char psinfo[60];
    sprintf( procfn, "/proc/%d/cmdline", pid );
    procfd = open( procfn, 00 );
    if ( procfd < 0 )
    {
 lprintf( 1, "cannot open %s", procfn );
 return ((void *)0);
    }
    l = read( procfd, psinfo, sizeof( psinfo ) -1 );
    if ( l < 0 )
    {
 lprintf( 1, "reading %s failed", procfn );
 close( procfd );
 return ((void *)0);
    }
    close( procfd );
    psinfo[l] = 0;
    for ( i=0; i<l; i++ )
 if ( psinfo[i] == 0 ) psinfo[i] = ' ';
    while( l>0 && ((*__ctype_b_loc ())[(int) ((psinfo[l-1]))] & (unsigned short) _ISspace) ) psinfo[--l]='\0';
    return psinfo;
}

variable_types: pid int

variable_types:
procfn
char [30]

variable_types: procfd int

variable_types: i int

variable_types: l int

variable_types:
psinfo
char [60]


decompiled_code:
void chat_timeout()
{
chat_has_timeout = 1;
}

canonical_decompiled_code:
void func0()
{
chat_has_timeout = 1;
}

original_code:
static RETSIGTYPE chat_timeout()
{
    chat_has_timeout = (1==1);
}

canonical_original_code:
static void chat_timeout()
{
    chat_has_timeout = (1==1);
}


decompiled_code:
__int64 __fastcall do_chat_send(int fd, void *buf)
{
char *v2; // rbx
char v3; // al
char v4; // r13
char *v5; // rbp
char v6; // al
__int64 result; // rax

v2 = (char *)buf;
delay(50LL);
lprintf(4LL, "send: ");
v3 = *(_BYTE *)buf;
if ( !*(_BYTE *)buf )
goto LABEL_14;
v4 = 0;
do
{
if ( v3 == 92 )
{
v5 = v2 + 1;
v6 = v2[1];
if ( v6 == 99 )
{
v4 = 1;
}
else if ( v6 == 100 )
{
lputs(4LL, "\\d");
delay(500LL);
}
else
{
write(fd, v2 + 1, 1uLL);
lputc(4LL, (unsigned int)v2[1]);
}
}
else
{
if ( write(fd, v2, 1uLL) != 1 )
{
lprintf(1LL, "do_chat: can't write to modem!");
return 0xFFFFFFFFLL;
}
lputc(4LL, (unsigned int)*v2);
v5 = v2;
}
v2 = v5 + 1;
v3 = v5[1];
}
while ( v3 );
result = 0LL;
if ( !v4 )
{
LABEL_14:
write(fd, "\r", 1uLL);
lputc(4LL, 13LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf)
{
char *v2; // rbx
char v3; // al
char v4; // r13
char *v5; // rbp
char v6; // al
__int64 result; // rax

v2 = (char *)buf;
delay(50LL);
lprintf(4LL, "send: ");
v3 = *(_BYTE *)buf;
if ( !*(_BYTE *)buf )
goto LABEL_14;
v4 = 0;
do
{
if ( v3 == 92 )
{
v5 = v2 + 1;
v6 = v2[1];
if ( v6 == 99 )
{
v4 = 1;
}
else if ( v6 == 100 )
{
lputs(4LL, "\\d");
delay(500LL);
}
else
{
write(fd, v2 + 1, 1uLL);
lputc(4LL, (unsigned int)v2[1]);
}
}
else
{
if ( write(fd, v2, 1uLL) != 1 )
{
lprintf(1LL, "do_chat: can't write to modem!");
return 0xFFFFFFFFLL;
}
lputc(4LL, (unsigned int)*v2);
v5 = v2;
}
v2 = v5 + 1;
v3 = v5[1];
}
while ( v3 );
result = 0LL;
if ( !v4 )
{
LABEL_14:
write(fd, "\r", 1uLL);
lputc(4LL, 13LL);
result = 0LL;
}
return result;
}

original_code:
int do_chat_send (int fd,char * p)
{
    boolean nocr = (1==0);
    delay(50);
    lprintf( 4, "send: " );
    while ( *p != 0 )
    {
 if ( *p == '\\' )
 {
     switch ( *(++p) )
     {
       case 'd': lputs( 4, "\\d"); delay(500); break;
       case 'c': nocr = (1==1); break;
       default:
  write( fd, p, 1 );
  lputc( 4, *p );
     }
 }
 else
 {
     if ( write( fd, p, 1 ) != 1 )
     {
  lprintf( 1, "do_chat: can't write to modem!" );
  return -1;
     }
     lputc( 4, *p );
 }
 p++;
    }
    if ( ! nocr )
    {
 write( fd, "\r", sizeof("\r")-1 );
 p = "\r";
 while ( *p ) lputc( 4, *(p++) );
    }
    return 0;
}

canonical_original_code:
int do_chat_send(int fd, char *p)
{
    char nocr = (1==0);
    delay(50);
    lprintf( 4, "send: " );
    while ( *p != 0 )
    {
 if ( *p == '\\' )
 {
     switch ( *(++p) )
     {
       case 'd': lputs( 4, "\\d"); delay(500); break;
       case 'c': nocr = (1==1); break;
       default:
  write( fd, p, 1 );
  lputc( 4, *p );
     }
 }
 else
 {
     if ( write( fd, p, 1 ) != 1 )
     {
  lprintf( 1, "do_chat: can't write to modem!" );
  return -1;
     }
     lputc( 4, *p );
 }
 p++;
    }
    if ( ! nocr )
    {
 write( fd, "\r", sizeof("\r")-1 );
 p = "\r";
 while ( *p ) lputc( 4, *(p++) );
    }
    return 0;
}

variable_types: fd int

variable_types: nocr char


decompiled_code:
__int64 __fastcall do_chat(__int64 fd, const char **a2, const char **a3, _DWORD *a4, unsigned int a5, char a6)
{
unsigned int v6; // er15
const char **v7; // r13
_DWORD *v9; // rbx
const char *v10; // rdx
int v11; // er12
char *v12; // rsi
int v13; // ebp
const char *v14; // rbx
int v15; // eax
int v16; // eax
char v17; // al
char *v18; // rdx
unsigned int v19; // ebx
const char *v20; // rbx
const char **v21; // rbp
int v22; // eax
int v24; // [rsp+Ch] [rbp-2CCh]
__int128 v28[3]; // [rsp+20h] [rbp-2B8h] BYREF
__int64 v29; // [rsp+50h] [rbp-288h]
int v30; // [rsp+58h] [rbp-280h]
__m128i v31; // [rsp+60h] [rbp-278h] BYREF
__m128i v32; // [rsp+70h] [rbp-268h] BYREF
__m128i v33; // [rsp+80h] [rbp-258h] BYREF
__int64 v34; // [rsp+90h] [rbp-248h]
int v35; // [rsp+98h] [rbp-240h]
char dest[250]; // [rsp+A0h] [rbp-238h] BYREF
char src[318]; // [rsp+19Ah] [rbp-13Eh] BYREF

v6 = fd;
v7 = a2;
v9 = a4;
tio_get(fd, &v31);
v28[0] = (__int128)_mm_loadu_si128(&v31);
v28[1] = (__int128)_mm_loadu_si128(&v32);
v28[2] = (__int128)_mm_loadu_si128(&v33);
v29 = v34;
v30 = v35;
tio_mode_raw(&v31);
tio_set((unsigned int)fd, &v31);
signal(14, (__sighandler_t)chat_timeout);
if ( a3 && v9 )
*v9 = 0;
v10 = *a2;
if ( !*a2 )
{
v19 = 0;
goto LABEL_47;
}
v24 = 0;
while ( !*v10 )
{
LABEL_10:
v12 = (char *)v7[1];
if ( !v12 )
{
v19 = 0;
goto LABEL_47;
}
v24 += 2;
do_chat_send(v6, v12);
v7 += 2;
v10 = *v7;
if ( !*v7 )
{
v19 = 0;
goto LABEL_47;
}
}
lprintf(4LL, "waiting for ``%s''", v10);
lprintf(5LL, "got: ");
chat_has_timeout = 0;
if ( v24 || a6 )
alarm(a5);
else
alarm(0);
v11 = 0;
while ( 1 )
{
if ( virtual_ring && !memcmp(*v7, "RING", 4uLL) )
{
lputs(4LL, " ``found''");
LABEL_9:
alarm(0);
goto LABEL_10;
}
v16 = read(v6, &dest[v11], 1uLL);
v13 = v16;
if ( v16 >= 0 )
{
if ( !chat_has_timeout )
{
if ( v16 > 0 )
{
lputc(5LL, (unsigned int)dest[v11]);
v17 = dest[v11];
if ( v17 == 13 || v17 == 10 || (v18 = lptr_1, lptr_1 >= byte_6C1) )
{
*lptr_1 = 0;
if ( lbuf_0 )
cndfind(&lbuf_0);
lptr_1 = &lbuf_0;
}
else
{
++lptr_1;
*v18 = v17;
}
}
goto LABEL_19;
}
LABEL_37:
lprintf(3LL, "timeout in chat script, waiting for `%s'", *v7);
LABEL_38:
alarm(0);
v19 = -1;
goto LABEL_47;
}
if ( *_errno_location() != 4 )
{
lprintf(1LL, "do_chat: error in read()");
goto LABEL_38;
}
if ( chat_has_timeout )
goto LABEL_37;
v13 = 0;
LABEL_19:
v11 += v13;
if ( v11 > 495 )
{
memcpy(dest, src, v11 - 249);
v11 -= 250;
}
v14 = *v7;
v15 = strlen(*v7);
if ( v11 >= v15 && !memcmp(&dest[v11 - v15], v14, v15) )
{
lputs(4LL, " ** found **");
goto LABEL_9;
}
if ( a3 )
{
v20 = *a3;
if ( *a3 )
break;
}
LABEL_24:
if ( v11 > 499 )
goto LABEL_9;
}
v21 = a3;
while ( 1 )
{
v22 = strlen(v20);
if ( v11 >= v22 && !memcmp(&dest[v11 - v22], v20, v22) )
break;
v21 += 2;
v20 = *v21;
if ( !*v21 )
goto LABEL_24;
}
lprintf(4LL, "found action string: ``%s''", v20);
*a4 = *((_DWORD *)v21 + 2);
alarm(0);
v19 = -1;
LABEL_47:
tio_set(v6, v28);
return v19;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 fd, const char **a2, const char **a3, _DWORD *a4, unsigned int a5, char a6)
{
unsigned int v6; // er15
const char **v7; // r13
_DWORD *v9; // rbx
const char *v10; // rdx
int v11; // er12
char *v12; // rsi
int v13; // ebp
const char *v14; // rbx
int v15; // eax
int v16; // eax
char v17; // al
char *v18; // rdx
unsigned int v19; // ebx
const char *v20; // rbx
const char **v21; // rbp
int v22; // eax
int v24; // [rsp+Ch] [rbp-2CCh]
__int128 v28[3]; // [rsp+20h] [rbp-2B8h] BYREF
__int64 v29; // [rsp+50h] [rbp-288h]
int v30; // [rsp+58h] [rbp-280h]
__m128i v31; // [rsp+60h] [rbp-278h] BYREF
__m128i v32; // [rsp+70h] [rbp-268h] BYREF
__m128i v33; // [rsp+80h] [rbp-258h] BYREF
__int64 v34; // [rsp+90h] [rbp-248h]
int v35; // [rsp+98h] [rbp-240h]
char dest[250]; // [rsp+A0h] [rbp-238h] BYREF
char src[318]; // [rsp+19Ah] [rbp-13Eh] BYREF

v6 = fd;
v7 = a2;
v9 = a4;
tio_get(fd, &v31);
v28[0] = (__int128)_mm_loadu_si128(&v31);
v28[1] = (__int128)_mm_loadu_si128(&v32);
v28[2] = (__int128)_mm_loadu_si128(&v33);
v29 = v34;
v30 = v35;
tio_mode_raw(&v31);
tio_set((unsigned int)fd, &v31);
signal(14, (__sighandler_t)chat_timeout);
if ( a3 && v9 )
*v9 = 0;
v10 = *a2;
if ( !*a2 )
{
v19 = 0;
goto LABEL_47;
}
v24 = 0;
while ( !*v10 )
{
LABEL_10:
v12 = (char *)v7[1];
if ( !v12 )
{
v19 = 0;
goto LABEL_47;
}
v24 += 2;
func1(v6, v12);
v7 += 2;
v10 = *v7;
if ( !*v7 )
{
v19 = 0;
goto LABEL_47;
}
}
lprintf(4LL, "waiting for ``%s''", v10);
lprintf(5LL, "got: ");
chat_has_timeout = 0;
if ( v24 || a6 )
alarm(a5);
else
alarm(0);
v11 = 0;
while ( 1 )
{
if ( virtual_ring && !memcmp(*v7, "RING", 4uLL) )
{
lputs(4LL, " ``found''");
LABEL_9:
alarm(0);
goto LABEL_10;
}
v16 = read(v6, &dest[v11], 1uLL);
v13 = v16;
if ( v16 >= 0 )
{
if ( !chat_has_timeout )
{
if ( v16 > 0 )
{
lputc(5LL, (unsigned int)dest[v11]);
v17 = dest[v11];
if ( v17 == 13 || v17 == 10 || (v18 = lptr_1, lptr_1 >= byte_6C1) )
{
*lptr_1 = 0;
if ( lbuf_0 )
cndfind(&lbuf_0);
lptr_1 = &lbuf_0;
}
else
{
++lptr_1;
*v18 = v17;
}
}
goto LABEL_19;
}
LABEL_37:
lprintf(3LL, "timeout in chat script, waiting for `%s'", *v7);
LABEL_38:
alarm(0);
v19 = -1;
goto LABEL_47;
}
if ( *_errno_location() != 4 )
{
lprintf(1LL, "do_chat: error in read()");
goto LABEL_38;
}
if ( chat_has_timeout )
goto LABEL_37;
v13 = 0;
LABEL_19:
v11 += v13;
if ( v11 > 495 )
{
memcpy(dest, src, v11 - 249);
v11 -= 250;
}
v14 = *v7;
v15 = strlen(*v7);
if ( v11 >= v15 && !memcmp(&dest[v11 - v15], v14, v15) )
{
lputs(4LL, " ** found **");
goto LABEL_9;
}
if ( a3 )
{
v20 = *a3;
if ( *a3 )
break;
}
LABEL_24:
if ( v11 > 499 )
goto LABEL_9;
}
v21 = a3;
while ( 1 )
{
v22 = strlen(v20);
if ( v11 >= v22 && !memcmp(&dest[v11 - v22], v20, v22) )
break;
v21 += 2;
v20 = *v21;
if ( !*v21 )
goto LABEL_24;
}
lprintf(4LL, "found action string: ``%s''", v20);
*a4 = *((_DWORD *)v21 + 2);
alarm(0);
v19 = -1;
LABEL_47:
tio_set(v6, v28);
return v19;
}

original_code:
int do_chat (int fd,char * expect_send[],chat_action_t actions[],action_t * action,int chat_timeout_time,boolean timeout_first)
{
char buffer[500];
int i,cnt;
int retcode = 0;
int str;
int h;
TIO tio, save_tio;
static char lbuf[100];
static char *lptr = lbuf;
    tio_get( fd, &tio );
    save_tio = tio;
    tio_mode_raw( &tio );
    tio_set( fd, &tio );
    signal( 14, chat_timeout );
    if ( actions != ((void *)0) && action != ((void *)0) ) *action = A_TIMOUT;
    str=0;
    while ( expect_send[str] != ((void *)0) )
    {
 i = 0;
 if ( strlen( expect_send[str] ) != 0 )
 {
     lprintf( 4, "waiting for ``%s''", expect_send[str] );
     lprintf( 5, "got: " );
     chat_has_timeout = (1==0);
     if ( str != 0 || timeout_first )
  alarm( chat_timeout_time );
     else
  alarm( 0 );
     do
     {
  if ( virtual_ring &&
       strncmp( expect_send[str], "RING", 4 ) == 0 )
  {
      lputs( 4, " ``found''" );
      break;
  }
  cnt = read( fd, &buffer[i], 1 );
  if ( cnt < 0 )
  {
      if ( (*__errno_location ()) == 4 ) cnt = 0;
      else
      {
          lprintf( 1, "do_chat: error in read()");
   retcode = -1;
   break;
      }
  }
  if ( chat_has_timeout )
  {
      lprintf( 3, "timeout in chat script, waiting for `%s'", expect_send[str] );
      retcode = -1;
      break;
  }
  if ( cnt > 0 )
  {
      lputc( 5, buffer[i] );
      if ( buffer[i] == '\r' || buffer[i] == '\n' ||
    (lptr >= lbuf+100 -3) )
      {
   *lptr = '\0';
   if (lbuf[0])
       cndfind(lbuf);
   lptr = lbuf;
      }
      else
   *lptr++ = buffer[i];
  }
  i += cnt;
  if ( i>500 -5 )
  {
      memcpy( &buffer[0], &buffer[500/2], i-500/2+1 );
      i-=500/2;
  }
  cnt = strlen( expect_send[str] );
  if ( i >= cnt &&
       memcmp( &buffer[i-cnt], expect_send[str], cnt ) == 0 )
  {
      lputs( 4, " ** found **" );
      break;
  }
                if ( actions != ((void *)0) )
    for ( h=0; actions[h].expect != ((void *)0); h ++ )
  {
      cnt = strlen( actions[h].expect );
      if ( i>=cnt &&
    memcmp( &buffer[i-cnt], actions[h].expect, cnt ) == 0 )
      {
   lprintf( 4,"found action string: ``%s''",
                   actions[h].expect );
   *action = actions[h].action;
   retcode = -1;
   break;
      }
  }
     }
     while ( i<500 && retcode != -1 );
     alarm(0);
     if ( retcode == -1 ) break;
 }
 str++;
 if ( expect_send[str] == ((void *)0) ) break;
 do_chat_send(fd, expect_send[str++] );
    }
    tio_set( fd, &save_tio );
    return retcode;
}

canonical_original_code:
int do_chat(int fd, char *expect_send[], struct chat_actions actions[], enum action_t *action, int chat_timeout_time, char timeout_first)
{
char buffer[500];
int i; int cnt;
int retcode = 0;
int str;
int h;
struct termios tio; struct termios save_tio;
static char lbuf[100];
static char *lptr = lbuf;
    tio_get( fd, &tio );
    save_tio = tio;
    tio_mode_raw( &tio );
    tio_set( fd, &tio );
    signal( 14, chat_timeout );
    if ( actions != ((void *)0) && action != ((void *)0) ) *action = A_TIMOUT;
    str=0;
    while ( expect_send[str] != ((void *)0) )
    {
 i = 0;
 if ( strlen( expect_send[str] ) != 0 )
 {
     lprintf( 4, "waiting for ``%s''", expect_send[str] );
     lprintf( 5, "got: " );
     chat_has_timeout = (1==0);
     if ( str != 0 || timeout_first )
  alarm( chat_timeout_time );
     else
  alarm( 0 );
     do
     {
  if ( virtual_ring &&
       strncmp( expect_send[str], "RING", 4 ) == 0 )
  {
      lputs( 4, " ``found''" );
      break;
  }
  cnt = read( fd, &buffer[i], 1 );
  if ( cnt < 0 )
  {
      if ( (*__errno_location ()) == 4 ) cnt = 0;
      else
      {
          lprintf( 1, "do_chat: error in read()");
   retcode = -1;
   break;
      }
  }
  if ( chat_has_timeout )
  {
      lprintf( 3, "timeout in chat script, waiting for `%s'", expect_send[str] );
      retcode = -1;
      break;
  }
  if ( cnt > 0 )
  {
      lputc( 5, buffer[i] );
      if ( buffer[i] == '\r' || buffer[i] == '\n' ||
    (lptr >= lbuf+100 -3) )
      {
   *lptr = '\0';
   if (lbuf[0])
       cndfind(lbuf);
   lptr = lbuf;
      }
      else
   *lptr++ = buffer[i];
  }
  i += cnt;
  if ( i>500 -5 )
  {
      memcpy( &buffer[0], &buffer[500/2], i-500/2+1 );
      i-=500/2;
  }
  cnt = strlen( expect_send[str] );
  if ( i >= cnt &&
       memcmp( &buffer[i-cnt], expect_send[str], cnt ) == 0 )
  {
      lputs( 4, " ** found **" );
      break;
  }
                if ( actions != ((void *)0) )
    for ( h=0; actions[h].expect != ((void *)0); h ++ )
  {
      cnt = strlen( actions[h].expect );
      if ( i>=cnt &&
    memcmp( &buffer[i-cnt], actions[h].expect, cnt ) == 0 )
      {
   lprintf( 4,"found action string: ``%s''",
                   actions[h].expect );
   *action = actions[h].action;
   retcode = -1;
   break;
      }
  }
     }
     while ( i<500 && retcode != -1 );
     alarm(0);
     if ( retcode == -1 ) break;
 }
 str++;
 if ( expect_send[str] == ((void *)0) ) break;
 do_chat_send(fd, expect_send[str++] );
    }
    tio_set( fd, &save_tio );
    return retcode;
}

variable_types: struct chat_actions { char *expect; enum action_t { A_TIMOUT=0, A_FAIL=1, A_FAX=2, A_VCON=3, A_CONN=4, A_RING1=5, A_RING2=6, A_RING3=7, A_RING4=8, A_RING5=9 } action; } []

variable_types: enum action_t { A_TIMOUT=0, A_FAIL=1, A_FAX=2, A_VCON=3, A_CONN=4, A_RING1=5, A_RING2=6, A_RING3=7, A_RING4=8, A_RING5=9 } *

variable_types:
buffer
char [500]

variable_types: cnt int

variable_types: retcode int

variable_types: str int

variable_types: h 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:
lbuf
char [100]

variable_types:
lptr
char *

user_defined_types: struct chat_actions { char *expect; enum action_t action; }


decompiled_code:
__int64 __fastcall clean_line(int fd, unsigned int a2)
{
int v2; // ebx
ssize_t v3; // rax
__int128 v5[3]; // [rsp+0h] [rbp-98h] BYREF
__int64 v6; // [rsp+30h] [rbp-68h]
int v7; // [rsp+38h] [rbp-60h]
__m128i v8; // [rsp+40h] [rbp-58h] BYREF
__m128i v9; // [rsp+50h] [rbp-48h] BYREF
__m128i v10; // [rsp+60h] [rbp-38h] BYREF
__int64 v11; // [rsp+70h] [rbp-28h]
int v12; // [rsp+78h] [rbp-20h]
char buf[26]; // [rsp+7Eh] [rbp-1Ah] BYREF

lprintf(5LL, "waiting for line to clear (VTIME=%d), read: ", a2);
tio_get((unsigned int)fd, &v8);
v5[0] = (__int128)_mm_loadu_si128(&v8);
v5[1] = (__int128)_mm_loadu_si128(&v9);
v5[2] = (__int128)_mm_loadu_si128(&v10);
v6 = v11;
v7 = v12;
v8.m128i_i32[3] &= 0xFFFFFFFD;
v9.m128i_i16[3] = (unsigned __int8)a2;
tio_set((unsigned int)fd, &v8);
v2 = 0;
while ( 1 )
{
v3 = read(fd, buf, 1uLL);
if ( v2 > 9999 || v3 <= 0 )
break;
if ( ++v2 <= 499 )
lputc(5LL, (unsigned int)buf[0]);
}
tio_set((unsigned int)fd, v5);
if ( v2 > 500 )
lprintf(3LL, "clean_line: only 500 of %d bytes logged", (unsigned int)v2);
if ( v2 > 9999 )
lprintf(0LL, "clean_line: got too much junk (dev=%s).", Device);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, unsigned int a2)
{
int v2; // ebx
ssize_t v3; // rax
__int128 v5[3]; // [rsp+0h] [rbp-98h] BYREF
__int64 v6; // [rsp+30h] [rbp-68h]
int v7; // [rsp+38h] [rbp-60h]
__m128i v8; // [rsp+40h] [rbp-58h] BYREF
__m128i v9; // [rsp+50h] [rbp-48h] BYREF
__m128i v10; // [rsp+60h] [rbp-38h] BYREF
__int64 v11; // [rsp+70h] [rbp-28h]
int v12; // [rsp+78h] [rbp-20h]
char buf[26]; // [rsp+7Eh] [rbp-1Ah] BYREF

lprintf(5LL, "waiting for line to clear (VTIME=%d), read: ", a2);
tio_get((unsigned int)fd, &v8);
v5[0] = (__int128)_mm_loadu_si128(&v8);
v5[1] = (__int128)_mm_loadu_si128(&v9);
v5[2] = (__int128)_mm_loadu_si128(&v10);
v6 = v11;
v7 = v12;
v8.m128i_i32[3] &= 0xFFFFFFFD;
v9.m128i_i16[3] = (unsigned __int8)a2;
tio_set((unsigned int)fd, &v8);
v2 = 0;
while ( 1 )
{
v3 = read(fd, buf, 1uLL);
if ( v2 > 9999 || v3 <= 0 )
break;
if ( ++v2 <= 499 )
lputc(5LL, (unsigned int)buf[0]);
}
tio_set((unsigned int)fd, v5);
if ( v2 > 500 )
lprintf(3LL, "clean_line: only 500 of %d bytes logged", (unsigned int)v2);
if ( v2 > 9999 )
lprintf(0LL, "clean_line: got too much junk (dev=%s).", Device);
return 0LL;
}

original_code:
int clean_line (int fd,int waittime)
{
    char buffer[2];
    int bytes = 0;
TIO tio, save_tio;
    lprintf( 5, "waiting for line to clear (VTIME=%d), read: ", waittime);
    tio_get( fd, &tio );
    save_tio = tio;
    tio.c_lflag &= ~0000002;
    tio.c_cc[6] = 0;
    tio.c_cc[5] = waittime;
    tio_set( fd, &tio );
    while ( read( fd, buffer, 1 ) > 0 &&
     bytes < 10000 )
    {
        if ( ++bytes < 500 ) lputc( 5, buffer[0] );
    }
    tio_set( fd, &save_tio );
    if ( bytes > 500 )
        lprintf( 3, "clean_line: only 500 of %d bytes logged", bytes );
    if ( bytes >= 10000 )
    {
 extern char * Device;
        lprintf( 0, "clean_line: got too much junk (dev=%s).", Device );
    }
    return 0;
}

canonical_original_code:
int clean_line(int fd, int waittime)
{
    char buffer[2];
    int bytes = 0;
struct termios tio; struct termios save_tio;
    lprintf( 5, "waiting for line to clear (VTIME=%d), read: ", waittime);
    tio_get( fd, &tio );
    save_tio = tio;
    tio.c_lflag &= ~0000002;
    tio.c_cc[6] = 0;
    tio.c_cc[5] = waittime;
    tio_set( fd, &tio );
    while ( read( fd, buffer, 1 ) > 0 &&
     bytes < 10000 )
    {
        if ( ++bytes < 500 ) lputc( 5, buffer[0] );
    }
    tio_set( fd, &save_tio );
    if ( bytes > 500 )
        lprintf( 3, "clean_line: only 500 of %d bytes logged", bytes );
    if ( bytes >= 10000 )
    {
 extern char *Device;
        lprintf( 0, "clean_line: got too much junk (dev=%s).", Device );
    }
    return 0;
}

variable_types: waittime int

variable_types:
buffer
char [2]

variable_types: bytes int

variable_types:
Device
char *

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


original_code:
int pbm_getint (int fd)
{
    char buf[50];
    int i;
    do
    {
 if ( read( fd, buf, 1 ) != 1 ) return -1;
 if ( buf[0] == '#' )
 {
     while( buf[0] != '\n' && read( fd, buf, 1 ) == 1 ) {}
 }
    }
    while ( ((*__ctype_b_loc ())[(int) ((buf[0]))] & (unsigned short int) _ISspace) );
    i = 1;
    while ( i < sizeof( buf ) -1 &&
     read( fd, &buf[i], 1 ) == 1 &&
     ! ((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISspace) )
    {
 i++;
    }
    if ( ! ((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short int) _ISspace) ) return -1;
    buf[i] = 0;
    return ( atoi( buf ) );
}

canonical_original_code:
int pbm_getint(int fd)
{
    char buf[50];
    int i;
    do
    {
 if ( read( fd, buf, 1 ) != 1 ) return -1;
 if ( buf[0] == '#' )
 {
     while( buf[0] != '\n' && read( fd, buf, 1 ) == 1 ) {}
 }
    }
    while ( ((*__ctype_b_loc ())[(int) ((buf[0]))] & (unsigned short) _ISspace) );
    i = 1;
    while ( i < sizeof( buf ) -1 &&
     read( fd, &buf[i], 1 ) == 1 &&
     ! ((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISspace) )
    {
 i++;
    }
    if ( ! ((*__ctype_b_loc ())[(int) ((buf[i]))] & (unsigned short) _ISspace) ) return -1;
    buf[i] = 0;
    return ( atoi( buf ) );
}

variable_types:
buf
char [50]

variable_types: i int


decompiled_code:
int __fastcall pbm_getheader(int fd)
{
ssize_t v1; // rax
char buf; // [rsp+6h] [rbp-12h] BYREF
char v4; // [rsp+7h] [rbp-11h]

v1 = read(fd, &buf, 2uLL);
if ( v1 == 2 && buf == 80 )
{
LODWORD(v1) = (unsigned __int8)(v4 - 49);
switch ( v4 )
{
case '1':
pbm_type = 1;
goto LABEL_7;
case '2':
pbm_type = 2;
goto LABEL_7;
case '3':
pbm_type = 3;
goto LABEL_7;
case '4':
pbm_type = 4;
goto LABEL_7;
case '5':
pbm_type = 5;
goto LABEL_7;
case '6':
pbm_type = 6;
LABEL_7:
pbm_xsize = pbm_getint(fd);
LODWORD(v1) = pbm_getint(fd);
pbm_ysize = v1;
break;
default:
pbm_type = 0;
break;
}
}
else
{
pbm_type = 0;
}
return v1;
}

canonical_decompiled_code:
int __fastcall func1(int fd)
{
ssize_t v1; // rax
char buf; // [rsp+6h] [rbp-12h] BYREF
char v4; // [rsp+7h] [rbp-11h]

v1 = read(fd, &buf, 2uLL);
if ( v1 == 2 && buf == 80 )
{
LODWORD(v1) = (unsigned __int8)(v4 - 49);
switch ( v4 )
{
case '1':
pbm_type = 1;
goto LABEL_7;
case '2':
pbm_type = 2;
goto LABEL_7;
case '3':
pbm_type = 3;
goto LABEL_7;
case '4':
pbm_type = 4;
goto LABEL_7;
case '5':
pbm_type = 5;
goto LABEL_7;
case '6':
pbm_type = 6;
LABEL_7:
pbm_xsize = func0(fd);
LODWORD(v1) = func0(fd);
pbm_ysize = v1;
break;
default:
pbm_type = 0;
break;
}
}
else
{
pbm_type = 0;
}
return v1;
}

original_code:
void pbm_getheader (int fd)
{
    char buf[10];
    if ( read( fd, buf, 2 ) != 2 || buf[0] != 'P' )
    {
 pbm_type = unknown; return;
    }
    switch( buf[1] )
    {
      case '1': pbm_type = pbm; break;
      case '2': pbm_type = pgm; break;
      case '3': pbm_type = ppm; break;
      case '4': pbm_type = pbm_raw; break;
      case '5': pbm_type = pgm_raw; break;
      case '6': pbm_type = ppm_raw; break;
      default: pbm_type = unknown; return;
    }
    pbm_xsize = pbm_getint( fd );
    pbm_ysize = pbm_getint( fd );
}

canonical_original_code:
void pbm_getheader(int fd)
{
    char buf[10];
    if ( read( fd, buf, 2 ) != 2 || buf[0] != 'P' )
    {
 pbm_type = unknown; return;
    }
    switch( buf[1] )
    {
      case '1': pbm_type = pbm; break;
      case '2': pbm_type = pgm; break;
      case '3': pbm_type = ppm; break;
      case '4': pbm_type = pbm_raw; break;
      case '5': pbm_type = pgm_raw; break;
      case '6': pbm_type = ppm_raw; break;
      default: pbm_type = unknown; return;
    }
    pbm_xsize = pbm_getint( fd );
    pbm_ysize = pbm_getint( fd );
}

variable_types: fd int

variable_types:
buf
char [10]


decompiled_code:
int __fastcall convert_pbm_raw(int fd, int a2)
{
int v2; // ebx
signed int v3; // er12
int v4; // ebx
int result; // eax
unsigned int i; // ebp
int v7; // eax
unsigned __int8 *v8; // r15
int v9; // er12
int v10; // er14
unsigned int v11; // ebp
int v12; // eax
int v13; // eax
char *v14; // [rsp+8h] [rbp-50h]
int v15; // [rsp+10h] [rbp-48h]
int v16; // [rsp+14h] [rbp-44h]
int v17; // [rsp+18h] [rbp-40h]
unsigned int v18; // [rsp+1Ch] [rbp-3Ch]

make_run_tables();
v2 = pbm_xsize + 7;
v3 = (pbm_xsize + 7) & 0xFFFFFFF8;
pbm_xsize = v3;
v4 = v2 >> 3;
v14 = (char *)malloc(v4);
if ( !v14 )
{
fprintf(stderr, "cannot malloc %d bytes: ", (unsigned int)v4);
perror(&s);
exit(5);
}
result = v3;
if ( v3 > a2 )
result = a2;
v16 = result;
v17 = 0;
v18 = a2 - result;
if ( pbm_ysize <= 0 )
return result;
do
{
for ( i = 0; v4 > (int)i; i += v7 )
{
v7 = read(fd, &v14[i], (int)(v4 - i));
if ( !v7 )
return fprintf(stderr, "line %d: want %d, got %d bytes, EOF", (unsigned int)v17, (unsigned int)v4, i);
}
if ( v16 <= 0 )
{
v11 = 0;
goto LABEL_26;
}
v8 = (unsigned __int8 *)v14;
v9 = 7;
v10 = 0;
v11 = 0;
v15 = 0;
do
{
if ( !v10 )
{
v13 = w_rtab[256 * (__int64)v9 + *v8];
v11 += v13;
v9 -= v13;
if ( v9 < 0 )
goto LABEL_20;
putwhitespan(v11);
LABEL_16:
v15 += v11;
v10 ^= 1u;
v11 = 0;
continue;
}
v12 = b_rtab[256 * (__int64)v9 + *v8];
v11 += v12;
v9 -= v12;
if ( v9 >= 0 )
{
putblackspan(v11);
goto LABEL_16;
}
LABEL_20:
if ( v9 != -1 )
fprintf(stderr, "bit panic: %d\n", (unsigned int)v9);
++v8;
v9 = 7;
}
while ( v16 > (int)(v11 + v15) );
if ( v10 )
{
putblackspan(v11);
putwhitespan(v18);
goto LABEL_27;
}
LABEL_26:
putwhitespan(v18 + v11);
LABEL_27:
puteol();
result = ++v17;
}
while ( pbm_ysize > v17 );
return result;
}

canonical_decompiled_code:
int __fastcall func3(int fd, int a2)
{
int v2; // ebx
signed int v3; // er12
int v4; // ebx
int result; // eax
unsigned int i; // ebp
int v7; // eax
unsigned __int8 *v8; // r15
int v9; // er12
int v10; // er14
unsigned int v11; // ebp
int v12; // eax
int v13; // eax
char *v14; // [rsp+8h] [rbp-50h]
int v15; // [rsp+10h] [rbp-48h]
int v16; // [rsp+14h] [rbp-44h]
int v17; // [rsp+18h] [rbp-40h]
unsigned int v18; // [rsp+1Ch] [rbp-3Ch]

make_run_tables();
v2 = pbm_xsize + 7;
v3 = (pbm_xsize + 7) & 0xFFFFFFF8;
pbm_xsize = v3;
v4 = v2 >> 3;
v14 = (char *)malloc(v4);
if ( !v14 )
{
fprintf(stderr, "cannot malloc %d bytes: ", (unsigned int)v4);
perror(&s);
exit(5);
}
result = v3;
if ( v3 > a2 )
result = a2;
v16 = result;
v17 = 0;
v18 = a2 - result;
if ( pbm_ysize <= 0 )
return result;
do
{
for ( i = 0; v4 > (int)i; i += v7 )
{
v7 = read(fd, &v14[i], (int)(v4 - i));
if ( !v7 )
return fprintf(stderr, "line %d: want %d, got %d bytes, EOF", (unsigned int)v17, (unsigned int)v4, i);
}
if ( v16 <= 0 )
{
v11 = 0;
goto LABEL_26;
}
v8 = (unsigned __int8 *)v14;
v9 = 7;
v10 = 0;
v11 = 0;
v15 = 0;
do
{
if ( !v10 )
{
v13 = w_rtab[256 * (__int64)v9 + *v8];
v11 += v13;
v9 -= v13;
if ( v9 < 0 )
goto LABEL_20;
putwhitespan(v11);
LABEL_16:
v15 += v11;
v10 ^= 1u;
v11 = 0;
continue;
}
v12 = b_rtab[256 * (__int64)v9 + *v8];
v11 += v12;
v9 -= v12;
if ( v9 >= 0 )
{
putblackspan(v11);
goto LABEL_16;
}
LABEL_20:
if ( v9 != -1 )
fprintf(stderr, "bit panic: %d\n", (unsigned int)v9);
++v8;
v9 = 7;
}
while ( v16 > (int)(v11 + v15) );
if ( v10 )
{
putblackspan(v11);
putwhitespan(v18);
goto LABEL_27;
}
LABEL_26:
putwhitespan(v18 + v11);
LABEL_27:
puteol();
result = ++v17;
}
while ( pbm_ysize > v17 );
return result;
}

original_code:
void convert_pbm_raw (int fd,int g3_page_width)
{
    extern void make_run_tables (void);
    extern char w_rtab[8][256],
                b_rtab[8][256];
    int x, y, maxx;
    int run, c;
    int bit;
    int ll;
    unsigned char * linebuf, * r;
    make_run_tables();
    pbm_xsize = ( pbm_xsize + 7 ) & ~7;
    ll = pbm_xsize / 8;
    linebuf = (unsigned char *) malloc( ll );
    if ( linebuf == ((void *)0) )
    {
 fprintf( stderr, "cannot malloc %d bytes: ", ll );
 perror( "" );
 exit(5);
    }
    if ( g3_page_width > pbm_xsize ) maxx = pbm_xsize;
                                else maxx = g3_page_width;
    for ( y=0; y<pbm_ysize; y++ )
    {
 int h;
 c = 0;
 run = 0;
 x = 0;
 bit = 7;
 h = 0;
 while ( h < ll )
 {
     int h2;
     h2 = read( fd, linebuf+h, ll-h );
     if ( h2 == 0 )
     {
  fprintf( stderr, "line %d: want %d, got %d bytes, EOF", y, ll, h );
  return;
     }
     h += h2;
 }
 r = linebuf;
 while ( x+run < maxx )
 {
     if ( c == 0 )
     {
  run += w_rtab[ bit ][ *r ];
  bit -= w_rtab[ bit ][ *r ];
     }
     else
     {
  run += b_rtab[ bit ][ *r ];
  bit -= b_rtab[ bit ][ *r ];
     }
     if ( bit < 0 )
     {
  if ( bit != -1 ) fprintf( stderr, "bit panic: %d\n", bit );
  bit = 7;
  r++;
     }
     else
     {
  if ( c == 0 )
      putwhitespan( run );
  else
      putblackspan( run );
                x += run;
  run = 0;
  c = !c;
     }
 }
 if ( c == 0 ) putwhitespan( run + (g3_page_width - maxx) );
 else
 {
     putblackspan( run );
     putwhitespan( g3_page_width - maxx );
 }
        puteol();
    }
}

canonical_original_code:
void convert_pbm_raw(int fd, int g3_page_width)
{
    extern void make_run_tables();
    extern char w_rtab[8][256]; extern char b_rtab[8][256];
    int x; int y; int maxx;
    int run; int c;
    int bit;
    int ll;
    unsigned char *linebuf; unsigned char *r;
    make_run_tables();
    pbm_xsize = ( pbm_xsize + 7 ) & ~7;
    ll = pbm_xsize / 8;
    linebuf = (unsigned char *) malloc( ll );
    if ( linebuf == ((void *)0) )
    {
 fprintf( stderr, "cannot malloc %d bytes: ", ll );
 perror( "" );
 exit(5);
    }
    if ( g3_page_width > pbm_xsize ) maxx = pbm_xsize;
                                else maxx = g3_page_width;
    for ( y=0; y<pbm_ysize; y++ )
    {
 int h;
 c = 0;
 run = 0;
 x = 0;
 bit = 7;
 h = 0;
 while ( h < ll )
 {
     int h2;
     h2 = read( fd, linebuf+h, ll-h );
     if ( h2 == 0 )
     {
  fprintf( stderr, "line %d: want %d, got %d bytes, EOF", y, ll, h );
  return;
     }
     h += h2;
 }
 r = linebuf;
 while ( x+run < maxx )
 {
     if ( c == 0 )
     {
  run += w_rtab[ bit ][ *r ];
  bit -= w_rtab[ bit ][ *r ];
     }
     else
     {
  run += b_rtab[ bit ][ *r ];
  bit -= b_rtab[ bit ][ *r ];
     }
     if ( bit < 0 )
     {
  if ( bit != -1 ) fprintf( stderr, "bit panic: %d\n", bit );
  bit = 7;
  r++;
     }
     else
     {
  if ( c == 0 )
      putwhitespan( run );
  else
      putblackspan( run );
                x += run;
  run = 0;
  c = !c;
     }
 }
 if ( c == 0 ) putwhitespan( run + (g3_page_width - maxx) );
 else
 {
     putblackspan( run );
     putwhitespan( g3_page_width - maxx );
 }
        puteol();
    }
}

variable_types: x int

variable_types: y int

variable_types: maxx int

variable_types: run int

variable_types: c int

variable_types: bit int

variable_types: ll int

variable_types:
linebuf
unsigned char *

variable_types:
r
unsigned char *