// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x215009
cmpq $0x0, -0x28(%rbp)
jne 0x21500e
jmp 0x2153c2
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x78(%rbp), %rcx
leaq -0x98(%rbp), %r8
leaq -0x80(%rbp), %r9
leaq -0xa0(%rbp), %rax
movq %rax, (%rsp)
callq 0x1c3580
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x88(%rbp), %rcx
leaq -0xa8(%rbp), %r8
leaq -0x90(%rbp), %r9
leaq -0xb0(%rbp), %rax
movq %rax, (%rsp)
callq 0x1c3580
cmpq $0x0, -0x98(%rbp)
je 0x2150d8
cmpq $0x0, -0xa8(%rbp)
jne 0x2150dd
jmp 0x2153c2
movq 0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x98(%rbp), %rax
jae 0x21523a
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq -0x50(%rbp), %r9
leaq -0x58(%rbp), %r10
leaq -0x60(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x213560
movq -0x48(%rbp), %rax
addq $0x1, %rax
cmpq -0x60(%rbp), %rax
jae 0x21515a
jmp 0x215229
movq -0x48(%rbp), %rax
addq $0x1, %rax
subq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x215198
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl $0x1, %eax
cltq
subq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, 0x18(%rbp)
je 0x2151c7
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq 0x18(%rbp), %r8
callq 0x2153d0
cmpl $0x0, %eax
je 0x2151c7
movq $0x1, -0x70(%rbp)
jmp 0x215229
movq -0x38(%rbp), %rax
shlq $0x3f, %rax
movq %rax, -0xb8(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shlq %cl, %rax
movq -0xb8(%rbp), %rcx
shrq %rax
orq %rax, %rcx
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rdx
andq 0x30(%rdx), %rax
orq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
shlq $0x20, %rcx
movq %rcx, 0x18(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x2150fd
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jae 0x215377
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq -0x50(%rbp), %r9
leaq -0x58(%rbp), %r10
leaq -0x60(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x213560
movq -0x48(%rbp), %rax
addq $0x1, %rax
cmpq -0x60(%rbp), %rax
jae 0x2152a2
jmp 0x215366
movq -0x48(%rbp), %rax
addq $0x1, %rax
subq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x2152e0
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl $0x1, %eax
cltq
subq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, 0x18(%rbp)
je 0x21530f
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq 0x18(%rbp), %r8
callq 0x2153d0
cmpl $0x0, %eax
je 0x21530f
movq $0x1, -0x70(%rbp)
jmp 0x215366
movq -0x38(%rbp), %rax
shlq $0x3f, %rax
movq %rax, -0xc0(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shlq %cl, %rax
movq -0xc0(%rbp), %rcx
shrq %rax
orq %rax, %rcx
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rdx
andq 0x30(%rdx), %rax
orq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rcx
movq -0x30(%rbp), %rax
orq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x215242
cmpq $0x0, -0x70(%rbp)
jne 0x215393
movq -0x30(%rbp), %rax
cmpq $-0x1, (%rax)
je 0x215393
movq -0x30(%rbp), %rax
cmpq $-0x1, 0x8(%rax)
jne 0x2153c2
movq -0x30(%rbp), %rax
movq $-0x1, 0x18(%rax)
movq -0x30(%rbp), %rax
movq $-0x1, 0x8(%rax)
movq -0x30(%rbp), %rax
movq $-0x1, (%rax)
movq -0x30(%rbp), %rax
movq $-0x1, 0x10(%rax)
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax,%rax)