// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, 0x30(%rsp)
movl %edx, 0x14(%rsp)
movl %esi, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rdi, %r15
movl 0x8(%rdi), %eax
movq %rax, 0x20(%rsp)
movq 0x20(%rdi), %rbx
callq 0x4938
movsd %xmm0, 0x28(%rsp)
movq 0x20(%r15), %rsi
xorl %r12d, %r12d
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r13d, %r13d
movq %rsi, (%rsp)
movl %r12d, %r12d
cmpq %r12, (%rsi)
jbe 0x4538
movq 0x10(%rsi), %rcx
imulq $0x28, %r12, %rbp
testb $0x40, 0x23(%rcx,%rbp)
jne 0x4533
cmpq %rax, %r13
jne 0x451d
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r15, %rdi
callq 0x3330
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
movq (%rsp), %rsi
movq %rax, %r15
movq 0x10(%rsi), %rcx
movq %r14, %rax
movl 0x20(%rcx,%rbp), %edx
andl %edi, %edx
shlq $0x20, %rdx
orq 0x18(%rcx,%rbp), %rdx
movq %rdx, (%r15,%r13,8)
incq %r13
incl %r12d
jmp 0x44d0
testq %r13, %r13
je 0x4667
leaq 0xa814(%rip), %rcx      # 0xed5c
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x30c0
movsd 0x11f3a(%rip), %xmm0    # 0x16498
xorl %eax, %eax
movsd 0x11f38(%rip), %xmm1    # 0x164a0
xorpd %xmm2, %xmm2
movsd 0x11f34(%rip), %xmm3    # 0x164a8
movapd 0x12004(%rip), %xmm4    # 0x16580
xorl %ecx, %ecx
movsd 0x18(%rsp), %xmm11
movl %ecx, %edx
cmpq %rdx, %r13
jbe 0x4637
movq (%r15,%rdx,8), %rdx
shrq $0x20, %rdx
je 0x4630
cvtsi2sd %rdx, %xmm7
xorpd %xmm6, %xmm6
xorl %edx, %edx
xorpd %xmm5, %xmm5
xorpd %xmm8, %xmm8
movl %edx, %edx
cmpq %rdx, %r13
jbe 0x45fd
movq (%r15,%rdx,8), %rsi
movl %esi, %edi
shrq $0x20, %rsi
cvtsi2sd %rsi, %xmm9
movapd %xmm9, %xmm10
divsd %xmm7, %xmm10
ucomisd %xmm11, %xmm10
cvtsi2sd %rdi, %xmm10
jae 0x45e5
addsd %xmm10, %xmm8
jmp 0x45f9
addsd %xmm10, %xmm6
mulsd %xmm10, %xmm9
divsd %xmm7, %xmm9
addsd %xmm9, %xmm5
incl %edx
jmp 0x45b2
movapd %xmm6, %xmm7
addsd %xmm8, %xmm7
divsd %xmm7, %xmm8
ucomisd %xmm1, %xmm8
ja 0x4637
ucomisd %xmm2, %xmm6
jbe 0x4630
divsd %xmm6, %xmm5
addsd %xmm3, %xmm5
andpd %xmm4, %xmm5
ucomisd %xmm5, %xmm0
jbe 0x4630
movl %ecx, %eax
movapd %xmm5, %xmm0
incl %ecx
jmp 0x4585
xorpd %xmm1, %xmm1
ucomisd 0x11e55(%rip), %xmm0    # 0x16498
jne 0x4647
jnp 0x4653
movl %eax, %eax
movl 0x4(%r15,%rax,8), %eax
cvtsi2sd %rax, %xmm1
movsd %xmm1, (%rsp)
movq %r15, %rdi
callq 0x3060
movsd (%rsp), %xmm0
jmp 0x466b
xorpd %xmm0, %xmm0
maxsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rsp)
pushq $0x4
popq %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x3240
movsd (%rsp), %xmm4
movq %rax, %rbp
movslq 0x10(%rsp), %r13
movslq 0x14(%rsp), %r12
imulq $0xa, %r14, %r15
xorl %r14d, %r14d
movq %rax, 0x18(%rsp)
cmpq %r14, %r15
je 0x4761
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
btl $0x1e, %eax
jb 0x4754
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cvtsi2sd %eax, %xmm0
divsd %xmm4, %xmm0
callq 0x3100
movq %r13, %rcx
cmpq %r13, %rax
jl 0x46fb
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl 0x14(%rsp), %eax
cmpq %r12, %rcx
jge 0x474c
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl 0x10(%rsp), %eax
cmpq %r13, %rcx
jl 0x474c
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movl %eax, (%rbp)
movsd (%rsp), %xmm4
addq $0xa, %r14
addq $0x4, %rbp
jmp 0x46a4
xorl %r8d, %r8d
cmpl $0x3e8, %r8d            # imm = 0x3E8
movq 0x18(%rsp), %rdi
je 0x4907
incl %r8d
xorpd %xmm0, %xmm0
movq %rdi, %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x47de
movq 0x10(%rbx), %rsi
movl 0x20(%rsi,%rcx,4), %edx
btl $0x1e, %edx
jb 0x47d4
movsd 0x18(%rsi,%rcx,4), %xmm1
cvtsi2sdl (%rax), %xmm2
unpcklps 0x11dea(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
cvtsi2sd %edx, %xmm3
subpd 0x11de8(%rip), %xmm1    # 0x165a0
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
addq $0xa, %rcx
addq $0x4, %rax
jmp 0x4782
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
divsd %xmm3, %xmm0
movsd 0x11cbe(%rip), %xmm2    # 0x164b0
cmpltsd %xmm2, %xmm3
movapd %xmm3, %xmm1
andnpd %xmm0, %xmm1
movsd 0x11c91(%rip), %xmm0    # 0x16498
andpd %xmm0, %xmm3
orpd %xmm1, %xmm3
maxsd 0x28(%rsp), %xmm3
movapd %xmm3, (%rsp)
movapd %xmm3, %xmm0
subsd %xmm4, %xmm0
andpd 0x11d56(%rip), %xmm0    # 0x16580
ucomisd %xmm0, %xmm2
ja 0x4907
movq %r8, 0x20(%rsp)
movq %rdi, %rbp
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x48f8
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
btl $0x1e, %eax
jb 0x48eb
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movq %r13, %rcx
cmpq %r13, %rax
jl 0x4897
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl 0x14(%rsp), %eax
cmpq %r12, %rcx
jge 0x48e8
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movq %rax, %rcx
movl 0x10(%rsp), %eax
cmpq %r13, %rcx
jl 0x48e8
movq 0x10(%rbx), %rax
movl 0x20(%rax,%r14,4), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0x3100
movl %eax, (%rbp)
addq $0xa, %r14
addq $0x4, %rbp
jmp 0x483f
movapd (%rsp), %xmm4
movq 0x20(%rsp), %r8
jmp 0x4764
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x4916
movq %rdi, (%rax)
jmp 0x4925
movsd %xmm4, (%rsp)
callq 0x3060
movsd (%rsp), %xmm4
movapd %xmm4, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq