// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1080, %rsp           # imm = 0x1080
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x1d8d0
movq %rax, -0x1068(%rbp)
movq -0x8(%rbp), %rdi
callq 0x2d80
movq %rax, -0x1058(%rbp)
cmpq $0x0, -0x1058(%rbp)
jne 0x1de06
movq 0x1b218(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rcx
leaq 0x133e2(%rip), %rsi     # 0x311d0
leaq 0x1482c(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
movl $0x1, %edi
callq 0x24c0
movq -0x10(%rbp), %rax
movl (%rax), %edi
callq 0x1cee0
movq %rax, -0x1060(%rbp)
movq $0x7ffffff, -0x38(%rbp) # imm = 0x7FFFFFF
movq $0x0, -0x40(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x1058(%rbp), %rdi
callq 0x3000
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x1e2a3
movq -0x48(%rbp), %rdi
callq 0x2d20
cmpl $0x0, %eax
je 0x1de62
jmp 0x1de38
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 0x1deb2
movq 0x1b16f(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
leaq 0x14726(%rip), %rsi     # 0x325ca
leaq 0x14776(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
movq -0x48(%rbp), %rdi
leaq -0x1050(%rbp), %rdx
leaq 0x14768(%rip), %rsi     # 0x3262c
leaq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %r8
movb $0x0, %al
callq 0x2360
movq -0x10(%rbp), %rdi
leaq -0x1050(%rbp), %rsi
callq 0x16e10
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x1df64
movq -0x1068(%rbp), %rdi
leaq -0x1050(%rbp), %rsi
leaq -0x1070(%rbp), %rdx
callq 0x1d9e0
movl %eax, -0x106c(%rbp)
cmpl $0x0, -0x1070(%rbp)
je 0x1df5f
leaq -0x1050(%rbp), %rdi
callq 0x2520
movq %rax, %rdx
movq -0x1068(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x106c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq 0x1b0b8(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq -0x1050(%rbp), %rcx
leaq 0x13a1e(%rip), %rsi     # 0x3196f
leaq 0x146c9(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
jmp 0x1de38
jmp 0x1df66
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x1060(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x14(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0x1e063
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1dfde
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x1078(%rbp)
jmp 0x1dfec
movl $0x2, %eax
movq %rax, -0x1078(%rbp)
jmp 0x1dfec
movq -0x1078(%rbp), %rcx
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x23a0
movq %rax, %rcx
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movl -0x18(%rbp), %eax
movl %eax, %edx
shlq $0x20, %rdx
orq $0x1, %rdx
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x1060(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl -0x14(%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 0x1e0b1
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x1060(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x14(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0x1e1ae
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1e129
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x1080(%rbp)
jmp 0x1e137
movl $0x2, %eax
movq %rax, -0x1080(%rbp)
jmp 0x1e137
movq -0x1080(%rbp), %rcx
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x23a0
movq %rax, %rcx
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
movl %eax, %edx
shlq $0x20, %rdx
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
orq %rax, %rdx
movq -0x1060(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x1060(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl -0x14(%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 0x1e29e
movq -0x1060(%rbp), %rdi
callq 0x1d050
movq %rax, -0x30(%rbp)
movq 0x1adc4(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x13747(%rip), %rsi     # 0x31991
leaq 0x143d0(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
jbe 0x1e296
movq -0x38(%rbp), %rax
shlq %rax
movq %rax, -0x38(%rbp)
movq 0x1ad7e(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x38(%rbp), %rcx
leaq 0x13745(%rip), %rsi     # 0x319cd
leaq 0x14392(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x1de38
movq -0x1060(%rbp), %rdi
callq 0x1d050
movq %rax, -0x30(%rbp)
movq 0x1ad3e(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x136c1(%rip), %rsi     # 0x31991
leaq 0x1434a(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
movq 0x1ad13(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x40(%rbp), %rcx
leaq 0x1434e(%rip), %rsi     # 0x32641
leaq 0x14327(%rip), %rdx     # 0x32621
movb $0x0, %al
callq 0x22c0
movl $0x0, -0x106c(%rbp)
movl -0x106c(%rbp), %eax
movq -0x1068(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x1e377
movq -0x1068(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x106c(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x106c(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x1e364
movq -0x1068(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x106c(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x2050
jmp 0x1e366
movl -0x106c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x106c(%rbp)
jmp 0x1e30b
movq -0x1068(%rbp), %rdi
callq 0x1dd40
movq -0x1058(%rbp), %rdi
callq 0x2f20
movq -0x1060(%rbp), %rax
addq $0x1080, %rsp           # imm = 0x1080
popq %rbp
retq
nop