// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_trans_rate_function_advance(ha_ug_index*, kvec_pe_hit*, hc_links*, bubble_type*, H_partition*, signed char*, mc_gg_status*, trans_idx*)
int get_trans_rate_function_advance(ha_ug_index* idx, kvec_pe_hit* hits, hc_links* link, bubble_type* bub, 
H_partition* hap, int8_t *s, mc_gg_status *sa, trans_idx* dis)
{
    kvec_t(uint64_t) buf;
    kv_init(buf);
    uint64_t beg, end, cnt[2];
    uint64_t k, i, t_d, r_idx, f_idx, med = (uint64_t)-1;
    uint32_t is_s;
    // int beg_status, end_status;

    buf.n = 0;
    for (k = 0; k < hits->a.n; ++k) 
    {
        beg = ((hits->a.a[k].s<<1)>>(64 - idx->uID_bits));
        end = ((hits->a.a[k].e<<1)>>(64 - idx->uID_bits));

        if(IF_HOM(beg, *bub)) continue;
        if(IF_HOM(end, *bub)) continue;
        if(is_hom_hit(hits->a.a[k])) continue;


        t_d = get_hic_distance(&(hits->a.a[k]), link, idx, NULL);
        if(t_d == (uint64_t)-1) continue;
        // if(beg == end)
        // {
        //     t_d = (t_d << 1);
        // }
        // else
        // {
        //     beg_status = (hap? get_phase_status(hap, beg):s[beg]);
        //     if(beg_status != 1 && beg_status != -1) continue;
        //     end_status = (hap? get_phase_status(hap, end):s[end]);
        //     if(end_status != 1 && end_status != -1) continue;
        //     if(beg_status != end_status)
        //     {
        //         t_d = (t_d << 1) + 1; 
        //     }
        //     else
        //     {
        //         t_d = (t_d << 1);
        //     }            
        // }
        is_s = is_same_phase(beg, end, hap, s, sa);
        if(is_s == (uint32_t)-1) continue;
        t_d = (t_d << 1) + 1 - is_s;

        kv_push(uint64_t, buf, t_d); 
    }

    ///might have bias, we may not use right linkage larger than trans rc linkage
    radix_sort_hc64(buf.a, buf.a+buf.n);

    for (k = 0, r_idx = f_idx = (uint64_t)-1; k < buf.n; k++)
    {
        if((buf.a[k]&1) == 0) r_idx = k;
        if((buf.a[k]&1) == 1) f_idx = k;
    }
    buf.n = MIN(r_idx, f_idx);

    
    trans_p_t* p = NULL;
    dis->n = 0;
    uint64_t bin_size = MIN(2250, buf.n>>8), m;
    if(bin_size == 0)
    {
        for (i = 8; i > 0; i--)
        {
            bin_size = buf.n>>i;
            if(bin_size > 0) break;
        }
        if(bin_size == 0) bin_size = buf.n;
    }
    i = 0;
    while (i < buf.n)
    {
        kv_pushp(trans_p_t, *dis, &p);
        p->beg = i;

        cnt[0] = cnt[1] = 0;
        k = MIN(i+bin_size, buf.n);
        for (; i < k; i++)
        {
            cnt[buf.a[i]&1]++;
        }
        p->end = i;
        p->cnt_0 = cnt[0];
        p->cnt_1 = cnt[1];
    }

    i = m = 0; med = (uint64_t)-1;
    while(i < dis->n)
    {
        if(dis->a[i].cnt_0 > 0 && dis->a[i].cnt_1 > 0)
        {
            dis->a[m] = dis->a[i];
            m++;
            i++;
            continue;
        }

        if(med == (uint64_t)-1) med = buf.a[dis->a[i].beg]>>1;
        k = i; cnt[0] = cnt[1] = 0;
        for (; i < dis->n; i++)
        {
            cnt[0] += dis->a[i].cnt_0;
            cnt[1] += dis->a[i].cnt_1;
            if(cnt[0] > 0 && cnt[1] > 0) break;
        }

        if(i < dis->n)
        {
            dis->a[m].cnt_0 = cnt[0];
            dis->a[m].cnt_1 = cnt[1];
            dis->a[m].beg = dis->a[k].beg;
            dis->a[m].end = dis->a[i].end;
            m++;
            i++;
            continue;
        }

        cnt[0] -= dis->a[k].cnt_0;
        cnt[1] -= dis->a[k].cnt_1;
        while (1)
        {
            cnt[0] += dis->a[k].cnt_0;
            cnt[1] += dis->a[k].cnt_1;
            if(cnt[0] > 0 && cnt[1] > 0) break;

            if(k == 0)
            {
                k = (uint64_t)-1;
                break;
            } 
            k--;
        }

        if(k != (uint64_t)-1)
        {
            dis->a[m].cnt_0 = cnt[0];
            dis->a[m].cnt_1 = cnt[1];
            dis->a[m].end = dis->a[i-1].end;
            continue;
        }
        m = 0;
        break;
    }
    dis->n = m;
    if(dis->n == 0)
    {
        kv_destroy(buf);
        return 0;
    } 

    for (i = 0; i < dis->n; i++)
    {
        dis->a[i].beg = buf.a[dis->a[i].beg]>>1;
        dis->a[i].end = (buf.a[dis->a[i].end-1]>>1);
    }

    for (i = 0; i < dis->n - 1; i++)
    {
        dis->a[i].end += ((dis->a[i+1].beg - dis->a[i].end)/2);
        dis->a[i+1].beg = dis->a[i].end;
    }

    // for (i = 0; i < dis->n; i++)
    // {
    //     if(i > 0 && dis->a[i].beg != dis->a[i-1].end) fprintf(stderr, "ERROR: dis->a[i].beg: %lu, dis->a[i-1].end: %lu\n", dis->a[i].beg, dis->a[i-1].end);
    //     fprintf(stderr, "beg: %lu, end: %lu, cnt_0: %lu, cnt_1: %lu, error_rate: %f\n", 
    //     dis->a[i].beg, dis->a[i].end, dis->a[i].cnt_0, dis->a[i].cnt_1, (double)(dis->a[i].cnt_1)/(double)(dis->a[i].cnt_1 + dis->a[i].cnt_0));
    // }
    
    LeastSquare_advance(dis, idx, med);
    // fprintf(stderr, "idx->a: %f, idx->b: %f, idx->frac: %f, med: %lu\n", 
    // (double)idx->a, (double)idx->b, (double)idx->frac, med);

    dis->max = dis->a[dis->n-1].end;


    
    if(idx->a < 0) idx->a = 0;
    if(idx->a == 0)
    {
        idx->b = MAX((((double)(dis->a[dis->n-1].cnt_1))/((double)(dis->a[dis->n-1].cnt_0 + dis->a[dis->n-1].cnt_1))), idx->b);
    }
    if(idx->b < 0 && get_trans(idx, dis->max) < 0)
    {
        idx->b = ((double)(dis->a[dis->n-1].cnt_1))/((double)(dis->a[dis->n-1].cnt_0 + dis->a[dis->n-1].cnt_1));
    } 

    // fprintf(stderr, "idx->a: %f, idx->b: %f, idx->frac: %f, med: %lu\n", 
    // (double)idx->a, (double)idx->b, (double)idx->frac, med);





    buf.n = 0;
    for (k = 0; k < hits->a.n; ++k) 
    {
        beg = ((hits->a.a[k].s<<1)>>(64 - idx->uID_bits));
        end = ((hits->a.a[k].e<<1)>>(64 - idx->uID_bits));

        if(IF_HOM(beg, *bub)) continue;
        if(IF_HOM(end, *bub)) continue;
        if(is_hom_hit(hits->a.a[k])) continue;
        if(beg == end) continue;

        t_d = get_hic_distance(&(hits->a.a[k]), link, idx, NULL);
        if(t_d == (uint64_t)-1) continue;
        kv_push(uint64_t, buf, t_d); 
    }

    ///might have bias, we may not use right linkage larger than trans rc linkage
    radix_sort_hc64(buf.a, buf.a+buf.n);
    if(buf.n == 0)
    {
        dis->med = 0;
    }
    else
    {
        dis->med = ((buf.n&1)?buf.a[buf.n>>1]:((buf.a[buf.n>>1]+buf.a[(buf.n>>1)-1])/2));
    }

    // fprintf(stderr, "dis->med: %lu\n", dis->med);
    
    kv_destroy(buf);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r8
movq %rdx, 0x40(%rsp)
movq %rdi, %rdx
xorl %r13d, %r13d
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %rcx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
cmpq (%rsi), %rbp
jae 0x10ab97
movq 0x10(%rsi), %rdi
movq (%rdi,%r13), %r12
addq %r12, %r12
movb 0x18(%rdx), %al
xorl %ecx, %ecx
subb %al, %cl
shrq %cl, %r12
movq 0x8(%rdi,%r13), %r15
addq %r15, %r15
shrq %cl, %r15
movq (%r8), %rcx
movq 0x80(%r8), %rax
movl (%rcx,%r12,4), %r9d
incq %rax
cmpq %r9, %rax
jb 0x10ab8b
movl (%rcx,%r15,4), %ecx
cmpq %rcx, %rax
jb 0x10ab8b
cmpq $-0x1, 0x10(%rdi,%r13)
je 0x10ab8b
addq %r13, %rdi
movq 0x40(%rsp), %rsi
xorl %ecx, %ecx
callq 0x102875
cmpq $-0x1, %rax
je 0x10ab7c
movq %rax, 0x20(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0xb0(%rsp), %r8
callq 0x10a991
cmpl $-0x1, %eax
je 0x10ab7c
movl %eax, %r15d
movq 0x28(%rsp), %rax
cmpq %rax, %r14
jne 0x10ab51
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq %rbx, %rdi
callq 0x2370
movq %rax, %rbx
movq %r12, 0x28(%rsp)
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rax
leaq 0x1(,%rax,2), %rax
movl %r15d, %ecx
subq %rcx, %rax
movq %rax, (%rbx,%r14,8)
incq %r14
jmp 0x10ab8b
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
incq %rbp
addq $0x20, %r13
jmp 0x10aa82
leaq (%rbx,%r14,8), %rsi
movq %rbx, %rdi
callq 0xf116c
pushq $-0x1
popq %rax
xorl %ecx, %ecx
movq %rax, %r15
movq 0xb8(%rsp), %r13
pushq $0x2
popq %rbp
cmpq %rcx, %r14
je 0x10abcc
testb $0x1, (%rbx,%rcx,8)
cmoveq %rcx, %rax
cmovneq %rcx, %r15
incq %rcx
jmp 0x10abb6
cmpq %r15, %rax
cmovbq %rax, %r15
andq $0x0, 0x8(%r13)
movq %r15, %rax
shrq $0x8, %rax
cmpq $0x8ca00, %r15          # imm = 0x8CA00
movl $0x8ca, %r14d           # imm = 0x8CA
cmovbq %rax, %r14
testq %r14, %r14
jne 0x10ac0f
pushq $0x8
popq %rcx
movq %rcx, %rax
movq %r15, %r14
subq $0x1, %rax
jb 0x10ac0f
shrq %cl, %r14
movq %rax, %rcx
testq %r14, %r14
je 0x10abfb
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
cmpq %r15, %r12
jae 0x10ac9c
movq (%r13), %rax
cmpq 0x10(%r13), %rcx
jne 0x10ac4e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %rbp, %rdx
movq %rdx, 0x10(%r13)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
xorpd %xmm1, %xmm1
movq %rax, (%r13)
movq 0x8(%r13), %rcx
imulq $0x28, %rcx, %rsi
incq %rcx
movq %rcx, 0x8(%r13)
leaq (%rax,%rsi), %rdx
movq %r12, (%rax,%rsi)
leaq (%r12,%r14), %rax
cmpq %r15, %rax
cmovaeq %r15, %rax
movapd %xmm1, 0x30(%rsp)
cmpq %rax, %r12
jae 0x10ac88
movl (%rbx,%r12,8), %esi
andl $0x1, %esi
incq 0x30(%rsp,%rsi,8)
incq %r12
jmp 0x10ac72
movq %r12, 0x8(%rdx)
movapd 0x30(%rsp), %xmm0
movupd %xmm0, 0x18(%rdx)
jmp 0x10ac18
pushq $-0x1
popq %rdx
xorl %r9d, %r9d
xorpd %xmm0, %xmm0
xorl %r12d, %r12d
imulq $0x28, %r9, %r11
imulq $0x28, %r12, %rsi
addq $0x20, %rsi
leaq 0x20(%r11), %r14
leaq 0x1(%r9), %rdi
leaq -0x8(%r11), %r8
cmpq %rcx, %r9
jae 0x10ae25
movq (%r13), %r10
movq 0x18(%r10,%r11), %r15
testq %r15, %r15
je 0x10ad22
cmpq $0x0, 0x20(%r10,%r11)
je 0x10ad22
leaq (%r10,%r11), %rcx
movq 0x20(%rcx), %r15
movq %r15, (%r10,%rsi)
movupd (%rcx), %xmm1
movupd 0x10(%rcx), %xmm2
movupd %xmm2, -0x10(%r10,%rsi)
movupd %xmm1, -0x20(%r10,%rsi)
incq %r12
incq %r9
movq 0x8(%r13), %rcx
addq $0x28, %r11
addq $0x28, %rsi
addq $0x28, %r14
incq %rdi
addq $0x28, %r8
jmp 0x10acc1
movq %r12, 0x20(%rsp)
cmpq $-0x1, %rdx
jne 0x10ad38
movq (%r10,%r11), %rdx
movq (%rbx,%rdx,8), %rdx
shrq %rdx
movapd %xmm0, 0x30(%rsp)
addq %r10, %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq %rcx, %r9
jae 0x10ad68
addq -0x8(%r14), %r13
setne %bpl
addq (%r14), %r12
setne %al
testb %al, %bpl
jne 0x10ada9
incq %r9
addq $0x28, %r14
jmp 0x10ad47
subq %r15, %r13
movq 0x20(%r10,%r11), %r9
subq %r9, %r12
addq %r10, %r8
movq %r13, %rax
movq %r12, %r11
movq %r9, %r12
addq %r11, %r12
movq %r15, %r13
addq %rax, %r13
je 0x10ad8f
testq %r12, %r12
jne 0x10ade6
cmpq $0x1, %rdi
je 0x10aedf
movq -0x8(%r8), %r15
movq (%r8), %r9
decq %rdi
addq $-0x28, %r8
jmp 0x10ad76
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq %r13, -0x8(%r10,%rsi)
movq %r12, (%r10,%rsi)
movq (%r10,%r11), %rax
movq %rax, -0x20(%r10,%rsi)
movq -0x18(%r14), %rax
movq %rax, -0x18(%r10,%rsi)
movq 0x20(%rsp), %r12
incq %r12
incq %r9
movq 0xb8(%rsp), %r13
jmp 0x10aca9
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
testq %rdi, %rdi
je 0x10aedf
movq %r13, -0x8(%r10,%rsi)
movq %r12, (%r10,%rsi)
imulq $0x28, %rcx, %rax
movq -0x20(%r10,%rax), %rax
movq %rax, -0x18(%r10,%rsi)
movq %rcx, %r9
movq 0xb8(%rsp), %r13
movq 0x20(%rsp), %r12
jmp 0x10aca9
movq %r12, 0x8(%r13)
testq %r12, %r12
je 0x10aeec
movq (%r13), %rcx
leaq 0x8(%rcx), %r9
movq %r12, %rdi
movq 0x18(%rsp), %rsi
subq $0x1, %rdi
jb 0x10ae6b
movq -0x8(%r9), %rax
movq (%r9), %r8
movq (%rbx,%rax,8), %rax
shrq %rax
movq %rax, -0x8(%r9)
movq -0x8(%rbx,%r8,8), %rax
shrq %rax
movq %rax, (%r9)
addq $0x28, %r9
jmp 0x10ae42
addq $0x28, %rcx
pushq $0x1
popq %r8
cmpq %r8, %r12
je 0x10ae98
movq -0x20(%rcx), %rax
movq (%rcx), %rdi
subq %rax, %rdi
shrq %rdi
addq %rax, %rdi
movq %rdi, -0x20(%rcx)
movq %rdi, (%rcx)
incq %r8
addq $0x28, %rcx
jmp 0x10ae73
movq %r13, %rdi
callq 0x10297a
movq 0x18(%rsp), %rdx
movq (%r13), %rcx
imulq $0x28, 0x8(%r13), %rsi
leaq (%rcx,%rsi), %rax
movq -0x20(%rcx,%rsi), %rcx
movq %rcx, 0x18(%r13)
fldt 0x80(%rdx)
fldz
fucomi %st(1), %st
jbe 0x10aef3
fstp %st(1)
fld %st(0)
fstpt 0x80(%rdx)
fld %st(0)
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
jmp 0x10af0d
movq 0xb8(%rsp), %rax
andq $0x0, 0x8(%rax)
xorl %ebp, %ebp
jmp 0x10b153
fxch %st(1)
fucomi %st(1), %st
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
jne 0x10b16c
jp 0x10b16c
movq -0x8(%rax), %rsi
movq %rsi, %xmm0
movq 0xd8352(%rip), %xmm1    # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xd8356(%rip), %xmm2    # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addq -0x10(%rax), %rsi
addsd %xmm0, %xmm3
movq %rsi, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, 0x70(%rsp)
fldl 0x70(%rsp)
fldt 0x90(%rdx)
fxch %st(1)
fucomi %st(1), %st
fxch %st(1)
fcmovnb %st(1), %st
fstp %st(1)
fld %st(0)
fstpt 0x90(%rdx)
fxch %st(2)
fucompi %st(2), %st
jbe 0x10b02d
movq %rcx, 0x68(%rsp)
xorl %esi, %esi
testq %rcx, %rcx
sets %sil
fildll 0x68(%rsp)
leaq 0xe2188(%rip), %rcx     # 0x1ed128
fadds (%rcx,%rsi,4)
fldt 0xa0(%rdx)
fdivrp %st, %st(1)
fmulp %st, %st(1)
faddp %st, %st(1)
fstpl 0x60(%rsp)
xorpd %xmm0, %xmm0
ucomisd 0x60(%rsp), %xmm0
fldz
fldz
fxch %st(1)
jbe 0x10b02d
fstp %st(0)
fstp %st(0)
movq -0x8(%rax), %rcx
movq %rcx, %xmm0
movq 0xd8296(%rip), %xmm1    # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xd829a(%rip), %xmm2    # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addq -0x10(%rax), %rcx
movq %rcx, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, 0x58(%rsp)
fldl 0x58(%rsp)
fstpt 0x90(%rdx)
fldz
fldz
fxch %st(1)
fstp %st(0)
fstp %st(0)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq (%r10), %r13
jae 0x10b110
movq 0x10(%r10), %rdi
movq (%rdi,%r12), %rax
addq %rax, %rax
movb 0x18(%rdx), %sil
xorl %ecx, %ecx
subb %sil, %cl
shrq %cl, %rax
movq 0x8(%rdi,%r12), %r11
addq %r11, %r11
shrq %cl, %r11
movq (%r9), %rsi
movq 0x80(%r9), %rcx
movl (%rsi,%rax,4), %r8d
incq %rcx
cmpq %r8, %rcx
jb 0x10b104
movl (%rsi,%r11,4), %esi
cmpq %rsi, %rcx
jb 0x10b104
cmpq $-0x1, 0x10(%rdi,%r12)
je 0x10b104
cmpq %r11, %rax
je 0x10b104
addq %r12, %rdi
movq 0x40(%rsp), %rsi
xorl %ecx, %ecx
callq 0x102875
cmpq $-0x1, %rax
je 0x10b0f5
movq %rax, %r15
movq 0x28(%rsp), %rax
cmpq %rax, %r14
jne 0x10b0dd
leaq (%rax,%rax), %rbp
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x2370
movq %rax, %rbx
movq %rbp, 0x28(%rsp)
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
movq 0x18(%rsp), %rdx
movq %r15, (%rbx,%r14,8)
incq %r14
jmp 0x10b104
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
movq 0x18(%rsp), %rdx
incq %r13
addq $0x20, %r12
jmp 0x10b03a
leaq (%rbx,%r14,8), %rsi
movq %rbx, %rdi
callq 0xf116c
testq %r14, %r14
je 0x10b142
movl %r14d, %ecx
shrq %r14
movq (%rbx,%r14,8), %rax
testb $0x1, %cl
movq 0xb8(%rsp), %rcx
jne 0x10b14c
addq -0x8(%rbx,%r14,8), %rax
shrq %rax
jmp 0x10b14c
xorl %eax, %eax
movq 0xb8(%rsp), %rcx
movq %rax, 0x20(%rcx)
pushq $0x1
popq %rbp
movq %rbx, %rdi
callq 0x2220
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
fldt 0x90(%rdx)
jmp 0x10af7d