// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rdi, %rbx
movq 0x20(%rdi), %rax
testq %rax, %rax
jne 0xa8ed
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x2250
movq %rax, 0x20(%rbx)
movq %rax, 0xa0(%rsp)
callq 0xaf12
movq %rax, 0x38(%rsp)
callq 0xaf12
movq %rax, 0x30(%rsp)
callq 0xaf12
movq %rax, 0x50(%rsp)
callq 0xaf12
movq %rax, 0xa8(%rsp)
movl 0x18(%rbx), %eax
movl %eax, 0x14(%rsp)
movq (%rbx), %rax
movq %rax, 0x90(%rsp)
xorl %esi, %esi
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %r14d, %r14d
xorl %edi, %edi
movq %r15, 0xb0(%rsp)
movq %rbx, 0x58(%rsp)
cmpq 0x90(%rsp), %rdi
movq %rsi, (%rsp)
je 0xaa88
movq %r14, 0x68(%rsp)
movq 0x10(%rbx), %rdx
movq %rdi, 0x60(%rsp)
imulq $0x58, %rdi, %rcx
movl 0x38(%rdx,%rcx), %edi
movq %rdi, 0x18(%rsp)
movq 0x40(%rdx,%rcx), %rdi
movq %rdi, 0x48(%rsp)
movq 0x48(%rdx,%rcx), %rdi
movq %rdi, 0x40(%rsp)
movq 0x50(%rdx,%rcx), %rcx
movq %rcx, 0x70(%rsp)
leaq (%rsi,%rsi), %r14
movq %rsi, %r15
shlq $0x5, %r15
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %r12d, %r12d
leaq (%rsi,%r12), %rcx
cmpq %r12, 0x18(%rsp)
je 0xaa61
movq 0x70(%rsp), %rdx
movq (%rdx,%r12,8), %rbp
movq 0x40(%rsp), %rdx
movq (%rdx,%r12,8), %rbx
cmpq 0x8(%rsp), %rcx
jne 0xa9f6
testq %rcx, %rcx
movq %r14, %rsi
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rsp)
shlq $0x5, %rsi
movq %rax, %rdi
callq 0x2350
shrq %rbp
movq %rbp, (%rax,%r15)
movq %rbx, 0x8(%rax,%r15)
andl $0x0, 0x10(%rax,%r15)
andq $0x0, 0x18(%rax,%r15)
movq 0x48(%rsp), %rcx
movl (%rcx,%r12,4), %ebp
shrl %ebp
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %r13d
setne %dl
cmpl %ecx, %ebp
setne %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xaa4a
addl 0x14(%rsp), %r13d
movl %ebp, %esi
subl %r13d, %esi
movq 0x38(%rsp), %rdi
movq %rax, %rbx
callq 0xaf27
movq %rbx, %rax
incq %r12
addq $0x2, %r14
addq $0x20, %r15
movl %ebp, %r13d
movq (%rsp), %rsi
jmp 0xa9b0
movq 0x68(%rsp), %r14
addq 0x18(%rsp), %r14
movq 0x60(%rsp), %rdi
incq %rdi
movq %rcx, %rsi
movq 0xb0(%rsp), %r15
movq 0x58(%rsp), %rbx
jmp 0xa951
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xb235
movq (%rsp), %rsi
cmpq %rsi, %r14
jne 0xaef3
testq %rsi, %rsi
je 0xaeca
movsd %xmm0, 0x18(%rsp)
leaq 0x86c(%rip), %rcx       # 0xb32a
pushq $0x20
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x20d0
movq (%rsp), %rcx
movq 0x8(%rbx), %rax
addq $0x28, %rbx
pushq $0x1
popq %r14
pushq $0x1
popq %rbp
movl %ebp, %esi
cmpq %r14, %rcx
je 0xab0d
cmpq %rax, (%rbx)
jne 0xaaf1
incl %esi
jmp 0xab04
movq 0x30(%rsp), %rdi
callq 0xaf27
movq (%rsp), %rcx
movq (%rbx), %rax
movl %ebp, %esi
incq %r14
addq $0x20, %rbx
jmp 0xaae3
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0xaf27
leaq 0x28(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
movq %rbx, %rdi
callq 0xb235
movsd %xmm0, 0x48(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xb349
movq %rax, %rdi
leaq 0x84(%rsp), %rsi
andl $0x0, (%rsi)
movq 0x88(%rsp), %rax
leal -0x1(%rax), %edx
movq %rdi, 0x70(%rsp)
movl %edx, %r13d
callq 0xb3c0
movl %eax, 0x14(%rsp)
leaq 0xc95(%rip), %rcx       # 0xb805
pushq $0x20
popq %rdx
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
callq 0x20d0
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
leaq 0x20(%rcx), %rbx
pushq $0x1
popq %r14
pushq $0x1
popq %rbp
movl %ebp, %esi
movq 0x50(%rsp), %rcx
cmpq %r14, %rdx
je 0xabce
cmpq %rax, (%rbx)
jne 0xabad
incl %esi
jmp 0xabc5
movq 0x50(%rsp), %rdi
callq 0xaf27
movq (%rsp), %rdx
movq 0x50(%rsp), %rcx
movq (%rbx), %rax
movl %ebp, %esi
incq %r14
addq $0x20, %rbx
jmp 0xab9f
movq %rcx, %rbx
movq %rcx, %rdi
movq %rdx, %r14
callq 0xaf27
leaq 0x24(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %rbx, %rdi
movq %rdx, %rbp
callq 0xb235
movsd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xb349
leaq 0x7c(%rsp), %rsi
andl $0x0, (%rsi)
movq %rax, 0x68(%rsp)
movq %rax, %rdi
movl %r13d, %edx
movl %r13d, 0x58(%rsp)
callq 0xb3c0
movl %eax, 0x60(%rsp)
leaq 0xf43e(%rip), %rsi      # 0x1a06d
leaq 0xf42c(%rip), %r13      # 0x1a062
movq %r15, %rdi
movq %r13, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2280
movq %r14, %xmm0
movq 0xebbd(%rip), %xmm1     # 0x19810
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xebc1(%rip), %xmm2     # 0x19820
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 0xf3f5(%rip), %rsi      # 0x1a095
movq %r15, %rdi
movq %r13, %rdx
movapd %xmm3, %xmm0
movb $0x1, %al
callq 0x2280
leaq 0xf405(%rip), %rsi      # 0x1a0bd
movq %r15, %rdi
movq %r13, %rdx
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x2280
movl 0x28(%rsp), %ecx
movl 0x2c(%rsp), %r8d
cvtsi2sd %r8d, %xmm0
mulsd 0xf29f(%rip), %xmm0     # 0x19f80
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0xf3ef(%rip), %rsi      # 0x1a0df
movq %r15, %rdi
movq %r13, %rdx
movb $0x1, %al
callq 0x2280
leaq 0xf413(%rip), %rsi      # 0x1a117
movq %r15, %rdi
movq %r13, %rdx
movsd 0x48(%rsp), %xmm0
movb $0x1, %al
callq 0x2280
movl 0x84(%rsp), %r14d
leaq 0xf413(%rip), %rsi      # 0x1a139
movq %r15, %rdi
movq %r13, %rdx
movl 0x14(%rsp), %r12d
movl %r12d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x2280
leaq 0x24(%rsp), %rax
movl (%rax), %ecx
movl (%rbp), %r8d
cvtsi2sd %r8d, %xmm0
mulsd 0xf22a(%rip), %xmm0     # 0x19f80
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0xf3fd(%rip), %rsi      # 0x1a162
movq %r15, %rdi
movq %r13, %rdx
movb $0x1, %al
callq 0x2280
leaq 0xf421(%rip), %rsi      # 0x1a19a
movq %r15, %rdi
movq %r13, %rdx
movsd 0x40(%rsp), %xmm0
movb $0x1, %al
callq 0x2280
leaq 0x7c(%rsp), %rax
movl (%rax), %ebp
leaq 0xf422(%rip), %rsi      # 0x1a1bc
movq %r15, %rdi
movq %r13, %rdx
movl 0x60(%rsp), %r13d
movl %r13d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2280
movq 0xa0(%rsp), %rcx
movq (%rsp), %rax
movq %rax, (%rcx)
movaps 0x90(%rsp), %xmm0
movsd %xmm0, 0x8(%rcx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x10(%rcx)
movl 0x28(%rsp), %eax
movl %eax, 0x28(%rcx)
movl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rcx)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x18(%rcx)
movl %r12d, 0x30(%rcx)
movl %r14d, 0x34(%rcx)
leaq 0x24(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x38(%rcx)
leaq 0x80(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x3c(%rcx)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x20(%rcx)
movl %r13d, 0x40(%rcx)
movl %ebp, 0x44(%rcx)
cmpl $0x2, 0x88(%rsp)
jl 0xae71
leaq 0xf3b1(%rip), %rsi      # 0x1a1e5
movq 0x38(%rsp), %rdi
movq %r15, %rdx
movl 0x58(%rsp), %ebp
movl %ebp, %ecx
callq 0xb824
leaq 0xf39c(%rip), %rsi      # 0x1a1ea
movq 0x30(%rsp), %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xb824
leaq 0xf38b(%rip), %rsi      # 0x1a1ef
movq %rbx, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xb824
movq 0x38(%rsp), %rdi
callq 0xb929
movq 0x30(%rsp), %rdi
callq 0xb929
movq %rbx, %rdi
callq 0xb929
movq 0xa8(%rsp), %rdi
callq 0xb929
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 0xf16f(%rip), %rsi      # 0x1a040
leaq 0xf18a(%rip), %rdx      # 0x1a062
movq %r15, %rdi
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
leaq 0xf104(%rip), %rdi      # 0x19ffe
leaq 0xf10d(%rip), %rsi      # 0x1a00e
leaq 0xf110(%rip), %rcx      # 0x1a018
movl $0x38c, %edx            # imm = 0x38C
callq 0x21d0