// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
callq 0x1d8d0
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x150a7(%rip), %rsi     # 0x32311
callq 0x41e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x1d2a7
movq 0x1bd77(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rcx
leaq 0x13f41(%rip), %rsi     # 0x311d0
leaq 0x152f2(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movl $0x1, %edi
callq 0x24c0
movq -0x48(%rbp), %rdi
callq 0x3630
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x1d346
movq -0x58(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1d344
movl -0x74(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x1d31b
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1d8f0
cmpl $0x0, %eax
jne 0x1d31b
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1d960
cmpl $0x0, %eax
je 0x1d344
movq 0x1bcd6(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x15267(%rip), %rsi     # 0x32593
leaq 0x15255(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movl $0x1, %edi
callq 0x24c0
jmp 0x1d346
movq -0x10(%rbp), %rax
movl (%rax), %edi
callq 0x1cee0
movq %rax, -0x60(%rbp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2290
movq %rax, -0x50(%rbp)
movq $0x7ffffff, -0x38(%rbp) # imm = 0x7FFFFFF
movq $0x0, -0x40(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x3970
cmpl $0x0, %eax
jl 0x1d7ca
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x1d3e6
movq 0x1bc3b(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
leaq 0x151f2(%rip), %rsi     # 0x325ca
leaq 0x151a9(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movzwl 0x1c00b(%rip), %ecx     # 0x39410
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1d411
jmp 0x1d380
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x16e10
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x1d4c4
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x70(%rbp), %rdx
callq 0x1d9e0
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x70(%rbp)
je 0x1d4bf
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2520
movq %rax, %rdx
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x6c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq 0x1bb64(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0x144be(%rip), %rsi     # 0x3196f
leaq 0x150d0(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
jmp 0x1d380
jmp 0x1d4c6
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x18(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0x1d5ab
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1d532
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x1d540
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x1d540
movq -0x90(%rbp), %rcx
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x23a0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movslq 0x4(%rax), %rdx
shlq $0x20, %rdx
orq $0x1, %rdx
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x1d5f6
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x18(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0x1d6db
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1d662
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0x1d670
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0x1d670
movq -0x98(%rbp), %rcx
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x23a0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rdi
callq 0x4140
movslq %eax, %rdx
shlq $0x20, %rdx
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
orq %rax, %rdx
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl -0x18(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jbe 0x1d7c5
movq -0x60(%rbp), %rdi
callq 0x1d050
movq %rax, -0x30(%rbp)
movq 0x1b89d(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x14220(%rip), %rsi     # 0x31991
leaq 0x14e10(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
jbe 0x1d7bd
movq -0x38(%rbp), %rax
shlq %rax
movq %rax, -0x38(%rbp)
movq 0x1b857(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x38(%rbp), %rcx
leaq 0x1421e(%rip), %rsi     # 0x319cd
leaq 0x14dd2(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x1d380
movq -0x60(%rbp), %rdi
callq 0x1d050
movq %rax, -0x30(%rbp)
movq 0x1b81a(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x1419d(%rip), %rsi     # 0x31991
leaq 0x14d8d(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movq 0x1b7ef(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x40(%rbp), %rcx
leaq 0x14dda(%rip), %rsi     # 0x325f1
leaq 0x14d6a(%rip), %rdx     # 0x32588
movb $0x0, %al
callq 0x22c0
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x68(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x1d87d
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x6c(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x6c(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x1d870
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x2050
jmp 0x1d872
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1d82c
movq -0x68(%rbp), %rdi
callq 0x1dd40
cmpq $0x0, -0x50(%rbp)
je 0x1d8a3
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x2050
movq -0x50(%rbp), %rdi
callq 0x2050
jmp 0x1d8a5
movq -0x58(%rbp), %rdi
callq 0x3570
movq -0x48(%rbp), %rdi
callq 0x4350
movq -0x60(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)