// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movq %rdi, %r12
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0xb260
movq %rax, 0x20(%rsp)
leaq 0xc1c4(%rip), %rsi      # 0x2240f
movq %r12, %rdi
callq 0x56db
testq %rax, %rax
je 0x166c8
movq %rax, %rbx
movq %rax, %rdi
callq 0x508a
movq %rax, %rbp
testl %r14d, %r14d
je 0x162e9
cmpq $0x0, 0x18(%rsp)
je 0x162e9
movq 0x18(%rsp), %rax
movl (%rax), %r14d
cmpl %r14d, (%rbp)
jne 0x166c3
testq %r14, %r14
je 0x162e9
movq 0x8(%rbp), %r15
movq 0x10(%rbp), %rax
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
addq $0x10, %rcx
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
cmpl (%rcx), %esi
jne 0x166c3
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %r14
jne 0x162a5
movq 0x18(%rsp), %rax
movq 0x8(%rax), %r12
xorl %r13d, %r13d
movq (%r15,%r13,8), %rdi
movq (%r12), %rsi
callq 0x22a0
testl %eax, %eax
jne 0x166c3
incq %r13
addq $0x18, %r12
cmpq %r13, %r14
jne 0x162c8
movq 0x18(%rsp), %rax
movl (%rax), %edi
callq 0x16058
movq %rax, 0x28(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2290
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5286
testl %eax, %eax
movq %rbp, 0x48(%rsp)
js 0x165a1
movq %rbp, %r14
addq $0x8, %r14
movq 0x20(%rsp), %r15
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x50(%rsp)
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x8(%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 0x163dc
movzwl 0xc(%r13), %eax
testl $0xf04, %eax            # imm = 0xF04
jne 0x163c3
movq (%r14), %rax
movslq (%r13), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x11945
cmpl $-0x1, %eax
jne 0x16411
movq (%r14), %rax
movslq (%r13), %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
leaq 0x3c(%rsp), %rdx
callq 0x166d0
cmpl $0x0, 0x3c(%rsp)
jne 0x163fd
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5286
leaq 0x1(%r12), %rcx
testl %eax, %eax
jns 0x16362
jmp 0x165b7
movq 0x14c15(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xc2dd(%rip), %rsi      # 0x226ca
leaq 0xc294(%rip), %rdx      # 0x22688
xorl %eax, %eax
callq 0x22c0
jmp 0x1637e
movq %r14, %rdi
movq %r13, %rsi
movq 0x50(%rsp), %rdx
movl %eax, %ecx
callq 0x36ca
jmp 0x163c3
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rsi
movl %eax, %eax
leaq (%rax,%rax,2), %r8
leaq (%rsi,%r8,8), %r15
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
movq %rsi, 0x10(%rsp)
movq %r8, 0x40(%rsp)
jne 0x1646c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi,%r8,8)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x40(%rsp), %r8
movq 0x10(%rsp), %rsi
movq %rax, 0x10(%rsi,%r8,8)
movl 0x4(%r13), %eax
shlq $0x20, %rax
incq %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %rax, (%rdi,%rcx,8)
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x164c3
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi,%r8,8)
movq 0x8(%r15), %rsi
shlq $0x3, %rsi
callq 0x2390
movq 0x10(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx,8)
movq %r13, %rdi
callq 0x56a1
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 0x8(%rsp), %r15
ja 0x16528
movq %r15, 0x10(%rsp)
movq 0x20(%rsp), %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5286
movq 0x10(%rsp), %rcx
movq %rcx, 0x30(%rsp)
testl %eax, %eax
movq %rcx, %rax
jns 0x1635c
jmp 0x165bc
movq 0x28(%rsp), %rdi
callq 0x16118
movq %rax, %r8
movq %rax, 0x10(%rsp)
movq 0x14ab7(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xb46e(%rip), %rsi      # 0x219b9
leaq 0xc136(%rip), %rdx      # 0x22688
movq %r15, %rcx
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
shrq %r15
cmpq %r15, 0x10(%rsp)
movq 0x20(%rsp), %r15
jbe 0x16503
movq 0x8(%rsp), %rcx
addq %rcx, %rcx
movq 0x14a79(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xb46c(%rip), %rsi      # 0x219f5
leaq 0xc0f8(%rip), %rdx      # 0x22688
movq %rcx, 0x8(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0x16503
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x8(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
movq 0x20(%rsp), %r15
jmp 0x165bc
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rdi
movq %rax, %r14
callq 0x16118
movq 0x14a28(%rip), %rcx     # 0x2aff8
movq (%rcx), %rdi
leaq 0xb3df(%rip), %rsi      # 0x219b9
leaq 0xc0a7(%rip), %rdx      # 0x22688
movq %r15, %rbp
xorl %r15d, %r15d
movq %r14, %rcx
movq %rax, %r8
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq 0x149f8(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xc0e7(%rip), %rsi      # 0x226f1
leaq 0xc077(%rip), %rdx      # 0x22688
movq %r12, %rcx
xorl %eax, %eax
callq 0x22c0
cmpl $0x0, (%rbp)
movq 0x48(%rsp), %r12
je 0x1665e
xorl %r14d, %r14d
movq 0x10(%rbp), %rax
movl %r14d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r15d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x1664f
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %rdi
callq 0x2060
incq %r14
movl (%rbp), %eax
addl $0x2, %r15d
cmpq %rax, %r14
jb 0x16629
movq 0x18(%rbp), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x10(%rbp), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x20(%rbp), %rsi
xorl %edi, %edi
callq 0xb302
xorl %edi, %edi
movq %rbp, %rsi
callq 0xb302
testq %r13, %r13
je 0x1669f
movq 0x30(%r13), %rdi
callq 0x2060
movq %r13, %rdi
callq 0x2060
movq %r12, %rdi
callq 0x501d
movq %rbx, %rdi
callq 0x5762
movq 0x28(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x36a2
movq %r12, %rdi
callq 0x3723