// File: /workspace/github_c_makefile_star2_O3/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 0xbf84
movq %rax, %rbx
movq %r15, %rdi
callq 0x4c27
testq %rax, %rax
je 0x17b2f
movq %rax, %r14
movl (%r12), %edi
callq 0x16f08
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 0x4d57
testq %rax, %rax
je 0x17a50
movq %rax, %r15
movq %rax, %rdi
callq 0x4c04
testl %eax, %eax
jne 0x17810
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 0x178c7
movq %r15, %rdi
leaq 0xbee8(%rip), %rsi      # 0x2375c
movq %r13, %rdx
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x2350
movq %r12, %rdi
movq %r13, %rsi
callq 0x12707
cmpl $-0x1, %eax
jne 0x178ef
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x1c(%rsp), %rdx
callq 0x1758a
cmpl $0x0, 0x1c(%rsp)
je 0x17810
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movl %eax, %edx
callq 0x36d8
jmp 0x17810
shrq $0x12, %rcx
movq 0x14726(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xbe1e(%rip), %rsi      # 0x236fa
leaq 0xbe6e(%rip), %rdx      # 0x23751
xorl %eax, %eax
callq 0x22c0
jmp 0x1786a
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rdi
movl %eax, %eax
leaq (%rax,%rax,2), %r8
leaq (%rdi,%r8,8), %r15
movq (%r15), %rcx
cmpq 0x8(%r15), %rcx
movq %rdi, 0x30(%rsp)
movq %r8, 0x28(%rsp)
jne 0x1794b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi,%r8,8)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2390
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdi
movq %rax, 0x10(%rdi,%r8,8)
movq (%r15), %rcx
jmp 0x1794f
movq 0x10(%r15), %rax
movl 0x24(%rsp), %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 0x179a1
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi,%r8,8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2390
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx,8)
movq (%r15), %rcx
movl 0x20(%rsp), %edx
shlq $0x20, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rsi, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rdx, (%rax,%rcx,8)
movq (%rsp), %rax
addq $0x2, %rax
movq %rax, (%rsp)
cmpq 0x8(%rsp), %rax
jbe 0x17810
movq 0x10(%rsp), %rdi
callq 0x16fc1
movq %rax, %r15
movq 0x14611(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xb000(%rip), %rsi      # 0x229f1
leaq 0xbd59(%rip), %rdx      # 0x23751
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 0x17810
movq 0x8(%rsp), %rcx
addq %rcx, %rcx
movq 0x145ca(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0xaff5(%rip), %rsi      # 0x22a2d
leaq 0xbd12(%rip), %rdx      # 0x23751
movq %rcx, 0x8(%rsp)
xorl %eax, %eax
callq 0x22c0
jmp 0x1780c
movq 0x10(%rsp), %rdi
callq 0x16fc1
movq 0x14597(%rip), %r13     # 0x2bff8
movq (%r13), %rdi
leaq 0xaf85(%rip), %rsi      # 0x229f1
leaq 0xbcde(%rip), %r15      # 0x23751
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 0xbcda(%rip), %rsi      # 0x23771
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22c0
movl (%rbx), %eax
testl %eax, %eax
je 0x17ae5
xorl %r15d, %r15d
movq 0x10(%rbx), %rcx
movq %r15, %rdx
shrq $0x4, %rdx
movl (%rcx,%rdx,4), %edx
movl %r12d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x17ad7
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x2060
movl (%rbx), %eax
incq %r15
movl %eax, %ecx
addl $0x2, %r12d
cmpq %rcx, %r15
jb 0x17aad
movq 0x18(%rbx), %rsi
xorl %edi, %edi
callq 0xc01f
movq 0x10(%rbx), %rsi
xorl %edi, %edi
callq 0xc01f
movq 0x20(%rbx), %rsi
xorl %edi, %edi
callq 0xc01f
xorl %edi, %edi
movq %rbx, %rsi
callq 0xc01f
movq %r14, %rdi
callq 0x4cf5
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 0x3719