// File: /workspace/github_c_makefile_star2_O3/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
movl $0x1, %esi
callq 0x36a0
movq %rax, %rbx
leaq 0x24f75(%rip), %rsi     # 0x4f386
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3460
leaq 0x26e00(%rip), %rsi     # 0x51225
movq %r14, %rdi
callq 0x26f1c
testq %rax, %rax
je 0x2a504
movq %rax, %r12
testb $0x60, 0x3(%rax)
je 0x2a447
movq %r12, %rdi
callq 0x2914f
movq %r12, %rdi
callq 0x29b64
movq %rax, %r15
movl (%r12), %eax
testq %r15, %r15
je 0x2a4ce
testl $0x60000000, %eax       # imm = 0x60000000
je 0x2a47c
leaq 0x24f24(%rip), %rdx     # 0x4f38d
movq %r12, %rdi
movq %r14, %rsi
callq 0x29182
testl %eax, %eax
js 0x2a541
movq %r12, %rdi
callq 0x2899c
testl %eax, %eax
js 0x2a521
leaq 0x2374f(%rip), %rsi     # 0x4dbe2
movq %rbx, %rdi
callq 0x3250
testq %rax, %rax
je 0x2a534
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x2a276
movq %r14, %rdi
callq 0x36c0
movq %rbx, %rdi
callq 0x3370
movq %r15, %rdi
callq 0x2a1fc
xorl %eax, %eax
jmp 0x2a4f8
testl $0x60000000, %eax       # imm = 0x60000000
sete %cl
testl %eax, %eax
setns %al
orb %cl, %al
je 0x2a566
movq %r12, %rdi
callq 0x2899c
movq %rbx, %rdi
callq 0x3370
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x34add(%rip), %rax     # 0x5efe8
movq (%rax), %rdi
leaq 0x25fbf(%rip), %rsi     # 0x504d4
movq %r14, %rdx
xorl %eax, %eax
callq 0x37e0
jmp 0x2a4eb
movq 0x34ac0(%rip), %rax     # 0x5efe8
movq (%rax), %rdi
leaq 0x26035(%rip), %rsi     # 0x50567
jmp 0x2a552
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4aed
jmp 0x2a4f3
movq 0x34aa0(%rip), %rax     # 0x5efe8
movq (%rax), %rdi
leaq 0x25fe9(%rip), %rsi     # 0x5053b
movq %r14, %rdx
xorl %eax, %eax
callq 0x37e0
movq %r15, %rdi
callq 0x2a1fc
jmp 0x2a4eb
callq 0x4b29
jmp 0x2a4e3