// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/syncmer.c
// Function: sr_db_stat
void sr_db_stat(sr_db_t *sr_db, FILE *fo, int verbose)
{
    size_t i, j, n, m;
    int c, w, p0, p1;
    uint64_t s;
    uint64_t h;
    kvec_t(syncmer_t) syncmers;
    kh_ctab_t *dist_ctab, *smer_ctab, *kmer_ctab, *smer_k_ctab;

    sr_stat_t *stats;
    stats = sr_db->stats;
    if (!stats) {
        MYCALLOC(stats, 1);
        sr_db->stats = stats;
    }

    dist_ctab = kh_ctab_init();
    smer_ctab = kh_ctab_init();
    kmer_ctab = kh_ctab_init();
    smer_k_ctab = kh_ctab_init();
    kv_init(syncmers);
    w = sr_db->k;
    n = sr_db->n;
    m = 0;
    for (i = 0; i < n; ++i) {
        sr_t s = sr_db->a[i];
        m += s.n;
        p0 = p1 = MAX_RD_LEN;
        for (j = 0; j < s.n; ++j) {
            syncmer_t a = {s.k_mer[j] >> 1, s.s_mer[j], 0, 0, 0};
            kv_push(syncmer_t, syncmers, a);
            p0 = p1;
            p1 = s.m_pos[j] >> 1;
            if (p0 != (int) MAX_RD_LEN && p1 != (int) MAX_RD_LEN)
                kh_ctab_put1(dist_ctab, p1 - p0 - w);
        }
    }

    double dist;
    dist = kh_ctab_stat(dist_ctab, NULL, NULL);
    
    assert(m == syncmers.n);
    if (syncmers.n == 0) {
        fprintf(fo, "[M::%s] empty syncmer collection\n", __func__);
        return;
    }
    
    syncmer_t *a;

    qsort(syncmers.a, syncmers.n, sizeof(syncmer_t), syncmer_s_cmpfunc);
    a = syncmers.a;
    s = a[0].s, c = 1;
    for (i = 1; i < syncmers.n; ++i) {
        if (a[i].s != s) {
            kh_ctab_put1(smer_ctab, c);
            s = a[i].s, c = 1;
        } else {
            ++c;
        }
    }
    kh_ctab_put1(smer_ctab, c);

#ifdef DEBUG_S_KMER_GROUP
    kvec_t(uint64_t) k_cnt;
    kv_init(k_cnt);
    a = syncmers.a;
    s = a[0].s, h = a[0].h, c = 1;
    for (i = 1; i < syncmers.n; ++i) {
        if (a[i].h != h) {
            kv_push(uint64_t, k_cnt, c);
            if (a[i].s != s) {
                qsort(k_cnt.a, k_cnt.n, sizeof(uint64_t), uint64_r_cmpfunc);
                fprintf(stderr, "[DEBUG_S_KMER_GROUP::%s] %lu", __func__, k_cnt.a[0]);
                for (j = 1; j < k_cnt.n; ++j) fprintf(stderr, " %lu", k_cnt.a[j]);
                fprintf(stderr, "\n");
                kv_size(k_cnt) = 0;
                s = a[i].s;
            }
            h = a[i].h, c = 1;
        } else {
            ++c;
        }
    }
    kv_push(uint64_t, k_cnt, c);
    qsort(k_cnt.a, k_cnt.n, sizeof(uint64_t), uint64_r_cmpfunc);
    fprintf(stderr, "[DEBUG_S_KMER_GROUP::%s] %lu", __func__, k_cnt.a[0]);
    for (j = 1; j < k_cnt.n; ++j) fprintf(stderr, " %lu", k_cnt.a[j]);
    fprintf(stderr, "\n");
    kv_destroy(k_cnt);
#endif

    int smer1, smeru, s_peak_hom, s_peak_het;
    int64_t *s_cnts;
    double smera;
    
    smera = kh_ctab_stat(smer_ctab, &smeru, &smer1);
    s_cnts = kh_ctab_cnt(smer_ctab, MAX_DEPTH);
    s_peak_hom = s_peak_het = 0;
    s_peak_hom = ha_analyze_count(MAX_DEPTH+1, LOWEST_CUT, s_cnts, &s_peak_het, verbose-1);

    qsort(syncmers.a, syncmers.n, sizeof(syncmer_t), syncmer_h_cmpfunc);
    a = syncmers.a;
    h = a[0].h, c = 1;
    for (i = 1; i < syncmers.n; ++i) {
        if (a[i].h != h) {
            kh_ctab_put1(kmer_ctab, c);
            h = a[i].h, c = 1;
        } else {
            ++c;
        }
    }
    kh_ctab_put1(kmer_ctab, c);
    
    int kmer1, kmeru, k_peak_hom, k_peak_het;
    int64_t *k_cnts;
    double kmera;
    
    kmera = kh_ctab_stat(kmer_ctab, &kmeru, &kmer1);
    k_cnts = kh_ctab_cnt(kmer_ctab, MAX_DEPTH);
    k_peak_hom = k_peak_het = 0;
    k_peak_hom = ha_analyze_count(MAX_DEPTH+1, LOWEST_CUT, k_cnts, &k_peak_het, verbose-1);

    fprintf(fo, "[M::%s] number syncmers collected: %lu\n", __func__, m);
    fprintf(fo, "[M::%s] number syncmers per read: %.3f\n", __func__, (double) m / n);
    fprintf(fo, "[M::%s] average kmer space: %.3f\n", __func__, dist);
    fprintf(fo, "[M::%s] number uniqe smer: %d; singletons: %d (%.3f%%)\n", __func__, smeru, smer1, (double) smer1 * 100 / smeru);
    fprintf(fo, "[M::%s] average smer count: %.3f\n", __func__, smera);
    fprintf(fo, "[M::%s] smer peak_hom: %d; peak_het: %d\n", __func__, s_peak_hom, s_peak_het);
    fprintf(fo, "[M::%s] number uniqe kmer: %d; singletons: %d (%.3f%%)\n", __func__, kmeru, kmer1, (double) kmer1 * 100 / kmeru);
    fprintf(fo, "[M::%s] average kmer count: %.3f\n", __func__, kmera);
    fprintf(fo, "[M::%s] kmer peak_hom: %d; peak_het: %d\n", __func__, k_peak_hom, k_peak_het);
    
    stats->syncmer_n = m;
    stats->syncmer_per_read = (double) m / n;
    stats->syncmer_avg_dist = dist;
    stats->smer_unique = smeru;
    stats->smer_singleton = smer1;
    stats->smer_avg_cnt = smera;
    stats->smer_peak_hom = s_peak_hom;
    stats->smer_peak_het = s_peak_het;
    stats->kmer_unique = kmeru;
    stats->kmer_singleton = kmer1;
    stats->kmer_avg_cnt = kmera;
    stats->kmer_peak_hom = k_peak_hom;
    stats->kmer_peak_het = k_peak_het;

    if (verbose > 1) {
        kh_ctab_print(dist_ctab, "DIST", fo, verbose - 1);
        kh_ctab_print(smer_ctab, "SMER", fo, verbose - 1);
        kh_ctab_print(kmer_ctab, "KMER", fo, verbose - 1);
    }

    kh_ctab_destroy(dist_ctab);
    kh_ctab_destroy(smer_ctab);
    kh_ctab_destroy(kmer_ctab);
    kh_ctab_destroy(smer_k_ctab);
    free(s_cnts);
    free(k_cnts);
    kv_destroy(syncmers);

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x88(%rsp)
movq %rsi, %rbp
movq %rdi, %r14
movq 0x20(%rdi), %rax
testq %rax, %rax
jne 0xd464
movl $0x1, %edi
movl $0x48, %esi
callq 0x2250
movq %rax, 0x20(%r14)
movq %rax, 0xa0(%rsp)
xorl %ebx, %ebx
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, 0x28(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, 0x48(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, 0x20(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
xorl %edx, %edx
movq %rdx, 0x18(%rax)
movq (%r14), %rcx
movq %rcx, 0x90(%rsp)
testq %rcx, %rcx
movq %rax, 0xa8(%rsp)
je 0xd656
movl 0x18(%r14), %eax
movl %eax, 0x68(%rsp)
xorl %esi, %esi
xorl %r15d, %r15d
xorl %edx, %edx
xorl %edi, %edi
xorl %r13d, %r13d
movq %rbp, 0xb0(%rsp)
movq %r14, 0x50(%rsp)
movq 0x10(%r14), %rax
imulq $0x58, %rsi, %rcx
movl 0x38(%rax,%rcx), %r8d
movq %r8, 0x8(%rsp)
testq %r8, %r8
je 0xd63e
movq %rsi, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq 0x40(%rax,%rcx), %rsi
movq %rsi, 0x40(%rsp)
movq 0x48(%rax,%rcx), %rsi
movq %rsi, 0x38(%rsp)
movq 0x50(%rax,%rcx), %rax
movq %rax, 0x30(%rsp)
movq %r13, %rbx
shlq $0x5, %rbx
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %r13, 0x70(%rsp)
leaq (%r12,%r13), %rax
movq 0x30(%rsp), %rcx
movq (%rcx,%r12,8), %r14
movq 0x38(%rsp), %rcx
movq (%rcx,%r12,8), %rbp
cmpq %rdi, %rax
jne 0xd5ad
leaq (%rdi,%rdi), %r13
testq %rdi, %rdi
movl $0x2, %eax
cmoveq %rax, %r13
movq %r13, %rsi
shlq $0x5, %rsi
movq %rdx, %rdi
callq 0x2360
movq %rax, %rdx
movq %r13, %rdi
movq 0x70(%rsp), %r13
shrq %r14
movq %r14, (%rdx,%rbx)
movq %rbp, 0x8(%rdx,%rbx)
movl $0x0, 0x10(%rdx,%rbx)
movq $0x0, 0x18(%rdx,%rbx)
movq 0x40(%rsp), %rax
movl (%rax,%r12,4), %r14d
shrl %r14d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %r15d
setne %al
cmpl %ecx, %r14d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xd60f
addl 0x68(%rsp), %r15d
movl %r14d, %esi
subl %r15d, %esi
movq %rdi, %rbp
movq 0x28(%rsp), %rdi
movq %rdx, %r15
callq 0xdba9
movq %rbp, %rdi
movq %r15, %rdx
incq %r12
addq $0x20, %rbx
movl %r14d, %r15d
cmpq %r12, 0x8(%rsp)
jne 0xd568
addq %r12, %r13
movq 0xb0(%rsp), %rbp
movq 0x60(%rsp), %r15
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %rsi
addq 0x8(%rsp), %r15
incq %rsi
cmpq 0x90(%rsp), %rsi
jne 0xd510
jmp 0xd65c
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rdx, 0x8(%rsp)
movq 0x28(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xdedb
cmpq %r13, %r15
jne 0xdb8a
testq %r13, %r13
je 0xdb61
movsd %xmm0, 0x40(%rsp)
leaq 0x963(%rip), %rcx       # 0xdff1
movl $0x20, %edx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x20d0
movl $0x1, %r12d
movl $0x1, %esi
cmpq $0x1, %r13
je 0xd6e6
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
leaq -0x1(%r13), %rbx
leaq 0x28(%rcx), %r14
cmpq %rax, (%r14)
jne 0xd6cb
incl %esi
jmp 0xd6dd
movq 0x48(%rsp), %rdi
callq 0xdba9
movq (%r14), %rax
movl $0x1, %esi
addq $0x20, %r14
decq %rbx
jne 0xd6c2
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0xdba9
leaq 0x18(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
movq %rbx, %rdi
callq 0xdedb
movsd %xmm0, 0x38(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xe010
movq %rax, %rdi
leaq 0x84(%rsp), %rsi
movl $0x0, (%rsi)
movq 0x88(%rsp), %rax
leal -0x1(%rax), %edx
movq %rdi, 0x70(%rsp)
movq %r13, %r14
movl %edx, %r13d
callq 0xe088
movl %eax, 0x60(%rsp)
leaq 0xe28(%rip), %rcx       # 0xe577
movl $0x20, %edx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x20d0
cmpq $0x1, %r14
je 0xd7b2
cmpq $0x3, %r14
movl $0x2, %ebx
cmovaeq %r14, %rbx
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
decq %rbx
leaq 0x20(%rcx), %r14
movl $0x1, %r12d
cmpq %rax, (%r14)
jne 0xd793
incl %r12d
jmp 0xd7a9
movq 0x20(%rsp), %rdi
movl %r12d, %esi
callq 0xdba9
movq (%r14), %rax
movl $0x1, %r12d
addq $0x20, %r14
decq %rbx
jne 0xd789
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
callq 0xdba9
leaq 0x14(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %rbx, %rdi
movq %rdx, %r14
callq 0xdedb
movsd %xmm0, 0x30(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xe010
leaq 0x7c(%rsp), %rsi
movl $0x0, (%rsi)
movq %rax, 0x68(%rsp)
movq %rax, %rdi
movl %r13d, %edx
movl %r13d, 0x50(%rsp)
callq 0xe088
movl %eax, 0x58(%rsp)
leaq 0x1080d(%rip), %rsi     # 0x1e025
leaq 0x107fb(%rip), %r12     # 0x1e01a
movq %rbp, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2290
movq %r15, %xmm0
movq 0xffd4(%rip), %xmm1     # 0x1d810
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xffd8(%rip), %xmm2     # 0x1d820
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq 0x90(%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movapd %xmm3, 0x90(%rsp)
leaq 0x107c4(%rip), %rsi     # 0x1e04d
movq %rbp, %rdi
movq %r12, %rdx
movapd %xmm3, %xmm0
movb $0x1, %al
callq 0x2290
leaq 0x107d4(%rip), %rsi     # 0x1e075
movq %rbp, %rdi
movq %r12, %rdx
movsd 0x40(%rsp), %xmm0
movb $0x1, %al
callq 0x2290
movl 0x18(%rsp), %ecx
movl 0x1c(%rsp), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd 0x10663(%rip), %xmm0    # 0x1df30
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x107b8(%rip), %rsi     # 0x1e097
movq %rbp, %rdi
movq %r12, %rdx
movb $0x1, %al
callq 0x2290
leaq 0x107dc(%rip), %rsi     # 0x1e0cf
movq %rbp, %rdi
movq %r12, %rdx
movsd 0x38(%rsp), %xmm0
movb $0x1, %al
callq 0x2290
movq %r15, %rbx
movl 0x84(%rsp), %r15d
leaq 0x107d9(%rip), %rsi     # 0x1e0f1
movq %rbp, %rdi
movq %r12, %rdx
movl 0x60(%rsp), %r13d
movl %r13d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x2290
leaq 0x14(%rsp), %rax
movl (%rax), %ecx
movl (%r14), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd 0x105e6(%rip), %xmm0    # 0x1df30
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x107be(%rip), %rsi     # 0x1e11a
movq %rbp, %rdi
movq %r12, %rdx
movb $0x1, %al
callq 0x2290
leaq 0x107e2(%rip), %rsi     # 0x1e152
movq %rbp, %rdi
movq %r12, %rdx
movsd 0x30(%rsp), %xmm0
movb $0x1, %al
callq 0x2290
leaq 0x7c(%rsp), %rax
movl (%rax), %r14d
leaq 0x107e2(%rip), %rsi     # 0x1e174
movq %rbp, %rdi
movq %r12, %rdx
movl 0x58(%rsp), %r12d
movl %r12d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x2290
movq 0xa0(%rsp), %rcx
movq %rbx, (%rcx)
movaps 0x90(%rsp), %xmm0
movsd %xmm0, 0x8(%rcx)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x10(%rcx)
movl 0x18(%rsp), %eax
movl %eax, 0x28(%rcx)
movl 0x1c(%rsp), %eax
movl %eax, 0x2c(%rcx)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x18(%rcx)
movl %r13d, 0x30(%rcx)
movl %r15d, 0x34(%rcx)
leaq 0x14(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x38(%rcx)
leaq 0x80(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x3c(%rcx)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x20(%rcx)
movl %r12d, 0x40(%rcx)
movl %r14d, 0x44(%rcx)
cmpl $0x2, 0x88(%rsp)
movq 0x48(%rsp), %rbx
jl 0xda6f
leaq 0x1076f(%rip), %rsi     # 0x1e19d
movq 0x28(%rsp), %rdi
movq %rbp, %rdx
movl 0x50(%rsp), %r14d
movl %r14d, %ecx
callq 0xe596
leaq 0x10758(%rip), %rsi     # 0x1e1a2
movq %rbx, %rdi
movq %rbp, %rdx
movl %r14d, %ecx
callq 0xe596
leaq 0x10748(%rip), %rsi     # 0x1e1a7
movq 0x20(%rsp), %rdi
movq %rbp, %rdx
movl %r14d, %ecx
callq 0xe596
movq 0x28(%rsp), %r15
testq %r15, %r15
je 0xdaa0
movq 0x10(%r15), %rsi
movq 0x18(%r15), %r14
movq %r14, %rdi
callq 0x1b5ca
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x1b5ca
movq %r14, %rdi
movq %r15, %rsi
callq 0x1b5ca
testq %rbx, %rbx
je 0xdacc
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x1b5ca
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x1b5ca
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1b5ca
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0xdafd
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x1b5ca
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x1b5ca
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1b5ca
movq 0xa8(%rsp), %rbx
testq %rbx, %rbx
je 0xdb31
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x1b5ca
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x1b5ca
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1b5ca
movq 0x70(%rsp), %rdi
callq 0x2060
movq 0x68(%rsp), %rdi
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2060
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10490(%rip), %rsi     # 0x1dff8
leaq 0x104ab(%rip), %rdx     # 0x1e01a
movq %rbp, %rdi
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2290
leaq 0x10425(%rip), %rdi     # 0x1dfb6
leaq 0x1042e(%rip), %rsi     # 0x1dfc6
leaq 0x10431(%rip), %rcx     # 0x1dfd0
movl $0x38c, %edx            # imm = 0x38C
callq 0x21d0