// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_catlog()
bool control_conn_t::process_catlog()
{
    // 1 byte packet type
    // 1 byte reserved for future use
    // handle
    constexpr int pkt_size = 2 + sizeof(handle_t);

    if (rbuf.get_length() < pkt_size) {
        chklen = pkt_size;
        return true;
    }

    handle_t handle;
    char flags = rbuf[1];

    rbuf.extract(&handle, 2, sizeof(handle));
    rbuf.consume(pkt_size);
    chklen = 0;

    service_record *service = find_service_for_key(handle);
    if (service == nullptr || (service->get_type() != service_type_t::PROCESS
            && service->get_type() != service_type_t::BGPROCESS
            && service->get_type() != service_type_t::SCRIPTED)) {
        char nak_rep[] = { (char)cp_rply::NAK };
        return queue_packet(nak_rep, 1);
    }

    base_process_service *bps = static_cast<base_process_service *>(service);
    if (bps->get_log_mode() != log_type_id::BUFFER) {
        char nak_rep[] = { (char)cp_rply::NAK };
        return queue_packet(nak_rep, 1);
    }

    auto buffer_details = bps->get_log_buffer();
    const char *bufaddr = buffer_details.first;
    unsigned buflen = buffer_details.second;

    std::vector<char> pkt = { (char)cp_rply::SERVICE_LOG, 0 /* flags; reserved for future */ };
    pkt.insert(pkt.end(), (char *)(&buflen), (char *)(&buflen + 1));
    pkt.insert(pkt.end(), bufaddr, bufaddr + buflen);
    if ((flags & 1) != 0) {
        bps->clear_log_buffer();
    }
    return queue_packet(std::move(pkt));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
movl $0x6, -0x14(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0x6, %eax
jae 0x5b589
movq -0xe8(%rbp), %rax
movl $0x6, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5b861
movq -0xe8(%rbp), %rdi
addq $0x80, %rdi
movl $0x1, %esi
callq 0x5e160
movq -0xe8(%rbp), %rdi
movb %al, -0x19(%rbp)
addq $0x80, %rdi
leaq -0x18(%rbp), %rsi
movl $0x2, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0xe8(%rbp), %rdi
addq $0x80, %rdi
movl $0x6, %esi
callq 0x5e1a0
movq -0xe8(%rbp), %rdi
movl $0x0, 0x7c(%rdi)
movl -0x18(%rbp), %esi
callq 0x5e970
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5b628
movq -0x28(%rbp), %rdi
callq 0x2d5a0
cmpl $0x1, %eax
je 0x5b650
movq -0x28(%rbp), %rdi
callq 0x2d5a0
cmpl $0x2, %eax
je 0x5b650
movq -0x28(%rbp), %rdi
callq 0x2d5a0
cmpl $0x3, %eax
je 0x5b650
movq -0xe8(%rbp), %rdi
movb 0x1de27(%rip), %al      # 0x7945c
movb %al, -0x29(%rbp)
leaq -0x29(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5b861
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2f280
cmpl $0x2, %eax
je 0x5b68e
movq -0xe8(%rbp), %rdi
movb 0x1ddea(%rip), %al      # 0x7945d
movb %al, -0x39(%rbp)
leaq -0x39(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5b861
movq -0x38(%rbp), %rdi
callq 0x5f6c0
movl %edx, -0x58(%rbp)
movq %rax, -0x60(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x6c(%rbp)
movb $0x49, -0x9a(%rbp)
movb $0x0, -0x99(%rbp)
leaq -0x9a(%rbp), %rax
movq %rax, -0x98(%rbp)
movq $0x2, -0x90(%rbp)
leaq -0x9b(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0xace0
movq -0xf0(%rbp), %rcx
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x5e4a0
jmp 0x5b717
leaq -0x9b(%rbp), %rdi
callq 0xa790
leaq -0x88(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x5f580
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x5f5b0
movq -0x100(%rbp), %rdi
leaq -0x68(%rbp), %rcx
movq -0xb8(%rbp), %rsi
leaq -0x6c(%rbp), %rdx
callq 0x5f720
movq %rax, -0xf8(%rbp)
jmp 0x5b774
movq -0xf8(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x88(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x5f580
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x5f5b0
movq -0x110(%rbp), %rdi
movq -0x68(%rbp), %rdx
movl -0x6c(%rbp), %eax
movq %rdx, %rcx
addq %rax, %rcx
movq -0xd0(%rbp), %rsi
callq 0x5f4e0
movq %rax, -0x108(%rbp)
jmp 0x5b7d8
movq -0x108(%rbp), %rax
movq %rax, -0xe0(%rbp)
movsbl -0x19(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x5b83d
movq -0x38(%rbp), %rdi
callq 0x5f7c0
jmp 0x5b83d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x9b(%rbp), %rdi
callq 0xa790
jmp 0x5b86f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x13f20
jmp 0x5b86f
movq -0xe8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x5bc70
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x13f20
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0xa8(%rbp), %rdi
callq 0xacc0
nopl (%rax,%rax)