// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %rbx
callq 0x4230
leaq 0x5(%rax), %rdi
movl $0x1, %esi
callq 0x4360
movq %rax, %r14
leaq 0xe01d(%rip), %rsi      # 0x54ddd
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4110
movq %r14, %rdi
callq 0x48bf3
testl %eax, %eax
movq %rbx, 0x20(%rsp)
movq %r14, (%rsp)
je 0x46ed5
movq %r14, %rdi
callq 0x4230
cltq
addq %r14, %rax
decq %rax
movq %rax, %r15
cmpq %r14, %rax
jb 0x46e08
leaq -0x1(%r15), %rax
cmpb $0x2f, (%r15)
jne 0x46df6
incq %r15
leaq 0xcb07(%rip), %rsi      # 0x53919
movq %r15, %rdi
callq 0x4600
movq %rax, %r12
testq %rax, %rax
jne 0x46f1f
leaq 0xc1cd(%rip), %rsi      # 0x52ffa
movq %r14, %rdi
callq 0x4883b
testq %rax, %rax
je 0x47112
movq %rax, %r13
leaq 0xedd0(%rip), %rsi      # 0x55c18
movq %r15, %rdi
callq 0x4600
testq %rax, %rax
je 0x47139
movq %rax, %rbx
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x4360
movq %rax, %r14
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rbp
subq %rsi, %rbp
movl $0x100000, %eax         # imm = 0x100000
cmpq %rax, %rbp
movl $0x100000, %r12d        # imm = 0x100000
cmovbq %rbp, %r12
movq %r14, %rdi
movq %r12, %rdx
callq 0x4420
addq %r12, 0x8(%r13)
movl $0x100000, %edx         # imm = 0x100000
cmpq $0xfffff, %rbp          # imm = 0xFFFFF
ja 0x46eb9
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x48187
movq %rax, %rdx
testl %edx, %edx
jle 0x46ee9
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x1, %esi
movq %r14, %rdi
movq %rbx, %rcx
callq 0x4a30
jmp 0x46e6e
leaq 0xc11e(%rip), %rsi      # 0x52ffa
movq %r14, %rdi
callq 0x4600
movq %rax, %r12
jmp 0x46f28
movq %r14, %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x4530
movq %r13, %rdi
callq 0x484b2
testl %eax, %eax
movq (%rsp), %r14
jne 0x47168
leaq 0xca05(%rip), %rsi      # 0x53919
movq %r15, %rdi
callq 0x4600
movq %rax, %r12
testq %r12, %r12
je 0x470cd
testq %r12, %r12
je 0x470ab
movl $0x1, %edi
movl $0x20, %esi
callq 0x4360
movq %rax, %r15
movl $0x1, %edi
movl $0x28, %esi
callq 0x4360
movq %rax, 0x18(%r15)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x4360
movq %rax, %r13
leaq 0x18(%rsp), %rbp
leaq 0x1c(%rsp), %rbx
movq %r13, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x4860
testq %rax, %rax
je 0x47010
movb (%r13), %r14b
movq %r13, %rdi
testb %r14b, %r14b
je 0x46fb9
callq 0x4580
movq (%rax), %rax
movq %r13, %rdi
movzbl %r14b, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x46fb9
movb 0x1(%rdi), %r14b
incq %rdi
testb %r14b, %r14b
jne 0x46fa2
movb $0x0, (%rdi)
incq %rdi
leaq 0xf43c(%rip), %rsi      # 0x56402
leaq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rbp, %r8
movq %rbx, %r9
xorl %eax, %eax
callq 0x4520
movslq 0x8(%rsp), %rdx
movl 0x1c(%rsp), %ecx
movl 0x18(%rsp), %r8d
movq 0x10(%rsp), %r9
movq %r15, %rdi
movq %r13, %rsi
callq 0x46702
testl %eax, %eax
je 0x46f72
movq %r13, %rdi
callq 0x4610
xorl %r15d, %r15d
jmp 0x47028
movq %r13, %rdi
callq 0x4610
movq %r12, %rdi
callq 0x49f0
testl %eax, %eax
jne 0x470fb
movq (%rsp), %rbx
movq %r12, %rdi
callq 0x4530
movq %rbx, %rdi
callq 0x4610
testq %r15, %r15
je 0x470dc
leaq 0xbfae(%rip), %rsi      # 0x52ffa
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x436f0
movq %rax, (%r15)
testq %rax, %rax
je 0x470d7
movl $0x60000000, %ecx       # imm = 0x60000000
andl (%rax), %ecx
cmpl $0x20000000, %ecx       # imm = 0x20000000
jne 0x470df
leaq 0xdd6d(%rip), %rdx      # 0x54de4
movq %rax, %rdi
movq %rbx, %rsi
callq 0x45b5e
testl %eax, %eax
jns 0x470df
movq 0x1cf3b(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0xf0ce(%rip), %rsi      # 0x56165
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4770
movq %r15, %rdi
callq 0x46a1e
jmp 0x470dc
leaq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x5ff5
testb $0x1, %al
je 0x470f1
movq 0x10(%rsp), %r15
jmp 0x470df
movq %r14, %rdi
callq 0x5fcd
jmp 0x470dc
callq 0x60d0
xorl %r15d, %r15d
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r12
jmp 0x46f31
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x608a
xorl %r15d, %r15d
jmp 0x4702c
movq 0x1ceaf(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0xf22e(%rip), %rsi      # 0x56351
xorl %r12d, %r12d
movq (%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
jmp 0x46f1f
movq 0x1ce88(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0xf23b(%rip), %rsi      # 0x56385
xorl %r12d, %r12d
movq %r15, %rdx
xorl %eax, %eax
callq 0x4770
movq %r13, %rdi
callq 0x4850c
movq (%rsp), %r14
jmp 0x46f1f
movq 0x1ce59(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0xf254(%rip), %rsi      # 0x563cd
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
jmp 0x46f0d