decompiled_code:
void __fastcall send_igmp_mld_query(__int64 a1, int a2, _WORD *a3, _WORD *a4, __int64 *a5, unsigned int a6)
{
__int16 v6; // r15
__int64 v7; // rbp
_DWORD *v8; // r13
_WORD *v9; // r12
_DWORD *v11; // r11
const char *v12; // r8
__int64 v13; // rcx
int v14; // er15
const char *v15; // rax
_DWORD *v16; // r11
bool v17; // r10
int v18; // edx
__int64 v19; // r14
_DWORD *v20; // rbp
unsigned __int64 v21; // r13
int v22; // er14
unsigned __int16 v23; // ax
__int64 v24; // r15
__int16 v25; // ax
__int64 v26; // r11
int v27; // edx
__int16 v28; // ax
__int16 v29; // ax
__m128i v30; // xmm2
unsigned int v31; // eax
ssize_t v32; // rax
int *v33; // rax
char *v34; // rax
const char *v35; // rax
unsigned __int64 v36; // r14
__int64 v37; // rbp
int v38; // er12
__int16 v39; // ax
size_t v40; // r14
unsigned int v41; // er12
int v42; // eax
ssize_t v43; // rax
int v44; // edx
_WORD *bufa; // [rsp+8h] [rbp-E0h]
_WORD *bufb; // [rsp+8h] [rbp-E0h]
int v48; // [rsp+10h] [rbp-D8h]
unsigned int v49; // [rsp+10h] [rbp-D8h]
_DWORD *v50; // [rsp+10h] [rbp-D8h]
const char *fda; // [rsp+18h] [rbp-D0h]
int fd; // [rsp+18h] [rbp-D0h]
_DWORD *v53; // [rsp+20h] [rbp-C8h]
_DWORD *v54; // [rsp+20h] [rbp-C8h]
_WORD *v55; // [rsp+28h] [rbp-C0h]
__int64 v56[2]; // [rsp+30h] [rbp-B8h] BYREF
__int64 v57[2]; // [rsp+40h] [rbp-A8h] BYREF
__m128i v58; // [rsp+50h] [rbp-98h]
__int64 v59; // [rsp+60h] [rbp-88h]
struct msghdr message; // [rsp+70h] [rbp-78h] BYREF

if ( !a1 )
{
v12 = "p_if is NULL\n";
v13 = 71LL;
goto LABEL_11;
}
v6 = *(_WORD *)(a1 + 80);
v7 = a1;
v8 = a3;
if ( v6 != *a3 || (v9 = a4) != 0LL && v6 != *a4 )
{
v12 = "family don't match, can't send igmp query\n";
v13 = 80LL;
LABEL_11:
logger_build(1LL, "ERROR", "send_igmp_mld_query", v13, v12);
return;
}
v48 = *(_DWORD *)(a1 + 224);
v11 = calloc(v48, 1uLL);
if ( !v11 )
{
logger_build(0LL, "FATAL", "send_igmp_mld_query", 87LL, "malloc failed\n");
exit(1);
}
if ( v6 != 2 )
{
if ( v6 == 10 )
{
v54 = v11;
v24 = 24LL;
fd = get_igmp_mld_socket(10LL);
*v54 = 130;
v25 = _bswap_16(10000);
v27 = *(_DWORD *)(v26 + 24);
*(_WORD *)(v26 + 4) = v25;
*(_DWORD *)(v26 + 24) = v27 & 0xF0 | (8 * (_BYTE)a6) & 8 | 0x7D02;
if ( a2 == 3 )
{
if ( a5 && (v36 = v48 - 48 - 16LL, v36 > 0x1B) )
{
v37 = v26;
v55 = v9;
v38 = 28;
do
{
if ( *((_WORD *)a5 + 8) == 10 && (unsigned int)imp_source_is_scheduled(a5, a6) )
{
v38 += 16;
v39 = *(_WORD *)(v37 + 26) + 1;
*(__m128i *)(16LL * *(unsigned __int16 *)(v37 + 26) + v37 + 28) = _mm_loadu_si128((const __m128i *)(a5 + 3));
*(_WORD *)(v37 + 26) = v39;
}
a5 = (__int64 *)*a5;
v24 = v38;
}
while ( a5 && v36 >= v38 );
v26 = v37;
v7 = a1;
v9 = v55;
}
else
{
v24 = 28LL;
}
}
if ( v9 )
{
*(__m128i *)(v26 + 8) = _mm_loadu_si128((const __m128i *)(v9 + 4));
v28 = _bswap_16(1000);
*(_WORD *)(v26 + 4) = v28;
}
bufa = (_WORD *)v26;
v29 = in_cusm(v26, 28LL);
*(_OWORD *)&message.msg_namelen = 0LL;
v30 = _mm_loadu_si128((const __m128i *)(v7 + 88));
bufa[1] = v29;
message.msg_iov = (iovec *)v56;
message.msg_control = v57;
v57[1] = 0x3200000029LL;
v31 = *(_DWORD *)(v7 + 212);
v56[0] = (__int64)bufa;
*(_QWORD *)&message.msg_flags = 0LL;
v56[1] = v24;
message.msg_name = v8;
message.msg_namelen = 28;
message.msg_iovlen = 1LL;
message.msg_controllen = 40LL;
v57[0] = 36LL;
v59 = v31;
v58 = v30;
v32 = sendmsg(fd, &message, 0);
v11 = bufa;
if ( v32 < 0 )
{
v33 = _errno_location();
v34 = strerror(*v33);
logger_build(1LL, "ERROR", "send_igmp_mld_query", 248LL, "mld query: sendmsg failed: %s\n", v34);
free(bufa);
return;
}
}
goto LABEL_8;
}
v53 = v11;
*(_OWORD *)&message.msg_name = 0LL;
v14 = get_igmp_mld_socket(2LL);
fda = (const char *)imp_pi_ntoa(v8);
v15 = (const char *)imp_pi_ntoa(a1 + 80);
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 101LL, "src = %s dst = %s \n", v15, fda);
v49 = v48 - 24;
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 104LL, "max_len = %d\n", v49);
v16 = v53;
v17 = a2 == 3;
*(_BYTE *)v53 = 17;
if ( a2 == 1 )
{
if ( !v9 )
{
v44 = v53[2];
v40 = 8LL;
v41 = 8;
*((_WORD *)v53 + 1) = 0;
v53[2] = v44 & 0xF0 | (8 * (_BYTE)a6) & 8 | 0x7D02;
goto LABEL_40;
}
}
else
{
*((_BYTE *)v53 + 1) = 100;
if ( !v9 )
goto LABEL_15;
}
v35 = (const char *)imp_pi_ntoa(v9);
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 115LL, "g = %s \n", v35);
v16 = v53;
v17 = a2 == 3;
v53[1] = *((_DWORD *)v9 + 1);
*((_BYTE *)v53 + 1) = 10;
LABEL_15:
v18 = v16[2];
*((_WORD *)v16 + 1) = 0;
v16[2] = v18 & 0xF0 | (8 * (_BYTE)a6) & 8 | 0x7D02;
if ( a5 && v17 )
{
v19 = (int)v49;
v20 = v16;
v50 = v8;
v21 = v19 - 4;
v22 = 0;
do
{
if ( *((_WORD *)a5 + 8) == 2 && (unsigned int)imp_source_is_scheduled(a5, a6) )
{
v22 += 4;
v23 = *((_WORD *)v20 + 5);
v20[v23 + 3] = *((_DWORD *)a5 + 5);
*((_WORD *)v20 + 5) = v23 + 1;
}
a5 = (__int64 *)*a5;
}
while ( a5 && v22 <= v21 );
v16 = v20;
v8 = v50;
v7 = a1;
goto LABEL_39;
}
if ( a2 == 3 )
{
LABEL_39:
v40 = 4 * (unsigned int)*((unsigned __int16 *)v16 + 5) + 12;
v41 = 4 * *((unsigned __int16 *)v16 + 5) + 12;
goto LABEL_40;
}
v40 = 8LL;
v41 = 8;
LABEL_40:
bufb = v16;
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 155LL, "igh_len = %d\n", v41);
bufb[1] = in_cusm(bufb, v41);
setsockopt(v14, 0, 32, (const void *)(v7 + 84), 4u);
v42 = v8[1];
LOWORD(message.msg_name) = 2;
HIDWORD(message.msg_name) = v42;
v43 = sendto(v14, bufb, v40, 0, (const struct sockaddr *)&message, 0x10u);
v11 = bufb;
if ( v43 == -1 )
{
logger_build(1LL, "ERROR", "send_igmp_mld_query", 165LL, "sendto failed\n");
free(bufb);
return;
}
LABEL_8:
free(v11);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, int a2, _WORD *a3, _WORD *a4, __int64 *a5, unsigned int a6)
{
__int16 v6; // r15
__int64 v7; // rbp
_DWORD *v8; // r13
_WORD *v9; // r12
_DWORD *v11; // r11
const char *v12; // r8
__int64 v13; // rcx
int v14; // er15
const char *v15; // rax
_DWORD *v16; // r11
bool v17; // r10
int v18; // edx
__int64 v19; // r14
_DWORD *v20; // rbp
unsigned __int64 v21; // r13
int v22; // er14
unsigned __int16 v23; // ax
__int64 v24; // r15
__int16 v25; // ax
__int64 v26; // r11
int v27; // edx
__int16 v28; // ax
__int16 v29; // ax
__m128i v30; // xmm2
unsigned int v31; // eax
ssize_t v32; // rax
int *v33; // rax
char *v34; // rax
const char *v35; // rax
unsigned __int64 v36; // r14
__int64 v37; // rbp
int v38; // er12
__int16 v39; // ax
size_t v40; // r14
unsigned int v41; // er12
int v42; // eax
ssize_t v43; // rax
int v44; // edx
_WORD *bufa; // [rsp+8h] [rbp-E0h]
_WORD *bufb; // [rsp+8h] [rbp-E0h]
int v48; // [rsp+10h] [rbp-D8h]
unsigned int v49; // [rsp+10h] [rbp-D8h]
_DWORD *v50; // [rsp+10h] [rbp-D8h]
const char *fda; // [rsp+18h] [rbp-D0h]
int fd; // [rsp+18h] [rbp-D0h]
_DWORD *v53; // [rsp+20h] [rbp-C8h]
_DWORD *v54; // [rsp+20h] [rbp-C8h]
_WORD *v55; // [rsp+28h] [rbp-C0h]
__int64 v56[2]; // [rsp+30h] [rbp-B8h] BYREF
__int64 v57[2]; // [rsp+40h] [rbp-A8h] BYREF
__m128i v58; // [rsp+50h] [rbp-98h]
__int64 v59; // [rsp+60h] [rbp-88h]
struct msghdr message; // [rsp+70h] [rbp-78h] BYREF

if ( !a1 )
{
v12 = "p_if is NULL\n";
v13 = 71LL;
goto LABEL_11;
}
v6 = *(_WORD *)(a1 + 80);
v7 = a1;
v8 = a3;
if ( v6 != *a3 || (v9 = a4) != 0LL && v6 != *a4 )
{
v12 = "family don't match, can't send igmp query\n";
v13 = 80LL;
LABEL_11:
logger_build(1LL, "ERROR", "send_igmp_mld_query", v13, v12);
return;
}
v48 = *(_DWORD *)(a1 + 224);
v11 = calloc(v48, 1uLL);
if ( !v11 )
{
logger_build(0LL, "FATAL", "send_igmp_mld_query", 87LL, "malloc failed\n");
exit(1);
}
if ( v6 != 2 )
{
if ( v6 == 10 )
{
v54 = v11;
v24 = 24LL;
fd = get_igmp_mld_socket(10LL);
*v54 = 130;
v25 = func0(10000);
v27 = *(_DWORD *)(v26 + 24);
*(_WORD *)(v26 + 4) = v25;
*(_DWORD *)(v26 + 24) = v27 & 0xF0 | (8 * (_BYTE)a6) & 8 | 0x7D02;
if ( a2 == 3 )
{
if ( a5 && (v36 = v48 - 48 - 16LL, v36 > 0x1B) )
{
v37 = v26;
v55 = v9;
v38 = 28;
do
{
if ( *((_WORD *)a5 + 8) == 10 && (unsigned int)imp_source_is_scheduled(a5, a6) )
{
v38 += 16;
v39 = *(_WORD *)(v37 + 26) + 1;
*(__m128i *)(16LL * *(unsigned __int16 *)(v37 + 26) + v37 + 28) = _mm_loadu_si128((const __m128i *)(a5 + 3));
*(_WORD *)(v37 + 26) = v39;
}
a5 = (__int64 *)*a5;
v24 = v38;
}
while ( a5 && v36 >= v38 );
v26 = v37;
v7 = a1;
v9 = v55;
}
else
{
v24 = 28LL;
}
}
if ( v9 )
{
*(__m128i *)(v26 + 8) = _mm_loadu_si128((const __m128i *)(v9 + 4));
v28 = func0(1000);
*(_WORD *)(v26 + 4) = v28;
}
bufa = (_WORD *)v26;
v29 = in_cusm(v26, 28LL);
*(_OWORD *)&message.msg_namelen = 0LL;
v30 = _mm_loadu_si128((const __m128i *)(v7 + 88));
bufa[1] = v29;
message.msg_iov = (iovec *)v56;
message.msg_control = v57;
v57[1] = 0x3200000029LL;
v31 = *(_DWORD *)(v7 + 212);
v56[0] = (__int64)bufa;
*(_QWORD *)&message.msg_flags = 0LL;
v56[1] = v24;
message.msg_name = v8;
message.msg_namelen = 28;
message.msg_iovlen = 1LL;
message.msg_controllen = 40LL;
v57[0] = 36LL;
v59 = v31;
v58 = v30;
v32 = sendmsg(fd, &message, 0);
v11 = bufa;
if ( v32 < 0 )
{
v33 = _errno_location();
v34 = strerror(*v33);
logger_build(1LL, "ERROR", "send_igmp_mld_query", 248LL, "mld query: sendmsg failed: %s\n", v34);
free(bufa);
return;
}
}
goto LABEL_8;
}
v53 = v11;
*(_OWORD *)&message.msg_name = 0LL;
v14 = get_igmp_mld_socket(2LL);
fda = (const char *)imp_pi_ntoa(v8);
v15 = (const char *)imp_pi_ntoa(a1 + 80);
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 101LL, "src = %s dst = %s \n", v15, fda);
v49 = v48 - 24;
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 104LL, "max_len = %d\n", v49);
v16 = v53;
v17 = a2 == 3;
*(_BYTE *)v53 = 17;
if ( a2 == 1 )
{
if ( !v9 )
{
v44 = v53[2];
v40 = 8LL;
v41 = 8;
*((_WORD *)v53 + 1) = 0;
v53[2] = v44 & 0xF0 | (8 * (_BYTE)a6) & 8 | 0x7D02;
goto LABEL_40;
}
}
else
{
*((_BYTE *)v53 + 1) = 100;
if ( !v9 )
goto LABEL_15;
}
v35 = (const char *)imp_pi_ntoa(v9);
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 115LL, "g = %s \n", v35);
v16 = v53;
v17 = a2 == 3;
v53[1] = *((_DWORD *)v9 + 1);
*((_BYTE *)v53 + 1) = 10;
LABEL_15:
v18 = v16[2];
*((_WORD *)v16 + 1) = 0;
v16[2] = v18 & 0xF0 | (8 * (_BYTE)a6) & 8 | 0x7D02;
if ( a5 && v17 )
{
v19 = (int)v49;
v20 = v16;
v50 = v8;
v21 = v19 - 4;
v22 = 0;
do
{
if ( *((_WORD *)a5 + 8) == 2 && (unsigned int)imp_source_is_scheduled(a5, a6) )
{
v22 += 4;
v23 = *((_WORD *)v20 + 5);
v20[v23 + 3] = *((_DWORD *)a5 + 5);
*((_WORD *)v20 + 5) = v23 + 1;
}
a5 = (__int64 *)*a5;
}
while ( a5 && v22 <= v21 );
v16 = v20;
v8 = v50;
v7 = a1;
goto LABEL_39;
}
if ( a2 == 3 )
{
LABEL_39:
v40 = 4 * (unsigned int)*((unsigned __int16 *)v16 + 5) + 12;
v41 = 4 * *((unsigned __int16 *)v16 + 5) + 12;
goto LABEL_40;
}
v40 = 8LL;
v41 = 8;
LABEL_40:
bufb = v16;
logger_build(4LL, "DEBUG", "send_igmp_mld_query", 155LL, "igh_len = %d\n", v41);
bufb[1] = in_cusm(bufb, v41);
setsockopt(v14, 0, 32, (const void *)(v7 + 84), 4u);
v42 = v8[1];
LOWORD(message.msg_name) = 2;
HIDWORD(message.msg_name) = v42;
v43 = sendto(v14, bufb, v40, 0, (const struct sockaddr *)&message, 0x10u);
v11 = bufb;
if ( v43 == -1 )
{
logger_build(1LL, "ERROR", "send_igmp_mld_query", 165LL, "sendto failed\n");
free(bufb);
return;
}
LABEL_8:
free(v11);
}

original_code:
void send_igmp_mld_query(imp_interface *p_if, im_version version,
    pi_addr *p_dst, pi_addr *p_gp_addr, imp_source* p_sources, int sflag)
{
    int family = 0;
    char *p = ((void *)0);
    int max_len = 0;
    if(p_if == ((void *)0)) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 71, "p_if is NULL\n");
        return;
    }
    family = p_if->if_addr.ss.ss_family;
    if(family != p_dst->ss.ss_family || (p_gp_addr != ((void *)0) &&
        family != p_gp_addr->ss.ss_family)) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 80, "family don't match, can't send igmp query\n");
        return;
    }
    max_len = p_if->if_mtu;
    if((p = malloc(max_len)) == ((void *)0)){
       logger_build(LOG_PRI_FATAL, "FATAL", __FUNCTION__, 87, "malloc failed\n");
       exit(1);
    }
    bzero(p, max_len);
    if(family == 2) {
        struct igmpv3_query* p_igh;
        struct sockaddr_in din;
        int igh_len = 0;
        int socket = get_igmp_mld_socket(2);
        bzero(&din, sizeof(struct sockaddr_in));
        logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 101, "src = %s dst = %s \n", imp_pi_ntoa(&p_if->if_addr), imp_pi_ntoa(p_dst));
        max_len -= (sizeof(struct iphdr) + 4);
        logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 104, "max_len = %d\n", max_len);
        p_igh = (struct igmpv3_query*)p;
        p_igh->type = 0x11;
        if(version != IM_IGMPv1)
            p_igh->code = 10 * 10; ;
        if (p_gp_addr != ((void *)0)) {
            logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 115, "g = %s \n", imp_pi_ntoa(p_gp_addr));
            p_igh->group = p_gp_addr->v4.sin_addr.s_addr;
            p_igh->code = 1 * 10;
        }
        p_igh->csum = 0;
        p_igh->qrv = 2;
        p_igh->qqic = 125;
        p_igh->nsrcs = 0;
        p_igh->suppress = sflag;
        while(p_sources && version == IM_IGMPv3_MLDv2) {
            if(igh_len > max_len - sizeof(struct in_addr))
                break;
            if(p_sources->src_addr.ss.ss_family == 2) {
                if(imp_source_is_scheduled(p_sources, sflag) == 0) {
                    p_sources = ((p_sources)->link.le_next);
                    continue;
                }
                p_igh->srcs[p_igh->nsrcs] = p_sources->src_addr.v4.sin_addr.s_addr;
                p_igh->nsrcs++;
                igh_len += sizeof(int);
            }
            p_sources = ((p_sources)->link.le_next);
        }
        if(version == IM_IGMPv3_MLDv2)
            igh_len = sizeof(struct igmpv3_query) + p_igh->nsrcs * sizeof(int);
        else
            igh_len = sizeof(struct igmphdr);
        logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 155, "igh_len  = %d\n", igh_len);
        p_igh->csum = in_cusm((unsigned short*)p_igh, igh_len);
        setsockopt(socket, IPPROTO_IP, 32,
            &p_if->if_addr.v4.sin_addr, sizeof(struct in_addr));
        din.sin_family = 2;
        din.sin_addr.s_addr = p_dst->v4.sin_addr.s_addr;
        if(sendto(socket, p_igh, igh_len, 0, (struct sockaddr*)&din, sizeof(din)) == -1){
            logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 165, "sendto failed\n");
        }
    }else if(family== 10) {
        struct imp_mldv2_query_hdr *p_hdr;
        int p_hdr_len;
        int socket = get_igmp_mld_socket(10);
        max_len -= (sizeof(struct ip6_hdr) + 8);
        p_hdr = (struct imp_mldv2_query_hdr *)p;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_type = 130;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_code = 0;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_cksum = 0;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_dataun.icmp6_un_data16[0] = htons(10000);
        p_hdr->suppress = sflag;
        p_hdr->qrv = 2;
        p_hdr->qqic = 125;
        p_hdr->nsrcs = 0;
        if (version == IM_IGMPv3_MLDv2)
            p_hdr_len = sizeof(struct imp_mldv2_query_hdr);
        else
            p_hdr_len = sizeof(struct mld_hdr);
        while(p_sources && version == IM_IGMPv3_MLDv2) {
            if(p_hdr_len > max_len - sizeof(struct in6_addr))
                break;
            if(p_sources->src_addr.ss.ss_family == 10) {
                if(imp_source_is_scheduled(p_sources, sflag) == 0) {
                    p_sources = ((p_sources)->link.le_next);
                    continue;
                }
                memcpy(&p_hdr->srcs[p_hdr->nsrcs],
                    &p_sources->src_addr.v6.sin6_addr, sizeof(struct in6_addr));
                p_hdr->nsrcs++;
                p_hdr_len += sizeof(struct in6_addr);
            }
            p_sources = ((p_sources)->link.le_next);
       }
       if (p_gp_addr != ((void *)0)) {
            memcpy(&p_hdr->mldh.mld_addr, &p_gp_addr->v6.sin6_addr,
                sizeof(struct in6_addr));
            p_hdr->mldh.mld_icmp6_hdr.icmp6_dataun.icmp6_un_data16[0] = htons(1000);
        }
        p_hdr->mldh.mld_icmp6_hdr.icmp6_cksum = in_cusm((unsigned short*)p_hdr,
            sizeof(struct imp_mldv2_query_hdr));
        unsigned char cbuf[((((sizeof(struct in6_pktinfo)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)))] = {0};
        struct iovec iov = {
            p_hdr,
            p_hdr_len
        };
        struct msghdr msg = {
            .msg_name = (void *)&p_dst->v6,
            .msg_namelen = sizeof(p_dst->v6),
            .msg_iov = &iov,
            .msg_iovlen = 1,
            .msg_control = cbuf,
            .msg_controllen = sizeof(cbuf)
        };
        struct cmsghdr *cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
        cmsg->cmsg_level = IPPROTO_IPV6;
        cmsg->cmsg_type = 50;
        cmsg->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(struct in6_pktinfo)));
        struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)((cmsg)->__cmsg_data);
        pktinfo->ipi6_ifindex = p_if->if_index;
        memcpy(&pktinfo->ipi6_addr, &p_if->if_addr.v6.sin6_addr, sizeof(struct in6_addr));
        if (sendmsg(socket, &msg, 0) < 0) {
            logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 248, "mld query: sendmsg failed: %s\n", strerror((*__errno_location ())));
        }
    }
    free(p);
    return;
}

canonical_original_code:
void send_igmp_mld_query(struct imp_interface *p_if, enum im_version version, union _pi_addr *p_dst, union _pi_addr *p_gp_addr, struct imp_source *p_sources, int sflag)
{
    int family = 0;
    char *p = ((void *)0);
    int max_len = 0;
    if(p_if == ((void *)0)) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 71, "p_if is NULL\n");
        return;
    }
    family = p_if->if_addr.ss.ss_family;
    if(family != p_dst->ss.ss_family || (p_gp_addr != ((void *)0) &&
        family != p_gp_addr->ss.ss_family)) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 80, "family don't match, can't send igmp query\n");
        return;
    }
    max_len = p_if->if_mtu;
    if((p = malloc(max_len)) == ((void *)0)){
       logger_build(LOG_PRI_FATAL, "FATAL", __FUNCTION__, 87, "malloc failed\n");
       exit(1);
    }
    bzero(p, max_len);
    if(family == 2) {
        struct igmpv3_query *p_igh;
        struct sockaddr_in din;
        int igh_len = 0;
        int socket = get_igmp_mld_socket(2);
        bzero(&din, sizeof(struct sockaddr_in));
        logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 101, "src = %s dst = %s \n", imp_pi_ntoa(&p_if->if_addr), imp_pi_ntoa(p_dst));
        max_len -= (sizeof(struct iphdr) + 4);
        logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 104, "max_len = %d\n", max_len);
        p_igh = (struct igmpv3_query *)p;
        p_igh->type = 0x11;
        if(version != IM_IGMPv1)
            p_igh->code = 10 * 10; ;
        if (p_gp_addr != ((void *)0)) {
            logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 115, "g = %s \n", imp_pi_ntoa(p_gp_addr));
            p_igh->group = p_gp_addr->v4.sin_addr.s_addr;
            p_igh->code = 1 * 10;
        }
        p_igh->csum = 0;
        p_igh->qrv = 2;
        p_igh->qqic = 125;
        p_igh->nsrcs = 0;
        p_igh->suppress = sflag;
        while(p_sources && version == IM_IGMPv3_MLDv2) {
            if(igh_len > max_len - sizeof(struct in_addr))
                break;
            if(p_sources->src_addr.ss.ss_family == 2) {
                if(imp_source_is_scheduled(p_sources, sflag) == 0) {
                    p_sources = ((p_sources)->link.le_next);
                    continue;
                }
                p_igh->srcs[p_igh->nsrcs] = p_sources->src_addr.v4.sin_addr.s_addr;
                p_igh->nsrcs++;
                igh_len += sizeof(int);
            }
            p_sources = ((p_sources)->link.le_next);
        }
        if(version == IM_IGMPv3_MLDv2)
            igh_len = sizeof(struct igmpv3_query) + p_igh->nsrcs * sizeof(int);
        else
            igh_len = sizeof(struct igmphdr);
        logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 155, "igh_len  = %d\n", igh_len);
        p_igh->csum = in_cusm((unsigned short *)p_igh, igh_len);
        setsockopt(socket, IPPROTO_IP, 32,
            &p_if->if_addr.v4.sin_addr, sizeof(struct in_addr));
        din.sin_family = 2;
        din.sin_addr.s_addr = p_dst->v4.sin_addr.s_addr;
        if(sendto(socket, p_igh, igh_len, 0, (struct sockaddr *)&din, sizeof(din)) == -1){
            logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 165, "sendto failed\n");
        }
    }else if(family== 10) {
        struct imp_mldv2_query_hdr *p_hdr;
        int p_hdr_len;
        int socket = get_igmp_mld_socket(10);
        max_len -= (sizeof(struct ip6_hdr) + 8);
        p_hdr = (struct imp_mldv2_query_hdr *)p;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_type = 130;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_code = 0;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_cksum = 0;
        p_hdr->mldh.mld_icmp6_hdr.icmp6_dataun.icmp6_un_data16[0] = htons(10000);
        p_hdr->suppress = sflag;
        p_hdr->qrv = 2;
        p_hdr->qqic = 125;
        p_hdr->nsrcs = 0;
        if (version == IM_IGMPv3_MLDv2)
            p_hdr_len = sizeof(struct imp_mldv2_query_hdr);
        else
            p_hdr_len = sizeof(struct mld_hdr);
        while(p_sources && version == IM_IGMPv3_MLDv2) {
            if(p_hdr_len > max_len - sizeof(struct in6_addr))
                break;
            if(p_sources->src_addr.ss.ss_family == 10) {
                if(imp_source_is_scheduled(p_sources, sflag) == 0) {
                    p_sources = ((p_sources)->link.le_next);
                    continue;
                }
                memcpy(&p_hdr->srcs[p_hdr->nsrcs],
                    &p_sources->src_addr.v6.sin6_addr, sizeof(struct in6_addr));
                p_hdr->nsrcs++;
                p_hdr_len += sizeof(struct in6_addr);
            }
            p_sources = ((p_sources)->link.le_next);
       }
       if (p_gp_addr != ((void *)0)) {
            memcpy(&p_hdr->mldh.mld_addr, &p_gp_addr->v6.sin6_addr,
                sizeof(struct in6_addr));
            p_hdr->mldh.mld_icmp6_hdr.icmp6_dataun.icmp6_un_data16[0] = htons(1000);
        }
        p_hdr->mldh.mld_icmp6_hdr.icmp6_cksum = in_cusm((unsigned short *)p_hdr,
            sizeof(struct imp_mldv2_query_hdr));
        unsigned char cbuf[-1] = {0};
        struct iovec iov = {
            p_hdr,
            p_hdr_len
        };
        struct msghdr msg = {
            .msg_name = (void *)&p_dst->v6,
            .msg_namelen = sizeof(p_dst->v6),
            .msg_iov = &iov,
            .msg_iovlen = 1,
            .msg_control = cbuf,
            .msg_controllen = sizeof(cbuf)
        };
        struct cmsghdr *cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
        cmsg->cmsg_level = IPPROTO_IPV6;
        cmsg->cmsg_type = 50;
        cmsg->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(struct in6_pktinfo)));
        struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)((cmsg)->__cmsg_data);
        pktinfo->ipi6_ifindex = p_if->if_index;
        memcpy(&pktinfo->ipi6_addr, &p_if->if_addr.v6.sin6_addr, sizeof(struct in6_addr));
        if (sendmsg(socket, &msg, 0) < 0) {
            logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 248, "mld query: sendmsg failed: %s\n", strerror((*__errno_location ())));
        }
    }
    free(p);
    return;
}

variable_types: struct imp_interface { struct { struct imp_interface *le_next; struct imp_interface **le_prev; } link; char if_name[64]; union _pi_addr { 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]; } v4; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } v6; struct sockaddr { unsigned short sa_family; char sa_data[14]; } sa; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } ss; } if_addr; enum if_type { INTERFACE_DOWNSTREAM=0, INTERFACE_UPSTREAM=1 } type; int if_index; int if_igmp_socket; int if_mld_socket; int if_mtu; int startup; struct { struct imp_group { struct { struct imp_group *le_next; struct imp_group **le_prev; } link; int if_index; struct imp_interface *parent_if; union _pi_addr group_addr; enum group_type { GROUP_EXCLUDE=0, GROUP_INCLUDE=1, GROUP_UNDEFINED=2 } type; enum im_version { IM_DISABLE=0, IM_IGMPv1=1, IM_IGMPv2_MLDv1=2, IM_IGMPv3_MLDv2=3 } version; struct imp_timer { struct { struct imp_timer *le_next; struct imp_timer **le_prev; } link; struct timeval { long tv_sec; long tv_usec; } tm; struct imp_timer *(*expire)(void *); void *expire_data; } *timer; struct imp_timer *v1_timer; struct imp_timer *v2_timer; struct imp_timer *sch_timer; int gs_sch_times; struct { struct imp_source { struct { struct imp_source *le_next; struct imp_source **le_prev; } link; union _pi_addr src_addr; struct imp_timer *timer; struct imp_group *parent_gp; int times; } *lh_first; } src_list; } *lh_first; } group_list; struct imp_timer *gq_timer; } *

variable_types: sflag int

variable_types: family int

variable_types:
p
char *

variable_types: struct igmpv3_query { unsigned char type; unsigned char code; unsigned short csum; unsigned int group; unsigned char qrv; unsigned char qqic; unsigned short nsrcs; unsigned int srcs[]; } *

variable_types: socket int

variable_types: struct imp_mldv2_query_hdr { struct mld_hdr { struct icmp6_hdr { unsigned char icmp6_type; unsigned char icmp6_code; unsigned short icmp6_cksum; union { unsigned int icmp6_un_data32[1]; unsigned short icmp6_un_data16[2]; unsigned char icmp6_un_data8[4]; } icmp6_dataun; } mld_icmp6_hdr; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } mld_addr; } mldh; unsigned char qrv; unsigned char qqic; unsigned short nsrcs; struct in6_addr srcs[]; } *

variable_types:
cbuf
unsigned char [-1]

variable_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec { void *iov_base; unsigned long iov_len; } *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

variable_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types: struct in6_pktinfo { struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ipi6_addr; unsigned int ipi6_ifindex; } *

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

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

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

user_defined_types: struct igmphdr { unsigned char type; unsigned char code; unsigned short csum; unsigned int group; }

user_defined_types: struct ip6_hdr { union { struct ip6_hdrctl ip6_un1; unsigned char ip6_un2_vfc; } ip6_ctlun; struct in6_addr ip6_src; struct in6_addr ip6_dst; }

user_defined_types: struct imp_mldv2_query_hdr { struct mld_hdr mldh; unsigned char qrv; unsigned char qqic; unsigned short nsrcs; struct in6_addr srcs[]; }

user_defined_types: struct mld_hdr { struct icmp6_hdr mld_icmp6_hdr; struct in6_addr mld_addr; }

user_defined_types: struct in6_pktinfo { struct in6_addr ipi6_addr; unsigned int ipi6_ifindex; }

user_defined_types: struct imp_interface { struct { struct imp_interface *le_next; struct imp_interface **le_prev; } link; char if_name[64]; union _pi_addr if_addr; enum if_type type; int if_index; int if_igmp_socket; int if_mld_socket; int if_mtu; int startup; struct { struct imp_group *lh_first; } group_list; struct imp_timer *gq_timer; }

user_defined_types: union _pi_addr { struct sockaddr_in v4; struct sockaddr_in6 v6; struct sockaddr sa; struct sockaddr_storage ss; }

user_defined_types: struct ip6_hdrctl { unsigned int ip6_un1_flow; unsigned short ip6_un1_plen; unsigned char ip6_un1_nxt; unsigned char ip6_un1_hlim; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct imp_group { struct { struct imp_group *le_next; struct imp_group **le_prev; } link; int if_index; struct imp_interface *parent_if; union _pi_addr group_addr; enum group_type type; enum im_version version; struct imp_timer *timer; struct imp_timer *v1_timer; struct imp_timer *v2_timer; struct imp_timer *sch_timer; int gs_sch_times; struct { struct imp_source *lh_first; } src_list; }


original_code:
void init_point_list(){
    p_pool.head = ((void *)0);
    p_pool.points_count = 0;
    int i;
    for(i=0; i<100; i++){
        p_pool.ipoint[i] = 0;
    }
}

variable_types: i int

return_type: void


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

result = 99LL;
do
{
if ( !*((_DWORD *)&p_pool + result + 601) )
return (unsigned int)result;
}
while ( result-- != 0 );
return 0xFFFFFFFFLL;
}

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

result = 99LL;
do
{
if ( !*((_DWORD *)&p_pool + result + 601) )
return (unsigned int)result;
}
while ( result-- != 0 );
return 0xFFFFFFFFLL;
}

original_code:
int find_free_pos(){
    int i=100 -1;
    while(i>=0){
        if(p_pool.ipoint[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}

canonical_original_code:
int find_free_pos(){
    int i = 100 -1;
    while(i>=0){
        if(p_pool.ipoint[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}

return_type: int


decompiled_code:
_BOOL8 is_pp_empty()
{
return *((_DWORD *)&p_pool + 600) == 0;
}

canonical_decompiled_code:
_BOOL8 func2()
{
return *((_DWORD *)&p_pool + 600) == 0;
}

original_code:
BOOL is_pp_empty(){
    if(p_pool.points_count == 0){
        return 1;
    }else{
        return 0;
    }
}

canonical_original_code:
int is_pp_empty(){
    if(p_pool.points_count == 0){
        return 1;
    }else{
        return 0;
    }
}


decompiled_code:
__int64 top_point()
{
return *(_QWORD *)p_pool[351];
}

canonical_decompiled_code:
__int64 func3()
{
return *(_QWORD *)p_pool[351];
}

original_code:
struct point top_point(){
     return p_pool.head->p;
 }

return_type: struct point { int x; int y; }


original_code:
struct point pop_point(){
    struct point p_rst;
    p_rst.x = p_pool.head->p.x;
    p_rst.y = p_pool.head->p.y;
    p_pool.points_count--;
    int freenode = p_pool.head->pos;
    p_pool.ipoint[freenode] = 0;
    p_pool.head = p_pool.head->next;
    return p_rst;
}

variable_types: freenode int


decompiled_code:
int __fastcall print_point(unsigned int *a1)
{
return printf("%d %d \n", *a1, a1[1]);
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1)
{
return printf("%d %d \n", *a1, a1[1]);
}

original_code:
void print_point(struct point* p){
    printf("%d %d \n", p->x, p->y);
}

canonical_original_code:
void print_point(struct point *p){
    printf("%d %d \n", p->x, p->y);
}

variable_types:
p
struct point { int x; int y; } *


decompiled_code:
void __fastcall p_quicksort(int *a1, int *a2)
{
__int64 *v2; // rbx

for ( ; a1 != a2; a1 = (int *)v2[2] )
{
v2 = get_partion(a1, a2);
p_quicksort(a1, v2);
}
}

canonical_decompiled_code:
void __fastcall func6(int *a1, int *a2)
{
__int64 *v2; // rbx

for ( ; a1 != a2; a1 = (int *)v2[2] )
{
v2 = get_partion(a1, a2);
func6(a1, v2);
}
}

original_code:
void p_quicksort(struct point_node *p_begin, struct point_node *p_end){
    if(p_begin != p_end){
        struct point_node *partion = get_partion(p_begin, p_end);
        p_quicksort(p_begin, partion);
        p_quicksort(partion->next, p_end);
    }
}

variable_types: struct point_node { struct point { int x; int y; } p; int pos; struct point_node *next; } *

user_defined_types: struct point_node { struct point p; int pos; struct point_node *next; }


decompiled_code:
void points_quicksort()
{
p_quicksort((int *)p_pool[351], *(int **)(p_pool[352] + 16));
}

original_code:
void points_quicksort(){
    p_quicksort(p_pool.head, p_pool.tail->next);
}


min

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

result = a2;
if ( (int)a1 <= (int)a2 )
result = a1;
return result;
}

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

result = a2;
if ( (int)a1 <= (int)a2 )
result = a1;
return result;
}

original_code:
int min(int x, int y){
    if(x <= y){
        return x;
    }else{
        return y;
    }
}

variable_types: x int

variable_types: y int


max

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

result = a2;
if ( (int)a1 >= (int)a2 )
result = a1;
return result;
}

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

result = a2;
if ( (int)a1 >= (int)a2 )
result = a1;
return result;
}

original_code:
int max(int x, int y){
    if(x >= y){
        return x;
    }else{
        return y;
    }
}


decompiled_code:
double __fastcall cross_product(__int64 a1, __int64 a2, __int64 a3)
{
return (double)((HIDWORD(a2) - HIDWORD(a3)) * ((int)a1 - (int)a3) - (HIDWORD(a1) - HIDWORD(a3)) * ((int)a2 - (int)a3));
}

canonical_decompiled_code:
double __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
return (double)((HIDWORD(a2) - HIDWORD(a3)) * ((int)a1 - (int)a3) - (HIDWORD(a1) - HIDWORD(a3)) * ((int)a2 - (int)a3));
}

original_code:
double cross_product(point_t p1, point_t p2, point_t p0){
    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
}

canonical_original_code:
double cross_product(struct point p1, struct point p2, struct point p0){
    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
}

return_type: double


direction

decompiled_code:
double __fastcall direction(__int64 a1, __int64 a2, __int64 a3)
{
return cross_product(a3, a2, a1);
}

canonical_decompiled_code:
double __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
return func2(a3, a2, a1);
}

original_code:
double direction(point_t pi, point_t pj, point_t pk){
    return cross_product(pk, pj, pi);
}

canonical_original_code:
double direction(struct point pi, struct point pj, struct point pk){
    return cross_product(pk, pj, pi);
}

variable_types:
pi
struct point { int x; int y; }

variable_types:
pj
struct point

variable_types:
pk
struct point


decompiled_code:
__int64 __fastcall on_segment(unsigned int a1, __int64 a2)
{
unsigned int v2; // er11
unsigned int v3; // edi
int v4; // eax
int v5; // er11
int v6; // edx
int v7; // er8
int v8; // er9
int v9; // eax
int v10; // ecx
int v11; // er10

min(a1, a2);
max(a1, a2);
v3 = v2;
min(v2, HIDWORD(a2));
v4 = max(v3, HIDWORD(a2));
v5 = v4;
LOBYTE(v4) = v6 <= v7;
LOBYTE(v6) = v8 <= v6;
v9 = v6 & v4;
LOBYTE(v6) = v11 >= v10;
return (v5 >= v11) & v6 & (unsigned int)v9;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, __int64 a2)
{
unsigned int v2; // er11
unsigned int v3; // edi
int v4; // eax
int v5; // er11
int v6; // edx
int v7; // er8
int v8; // er9
int v9; // eax
int v10; // ecx
int v11; // er10

func0(a1, a2);
func1(a1, a2);
v3 = v2;
func0(v2, HIDWORD(a2));
v4 = func1(v3, HIDWORD(a2));
v5 = v4;
LOBYTE(v4) = v6 <= v7;
LOBYTE(v6) = v8 <= v6;
v9 = v6 & v4;
LOBYTE(v6) = v11 >= v10;
return (v5 >= v11) & v6 & (unsigned int)v9;
}

original_code:
BOOL on_segment(point_t pi, point_t pj, point_t pk){
    int minx = min(pi.x, pj.x);
    int maxx = max(pi.x, pj.x);
    int miny = min(pi.y, pj.y);
    int maxy = max(pi.y, pj.y);
    if(pk.x >= minx && pk.x<=maxx && pk.y >= miny && pk.y <= maxy){
        return 1;
    }else{
        return 0;
    }
}

canonical_original_code:
int on_segment(struct point pi, struct point pj, struct point pk){
    int minx = min(pi.x, pj.x);
    int maxx = max(pi.x, pj.x);
    int miny = min(pi.y, pj.y);
    int maxy = max(pi.y, pj.y);
    if(pk.x >= minx && pk.x<=maxx && pk.y >= miny && pk.y <= maxy){
        return 1;
    }else{
        return 0;
    }
}

variable_types: minx int

variable_types: maxx int

variable_types: miny int

variable_types: maxy int


decompiled_code:
__int64 __fastcall seg_intersection(__int64 a1, __int64 a2, __int64 a3, __int64 a4, _DWORD *a5)
{
double v5; // xmm2_8
__int64 result; // rax

v5 = (double)((HIDWORD(a3) - HIDWORD(a4)) * ((int)a1 - (int)a3) - ((int)a3 - (int)a4) * (HIDWORD(a1) - HIDWORD(a3)))
/ (double)((HIDWORD(a3) - HIDWORD(a4)) * ((int)a1 - (int)a2) - ((int)a3 - (int)a4) * (HIDWORD(a1) - HIDWORD(a2)));
*a5 = (int)((double)((int)a2 - (int)a1) * v5 + (double)(int)a1);
result = (unsigned int)(int)((double)(HIDWORD(a2) - HIDWORD(a1)) * v5 + (double)SHIDWORD(a1));
a5[1] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, _DWORD *a5)
{
double v5; // xmm2_8
__int64 result; // rax

v5 = (double)((HIDWORD(a3) - HIDWORD(a4)) * ((int)a1 - (int)a3) - ((int)a3 - (int)a4) * (HIDWORD(a1) - HIDWORD(a3)))
/ (double)((HIDWORD(a3) - HIDWORD(a4)) * ((int)a1 - (int)a2) - ((int)a3 - (int)a4) * (HIDWORD(a1) - HIDWORD(a2)));
*a5 = (int)((double)((int)a2 - (int)a1) * v5 + (double)(int)a1);
result = (unsigned int)(int)((double)(HIDWORD(a2) - HIDWORD(a1)) * v5 + (double)SHIDWORD(a1));
a5[1] = result;
return result;
}

original_code:
void seg_intersection(point_t u1, point_t u2, point_t v1, point_t v2, point_t *rst){
    *rst = u1;
    double t=(double)((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
        /(double)((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    rst->x = rst->x + ((u2.x-u1.x)*t);
    rst->y = rst->y + ((u2.y-u1.y)*t);
}

canonical_original_code:
void seg_intersection(struct point u1, struct point u2, struct point v1, struct point v2, struct point *rst){
    *rst = u1;
    double t = (double)((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
        /(double)((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    rst->x = rst->x + ((u2.x-u1.x)*t);
    rst->y = rst->y + ((u2.y-u1.y)*t);
}

variable_types:
rst
struct point *

variable_types: t double


decompiled_code:
__int64 __fastcall get_seg_intersection(__int64 a1, __int64 a2, __int64 a3, __int64 a4, _DWORD *a5)
{
double v8; // xmm1_8
double v9; // xmm2_8
double v10; // xmm3_8
double v11; // xmm4_8
double v12; // xmm5_8
__int64 result; // rax

v8 = direction(a3, a4, a1);
v9 = direction(a3, a4, a2);
v10 = direction(a1, a2, a3);
v11 = direction(a1, a2, a4);
if ( v12 < 0.0 && v10 * v11 < 0.0
|| v8 == 0.0 && (unsigned int)on_segment(a3, a4)
|| v9 == 0.0 && (unsigned int)on_segment(a3, a4)
|| v10 == 0.0 && (unsigned int)on_segment(a1, a2) )
{
goto LABEL_13;
}
if ( v11 != 0.0 )
return 0LL;
result = on_segment(a1, a2);
if ( (_DWORD)result )
{
LABEL_13:
seg_intersection(a1, a2, a3, a4, a5);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3, __int64 a4, _DWORD *a5)
{
double v8; // xmm1_8
double v9; // xmm2_8
double v10; // xmm3_8
double v11; // xmm4_8
double v12; // xmm5_8
__int64 result; // rax

v8 = func3(a3, a4, a1);
v9 = func3(a3, a4, a2);
v10 = func3(a1, a2, a3);
v11 = func3(a1, a2, a4);
if ( v12 < 0.0 && v10 * v11 < 0.0
|| v8 == 0.0 && (unsigned int)func4(a3, a4)
|| v9 == 0.0 && (unsigned int)func4(a3, a4)
|| v10 == 0.0 && (unsigned int)func4(a1, a2) )
{
goto LABEL_13;
}
if ( v11 != 0.0 )
return 0LL;
result = func4(a1, a2);
if ( (_DWORD)result )
{
LABEL_13:
func5(a1, a2, a3, a4, a5);
result = 1LL;
}
return result;
}

original_code:
BOOL get_seg_intersection(point_t p1, point_t p2, point_t p3, point_t p4, point_t *rst){
    double d1 = direction(p3, p4, p1);
    double d2 = direction(p3, p4, p2);
    double d3 = direction(p1, p2, p3);
    double d4 = direction(p1, p2, p4);
    if(d1*d2 < 0 && d3*d4 < 0){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d1 == 0 && on_segment(p3, p4, p1)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d2 == 0 && on_segment(p3, p4, p2)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d3 == 0 && on_segment(p1, p2, p3)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d4 == 0 && on_segment(p1, p2, p4)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }
    return 0;
}

canonical_original_code:
int get_seg_intersection(struct point p1, struct point p2, struct point p3, struct point p4, struct point *rst){
    double d1 = direction(p3, p4, p1);
    double d2 = direction(p3, p4, p2);
    double d3 = direction(p1, p2, p3);
    double d4 = direction(p1, p2, p4);
    if(d1*d2 < 0 && d3*d4 < 0){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d1 == 0 && on_segment(p3, p4, p1)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d2 == 0 && on_segment(p3, p4, p2)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d3 == 0 && on_segment(p1, p2, p3)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }else if(d4 == 0 && on_segment(p1, p2, p4)){
        seg_intersection(p1, p2, p3, p4, rst);
        return 1;
    }
    return 0;
}


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

result = 0LL;
*((_QWORD *)&arc_pool + 751) = 0LL;
*(&arc_pool + 1500) = 0;
memset((void *)(unsigned int)((_DWORD)&arc_pool + 5600), 0, 0x190uLL);
return result;
}

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

result = 0LL;
*((_QWORD *)&arc_pool + 751) = 0LL;
*(&arc_pool + 1500) = 0;
memset((void *)(unsigned int)((_DWORD)&arc_pool + 5600), 0, 0x190uLL);
return result;
}

original_code:
void init_arc_pool(){
    arc_pool.head = ((void *)0);
    arc_pool.count = 0;
    int i;
    for(i=0; i<100; i++){
        arc_pool.iarc[i] = 0;
    }
}


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

result = 99LL;
do
{
if ( !arc_pool[result + 1400] )
return (unsigned int)result;
--result;
}
while ( result );
return 0xFFFFFFFFLL;
}

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

result = 99LL;
do
{
if ( !arc_pool[result + 1400] )
return (unsigned int)result;
--result;
}
while ( result );
return 0xFFFFFFFFLL;
}

original_code:
int find_free_pos_arc(){
    int i=100 -1;
    while(i>0){
        if(arc_pool.iarc[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}

canonical_original_code:
int find_free_pos_arc(){
    int i = 100 -1;
    while(i>0){
        if(arc_pool.iarc[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}


decompiled_code:
int *dispatch_arc()
{
int v0; // eax
int v1; // ecx
int v2; // edx
int *result; // rax

if ( arc_pool[1500] > 99 )
{
v_error("arc pool is full!");
result = 0LL;
}
else
{
v0 = find_free_pos_arc();
v2 = v0;
if ( v0 == -1 )
{
v_error("no space for arc");
result = 0LL;
}
else
{
arc_pool[1500] = v1 + 1;
arc_pool[v0 + 1400] = 1;
result = &arc_pool[14 * v0];
result[12] = v2;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 4) = 0LL;
*((_QWORD *)result + 5) = 0LL;
*((_QWORD *)result + 2) = 0LL;
*((_QWORD *)result + 1) = 0LL;
}
}
return result;
}

canonical_decompiled_code:
int *func2()
{
int v0; // eax
int v1; // ecx
int v2; // edx
int *result; // rax

if ( arc_pool[1500] > 99 )
{
v_error("arc pool is full!");
result = 0LL;
}
else
{
v0 = func1();
v2 = v0;
if ( v0 == -1 )
{
v_error("no space for arc");
result = 0LL;
}
else
{
arc_pool[1500] = v1 + 1;
arc_pool[v0 + 1400] = 1;
result = &arc_pool[14 * v0];
result[12] = v2;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 4) = 0LL;
*((_QWORD *)result + 5) = 0LL;
*((_QWORD *)result + 2) = 0LL;
*((_QWORD *)result + 1) = 0LL;
}
}
return result;
}

original_code:
struct arc* dispatch_arc(){
    if(arc_pool.count >= 100){
        v_error("arc pool is full!");
        return ((void *)0);
    }
    int free_pos = find_free_pos_arc();
    if(free_pos == -1){
        v_error("no space for arc");
        return ((void *)0);
    }
    arc_pool.arcs[free_pos].pos = free_pos;
    arc_pool.arcs[free_pos].e = ((void *)0);
    arc_pool.arcs[free_pos].s0 = ((void *)0);
    arc_pool.arcs[free_pos].s1 = ((void *)0);
    arc_pool.arcs[free_pos].next = ((void *)0);
    arc_pool.arcs[free_pos].prev = ((void *)0);
    arc_pool.count++;
    arc_pool.iarc[free_pos] = 1;
    arc_t *free_arc = &arc_pool.arcs[free_pos];
    return free_arc;
}

canonical_original_code:
struct arc *dispatch_arc(){
    if(arc_pool.count >= 100){
        v_error("arc pool is full!");
        return ((void *)0);
    }
    int free_pos = find_free_pos_arc();
    if(free_pos == -1){
        v_error("no space for arc");
        return ((void *)0);
    }
    arc_pool.arcs[free_pos].pos = free_pos;
    arc_pool.arcs[free_pos].e = ((void *)0);
    arc_pool.arcs[free_pos].s0 = ((void *)0);
    arc_pool.arcs[free_pos].s1 = ((void *)0);
    arc_pool.arcs[free_pos].next = ((void *)0);
    arc_pool.arcs[free_pos].prev = ((void *)0);
    arc_pool.count++;
    arc_pool.iarc[free_pos] = 1;
    struct arc *free_arc = &arc_pool.arcs[free_pos];
    return free_arc;
}

variable_types: struct arc { struct point { int x; int y; } p; struct arc *prev; struct event { struct point p; double x; struct arc *a; int valid; } *e; struct seg { struct point start; int done; } *s0; struct seg *s1; int pos; } *

user_defined_types: struct arc { struct point p; struct arc *prev; struct event *e; struct seg *s0; struct seg *s1; int pos; }


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

result = *(int *)(a1 + 48);
--arc_pool[1500];
arc_pool[result + 1400] = 0;
return result;
}

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

result = *(int *)(a1 + 48);
--arc_pool[1500];
arc_pool[result + 1400] = 0;
return result;
}

original_code:
void destory_arc(arc_t *arc){
    int pos = arc->pos;
    arc_pool.iarc[pos] = 0;
    arc_pool.count--;
    return;
}

canonical_original_code:
void destory_arc(struct arc *arc){
    int pos = arc->pos;
    arc_pool.iarc[pos] = 0;
    arc_pool.count--;
    return;
}

variable_types: pos int

user_defined_types: struct event { struct point p; double x; struct arc *a; int valid; }

user_defined_types: struct seg { struct point start; int done; }


decompiled_code:
int init_event_list()
{
*((_QWORD *)&event_list + 451) = 0LL;
*(&event_list + 900) = 0;
memset((void *)(unsigned int)((_DWORD)&event_list + 3200), 0, 0x190uLL);
return puts("init event list successed");
}

canonical_decompiled_code:
int func4()
{
*((_QWORD *)&event_list + 451) = 0LL;
*(&event_list + 900) = 0;
memset((void *)(unsigned int)((_DWORD)&event_list + 3200), 0, 0x190uLL);
return puts("init event list successed");
}

original_code:
void init_event_list(){
    event_list.head = ((void *)0);
    event_list.count = 0;
    int i;
    for(i=0; i<100; i++){
        event_list.i[i] = 0;
    }
    printf("init event list successed\n");
}

variable_types: i int


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

result = 99LL;
do
{
if ( !event_list[result + 800] )
return (unsigned int)result;
--result;
}
while ( result );
return 0xFFFFFFFFLL;
}

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

result = 99LL;
do
{
if ( !event_list[result + 800] )
return (unsigned int)result;
--result;
}
while ( result );
return 0xFFFFFFFFLL;
}

original_code:
int find_free_pos_e_list(){
    int i=100 -1;
    while(i>0){
        if(event_list.i[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}

canonical_original_code:
int find_free_pos_e_list(){
    int i = 100 -1;
    while(i>0){
        if(event_list.i[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}


decompiled_code:
int *dispatch_event()
{
int v0; // eax
int v1; // ecx
int *result; // rax

if ( event_list[900] > 99 )
{
v_error("arc pool is full!");
result = 0LL;
}
else
{
v0 = find_free_pos_e_list();
if ( v0 == -1 )
{
v_error("no space for event");
result = 0LL;
}
else
{
event_list[900] = v1 + 1;
event_list[v0 + 800] = 1;
result = &event_list[8 * v0];
}
}
return result;
}

canonical_decompiled_code:
int *func6()
{
int v0; // eax
int v1; // ecx
int *result; // rax

if ( event_list[900] > 99 )
{
v_error("arc pool is full!");
result = 0LL;
}
else
{
v0 = func5();
if ( v0 == -1 )
{
v_error("no space for event");
result = 0LL;
}
else
{
event_list[900] = v1 + 1;
event_list[v0 + 800] = 1;
result = &event_list[8 * v0];
}
}
return result;
}

original_code:
struct event* dispatch_event(){
    if(event_list.count >= 100){
        v_error("arc pool is full!");
        return ((void *)0);
    }
    int free_pos = find_free_pos_e_list();
    if(free_pos == -1){
        v_error("no space for event");
        return ((void *)0);
    }
    event_list.i[free_pos] = 1;
    event_list.count++;
    event_t *free_event = &event_list.events[free_pos];
    return free_event;
}

canonical_original_code:
struct event *dispatch_event(){
    if(event_list.count >= 100){
        v_error("arc pool is full!");
        return ((void *)0);
    }
    int free_pos = find_free_pos_e_list();
    if(free_pos == -1){
        v_error("no space for event");
        return ((void *)0);
    }
    event_list.i[free_pos] = 1;
    event_list.count++;
    struct event *free_event = &event_list.events[free_pos];
    return free_event;
}

variable_types: struct event { struct point { int x; int y; } p; double x; struct arc { struct point p; struct arc *prev; struct event *e; struct seg { struct point start; int done; } *s0; struct seg *s1; int pos; } *a; int valid; } *


decompiled_code:
int init_seg_pool()
{
__int64 *v0; // rbx
int result; // eax

*((_DWORD *)&seg_pool + 600) = 1;
v0 = (__int64 *)(unsigned int)((_DWORD)&seg_pool + 2000);
do
{
*(_DWORD *)v0 = 0;
v0 = (__int64 *)((char *)v0 + 4);
result = puts("init seg_pool successed");
}
while ( v0 != &seg_pool + 300 );
return result;
}

canonical_decompiled_code:
int func0()
{
__int64 *v0; // rbx
int result; // eax

*((_DWORD *)&seg_pool + 600) = 1;
v0 = (__int64 *)(unsigned int)((_DWORD)&seg_pool + 2000);
do
{
*(_DWORD *)v0 = 0;
v0 = (__int64 *)((char *)v0 + 4);
result = puts("init seg_pool successed");
}
while ( v0 != &seg_pool + 300 );
return result;
}

original_code:
void init_seg_pool(){
    seg_pool.segs_count = 1;
    int i=0;
    for(;i<100;++i){
        seg_pool.i[i] = 0;
        printf("init seg_pool successed\n");
    }
}

canonical_original_code:
void init_seg_pool(){
    seg_pool.segs_count = 1;
    int i = 0;
    for(;i<100;++i){
        seg_pool.i[i] = 0;
        printf("init seg_pool successed\n");
    }
}


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

result = 99LL;
do
{
if ( !*((_DWORD *)&seg_pool + result + 500) )
return (unsigned int)result;
}
while ( result-- != 0 );
return 0xFFFFFFFFLL;
}

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

result = 99LL;
do
{
if ( !*((_DWORD *)&seg_pool + result + 500) )
return (unsigned int)result;
}
while ( result-- != 0 );
return 0xFFFFFFFFLL;
}

original_code:
int find_free_segs(){
    int i=100 -1;
    while(i>=0){
        if(seg_pool.i[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}

canonical_original_code:
int find_free_segs(){
    int i = 100 -1;
    while(i>=0){
        if(seg_pool.i[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}


decompiled_code:
char *dispatch_seg()
{
int v0; // eax
int v1; // ecx
__int64 v2; // rdx
char *v3; // r12

if ( *((int *)&seg_pool + 600) > 99 )
{
v3 = 0LL;
v_error("seg pool is full!");
}
else
{
v0 = find_free_segs();
if ( v0 == -1 )
{
v3 = 0LL;
v_error("no space for segs");
}
else
{
*((_DWORD *)&seg_pool + 600) = v1 + 1;
*((_DWORD *)&seg_pool + v0 + 500) = 1;
v2 = 20LL * v0;
*(__int64 *)((char *)&seg_pool + v2) = 0LL;
*(_DWORD *)((char *)&seg_pool + v2 + 16) = 0;
v3 = (char *)&seg_pool + v2;
*(__int64 *)((char *)&seg_pool + v2 + 8) = 0LL;
push_back_seg((char *)&seg_pool + v2);
}
}
return v3;
}

canonical_decompiled_code:
char *func2()
{
int v0; // eax
int v1; // ecx
__int64 v2; // rdx
char *v3; // r12

if ( *((int *)&seg_pool + 600) > 99 )
{
v3 = 0LL;
v_error("seg pool is full!");
}
else
{
v0 = func1();
if ( v0 == -1 )
{
v3 = 0LL;
v_error("no space for segs");
}
else
{
*((_DWORD *)&seg_pool + 600) = v1 + 1;
*((_DWORD *)&seg_pool + v0 + 500) = 1;
v2 = 20LL * v0;
*(__int64 *)((char *)&seg_pool + v2) = 0LL;
*(_DWORD *)((char *)&seg_pool + v2 + 16) = 0;
v3 = (char *)&seg_pool + v2;
*(__int64 *)((char *)&seg_pool + v2 + 8) = 0LL;
push_back_seg((char *)&seg_pool + v2);
}
}
return v3;
}

original_code:
seg_t * dispatch_seg(){
    if(seg_pool.segs_count >= 100){
        v_error("seg pool is full!");
        return ((void *)0);
    }
    int free_pos = find_free_segs();
    if(free_pos == -1){
        v_error("no space for segs");
        return ((void *)0);
    }
    seg_pool.i[free_pos] = 1;
    seg_pool.segs_count++;
    seg_pool.segs[free_pos].start.x = 0;
    seg_pool.segs[free_pos].start.y = 0;
    seg_pool.segs[free_pos].end.x = 0;
    seg_pool.segs[free_pos].end.y = 0;
    seg_pool.segs[free_pos].done = 0;
    seg_t *free_seg = &seg_pool.segs[free_pos];
    push_back_seg(free_seg);
    return free_seg;
}

canonical_original_code:
struct seg *dispatch_seg(){
    if(seg_pool.segs_count >= 100){
        v_error("seg pool is full!");
        return ((void *)0);
    }
    int free_pos = find_free_segs();
    if(free_pos == -1){
        v_error("no space for segs");
        return ((void *)0);
    }
    seg_pool.i[free_pos] = 1;
    seg_pool.segs_count++;
    seg_pool.segs[free_pos].start.x = 0;
    seg_pool.segs[free_pos].start.y = 0;
    seg_pool.segs[free_pos].end.x = 0;
    seg_pool.segs[free_pos].end.y = 0;
    seg_pool.segs[free_pos].done = 0;
    struct seg *free_seg = &seg_pool.segs[free_pos];
    push_back_seg(free_seg);
    return free_seg;
}

variable_types: struct seg { struct point { int x; int y; } start; int done; } *


decompiled_code:
_BOOL8 __fastcall is_point_within_area(_DWORD *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 <= 0x7D0u )
result = a1[1] <= 0x7D0u;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_DWORD *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 <= 0x7D0u )
result = a1[1] <= 0x7D0u;
return result;
}

original_code:
BOOL is_point_within_area(point_t *p){
    if(p->x >= 0 && p->x <= 2000 && p->y >= 0 && p->y <= 2000){
        return 1;
    }else{
        return 0;
    }
}

canonical_original_code:
int is_point_within_area(struct point *p){
    if(p->x >= 0 && p->x <= 2000 && p->y >= 0 && p->y <= 2000){
        return 1;
    }else{
        return 0;
    }
}

variable_types:
p
struct point { int x; int y; } *


decompiled_code:
_BOOL8 __fastcall is_points_equal(__int64 a1, __int64 a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = HIDWORD(a1) == HIDWORD(a2);
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, __int64 a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( (_DWORD)a1 == (_DWORD)a2 )
result = HIDWORD(a1) == HIDWORD(a2);
return result;
}

original_code:
BOOL is_points_equal(point_t a, point_t b){
    if(a.x == b.x && a.y == b.y){
        return 1;
    }else{
        return 0;
    }
}

canonical_original_code:
int is_points_equal(struct point a, struct point b){
    if(a.x == b.x && a.y == b.y){
        return 1;
    }else{
        return 0;
    }
}

variable_types:
a
struct point { int x; int y; }

variable_types:
b
struct point


decompiled_code:
void __fastcall add_intersections(__int64 *a1, int *a2, __int64 a3)
{
__int64 v3; // r9
__int64 *v4; // r11
int v8; // edx

v3 = *a2;
v4 = a1;
if ( (_DWORD)v3 )
{
v8 = 0;
while ( (int)v3 > v8 )
{
if ( is_points_equal(*a1, a3) )
return;
}
v4[v3] = a3;
++*a2;
}
else
{
*a1 = a3;
++*a2;
}
}

original_code:
void add_intersections(point_t * intersections, int *count, point_t p){
    if(*count == 0){
        intersections[*count] = p;
        (*count)++;
        return;
    }
    int i = 0;
    while(i < *count){
        if(is_points_equal( intersections[i++], p)) return;
    }
    intersections[*count] = p;
    (*count)++;
}

canonical_original_code:
void add_intersections(struct point *intersections, int *count, struct point p){
    if(*count == 0){
        intersections[*count] = p;
        (*count)++;
        return;
    }
    int i = 0;
    while(i < *count){
        if(is_points_equal( intersections[i++], p)) return;
    }
    intersections[*count] = p;
    (*count)++;
}

variable_types:
intersections
struct point { int x; int y; } *

variable_types:
count
int *

variable_types:
p
struct point


decompiled_code:
__int64 __fastcall rebuild_seg(_QWORD *a1)
{
__int64 result; // rax
__int64 v2; // rbp
__int64 v3; // r13
__int64 v4; // r14
__int64 v5; // rax
__int64 v6; // rcx
__int64 v7; // rdx
__int64 v8; // r12
unsigned int v9; // [rsp+4h] [rbp-54h] BYREF
__int64 v10; // [rsp+8h] [rbp-50h] BYREF
__int64 v11; // [rsp+10h] [rbp-48h] BYREF
__int64 v12; // [rsp+18h] [rbp-40h]

if ( !is_point_within_area(a1) || (result = is_point_within_area((_DWORD *)a1 + 2), (_DWORD)result != 1) )
{
v2 = build_point(0LL, 0LL);
v3 = build_point(2000LL, 0LL);
v4 = build_point(2000LL, 2000LL);
v5 = build_point(0LL, 2000LL);
v6 = a1[1];
v7 = *a1;
v9 = 0;
v8 = v5;
if ( (unsigned int)get_seg_intersection(v2, v3, v7, v6, &v10) == 1 )
add_intersections(&v11, (int *)&v9, v10);
if ( (unsigned int)get_seg_intersection(v8, v4, *a1, a1[1], &v10) == 1 )
add_intersections(&v11, (int *)&v9, v10);
if ( (unsigned int)get_seg_intersection(v2, v8, *a1, a1[1], &v10) == 1 )
add_intersections(&v11, (int *)&v9, v10);
if ( (unsigned int)get_seg_intersection(v3, v4, *a1, a1[1], &v10) == 1 )
add_intersections(&v11, (int *)&v9, v10);
result = v9;
if ( v9 )
{
if ( v9 == 2 )
{
*a1 = v11;
result = v12;
a1[1] = v12;
}
else if ( v9 == 1 )
{
if ( !is_point_within_area(a1) )
*a1 = v11;
result = is_point_within_area((_DWORD *)a1 + 2);
if ( !(_DWORD)result )
{
result = v11;
a1[1] = v11;
}
}
}
else
{
*a1 = 0LL;
a1[1] = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1)
{
__int64 result; // rax
__int64 v2; // rbp
__int64 v3; // r13
__int64 v4; // r14
__int64 v5; // rax
__int64 v6; // rcx
__int64 v7; // rdx
__int64 v8; // r12
unsigned int v9; // [rsp+4h] [rbp-54h] BYREF
__int64 v10; // [rsp+8h] [rbp-50h] BYREF
__int64 v11; // [rsp+10h] [rbp-48h] BYREF
__int64 v12; // [rsp+18h] [rbp-40h]

if ( !func0(a1) || (result = func0((_DWORD *)a1 + 2), (_DWORD)result != 1) )
{
v2 = build_point(0LL, 0LL);
v3 = build_point(2000LL, 0LL);
v4 = build_point(2000LL, 2000LL);
v5 = build_point(0LL, 2000LL);
v6 = a1[1];
v7 = *a1;
v9 = 0;
v8 = v5;
if ( (unsigned int)get_seg_intersection(v2, v3, v7, v6, &v10) == 1 )
func2(&v11, (int *)&v9, v10);
if ( (unsigned int)get_seg_intersection(v8, v4, *a1, a1[1], &v10) == 1 )
func2(&v11, (int *)&v9, v10);
if ( (unsigned int)get_seg_intersection(v2, v8, *a1, a1[1], &v10) == 1 )
func2(&v11, (int *)&v9, v10);
if ( (unsigned int)get_seg_intersection(v3, v4, *a1, a1[1], &v10) == 1 )
func2(&v11, (int *)&v9, v10);
result = v9;
if ( v9 )
{
if ( v9 == 2 )
{
*a1 = v11;
result = v12;
a1[1] = v12;
}
else if ( v9 == 1 )
{
if ( !func0(a1) )
*a1 = v11;
result = func0((_DWORD *)a1 + 2);
if ( !(_DWORD)result )
{
result = v11;
a1[1] = v11;
}
}
}
else
{
*a1 = 0LL;
a1[1] = 0LL;
}
}
return result;
}

original_code:
void rebuild_seg(seg_t *seg){
    if(is_point_within_area(&(seg->start)) == 1 && is_point_within_area(&(seg->end)) == 1){
        return;
    }else{
        point_t x0_y0 = build_point(0, 0);
        point_t x1_y0 = build_point(2000, 0);
        point_t x1_y1 = build_point(2000, 2000);
        point_t x0_y1 = build_point(0, 2000);
        point_t intersections[4];
        int count = 0;
        point_t its;
        BOOL is_its = get_seg_intersection(x0_y0, x1_y0, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        is_its = get_seg_intersection(x0_y1, x1_y1, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        is_its = get_seg_intersection(x0_y0, x0_y1, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        is_its = get_seg_intersection(x1_y0, x1_y1, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        if(count == 0){
            seg->start.x = 0;
            seg->start.y = 0;
            seg->end.x = 0;
            seg->end.y = 0;
            return;
        }
        if(count == 2){
            seg->start = intersections[0];
            seg->end = intersections[1];
        }
        if(count == 1){
            if(is_point_within_area(&(seg->start)) == 0){
                seg->start = intersections[0];
            }
            if(is_point_within_area(&(seg->end)) == 0){
                seg->end = intersections[0];
            }
        }
    }
}

canonical_original_code:
void rebuild_seg(struct seg *seg){
    if(is_point_within_area(&(seg->start)) == 1 && is_point_within_area(&(seg->end)) == 1){
        return;
    }else{
        struct point x0_y0 = build_point(0, 0);
        struct point x1_y0 = build_point(2000, 0);
        struct point x1_y1 = build_point(2000, 2000);
        struct point x0_y1 = build_point(0, 2000);
        struct point intersections[4];
        int count = 0;
        struct point its;
        int is_its = get_seg_intersection(x0_y0, x1_y0, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        is_its = get_seg_intersection(x0_y1, x1_y1, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        is_its = get_seg_intersection(x0_y0, x0_y1, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        is_its = get_seg_intersection(x1_y0, x1_y1, seg->start, seg->end, &its);
        if(is_its == 1){
            add_intersections(intersections, &count, its);
        }
        if(count == 0){
            seg->start.x = 0;
            seg->start.y = 0;
            seg->end.x = 0;
            seg->end.y = 0;
            return;
        }
        if(count == 2){
            seg->start = intersections[0];
            seg->end = intersections[1];
        }
        if(count == 1){
            if(is_point_within_area(&(seg->start)) == 0){
                seg->start = intersections[0];
            }
            if(is_point_within_area(&(seg->end)) == 0){
                seg->end = intersections[0];
            }
        }
    }
}

variable_types:
intersections
struct point { int x; int y; } [4]

variable_types: count int

variable_types:
its
struct point { int x; int y; }


decompiled_code:
__int64 __fastcall rebuild_seg_list(__int64 a1)
{
__int64 result; // rax
__int64 v3; // rbx
_QWORD *v4; // rdi

result = *(unsigned int *)(a1 + 800);
if ( (int)result > 0 )
{
v3 = 0LL;
do
{
v4 = *(_QWORD **)(a1 + 8 * v3++);
result = rebuild_seg(v4);
}
while ( *(_DWORD *)(a1 + 800) > (int)v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 result; // rax
__int64 v3; // rbx
_QWORD *v4; // rdi

result = *(unsigned int *)(a1 + 800);
if ( (int)result > 0 )
{
v3 = 0LL;
do
{
v4 = *(_QWORD **)(a1 + 8 * v3++);
result = func3(v4);
}
while ( *(_DWORD *)(a1 + 800) > (int)v3 );
}
return result;
}

original_code:
void rebuild_seg_list(seg_list_t * sl){
    int i = 0;
    for(;i<sl->segs_count; i++){
        rebuild_seg(sl->segs[i]);
    }
}

canonical_original_code:
void rebuild_seg_list(struct seg_list *sl){
    int i = 0;
    for(;i<sl->segs_count; i++){
        rebuild_seg(sl->segs[i]);
    }
}

variable_types: struct seg_list { struct seg { struct point { int x; int y; } start; int done; } *segs[100]; int segs_count; } *

user_defined_types: struct seg_list { struct seg *segs[100]; int segs_count; }


decompiled_code:
void init_seg_list()
{
*((_DWORD *)&seg_list + 200) = 0;
}

original_code:
void init_seg_list(void){
    seg_list.segs_count = 0;
}

canonical_original_code:
void init_seg_list(){
    seg_list.segs_count = 0;
}


decompiled_code:
_BOOL8 is_seg_list_full()
{
return *((_DWORD *)&seg_list + 200) > 99;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return *((_DWORD *)&seg_list + 200) > 99;
}

original_code:
int is_seg_list_full(){
    if(seg_list.segs_count >= 100){
        return 1;
    }else{
        return 0;
    }
}


decompiled_code:
__int64 __fastcall push_back_seg(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

if ( is_seg_list_full() )
return v_error("seg list is full");
result = LODWORD(seg_list[100]);
if ( (_DWORD)result )
{
v2 = (int)result;
result = (unsigned int)(result + 1);
seg_list[v2] = a1;
LODWORD(seg_list[100]) = result;
}
else
{
seg_list[0] = a1;
LODWORD(seg_list[100]) = 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

if ( func1() )
return v_error("seg list is full");
result = LODWORD(seg_list[100]);
if ( (_DWORD)result )
{
v2 = (int)result;
result = (unsigned int)(result + 1);
seg_list[v2] = a1;
LODWORD(seg_list[100]) = result;
}
else
{
seg_list[0] = a1;
LODWORD(seg_list[100]) = 1;
}
return result;
}

original_code:
void push_back_seg(seg_t* s){
    if(is_seg_list_full()){
        v_error("seg list is full");
        return;
    }
    if(seg_list.segs_count == 0){
        seg_list.segs[0] = s;
        seg_list.segs_count++;
    }else{
        seg_list.segs[seg_list.segs_count] = s;
        seg_list.segs_count++;
    }
}

canonical_original_code:
void push_back_seg(struct seg *s){
    if(is_seg_list_full()){
        v_error("seg list is full");
        return;
    }
    if(seg_list.segs_count == 0){
        seg_list.segs[0] = s;
        seg_list.segs_count++;
    }else{
        seg_list.segs[seg_list.segs_count] = s;
        seg_list.segs_count++;
    }
}


original_code:
void init_event_pool(){
    e_queue.events_count = 0;
    int i=0;
    for(; i<100; i++){
        e_queue.ievents[i] = 0;
    }
    e_queue.head = ((void *)0);
    e_queue.tail = ((void *)0);
}

canonical_original_code:
void init_event_pool(){
    e_queue.events_count = 0;
    int i = 0;
    for(; i<100; i++){
        e_queue.ievents[i] = 0;
    }
    e_queue.head = ((void *)0);
    e_queue.tail = ((void *)0);
}


decompiled_code:
_BOOL8 is_ep_empty()
{
return *((_DWORD *)&e_queue + 600) == 0;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return *((_DWORD *)&e_queue + 600) == 0;
}

original_code:
int is_ep_empty(){
    if(e_queue.events_count == 0){
        return 1;
    }else{
        return 0;
    }
}


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

result = 99LL;
do
{
if ( !*((_DWORD *)&e_queue + result + 601) )
return (unsigned int)result;
}
while ( result-- != 0 );
return 0xFFFFFFFFLL;
}

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

result = 99LL;
do
{
if ( !*((_DWORD *)&e_queue + result + 601) )
return (unsigned int)result;
}
while ( result-- != 0 );
return 0xFFFFFFFFLL;
}

original_code:
int find_free_pos_e(){
    int i = 100 -1;
    while(i>=0){
        if(e_queue.ievents[i] == 0){
            return i;
        }
        --i;
    }
    return -1;
}


decompiled_code:
__int64 top_event()
{
return *(_QWORD *)e_queue[351];
}

canonical_decompiled_code:
__int64 func3()
{
return *(_QWORD *)e_queue[351];
}

original_code:
struct event* top_event(){
    return e_queue.head->e;
}

canonical_original_code:
struct event *top_event(){
    return e_queue.head->e;
}

return_type: struct event { struct point { int x; int y; } p; double x; struct arc { struct point p; struct arc *prev; struct event *e; struct seg { struct point start; int done; } *s0; struct seg *s1; int pos; } *a; int valid; } *


decompiled_code:
void __fastcall e_quicksort(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rbx

for ( ; a1 != a2; a1 = (_QWORD *)v2[2] )
{
v2 = get_partion_e(a1, a2);
e_quicksort(a1, v2);
}
}

canonical_decompiled_code:
void __fastcall func4(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rbx

for ( ; a1 != a2; a1 = (_QWORD *)v2[2] )
{
v2 = get_partion_e(a1, a2);
func4(a1, v2);
}
}

original_code:
void e_quicksort(event_node_t * e_begin, event_node_t *e_end){
    if(e_begin != e_end){
        event_node_t *partion = get_partion_e(e_begin, e_end);
        e_quicksort(e_begin, partion);
        e_quicksort(partion->next, e_end);
    }
}

canonical_original_code:
void e_quicksort(struct event_node *e_begin, struct event_node *e_end){
    if(e_begin != e_end){
        struct event_node *partion = get_partion_e(e_begin, e_end);
        e_quicksort(e_begin, partion);
        e_quicksort(partion->next, e_end);
    }
}

variable_types: struct event_node { struct event { struct point { int x; int y; } p; double x; struct arc { struct point p; struct arc *prev; struct event *e; struct seg { struct point start; int done; } *s0; struct seg *s1; int pos; } *a; int valid; } *e; int pos; struct event_node *next; } *

user_defined_types: struct event_node { struct event *e; int pos; struct event_node *next; }


decompiled_code:
void events_quicksort()
{
e_quicksort((_QWORD *)e_queue[351], *(_QWORD **)(e_queue[352] + 16));
}

original_code:
void events_quicksort(){
    e_quicksort(e_queue.head, e_queue.tail->next);
}


decompiled_code:
void __fastcall push_event(__int64 a1)
{
int v1; // eax
__int64 v2; // rcx
int v3; // edx
int v4; // esi
__int64 v5; // rax
__int64 *v6; // rax
bool v7; // zf

v1 = find_free_pos_e();
if ( v1 == -1 )
{
v_error("can not find free node for the new event!");
}
else
{
v2 = v1;
v3 = v1;
v4 = e_queue[300];
v5 = 3LL * v1;
e_queue[v5] = a1;
v6 = &e_queue[v5];
v7 = e_queue[351] == 0;
*((_DWORD *)v6 + 2) = v3;
v6[2] = 0LL;
if ( v7 )
{
e_queue[351] = (__int64)v6;
}
else
{
*(_QWORD *)(e_queue[352] + 16) = v6;
v6[2] = 0LL;
}
e_queue[352] = (__int64)v6;
LODWORD(e_queue[300]) = v4 + 1;
*((_DWORD *)&e_queue[300] + v2 + 1) = 1;
events_quicksort();
}
}

original_code:
void push_event(struct event *e){
    int free_node = find_free_pos_e();
    if(free_node == -1){
        v_error("can not find free node for the new event!");
        return;
    }
    e_queue.events[free_node].e = e;
    e_queue.events[free_node].pos = free_node;
    e_queue.events[free_node].next = ((void *)0);
    if(e_queue.head == ((void *)0)){
        e_queue.head = &(e_queue.events[free_node]);
        e_queue.tail = &(e_queue.events[free_node]);
        e_queue.events_count++;
        e_queue.ievents[free_node] = 1;
    }else{
        e_queue.tail->next = &(e_queue.events[free_node]);
        e_queue.tail = &(e_queue.events[free_node]);
        e_queue.tail->next = ((void *)0);
        e_queue.events_count++;
        e_queue.ievents[free_node] = 1;
    }
    events_quicksort();
}


KeyExpansion

repo: haiphan/haitech

decompiled_code:
__int64 KeyExpansion()
{
__int64 result; // rax
int v1; // er8
__int64 v2; // rcx
int v3; // edx
__int64 v4; // r9
unsigned int v5; // edx
char v6; // si
char v7; // di
unsigned int v8; // er10
unsigned int v9; // er11
char v10; // si
char v11; // r10
char v12; // r11
__int64 v13; // rcx
unsigned int v14; // edx
char v15; // si
char v16; // di
unsigned int v17; // er10
unsigned int v18; // er11
char v19; // si
char v20; // r10
char v21; // r11

if ( Nkb > 0 )
memcpy(RoundKey, &Key, Nkb);
result = (unsigned int)Nr;
v1 = Nk;
v2 = 4LL;
v3 = 4 * Nr + 4;
v4 = v3;
if ( !(((Nr & 0x20000000) != 0) ^ __OFSUB__(v3, 4) | (v3 == 4)) )
{
do
{
v5 = RoundKey[v2 - 1];
v6 = BYTE1(v5);
v7 = v5;
v8 = HIWORD(v5);
v9 = HIBYTE(v5);
if ( (v2 & 3) == 0 )
{
v6 = sbox[BYTE2(v5)];
LOBYTE(v8) = sbox[HIBYTE(v5)];
LOBYTE(v9) = sbox[(unsigned __int8)v5];
v7 = rcon[(int)v2 / v1 - 1] ^ sbox[BYTE1(v5)];
}
v10 = BYTE1(RoundKey[v2 - 4]) ^ v6;
v11 = BYTE2(RoundKey[v2 - 4]) ^ v8;
v12 = HIBYTE(RoundKey[v2 - 4]) ^ v9;
LOBYTE(RoundKey[v2]) = LOBYTE(RoundKey[v2 - 4]) ^ v7;
BYTE1(RoundKey[v2]) = v10;
BYTE2(RoundKey[v2]) = v11;
HIBYTE(RoundKey[v2]) = v12;
v13 = v2 + 1;
v14 = RoundKey[v13 - 1];
result = (unsigned int)v13;
v15 = BYTE1(v14);
v16 = v14;
v17 = HIWORD(v14);
v18 = HIBYTE(v14);
if ( (v13 & 3) == 0 )
{
LOBYTE(v17) = sbox[HIBYTE(v14)];
LOBYTE(v18) = sbox[(unsigned __int8)v14];
v15 = sbox[BYTE2(v14)];
result = (int)v13 / v1 - 1;
v16 = rcon[result] ^ sbox[BYTE1(v14)];
}
v19 = BYTE1(RoundKey[v13 - 4]) ^ v15;
v20 = BYTE2(RoundKey[v13 - 4]) ^ v17;
v21 = HIBYTE(RoundKey[v13 - 4]) ^ v18;
LOBYTE(RoundKey[v13]) = LOBYTE(RoundKey[v13 - 4]) ^ v16;
BYTE1(RoundKey[v13]) = v19;
BYTE2(RoundKey[v13]) = v20;
HIBYTE(RoundKey[v13]) = v21;
v2 = v13 + 1;
}
while ( v4 != v2 );
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
int v1; // er8
__int64 v2; // rcx
int v3; // edx
__int64 v4; // r9
unsigned int v5; // edx
char v6; // si
char v7; // di
unsigned int v8; // er10
unsigned int v9; // er11
char v10; // si
char v11; // r10
char v12; // r11
__int64 v13; // rcx
unsigned int v14; // edx
char v15; // si
char v16; // di
unsigned int v17; // er10
unsigned int v18; // er11
char v19; // si
char v20; // r10
char v21; // r11

if ( Nkb > 0 )
memcpy(RoundKey, &Key, Nkb);
result = (unsigned int)Nr;
v1 = Nk;
v2 = 4LL;
v3 = 4 * Nr + 4;
v4 = v3;
if ( !(((Nr & 0x20000000) != 0) ^ __OFSUB__(v3, 4) | (v3 == 4)) )
{
do
{
v5 = RoundKey[v2 - 1];
v6 = BYTE1(v5);
v7 = v5;
v8 = HIWORD(v5);
v9 = HIBYTE(v5);
if ( (v2 & 3) == 0 )
{
v6 = sbox[BYTE2(v5)];
LOBYTE(v8) = sbox[HIBYTE(v5)];
LOBYTE(v9) = sbox[(unsigned __int8)v5];
v7 = rcon[(int)v2 / v1 - 1] ^ sbox[BYTE1(v5)];
}
v10 = BYTE1(RoundKey[v2 - 4]) ^ v6;
v11 = BYTE2(RoundKey[v2 - 4]) ^ v8;
v12 = HIBYTE(RoundKey[v2 - 4]) ^ v9;
LOBYTE(RoundKey[v2]) = LOBYTE(RoundKey[v2 - 4]) ^ v7;
BYTE1(RoundKey[v2]) = v10;
BYTE2(RoundKey[v2]) = v11;
HIBYTE(RoundKey[v2]) = v12;
v13 = v2 + 1;
v14 = RoundKey[v13 - 1];
result = (unsigned int)v13;
v15 = BYTE1(v14);
v16 = v14;
v17 = HIWORD(v14);
v18 = HIBYTE(v14);
if ( (v13 & 3) == 0 )
{
LOBYTE(v17) = sbox[HIBYTE(v14)];
LOBYTE(v18) = sbox[(unsigned __int8)v14];
v15 = sbox[BYTE2(v14)];
result = (int)v13 / v1 - 1;
v16 = rcon[result] ^ sbox[BYTE1(v14)];
}
v19 = BYTE1(RoundKey[v13 - 4]) ^ v15;
v20 = BYTE2(RoundKey[v13 - 4]) ^ v17;
v21 = HIBYTE(RoundKey[v13 - 4]) ^ v18;
LOBYTE(RoundKey[v13]) = LOBYTE(RoundKey[v13 - 4]) ^ v16;
BYTE1(RoundKey[v13]) = v19;
BYTE2(RoundKey[v13]) = v20;
HIBYTE(RoundKey[v13]) = v21;
v2 = v13 + 1;
}
while ( v4 != v2 );
}
return result;
}

original_code:
void KeyExpansion()
{
 int i,j;
 unsigned char temp[4],k;
 for(i=0;i<Nkb;i++)
 {
  RoundKey[i]=Key[i];
 }
 i=4;
 while (i < (4 * (Nr+1)))
 {
     for(j=0;j<4;j++)
     {
      temp[j]=RoundKey[(i-1) * 4 + j];
     }
     if (i % 4 == 0)
     {
      {
       k = temp[0];
       temp[0] = temp[1];
       temp[1] = temp[2];
       temp[2] = temp[3];
       temp[3] = k;
      }
      {
       temp[0]=sbox[temp[0]];
       temp[1]=sbox[temp[1]];;
       temp[2]=sbox[temp[2]];;
       temp[3]=sbox[temp[3]];;
      }
      temp[0] = temp[0] ^ rcon[i/Nk - 1];
     }
     RoundKey[i*4+0] = RoundKey[(i-4)*4+0] ^ temp[0];
     RoundKey[i*4+1] = RoundKey[(i-4)*4+1] ^ temp[1];
     RoundKey[i*4+2] = RoundKey[(i-4)*4+2] ^ temp[2];
     RoundKey[i*4+3] = RoundKey[(i-4)*4+3] ^ temp[3];
     i++;
 }
}

canonical_original_code:
void KeyExpansion()
{
 int i; int j;
 unsigned char temp[4]; unsigned char k;
 for(i=0;i<Nkb;i++)
 {
  RoundKey[i]=Key[i];
 }
 i=4;
 while (i < (4 * (Nr+1)))
 {
     for(j=0;j<4;j++)
     {
      temp[j]=RoundKey[(i-1) * 4 + j];
     }
     if (i % 4 == 0)
     {
      {
       k = temp[0];
       temp[0] = temp[1];
       temp[1] = temp[2];
       temp[2] = temp[3];
       temp[3] = k;
      }
      {
       temp[0]=sbox[temp[0]];
       temp[1]=sbox[temp[1]];;
       temp[2]=sbox[temp[2]];;
       temp[3]=sbox[temp[3]];;
      }
      temp[0] = temp[0] ^ rcon[i/Nk - 1];
     }
     RoundKey[i*4+0] = RoundKey[(i-4)*4+0] ^ temp[0];
     RoundKey[i*4+1] = RoundKey[(i-4)*4+1] ^ temp[1];
     RoundKey[i*4+2] = RoundKey[(i-4)*4+2] ^ temp[2];
     RoundKey[i*4+3] = RoundKey[(i-4)*4+3] ^ temp[3];
     i++;
 }
}

variable_types: j int

variable_types:
temp
unsigned char [4]

variable_types:
k
unsigned char


AddRoundKey

decompiled_code:
__int64 __fastcall AddRoundKey(int a1)
{
int v1; // edx
int v2; // edi
char v3; // r11
char v4; // r8
char v5; // r11
char v6; // r10
char v7; // r8
char v8; // cl
char v9; // cl
char v10; // r11
char v11; // r11
__int64 result; // rax

v1 = 4 * a1;
v2 = 16 * a1;
v3 = *(&state + 1);
state ^= *((_BYTE *)RoundKey + v2);
*(&state + 4) ^= *((_BYTE *)RoundKey + v2 + 1);
v4 = *((_BYTE *)RoundKey + v2 + 2);
v5 = LOBYTE(RoundKey[v1 + 1]) ^ v3;
*(&state + 12) ^= *((_BYTE *)RoundKey + v2 + 3);
*(&state + 13) ^= HIBYTE(RoundKey[v1 + 1]);
v6 = BYTE2(RoundKey[v1 + 1]);
*(&state + 8) ^= v4;
v7 = BYTE1(RoundKey[v1 + 1]);
*(&state + 1) = v5;
v8 = *(&state + 2);
*(&state + 5) ^= v7;
v9 = LOBYTE(RoundKey[v1 + 2]) ^ v8;
*(&state + 9) ^= v6;
LOBYTE(v2) = BYTE1(RoundKey[v1 + 2]);
v10 = BYTE2(RoundKey[v1 + 2]);
*(&state + 2) = v9;
*(&state + 6) ^= v2;
*(&state + 10) ^= v10;
*(&state + 14) ^= HIBYTE(RoundKey[v1 + 2]);
LOBYTE(v2) = BYTE1(RoundKey[v1 + 3]);
v11 = BYTE2(RoundKey[v1 + 3]);
result = HIBYTE(RoundKey[v1 + 3]);
*(&state + 3) ^= LOBYTE(RoundKey[v1 + 3]);
*(&state + 7) ^= v2;
*(&state + 11) ^= v11;
*(&state + 15) ^= result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // edx
int v2; // edi
char v3; // r11
char v4; // r8
char v5; // r11
char v6; // r10
char v7; // r8
char v8; // cl
char v9; // cl
char v10; // r11
char v11; // r11
__int64 result; // rax

v1 = 4 * a1;
v2 = 16 * a1;
v3 = *(&state + 1);
state ^= *((_BYTE *)RoundKey + v2);
*(&state + 4) ^= *((_BYTE *)RoundKey + v2 + 1);
v4 = *((_BYTE *)RoundKey + v2 + 2);
v5 = LOBYTE(RoundKey[v1 + 1]) ^ v3;
*(&state + 12) ^= *((_BYTE *)RoundKey + v2 + 3);
*(&state + 13) ^= HIBYTE(RoundKey[v1 + 1]);
v6 = BYTE2(RoundKey[v1 + 1]);
*(&state + 8) ^= v4;
v7 = BYTE1(RoundKey[v1 + 1]);
*(&state + 1) = v5;
v8 = *(&state + 2);
*(&state + 5) ^= v7;
v9 = LOBYTE(RoundKey[v1 + 2]) ^ v8;
*(&state + 9) ^= v6;
LOBYTE(v2) = BYTE1(RoundKey[v1 + 2]);
v10 = BYTE2(RoundKey[v1 + 2]);
*(&state + 2) = v9;
*(&state + 6) ^= v2;
*(&state + 10) ^= v10;
*(&state + 14) ^= HIBYTE(RoundKey[v1 + 2]);
LOBYTE(v2) = BYTE1(RoundKey[v1 + 3]);
v11 = BYTE2(RoundKey[v1 + 3]);
result = HIBYTE(RoundKey[v1 + 3]);
*(&state + 3) ^= LOBYTE(RoundKey[v1 + 3]);
*(&state + 7) ^= v2;
*(&state + 11) ^= v11;
*(&state + 15) ^= result;
return result;
}

original_code:
void AddRoundKey(int round)
{
 int i,j;
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   state[j][i] ^= RoundKey[round * 4 * 4 + i * 4 + j];
  }
 }
}

canonical_original_code:
void AddRoundKey(int round)
{
 int i; int j;
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   state[j][i] ^= RoundKey[round * 4 * 4 + i * 4 + j];
  }
 }
}

variable_types: round int

variable_types: i int


SubBytes

decompiled_code:
__int64 SubBytes()
{
__int64 v0; // r9
__int64 v1; // r11
char v2; // si
char v3; // r8
__int64 v4; // rdx
char v5; // r10
char v6; // al
char v7; // cl
__int64 v8; // rsi
__int64 v9; // r8
__int64 v10; // r10
char v11; // di
__int64 result; // rax
char v13; // cl
char v14; // di

v0 = (unsigned __int8)*(&state + 3);
v1 = (unsigned __int8)*(&state + 4);
v2 = sbox[(unsigned __int8)*(&state + 1)];
v3 = sbox[(unsigned __int8)*(&state + 2)];
state = sbox[(unsigned __int8)state];
v4 = (unsigned __int8)*(&state + 5);
v5 = sbox[v0];
v6 = sbox[v1];
*(&state + 1) = v2;
v7 = sbox[v4];
v8 = (unsigned __int8)*(&state + 6);
*(&state + 2) = v3;
*(&state + 3) = v5;
v9 = (unsigned __int8)*(&state + 7);
v10 = (unsigned __int8)*(&state + 8);
*(&state + 4) = v6;
*(&state + 5) = v7;
v11 = sbox[v8];
LOBYTE(v0) = sbox[v9];
LOBYTE(v1) = sbox[v10];
LOBYTE(v4) = sbox[(unsigned __int8)*(&state + 9)];
LOBYTE(v8) = sbox[(unsigned __int8)*(&state + 10)];
*(&state + 6) = v11;
*(&state + 7) = v0;
*(&state + 8) = v1;
*(&state + 9) = v4;
*(&state + 10) = v8;
LOBYTE(v10) = sbox[(unsigned __int8)*(&state + 12)];
result = (unsigned __int8)sbox[(unsigned __int8)*(&state + 13)];
v13 = sbox[(unsigned __int8)*(&state + 14)];
v14 = sbox[(unsigned __int8)*(&state + 15)];
*(&state + 11) = sbox[(unsigned __int8)*(&state + 11)];
*(&state + 12) = v10;
*(&state + 13) = result;
*(&state + 14) = v13;
*(&state + 15) = v14;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // r9
__int64 v1; // r11
char v2; // si
char v3; // r8
__int64 v4; // rdx
char v5; // r10
char v6; // al
char v7; // cl
__int64 v8; // rsi
__int64 v9; // r8
__int64 v10; // r10
char v11; // di
__int64 result; // rax
char v13; // cl
char v14; // di

v0 = (unsigned __int8)*(&state + 3);
v1 = (unsigned __int8)*(&state + 4);
v2 = sbox[(unsigned __int8)*(&state + 1)];
v3 = sbox[(unsigned __int8)*(&state + 2)];
state = sbox[(unsigned __int8)state];
v4 = (unsigned __int8)*(&state + 5);
v5 = sbox[v0];
v6 = sbox[v1];
*(&state + 1) = v2;
v7 = sbox[v4];
v8 = (unsigned __int8)*(&state + 6);
*(&state + 2) = v3;
*(&state + 3) = v5;
v9 = (unsigned __int8)*(&state + 7);
v10 = (unsigned __int8)*(&state + 8);
*(&state + 4) = v6;
*(&state + 5) = v7;
v11 = sbox[v8];
LOBYTE(v0) = sbox[v9];
LOBYTE(v1) = sbox[v10];
LOBYTE(v4) = sbox[(unsigned __int8)*(&state + 9)];
LOBYTE(v8) = sbox[(unsigned __int8)*(&state + 10)];
*(&state + 6) = v11;
*(&state + 7) = v0;
*(&state + 8) = v1;
*(&state + 9) = v4;
*(&state + 10) = v8;
LOBYTE(v10) = sbox[(unsigned __int8)*(&state + 12)];
result = (unsigned __int8)sbox[(unsigned __int8)*(&state + 13)];
v13 = sbox[(unsigned __int8)*(&state + 14)];
v14 = sbox[(unsigned __int8)*(&state + 15)];
*(&state + 11) = sbox[(unsigned __int8)*(&state + 11)];
*(&state + 12) = v10;
*(&state + 13) = result;
*(&state + 14) = v13;
*(&state + 15) = v14;
return result;
}

original_code:
void SubBytes()
{
 int i,j;
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   state[i][j] = sbox[state[i][j]];
  }
 }
}

canonical_original_code:
void SubBytes()
{
 int i; int j;
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   state[i][j] = sbox[state[i][j]];
  }
 }
}


ShiftRows

original_code:
void ShiftRows()
{
 unsigned char temp;
 temp=state[1][0];
 state[1][0]=state[1][1];
 state[1][1]=state[1][2];
 state[1][2]=state[1][3];
 state[1][3]=temp;
 temp=state[2][0];
 state[2][0]=state[2][2];
 state[2][2]=temp;
 temp=state[2][1];
 state[2][1]=state[2][3];
 state[2][3]=temp;
 temp=state[3][0];
 state[3][0]=state[3][3];
 state[3][3]=state[3][2];
 state[3][2]=state[3][1];
 state[3][1]=temp;
}

variable_types:
temp
unsigned char


original_code:
void aes128e(unsigned char *c, const unsigned char *p, const unsigned char *k) {
 int i,j,round=0;
 for(i=0;i<Nkb;i++)
 {
  Key[i]=k[i];
 }
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   state[j][i] = p[i*4 + j];
  }
 }
 KeyExpansion();
 AddRoundKey(0);
 for(round=1;round<Nr;round++)
 {
  SubBytes();
  ShiftRows();
  MixColumns();
  AddRoundKey(round);
 }
 SubBytes();
 ShiftRows();
 AddRoundKey(Nr);
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   c[i*4+j]=state[j][i];
  }
 }
}

canonical_original_code:
void aes128e(unsigned char *c, unsigned char *p, unsigned char *k) {
 int i; int j; int round = 0;
 for(i=0;i<Nkb;i++)
 {
  Key[i]=k[i];
 }
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   state[j][i] = p[i*4 + j];
  }
 }
 KeyExpansion();
 AddRoundKey(0);
 for(round=1;round<Nr;round++)
 {
  SubBytes();
  ShiftRows();
  MixColumns();
  AddRoundKey(round);
 }
 SubBytes();
 ShiftRows();
 AddRoundKey(Nr);
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   c[i*4+j]=state[j][i];
  }
 }
}

variable_types:
c
unsigned char *

variable_types:
p
unsigned char *

variable_types:
k
unsigned char *


repo: haipome/fls

decompiled_code:
int __fastcall get_head(char *s, const char *a2)
{
char *v2; // rax

v2 = getenv("HOME");
return sprintf(s, "%s%s", v2, a2);
}

canonical_decompiled_code:
int __fastcall func0(char *s, const char *a2)
{
char *v2; // rax

v2 = getenv("HOME");
return sprintf(s, "%s%s", v2, a2);
}

original_code:
static void get_head(char *head, char *name)
{
 sprintf(head, "%s%s", getenv("HOME"), name);
}

variable_types:
head
char *

variable_types:
name
char *


decompiled_code:
char *__fastcall add_id_tag(char *s)
{
sprintf(s, "%3u ", localaddr);
return &s[strlen(s)];
}

canonical_decompiled_code:
char *__fastcall func1(char *s)
{
sprintf(s, "%3u ", localaddr);
return &s[strlen(s)];
}

original_code:
static char * add_id_tag(char * buf)
{
 sprintf(buf, "%3u  ", localaddr.id);
 return (buf + strlen(buf));
}

canonical_original_code:
static char *add_id_tag(char *buf)
{
 sprintf(buf, "%3u  ", localaddr.id);
 return (buf + strlen(buf));
}

variable_types:
buf
char *

return_type: char *


decompiled_code:
__int64 __fastcall name_in_date(char *src, char *dest, char *a3)
{
struct tm *v4; // r14
size_t v5; // rax
size_t v6; // rax
time_t timer; // [rsp+8h] [rbp-30h] BYREF

time(&timer);
v4 = localtime(&timer);
strncpy(dest, src, 0x63uLL);
v5 = strlen(src);
strftime(&dest[v5], 0x14uLL, "%Y%m%d", v4);
v6 = strlen(dest);
strncat(dest, a3, 99 - v6);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *src, char *dest, char *a3)
{
struct tm *v4; // r14
size_t v5; // rax
size_t v6; // rax
time_t timer; // [rsp+8h] [rbp-30h] BYREF

time(&timer);
v4 = localtime(&timer);
strncpy(dest, src, 0x63uLL);
v5 = strlen(src);
strftime(&dest[v5], 0x14uLL, "%Y%m%d", v4);
v6 = strlen(dest);
strncat(dest, a3, 99 - v6);
return 1LL;
}

original_code:
static int name_in_date(const char * head, char * name, const char * tail)
{
 time_t now;
 struct tm * today;
 time(&now);
 today = localtime(&now);
 strncpy(name, head, 100 - 1);
 strftime((name + strlen(head)), 20, "%Y%m%d", today);
 strncat(name, tail, 100 - strlen(name) - 1);
 return 1;
}

canonical_original_code:
static int name_in_date(char *head, char *name, char *tail)
{
 long now;
 struct tm *today;
 time(&now);
 today = localtime(&now);
 strncpy(name, head, 100 - 1);
 strftime((name + strlen(head)), 20, "%Y%m%d", today);
 strncat(name, tail, 100 - strlen(name) - 1);
 return 1;
}

variable_types:
tail
char *

variable_types: now long

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


decompiled_code:
void log_init()
{
if ( logs_status != 1 )
{
get_head(src, "/wb/fls/log/gene/flsd");
name_in_date(src, dest, ".log");
logs = fopen(dest, "a+");
if ( !logs )
{
fwrite("In log_init: cannot open general log file\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
get_head(s, "/wb/fls/log/freq/freq");
name_in_date(s, byte_AB8, ".log");
stream = fopen(byte_AB8, "a+");
if ( !stream )
{
fwrite("In log_init: cannot open freqence log file\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
get_head(byte_BEC, "/wb/fls/log/over/over");
name_in_date(byte_BEC, byte_B88, ".log");
qword_B80 = fopen(byte_B88, "a+");
if ( !qword_B80 )
{
fwrite("In log_init: cannot open over limit log file\n", 1uLL, 0x2DuLL, stderr);
exit(1);
}
get_head(byte_CBC, "/wb/fls/log/warn/warn");
name_in_date(byte_CBC, byte_C58, ".log");
qword_C50 = fopen(byte_C58, "a+");
if ( !qword_C50 )
{
fwrite("In log_init: cannot open warning log file\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
logs_status = 1;
}
}

canonical_decompiled_code:
void func5()
{
if ( logs_status != 1 )
{
func0(src, "/wb/fls/log/gene/flsd");
func2(src, dest, ".log");
logs = fopen(dest, "a+");
if ( !logs )
{
fwrite("In log_init: cannot open general log file\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
func0(s, "/wb/fls/log/freq/freq");
func2(s, byte_AB8, ".log");
stream = fopen(byte_AB8, "a+");
if ( !stream )
{
fwrite("In log_init: cannot open freqence log file\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
func0(byte_BEC, "/wb/fls/log/over/over");
func2(byte_BEC, byte_B88, ".log");
qword_B80 = fopen(byte_B88, "a+");
if ( !qword_B80 )
{
fwrite("In log_init: cannot open over limit log file\n", 1uLL, 0x2DuLL, stderr);
exit(1);
}
func0(byte_CBC, "/wb/fls/log/warn/warn");
func2(byte_CBC, byte_C58, ".log");
qword_C50 = fopen(byte_C58, "a+");
if ( !qword_C50 )
{
fwrite("In log_init: cannot open warning log file\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
logs_status = 1;
}
}

original_code:
void log_init(void)
{
 if (logs_status == 1)
  return;
 get_head(logs.gene_log.head, genehead);
 name_in_date(logs.gene_log.head, logs.gene_log.name, commtail);
 if ((logs.gene_log.fp = fopen(logs.gene_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open general log file\n", stderr);
  exit(1);
 }
 get_head(logs.freq_log.head, freqhead);
 name_in_date(logs.freq_log.head, logs.freq_log.name, commtail);
 if ((logs.freq_log.fp = fopen(logs.freq_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open freqence log file\n", stderr);
  exit(1);
 }
 get_head(logs.over_log.head, overhead);
 name_in_date(logs.over_log.head, logs.over_log.name, commtail);
 if ((logs.over_log.fp = fopen(logs.over_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open over limit log file\n", stderr);
  exit(1);
 }
 get_head(logs.warn_log.head, warnhead);
 name_in_date(logs.warn_log.head, logs.warn_log.name, commtail);
 if ((logs.warn_log.fp = fopen(logs.warn_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open warning log file\n", stderr);
  exit(1);
 }
 logs_status = 1;
 return;
}

canonical_original_code:
void log_init()
{
 if (logs_status == 1)
  return;
 get_head(logs.gene_log.head, genehead);
 name_in_date(logs.gene_log.head, logs.gene_log.name, commtail);
 if ((logs.gene_log.fp = fopen(logs.gene_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open general log file\n", stderr);
  exit(1);
 }
 get_head(logs.freq_log.head, freqhead);
 name_in_date(logs.freq_log.head, logs.freq_log.name, commtail);
 if ((logs.freq_log.fp = fopen(logs.freq_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open freqence log file\n", stderr);
  exit(1);
 }
 get_head(logs.over_log.head, overhead);
 name_in_date(logs.over_log.head, logs.over_log.name, commtail);
 if ((logs.over_log.fp = fopen(logs.over_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open over limit log file\n", stderr);
  exit(1);
 }
 get_head(logs.warn_log.head, warnhead);
 name_in_date(logs.warn_log.head, logs.warn_log.name, commtail);
 if ((logs.warn_log.fp = fopen(logs.warn_log.name, "a+")) == ((void *)0))
 {
  fputs("In log_init: cannot open warning log file\n", stderr);
  exit(1);
 }
 logs_status = 1;
 return;
}


decompiled_code:
__int64 log_close()
{
_BOOL4 v1; // er12
int v2; // ebx

if ( !logs_status )
return 1LL;
v1 = 0;
v2 = fclose(logs);
if ( fclose(stream) != -1 )
v1 = v2 != -1;
if ( fclose(qword_B80) == -1 )
v1 = 0;
if ( fclose(qword_C50) == -1 )
v1 = 0;
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
_BOOL4 v1; // er12
int v2; // ebx

if ( !logs_status )
return 1LL;
v1 = 0;
v2 = fclose(logs);
if ( fclose(stream) != -1 )
v1 = v2 != -1;
if ( fclose(qword_B80) == -1 )
v1 = 0;
if ( fclose(qword_C50) == -1 )
v1 = 0;
return v1;
}

original_code:
int log_close(void)
{
 int stat = 1;
 if (logs_status != 0)
 {
  if (fclose(logs.gene_log.fp) == (-1))
   stat = 0;
  if (fclose(logs.freq_log.fp) == (-1))
   stat = 0;
  if (fclose(logs.over_log.fp) == (-1))
   stat = 0;
  if (fclose(logs.warn_log.fp) == (-1))
   stat = 0;
 }
 return stat;
}

canonical_original_code:
int log_close()
{
 int stat = 1;
 if (logs_status != 0)
 {
  if (fclose(logs.gene_log.fp) == (-1))
   stat = 0;
  if (fclose(logs.freq_log.fp) == (-1))
   stat = 0;
  if (fclose(logs.over_log.fp) == (-1))
   stat = 0;
  if (fclose(logs.warn_log.fp) == (-1))
   stat = 0;
 }
 return stat;
}

variable_types: stat int


decompiled_code:
__int64 fls_init()
{
signal_init();
log_init();
if ( !(unsigned int)readconf() )
err_exit("read config file error");
net_init();
shamem_init();
limit_init();
manage_fls_init();
if ( manage_switch )
read_rflsl();
if ( !(unsigned int)inform_start() )
err_exit("In fls_init: cannot connect to manage server");
pid_conf();
return run_msg("started.");
}

canonical_decompiled_code:
__int64 func0()
{
signal_init();
log_init();
if ( !(unsigned int)readconf() )
err_exit("read config file error");
net_init();
shamem_init();
limit_init();
manage_fls_init();
if ( manage_switch )
read_rflsl();
if ( !(unsigned int)inform_start() )
err_exit("In fls_init: cannot connect to manage server");
pid_conf();
return run_msg("started.");
}

original_code:
void fls_init(void)
{
 signal_init();
 log_init();
 if (!readconf())
  err_exit("read config file error");
 net_init();
 shamem_init();
 limit_init();
 manage_fls_init();
 if (manage_switch)
  read_rflsl();
 if (!inform_start())
  err_exit("In fls_init: cannot connect to manage server");
 pid_conf();
 run_msg("started.");
 return;
}

canonical_original_code:
void fls_init()
{
 signal_init();
 log_init();
 if (!readconf())
  err_exit("read config file error");
 net_init();
 shamem_init();
 limit_init();
 manage_fls_init();
 if (manage_switch)
  read_rflsl();
 if (!inform_start())
  err_exit("In fls_init: cannot connect to manage server");
 pid_conf();
 run_msg("started.");
 return;
}


decompiled_code:
void fls_close()
{
fls_switch = 0;
}

canonical_decompiled_code:
void func1()
{
fls_switch = 0;
}

original_code:
void fls_close(int sig)
{
 extern int fls_switch;
 fls_switch = 0;
}

variable_types: sig int


decompiled_code:
void __noreturn fls_exit()
{
run_msg("closed.");
if ( !(unsigned int)inform_close() )
run_msg("In fls_exit: cannot connect to manage fls");
log_close();
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
run_msg("closed.");
if ( !(unsigned int)inform_close() )
run_msg("In fls_exit: cannot connect to manage fls");
log_close();
exit(0);
}

original_code:
void fls_exit(void)
{
 run_msg("closed.");
 if (!inform_close())
  run_msg("In fls_exit: cannot connect to manage fls")
  ;
 log_close();
 exit(0);
}

canonical_original_code:
void fls_exit()
{
 run_msg("closed.");
 if (!inform_close())
  run_msg("In fls_exit: cannot connect to manage fls")
  ;
 log_close();
 exit(0);
}


decompiled_code:
void __noreturn send_exit_signal()
{
__pid_t v0; // edi

v0 = getconf2("processid", _id_offset);
if ( (unsigned int)(v0 + 1) > 1 )
{
kill(v0, 10);
exit(0);
}
fwrite("In send_exit_signal: get pid error\n", 1uLL, 0x23uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
__pid_t v0; // edi

v0 = getconf2("processid", _id_offset);
if ( (unsigned int)(v0 + 1) > 1 )
{
kill(v0, 10);
exit(0);
}
fwrite("In send_exit_signal: get pid error\n", 1uLL, 0x23uLL, stderr);
exit(1);
}

original_code:
void send_exit_signal()
{
 pid_t pid;
 pid = (pid_t)getconf2("processid", __id_offset);
 if (pid == 0 || pid == -1)
 {
  fputs("In send_exit_signal: get pid error\n", stderr);
  exit(1);
 }
 kill(pid, 10);
 exit(0);
}

canonical_original_code:
void send_exit_signal()
{
 int pid;
 pid = (int)getconf2("processid", __id_offset);
 if (pid == 0 || pid == -1)
 {
  fputs("In send_exit_signal: get pid error\n", stderr);
  exit(1);
 }
 kill(pid, 10);
 exit(0);
}

variable_types: pid int


decompiled_code:
__sighandler_t signal_init()
{
__sighandler_t result; // rax

if ( signal(10, (__sighandler_t)&fls_close) == (__sighandler_t)-1LL )
{
fwrite("In signal_init: cannot catch SIGUSR1\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
if ( signal(2, (__sighandler_t)&fls_close) == (__sighandler_t)-1LL
|| (result = signal(3, (__sighandler_t)&fls_close), result == (__sighandler_t)-1LL) )
{
fwrite("In signal_init: cannot catch SIGINT\n", 1uLL, 0x24uLL, stderr);
exit(1);
}
return result;
}

canonical_decompiled_code:
__sighandler_t func1()
{
__sighandler_t result; // rax

if ( signal(10, (__sighandler_t)&fls_close) == (__sighandler_t)-1LL )
{
fwrite("In signal_init: cannot catch SIGUSR1\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
if ( signal(2, (__sighandler_t)&fls_close) == (__sighandler_t)-1LL
|| (result = signal(3, (__sighandler_t)&fls_close), result == (__sighandler_t)-1LL) )
{
fwrite("In signal_init: cannot catch SIGINT\n", 1uLL, 0x24uLL, stderr);
exit(1);
}
return result;
}

original_code:
void signal_init(void)
{
 if (signal(10, fls_close) == ((__sighandler_t) -1))
 {
  fputs("In signal_init: cannot catch SIGUSR1\n", stderr);
  exit(1);
 }
 if (signal(2, fls_close) == ((__sighandler_t) -1))
 {
  fputs("In signal_init: cannot catch SIGINT\n", stderr);
  exit(1);
 }
 if (signal(3, fls_close) == ((__sighandler_t) -1))
 {
  fputs("In signal_init: cannot catch SIGINT\n", stderr);
  exit(1);
 }
}

canonical_original_code:
void signal_init()
{
 if (signal(10, fls_close) == ((__sighandler_t) -1))
 {
  fputs("In signal_init: cannot catch SIGUSR1\n", stderr);
  exit(1);
 }
 if (signal(2, fls_close) == ((__sighandler_t) -1))
 {
  fputs("In signal_init: cannot catch SIGINT\n", stderr);
  exit(1);
 }
 if (signal(3, fls_close) == ((__sighandler_t) -1))
 {
  fputs("In signal_init: cannot catch SIGINT\n", stderr);
  exit(1);
 }
}


decompiled_code:
__int64 pid_conf()
{
__pid_t v0; // eax
__int64 result; // rax

readconf();
v0 = getpid();
result = setconf2("processid", _id_offset, v0);
if ( !(_DWORD)result )
result = run_msg("In pid_conf: write pid error");
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__pid_t v0; // eax
__int64 result; // rax

readconf();
v0 = getpid();
result = setconf2("processid", _id_offset, v0);
if ( !(_DWORD)result )
result = run_msg("In pid_conf: write pid error");
return result;
}

original_code:
void pid_conf(void)
{
 readconf();
 pid_t pid = getpid();
 if (setconf2("processid", __id_offset, pid) == 0)
  run_msg("In pid_conf: write pid error");
}

canonical_original_code:
void pid_conf()
{
 readconf();
 int pid = getpid();
 if (setconf2("processid", __id_offset, pid) == 0)
  run_msg("In pid_conf: write pid error");
}


decompiled_code:
int pid_print()
{
__int64 v0; // rax

readconf();
v0 = getconf2("processid", _id_offset);
return printf("%d\n", v0);
}

canonical_decompiled_code:
int func3()
{
__int64 v0; // rax

readconf();
v0 = getconf2("processid", _id_offset);
return printf("%d\n", v0);
}

original_code:
void pid_print(void)
{
 readconf();
 printf("%d\n", (int)getconf2("processid", __id_offset));
}

canonical_original_code:
void pid_print()
{
 readconf();
 printf("%d\n", (int)getconf2("processid", __id_offset));
}