// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r14
callq 0x1283c
movq %rax, %r15
leaq 0xcf54(%rip), %rsi      # 0x1f311
movq %r14, %rdi
callq 0x339b
testq %rax, %rax
je 0x12818
movq %rax, %rbx
movq %r15, 0x28(%rsp)
movq %rax, %rdi
callq 0x2d85
testl %ebp, %ebp
movq %rax, 0x18(%rsp)
jne 0x12783
movl (%r12), %r13d
movl %r13d, %edi
callq 0x121fc
movq %rax, 0x20(%rsp)
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x2290
movq %rax, %rbp
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x10(%rsp)
movl $0xf4240, %r14d         # imm = 0xF4240
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r15, 0x40(%rsp)
incq %r13
movq %r13, %rax
xorl %edx, %edx
divq %r14
movq %rax, %r15
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2fb4
testl %eax, %eax
js 0x126a4
imulq $0xf4240, %r15, %rcx    # imm = 0xF4240
movq %r13, %rax
xorl %edx, %edx
divq %r14
cmpq %r13, %rcx
je 0x12514
movzwl 0xc(%rbp), %eax
testw $0xf04, %ax             # imm = 0xF04
jne 0x12425
movq 0x18(%rsp), %r15
movq 0x8(%r15), %rax
movslq (%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq %r12, %rdi
callq 0xe65a
cmpl $-0x1, %eax
jne 0x1253b
movq 0x8(%r15), %rax
movslq (%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x28(%rsp), %rdi
leaq 0x34(%rsp), %rdx
callq 0x12849
cmpl $0x0, 0x34(%rsp)
je 0x12425
movl %eax, %r15d
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movslq (%rbp), %rcx
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx,8), %rdi
callq 0x2510
movq 0x28(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl %r15d, %edx
movq %rax, (%rcx,%rdx,8)
movq 0x15b13(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0xc486(%rip), %rsi      # 0x1e987
leaq 0xd078(%rip), %rdx      # 0x1f580
xorl %eax, %eax
callq 0x22c0
jmp 0x12425
movq 0x15add(%rip), %rcx     # 0x27ff8
movq (%rcx), %rdi
leaq 0xd09d(%rip), %rsi      # 0x1f5c2
leaq 0xd054(%rip), %rdx      # 0x1f580
movq %rax, %rcx
xorl %eax, %eax
callq 0x22c0
jmp 0x1245e
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rdi
movl %eax, %eax
imulq $0x18, %rax, %r8
leaq (%rdi,%r8), %r15
movq (%rdi,%r8), %rcx
cmpq 0x8(%rdi,%r8), %rcx
movq %rdi, 0x8(%rsp)
movq %r8, 0x38(%rsp)
jne 0x12597
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi,%r8)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x38(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi,%r8)
movq (%r15), %rcx
jmp 0x1259b
movq 0x10(%r15), %rax
movl 0x4(%rbp), %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 0x125e7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi,%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2390
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx)
movq %rbp, %rdi
callq 0x3362
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 0x40(%rsp), %r15
addq $0x2, %r15
cmpq 0x10(%rsp), %r15
jbe 0x12420
movq 0x20(%rsp), %rdi
callq 0x122af
movq %rax, %r8
movq %rax, 0x8(%rsp)
movq 0x159be(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xc365(%rip), %rsi      # 0x1e9a9
leaq 0xcf35(%rip), %rdx      # 0x1f580
movq %r15, %rcx
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
shrq %r15
movq 0x8(%rsp), %rax
cmpq %r15, %rax
movq %rax, %r15
jbe 0x12420
movq 0x10(%rsp), %rcx
addq %rcx, %rcx
movq 0x1597b(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xc35e(%rip), %rsi      # 0x1e9e5
leaq 0xcef2(%rip), %rdx      # 0x1f580
movq %rcx, 0x10(%rsp)
xorl %eax, %eax
callq 0x22c0
movq 0x8(%rsp), %r15
jmp 0x12420
movq 0x20(%rsp), %rdi
callq 0x122af
movq 0x15943(%rip), %r12     # 0x27ff8
movq (%r12), %rdi
leaq 0xc2e9(%rip), %rsi      # 0x1e9a9
leaq 0xceb9(%rip), %r15      # 0x1f580
xorl %r14d, %r14d
movq %r15, %rdx
movq 0x40(%rsp), %rcx
movq %rax, %r8
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq (%r12), %rdi
decq %r13
leaq 0xcefa(%rip), %rsi      # 0x1f5e9
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x22c0
xorl %r15d, %r15d
movq 0x28(%rsp), %r12
movl (%r12), %eax
cmpq %rax, %r15
jae 0x1273f
movq 0x10(%r12), %rax
movq %r15, %rcx
shrq $0x4, %rcx
movl (%rax,%rcx,4), %eax
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x12736
movq 0x18(%r12), %rax
movq (%rax,%r15,8), %rdi
callq 0x2060
incq %r15
addl $0x2, %r14d
jmp 0x12704
movq %r12, %rdi
callq 0x12a2f
testq %rbp, %rbp
je 0x1275d
movq 0x30(%rbp), %rdi
callq 0x2060
movq %rbp, %rdi
callq 0x2060
movq 0x18(%rsp), %rdi
callq 0x2d2a
movq %rbx, %rdi
callq 0x3496
movq 0x20(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r12), %r13d
cmpl %r13d, (%rax)
jne 0x127f1
movq 0x18(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x8(%rcx), %r14
imulq $0x6, %r13, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x127be
movl (%rax), %esi
movq 0x8(%r12), %rdi
leaq 0x6(%rdx), %r8
addq $0x4, %rax
cmpl 0x10(%rdi,%rdx,4), %esi
movq %r8, %rdx
je 0x1279f
jmp 0x127f1
leaq (,%r13,2), %r15
addq %r13, %r15
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x123ef
movq (%r14), %rdi
movq 0x8(%r12), %rax
movq (%rax,%rbp,8), %rsi
callq 0x22a0
addq $0x3, %rbp
addq $0x8, %r14
testl %eax, %eax
je 0x127cb
movq 0x15800(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xcd89(%rip), %rsi      # 0x1f58b
leaq 0xcd77(%rip), %rdx      # 0x1f580
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0
movq 0x157d9(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xb9c7(%rip), %rsi      # 0x1e1f0
leaq 0xcd50(%rip), %rdx      # 0x1f580
movq %r14, %rcx
xorl %eax, %eax
callq 0x22c0
jmp 0x12810