// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/htslib/faidx.c
// Function: fai_build
int fai_build(const char *fn)
{
    char *str;
    BGZF *bgzf;
    FILE *fp;
    faidx_t *fai;
    str = (char*)calloc(strlen(fn) + 5, 1);
    sprintf(str, "%s.fai", fn);
    bgzf = bgzf_open(fn, "r");
    if ( !bgzf ) {
        fprintf(stderr, "[fai_build] fail to open the FASTA file %s\n",fn);
        free(str);
        return -1;
    }
    if ( bgzf->is_compressed ) bgzf_index_build_init(bgzf);
    fai = fai_build_core(bgzf);
    if ( !fai )
    {
        if ( bgzf->is_compressed && bgzf->is_gzip ) fprintf(stderr,"Cannot index files compressed with gzip, please use bgzip\n");
        bgzf_close(bgzf);
        free(str);
        return -1;
    }
    if ( bgzf->is_compressed ) {
        if (bgzf_index_dump(bgzf, fn, ".gzi") < 0) {
            fprintf(stderr, "[fai_build] fail to make bgzf index %s.gzi\n", fn);
            fai_destroy(fai); free(str);
            return -1;
        }
    }
    if (bgzf_close(bgzf) < 0) {
        fprintf(stderr, "[fai_build] Error on closing %s\n", fn);
        fai_destroy(fai); free(str);
        return -1;
    }
    fp = fopen(str, "wb");
    if ( !fp ) {
        fprintf(stderr, "[fai_build] fail to write FASTA index %s\n",str);
        fai_destroy(fai); free(str);
        return -1;
    }
    fai_save(fai, fp);
    fclose(fp);
    free(str);
    fai_destroy(fai);
    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
callq 0x3380
leaq 0x5(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x36b0
movq %rax, %rbx
leaq 0x1e1cd(%rip), %rsi     # 0x411c1
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3460
leaq 0x1ff71(%rip), %rsi     # 0x42f79
movq %r14, %rdi
callq 0x1fde4
testq %rax, %rax
je 0x230e5
movq %rax, %r12
testb $0x60, 0x3(%rax)
je 0x2302a
movq %r12, %rdi
callq 0x21d59
movq %r12, %rdi
callq 0x227a0
movq %rax, %r15
movl (%r12), %eax
testq %r15, %r15
je 0x230b1
testl $0x60000000, %eax       # imm = 0x60000000
je 0x2305f
leaq 0x1e17c(%rip), %rdx     # 0x411c8
movq %r12, %rdi
movq %r14, %rsi
callq 0x21d86
testl %eax, %eax
js 0x2312b
movq %r12, %rdi
callq 0x216cc
testl %eax, %eax
js 0x23102
leaq 0x1cb6c(%rip), %rsi     # 0x3fbe2
movq %rbx, %rdi
callq 0x3250
testq %rax, %rax
je 0x23115
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x22e90
movq %r14, %rdi
callq 0x36d0
movq %rbx, %rdi
callq 0x3370
movq %r15, %rdi
callq 0x22e1e
xorl %eax, %eax
jmp 0x230d9
testl $0x60000000, %eax       # imm = 0x60000000
sete %cl
testl %eax, %eax
setns %al
orb %cl, %al
je 0x23153
movq %r12, %rdi
callq 0x216cc
movq %rbx, %rdi
callq 0x3370
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x2fefc(%rip), %rax     # 0x52fe8
movq (%rax), %rdi
leaq 0x1f1fe(%rip), %rsi     # 0x422f4
movq %r14, %rdx
xorl %eax, %eax
callq 0x37f0
jmp 0x230ce
movq 0x2fedf(%rip), %rax     # 0x52fe8
movq (%rax), %rdi
leaq 0x1f274(%rip), %rsi     # 0x42387
jmp 0x2313c
movq 0x2fecc(%rip), %rax     # 0x52fe8
movq (%rax), %rdi
leaq 0x1f282(%rip), %rsi     # 0x423a8
movq %rbx, %rdx
jmp 0x2313f
movq 0x2feb6(%rip), %rax     # 0x52fe8
movq (%rax), %rdi
leaq 0x1f21f(%rip), %rsi     # 0x4235b
movq %r14, %rdx
xorl %eax, %eax
callq 0x37f0
movq %r15, %rdi
callq 0x22e1e
jmp 0x230ce
movq 0x2fe8e(%rip), %rax     # 0x52fe8
movq (%rax), %rcx
leaq 0x1f1bc(%rip), %rdi     # 0x42320
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x3780
jmp 0x230c6