// File: /workspace/github_c_makefile_star2_O2/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, %r12d
movq %rsi, %r15
movq %rdi, %r14
leaq 0x5b5de(%rip), %rsi     # 0x7a526
callq 0x111c7
testq %rax, %rax
je 0x1efc9
movq %rax, %rbx
testl %ebp, %ebp
je 0x1ef63
movq %rbx, %rdi
movl %ebp, %esi
callq 0x12668
movl 0x4c(%rbx), %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1efd1
cmpl $0x2, 0x54(%rbx)
jne 0x1efec
movq %rbx, %rdi
callq 0x2064e
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x1f151
movq %rax, %r13
testl %r12d, %r12d
movq %r14, 0x10(%rsp)
movq %r15, 0x8(%rsp)
movl %r12d, 0x4(%rsp)
jle 0x1f051
xorl %r15d, %r15d
xorl %r14d, %r14d
movl (%r13), %edi
cmpl %edi, %r15d
jge 0x1f028
movq %r13, %rdi
movl %r15d, %esi
callq 0x6cfd3
cmpq %rax, %r14
cmovleq %rax, %r14
incl %r15d
jmp 0x1efa9
pushq $-0x2
popq %rbp
jmp 0x1f159
cmpl $0x6, %eax
jne 0x1f021
movq 0x38(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3dc15
movl %eax, %ebp
jmp 0x1f151
cmpl $0x4, %eax
leaq 0x58dd1(%rip), %rax     # 0x77dc7
leaq 0x58dc6(%rip), %rcx     # 0x77dc3
cmoveq %rax, %rcx
leaq 0x5a845(%rip), %rsi     # 0x7984d
leaq 0x5a84f(%rip), %rdx     # 0x7985e
pushq $0x1
popq %rdi
movq %r14, %r8
xorl %eax, %eax
callq 0x110c6
jmp 0x1f14e
pushq $-0x3
jmp 0x1f150
pushq $0x1
popq %rax
movl %r12d, %ecx
shll %cl, %eax
addq $0x100, %r14            # imm = 0x100
cltq
xorl %esi, %esi
xorl %r8d, %r8d
cmpq %rax, %r14
jle 0x1f04c
incl %r8d
shlq $0x3, %rax
jmp 0x1f03e
movl %r12d, %ecx
jmp 0x1f05f
movl (%r13), %edi
pushq $0xe
popq %rcx
pushq $0x5
popq %r8
pushq $0x1
popq %rsi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %r9
shlq $0x10, %r9
movzwl 0x10(%rax), %edx
orq %r9, %rdx
callq 0x13187
movq %rax, %r15
callq 0x1dec4
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x233e2
testl %eax, %eax
js 0x1f139
movl 0x8(%r12), %ebp
movq (%r12), %r14
movq %r12, %rdi
callq 0x1e518
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 0x1390f
testl %eax, %eax
jns 0x1f082
movq 0x38(%r12), %r14
movl 0x8(%r12), %esi
movq %r13, %rdi
callq 0x6cf95
movq %rax, %rbp
movl 0x8(%r12), %esi
movq %r13, %rdi
callq 0x6cfd3
movzwl 0x10(%r12), %r10d
movq (%r12), %r11
incq %r11
leaq 0x5ae2a(%rip), %rsi     # 0x79f41
leaq 0x5ab02(%rip), %rdx     # 0x79c20
pushq $0x1
popq %rdi
movq %r14, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x110c6
popq %rax
popq %rcx
jmp 0x1f13e
cmpl $-0x1, %eax
je 0x1f16a
movq %r12, %rdi
callq 0x1df92
movq %r15, %rdi
callq 0x12502
pushq $-0x1
popq %rbp
movq %rbx, %rdi
callq 0x1239d
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 0x13265
movq %r13, %rdi
callq 0x1d61e
movq %r12, %rdi
callq 0x1df92
testq %r15, %r15
pushq $-0x1
popq %rbp
je 0x1f151
xorl %ecx, %ecx
cmpl $0x0, 0x4(%rsp)
setle %cl
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x13edb
testl %eax, %eax
pushq $-0x4
popq %rbp
cmovnsl %eax, %ebp
movq %r15, %rdi
callq 0x12502
jmp 0x1f151