// File: /workspace/github_cpp_makefile_star2_O1/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 $0x418, %rsp            # imm = 0x418
movl %ecx, %r14d
movq %rsi, %rbx
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0x4(%rsp), %rcx
movl $0x1, %r15d
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xd198
testb %al, %al
je 0xbf8d
movl 0x4(%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 0x8(%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 0xbccf
addl %eax, %r12d
movslq %eax, %rcx
addq %rcx, %r14
subl %eax, %r15d
xorl %ecx, %ecx
jmp 0xbcf0
callq 0x4040
movl $0x2, %ecx
cmpl $0x4, (%rax)
je 0xbcf0
testl %r12d, %r12d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmovgl %r12d, %r13d
movl $0x1, %ecx
testb $0x1, %cl
jne 0xbcfc
testl %r15d, %r15d
jg 0xbcad
jmp 0xbcff
movl %r13d, %r12d
cmpl $-0x1, %r12d
je 0xbfe9
movq %rbx, %rdi
movl %ebp, %esi
callq 0xedbd
movl 0x400(%rbx), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movzbl (%rbx,%rcx), %eax
cmpl $0x49, %eax
je 0xbd51
movq 0x112b1(%rip), %rdi     # 0x1cfe8
cmpl $0x33, %eax
jne 0xbdf9
leaq 0xb5df(%rip), %rsi      # 0x17326
movl $0x46, %edx
jmp 0xbe05
movl $0x1, %r15d
cmpl $0x5, 0x404(%rbx)
ja 0xbd75
movq %rbx, %rdi
movl %ebp, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %r14d
movl %r14d, %r15d
jg 0xbd51
cmpl $-0x1, %r15d
je 0xbd85
testl %r15d, %r15d
jne 0xbd93
jmp 0xbfa2
callq 0x4040
cmpl $0x4, (%rax)
jne 0xbfd7
cmpl $-0x1, %r15d
je 0xbd51
movl 0x400(%rbx), %r14d
leal 0x2(%r14), %eax
addl $0xfffffc02, %r14d      # imm = 0xFFFFFC02
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r14d
leal -0x3fd(%r14), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xbe15
movl $0x400, %r15d           # imm = 0x400
subl %r14d, %r15d
movl %r14d, %esi
addq %rbx, %rsi
movq %rsp, %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0x4270
addq %r12, %r15
addl $0xfffffc04, %r14d      # imm = 0xFFFFFC04
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4270
jmp 0xbe1e
leaq 0xa372(%rip), %rsi      # 0x16172
movl $0x1a, %edx
callq 0x4330
movl $0x1, %r15d
jmp 0xbf8d
movl %r14d, %eax
movl (%rbx,%rax), %eax
movl %eax, (%rsp)
movl 0x400(%rbx), %eax
leal 0x6(%rax), %ecx
addl $0xfffffc06, %eax       # imm = 0xFFFFFC06
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl %eax, 0x400(%rbx)
addl $-0x6, 0x404(%rbx)
cmpl $0x0, (%rsp)
je 0xbf8a
movq 0x11155(%rip), %rdi     # 0x1cfa8
callq 0x4220
cmpl $0x0, (%rsp)
je 0xbf72
leaq 0x10(%rsp), %r14
cmpl $0x0, 0x404(%rbx)
jne 0xbeba
movl $0x1, %r12d
cmpl $0x0, 0x404(%rbx)
je 0xbe8f
cmpl $-0x1, %r12d
je 0xbea6
testl %r12d, %r12d
jne 0xbeb4
jmp 0xbfa2
movq %rbx, %rdi
movl %ebp, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %r15d
movl %r15d, %r12d
jg 0xbe70
jmp 0xbe7f
callq 0x4040
cmpl $0x4, (%rax)
jne 0xbfb4
cmpl $-0x1, %r12d
je 0xbe70
movl 0x400(%rbx), %ecx
movl 0x404(%rbx), %r13d
movl (%rsp), %eax
cmpl %r13d, %eax
cmovbl %eax, %r13d
leal -0x400(%rcx), %eax
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
leal (%rax,%r13), %ecx
movl %r13d, %r15d
movl %eax, %esi
cmpl $0x401, %ecx            # imm = 0x401
jb 0xbf17
movl $0x400, %r12d           # imm = 0x400
subl %eax, %r12d
addq %rbx, %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x4270
leaq (%r12,%r14), %rdi
movl %r13d, %edx
subl %r12d, %edx
movq %rbx, %rsi
jmp 0xbf20
addq %rbx, %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4270
movl $0x1, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4100
movl 0x400(%rbx), %eax
leal (%rax,%r13), %ecx
cmpl $0x400, %ecx            # imm = 0x400
leal -0x400(%rax,%r13), %eax
cmovbl %ecx, %eax
movl %eax, 0x400(%rbx)
subl %r13d, 0x404(%rbx)
subl %r13d, (%rsp)
jne 0xbe67
decl %r13d
cmpb $0xa, 0x10(%rsp,%r13)
je 0xbf8a
movq 0x1102f(%rip), %rdi     # 0x1cfa8
leaq 0xb3ed(%rip), %rsi      # 0x1736d
movl $0x28, %edx
callq 0x4330
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x4190
movl $0x0, (%rax)
jmp 0xbfc6
movq %rax, %r15
movl $0x4, %edi
callq 0x4190
movl (%r15), %ecx
movl %ecx, (%rax)
leaq 0x10b73(%rip), %rsi     # 0x1cb40
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0
movq %rax, %r14
movl $0x4, %edi
callq 0x4190
movl (%r14), %ecx
jmp 0xbfc4
movl $0x4, %edi
callq 0x4190
movq %rax, %rbx
callq 0x4040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x10b4a(%rip), %rsi     # 0x1cb50
movq %rbx, %rdi
jmp 0xbfd0