// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/syncmer.c
// Function: syncmer_link_coverage_analysis
int syncmer_link_coverage_analysis(sr_db_t *sr_db, syncmer_db_t *scm_db, uint32_t min_k_cov, uint32_t min_n_seq, 
        uint32_t min_pt, double min_f, double **_beta, double **_bse, double **_r2, int verbose)
{
    uint64_t i, j;
    syncmer_t *scm;
    
    min_pt = MAX(min_pt, 30);
    min_f  = MAX(min_f,  .0);

    scm = scm_db->a;

    // collect read length - syncmer count
    kh_ctab_t *rl_ctab;
    int64_t *rl_cnts, *rd_cnts;
    uint32_t max_n;
    rl_ctab = kh_ctab_init();
    max_n = 0;
    for (i = 0; i < sr_db->n; ++i) {
        max_n = MAX(max_n, sr_db->a[i].n);
        kh_ctab_put1(rl_ctab, sr_db->a[i].n);
    }
    if (max_n == 0) return 0;

    rl_cnts = kh_ctab_cnt(rl_ctab, max_n);
    kh_ctab_destroy(rl_ctab);
    
    // rl_cnts cumsum from end to beg
    // rl_cnts size is max_n+1
    for (i = 0; i < max_n; ++i)
        rl_cnts[max_n-i-1] += rl_cnts[max_n-i];

#ifdef DEBUG_LINK_COVERAGE
    for (i = 0; i <= max_n; ++i)
        fprintf(stderr, "[DEBUG_LINK_COVERAGE::%s] rl_cnts: %lu %ld\n", __func__, i, rl_cnts[i]);
#endif

    kh_scm_t *a_cov; // arc cov
    kh_kcn_t *k_cn; // scm copy number
    sr_t *s;
    uint32_t a, c, v_v, n1, *pt_n;
    uint64_t v0, v1, *scm_id;
    uint128_t v_p;
    double *beta, *bse, *r2, c0, c1;
    kvec_t(pt1_t) pt1;
    khint_t k;

    a_cov = kh_scm_init();
    k_cn = kh_kcn_init();
    kv_init(pt1);
    MYMALLOC(scm_id, max_n);
    MYCALLOC(rd_cnts, max_n+1);
    MYCALLOC(beta, max_n);
    MYCALLOC(bse, max_n);
    MYCALLOC(r2, max_n);
    MYCALLOC(pt_n, max_n);
    n1 = 0;
    for (i = 2; i < max_n; ++i) {
        if (rl_cnts[i] < min_n_seq) break;
        // do coverage analysis
        // the gap will be i-2
        kh_scm_m_clear(a_cov);
        for (j = 0; j < sr_db->n; ++j) {
            s = &sr_db->a[j];
            if (s->n < i) continue;
            // collect scm_id along reads
            for (a = 0; a < s->n; ++a)
                scm_id[a] = s->k_mer[a] >> 1;
            // collect coverage
            for (a = i-1; a < s->n; ++a) {
                if (scm[scm_id[a+1-i]].cov < min_k_cov || 
                        scm[scm_id[a]].cov < min_k_cov)
                    continue;
                v0 = scm_id[a+1-i] << 1 | (s->m_pos[a+1-i] & 1);
                v1 = scm_id[a] << 1 | (s->m_pos[a] & 1);
                v0 <= v1? kh_scm_add(a_cov, (uint128_t) v0 << 64 | v1, 1) :
                    kh_scm_add(a_cov, ((uint128_t) v1^1) << 64 | (v0^1), 1);
                // rd_cnts collects the pair number of a given gap in the data
                ++rd_cnts[i];
            }
        }

        if (i == 2) {
            // kmer copy number estimation
            for (k = (khint_t) 0; k < kh_end(a_cov); ++k) {
                if (!kh_exist(a_cov, k)) continue;
                v_p = kh_key(a_cov, k);
                kh_kcn_put1(k_cn, (uint64_t) (v_p >> 65));
                kh_kcn_put1(k_cn, ((uint64_t) v_p) >> 1);
            }

#ifdef DEBUG_LINK_COVERAGE
            for (k = (khint_t) 0; k < kh_end(k_cn); ++k) {
                if (!kh_exist(k_cn, k)) continue;
                fprintf(stderr, "[DEBUG_LINK_COVERAGE::%s] k_cn: %lu %d\n", __func__, i-2, kh_val(k_cn, k));
            }
#endif
        }

#ifdef DEBUG_LINK_COVERAGE
        if ((i-2) % 3 == 0) {
            // print arc cov stats
            for (k = (khint_t) 0; k < kh_end(a_cov); ++k) {
                if (!kh_exist(a_cov, k)) continue;
                v_p = kh_key(a_cov, k);
                v_v = kh_val(a_cov, k);
                v0 = (uint64_t) (v_p >> 65);
                v1 = ((uint64_t) v_p) >> 1;
                c0 = MAX(2, kh_kcn_get1(k_cn, v0)) / 2.0;
                c1 = MAX(2, kh_kcn_get1(k_cn, v1)) / 2.0;
                c = (uint32_t) (MIN(scm[v0].cov/c0, scm[v1].cov/c1));
                v_v = MIN(v_v, c);
                fprintf(stderr, "[DEBUG_LINK_COVERAGE::%s] lc_cnts: %lu %u %u\n", __func__, i-2, c, v_v);
            }
        }
#endif

        // collect covs
        pt1.n = 0;
        for (k = (khint_t) 0; k < kh_end(a_cov); ++k) {
            if (!kh_exist(a_cov, k)) continue;
            v_p = kh_key(a_cov, k);
            v_v = kh_val(a_cov, k);
            v0 = (uint64_t) (v_p >> 65);
            v1 = ((uint64_t) v_p) >> 1;
            c0 = MAX(2, kh_kcn_get1(k_cn, v0)) / 2.0;
            c1 = MAX(2, kh_kcn_get1(k_cn, v1)) / 2.0;
            c = (uint32_t) (MIN(scm[v0].cov/c0, scm[v1].cov/c1));
            v_v = MIN(v_v, c);
            pt1_t pt = {c, v_v, (double) v_v / c};
            kv_push(pt1_t, pt1, pt);
        }
        
        uint32_t beg, end;
        /***
        beg = floor(pt1.n * .01);
        end =  ceil(pt1.n * .99);
        // sort pt by c
        qsort(pt1.a, pt1.n, sizeof(pt1_t), pt1_c_cmpfunc);
        // mask the lower 1% and upper 1% of data
        for (j = 0; j < beg; ++j) pt1.a[j].f = -1.0;
        for (j = end; j < pt1.n; ++j) pt1.a[j].f = -1.0;
        // sort pt by l
        qsort(pt1.a, pt1.n, sizeof(pt1_t), pt1_l_cmpfunc);
        // mask the lower 1% and upper 1% of data
        for (j = 0; j < beg; ++j) pt1.a[j].f = -1.0;
        for (j = end; j < pt1.n; ++j) pt1.a[j].f = -1.0;
        **/
        beg = floor(pt1.n * .05);
        end =  ceil(pt1.n * .95);
        // sort pt by l/c
        qsort(pt1.a, pt1.n, sizeof(pt1_t), pt1_f_cmpfunc);
        // estimate slope using the middle 90% data
        while (beg < end && pt1.a[beg].f < min_f)
            ++beg;
        
        if (end - beg < min_pt)
            break;

        if (verbose > 1) {
            for (j = beg; j < end; ++j)
                fprintf(stderr, "[M::%s] pt1: %lu %lu %u %u %.6f\n", __func__, i-2, j, pt1.a[j].c, pt1.a[j].l, pt1.a[j].f);
        }

        // do estimation
        double xy, x2, ybar;
        xy = x2 = ybar = .0;
        for (j = beg; j < end; ++j) {
            xy += (double) pt1.a[j].c * pt1.a[j].l;
            x2 += (double) pt1.a[j].c * pt1.a[j].c;
            ybar += pt1.a[j].l;
        }
        beta[i] = xy / x2;
        ybar /= end - beg;
        // calculate r2
        double res, tot, r;
        res = tot = .0;
        for (j = beg; j < end; ++j) {
            r = pt1.a[j].l - beta[i] * pt1.a[j].c;
            res += r * r;
            r = pt1.a[j].l - ybar;
            tot += r * r;
        }
        bse[i] = sqrt(res / x2 / (end - beg - 1));
        r2[i] = 1 - (tot == .0? .0 : res / tot);
        pt_n[i] = end - beg;
        n1 = i;
    }

#ifdef DEBUG_LINK_COVERAGE
    for (i = 0; i <= max_n; ++i)
        fprintf(stderr, "[DEBUG_LINK_COVERAGE::%s] rd_cnts: %lu %ld\n", __func__, i, rd_cnts[i]);
    for (i = 2; i < max_n; ++i)
        fprintf(stderr, "[DEBUG_LINK_COVERAGE::%s] rd_regs: %lu %u %.6f %.6f %.6f\n", __func__, 
                i-2, pt_n[i], beta[i], bse[i], r2[i]);
#endif

    if (verbose > 0) {
        for (i = 2; i < n1; ++i)
            fprintf(stderr, "[M::%s] G: %lu N: %u D: %ld coeff: %.6f bse: %.6f R2: %.6f\n", __func__, 
                    i-2, pt_n[i], rd_cnts[i], beta[i], bse[i], r2[i]);
    }

    if (n1 > 0) {
        // copy results
        if (_beta) {
            MYMALLOC(*_beta, n1 - 1);
            memcpy(*_beta, &beta[2], n1 - 1);
        }
        if (_bse) {
            MYMALLOC(*_bse, n1 - 1);
            memcpy(*_bse, &bse[2], n1 - 1);
        }
        if (_r2) {
            MYMALLOC(*_r2, n1 - 1);
            memcpy(*_r2, &r2[2], n1 - 1);
        }
    }

    kh_scm_destroy(a_cov);
    kh_kcn_destroy(k_cn);
    kv_destroy(pt1);
    free(scm_id);
    free(rl_cnts);
    free(rd_cnts);
    free(beta);
    free(bse);
    free(r2);
    free(pt_n);

    return n1 > 0? (n1 - 1) : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, 0xd0(%rsp)
movsd %xmm0, 0x68(%rsp)
movl %r8d, %ebx
movl %ecx, 0x8(%rsp)
movl %edx, 0x14(%rsp)
movq %rdi, %r15
movq 0x10(%rsi), %rax
movq %rax, 0x78(%rsp)
xorl %r13d, %r13d
pushq $0x38
popq %rbp
callq 0xaf12
movq %rax, %r14
xorl %r12d, %r12d
cmpq (%r15), %r12
jae 0xc900
movq 0x10(%r15), %rax
movl (%rax,%rbp), %esi
cmpl %esi, %r13d
cmovbel %esi, %r13d
movq %r14, %rdi
callq 0xaf27
incq %r12
addq $0x58, %rbp
jmp 0xc8dc
cmpl $0x1f, %ebx
pushq $0x1e
popq %rax
cmovael %ebx, %eax
movl %eax, 0x94(%rsp)
testl %r13d, %r13d
je 0xd2d3
movq %r15, 0x88(%rsp)
xorpd %xmm0, %xmm0
movsd 0x68(%rsp), %xmm1
maxsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0xb349
movq %rax, %r12
movq %r14, %rdi
callq 0xb929
movl %r13d, %eax
movq %rax, 0xa0(%rsp)
decq %rax
cmpq $-0x1, %rax
je 0xc96d
movq 0x8(%r12,%rax,8), %rcx
addq %rcx, (%r12,%rax,8)
decq %rax
jmp 0xc959
movq $0x0, 0x98(%rsp)
pushq $0x1
popq %r14
pushq $0x20
popq %rbx
xorl %edi, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x17820
movq %rax, %r15
andq $0x0, 0x18(%rax)
xorl %edi, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x17820
movq %rax, 0x80(%rsp)
andq $0x0, 0x18(%rax)
movq 0xa0(%rsp), %r14
leaq (,%r14,8), %rdi
callq 0x2310
movq %rax, %rbp
incl %r13d
pushq $0x8
popq %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2250
movq %rax, 0x70(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2250
movq %rax, 0x30(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2250
movq %rax, 0x28(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2250
movq %rax, 0x20(%rsp)
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x2250
movq %rax, 0x60(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0xd8(%rsp)
movq %rbp, %r8
addq $-0x8, %r8
pushq $0x2
popq %r10
pushq $-0x4
popq %rcx
movq %rcx, 0xa8(%rsp)
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
pushq $0x1
popq %rcx
movq %rcx, 0xb0(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0xc8(%rsp)
movq %r12, 0x58(%rsp)
movq %rbp, 0xc0(%rsp)
movq %r15, 0xb8(%rsp)
movl 0x14(%rsp), %r13d
cmpq 0xa0(%rsp), %r10
movq 0x88(%rsp), %rax
jae 0xd0e2
movq 0xd8(%rsp), %rcx
cmpq %rcx, (%r12,%r10,8)
jl 0xd0e2
testq %r15, %r15
movq %r8, 0x38(%rsp)
movq %r10, 0x18(%rsp)
je 0xcafc
movq 0x8(%r15), %rdi
testq %rdi, %rdi
je 0xcafc
movl (%r15), %ecx
pushq $0x1
popq %rax
movl %eax, %edx
shll %cl, %edx
shrl $0x3, %edx
andl $-0x4, %edx
cmpl $0x5, %ecx
pushq $0x4
popq %rax
cmovbl %eax, %edx
xorl %esi, %esi
callq 0x21e0
movq 0x18(%rsp), %r10
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq 0x38(%rsp), %r8
movq 0x88(%rsp), %rax
andl $0x0, 0x4(%r15)
xorl %ebx, %ebx
movq 0x78(%rsp), %rdi
cmpq (%rax), %rbx
jae 0xcbff
movq 0x10(%rax), %r14
imulq $0x58, %rbx, %rcx
movl 0x38(%r14,%rcx), %eax
cmpq %rax, %r10
jbe 0xcb2b
incq %rbx
movq 0x88(%rsp), %rax
jmp 0xcb03
addq %rcx, %r14
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xcb4a
movq 0x50(%r14), %rdx
movq (%rdx,%rcx,8), %rdx
shrq %rdx
movq %rdx, (%rbp,%rcx,8)
incq %rcx
jmp 0xcb30
movq 0xb0(%rsp), %r12
movl %eax, %eax
cmpq %rax, %r12
jae 0xcb1e
movq (%r8,%r12,8), %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
movl 0x10(%rdi,%rdx), %edx
andl %r9d, %edx
cmpl %r13d, %edx
jb 0xcb88
movq (%rbp,%r12,8), %rsi
movq %rsi, %rdx
shlq $0x5, %rdx
movl 0x10(%rdi,%rdx), %edx
andl %r9d, %edx
cmpl %r13d, %edx
jae 0xcb8d
incq %r12
jmp 0xcb54
movq 0x40(%r14), %rax
leaq (%rax,%r12,4), %rdx
movq 0xa8(%rsp), %rdi
movl (%rdi,%rdx), %edx
andl $0x1, %edx
leaq (%rdx,%rcx,2), %rdx
movl (%rax,%r12,4), %eax
andl $0x1, %eax
leaq (%rax,%rsi,2), %rax
cmpq %rax, %rdx
jbe 0xcbca
xorq $0x1, %rax
xorq $0x1, %rdx
movq %r15, %rdi
movq %rdx, %rsi
movq %rax, %rdx
jmp 0xcbd0
movq %r15, %rdi
movq %rax, %rsi
callq 0xd2e7
incq %r12
movq 0x70(%rsp), %rax
movq 0x18(%rsp), %r10
incq (%rax,%r10,8)
movl 0x38(%r14), %eax
movq 0x78(%rsp), %rdi
movq 0x38(%rsp), %r8
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0xcb52
cmpq $0x2, %r10
jne 0xcc6d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%r15), %rax
testq %rax, %rax
je 0xcc1f
movb (%r15), %cl
pushq $0x1
popq %rdx
shlq %cl, %rdx
jmp 0xcc21
xorl %edx, %edx
cmpq %rdx, %r12
jae 0xcc6d
movq 0x8(%r15), %rcx
movq %r12, %rdx
shrq $0x5, %rdx
movl (%rcx,%rdx,4), %ecx
btl %r12d, %ecx
jae 0xcc64
movq (%rax,%r14), %rbx
movq 0x8(%rax,%r14), %rsi
shrq %rsi
movq 0x80(%rsp), %r13
movq %r13, %rdi
callq 0xd6a7
shrq %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xd6a7
incq %r12
addq $0x18, %r14
jmp 0xcc0b
pushq $0x10
popq %rsi
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x10(%r15), %rax
testq %rax, %rax
je 0xcc8a
movb (%r15), %cl
pushq $0x1
popq %rdx
shlq %cl, %rdx
jmp 0xcc8c
xorl %edx, %edx
cmpq %rdx, %r12
jae 0xce0c
movq 0x8(%r15), %rcx
movq %r12, %rdx
shrq $0x5, %rdx
movl (%rcx,%rdx,4), %ecx
btl %r12d, %ecx
jae 0xce00
movq %r13, 0x40(%rsp)
movq -0x10(%rax,%rsi), %r13
movq -0x8(%rax,%rsi), %rbx
movq %rsi, 0xe0(%rsp)
movl (%rax,%rsi), %ebp
shrq %rbx
shrq %r13
movq 0x80(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0xd9cf
movl %eax, %r14d
movq %r15, %rdi
movq %r13, %rsi
callq 0xd9cf
movq 0x78(%rsp), %rcx
shlq $0x5, %rbx
shlq $0x5, %r13
movd %r14d, %xmm0
movd %eax, %xmm1
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
pshufd $0x50, %xmm0, %xmm1     # xmm1 = xmm0[0,0,1,1]
pcmpgtd 0xd22b(%rip), %xmm1     # 0x19f40
punpckldq 0xcb13(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movdqa 0xd20b(%rip), %xmm2     # 0x19f30
por %xmm2, %xmm0
subpd %xmm2, %xmm0
mulpd 0xd21b(%rip), %xmm0     # 0x19f50
andpd %xmm1, %xmm0
andnpd 0xd21f(%rip), %xmm1     # 0x19f60
orpd %xmm0, %xmm1
movss 0x10(%rcx,%r13), %xmm0
movq 0x40(%rsp), %r15
movss 0x10(%rcx,%rbx), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
andpd 0xd20e(%rip), %xmm2     # 0x19f70
cvtdq2pd %xmm2, %xmm0
divpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
cmpl %ebx, %ebp
cmovael %ebx, %ebp
movq 0xc8(%rsp), %rax
cmpq %rax, %r15
jne 0xcdb6
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
movq %r14, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
callq 0x2350
movq %r14, 0xc8(%rsp)
jmp 0xcdbb
movq 0x8(%rsp), %rax
cvtsi2sd %rbp, %xmm0
movl %ebx, %ecx
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
leaq 0x1(%r15), %r13
shlq $0x4, %r15
movl %ebx, (%rax,%r15)
movl %ebp, 0x4(%rax,%r15)
movq %rax, 0x8(%rsp)
movsd %xmm0, 0x8(%rax,%r15)
movq 0xc0(%rsp), %rbp
movq 0xb8(%rsp), %r15
movq 0xe0(%rsp), %rsi
incq %r12
addq $0x18, %rsi
jmp 0xcc76
movq %r13, %xmm0
punpckldq 0xc9f7(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xc9ff(%rip), %xmm0     # 0x19820
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movapd %xmm1, %xmm0
mulsd 0xd151(%rip), %xmm0     # 0x19f90
callq 0x2400
cvttsd2si %xmm0, %r14
movapd 0x40(%rsp), %xmm0
mulsd 0xd141(%rip), %xmm0     # 0x19f98
callq 0x24e0
cvttsd2si %xmm0, %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
pushq $0x10
popq %rdx
leaq 0xbef(%rip), %rcx       # 0xda65
callq 0x20d0
movq 0x18(%rsp), %rsi
movl %ebx, %r13d
cmpl %ebx, %r14d
cmoval %r14d, %ebx
movl %r14d, %eax
movq %rax, %rcx
shlq $0x4, %rcx
addq %r12, %rcx
addq $0x8, %rcx
movsd 0x68(%rsp), %xmm0
cmpq %r13, %rax
jae 0xceb7
ucomisd (%rcx), %xmm0
jbe 0xceb5
incq %rax
addq $0x10, %rcx
jmp 0xcea1
movl %eax, %ebx
movl %r13d, %r14d
subl %ebx, %r14d
cmpl 0x94(%rsp), %r14d
jb 0xd0e2
movl %ebx, %r12d
cmpl $0x1, 0x130(%rsp)
jle 0xcf41
leaq -0x2(%rsi), %rax
movq %rax, 0x40(%rsp)
movq %r12, %rax
shlq $0x4, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax), %rbp
addq $0x8, %rbp
movq %r12, %rbx
cmpq %r13, %rbx
jae 0xcf41
movq 0x140ec(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
movl -0x8(%rbp), %r9d
movl -0x4(%rbp), %eax
movsd (%rbp), %xmm0
movl %eax, (%rsp)
leaq 0xd3cd(%rip), %rsi      # 0x1a2ea
leaq 0xd3e7(%rip), %rdx      # 0x1a30b
movq 0x40(%rsp), %rcx
movq %rbx, %r8
movb $0x1, %al
callq 0x2280
movq 0x18(%rsp), %rsi
incq %rbx
addq $0x10, %rbp
jmp 0xcef8
movq %r12, %rax
shlq $0x4, %rax
movq 0x8(%rsp), %rcx
addq %rax, %rcx
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movq %r12, %rdx
cmpq %r13, %rdx
jae 0xcf9c
movsd (%rcx), %xmm2
unpcklps 0xc8c5(%rip), %xmm2     # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
movapd 0xcfbd(%rip), %xmm3     # 0x19f30
orpd %xmm3, %xmm2
subpd %xmm3, %xmm2
movapd %xmm2, %xmm3
unpcklpd %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0]
mulpd %xmm2, %xmm3
addpd %xmm3, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm2, %xmm0
incq %rdx
addq $0x10, %rcx
jmp 0xcf5b
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
divsd %xmm1, %xmm2
movq 0x30(%rsp), %rcx
movsd %xmm2, (%rcx,%rsi,8)
movl %r14d, %ecx
cvtsi2sd %rcx, %xmm3
divsd %xmm3, %xmm0
xorpd 0xcf5a(%rip), %xmm2     # 0x19f20
movq 0x8(%rsp), %rcx
addq %rcx, %rax
addq $0x4, %rax
xorpd %xmm5, %xmm5
movq 0xc0(%rsp), %rbp
cmpq %r13, %r12
jae 0xd013
movl -0x4(%rax), %ecx
movl (%rax), %edx
cvtsi2sd %rdx, %xmm3
cvtsi2sd %rcx, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
subsd %xmm0, %xmm3
unpcklpd %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0]
mulpd %xmm4, %xmm4
addpd %xmm4, %xmm5
incq %r12
addq $0x10, %rax
jmp 0xcfde
movapd %xmm5, %xmm0
leal -0x1(%r14), %eax
cvtsi2sd %rax, %xmm2
divsd %xmm1, %xmm0
divsd %xmm2, %xmm0
ucomisd 0xcf78(%rip), %xmm0     # 0x19fa8
jb 0xd038
sqrtsd %xmm0, %xmm0
jmp 0xd04e
movapd %xmm5, 0x40(%rsp)
callq 0x2480
movapd 0x40(%rsp), %xmm5
movq 0x18(%rsp), %rsi
movq 0x58(%rsp), %r12
movl 0x14(%rsp), %r13d
movq 0x38(%rsp), %r8
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax,%rsi,8)
movapd %xmm5, %xmm0
unpckhpd %xmm5, %xmm0            # xmm0 = xmm0[1],xmm5[1]
divsd %xmm0, %xmm5
movsd 0xc74f(%rip), %xmm3     # 0x197d0
movapd %xmm3, %xmm1
subsd %xmm5, %xmm1
cmpeqsd 0xcf16(%rip), %xmm0     # 0x19fa8
movapd %xmm0, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm3, %xmm0
orpd %xmm2, %xmm0
movq 0x20(%rsp), %rax
movlpd %xmm0, (%rax,%rsi,8)
movq 0x60(%rsp), %rax
movl %r14d, (%rax,%rsi,4)
movl %esi, %eax
movq %rax, 0x98(%rsp)
movq %rsi, %rax
incq %rax
incq 0xb0(%rsp)
addq $-0x4, 0xa8(%rsp)
addq $-0x8, %r8
movq %rax, %r10
jmp 0xca7f
cmpl $0x0, 0x130(%rsp)
movq 0x98(%rsp), %r13
jle 0xd161
movl %r13d, %r14d
pushq $0x2
popq %r15
leaq 0xd228(%rip), %rbx      # 0x1a32a
leaq 0xd202(%rip), %r12      # 0x1a30b
cmpq %r14, %r15
jae 0xd161
movq 0x13edb(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
movq 0x60(%rsp), %rax
movl (%rax,%r15,4), %r8d
movq 0x70(%rsp), %rax
movq (%rax,%r15,8), %r9
movq 0x30(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movq 0x28(%rsp), %rax
movsd (%rax,%r15,8), %xmm1
movq 0x20(%rsp), %rax
movsd (%rax,%r15,8), %xmm2
leaq -0x2(%r15), %rcx
movq %rbx, %rsi
movq %r12, %rdx
movb $0x3, %al
callq 0x2280
incq %r15
jmp 0xd109
testl %r13d, %r13d
movq 0x58(%rsp), %r14
movq 0x128(%rsp), %r15
je 0xd210
movq 0xd0(%rsp), %r12
testq %r12, %r12
je 0xd1ad
leal -0x1(%r13), %ebx
leaq (,%rbx,8), %rdi
callq 0x2310
movq %rax, (%r12)
movq 0x30(%rsp), %rsi
addq $0x10, %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x22b0
movq 0x120(%rsp), %r12
testq %r12, %r12
je 0xd1e3
leal -0x1(%r13), %ebx
leaq (,%rbx,8), %rdi
callq 0x2310
movq %rax, (%r12)
movq 0x28(%rsp), %rsi
addq $0x10, %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x22b0
testq %r15, %r15
je 0xd210
leal -0x1(%r13), %ebx
leaq (,%rbx,8), %rdi
callq 0x2310
movq %rax, (%r15)
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x22b0
movq 0xb8(%rsp), %r15
testq %r15, %r15
je 0xd244
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rbx
movq %rbx, %rdi
callq 0x178a7
movq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0x178a7
movq %rbx, %rdi
movq %r15, %rsi
callq 0x178a7
movq 0x80(%rsp), %r15
testq %r15, %r15
je 0xd278
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rbx
movq %rbx, %rdi
callq 0x178a7
movq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0x178a7
movq %rbx, %rdi
movq %r15, %rsi
callq 0x178a7
movq 0x8(%rsp), %rdi
callq 0x2060
movq %rbp, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x2060
movq 0x70(%rsp), %rdi
callq 0x2060
movq 0x30(%rsp), %rdi
callq 0x2060
movq 0x28(%rsp), %rdi
callq 0x2060
movq 0x20(%rsp), %rdi
callq 0x2060
movq 0x60(%rsp), %rdi
callq 0x2060
xorl %eax, %eax
subl $0x1, %r13d
cmovbl %eax, %r13d
movl %r13d, %eax
jmp 0xd2d5
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq