// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: infer_mmhap_copy(ma_ug_t*, asg_t*, ma_hit_t_alloc*, unsigned char*, unsigned long, unsigned long, unsigned long)
uint64_t infer_mmhap_copy(ma_ug_t *ug, asg_t *sg, ma_hit_t_alloc *src, uint8_t *ff, uint64_t uid, uint64_t het_cov, uint64_t n_hap)
{
    ma_hit_t *h; ma_utg_t *u;
    uint64_t C_bases, R_bases, cc, dd, k, j, rid, tn, md, mk;
    set_rset(ug, uid, ff, 1);

    u = &(ug->u.a[uid]);
    for (k = C_bases = R_bases = 0; k < u->n; k++) {
        rid = u->a[k]>>33; R_bases += sg->seq[rid].len;
        for (j = 0; j < src[rid].length; j++) {
            h = &(src[rid].buffer[j]);
            tn = Get_tn((*h));
            if((!sg->seq[tn].del) && (!ff[tn])) continue;
            if((sg->seq[tn].del) && (!check_nc_status(sg, src, ff, tn))) continue;
            C_bases += (Get_qe((*h)) - Get_qs((*h)));
        }
        // res[k] = C_bases;
    }

    set_rset(ug, uid, ff, 0);

    if(R_bases) C_bases /= R_bases;
    else C_bases = 0;

    md = mk = (uint64_t)-1; 
    for (k = 1; k <= n_hap; k++) {
        cc = het_cov*k;
        dd = ((C_bases>=cc)?(C_bases-cc):(cc-C_bases));
        if(dd <= md) {
            md = dd; mk = k;
        }
    }
    if(mk == ((uint64_t)-1)) mk = n_hap;
    return mk;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, (%rsp)
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
pushq $0x1
popq %rcx
movl %r14d, %esi
movq %rbx, %rdx
callq 0x8a39a
movq %rbx, %rdx
movq %r15, 0x18(%rsp)
movq 0x10(%r15), %rax
movq %r14, 0x20(%rsp)
imulq $0x28, %r14, %rcx
leaq (%rax,%rcx), %rsi
movq %rsi, 0x28(%rsp)
movl 0x10(%rax,%rcx), %eax
movq %rax, 0x30(%rsp)
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq 0x30(%rsp), %rcx
je 0x8a939
movq %rbx, 0x48(%rsp)
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x40(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %r14
movl (%r14,%rax,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, 0x38(%rsp)
imulq $0x18, %rax, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0xc(%rcx,%rax), %eax
imulq $0x28, %rax, %r12
xorl %ebp, %ebp
movq %rdi, 0x50(%rsp)
cmpq %rbp, %r12
je 0x8a922
movq (%rdi), %r15
movl 0xc(%r15,%rbp), %ecx
cmpl $0x0, (%r14,%rcx,8)
js 0x8a8fc
cmpb $0x0, (%rdx,%rcx)
je 0x8a8f6
movl 0x8(%r15,%rbp), %eax
subl (%r15,%rbp), %eax
addq %rax, %r13
addq $0x28, %rbp
jmp 0x8a8d0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rbx
movq %rdx, %r13
callq 0x8a4ca
movq 0x50(%rsp), %rdi
movq %r13, %rdx
movq %rbx, %r13
testl %eax, %eax
jne 0x8a8ea
jmp 0x8a8f6
movq 0x48(%rsp), %rbx
addq 0x38(%rsp), %rbx
movq 0x40(%rsp), %rcx
incq %rcx
jmp 0x8a876
xorl %r12d, %r12d
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
xorl %ecx, %ecx
callq 0x8a39a
testq %rbx, %rbx
je 0x8a95d
movq %r13, %rax
xorl %edx, %edx
divq %rbx
movq %rax, %r12
movq (%rsp), %r10
movq 0x90(%rsp), %rax
movq %r12, %r11
negq %r11
movq %r12, %rcx
subq %r10, %rcx
pushq $-0x1
popq %rdx
pushq $0x1
popq %rsi
movq %r10, %r8
movq %rdx, %rdi
cmpq %rax, %rsi
ja 0x8a9a7
leaq (%r11,%r8), %r9
cmpq %r8, %r12
cmovaeq %rcx, %r9
cmpq %rdx, %r9
cmovbq %r9, %rdx
cmovbeq %rsi, %rdi
incq %rsi
addq %r10, %r8
subq %r10, %rcx
jmp 0x8a981
cmpq $-0x1, %rdi
cmovneq %rdi, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq