// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/path.c
// Function: graph_sequence_coverage_precise
double graph_sequence_coverage_precise(asg_t *asg, double min_cf, int min_copy, int max_copy, int **_copy_number)
{
    uint32_t i, n_seg, iter;
    int *copy_number;
    double total_covs, total_lens, avg_cov, new_avg_cov, min_avg_cov;
    asmg_t *g;
    
    n_seg = asg->n_seg;
    g = asg->asmg;
    min_avg_cov = graph_sequence_coverage_lower_bound(asg, 0.3);
    avg_cov = graph_sequence_coverage_rough(asg, min_cf);
#ifdef DEBUG_SEG_COPY_EST
    fprintf(stderr, "[DEBUG_SEG_COPY_EST::%s] min coverage: %.3f; rough avg coverage: %.3f\n",
            __func__, min_avg_cov, avg_cov);
#endif
    avg_cov = MAX(avg_cov, min_avg_cov);
    MYCALLOC(copy_number, n_seg);
    for (i = 0; i < n_seg; ++i) {
        if (g->vtx[i].del) continue;
        copy_number[i] = MIN(MAX(min_copy, lround((double) g->vtx[i].cov / avg_cov)), max_copy);
    }

    iter = 0;
    while (iter++ < EM_MAX_ITER) {
#ifdef DEBUG_SEG_COPY_EST
        fprintf(stderr, "[DEBUG_SEG_COPY_EST::%s] iteration %u avg coverage: %.3f\n", __func__, iter, avg_cov);
#endif
        total_covs = total_lens = 0;
        for (i = 0; i < n_seg; ++i) {
            if (g->vtx[i].del) continue;
            total_lens += (double) g->vtx[i].len * copy_number[i];
            total_covs += (double) g->vtx[i].len * g->vtx[i].cov;
        }
        // FIXME total_lens could be zero
        new_avg_cov = total_lens < FLT_EPSILON? DBL_MAX : (total_covs / total_lens);
        new_avg_cov = MAX(new_avg_cov, min_avg_cov);
        if (fabs(new_avg_cov - avg_cov) < FLT_EPSILON) 
            break; // converged
        avg_cov = new_avg_cov;
        for (i = 0; i < n_seg; ++i) {
            if (g->vtx[i].del) continue;
            copy_number[i] = MIN(MAX(min_copy, lround((double) g->vtx[i].cov / avg_cov)), max_copy);
        }
    }

#ifdef DEBUG_SEG_COPY_EST
    fprintf(stderr, "[DEBUG_SEG_COPY_EST::%s] sequence copy number estimation finished in %u iterations with an average sequence coverage %.3f\n",
            __func__, iter, avg_cov);
    for (i = 0; i < n_seg; ++i) {
        if (g->vtx[i].del) continue;
        fprintf(stderr, "[DEBUG_SEG_COPY_EST::%s] %s %u %d\n", __func__, asg->seg[i].name, g->vtx[i].cov, copy_number[i]);
    }
#endif
    
    if (_copy_number)
        *_copy_number = copy_number;
    else
        free(copy_number);

    return avg_cov;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x60(%rsp)
movl %edx, %r15d
movl %esi, 0xc(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %rdi, %r12
movq 0x8(%rdi), %rax
movq 0x20(%rdi), %rbx
movq %rax, 0x48(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
callq 0x51c0
movsd %xmm0, 0x58(%rsp)
movq 0x20(%r12), %rsi
cmpq $0x0, (%rsi)
je 0x4d28
movl $0x1, %ebp
xorl %ecx, %ecx
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
xorl %r13d, %r13d
xorl %eax, %eax
xorl %r12d, %r12d
movl %r15d, 0x54(%rsp)
movq %rsi, 0x10(%rsp)
movq 0x10(%rsi), %rdx
leaq (%rcx,%rcx,4), %r14
testb $0x40, 0x23(%rdx,%r14,8)
jne 0x4d1d
cmpq %rax, %r13
jne 0x4d02
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq %r12, %rdi
callq 0x3330
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
movq 0x10(%rsp), %rsi
movq %rax, %r12
movq %r15, %rax
movl 0x54(%rsp), %r15d
movq 0x10(%rsi), %rcx
movl 0x20(%rcx,%r14,8), %edx
andl %edi, %edx
shlq $0x20, %rdx
orq 0x18(%rcx,%r14,8), %rdx
movq %rdx, (%r12,%r13,8)
incq %r13
movl %ebp, %ecx
incl %ebp
cmpq %rcx, (%rsi)
ja 0x4cb8
jmp 0x4d2e
xorl %r12d, %r12d
xorl %r13d, %r13d
testq %r13, %r13
je 0x4e6c
leaq 0xbadf(%rip), %rcx      # 0x1081d
movl $0x8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x30c0
movsd 0x1473a(%rip), %xmm0    # 0x19490
xorl %ecx, %ecx
movsd 0x14738(%rip), %xmm1    # 0x19498
xorpd %xmm2, %xmm2
movsd 0x14734(%rip), %xmm3    # 0x194a0
movapd 0x1480c(%rip), %xmm4    # 0x19580
xorl %eax, %eax
movsd 0x40(%rsp), %xmm11
movq (%r12,%rcx,8), %rdx
shrq $0x20, %rdx
je 0x4e27
xorps %xmm8, %xmm8
cvtsi2sd %rdx, %xmm8
xorpd %xmm7, %xmm7
xorl %edx, %edx
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
movq (%r12,%rdx,8), %rsi
movl %esi, %edi
shrq $0x20, %rsi
xorps %xmm9, %xmm9
cvtsi2sd %rsi, %xmm9
movapd %xmm9, %xmm10
divsd %xmm8, %xmm10
ucomisd %xmm11, %xmm10
xorps %xmm10, %xmm10
cvtsi2sd %rdi, %xmm10
jae 0x4dd6
addsd %xmm10, %xmm7
jmp 0x4dea
addsd %xmm10, %xmm6
mulsd %xmm10, %xmm9
divsd %xmm8, %xmm9
addsd %xmm9, %xmm5
incq %rdx
movl %edx, %esi
cmpq %rsi, %r13
ja 0x4da2
movapd %xmm6, %xmm8
addsd %xmm7, %xmm8
divsd %xmm8, %xmm7
ucomisd %xmm1, %xmm7
ja 0x4e32
ucomisd %xmm2, %xmm6
jbe 0x4e27
divsd %xmm6, %xmm5
addsd %xmm3, %xmm5
andpd %xmm4, %xmm5
ucomisd %xmm5, %xmm0
jbe 0x4e27
movl %ecx, %eax
movapd %xmm5, %xmm0
incl %ecx
cmpq %rcx, %r13
ja 0x4d7d
xorpd %xmm1, %xmm1
ucomisd 0x14652(%rip), %xmm0    # 0x19490
movq 0x48(%rsp), %r13
jne 0x4e47
jnp 0x4e56
movl %eax, %eax
movl 0x4(%r12,%rax,8), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movapd %xmm1, 0x10(%rsp)
movq %r12, %rdi
callq 0x3060
movapd 0x10(%rsp), %xmm0
jmp 0x4e75
xorpd %xmm0, %xmm0
movq 0x48(%rsp), %r13
maxsd 0x58(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movl $0x4, %esi
movq 0x28(%rsp), %rdi
callq 0x3240
movq %rax, 0x30(%rsp)
movslq %r15d, %rax
movq %rax, 0x40(%rsp)
testl %r13d, %r13d
je 0x4f91
movslq 0xc(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,4), %r14
xorl %ebp, %ebp
movq 0x30(%rsp), %r12
movq 0x10(%rbx), %rax
movl 0x20(%rax,%rbp), %eax
btl $0x1e, %eax
jb 0x4f80
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movq 0x38(%rsp), %rdx
movq %rdx, %rcx
cmpq %rdx, %rax
jl 0x4f22
movq 0x10(%rbx), %rax
movl 0x20(%rax,%rbp), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl %r15d, %eax
cmpq 0x40(%rsp), %rcx
jge 0x4f7c
movq 0x10(%rbx), %rax
movl 0x20(%rax,%rbp), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpq 0x38(%rsp), %rcx
jl 0x4f7c
movq 0x10(%rbx), %rax
movl 0x20(%rax,%rbp), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movl %eax, (%r12)
addq $0x4, %r12
addq $0x28, %rbp
cmpq %rbp, %r14
jne 0x4ec8
movslq 0xc(%rsp), %r14
movq 0x28(%rsp), %rsi
leaq (,%rsi,8), %rax
leaq (%rax,%rax,4), %rbp
movl $0x1, %r8d
movapd 0x10(%rsp), %xmm4
movq 0x30(%rsp), %rdi
movapd %xmm4, %xmm5
testl %r13d, %r13d
je 0x502e
movq 0x10(%rbx), %rax
addq $0x20, %rax
xorpd %xmm4, %xmm4
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movl (%rax), %edx
btl $0x1e, %edx
jb 0x5020
movsd -0x8(%rax), %xmm1
unpcklps 0x145a9(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x145b1(%rip), %xmm1    # 0x195a0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
xorps %xmm3, %xmm3
cvtsi2sdl (%rdi,%rcx,4), %xmm3
addsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm4
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rcx
addq $0x28, %rax
cmpq %rcx, %rsi
jne 0x4fd3
jmp 0x5036
xorpd %xmm0, %xmm0
xorpd %xmm4, %xmm4
divsd %xmm4, %xmm0
movsd 0x14466(%rip), %xmm2    # 0x194a8
cmpltsd %xmm2, %xmm4
movapd %xmm4, %xmm1
andnpd %xmm0, %xmm1
movsd 0x14439(%rip), %xmm0    # 0x19490
andpd %xmm0, %xmm4
orpd %xmm1, %xmm4
maxsd 0x58(%rsp), %xmm4
movapd %xmm4, %xmm0
subsd %xmm5, %xmm0
andpd 0x1450b(%rip), %xmm0    # 0x19580
ucomisd %xmm0, %xmm2
ja 0x518d
movl %r8d, 0x38(%rsp)
testl %r13d, %r13d
je 0x5165
xorl %r12d, %r12d
movq 0x30(%rsp), %r13
movapd %xmm4, 0x10(%rsp)
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r12), %eax
btl $0x1e, %eax
jb 0x5154
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm4, %xmm0
callq 0x3100
movq %r14, %rcx
cmpq %r14, %rax
jl 0x50f0
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r12), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl %r15d, %eax
cmpq 0x40(%rsp), %rcx
jge 0x514a
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r12), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpq %r14, %rcx
jl 0x514a
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r12), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movl %eax, (%r13)
movapd 0x10(%rsp), %xmm4
addq $0x4, %r13
addq $0x28, %r12
cmpq %r12, %rbp
jne 0x509b
movl 0x38(%rsp), %r8d
incl %r8d
movapd %xmm4, %xmm5
cmpl $0x3e9, %r8d            # imm = 0x3E9
movq 0x48(%rsp), %r13
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdi
jne 0x4fb8
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x519c
movq %rdi, (%rax)
jmp 0x51ad
movsd %xmm5, 0x10(%rsp)
callq 0x3060
movsd 0x10(%rsp), %xmm5
movapd %xmm5, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq