// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %r8, %r12
movq %rcx, %r14
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
movq 0x90(%rsp), %rbx
movl %r12d, %esi
movq %rcx, %rdx
movl $0x1, %ecx
callq 0xc680d
movq 0x10(%rbp), %rax
leaq (%r12,%r12,4), %rcx
movl 0x10(%rax,%rcx,8), %edx
movq %rdx, 0x30(%rsp)
testq %rdx, %rdx
je 0xc6e31
movq %rbp, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, 0x10(%rsp)
leaq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movq %r13, 0x50(%rsp)
movq 0x20(%r13), %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ecx, %ecx
movq 0x8(%rsp), %rdx
movq %r12, 0x48(%rsp)
movq 0x28(%rsp), %rax
movq %rcx, 0x40(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl (%rbx,%rax,8), %ecx
movq %rcx, 0x38(%rsp)
leaq (%rax,%rax,2), %rcx
movl 0xc(%rdx,%rcx,8), %eax
testq %rax, %rax
je 0xc6dd6
leaq (%rdx,%rcx,8), %rcx
movq (%rcx), %rbp
shlq $0x3, %rax
leaq (%rax,%rax,4), %r13
xorl %r12d, %r12d
movl 0xc(%rbp,%r12), %ecx
cmpl $0x0, (%rbx,%rcx,8)
js 0xc6db9
cmpb $0x0, (%r14,%rcx)
je 0xc6dae
movl 0x8(%rbp,%r12), %eax
subl (%rbp,%r12), %eax
addq %rax, %r15
addq $0x28, %r12
cmpq %r12, %r13
jne 0xc6d8f
jmp 0xc6dd6
movq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0xc6974
movq 0x8(%rsp), %rdx
testl %eax, %eax
jne 0xc6da1
jmp 0xc6dae
movq 0x38(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %r12
addq %rax, %r12
movq 0x40(%rsp), %rcx
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0xc6d51
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xc680d
testq %r12, %r12
je 0xc6e22
movq %r15, %rax
xorl %edx, %edx
divq %r12
movq %rax, %r13
movq 0x90(%rsp), %rbx
movq 0x10(%rsp), %r15
jmp 0xc6e44
xorl %r13d, %r13d
movq %rbp, %rdi
movl %r12d, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xc680d
testq %rbx, %rbx
je 0xc6ea6
leaq 0x1(%rbx), %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovaeq %rax, %rcx
movq %r13, %rdx
negq %rdx
movq %r13, %rsi
subq %r15, %rsi
movl $0x1, %edi
movq $-0x1, %rax
movq %r15, %r8
movq $-0x1, %r9
leaq (%rdx,%r8), %r10
cmpq %r8, %r13
cmovaeq %rsi, %r10
cmpq %r9, %r10
cmovbq %r10, %r9
cmovbeq %rdi, %rax
incq %rdi
addq %r15, %r8
subq %r15, %rsi
cmpq %rdi, %rcx
jne 0xc6e7c
cmpq $-0x1, %rax
jne 0xc6ea9
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq