// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rcx, %r12
testq %rdx, %rdx
sete %al
testq %r8, %r8
sete %cl
orb %al, %cl
jne 0x121dae
movq %r9, %rbx
movq %r8, %r15
movq %rdi, %r14
leaq 0x30(%rsp), %rcx
andq $0x0, (%rcx)
leaq 0x28(%rsp), %rax
andq $0x0, (%rax)
leaq 0x60(%rsp), %rbp
andq $0x0, (%rbp)
leaq 0x50(%rsp), %rax
andq $0x0, (%rax)
movq %rax, %r13
subq $0x8, %rsp
leaq 0x60(%rsp), %rax
leaq 0x78(%rsp), %r9
movq %rbp, %r8
pushq %rax
callq 0xf5f03
popq %rax
popq %rcx
subq $0x8, %rsp
leaq 0x50(%rsp), %rax
leaq 0x70(%rsp), %r9
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x30(%rsp), %rcx
movq %r13, %r15
movq %r13, %r8
pushq %rax
callq 0xf5f03
popq %rax
popq %rcx
movq (%rbp), %r13
testq %r13, %r13
sete %al
movq (%r15), %rbp
testq %rbp, %rbp
sete %cl
orb %al, %cl
jne 0x121dae
movq 0xb0(%rsp), %rax
movq %rax, 0x10(%rbx)
andq $0x0, 0x18(%rbx)
xorl %r15d, %r15d
movq 0x30(%rsp), %r12
subq $0x1, %r13
jb 0x121cb3
movq %r14, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x10(%rsp), %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x120d2b
popq %rax
popq %rcx
movq 0x8(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rdx
subq %rdx, %rsi
jb 0x121ca4
movq %rsi, 0x8(%rsp)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rax
testq %rdi, %rdi
movq 0xb8(%rsp), %r8
je 0x121c59
movq (%r14), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %r9
movl (%rcx,%r9), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
notq %rsi
addq %rcx, %rsi
movq %rsi, 0x8(%rsp)
testq %r8, %r8
je 0x121c81
movq %rax, %rcx
callq 0x121dcc
pushq $0x1
popq %rcx
testl %eax, %eax
jne 0x121ca7
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movb 0x18(%r14), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shlq %cl, %rax
shrdq $0x1, %rdi, %rax
andq 0x30(%r14), %rsi
orq %rax, %rsi
movq %rsi, (%rbx)
shlq $0x20, %rdx
movq %rdx, 0x18(%rbx)
movq %r15, %rcx
addq $0x10, %r12
movq %rcx, %r15
jmp 0x121bd0
movq 0x28(%rsp), %r12
pushq $0x1
popq %r13
subq $0x1, %rbp
jb 0x121d9c
movq %r14, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x10(%rsp), %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x120d2b
popq %rax
popq %rcx
movq 0x8(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rdx
subq %rdx, %rsi
jb 0x121d8d
movq %rsi, 0x8(%rsp)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rax
testq %rdi, %rdi
movq 0xb8(%rsp), %r8
je 0x121d45
movq (%r14), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %r9
movl (%rcx,%r9), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
notq %rsi
addq %rcx, %rsi
movq %rsi, 0x8(%rsp)
testq %r8, %r8
je 0x121d6d
movq %rax, %rcx
callq 0x121dcc
movq %r13, %rcx
testl %eax, %eax
jne 0x121d90
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movb 0x18(%r14), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shlq %cl, %rax
shrdq $0x1, %rdi, %rax
andq 0x30(%r14), %rsi
orq %rax, %rsi
movq %rsi, 0x8(%rbx)
orq %rdx, 0x18(%rbx)
movq %r15, %rcx
addq $0x10, %r12
movq %rcx, %r15
jmp 0x121cbc
testq %r15, %r15
je 0x121dbd
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%rbx)
movdqu %xmm0, (%rbx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $-0x1, (%rbx)
je 0x121da1
cmpq $-0x1, 0x8(%rbx)
jne 0x121dae
jmp 0x121da1