// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13e30
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13e60
movq %rax, -0x28(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x13e90
testb $0x1, %al
jne 0x1d396
jmp 0x1d841
movq -0x90(%rbp), %rax
movl 0x278(%rax), %eax
movq %rax, -0x98(%rbp)
subq $0x4, %rax
ja 0x1d830
movq -0x98(%rbp), %rax
leaq 0x95e56(%rip), %rcx     # 0xb3218
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x1d3cd
movq -0x10(%rbp), %rdi
callq 0x13e60
movq %rax, -0x30(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x13e90
testb $0x1, %al
jne 0x1d3f0
jmp 0x1d4a2
leaq -0x18(%rbp), %rdi
callq 0x13ed0
movzbl (%rax), %eax
cmpl $0x1a, %eax
je 0x1d492
leaq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x21510
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13e60
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x21550
testb $0x1, %al
jne 0x1d437
jmp 0x1d452
movq -0x90(%rbp), %rax
movl $0x1, 0x278(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x1d4a2
leaq -0x38(%rbp), %rdi
callq 0x13ed0
movzbl (%rax), %eax
cmpl $0x1a, %eax
jne 0x1d490
movq -0x90(%rbp), %rax
movl $0x2, 0x278(%rax)
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x21510
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x1d4a2
jmp 0x1d492
jmp 0x1d494
leaq -0x18(%rbp), %rdi
callq 0x13ef0
jmp 0x1d3cd
jmp 0x1d83c
leaq -0x18(%rbp), %rdi
callq 0x13ed0
movzbl (%rax), %eax
cmpl $0x1a, %eax
jne 0x1d4d4
movq -0x90(%rbp), %rax
movl $0x2, 0x278(%rax)
leaq -0x18(%rbp), %rdi
callq 0x13ef0
jmp 0x1d4e5
movq -0x90(%rbp), %rdi
callq 0x1d8a0
jmp 0x1d83c
jmp 0x1d83c
leaq -0x18(%rbp), %rdi
callq 0x13ed0
movzbl (%rax), %edi
callq 0x21590
movl %eax, %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x240(%rax)
cmpl $0x0, 0x240(%rax)
jne 0x1d524
movq -0x90(%rbp), %rdi
callq 0x1d8a0
jmp 0x1d83c
movq -0x90(%rbp), %rdi
addq $0x248, %rdi            # imm = 0x248
callq 0x21600
movq -0x90(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
callq 0x21600
movq -0x90(%rbp), %rax
movl $0x3, 0x278(%rax)
leaq -0x18(%rbp), %rdi
callq 0x13ef0
jmp 0x1d83c
movq -0x90(%rbp), %rax
movl 0x240(%rax), %edi
callq 0x21620
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13e60
movq %rax, -0x58(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x13e90
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x99(%rbp)
jne 0x1d5aa
jmp 0x1d5ca
movq -0x90(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
callq 0x14c40
cmpq -0x50(%rbp), %rax
setb %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x1d5d9
jmp 0x1d6a8
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x21690
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x13ed0
movb (%rax), %al
movb %al, -0x59(%rbp)
movzbl -0x59(%rbp), %eax
cmpl $0x1a, %eax
jne 0x1d65a
movq -0x10(%rbp), %rdi
callq 0x13e60
movq %rax, -0x70(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x21550
testb $0x1, %al
jne 0x1d61f
jmp 0x1d632
movq -0x90(%rbp), %rax
movl $0x4, 0x278(%rax)
jmp 0x1d6a8
leaq -0x18(%rbp), %rdi
callq 0x13ed0
movzbl (%rax), %eax
cmpl $0x1a, %eax
je 0x1d651
movq -0x90(%rbp), %rdi
callq 0x1d8a0
jmp 0x1d6a8
leaq -0x18(%rbp), %rdi
callq 0x13ef0
movq -0x90(%rbp), %rdi
addq $0x248, %rdi            # imm = 0x248
callq 0x14c40
cmpq $0x7, %rax
jae 0x1d68c
movq -0x90(%rbp), %rdi
addq $0x248, %rdi            # imm = 0x248
leaq -0x59(%rbp), %rsi
callq 0x216d0
jmp 0x1d6a3
movq -0x90(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
leaq -0x59(%rbp), %rsi
callq 0x216d0
jmp 0x1d57f
movq -0x90(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
callq 0x14c40
cmpq -0x50(%rbp), %rax
jb 0x1d6f3
movq -0x90(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
callq 0x1d8d0
movq -0x90(%rbp), %rax
movl $0x1, 0x278(%rax)
jmp 0x1d83c
leaq -0x18(%rbp), %rdi
callq 0x13ed0
movzbl (%rax), %eax
cmpl $0x1a, %eax
je 0x1d71a
movq -0x90(%rbp), %rdi
callq 0x1d8a0
jmp 0x1d83c
movq -0x90(%rbp), %rdi
addq $0x248, %rdi            # imm = 0x248
callq 0x14c40
cmpq $0x7, %rax
jae 0x1d770
movq -0x90(%rbp), %rax
addq $0x248, %rax            # imm = 0x248
movq %rax, -0xa8(%rbp)
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x21690
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x13ed0
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0x216d0
jmp 0x1d7ab
movq -0x90(%rbp), %rax
addq $0x260, %rax            # imm = 0x260
movq %rax, -0xb0(%rbp)
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x21690
movq %rax, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x13ed0
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x216d0
movq -0x90(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
callq 0x14c40
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, -0xb8(%rbp)
movl 0x240(%rax), %edi
callq 0x21620
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
jb 0x1d81d
movq -0x90(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
callq 0x1d8d0
movq -0x90(%rbp), %rax
movl $0x1, 0x278(%rax)
jmp 0x1d82e
movq -0x90(%rbp), %rax
movl $0x3, 0x278(%rax)
jmp 0x1d83c
movq -0x90(%rbp), %rdi
callq 0x1d8a0
jmp 0x1d373
movq -0x90(%rbp), %rax
testb $0x1, 0x230(%rax)
jne 0x1d88a
movq -0x10(%rbp), %rdi
callq 0x13e60
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x21740
movq %rax, %rdx
movq -0x90(%rbp), %rax
movl 0x238(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x238(%rax)
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)