// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq 0x20(%rdi), %rax
testq %rax, %rax
jne 0xdcd0
movl $0x1, %edi
movl $0x48, %esi
callq 0x2250
movq %rax, 0x20(%r15)
movq %rax, 0xa8(%rsp)
xorl %ebx, %ebx
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1d660
movq %rax, %r14
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1d660
movq %rax, 0x28(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1d660
movq %rax, 0x20(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1d660
movq %rbx, 0x18(%rax)
movq (%r15), %rcx
movq %rcx, 0x90(%rsp)
testq %rcx, %rcx
je 0xe3cb
movq %r14, 0x30(%rsp)
movq %rax, 0xa0(%rsp)
movl 0x18(%r15), %eax
movl %eax, 0x70(%rsp)
xorl %esi, %esi
xorl %r14d, %r14d
xorl %eax, %eax
xorl %edi, %edi
xorl %ebx, %ebx
movq %rbp, 0xb0(%rsp)
movq %r15, 0x58(%rsp)
movq 0x10(%r15), %rdx
imulq $0x58, %rsi, %rcx
movl 0x38(%rdx,%rcx), %r8d
movq %r8, 0x8(%rsp)
testq %r8, %r8
je 0xdead
movq %rsi, 0x60(%rsp)
movq %r14, 0x68(%rsp)
movq 0x40(%rdx,%rcx), %rsi
movq %rsi, 0x48(%rsp)
movq 0x48(%rdx,%rcx), %rsi
movq %rsi, 0x40(%rsp)
movq 0x50(%rdx,%rcx), %rcx
movq %rcx, 0x38(%rsp)
movq %rbx, %r14
shlq $0x5, %r14
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %rbx, 0x50(%rsp)
leaq (%rbx,%r12), %rcx
movq 0x38(%rsp), %rdx
movq (%rdx,%r12,8), %r15
movq 0x40(%rsp), %rdx
movq (%rdx,%r12,8), %rbp
cmpq %rdi, %rcx
jne 0xde16
leaq (%rdi,%rdi), %rbx
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rbx
movq %rbx, %rsi
shlq $0x5, %rsi
movq %rax, %rdi
callq 0x2350
movq %rbx, %rdi
movq 0x50(%rsp), %rbx
shrq %r15
movq %r15, (%rax,%r14)
movq %rbp, 0x8(%rax,%r14)
movl $0x0, 0x10(%rax,%r14)
movq $0x0, 0x18(%rax,%r14)
movq 0x48(%rsp), %rcx
movl (%rcx,%r12,4), %r15d
shrl %r15d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %r13d
setne %dl
cmpl %ecx, %r15d
setne %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xde7e
addl 0x70(%rsp), %r13d
movl %r15d, %esi
subl %r13d, %esi
movq %rdi, %r13
movq 0x30(%rsp), %rdi
movq %rax, %rbx
callq 0xe41f
movq %r13, %rdi
movq %rbx, %rax
movq 0x50(%rsp), %rbx
incq %r12
addq $0x20, %r14
movl %r15d, %r13d
cmpq %r12, 0x8(%rsp)
jne 0xddd4
addq %r12, %rbx
movq 0xb0(%rsp), %rbp
movq 0x58(%rsp), %r15
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %rsi
addq 0x8(%rsp), %r14
incq %rsi
cmpq 0x90(%rsp), %rsi
jne 0xdd7c
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe728
cmpq %rbx, %r14
jne 0xe400
testq %rbx, %rbx
je 0xe3d7
movsd %xmm0, 0x48(%rsp)
leaq 0x92b(%rip), %rcx       # 0xe820
movl $0x20, %edx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x20d0
movl $0x1, %r15d
movl $0x1, %esi
movq %rbx, %r13
cmpq $0x1, %rbx
je 0xdf50
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
leaq -0x1(%r13), %rbx
leaq 0x28(%rcx), %r14
cmpq %rax, (%r14)
jne 0xdf35
incl %esi
jmp 0xdf47
movq 0x28(%rsp), %rdi
callq 0xe41f
movq (%r14), %rax
movl $0x1, %esi
addq $0x20, %r14
decq %rbx
jne 0xdf2c
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0xe41f
leaq 0x18(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
movq %rbx, %rdi
callq 0xe728
movsd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xe83f
movq %rax, %rdi
leaq 0x84(%rsp), %rsi
movl $0x0, (%rsi)
movq 0x88(%rsp), %rax
leal -0x1(%rax), %edx
movq %rdi, 0x70(%rsp)
movl %edx, %r12d
callq 0xe8b7
movl %eax, 0x60(%rsp)
leaq 0xdfc(%rip), %rcx       # 0xedb2
movl $0x20, %edx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x20d0
cmpq $0x1, %r13
je 0xe00d
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
leaq -0x1(%r13), %rbx
leaq 0x20(%rcx), %r14
movl $0x1, %r15d
cmpq %rax, (%r14)
jne 0xdfee
incl %r15d
jmp 0xe004
movq 0x20(%rsp), %rdi
movl %r15d, %esi
callq 0xe41f
movq (%r14), %rax
movl $0x1, %r15d
addq $0x20, %r14
decq %rbx
jne 0xdfe4
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movl %r15d, %esi
callq 0xe41f
leaq 0x14(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %rbx, %rdi
movq %rdx, %r15
callq 0xe728
movsd %xmm0, 0x38(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xe83f
leaq 0x7c(%rsp), %rsi
movl $0x0, (%rsi)
movq %rax, 0x68(%rsp)
movq %rax, %rdi
movl %r12d, %edx
movl %r12d, 0x78(%rsp)
callq 0xe8b7
movl %eax, 0x58(%rsp)
leaq 0x12042(%rip), %rsi     # 0x200b5
leaq 0x12030(%rip), %r14     # 0x200aa
movq %rbp, %rdi
movq %r14, %rdx
movq %r13, 0x50(%rsp)
movq %r13, %rcx
xorl %eax, %eax
callq 0x2280
movq %r13, %xmm0
movq 0x11764(%rip), %xmm1    # 0x1f800
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x11768(%rip), %xmm2    # 0x1f810
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 0x11ff4(%rip), %rsi     # 0x200dd
movq %rbp, %rdi
movq %r14, %rdx
movapd %xmm3, %xmm0
movb $0x1, %al
callq 0x2280
leaq 0x12004(%rip), %rsi     # 0x20105
movq %rbp, %rdi
movq %r14, %rdx
movsd 0x48(%rsp), %xmm0
movb $0x1, %al
callq 0x2280
movl 0x18(%rsp), %ecx
movl 0x1c(%rsp), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd 0x11e93(%rip), %xmm0    # 0x1ffc0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x11fe8(%rip), %rsi     # 0x20127
movq %rbp, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x2280
leaq 0x1200c(%rip), %rsi     # 0x2015f
movq %rbp, %rdi
movq %r14, %rdx
movsd 0x40(%rsp), %xmm0
movb $0x1, %al
callq 0x2280
movl 0x84(%rsp), %r13d
leaq 0x1200c(%rip), %rsi     # 0x20181
movq %rbp, %rdi
movq %r14, %rdx
movl 0x60(%rsp), %r12d
movl %r12d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x2280
leaq 0x14(%rsp), %rax
movl (%rax), %ecx
movl (%r15), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd 0x11e19(%rip), %xmm0    # 0x1ffc0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x11ff1(%rip), %rsi     # 0x201aa
movq %rbp, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x2280
leaq 0x12015(%rip), %rsi     # 0x201e2
movq %rbp, %rdi
movq %r14, %rdx
movsd 0x38(%rsp), %xmm0
movb $0x1, %al
callq 0x2280
leaq 0x7c(%rsp), %rax
movl (%rax), %r15d
leaq 0x12015(%rip), %rsi     # 0x20204
movq %rbp, %rdi
movq %r14, %rdx
movl 0x58(%rsp), %r14d
movl %r14d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x2280
movq 0xa8(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rax, (%rcx)
movaps 0x90(%rsp), %xmm0
movsd %xmm0, 0x8(%rcx)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x10(%rcx)
movl 0x18(%rsp), %eax
movl %eax, 0x28(%rcx)
movl 0x1c(%rsp), %eax
movl %eax, 0x2c(%rcx)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x18(%rcx)
movl %r12d, 0x30(%rcx)
movl %r13d, 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 0x38(%rsp), %xmm0
movsd %xmm0, 0x20(%rcx)
movl %r14d, 0x40(%rcx)
movl %r15d, 0x44(%rcx)
cmpl $0x2, 0x88(%rsp)
jl 0xe2cc
leaq 0x11fa2(%rip), %rsi     # 0x2022d
movq 0x30(%rsp), %rdi
movq %rbp, %rdx
movl 0x78(%rsp), %r14d
movl %r14d, %ecx
callq 0xedd1
leaq 0x11f8b(%rip), %rsi     # 0x20232
movq 0x28(%rsp), %rdi
movq %rbp, %rdx
movl %r14d, %ecx
callq 0xedd1
leaq 0x11f79(%rip), %rsi     # 0x20237
movq %rbx, %rdi
movq %rbp, %rdx
movl %r14d, %ecx
callq 0xedd1
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0xe302
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rbx
movq %rbx, %rdi
callq 0x1d6fb
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1d6fb
movq %rbx, %rdi
movq 0x20(%rsp), %rbx
movq %r14, %rsi
callq 0x1d6fb
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0xe338
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rbx
movq %rbx, %rdi
callq 0x1d6fb
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1d6fb
movq %rbx, %rdi
movq 0x20(%rsp), %rbx
movq %r14, %rsi
callq 0x1d6fb
testq %rbx, %rbx
je 0xe367
movq 0x10(%rbx), %rsi
movq %rbx, %r14
movq 0x18(%rbx), %rbx
movq %rbx, %rdi
callq 0x1d6fb
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1d6fb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1d6fb
movq 0xa0(%rsp), %r14
testq %r14, %r14
je 0xe39b
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rbx
movq %rbx, %rdi
callq 0x1d6fb
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1d6fb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1d6fb
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
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe728
leaq 0x11caa(%rip), %rsi     # 0x20088
leaq 0x11cc5(%rip), %rdx     # 0x200aa
movq %rbp, %rdi
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
leaq 0x11c3f(%rip), %rdi     # 0x20046
leaq 0x11c48(%rip), %rsi     # 0x20056
leaq 0x11c4b(%rip), %rcx     # 0x20060
movl $0x38c, %edx            # imm = 0x38C
callq 0x21d0