// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: set_pe_pos(ha_ug_index*, s_hit*, unsigned long, s_hit*, unsigned long, pe_hit*, unsigned long, trans_chain*)
inline void set_pe_pos(ha_ug_index* idx, s_hit *l1, uint64_t occ1, s_hit *l2, uint64_t occ2, 
pe_hit* x, uint64_t rid, trans_chain* t_ch)
{
    if(occ1 == 0 || occ2 == 0) return;
    uint64_t rev, uID, ref_p, self_p, eLen, tLen, i, is_unreliable = 0;
    s_hit *l1_5 = NULL, *l1_3 = NULL, *l2_5 = NULL, *l2_3 = NULL; 
    uint64_t l1_5_occ = 0, l1_3_occ = 0, l2_5_occ = 0, l2_3_occ = 0;

    /***************************for debug******************************/
    // fprintf(stderr, "\nrid: %lu, occ1: %lu, occ2: %lu\n", rid, occ1, occ2);
    // for (i = 0; i < occ1; i++)
    // {
    //     interpret_pos(idx, &l1[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     fprintf(stderr, "***-1-rev: %lu, uID: %lu, ref_p: %lu, self_p: %lu\n", 
    //     rev, uID, ref_p, self_p);
    // }

    // for (i = 0; i < occ2; i++)
    // {
    //     interpret_pos(idx, &l2[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     fprintf(stderr, "***-2-rev: %lu, uID: %lu, ref_p: %lu, self_p: %lu\n", 
    //     rev, uID, ref_p, self_p);
    // }
    /***************************for debug******************************/


    get_5_3_list(idx, l1, occ1, &l1_5, &l1_5_occ, &l1_3, &l1_3_occ);
    get_5_3_list(idx, l2, occ2, &l2_5, &l2_5_occ, &l2_3, &l2_3_occ);
    if(l1_5_occ == 0 || l2_5_occ == 0) return;
    x->id = rid; x->len = 0;

    ///if(l1_5_occ != 1 || l2_5_occ != 1) fprintf(stderr, "ERROR\n");

    for (i = 0; i < l1_5_occ; i++)
    {
        interpret_pos(idx, &l1_5[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
        if((ref_p + 1) < tLen) continue;
        ref_p = ref_p + 1 - tLen; 
        if(rev) ref_p = idx->ug->u.a[uID].len - 1 - ref_p;

        if(t_ch && (is_unreliable_hits(rev, ref_p, tLen, uID, t_ch)))
        {
            is_unreliable = 1;
            continue;
        }
        x->s = (rev<<63) | ((uID << (64-idx->uID_bits))>>1) | (ref_p & idx->pos_mode);
        x->len = tLen; x->len <<= 32;
    }
    
    for (i = 0; i < l2_5_occ; i++)
    {
        interpret_pos(idx, &l2_5[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
        if((ref_p + 1) < tLen) continue;
        ref_p = ref_p + 1 - tLen;  
        if(rev) ref_p = idx->ug->u.a[uID].len - 1 - ref_p;

        if(t_ch && (is_unreliable_hits(rev, ref_p, tLen, uID, t_ch)))
        {
            is_unreliable = 1;
            continue;
        }
        x->e = (rev<<63) | ((uID << (64-idx->uID_bits))>>1) | (ref_p & idx->pos_mode);
        x->len |= tLen;
    }

    if(is_unreliable || x->s == (uint64_t)-1 || x->e == (uint64_t)-1)
    {
        x->id = x->s = x->e = x->len = (uint64_t)-1;
        return;
    }


    /****************************may have bugs********************************/
    // for (i = 0; i < l1_3_occ; i++)
    // {
    //     interpret_pos(idx, &l1_3[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     if(uID != ((x->s << 1) >> (64 - idx->uID_bits)) && 
    //                         uID != ((x->e << 1) >> (64 - idx->uID_bits)))
    //     {
    //         x->id = x->s = x->e = x->len = (uint64_t)-1;
    //         return;
    //     }
    // }

    // for (i = 0; i < l2_3_occ; i++)
    // {
    //     interpret_pos(idx, &l2_3[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     if(uID != ((x->s << 1) >> (64 - idx->uID_bits)) && 
    //                         uID != ((x->e << 1) >> (64 - idx->uID_bits)))
    //     {
    //         x->id = x->s = x->e = x->len = (uint64_t)-1;
    //         return;
    //     }
    // }
    /****************************may have bugs********************************/



     
    /***************************for debug******************************/
    // fprintf(stderr, "-------------saved: x->occ1: %u, x->occ2: %u-------------\n", x->occ1, x->occ2);
    // for (i = 0; i < x->occ1; i++)
    // {
    //     fprintf(stderr, "###-1-rev: %lu, uID: %lu, ref_p: %lu\n", 
    //     x->a[i]>>63, (x->a[i]<<1)>>(64-idx->uID_bits), x->a[i] & idx->pos_mode);
    // }

    // for (i = 0; i < x->occ2; i++)
    // {
    //     fprintf(stderr, "###-2-rev: %lu, uID: %lu, ref_p: %lu\n", 
    //     x->a[i+x->occ1]>>63, (x->a[i+x->occ1]<<1)>>(64-idx->uID_bits), x->a[i+x->occ1] & idx->pos_mode);
    // }
    // fprintf(stderr, "-------------get_pe_s-rev: %lu, uID: %lu, ref_p: %lu-------------\n", 
    //     get_pe_s(*x)>>63, (get_pe_s(*x)<<1)>>(64-idx->uID_bits), get_pe_s(*x) & idx->pos_mode);
    // fprintf(stderr, "-------------get_pe_e-rev: %lu, uID: %lu, ref_p: %lu-------------\n", 
    //     get_pe_e(*x)>>63, (get_pe_e(*x)<<1)>>(64-idx->uID_bits), get_pe_e(*x) & idx->pos_mode);
    /***************************for debug******************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, %r12
movq %rdi, 0x28(%rsp)
testq %rdx, %rdx
sete %al
testq %r8, %r8
sete %cl
orb %al, %cl
jne 0x166743
movq %r8, %r15
xorl %eax, %eax
leaq 0x50(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x48(%rsp), %r13
movq %rax, (%r13)
leaq 0x70(%rsp), %rbp
movq %rax, (%rbp)
leaq 0x60(%rsp), %r14
movq %rax, (%r14)
leaq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq %r9, 0x20(%rsp)
leaq 0x80(%rsp), %r9
movq %rbp, %r8
callq 0x13d49f
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
leaq 0x78(%rsp), %r9
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x13d49f
movq 0x20(%rsp), %r9
movq (%rbp), %rax
movq %rax, 0x40(%rsp)
testq %rax, %rax
sete %al
movq (%r14), %rdx
testq %rdx, %rdx
sete %cl
orb %al, %cl
jne 0x166743
movq %rdx, 0x18(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%r9)
movq $0x0, 0x18(%r9)
movq 0x50(%rsp), %r14
movl $0x40, %eax
movq 0x28(%rsp), %rcx
subq 0x18(%rcx), %rax
movq 0x28(%rcx), %r15
movq 0x30(%rcx), %rcx
decq %r15
movq %rcx, 0x10(%rsp)
shrq %rcx
movq %rcx, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r11d, %r11d
xorl %r13d, %r13d
movq %r15, 0x8(%rsp)
movq %r11, %rbp
movq %r13, %rcx
shlq $0x4, %rcx
movq (%r14,%rcx), %rsi
movq 0x8(%r14,%rcx), %r10
leaq (%rsi,%rsi), %r12
movl %eax, %ecx
shrq %cl, %r12
movl %r10d, %edx
movq %r10, %rcx
shrq $0x20, %rcx
movzwl %cx, %ecx
shrq $0x30, %r10
addq %rcx, %r10
movq 0x10(%rsp), %rdi
andq %rsi, %rdi
movq %rdi, %r8
movl %r15d, %ecx
shrq %cl, %r8
movq 0x38(%rsp), %rcx
andq %rsi, %rcx
negq %rcx
testq %r8, %r8
cmoveq %rdi, %rcx
leaq (%rdx,%rcx), %rbx
incq %rbx
subq %r10, %rbx
jb 0x166595
testq %rsi, %rsi
jns 0x1664b8
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%r12,%r12,4), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
notq %rbx
addq %rcx, %rbx
cmpq $0x0, 0xc8(%rsp)
je 0x16656f
movq 0xc8(%rsp), %rcx
movq 0x30(%rcx), %rdx
leaq (%r12,%r12,2), %r11
movq (%rdx,%r11,8), %rcx
testq %rcx, %rcx
je 0x16656f
leaq -0x1(%r10), %rdi
movq %rbx, %r15
subq %r10, %r15
incq %r15
testq %rsi, %rsi
movl $0x0, %r8d
cmovsq %r8, %rdi
cmovnsq %rbx, %r15
addq %rbx, %rdi
testq %rdi, %rdi
cmovleq %r8, %rdi
testq %r15, %r15
cmovleq %r8, %r15
leaq (%rdx,%r11,8), %rdx
movq 0x10(%rdx), %r8
cmpl %edi, (%r8)
jg 0x166533
movl $0x1, %r11d
cmpl 0x8(%r8), %r15d
jg 0x166533
movq 0x8(%rsp), %r15
jmp 0x166598
addq $0x18, %r8
movl $0x1, %r11d
movq %r11, %r9
cmpq %r11, %rcx
je 0x16655a
movq %r8, %rdx
leaq 0x1(%r9), %r11
addq $0x10, %r8
cmpl %edi, -0x8(%rdx)
jg 0x16653d
cmpl (%rdx), %r15d
jg 0x16653d
movl $0x1, %r11d
cmpq %rcx, %r9
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r15
jb 0x166598
movl %eax, %ecx
shlq %cl, %r12
shrq $0x3f, %rsi
shrdq $0x1, %rsi, %r12
andq 0x10(%rsp), %rbx
orq %r12, %rbx
movq %rbx, (%r9)
shlq $0x20, %r10
movq %r10, 0x18(%r9)
movq %r10, 0x30(%rsp)
movq %rbp, %r11
incq %r13
cmpq 0x40(%rsp), %r13
jne 0x166431
movq 0x48(%rsp), %r14
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
movq %r12, %rcx
shlq $0x4, %rcx
movq (%r14,%rcx), %r13
movq 0x8(%r14,%rcx), %r10
leaq (,%r13,2), %rsi
movl %eax, %ecx
shrq %cl, %rsi
movl %r10d, %edx
movq %r10, %rcx
shrq $0x20, %rcx
movzwl %cx, %ecx
shrq $0x30, %r10
addq %rcx, %r10
movq 0x10(%rsp), %rdi
andq %r13, %rdi
movq %rdi, %r8
movl %r15d, %ecx
shrq %cl, %r8
movq 0x38(%rsp), %rcx
andq %r13, %rcx
negq %rcx
testq %r8, %r8
cmoveq %rdi, %rcx
leaq (%rdx,%rcx), %rbx
incq %rbx
subq %r10, %rbx
jb 0x166723
testq %r13, %r13
jns 0x16663b
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rsi,%rsi,4), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
notq %rbx
addq %rcx, %rbx
cmpq $0x0, 0xc8(%rsp)
je 0x1666f3
movq 0xc8(%rsp), %rcx
movq 0x30(%rcx), %rdx
leaq (%rsi,%rsi,2), %r15
movq (%rdx,%r15,8), %rcx
testq %rcx, %rcx
je 0x1666f3
leaq -0x1(%r10), %rdi
movq %rbx, %rbp
subq %r10, %rbp
incq %rbp
testq %r13, %r13
movl $0x0, %r8d
cmovsq %r8, %rdi
cmovnsq %rbx, %rbp
addq %rbx, %rdi
testq %rdi, %rdi
cmovleq %r8, %rdi
testq %rbp, %rbp
cmovleq %r8, %rbp
leaq (%rdx,%r15,8), %rdx
movq 0x10(%rdx), %rdx
cmpl %edi, (%rdx)
jg 0x1666b4
cmpl 0x8(%rdx), %ebp
jg 0x1666b4
movl $0x1, %r11d
movq 0x18(%rsp), %rbp
jmp 0x16671e
addq $0x18, %rdx
movl $0x1, %r15d
movq %r15, %r9
cmpq %r15, %rcx
je 0x1666dc
movq %rdx, %r8
leaq 0x1(%r9), %r15
addq $0x10, %rdx
cmpl %edi, -0x8(%r8)
jg 0x1666be
cmpl (%r8), %ebp
jg 0x1666be
cmpq %rcx, %r9
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %rbp
jae 0x1666f3
movl $0x1, %r11d
jmp 0x16671e
movl %eax, %ecx
shlq %cl, %rsi
shrq $0x3f, %r13
shrdq $0x1, %r13, %rsi
andq 0x10(%rsp), %rbx
orq %rsi, %rbx
movq %rbx, 0x8(%r9)
movq 0x30(%rsp), %rdx
orq %r10, %rdx
movq %rdx, 0x30(%rsp)
movq %rdx, 0x18(%r9)
movq 0x8(%rsp), %r15
incq %r12
cmpq %rbp, %r12
jne 0x1665b3
testq %r11, %r11
je 0x166755
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%r9)
movdqu %xmm0, (%r9)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $-0x1, (%r9)
je 0x166734
cmpq $-0x1, 0x8(%r9)
jne 0x166743
jmp 0x166734