// File: /workspace/github_cpp_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %r15
leaq 0x248(%rdi), %rbp
leaq 0x260(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x30c1f(%rip), %r13     # 0x432d0
movq %r15, 0x8(%rsp)
movq 0x8(%r14), %rax
cmpq %rax, %r15
je 0x128de
movl 0x278(%rbx), %ecx
cmpq $0x4, %rcx
ja 0x1285d
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
incq %r15
leaq -0x1(%r15), %rcx
cmpq %rax, %rcx
je 0x12877
cmpb $0x1a, (%rcx)
je 0x126dd
cmpq %rax, %r15
je 0x12889
cmpb $0x1a, (%r15)
jne 0x126dd
jmp 0x1284b
cmpb $0x1a, (%r15)
jne 0x1285d
movq 0x250(%rbx), %rax
subq 0x248(%rbx), %rax
cmpq $0x7, %rax
movq 0x10(%rsp), %rdi
cmovbq %rbp, %rdi
movq %r15, %rsi
callq 0x139fe
incq %r15
movq 0x268(%rbx), %r12
subq 0x260(%rbx), %r12
movl 0x240(%rbx), %edi
callq 0x139e6
cmpq %rax, %r12
jae 0x128be
movl $0x3, 0x278(%rbx)
jmp 0x126b6
movzbl (%r15), %edi
callq 0x139d2
movl %eax, 0x240(%rbx)
testl %eax, %eax
je 0x1285d
movq 0x248(%rbx), %rax
cmpq %rax, 0x250(%rbx)
je 0x12795
movq %rax, 0x250(%rbx)
movq 0x260(%rbx), %rax
cmpq %rax, 0x268(%rbx)
je 0x127ac
movq %rax, 0x268(%rbx)
movl $0x3, 0x278(%rbx)
incq %r15
jmp 0x126b6
movl 0x240(%rbx), %edi
callq 0x139e6
movq %rax, %r13
movq %r15, %r12
movq 0x8(%r14), %rcx
movq 0x268(%rbx), %rax
subq 0x260(%rbx), %rax
cmpq %rcx, %r12
je 0x1287f
cmpq %r13, %rax
jae 0x1287f
leaq 0x1(%r12), %r15
movb (%r12), %dl
movb %dl, 0x7(%rsp)
cmpb $0x1a, %dl
jne 0x1281a
cmpq %rcx, %r15
je 0x12884
cmpb $0x1a, (%r15)
jne 0x12898
addq $0x2, %r12
jmp 0x1281d
movq %r15, %r12
movq 0x250(%rbx), %rax
subq 0x248(%rbx), %rax
movq %rbp, %rdi
cmpq $0x7, %rax
jb 0x12839
movq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rsi
callq 0x139fe
jmp 0x127cf
cmpb $0x1a, (%r15)
jne 0x1285d
movl $0x2, 0x278(%rbx)
incq %r15
jmp 0x126b6
andl $0x0, 0x234(%rbx)
movb $0x0, 0x230(%rbx)
andl $0x0, 0x278(%rbx)
jmp 0x126b6
movq %rax, %r15
jmp 0x126b6
movq %r12, %r15
jmp 0x128ae
pushq $0x4
popq %rcx
jmp 0x128a8
movl $0x1, 0x278(%rbx)
jmp 0x126b6
andl $0x0, 0x234(%rbx)
movb $0x0, 0x230(%rbx)
xorl %ecx, %ecx
movl %ecx, 0x278(%rbx)
cmpq %r13, %rax
leaq 0x30a18(%rip), %r13     # 0x432d0
jb 0x126b6
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x12918
movl $0x1, 0x278(%rbx)
jmp 0x126b1
cmpb $0x0, 0x230(%rbx)
jne 0x128f3
subl 0x8(%rsp), %r15d
addl %r15d, 0x238(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq