original_code:
void bmc_update_ebest(struct pp_globals *ppg)
{
 int i, best;
 struct pp_instance *ppi, *ppi_best;
 for (i = 1, best = 0; i < ppg->defaultDS->numberPorts; i++) {
  ppi_best = &ppg->pp_instances[best];
  ppi = &ppg->pp_instances[i];
  if ((ppi->frgn_rec_num > 0) &&
    (bmc_dataset_cmp(ppi,
    &ppi_best->frgn_master[ppi_best->frgn_rec_best],
    &ppi->frgn_master[ppi->frgn_rec_best])
    < 0))
    best = i;
 }
 if (ppg->ebest_idx != best) {
  ppg->ebest_idx = best;
  ppg->ebest_updated = 1;
 }
}

canonical_original_code:
void bmc_update_ebest(struct pp_globals *ppg)
{
 int i; int best;
 struct pp_instance *ppi; struct pp_instance *ppi_best;
 for (i = 1, best = 0; i < ppg->defaultDS->numberPorts; i++) {
  ppi_best = &ppg->pp_instances[best];
  ppi = &ppg->pp_instances[i];
  if ((ppi->frgn_rec_num > 0) &&
    (bmc_dataset_cmp(ppi,
    &ppi_best->frgn_master[ppi_best->frgn_rec_best],
    &ppi->frgn_master[ppi->frgn_rec_best])
    < 0))
    best = i;
 }
 if (ppg->ebest_idx != best) {
  ppg->ebest_idx = best;
  ppg->ebest_updated = 1;
 }
}

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: i int

variable_types: best 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 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_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 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 PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }


bmc

repo: tinito/ppsi-chibios

original_code:
int bmc(struct pp_instance *ppi)
{
 struct pp_frgn_master *frgn_master = ppi->frgn_master;
 int i, best;
 if (!ppi->frgn_rec_num)
  if (ppi->state == PPS_MASTER) {
   m1(ppi);
   return ppi->state;
  }
 for (i = 1, best = 0; i < ppi->frgn_rec_num; i++)
  if (bmc_dataset_cmp(ppi, &frgn_master[i], &frgn_master[best])
      < 0)
   best = i;
 ({ if (1) __pp_diag(ppi, pp_dt_bmc, 1, "Best foreign master is %i\n", best); 1; });
 if (ppi->frgn_rec_best != best) {
  ppi->frgn_rec_best = best;
  bmc_update_ebest(GLBS(ppi));
 }
 return bmc_state_decision(ppi, &frgn_master[best]);
}

canonical_original_code:
int bmc(struct pp_instance *ppi)
{
 struct pp_frgn_master *frgn_master = ppi->frgn_master;
 int i; int best;
 if (!ppi->frgn_rec_num)
  if (ppi->state == PPS_MASTER) {
   m1(ppi);
   return ppi->state;
  }
 for (i = 1, best = 0; i < ppi->frgn_rec_num; i++)
  if (bmc_dataset_cmp(ppi, &frgn_master[i], &frgn_master[best])
      < 0)
   best = i;
 ({ if (1) __pp_diag(ppi, pp_dt_bmc, 1, "Best foreign master is %i\n", best); 1; });
 if (ppi->frgn_rec_best != best) {
  ppi->frgn_rec_best = best;
  bmc_update_ebest(GLBS(ppi));
 }
 return bmc_state_decision(ppi, &frgn_master[best]);
}

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

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_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

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

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 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 Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }

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


decompiled_code:
__int64 __fastcall pp_faulty(__int64 a1)
{
_pp_diag(a1, 7LL, 1LL, "Faulty state detected\n");
*(_DWORD *)(a1 + 4) = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_pp_diag(a1, 7LL, 1LL, "Faulty state detected\n");
*(_DWORD *)(a1 + 4) = 1;
return 0LL;
}

original_code:
int pp_faulty(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 ({ if (1) __pp_diag(ppi, pp_dt_fsm, 1, "Faulty state detected\n"); 1; });
 ppi->next_state = PPS_INITIALIZING;
 return 0;
}

variable_types:
pkt
unsigned char *

variable_types: plen int

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 UInteger48 { unsigned int lsb; unsigned short msb; }


puts

decompiled_code:
__int64 puts()
{
pp_puts();
return 0LL;
}

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

original_code:
int puts(const char *s)
{
 pp_puts(s);
 return 0;
}

canonical_original_code:
int puts(char *s)
{
 pp_puts(s);
 return 0;
}

variable_types:
s
char *


memcmp

decompiled_code:
__int64 __fastcall memcmp(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v6; // rax
int v7; // edx
int v8; // ecx
unsigned int v9; // edx

v6 = 0LL;
while ( a3 != v6 )
{
v7 = *(unsigned __int8 *)(a1 + v6);
v8 = *(unsigned __int8 *)(a2 + v6++);
v9 = v7 - v8;
if ( v9 )
return v9;
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v6; // rax
int v7; // edx
int v8; // ecx
unsigned int v9; // edx

v6 = 0LL;
while ( a3 != v6 )
{
v7 = *(unsigned __int8 *)(a1 + v6);
v8 = *(unsigned __int8 *)(a2 + v6++);
v9 = v7 - v8;
if ( v9 )
return v9;
}
return 0;
}

original_code:
int memcmp(const void *s1, const void *s2, size_t n)
{
 register const unsigned char *r1 = s1;
 register const unsigned char *r2 = s2;
 int r = 0;
 while (n-- && ((r = ((int)(*r1++)) - *r2++) == 0))
  ;
 return r;
}

canonical_original_code:
int memcmp(void *s1, void *s2, unsigned long n)
{
 register unsigned char *r1 = s1;
 register unsigned char *r2 = s2;
 int r = 0;
 while (n-- && ((r = ((int)(*r1++)) - *r2++) == 0))
  ;
 return r;
}

variable_types:
n
unsigned long

variable_types: r int


strtol

decompiled_code:
__int64 __fastcall strtol(char *a1, char **a2, int a3)
{
int v3; // er8
__int64 v4; // rdx
char v6; // al

v3 = a3;
if ( !a3 )
v3 = 10;
v4 = 0LL;
if ( !*a1 )
return 0LL;
do
{
if ( a2 )
*a2 = a1;
if ( v3 > 10 )
break;
v6 = *a1;
if ( *a1 <= 47 )
break;
if ( v3 + 47 < v6 )
break;
++a1;
v4 = v3 * v4 + v6 - 48;
}
while ( *a1 );
return v4;
}

original_code:
long int strtol(const char *s, char **end, int base)
{
 long res = 0;
 if (!base)
  base = 10;
 while (*s) {
  if (end)
   *end = (void *)s;
  if (base > 10 || *s < '0' || *s >= '0' + base) {
   return res;
  }
  res = res * base + *(s++) - '0';
 }
 return res;
}

canonical_original_code:
long strtol(char *s, char **end, int base)
{
 long res = 0;
 if (!base)
  base = 10;
 while (*s) {
  if (end)
   *end = (void *)s;
  if (base > 10 || *s < '0' || *s >= '0' + base) {
   return res;
  }
  res = res * base + *(s++) - '0';
 }
 return res;
}

variable_types:
end
char **

variable_types: base int

variable_types: res long

return_type: long


atoi

decompiled_code:
__int64 __fastcall atoi(char *a1)
{
return strtol(a1, 0LL, 10);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *a1)
{
return func4(a1, 0LL, 10);
}

original_code:
int atoi(const char *s)
{
 return strtol(s, ((void *)0), 10);
}

canonical_original_code:
int atoi(char *s)
{
 return strtol(s, ((void *)0), 10);
}


decompiled_code:
void __fastcall pp_timeout_clr(__int64 a1, int a2)
{
*(_QWORD *)(a1 + 8LL * a2 + 1248) = 0LL;
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2)
{
*(_QWORD *)(a1 + 8LL * a2 + 1248) = 0LL;
}

original_code:
static inline void pp_timeout_clr(struct pp_instance *ppi, int index)
{
 ppi->timeouts[index] = 0;
}

variable_types: index int

return_type: void

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_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_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 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 PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }


decompiled_code:
__int64 __fastcall pp_ms_to_timeout(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 v4; // rbx

v2 = *(_QWORD *)(a1 + 8LL * a2 + 1248);
result = 0LL;
if ( v2 )
{
v4 = v2 - (*(__int64 (__fastcall **)(__int64, _QWORD))(*(_QWORD *)(a1 + 56) + 40LL))(a1, 0LL);
if ( v4 < 0 )
LODWORD(v4) = 0;
result = (unsigned int)v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 v4; // rbx

v2 = *(_QWORD *)(a1 + 8LL * a2 + 1248);
result = 0LL;
if ( v2 )
{
v4 = v2 - (*(__int64 (__fastcall **)(__int64, _QWORD))(*(_QWORD *)(a1 + 56) + 40LL))(a1, 0LL);
if ( v4 < 0 )
LODWORD(v4) = 0;
result = (unsigned int)v4;
}
return result;
}

original_code:
static inline int pp_ms_to_timeout(struct pp_instance *ppi, int index)
{
 signed long ret;
 if (!ppi->timeouts[index])
  return 0;
 ret = ppi->timeouts[index] - ppi->t_ops->calc_timeout(ppi, 0);
 return ret <= 0 ? 0 : ret;
}

canonical_original_code:
static inline int pp_ms_to_timeout(struct pp_instance *ppi, int index)
{
 long ret;
 if (!ppi->timeouts[index])
  return 0;
 ret = ppi->timeouts[index] - ppi->t_ops->calc_timeout(ppi, 0);
 return ret <= 0 ? 0 : ret;
}

variable_types: ret long

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 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 Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }

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


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

v2 = pp_timeout(a1, a2);
if ( v2 )
pp_timeout_clr(a1, a2);
return v2;
}

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

v2 = func8(a1, a2);
if ( v2 )
func6(a1, a2);
return v2;
}

original_code:
static inline int pp_timeout_z(struct pp_instance *ppi, int index)
{
 int ret = pp_timeout(ppi, index);
 if (ret)
  pp_timeout_clr(ppi, index);
 return ret;
}

variable_types: ret 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_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 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 UInteger48 { unsigned int lsb; unsigned short msb; }


decompiled_code:
__int64 __fastcall pp_master(__int64 a1, __int64 a2, unsigned int a3)
{
int v4; // ebp
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
int v8; // ebp
int v9; // eax
__int64 v11; // rax
__int64 v12; // rcx
__int64 (__fastcall *v13)(__int64, __int64, _QWORD, __int64); // rax
__int64 v14; // rax
__int64 v15; // rdx
__int64 v16; // rcx

v4 = *(_DWORD *)(a1 + 12);
if ( v4 )
{
v5 = DSPOR(a1);
pp_timeout_rand(a1, 1LL, (unsigned int)*(char *)(v5 + 13));
v6 = DSPOR(a1);
pp_timeout_rand(a1, 3LL, (unsigned int)*(char *)(v6 + 11));
v4 = msg_issue_announce(a1);
if ( v4 < 0 )
goto LABEL_6;
}
if ( (unsigned int)pp_timeout_z(a1, 1u) )
{
if ( (int)msg_issue_sync(a1) < 0 )
goto LABEL_6;
v7 = OPTS(a1);
add_TimeInternal(a1 + 688, a1 + 688, v7 + 24);
v4 = msg_issue_followup(a1, a1 + 688);
if ( v4 )
goto LABEL_6;
v11 = DSPOR(a1);
pp_timeout_rand(a1, 1LL, (unsigned int)*(char *)(v11 + 13));
}
if ( (unsigned int)pp_timeout_z(a1, 3u) )
{
if ( (int)msg_issue_announce(a1) < 0 )
goto LABEL_6;
v14 = DSPOR(a1);
pp_timeout_rand(a1, 3LL, (unsigned int)*(char *)(v14 + 11));
v4 = 0;
if ( !a3 )
goto LABEL_20;
}
else if ( !a3 )
{
goto LABEL_19;
}
v12 = *(unsigned __int8 *)(a1 + 1345);
v13 = (__int64 (__fastcall *)(__int64, __int64, _QWORD, __int64))pp_hooks[4];
if ( v13 )
{
LODWORD(v12) = v13(a1, a2, a3, v12);
if ( (int)v12 < 0 )
goto LABEL_6;
}
switch ( (_DWORD)v12 )
{
case 1:
msg_copy_header(a1 + 1380, a1 + 1344);
msg_issue_delay_resp(a1, a1 + 668, v15, v16);
break;
case 0xB:
v4 = st_com_master_handle_announce(a1, a2, a3);
break;
case 0:
v4 = st_com_master_handle_sync(a1, a2, a3);
break;
}
LABEL_19:
if ( v4 )
{
LABEL_6:
*(_DWORD *)(a1 + 4) = 2;
goto LABEL_7;
}
LABEL_20:
if ( (*(_BYTE *)(a1 + 1416) & 4) != 0 || *(_BYTE *)(DSDEF(a1) + 14) == 0xFF )
*(_DWORD *)(a1 + 4) = 4;
LABEL_7:
v8 = pp_ms_to_timeout(a1, 3);
v9 = pp_ms_to_timeout(a1, 1);
if ( v8 > v9 )
v8 = v9;
*(_DWORD *)(a1 + 8) = v8;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, unsigned int a3)
{
int v4; // ebp
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
int v8; // ebp
int v9; // eax
__int64 v11; // rax
__int64 v12; // rcx
__int64 (__fastcall *v13)(__int64, __int64, _QWORD, __int64); // rax
__int64 v14; // rax
__int64 v15; // rdx
__int64 v16; // rcx

v4 = *(_DWORD *)(a1 + 12);
if ( v4 )
{
v5 = func5(a1);
pp_timeout_rand(a1, 1LL, (unsigned int)*(char *)(v5 + 13));
v6 = func5(a1);
pp_timeout_rand(a1, 3LL, (unsigned int)*(char *)(v6 + 11));
v4 = msg_issue_announce(a1);
if ( v4 < 0 )
goto LABEL_6;
}
if ( (unsigned int)func9(a1, 1u) )
{
if ( (int)msg_issue_sync(a1) < 0 )
goto LABEL_6;
v7 = func2(a1);
add_TimeInternal(a1 + 688, a1 + 688, v7 + 24);
v4 = msg_issue_followup(a1, a1 + 688);
if ( v4 )
goto LABEL_6;
v11 = func5(a1);
pp_timeout_rand(a1, 1LL, (unsigned int)*(char *)(v11 + 13));
}
if ( (unsigned int)func9(a1, 3u) )
{
if ( (int)msg_issue_announce(a1) < 0 )
goto LABEL_6;
v14 = func5(a1);
pp_timeout_rand(a1, 3LL, (unsigned int)*(char *)(v14 + 11));
v4 = 0;
if ( !a3 )
goto LABEL_20;
}
else if ( !a3 )
{
goto LABEL_19;
}
v12 = *(unsigned __int8 *)(a1 + 1345);
v13 = (__int64 (__fastcall *)(__int64, __int64, _QWORD, __int64))pp_hooks[4];
if ( v13 )
{
LODWORD(v12) = v13(a1, a2, a3, v12);
if ( (int)v12 < 0 )
goto LABEL_6;
}
switch ( (_DWORD)v12 )
{
case 1:
msg_copy_header(a1 + 1380, a1 + 1344);
msg_issue_delay_resp(a1, a1 + 668, v15, v16);
break;
case 0xB:
v4 = st_com_master_handle_announce(a1, a2, a3);
break;
case 0:
v4 = st_com_master_handle_sync(a1, a2, a3);
break;
}
LABEL_19:
if ( v4 )
{
LABEL_6:
*(_DWORD *)(a1 + 4) = 2;
goto LABEL_7;
}
LABEL_20:
if ( (*(_BYTE *)(a1 + 1416) & 4) != 0 || *(_BYTE *)(func4(a1) + 14) == 0xFF )
*(_DWORD *)(a1 + 4) = 4;
LABEL_7:
v8 = func7(a1, 3);
v9 = func7(a1, 1);
if ( v8 > v9 )
v8 = v9;
*(_DWORD *)(a1 + 8) = v8;
return 0LL;
}

original_code:
int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 TimeInternal *time_snt;
 int msgtype, d1, d2;
 int e = 0;
 if (ppi->is_new_state) {
  pp_timeout_rand(ppi, PP_TO_SYNC, DSPOR(ppi)->logSyncInterval);
  pp_timeout_rand(ppi, PP_TO_ANN_INTERVAL,
    DSPOR(ppi)->logAnnounceInterval);
  if ((e = msg_issue_announce(ppi)) < 0)
   goto out;
 }
 if (pp_timeout_z(ppi, PP_TO_SYNC)) {
  if ((e = msg_issue_sync(ppi) < 0))
   goto out;
  time_snt = &ppi->last_snt_time;
  add_TimeInternal(time_snt, time_snt,
     &OPTS(ppi)->outbound_latency);
  if ((e = msg_issue_followup(ppi, time_snt)))
   goto out;
  pp_timeout_rand(ppi, PP_TO_SYNC, DSPOR(ppi)->logSyncInterval);
 }
 if (pp_timeout_z(ppi, PP_TO_ANN_INTERVAL)) {
  if ((e = msg_issue_announce(ppi) < 0))
   goto out;
  pp_timeout_rand(ppi, PP_TO_ANN_INTERVAL,
    DSPOR(ppi)->logAnnounceInterval);
 }
 if (plen == 0)
  goto out;
 msgtype = ppi->received_ptp_header.messageType;
 if (pp_hooks.master_msg)
  msgtype = pp_hooks.master_msg(ppi, pkt, plen, msgtype);
 if (msgtype < 0) {
  e = msgtype;
  goto out;
 }
 switch (msgtype) {
 case PPM_NOTHING_TO_DO:
  break;
 case PPM_ANNOUNCE:
  e = st_com_master_handle_announce(ppi, pkt, plen);
  break;
 case PPM_SYNC:
  e = st_com_master_handle_sync(ppi, pkt, plen);
  break;
 case PPM_DELAY_REQ:
  msg_copy_header(&ppi->delay_req_hdr,
    &ppi->received_ptp_header);
  msg_issue_delay_resp(ppi, &ppi->last_rcv_time);
  break;
 default:
  break;
 }
out:
 if (e == 0) {
  if (ppi->slave_only ||
   DSDEF(ppi)->clockQuality.clockClass == 255)
   ppi->next_state = PPS_LISTENING;
 } else {
  ppi->next_state = PPS_FAULTY;
 }
 d1 = pp_ms_to_timeout(ppi, PP_TO_ANN_INTERVAL);
 d2 = pp_ms_to_timeout(ppi, PP_TO_SYNC);
 ppi->next_delay = d1 < d2 ? d1 : d2;
 return 0;
}

canonical_original_code:
int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 struct TimeInternal *time_snt;
 int msgtype; int d1; int d2;
 int e = 0;
 if (ppi->is_new_state) {
  pp_timeout_rand(ppi, PP_TO_SYNC, DSPOR(ppi)->logSyncInterval);
  pp_timeout_rand(ppi, PP_TO_ANN_INTERVAL,
    DSPOR(ppi)->logAnnounceInterval);
  if ((e = msg_issue_announce(ppi)) < 0)
   goto out;
 }
 if (pp_timeout_z(ppi, PP_TO_SYNC)) {
  if ((e = msg_issue_sync(ppi) < 0))
   goto out;
  time_snt = &ppi->last_snt_time;
  add_TimeInternal(time_snt, time_snt,
     &OPTS(ppi)->outbound_latency);
  if ((e = msg_issue_followup(ppi, time_snt)))
   goto out;
  pp_timeout_rand(ppi, PP_TO_SYNC, DSPOR(ppi)->logSyncInterval);
 }
 if (pp_timeout_z(ppi, PP_TO_ANN_INTERVAL)) {
  if ((e = msg_issue_announce(ppi) < 0))
   goto out;
  pp_timeout_rand(ppi, PP_TO_ANN_INTERVAL,
    DSPOR(ppi)->logAnnounceInterval);
 }
 if (plen == 0)
  goto out;
 msgtype = ppi->received_ptp_header.messageType;
 if (pp_hooks.master_msg)
  msgtype = pp_hooks.master_msg(ppi, pkt, plen, msgtype);
 if (msgtype < 0) {
  e = msgtype;
  goto out;
 }
 switch (msgtype) {
 case PPM_NOTHING_TO_DO:
  break;
 case PPM_ANNOUNCE:
  e = st_com_master_handle_announce(ppi, pkt, plen);
  break;
 case PPM_SYNC:
  e = st_com_master_handle_sync(ppi, pkt, plen);
  break;
 case PPM_DELAY_REQ:
  msg_copy_header(&ppi->delay_req_hdr,
    &ppi->received_ptp_header);
  msg_issue_delay_resp(ppi, &ppi->last_rcv_time);
  break;
 default:
  break;
 }
out:
 if (e == 0) {
  if (ppi->slave_only ||
   DSDEF(ppi)->clockQuality.clockClass == 255)
   ppi->next_state = PPS_LISTENING;
 } else {
  ppi->next_state = PPS_FAULTY;
 }
 d1 = pp_ms_to_timeout(ppi, PP_TO_ANN_INTERVAL);
 d2 = pp_ms_to_timeout(ppi, PP_TO_SYNC);
 ppi->next_delay = d1 < d2 ? d1 : d2;
 return 0;
}

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

variable_types: msgtype int

variable_types: e int

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 pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

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

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


decompiled_code:
__int64 __fastcall minipc_client_create(__int64 a1, unsigned __int16 a2)
{
return _minipc_link_create(a1, a2 | 0x20000u);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int16 a2)
{
return _minipc_link_create(a1, a2 | 0x20000u);
}

original_code:
struct minipc_ch *minipc_client_create(const char *name, int f)
{
 return __minipc_link_create(name, ((f) & 0xffff) | 0x00020000);
}

canonical_original_code:
struct minipc_ch *minipc_client_create(char *name, int f)
{
 return __minipc_link_create(name, ((f) & 0xffff) | 0x00020000);
}

variable_types:
name
char *

variable_types: f int

return_type: struct minipc_ch { int fd; } *


decompiled_code:
__int64 __fastcall minipc_server_create(__int64 a1, unsigned __int16 a2)
{
return _minipc_link_create(a1, a2 | 0x10000u);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int16 a2)
{
return _minipc_link_create(a1, a2 | 0x10000u);
}

original_code:
struct minipc_ch *minipc_server_create(const char *name, int f)
{
 return __minipc_link_create(name, ((f) & 0xffff) | 0x00010000);
}

canonical_original_code:
struct minipc_ch *minipc_server_create(char *name, int f)
{
 return __minipc_link_create(name, ((f) & 0xffff) | 0x00010000);
}


decompiled_code:
const char *__fastcall minipc_get_next_arg(const char *a1, unsigned int a2)
{
size_t v2; // rsi

if ( HIWORD(a2) == 4 )
v2 = (strlen(a1) + 4) >> 2;
else
LODWORD(v2) = ((unsigned int)(unsigned __int16)a2 + 3) >> 2;
return &a1[4 * (int)v2];
}

canonical_decompiled_code:
const char *__fastcall func7(const char *a1, unsigned int a2)
{
size_t v2; // rsi

if ( HIWORD(a2) == 4 )
v2 = (strlen(a1) + 4) >> 2;
else
LODWORD(v2) = ((unsigned int)(unsigned __int16)a2 + 3) >> 2;
return &a1[4 * (int)v2];
}

original_code:
uint32_t *minipc_get_next_arg(uint32_t arg[], uint32_t atype)
{
 int asize;
 char *s = (void *)arg;
 if (((atype) >> 16) != MINIPC_ATYPE_STRING)
  asize = (((((atype) & 0xffff)) + 3) >> 2);
 else
  asize = (((strlen(s) + 1) + 3) >> 2);
 return arg + asize;
}

canonical_original_code:
unsigned int *minipc_get_next_arg(unsigned int arg[], unsigned int atype)
{
 int asize;
 char *s = (void *)arg;
 if (((atype) >> 16) != MINIPC_ATYPE_STRING)
  asize = (((((atype) & 0xffff)) + 3) >> 2);
 else
  asize = (((strlen(s) + 1) + 3) >> 2);
 return arg + asize;
}

variable_types:
arg
unsigned int []

variable_types:
atype
unsigned int

variable_types: asize int

return_type: unsigned int *


NP

decompiled_code:
__int64 __fastcall NP(__int64 a1)
{
return a1 + 496;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return a1 + 496;
}

original_code:
static inline struct pp_net_path *NP(struct pp_instance *ppi)
{
 return &ppi->np;
}

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

return_type: 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; } *

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


decompiled_code:
__int64 __fastcall run_all_state_machines(__int64 a1)
{
__int64 v1; // r12
unsigned int v2; // ebx
int v3; // ebp
signed int v4; // eax

if ( *(int *)(a1 + 64) <= 0 )
return 0;
v1 = 0LL;
v2 = 0;
v3 = 0;
do
{
v4 = pp_state_machine(*(_QWORD *)a1 + v1, 0LL, 0LL);
if ( !v3 )
v2 = v4;
if ( (int)v2 > v4 )
v2 = v4;
++v3;
v1 += 1440LL;
}
while ( *(_DWORD *)(a1 + 64) > v3 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // r12
unsigned int v2; // ebx
int v3; // ebp
signed int v4; // eax

if ( *(int *)(a1 + 64) <= 0 )
return 0;
v1 = 0LL;
v2 = 0;
v3 = 0;
do
{
v4 = pp_state_machine(*(_QWORD *)a1 + v1, 0LL, 0LL);
if ( !v3 )
v2 = v4;
if ( (int)v2 > v4 )
v2 = v4;
++v3;
v1 += 1440LL;
}
while ( *(_DWORD *)(a1 + 64) > v3 );
return v2;
}

original_code:
static int run_all_state_machines(struct pp_globals *ppg)
{
 int j;
 int delay_ms = 0, delay_ms_j;
 for (j = 0; j < ppg->nlinks; j++) {
  struct pp_instance *ppi = &ppg->pp_instances[j];
  delay_ms_j = pp_state_machine(ppi, ((void *)0), 0);
  if (j == 0)
   delay_ms = delay_ms_j;
  if (delay_ms_j < delay_ms)
   delay_ms = delay_ms_j;
 }
 return delay_ms;
}

canonical_original_code:
static int run_all_state_machines(struct pp_globals *ppg)
{
 int j;
 int delay_ms = 0; int delay_ms_j;
 for (j = 0; j < ppg->nlinks; j++) {
  struct pp_instance *ppi = &ppg->pp_instances[j];
  delay_ms_j = pp_state_machine(ppi, ((void *)0), 0);
  if (j == 0)
   delay_ms = delay_ms_j;
  if (delay_ms_j < delay_ms)
   delay_ms = delay_ms_j;
 }
 return delay_ms;
}

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: j int

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


original_code:
void unix_main_loop(struct pp_globals *ppg)
{
 struct pp_instance *ppi;
 int delay_ms;
 int j;
 for (j = 0; j < ppg->nlinks; j++) {
  ppi = &ppg->pp_instances[j];
  if (ppi->ethernet_mode)
   NP(ppi)->ptp_offset = 14;
  ppi->is_new_state = 1;
 }
 delay_ms = run_all_state_machines(ppg);
 while (1) {
  int i;
  if (ppg->ebest_updated) {
   for (j = 0; j < ppg->nlinks; j++) {
    int new_state;
    struct pp_instance *ppi = &ppg->pp_instances[j];
    new_state = bmc(ppi);
    if (new_state != ppi->state) {
     ppi->state = new_state;
     ppi->is_new_state = 1;
    }
   }
   ppg->ebest_updated = 0;
  }
  i = unix_net_check_pkt(ppg, delay_ms);
  if (i < 0)
   continue;
  if (i == 0) {
   delay_ms = run_all_state_machines(ppg);
   continue;
  }
  delay_ms = -1;
  for (j = 0; j < ppg->nlinks; j++) {
   int tmp_d;
   ppi = &ppg->pp_instances[j];
   if ((NP(ppi)->ch[PP_NP_GEN].pkt_present) ||
       (NP(ppi)->ch[PP_NP_EVT].pkt_present)) {
    i = ppi->n_ops->recv(ppi, ppi->rx_frame,
      200 - 4,
      &ppi->last_rcv_time);
    ppi->last_rcv_time.seconds
     += DSPRO(ppi)->currentUtcOffset;
    if (i < 44) {
     ({ if (1) __pp_diag(ppi, pp_dt_frames, 1, "Error or short frame: " "%d < %d\n", i, 44); 1; });
     continue;
    }
    tmp_d = pp_state_machine(ppi, ppi->rx_ptp,
     i - NP(ppi)->ptp_offset);
    if ((delay_ms == -1) || (tmp_d < delay_ms))
     delay_ms = tmp_d;
   }
  }
 }
}

variable_types: i int

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 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_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 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 ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; }

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

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


original_code:
int get_stamp(struct timespec ts[4])
{
 if (__stamp_errno) {
  (*__errno_location ()) = __stamp_errno;
  memset(ts, 0, 4 * sizeof(ts[0]));
  return -1;
 }
 ts[0] = __stamp_ns;
 memcpy(ts + 1, __stamp_hw_info, sizeof(__stamp_hw_info));
 return 0;
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } [4]


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

v2 = pp_vsprintf(print_buf, a1, a2);
puts(print_buf);
return v2;
}

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

v2 = pp_vsprintf(print_buf, a1, a2);
puts(print_buf);
return v2;
}

original_code:
int pp_vprintf(const char *fmt, va_list args)
{
 int ret;
 ret = pp_vsprintf(print_buf, fmt, args);
 puts(print_buf);
 return ret;
}

canonical_original_code:
int pp_vprintf(char *fmt, __builtin_va_list args)
{
 int ret;
 ret = pp_vsprintf(print_buf, fmt, args);
 puts(print_buf);
 return ret;
}

variable_types:
fmt
char *


decompiled_code:
__int64 pp_sprintf(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return pp_vsprintf(a1, a2, va);
}

canonical_decompiled_code:
__int64 func1(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return pp_vsprintf(a1, a2, va);
}

original_code:
int pp_sprintf(char *s, const char *fmt, ...)
{
 va_list args;
 int ret;
 __builtin_va_start(args,fmt);
 ret = pp_vsprintf(s, fmt, args);
 __builtin_va_end(args);
 return ret;
}

canonical_original_code:
int pp_sprintf(char *s, char *fmt, ...)
{
 __builtin_va_list args;
 int ret;
 __builtin_va_start(args,fmt);
 ret = pp_vsprintf(s, fmt, args);
 __builtin_va_end(args);
 return ret;
}

variable_types:
s
char *


decompiled_code:
__int64 pp_printf(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return pp_vprintf(a1, (__int64)va);
}

canonical_decompiled_code:
__int64 func2(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return func0(a1, (__int64)va);
}

original_code:
int pp_printf(const char *fmt, ...)
{
 va_list args;
 int ret;
 __builtin_va_start(args,fmt);
 ret = pp_vprintf(fmt, args);
 __builtin_va_end(args);
 return ret;
}

canonical_original_code:
int pp_printf(char *fmt, ...)
{
 __builtin_va_list args;
 int ret;
 __builtin_va_start(args,fmt);
 ret = pp_vprintf(fmt, args);
 __builtin_va_end(args);
 return ret;
}

variable_types: ret int


decompiled_code:
unsigned __int64 __fastcall pp_diag_parse(const char *a1)
{
char v1; // al
int v2; // ecx
unsigned __int64 v3; // rbx

v1 = *a1;
if ( !*a1 )
return 0LL;
v2 = 28;
v3 = 0LL;
do
{
if ( (unsigned __int8)(v1 - 48) > 3u )
break;
v3 |= (v1 - 48) << v2;
v2 -= 4;
v1 = *++a1;
if ( v2 <= 3 )
break;
}
while ( v1 );
if ( v1 )
pp_printf("%s: error parsing \"%s\"\n", "pp_diag_parse", a1);
return v3;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(const char *a1)
{
char v1; // al
int v2; // ecx
unsigned __int64 v3; // rbx

v1 = *a1;
if ( !*a1 )
return 0LL;
v2 = 28;
v3 = 0LL;
do
{
if ( (unsigned __int8)(v1 - 48) > 3u )
break;
v3 |= (v1 - 48) << v2;
v2 -= 4;
v1 = *++a1;
if ( v2 <= 3 )
break;
}
while ( v1 );
if ( v1 )
pp_printf("%s: error parsing \"%s\"\n", "pp_diag_parse", a1);
return v3;
}

original_code:
unsigned long pp_diag_parse(char *diaglevel)
{
 unsigned long res = 0;
 int i = 28;
 while (*diaglevel && i >= 4) {
  if (*diaglevel < '0' || *diaglevel > '3')
   break;
  res |= ((*diaglevel - '0') << i);
  i -= 4;
  diaglevel++;
 }
 if (*diaglevel)
  pp_printf("%s: error parsing \"%s\"\n", __func__, diaglevel);
 return res;
}

variable_types:
diaglevel
char *

variable_types:
res
unsigned long

return_type: unsigned long


canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
*(_QWORD *)(a1 + 8LL * a2 + 1248) = 0LL;
}

canonical_original_code:
static inline void pp_timeout_clr(struct pp_instance *ppi, int index)
{
 ppi->timeouts[index] = 0;
}

variable_types: index int

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 pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

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

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


canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 v4; // rbx

v2 = *(_QWORD *)(a1 + 8LL * a2 + 1248);
result = 0LL;
if ( v2 )
{
v4 = v2 - (*(__int64 (__fastcall **)(__int64, _QWORD))(*(_QWORD *)(a1 + 56) + 40LL))(a1, 0LL);
if ( v4 < 0 )
LODWORD(v4) = 0;
result = (unsigned int)v4;
}
return result;
}

variable_types: ret long

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_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 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 Timestamp { struct UInteger48 secondsField; unsigned int nanosecondsField; }

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


decompiled_code:
__int64 __fastcall pp_timeout_restart_annrec(__int64 a1)
{
__int64 v1; // rax

v1 = DSPOR(a1);
return pp_timeout_set(a1, 2, 1000 * (*(unsigned __int8 *)(v1 + 12) << *(_BYTE *)(v1 + 11)));
}

original_code:
static inline void pp_timeout_restart_annrec(struct pp_instance *ppi)
{
 pp_timeout_set(ppi, PP_TO_ANN_RECEIPT,
         ((DSPOR(ppi)->announceReceiptTimeout) <<
   DSPOR(ppi)->logAnnounceInterval) * 1000);
}

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 pp_frgn_master { struct PortIdentity port_id; struct MsgAnnounce ann; struct MsgHeader hdr; }

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

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

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 UInteger48 { unsigned int lsb; unsigned short msb; }


decompiled_code:
__int64 __fastcall pp_slave(void *s1, void *s2, size_t n)
{
unsigned int v3; // ebp
unsigned __int8 v4; // al
int v5; // eax
int (__fastcall *v6)(const void *, const void *, size_t); // rax
__int64 v7; // rax
_WORD *v8; // rbp
int v9; // ebp
int v10; // eax
int v11; // ebp
unsigned int (__fastcall *v13)(void *); // rax
__int64 v14; // rax
__int64 v15; // rax
_BYTE v16[12]; // [rsp+0h] [rbp-38h] BYREF
char s2a[8]; // [rsp+Ch] [rbp-2Ch] BYREF
__int16 v18; // [rsp+14h] [rbp-24h]

v3 = n;
if ( *((_DWORD *)s1 + 3) )
{
((void (*)(void))pp_init_clock)();
v6 = (int (__fastcall *)(const void *, const void *, size_t))pp_hooks[5];
if ( v6 && v6(s1, s2, v3) )
goto LABEL_14;
*((_BYTE *)s1 + 1416) &= 0xFDu;
pp_timeout_restart_annrec((__int64)s1);
v7 = DSPOR((__int64)s1);
pp_timeout_rand(s1, 0LL, (unsigned int)*(char *)(v7 + 10));
}
if ( !v3 )
goto LABEL_21;
v4 = *((_BYTE *)s1 + 1345);
if ( v4 != 9 )
{
if ( v4 > 9u )
{
if ( v4 != 11 )
goto LABEL_21;
v5 = st_com_slave_handle_announce(s1, s2, v3);
}
else if ( v4 )
{
if ( v4 != 8 )
goto LABEL_21;
v5 = st_com_slave_handle_followup(s1, s2, v3);
}
else
{
v5 = st_com_slave_handle_sync(s1, s2, v3);
}
if ( v5 )
goto LABEL_14;
LABEL_21:
v9 = st_com_execute_slave(s1);
if ( !(unsigned int)pp_timeout_z((__int64)s1, 0) )
goto LABEL_22;
goto LABEL_37;
}
if ( (int)v3 > 53 )
{
msg_unpack_delay_resp(s2, v16);
v8 = (_WORD *)DSPOR((__int64)s1);
if ( memcmp(v8, s2a, 8uLL)
|| *((_WORD *)s1 + 659) != *((_WORD *)s1 + 687)
|| v8[4] != v18
|| (*((_BYTE *)s1 + 1416) & 1) == 0 )
{
_pp_diag(s1);
goto LABEL_21;
}
to_TimeInternal((char *)s1 + 628, v16);
v13 = (unsigned int (__fastcall *)(void *))pp_hooks[6];
if ( !v13 )
{
pp_update_delay(s1, (char *)s1 + 648);
goto LABEL_35;
}
if ( !v13(s1) )
{
LABEL_35:
*((_BYTE *)s1 + 1314) = *((_BYTE *)s1 + 1377);
goto LABEL_21;
}
}
LABEL_14:
if ( !(unsigned int)pp_timeout_z((__int64)s1, 0) )
{
LABEL_15:
*((_DWORD *)s1 + 1) = 2;
return 0LL;
}
LABEL_37:
v9 = msg_issue_delay_req(s1);
*((__m128i *)s1 + 38) = _mm_loadu_si128((const __m128i *)s1 + 43);
*((_DWORD *)s1 + 156) = *((_DWORD *)s1 + 176);
v14 = DSPOR((__int64)s1);
pp_timeout_rand(s1, 0LL, (unsigned int)*(char *)(v14 + 10));
v15 = OPTS((__int64)s1);
add_TimeInternal((char *)s1 + 608, (char *)s1 + 608, v15 + 24);
LABEL_22:
if ( v9 )
goto LABEL_15;
if ( *((_DWORD *)s1 + 1) != *(_DWORD *)s1 )
{
pp_timeout_clr((__int64)s1, 2);
pp_timeout_clr((__int64)s1, 0);
pp_init_clock(s1);
}
v10 = pp_ms_to_timeout((__int64)s1, 2);
v11 = v10;
if ( *((_QWORD *)s1 + 156) )
v10 = pp_ms_to_timeout((__int64)s1, 0);
if ( v10 > v11 )
v10 = v11;
*((_DWORD *)s1 + 2) = v10;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *s1, void *s2, size_t n)
{
unsigned int v3; // ebp
unsigned __int8 v4; // al
int v5; // eax
int (__fastcall *v6)(const void *, const void *, size_t); // rax
__int64 v7; // rax
_WORD *v8; // rbp
int v9; // ebp
int v10; // eax
int v11; // ebp
unsigned int (__fastcall *v13)(void *); // rax
__int64 v14; // rax
__int64 v15; // rax
_BYTE v16[12]; // [rsp+0h] [rbp-38h] BYREF
char s2a[8]; // [rsp+Ch] [rbp-2Ch] BYREF
__int16 v18; // [rsp+14h] [rbp-24h]

v3 = n;
if ( *((_DWORD *)s1 + 3) )
{
((void (*)(void))pp_init_clock)();
v6 = (int (__fastcall *)(const void *, const void *, size_t))pp_hooks[5];
if ( v6 && v6(s1, s2, v3) )
goto LABEL_14;
*((_BYTE *)s1 + 1416) &= 0xFDu;
func7((__int64)s1);
v7 = func3((__int64)s1);
pp_timeout_rand(s1, 0LL, (unsigned int)*(char *)(v7 + 10));
}
if ( !v3 )
goto LABEL_21;
v4 = *((_BYTE *)s1 + 1345);
if ( v4 != 9 )
{
if ( v4 > 9u )
{
if ( v4 != 11 )
goto LABEL_21;
v5 = st_com_slave_handle_announce(s1, s2, v3);
}
else if ( v4 )
{
if ( v4 != 8 )
goto LABEL_21;
v5 = st_com_slave_handle_followup(s1, s2, v3);
}
else
{
v5 = st_com_slave_handle_sync(s1, s2, v3);
}
if ( v5 )
goto LABEL_14;
LABEL_21:
v9 = st_com_execute_slave(s1);
if ( !(unsigned int)func9((__int64)s1, 0) )
goto LABEL_22;
goto LABEL_37;
}
if ( (int)v3 > 53 )
{
msg_unpack_delay_resp(s2, v16);
v8 = (_WORD *)func3((__int64)s1);
if ( memcmp(v8, s2a, 8uLL)
|| *((_WORD *)s1 + 659) != *((_WORD *)s1 + 687)
|| v8[4] != v18
|| (*((_BYTE *)s1 + 1416) & 1) == 0 )
{
_pp_diag(s1);
goto LABEL_21;
}
to_TimeInternal((char *)s1 + 628, v16);
v13 = (unsigned int (__fastcall *)(void *))pp_hooks[6];
if ( !v13 )
{
pp_update_delay(s1, (char *)s1 + 648);
goto LABEL_35;
}
if ( !v13(s1) )
{
LABEL_35:
*((_BYTE *)s1 + 1314) = *((_BYTE *)s1 + 1377);
goto LABEL_21;
}
}
LABEL_14:
if ( !(unsigned int)func9((__int64)s1, 0) )
{
LABEL_15:
*((_DWORD *)s1 + 1) = 2;
return 0LL;
}
LABEL_37:
v9 = msg_issue_delay_req(s1);
*((__m128i *)s1 + 38) = _mm_loadu_si128((const __m128i *)s1 + 43);
*((_DWORD *)s1 + 156) = *((_DWORD *)s1 + 176);
v14 = func3((__int64)s1);
pp_timeout_rand(s1, 0LL, (unsigned int)*(char *)(v14 + 10));
v15 = func2((__int64)s1);
add_TimeInternal((char *)s1 + 608, (char *)s1 + 608, v15 + 24);
LABEL_22:
if ( v9 )
goto LABEL_15;
if ( *((_DWORD *)s1 + 1) != *(_DWORD *)s1 )
{
func5((__int64)s1, 2);
func5((__int64)s1, 0);
pp_init_clock(s1);
}
v10 = func6((__int64)s1, 2);
v11 = v10;
if ( *((_QWORD *)s1 + 156) )
v10 = func6((__int64)s1, 0);
if ( v10 > v11 )
v10 = v11;
*((_DWORD *)s1 + 2) = v10;
return 0LL;
}

original_code:
int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 int e = 0;
 MsgHeader *hdr = &ppi->received_ptp_header;
 MsgDelayResp resp;
 int d1, d2;
 if (ppi->is_new_state) {
  pp_init_clock(ppi);
  if (pp_hooks.new_slave)
   e = pp_hooks.new_slave(ppi, pkt, plen);
  if (e)
   goto out;
  ppi->waiting_for_follow = FALSE;
  pp_timeout_restart_annrec(ppi);
  pp_timeout_rand(ppi, PP_TO_DELAYREQ,
    DSPOR(ppi)->logMinDelayReqInterval);
 }
 if (plen == 0)
  goto out;
 switch (hdr->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;
 case PPM_DELAY_REQ:
  break;
 case PPM_DELAY_RESP:
  e = (plen < 54);
  if (e)
   break;
  msg_unpack_delay_resp(pkt, &resp);
  if ((memcmp(&DSPOR(ppi)->portIdentity.clockIdentity,
   &resp.requestingPortIdentity.clockIdentity,
   sizeof(ClockIdentity)) == 0) &&
   ((ppi->sent_seq[PPM_DELAY_REQ]) ==
    hdr->sequenceId) &&
   (DSPOR(ppi)->portIdentity.portNumber ==
   resp.requestingPortIdentity.portNumber)
   && ppi->is_from_cur_par) {
   to_TimeInternal(&ppi->t4, &resp.receiveTimestamp);
   if (pp_hooks.update_delay)
    e = pp_hooks.update_delay(ppi);
   else
    pp_update_delay(ppi, &ppi->cField);
   if (e)
    goto out;
   ppi->log_min_delay_req_interval =
    hdr->logMessageInterval;
  } else {
   ({ if (1) __pp_diag(ppi, pp_dt_frames, 2, "pp_slave : " "Delay Resp doesn't match Delay Req\n"); 1; });
  }
  break;
 default:
  break;
 }
out:
 if (e == 0)
  e = st_com_execute_slave(ppi);
 if (pp_timeout_z(ppi, PP_TO_DELAYREQ)) {
  e = msg_issue_delay_req(ppi);
  ppi->t3 = ppi->last_snt_time;
  pp_timeout_rand(ppi, PP_TO_DELAYREQ,
    DSPOR(ppi)->logMinDelayReqInterval);
  add_TimeInternal(&ppi->t3,
     &ppi->t3,
     &OPTS(ppi)->outbound_latency);
 }
 if (e) {
  ppi->next_state = PPS_FAULTY;
  return 0;
 }
 if (ppi->next_state != ppi->state) {
  pp_timeout_clr(ppi, PP_TO_ANN_RECEIPT);
  pp_timeout_clr(ppi, PP_TO_DELAYREQ);
  pp_init_clock(ppi);
 }
 d1 = d2 = pp_ms_to_timeout(ppi, PP_TO_ANN_RECEIPT);
 if (ppi->timeouts[PP_TO_DELAYREQ])
  d2 = pp_ms_to_timeout(ppi, PP_TO_DELAYREQ);
 ppi->next_delay = d1 < d2 ? d1 : d2;
 return 0;
}

canonical_original_code:
int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 int e = 0;
 struct MsgHeader *hdr = &ppi->received_ptp_header;
 struct MsgDelayResp resp;
 int d1; int d2;
 if (ppi->is_new_state) {
  pp_init_clock(ppi);
  if (pp_hooks.new_slave)
   e = pp_hooks.new_slave(ppi, pkt, plen);
  if (e)
   goto out;
  ppi->waiting_for_follow = FALSE;
  pp_timeout_restart_annrec(ppi);
  pp_timeout_rand(ppi, PP_TO_DELAYREQ,
    DSPOR(ppi)->logMinDelayReqInterval);
 }
 if (plen == 0)
  goto out;
 switch (hdr->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;
 case PPM_DELAY_REQ:
  break;
 case PPM_DELAY_RESP:
  e = (plen < 54);
  if (e)
   break;
  msg_unpack_delay_resp(pkt, &resp);
  if ((memcmp(&DSPOR(ppi)->portIdentity.clockIdentity,
   &resp.requestingPortIdentity.clockIdentity,
   sizeof(ClockIdentity)) == 0) &&
   ((ppi->sent_seq[PPM_DELAY_REQ]) ==
    hdr->sequenceId) &&
   (DSPOR(ppi)->portIdentity.portNumber ==
   resp.requestingPortIdentity.portNumber)
   && ppi->is_from_cur_par) {
   to_TimeInternal(&ppi->t4, &resp.receiveTimestamp);
   if (pp_hooks.update_delay)
    e = pp_hooks.update_delay(ppi);
   else
    pp_update_delay(ppi, &ppi->cField);
   if (e)
    goto out;
   ppi->log_min_delay_req_interval =
    hdr->logMessageInterval;
  } else {
   ({ if (1) __pp_diag(ppi, pp_dt_frames, 2, "pp_slave : " "Delay Resp doesn't match Delay Req\n"); 1; });
  }
  break;
 default:
  break;
 }
out:
 if (e == 0)
  e = st_com_execute_slave(ppi);
 if (pp_timeout_z(ppi, PP_TO_DELAYREQ)) {
  e = msg_issue_delay_req(ppi);
  ppi->t3 = ppi->last_snt_time;
  pp_timeout_rand(ppi, PP_TO_DELAYREQ,
    DSPOR(ppi)->logMinDelayReqInterval);
  add_TimeInternal(&ppi->t3,
     &ppi->t3,
     &OPTS(ppi)->outbound_latency);
 }
 if (e) {
  ppi->next_state = PPS_FAULTY;
  return 0;
 }
 if (ppi->next_state != ppi->state) {
  pp_timeout_clr(ppi, PP_TO_ANN_RECEIPT);
  pp_timeout_clr(ppi, PP_TO_DELAYREQ);
  pp_init_clock(ppi);
 }
 d1 = d2 = pp_ms_to_timeout(ppi, PP_TO_ANN_RECEIPT);
 if (ppi->timeouts[PP_TO_DELAYREQ])
  d2 = pp_ms_to_timeout(ppi, PP_TO_DELAYREQ);
 ppi->next_delay = d1 < d2 ? d1 : d2;
 return 0;
}

variable_types:
pkt
unsigned char *

variable_types: plen int

variable_types: e int

variable_types: 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 { struct ClockIdentity { unsigned char id[8]; } clockIdentity; unsigned short portNumber; } sourcePortIdentity; unsigned short sequenceId; unsigned char controlField; char logMessageInterval; } *

variable_types: struct MsgDelayResp { struct Timestamp { struct UInteger48 { unsigned int lsb; unsigned short msb; } secondsField; unsigned int nanosecondsField; } receiveTimestamp; struct PortIdentity { struct ClockIdentity { unsigned char id[8]; } clockIdentity; unsigned short portNumber; } requestingPortIdentity; }

user_defined_types: struct MsgDelayResp { struct Timestamp receiveTimestamp; struct PortIdentity requestingPortIdentity; }

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 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 PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }

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_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 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:
__syscall_slong_t __fastcall get_diff(struct timespec *tp)
{
struct timespec v2; // [rsp+0h] [rbp-38h] BYREF
struct timespec tpa; // [rsp+10h] [rbp-28h] BYREF
struct sched_param param; // [rsp+2Ch] [rbp-Ch] BYREF

param.__sched_priority = 1;
sched_setscheduler(0, 1, &param);
clock_gettime(0, &tpa);
clock_gettime(1, tp);
clock_gettime(0, &v2);
sched_setscheduler(0, 0, 0LL);
return tpa.tv_nsec
+ 1000000000 * tpa.tv_sec
+ (v2.tv_nsec + 1000000000 * v2.tv_sec - (tpa.tv_nsec + 1000000000 * tpa.tv_sec)) / 2
- (tp->tv_nsec
+ 1000000000 * tp->tv_sec);
}

canonical_decompiled_code:
__syscall_slong_t __fastcall func7(struct timespec *tp)
{
struct timespec v2; // [rsp+0h] [rbp-38h] BYREF
struct timespec tpa; // [rsp+10h] [rbp-28h] BYREF
struct sched_param param; // [rsp+2Ch] [rbp-Ch] BYREF

param.__sched_priority = 1;
sched_setscheduler(0, 1, &param);
clock_gettime(0, &tpa);
clock_gettime(1, tp);
clock_gettime(0, &v2);
sched_setscheduler(0, 0, 0LL);
return tpa.tv_nsec
+ 1000000000 * tpa.tv_sec
+ (v2.tv_nsec + 1000000000 * v2.tv_sec - (tpa.tv_nsec + 1000000000 * tpa.tv_sec)) / 2
- (tp->tv_nsec
+ 1000000000 * tp->tv_sec);
}

original_code:
static int64_t get_diff(struct timespec *ts1p)
{
 struct sched_param prio;
 struct timespec ts2, ts3;
 int64_t d1, d2, d3;
 prio.sched_priority = 1;
 if (sched_setscheduler(0, 1, &prio)<0) {
 }
 clock_gettime(0, &ts2);
 clock_gettime(1, ts1p);
 clock_gettime(0, &ts3);
 if (sched_setscheduler(0, 0, ((void *)0))<0) {
 }
 d1 = ts1p->tv_sec * 1000LL * 1000 * 1000 + ts1p->tv_nsec;
 d2 = ts2.tv_sec * 1000LL * 1000 * 1000 + ts2.tv_nsec;
 d3 = ts3.tv_sec * 1000LL * 1000 * 1000 + ts3.tv_nsec;
 d2 += (d3 - d2) / 2;
 return d2 - d1;
}

canonical_original_code:
static long get_diff(struct timespec *ts1p)
{
 struct sched_param prio;
 struct timespec ts2; struct timespec ts3;
 long d1; long d2; long d3;
 prio.sched_priority = 1;
 if (sched_setscheduler(0, 1, &prio)<0) {
 }
 clock_gettime(0, &ts2);
 clock_gettime(1, ts1p);
 clock_gettime(0, &ts3);
 if (sched_setscheduler(0, 0, ((void *)0))<0) {
 }
 d1 = ts1p->tv_sec * 1000LL * 1000 * 1000 + ts1p->tv_nsec;
 d2 = ts2.tv_sec * 1000LL * 1000 * 1000 + ts2.tv_nsec;
 d3 = ts3.tv_sec * 1000LL * 1000 * 1000 + ts3.tv_nsec;
 d2 += (d3 - d2) / 2;
 return d2 - d1;
}

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

variable_types: struct sched_param { int sched_priority; }


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
__syscall_slong_t v4; // r15
__syscall_slong_t v5; // rbp
__int64 v6; // r14
__syscall_slong_t v7; // r12
__time_t v8; // rdx
__syscall_slong_t v9; // rbx
struct timeval v10; // [rsp+0h] [rbp-B8h] BYREF
char s[32]; // [rsp+10h] [rbp-A8h] BYREF
struct tm tp; // [rsp+30h] [rbp-88h] BYREF
struct timespec v13; // [rsp+70h] [rbp-48h] BYREF

v3 = 10;
if ( argc > 1 )
{
v3 = atoi(argv[1]);
if ( !v3 )
v3 = 10;
}
fprintf(stderr, "%s: looping every %i millisecs\n", *argv, v3);
v4 = get_diff(&v13);
v5 = v13.tv_nsec / 1000 + 1000000 * v13.tv_sec;
v6 = (int)(1000 * v3);
v7 = 0LL;
while ( 1 )
{
v9 = get_diff(&v13) - v4;
if ( v7 + 500000 < v9 || v7 - 500000 > v9 )
{
gettimeofday(&v10, 0LL);
localtime_r(&v10.tv_sec, &tp);
strftime(s, 0x20uLL, "%y-%m-%d-%H:%M:%S", &tp);
printf("%s: %10lli us\n", s, v9 / 1000);
fflush(stdout);
v7 = v9;
}
v5 += v6;
v8 = 1000000 * v13.tv_sec + v13.tv_nsec / 1000;
if ( v5 > v8 )
usleep(v5 - v8);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
__syscall_slong_t v4; // r15
__syscall_slong_t v5; // rbp
__int64 v6; // r14
__syscall_slong_t v7; // r12
__time_t v8; // rdx
__syscall_slong_t v9; // rbx
struct timeval v10; // [rsp+0h] [rbp-B8h] BYREF
char s[32]; // [rsp+10h] [rbp-A8h] BYREF
struct tm tp; // [rsp+30h] [rbp-88h] BYREF
struct timespec v13; // [rsp+70h] [rbp-48h] BYREF

v3 = 10;
if ( argc > 1 )
{
v3 = atoi(argv[1]);
if ( !v3 )
v3 = 10;
}
fprintf(stderr, "%s: looping every %i millisecs\n", *argv, v3);
v4 = func7(&v13);
v5 = v13.tv_nsec / 1000 + 1000000 * v13.tv_sec;
v6 = (int)(1000 * v3);
v7 = 0LL;
while ( 1 )
{
v9 = func7(&v13) - v4;
if ( v7 + 500000 < v9 || v7 - 500000 > v9 )
{
gettimeofday(&v10, 0LL);
localtime_r(&v10.tv_sec, &tp);
strftime(s, 0x20uLL, "%y-%m-%d-%H:%M:%S", &tp);
printf("%s: %10lli us\n", s, v9 / 1000);
fflush(stdout);
v7 = v9;
}
v5 += v6;
v8 = 1000000 * v13.tv_sec + v13.tv_nsec / 1000;
if ( v5 > v8 )
usleep(v5 - v8);
}
}

original_code:
int main(int argc, char **argv)
{
 int64_t now, diff0, diff, newdiff, prevprint = 0;
 int64_t nextloop;
 struct timespec ts;
 int msecs = 10;
 if (argc > 1) {
  msecs = atoi(argv[1]);
  if (!msecs)
   msecs = 10;
 }
 fprintf(stderr, "%s: looping every %i millisecs\n", argv[0], msecs);
 diff0 = get_diff(&ts);
 nextloop = ts.tv_sec * 1000LL * 1000 + ts.tv_nsec / 1000;
 while (1) {
  diff = get_diff(&ts);
  newdiff = diff - diff0;
  if (newdiff > prevprint + 500 * 1000
      || newdiff < prevprint - 500 * 1000) {
   struct timeval tv;
   struct tm tm;
   char s[32];
   gettimeofday(&tv, ((void *)0));
   localtime_r(&tv.tv_sec, &tm);
   strftime(s, sizeof(s), "%y-%m-%d-%H:%M:%S", &tm);
   printf("%s: %10lli us\n", s, (long long)newdiff / 1000);
   prevprint = newdiff;
   fflush(stdout);
  }
  nextloop += msecs * 1000;
  now = ts.tv_sec * 1000LL * 1000 + ts.tv_nsec / 1000;
  if (now < nextloop)
   usleep(nextloop-now);
 }
}

canonical_original_code:
int main(int argc, char **argv)
{
 long now; long diff0; long diff; long newdiff; long prevprint = 0;
 long nextloop;
 struct timespec ts;
 int msecs = 10;
 if (argc > 1) {
  msecs = atoi(argv[1]);
  if (!msecs)
   msecs = 10;
 }
 fprintf(stderr, "%s: looping every %i millisecs\n", argv[0], msecs);
 diff0 = get_diff(&ts);
 nextloop = ts.tv_sec * 1000LL * 1000 + ts.tv_nsec / 1000;
 while (1) {
  diff = get_diff(&ts);
  newdiff = diff - diff0;
  if (newdiff > prevprint + 500 * 1000
      || newdiff < prevprint - 500 * 1000) {
   struct timeval tv;
   struct tm tm;
   char s[32];
   gettimeofday(&tv, ((void *)0));
   localtime_r(&tv.tv_sec, &tm);
   strftime(s, sizeof(s), "%y-%m-%d-%H:%M:%S", &tm);
   printf("%s: %10lli us\n", s, (long long)newdiff / 1000);
   prevprint = newdiff;
   fflush(stdout);
  }
  nextloop += msecs * 1000;
  now = ts.tv_sec * 1000LL * 1000 + ts.tv_nsec / 1000;
  if (now < nextloop)
   usleep(nextloop-now);
 }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: now long

variable_types: diff long

variable_types: newdiff long

variable_types: prevprint long

variable_types: nextloop long

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

variable_types: msecs int

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

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

variable_types:
s
char [32]


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

result = _mpc_poll_usec;
if ( a1 <= 0 )
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
else
{
_mpc_poll_usec = a1;
}
return result;
}

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

result = _mpc_poll_usec;
if ( a1 <= 0 )
{
*_errno_location() = 22;
result = 0xFFFFFFFFLL;
}
else
{
_mpc_poll_usec = a1;
}
return result;
}

original_code:
int minipc_set_poll(int usec)
{
 int ret = __mpc_poll_usec;
 if (usec <= 0) {
  (*__errno_location ()) = 22;
  return -1;
 }
 __mpc_poll_usec = usec;
 return ret;
}

variable_types: usec int

variable_types: ret int