// File: chhylp123[P]hifiasm/hic.cpp
// Function: get_MAPQ(u_trans_t*, unsigned int, unsigned int, double)
uint32_t get_MAPQ(u_trans_t *a, uint32_t a_n, uint32_t idx, double secondRate)
{
    uint32_t i;
    u_trans_t *p = &(a[idx]);
    uint64_t ovlp = 0;
    double aw = 0;
    for (i = 0; i < a_n; i++)
    {
        if(i == idx) continue;
        ovlp = ((MIN(a[i].qe, p->qe) > MAX(a[i].qs, p->qs))?
                                        (MIN(a[i].qe, p->qe) - MAX(a[i].qs, p->qs)):0);
        // if(p->qn == 447 && p->tn == 5495)
        // {
        //     fprintf(stderr, "ovlp: %lu, secondRate: %f, len: %u\n", ovlp, secondRate, MIN(a[i].qe - a[i].qs, p->qe - p->qs));
        // }
        if(ovlp == 0) continue;
        if(ovlp <= (secondRate*MIN(a[i].qe - a[i].qs, p->qe - p->qs))) continue;
        
        aw += a[i].nw;
    }
    // if(p->qn == 447 && p->tn == 5495) fprintf(stderr, "aw: %f\n", aw);
    aw = aw/p->nw;
    // if(p->qn == 447 && p->tn == 5495) fprintf(stderr, "aw: %f\n", aw);
    if(aw >= 1) return 16;
    if(aw >= 0.75) return 8;
    if(aw >= 0.5) return 4;
    if(aw >= 0.25) return 2;
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl -0x18(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x2069e1
movl -0x24(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x2067d5
jmp 0x2069d3
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
cmpl 0x4(%rcx), %eax
ja 0x206805
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x44(%rbp)
jmp 0x20680f
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jb 0x206842
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x20684b
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
movl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x2068dc
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
cmpl 0x4(%rcx), %eax
ja 0x206889
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x50(%rbp)
jmp 0x206893
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jb 0x2068c6
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
jmp 0x2068cf
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
movl -0x58(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2068e3
xorl %eax, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2068e3
movl -0x5c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x2068f8
jmp 0x2069d3
movaps 0x17b711(%rip), %xmm1   # 0x382010
movq -0x38(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x17b710(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
movq -0x30(%rbp), %rcx
movl 0x4(%rcx), %ecx
movq -0x30(%rbp), %rdx
subl (%rdx), %ecx
cmpl %ecx, %eax
ja 0x206986
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
movl %eax, -0x74(%rbp)
jmp 0x206996
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
subl (%rcx), %eax
movl %eax, -0x74(%rbp)
movsd -0x70(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
movl -0x74(%rbp), %eax
movl %eax, %eax
cvtsi2sd %rax, %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x2069b6
jmp 0x2069d3
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd 0x20(%rax), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2067bc
movsd -0x40(%rbp), %xmm0
movq -0x30(%rbp), %rax
divsd 0x20(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movsd 0x17b5ff(%rip), %xmm1   # 0x382000
ucomisd %xmm1, %xmm0
jb 0x206a10
movl $0x10, -0x4(%rbp)
jmp 0x206a6b
movsd -0x40(%rbp), %xmm0
movsd 0x178603(%rip), %xmm1   # 0x37f020
ucomisd %xmm1, %xmm0
jb 0x206a2c
movl $0x8, -0x4(%rbp)
jmp 0x206a6b
movsd -0x40(%rbp), %xmm0
movsd 0x17b837(%rip), %xmm1   # 0x382270
ucomisd %xmm1, %xmm0
jb 0x206a48
movl $0x4, -0x4(%rbp)
jmp 0x206a6b
movsd -0x40(%rbp), %xmm0
movsd 0x17b7fb(%rip), %xmm1   # 0x382250
ucomisd %xmm1, %xmm0
jb 0x206a64
movl $0x2, -0x4(%rbp)
jmp 0x206a6b
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq