decompiled_code:
__int64 __fastcall pp_timeout_set(__int64 a1, int a2, unsigned int a3)
{
__int64 result; // rax

result = (*(__int64 (__fastcall **)(__int64, _QWORD))(*(_QWORD *)(a1 + 56) + 40LL))(a1, a3);
*(_QWORD *)(a1 + 8LL * a2 + 1248) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, unsigned int a3)
{
__int64 result; // rax

result = (*(__int64 (__fastcall **)(__int64, _QWORD))(*(_QWORD *)(a1 + 56) + 40LL))(a1, a3);
*(_QWORD *)(a1 + 8LL * a2 + 1248) = result;
return result;
}

original_code:
static inline void pp_timeout_set(struct pp_instance *ppi, int index,
      int millisec)
{
 ppi->timeouts[index] = ppi->t_ops->calc_timeout(ppi, millisec);
}

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

variable_types: millisec int

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

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

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

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


repo: tinito/ppsi-chibios

decompiled_code:
__int64 __fastcall pp_timeout_log(__int64 a1, int a2)
{
return _pp_diag(a1, 6LL, 1LL, "timeout expired: %s\n", (&timeout_names)[a2]);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
return _pp_diag(a1, 6LL, 1LL, "timeout expired: %s\n", (&timeout_names)[a2]);
}

original_code:
void pp_timeout_log(struct pp_instance *ppi, int index)
{
 ({ if (1) __pp_diag(ppi, pp_dt_time, 1, "timeout expired: %s\n", timeout_names[index]); 1; });
}

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_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 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 DSCurrent { unsigned short stepsRemoved; struct TimeInternal offsetFromMaster; struct TimeInternal meanPathDelay; unsigned short primarySlavePortNumber; }

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

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

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

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

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

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

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

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

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


original_code:
void pp_timeout_rand(struct pp_instance *ppi, int index, int logval)
{
 static uint32_t seed;
 uint32_t rval;
 int millisec;
 if (!seed) {
  uint32_t *p;
  p = (void *)(&DSDEF(ppi)->clockIdentity)
   + sizeof(ClockIdentity) - 4;
  seed = *p;
 }
 seed *= 1103515245;
 seed += 12345;
 rval = (unsigned int) (seed / 65536) % 2048;
 seed *= 1103515245;
 seed += 12345;
 rval <<= 10;
 rval ^= (unsigned int) (seed / 65536) % 1024;
 millisec = (1 << logval) * 400;
 millisec = (millisec * 2) + rval % millisec;
 pp_timeout_set(ppi, index, millisec);
}

canonical_original_code:
void pp_timeout_rand(struct pp_instance *ppi, int index, int logval)
{
 static unsigned int seed;
 unsigned int rval;
 int millisec;
 if (!seed) {
  unsigned int *p;
  p = (void *)(&DSDEF(ppi)->clockIdentity)
   + sizeof(ClockIdentity) - 4;
  seed = *p;
 }
 seed *= 1103515245;
 seed += 12345;
 rval = (unsigned int) (seed / 65536) % 2048;
 seed *= 1103515245;
 seed += 12345;
 rval <<= 10;
 rval ^= (unsigned int) (seed / 65536) % 1024;
 millisec = (1 << logval) * 400;
 millisec = (millisec * 2) + rval % millisec;
 pp_timeout_set(ppi, index, millisec);
}

variable_types: logval int

variable_types:
seed
unsigned int

variable_types:
rval
unsigned int

variable_types:
p
unsigned 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 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 DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

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

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


original_code:
void print_spaces(struct TimeInternal *ti)
{
 static struct TimeInternal prev_ti;
 int i, diffms;
 if (prev_ti.seconds) {
  diffms = (ti->seconds - prev_ti.seconds) * 1000
   + (ti->nanoseconds / 1000 / 1000)
   - (prev_ti.nanoseconds / 1000 / 1000);
  for (i = 250; i < 2500 && i < diffms; i += 250)
   printf("\n");
  printf("TIMEDELTA: %i ms\n", diffms);
 }
 prev_ti = *ti;
}

canonical_original_code:
void print_spaces(struct TimeInternal *ti)
{
 static struct TimeInternal prev_ti;
 int i; int diffms;
 if (prev_ti.seconds) {
  diffms = (ti->seconds - prev_ti.seconds) * 1000
   + (ti->nanoseconds / 1000 / 1000)
   - (prev_ti.nanoseconds / 1000 / 1000);
  for (i = 250; i < 2500 && i < diffms; i += 250)
   printf("\n");
  printf("TIMEDELTA: %i ms\n", diffms);
 }
 prev_ti = *ti;
}

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

variable_types: i int

variable_types: diffms int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // ax
int v4; // eax
int v5; // ebp
const char *v6; // r13
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int v13; // eax
__int16 v14; // ax
unsigned int v15; // er12
int v16; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-68Ch] BYREF
struct timeval tv; // [rsp+10h] [rbp-688h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-678h] BYREF
__m128i v20[2]; // [rsp+30h] [rbp-668h] BYREF
__int16 s1[748]; // [rsp+50h] [rbp-648h] BYREF
int v22; // [rsp+628h] [rbp-70h]
char dest[8]; // [rsp+630h] [rbp-68h] BYREF
__int64 v24; // [rsp+638h] [rbp-60h]
__int64 v25; // [rsp+640h] [rbp-58h]
__int64 v26; // [rsp+648h] [rbp-50h]
__int64 v27; // [rsp+650h] [rbp-48h]
__int64 optval[7]; // [rsp+660h] [rbp-38h] BYREF

v3 = _bswap_16(3);
v4 = socket(17, 3, v3);
if ( v4 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "%s: socket(): %s\n", *argv, v10);
exit(1);
}
v5 = v4;
v6 = "eth0";
if ( argc > 1 )
v6 = argv[1];
*(_QWORD *)dest = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
strcpy(dest, v6);
if ( ioctl(v5, 0x8933uLL, dest) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
fprintf(stderr, "%s: ioctl(GETIFINDEX(%s)): %s\n", *argv, v6, v12);
exit(1);
}
fprintf(stderr, "Dumping PTP packets from interface \"%s\"\n", v6);
optval[1] = 0LL;
optval[0] = (unsigned int)v25 | 0x100000000LL;
if ( setsockopt(3, 263, 1, optval, 0x10u) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "%s: set promiscuous(%s): %s\n", *argv, v6, v8);
exit(1);
}
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v16 = recvfrom(v5, s1, 0x5DCuLL, 32, &addr, &addr_len);
gettimeofday(&tv, 0LL);
v20[0].m128i_i32[0] = tv.tv_sec;
v20[0].m128i_i32[1] = 1000 * LODWORD(tv.tv_usec);
if ( (unsigned int)v16 > 0x5DC )
break;
v15 = v16;
if ( prevlen_1 != v16 || memcmp(s1, prev_0, v16) )
{
memcpy(prev_0, s1, v16);
prevlen_1 = v16;
if ( v16 > 13 )
goto LABEL_23;
}
}
v15 = prevlen_1;
if ( prevlen_1 == 1500 )
{
if ( memcmp(s1, prev_0, 0x5DCuLL) )
{
qmemcpy(prev_0, s1, 0x5D8uLL);
prev_0[374] = v22;
prevlen_1 = 1500;
LABEL_23:
v14 = _bswap_16(s1[6]);
if ( v14 != 2048 )
goto LABEL_24;
if ( v15 > 0x29 )
{
LABEL_17:
if ( HIBYTE(s1[11]) == 17 && (unsigned int)(unsigned __int16)_bswap_16(s1[18]) - 319 <= 1 )
{
print_spaces(v20);
v13 = dump_udppkt(&unk_A9A, s1, v15, v20);
LABEL_12:
if ( !v13 )
putchar(10);
fflush(stdout);
}
}
}
}
else
{
qmemcpy(prev_0, s1, 0x5D8uLL);
prev_0[374] = v22;
prevlen_1 = 1500;
v14 = _bswap_16(s1[6]);
v15 = 1500;
if ( v14 == 2048 )
{
v15 = 1500;
goto LABEL_17;
}
LABEL_24:
if ( v14 == -30473 )
{
print_spaces(v20);
v13 = dump_1588pkt(&unk_A9A, s1, v15, v20);
goto LABEL_12;
}
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // ax
int v4; // eax
int v5; // ebp
const char *v6; // r13
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int v13; // eax
__int16 v14; // ax
unsigned int v15; // er12
int v16; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-68Ch] BYREF
struct timeval tv; // [rsp+10h] [rbp-688h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-678h] BYREF
__m128i v20[2]; // [rsp+30h] [rbp-668h] BYREF
__int16 s1[748]; // [rsp+50h] [rbp-648h] BYREF
int v22; // [rsp+628h] [rbp-70h]
char dest[8]; // [rsp+630h] [rbp-68h] BYREF
__int64 v24; // [rsp+638h] [rbp-60h]
__int64 v25; // [rsp+640h] [rbp-58h]
__int64 v26; // [rsp+648h] [rbp-50h]
__int64 v27; // [rsp+650h] [rbp-48h]
__int64 optval[7]; // [rsp+660h] [rbp-38h] BYREF

v3 = func0(3);
v4 = socket(17, 3, v3);
if ( v4 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "%s: socket(): %s\n", *argv, v10);
exit(1);
}
v5 = v4;
v6 = "eth0";
if ( argc > 1 )
v6 = argv[1];
*(_QWORD *)dest = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
strcpy(dest, v6);
if ( ioctl(v5, 0x8933uLL, dest) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
fprintf(stderr, "%s: ioctl(GETIFINDEX(%s)): %s\n", *argv, v6, v12);
exit(1);
}
fprintf(stderr, "Dumping PTP packets from interface \"%s\"\n", v6);
optval[1] = 0LL;
optval[0] = (unsigned int)v25 | 0x100000000LL;
if ( setsockopt(3, 263, 1, optval, 0x10u) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "%s: set promiscuous(%s): %s\n", *argv, v6, v8);
exit(1);
}
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v16 = recvfrom(v5, s1, 0x5DCuLL, 32, &addr, &addr_len);
gettimeofday(&tv, 0LL);
v20[0].m128i_i32[0] = tv.tv_sec;
v20[0].m128i_i32[1] = 1000 * LODWORD(tv.tv_usec);
if ( (unsigned int)v16 > 0x5DC )
break;
v15 = v16;
if ( prevlen_1 != v16 || memcmp(s1, prev_0, v16) )
{
memcpy(prev_0, s1, v16);
prevlen_1 = v16;
if ( v16 > 13 )
goto LABEL_23;
}
}
v15 = prevlen_1;
if ( prevlen_1 == 1500 )
{
if ( memcmp(s1, prev_0, 0x5DCuLL) )
{
qmemcpy(prev_0, s1, 0x5D8uLL);
prev_0[374] = v22;
prevlen_1 = 1500;
LABEL_23:
v14 = func0(s1[6]);
if ( v14 != 2048 )
goto LABEL_24;
if ( v15 > 0x29 )
{
LABEL_17:
if ( HIBYTE(s1[11]) == 17 && (unsigned int)(unsigned __int16)func0(s1[18]) - 319 <= 1 )
{
func1(v20);
v13 = dump_udppkt(&unk_A9A, s1, v15, v20);
LABEL_12:
if ( !v13 )
putchar(10);
fflush(stdout);
}
}
}
}
else
{
qmemcpy(prev_0, s1, 0x5D8uLL);
prev_0[374] = v22;
prevlen_1 = 1500;
v14 = func0(s1[6]);
v15 = 1500;
if ( v14 == 2048 )
{
v15 = 1500;
goto LABEL_17;
}
LABEL_24:
if ( v14 == -30473 )
{
func1(v20);
v13 = dump_1588pkt(&unk_A9A, s1, v15, v20);
goto LABEL_12;
}
}
}
}

original_code:
int main(int argc, char **argv)
{
 int sock, ret;
 struct packet_mreq req;
 struct ifreq ifr;
 char *ifname = "eth0";
 sock = socket(17, SOCK_RAW, htons(0x0003));
 if (sock < 0) {
  fprintf(stderr, "%s: socket(): %s\n", argv[0], strerror((*__errno_location ())));
  exit(1);
 }
 if (argc > 1)
  ifname = argv[1];
 memset(&ifr, 0, sizeof(ifr));
 strcpy(ifr.ifr_ifrn.ifrn_name, ifname);
 if (ioctl(sock, 0x8933, &ifr) < 0) {
  fprintf(stderr, "%s: ioctl(GETIFINDEX(%s)): %s\n", argv[0],
   ifname, strerror((*__errno_location ())));
  exit(1);
 }
 fprintf(stderr, "Dumping PTP packets from interface \"%s\"\n", ifname);
 memset(&req, 0, sizeof(req));
 req.mr_ifindex = ifr.ifr_ifru.ifru_ivalue;
 req.mr_type = 1;
 if (setsockopt(3, 263, 1,
         &req, sizeof(req)) < 0) {
  fprintf(stderr, "%s: set promiscuous(%s): %s\n", argv[0],
   ifname, strerror((*__errno_location ())));
  exit(1);
 }
 while(1) {
  struct ethhdr *eth;
  struct iphdr *ip;
  static unsigned char prev[1500];
  static int prevlen;
  unsigned char buf[1500];
  struct TimeInternal ti;
  struct timeval tv;
  struct sockaddr_in from;
  socklen_t fromlen = sizeof(from);
  int len;
  len = recvfrom(sock, buf, sizeof(buf), MSG_TRUNC,
          (struct sockaddr *) &from, &fromlen);
  gettimeofday(&tv, ((void *)0));
  ti.seconds = tv.tv_sec;
  ti.nanoseconds = tv.tv_usec * 1000;
  if (len > sizeof(buf))
   len = sizeof(buf);
  if (len == prevlen && !memcmp(buf, prev, len))
   continue;
  memcpy(prev, buf, len); prevlen = len;
  if (len < 14)
   continue;
  eth = (void *)buf;
  ip = (void *)(buf + 14);
  switch(ntohs(eth->h_proto)) {
  case 0x0800:
  {
   struct udphdr *udp = (void *)(ip + 1);
   int udpdest = ntohs(udp->dest);
   if (len < 14 + sizeof(*ip) + sizeof(*udp))
    continue;
   if (ip->protocol != IPPROTO_UDP)
    continue;
   if (udpdest != 319 && udpdest != 320)
    continue;
   print_spaces(&ti);
   ret = dump_udppkt("", buf, len, &ti);
   break;
  }
  case 0x88F7:
   print_spaces(&ti);
   ret = dump_1588pkt("", buf, len, &ti);
   break;
  default:
   ret = -1;
   continue;
  }
  if (ret == 0)
   putchar('\n');
  fflush(stdout);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int sock; int ret;
 struct packet_mreq req;
 struct ifreq ifr;
 char *ifname = "eth0";
 sock = socket(17, SOCK_RAW, htons(0x0003));
 if (sock < 0) {
  fprintf(stderr, "%s: socket(): %s\n", argv[0], strerror((*__errno_location ())));
  exit(1);
 }
 if (argc > 1)
  ifname = argv[1];
 memset(&ifr, 0, sizeof(ifr));
 strcpy(ifr.ifr_ifrn.ifrn_name, ifname);
 if (ioctl(sock, 0x8933, &ifr) < 0) {
  fprintf(stderr, "%s: ioctl(GETIFINDEX(%s)): %s\n", argv[0],
   ifname, strerror((*__errno_location ())));
  exit(1);
 }
 fprintf(stderr, "Dumping PTP packets from interface \"%s\"\n", ifname);
 memset(&req, 0, sizeof(req));
 req.mr_ifindex = ifr.ifr_ifru.ifru_ivalue;
 req.mr_type = 1;
 if (setsockopt(3, 263, 1,
         &req, sizeof(req)) < 0) {
  fprintf(stderr, "%s: set promiscuous(%s): %s\n", argv[0],
   ifname, strerror((*__errno_location ())));
  exit(1);
 }
 while(1) {
  struct ethhdr *eth;
  struct iphdr *ip;
  static unsigned char prev[1500];
  static int prevlen;
  unsigned char buf[1500];
  struct TimeInternal ti;
  struct timeval tv;
  struct sockaddr_in from;
  unsigned int fromlen = sizeof(from);
  int len;
  len = recvfrom(sock, buf, sizeof(buf), MSG_TRUNC,
          (struct sockaddr *) &from, &fromlen);
  gettimeofday(&tv, ((void *)0));
  ti.seconds = tv.tv_sec;
  ti.nanoseconds = tv.tv_usec * 1000;
  if (len > sizeof(buf))
   len = sizeof(buf);
  if (len == prevlen && !memcmp(buf, prev, len))
   continue;
  memcpy(prev, buf, len); prevlen = len;
  if (len < 14)
   continue;
  eth = (void *)buf;
  ip = (void *)(buf + 14);
  switch(ntohs(eth->h_proto)) {
  case 0x0800:
  {
   struct udphdr *udp = (void *)(ip + 1);
   int udpdest = ntohs(udp->dest);
   if (len < 14 + sizeof(*ip) + sizeof(*udp))
    continue;
   if (ip->protocol != IPPROTO_UDP)
    continue;
   if (udpdest != 319 && udpdest != 320)
    continue;
   print_spaces(&ti);
   ret = dump_udppkt("", buf, len, &ti);
   break;
  }
  case 0x88F7:
   print_spaces(&ti);
   ret = dump_1588pkt("", buf, len, &ti);
   break;
  default:
   ret = -1;
   continue;
  }
  if (ret == 0)
   putchar('\n');
  fflush(stdout);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: sock int

variable_types: ret int

variable_types: struct packet_mreq { int mr_ifindex; unsigned short mr_type; unsigned short mr_alen; unsigned char mr_address[8]; }

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

variable_types:
ifname
char *

variable_types: struct ethhdr { unsigned char h_dest[6]; unsigned char h_source[6]; unsigned short h_proto; } *

variable_types: struct iphdr { unsigned int ihl; unsigned int version; unsigned char tos; unsigned short tot_len; unsigned short id; unsigned short frag_off; unsigned char ttl; unsigned char protocol; unsigned short check; unsigned int saddr; unsigned int daddr; } *

variable_types:
prev
unsigned char [1500]

variable_types: prevlen int

variable_types:
buf
unsigned char [1500]

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

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

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types:
fromlen
unsigned int

variable_types: len int

variable_types: struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } *

variable_types: udpdest int

return_type: int

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }


decompiled_code:
__int64 __fastcall pp_disabled(__int64 a1)
{
*(_DWORD *)(a1 + 8) = 1000;
return 0LL;
}

original_code:
int pp_disabled(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 ppi->next_delay = 1000;
 return 0;
}

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

variable_types:
pkt
unsigned char *

variable_types: plen int

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


original_code:
int pp_open_globals(struct pp_globals *ppg)
{
 int i;
 struct DSDefault *def = ppg->defaultDS;
 def->twoStepFlag = TRUE;
 def->numberPorts = ppg->nlinks > 0 ? ppg->nlinks : 1;
 struct pp_runtime_opts *rt_opts;
 if (!ppg->rt_opts)
  ppg->rt_opts = &default_rt_opts;
 rt_opts = ppg->rt_opts;
 memcpy(&def->clockQuality, &rt_opts->clock_quality,
     sizeof(ClockQuality));
 if (def->numberPorts == 1)
  def->slaveOnly = ppg->pp_instances[0].slave_only;
 else
  def->slaveOnly = 1;
 def->priority1 = rt_opts->prio1;
 def->priority2 = rt_opts->prio2;
 def->domainNumber = rt_opts->domain_number;
 for (i = 0; i < def->numberPorts; i++) {
  if (def->slaveOnly && !ppg->pp_instances[i].slave_only)
   def->slaveOnly = 0;
  ppg->pp_instances[i].state = PPS_INITIALIZING;
  ppg->pp_instances[i].port_idx = i;
 }
 if (def->slaveOnly) {
  pp_printf("Slave Only, clock class set to 255\n");
  def->clockQuality.clockClass = 255;
 }
 if (pp_hooks.open)
  return pp_hooks.open(ppg, rt_opts);
 return 0;
}

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

variable_types: i int

variable_types: struct DSDefault { enum Boolean { FALSE=0, TRUE=1 } twoStepFlag; struct ClockIdentity { unsigned char id[8]; } clockIdentity; unsigned short numberPorts; struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; } clockQuality; unsigned char priority1; unsigned char priority2; unsigned char domainNumber; enum Boolean { FALSE=0, TRUE=1 } slaveOnly; } *

variable_types: struct pp_runtime_opts { struct ClockQuality { unsigned char clockClass; unsigned char clockAccuracy; unsigned short offsetScaledLogVariance; } clock_quality; struct TimeInternal { int seconds; int nanoseconds; int phase; int correct; int raw_ahead; } 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 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 PortIdentity { struct ClockIdentity clockIdentity; unsigned short portNumber; }


decompiled_code:
__int64 pp_close_globals()
{
__int64 (*v0)(void); // rdx
__int64 result; // rax

v0 = (__int64 (*)(void))pp_hooks[2];
result = 0LL;
if ( v0 )
result = v0();
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 (*v0)(void); // rdx
__int64 result; // rax

v0 = (__int64 (*)(void))pp_hooks[2];
result = 0LL;
if ( v0 )
result = v0();
return result;
}

original_code:
int pp_close_globals(struct pp_globals *ppg)
{
 if (pp_hooks.close)
  return pp_hooks.close(ppg);
 return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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


decompiled_code:
__int64 __fastcall 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_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

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

user_defined_types: struct 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 MsgAnnounce { struct Timestamp originTimestamp; short currentUtcOffset; unsigned char grandmasterPriority1; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority2; struct ClockIdentity grandmasterIdentity; unsigned short stepsRemoved; unsigned char timeSource; unsigned long ext_specific; }

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

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

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

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

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


decompiled_code:
__int64 __fastcall pp_listening(__int64 a1, __int64 a2, unsigned int a3)
{
unsigned int (*v4)(void); // rax
char v5; // al
int v6; // eax

v4 = (unsigned int (*)(void))pp_hooks[3];
if ( v4 && v4() )
goto LABEL_10;
if ( *(_DWORD *)(a1 + 12) )
pp_timeout_restart_annrec(a1);
if ( a3 )
{
v5 = *(_BYTE *)(a1 + 1345);
if ( v5 )
{
if ( v5 != 11 )
goto LABEL_15;
v6 = st_com_master_handle_announce(a1, a2, a3);
}
else
{
v6 = st_com_master_handle_sync(a1, a2, a3);
}
if ( v6 )
{
LABEL_10:
*(_DWORD *)(a1 + 4) = 2;
goto LABEL_11;
}
}
LABEL_15:
if ( (unsigned int)st_com_execute_slave(a1) )
goto LABEL_10;
LABEL_11:
if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)a1 )
pp_timeout_clr(a1, 2);
*(_DWORD *)(a1 + 8) = pp_ms_to_timeout(a1, 2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, unsigned int a3)
{
unsigned int (*v4)(void); // rax
char v5; // al
int v6; // eax

v4 = (unsigned int (*)(void))pp_hooks[3];
if ( v4 && v4() )
goto LABEL_10;
if ( *(_DWORD *)(a1 + 12) )
func4(a1);
if ( a3 )
{
v5 = *(_BYTE *)(a1 + 1345);
if ( v5 )
{
if ( v5 != 11 )
goto LABEL_15;
v6 = st_com_master_handle_announce(a1, a2, a3);
}
else
{
v6 = st_com_master_handle_sync(a1, a2, a3);
}
if ( v6 )
{
LABEL_10:
*(_DWORD *)(a1 + 4) = 2;
goto LABEL_11;
}
}
LABEL_15:
if ( (unsigned int)st_com_execute_slave(a1) )
goto LABEL_10;
LABEL_11:
if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)a1 )
func2(a1, 2);
*(_DWORD *)(a1 + 8) = func3(a1, 2);
return 0LL;
}

original_code:
int pp_listening(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 int e = 0;
 if (pp_hooks.listening)
  e = pp_hooks.listening(ppi, pkt, plen);
 if (e)
  goto out;
 if (ppi->is_new_state)
  pp_timeout_restart_annrec(ppi);
 if (plen == 0)
  goto out;
 switch (ppi->received_ptp_header.messageType) {
 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;
 default:
  break;
 }
out:
 if (e == 0)
  e = st_com_execute_slave(ppi);
 if (e != 0)
  ppi->next_state = PPS_FAULTY;
 if (ppi->next_state != ppi->state)
  pp_timeout_clr(ppi, PP_TO_ANN_RECEIPT);
 ppi->next_delay = pp_ms_to_timeout(ppi, PP_TO_ANN_RECEIPT);
 return 0;
}

variable_types: e 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_runtime_opts { struct ClockQuality clock_quality; struct TimeInternal inbound_latency; int max_rst; int max_dly; int ttl; unsigned int no_adjust; short ap; short s; char announce_intvl; char sync_intvl; unsigned char prio1; unsigned char prio2; unsigned char domain_number; void *arch_opts; }

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

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

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

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


canonical_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_network_operations { int (*init)(struct pp_instance *ppi); int (*exit)(struct pp_instance *ppi); int (*recv)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t); int (*send)(struct pp_instance *ppi, void *pkt, int len, struct TimeInternal *t, int chtype, int use_pdelay_addr); }

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

user_defined_types: struct 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 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_passive(__int64 a1, __int64 a2, unsigned int a3)
{
char v4; // al
int v5; // eax

if ( *(_DWORD *)(a1 + 12) )
pp_timeout_restart_annrec(a1);
if ( !a3 )
goto LABEL_8;
v4 = *(_BYTE *)(a1 + 1345);
if ( v4 )
{
if ( v4 != 11 )
goto LABEL_8;
v5 = st_com_master_handle_announce(a1, a2, a3);
}
else
{
v5 = st_com_master_handle_sync(a1, a2, a3);
}
if ( !v5 )
{
LABEL_8:
if ( !(unsigned int)st_com_execute_slave(a1) )
goto LABEL_10;
}
*(_DWORD *)(a1 + 4) = 2;
LABEL_10:
if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)a1 )
pp_timeout_clr(a1, 2);
*(_DWORD *)(a1 + 8) = 1000;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, unsigned int a3)
{
char v4; // al
int v5; // eax

if ( *(_DWORD *)(a1 + 12) )
func3(a1);
if ( !a3 )
goto LABEL_8;
v4 = *(_BYTE *)(a1 + 1345);
if ( v4 )
{
if ( v4 != 11 )
goto LABEL_8;
v5 = st_com_master_handle_announce(a1, a2, a3);
}
else
{
v5 = st_com_master_handle_sync(a1, a2, a3);
}
if ( !v5 )
{
LABEL_8:
if ( !(unsigned int)st_com_execute_slave(a1) )
goto LABEL_10;
}
*(_DWORD *)(a1 + 4) = 2;
LABEL_10:
if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)a1 )
func2(a1, 2);
*(_DWORD *)(a1 + 8) = 1000;
return 0LL;
}

original_code:
int pp_passive(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
 int e = 0;
 if (ppi->is_new_state)
  pp_timeout_restart_annrec(ppi);
 if (plen == 0)
  goto no_incoming_msg;
 switch (ppi->received_ptp_header.messageType) {
 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;
 default:
  break;
 }
no_incoming_msg:
 if (e == 0)
  e = st_com_execute_slave(ppi);
 if (e != 0)
  ppi->next_state = PPS_FAULTY;
 if (ppi->next_state != ppi->state) {
  pp_timeout_clr(ppi, PP_TO_ANN_RECEIPT);
 }
 ppi->next_delay = 1000;
 return 0;
}

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 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 DSParent { struct PortIdentity parentPortIdentity; unsigned short observedParentOffsetScaledLogVariance; int observedParentClockPhaseChangeRate; struct ClockIdentity grandmasterIdentity; struct ClockQuality grandmasterClockQuality; unsigned char grandmasterPriority1; unsigned char grandmasterPriority2; }

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

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


original_code:
void mtp_result(struct mtp_packet *pkt)
{
 int i;
 long total, remote, rtt;
 signed long long delta;
 for (i = 0; i < 3; i++) {
  if (pkt->t[0][i].tv_sec == 0)
   continue;
  if (0) {
   int j;
   printf("Times at slot %i:\n", i);
   for (j = 0; j < 4; j++) {
    printf("   t%i: %li.%09li\n", i,
           pkt->t[j][i].tv_sec,
           pkt->t[j][i].tv_nsec);
   }
  }
  total = tsdiff(pkt->t[3] + i, pkt->t[0] + i);
  remote = tsdiff(pkt->t[2] + i, pkt->t[1] + i);
  rtt = total - remote;
  delta = tsdiff(pkt->t[1] + i, pkt->t[0] + i);
  if (0) {
   printf("total %li\n", total);
   printf("remote %li\n", remote);
   printf("rtt %li\n", rtt);
   printf("bigdelta %lli\n", delta);
  }
  delta -= rtt / 2;
  printf("%i: rtt %12.9lf   delta %13.9lf\n",
         i, (double)rtt / (1000*1000*1000),
         (double)delta / (1000*1000*1000));
 }
}

canonical_original_code:
void mtp_result(struct mtp_packet *pkt)
{
 int i;
 long total; long remote; long rtt;
 long long delta;
 for (i = 0; i < 3; i++) {
  if (pkt->t[0][i].tv_sec == 0)
   continue;
  if (0) {
   int j;
   printf("Times at slot %i:\n", i);
   for (j = 0; j < 4; j++) {
    printf("   t%i: %li.%09li\n", i,
           pkt->t[j][i].tv_sec,
           pkt->t[j][i].tv_nsec);
   }
  }
  total = tsdiff(pkt->t[3] + i, pkt->t[0] + i);
  remote = tsdiff(pkt->t[2] + i, pkt->t[1] + i);
  rtt = total - remote;
  delta = tsdiff(pkt->t[1] + i, pkt->t[0] + i);
  if (0) {
   printf("total %li\n", total);
   printf("remote %li\n", remote);
   printf("rtt %li\n", rtt);
   printf("bigdelta %lli\n", delta);
  }
  delta -= rtt / 2;
  printf("%i: rtt %12.9lf   delta %13.9lf\n",
         i, (double)rtt / (1000*1000*1000),
         (double)delta / (1000*1000*1000));
 }
}

variable_types: struct mtp_packet { unsigned int tragic; unsigned int ptype; struct timespec { long tv_sec; long tv_nsec; } t[4][3]; } *

variable_types: total long

variable_types: remote long

variable_types: rtt long

variable_types:
delta
long long

variable_types: j int

user_defined_types: struct mtp_packet { unsigned int tragic; unsigned int ptype; struct timespec t[4][3]; }


charForHexInt

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

result = a1 + 55;
if ( a1 <= 9 )
result = a1 + 48;
return result;
}

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

result = a1 + 55;
if ( a1 <= 9 )
result = a1 + 48;
return result;
}

original_code:
char charForHexInt (int n) {
    if (n >= 0 && n < 10)
        return '0' + n;
    return 'A' + n - 10;
}

canonical_original_code:
char charForHexInt(int n) {
    if (n >= 0 && n < 10)
        return '0' + n;
    return 'A' + n - 10;
}

variable_types: n int

return_type: char


decompiled_code:
int __fastcall dump_urb(int a1, int a2, int a3, int a4, int a5, int a6, __int64 a7, int a8, void *src, __int64 a10, __int64 a11, __int64 a12)
{
int result; // eax
const char *v13; // rcx
const char *v14; // rdx
__int64 i; // rbx
unsigned __int8 v16; // bl
const char *v17; // rdx
unsigned __int8 v18; // al
unsigned __int8 v19; // al
unsigned __int8 v20; // [rsp+0h] [rbp-48h] BYREF
unsigned __int8 v21; // [rsp+1h] [rbp-47h]
unsigned __int16 v22; // [rsp+2h] [rbp-46h]
unsigned __int16 v23; // [rsp+4h] [rbp-44h]
unsigned __int16 v24; // [rsp+6h] [rbp-42h]
unsigned int v25; // [rsp+8h] [rbp-40h]

printf(" type = ");
fflush(stdout);
if ( (_BYTE)a7 == 1 )
{
puts("USBDEVFS_URB_TYPE_INTERRUPT");
result = fflush(stdout);
}
else if ( (unsigned __int8)a7 <= 1u )
{
puts("USBDEVFS_URB_TYPE_ISO");
result = fflush(stdout);
}
else if ( (unsigned __int8)(a7 - 2) > 1u )
{
result = printf("UNKNOWN %d\n", (unsigned __int8)a7);
}
else
{
if ( (_BYTE)a7 == 3 )
puts("USBDEVFS_URB_TYPE_BULK");
else
puts("USBDEVFS_URB_TYPE_CONTROL");
fflush(stdout);
if ( (a7 & 0x8000) != 0 )
printf(" endpoint = %d|USB_DIR_IN\n", (unsigned __int8)(BYTE1(a7) + 0x80));
else
printf(" endpoint = %d|USB_DIR_OUT\n", BYTE1(a7));
fflush(stdout);
if ( HIDWORD(a7) )
{
switch ( HIDWORD(a7) )
{
case 0xFFFFFF87:
puts(
" status = -EREMOTEIO: data read from EP didnt fill the buffer, and URB_SHORT_NOT_OK was set"
" in urb->transfer_flags.");
break;
case 0xFFFFFF8D:
puts(" status = -EINPROGRESS: URB still pending, no results yet");
break;
case 0xFFFFFF92:
puts(" status = -ETIMEDOUT: timeout expired before the transfer completed");
break;
case 0xFFFFFF94:
puts(
" status = Device or host controller is disabled due to a problem that couldnt worked around"
", e.g. physical disconnect");
break;
case 0xFFFFFF98:
puts(" status = -ECONNRESET: URB was asynchronously unlinked by usb_unlink_urb");
break;
case 0xFFFFFFAC:
puts(" status = -EILSEQ: CRC mismatch / no response within turn-around time / unknown error");
break;
case 0xFFFFFFB5:
puts(
" status = -EOVERFLOW: amount returned by EP greater than either the max pckt size of EP or "
"the remaining buff size.");
break;
case 0xFFFFFFB9:
puts(" status = -EPROTO: bitstuff error / no response within turn-around time / unknown error");
break;
case 0xFFFFFFBA:
puts(
" status = -ECOMM: During IN transf, host controller received data from endpoint faster than"
" could writte to memory");
break;
case 0xFFFFFFC1:
puts(
" status = -ENOSR: During OUT transf, host controller couldnt get data fast enough to keep u"
"p with the USB data rate");
break;
case 0xFFFFFFC2:
puts(" status = -ETIME: no response within turn-around time");
break;
case 0xFFFFFFE0:
puts(" status = -EPIPE: Endpoint stalled");
break;
case 0xFFFFFFEA:
puts(" status = -EINVAL: ISO madness, if this happens: Log off and go home");
break;
case 0xFFFFFFED:
puts(" status = -ENODEV: Device was removed");
break;
case 0xFFFFFFEE:
puts(" status = -EXDEV: ISO transfer only partially completed");
break;
case 0xFFFFFFFE:
puts(" status = -ENOENT : URB was synchronously unlinked by usb_unlink_urb");
break;
default:
puts(" status = UNKNOWN : error code is not known");
break;
}
}
else
{
puts(" status = 0 (OK)");
}
fflush(stdout);
v13 = "USBDEVFS_URB_ISO_ASAP";
if ( (a8 & 2) == 0 )
v13 = (const char *)&unk_662;
v14 = "USBDEVFS_URB_SHORT_NOT_OK";
if ( (a8 & 1) == 0 )
v14 = (const char *)&unk_662;
printf(" flags = %d (%s %s)\n", (unsigned int)a8, v14, v13);
fflush(stdout);
printf(" buffer_length = %d\n", (unsigned int)a10);
fflush(stdout);
printf(" actual_length = %d\n", HIDWORD(a10));
fflush(stdout);
if ( a1 )
{
puts(" BUFFER:");
fflush(stdout);
printf(" ");
fflush(stdout);
if ( (_DWORD)a10 )
{
printf("0x%02x ", *(unsigned __int8 *)src);
for ( i = 1LL; ; ++i )
{
fflush(stdout);
if ( (unsigned int)(a10 - 1) + 1LL == i )
break;
printf("0x%02x ", *((unsigned __int8 *)src + i));
if ( (int)i % 16 == 15 )
printf("\n ");
}
}
putchar(10);
fflush(stdout);
}
else
{
puts(" BUFFER: (not shown)");
fflush(stdout);
}
printf(" start_frame = %d\n", (unsigned int)a11);
fflush(stdout);
printf(" number_of_packets = %d\n", HIDWORD(a11));
fflush(stdout);
printf(" error_count = %d\n", (unsigned int)a12);
fflush(stdout);
printf(" signr = %d\n", HIDWORD(a12));
fflush(stdout);
puts(" usercontext = 0x--------");
fflush(stdout);
result = fflush(stdout);
if ( (_BYTE)a7 == 2 )
{
memcpy(&v20, src, (int)a10);
puts("Control Packet Contents:");
v16 = v20;
v17 = "USB_DIR_IN";
if ( (v20 & 0x80u) == 0 )
v17 = "USB_DIR_OUT";
printf(" bRequestType = (0x%02x) : %s | ", v20, v17);
v18 = v16 & 0x60;
if ( (v16 & 0x60) == 64 )
{
printf("USB_TYPE_VENDOR | ");
}
else if ( v18 > 0x40u )
{
if ( v18 == 96 )
printf("USB_TYPE_RESERVED | ");
}
else if ( v18 )
{
if ( v18 == 32 )
printf("USB_TYPE_CLASS | ");
}
else
{
printf("USB_TYPE_STANDARD | ");
}
v19 = v16 & 0x1F;
if ( (v16 & 0x1F) == 2 )
{
puts("USB_RECIP_ENDPOINT");
}
else if ( v19 > 2u )
{
if ( v19 == 3 )
puts("USB_RECIP_OTHER");
}
else if ( v19 )
{
if ( v19 == 1 )
puts("USB_RECIP_INTERFACE");
}
else
{
puts("USB_RECIP_DEVICE");
}
printf(" bRequest = ");
switch ( v21 )
{
case 0u:
puts("USB_REQ_GET_STATUS");
break;
case 1u:
puts("USB_REQ_CLEAR_FEATURE");
break;
case 3u:
puts("USB_REQ_SET_FEATURE");
break;
case 5u:
puts("USB_REQ_SET_ADDRESS");
break;
case 6u:
puts("USB_REQ_GET_DESCRIPTOR");
break;
case 7u:
puts("USB_REQ_SET_DESCRIPTOR");
break;
case 8u:
puts("USB_REQ_GET_CONFIGURATION");
break;
case 9u:
puts("USB_REQ_SET_CONFIGURATION");
break;
case 0xAu:
puts("USB_REQ_GET_INTERFACE");
break;
case 0xBu:
puts("USB_REQ_SET_INTERFACE");
break;
case 0xCu:
puts("USB_REQ_SYNCH_FRAME");
break;
default:
printf("%d\n", v21);
break;
}
printf(" wValue = 0x%04x\n", v22);
printf(" wIndex = 0x%04x\n", v23);
printf(" wLength = %d\n", v24);
printf(" timeout = %d\n", v25);
printf(" data = 0x--------");
result = fflush(stdout);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, int a3, int a4, int a5, int a6, __int64 a7, int a8, void *src, __int64 a10, __int64 a11, __int64 a12)
{
int result; // eax
const char *v13; // rcx
const char *v14; // rdx
__int64 i; // rbx
unsigned __int8 v16; // bl
const char *v17; // rdx
unsigned __int8 v18; // al
unsigned __int8 v19; // al
unsigned __int8 v20; // [rsp+0h] [rbp-48h] BYREF
unsigned __int8 v21; // [rsp+1h] [rbp-47h]
unsigned __int16 v22; // [rsp+2h] [rbp-46h]
unsigned __int16 v23; // [rsp+4h] [rbp-44h]
unsigned __int16 v24; // [rsp+6h] [rbp-42h]
unsigned int v25; // [rsp+8h] [rbp-40h]

printf(" type = ");
fflush(stdout);
if ( (_BYTE)a7 == 1 )
{
puts("USBDEVFS_URB_TYPE_INTERRUPT");
result = fflush(stdout);
}
else if ( (unsigned __int8)a7 <= 1u )
{
puts("USBDEVFS_URB_TYPE_ISO");
result = fflush(stdout);
}
else if ( (unsigned __int8)(a7 - 2) > 1u )
{
result = printf("UNKNOWN %d\n", (unsigned __int8)a7);
}
else
{
if ( (_BYTE)a7 == 3 )
puts("USBDEVFS_URB_TYPE_BULK");
else
puts("USBDEVFS_URB_TYPE_CONTROL");
fflush(stdout);
if ( (a7 & 0x8000) != 0 )
printf(" endpoint = %d|USB_DIR_IN\n", (unsigned __int8)(BYTE1(a7) + 0x80));
else
printf(" endpoint = %d|USB_DIR_OUT\n", BYTE1(a7));
fflush(stdout);
if ( HIDWORD(a7) )
{
switch ( HIDWORD(a7) )
{
case 0xFFFFFF87:
puts(
" status = -EREMOTEIO: data read from EP didnt fill the buffer, and URB_SHORT_NOT_OK was set"
" in urb->transfer_flags.");
break;
case 0xFFFFFF8D:
puts(" status = -EINPROGRESS: URB still pending, no results yet");
break;
case 0xFFFFFF92:
puts(" status = -ETIMEDOUT: timeout expired before the transfer completed");
break;
case 0xFFFFFF94:
puts(
" status = Device or host controller is disabled due to a problem that couldnt worked around"
", e.g. physical disconnect");
break;
case 0xFFFFFF98:
puts(" status = -ECONNRESET: URB was asynchronously unlinked by usb_unlink_urb");
break;
case 0xFFFFFFAC:
puts(" status = -EILSEQ: CRC mismatch / no response within turn-around time / unknown error");
break;
case 0xFFFFFFB5:
puts(
" status = -EOVERFLOW: amount returned by EP greater than either the max pckt size of EP or "
"the remaining buff size.");
break;
case 0xFFFFFFB9:
puts(" status = -EPROTO: bitstuff error / no response within turn-around time / unknown error");
break;
case 0xFFFFFFBA:
puts(
" status = -ECOMM: During IN transf, host controller received data from endpoint faster than"
" could writte to memory");
break;
case 0xFFFFFFC1:
puts(
" status = -ENOSR: During OUT transf, host controller couldnt get data fast enough to keep u"
"p with the USB data rate");
break;
case 0xFFFFFFC2:
puts(" status = -ETIME: no response within turn-around time");
break;
case 0xFFFFFFE0:
puts(" status = -EPIPE: Endpoint stalled");
break;
case 0xFFFFFFEA:
puts(" status = -EINVAL: ISO madness, if this happens: Log off and go home");
break;
case 0xFFFFFFED:
puts(" status = -ENODEV: Device was removed");
break;
case 0xFFFFFFEE:
puts(" status = -EXDEV: ISO transfer only partially completed");
break;
case 0xFFFFFFFE:
puts(" status = -ENOENT : URB was synchronously unlinked by usb_unlink_urb");
break;
default:
puts(" status = UNKNOWN : error code is not known");
break;
}
}
else
{
puts(" status = 0 (OK)");
}
fflush(stdout);
v13 = "USBDEVFS_URB_ISO_ASAP";
if ( (a8 & 2) == 0 )
v13 = (const char *)&unk_662;
v14 = "USBDEVFS_URB_SHORT_NOT_OK";
if ( (a8 & 1) == 0 )
v14 = (const char *)&unk_662;
printf(" flags = %d (%s %s)\n", (unsigned int)a8, v14, v13);
fflush(stdout);
printf(" buffer_length = %d\n", (unsigned int)a10);
fflush(stdout);
printf(" actual_length = %d\n", HIDWORD(a10));
fflush(stdout);
if ( a1 )
{
puts(" BUFFER:");
fflush(stdout);
printf(" ");
fflush(stdout);
if ( (_DWORD)a10 )
{
printf("0x%02x ", *(unsigned __int8 *)src);
for ( i = 1LL; ; ++i )
{
fflush(stdout);
if ( (unsigned int)(a10 - 1) + 1LL == i )
break;
printf("0x%02x ", *((unsigned __int8 *)src + i));
if ( (int)i % 16 == 15 )
printf("\n ");
}
}
putchar(10);
fflush(stdout);
}
else
{
puts(" BUFFER: (not shown)");
fflush(stdout);
}
printf(" start_frame = %d\n", (unsigned int)a11);
fflush(stdout);
printf(" number_of_packets = %d\n", HIDWORD(a11));
fflush(stdout);
printf(" error_count = %d\n", (unsigned int)a12);
fflush(stdout);
printf(" signr = %d\n", HIDWORD(a12));
fflush(stdout);
puts(" usercontext = 0x--------");
fflush(stdout);
result = fflush(stdout);
if ( (_BYTE)a7 == 2 )
{
memcpy(&v20, src, (int)a10);
puts("Control Packet Contents:");
v16 = v20;
v17 = "USB_DIR_IN";
if ( (v20 & 0x80u) == 0 )
v17 = "USB_DIR_OUT";
printf(" bRequestType = (0x%02x) : %s | ", v20, v17);
v18 = v16 & 0x60;
if ( (v16 & 0x60) == 64 )
{
printf("USB_TYPE_VENDOR | ");
}
else if ( v18 > 0x40u )
{
if ( v18 == 96 )
printf("USB_TYPE_RESERVED | ");
}
else if ( v18 )
{
if ( v18 == 32 )
printf("USB_TYPE_CLASS | ");
}
else
{
printf("USB_TYPE_STANDARD | ");
}
v19 = v16 & 0x1F;
if ( (v16 & 0x1F) == 2 )
{
puts("USB_RECIP_ENDPOINT");
}
else if ( v19 > 2u )
{
if ( v19 == 3 )
puts("USB_RECIP_OTHER");
}
else if ( v19 )
{
if ( v19 == 1 )
puts("USB_RECIP_INTERFACE");
}
else
{
puts("USB_RECIP_DEVICE");
}
printf(" bRequest = ");
switch ( v21 )
{
case 0u:
puts("USB_REQ_GET_STATUS");
break;
case 1u:
puts("USB_REQ_CLEAR_FEATURE");
break;
case 3u:
puts("USB_REQ_SET_FEATURE");
break;
case 5u:
puts("USB_REQ_SET_ADDRESS");
break;
case 6u:
puts("USB_REQ_GET_DESCRIPTOR");
break;
case 7u:
puts("USB_REQ_SET_DESCRIPTOR");
break;
case 8u:
puts("USB_REQ_GET_CONFIGURATION");
break;
case 9u:
puts("USB_REQ_SET_CONFIGURATION");
break;
case 0xAu:
puts("USB_REQ_GET_INTERFACE");
break;
case 0xBu:
puts("USB_REQ_SET_INTERFACE");
break;
case 0xCu:
puts("USB_REQ_SYNCH_FRAME");
break;
default:
printf("%d\n", v21);
break;
}
printf(" wValue = 0x%04x\n", v22);
printf(" wIndex = 0x%04x\n", v23);
printf(" wLength = %d\n", v24);
printf(" timeout = %d\n", v25);
printf(" data = 0x--------");
result = fflush(stdout);
}
}
return result;
}

original_code:
void dump_urb( struct usbdevfs_urb urb, int dumpData )
{
    unsigned int buffer_length = urb.buffer_length;
    printf("    type                 = ");
    fflush(stdout);
    switch (urb.type) {
        case 0:
            printf("USBDEVFS_URB_TYPE_ISO\n");
            fflush(stdout);
            break;
        case 1:
            printf("USBDEVFS_URB_TYPE_INTERRUPT\n");
            fflush(stdout);
            break;
        case 3:
        case 2:
            if (3==urb.type)
                printf("USBDEVFS_URB_TYPE_BULK\n");
            else
            if (2==urb.type)
                printf("USBDEVFS_URB_TYPE_CONTROL\n");
            fflush(stdout);
            if (0x80==(urb.endpoint&0x80))
                printf("    endpoint             = %d|USB_DIR_IN\n",urb.endpoint^0x80);
            else
                printf("    endpoint             = %d|USB_DIR_OUT\n",urb.endpoint);
            fflush(stdout);
            if( 0!=urb.status ){
                switch (urb.status){
                    case -2:
                        printf("    status               = -ENOENT  : URB was synchronously unlinked by usb_unlink_urb\n");
                        break;
                    case -115:
                        printf("    status               = -EINPROGRESS: URB still pending, no results yet\n");
                        break;
                    case -71:
                        printf("    status               = -EPROTO: bitstuff error / no response within turn-around time / unknown error\n");
                        break;
                    case -84:
                        printf("    status               = -EILSEQ: CRC mismatch / no response within turn-around time / unknown error\n");
                        break;
                    case -62:
                        printf("    status               = -ETIME: no response within turn-around time\n");
                        break;
                    case -110:
                        printf("    status               = -ETIMEDOUT: timeout expired before the transfer completed\n");
                        break;
                    case -32:
                        printf("    status               = -EPIPE: Endpoint stalled\n");
                        break;
                    case -70:
                        printf("    status               = -ECOMM: During IN transf, host controller received data from endpoint faster than could writte to memory\n");
                        break;
                    case -63:
                        printf("    status               = -ENOSR: During OUT transf, host controller couldnt get data fast enough to keep up with the USB data rate\n");
                        break;
                    case -75:
                        printf("    status               = -EOVERFLOW: amount returned by EP greater than either the max pckt size of EP or the remaining buff size.\n");
                        break;
                    case -121:
                        printf("    status               = -EREMOTEIO: data read from EP didnt fill the buffer, and URB_SHORT_NOT_OK was set in urb->transfer_flags.\n");
                        break;
                    case -19:
                        printf("    status               = -ENODEV: Device was removed\n");
                        break;
                    case -18:
                        printf("    status               = -EXDEV: ISO transfer only partially completed\n");
                        break;
                    case -22:
                        printf("    status               = -EINVAL: ISO madness, if this happens: Log off and go home\n");
                        break;
                    case -104:
                        printf("    status               = -ECONNRESET: URB was asynchronously unlinked by usb_unlink_urb\n");
                        break;
                    case -108:
                        printf("    status               = Device or host controller is disabled due to a problem that couldnt worked around, e.g. physical disconnect\n");
                        break;
                    default:
                        printf("    status               = UNKNOWN  : error code is not known\n");
                        break;
                }
            } else {
                printf("    status               = 0 (OK)\n");
            }
            fflush(stdout);
            printf("    flags                = %d (%s %s)\n",urb.flags,(urb.flags&0x01?"USBDEVFS_URB_SHORT_NOT_OK":""),(urb.flags&0x02?"USBDEVFS_URB_ISO_ASAP":""));
            fflush(stdout);
            printf("    buffer_length        = %d\n",urb.buffer_length);
            fflush(stdout);
            printf("    actual_length        = %d\n",urb.actual_length);
            fflush(stdout);
            if (dumpData){
                printf("    BUFFER:\n");
                fflush(stdout);
                printf("      ");
                fflush(stdout);
                for (int ii=0; ii<buffer_length; ii++ ){
                    unsigned char *cp = urb.buffer;
                    printf("0x%02x ",cp[ii]);
                    if (ii%16==15)
                        printf("\n      ");
                    fflush(stdout);
                }
                printf("\n");
                fflush(stdout);
            } else {
                printf("    BUFFER: (not shown)\n");
                fflush(stdout);
            }
            printf("    start_frame          = %d\n",urb.start_frame);
            fflush(stdout);
            printf("    number_of_packets    = %d\n",urb.number_of_packets);
            fflush(stdout);
            printf("    error_count          = %d\n",urb.error_count);
            fflush(stdout);
            printf("    signr                = %d\n",urb.signr);
            fflush(stdout);
            printf("    usercontext          = 0x--------\n");
            fflush(stdout);
         if (urb.type==0){
          printf("    iso_frame_desc[0].length:        %d\n", urb.iso_frame_desc[0].length);
          printf("    iso_frame_desc[0].actual_length: %d\n", urb.iso_frame_desc[0].actual_length);
          printf("    iso_frame_desc[0].status:        %d\n", urb.iso_frame_desc[0].status);
         }
            fflush(stdout);
            if (2==urb.type){
                struct usbdevfs_ctrltransfer txf;
                memcpy(&txf,urb.buffer,urb.buffer_length);
                printf("Control Packet Contents:\n");
                printf("        bRequestType     = (0x%02x) : %s | ", txf.bRequestType, ((txf.bRequestType&0x80)==0x80?"USB_DIR_IN":"USB_DIR_OUT"));
                switch (txf.bRequestType&(0x03 << 5)){
                    case (0x00 << 5): printf("USB_TYPE_STANDARD | "); break;
                    case (0x01 << 5): printf("USB_TYPE_CLASS | "); break;
                    case (0x02 << 5): printf("USB_TYPE_VENDOR | "); break;
                    case (0x03 << 5): printf("USB_TYPE_RESERVED | "); break;
                }
                switch (txf.bRequestType&0x1f){
                    case 0x00: printf("USB_RECIP_DEVICE\n"); break;
                    case 0x01: printf("USB_RECIP_INTERFACE\n"); break;
                    case 0x02: printf("USB_RECIP_ENDPOINT\n"); break;
                    case 0x03: printf("USB_RECIP_OTHER\n"); break;
                }
                printf("        bRequest         = ");
                switch (txf.bRequest){
                    case 0x00: printf("USB_REQ_GET_STATUS\n"); break;
                    case 0x01: printf("USB_REQ_CLEAR_FEATURE\n"); break;
                    case 0x03: printf("USB_REQ_SET_FEATURE\n"); break;
                    case 0x05: printf("USB_REQ_SET_ADDRESS\n"); break;
                    case 0x06: printf("USB_REQ_GET_DESCRIPTOR\n"); break;
                    case 0x07: printf("USB_REQ_SET_DESCRIPTOR\n"); break;
                    case 0x08: printf("USB_REQ_GET_CONFIGURATION\n"); break;
                    case 0x09: printf("USB_REQ_SET_CONFIGURATION\n"); break;
                    case 0x0A: printf("USB_REQ_GET_INTERFACE\n"); break;
                    case 0x0B: printf("USB_REQ_SET_INTERFACE\n"); break;
                    case 0x0C: printf("USB_REQ_SYNCH_FRAME\n"); break;
                    default: printf("%d\n", txf.bRequest); break;
                }
                printf("        wValue           = 0x%04x\n", txf.wValue);
                printf("        wIndex           = 0x%04x\n", txf.wIndex);
                printf("        wLength          = %d\n", txf.wLength);
                printf("        timeout          = %d\n", txf.timeout);
                printf("        data             = 0x--------");
                fflush(stdout);
            }
            break;
        default:
            printf("UNKNOWN %d\n",urb.type);
            buffer_length = 100;
            break;
    }
}

canonical_original_code:
void dump_urb(struct usbdevfs_urb urb, int dumpData)
{
    unsigned int buffer_length = urb.buffer_length;
    printf("    type                 = ");
    fflush(stdout);
    switch (urb.type) {
        case 0:
            printf("USBDEVFS_URB_TYPE_ISO\n");
            fflush(stdout);
            break;
        case 1:
            printf("USBDEVFS_URB_TYPE_INTERRUPT\n");
            fflush(stdout);
            break;
        case 3:
        case 2:
            if (3==urb.type)
                printf("USBDEVFS_URB_TYPE_BULK\n");
            else
            if (2==urb.type)
                printf("USBDEVFS_URB_TYPE_CONTROL\n");
            fflush(stdout);
            if (0x80==(urb.endpoint&0x80))
                printf("    endpoint             = %d|USB_DIR_IN\n",urb.endpoint^0x80);
            else
                printf("    endpoint             = %d|USB_DIR_OUT\n",urb.endpoint);
            fflush(stdout);
            if( 0!=urb.status ){
                switch (urb.status){
                    case -2:
                        printf("    status               = -ENOENT  : URB was synchronously unlinked by usb_unlink_urb\n");
                        break;
                    case -115:
                        printf("    status               = -EINPROGRESS: URB still pending, no results yet\n");
                        break;
                    case -71:
                        printf("    status               = -EPROTO: bitstuff error / no response within turn-around time / unknown error\n");
                        break;
                    case -84:
                        printf("    status               = -EILSEQ: CRC mismatch / no response within turn-around time / unknown error\n");
                        break;
                    case -62:
                        printf("    status               = -ETIME: no response within turn-around time\n");
                        break;
                    case -110:
                        printf("    status               = -ETIMEDOUT: timeout expired before the transfer completed\n");
                        break;
                    case -32:
                        printf("    status               = -EPIPE: Endpoint stalled\n");
                        break;
                    case -70:
                        printf("    status               = -ECOMM: During IN transf, host controller received data from endpoint faster than could writte to memory\n");
                        break;
                    case -63:
                        printf("    status               = -ENOSR: During OUT transf, host controller couldnt get data fast enough to keep up with the USB data rate\n");
                        break;
                    case -75:
                        printf("    status               = -EOVERFLOW: amount returned by EP greater than either the max pckt size of EP or the remaining buff size.\n");
                        break;
                    case -121:
                        printf("    status               = -EREMOTEIO: data read from EP didnt fill the buffer, and URB_SHORT_NOT_OK was set in urb->transfer_flags.\n");
                        break;
                    case -19:
                        printf("    status               = -ENODEV: Device was removed\n");
                        break;
                    case -18:
                        printf("    status               = -EXDEV: ISO transfer only partially completed\n");
                        break;
                    case -22:
                        printf("    status               = -EINVAL: ISO madness, if this happens: Log off and go home\n");
                        break;
                    case -104:
                        printf("    status               = -ECONNRESET: URB was asynchronously unlinked by usb_unlink_urb\n");
                        break;
                    case -108:
                        printf("    status               = Device or host controller is disabled due to a problem that couldnt worked around, e.g. physical disconnect\n");
                        break;
                    default:
                        printf("    status               = UNKNOWN  : error code is not known\n");
                        break;
                }
            } else {
                printf("    status               = 0 (OK)\n");
            }
            fflush(stdout);
            printf("    flags                = %d (%s %s)\n",urb.flags,(urb.flags&0x01?"USBDEVFS_URB_SHORT_NOT_OK":""),(urb.flags&0x02?"USBDEVFS_URB_ISO_ASAP":""));
            fflush(stdout);
            printf("    buffer_length        = %d\n",urb.buffer_length);
            fflush(stdout);
            printf("    actual_length        = %d\n",urb.actual_length);
            fflush(stdout);
            if (dumpData){
                printf("    BUFFER:\n");
                fflush(stdout);
                printf("      ");
                fflush(stdout);
                for (int ii = 0; ii<buffer_length; ii++ ){
                    unsigned char *cp = urb.buffer;
                    printf("0x%02x ",cp[ii]);
                    if (ii%16==15)
                        printf("\n      ");
                    fflush(stdout);
                }
                printf("\n");
                fflush(stdout);
            } else {
                printf("    BUFFER: (not shown)\n");
                fflush(stdout);
            }
            printf("    start_frame          = %d\n",urb.start_frame);
            fflush(stdout);
            printf("    number_of_packets    = %d\n",urb.number_of_packets);
            fflush(stdout);
            printf("    error_count          = %d\n",urb.error_count);
            fflush(stdout);
            printf("    signr                = %d\n",urb.signr);
            fflush(stdout);
            printf("    usercontext          = 0x--------\n");
            fflush(stdout);
         if (urb.type==0){
          printf("    iso_frame_desc[0].length:        %d\n", urb.iso_frame_desc[0].length);
          printf("    iso_frame_desc[0].actual_length: %d\n", urb.iso_frame_desc[0].actual_length);
          printf("    iso_frame_desc[0].status:        %d\n", urb.iso_frame_desc[0].status);
         }
            fflush(stdout);
            if (2==urb.type){
                struct usbdevfs_ctrltransfer txf;
                memcpy(&txf,urb.buffer,urb.buffer_length);
                printf("Control Packet Contents:\n");
                printf("        bRequestType     = (0x%02x) : %s | ", txf.bRequestType, ((txf.bRequestType&0x80)==0x80?"USB_DIR_IN":"USB_DIR_OUT"));
                switch (txf.bRequestType&(0x03 << 5)){
                    case (0x00 << 5): printf("USB_TYPE_STANDARD | "); break;
                    case (0x01 << 5): printf("USB_TYPE_CLASS | "); break;
                    case (0x02 << 5): printf("USB_TYPE_VENDOR | "); break;
                    case (0x03 << 5): printf("USB_TYPE_RESERVED | "); break;
                }
                switch (txf.bRequestType&0x1f){
                    case 0x00: printf("USB_RECIP_DEVICE\n"); break;
                    case 0x01: printf("USB_RECIP_INTERFACE\n"); break;
                    case 0x02: printf("USB_RECIP_ENDPOINT\n"); break;
                    case 0x03: printf("USB_RECIP_OTHER\n"); break;
                }
                printf("        bRequest         = ");
                switch (txf.bRequest){
                    case 0x00: printf("USB_REQ_GET_STATUS\n"); break;
                    case 0x01: printf("USB_REQ_CLEAR_FEATURE\n"); break;
                    case 0x03: printf("USB_REQ_SET_FEATURE\n"); break;
                    case 0x05: printf("USB_REQ_SET_ADDRESS\n"); break;
                    case 0x06: printf("USB_REQ_GET_DESCRIPTOR\n"); break;
                    case 0x07: printf("USB_REQ_SET_DESCRIPTOR\n"); break;
                    case 0x08: printf("USB_REQ_GET_CONFIGURATION\n"); break;
                    case 0x09: printf("USB_REQ_SET_CONFIGURATION\n"); break;
                    case 0x0A: printf("USB_REQ_GET_INTERFACE\n"); break;
                    case 0x0B: printf("USB_REQ_SET_INTERFACE\n"); break;
                    case 0x0C: printf("USB_REQ_SYNCH_FRAME\n"); break;
                    default: printf("%d\n", txf.bRequest); break;
                }
                printf("        wValue           = 0x%04x\n", txf.wValue);
                printf("        wIndex           = 0x%04x\n", txf.wIndex);
                printf("        wLength          = %d\n", txf.wLength);
                printf("        timeout          = %d\n", txf.timeout);
                printf("        data             = 0x--------");
                fflush(stdout);
            }
            break;
        default:
            printf("UNKNOWN %d\n",urb.type);
            buffer_length = 100;
            break;
    }
}

variable_types: struct usbdevfs_urb { unsigned char type; unsigned char endpoint; int status; unsigned int flags; void *buffer; int buffer_length; int actual_length; int start_frame; union { int number_of_packets; unsigned int stream_id; }; int error_count; unsigned int signr; void *usercontext; struct usbdevfs_iso_packet_desc { unsigned int length; unsigned int actual_length; unsigned int status; } iso_frame_desc[]; }

variable_types: dumpData int

variable_types: ii int

variable_types:
cp
unsigned char *

variable_types: struct usbdevfs_ctrltransfer { unsigned char bRequestType; unsigned char bRequest; unsigned short wValue; unsigned short wIndex; unsigned short wLength; unsigned int timeout; void *data; }

user_defined_types: struct usbdevfs_urb { unsigned char type; unsigned char endpoint; int status; unsigned int flags; void *buffer; int buffer_length; int actual_length; int start_frame; union { int number_of_packets; unsigned int stream_id; }; int error_count; unsigned int signr; void *usercontext; struct usbdevfs_iso_packet_desc iso_frame_desc[]; }


original_code:
void myset_del(myset *s, void *data)
{
 int pos = s->hash(data) % s->size;
 mylist **cl = &s->buckets[pos];
 mylist *dl;
 while(*cl){
  if(s->cmp((*cl)->data, data) == 0){
   dl = *cl;
   *cl = (*cl)->next;
   mylist_free(dl);
   s->len--;
   break;
  }
  cl = &(*cl)->next;
 }
}

canonical_original_code:
void myset_del(struct myset *s, void *data)
{
 int pos = s->hash(data) % s->size;
 struct _list **cl = &s->buckets[pos];
 struct _list *dl;
 while(*cl){
  if(s->cmp((*cl)->data, data) == 0){
   dl = *cl;
   *cl = (*cl)->next;
   mylist_free(dl);
   s->len--;
   break;
  }
  cl = &(*cl)->next;
 }
}

variable_types: struct myset { int size; struct _list { void *data; struct _list *next; } **buckets; int len; int (*cmp)(void *, void *); int (*hash)(void *); } *

variable_types:
data
void *

variable_types: pos int

user_defined_types: struct myset { int size; struct _list **buckets; int len; int (*cmp)(void *, void *); int (*hash)(void *); }


original_code:
int myset_find(myset *s, void *data)
{
 int pos = s->hash(data) % s->size;
 mylist *cl = s->buckets[pos];
 while(cl){
  if(s->cmp(cl->data, data) == 0)
   return 1;
  cl = cl->next;
 }
 return 0;
}

canonical_original_code:
int myset_find(struct myset *s, void *data)
{
 int pos = s->hash(data) % s->size;
 struct _list *cl = s->buckets[pos];
 while(cl){
  if(s->cmp(cl->data, data) == 0)
   return 1;
  cl = cl->next;
 }
 return 0;
}

variable_types: struct _list { void *data; struct _list *next; } *


decompiled_code:
__int64 __fastcall myset_add(__int64 a1, __int64 a2)
{
__int64 result; // rax
int v3; // er12

result = myset_find(a1, a2);
if ( !(_DWORD)result )
{
v3 = (*(int (__fastcall **)(__int64))(a1 + 32))(a2) % *(_DWORD *)a1;
result = (__int64)mylist_new(a2);
*(_QWORD *)(result + 8) = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v3);
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v3) = result;
++*(_DWORD *)(a1 + 16);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
__int64 result; // rax
int v3; // er12

result = func9(a1, a2);
if ( !(_DWORD)result )
{
v3 = (*(int (__fastcall **)(__int64))(a1 + 32))(a2) % *(_DWORD *)a1;
result = (__int64)mylist_new(a2);
*(_QWORD *)(result + 8) = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v3);
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * v3) = result;
++*(_DWORD *)(a1 + 16);
}
return result;
}

original_code:
void myset_add(myset *s, void *data)
{
 if(!myset_find(s, data)){
  int pos = s->hash(data) % s->size;
  mylist *l = mylist_new(data);
  l->next = s->buckets[pos];
  s->buckets[pos] = l;
  s->len++;
 }
}

canonical_original_code:
void myset_add(struct myset *s, void *data)
{
 if(!myset_find(s, data)){
  int pos = s->hash(data) % s->size;
  struct _list *l = mylist_new(data);
  l->next = s->buckets[pos];
  s->buckets[pos] = l;
  s->len++;
 }
}

user_defined_types: struct _list { void *data; struct _list *next; }


decompiled_code:
__int64 __fastcall myset_add_set(__int64 a1, __int64 a2)
{
__int64 i; // r14
__int64 result; // rax
__int64 *j; // rbx

for ( i = 0LL; *(_DWORD *)a2 > (int)i; ++i )
{
result = *(_QWORD *)(a2 + 8);
for ( j = *(__int64 **)(result + 8 * i); j; j = *(__int64 **)(result + 8 * i) )
{
myset_add(a1, *j);
*(_QWORD *)(*(_QWORD *)(a2 + 8) + 8 * i) = j[1];
mylist_free(j);
result = *(_QWORD *)(a2 + 8);
}
}
return result;
}

