// File: karel-brinda[P]ococo/ext/htslib/hts.c
// Function: hts_idx_load_local
static hts_idx_t *hts_idx_load_local(const char *fn)
{
    uint8_t magic[4];
    int i, is_be;
    hts_idx_t *idx = NULL;
    uint8_t *meta = NULL;
    BGZF *fp = bgzf_open(fn, "r");
    if (fp == NULL) return NULL;
    is_be = ed_is_big();
    if (bgzf_read(fp, magic, 4) != 4) goto fail;

    if (memcmp(magic, "CSI\1", 4) == 0) {
        uint32_t x[3], n;
        if (bgzf_read(fp, x, 12) != 12) goto fail;
        if (is_be) for (i = 0; i < 3; ++i) ed_swap_4p(&x[i]);
        if (x[2]) {
            if ((meta = (uint8_t*)malloc(x[2])) == NULL) goto fail;
            if (bgzf_read(fp, meta, x[2]) != x[2]) goto fail;
        }
        if (bgzf_read(fp, &n, 4) != 4) goto fail;
        if (is_be) ed_swap_4p(&n);
        if ((idx = hts_idx_init(n, HTS_FMT_CSI, 0, x[0], x[1])) == NULL) goto fail;
        idx->l_meta = x[2];
        idx->meta = meta;
        meta = NULL;
        if (hts_idx_load_core(idx, fp, HTS_FMT_CSI) < 0) goto fail;
    }
    else if (memcmp(magic, "TBI\1", 4) == 0) {
        uint32_t x[8];
        if (bgzf_read(fp, x, 32) != 32) goto fail;
        if (is_be) for (i = 0; i < 8; ++i) ed_swap_4p(&x[i]);
        if ((idx = hts_idx_init(x[0], HTS_FMT_TBI, 0, 14, 5)) == NULL) goto fail;
        idx->l_meta = 28 + x[7];
        if ((idx->meta = (uint8_t*)malloc(idx->l_meta)) == NULL) goto fail;
        memcpy(idx->meta, &x[1], 28);
        if (bgzf_read(fp, idx->meta + 28, x[7]) != x[7]) goto fail;
        if (hts_idx_load_core(idx, fp, HTS_FMT_TBI) < 0) goto fail;
    }
    else if (memcmp(magic, "BAI\1", 4) == 0) {
        uint32_t n;
        if (bgzf_read(fp, &n, 4) != 4) goto fail;
        if (is_be) ed_swap_4p(&n);
        idx = hts_idx_init(n, HTS_FMT_BAI, 0, 14, 5);
        if (hts_idx_load_core(idx, fp, HTS_FMT_BAI) < 0) goto fail;
    }
    else { errno = EINVAL; goto fail; }

    bgzf_close(fp);
    return idx;

fail:
    bgzf_close(fp);
    hts_idx_destroy(idx);
    free(meta);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x624d5(%rip), %rsi     # 0x7b9ef
callq 0x62d50
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x19537
movq $0x0, -0x8(%rbp)
jmp 0x19885
callq 0x13f80
movl %eax, -0x1c(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x4, %edx
callq 0x64bb0
cmpq $0x4, %rax
je 0x1955c
jmp 0x19862
leaq -0x14(%rbp), %rax
movl (%rax), %eax
subl $0x1495343, %eax        # imm = 0x1495343
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x19693
movq -0x38(%rbp), %rdi
leaq -0x44(%rbp), %rsi
movl $0xc, %edx
callq 0x64bb0
cmpq $0xc, %rax
je 0x19593
jmp 0x19862
cmpl $0x0, -0x1c(%rbp)
je 0x195c7
movl $0x0, -0x18(%rbp)
cmpl $0x3, -0x18(%rbp)
jge 0x195c5
movslq -0x18(%rbp), %rax
leaq -0x44(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
callq 0x1a550
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x195a0
jmp 0x195c7
cmpl $0x0, -0x3c(%rbp)
je 0x19607
movl -0x3c(%rbp), %eax
movl %eax, %edi
callq 0x47a0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x195e6
jmp 0x19862
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x3c(%rbp), %eax
movl %eax, %edx
callq 0x64bb0
movl -0x3c(%rbp), %ecx
cmpq %rcx, %rax
je 0x19605
jmp 0x19862
jmp 0x19607
movq -0x38(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x64bb0
cmpq $0x4, %rax
je 0x19624
jmp 0x19862
cmpl $0x0, -0x1c(%rbp)
je 0x19633
leaq -0x48(%rbp), %rdi
callq 0x1a550
movl -0x48(%rbp), %edi
movl -0x44(%rbp), %ecx
movl -0x40(%rbp), %r8d
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x15fa0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x19657
jmp 0x19862
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0x1a580
cmpl $0x0, %eax
jge 0x1968e
jmp 0x19862
jmp 0x1984f
leaq -0x14(%rbp), %rax
movl (%rax), %eax
subl $0x1494254, %eax        # imm = 0x1494254
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x197c4
movq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x20, %edx
callq 0x64bb0
cmpq $0x20, %rax
je 0x196ca
jmp 0x19862
cmpl $0x0, -0x1c(%rbp)
je 0x196fe
movl $0x0, -0x18(%rbp)
cmpl $0x8, -0x18(%rbp)
jge 0x196fc
movslq -0x18(%rbp), %rax
leaq -0x70(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
callq 0x1a550
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x196d7
jmp 0x196fe
movl -0x70(%rbp), %edi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0xe, %ecx
movl $0x5, %r8d
callq 0x15fa0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x19729
jmp 0x19862
movl -0x54(%rbp), %ecx
addl $0x1c, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edi
callq 0x47a0
movq -0x28(%rbp), %rcx
movq %rax, 0x38(%rcx)
cmpq $0x0, %rax
jne 0x19757
jmp 0x19862
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x6c(%rbp), %rcx
movq %rcx, (%rax)
movq -0x64(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x5c(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x54(%rbp), %ecx
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rsi
addq $0x1c, %rsi
movl -0x54(%rbp), %eax
movl %eax, %edx
callq 0x64bb0
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
je 0x197a3
jmp 0x19862
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x2, %edx
callq 0x1a580
cmpl $0x0, %eax
jge 0x197bf
jmp 0x19862
jmp 0x1984d
leaq -0x14(%rbp), %rax
movl (%rax), %eax
subl $0x1494142, %eax        # imm = 0x1494142
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1983e
movq -0x38(%rbp), %rdi
leaq -0x74(%rbp), %rsi
movl $0x4, %edx
callq 0x64bb0
cmpq $0x4, %rax
je 0x197f4
jmp 0x19862
cmpl $0x0, -0x1c(%rbp)
je 0x19803
leaq -0x74(%rbp), %rdi
callq 0x1a550
movl -0x74(%rbp), %edi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0xe, %ecx
movl $0x5, %r8d
callq 0x15fa0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x1a580
cmpl $0x0, %eax
jge 0x1983c
jmp 0x19862
jmp 0x1984b
callq 0x4070
movl $0x16, (%rax)
jmp 0x19862
jmp 0x1984d
jmp 0x1984f
movq -0x38(%rbp), %rdi
callq 0x65880
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x19885
movq -0x38(%rbp), %rdi
callq 0x65880
movq -0x28(%rbp), %rdi
callq 0x171d0
movq -0x30(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)