decompiled_code:
__int64 __fastcall voice_register_event_handler(__int64 a1)
{
program_handle_event = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
program_handle_event = a1;
return 0LL;
}

original_code:
int voice_register_event_handler(int (*new_program_handle_event) (int event,
 event_data data))
     {
     program_handle_event = new_program_handle_event;
     return((0));
     }

canonical_original_code:
int voice_register_event_handler(int (*new_program_handle_event)(int event, union event_data data))
     {
     program_handle_event = new_program_handle_event;
     return((0));
     }

variable_types: int (*)(int event, union event_data { char c; int i; void *p; struct { int frequency; int length; } beep; } data)


repo: Distrotech/mgetty

decompiled_code:
__int64 voice_unregister_event_handler()
{
program_handle_event = 0LL;
return 0LL;
}

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

original_code:
int voice_unregister_event_handler(void)
     {
     program_handle_event = ((void *)0);
     return((0));
     }

canonical_original_code:
int voice_unregister_event_handler()
     {
     program_handle_event = ((void *)0);
     return((0));
     }

return_type: int


decompiled_code:
__int64 __fastcall voice_handle_event(signed int a1, __int64 a2)
{
const char *v2; // rax
const char *v3; // r13
char *v4; // rax
unsigned int v5; // er12
unsigned int v6; // eax
char *v8; // rax
char v9[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = "NUL";
v9[0] = a2;
if ( (_BYTE)a2 )
v2 = v9;
v9[1] = 0;
v3 = v2;
v4 = event_name(a1);
lprintf(6LL, "%s: voice_handle_event got event %s with data <%s>", program_name, v4, v3);
if ( a1 != 8198 || !*((_QWORD *)&cvd + 43) )
{
v5 = voice_shell_handle_event((unsigned int)a1, a2);
if ( v5 == -2 )
{
if ( !program_handle_event || (v6 = program_handle_event((unsigned int)a1, a2), v6 == -2) )
{
if ( a1 <= 16387 )
{
if ( a1 <= 16385 )
{
switch ( voice_modem_state )
{
case 0:
LABEL_19:
if ( (unsigned int)(a1 - 8193) > 5 || ((1LL << ((unsigned __int8)a1 - 1)) & 0x29) == 0 )
goto LABEL_15;
return (*(unsigned int (**)(void))(voice_modem + 384LL))();
case 5:
LABEL_24:
if ( (unsigned int)(a1 - 8193) > 8 || ((1LL << ((unsigned __int8)a1 - 1)) & 0x1ED) == 0 )
goto LABEL_15;
return (*(unsigned int (**)(void))(voice_modem + 392LL))();
case 6:
LABEL_22:
switch ( a1 )
{
case 8193:
case 8195:
case 8196:
case 8198:
case 8199:
case 8200:
case 8201:
case 8206:
case 8210:
return (*(unsigned int (**)(void))(voice_modem + 400LL))();
default:
goto LABEL_15;
}
case 7:
LABEL_23:
switch ( a1 )
{
case 8193:
case 8195:
case 8196:
case 8198:
case 8201:
case 8207:
return (*(unsigned int (**)(void))(voice_modem + 408LL))();
default:
goto LABEL_15;
}
case 8:
LABEL_18:
switch ( a1 )
{
case 8193:
case 8195:
case 8196:
case 8198:
case 8201:
case 8206:
case 8210:
return (*(unsigned int (**)(void))(voice_modem + 472LL))();
default:
goto LABEL_15;
}
default:
LABEL_15:
if ( a1 == 0x4000 )
{
alarm(*((_DWORD *)&cvd + 152));
return 0LL;
}
LABEL_30:
v9[0] = a2;
v8 = event_name(a1);
lprintf(6LL, "%s: voice_handle_event got unknown event %s with data <%s>", program_name, v8, v3);
break;
}
return v5;
}
}
else if ( (unsigned int)(a1 - 16389) > 1 )
{
switch ( voice_modem_state )
{
case 0:
goto LABEL_19;
case 5:
goto LABEL_24;
case 6:
goto LABEL_22;
case 7:
goto LABEL_23;
case 8:
goto LABEL_18;
default:
goto LABEL_30;
}
}
lprintf(4LL, "%s: Received signal to terminate", program_name);
voice_stop_current_action();
(*(void (__fastcall **)(__int64))(voice_modem + 376LL))(1LL);
voice_mode_off();
voice_close_device();
voice_unregister_event_handler(99LL);
exit(99);
}
v5 = v6;
}
return v5;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(signed int a1, __int64 a2)
{
const char *v2; // rax
const char *v3; // r13
char *v4; // rax
unsigned int v5; // er12
unsigned int v6; // eax
char *v8; // rax
char v9[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v2 = "NUL";
v9[0] = a2;
if ( (_BYTE)a2 )
v2 = v9;
v9[1] = 0;
v3 = v2;
v4 = func0(a1);
lprintf(6LL, "%s: voice_handle_event got event %s with data <%s>", program_name, v4, v3);
if ( a1 != 8198 || !*((_QWORD *)&cvd + 43) )
{
v5 = voice_shell_handle_event((unsigned int)a1, a2);
if ( v5 == -2 )
{
if ( !program_handle_event || (v6 = program_handle_event((unsigned int)a1, a2), v6 == -2) )
{
if ( a1 <= 16387 )
{
if ( a1 <= 16385 )
{
switch ( voice_modem_state )
{
case 0:
LABEL_19:
if ( (unsigned int)(a1 - 8193) > 5 || ((1LL << ((unsigned __int8)a1 - 1)) & 0x29) == 0 )
goto LABEL_15;
return (*(unsigned int (**)(void))(voice_modem + 384LL))();
case 5:
LABEL_24:
if ( (unsigned int)(a1 - 8193) > 8 || ((1LL << ((unsigned __int8)a1 - 1)) & 0x1ED) == 0 )
goto LABEL_15;
return (*(unsigned int (**)(void))(voice_modem + 392LL))();
case 6:
LABEL_22:
switch ( a1 )
{
case 8193:
case 8195:
case 8196:
case 8198:
case 8199:
case 8200:
case 8201:
case 8206:
case 8210:
return (*(unsigned int (**)(void))(voice_modem + 400LL))();
default:
goto LABEL_15;
}
case 7:
LABEL_23:
switch ( a1 )
{
case 8193:
case 8195:
case 8196:
case 8198:
case 8201:
case 8207:
return (*(unsigned int (**)(void))(voice_modem + 408LL))();
default:
goto LABEL_15;
}
case 8:
LABEL_18:
switch ( a1 )
{
case 8193:
case 8195:
case 8196:
case 8198:
case 8201:
case 8206:
case 8210:
return (*(unsigned int (**)(void))(voice_modem + 472LL))();
default:
goto LABEL_15;
}
default:
LABEL_15:
if ( a1 == 0x4000 )
{
alarm(*((_DWORD *)&cvd + 152));
return 0LL;
}
LABEL_30:
v9[0] = a2;
v8 = func0(a1);
lprintf(6LL, "%s: voice_handle_event got unknown event %s with data <%s>", program_name, v8, v3);
break;
}
return v5;
}
}
else if ( (unsigned int)(a1 - 16389) > 1 )
{
switch ( voice_modem_state )
{
case 0:
goto LABEL_19;
case 5:
goto LABEL_24;
case 6:
goto LABEL_22;
case 7:
goto LABEL_23;
case 8:
goto LABEL_18;
default:
goto LABEL_30;
}
}
lprintf(4LL, "%s: Received signal to terminate", program_name);
func1();
(*(void (__fastcall **)(__int64))(voice_modem + 376LL))(1LL);
voice_mode_off();
voice_close_device();
func3(99LL);
exit(99);
}
v5 = v6;
}
return v5;
}
return 0LL;
}

original_code:
int voice_handle_event(int event, event_data data)
     {
     int result;
     char buffer[2];
     buffer[0] = data.c;
     buffer[1] = '\0';
     lprintf(6,
             "%s: voice_handle_event got event %s with data <%s>",
             program_name,
             event_name(event),
             data.c ? buffer
                    : "NUL");
     if ((event == (0x2006)) && (cvd.ignore_fax_dle.d.i))
          return((0));
     if ((result = voice_shell_handle_event(event, data)) != (-2))
          return(result);
     if (program_handle_event != ((void *)0))
          if ((result = program_handle_event(event, data)) != (-2))
               return(result);
     switch (event)
          {
          case (0x4002):
          case (0x4003):
          case (0x4005):
          case (0x4006):
               lprintf(4, "%s: Received signal to terminate",
                program_name);
               voice_stop_current_action();
               voice_modem->set_device((0x0001));
               voice_mode_off();
               voice_close_device();
               voice_unregister_event_handler();
               exit(99);
          };
     switch (voice_modem_state)
          {
          case (0x0000):
               switch (event)
                    {
                    case (0x2001):
                    case (0x2004):
                    case (0x2006):
                         return(voice_modem->stop_dialing());
                    };
               break;
          case (0x0005):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x2008):
                    case (0x2007):
                         return(voice_modem->stop_playing());
                    };
               break;
          case (0x0006):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x200e):
                    case (0x2012):
                    case (0x2008):
                    case (0x2007):
                         return(voice_modem->stop_recording());
                    };
               break;
   case (0x0008):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x200e):
                    case (0x2012):
                         return(voice_modem->stop_duplex_voice());
                    };
        break;
          case (0x0007):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x200f):
                         return(voice_modem->stop_waiting());
                    };
               break;
          };
     if (event == (0x4000))
          {
          alarm(cvd.watchdog_timeout.d.i);
          return((0));
          }
     buffer[0] = data.c;
     lprintf(6,
             "%s: voice_handle_event got unknown event %s with data <%s>",
             program_name, event_name(event), data.c ? buffer : "NUL");
     return((-2));
     }

canonical_original_code:
int voice_handle_event(int event, union event_data data)
     {
     int result;
     char buffer[2];
     buffer[0] = data.c;
     buffer[1] = '\0';
     lprintf(6,
             "%s: voice_handle_event got event %s with data <%s>",
             program_name,
             event_name(event),
             data.c ? buffer
                    : "NUL");
     if ((event == (0x2006)) && (cvd.ignore_fax_dle.d.i))
          return((0));
     if ((result = voice_shell_handle_event(event, data)) != (-2))
          return(result);
     if (program_handle_event != ((void *)0))
          if ((result = program_handle_event(event, data)) != (-2))
               return(result);
     switch (event)
          {
          case (0x4002):
          case (0x4003):
          case (0x4005):
          case (0x4006):
               lprintf(4, "%s: Received signal to terminate",
                program_name);
               voice_stop_current_action();
               voice_modem->set_device((0x0001));
               voice_mode_off();
               voice_close_device();
               voice_unregister_event_handler();
               exit(99);
          };
     switch (voice_modem_state)
          {
          case (0x0000):
               switch (event)
                    {
                    case (0x2001):
                    case (0x2004):
                    case (0x2006):
                         return(voice_modem->stop_dialing());
                    };
               break;
          case (0x0005):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x2008):
                    case (0x2007):
                         return(voice_modem->stop_playing());
                    };
               break;
          case (0x0006):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x200e):
                    case (0x2012):
                    case (0x2008):
                    case (0x2007):
                         return(voice_modem->stop_recording());
                    };
               break;
   case (0x0008):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x200e):
                    case (0x2012):
                         return(voice_modem->stop_duplex_voice());
                    };
        break;
          case (0x0007):
               switch (event)
                    {
                    case (0x2009):
                    case (0x2001):
                    case (0x2003):
                    case (0x2004):
                    case (0x2006):
                    case (0x200f):
                         return(voice_modem->stop_waiting());
                    };
               break;
          };
     if (event == (0x4000))
          {
          alarm(cvd.watchdog_timeout.d.i);
          return((0));
          }
     buffer[0] = data.c;
     lprintf(6,
             "%s: voice_handle_event got unknown event %s with data <%s>",
             program_name, event_name(event), data.c ? buffer : "NUL");
     return((-2));
     }

variable_types: event int

variable_types: union event_data { char c; int i; void *p; struct { int frequency; int length; } beep; }

variable_types: result int

variable_types:
buffer
char [2]


decompiled_code:
void __fastcall clear_event(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func5(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void clear_event(event_type* event)
     {
     if (event != ((void *)0))
          free(event);
     event = ((void *)0);
     }

canonical_original_code:
void clear_event(struct event_type *event)
     {
     if (event != ((void *)0))
          free(event);
     event = ((void *)0);
     }

variable_types: struct event_type { int event; union event_data { char c; int i; void *p; struct { int frequency; int length; } beep; } data; } *

return_type: void

user_defined_types: struct event_type { int event; union event_data data; }


decompiled_code:
__int64 __fastcall queue_event(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rbp
int v3; // ebx
__int64 v4; // rax
unsigned __int64 v5; // rdx
unsigned int v6; // edi
char *v7; // rax
__int64 result; // rax

v1 = last_event;
v2 = 4LL * last_event;
++event_queue[v2];
if ( ++event_count > 63 )
{
lprintf(3LL, "%s: event queue full, ignoring event", program_name);
--event_queue[v2];
--event_count;
result = 0xFFFFFFFFLL;
}
else
{
v3 = v1;
if ( event_queue[v2] > 1 )
{
do
{
--event_queue[4 * v1];
v3 = (v3 + 1) % 64;
v4 = 4LL * v3;
++event_queue[v4];
v1 = v3;
}
while ( event_queue[v4] > 1 );
}
v5 = 4 * v1;
last_event = (last_event + 1) % 64;
qword_7C8[v5 / 2] = (__int64)a1;
v6 = *a1;
--event_queue[v5];
v7 = event_name(v6);
lprintf(6LL, "%s: queued event %s at position %04d", program_name, v7, (unsigned int)v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1)
{
__int64 v1; // rdx
__int64 v2; // rbp
int v3; // ebx
__int64 v4; // rax
unsigned __int64 v5; // rdx
unsigned int v6; // edi
char *v7; // rax
__int64 result; // rax

v1 = last_event;
v2 = 4LL * last_event;
++event_queue[v2];
if ( ++event_count > 63 )
{
lprintf(3LL, "%s: event queue full, ignoring event", program_name);
--event_queue[v2];
--event_count;
result = 0xFFFFFFFFLL;
}
else
{
v3 = v1;
if ( event_queue[v2] > 1 )
{
do
{
--event_queue[4 * v1];
v3 = (v3 + 1) % 64;
v4 = 4LL * v3;
++event_queue[v4];
v1 = v3;
}
while ( event_queue[v4] > 1 );
}
v5 = 4 * v1;
last_event = (last_event + 1) % 64;
qword_7C8[v5 / 2] = (__int64)a1;
v6 = *a1;
--event_queue[v5];
v7 = func0(v6);
lprintf(6LL, "%s: queued event %s at position %04d", program_name, v7, (unsigned int)v3);
result = 0LL;
}
return result;
}

original_code:
int queue_event(event_type* event)
     {
     int event_number = last_event;
     event_queue[event_number].write_lock++;
     event_count++;
     if (event_count >= 64)
          {
          lprintf(3, "%s: event queue full, ignoring event", program_name);
          event_queue[event_number].write_lock--;
          event_count--;
          return((-1));
          };
     while(event_queue[event_number].write_lock > 1)
          {
          event_queue[event_number].write_lock--;
          event_number = (event_number + 1) % 64;
          event_queue[event_number].write_lock++;
          };
     last_event = (last_event + 1) % 64;
     event_queue[event_number].event = event;
     event_queue[event_number].write_lock--;
     lprintf(6, "%s: queued event %s at position %04d", program_name, event_name(event->event),
      event_number);
     return((0));
     }

canonical_original_code:
int queue_event(struct event_type *event)
     {
     int event_number = last_event;
     event_queue[event_number].write_lock++;
     event_count++;
     if (event_count >= 64)
          {
          lprintf(3, "%s: event queue full, ignoring event", program_name);
          event_queue[event_number].write_lock--;
          event_count--;
          return((-1));
          };
     while(event_queue[event_number].write_lock > 1)
          {
          event_queue[event_number].write_lock--;
          event_number = (event_number + 1) % 64;
          event_queue[event_number].write_lock++;
          };
     last_event = (last_event + 1) % 64;
     event_queue[event_number].event = event;
     event_queue[event_number].write_lock--;
     lprintf(6, "%s: queued event %s at position %04d", program_name, event_name(event->event),
      event_number);
     return((0));
     }

user_defined_types: union event_data { char c; int i; void *p; struct { int frequency; int length; } beep; }


decompiled_code:
__int64 reset_watchdog()
{
unsigned int *v0; // rax

v0 = create_event(0x4000);
return queue_event(v0);
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int *v0; // rax

v0 = create_event(0x4000);
return func6(v0);
}

original_code:
void reset_watchdog(void)
     {
     queue_event(create_event((0x4000)));
     }

canonical_original_code:
void reset_watchdog()
     {
     queue_event(create_event((0x4000)));
     }


decompiled_code:
__int64 unqueue_event()
{
__int64 v0; // rax
int *v1; // rdx
int v2; // ebx
__int64 v3; // rdx
int *v4; // rbp
char *v5; // rax
__int64 result; // rax

v0 = first_event;
v1 = &event_queue[4 * first_event];
++v1[1];
if ( --event_count < 0 )
{
--v1[1];
++event_count;
result = 0LL;
}
else
{
v2 = v0;
if ( v1[1] > 1 )
{
do
{
--dword_7C4[4 * v0];
v2 = (v2 + 1) % 64;
v0 = v2;
v3 = 4LL * v2;
++dword_7C4[v3];
}
while ( event_queue[v3 + 1] > 1 );
}
v4 = &event_queue[4 * v0];
if ( *v4 > 0 )
{
--v4[1];
++event_count;
result = 0LL;
}
else
{
first_event = (first_event + 1) % 64;
--v4[1];
v5 = event_name(**((_DWORD **)v4 + 1));
lprintf(6LL, "%s: unqueued event %s at position %04d", program_name, v5, (unsigned int)v2);
result = *((_QWORD *)v4 + 1);
}
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 v0; // rax
int *v1; // rdx
int v2; // ebx
__int64 v3; // rdx
int *v4; // rbp
char *v5; // rax
__int64 result; // rax

v0 = first_event;
v1 = &event_queue[4 * first_event];
++v1[1];
if ( --event_count < 0 )
{
--v1[1];
++event_count;
result = 0LL;
}
else
{
v2 = v0;
if ( v1[1] > 1 )
{
do
{
--dword_7C4[4 * v0];
v2 = (v2 + 1) % 64;
v0 = v2;
v3 = 4LL * v2;
++dword_7C4[v3];
}
while ( event_queue[v3 + 1] > 1 );
}
v4 = &event_queue[4 * v0];
if ( *v4 > 0 )
{
--v4[1];
++event_count;
result = 0LL;
}
else
{
first_event = (first_event + 1) % 64;
--v4[1];
v5 = func0(**((_DWORD **)v4 + 1));
lprintf(6LL, "%s: unqueued event %s at position %04d", program_name, v5, (unsigned int)v2);
result = *((_QWORD *)v4 + 1);
}
}
return result;
}

original_code:
event_type* unqueue_event(void)
     {
     int event_number = first_event;
     event_queue[event_number].read_lock++;
     event_count--;
     if (event_count < 0)
          {
          event_queue[event_number].read_lock--;
          event_count++;
          return(((void *)0));
          };
     while(event_queue[event_number].read_lock > 1)
          {
          event_queue[event_number].read_lock--;
          event_number = (event_number + 1) % 64;
          event_queue[event_number].read_lock++;
          };
     if (event_queue[event_number].write_lock > 0)
          {
          event_queue[event_number].read_lock--;
          event_count++;
          return(((void *)0));
          };
     first_event = (first_event + 1) % 64;
     event_queue[event_number].read_lock--;
     lprintf(6, "%s: unqueued event %s at position %04d", program_name,
      event_name(event_queue[event_number].event->event), event_number);
     return(event_queue[event_number].event);
     }

canonical_original_code:
struct event_type *unqueue_event()
     {
     int event_number = first_event;
     event_queue[event_number].read_lock++;
     event_count--;
     if (event_count < 0)
          {
          event_queue[event_number].read_lock--;
          event_count++;
          return(((void *)0));
          };
     while(event_queue[event_number].read_lock > 1)
          {
          event_queue[event_number].read_lock--;
          event_number = (event_number + 1) % 64;
          event_queue[event_number].read_lock++;
          };
     if (event_queue[event_number].write_lock > 0)
          {
          event_queue[event_number].read_lock--;
          event_count++;
          return(((void *)0));
          };
     first_event = (first_event + 1) % 64;
     event_queue[event_number].read_lock--;
     lprintf(6, "%s: unqueued event %s at position %04d", program_name,
      event_name(event_queue[event_number].event->event), event_number);
     return(event_queue[event_number].event);
     }

return_type: struct event_type { int event; union event_data { char c; int i; void *p; struct { int frequency; int length; } beep; } data; } *


decompiled_code:
__int64 voice_check_events()
{
__int64 result; // rax
void *v1; // rbp

while ( 1 )
{
result = unqueue_event();
v1 = (void *)result;
if ( !result )
break;
if ( (unsigned int)voice_handle_event(*(_DWORD *)result, *(_QWORD *)(result + 8)) == -1 )
{
lprintf(3LL, "%s: Could not handle event, something failed", program_name);
exit(99);
}
clear_event(v1);
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
__int64 result; // rax
void *v1; // rbp

while ( 1 )
{
result = func8();
v1 = (void *)result;
if ( !result )
break;
if ( (unsigned int)func4(*(_DWORD *)result, *(_QWORD *)(result + 8)) == -1 )
{
lprintf(3LL, "%s: Could not handle event, something failed", program_name);
exit(99);
}
func5(v1);
}
return result;
}

original_code:
void voice_check_events(void)
     {
     event_type* event;
     while ((event = unqueue_event()) != ((void *)0))
          {
          if (voice_handle_event(event->event, event->data) == (-1))
               {
               lprintf(3, "%s: Could not handle event, something failed", program_name);
               exit(99);
               };
          clear_event(event);
          };
     }

canonical_original_code:
void voice_check_events()
     {
     struct event_type *event;
     while ((event = unqueue_event()) != ((void *)0))
          {
          if (voice_handle_event(event->event, event->data) == (-1))
               {
               lprintf(3, "%s: Could not handle event, something failed", program_name);
               exit(99);
               };
          clear_event(event);
          };
     }


Postprocessing

original_code:
static void Postprocessing (struct gsm_state * S, register word * s)
{
     register int k;
     register word msr = S->msr;
     register longword ltmp;
     register word tmp;
     for (k = 160; k--; s++) {
          tmp = (((((longword)(msr) * (longword)(28180) + 16384)) >> (15)));
          msr = ((ulongword)((ltmp = (longword)(*s) + (longword)(tmp)) - ((-32767)-1)) > ( 32767) - ((-32767)-1) ? (ltmp > 0 ? ( 32767) : ((-32767)-1)) : ltmp);
          *s = ((ulongword)((ltmp = (longword)(msr) + (longword)(msr)) - ((-32767)-1)) > ( 32767) - ((-32767)-1) ? (ltmp > 0 ? ( 32767) : ((-32767)-1)) : ltmp) & 0xFFF8;
     }
     S->msr = msr;
}

canonical_original_code:
static void Postprocessing(struct gsm_state *S, short *s)
{
     register int k;
     register short msr = S->msr;
     register long ltmp;
     register short tmp;
     for (k = 160; k--; s++) {
          tmp = (((((longword)(msr) * (longword)(28180) + 16384)) >> (15)));
          msr = ((ulongword)((ltmp = (longword)(*s) + (longword)(tmp)) - ((-32767)-1)) > ( 32767) - ((-32767)-1) ? (ltmp > 0 ? ( 32767) : ((-32767)-1)) : ltmp);
          *s = ((ulongword)((ltmp = (longword)(msr) + (longword)(msr)) - ((-32767)-1)) > ( 32767) - ((-32767)-1) ? (ltmp > 0 ? ( 32767) : ((-32767)-1)) : ltmp) & 0xFFF8;
     }
     S->msr = msr;
}

variable_types: struct gsm_state { short dp0[280]; short z1; long L_z2; int mp; short u[8]; short LARpp[2][8]; short j; short ltp_cut; short nrp; short v[9]; short msr; char verbose; char fast; } *

variable_types:
s
short *

variable_types: k int

variable_types: msr short

variable_types: ltmp long

variable_types: tmp short


decompiled_code:
__int64 __fastcall Gsm_Decoder(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int16 *a8)
{
__int64 v9; // rbx
char *v11; // r15
__int64 i; // rax
char v17[80]; // [rsp+20h] [rbp-1C8h] BYREF
char v18[376]; // [rsp+70h] [rbp-178h] BYREF

v9 = 0LL;
v11 = v18;
do
{
Gsm_RPE_Decoding(a1, (unsigned int)*(__int16 *)(a6 + v9), (unsigned int)*(__int16 *)(a5 + v9), a7, v17);
Gsm_Long_Term_Synthesis_Filtering(
a1,
(unsigned int)*(__int16 *)(a3 + v9),
(unsigned int)*(__int16 *)(a4 + v9),
v17,
a1 + 240);
for ( i = 0LL; i != 80; i += 2LL )
*(_WORD *)&v11[i] = *(_WORD *)(a1 + i + 240);
v9 += 2LL;
a7 += 26LL;
v11 += 80;
}
while ( v9 != 8 );
Gsm_Short_Term_Synthesis_Filter(a1, a2, v18, a8);
return Postprocessing(a1, a8);
}

original_code:
void Gsm_Decoder (struct gsm_state * S, word * LARcr, word * Ncr, word * bcr, word * Mcr, word * xmaxcr, word * xMcr, word * s)
{
     int j, k;
     word erp[40], wt[160];
     word * drp = S->dp0 + 120;
     for (j=0; j <= 3; j++, xmaxcr++, bcr++, Ncr++, Mcr++, xMcr += 13) {
          Gsm_RPE_Decoding( S, *xmaxcr, *Mcr, xMcr, erp );
          Gsm_Long_Term_Synthesis_Filtering( S, *Ncr, *bcr, erp, drp );
          for (k = 0; k <= 39; k++) wt[ j * 40 + k ] = drp[ k ];
     }
     Gsm_Short_Term_Synthesis_Filter( S, LARcr, wt, s );
     Postprocessing(S, s);
}

canonical_original_code:
void Gsm_Decoder(struct gsm_state *S, short *LARcr, short *Ncr, short *bcr, short *Mcr, short *xmaxcr, short *xMcr, short *s)
{
     int j; int k;
     short erp[40]; short wt[160];
     short *drp = S->dp0 + 120;
     for (j=0; j <= 3; j++, xmaxcr++, bcr++, Ncr++, Mcr++, xMcr += 13) {
          Gsm_RPE_Decoding( S, *xmaxcr, *Mcr, xMcr, erp );
          Gsm_Long_Term_Synthesis_Filtering( S, *Ncr, *bcr, erp, drp );
          for (k = 0; k <= 39; k++) wt[ j * 40 + k ] = drp[ k ];
     }
     Gsm_Short_Term_Synthesis_Filter( S, LARcr, wt, s );
     Postprocessing(S, s);
}

variable_types:
LARcr
short *

variable_types:
Ncr
short *

variable_types:
bcr
short *

variable_types:
Mcr
short *

variable_types:
xmaxcr
short *

variable_types:
xMcr
short *

variable_types: j int

variable_types:
erp
short [40]

variable_types:
wt
short [160]

variable_types:
drp
short *

user_defined_types: struct gsm_state { short dp0[280]; short z1; long L_z2; int mp; short u[8]; short LARpp[2][8]; short j; short ltp_cut; short nrp; short v[9]; short msr; char verbose; char fast; }


decompiled_code:
__int64 __fastcall mg_init_data(__int64 a1, __int64 a2, char a3, __int64 a4)
{
int v6; // eax
int v8; // eax
int v9; // ebx
int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( !(unsigned int)do_chat(a1, a2, &init_chat_actions, v10, 20LL, 1LL) )
return 0LL;
if ( a3 )
{
v8 = tio_get_rs232_lines((unsigned int)a1);
if ( v8 != -1 && (v8 & 0x120) == 0 )
{
lprintf(3LL, "No DSR/CTS signals, assuming modem is switched off, waiting...");
while ( (tio_get_rs232_lines((unsigned int)a1) & 0x120) == 0 )
sleep(0x3Cu);
}
}
v6 = v10[0];
if ( !v10[0] && a4 )
{
lprintf(3LL, "init chat timed out, trying force-init-chat");
if ( (unsigned int)do_chat((unsigned int)a1, a4, &init_chat_actions, v10, 20LL, 1LL) && !v10[0] )
goto LABEL_18;
lprintf(5LL, "force-init succeeded, retrying init-chat");
clean_line((unsigned int)a1, 3LL);
if ( !(unsigned int)do_chat((unsigned int)a1, a2, &init_chat_actions, v10, 20LL, 1LL) )
return 0LL;
v6 = v10[0];
}
v9 = 22;
if ( !v6 )
LABEL_18:
v9 = 4;
*_errno_location() = v9;
lprintf(1LL, "init chat failed, exiting...");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, char a3, __int64 a4)
{
int v6; // eax
int v8; // eax
int v9; // ebx
int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( !(unsigned int)do_chat(a1, a2, &init_chat_actions, v10, 20LL, 1LL) )
return 0LL;
if ( a3 )
{
v8 = tio_get_rs232_lines((unsigned int)a1);
if ( v8 != -1 && (v8 & 0x120) == 0 )
{
lprintf(3LL, "No DSR/CTS signals, assuming modem is switched off, waiting...");
while ( (tio_get_rs232_lines((unsigned int)a1) & 0x120) == 0 )
sleep(0x3Cu);
}
}
v6 = v10[0];
if ( !v10[0] && a4 )
{
lprintf(3LL, "init chat timed out, trying force-init-chat");
if ( (unsigned int)do_chat((unsigned int)a1, a4, &init_chat_actions, v10, 20LL, 1LL) && !v10[0] )
goto LABEL_18;
lprintf(5LL, "force-init succeeded, retrying init-chat");
clean_line((unsigned int)a1, 3LL);
if ( !(unsigned int)do_chat((unsigned int)a1, a2, &init_chat_actions, v10, 20LL, 1LL) )
return 0LL;
v6 = v10[0];
}
v9 = 22;
if ( !v6 )
LABEL_18:
v9 = 4;
*_errno_location() = v9;
lprintf(1LL, "init chat failed, exiting...");
return 0xFFFFFFFFLL;
}

original_code:
int mg_init_data (int fd,char * chat_seq[],boolean need_dsr,char * force_chat_seq[])
{
    action_t what_action;
    if ( do_chat( fd, chat_seq, init_chat_actions,
   &what_action, init_chat_timeout, (1==1) ) == 0 )
    {
 return 0;
    }
    if ( need_dsr )
    {
        int rs_lines = tio_get_rs232_lines(fd);
 if ( rs_lines != -1 &&
       (rs_lines & (0x100|0x020) ) == 0 )
 {
     lprintf( 3, "No DSR/CTS signals, assuming modem is switched off, waiting..." );
     while( (tio_get_rs232_lines(fd) & (0x100|0x020) ) == 0)
     {
  sleep(60);
     }
 }
    }
    if ( what_action == A_TIMOUT && force_chat_seq != ((void *)0) )
    {
 lprintf( 3, "init chat timed out, trying force-init-chat" );
 if ( do_chat( fd, force_chat_seq, init_chat_actions,
       &what_action, init_chat_timeout, (1==1) ) == 0 ||
      what_action != A_TIMOUT )
        {
     lprintf( 5, "force-init succeeded, retrying init-chat");
     clean_line(fd, 3);
     if ( do_chat( fd, chat_seq, init_chat_actions,
    &what_action, init_chat_timeout, (1==1) ) == 0 )
     {
  return 0;
     }
        }
    }
    (*__errno_location ()) = ( what_action == A_TIMOUT )? 4: 22;
    lprintf( 1, "init chat failed, exiting..." );
    return -1;
}

canonical_original_code:
int mg_init_data(int fd, char *chat_seq[], char need_dsr, char *force_chat_seq[])
{
    enum action_t what_action;
    if ( do_chat( fd, chat_seq, init_chat_actions,
   &what_action, init_chat_timeout, (1==1) ) == 0 )
    {
 return 0;
    }
    if ( need_dsr )
    {
        int rs_lines = tio_get_rs232_lines(fd);
 if ( rs_lines != -1 &&
       (rs_lines & (0x100|0x020) ) == 0 )
 {
     lprintf( 3, "No DSR/CTS signals, assuming modem is switched off, waiting..." );
     while( (tio_get_rs232_lines(fd) & (0x100|0x020) ) == 0)
     {
  sleep(60);
     }
 }
    }
    if ( what_action == A_TIMOUT && force_chat_seq != ((void *)0) )
    {
 lprintf( 3, "init chat timed out, trying force-init-chat" );
 if ( do_chat( fd, force_chat_seq, init_chat_actions,
       &what_action, init_chat_timeout, (1==1) ) == 0 ||
      what_action != A_TIMOUT )
        {
     lprintf( 5, "force-init succeeded, retrying init-chat");
     clean_line(fd, 3);
     if ( do_chat( fd, chat_seq, init_chat_actions,
    &what_action, init_chat_timeout, (1==1) ) == 0 )
     {
  return 0;
     }
        }
    }
    (*__errno_location ()) = ( what_action == A_TIMOUT )? 4: 22;
    lprintf( 1, "init chat failed, exiting..." );
    return -1;
}

variable_types: fd int

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 }


decompiled_code:
__int64 __fastcall mg_init_fax(unsigned int a1, __int64 a2, __int64 a3, char a4, unsigned int a5)
{
int v9; // eax
__int64 v10; // rdx
unsigned int v11; // er12
const char *v13; // rdx
const char *v14; // rdi
const char *v15; // rdi
const char *v16; // rdi
char v17[72]; // [rsp+0h] [rbp-48h] BYREF

v9 = fax_get_modem_type();
modem_type = v9;
if ( v9 == 1 )
{
v11 = -1;
lprintf(5LL, "no class 2/2.0 faxmodem, no faxing available");
return v11;
}
if ( (unsigned int)(v9 - 2) <= 1 )
{
v13 = "A";
if ( v9 == 2 )
v13 = "E";
sprintf(v17, "AT+FA%s=%d", v13, a4 == 0);
if ( (unsigned int)mdm_command(v17, a1) == -1 )
lprintf(4LL, "cannot set answer/reception flags");
fax_set_bor(a1, 1LL);
v9 = modem_type;
if ( modem_type != 5 )
{
LABEL_4:
if ( v9 != 4 )
goto LABEL_5;
goto LABEL_21;
}
}
else if ( v9 != 5 )
{
goto LABEL_4;
}
v14 = "AT+FAA=0;+FCR=1";
if ( !a4 )
v14 = "AT+FAA=1;+FCR=1";
if ( (unsigned int)mdm_command(v14, a1) == -1 )
lprintf(4LL, "cannot set answer/reception flags");
if ( (unsigned int)fax_set_bor(a1, 1LL) == -1 )
{
lprintf(4LL, "cannot set bit order, trying +BOR=0");
fax_set_bor(a1, 0LL);
}
v15 = "AT+FNR=1,1,1,1";
if ( (modem_quirks & 0x40) == 0 )
v15 = "AT+FNR=1,1,1,0";
mdm_command(v15, a1);
if ( modem_type == 4 )
{
LABEL_21:
v16 = "AT+FAA=0;+FCR=1";
if ( !a4 )
{
if ( (modem_quirks & 1) == 0 && (unsigned int)mdm_command("AT+FCLASS=0", a1) == -1 )
lprintf(4LL, "weird: cannot set class 0");
v16 = "AT+FAA=1;+FCR=1";
}
if ( (unsigned int)mdm_command(v16, a1) == -1 )
lprintf(4LL, "cannot set answer/reception flags");
if ( (unsigned int)fax_set_bor(a1, 0LL) == -1 )
lprintf(4LL, "cannot set bit order. Huh?");
}
LABEL_5:
fax_set_l_id(a1, a3);
v10 = a5;
v11 = 0;
if ( (unsigned int)fax_set_fdcc(a1, 1LL, v10, 0LL) != -1 )
return v11;
lprintf(4LL, "huh? Cannot set +FDCC parameters");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3, char a4, unsigned int a5)
{
int v9; // eax
__int64 v10; // rdx
unsigned int v11; // er12
const char *v13; // rdx
const char *v14; // rdi
const char *v15; // rdi
const char *v16; // rdi
char v17[72]; // [rsp+0h] [rbp-48h] BYREF

v9 = fax_get_modem_type();
modem_type = v9;
if ( v9 == 1 )
{
v11 = -1;
lprintf(5LL, "no class 2/2.0 faxmodem, no faxing available");
return v11;
}
if ( (unsigned int)(v9 - 2) <= 1 )
{
v13 = "A";
if ( v9 == 2 )
v13 = "E";
sprintf(v17, "AT+FA%s=%d", v13, a4 == 0);
if ( (unsigned int)mdm_command(v17, a1) == -1 )
lprintf(4LL, "cannot set answer/reception flags");
fax_set_bor(a1, 1LL);
v9 = modem_type;
if ( modem_type != 5 )
{
LABEL_4:
if ( v9 != 4 )
goto LABEL_5;
goto LABEL_21;
}
}
else if ( v9 != 5 )
{
goto LABEL_4;
}
v14 = "AT+FAA=0;+FCR=1";
if ( !a4 )
v14 = "AT+FAA=1;+FCR=1";
if ( (unsigned int)mdm_command(v14, a1) == -1 )
lprintf(4LL, "cannot set answer/reception flags");
if ( (unsigned int)fax_set_bor(a1, 1LL) == -1 )
{
lprintf(4LL, "cannot set bit order, trying +BOR=0");
fax_set_bor(a1, 0LL);
}
v15 = "AT+FNR=1,1,1,1";
if ( (modem_quirks & 0x40) == 0 )
v15 = "AT+FNR=1,1,1,0";
mdm_command(v15, a1);
if ( modem_type == 4 )
{
LABEL_21:
v16 = "AT+FAA=0;+FCR=1";
if ( !a4 )
{
if ( (modem_quirks & 1) == 0 && (unsigned int)mdm_command("AT+FCLASS=0", a1) == -1 )
lprintf(4LL, "weird: cannot set class 0");
v16 = "AT+FAA=1;+FCR=1";
}
if ( (unsigned int)mdm_command(v16, a1) == -1 )
lprintf(4LL, "cannot set answer/reception flags");
if ( (unsigned int)fax_set_bor(a1, 0LL) == -1 )
lprintf(4LL, "cannot set bit order. Huh?");
}
LABEL_5:
fax_set_l_id(a1, a3);
v10 = a5;
v11 = 0;
if ( (unsigned int)fax_set_fdcc(a1, 1LL, v10, 0LL) != -1 )
return v11;
lprintf(4LL, "huh? Cannot set +FDCC parameters");
return 0LL;
}

original_code:
int mg_init_fax (int fd,char * mclass,char * fax_id,boolean fax_only,int fax_max_speed)
{
    modem_type = fax_get_modem_type( fd, mclass );
    if ( modem_type == Mt_data )
    {
 lprintf( 5, "no class 2/2.0 faxmodem, no faxing available" );
 return -1;
    }
    if ( modem_type == Mt_class1 || modem_type == Mt_class1_0 )
    {
        char aabuf[20];
 sprintf( aabuf, "AT+FA%s=%d",
   modem_type == Mt_class1? "E": "A",
   fax_only? 0:1 );
 if ( mdm_command( aabuf, fd ) == -1 )
 {
     lprintf( 4, "cannot set answer/reception flags" );
 }
 fax_set_bor( fd, 1 );
    }
    if ( modem_type == Mt_class2_0 )
    {
 if ( mdm_command( fax_only? "AT+FAA=0;+FCR=1":
               "AT+FAA=1;+FCR=1", fd ) == -1 )
 {
     lprintf( 4, "cannot set answer/reception flags" );
 }
 if ( fax_set_bor( fd, 1 ) == -1 )
 {
     lprintf( 4, "cannot set bit order, trying +BOR=0" );
     fax_set_bor( fd, 0 );
 }
 mdm_command( (modem_quirks & 0x40)? "AT+FNR=1,1,1,1"
       : "AT+FNR=1,1,1,0", fd );
    }
    if ( modem_type == Mt_class2 )
    {
 if ( !fax_only && ! ( modem_quirks & 0x01 ) )
 {
     if ( mdm_command( "AT+FCLASS=0", fd ) == -1 )
     {
  lprintf( 4, "weird: cannot set class 0" );
     }
 }
 if ( mdm_command( fax_only? "AT+FAA=0;+FCR=1":
               "AT+FAA=1;+FCR=1", fd ) == -1 )
 {
     lprintf( 4, "cannot set answer/reception flags" );
 }
 if ( fax_set_bor( fd, 0 ) == -1 )
 {
     lprintf( 4, "cannot set bit order. Huh?" );
 }
    }
    fax_set_l_id( fd, fax_id );
    if ( fax_set_fdcc( fd, 1, fax_max_speed, 0 ) == -1 )
    {
 lprintf( 4, "huh? Cannot set +FDCC parameters" );
    }
    return 0;
}

canonical_original_code:
int mg_init_fax(int fd, char *mclass, char *fax_id, char fax_only, int fax_max_speed)
{
    modem_type = fax_get_modem_type( fd, mclass );
    if ( modem_type == Mt_data )
    {
 lprintf( 5, "no class 2/2.0 faxmodem, no faxing available" );
 return -1;
    }
    if ( modem_type == Mt_class1 || modem_type == Mt_class1_0 )
    {
        char aabuf[20];
 sprintf( aabuf, "AT+FA%s=%d",
   modem_type == Mt_class1? "E": "A",
   fax_only? 0:1 );
 if ( mdm_command( aabuf, fd ) == -1 )
 {
     lprintf( 4, "cannot set answer/reception flags" );
 }
 fax_set_bor( fd, 1 );
    }
    if ( modem_type == Mt_class2_0 )
    {
 if ( mdm_command( fax_only? "AT+FAA=0;+FCR=1":
               "AT+FAA=1;+FCR=1", fd ) == -1 )
 {
     lprintf( 4, "cannot set answer/reception flags" );
 }
 if ( fax_set_bor( fd, 1 ) == -1 )
 {
     lprintf( 4, "cannot set bit order, trying +BOR=0" );
     fax_set_bor( fd, 0 );
 }
 mdm_command( (modem_quirks & 0x40)? "AT+FNR=1,1,1,1"
       : "AT+FNR=1,1,1,0", fd );
    }
    if ( modem_type == Mt_class2 )
    {
 if ( !fax_only && ! ( modem_quirks & 0x01 ) )
 {
     if ( mdm_command( "AT+FCLASS=0", fd ) == -1 )
     {
  lprintf( 4, "weird: cannot set class 0" );
     }
 }
 if ( mdm_command( fax_only? "AT+FAA=0;+FCR=1":
               "AT+FAA=1;+FCR=1", fd ) == -1 )
 {
     lprintf( 4, "cannot set answer/reception flags" );
 }
 if ( fax_set_bor( fd, 0 ) == -1 )
 {
     lprintf( 4, "cannot set bit order. Huh?" );
 }
    }
    fax_set_l_id( fd, fax_id );
    if ( fax_set_fdcc( fd, 1, fax_max_speed, 0 ) == -1 )
    {
 lprintf( 4, "huh? Cannot set +FDCC parameters" );
    }
    return 0;
}

variable_types:
mclass
char *

variable_types:
aabuf
char [20]


decompiled_code:
__int64 __fastcall faxpoll_server_init(unsigned int a1, const char *a2)
{
const char *v4; // rdi
int v5; // eax
const char *v6; // rsi
__int64 v7; // rdi

faxpoll_server_file = 0LL;
if ( access(a2, 4) )
return lprintf(1LL, "cannot access/read '%s'", a2);
v4 = "AT+FLP=1";
if ( modem_type != 5 )
v4 = "AT+FLPL=1";
v5 = mdm_command(v4, a1);
v6 = "faxpoll_server_init: no polling available";
v7 = 3LL;
if ( v5 != -1 )
{
faxpoll_server_file = a2;
v6 = "faxpoll_server_init: OK, waiting for poll";
v7 = 5LL;
}
return lprintf(v7, v6);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, const char *a2)
{
const char *v4; // rdi
int v5; // eax
const char *v6; // rsi
__int64 v7; // rdi

faxpoll_server_file = 0LL;
if ( access(a2, 4) )
return lprintf(1LL, "cannot access/read '%s'", a2);
v4 = "AT+FLP=1";
if ( modem_type != 5 )
v4 = "AT+FLPL=1";
v5 = mdm_command(v4, a1);
v6 = "faxpoll_server_init: no polling available";
v7 = 3LL;
if ( v5 != -1 )
{
faxpoll_server_file = a2;
v6 = "faxpoll_server_init: OK, waiting for poll";
v7 = 5LL;
}
return lprintf(v7, v6);
}

original_code:
void faxpoll_server_init (int fd,char * f)
{
    faxpoll_server_file = ((void *)0);
    if ( access( f, 4 ) != 0 )
    {
 lprintf( 1, "cannot access/read '%s'", f );
    }
    else if ( mdm_command( modem_type == Mt_class2_0? "AT+FLP=1":"AT+FLPL=1",
      fd ) == -1)
    {
 lprintf( 3, "faxpoll_server_init: no polling available" );
    }
    else
    {
 faxpoll_server_file = f;
 lprintf( 5, "faxpoll_server_init: OK, waiting for poll" );
    }
}

canonical_original_code:
void faxpoll_server_init(int fd, char *f)
{
    faxpoll_server_file = ((void *)0);
    if ( access( f, 4 ) != 0 )
    {
 lprintf( 1, "cannot access/read '%s'", f );
    }
    else if ( mdm_command( modem_type == Mt_class2_0? "AT+FLP=1":"AT+FLPL=1",
      fd ) == -1)
    {
 lprintf( 3, "faxpoll_server_init: no polling available" );
    }
    else
    {
 faxpoll_server_file = f;
 lprintf( 5, "faxpoll_server_init: OK, waiting for poll" );
    }
}

variable_types:
f
char *


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

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

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

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

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

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

variable_types:
devname
char *

variable_types: blocking char


decompiled_code:
__int64 __fastcall mg_init_device(int fd, char a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // er12
_BYTE v8[64]; // [rsp+0h] [rbp-A8h] BYREF
int v9[4]; // [rsp+40h] [rbp-68h] BYREF
int v10; // [rsp+50h] [rbp-58h]

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

canonical_decompiled_code:
__int64 __fastcall func4(int fd, char a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // er12
_BYTE v8[64]; // [rsp+0h] [rbp-A8h] BYREF
int v9[4]; // [rsp+40h] [rbp-68h] BYREF
int v10; // [rsp+50h] [rbp-58h]

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

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

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

variable_types:
portspeed
unsigned int

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

variable_types: off int

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


decompiled_code:
__int64 __fastcall mg_get_device(char *file, char a2, char a3, unsigned int a4, unsigned int a5)
{
int v5; // er15
unsigned int v9; // eax
unsigned int v10; // eax
__int64 v11; // rdx
unsigned int v12; // er8
unsigned int v14; // [rsp+Ch] [rbp-3Ch]
unsigned int v15; // [rsp+Ch] [rbp-3Ch]

v5 = 2;
delay(500LL);
v9 = mg_open_device(file, a2);
if ( v9 == -1 )
{
LABEL_10:
v14 = v9;
lprintf(0LL, "open device %s failed", file);
v12 = v14;
}
else
{
while ( 1 )
{
v10 = tio_get_rs232_lines(0LL);
v11 = v10;
if ( v10 != -1 )
{
if ( (v10 & 0x40) != 0 )
{
v15 = v10;
lprintf(4LL, "WARNING: DCD line still active, check modem settings (AT&Dx)", v10);
v11 = v15;
}
BYTE1(v11) &= 1u;
if ( !BYTE1(v11) )
lprintf(3LL, "WARNING: DSR is off - modem turned off or bad cable?", v11);
}
if ( (unsigned int)mg_init_device(0, a3, a4, a5) != -1 )
break;
if ( v5 == 1 )
{
lprintf(0LL, "mg_init_device failed, exiting");
return (unsigned int)-1;
}
v5 = 1;
lprintf(3LL, "mg_init_device failed, trying again");
v9 = mg_open_device(file, a2);
if ( v9 == -1 )
goto LABEL_10;
}
v12 = 0;
}
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *file, char a2, char a3, unsigned int a4, unsigned int a5)
{
int v5; // er15
unsigned int v9; // eax
unsigned int v10; // eax
__int64 v11; // rdx
unsigned int v12; // er8
unsigned int v14; // [rsp+Ch] [rbp-3Ch]
unsigned int v15; // [rsp+Ch] [rbp-3Ch]

v5 = 2;
delay(500LL);
v9 = func3(file, a2);
if ( v9 == -1 )
{
LABEL_10:
v14 = v9;
lprintf(0LL, "open device %s failed", file);
v12 = v14;
}
else
{
while ( 1 )
{
v10 = tio_get_rs232_lines(0LL);
v11 = v10;
if ( v10 != -1 )
{
if ( (v10 & 0x40) != 0 )
{
v15 = v10;
lprintf(4LL, "WARNING: DCD line still active, check modem settings (AT&Dx)", v10);
v11 = v15;
}
BYTE1(v11) &= 1u;
if ( !BYTE1(v11) )
lprintf(3LL, "WARNING: DSR is off - modem turned off or bad cable?", v11);
}
if ( (unsigned int)func4(0, a3, a4, a5) != -1 )
break;
if ( v5 == 1 )
{
lprintf(0LL, "mg_init_device failed, exiting");
return (unsigned int)-1;
}
v5 = 1;
lprintf(3LL, "mg_init_device failed, trying again");
v9 = func3(file, a2);
if ( v9 == -1 )
goto LABEL_10;
}
v12 = 0;
}
return v12;
}

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

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


decompiled_code:
__int64 __fastcall mg_get_ctty(int fd)
{
unsigned int v1; // er12

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

canonical_decompiled_code:
__int64 __fastcall func6(int fd)
{
unsigned int v1; // er12

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

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

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


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

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

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

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

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


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

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

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

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

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


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

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

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

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

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

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

variable_types: speed int

variable_types: i int


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

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

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

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

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

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

variable_types:
speed
unsigned int

variable_types: symspeed int


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

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

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

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

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

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

variable_types:
cbaud
unsigned int

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


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

result = 256LL;
*(_DWORD *)a1 &= 0xFFFFFC3F;
*(_DWORD *)(a1 + 12) &= 0xFFFFFFFC;
*(_DWORD *)(a1 + 4) = 0;
*(_WORD *)(a1 + 22) = 256;
return result;
}

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

result = 256LL;
*(_DWORD *)a1 &= 0xFFFFFC3F;
*(_DWORD *)(a1 + 12) &= 0xFFFFFFFC;
*(_DWORD *)(a1 + 4) = 0;
*(_WORD *)(a1 + 22) = 256;
return result;
}


variable_types: local int


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

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

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

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


variable_types: fd int

variable_types: type int


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

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

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

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

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

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


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

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

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

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

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

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

variable_types: mctl int


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

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

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

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

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

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

variable_types: queue int

variable_types: r int


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

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

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

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

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

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


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

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

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

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

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

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


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

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

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

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


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

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

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

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

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

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

variable_types: flags int


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

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

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

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

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

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

variable_types: rc int


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

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

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

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

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

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

variable_types: device int


decompiled_code:
__int64 __fastcall Digi_RAS_set_compression(__int64 *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 v5; // rcx
const char *v6; // rdi

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

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, __int64 *a2, _DWORD *a3)
{
__int64 v4; // rcx
__int64 v5; // rcx
const char *v6; // rdi

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

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

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

variable_types:
compression
long *

variable_types:
speed
long *

variable_types:
bits
int *


decompiled_code:
__int64 Digi_RAS_init()
{
__int64 v0; // rax
__int64 v1; // rdx
__int64 v2; // rax
__int64 v3; // rdx
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

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

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
__int64 v1; // rdx
__int64 v2; // rax
__int64 v3; // rdx
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

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

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

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

variable_types:
buffer
char [-1]


decompiled_code:
__int64 voice_open_device()
{
const char *v0; // rdx
const char *v1; // r12
const char *v2; // rbp
char *v3; // rax
const char *v4; // rsi
size_t v5; // rdx
size_t v6; // r14
char *v7; // rcx
char i; // al
unsigned int v9; // er14
char v11[296]; // [rsp+0h] [rbp-128h] BYREF

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

canonical_decompiled_code:
__int64 func0()
{
const char *v0; // rdx
const char *v1; // r12
const char *v2; // rbp
char *v3; // rax
const char *v4; // rsi
size_t v5; // rdx
size_t v6; // r14
char *v7; // rcx
char i; // al
unsigned int v9; // er14
char v11[296]; // [rsp+0h] [rbp-128h] BYREF

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

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

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

variable_types:
p
char *

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


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

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

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

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


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

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

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

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


decompiled_code:
__int64 __fastcall Sierra_beep(int a1, int a2)
{
int v2; // ebp
double v3; // xmm0_8

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

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
int v2; // ebp
double v3; // xmm0_8

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

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

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

variable_types: frequency int

variable_types: length int

variable_types: freq double

variable_types: time int

variable_types: i int

variable_types: d char


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

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

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