// File: /workspace/github_cpp_makefile_star2_O1/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 %r9, 0x10(%rsp)
movq %rcx, %r12
movq %rdi, 0x18(%rsp)
testq %rdx, %rdx
sete %al
testq %r8, %r8
sete %cl
orb %al, %cl
jne 0x15fe0d
movq %r8, %r15
xorl %eax, %eax
leaq 0x50(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x80(%rsp), %r9
movq %rax, (%r9)
leaq 0x40(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x78(%rsp), %rbp
movq %rax, (%rbp)
leaq 0x70(%rsp), %r14
movq %rax, (%r14)
leaq 0x68(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x60(%rsp), %r13
movq %rax, (%r13)
leaq 0x58(%rsp), %rbx
movq %rax, (%rbx)
movq %rdi, (%rsp)
movq %r14, %r8
callq 0x1376a7
movq %rbx, (%rsp)
movq %r12, %rsi
movq %r15, %rdx
leaq 0x40(%rsp), %rcx
movq %r13, %r8
movq %rbp, %r9
callq 0x1376a7
movq (%r14), %rax
movq %rax, 0x38(%rsp)
testq %rax, %rax
sete %al
movq (%r13), %r14
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x15fe0d
movq 0xc0(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq $0x0, 0x18(%rcx)
movq 0x50(%rsp), %r10
movl $0x40, %eax
movq 0x18(%rsp), %rcx
subq 0x18(%rcx), %rax
movq 0x28(%rcx), %rdx
movq 0x30(%rcx), %rcx
decq %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, 0x20(%rsp)
shrq %rcx
movq %rcx, 0x48(%rsp)
xorl %r8d, %r8d
xorl %r12d, %r12d
movq %r14, 0x28(%rsp)
movq %r8, %r13
movq %r12, %rcx
shlq $0x4, %rcx
movq (%r10,%rcx), %rdi
movq 0x8(%r10,%rcx), %rbp
leaq (%rdi,%rdi), %r15
movl %eax, %ecx
shrq %cl, %r15
movl %ebp, %edx
movq %rbp, %rcx
shrq $0x20, %rcx
movzwl %cx, %ecx
shrq $0x30, %rbp
addq %rcx, %rbp
movq 0x20(%rsp), %rsi
andq %rdi, %rsi
movq %rsi, %r8
movq 0x30(%rsp), %rcx
shrq %cl, %r8
movq 0x48(%rsp), %rcx
andq %rdi, %rcx
negq %rcx
testq %r8, %r8
cmoveq %rsi, %rcx
leaq (%rdx,%rcx), %rsi
incq %rsi
subq %rbp, %rsi
jb 0x15fc20
testq %rdi, %rdi
jns 0x15fb48
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%r15,%r15,4), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
notq %rsi
addq %rcx, %rsi
cmpq $0x0, 0xc8(%rsp)
je 0x15fbfa
movq 0xc8(%rsp), %rcx
movq 0x30(%rcx), %rdx
leaq (%r15,%r15,2), %r8
movq (%rdx,%r8,8), %rcx
testq %rcx, %rcx
setne %r9b
je 0x15fbef
leaq -0x1(%rbp), %rbx
movq %rsi, %r9
subq %rbp, %r9
incq %r9
testq %rdi, %rdi
movl $0x0, %r11d
cmovsq %r11, %rbx
cmovnsq %rsi, %r9
addq %rsi, %rbx
testq %rbx, %rbx
cmovleq %r11, %rbx
testq %r9, %r9
cmovleq %r11, %r9
leaq (%rdx,%r8,8), %rdx
movq 0x10(%rdx), %r11
cmpl %ebx, (%r11)
jg 0x15fbbc
movl $0x1, %r8d
cmpl 0x8(%r11), %r9d
jle 0x15fc23
addq $0x18, %r11
movl $0x1, %r14d
movq %r14, %r8
cmpq %r14, %rcx
je 0x15fbe3
movq %r11, %rdx
leaq 0x1(%r8), %r14
addq $0x10, %r11
cmpl %ebx, -0x8(%rdx)
jg 0x15fbc6
cmpl (%rdx), %r9d
jg 0x15fbc6
cmpq %rcx, %r8
setb %r9b
movq 0x28(%rsp), %r14
movl $0x1, %r8d
testb %r9b, %r9b
jne 0x15fc23
shrq $0x3f, %rdi
movl %eax, %ecx
shlq %cl, %r15
shrdq $0x1, %rdi, %r15
andq 0x20(%rsp), %rsi
orq %r15, %rsi
movq 0x10(%rsp), %rcx
movq %rsi, (%rcx)
shlq $0x20, %rbp
movq %rbp, 0x18(%rcx)
movq %r13, %r8
incq %r12
cmpq 0x38(%rsp), %r12
jne 0x15fac0
testq %r14, %r14
je 0x15fdf3
movl $0x40, %r11d
movq 0x18(%rsp), %rax
subq 0x18(%rax), %r11
movq 0x40(%rsp), %r9
movq 0x28(%rax), %rcx
movq 0x30(%rax), %r10
decq %rcx
movq %rcx, 0x20(%rsp)
movq %r10, %rax
shrq %rax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
movq %r11, 0x30(%rsp)
movq %r15, %rax
shlq $0x4, %rax
movq (%r9,%rax), %rbp
movq 0x8(%r9,%rax), %r12
leaq (,%rbp,2), %r13
movl %r11d, %ecx
shrq %cl, %r13
movl %r12d, %eax
movq %r12, %rcx
shrq $0x20, %rcx
movzwl %cx, %ecx
shrq $0x30, %r12
addq %rcx, %r12
movq %r10, %rdx
andq %rbp, %rdx
movq %rdx, %rsi
movq 0x20(%rsp), %rcx
shrq %cl, %rsi
movq 0x38(%rsp), %rcx
andq %rbp, %rcx
negq %rcx
testq %rsi, %rsi
cmoveq %rdx, %rcx
leaq (%rax,%rcx), %rdi
incq %rdi
subq %r12, %rdi
jb 0x15fddf
testq %rbp, %rbp
jns 0x15fd00
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
notq %rdi
addq %rax, %rdi
cmpq $0x0, 0xc8(%rsp)
je 0x15fdbd
movq 0xc8(%rsp), %rax
movq 0x30(%rax), %r14
leaq (,%r13,2), %rdx
addq %r13, %rdx
movq (%r14,%rdx,8), %rcx
testq %rcx, %rcx
setne %al
je 0x15fdaf
leaq -0x1(%r12), %rsi
movq %rdi, %rbx
subq %r12, %rbx
incq %rbx
testq %rbp, %rbp
movl $0x0, %eax
cmovsq %rax, %rsi
cmovnsq %rdi, %rbx
addq %rdi, %rsi
testq %rsi, %rsi
cmovleq %rax, %rsi
testq %rbx, %rbx
cmovleq %rax, %rbx
leaq (%r14,%rdx,8), %rax
movq 0x10(%rax), %rax
cmpl %esi, (%rax)
movq 0x28(%rsp), %r14
jg 0x15fd7c
movl $0x1, %edx
cmpl 0x8(%rax), %ebx
jle 0x15fde2
addq $0x18, %rax
movl $0x1, %r11d
movq %r11, %rdx
cmpq %r11, %rcx
je 0x15fda4
movq %rax, %r14
leaq 0x1(%rdx), %r11
addq $0x10, %rax
cmpl %esi, -0x8(%r14)
jg 0x15fd86
cmpl (%r14), %ebx
jg 0x15fd86
cmpq %rcx, %rdx
setb %al
movq 0x30(%rsp), %r11
movl $0x1, %edx
testb %al, %al
movq 0x28(%rsp), %r14
jne 0x15fde2
movl %r11d, %ecx
shlq %cl, %r13
shrq $0x3f, %rbp
shrdq $0x1, %rbp, %r13
andq %r10, %rdi
orq %r13, %rdi
movq 0x10(%rsp), %rax
movq %rdi, 0x8(%rax)
orq %r12, 0x18(%rax)
movq %r8, %rdx
incq %r15
movq %rdx, %r8
cmpq %r14, %r15
jne 0x15fc71
jmp 0x15fdf6
movq %r8, %rdx
testq %rdx, %rdx
je 0x15fe1f
pcmpeqd %xmm0, %xmm0
movq 0x10(%rsp), %rax
movdqu %xmm0, 0x10(%rax)
movdqu %xmm0, (%rax)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
cmpq $-0x1, (%rax)
je 0x15fdfb
movq 0x10(%rsp), %rax
cmpq $-0x1, 0x8(%rax)
jne 0x15fe0d
jmp 0x15fdfb