// File: karel-brinda[P]ococo/ext/htslib/sam.c
// Function: bam_index
static hts_idx_t *bam_index(BGZF *fp, int min_shift)
{
    int n_lvls, i, fmt, ret;
    bam1_t *b;
    hts_idx_t *idx;
    bam_hdr_t *h;
    h = bam_hdr_read(fp);
    if (h == NULL) return NULL;
    if (min_shift > 0) {
        int64_t max_len = 0, s;
        for (i = 0; i < h->n_targets; ++i)
            if (max_len < h->target_len[i]) max_len = h->target_len[i];
        max_len += 256;
        for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
        fmt = HTS_FMT_CSI;
    } else min_shift = 14, n_lvls = 5, fmt = HTS_FMT_BAI;
    idx = hts_idx_init(h->n_targets, fmt, bgzf_tell(fp), min_shift, n_lvls);
    bam_hdr_destroy(h);
    b = bam_init1();
    while ((ret = bam_read1(fp, b)) >= 0) {
        ret = hts_idx_push(idx, b->core.tid, b->core.pos, bam_endpos(b), bgzf_tell(fp), !(b->core.flag&BAM_FUNMAP));
        if (ret < 0) goto err; // unsorted
    }
    if (ret < -1) goto err; // corrupted BAM file

    hts_idx_finish(idx, bgzf_tell(fp));
    bam_destroy1(b);
    return idx;

err:
    bam_destroy1(b);
    hts_idx_destroy(idx);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1ac70
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x1c3c0
movq $0x0, -0x8(%rbp)
jmp 0x1c5c3
cmpl $0x0, -0x14(%rbp)
jle 0x1c46d
movq $0x0, -0x48(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1c41c
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x1c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
cmpq %rcx, %rax
jge 0x1c40f
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rax, -0x48(%rbp)
jmp 0x1c411
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1c3d9
movq -0x48(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x48(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
cltq
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x1c464
jmp 0x1c44d
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x50(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x50(%rbp)
jmp 0x1c441
movl $0x0, -0x20(%rbp)
jmp 0x1c482
movl $0xe, -0x14(%rbp)
movl $0x5, -0x18(%rbp)
movl $0x1, -0x20(%rbp)
movq -0x40(%rbp), %rax
movl (%rax), %edi
movl -0x20(%rbp), %esi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
shlq $0x10, %rdx
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
callq 0x15fa0
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
callq 0x1aa50
callq 0x1b5f0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x1b920
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jl 0x1c568
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1b880
movq -0x60(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x54(%rbp), %edx
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %r8
shlq $0x10, %r8
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %r8
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x16a70
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x1c563
jmp 0x1c5a9
jmp 0x1c4ca
cmpl $-0x1, -0x24(%rbp)
jge 0x1c570
jmp 0x1c5a9
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x10, %rsi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rsi
callq 0x16130
movq -0x30(%rbp), %rdi
callq 0x1b610
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1c5c3
movq -0x30(%rbp), %rdi
callq 0x1b610
movq -0x38(%rbp), %rdi
callq 0x171d0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)