// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
movl $0x1, %edi
movl $0x20, %esi
callq 0x4360
movq %rax, %rbx
movl $0x1, %edi
movl $0x28, %esi
callq 0x4360
movq %rbx, 0x18(%rsp)
movq %rax, 0x18(%rbx)
movq $-0x1, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
xorl %eax, %eax
movq $0x0, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq 0x30(%rsp), %r13
movl %eax, %ebp
testq %r13, %r13
sets %al
testl %ebp, %ebp
sete %cl
andb %al, %cl
cmpl $0x2, %ebp
sete %bl
orb %cl, %bl
movq %r15, %rdi
callq 0x4476a
testl %eax, %eax
js 0x456e5
movl %eax, %r12d
cmpl $0xa, %eax
jne 0x4547b
cmpl $0x1, %ebp
je 0x45467
testb %bl, %bl
jne 0x4543b
movl $0x2, %eax
testl %ebp, %ebp
je 0x45424
jmp 0x4548a
movq %r15, %rdi
callq 0x44fbc
movq %rax, 0x20(%rsp)
movl $0x1, %eax
jmp 0x45424
cmpl $0x3e, %r12d
movq 0x28(%rsp), %rbx
je 0x45523
cmpl $0x3, %ebp
je 0x4571e
xorl %r13d, %r13d
movl $0x1, %r14d
callq 0x4580
movq %rax, %rbx
movl %r13d, %eax
movq (%rbx), %rcx
movl %r12d, %edx
movzwl (%rcx,%rdx,2), %r13d
shrl $0xf, %r13d
addl %eax, %r13d
movq %r15, %rdi
callq 0x4476a
incl %r14d
testl %eax, %eax
js 0x454d0
movl %eax, %r12d
cmpl $0xa, %eax
jne 0x454a4
cmpl $0x2, %ebp
movl %ebp, %eax
movl $0x3, %ecx
cmovel %ecx, %eax
cmpl $0x3, %eax
jne 0x454eb
testl %r13d, %r13d
jne 0x45731
movl %r13d, %ecx
addq %rcx, 0x30(%rsp)
testl %ebp, %ebp
je 0x4550a
cmpl $0x1, %ebp
movq 0x28(%rsp), %rcx
jne 0x4541f
jmp 0x456d4
xorl 0x14(%rsp), %r14d
xorl 0x10(%rsp), %r13d
xorl %eax, %eax
orl %r14d, %r13d
setne %al
addl %eax, %eax
jmp 0x4541f
testq %r13, %r13
js 0x45550
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %r8d
movq 0x20(%rsp), %r9
callq 0x4578b
testl %eax, %eax
jne 0x4575d
movq %r15, %rdi
callq 0x4476a
movl %eax, %ebp
testl %eax, %eax
js 0x45617
callq 0x4580
movq %rax, %r13
xorl %r12d, %r12d
movq (%r13), %rax
movl %ebp, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x455e3
leaq 0x1(%r12), %r14
cmpq %r14, %rbx
jae 0x455f2
movq %rbx, 0x28(%rsp)
movq %r12, %rax
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %rbx
shrq $0x20, %rbx
orq %rax, %rbx
incq %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x48d0
testq %rax, %rax
jne 0x455f7
movq %r12, %r14
movq 0x28(%rsp), %rbx
jmp 0x45600
testq %r12, %r12
jne 0x4561c
cmpl $0xa, %ebp
je 0x4561c
xorl %r14d, %r14d
jmp 0x45600
movq 0x8(%rsp), %rax
movb %bpl, (%rax,%r12)
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x4476a
movl %eax, %ebp
movq %r14, %r12
testl %eax, %eax
jns 0x4556d
jmp 0x4561f
xorl %r14d, %r14d
jmp 0x4561f
movq %r12, %r14
leaq 0x2(%r14), %rax
cmpq %rax, %rbx
jae 0x45681
movq %rbx, %r12
leaq 0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rbx
shrq $0x20, %rbx
orq %rcx, %rbx
incq %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x48d0
testq %rax, %rax
jne 0x45686
jmp 0x45693
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%r14)
movq %rbx, %r12
movq %rax, 0x8(%rsp)
testl %ebp, %ebp
js 0x45758
cmpl $0xa, %ebp
je 0x456b1
movq %r15, %rdi
callq 0x4476a
testl %eax, %eax
js 0x456b1
cmpl $0xa, %eax
jne 0x456a0
movq %r15, %rdi
callq 0x44fbc
movq %rax, 0x20(%rsp)
movl $0x1, %eax
movq $0x0, 0x30(%rsp)
movq %r12, %rcx
jmp 0x4541a
xorl %eax, %eax
movl %r14d, 0x14(%rsp)
movl %r13d, 0x10(%rsp)
jmp 0x4541a
testq %r13, %r13
js 0x4575d
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
movq %r13, %rdx
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %r8d
movq 0x20(%rsp), %r9
callq 0x4578b
testl %eax, %eax
jne 0x45767
movq %r14, %rdi
callq 0x4610
jmp 0x45779
movq 0x1c8a3(%rip), %rax     # 0x61fc8
movq (%rax), %rdi
leaq 0xeae6(%rip), %rsi      # 0x54215
jmp 0x45742
movq 0x1c890(%rip), %rax     # 0x61fc8
movq (%rax), %rdi
leaq 0xeb19(%rip), %rsi      # 0x5425b
movq 0x8(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x4770
movq 0x18(%rsp), %rbx
jmp 0x45767
callq 0x5e7f
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x45a85
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq