// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/sam.c
// Function: sam_index
static hts_idx_t *sam_index(htsFile *fp, int min_shift)
{
    int n_lvls, i, fmt, ret;
    bam1_t *b;
    hts_idx_t *idx;
    sam_hdr_t *h;
    h = sam_hdr_read(fp);
    if (h == NULL) return NULL;
    if (min_shift > 0) {
        hts_pos_t max_len = 0, s;
        for (i = 0; i < h->n_targets; ++i) {
            hts_pos_t len = sam_hdr_tid2len(h, i);
            if (max_len < len) max_len = len;
        }
        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->fp.bgzf), min_shift, n_lvls);
    b = bam_init1();
    while ((ret = sam_read1(fp, h, b)) >= 0) {
        ret = hts_idx_push(idx, b->core.tid, b->core.pos, bam_endpos(b), bgzf_tell(fp->fp.bgzf), !(b->core.flag&BAM_FUNMAP));
        if (ret < 0) { // unsorted or doesn't fit
            hts_log_error("Read '%s' with ref_name='%s', ref_length=%"PRIhts_pos", flags=%d, pos=%"PRIhts_pos" cannot be indexed", bam_get_qname(b), sam_hdr_tid2name(h, b->core.tid), sam_hdr_tid2len(h, b->core.tid), b->core.flag, b->core.pos+1);
            goto err;
        }
    }
    if (ret < -1) goto err; // corrupted BAM file

    hts_idx_finish(idx, bgzf_tell(fp->fp.bgzf));
    sam_hdr_destroy(h);
    bam_destroy1(b);
    return idx;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0x378d0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x34f13
movq $0x0, -0x8(%rbp)
jmp 0x35195
cmpl $0x0, -0x14(%rbp)
jle 0x34fb7
movq $0x0, -0x48(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x34f66
movq -0x40(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0xdda90
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x34f59
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x34f5b
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x34f2c
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 0x34fae
jmp 0x34f97
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x50(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x50(%rbp)
jmp 0x34f8b
movl $0x0, -0x20(%rbp)
jmp 0x34fcc
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 0x38(%rax), %rax
movq 0x18(%rax), %rdx
shlq $0x10, %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x10(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
callq 0x1ecb0
movq %rax, -0x38(%rbp)
callq 0x32db0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x3ca10
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jl 0x35129
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
callq 0x33a20
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %esi
movq -0x60(%rbp), %rdx
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %r8
shlq $0x10, %r8
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x10(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %r8
movq -0x30(%rbp), %rax
movzwl 0x10(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1f990
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x35124
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xdda00
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xdda90
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %r8
movq %rax, %r9
movq -0x30(%rbp), %rax
movzwl 0x10(%rax), %r10d
movq -0x30(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movl $0x1, %edi
leaq 0xbd3f9(%rip), %rsi     # 0xf2504
leaq 0xbd0d1(%rip), %rdx     # 0xf21e3
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x1b030
jmp 0x3517b
jmp 0x35013
cmpl $-0x1, -0x24(%rbp)
jge 0x35131
jmp 0x3517b
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rsi
shlq $0x10, %rsi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x10(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rsi
callq 0x1ee60
movq -0x40(%rbp), %rdi
callq 0x31e70
movq -0x30(%rbp), %rdi
callq 0x32ff0
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x35195
movq -0x30(%rbp), %rdi
callq 0x32ff0
movq -0x38(%rbp), %rdi
callq 0x1d560
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)