// File: /workspace/github_cpp_makefile_star2_O1/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), %rbp
movq 0x8(%rsi), %rax
cmpq %rax, %rbp
je 0x12f76
movq %rsi, %r14
leaq 0x248(%rbx), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x260(%rbx), %rcx
movq %rcx, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x3d5b0(%rip), %r15     # 0x50210
movq %rbp, 0x10(%rsp)
movl 0x278(%rbx), %ecx
cmpq $0x4, %rcx
ja 0x12d9f
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
cmpq %rax, %rbp
je 0x12f67
cmpb $0x1a, (%rbp)
jne 0x12c92
movq %rbp, %rcx
jmp 0x12ccb
leaq 0x1(%rbp), %rcx
cmpq %rax, %rcx
je 0x12cb7
movb $0x1, %dl
cmpb $0x1a, (%rcx)
jne 0x12cb2
movl $0x2, 0x278(%rbx)
addq $0x2, %rbp
xorl %edx, %edx
movq %rbp, %rcx
jmp 0x12cc3
movl $0x1, 0x278(%rbx)
xorl %edx, %edx
testb %dl, %dl
je 0x12eee
movq %rcx, %rbp
incq %rbp
jmp 0x12c7e
movb (%rbp), %al
cmpb $0x1a, %al
jne 0x12d9f
movq 0x250(%rbx), %rsi
movq %rsi, %rcx
subq 0x248(%rbx), %rcx
cmpq $0x6, %rcx
ja 0x12ed3
cmpq 0x258(%rbx), %rsi
je 0x12ef3
movb %al, (%rsi)
incq 0x250(%rbx)
jmp 0x12f07
movb (%rbp), %al
leal -0x31(%rax), %ecx
cmpb $0x5, %cl
jae 0x12d98
addb $-0x30, %al
movzbl %al, %eax
movl %eax, 0x240(%rbx)
movq 0x248(%rbx), %rax
cmpq %rax, 0x250(%rbx)
je 0x12d41
movq %rax, 0x250(%rbx)
movq 0x260(%rbx), %rax
cmpq %rax, 0x268(%rbx)
je 0x12d58
movq %rax, 0x268(%rbx)
movl $0x3, 0x278(%rbx)
jmp 0x12d90
movl 0x240(%rbx), %eax
decl %eax
cmpl $0x4, %eax
ja 0x12db9
movl %eax, %eax
leaq 0x3d4c6(%rip), %rcx     # 0x50240
movq (%rcx,%rax,8), %r12
jmp 0x12dbc
cmpb $0x1a, (%rbp)
jne 0x12d9f
movl $0x2, 0x278(%rbx)
incq %rbp
jmp 0x12f67
movl %r12d, 0x240(%rbx)
movl %r12d, 0x234(%rbx)
movb $0x0, 0x230(%rbx)
movl %r12d, 0x278(%rbx)
jmp 0x12f67
xorl %r12d, %r12d
movq 0x8(%r14), %rax
cmpq %rax, %rbp
je 0x12e92
movq 0x268(%rbx), %rsi
movq %rsi, %rcx
subq 0x260(%rbx), %rcx
cmpq %r12, %rcx
jae 0x12e92
leaq 0x1(%rbp), %r13
movb (%rbp), %cl
movb %cl, 0xf(%rsp)
cmpb $0x1a, %cl
jne 0x12e06
cmpq %rax, %r13
je 0x12e45
cmpb $0x1a, (%r13)
jne 0x12e53
addq $0x2, %rbp
movq %rbp, %r13
movq 0x250(%rbx), %rax
movq %rax, %rdx
subq 0x248(%rbx), %rdx
cmpq $0x6, %rdx
ja 0x12e31
cmpq 0x258(%rbx), %rax
je 0x12e6a
movb %cl, (%rax)
incq 0x250(%rbx)
jmp 0x12e83
cmpq 0x270(%rbx), %rsi
je 0x12e74
movb %cl, (%rsi)
incq 0x268(%rbx)
jmp 0x12e83
movl $0x4, 0x278(%rbx)
xorl %eax, %eax
jmp 0x12e85
xorl %eax, %eax
movl %eax, 0x234(%rbx)
movb $0x0, 0x230(%rbx)
movl %eax, 0x278(%rbx)
jmp 0x12e85
movq 0x20(%rsp), %rdi
movq %rax, %rsi
jmp 0x12e79
movq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x19aea
movb $0x1, %al
movq %r13, %rbp
testb %al, %al
jne 0x12dbc
jmp 0x12e95
movq %rbp, %r13
movq 0x268(%rbx), %rax
subq 0x260(%rbx), %rax
cmpq %r12, %rax
jb 0x12ec8
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x12fb4
movl $0x1, 0x278(%rbx)
movq %r13, 0x10(%rsp)
movq %r13, %rbp
xorl %r12d, %r12d
jmp 0x12f67
movq 0x268(%rbx), %rsi
cmpq 0x270(%rbx), %rsi
je 0x12efa
movb %al, (%rsi)
incq 0x268(%rbx)
jmp 0x12f07
movq %rcx, %rbp
jmp 0x12f67
movq 0x20(%rsp), %rdi
jmp 0x12eff
movq 0x18(%rsp), %rdi
movq %rbp, %rdx
callq 0x19aea
movq 0x268(%rbx), %rax
subq 0x260(%rbx), %rax
movl 0x240(%rbx), %ecx
decl %ecx
cmpl $0x4, %ecx
ja 0x12f31
movl %ecx, %ecx
leaq 0x3d315(%rip), %rdx     # 0x50240
movq (%rdx,%rcx,8), %rcx
jmp 0x12f33
xorl %ecx, %ecx
incq %rbp
cmpq %rcx, %rax
jae 0x12f47
movl $0x3, 0x278(%rbx)
jmp 0x12f67
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x12fb4
movl $0x1, 0x278(%rbx)
movq %rbp, 0x10(%rsp)
movq 0x8(%r14), %rax
cmpq %rax, %rbp
jne 0x12c65
jmp 0x12f7b
movq %rbp, 0x10(%rsp)
cmpb $0x0, 0x230(%rbx)
jne 0x12f8e
subl 0x10(%rsp), %eax
addl %eax, 0x238(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop