// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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 0x4210
leaq 0x5(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x4370
movq %rax, %rbx
leaq 0xcd87(%rip), %rsi      # 0x45e03
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x4100
leaq 0xb825(%rip), %rsi      # 0x448b5
movq %r14, %rdi
callq 0x35ed4
testq %rax, %rax
je 0x3916d
movq %rax, %r12
testb $0x60, 0x3(%rax)
je 0x390b2
movq %r12, %rdi
callq 0x37e49
movq %r12, %rdi
callq 0x38890
movq %rax, %r15
movl (%r12), %eax
testq %r15, %r15
je 0x39139
testl $0x60000000, %eax       # imm = 0x60000000
je 0x390e7
leaq 0xcd36(%rip), %rdx      # 0x45e0a
movq %r12, %rdi
movq %r14, %rsi
callq 0x37e76
testl %eax, %eax
js 0x391b3
movq %r12, %rdi
callq 0x377bc
testl %eax, %eax
js 0x3918a
leaq 0xdb2a(%rip), %rsi      # 0x46c28
movq %rbx, %rdi
callq 0x45f0
testq %rax, %rax
je 0x3919d
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x38f18
movq %r14, %rdi
callq 0x4530
movq %rbx, %rdi
callq 0x4600
movq %r15, %rdi
callq 0x38ea6
xorl %eax, %eax
jmp 0x39161
testl $0x60000000, %eax       # imm = 0x60000000
sete %cl
testl %eax, %eax
setns %al
orb %cl, %al
je 0x391db
movq %r12, %rdi
callq 0x377bc
movq %rbx, %rdi
callq 0x4600
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x1de5c(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xde79(%rip), %rsi      # 0x46ff7
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
jmp 0x39156
movq 0x1de3f(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdeef(%rip), %rsi      # 0x4708a
jmp 0x391c4
movq 0x1de2c(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdefd(%rip), %rsi      # 0x470ab
movq %rbx, %rdx
jmp 0x391c7
movq 0x1de16(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xde9a(%rip), %rsi      # 0x4705e
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
movq %r15, %rdi
callq 0x38ea6
jmp 0x39156
movq 0x1ddee(%rip), %rax     # 0x56fd0
movq (%rax), %rcx
leaq 0xde37(%rip), %rdi      # 0x47023
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x4a20
jmp 0x3914e