// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::send_data()
bool control_conn_t::send_data() noexcept
{
    if (outbuf.empty() && bad_conn_close) {
        if (oom_close) {
            // Send oom response
            char oomBuf[] = { (char)cp_rply::OOM };
            bp_sys::write(iob.get_watched_fd(), oomBuf, 1);
        }
        return true;
    }
    
    vector<char> & pkt = outbuf.front();
    char *data = pkt.data();
    int written = bp_sys::write(iob.get_watched_fd(), data + outpkt_index, pkt.size() - outpkt_index);
    if (written == -1) {
        if (errno == EPIPE) {
            // read end closed
            return true;
        }
        else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
            // spurious readiness notification?
            return false;
        }
        else {
            log(loglevel_t::ERROR, "Error writing to control connection: ", strerror(errno));
            return true;
        }
    }

    outpkt_index += written;
    if (outpkt_index == pkt.size()) {
        // We've finished this packet, move on to the next:
        outbuf_size -= pkt.size();
        outbuf.pop_front();
        outpkt_index = 0;
        if (oom_close) {
            // remain active, try to send cp_rply::OOM shortly
            return false;
        }
        if (outbuf.empty() && bad_conn_close) {
            return true;
        }
    }
    
    // more to send
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x40(%rbp)
addq $0x4f0, %rdi            # imm = 0x4F0
callq 0x11bd0
testb $0x1, %al
jne 0x5ce76
jmp 0x5cebe
movq -0x40(%rbp), %rax
testb $0x1, 0x78(%rax)
je 0x5cebe
movq -0x40(%rbp), %rax
testb $0x1, 0x79(%rax)
je 0x5ceb5
movq -0x40(%rbp), %rdi
movb $0x35, -0x11(%rbp)
addq $0x10, %rdi
callq 0x5fb70
movl %eax, -0x44(%rbp)
jmp 0x5cea0
movl -0x44(%rbp), %edi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
callq 0xa200
jmp 0x5ceb3
jmp 0x5ceb5
movb $0x1, -0x1(%rbp)
jmp 0x5d049
movq -0x40(%rbp), %rdi
addq $0x4f0, %rdi            # imm = 0x4F0
callq 0x5fd10
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31180
movq -0x40(%rbp), %rdi
movq %rax, -0x28(%rbp)
addq $0x10, %rdi
callq 0x5fb70
movl %eax, -0x48(%rbp)
jmp 0x5cef1
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movl 0x50c(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31650
movl -0x48(%rbp), %edi
movq -0x58(%rbp), %rsi
movq %rax, %rdx
movq -0x40(%rbp), %rax
movl 0x50c(%rax), %eax
subq %rax, %rdx
callq 0xa200
movq %rax, -0x50(%rbp)
jmp 0x5cf31
movq -0x50(%rbp), %rax
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x5cfa6
callq 0xa060
cmpl $0x20, (%rax)
jne 0x5cf51
movb $0x1, -0x1(%rbp)
jmp 0x5d049
callq 0xa060
cmpl $0xb, (%rax)
je 0x5cf6f
callq 0xa060
cmpl $0xb, (%rax)
je 0x5cf6f
callq 0xa060
cmpl $0x4, (%rax)
jne 0x5cf78
movb $0x0, -0x1(%rbp)
jmp 0x5d049
callq 0xa060
movl (%rax), %edi
callq 0xa1f0
movq %rax, -0x38(%rbp)
movl $0x3, %edi
leaq 0x1c54d(%rip), %rsi     # 0x794e1
leaq -0x38(%rbp), %rdx
callq 0x5cc20
movb $0x1, -0x1(%rbp)
jmp 0x5d049
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl 0x50c(%rax), %ecx
movl %ecx, 0x50c(%rax)
movl 0x50c(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31650
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jne 0x5d045
movq -0x20(%rbp), %rdi
callq 0x31650
movq -0x40(%rbp), %rdi
movq %rax, %rcx
movl 0x508(%rdi), %eax
subq %rcx, %rax
movl %eax, 0x508(%rdi)
addq $0x4f0, %rdi            # imm = 0x4F0
callq 0x5fd40
movq -0x40(%rbp), %rax
movl $0x0, 0x50c(%rax)
testb $0x1, 0x79(%rax)
je 0x5d01d
movb $0x0, -0x1(%rbp)
jmp 0x5d049
movq -0x40(%rbp), %rdi
addq $0x4f0, %rdi            # imm = 0x4F0
callq 0x11bd0
testb $0x1, %al
jne 0x5d033
jmp 0x5d043
movq -0x40(%rbp), %rax
testb $0x1, 0x78(%rax)
je 0x5d043
movb $0x1, -0x1(%rbp)
jmp 0x5d049
jmp 0x5d045
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
movq %rax, %rdi
callq 0xfa30
nopl (%rax)