// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/cov.c
// Function: bed_cstats
cov_t *bed_cstats(const char *bed, sdict_t *sdict)
{
    uint32_t i, s, e;
    uint64_t n, m, max_m, n_recs;
    char *line, cname[4096];
    iostream_t *fp;
    cov_t *covs;
    
    khash_t(str) *hmseq; // for absent sequences
    khint_t k;
    int absent;
    hmseq = kh_init(str);

    fp = iostream_open(bed);
    if (fp == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, bed);
        exit(EXIT_FAILURE);
    }
    
    covs = cov_init(sdict->n);
    max_m = 0x7FFFFFFULL; // 128MB - ~1GB mem for covs
    n_recs = 0;
    n = 0;
    line = 0;
    while ((line = iostream_getline(fp)) != NULL) {
        if (is_empty_line(line))
            continue;
        ++n_recs;
        if (n_recs % 1000000 == 0)
            fprintf(stderr, "[I::%s] %lu million records processed\n", __func__, n_recs / 1000000);
        sscanf(line, "%s %u %u %*s %*s %*s", cname, &s, &e);
        i = sd_get(sdict, cname);
        if (i == UINT32_MAX) {
            k = kh_put(str, hmseq, cname, &absent);
            if (absent) {
                kh_key(hmseq, k) = strdup(cname);
                fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname);
            }
            continue;
        }
        // BED file coordinate is 0-based
        kv_push(uint64_t, covs->p[i], (uint64_t) s << 32 | (uint32_t) 1);
        kv_push(uint64_t, covs->p[i], (uint64_t) e << 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 BED 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);

    iostream_close(fp);

    return covs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1048, %rsp           # imm = 0x1048
movq %rsi, %r12
movq %rdi, %r15
callq 0x1283c
movq %rax, %rbx
movq %r15, %rdi
callq 0x2a3e
testq %rax, %rax
je 0x12dc0
movq %rax, %r14
movl (%r12), %edi
callq 0x121fc
movq %rax, 0x18(%rsp)
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x10(%rsp)
leaq 0x40(%rsp), %r15
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r13, 0x8(%rsp)
movq %r14, %rdi
callq 0x2b6c
testq %rax, %rax
je 0x12d0a
movq %rax, %r13
movq %rax, %rdi
callq 0x2a1b
testl %eax, %eax
jne 0x12abf
incq %rbp
movq %rbp, %rax
xorl %edx, %edx
movl $0xf4240, %ecx          # imm = 0xF4240
divq %rcx
testq %rdx, %rdx
je 0x12b7a
movq %r13, %rdi
leaq 0xcb22(%rip), %rsi      # 0x1f624
movq %r15, %rdx
leaq 0x34(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x2350
movq %r12, %rdi
movq %r15, %rsi
callq 0xe65a
cmpl $-0x1, %eax
jne 0x12ba1
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x2c(%rsp), %rdx
callq 0x12849
cmpl $0x0, 0x2c(%rsp)
je 0x12abf
movl %eax, %r13d
movq %r15, %rdi
callq 0x2510
movq 0x18(%rbx), %rcx
movl %r13d, %edx
movq %rax, (%rcx,%rdx,8)
movq 0x1549e(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xbe23(%rip), %rsi      # 0x1e987
leaq 0xcaae(%rip), %rdx      # 0x1f619
movq %r15, %rcx
xorl %eax, %eax
callq 0x22c0
jmp 0x12abf
movq 0x15477(%rip), %rcx     # 0x27ff8
movq (%rcx), %rdi
leaq 0xca37(%rip), %rsi      # 0x1f5c2
leaq 0xca87(%rip), %rdx      # 0x1f619
movq %rax, %rcx
xorl %eax, %eax
callq 0x22c0
jmp 0x12af8
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %r8
movl %eax, %eax
imulq $0x18, %rax, %r13
leaq (%r8,%r13), %rdi
movq (%r8,%r13), %rcx
cmpq 0x8(%r8,%r13), %rcx
movq %rdi, 0x20(%rsp)
movq %r8, 0x38(%rsp)
jne 0x12c02
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8,%r13)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x38(%rsp), %r8
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%r8,%r13)
movq (%rdi), %rcx
jmp 0x12c06
movq 0x10(%rdi), %rax
movl 0x34(%rsp), %edx
shlq $0x20, %rdx
incq %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
movq %rdx, (%rax,%rcx,8)
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x12c56
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8,%r13)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2390
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rcx
movq %rax, 0x10(%rcx,%r13)
movq (%rdi), %rcx
movl 0x30(%rsp), %edx
shlq $0x20, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rsi, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
movq %rdx, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x8(%rsp)
cmpq 0x10(%rsp), %rax
jbe 0x12abf
movq 0x18(%rsp), %rdi
callq 0x122af
movq %rax, %r13
movq 0x1535a(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xbd01(%rip), %rsi      # 0x1e9a9
leaq 0xc96a(%rip), %rdx      # 0x1f619
movq 0x8(%rsp), %rcx
movq %r13, %r8
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq 0x8(%rsp), %rax
shrq %rax
cmpq %rax, %r13
movq %r13, 0x8(%rsp)
jbe 0x12abf
movq 0x10(%rsp), %rcx
addq %rcx, %rcx
movq 0x15310(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xbcf3(%rip), %rsi      # 0x1e9e5
leaq 0xc920(%rip), %rdx      # 0x1f619
movq %rcx, 0x10(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0x12aba
movq 0x18(%rsp), %rdi
callq 0x122af
movq 0x152dd(%rip), %r13     # 0x27ff8
movq (%r13), %rdi
leaq 0xbc83(%rip), %rsi      # 0x1e9a9
leaq 0xc8ec(%rip), %r15      # 0x1f619
xorl %r12d, %r12d
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq %rax, %r8
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq (%r13), %rdi
leaq 0xc8e7(%rip), %rsi      # 0x1f639
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22c0
xorl %r15d, %r15d
movl (%rbx), %eax
cmpq %rax, %r15
jae 0x12d99
movq 0x10(%rbx), %rax
movq %r15, %rcx
shrq $0x4, %rcx
movl (%rax,%rcx,4), %eax
movl %r12d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x12d90
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x2060
incq %r15
addl $0x2, %r12d
jmp 0x12d62
movq %rbx, %rdi
callq 0x12a2f
movq %r14, %rdi
callq 0x2b0a
movq 0x18(%rsp), %rax
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x15231(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0xb41f(%rip), %rsi      # 0x1e1f0
leaq 0xc841(%rip), %rdx      # 0x1f619
movq %r15, %rcx
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0