original_code:
void myset_add_set(myset *s, myset *d)
{
 mylist *l;
 int i = 0;
 for(i = 0; i < d->size; i++){
  while(d->buckets[i]){
   l = d->buckets[i];
   myset_add(s, l->data);
   d->buckets[i] = l->next;
   mylist_free(l);
  }
 }
}

canonical_original_code:
void myset_add_set(struct myset *s, struct myset *d)
{
 struct _list *l;
 int i = 0;
 for(i = 0; i < d->size; i++){
  while(d->buckets[i]){
   l = d->buckets[i];
   myset_add(s, l->data);
   d->buckets[i] = l->next;
   mylist_free(l);
  }
 }
}

variable_types:
d
struct myset *

variable_types: i int


decompiled_code:
__int64 __fastcall myset_length(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
int myset_length(myset *s)
{
 return s->len;
}

canonical_original_code:
int myset_length(struct myset *s)
{
 return s->len;
}


original_code:
void myset_clear(myset *s)
{
 mylist **cl, *dl;
 int i = 0;
 for(i = 0; i < s->size; i++){
  cl = &s->buckets[i];
  while(*cl){
   dl = *cl;
   *cl = (*cl)->next;
   cl = &(*cl)->next;
   mylist_free(dl);
  }
 }
 s->len = 0;
}

canonical_original_code:
void myset_clear(struct myset *s)
{
 struct _list **cl; struct _list *dl;
 int i = 0;
 for(i = 0; i < s->size; i++){
  cl = &s->buckets[i];
  while(*cl){
   dl = *cl;
   *cl = (*cl)->next;
   cl = &(*cl)->next;
   mylist_free(dl);
  }
 }
 s->len = 0;
}

variable_types: struct _list { void *data; struct _list *next; } **