// File: karel-brinda[P]ococo/ext/htslib/faidx.c
// Function: fai_build_core
faidx_t *fai_build_core(BGZF *bgzf)
{
    kstring_t name = { 0, 0, NULL };
    int c;
    int line_len, line_blen, state;
    int l1, l2;
    faidx_t *idx;
    uint64_t offset;
    int64_t len;

    idx = (faidx_t*)calloc(1, sizeof(faidx_t));
    idx->hash = kh_init(s);
    len = line_len = line_blen = -1; state = 0; l1 = l2 = -1; offset = 0;
    while ( (c=bgzf_getc(bgzf))>=0 ) {
        if (c == '\n') { // an empty line
            if (state == 1) {
                offset = bgzf_utell(bgzf);
                continue;
            } else if ((state == 0 && len < 0) || state == 2) continue;
            else if (state == 0) { state = 2; continue; }
        }
        if (c == '>') { // fasta header
            if (len >= 0) {
                if (fai_insert_index(idx, name.s, len, line_len, line_blen, offset) != 0)
                    goto fail;
            }

            name.l = 0;
            while ((c = bgzf_getc(bgzf)) >= 0)
                if (! isspace(c)) kputc_(c, &name);
                else if (name.l > 0 || c == '\n') break;
            kputsn("", 0, &name);

            if ( c<0 ) {
                fprintf(stderr, "[fai_build_core] the last entry has no sequence\n");
                goto fail;
            }
            if (c != '\n') while ( (c=bgzf_getc(bgzf))>=0 && c != '\n');
            state = 1; len = 0;
            offset = bgzf_utell(bgzf);
        } else {
            if (state == 3) {
                fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name.s);
                goto fail;
            }
            if (state == 2) state = 3;
            l1 = l2 = 0;
            do {
                ++l1;
                if (isgraph(c)) ++l2;
            } while ( (c=bgzf_getc(bgzf))>=0 && c != '\n');
            if (state == 3 && l2) {
                fprintf(stderr, "[fai_build_core] different line length in sequence '%s'.\n", name.s);
                goto fail;
            }
            ++l1; len += l2;
            if (state == 1) line_len = l1, line_blen = l2, state = 0;
            else if (state == 0) {
                if (l1 != line_len || l2 != line_blen) state = 2;
            }
        }
    }

    if (len >= 0) {
        if (fai_insert_index(idx, name.s, len, line_len, line_blen, offset) != 0)
            goto fail;
    } else {
        goto fail;
    }

    free(name.s);
    return idx;

fail:
    free(name.s);
    fai_destroy(idx);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x42d0
movl $0x1, %edi
movl $0x20, %esi
callq 0x4370
movq %rax, -0x48(%rbp)
callq 0x68250
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movq $-0x1, -0x58(%rbp)
movl $0x0, -0x38(%rbp)
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66000
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jl 0x681e4
cmpl $0xa, -0x2c(%rbp)
jne 0x67fac
cmpl $0x1, -0x38(%rbp)
jne 0x67f82
movq -0x10(%rbp), %rdi
callq 0x66f50
movq %rax, -0x50(%rbp)
jmp 0x67f52
cmpl $0x0, -0x38(%rbp)
jne 0x67f8f
cmpq $0x0, -0x58(%rbp)
jl 0x67f95
cmpl $0x2, -0x38(%rbp)
jne 0x67f97
jmp 0x67f52
cmpl $0x0, -0x38(%rbp)
jne 0x67fa6
movl $0x2, -0x38(%rbp)
jmp 0x67f52
jmp 0x67fa8
jmp 0x67faa
jmp 0x67fac
cmpl $0x3e, -0x2c(%rbp)
jne 0x680c6
cmpq $0x0, -0x58(%rbp)
jl 0x67fe5
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
movq -0x50(%rbp), %r9
callq 0x68270
cmpl $0x0, %eax
je 0x67fe3
jmp 0x68225
jmp 0x67fe5
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66000
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jl 0x68039
callq 0x4610
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
jne 0x68026
movl -0x2c(%rbp), %edi
leaq -0x28(%rbp), %rsi
callq 0x68420
jmp 0x68037
cmpq $0x0, -0x28(%rbp)
ja 0x68033
cmpl $0xa, -0x2c(%rbp)
jne 0x68035
jmp 0x68039
jmp 0x68037
jmp 0x67fed
leaq 0x1448c(%rip), %rdi     # 0x7c4cc
xorl %esi, %esi
leaq -0x28(%rbp), %rdx
callq 0x68490
cmpl $0x0, -0x2c(%rbp)
jge 0x6806e
movq 0x21f78(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x16392(%rip), %rsi     # 0x7e3f4
movb $0x0, %al
callq 0x4830
jmp 0x68225
cmpl $0xa, -0x2c(%rbp)
je 0x680a5
jmp 0x68076
movq -0x10(%rbp), %rdi
callq 0x66000
movl %eax, %ecx
movl %ecx, -0x2c(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x59(%rbp)
jl 0x68098
cmpl $0xa, -0x2c(%rbp)
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x680a1
jmp 0x680a3
jmp 0x68076
jmp 0x680a5
movl $0x1, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66f50
movq %rax, -0x50(%rbp)
jmp 0x681df
cmpl $0x3, -0x38(%rbp)
jne 0x680ed
movq 0x21efd(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x16344(%rip), %rsi     # 0x7e425
movb $0x0, %al
callq 0x4830
jmp 0x68225
cmpl $0x2, -0x38(%rbp)
jne 0x680fa
movl $0x3, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
callq 0x4610
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x68134
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x68136
movq -0x10(%rbp), %rdi
callq 0x66000
movl %eax, %ecx
movl %ecx, -0x2c(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x5a(%rbp)
jl 0x68158
cmpl $0xa, -0x2c(%rbp)
setne %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0x68108
cmpl $0x3, -0x38(%rbp)
jne 0x6818c
cmpl $0x0, -0x40(%rbp)
je 0x6818c
movq 0x21e5e(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x162eb(%rip), %rsi     # 0x7e46b
movb $0x0, %al
callq 0x4830
jmp 0x68225
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movslq -0x40(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x1, -0x38(%rbp)
jne 0x681bc
movl -0x3c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
jmp 0x681dd
cmpl $0x0, -0x38(%rbp)
jne 0x681db
movl -0x3c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jne 0x681d2
movl -0x40(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x681d9
movl $0x2, -0x38(%rbp)
jmp 0x681db
jmp 0x681dd
jmp 0x681df
jmp 0x67f52
cmpq $0x0, -0x58(%rbp)
jl 0x68210
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
movq -0x50(%rbp), %r9
callq 0x68270
cmpl $0x0, %eax
je 0x6820e
jmp 0x68225
jmp 0x68212
jmp 0x68225
movq -0x18(%rbp), %rdi
callq 0x46a0
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6823f
movq -0x18(%rbp), %rdi
callq 0x46a0
movq -0x48(%rbp), %rdi
callq 0x68520
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)