// File: /workspace/github_c_makefile_star2_O1/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
movl $0x1, %esi
movl $0x28, %edx
xorl %edi, %edi
callq 0xb260
movq %rax, %rbx
movq %r15, %rdi
callq 0x4d17
testq %rax, %rax
je 0x16c61
movq %rax, %r14
movl (%r12), %edi
callq 0x16058
movq %rax, 0x10(%rsp)
movq %rbx, %rax
addq $0x18, %rax
movq %rax, 0x38(%rsp)
movl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %r13
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r15, (%rsp)
movq %r14, %rdi
callq 0x4e4f
testq %rax, %rax
je 0x16b87
movq %rax, %r15
movq %rax, %rdi
callq 0x4cf4
testl %eax, %eax
jne 0x16940
incq %rbp
movq %rbp, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %rcx
movq %rbp, %rax
movabsq $0x790fb65668c26139, %rdx # imm = 0x790FB65668C26139
imulq %rdx, %rax
rorq $0x6, %rax
movabsq $0x10c6f7a0b5ed, %rdx   # imm = 0x10C6F7A0B5ED
cmpq %rdx, %rax
jbe 0x169f7
movq %r15, %rdi
leaq 0xbd88(%rip), %rsi      # 0x2272c
movq %r13, %rdx
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x2350
movq %r12, %rdi
movq %r13, %rsi
callq 0x11945
cmpl $-0x1, %eax
jne 0x16a1f
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x1c(%rsp), %rdx
callq 0x166d0
cmpl $0x0, 0x1c(%rsp)
je 0x16940
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movl %eax, %edx
callq 0x374e
jmp 0x16940
shrq $0x12, %rcx
movq 0x145f6(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xbcbe(%rip), %rsi      # 0x226ca
leaq 0xbd0e(%rip), %rdx      # 0x22721
xorl %eax, %eax
callq 0x22c0
jmp 0x1699a
movq 0x10(%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, 0x30(%rsp)
movq %r8, 0x28(%rsp)
jne 0x16a7a
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 0x28(%rsp), %r8
movq 0x30(%rsp), %rsi
movq %rax, 0x10(%rsi,%r8,8)
movl 0x24(%rsp), %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 0x16ad1
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 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx,8)
movl 0x20(%rsp), %eax
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 (%rsp), %rax
addq $0x2, %rax
movq %rax, (%rsp)
cmpq 0x8(%rsp), %rax
jbe 0x16940
movq 0x10(%rsp), %rdi
callq 0x16118
movq %rax, %r15
movq 0x144da(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xae91(%rip), %rsi      # 0x219b9
leaq 0xbbf2(%rip), %rdx      # 0x22721
movq (%rsp), %rcx
movq %r15, %r8
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq (%rsp), %rax
shrq %rax
cmpq %rax, %r15
movq %r15, (%rsp)
jbe 0x16940
movq 0x8(%rsp), %rcx
addq %rcx, %rcx
movq 0x14493(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0xae86(%rip), %rsi      # 0x219f5
leaq 0xbbab(%rip), %rdx      # 0x22721
movq %rcx, 0x8(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0x1693c
movq 0x10(%rsp), %rdi
callq 0x16118
movq 0x14460(%rip), %r13     # 0x2aff8
movq (%r13), %rdi
leaq 0xae16(%rip), %rsi      # 0x219b9
leaq 0xbb77(%rip), %r15      # 0x22721
xorl %r12d, %r12d
movq %r15, %rdx
movq (%rsp), %rcx
movq %rax, %r8
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x22c0
movq (%r13), %rdi
leaq 0xbb73(%rip), %rsi      # 0x22741
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22c0
cmpl $0x0, (%rbx)
je 0x16c17
xorl %r15d, %r15d
movq 0x10(%rbx), %rax
movl %r15d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r12d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x16c09
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x2060
incq %r15
movl (%rbx), %eax
addl $0x2, %r12d
cmpq %rax, %r15
jb 0x16be3
movq 0x18(%rbx), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x10(%rbx), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x20(%rbx), %rsi
xorl %edi, %edi
callq 0xb302
xorl %edi, %edi
movq %rbx, %rsi
callq 0xb302
movq %r14, %rdi
callq 0x4ded
movq 0x10(%rsp), %rax
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x378f