// File: 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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4230
movq %rax, %rdi
addq $0x5, %rdi
movl $0x1, %esi
callq 0x4370
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1498f(%rip), %rsi     # 0x7d1c8
movb $0x0, %al
callq 0x4100
movq -0x10(%rbp), %rdi
leaq 0x131a4(%rip), %rsi     # 0x7b9ef
callq 0x62d50
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x6888c
movq 0x2176e(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x15c47(%rip), %rsi     # 0x7e4b7
movb $0x0, %al
callq 0x4830
movq -0x18(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68a3e
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x1d, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x688a6
movq -0x20(%rbp), %rdi
callq 0x663c0
movq -0x20(%rbp), %rdi
callq 0x67ee0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x6890f
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x1d, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x688f1
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x688f1
movq 0x216f0(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x15bf9(%rip), %rsi     # 0x7e4e3
movb $0x0, %al
callq 0x4830
movq -0x20(%rbp), %rdi
callq 0x65880
movq -0x18(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68a3e
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x1d, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x68975
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x148a0(%rip), %rdx     # 0x7d1cf
callq 0x66420
cmpl $0x0, %eax
jge 0x68973
movq 0x21690(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x15bd0(%rip), %rsi     # 0x7e51e
movb $0x0, %al
callq 0x4830
movq -0x30(%rbp), %rdi
callq 0x68520
movq -0x18(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68a3e
jmp 0x68975
movq -0x20(%rbp), %rdi
callq 0x65880
cmpl $0x0, %eax
jge 0x689bd
movq 0x21646(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x15bb2(%rip), %rsi     # 0x7e54a
movb $0x0, %al
callq 0x4830
movq -0x30(%rbp), %rdi
callq 0x68520
movq -0x18(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68a3e
movq -0x18(%rbp), %rdi
leaq 0x15720(%rip), %rsi     # 0x7e0e8
callq 0x4690
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x68a0f
movq 0x215f1(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x15b7e(%rip), %rsi     # 0x7e56b
movb $0x0, %al
callq 0x4830
movq -0x30(%rbp), %rdi
callq 0x68520
movq -0x18(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68a3e
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x685a0
movq -0x28(%rbp), %rdi
callq 0x4590
movq -0x18(%rbp), %rdi
callq 0x46a0
movq -0x30(%rbp), %rdi
callq 0x68520
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)