// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/cov.c
// Function: bam_cstats
cov_t *bam_cstats(const char *bam, sdict_t *sdict, int match_header)
{
    uint32_t i, flag;
    uint64_t n, m, max_m, n_recs;
    bamFile fp;
    bam1_t *b1;
    bam_header_t *h;
    cov_t *covs;
    
    khash_t(str) *hmseq; // for absent sequences
    khint_t k;
    int absent;
    hmseq = kh_init(str);

    fp = bam_open(bam, "r");
    if (fp == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, bam);
        exit(EXIT_FAILURE);
    }

    h = bam_header_read(fp);
    if (match_header) {
        int n_targets;
        char **target_name;
        uint32_t *target_len;

        n_targets = h->n_targets;
        target_len = h->target_len;
        target_name = h->target_name;

        // check consistency between sdict and bam header
        if (sdict && (n_targets != sdict->n || 
                    diff_target_length(target_len, sdict) || 
                    diff_target_name(target_name, sdict))) {
            fprintf(stderr, "[E::%s] sequence dictionary does not match BAM header\n", __func__);
            exit(EXIT_FAILURE);
        }
    }

    covs = cov_init(sdict->n);
    b1 = bam_init1();
    max_m = 0x7FFFFFFULL; // 128MB - ~1GB mem for covs
    n_recs = 0;
    n = 0;
    while (bam_read1(fp, b1) >= 0) {
        ++n_recs;
        if (n_recs % 1000000 == 0)
            fprintf(stderr, "[I::%s] %lu million records processed\n", __func__, n_recs / 1000000);
        flag = b1->core.flag;
        if (flag & mask) continue;
        i = sd_get(sdict, h->target_name[b1->core.tid]);
        if (i == UINT32_MAX) {
            k = kh_put(str, hmseq, h->target_name[b1->core.tid], &absent);
            if (absent) {
                kh_key(hmseq, k) = strdup(h->target_name[b1->core.tid]);
                fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, h->target_name[b1->core.tid]);
            }
            continue;
        }
        // BAM file coordinate is 0-based
        kv_push(uint64_t, covs->p[i], (uint64_t) b1->core.pos << 32 | (uint32_t) 1);
        kv_push(uint64_t, covs->p[i], (uint64_t) get_target_end(b1) << 32 | (uint32_t) -1);
        n += 2;
        if (n > max_m) {
            m = pos_compression(covs); // m is the position size after compression
            fprintf(stderr, "[I::%s] position compression n = %lu, m = %lu, max_m = %lu\n", __func__, n, m, max_m);
            if (m > n>>1) {
                max_m <<= 1; // increase m limit if compression ratio smaller than 0.5
                fprintf(stderr, "[I::%s] position memory buffer expanded max_m = %lu\n", __func__, max_m);
            }
            n = m;
        }
    }
    m = pos_compression(covs);
    fprintf(stderr, "[I::%s] position compression n = %lu, m = %lu, max_m = %lu\n", __func__, n, m, max_m);
    
    fprintf(stderr, "[I::%s] total number BAM records processed %lu\n", __func__, n_recs);
    
    for (k = 0; k < kh_end(hmseq); ++k)
        if (kh_exist(hmseq, k))
            free((char *) kh_key(hmseq, k));
    kh_destroy(str, hmseq);

    bam_destroy1(b1);
    bam_header_destroy(h);
    bam_close(fp);

    return covs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %r14d
movq %rsi, 0x28(%rsp)
movq %rdi, %r12
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0xbf84
movq %rax, %r15
leaq 0xc351(%rip), %rsi      # 0x23449
movq %r12, %rdi
callq 0x55c3
testq %rax, %rax
je 0x17582
movq %rax, %rbx
movq %rax, %rdi
callq 0x4f95
movq %rax, %rbp
testl %r14d, %r14d
movq %r15, 0x8(%rsp)
jne 0x1712a
movq 0x28(%rsp), %rax
movl (%rax), %edi
jmp 0x171a3
movq 0x28(%rsp), %rax
movl (%rax), %r14d
cmpl %r14d, (%rbp)
jne 0x1757d
xorl %edi, %edi
testl %r14d, %r14d
movq 0x8(%rsp), %r15
je 0x171a3
movq 0x10(%rbp), %rax
movq 0x8(%rbp), %r15
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %r12
leaq 0x10(%r12), %rcx
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
cmpl (%rcx), %esi
jne 0x1757d
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %r14
jne 0x17160
xorl %r13d, %r13d
movq (%r15,%r13,8), %rdi
movq (%r12), %rsi
callq 0x22a0
testl %eax, %eax
jne 0x1757d
incq %r13
addq $0x18, %r12
cmpq %r13, %r14
jne 0x1717a
movl %r14d, %edi
movq 0x8(%rsp), %r15
callq 0x16f08
movq %rax, 0x20(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2290
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5195
testl %eax, %eax
movq %rbp, 0x48(%rsp)
js 0x1744c
movq %rbp, %r14
addq $0x8, %r14
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x50(%rsp)
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x10(%rsp)
movabsq $0x431bde82d7b634db, %rbp # imm = 0x431BDE82D7B634DB
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
incq %r12
movq %r12, %rcx
movq %rcx, %r12
movq %rcx, %rax
mulq %rbp
movq %rdx, %rcx
shrq $0x12, %rcx
imulq $0xf4240, %rcx, %rax    # imm = 0xF4240
cmpq %r12, %rax
je 0x1728a
movzwl 0xc(%r13), %eax
testl $0xf04, %eax            # imm = 0xF04
jne 0x17271
movq (%r14), %rax
movslq (%r13), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x28(%rsp), %rdi
callq 0x12707
cmpl $-0x1, %eax
jne 0x172bf
movq (%r14), %rax
movslq (%r13), %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
leaq 0x3c(%rsp), %rdx
callq 0x1758a
cmpl $0x0, 0x3c(%rsp)
jne 0x172ab
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5195
leaq 0x1(%r12), %rcx
testl %eax, %eax
jns 0x17210
jmp 0x1745d
movq 0x14d67(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xc45f(%rip), %rsi      # 0x236fa
leaq 0xc416(%rip), %rdx      # 0x236b8
xorl %eax, %eax
callq 0x22c0
jmp 0x1722c
movq %r14, %rdi
movq %r13, %rsi
movq 0x50(%rsp), %rdx
movl %eax, %ecx
callq 0x3654
jmp 0x17271
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rdi
movl %eax, %eax
leaq (%rax,%rax,2), %r8
leaq (%rdi,%r8,8), %r15
movq (%r15), %rcx
cmpq 0x8(%r15), %rcx
movq %rdi, 0x18(%rsp)
movq %r8, 0x40(%rsp)
jne 0x1731b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi,%r8,8)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x40(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%rdi,%r8,8)
movq (%r15), %rcx
jmp 0x1731f
movq 0x10(%r15), %rax
movl 0x4(%r13), %edx
shlq $0x20, %rdx
incq %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rdx, (%rax,%rcx,8)
movq (%r15), %rcx
cmpq 0x8(%r15), %rcx
jne 0x1736e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi,%r8,8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2390
movq 0x18(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx,8)
movq %r13, %rdi
callq 0x5589
shlq $0x20, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
orq %rcx, %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rax, (%rdx,%rcx,8)
movq 0x30(%rsp), %r15
addq $0x2, %r15
cmpq 0x10(%rsp), %r15
ja 0x173d3
movq %r15, 0x18(%rsp)
movq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5195
movq 0x18(%rsp), %rcx
movq %rcx, 0x30(%rsp)
testl %eax, %eax
movq %rcx, %rax
jns 0x1720a
jmp 0x17462
movq 0x20(%rsp), %rdi
callq 0x16fc1
movq %rax, %r8
movq %rax, 0x18(%rsp)
movq 0x14c0c(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xb5fb(%rip), %rsi      # 0x229f1
leaq 0xc2bb(%rip), %rdx      # 0x236b8
movq %r15, %rcx
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
shrq %r15
cmpq %r15, 0x18(%rsp)
movq 0x8(%rsp), %r15
jbe 0x173ae
movq 0x10(%rsp), %rcx
addq %rcx, %rcx
movq 0x14bce(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xb5f9(%rip), %rsi      # 0x22a2d
leaq 0xc27d(%rip), %rdx      # 0x236b8
movq %rcx, 0x10(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0x173ae
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x10(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
jmp 0x17462
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rdi
movq %rax, %rbp
callq 0x16fc1
movq 0x14b82(%rip), %rcx     # 0x2bff8
movq (%rcx), %rdi
leaq 0xb571(%rip), %rsi      # 0x229f1
leaq 0xc231(%rip), %rdx      # 0x236b8
movq %r15, %r14
xorl %r15d, %r15d
movq %rbp, %rcx
movq %rax, %r8
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq 0x14b52(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xc271(%rip), %rsi      # 0x23721
leaq 0xc201(%rip), %rdx      # 0x236b8
movq %r12, %rcx
xorl %eax, %eax
callq 0x22c0
movl (%r14), %eax
testl %eax, %eax
movq 0x48(%rsp), %r12
je 0x17513
xorl %r14d, %r14d
movq 0x8(%rsp), %rbp
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdx
shrq $0x4, %rdx
movl (%rcx,%rdx,4), %edx
movl %r15d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x17505
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %rdi
callq 0x2060
movl (%rbp), %eax
incq %r14
movl %eax, %ecx
addl $0x2, %r15d
cmpq %rcx, %r14
jb 0x174d5
movq 0x8(%rsp), %r14
movq 0x18(%r14), %rsi
xorl %edi, %edi
callq 0xc01f
movq 0x10(%r14), %rsi
xorl %edi, %edi
callq 0xc01f
movq 0x20(%r14), %rsi
xorl %edi, %edi
callq 0xc01f
xorl %edi, %edi
movq %r14, %rsi
callq 0xc01f
testq %r13, %r13
je 0x17559
movq 0x30(%r13), %rdi
callq 0x2060
movq %r13, %rdi
callq 0x2060
movq %r12, %rdi
callq 0x4f25
movq %rbx, %rdi
callq 0x5640
movq 0x20(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x362c
movq %r12, %rdi
callq 0x36ad