// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %r8, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movl %ebx, %esi
movq %rcx, 0x8(%rsp)
movq %rcx, %rdx
movl $0x1, %ecx
callq 0xc5a70
movq %r15, 0x18(%rsp)
movq 0x10(%r15), %rax
movq %rbx, 0x20(%rsp)
leaq (%rbx,%rbx,4), %rcx
movl 0x10(%rax,%rcx,8), %edx
movq %rdx, 0x30(%rsp)
testq %rdx, %rdx
je 0xc6053
leaq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movq %r12, 0x50(%rsp)
movq 0x20(%r12), %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %r14, %rsi
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(%rsi,%rcx,8), %eax
testq %rax, %rax
je 0xc602c
leaq (%rsi,%rcx,8), %rcx
movq (%rcx), %r12
shlq $0x3, %rax
leaq (%rax,%rax,4), %rbp
xorl %r13d, %r13d
movl 0xc(%r12,%r13), %ecx
movl (%rbx,%rcx,8), %eax
testl %eax, %eax
js 0xc5ffe
cmpb $0x0, (%rdx,%rcx)
je 0xc6023
testl %eax, %eax
jns 0xc6017
movq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0xc5bd3
movq 0x8(%rsp), %rdx
movq %r14, %rsi
testl %eax, %eax
je 0xc6023
movl 0x8(%r12,%r13), %eax
subl (%r12,%r13), %eax
addq %rax, %r15
addq $0x28, %r13
cmpq %r13, %rbp
jne 0xc5fe8
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 0xc5faa
jmp 0xc605e
xorl %r12d, %r12d
xorl %r15d, %r15d
movq 0x8(%rsp), %rdx
movq 0x90(%rsp), %r13
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
xorl %ecx, %ecx
callq 0xc5a70
testq %r12, %r12
je 0xc6089
movq %r15, %rax
xorl %edx, %edx
divq %r12
movq %rax, %rbp
movq 0x10(%rsp), %r11
testq %r13, %r13
je 0xc60ec
leaq 0x1(%r13), %rcx
cmpq $0x3, %rcx
movl $0x2, %eax
cmovaeq %rcx, %rax
movq %rbp, %rdx
negq %rdx
movq %rbp, %rsi
subq %r11, %rsi
movl $0x1, %edi
movq $-0x1, %rcx
movq %r11, %r8
movq $-0x1, %r9
leaq (%rdx,%r8), %r10
cmpq %r8, %rbp
cmovaeq %rsi, %r10
cmpq %r9, %r10
cmovbq %r10, %r9
cmovbeq %rdi, %rcx
incq %rdi
addq %r11, %r8
subq %r11, %rsi
cmpq %rdi, %rax
jne 0xc60c6
jmp 0xc60f3
movq $-0x1, %rcx
cmpq $-0x1, %rcx
cmovneq %rcx, %r13
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq