// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::queue_packet(std::vector<char, std::allocator<char>>&&)
bool control_conn_t::queue_packet(std::vector<char> &&pkt) noexcept
{
    bool was_empty = outbuf.empty();
    
    if (was_empty) {
        outpkt_index = 0;
        // We can try sending the packet immediately:
        int wr = bp_sys::write(iob.get_watched_fd(), pkt.data(), pkt.size());
        if (wr == -1) {
            if (errno == EPIPE) {
                return false;
            }
            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
                log(loglevel_t::WARN, "Error writing to control connection: ", strerror(errno));
                return false;
            }
            // EAGAIN etc: fall through to below
        }
        else {
            if ((unsigned)wr == pkt.size()) {
                // Ok, all written.
                return true;
            }
            outpkt_index = wr;
        }
    }
    
    try {
        outbuf.emplace_back(std::move(pkt));
        outbuf_size += pkt.size();
        return true;
    }
    catch (std::bad_alloc &baexc) {
        // Mark the connection bad, and stop reading further requests
        bad_conn_close = true;
        oom_close = true;
        if (was_empty) {
            // We can't send out-of-memory response as we already wrote as much as we
            // could above. Neither can we later send the response since we have currently
            // sent an incomplete packet. All we can do is close the connection.
            return false;
        }
        else {
            return true;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x50(%rbp)
addq $0x4f0, %rdi            # imm = 0x4F0
callq 0x11bd0
andb $0x1, %al
movb %al, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
je 0x5bd8c
movq -0x50(%rbp), %rdi
movl $0x0, 0x50c(%rdi)
addq $0x10, %rdi
callq 0x5fb70
movl %eax, -0x54(%rbp)
jmp 0x5bcbf
movq -0x18(%rbp), %rdi
callq 0x31180
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
callq 0x31650
movl -0x54(%rbp), %edi
movq -0x68(%rbp), %rsi
movq %rax, %rdx
callq 0xa200
movq %rax, -0x60(%rbp)
jmp 0x5bcea
movq -0x60(%rbp), %rax
movl %eax, -0x20(%rbp)
cmpl $-0x1, -0x20(%rbp)
jne 0x5bd58
callq 0xa060
cmpl $0x20, (%rax)
jne 0x5bd0a
movb $0x0, -0x1(%rbp)
jmp 0x5be22
callq 0xa060
cmpl $0xb, (%rax)
je 0x5bd56
callq 0xa060
cmpl $0xb, (%rax)
je 0x5bd56
callq 0xa060
cmpl $0x4, (%rax)
je 0x5bd56
callq 0xa060
movl (%rax), %edi
callq 0xa1f0
movq %rax, -0x28(%rbp)
movl $0x2, %edi
leaq 0x1d79d(%rip), %rsi     # 0x794e1
leaq -0x28(%rbp), %rdx
callq 0x5cc20
movb $0x0, -0x1(%rbp)
jmp 0x5be22
jmp 0x5bd8a
movl -0x20(%rbp), %eax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
callq 0x31650
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
jne 0x5bd7d
movb $0x1, -0x1(%rbp)
jmp 0x5be22
movq -0x50(%rbp), %rax
movl -0x20(%rbp), %ecx
movl %ecx, 0x50c(%rax)
jmp 0x5bd8c
movq -0x50(%rbp), %rdi
addq $0x4f0, %rdi            # imm = 0x4F0
movq -0x18(%rbp), %rsi
callq 0x5fbe0
jmp 0x5bda2
movq -0x18(%rbp), %rdi
callq 0x31650
movq %rax, %rdx
movq -0x50(%rbp), %rax
movl 0x508(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x508(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5be22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0x5be35
movq -0x30(%rbp), %rdi
callq 0xa240
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, -0x40(%rbp)
movb $0x1, 0x78(%rax)
movb $0x1, 0x79(%rax)
testb $0x1, -0x19(%rbp)
je 0x5be0e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x5be19
movb $0x1, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
callq 0xac30
jmp 0x5be20
jmp 0x5be22
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
movq %rax, %rdi
callq 0xfa30
movq -0x30(%rbp), %rdi
callq 0xfa30
nop