// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %r12
leaq 0x747d3(%rip), %rsi     # 0x985cb
callq 0x13ee8
testq %rax, %rax
je 0x23e9e
movq %rax, %rbx
testl %ebp, %ebp
je 0x23e17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1543b
movl 0x4c(%rbx), %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x23ea8
cmpl $0x2, 0x54(%rbx)
jne 0x23ecc
movq %r14, 0x10(%rsp)
movq %rbx, %rdi
callq 0x25821
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x24049
movq %rax, %r13
movl (%rax), %edi
testl %r15d, %r15d
movl %r15d, 0x4(%rsp)
movq %r12, 0x8(%rsp)
jle 0x23f03
testl %edi, %edi
jle 0x23f15
xorl %eax, %eax
xorl %r15d, %r15d
movq %rax, %r14
movq %r13, %rdi
movl %r15d, %esi
callq 0x8947e
cmpq %rax, %r14
cmovgq %r14, %rax
incl %r15d
movl (%r13), %edi
cmpl %edi, %r15d
jl 0x23e70
addq $0x100, %rax            # imm = 0x100
movl 0x4(%rsp), %r15d
jmp 0x23f1a
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x24051
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpl $0x6, %eax
jne 0x24049
movq 0x38(%rbx), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4a43d
movl %eax, %ebp
jmp 0x24049
cmpl $0x4, %eax
leaq 0x71f08(%rip), %rax     # 0x95dde
leaq 0x71efd(%rip), %rcx     # 0x95dda
cmoveq %rax, %rcx
leaq 0x73a15(%rip), %rsi     # 0x978fd
leaq 0x73a1f(%rip), %rdx     # 0x9790e
movl $0x1, %edi
movq %r12, %r8
xorl %eax, %eax
callq 0x13de5
jmp 0x24044
movl $0xe, %ecx
movl $0x5, %r8d
movl $0x1, %esi
jmp 0x23f47
movl $0x100, %eax            # imm = 0x100
movl $0x1, %edx
movl %r15d, %ecx
shll %cl, %edx
movslq %edx, %rcx
xorl %esi, %esi
cmpq %rcx, %rax
jle 0x23f41
xorl %esi, %esi
xorl %r8d, %r8d
incl %r8d
shlq $0x3, %rcx
cmpq %rcx, %rax
jg 0x23f33
jmp 0x23f44
xorl %r8d, %r8d
movl %r15d, %ecx
movq 0x38(%rbx), %rax
movq 0x18(%rax), %r9
shlq $0x10, %r9
movzwl 0x10(%rax), %edx
orq %r9, %rdx
callq 0x15fba
movq %rax, %r15
movl $0x1, %edi
movl $0x50, %esi
callq 0x4300
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x297ef
testl %eax, %eax
js 0x2402f
movl 0x8(%r12), %ebp
movq (%r12), %r14
movq %r12, %rdi
callq 0x23104
movq 0x38(%rbx), %rcx
movq 0x18(%rcx), %rdx
shlq $0x10, %rdx
movzwl 0x10(%rcx), %r8d
orq %rdx, %r8
movzwl 0x10(%r12), %ecx
xorl %r9d, %r9d
testb $0x4, %cl
sete %r9b
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %rax, %rcx
callq 0x1699a
testl %eax, %eax
jns 0x23f74
movq 0x38(%r12), %r14
movl 0x8(%r12), %esi
movq %r13, %rdi
callq 0x8943c
movq %rax, %rbp
movl 0x8(%r12), %esi
movq %r13, %rdi
callq 0x8947e
movzwl 0x10(%r12), %r10d
movq (%r12), %r11
incq %r11
leaq 0x73fdd(%rip), %rsi     # 0x97fe6
leaq 0x73cb5(%rip), %rdx     # 0x97cc5
movl $0x1, %edi
movq %r14, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x13de5
addq $0x10, %rsp
jmp 0x24034
cmpl $-0x1, %eax
je 0x24062
movq %r12, %rdi
callq 0x22b2b
movq %r15, %rdi
callq 0x152c3
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x1514a
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 %r15, %rdi
callq 0x160ae
movq %r13, %rdi
callq 0x22102
movq %r12, %rdi
callq 0x22b2b
testq %r15, %r15
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
je 0x24049
xorl %ecx, %ecx
cmpl $0x0, 0x4(%rsp)
setle %cl
movq %r15, %rdi
callq 0x16f96
testl %eax, %eax
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
cmovnsl %eax, %ebp
movq %r15, %rdi
callq 0x152c3
jmp 0x24049