// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/htslib/faidx.c
// Function: fai_load
faidx_t *fai_load(const char *fn)
{
    char *str;
    FILE *fp;
    faidx_t *fai;
    str = (char*)calloc(strlen(fn) + 5, 1);
    sprintf(str, "%s.fai", fn);

    if (hisremote(str))
    {
        fp = download_and_open(str);
        if ( !fp )
        {
            fprintf(stderr, "[fai_load] failed to open remote FASTA index %s\n", str);
            free(str);
            return 0;
        }
    }
    else
        fp = fopen(str, "rb");

    if (fp == 0) {
        fprintf(stderr, "[fai_load] build FASTA index.\n");
        if (fai_build(fn) < 0) {
            free(str);
            return 0;
        }
        fp = fopen(str, "rb");
        if (fp == 0) {
            fprintf(stderr, "[fai_load] failed to open FASTA index: %s\n", strerror(errno));
            free(str);
            return 0;
        }
    }

    fai = fai_read(fp, str);
    fclose(fp);
    free(str);
    if (fai == NULL) {
        return NULL;
    }

    fai->bgzf = bgzf_open(fn, "rb");
    if (fai->bgzf == 0) {
        fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
        return 0;
    }
    if ( fai->bgzf->is_compressed==1 )
    {
        if ( bgzf_index_load(fai->bgzf, fn, ".gzi") < 0 )
        {
            fprintf(stderr, "[fai_load] failed to load .gzi index: %s[.gzi]\n", fn);
            fai_destroy(fai);
            return NULL;
        }
    }
    return fai;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
callq 0x4210
leaq 0x5(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x4370
movq %rax, %r14
leaq 0xcbdb(%rip), %rsi      # 0x45e03
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4100
movq %r14, %rdi
callq 0x3aa79
testl %eax, %eax
movq %r14, 0x8(%rsp)
movq %rbx, 0x28(%rsp)
je 0x3934b
movq %r14, %rdi
callq 0x4210
cltq
addq %r14, %rax
decq %rax
movq %rax, %r15
cmpq %r14, %rax
jb 0x39271
leaq -0x1(%r15), %rax
cmpb $0x2f, (%r15)
jne 0x3925f
incq %r15
leaq 0xb63a(%rip), %rsi      # 0x448b5
movq %r15, %rdi
callq 0x45f0
testq %rax, %rax
jne 0x39408
leaq 0xadd7(%rip), %rsi      # 0x4406a
movq %r14, %rdi
callq 0x3a708
testq %rax, %rax
je 0x395dc
movq %rax, %r12
leaq 0xd97a(%rip), %rsi      # 0x46c28
movq %r15, %rdi
callq 0x45f0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x395f9
pushq $0x1
popq %rsi
movl $0x100000, %edi         # imm = 0x100000
callq 0x4370
movq %rax, %r14
movq 0x8(%r12), %rbx
movq 0x10(%r12), %r13
subq %rbx, %r13
movl $0x100000, %eax         # imm = 0x100000
cmpq %rax, %r13
movl $0x100000, %ebp         # imm = 0x100000
cmovbq %r13, %rbp
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x4420
addq %rbp, %rbx
movq %rbx, 0x8(%r12)
movl $0x100000, %eax         # imm = 0x100000
cmpq $0xfffff, %r13          # imm = 0xFFFFF
ja 0x39331
movl $0x100000, %edx         # imm = 0x100000
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rcx
callq 0x3a0c7
testl %eax, %eax
jle 0x393c9
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r14, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
movq 0x20(%rsp), %rcx
callq 0x4a20
jmp 0x392d4
leaq 0xad18(%rip), %rsi      # 0x4406a
movq %r14, %rdi
callq 0x45f0
testq %rax, %rax
jne 0x39408
movq %rbx, %r15
movq 0x1dc63(%rip), %rbx     # 0x56fd0
movq (%rbx), %rcx
leaq 0xdd8f(%rip), %rdi      # 0x47106
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x4a20
movq %r15, %rdi
callq 0x39056
testl %eax, %eax
js 0x39666
leaq 0xacd1(%rip), %rsi      # 0x4406a
movq %r14, %rdi
callq 0x45f0
testq %rax, %rax
jne 0x39408
movq (%rbx), %rbx
callq 0x4060
movl (%rax), %edi
callq 0x4190
leaq 0xdd69(%rip), %rsi      # 0x47125
xorl %ebp, %ebp
movq %rbx, %rdi
movq %rax, %rdx
jmp 0x39632
movq %r14, %rdi
callq 0x4600
movq 0x20(%rsp), %rdi
callq 0x4530
movq %r12, %rdi
callq 0x3a3a2
testl %eax, %eax
movq 0x8(%rsp), %r14
jne 0x39646
leaq 0xb4be(%rip), %rsi      # 0x448b5
movq %r15, %rdi
callq 0x45f0
testq %rax, %rax
je 0x3961c
movq %rax, %r12
pushq $0x1
popq %rbx
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x4370
movq %rax, %rbp
callq 0x38bac
movq %rax, 0x18(%rbp)
movl $0x10000, %edi          # imm = 0x10000
movq %rbx, %rsi
callq 0x4370
movq %rax, %r13
movq %rax, %rbx
incq %rbx
movq %r13, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x4850
testq %rax, %rax
je 0x394ce
movq %rbx, %r14
movzbl -0x1(%r14), %r15d
testq %r15, %r15
je 0x39472
callq 0x4580
movq (%rax), %rax
cmpw $0x0, (%rax,%r15,2)
jns 0x39472
incq %r14
jmp 0x39453
movb $0x0, -0x1(%r14)
movq %r14, %rdi
leaq 0xdf91(%rip), %rsi      # 0x47412
leaq 0x1c(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0x4510
movslq 0x1c(%rsp), %rdx
movl 0x18(%rsp), %ecx
movl 0x14(%rsp), %r8d
movq 0x30(%rsp), %r9
movq %rbp, %rdi
movq %r13, %rsi
callq 0x38bb7
testl %eax, %eax
je 0x3943b
movq %r13, %rdi
callq 0x4600
xorl %ebp, %ebp
jmp 0x394e6
movq %r13, %rdi
callq 0x4600
movq %r12, %rdi
callq 0x49d0
testl %eax, %eax
jne 0x3959b
movq 0x8(%rsp), %rbx
movq %r12, %rdi
callq 0x4530
movq %rbx, %rdi
callq 0x4600
testq %rbp, %rbp
je 0x39587
leaq 0xab5f(%rip), %rsi      # 0x4406a
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x35ed4
movq %rax, (%rbp)
testq %rax, %rax
je 0x3956b
movl $0x60000000, %ecx       # imm = 0x60000000
andl (%rax), %ecx
cmpl $0x20000000, %ecx       # imm = 0x20000000
jne 0x39589
leaq 0xc8d3(%rip), %rdx      # 0x45e0a
movq %rax, %rdi
movq %rbx, %rsi
callq 0x380f4
testl %eax, %eax
jns 0x39589
movq 0x1da83(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdc1e(%rip), %rsi      # 0x47175
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4770
movq %rbp, %rdi
callq 0x38ea6
jmp 0x39587
movq 0x1da5e(%rip), %rax     # 0x56fd0
movq (%rax), %rcx
leaq 0xdbd4(%rip), %rdi      # 0x47150
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x4a20
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1da2e(%rip), %rax     # 0x56fd0
movq (%rax), %rbx
callq 0x4060
movl (%rax), %edi
callq 0x4190
leaq 0xde65(%rip), %rsi      # 0x4741d
movq %rbx, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x4770
movq %rbp, %rdi
callq 0x38ea6
xorl %ebp, %ebp
jmp 0x394eb
movq 0x1d9ed(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdd74(%rip), %rsi      # 0x47361
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
jmp 0x3961c
movq 0x1d9d0(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdd8b(%rip), %rsi      # 0x47395
movq %r15, %rdx
xorl %eax, %eax
callq 0x4770
movq %r12, %rdi
callq 0x3a3fa
movq 0x1d9ad(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdaa8(%rip), %rsi      # 0x470d5
xorl %ebp, %ebp
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
movq %r14, %rdi
callq 0x4600
jmp 0x39589
movq 0x1d983(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xdd86(%rip), %rsi      # 0x473dd
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
jmp 0x393f0
movq %r14, %rdi
callq 0x4600
jmp 0x39587