// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rcx, 0x48(%rsp)
movl %edx, 0xc(%rsp)
movl %esi, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %rdi, %rbx
movl 0x8(%rdi), %r15d
movq 0x20(%rdi), %r12
movq %r12, %rdi
callq 0x52a4
movsd %xmm0, 0x38(%rsp)
movq 0x20(%rbx), %rsi
movq (%rsi), %rcx
xorpd %xmm0, %xmm0
testq %rcx, %rcx
je 0x4f54
movq 0x10(%rsi), %rax
movl $0x1, %ebp
xorl %edx, %edx
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %r13d, %r13d
movq %rsi, 0x10(%rsp)
movq %r15, 0x50(%rsp)
leaq (%rdx,%rdx,4), %r14
testb $0x40, 0x23(%rax,%r14,8)
jne 0x4e12
cmpq %r8, %rbx
jne 0x4df7
leaq (%rbx,%rbx), %r15
testq %rbx, %rbx
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq %r13, %rdi
callq 0x3330
movq %r15, %r8
movq 0x50(%rsp), %r15
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
movq 0x10(%rsp), %rsi
xorpd %xmm0, %xmm0
movq %rax, %r13
movq 0x10(%rsi), %rax
movl 0x20(%rax,%r14,8), %ecx
andl %edi, %ecx
shlq $0x20, %rcx
orq 0x18(%rax,%r14,8), %rcx
movq %rcx, (%r13,%rbx,8)
incq %rbx
movq (%rsi), %rcx
movl %ebp, %edx
incl %ebp
cmpq %rdx, %rcx
ja 0x4da9
testq %rbx, %rbx
je 0x4f54
leaq 0xc311(%rip), %rcx      # 0x1113c
movl $0x8, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x30c0
movsd 0x1564d(%rip), %xmm0    # 0x1a490
xorl %ecx, %ecx
movsd 0x1564b(%rip), %xmm1    # 0x1a498
xorpd %xmm2, %xmm2
movsd 0x15647(%rip), %xmm3    # 0x1a4a0
movapd 0x1571f(%rip), %xmm4    # 0x1a580
xorl %eax, %eax
movsd 0x28(%rsp), %xmm11
movq (%r13,%rcx,8), %rdx
shrq $0x20, %rdx
je 0x4f16
xorps %xmm8, %xmm8
cvtsi2sd %rdx, %xmm8
xorpd %xmm7, %xmm7
xorl %edx, %edx
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
movq (%r13,%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 0x4ec5
addsd %xmm10, %xmm7
jmp 0x4ed9
addsd %xmm10, %xmm6
mulsd %xmm10, %xmm9
divsd %xmm8, %xmm9
addsd %xmm9, %xmm5
incq %rdx
movl %edx, %esi
cmpq %rsi, %rbx
ja 0x4e90
movapd %xmm6, %xmm8
addsd %xmm7, %xmm8
divsd %xmm8, %xmm7
ucomisd %xmm1, %xmm7
ja 0x4f21
ucomisd %xmm2, %xmm6
jbe 0x4f16
divsd %xmm6, %xmm5
addsd %xmm3, %xmm5
andpd %xmm4, %xmm5
ucomisd %xmm5, %xmm0
jbe 0x4f16
movl %ecx, %eax
movapd %xmm5, %xmm0
incl %ecx
cmpq %rcx, %rbx
ja 0x4e6a
xorpd %xmm1, %xmm1
ucomisd 0x15563(%rip), %xmm0    # 0x1a490
jne 0x4f31
jnp 0x4f40
movl %eax, %eax
movl 0x4(%r13,%rax,8), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movapd %xmm1, 0x10(%rsp)
movq %r13, %rdi
callq 0x3060
movapd 0x10(%rsp), %xmm0
maxsd 0x38(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movl $0x4, %esi
movq %r15, %rdi
callq 0x3240
movq %rax, %r14
testq %r15, %r15
je 0x5076
movslq 0x8(%rsp), %rbx
movslq 0xc(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq (,%r15,8), %rax
leaq (%rax,%rax,4), %rcx
xorl %ebp, %ebp
movq %r14, %r13
movapd 0x10(%rsp), %xmm2
movq %rcx, 0x30(%rsp)
movq 0x10(%r12), %rax
movl 0x20(%rax,%rbp), %eax
btl $0x1e, %eax
jb 0x5063
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm2, %xmm0
callq 0x3100
movq %rbx, %rcx
cmpq %rbx, %rax
jl 0x4ff9
movq 0x10(%r12), %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 0x28(%rsp), %rcx
jge 0x5054
movq 0x10(%r12), %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 0x8(%rsp), %eax
cmpq %rbx, %rcx
jl 0x5054
movq 0x10(%r12), %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, (%r13)
movapd 0x10(%rsp), %xmm2
movq 0x30(%rsp), %rcx
addq $0x4, %r13
addq $0x28, %rbp
cmpq %rbp, %rcx
jne 0x4fa4
jmp 0x508b
movslq 0x8(%rsp), %rbx
movslq 0xc(%rsp), %rax
movq %rax, 0x28(%rsp)
movapd 0x10(%rsp), %xmm2
leaq (,%r15,8), %rax
leaq (%rax,%rax,4), %rbp
movl $0x1, %esi
movq %r14, 0x40(%rsp)
movapd %xmm2, %xmm4
testq %r15, %r15
je 0x5115
movq 0x10(%r12), %rax
addq $0x20, %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movl (%rax), %edx
btl $0x1e, %edx
jb 0x5107
movsd -0x8(%rax), %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl (%r14,%rcx,4), %xmm2
unpcklps 0x154ba(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
subpd 0x154b5(%rip), %xmm1    # 0x1a5a0
unpcklpd %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0]
movapd %xmm1, %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addpd %xmm2, %xmm1
mulpd %xmm3, %xmm1
addpd %xmm1, %xmm0
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r15
jne 0x50b9
jmp 0x5119
xorpd %xmm0, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
divsd %xmm2, %xmm0
movsd 0x1537b(%rip), %xmm3    # 0x1a4a8
cmpltsd %xmm3, %xmm2
movapd %xmm2, %xmm1
andnpd %xmm0, %xmm1
movsd 0x1534e(%rip), %xmm0    # 0x1a490
andpd %xmm0, %xmm2
orpd %xmm1, %xmm2
maxsd 0x38(%rsp), %xmm2
movapd %xmm2, %xmm0
subsd %xmm4, %xmm0
andpd 0x15420(%rip), %xmm0    # 0x1a580
ucomisd %xmm0, %xmm3
ja 0x526e
movl %esi, 0x30(%rsp)
testq %r15, %r15
je 0x5253
xorl %r13d, %r13d
movq 0x40(%rsp), %r14
movapd %xmm2, 0x10(%rsp)
movq 0x10(%r12), %rax
movl 0x20(%rax,%r13), %eax
btl $0x1e, %eax
jb 0x5242
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm2, %xmm0
callq 0x3100
movq %rbx, %rcx
cmpq %rbx, %rax
jl 0x51dc
movq 0x10(%r12), %rax
movl 0x20(%rax,%r13), %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 0x28(%rsp), %rcx
jge 0x5239
movq 0x10(%r12), %rax
movl 0x20(%rax,%r13), %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 0x8(%rsp), %eax
cmpq %rbx, %rcx
jl 0x5239
movq 0x10(%r12), %rax
movl 0x20(%rax,%r13), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x3100
movl %eax, (%r14)
movapd 0x10(%rsp), %xmm2
addq $0x4, %r14
addq $0x28, %r13
cmpq %r13, %rbp
jne 0x5185
movl 0x30(%rsp), %esi
incl %esi
movapd %xmm2, %xmm4
cmpl $0x3e9, %esi            # imm = 0x3E9
movq 0x40(%rsp), %r14
jne 0x50a1
movq 0x48(%rsp), %rax
testq %rax, %rax
je 0x527d
movq %r14, (%rax)
jmp 0x5291
movq %r14, %rdi
movsd %xmm4, 0x10(%rsp)
callq 0x3060
movsd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq