// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/sam.c
// Function: sam_index_build3
int sam_index_build3(const char *fn, const char *fnidx, int min_shift, int nthreads)
{
    hts_idx_t *idx;
    htsFile *fp;
    int ret = 0;

    if ((fp = hts_open(fn, "r")) == 0) return -2;
    if (nthreads)
        hts_set_threads(fp, nthreads);

    switch (fp->format.format) {
    case cram:

        ret = cram_index_build(fp->fp.cram, fn, fnidx);
        break;

    case bam:
    case sam:
        if (fp->format.compression != bgzf) {
            hts_log_error("%s file \"%s\" not BGZF compressed",
                          fp->format.format == bam ? "BAM" : "SAM", fn);
            ret = -1;
            break;
        }
        idx = sam_index(fp, min_shift);
        if (idx) {
            ret = hts_idx_save_as(idx, fn, fnidx, (min_shift > 0)? HTS_FMT_CSI : HTS_FMT_BAI);
            if (ret < 0) ret = -4;
            hts_idx_destroy(idx);
        }
        else ret = -1;
        break;

    default:
        ret = -3;
        break;
    }
    hts_close(fp);

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r15d
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r13
leaq 0x76e0b(%rip), %rsi     # 0x9b5c4
callq 0x1447b
testq %rax, %rax
je 0x24852
movq %rax, %rbx
testl %r15d, %r15d
je 0x247da
movq %rbx, %rdi
movl %r15d, %esi
callq 0x159d1
movl 0x4c(%rbx), %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x2485c
cmpl $0x2, 0x54(%rbx)
jne 0x24880
movl %ebp, 0x4(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, %rdi
callq 0x2621d
testq %rax, %rax
movq %r13, 0x8(%rsp)
je 0x249f7
movq %rax, %r12
cmpl $0x0, 0x4(%rsp)
jle 0x248b7
cmpl $0x0, (%r12)
jle 0x248c9
xorl %eax, %eax
xorl %r15d, %r15d
movq %rax, %r14
movq %r12, %rdi
movl %r15d, %esi
callq 0x8bdc3
cmpq %rax, %r14
cmovgq %r14, %rax
incl %r15d
cmpl (%r12), %r15d
jl 0x2482c
addq $0x100, %rax            # imm = 0x100
jmp 0x248ce
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x24a3d
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpl $0x6, %eax
jne 0x24a35
movq 0x38(%rbx), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4cc70
movl %eax, %ebp
jmp 0x24a35
cmpl $0x4, %eax
leaq 0x744b4(%rip), %rax     # 0x98d3e
leaq 0x744a9(%rip), %rcx     # 0x98d3a
cmoveq %rax, %rcx
leaq 0x76051(%rip), %rsi     # 0x9a8ed
leaq 0x7605b(%rip), %rdx     # 0x9a8fe
movl $0x1, %edi
movq %r13, %r8
xorl %eax, %eax
callq 0x14378
jmp 0x24a30
movl $0xe, %ecx
movl $0x5, %r8d
movl $0x1, %esi
jmp 0x248fe
movl $0x100, %eax            # imm = 0x100
movl $0x1, %edx
movl 0x4(%rsp), %ecx
shll %cl, %edx
movslq %edx, %rdx
xorl %esi, %esi
movl $0x0, %r8d
cmpq %rdx, %rax
jle 0x248fe
xorl %esi, %esi
xorl %r8d, %r8d
incl %r8d
shlq $0x3, %rdx
cmpq %rdx, %rax
jg 0x248ee
movl 0x4(%rsp), %ecx
movl (%r12), %edi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %r9
shlq $0x10, %r9
movzwl 0x10(%rax), %edx
orq %r9, %rdx
callq 0x16550
movq %rax, %r13
movl $0x1, %edi
movl $0x50, %esi
callq 0x4300
movq %rax, %r15
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2a3f9
testl %eax, %eax
js 0x249e2
movl 0x8(%r15), %ebp
movq (%r15), %r14
movq %r15, %rdi
callq 0x23b0a
movq 0x38(%rbx), %rcx
movq 0x18(%rcx), %rdx
shlq $0x10, %rdx
movzwl 0x10(%rcx), %r8d
orq %rdx, %r8
movzwl 0x10(%r15), %ecx
xorl %r9d, %r9d
testb $0x4, %cl
sete %r9b
movq %r13, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %rax, %rcx
callq 0x16eed
testl %eax, %eax
jns 0x2492f
movq 0x38(%r15), %r14
movl 0x8(%r15), %esi
movq %r12, %rdi
callq 0x8bd81
movq %rax, %rbp
movl 0x8(%r15), %esi
movq %r12, %rdi
callq 0x8bdc3
movzwl 0x10(%r15), %r10d
movq (%r15), %r11
incq %r11
leaq 0x76617(%rip), %rsi     # 0x9afd3
leaq 0x762ef(%rip), %rdx     # 0x9acb2
movl $0x1, %edi
movq %r14, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x14378
addq $0x10, %rsp
jmp 0x249e7
cmpl $-0x1, %eax
je 0x24a4e
movq %r15, %rdi
callq 0x23544
movq %r13, %rdi
callq 0x1585e
xorl %r13d, %r13d
movl 0x4(%rsp), %eax
testq %r13, %r13
movq 0x10(%rsp), %rdx
je 0x24a30
xorl %ecx, %ecx
testl %eax, %eax
setle %cl
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x174c7
testl %eax, %eax
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
cmovnsl %eax, %ebp
movq %r13, %rdi
callq 0x1585e
jmp 0x24a35
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x156e5
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rcx
shlq $0x10, %rcx
movzwl 0x10(%rax), %esi
orq %rcx, %rsi
movq %r13, %rdi
callq 0x1664f
movq %r12, %rdi
callq 0x22ae6
movq %r15, %rdi
callq 0x23544
jmp 0x249fa