// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/dinitctl.cc
// Function: cat_service_log(int, cpbuffer<1024u>&, char const*, bool)
static int cat_service_log(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool do_clear)
{
    using std::cout;
    using std::cerr;

    handle_t handle;
    if (!load_service(socknum, rbuffer, service_name, &handle, nullptr, true)) {
        return 1;
    }

    char flags = do_clear ? 1 : 0;

    // Issue CATLOG
    auto m = membuf()
             .append((char)cp_cmd::CATLOG)
             .append<char>(flags)
             .append(handle);
    write_all_x(socknum, m);

    wait_for_reply(rbuffer, socknum);
    if (rbuffer[0] == (char)cp_rply::NAK) {
        cerr << "dinitctl: cannot cat log for service not configured to buffer output.\n";
        return 1;
    }
    if (rbuffer[0] != (char)cp_rply::SERVICE_LOG) {
        cerr << "dinitctl: protocol error.\n";
        return 1;
    }

    fill_buffer_to(rbuffer, socknum, 2 + sizeof(unsigned));
    unsigned bufsize;
    rbuffer.extract(&bufsize, 2, sizeof(unsigned));
    rbuffer.consume(2 + sizeof(unsigned));

    // output the log
    if (bufsize > 0) {
        cout << std::flush;

        bool trailing_nl = false;
        char output_buf[cpbuffer_t::get_size()];
        while (bufsize > 0) {
            unsigned l = rbuffer.get_length();
            if (l == 0) {
                fill_buffer_to(rbuffer, socknum, 1);
            }
            l = std::min(rbuffer.get_length(), bufsize);
            rbuffer.extract(output_buf, 0, l);
            write(STDOUT_FILENO, output_buf, l);
            rbuffer.consume(l);
            bufsize -= l;
            trailing_nl = (output_buf[l - 1] == '\n');
        }

        if (!trailing_nl) {
            cout << "\n(last line is truncated or incomplete)\n";
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %ecx, %r14d
movq %rsi, %rbx
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0xc(%rsp), %rcx
movl $0x1, %r15d
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xd2a8
testb %al, %al
je 0xc0bd
movl 0xc(%rsp), %eax
movq %rax, %rcx
shlq $0x10, %rcx
testb %r14b, %r14b
movl $0x114, %edx            # imm = 0x114
movl $0x14, %esi
cmovneq %rdx, %rsi
orq %rcx, %rsi
shrl $0x10, %eax
leaq 0x18(%rsp), %r14
movw %ax, 0x4(%r14)
movl %esi, (%r14)
movl $0x6, %r15d
movl %r15d, %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0x4100
cmpl $-0x1, %eax
je 0xbdff
addl %eax, %r12d
movslq %eax, %rcx
addq %rcx, %r14
subl %eax, %r15d
jmp 0xbe0d
callq 0x4040
cmpl $0x4, (%rax)
jne 0xbea5
testl %r15d, %r15d
jg 0xbddf
cmpl $-0x1, %r12d
je 0xbeae
movq %rbx, %rdi
movl %ebp, %esi
callq 0xed47
movl 0x400(%rbx), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movzbl (%rbx,%rcx), %eax
cmpl $0x49, %eax
je 0xbe7e
movq 0x1119e(%rip), %rdi     # 0x1cfe8
cmpl $0x33, %eax
jne 0xbe89
leaq 0xb4bb(%rip), %rsi      # 0x17311
movl $0x46, %edx
jmp 0xbe95
movq %rbx, %rdi
movl %ebp, %esi
callq 0xef52
testl %eax, %eax
jg 0xbe7e
cmpl $-0x1, %eax
jne 0xbed3
callq 0x4040
cmpl $0x4, (%rax)
jne 0xc107
cmpl $0x5, 0x404(%rbx)
jbe 0xbe5d
jmp 0xbedb
leaq 0xa2cd(%rip), %rsi      # 0x1615d
movl $0x1a, %edx
callq 0x4330
movl $0x1, %r15d
jmp 0xc0bd
testl %r12d, %r12d
jg 0xbe1c
movl $0x4, %edi
callq 0x4190
movq %rax, %rbx
callq 0x4040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x10c85(%rip), %rsi     # 0x1cb50
movq %rbx, %rdi
jmp 0xc100
testl %eax, %eax
je 0xc0d2
movl 0x400(%rbx), %r12d
leal 0x2(%r12), %eax
leal -0x3fe(%r12), %r14d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r14d
leal -0x3fd(%r14), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xbf41
movl $0x400, %r13d           # imm = 0x400
subl %r14d, %r13d
movl %r14d, %esi
addq %rbx, %rsi
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r13, %rdx
callq 0x4270
leaq (%rsp,%r13), %rdi
addq $0x8, %rdi
addl $0xfffffc04, %r14d      # imm = 0xFFFFFC04
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4270
jmp 0xbf47
movl %r14d, %r15d
addq %rbx, %r15
movl (%r15), %r13d
leal 0x6(%r12), %eax
addl $0xfffffc06, %r12d      # imm = 0xFFFFFC06
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r12d
movl %r12d, 0x400(%rbx)
addl $-0x6, 0x404(%rbx)
testl %r13d, %r13d
je 0xc0ba
movq 0x1102b(%rip), %rdi     # 0x1cfa8
callq 0x4220
movl 0x404(%rbx), %eax
leaq 0x20(%rsp), %r12
movl %eax, %r14d
testl %eax, %eax
je 0xc009
cmpl %r14d, %r13d
cmovbl %r13d, %r14d
movl 0x400(%rbx), %ecx
leal -0x400(%rcx), %eax
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
leal (%rax,%r14), %ecx
movl %eax, %esi
movl %r14d, %r15d
cmpl $0x401, %ecx            # imm = 0x401
jb 0xc040
movq %r15, 0x10(%rsp)
movq %r12, %r15
movl $0x400, %r12d           # imm = 0x400
subl %eax, %r12d
addq %rbx, %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x4270
leaq (%rsp,%r12), %rdi
addq $0x20, %rdi
movl %r14d, %edx
subl %r12d, %edx
movq %r15, %r12
movq 0x10(%rsp), %r15
movq %rbx, %rsi
jmp 0xc049
callq 0x4040
cmpl $0x4, (%rax)
jne 0xc0e4
movl 0x404(%rbx), %r14d
testl %r14d, %r14d
jne 0xbf94
movq %rbx, %rdi
movl %ebp, %esi
callq 0xef52
testl %eax, %eax
jg 0xc009
cmpl $-0x1, %eax
je 0xbffb
testl %eax, %eax
je 0xc0d2
movl 0x404(%rbx), %r14d
jmp 0xbf94
addq %rbx, %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x4270
movl $0x1, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4100
movl 0x400(%rbx), %ecx
movl 0x404(%rbx), %eax
leal (%rcx,%r14), %edx
cmpl $0x400, %edx            # imm = 0x400
leal -0x400(%rcx,%r14), %ecx
cmovbl %edx, %ecx
movl %ecx, 0x400(%rbx)
subl %r14d, %eax
movl %eax, 0x404(%rbx)
subl %r14d, %r13d
jne 0xbf8d
decl %r14d
cmpb $0xa, 0x20(%rsp,%r14)
je 0xc0ba
movq 0x10eff(%rip), %rdi     # 0x1cfa8
leaq 0xb2a8(%rip), %rsi      # 0x17358
movl $0x28, %edx
callq 0x4330
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x4190
movl $0x0, (%rax)
jmp 0xc0f6
movq %rax, %r15
movl $0x4, %edi
callq 0x4190
movl (%r15), %ecx
movl %ecx, (%rax)
leaq 0x10a43(%rip), %rsi     # 0x1cb40
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0
movq %rax, %r14
movl $0x4, %edi
callq 0x4190
movl (%r14), %ecx
jmp 0xc0f4