// File: flightaware[P]beast-splitter/beast_input_serial.cc
// Function: beast::SerialInput::start_reading(boost::system::error_code const&)
void SerialInput::start_reading(const boost::system::error_code &ec) {
    if (ec) {
        assert(ec == boost::asio::error::operation_aborted);
        return;
    }

    auto self(std::static_pointer_cast<SerialInput>(shared_from_this()));
    std::shared_ptr<helpers::bytebuf> buf;

    buf.swap(readbuf);
    if (buf) {
        buf->resize(read_buffer_size);
    } else {
        buf = std::make_shared<helpers::bytebuf>(read_buffer_size);
    }

    read_timer.expires_from_now(read_interval);
    port.async_read_some(boost::asio::buffer(*buf), [this, self, buf](const boost::system::error_code &ec, std::size_t len) {
        if (ec) {
            readbuf = buf;
            handle_error(ec);
        } else {
            buf->resize(len);
            parse_input(*buf);
            check_framing_errors();
            readbuf = buf;

            // If we didn't get a full-ish buffer, then wait a bit before the next read so we don't
            // spin reading only a few bytes each time.

            // (unfortunately, boost::asio's edge-triggered epoll still gets woken repeatedly each time a
            // little more data arrives, but at least we don't have to do a bunch of work on every one of
            // those)
            if (len < read_buffer_size * 3 / 4) {
                read_timer.async_wait(std::bind(&SerialInput::start_reading, self, std::placeholders::_1));
            } else {
                start_reading();
            }
        }
    });
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x262b0
testb $0x1, %al
jne 0x3648d
jmp 0x364e6
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x20(%rbp), %rdi
movl $0x7d, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x3ace0
movq -0xc0(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x3aee0
testb $0x1, %al
jne 0x364c0
jmp 0x364c2
jmp 0x364e1
leaq 0x7df08(%rip), %rdi     # 0xb43d1
leaq 0x7df2d(%rip), %rsi     # 0xb43fd
movl $0xae, %edx
leaq 0x7df37(%rip), %rcx     # 0xb4413
callq 0x125d0
jmp 0x36682
movq -0xb8(%rbp), %rsi
addq $0x8, %rsi
leaq -0x40(%rbp), %rdi
callq 0x212b0
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3b0d0
leaq -0x40(%rbp), %rdi
callq 0x21430
leaq -0x50(%rbp), %rdi
callq 0x3b120
movq -0xb8(%rbp), %rsi
addq $0x430, %rsi            # imm = 0x430
leaq -0x50(%rbp), %rdi
callq 0x3b140
leaq -0x50(%rbp), %rdi
callq 0x3b180
testb $0x1, %al
jne 0x3653f
jmp 0x36573
leaq -0x50(%rbp), %rdi
callq 0x3b1a0
movq %rax, %rdi
movq -0xb8(%rbp), %rax
movq 0x2a0(%rax), %rsi
callq 0x3b1c0
jmp 0x36560
jmp 0x365a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x366a3
movq -0xb8(%rbp), %rsi
addq $0x2a0, %rsi            # imm = 0x2A0
leaq -0x70(%rbp), %rdi
callq 0x3a4a0
jmp 0x3658c
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x3b250
leaq -0x70(%rbp), %rdi
callq 0x214f0
movq -0xb8(%rbp), %rsi
movq %rsi, %rax
addq $0x3c0, %rax            # imm = 0x3C0
movq %rax, -0xc8(%rbp)
addq $0x2a8, %rsi            # imm = 0x2A8
leaq -0x78(%rbp), %rdi
callq 0x213c0
jmp 0x365cb
movq -0xc8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x21350
jmp 0x365dd
movq -0xb8(%rbp), %rax
addq $0x2d0, %rax            # imm = 0x2D0
movq %rax, -0xd0(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x3aeb0
movq %rax, %rdi
callq 0x3ae40
movq %rax, %rcx
movq -0xb8(%rbp), %rax
movq %rcx, -0x88(%rbp)
movq %rdx, -0x80(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3b280
movq -0xd8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x214c0
movq -0xd0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x36c40
jmp 0x36664
leaq -0xb0(%rbp), %rdi
callq 0x36c80
leaq -0x50(%rbp), %rdi
callq 0x214f0
leaq -0x30(%rbp), %rdi
callq 0x3b2b0
addq $0xe0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x36c80
leaq -0x50(%rbp), %rdi
callq 0x214f0
leaq -0x30(%rbp), %rdi
callq 0x3b2b0
movq -0x58(%rbp), %rdi
callq 0x12f70
nop