// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rdx
movl $0x1, %ecx
callq 0x107590
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x107e89
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq $0x18, -0x78(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x107e76
movq -0x18(%rbp), %rax
imulq $0x18, -0x78(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x70(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x107e16
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
jne 0x107e16
jmp 0x107e65
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x107e4b
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x80(%rbp), %rax
movl %eax, %ecx
callq 0x107850
cmpl $0x0, %eax
jne 0x107e4b
jmp 0x107e65
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x107dad
jmp 0x107e78
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x107d5b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x107590
cmpq $0x0, -0x50(%rbp)
je 0x107eb8
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rax, -0x48(%rbp)
jmp 0x107ec0
movq $0x0, -0x48(%rbp)
movq $-0x1, -0x90(%rbp)
movq $-0x1, -0x88(%rbp)
movq $0x1, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq 0x10(%rbp), %rax
ja 0x107f5d
movq -0x30(%rbp), %rax
imulq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jb 0x107f10
movq -0x48(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x107f1f
movq -0x58(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x88(%rbp), %rax
ja 0x107f4d
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x107f4f
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x107ede
cmpq $-0x1, -0x90(%rbp)
jne 0x107f72
movq 0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)