decompiled_code:
__int64 __fastcall pp_state_machine(__int64 a1, _BYTE *a2, unsigned int a3)
{
_BYTE *v3; // r13
unsigned int v4; // er12
int v5; // eax
int v6; // edx
char *v7; // rbx
__int64 result; // rax
int v9; // eax
unsigned int v10; // eax
int v11; // eax
const char *v12; // rsi

v3 = a2;
v4 = a3;
if ( a3 )
{
_pp_diag(
a1,
5LL,
1LL,
"RECV %02d bytes at %d.%09d (type %x, %s)\n",
a3,
*(unsigned int *)(a1 + 668),
*(unsigned int *)(a1 + 672),
*a2 & 0xF,
*((const char **)&pp_msg_names + (*a2 & 0xF)));
if ( (int)v4 <= 33 || (unsigned int)msg_unpack_header(a1, a2, v4) )
{
v4 = 0;
v3 = 0LL;
}
}
v5 = pp_state_table;
v6 = *(_DWORD *)a1;
if ( pp_state_table )
{
v7 = (char *)&pp_state_table;
while ( v6 != v5 )
{
v5 = *((_DWORD *)v7 + 6);
v7 += 24;
if ( !v5 )
goto LABEL_6;
}
v9 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(a1 + 4) = v6;
*(_DWORD *)(a1 + 8) = 0;
if ( v9 )
pp_diag_fsm(a1, *((const char **)v7 + 1), 0, v4);
v10 = (*((__int64 (__fastcall **)(__int64, _BYTE *, _QWORD))v7 + 2))(a1, v3, v4);
if ( v10 )
pp_printf("fsm for %s: Error %i in %s\n", *(const char **)(a1 + 1424), v10, *((const char **)v7 + 1));
v11 = *(_DWORD *)(a1 + 4);
v12 = (const char *)*((_QWORD *)v7 + 1);
if ( *(_DWORD *)a1 == v11 )
{
*(_DWORD *)(a1 + 12) = 0;
pp_diag_fsm(a1, v12, 1, 0LL);
result = *(unsigned int *)(a1 + 8);
}
else
{
*(_DWORD *)a1 = v11;
*(_DWORD *)(a1 + 12) = 1;
pp_diag_fsm(a1, v12, 2, 0LL);
result = 0LL;
}
}
else
{
LABEL_6:
pp_printf("fsm: Unknown state for iface %s\n", *(const char **)(a1 + 1424));
result = 10000LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _BYTE *a2, unsigned int a3)
{
_BYTE *v3; // r13
unsigned int v4; // er12
int v5; // eax
int v6; // edx
char *v7; // rbx
__int64 result; // rax
int v9; // eax
unsigned int v10; // eax
int v11; // eax
const char *v12; // rsi

v3 = a2;
v4 = a3;
if ( a3 )
{
_pp_diag(
a1,
5LL,
1LL,
"RECV %02d bytes at %d.%09d (type %x, %s)\n",
a3,
*(unsigned int *)(a1 + 668),
*(unsigned int *)(a1 + 672),
*a2 & 0xF,
*((const char **)&pp_msg_names + (*a2 & 0xF)));
if ( (int)v4 <= 33 || (unsigned int)msg_unpack_header(a1, a2, v4) )
{
v4 = 0;
v3 = 0LL;
}
}
v5 = pp_state_table;
v6 = *(_DWORD *)a1;
if ( pp_state_table )
{
v7 = (char *)&pp_state_table;
while ( v6 != v5 )
{
v5 = *((_DWORD *)v7 + 6);
v7 += 24;
if ( !v5 )
goto LABEL_6;
}
v9 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(a1 + 4) = v6;
*(_DWORD *)(a1 + 8) = 0;
if ( v9 )
func1(a1, *((const char **)v7 + 1), 0, v4);
v10 = (*((__int64 (__fastcall **)(__int64, _BYTE *, _QWORD))v7 + 2))(a1, v3, v4);
if ( v10 )
pp_printf("fsm for %s: Error %i in %s\n", *(const char **)(a1 + 1424), v10, *((const char **)v7 + 1));
v11 = *(_DWORD *)(a1 + 4);
v12 = (const char *)*((_QWORD *)v7 + 1);
if ( *(_DWORD *)a1 == v11 )
{
*(_DWORD *)(a1 + 12) = 0;
func1(a1, v12, 1, 0LL);
result = *(unsigned int *)(a1 + 8);
}
else
{
*(_DWORD *)a1 = v11;
*(_DWORD *)(a1 + 12) = 1;
func1(a1, v12, 2, 0LL);
result = 0LL;
}
}
else
{
LABEL_6:
pp_printf("fsm: Unknown state for iface %s\n", *(const char **)(a1 + 1424));
result = 10000LL;
}
return result;
}

original_code:
int pp_state_machine(struct pp_instance *ppi, uint8_t *packet, int plen)
{
 struct pp_state_table_item *ip;
 int state, err = 0;
 if (plen)
  ({ if (1) __pp_diag(ppi, pp_dt_frames, 1, "RECV %02d bytes at %d.%09d (type %x, %s)\n", plen, (int)ppi->last_rcv_time.seconds, (int)ppi->last_rcv_time.nanoseconds, packet[0] & 0xf, pp_msg_names[packet[0] & 0xf]); 1; });
 if (plen) {
  if (plen >= 34)
   err = msg_unpack_header(ppi, packet, plen);
  else
   err = 1;
  if (err) {
   plen = 0;
   packet = ((void *)0);
  }
 }
 state = ppi->state;
 for (ip = pp_state_table; ip->state != PPS_END_OF_TABLE; ip++) {
  if (ip->state != state)
   continue;
  ppi->next_state = state;
  ppi->next_delay = 0;
  if (ppi->is_new_state)
   pp_diag_fsm(ppi, ip->name, STATE_ENTER, plen);
  err = ip->f1(ppi, packet, plen);
  if (err)
   pp_printf("fsm for %s: Error %i in %s\n",
      ppi->iface_name, err, ip->name);
  if (ppi->state != ppi->next_state) {
   ppi->state = ppi->next_state;
   ppi->is_new_state = 1;
   pp_diag_fsm(ppi, ip->name, STATE_LEAVE, 0);
   return 0;
  }
  ppi->is_new_state = 0;
  pp_diag_fsm(ppi, ip->name, STATE_LOOP, 0);
  return ppi->next_delay;
 }
 pp_printf("fsm: Unknown state for iface %s\n", ppi->iface_name);
 return 10000;
}

canonical_original_code:
int pp_state_machine(struct pp_instance *ppi, unsigned char *packet, int plen)
{
 struct pp_state_table_item *ip;
 int state; int err = 0;
 if (plen)
  ({ if (1) __pp_diag(ppi, pp_dt_frames, 1, "RECV %02d bytes at %d.%09d (type %x, %s)\n", plen, (int)ppi->last_rcv_time.seconds, (int)ppi->last_rcv_time.nanoseconds, packet[0] & 0xf, pp_msg_names[packet[0] & 0xf]); 1; });
 if (plen) {
  if (plen >= 34)
   err = msg_unpack_header(ppi, packet, plen);
  else
   err = 1;
  if (err) {
   plen = 0;
   packet = ((void *)0);
  }
 }
 state = ppi->state;
 for (ip = pp_state_table; ip->state != PPS_END_OF_TABLE; ip++) {
  if (ip->state != state)
   continue;
  ppi->next_state = state;
  ppi->next_delay = 0;
  if (ppi->is_new_state)
   pp_diag_fsm(ppi, ip->name, STATE_ENTER, plen);
  err = ip->f1(ppi, packet, plen);
  if (err)
   pp_printf("fsm for %s: Error %i in %s\n",
      ppi->iface_name, err, ip->name);
  if (ppi->state != ppi->next_state) {
   ppi->state = ppi->next_state;
   ppi->is_new_state = 1;
   pp_diag_fsm(ppi, ip->name, STATE_LEAVE, 0);
   return 0;
  }
  ppi->is_new_state = 0;
  pp_diag_fsm(ppi, ip->name, STATE_LOOP, 0);
  return ppi->next_delay;
 }
 pp_printf("fsm: Unknown state for iface %s\n", ppi->iface_name);
 return 10000;
}

variable_types:
packet
unsigned char *

variable_types: struct pp_state_table_item { int state; char *name; int (*f1)(struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals { struct pp_instance *pp_instances; struct pp_servo { struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; } m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr { int nsec_prev; int y; int s_exp; } owd_fltr; struct pp_ofm_fltr { int nsec_prev; int y; } ofm_fltr; } *servo; struct pp_runtime_opts { struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; } clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; } *rt_opts; struct DSDefault { enum Boolean { FALSE=0, TRUE=1 } twoStepFlag; struct ClockIdentity { unsigned char id[8]; } clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean { FALSE=0, TRUE=1 } slaveOnly; } *defaultDS; struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; } *currentDS; struct DSParent { struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; } parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; } *parentDS; struct DSTimeProperties { short currentUtcOffset; enum Boolean { FALSE=0, TRUE=1 } currentUtcOffsetValid; enum Boolean { FALSE=0, TRUE=1 } leap59; enum Boolean { FALSE=0, TRUE=1 } leap61; enum Boolean { FALSE=0, TRUE=1 } timeTraceable; enum Boolean { FALSE=0, TRUE=1 } frequencyTraceable; enum Boolean { FALSE=0, TRUE=1 } ptpTimescale; unsigned char timeSource; } *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; } *links; void *arch_data; } *glbs; struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); } *n_ops; struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); } *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path { struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; } ch[-1]; int mcast_addr; int ptp_offset; } np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce { struct Timestamp { struct UInteger48 { unsigned int lsb; unsigned short msb; } secondsField; unsigned int nanosecondsField; } originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; } ann; struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 { unsigned int lsb; int msb; } correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; } hdr; } frgn_master[5]; struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; } *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; } *ppi, unsigned char *packet, int plen); } *

variable_types: state int

variable_types: err int

user_defined_types: struct pp_state_table_item { int state; char *name; int (*f1)(struct pp_instance *ppi, unsigned char *packet, int plen); }

user_defined_types: struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

user_defined_types: struct DSTimeProperties { short currentUtcOffset; enum Boolean currentUtcOffsetValid; enum Boolean leap59; enum Boolean leap61; enum Boolean timeTraceable; enum Boolean frequencyTraceable; enum Boolean ptpTimescale; unsigned char timeSource; }

user_defined_types: struct Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }


repo: tinito/ppsi-chibios

decompiled_code:
__int64 __fastcall pp_uncalibrated(__int64 a1, __int64 a2, int a3)
{
char v3; // al
__int64 result; // rax
int v5; // eax

if ( a3 )
{
v3 = *(_BYTE *)(a1 + 1345);
if ( v3 == 8 )
{
v5 = st_com_slave_handle_followup();
}
else if ( v3 == 11 )
{
v5 = st_com_slave_handle_announce();
}
else
{
if ( v3 )
goto LABEL_5;
v5 = st_com_slave_handle_sync();
}
if ( v5 )
goto LABEL_9;
}
LABEL_5:
if ( !(unsigned int)st_com_execute_slave(a1) )
{
*(_DWORD *)(a1 + 8) = 1000;
return 0LL;
}
LABEL_9:
*(_DWORD *)(a1 + 4) = 2;
result = 0LL;
*(_DWORD *)(a1 + 8) = 1000;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3)
{
char v3; // al
__int64 result; // rax
int v5; // eax

if ( a3 )
{
v3 = *(_BYTE *)(a1 + 1345);
if ( v3 == 8 )
{
v5 = st_com_slave_handle_followup();
}
else if ( v3 == 11 )
{
v5 = st_com_slave_handle_announce();
}
else
{
if ( v3 )
goto LABEL_5;
v5 = st_com_slave_handle_sync();
}
if ( v5 )
goto LABEL_9;
}
LABEL_5:
if ( !(unsigned int)st_com_execute_slave(a1) )
{
*(_DWORD *)(a1 + 8) = 1000;
return 0LL;
}
LABEL_9:
*(_DWORD *)(a1 + 4) = 2;
result = 0LL;
*(_DWORD *)(a1 + 8) = 1000;
return result;
}

original_code:
int pp_uncalibrated(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 int e = 0;
 if (plen == 0)
  goto no_incoming_msg;
 switch (ppi->received_ptp_header.messageType) {
 case PPM_ANNOUNCE:
  e = st_com_slave_handle_announce(ppi, pkt, plen);
  break;
 case PPM_SYNC:
  e = st_com_slave_handle_sync(ppi, pkt, plen);
  break;
 case PPM_FOLLOW_UP:
  e = st_com_slave_handle_followup(ppi, pkt, plen);
  break;
 default:
  break;
 }
no_incoming_msg:
 if (e == 0)
  e = st_com_execute_slave(ppi);
 if (e != 0)
  ppi->next_state = PPS_FAULTY;
 ppi->next_delay = 1000;
 return 0;
}

variable_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals { struct pp_instance *pp_instances; struct pp_servo { struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; } m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr { int nsec_prev; int y; int s_exp; } owd_fltr; struct pp_ofm_fltr { int nsec_prev; int y; } ofm_fltr; } *servo; struct pp_runtime_opts { struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; } clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; } *rt_opts; struct DSDefault { enum Boolean { FALSE=0, TRUE=1 } twoStepFlag; struct ClockIdentity { unsigned char id[8]; } clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean { FALSE=0, TRUE=1 } slaveOnly; } *defaultDS; struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; } *currentDS; struct DSParent { struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; } parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; } *parentDS; struct DSTimeProperties { short currentUtcOffset; enum Boolean { FALSE=0, TRUE=1 } currentUtcOffsetValid; enum Boolean { FALSE=0, TRUE=1 } leap59; enum Boolean { FALSE=0, TRUE=1 } leap61; enum Boolean { FALSE=0, TRUE=1 } timeTraceable; enum Boolean { FALSE=0, TRUE=1 } frequencyTraceable; enum Boolean { FALSE=0, TRUE=1 } ptpTimescale; unsigned char timeSource; } *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; } *links; void *arch_data; } *glbs; struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); } *n_ops; struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); } *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path { struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; } ch[-1]; int mcast_addr; int ptp_offset; } np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce { struct Timestamp { struct UInteger48 { unsigned int lsb; unsigned short msb; } secondsField; unsigned int nanosecondsField; } originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; } ann; struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 { unsigned int lsb; int msb; } correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; } hdr; } frgn_master[5]; struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; } *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; } *

variable_types:
pkt
unsigned char *

variable_types: plen int

variable_types: e int

return_type: int

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

user_defined_types: struct MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

user_defined_types: struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; }

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }


decompiled_code:
void __fastcall clear_TimeInternal(__int64 a1)
{
*(_DWORD *)(a1 + 16) = 0;
*(_OWORD *)a1 = 0LL;
}

original_code:
static inline void clear_TimeInternal(struct TimeInternal *t)
{
 memset(t, 0, sizeof(*t));
}

variable_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; } *

return_type: void


decompiled_code:
__int64 __fastcall pp_init_clock(__int64 a1)
{
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rdi
__int64 result; // rax

_pp_diag(a1, 4LL, 1LL, "Initializing\n");
v2 = SRV_isra_0(*(_QWORD *)(a1 + 40));
clear_TimeInternal(v2);
v3 = SRV_isra_0(*(_QWORD *)(a1 + 40));
clear_TimeInternal(v3 + 20);
v4 = *(_QWORD *)(a1 + 40);
*(_DWORD *)(SRV_isra_0(v4) + 80) = 0;
*(_DWORD *)(SRV_isra_0(v4) + 92) = 0;
result = OPTS_isra_0(v4);
if ( (*(_BYTE *)(result + 56) & 1) == 0 )
result = (*(__int64 (__fastcall **)(__int64, _QWORD, _QWORD))(*(_QWORD *)(a1 + 56) + 16LL))(a1, 0LL, 0LL);
return result;
}

original_code:
void pp_init_clock(struct pp_instance *ppi)
{
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "Initializing\n"); 1; });
 clear_TimeInternal(&SRV(ppi)->m_to_s_dly);
 clear_TimeInternal(&SRV(ppi)->s_to_m_dly);
 SRV(ppi)->obs_drift = 0;
 SRV(ppi)->owd_fltr.s_exp = 0;
 if (!OPTS(ppi)->no_adjust)
  ppi->t_ops->adjust(ppi, 0, 0);
}

user_defined_types: struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

user_defined_types: struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); }

user_defined_types: struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; }

user_defined_types: struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; }

user_defined_types: struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

user_defined_types: struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; }

user_defined_types: struct Integer64 { unsigned int lsb; int msb; }

user_defined_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }

user_defined_types: struct pp_owd_fltr { int nsec_prev; int y; int s_exp; }

user_defined_types: struct pp_ofm_fltr { int nsec_prev; int y; }

user_defined_types: struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; }

user_defined_types: struct ClockIdentity { unsigned char id[8]; }

user_defined_types: struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }

user_defined_types: struct UInteger48 { unsigned int lsb; unsigned short msb; }


original_code:
void pp_update_delay(struct pp_instance *ppi, TimeInternal *correction_field)
{
 TimeInternal s_to_m_dly;
 TimeInternal *mpd = &DSCUR(ppi)->meanPathDelay;
 struct pp_owd_fltr *owd_fltr = &SRV(ppi)->owd_fltr;
 int s;
 sub_TimeInternal(&s_to_m_dly, &ppi->t4, &ppi->t3);
 if (OPTS(ppi)->max_dly) {
  ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay " "greater than 1 second\n", __func__); 1; });
  if (s_to_m_dly.seconds)
   return;
  if (s_to_m_dly.nanoseconds > OPTS(ppi)->max_dly)
   ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay %d greater " "than administratively set maximum %d\n", __func__, (int)s_to_m_dly.nanoseconds, (int)OPTS(ppi)->max_dly); 1; });
  if (s_to_m_dly.nanoseconds > OPTS(ppi)->max_dly)
   return;
 }
 if (!OPTS(ppi)->ofst_first_updated)
  return;
 sub_TimeInternal(&SRV(ppi)->delay_sm, &ppi->t4, &ppi->t3);
 add_TimeInternal(mpd, &SRV(ppi)->delay_sm, &SRV(ppi)->delay_ms);
 sub_TimeInternal(mpd, mpd, correction_field);
 div2_TimeInternal(mpd);
 if (mpd->seconds) {
  owd_fltr->s_exp = 0;
  owd_fltr->nsec_prev = 0;
  return;
 }
 s = OPTS(ppi)->s;
 while (abs(owd_fltr->y) >> (31 - s))
  --s;
 if (owd_fltr->s_exp < 1)
  owd_fltr->s_exp = 1;
 else if (owd_fltr->s_exp < 1 << s)
  ++owd_fltr->s_exp;
 else if (owd_fltr->s_exp > 1 << s)
  owd_fltr->s_exp = 1 << s;
 mpd->nanoseconds = (mpd->nanoseconds + owd_fltr->nsec_prev) / 2;
 owd_fltr->nsec_prev = mpd->nanoseconds;
 owd_fltr->y = (owd_fltr->y * (owd_fltr->s_exp - 1) + mpd->nanoseconds)
  / owd_fltr->s_exp;
 mpd->nanoseconds = owd_fltr->y;
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "delay filter %d, %d\n", (int)owd_fltr->y, (int)owd_fltr->s_exp); 1; });
}

canonical_original_code:
void pp_update_delay(struct pp_instance *ppi, struct TimeInternal *correction_field)
{
 struct TimeInternal s_to_m_dly;
 struct TimeInternal *mpd = &DSCUR(ppi)->meanPathDelay;
 struct pp_owd_fltr *owd_fltr = &SRV(ppi)->owd_fltr;
 int s;
 sub_TimeInternal(&s_to_m_dly, &ppi->t4, &ppi->t3);
 if (OPTS(ppi)->max_dly) {
  ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay " "greater than 1 second\n", __func__); 1; });
  if (s_to_m_dly.seconds)
   return;
  if (s_to_m_dly.nanoseconds > OPTS(ppi)->max_dly)
   ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay %d greater " "than administratively set maximum %d\n", __func__, (int)s_to_m_dly.nanoseconds, (int)OPTS(ppi)->max_dly); 1; });
  if (s_to_m_dly.nanoseconds > OPTS(ppi)->max_dly)
   return;
 }
 if (!OPTS(ppi)->ofst_first_updated)
  return;
 sub_TimeInternal(&SRV(ppi)->delay_sm, &ppi->t4, &ppi->t3);
 add_TimeInternal(mpd, &SRV(ppi)->delay_sm, &SRV(ppi)->delay_ms);
 sub_TimeInternal(mpd, mpd, correction_field);
 div2_TimeInternal(mpd);
 if (mpd->seconds) {
  owd_fltr->s_exp = 0;
  owd_fltr->nsec_prev = 0;
  return;
 }
 s = OPTS(ppi)->s;
 while (abs(owd_fltr->y) >> (31 - s))
  --s;
 if (owd_fltr->s_exp < 1)
  owd_fltr->s_exp = 1;
 else if (owd_fltr->s_exp < 1 << s)
  ++owd_fltr->s_exp;
 else if (owd_fltr->s_exp > 1 << s)
  owd_fltr->s_exp = 1 << s;
 mpd->nanoseconds = (mpd->nanoseconds + owd_fltr->nsec_prev) / 2;
 owd_fltr->nsec_prev = mpd->nanoseconds;
 owd_fltr->y = (owd_fltr->y * (owd_fltr->s_exp - 1) + mpd->nanoseconds)
  / owd_fltr->s_exp;
 mpd->nanoseconds = owd_fltr->y;
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "delay filter %d, %d\n", (int)owd_fltr->y, (int)owd_fltr->s_exp); 1; });
}

variable_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }

variable_types: struct pp_owd_fltr { int nsec_prev; int y; int s_exp; } *

variable_types: s int

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

user_defined_types: struct MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

user_defined_types: struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; }

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

user_defined_types: struct DSTimeProperties { short currentUtcOffset; enum Boolean currentUtcOffsetValid; enum Boolean leap59; enum Boolean leap61; enum Boolean timeTraceable; enum Boolean frequencyTraceable; enum Boolean ptpTimescale; unsigned char timeSource; }

user_defined_types: struct Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }


original_code:
void pp_update_offset(struct pp_instance *ppi, TimeInternal *correction_field)
{
 TimeInternal m_to_s_dly;
 struct pp_ofm_fltr *ofm_fltr = &SRV(ppi)->ofm_fltr;
 sub_TimeInternal(&m_to_s_dly, &ppi->t2, &ppi->t1);
 if (OPTS(ppi)->max_dly) {
  if (m_to_s_dly.seconds) {
   ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay greater " "than 1 second\n", __func__); 1; });
   return;
  }
  if (m_to_s_dly.nanoseconds > OPTS(ppi)->max_dly) {
   ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay %d greater " "than administratively set maximum %d\n", __func__, (int)m_to_s_dly.nanoseconds, (int)OPTS(ppi)->max_dly); 1; });
   return;
  }
 }
 SRV(ppi)->m_to_s_dly = m_to_s_dly;
 sub_TimeInternal(&SRV(ppi)->delay_ms, &ppi->t2, &ppi->t1);
 sub_TimeInternal(&SRV(ppi)->m_to_s_dly,
  &SRV(ppi)->m_to_s_dly, correction_field);
 sub_TimeInternal(&DSCUR(ppi)->offsetFromMaster,
   &SRV(ppi)->m_to_s_dly,
   &DSCUR(ppi)->meanPathDelay);
 if (DSCUR(ppi)->offsetFromMaster.seconds) {
  ofm_fltr->nsec_prev = 0;
  return;
 }
 ofm_fltr->y = DSCUR(ppi)->offsetFromMaster.nanoseconds / 2 +
  ofm_fltr->nsec_prev / 2;
 ofm_fltr->nsec_prev = DSCUR(ppi)->offsetFromMaster.nanoseconds;
 DSCUR(ppi)->offsetFromMaster.nanoseconds = ofm_fltr->y;
 if (!OPTS(ppi)->ofst_first_updated)
  OPTS(ppi)->ofst_first_updated = 1;
}

canonical_original_code:
void pp_update_offset(struct pp_instance *ppi, struct TimeInternal *correction_field)
{
 struct TimeInternal m_to_s_dly;
 struct pp_ofm_fltr *ofm_fltr = &SRV(ppi)->ofm_fltr;
 sub_TimeInternal(&m_to_s_dly, &ppi->t2, &ppi->t1);
 if (OPTS(ppi)->max_dly) {
  if (m_to_s_dly.seconds) {
   ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay greater " "than 1 second\n", __func__); 1; });
   return;
  }
  if (m_to_s_dly.nanoseconds > OPTS(ppi)->max_dly) {
   ({ if (1) __pp_diag(ppi, pp_dt_servo, 1, "%s aborted, delay %d greater " "than administratively set maximum %d\n", __func__, (int)m_to_s_dly.nanoseconds, (int)OPTS(ppi)->max_dly); 1; });
   return;
  }
 }
 SRV(ppi)->m_to_s_dly = m_to_s_dly;
 sub_TimeInternal(&SRV(ppi)->delay_ms, &ppi->t2, &ppi->t1);
 sub_TimeInternal(&SRV(ppi)->m_to_s_dly,
  &SRV(ppi)->m_to_s_dly, correction_field);
 sub_TimeInternal(&DSCUR(ppi)->offsetFromMaster,
   &SRV(ppi)->m_to_s_dly,
   &DSCUR(ppi)->meanPathDelay);
 if (DSCUR(ppi)->offsetFromMaster.seconds) {
  ofm_fltr->nsec_prev = 0;
  return;
 }
 ofm_fltr->y = DSCUR(ppi)->offsetFromMaster.nanoseconds / 2 +
  ofm_fltr->nsec_prev / 2;
 ofm_fltr->nsec_prev = DSCUR(ppi)->offsetFromMaster.nanoseconds;
 DSCUR(ppi)->offsetFromMaster.nanoseconds = ofm_fltr->y;
 if (!OPTS(ppi)->ofst_first_updated)
  OPTS(ppi)->ofst_first_updated = 1;
}

variable_types: struct pp_ofm_fltr { int nsec_prev; int y; } *

user_defined_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }

user_defined_types: struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

user_defined_types: struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); }

user_defined_types: struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; }

user_defined_types: struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }

user_defined_types: struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

user_defined_types: struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; }

user_defined_types: struct Integer64 { unsigned int lsb; int msb; }


decompiled_code:
__int64 __fastcall pp_update_clock(__int64 a1)
{
unsigned int *v1; // rax
__int64 v2; // rdx
const char *v3; // rcx
__int64 v4; // rax
__int64 v5; // rdx
const char *v6; // rcx
__int64 v7; // rax
__int64 v8; // rdx
const char *v9; // rcx
__int64 v10; // rax
__int64 v11; // rdx
const char *v12; // rcx

_pp_update_clock(a1);
v1 = (unsigned int *)SRV_isra_0(*(_QWORD *)(a1 + 40));
_pp_diag(a1, 4LL, v2, v3, *v1, v1[1]);
v4 = DSCUR_isra_0(*(_QWORD *)(a1 + 40));
_pp_diag(a1, 4LL, v5, v6, *(unsigned int *)(v4 + 24), *(unsigned int *)(v4 + 28));
v7 = DSCUR_isra_0(*(_QWORD *)(a1 + 40));
_pp_diag(a1, 4LL, v8, v9, *(unsigned int *)(v7 + 4), *(unsigned int *)(v7 + 8));
v10 = SRV_isra_0(*(_QWORD *)(a1 + 40));
return _pp_diag(a1, 4LL, v11, v12, *(unsigned int *)(v10 + 80));
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
unsigned int *v1; // rax
__int64 v2; // rdx
const char *v3; // rcx
__int64 v4; // rax
__int64 v5; // rdx
const char *v6; // rcx
__int64 v7; // rax
__int64 v8; // rdx
const char *v9; // rcx
__int64 v10; // rax
__int64 v11; // rdx
const char *v12; // rcx

func7(a1);
v1 = (unsigned int *)func5(*(_QWORD *)(a1 + 40));
_pp_diag(a1, 4LL, v2, v3, *v1, v1[1]);
v4 = func4(*(_QWORD *)(a1 + 40));
_pp_diag(a1, 4LL, v5, v6, *(unsigned int *)(v4 + 24), *(unsigned int *)(v4 + 28));
v7 = func4(*(_QWORD *)(a1 + 40));
_pp_diag(a1, 4LL, v8, v9, *(unsigned int *)(v7 + 4), *(unsigned int *)(v7 + 8));
v10 = func5(*(_QWORD *)(a1 + 40));
return _pp_diag(a1, 4LL, v11, v12, *(unsigned int *)(v10 + 80));
}

original_code:
void pp_update_clock(struct pp_instance *ppi)
{
 __pp_update_clock(ppi);
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 2, "Raw offset from master: %9i.%09i\n", (int)SRV(ppi)->m_to_s_dly.seconds, (int)SRV(ppi)->m_to_s_dly.nanoseconds); 1; });
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 2, "One-way delay averaged: %9i.%09i\n", (int)DSCUR(ppi)->meanPathDelay.seconds, (int)DSCUR(ppi)->meanPathDelay.nanoseconds); 1; });
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 2, "Offset from master:     %9i.%09i\n", (int)DSCUR(ppi)->offsetFromMaster.seconds, (int)DSCUR(ppi)->offsetFromMaster.nanoseconds); 1; });
 ({ if (1) __pp_diag(ppi, pp_dt_servo, 2, "Observed drift: %9i\n", (int)SRV(ppi)->obs_drift); 1; });
}

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

user_defined_types: struct pp_owd_fltr { int nsec_prev; int y; int s_exp; }

user_defined_types: struct pp_ofm_fltr { int nsec_prev; int y; }

user_defined_types: struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; }

user_defined_types: struct ClockIdentity { unsigned char id[8]; }

user_defined_types: struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }

user_defined_types: struct UInteger48 { unsigned int lsb; unsigned short msb; }


original_code:
static void normalize_TimeInternal(TimeInternal *r)
{
 r->seconds += r->nanoseconds / (1000*1000*1000);
 r->nanoseconds -= r->nanoseconds / (1000*1000*1000) * (1000*1000*1000);
 if (r->seconds > 0 && r->nanoseconds < 0) {
  r->seconds -= 1;
  r->nanoseconds += (1000*1000*1000);
 } else if (r->seconds < 0 && r->nanoseconds > 0) {
  r->seconds += 1;
  r->nanoseconds -= (1000*1000*1000);
 }
}

canonical_original_code:
static void normalize_TimeInternal(struct TimeInternal *r)
{
 r->seconds += r->nanoseconds / (1000*1000*1000);
 r->nanoseconds -= r->nanoseconds / (1000*1000*1000) * (1000*1000*1000);
 if (r->seconds > 0 && r->nanoseconds < 0) {
  r->seconds -= 1;
  r->nanoseconds += (1000*1000*1000);
 } else if (r->seconds < 0 && r->nanoseconds > 0) {
  r->seconds += 1;
  r->nanoseconds -= (1000*1000*1000);
 }
}

user_defined_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }


decompiled_code:
__int64 __fastcall from_TimeInternal(_DWORD *a1, __int64 a2)
{
int v2; // edx
unsigned int v3; // er12

if ( (int)*a1 < 0 || (v2 = a1[1], v2 < 0) )
{
v3 = -1;
if ( pp_diag_verbosity )
pp_printf("ERROR: Negative value cannot be converted into timestamp\n");
}
else
{
*(_DWORD *)a2 = *a1;
v3 = 0;
*(_DWORD *)(a2 + 8) = v2;
*(_WORD *)(a2 + 4) = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, __int64 a2)
{
int v2; // edx
unsigned int v3; // er12

if ( (int)*a1 < 0 || (v2 = a1[1], v2 < 0) )
{
v3 = -1;
if ( pp_diag_verbosity )
pp_printf("ERROR: Negative value cannot be converted into timestamp\n");
}
else
{
*(_DWORD *)a2 = *a1;
v3 = 0;
*(_DWORD *)(a2 + 8) = v2;
*(_WORD *)(a2 + 4) = 0;
}
return v3;
}

original_code:
int from_TimeInternal(TimeInternal *internal, Timestamp *external)
{
 if ((internal->seconds & ~0x7fffffff) ||
     (internal->nanoseconds & ~0x7fffffff)) {
  if (pp_diag_verbosity) pp_printf("ERROR: " "Negative value cannot be converted into " "timestamp\n");
  return -1;
 } else {
  external->secondsField.lsb = internal->seconds;
  external->nanosecondsField = internal->nanoseconds;
  external->secondsField.msb = 0;
 }
 return 0;
}

canonical_original_code:
int from_TimeInternal(struct TimeInternal *internal, struct Timestamp *external)
{
 if ((internal->seconds & ~0x7fffffff) ||
     (internal->nanoseconds & ~0x7fffffff)) {
  if (pp_diag_verbosity) pp_printf("ERROR: " "Negative value cannot be converted into " "timestamp\n");
  return -1;
 } else {
  external->secondsField.lsb = internal->seconds;
  external->nanosecondsField = internal->nanoseconds;
  external->secondsField.msb = 0;
 }
 return 0;
}

variable_types: struct Timestamp { struct UInteger48 { unsigned int lsb; unsigned short msb; } secondsField; unsigned int nanosecondsField; } *

user_defined_types: struct Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }


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

if ( *a2 > 0x7FFFFFFEu )
{
v2 = -1;
if ( pp_diag_verbosity )
pp_printf("ERROR: to_TimeInternal: seconds field is higher than signed integer (32bits)\n");
}
else
{
*a1 = *a2;
v2 = 0;
a1[1] = a2[2];
}
return v2;
}

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

if ( *a2 > 0x7FFFFFFEu )
{
v2 = -1;
if ( pp_diag_verbosity )
pp_printf("ERROR: to_TimeInternal: seconds field is higher than signed integer (32bits)\n");
}
else
{
*a1 = *a2;
v2 = 0;
a1[1] = a2[2];
}
return v2;
}

original_code:
int to_TimeInternal(TimeInternal *internal, Timestamp *external)
{
 if (external->secondsField.lsb < 0x7fffffff) {
  internal->seconds = external->secondsField.lsb;
  internal->nanoseconds = external->nanosecondsField;
  return 0;
 } else {
  if (pp_diag_verbosity) pp_printf("ERROR: " "to_TimeInternal: " "seconds field is higher than signed integer (32bits)\n");
  return -1;
 }
}

canonical_original_code:
int to_TimeInternal(struct TimeInternal *internal, struct Timestamp *external)
{
 if (external->secondsField.lsb < 0x7fffffff) {
  internal->seconds = external->secondsField.lsb;
  internal->nanoseconds = external->nanosecondsField;
  return 0;
 } else {
  if (pp_diag_verbosity) pp_printf("ERROR: " "to_TimeInternal: " "seconds field is higher than signed integer (32bits)\n");
  return -1;
 }
}


decompiled_code:
__int64 __fastcall add_TimeInternal(int *a1, _DWORD *a2, _DWORD *a3)
{
*a1 = *a2 + *a3;
a1[1] = a2[1] + a3[1];
return normalize_TimeInternal(a1);
}

original_code:
void add_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y)
{
 r->seconds = x->seconds + y->seconds;
 r->nanoseconds = x->nanoseconds + y->nanoseconds;
 normalize_TimeInternal(r);
}

canonical_original_code:
void add_TimeInternal(struct TimeInternal *r, struct TimeInternal *x, struct TimeInternal *y)
{
 r->seconds = x->seconds + y->seconds;
 r->nanoseconds = x->nanoseconds + y->nanoseconds;
 normalize_TimeInternal(r);
}

variable_types:
x
struct TimeInternal *

variable_types:
y
struct TimeInternal *


decompiled_code:
__int64 __fastcall sub_TimeInternal(int *a1, _DWORD *a2, _DWORD *a3)
{
*a1 = *a2 - *a3;
a1[1] = a2[1] - a3[1];
return normalize_TimeInternal(a1);
}

original_code:
void sub_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y)
{
 r->seconds = x->seconds - y->seconds;
 r->nanoseconds = x->nanoseconds - y->nanoseconds;
 normalize_TimeInternal(r);
}

canonical_original_code:
void sub_TimeInternal(struct TimeInternal *r, struct TimeInternal *x, struct TimeInternal *y)
{
 r->seconds = x->seconds - y->seconds;
 r->nanoseconds = x->nanoseconds - y->nanoseconds;
 normalize_TimeInternal(r);
}


decompiled_code:
__int64 __fastcall div2_TimeInternal(int *a1)
{
int v1; // eax

v1 = a1[1] + 1000000000 * (*a1 % 2);
*a1 /= 2;
a1[1] = v1 / 2;
return normalize_TimeInternal(a1);
}

original_code:
void div2_TimeInternal(TimeInternal *r)
{
 r->nanoseconds += r->seconds % 2 * 1000000000;
 r->seconds /= 2;
 r->nanoseconds /= 2;
 normalize_TimeInternal(r);
}

canonical_original_code:
void div2_TimeInternal(struct TimeInternal *r)
{
 r->nanoseconds += r->seconds % 2 * 1000000000;
 r->seconds /= 2;
 r->nanoseconds /= 2;
 normalize_TimeInternal(r);
}


decompiled_code:
void __fastcall display_TimeInternal(const char *a1, int *a2)
{
const char *v2; // r9
__int64 v3; // r8
__int64 v4; // rcx

if ( pp_diag_verbosity > 1 )
{
v2 = "-";
v3 = abs32(a2[1]);
v4 = abs32(*a2);
if ( *a2 >= 0 && (a2[1] >= 0 || *a2) )
v2 = " ";
pp_printf("%s: %s%d.%09d\n", a1, v2, v4, v3);
}
}

original_code:
void display_TimeInternal(const char *label, TimeInternal *t)
{
 if (pp_diag_verbosity > 1) pp_printf("%s: %s%d.%09d\n", label, (t->seconds < 0 || (t->seconds == 0 && t->nanoseconds < 0)) ? "-" : " ", (int)abs(t->seconds), (int)abs(t->nanoseconds));
}

canonical_original_code:
void display_TimeInternal(char *label, struct TimeInternal *t)
{
 if (pp_diag_verbosity > 1) pp_printf("%s: %s%d.%09d\n", label, (t->seconds < 0 || (t->seconds == 0 && t->nanoseconds < 0)) ? "-" : " ", (int)abs(t->seconds), (int)abs(t->nanoseconds));
}

variable_types:
label
char *


decompiled_code:
__int64 __fastcall str_to_tv(char *s, _QWORD *a2)
{
char *v2; // rbp
int v3; // er12
char *v4; // rax
const char *v5; // rbp
int v6; // edx
int v7; // ecx
int v8; // eax

v2 = s;
if ( strlen(s) > 0x20 )
return 0xFFFFFFFFLL;
v3 = 1;
if ( *s == 45 )
{
v2 = s + 1;
v3 = -1;
}
if ( (int)__isoc99_sscanf() > 2 )
return 0xFFFFFFFFLL;
v4 = strchr(v2, 46);
if ( v4 )
{
v5 = v4 + 1;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
v6 = strlen(v5);
if ( v6 <= 6 )
{
v7 = 0;
v8 = 0;
if ( v6 != 6 )
{
do
{
++v6;
v8 *= 10;
}
while ( v6 != 6 );
v7 = v8;
}
goto LABEL_11;
}
}
return 0xFFFFFFFFLL;
}
v7 = 0;
LABEL_11:
a2[1] = v7;
*a2 = 0LL;
if ( !v7 || v3 != -1 )
return 0LL;
*a2 = -1LL;
a2[1] = 1000000 - v7;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s, _QWORD *a2)
{
char *v2; // rbp
int v3; // er12
char *v4; // rax
const char *v5; // rbp
int v6; // edx
int v7; // ecx
int v8; // eax

v2 = s;
if ( strlen(s) > 0x20 )
return 0xFFFFFFFFLL;
v3 = 1;
if ( *s == 45 )
{
v2 = s + 1;
v3 = -1;
}
if ( (int)__isoc99_sscanf() > 2 )
return 0xFFFFFFFFLL;
v4 = strchr(v2, 46);
if ( v4 )
{
v5 = v4 + 1;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
v6 = strlen(v5);
if ( v6 <= 6 )
{
v7 = 0;
v8 = 0;
if ( v6 != 6 )
{
do
{
++v6;
v8 *= 10;
}
while ( v6 != 6 );
v7 = v8;
}
goto LABEL_11;
}
}
return 0xFFFFFFFFLL;
}
v7 = 0;
LABEL_11:
a2[1] = v7;
*a2 = 0LL;
if ( !v7 || v3 != -1 )
return 0LL;
*a2 = -1LL;
a2[1] = 1000000 - v7;
return 0LL;
}

original_code:
static inline int str_to_tv(char *s, struct timeval *tv)
{
 int i, sign = 1, micro = 0, sec = 0;
 char *smicro = ((void *)0);
 char c;
 if (strlen(s) > 32)
  return -1;
 if (s[0] == '-') {
  sign = -1;
  s++;
 }
 if (sscanf(s, "%d.%d%c", &sec, &micro, &c) > 2)
  return -1;
 smicro = strchr(s, '.');
 if (smicro) {
  smicro++;
  if (sscanf(smicro, "%d%c", &micro, &c) != 1)
   return -1;
 }
 if (smicro) {
  i = strlen(smicro);
  if (i > 6)
   return -1;
  while (i < 6) {
   micro *= 10;
   i++;
  }
 }
 tv->tv_sec = sec * sign;
 tv->tv_usec = micro;
 if (sign < 0 && micro) {
  tv->tv_sec--;
  tv->tv_usec = 1000 * 1000 - micro;
 }
 return 0;
}

canonical_original_code:
static inline int str_to_tv(char *s, struct timeval *tv)
{
 int i; int sign = 1; int micro = 0; int sec = 0;
 char *smicro = ((void *)0);
 char c;
 if (strlen(s) > 32)
  return -1;
 if (s[0] == '-') {
  sign = -1;
  s++;
 }
 if (sscanf(s, "%d.%d%c", &sec, &micro, &c) > 2)
  return -1;
 smicro = strchr(s, '.');
 if (smicro) {
  smicro++;
  if (sscanf(smicro, "%d%c", &micro, &c) != 1)
   return -1;
 }
 if (smicro) {
  i = strlen(smicro);
  if (i > 6)
   return -1;
  while (i < 6) {
   micro *= 10;
   i++;
  }
 }
 tv->tv_sec = sec * sign;
 tv->tv_usec = micro;
 if (sign < 0 && micro) {
  tv->tv_sec--;
  tv->tv_usec = 1000 * 1000 - micro;
 }
 return 0;
}

variable_types:
s
char *

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

variable_types: i int

variable_types: sign int

variable_types: micro int

variable_types: sec int

variable_types:
smicro
char *

variable_types: c char


decompiled_code:
int __fastcall cmd_line_parse_two(char *nptr, int *a2, int *a3)
{
char v5; // dl
__int64 v6; // rax
__int64 v7; // rbp
int v8; // eax
__int64 v9; // rdi
char *v10; // rbp
int result; // eax

*a3 = 0;
*a2 = 0;
v5 = *nptr;
if ( *nptr )
{
v6 = 0LL;
while ( 1 )
{
v7 = (int)v6;
if ( v5 == 44 )
break;
v5 = nptr[++v6];
if ( !v5 )
goto LABEL_6;
}
nptr[v6] = 0;
v8 = atoi(nptr);
v9 = v7 + 1;
v10 = &nptr[v7];
*a2 = v8;
}
else
{
LABEL_6:
v10 = nptr;
v9 = 1LL;
}
result = atoi(&nptr[v9]);
*a3 = result;
*v10 = 44;
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *nptr, int *a2, int *a3)
{
char v5; // dl
__int64 v6; // rax
__int64 v7; // rbp
int v8; // eax
__int64 v9; // rdi
char *v10; // rbp
int result; // eax

*a3 = 0;
*a2 = 0;
v5 = *nptr;
if ( *nptr )
{
v6 = 0LL;
while ( 1 )
{
v7 = (int)v6;
if ( v5 == 44 )
break;
v5 = nptr[++v6];
if ( !v5 )
goto LABEL_6;
}
nptr[v6] = 0;
v8 = atoi(nptr);
v9 = v7 + 1;
v10 = &nptr[v7];
*a2 = v8;
}
else
{
LABEL_6:
v10 = nptr;
v9 = 1LL;
}
result = atoi(&nptr[v9]);
*a3 = result;
*v10 = 44;
return result;
}

original_code:
static void cmd_line_parse_two(char *a, int *n1, int *n2)
{
 int i, comma = 0;
 *n1 = *n2 = 0;
 for (i = 0; a[i] != '\0'; i++) {
  if (a[i] == ',') {
   comma = i;
   a[i] = '\0';
   *n1 = atoi(a);
   break;
  }
 }
 *n2 = atoi(&a[comma+1]);
 a[comma] = ',';
}

canonical_original_code:
static void cmd_line_parse_two(char *a, int *n1, int *n2)
{
 int i; int comma = 0;
 *n1 = *n2 = 0;
 for (i = 0; a[i] != '\0'; i++) {
  if (a[i] == ',') {
   comma = i;
   a[i] = '\0';
   *n1 = atoi(a);
   break;
  }
 }
 *n2 = atoi(&a[comma+1]);
 a[comma] = ',';
}

variable_types:
a
char *

variable_types: comma int


decompiled_code:
__int64 cmd_line_print_help()
{
char **v0; // rbx
const char *v1; // rdx
const char *v2; // rsi
__int64 result; // rax

v0 = off_BF0;
pp_printf("\nUsage: ppsi [OPTION]\n\n");
v1 = "show this page";
v2 = "-?";
while ( 1 )
{
v0 += 2;
result = pp_printf("%s\n\t%s\n", v2, v1);
v2 = *(v0 - 2);
if ( !v2 )
break;
v1 = *(v0 - 1);
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
char **v0; // rbx
const char *v1; // rdx
const char *v2; // rsi
__int64 result; // rax

v0 = off_BF0;
pp_printf("\nUsage: ppsi [OPTION]\n\n");
v1 = "show this page";
v2 = "-?";
while ( 1 )
{
v0 += 2;
result = pp_printf("%s\n\t%s\n", v2, v1);
v2 = *(v0 - 2);
if ( !v2 )
break;
v1 = *(v0 - 1);
}
return result;
}

original_code:
static void cmd_line_print_help(void)
{
 int i = 0;
 pp_printf("\nUsage: ppsi [OPTION]\n\n");
 while (1) {
  struct cmd_line_opt *o = &cmd_line_list[i];
  if (o->flag == ((void *)0))
   break;
  pp_printf("%s\n\t%s\n", o->flag, o->help);
  i++;
 }
}

canonical_original_code:
static void cmd_line_print_help()
{
 int i = 0;
 pp_printf("\nUsage: ppsi [OPTION]\n\n");
 while (1) {
  struct cmd_line_opt *o = &cmd_line_list[i];
  if (o->flag == ((void *)0))
   break;
  pp_printf("%s\n\t%s\n", o->flag, o->help);
  i++;
 }
}

variable_types: struct cmd_line_opt { char *flag; char *help; } *


original_code:
int pp_parse_cmdline(struct pp_globals *ppg, int argc, char **argv)
{
 int i, err = 0;
 int j;
 char *a;
 int n1, n2;
 for (i = 1; i < argc; i++) {
  a = argv[i];
  if (a[0] != '-')
   err = 1;
  else if (a[1] == '?')
   err = 1;
  else if (a[1] && a[2])
   err = 1;
  if (err) {
   cmd_line_print_help();
   return -1;
  }
  if (1) {
   switch (a[1]) {
   case 'V':
    pp_diag_verbosity++;
    break;
   case 'd':
    a = argv[++i];
    pp_global_flags = pp_diag_parse(a);
    break;
   case 'x':
    GOPTS(ppg)->no_rst_clk = 1;
    break;
   case 'O':
    a = argv[++i];
    GOPTS(ppg)->max_rst = atoi(a);
    if (GOPTS(ppg)->max_rst < (1000*1000*1000)) {
     pp_printf("Use -x to prevent jumps of"
     " more than one second\n");
     return -1;
    }
    break;
   case 'M':
    a = argv[++i];
    GOPTS(ppg)->max_dly = atoi(a);
    if (GOPTS(ppg)->max_dly < (1000*1000*1000)) {
     pp_printf("Use -x to prevent jumps of"
      " more than one second\n");
     return -1;
    }
    break;
   case 't':
    GOPTS(ppg)->no_adjust = 1;
    break;
   case 'a':
    a = argv[++i];
    cmd_line_parse_two(a, &n1, &n2);
    if (n1 < 1 || n2 < 1)
     return -1;
    GOPTS(ppg)->ap = n1;
    GOPTS(ppg)->ai = n2;
    break;
   case 'w':
    a = argv[++i];
    GOPTS(ppg)->s = atoi(a);
    break;
   case 'b':
    a = argv[++i];
    if (ppg->nlinks == 1)
     ppg->pp_instances[0].iface_name = a;
    else {
     pp_printf("Can not use -b option in multi-link conf");
     return -1;
    }
    break;
   case 'l':
    a = argv[++i];
    cmd_line_parse_two(a, &n1, &n2);
    GOPTS(ppg)->inbound_latency.nanoseconds = n1;
    GOPTS(ppg)->outbound_latency.nanoseconds = n2;
    break;
   case 'i':
    a = argv[++i];
    GOPTS(ppg)->domain_number = atoi(a);
    break;
   case 'y':
    a = argv[++i];
    GOPTS(ppg)->sync_intvl = atoi(a);
    break;
   case 'n':
    a = argv[++i];
    GOPTS(ppg)->announce_intvl = atoi(a);
    if (GOPTS(ppg)->announce_intvl < 0)
     GOPTS(ppg)->announce_intvl = 0;
    if (GOPTS(ppg)->announce_intvl > 4)
     GOPTS(ppg)->announce_intvl = 4;
    break;
   case 'g':
    for (j = 0; j < ppg->nlinks; j++)
     ppg->pp_instances[j].slave_only = 1;
    break;
   case 'v':
    a = argv[++i];
    GOPTS(ppg)->clock_quality.
     offsetScaledLogVariance = atoi(a);
    break;
   case 'r':
    a = argv[++i];
    GOPTS(ppg)->clock_quality.clockAccuracy =
     atoi(a);
    break;
   case 's':
    a = argv[++i];
    GOPTS(ppg)->clock_quality.clockClass =
     atoi(a);
    break;
   case 'p':
    a = argv[++i];
    GOPTS(ppg)->prio1 = atoi(a);
    break;
   case 'q':
    a = argv[++i];
    GOPTS(ppg)->prio2 = atoi(a);
    break;
   case 'h':
    break;
   case 'e':
    for (j = 0; j < ppg->nlinks; j++)
     ppg->pp_instances[j].ethernet_mode = 1;
    break;
   case 'G':
   default:
    cmd_line_print_help();
    return -1;
   }
  }
  }
 return 0;
}

canonical_original_code:
int pp_parse_cmdline(struct pp_globals *ppg, int argc, char **argv)
{
 int i; int err = 0;
 int j;
 char *a;
 int n1; int n2;
 for (i = 1; i < argc; i++) {
  a = argv[i];
  if (a[0] != '-')
   err = 1;
  else if (a[1] == '?')
   err = 1;
  else if (a[1] && a[2])
   err = 1;
  if (err) {
   cmd_line_print_help();
   return -1;
  }
  if (1) {
   switch (a[1]) {
   case 'V':
    pp_diag_verbosity++;
    break;
   case 'd':
    a = argv[++i];
    pp_global_flags = pp_diag_parse(a);
    break;
   case 'x':
    GOPTS(ppg)->no_rst_clk = 1;
    break;
   case 'O':
    a = argv[++i];
    GOPTS(ppg)->max_rst = atoi(a);
    if (GOPTS(ppg)->max_rst < (1000*1000*1000)) {
     pp_printf("Use -x to prevent jumps of"
     " more than one second\n");
     return -1;
    }
    break;
   case 'M':
    a = argv[++i];
    GOPTS(ppg)->max_dly = atoi(a);
    if (GOPTS(ppg)->max_dly < (1000*1000*1000)) {
     pp_printf("Use -x to prevent jumps of"
      " more than one second\n");
     return -1;
    }
    break;
   case 't':
    GOPTS(ppg)->no_adjust = 1;
    break;
   case 'a':
    a = argv[++i];
    cmd_line_parse_two(a, &n1, &n2);
    if (n1 < 1 || n2 < 1)
     return -1;
    GOPTS(ppg)->ap = n1;
    GOPTS(ppg)->ai = n2;
    break;
   case 'w':
    a = argv[++i];
    GOPTS(ppg)->s = atoi(a);
    break;
   case 'b':
    a = argv[++i];
    if (ppg->nlinks == 1)
     ppg->pp_instances[0].iface_name = a;
    else {
     pp_printf("Can not use -b option in multi-link conf");
     return -1;
    }
    break;
   case 'l':
    a = argv[++i];
    cmd_line_parse_two(a, &n1, &n2);
    GOPTS(ppg)->inbound_latency.nanoseconds = n1;
    GOPTS(ppg)->outbound_latency.nanoseconds = n2;
    break;
   case 'i':
    a = argv[++i];
    GOPTS(ppg)->domain_number = atoi(a);
    break;
   case 'y':
    a = argv[++i];
    GOPTS(ppg)->sync_intvl = atoi(a);
    break;
   case 'n':
    a = argv[++i];
    GOPTS(ppg)->announce_intvl = atoi(a);
    if (GOPTS(ppg)->announce_intvl < 0)
     GOPTS(ppg)->announce_intvl = 0;
    if (GOPTS(ppg)->announce_intvl > 4)
     GOPTS(ppg)->announce_intvl = 4;
    break;
   case 'g':
    for (j = 0; j < ppg->nlinks; j++)
     ppg->pp_instances[j].slave_only = 1;
    break;
   case 'v':
    a = argv[++i];
    GOPTS(ppg)->clock_quality.
     offsetScaledLogVariance = atoi(a);
    break;
   case 'r':
    a = argv[++i];
    GOPTS(ppg)->clock_quality.clockAccuracy =
     atoi(a);
    break;
   case 's':
    a = argv[++i];
    GOPTS(ppg)->clock_quality.clockClass =
     atoi(a);
    break;
   case 'p':
    a = argv[++i];
    GOPTS(ppg)->prio1 = atoi(a);
    break;
   case 'q':
    a = argv[++i];
    GOPTS(ppg)->prio2 = atoi(a);
    break;
   case 'h':
    break;
   case 'e':
    for (j = 0; j < ppg->nlinks; j++)
     ppg->pp_instances[j].ethernet_mode = 1;
    break;
   case 'G':
   default:
    cmd_line_print_help();
    return -1;
   }
  }
  }
 return 0;
}

variable_types: struct pp_globals { struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; } *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); } *n_ops; struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); } *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path { struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; } ch[-1]; int mcast_addr; int ptp_offset; } np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master { struct PortIdentity { struct ClockIdentity { unsigned char id[8]; } clockIdentity; unsigned short portNumber; } port_id; struct MsgAnnounce { struct Timestamp { struct UInteger48 { unsigned int lsb; unsigned short msb; } secondsField; unsigned int nanosecondsField; } originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; } grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; } ann; struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 { unsigned int lsb; int msb; } correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; } hdr; } frgn_master[5]; struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; } *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; } *pp_instances; struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr { int nsec_prev; int y; int s_exp; } owd_fltr; struct pp_ofm_fltr { int nsec_prev; int y; } ofm_fltr; } *servo; struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; } *rt_opts; struct DSDefault { enum Boolean { FALSE=0, TRUE=1 } twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean { FALSE=0, TRUE=1 } slaveOnly; } *defaultDS; struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; } *currentDS; struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; } *parentDS; struct DSTimeProperties { short currentUtcOffset; enum Boolean { FALSE=0, TRUE=1 } currentUtcOffsetValid; enum Boolean { FALSE=0, TRUE=1 } leap59; enum Boolean { FALSE=0, TRUE=1 } leap61; enum Boolean { FALSE=0, TRUE=1 } timeTraceable; enum Boolean { FALSE=0, TRUE=1 } frequencyTraceable; enum Boolean { FALSE=0, TRUE=1 } ptpTimescale; unsigned char timeSource; } *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; } *links; void *arch_data; } *

variable_types: argc int

variable_types:
argv
char **

variable_types: err int

variable_types: j int

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct DSTimeProperties { short currentUtcOffset; enum Boolean currentUtcOffsetValid; enum Boolean leap59; enum Boolean leap61; enum Boolean timeTraceable; enum Boolean frequencyTraceable; enum Boolean ptpTimescale; unsigned char timeSource; }

user_defined_types: struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

user_defined_types: struct MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

user_defined_types: struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; }


decompiled_code:
int __fastcall pp_puts(const char *a1)
{
return fputs(a1, stdout);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fputs(a1, stdout);
}

original_code:
void pp_puts(const char *s)
{
 fputs(s, stdout);
}

canonical_original_code:
void pp_puts(char *s)
{
 fputs(s, stdout);
}


decompiled_code:
__int64 __fastcall handle_iface(__int64 a1, const char *a2)
{
strcpy((char *)(*(_QWORD *)(a1 + 72) + 44LL * *(int *)(a1 + 64) + 16), a2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
strcpy((char *)(*(_QWORD *)(a1 + 72) + 44LL * *(int *)(a1 + 64) + 16), a2);
return 1LL;
}

original_code:
static int handle_iface(struct pp_globals *ppg, char *val)
{
 strcpy(ppg->links[ppg->nlinks].iface_name, val);
 return 1;
}

variable_types:
val
char *

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }

user_defined_types: struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

user_defined_types: struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

user_defined_types: struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; }

user_defined_types: struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); }

user_defined_types: struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; }

user_defined_types: struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; }

user_defined_types: struct Integer64 { unsigned int lsb; int msb; }


decompiled_code:
__int64 __fastcall handle_link(__int64 a1, const char *a2)
{
int v2; // eax

v2 = *(_DWORD *)(a1 + 64) + 1;
*(_DWORD *)(a1 + 64) = v2;
strcpy((char *)(*(_QWORD *)(a1 + 72) + 44LL * v2), a2);
return 1LL;
}

original_code:
static int handle_link(struct pp_globals *ppg, char *val)
{
 ppg->nlinks++;
 strcpy(ppg->links[ppg->nlinks].link_name, val);
 return 1;
}

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_owd_fltr { int nsec_prev; int y; int s_exp; }

user_defined_types: struct pp_ofm_fltr { int nsec_prev; int y; }

user_defined_types: struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

user_defined_types: struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }

user_defined_types: struct ClockIdentity { unsigned char id[8]; }

user_defined_types: struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; }

user_defined_types: struct UInteger48 { unsigned int lsb; unsigned short msb; }

user_defined_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }


decompiled_code:
__int64 __fastcall detect_keyword(char *s2)
{
unsigned int v1; // er12
const char *v3; // rdi
const char **v4; // rbx

v1 = 0;
v3 = "link";
v4 = (const char **)&off_5E0;
while ( strcmp(v3, s2) )
{
if ( ++v1 == 14 )
return (unsigned int)-1;
v3 = *v4;
v4 += 3;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s2)
{
unsigned int v1; // er12
const char *v3; // rdi
const char **v4; // rbx

v1 = 0;
v3 = "link";
v4 = (const char **)&off_5E0;
while ( strcmp(v3, s2) )
{
if ( ++v1 == 14 )
return (unsigned int)-1;
v3 = *v4;
v4 += 3;
}
return v1;
}

original_code:
static int detect_keyword(char *kw)
{
 int i;
 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++)
  if (!strcmp(keywords[i].key, kw))
   return i;
 return KW_INV;
}

variable_types:
kw
char *


decompiled_code:
__int64 __fastcall handle_key_val(__int64 a1, char *a2, const char *a3)
{
unsigned int v4; // eax
__int64 result; // rax

v4 = detect_keyword(a2);
if ( v4 <= 0xD && ((1LL << v4) & 0x3227) != 0 )
result = ((__int64 (__fastcall *)(__int64, const char *))*(&funcs_109 + 3 * (int)v4))(a1, a3);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char *a2, const char *a3)
{
unsigned int v4; // eax
__int64 result; // rax

v4 = func2(a2);
if ( v4 <= 0xD && ((1LL << v4) & 0x3227) != 0 )
result = ((__int64 (__fastcall *)(__int64, const char *))*(&funcs_109 + 3 * (int)v4))(a1, a3);
else
result = 0LL;
return result;
}

original_code:
static int handle_key_val(struct pp_globals *ppg, char *key, char *val)
{
 int k_id;
 k_id = detect_keyword(key);
 switch(k_id) {
  case KW_LINK:
  case KW_IFACE:
  case KW_PROTO:
  case KW_ROLE:
  case KW_EXT:
  case KW_CLOCK_CLASS:
  case KW_CLOCK_ACCURACY:
   return keywords[k_id].handle_key(ppg, val);
  default:
   return 0;
 }
}

variable_types:
key
char *

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }

user_defined_types: struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

user_defined_types: struct DSTimeProperties { short currentUtcOffset; enum Boolean currentUtcOffsetValid; enum Boolean leap59; enum Boolean leap61; enum Boolean timeTraceable; enum Boolean frequencyTraceable; enum Boolean ptpTimescale; unsigned char timeSource; }

user_defined_types: struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

user_defined_types: struct MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

user_defined_types: struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; }

user_defined_types: struct Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }


decompiled_code:
__int64 __fastcall handle_ext(__int64 a1, char *a2)
{
int v2; // eax
unsigned int v3; // er8
unsigned int v4; // eax

v2 = detect_keyword(a2);
v3 = 0;
v4 = v2 - 10;
if ( v4 <= 1 )
{
v3 = 1;
*(_DWORD *)(*(_QWORD *)(a1 + 72) + 44LL * *(int *)(a1 + 64) + 40) = v4;
}
return v3;
}

original_code:
static int handle_ext(struct pp_globals *ppg, char *val)
{
 int v_id;
 v_id = detect_keyword(val);
 switch(v_id) {
  case KW_NONE:
  case KW_WHITERAB:
   ppg->links[ppg->nlinks].ext = v_id - KW_NONE;
   break;
  default:
   return 0;
 }
 return 1;
}

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

user_defined_types: struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_owd_fltr { int nsec_prev; int y; int s_exp; }

user_defined_types: struct pp_ofm_fltr { int nsec_prev; int y; }

user_defined_types: struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); }

user_defined_types: struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; }

user_defined_types: struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; }

user_defined_types: struct Integer64 { unsigned int lsb; int msb; }


decompiled_code:
__int64 __fastcall handle_role(__int64 a1, char *a2)
{
int v2; // eax
unsigned int v3; // er8
unsigned int v4; // eax

v2 = detect_keyword(a2);
v3 = 0;
v4 = v2 - 6;
if ( v4 <= 2 )
{
v3 = 1;
*(_DWORD *)(*(_QWORD *)(a1 + 72) + 44LL * *(int *)(a1 + 64) + 36) = v4;
}
return v3;
}

original_code:
static int handle_role(struct pp_globals *ppg, char *val)
{
 int v_id;
 v_id = detect_keyword(val);
 switch(v_id) {
  case KW_AUTO:
  case KW_MASTER:
  case KW_SLAVE:
   ppg->links[ppg->nlinks].role = v_id - KW_AUTO;
   break;
  default:
   return 0;
 }
 return 1;
}

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }

user_defined_types: struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

user_defined_types: struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }

user_defined_types: struct ClockIdentity { unsigned char id[8]; }

user_defined_types: struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; }

user_defined_types: struct UInteger48 { unsigned int lsb; unsigned short msb; }

user_defined_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }


decompiled_code:
__int64 __fastcall handle_proto(__int64 a1, char *a2)
{
int v2; // eax
unsigned int v3; // er8
unsigned int v4; // eax

v2 = detect_keyword(a2);
v3 = 0;
v4 = v2 - 3;
if ( v4 <= 1 )
{
v3 = 1;
*(_DWORD *)(*(_QWORD *)(a1 + 72) + 44LL * *(int *)(a1 + 64) + 32) = v4;
}
return v3;
}

original_code:
static int handle_proto(struct pp_globals *ppg, char *val)
{
 int v_id;
 v_id = detect_keyword(val);
 switch(v_id) {
  case KW_RAW:
  case KW_UDP:
   ppg->links[ppg->nlinks].proto = v_id - KW_RAW;
   break;
  default:
   return 0;
 }
 return 1;
}

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

user_defined_types: struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

user_defined_types: struct DSTimeProperties { short currentUtcOffset; enum Boolean currentUtcOffsetValid; enum Boolean leap59; enum Boolean leap61; enum Boolean timeTraceable; enum Boolean frequencyTraceable; enum Boolean ptpTimescale; unsigned char timeSource; }

user_defined_types: struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

user_defined_types: struct MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

user_defined_types: struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; }

user_defined_types: struct Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }


decompiled_code:
__int64 __fastcall handle_clock_class(__int64 a1, const char *a2)
{
_BYTE *v2; // rax
char v3; // dl

atoi(a2);
v2 = (_BYTE *)GOPTS_isra_0(*(_QWORD *)(a1 + 16));
*v2 = v3;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, const char *a2)
{
_BYTE *v2; // rax
char v3; // dl

atoi(a2);
v2 = (_BYTE *)func7(*(_QWORD *)(a1 + 16));
*v2 = v3;
return 1LL;
}

original_code:
static int handle_clock_class(struct pp_globals *ppg, char *val)
{
 GOPTS(ppg)->clock_quality.clockClass = atoi(val);
 return 1;
}

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }

user_defined_types: struct pp_owd_fltr { int nsec_prev; int y; int s_exp; }

user_defined_types: struct pp_ofm_fltr { int nsec_prev; int y; }

user_defined_types: struct pp_time_operations { int (*get)(struct pp_instance *ppi, struct TimeInternal *t); int (*set)(struct pp_instance *ppi, struct TimeInternal *t); int (*adjust)(struct pp_instance *ppi, long offset_ns, long freq_ppm); int (*adjust_offset)(struct pp_instance *ppi, long offset_ns); int (*adjust_freq)(struct pp_instance *ppi, long freq_ppm); unsigned long (*calc_timeout)(struct pp_instance *ppi, int millisec); }

user_defined_types: struct DSPort { struct PortIdentity portIdentity; char logMinDelayReqInterval; char logAnnounceInterval; unsigned char announceReceiptTimeout; char logSyncInterval; unsigned char versionNumber; void *ext_dsport; }

user_defined_types: struct pp_channel { union { int fd; void *custom; }; void *arch_data; unsigned char addr[6]; unsigned char peer[6]; int pkt_present; }

user_defined_types: struct Integer64 { unsigned int lsb; int msb; }


decompiled_code:
__int64 __fastcall handle_clock_accuracy(__int64 a1, const char *a2)
{
__int64 v2; // rax
char v3; // dl

atoi(a2);
v2 = GOPTS_isra_0(*(_QWORD *)(a1 + 16));
*(_BYTE *)(v2 + 1) = v3;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, const char *a2)
{
__int64 v2; // rax
char v3; // dl

atoi(a2);
v2 = func7(*(_QWORD *)(a1 + 16));
*(_BYTE *)(v2 + 1) = v3;
return 1LL;
}

original_code:
static int handle_clock_accuracy(struct pp_globals *ppg, char *val)
{
 GOPTS(ppg)->clock_quality.clockAccuracy = atoi(val);
 return 1;
}

user_defined_types: struct pp_globals { struct pp_instance *pp_instances; struct pp_servo *servo; struct pp_runtime_opts *rt_opts; struct DSDefault *defaultDS; struct DSCurrent *currentDS; struct DSParent *parentDS; struct DSTimeProperties *timePropertiesDS; int ebest_idx; int ebest_updated; int nlinks; int max_links; struct pp_link *links; void *arch_data; }

user_defined_types: struct DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

user_defined_types: struct DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

user_defined_types: struct DSTimeProperties { short currentUtcOffset; enum Boolean currentUtcOffsetValid; enum Boolean leap59; enum Boolean leap61; enum Boolean timeTraceable; enum Boolean frequencyTraceable; enum Boolean ptpTimescale; unsigned char timeSource; }

user_defined_types: struct pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

user_defined_types: struct PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }

user_defined_types: struct ClockIdentity { unsigned char id[8]; }

user_defined_types: struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; }

user_defined_types: struct UInteger48 { unsigned int lsb; unsigned short msb; }

user_defined_types: struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; }


original_code:
int pp_parse_conf(struct pp_globals *ppg, char *conf, int len)
{
 int st = 0;
 char c;
 char key[16], val[16];
 int toklen = 0;
 int ln = 1;
 int newln;
 int i;
 ppg->nlinks = -1;
 memset(ppg->links, 0, ppg->max_links * sizeof(struct pp_link));
 for (i = 0; i < len; i++) {
  c = conf[i];
  newln = 0;
  if ((c == '\n') || (c == '\0')) {
   ln++;
   newln = 1;
   if (st == 2)
    goto ret_invalid_line;
  }
  switch (st) {
  case 0:
   if ((c == ' ') || (c == '\t') || (c == '\r') || (newln))
    continue;
   if (c == '#')
    st = 1;
   else {
    key[0] = c;
    toklen = 1;
    st = 2;
   }
   break;
  case 1:
   if (newln)
    st = 0;
   else
    continue;
   break;
  case 2:
   if (toklen == sizeof(key))
    goto ret_invalid_line;
   if ((c != ' ') && (c != '\t'))
    key[toklen++] = c;
   else {
    st = 3;
    key[toklen] = '\0';
    toklen = 0;
   }
   break;
  case 3:
   if (toklen == sizeof(val))
    goto ret_invalid_line;
   if ((c != ' ') && (c != '\t') && (c != '\r') && (!newln))
    val[toklen++] = c;
   else if (newln) {
    val[toklen] = '\0';
    if (!handle_key_val(ppg, key, val))
     goto ret_invalid_line;
    st = 0;
    toklen = 0;
   }
  }
 }
 ppg->nlinks++;
 return 0;
ret_invalid_line:
 ln--;
 return -ln;
}

canonical_original_code:
int pp_parse_conf(struct pp_globals *ppg, char *conf, int len)
{
 int st = 0;
 char c;
 char key[16]; char val[16];
 int toklen = 0;
 int ln = 1;
 int newln;
 int i;
 ppg->nlinks = -1;
 memset(ppg->links, 0, ppg->max_links * sizeof(struct pp_link));
 for (i = 0; i < len; i++) {
  c = conf[i];
  newln = 0;
  if ((c == '\n') || (c == '\0')) {
   ln++;
   newln = 1;
   if (st == 2)
    goto ret_invalid_line;
  }
  switch (st) {
  case 0:
   if ((c == ' ') || (c == '\t') || (c == '\r') || (newln))
    continue;
   if (c == '#')
    st = 1;
   else {
    key[0] = c;
    toklen = 1;
    st = 2;
   }
   break;
  case 1:
   if (newln)
    st = 0;
   else
    continue;
   break;
  case 2:
   if (toklen == sizeof(key))
    goto ret_invalid_line;
   if ((c != ' ') && (c != '\t'))
    key[toklen++] = c;
   else {
    st = 3;
    key[toklen] = '\0';
    toklen = 0;
   }
   break;
  case 3:
   if (toklen == sizeof(val))
    goto ret_invalid_line;
   if ((c != ' ') && (c != '\t') && (c != '\r') && (!newln))
    val[toklen++] = c;
   else if (newln) {
    val[toklen] = '\0';
    if (!handle_key_val(ppg, key, val))
     goto ret_invalid_line;
    st = 0;
    toklen = 0;
   }
  }
 }
 ppg->nlinks++;
 return 0;
ret_invalid_line:
 ln--;
 return -ln;
}

variable_types:
conf
char *

variable_types: len int

variable_types: st int

variable_types:
key
char [16]

variable_types:
val
char [16]

variable_types: toklen int

variable_types: ln int

variable_types: newln int

variable_types: i int

user_defined_types: struct pp_link { char link_name[16]; char iface_name[16]; int proto; int role; int ext; }

user_defined_types: struct pp_instance { int state; int next_state; void *arch_data; void *ext_data; unsigned long flags; struct pp_globals *glbs; struct pp_network_operations *n_ops; struct pp_time_operations *t_ops; unsigned char tx_buffer[200]; unsigned char rx_buffer[200]; void *tx_frame; struct pp_net_path np; struct TimeInternal t1; struct TimeInternal cField; struct TimeInternal last_rcv_time; unsigned short frgn_rec_num; short frgn_rec_i; short frgn_rec_best; struct pp_frgn_master frgn_master[5]; struct DSPort *portDS; unsigned long timeouts[-1]; unsigned short recv_sync_sequence_id; char log_min_delay_req_interval; unsigned short sent_seq[-1]; struct MsgHeader received_ptp_header; struct MsgHeader delay_req_hdr; unsigned int is_from_cur_par; char *iface_name; int port_idx; }

user_defined_types: struct pp_servo { struct TimeInternal m_to_s_dly; struct TimeInternal s_to_m_dly; struct TimeInternal delay_ms; struct TimeInternal delay_sm; int obs_drift; struct pp_owd_fltr owd_fltr; struct pp_ofm_fltr ofm_fltr; }

user_defined_types: struct pp_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

user_defined_types: struct DSDefault { enum Boolean twoStepFlag; struct ClockIdentity clockIdentity; unsigned short numberPorts; struct ClockQuality clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean slaveOnly; }

user_defined_types: struct pp_net_path { struct pp_channel ch[-1]; int mcast_addr; int ptp_offset; }

user_defined_types: struct pp_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

user_defined_types: struct MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

user_defined_types: struct MsgHeader { unsigned char transportSpecific; unsigned char messageType; unsigned char versionPTP; unsigned short messageLength; unsigned char domainNumber; unsigned char flagField[2]; struct Integer64 correctionfield; struct PortIdentity sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; }

user_defined_types: struct Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }