// File: /workspace/github_cpp_makefile_star2_O3/flightaware[P]beast-splitter/beast_input.cc
// Function: beast::BeastInput::parse_input(std::vector<unsigned char, std::allocator<unsigned char>> const&)
void BeastInput::parse_input(const helpers::bytebuf &buf) {
    auto p = buf.begin();
    auto last_good_message_end = p;

    while (p != buf.end()) {
        switch (state) {
        case ParserState::RESYNC:
            // Scanning for <not-1A> <1A> <typebyte> <data...>
            for (; p != buf.end(); ++p) {
                if (*p != 0x1A) {
                    auto q = p + 1;
                    if (q == buf.end()) {
                        // can't decide yet
                        state = ParserState::READ_1A;
                        p = q;
                        break;
                    }

                    if (*q == 0x1A) {
                        state = ParserState::READ_TYPE;
                        p = q + 1;
                        break;
                    }
                }
            }

            break;

        case ParserState::READ_1A:
            // Expecting <1A> <typebyte> <data...>
            if (*p == 0x1A) {
                state = ParserState::READ_TYPE;
                ++p;
            } else {
                lost_sync();
                break;
            }
            break;

        case ParserState::READ_TYPE:
            // Expecting <typebyte> <data...>
            messagetype = messagetype_from_byte(*p);
            if (messagetype == modes::MessageType::INVALID) {
                lost_sync();
                break;
            } else {
                metadata.clear();
                messagedata.clear();
                state = ParserState::READ_DATA;
                ++p;
            }

            break;

        case ParserState::READ_DATA: {
            // Reading message contents
            std::size_t msglen = modes::message_size(messagetype);
            while (p != buf.end() && messagedata.size() < msglen) {
                uint8_t b = *p++;
                if (b == 0x1A) {
                    if (p == buf.end()) {
                        // Can't handle it this time around.
                        state = ParserState::READ_ESCAPED_1A;
                        break;
                    }

                    if (*p != 0x1A) {
                        lost_sync();
                        break;
                    }

                    // valid 1A escape, consume it
                    ++p;
                }

                if (metadata.size() < 7)
                    metadata.push_back(b);
                else
                    messagedata.push_back(b);
            }

            if (messagedata.size() >= msglen) {
                // Done with this message.
                saw_good_message();
                last_good_message_end = p;
                dispatch_message();
                state = ParserState::READ_1A;
            }
        } break;

        case ParserState::READ_ESCAPED_1A: {
            // This happens if we see a 1A as the final
            // byte of a read; READ_DATA cannot handle
            // the escape immediately and sets state to
            // READ_ESCAPED_1A to handle the first
            // byte of the next read.

            if (*p != 0x1A) {
                lost_sync();
                break;
            }

            // valid 1A escape
            if (metadata.size() < 7)
                metadata.push_back(*p++);
            else
                messagedata.push_back(*p++);

            if (messagedata.size() >= modes::message_size(messagetype)) {
                saw_good_message();
                last_good_message_end = p;
                dispatch_message();
                state = ParserState::READ_1A;
            } else {
                state = ParserState::READ_DATA;
            }
        } break;

        default:
            // WAT
            lost_sync();
            break;
        }
    }

    if (!good_sync) {
        bad_bytes_count += (buf.end() - last_good_message_end);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq (%rsi), %r15
movq 0x8(%rsi), %rax
cmpq %rax, %r15
je 0x11dd7
movq %rsi, %r14
leaq 0x248(%rbx), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x260(%rbx), %rcx
movq %rcx, 0x18(%rsp)
xorl %ebp, %ebp
leaq 0x3c7a5(%rip), %r12     # 0x4e2b0
movq %r15, 0x10(%rsp)
movl 0x278(%rbx), %ecx
cmpq $0x4, %rcx
ja 0x11c2e
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
cmpq %rax, %r15
je 0x11dc8
incq %r15
cmpb $0x1a, -0x1(%r15)
je 0x11b4f
cmpq %rax, %r15
je 0x11d14
cmpb $0x1a, (%r15)
je 0x11c16
leaq 0x1(%r15), %rcx
cmpq %rax, %r15
movq %rcx, %r15
jne 0x11b35
movq %rax, %r15
jmp 0x11dc8
cmpb $0x1a, (%r15)
jne 0x11c2e
movq 0x250(%rbx), %rsi
movq %rsi, %rax
subq 0x248(%rbx), %rax
cmpq $0x6, %rax
ja 0x11cf8
cmpq 0x258(%rbx), %rsi
je 0x11d5b
movb $0x1a, (%rsi)
incq 0x250(%rbx)
jmp 0x11d6f
movb (%r15), %al
leal -0x31(%rax), %ecx
cmpb $0x5, %cl
jae 0x11c28
addb $-0x30, %al
movzbl %al, %eax
movl %eax, 0x240(%rbx)
movq 0x248(%rbx), %rax
cmpq %rax, 0x250(%rbx)
je 0x11bd1
movq %rax, 0x250(%rbx)
movq 0x260(%rbx), %rax
cmpq %rax, 0x268(%rbx)
je 0x11be8
movq %rax, 0x268(%rbx)
movl $0x3, 0x278(%rbx)
jmp 0x11c20
movl 0x240(%rbx), %ecx
decl %ecx
cmpl $0x4, %ecx
ja 0x11c46
movl %ecx, %ecx
leaq 0x3c6d6(%rip), %rdx     # 0x4e2e0
movq (%rdx,%rcx,8), %r13
jmp 0x11c49
cmpb $0x1a, (%r15)
jne 0x11c2e
movl $0x2, 0x278(%rbx)
incq %r15
jmp 0x11dc8
movl %ebp, 0x240(%rbx)
movl %ebp, 0x234(%rbx)
movb $0x0, 0x230(%rbx)
movl %ebp, 0x278(%rbx)
jmp 0x11dc8
xorl %r13d, %r13d
cmpq %rax, %r15
je 0x11d46
movq 0x268(%rbx), %rsi
movq %rsi, %rcx
subq 0x260(%rbx), %rcx
cmpq %r13, %rcx
jae 0x11d46
leaq 0x1(%r15), %rdx
movb (%r15), %cl
movb %cl, 0xf(%rsp)
cmpb $0x1a, %cl
jne 0x11c94
cmpq %rax, %rdx
je 0x11d23
cmpb $0x1a, (%rdx)
jne 0x11d2a
addq $0x2, %r15
jmp 0x11c97
movq %rdx, %r15
movq 0x250(%rbx), %rax
movq %rax, %rdx
subq 0x248(%rbx), %rdx
cmpq $0x6, %rdx
ja 0x11cc2
cmpq 0x258(%rbx), %rax
je 0x11cd6
movb %cl, (%rax)
incq 0x250(%rbx)
jmp 0x11cef
cmpq 0x270(%rbx), %rsi
je 0x11ce0
movb %cl, (%rsi)
incq 0x268(%rbx)
jmp 0x11cef
movq 0x20(%rsp), %rdi
movq %rax, %rsi
jmp 0x11ce5
movq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x18716
movq 0x8(%r14), %rax
jmp 0x11c49
movq 0x268(%rbx), %rsi
cmpq 0x270(%rbx), %rsi
je 0x11d62
movb $0x1a, (%rsi)
incq 0x268(%rbx)
jmp 0x11d6f
movl $0x1, 0x278(%rbx)
jmp 0x11dc8
movl $0x4, %eax
jmp 0x11d3d
movl $0x0, 0x234(%rbx)
movb $0x0, 0x230(%rbx)
xorl %eax, %eax
movl %eax, 0x278(%rbx)
movq %rdx, %r15
movq 0x268(%rbx), %rax
subq 0x260(%rbx), %rax
cmpq %r13, %rax
jb 0x11dc8
jmp 0x11da8
movq 0x20(%rsp), %rdi
jmp 0x11d67
movq 0x18(%rsp), %rdi
movq %r15, %rdx
callq 0x18716
incq %r15
movl 0x240(%rbx), %eax
decl %eax
cmpl $0x4, %eax
ja 0x11da8
movq 0x268(%rbx), %rcx
subq 0x260(%rbx), %rcx
movl %eax, %eax
leaq 0x3c54a(%rip), %rdx     # 0x4e2e0
cmpq (%rdx,%rax,8), %rcx
jae 0x11da8
movl $0x3, 0x278(%rbx)
jmp 0x11dc8
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x11e16
movl $0x1, 0x278(%rbx)
movq %r15, 0x10(%rsp)
movq 0x8(%r14), %rax
cmpq %rax, %r15
jne 0x11b10
jmp 0x11ddc
movq %r15, 0x10(%rsp)
cmpb $0x0, 0x230(%rbx)
jne 0x11df1
subl 0x10(%rsp), %r15d
addl %r15d, 0x238(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq