// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %r9, %rbp
movsd %xmm0, 0x80(%rsp)
movl %ecx, 0x10(%rsp)
movl %edx, 0x24(%rsp)
movq %rdi, %rbx
cmpl $0x1f, %r8d
movl $0x1e, %eax
cmovael %r8d, %eax
movl %eax, 0x20(%rsp)
movq 0x10(%rsi), %rax
movq %rax, (%rsp)
xorl %r13d, %r13d
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, %r15
movq $0x0, 0x18(%rax)
cmpq $0x0, (%rbx)
je 0xf761
xorl %r14d, %r14d
movl $0x38, %r12d
xorl %r13d, %r13d
movq 0x10(%rbx), %rax
movl (%rax,%r12), %esi
cmpl %esi, %r13d
cmovbel %esi, %r13d
movq %r15, %rdi
callq 0xdba9
incq %r14
addq $0x58, %r12
cmpq (%rbx), %r14
jb 0xf73e
movq %rbx, 0x98(%rsp)
testl %r13d, %r13d
je 0x10441
movq %r15, %rdi
movl %r13d, %esi
callq 0xe010
movq %rax, 0x70(%rsp)
testq %r15, %r15
je 0xf7ae
movq 0x10(%r15), %rsi
movq 0x18(%r15), %r12
movq %r12, %rdi
callq 0x1b5ca
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x1b5ca
movq %r12, %rdi
movq %r15, %rsi
callq 0x1b5ca
movq %rbp, 0xa8(%rsp)
movsd 0x80(%rsp), %xmm0
maxsd 0xe799(%rip), %xmm0     # 0x1df60
movsd %xmm0, 0x80(%rsp)
movl %r13d, %edi
xorl %eax, %eax
cmpq $0x1, %rdi
movl $0x0, %ecx
sbbq %rdi, %rcx
movq 0x70(%rsp), %rsi
movq (%rsi,%rdi,8), %rdx
movq %rdi, 0x68(%rsp)
leaq (%rsi,%rdi,8), %rsi
addq $-0x8, %rsi
addq (%rsi,%rax,8), %rdx
movq %rdx, (%rsi,%rax,8)
decq %rax
cmpq %rax, %rcx
jne 0xf7f7
xorl %ebx, %ebx
movl $0x1, %eax
movq %rax, 0x78(%rsp)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, 0x18(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, %rbp
movq $0x0, 0x40(%rsp)
movq %rbx, 0x18(%rax)
movq 0x68(%rsp), %rbx
leaq (,%rbx,8), %rdi
callq 0x2320
movq %rax, %r15
leal 0x1(%r13), %edi
movl $0x8, %esi
callq 0x2250
movq %rax, 0x88(%rsp)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2250
movq %rax, 0x38(%rsp)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2250
movq %rax, 0x30(%rsp)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2250
movq %rax, 0x28(%rsp)
movl $0x4, %esi
movq %rbx, %rdi
callq 0x2250
movq %rax, 0x60(%rsp)
movl $0x0, %r14d
cmpl $0x3, %r13d
movq %rbp, 0x90(%rsp)
movq (%rsp), %r9
jb 0x10236
movl 0x10(%rsp), %eax
movq %rax, 0xb0(%rsp)
addq $-0x2, 0x68(%rsp)
movq $0x0, 0xa0(%rsp)
movl $0x2, %r8d
xorl %r14d, %r14d
movq $0x0, 0x40(%rsp)
movq 0x70(%rsp), %rax
movq 0xb0(%rsp), %rcx
cmpq %rcx, (%rax,%r8,8)
jl 0x10236
movq %r8, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0xf974
movq 0x8(%r8), %rdi
testq %rdi, %rdi
je 0xf974
movl (%r8), %ecx
movl $0x1, %edx
shll %cl, %edx
shrl $0x3, %edx
andl $-0x4, %edx
cmpl $0x5, %ecx
movl $0x4, %eax
cmovbl %eax, %edx
xorl %esi, %esi
callq 0x21e0
movq (%rsp), %r9
movq 0x18(%rsp), %r8
movl $0x0, 0x4(%r8)
movq 0x98(%rsp), %rax
cmpq $0x0, (%rax)
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
je 0xfa9e
movq 0x10(%rsp), %rcx
decl %ecx
movl %ecx, 0x50(%rsp)
xorl %r12d, %r12d
movq 0x10(%rax), %r13
imulq $0x58, %r12, %rcx
movl 0x38(%r13,%rcx), %eax
cmpq %rax, 0x10(%rsp)
ja 0xfa8a
addq %rcx, %r13
testq %rax, %rax
je 0xf9d2
movq 0x50(%r13), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq %rsi
movq %rsi, (%r15,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0xf9bf
movl 0x50(%rsp), %eax
cmpl 0x38(%r13), %eax
movl 0x24(%rsp), %esi
jae 0xfa8a
xorl %ebx, %ebx
movq 0x78(%rsp), %r14
movq (%r15,%rbx,2), %rax
movq %rax, %rcx
shlq $0x5, %rcx
movl 0x10(%r9,%rcx), %ecx
andl %edi, %ecx
cmpl %esi, %ecx
jb 0xfa76
movq (%r15,%r14,8), %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
movl 0x10(%r9,%rdx), %edx
andl %edi, %edx
cmpl %esi, %edx
jb 0xfa76
movq 0x40(%r13), %rsi
movl (%rsi,%rbx), %edx
andl $0x1, %edx
leaq (%rdx,%rax,2), %rdx
movl (%rsi,%r14,4), %eax
andl $0x1, %eax
leaq (%rax,%rcx,2), %rax
cmpq %rax, %rdx
jbe 0xfa48
xorq $0x1, %rax
xorq $0x1, %rdx
movq %r8, %rdi
movq %rdx, %rsi
movq %rax, %rdx
jmp 0xfa4e
movq %r8, %rdi
movq %rax, %rsi
callq 0x10455
movq 0x88(%rsp), %rax
movq 0x10(%rsp), %rcx
incq (%rax,%rcx,8)
movq 0x18(%rsp), %r8
movl 0x24(%rsp), %esi
movq (%rsp), %r9
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
incq %r14
movl 0x38(%r13), %eax
addq $0x4, %rbx
cmpq %rax, %r14
jb 0xf9eb
incq %r12
movq 0x98(%rsp), %rax
cmpq (%rax), %r12
jb 0xf999
cmpq $0x2, 0x10(%rsp)
jne 0xfb0c
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%r8), %rax
testq %rax, %rax
je 0xfac2
movb (%r8), %cl
movl $0x1, %edx
shlq %cl, %rdx
jmp 0xfac4
xorl %edx, %edx
cmpq %rdx, %r12
jae 0xfb0c
movq 0x8(%r8), %rcx
movl %r12d, %edx
shrl $0x5, %edx
movl (%rcx,%rdx,4), %ecx
btl %r12d, %ecx
jae 0xfb03
movq (%rax,%r14), %rbx
movq 0x8(%rax,%r14), %rsi
shrq %rsi
movq %rbp, %rdi
callq 0x10993
shrq %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x10993
movq 0x18(%rsp), %r8
incq %r12
addq $0x18, %r14
jmp 0xfaac
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0x10(%r8), %rsi
testq %rsi, %rsi
je 0xfb27
movb (%r8), %cl
movl $0x1, %eax
shlq %cl, %rax
jmp 0xfb29
xorl %eax, %eax
cmpq %rax, %rbx
jae 0xff7f
movq 0x8(%r8), %rax
movl %ebx, %ecx
shrl $0x5, %ecx
movl (%rax,%rcx,4), %eax
btl %ebx, %eax
jae 0xff77
leaq (%rbx,%rbx,2), %r8
movq 0x8(%rsi,%r8,8), %rax
shrq %rax
movq 0x10(%rbp), %rdx
xorl %r9d, %r9d
movl $0x0, %edi
testq %rdx, %rdx
je 0xfbf3
movb (%rbp), %cl
movl $0x1, %edi
shll %cl, %edi
movq %rax, %r10
notq %r10
movq %rax, %r11
shlq $0x15, %r11
addq %r10, %r11
movq %r11, %r10
shrq $0x18, %r10
xorq %r11, %r10
imulq $0x109, %r10, %r10      # imm = 0x109
movq %r10, %r11
shrq $0xe, %r11
xorq %r10, %r11
leaq (%r11,%r11,4), %r10
leaq (%r11,%r10,4), %r10
movq %r10, %r11
shrq $0x1c, %r11
xorl %r10d, %r11d
imull $0x1e3779b9, %r11d, %r10d # imm = 0x1E3779B9
negb %cl
shrl %cl, %r10d
leal -0x1(%rdi), %ecx
movq 0x8(%rbp), %r14
movl %r10d, %r11d
movl %r11d, %r12d
shrl $0x5, %r12d
movl (%r14,%r12,4), %r12d
btl %r11d, %r12d
jae 0xfbf3
movl %r11d, %r12d
leaq (%r12,%r12,2), %r12
cmpq %rax, (%rdx,%r12,4)
je 0xfbf0
incl %r11d
andl %ecx, %r11d
cmpl %r10d, %r11d
jne 0xfbc5
jmp 0xfbf3
movl %r11d, %edi
testq %rdx, %rdx
je 0xfc04
movb (%rbp), %cl
movl $0x1, %r9d
shll %cl, %r9d
movsd 0xdbc4(%rip), %xmm0     # 0x1d7d0
cmpl %r9d, %edi
jae 0xfcfb
movl %edi, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x2, 0x8(%rdx,%rcx,4)
movsd 0xdba8(%rip), %xmm0     # 0x1d7d0
jl 0xfcfb
xorl %r9d, %r9d
movl $0x0, %edi
testq %rdx, %rdx
je 0xfcca
movb (%rbp), %cl
movl $0x1, %edi
shll %cl, %edi
movq %rax, %r10
notq %r10
movq %rax, %r11
shlq $0x15, %r11
addq %r10, %r11
movq %r11, %r10
shrq $0x18, %r10
xorq %r11, %r10
imulq $0x109, %r10, %r10      # imm = 0x109
movq %r10, %r11
shrq $0xe, %r11
xorq %r10, %r11
leaq (%r11,%r11,4), %r10
leaq (%r11,%r10,4), %r10
movq %r10, %r11
shrq $0x1c, %r11
xorl %r10d, %r11d
imull $0x1e3779b9, %r11d, %r10d # imm = 0x1E3779B9
negb %cl
shrl %cl, %r10d
leal -0x1(%rdi), %ecx
movq 0x8(%rbp), %r14
movl %r10d, %r11d
movl %r11d, %r12d
shrl $0x5, %r12d
movl (%r14,%r12,4), %r12d
btl %r11d, %r12d
jae 0xfcca
movl %r11d, %r12d
leaq (%r12,%r12,2), %r12
cmpq %rax, (%rdx,%r12,4)
je 0xfcc7
incl %r11d
andl %ecx, %r11d
cmpl %r10d, %r11d
jne 0xfc9c
jmp 0xfcca
movl %r11d, %edi
testq %rdx, %rdx
je 0xfcdb
movb (%rbp), %cl
movl $0x1, %r9d
shll %cl, %r9d
xorpd %xmm0, %xmm0
cmpl %r9d, %edi
jae 0xfcfb
movl %edi, %ecx
leaq (%rcx,%rcx,2), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rdx,%rcx,4), %xmm0
mulsd 0xe25d(%rip), %xmm0     # 0x1df58
movq (%rsi,%r8,8), %rdi
movl 0x10(%rsi,%r8,8), %r12d
shrq %rdi
xorl %r8d, %r8d
movl $0x0, %esi
testq %rdx, %rdx
je 0xfda3
movb (%rbp), %cl
movl $0x1, %esi
shll %cl, %esi
movq %rdi, %r9
notq %r9
movq %rdi, %r10
shlq $0x15, %r10
addq %r9, %r10
movq %r10, %r9
shrq $0x18, %r9
xorq %r10, %r9
imulq $0x109, %r9, %r9        # imm = 0x109
movq %r9, %r10
shrq $0xe, %r10
xorq %r9, %r10
leaq (%r10,%r10,4), %r9
leaq (%r10,%r9,4), %r9
movq %r9, %r10
shrq $0x1c, %r10
xorl %r9d, %r10d
imull $0x1e3779b9, %r10d, %r9d # imm = 0x1E3779B9
negb %cl
shrl %cl, %r9d
leal -0x1(%rsi), %ecx
movq 0x8(%rbp), %r11
movl %r9d, %r10d
movl %r10d, %r14d
shrl $0x5, %r14d
movl (%r11,%r14,4), %r14d
btl %r10d, %r14d
jae 0xfda3
movl %r10d, %r14d
leaq (%r14,%r14,2), %r14
cmpq %rdi, (%rdx,%r14,4)
je 0xfda0
incl %r10d
andl %ecx, %r10d
cmpl %r9d, %r10d
jne 0xfd75
jmp 0xfda3
movl %r10d, %esi
testq %rdx, %rdx
je 0xfdb4
movb (%rbp), %cl
movl $0x1, %r8d
shll %cl, %r8d
movsd 0xda14(%rip), %xmm1     # 0x1d7d0
cmpl %r8d, %esi
movq (%rsp), %r9
jae 0xfeb3
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x2, 0x8(%rdx,%rcx,4)
movsd 0xd9f4(%rip), %xmm1     # 0x1d7d0
jl 0xfeb3
xorl %r8d, %r8d
movl $0x0, %esi
testq %rdx, %rdx
je 0xfe82
movb (%rbp), %cl
movl $0x1, %esi
shll %cl, %esi
movq %rdi, %r9
notq %r9
movq %rdi, %r10
shlq $0x15, %r10
addq %r9, %r10
movq %r10, %r9
shrq $0x18, %r9
xorq %r10, %r9
imulq $0x109, %r9, %r9        # imm = 0x109
movq %r9, %r10
shrq $0xe, %r10
xorq %r9, %r10
leaq (%r10,%r10,4), %r9
leaq (%r10,%r9,4), %r9
movq %r9, %r10
shrq $0x1c, %r10
xorl %r9d, %r10d
imull $0x1e3779b9, %r10d, %r9d # imm = 0x1E3779B9
negb %cl
shrl %cl, %r9d
leal -0x1(%rsi), %ecx
movq 0x8(%rbp), %r11
movl %r9d, %r10d
movl %r10d, %r14d
shrl $0x5, %r14d
movl (%r11,%r14,4), %r14d
btl %r10d, %r14d
jae 0xfe7e
movl %r10d, %r14d
leaq (%r14,%r14,2), %r14
cmpq %rdi, (%rdx,%r14,4)
je 0xfe7b
incl %r10d
andl %ecx, %r10d
cmpl %r9d, %r10d
jne 0xfe50
jmp 0xfe7e
movl %r10d, %esi
movq (%rsp), %r9
testq %rdx, %rdx
je 0xfe93
movb (%rbp), %cl
movl $0x1, %r8d
shll %cl, %r8d
xorpd %xmm1, %xmm1
cmpl %r8d, %esi
jae 0xfeb3
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
xorps %xmm1, %xmm1
cvtsi2sdl 0x8(%rdx,%rcx,4), %xmm1
mulsd 0xe0a5(%rip), %xmm1     # 0x1df58
shlq $0x5, %rax
movl 0x10(%r9,%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2sd %eax, %xmm2
divsd %xmm0, %xmm2
shlq $0x5, %rdi
movl 0x10(%r9,%rdi), %eax
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
minsd %xmm0, %xmm2
cvttsd2si %xmm2, %r14
cmpl %r14d, %r12d
cmovael %r14d, %r12d
movq 0xa0(%rsp), %rax
cmpq %rax, %r13
jne 0xff36
leaq (%rax,%rax), %rbp
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbp
movq %rbp, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
callq 0x2360
movq %rbp, 0xa0(%rsp)
movq 0x90(%rsp), %rbp
movq 0x18(%rsp), %r8
jmp 0xff40
movq 0x18(%rsp), %r8
movq 0x8(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movl %r14d, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
leaq 0x1(%r13), %rcx
shlq $0x4, %r13
movl %r14d, (%rax,%r13)
movl %r12d, 0x4(%rax,%r13)
movq %rax, 0x8(%rsp)
movsd %xmm0, 0x8(%rax,%r13)
movq %rcx, %r13
incq %rbx
jmp 0xfb11
movq %r13, %xmm0
punpckldq 0xd884(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xd88c(%rip), %xmm0     # 0x1d820
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
movapd %xmm1, %xmm0
mulsd 0xdf8e(%rip), %xmm0     # 0x1df40
callq 0x2410
cvttsd2si %xmm0, %rbx
movapd 0x50(%rsp), %xmm0
mulsd 0xdf7e(%rip), %xmm0     # 0x1df48
callq 0x24f0
cvttsd2si %xmm0, %rax
movq %rax, 0x50(%rsp)
movl %eax, %r12d
movl $0x10, %edx
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0xd1d(%rip), %rcx       # 0x10d10
callq 0x20d0
cmpl 0x50(%rsp), %ebx
jae 0x10037
movl %ebx, %ebx
movq %rbx, %rax
shlq $0x4, %rax
addq %r14, %rax
addq $0x8, %rax
movq (%rsp), %r9
movq 0x10(%rsp), %r8
movsd 0x80(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x10040
incq %rbx
addq $0x10, %rax
cmpq %rbx, %r12
jne 0x10017
movl %r12d, %ebx
jmp 0x10040
movq (%rsp), %r9
movq 0x10(%rsp), %r8
movl %r12d, %r13d
subl %ebx, %r13d
cmpl 0x20(%rsp), %r13d
jb 0x101f1
cmpl $0x2, 0x100(%rsp)
jl 0x10064
cmpl %ebx, %r12d
ja 0x10216
movl %ebx, %eax
xorpd %xmm6, %xmm6
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpl %ebx, %r12d
jbe 0x100ca
movq %r12, %rcx
subq %rax, %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
addq $0x4, %rdx
movl -0x4(%rdx), %esi
movl (%rdx), %edi
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %rdi, %xmm4
movapd %xmm3, %xmm5
mulsd %xmm4, %xmm5
addsd %xmm5, %xmm2
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm1
addsd %xmm4, %xmm0
addq $0x10, %rdx
decq %rcx
jne 0x10094
divsd %xmm1, %xmm2
movq 0x38(%rsp), %rcx
movsd %xmm2, (%rcx,%r8,8)
xorpd %xmm5, %xmm5
cmpl %ebx, %r12d
movq 0x8(%rsp), %r14
jbe 0x1014a
movl %r13d, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
divsd %xmm3, %xmm0
xorpd 0xde22(%rip), %xmm2     # 0x1df20
subq %rax, %r12
shlq $0x4, %rax
addq %r14, %rax
addq $0x4, %rax
xorpd %xmm6, %xmm6
movl (%rax), %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
movl -0x4(%rax), %ecx
xorps %xmm4, %xmm4
cvtsi2sd %rcx, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
mulsd %xmm4, %xmm4
addsd %xmm4, %xmm6
subsd %xmm0, %xmm3
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm5
addq $0x10, %rax
decq %r12
jne 0x10110
movapd %xmm6, %xmm0
leal -0x1(%r13), %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
divsd %xmm1, %xmm0
divsd %xmm2, %xmm0
ucomisd 0xddf6(%rip), %xmm0     # 0x1df60
jb 0x10172
sqrtsd %xmm0, %xmm0
jmp 0x1019d
movapd %xmm6, 0x50(%rsp)
movapd %xmm5, 0x40(%rsp)
callq 0x2490
movapd 0x40(%rsp), %xmm5
movapd 0x50(%rsp), %xmm6
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r14
movq (%rsp), %r9
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax,%r8,8)
divsd %xmm5, %xmm6
movsd 0xd61c(%rip), %xmm2     # 0x1d7d0
movapd %xmm2, %xmm0
subsd %xmm6, %xmm0
cmpeqsd 0xdd9b(%rip), %xmm5     # 0x1df60
movapd %xmm5, %xmm1
andnpd %xmm0, %xmm1
andpd %xmm2, %xmm5
orpd %xmm1, %xmm5
movq 0x28(%rsp), %rax
movlpd %xmm5, (%rax,%r8,8)
movq 0x60(%rsp), %rax
movl %r13d, (%rax,%r8,4)
movl %r8d, %eax
movq %rax, 0x40(%rsp)
cmpl 0x20(%rsp), %r13d
jb 0x10236
incq %r8
movq 0x78(%rsp), %rcx
leaq 0x1(%rcx), %rax
cmpq 0x68(%rsp), %rcx
movq %rax, 0x78(%rsp)
jne 0xf90d
jmp 0x10236
movl %ebx, %edi
movq %r8, %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0x2a67
movq 0x10(%rsp), %r8
movq (%rsp), %r9
jmp 0x10064
movq %r14, 0x8(%rsp)
cmpl $0x0, 0x100(%rsp)
jle 0x102c0
cmpl $0x3, 0x40(%rsp)
jb 0x102c0
movl 0x40(%rsp), %r13d
addq $-0x2, %r13
xorl %ebx, %ebx
movq 0x13d92(%rip), %rbp     # 0x23ff0
leaq 0xe07d(%rip), %r14      # 0x1e2e2
leaq 0xe057(%rip), %r12      # 0x1e2c3
movq (%rbp), %rdi
movq 0x60(%rsp), %rax
movl 0x8(%rax,%rbx,4), %r8d
movq 0x88(%rsp), %rax
movq 0x10(%rax,%rbx,8), %r9
movq 0x38(%rsp), %rax
movsd 0x10(%rax,%rbx,8), %xmm0
movq 0x30(%rsp), %rax
movsd 0x10(%rax,%rbx,8), %xmm1
movq 0x28(%rsp), %rax
movsd 0x10(%rax,%rbx,8), %xmm2
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movb $0x3, %al
callq 0x2290
incq %rbx
cmpq %rbx, %r13
jne 0x1026c
movq 0x40(%rsp), %rbp
testl %ebp, %ebp
movq 0x90(%rsp), %r12
movq 0xf8(%rsp), %r13
movq 0x18(%rsp), %rax
je 0x10386
movq 0xa8(%rsp), %r14
testq %r14, %r14
je 0x1031b
leal -0x1(%rbp), %ebx
leaq (,%rbx,8), %rdi
callq 0x2320
movq %rax, (%r14)
movq 0x38(%rsp), %rsi
addq $0x10, %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x22c0
movq 0x18(%rsp), %rax
movq 0xf0(%rsp), %r14
testq %r14, %r14
je 0x10354
leal -0x1(%rbp), %ebx
leaq (,%rbx,8), %rdi
callq 0x2320
movq %rax, (%r14)
movq 0x30(%rsp), %rsi
addq $0x10, %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x22c0
movq 0x18(%rsp), %rax
testq %r13, %r13
je 0x10386
leal -0x1(%rbp), %ebx
leaq (,%rbx,8), %rdi
callq 0x2320
movq %rax, (%r13)
movq 0x28(%rsp), %rsi
addq $0x10, %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x22c0
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x103b5
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rbx
movq %rbx, %rdi
movq %rax, %r14
callq 0x1b5ca
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1b5ca
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b5ca
testq %r12, %r12
je 0x103e4
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rbx
movq %rbx, %rdi
callq 0x1b5ca
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x1b5ca
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1b5ca
movq 0x8(%rsp), %rdi
callq 0x2060
movq %r15, %rdi
callq 0x2060
movq 0x70(%rsp), %rdi
callq 0x2060
movq 0x88(%rsp), %rdi
callq 0x2060
movq 0x38(%rsp), %rdi
callq 0x2060
movq 0x30(%rsp), %rdi
callq 0x2060
movq 0x28(%rsp), %rdi
callq 0x2060
movq 0x60(%rsp), %rdi
callq 0x2060
xorl %eax, %eax
subl $0x1, %ebp
cmovbl %eax, %ebp
movl %ebp, %eax
jmp 0x10443
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq