// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/dinitctl.cc
// Function: list_services(int, cpbuffer<1024u>&, unsigned short)
static int list_services(int socknum, cpbuffer_t &rbuffer, uint16_t proto_version)
{
    using std::cout;
    using std::cerr;
    
    char cmdbuf[] = { (char)cp_cmd::LISTSERVICES };
    if (proto_version >= 5) {
        cmdbuf[0] = (char)cp_cmd::LISTSERVICES5;
    }
    write_all_x(socknum, cmdbuf, 1);

    unsigned status_buffer_size = proto_version < 5 ? STATUS_BUFFER_SIZE : STATUS_BUFFER5_SIZE;

    wait_for_reply(rbuffer, socknum);
    while (rbuffer[0] == (char)cp_rply::SVCINFO) {
        // Packet: SVCINFO (1), name length (1), status buffer (STATUS_BUFFER_SIZE), name (N)
        int hdrsize = 2 + status_buffer_size;
        fill_buffer_to(rbuffer, socknum, hdrsize);
        unsigned name_len = (unsigned char)rbuffer[1];
        service_state_t current = static_cast<service_state_t>(rbuffer[2]);
        service_state_t target = static_cast<service_state_t>(rbuffer[3]);

        int console_flags = rbuffer[4];
        bool has_console = (console_flags & 2) != 0;
        bool waiting_console = (console_flags & 1) != 0;
        bool was_skipped = (console_flags & 4) != 0;
        bool marked_active = (console_flags & 8) != 0;
        bool has_pid = (console_flags & 16) != 0;

        stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[5]);

        pid_t service_pid;
        int exit_status;
        int exit_si_code;
        int exit_si_status;
        if (has_pid) {
            // 8 = SVCINFO (1) + name length (1)
            //                 + current state (1) + target state (1) + flags (1)
            //                 + stop reason (1) + exec failure stage (2)
            rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid));
        }
        else {
            if (proto_version < 5) {
                rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
            }
            else {
                rbuffer.extract((char *)&exit_si_code, 8, sizeof(exit_si_code));
                rbuffer.extract((char *)&exit_si_status, 8 + sizeof(exit_si_code), sizeof(exit_si_status));
            }
        }

        fill_buffer_to(rbuffer, socknum, name_len + hdrsize);

        char *name_ptr = rbuffer.get_ptr(hdrsize);
        unsigned clength = std::min(rbuffer.get_contiguous_length(name_ptr), name_len);

        std::string name {name_ptr, clength};
        name.append(rbuffer.get_buf_base(), name_len - clength);

        cout << "[";

        // [ ] if marked active; otherwise, { } if target state is STARTED
        //  +  if started, 's' if skipped, space otherwise
        char lbracket = target == service_state_t::STARTED ? '{' : ' ';
        char rbracket = target == service_state_t::STARTED ? '}' : ' ';
        cout << (marked_active ? '[' : lbracket);
        if (current == service_state_t::STARTED) {
            cout << (was_skipped ? 's' : '+');
        }
        else {
            cout << ' ';
        }
        cout << (marked_active ? ']' : rbracket);
        
        if (current == service_state_t::STARTING) {
            cout << "<<";
        }
        else if (current == service_state_t::STOPPING) {
            cout << ">>";
        }
        else {
            cout << "  ";
        }
        
        cout << (target == service_state_t::STOPPED ? '{' : ' ');
        if (current == service_state_t::STOPPED) {
            bool did_fail = false;
            if (stop_reason == stopped_reason_t::TERMINATED) {
                if (proto_version < 5) {
                    if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) {
                        did_fail = true;
                    }
                }
                else {
                    did_fail = (exit_si_code != CLD_EXITED) || (exit_si_status != 0);
                }
            }
            else {
                did_fail = (stop_reason != stopped_reason_t::NORMAL);
            }

            cout << (did_fail ? 'X' : '-');
        }
        else {
            cout << ' ';
        }
        cout << (target == service_state_t::STOPPED ? '}' : ' ');

        cout << "] " << name;

        if (current != service_state_t::STOPPED && has_pid) {
            cout << " (pid: " << service_pid << ")";
        }
        
        if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) {
            if (proto_version < 5) {
                if (WIFEXITED(exit_status)) {
                    cout << " (exit status: " << WEXITSTATUS(exit_status) << ")";
                }
                else if (WIFSIGNALED(exit_status)) {
                    cout << " (signal: " << WTERMSIG(exit_status) << ")";
                }
            }
            else {
                if (exit_si_code == CLD_EXITED) {
                    cout << " (exit status: " << exit_si_status << ")";
                }
                else if (exit_si_code == CLD_KILLED) {
                    cout << " (signal: " << exit_si_status << ")";
                }
            }
        }

        if (has_console) {
            cout << " (has console)";
        }
        else if (waiting_console) {
            cout << " (waiting for console)";
        }

        cout << "\n";

        rbuffer.consume(hdrsize + name_len);
        wait_for_reply(rbuffer, socknum);
    }

    if (rbuffer[0] != (char)cp_rply::LISTDONE) {
        cerr << "dinitctl: control socket protocol error\n";
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movl %edi, %ebp
movl %edx, 0x14(%rsp)
cmpw $0x5, %dx
movl $0x19, %eax
movl $0x8, %ecx
cmovael %eax, %ecx
leaq 0x27(%rsp), %rbx
movb %cl, (%rbx)
movl $0x1, %r12d
xorl %r15d, %r15d
movl %r12d, %edx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x4100
cmpl $-0x1, %eax
je 0xa2cc
addl %eax, %r15d
movslq %eax, %rcx
addq %rcx, %rbx
subl %eax, %r12d
jmp 0xa2da
callq 0x4040
cmpl $0x4, (%rax)
jne 0xa9e1
testl %r12d, %r12d
jg 0xa2ac
cmpl $-0x1, %r15d
je 0xa9ea
xorl %ebx, %ebx
cmpw $0x5, 0x14(%rsp)
setae %bl
movq %r14, %rdi
movl %ebp, %esi
callq 0xed47
leaq 0x400(%r14), %rax
movq %rax, 0x50(%rsp)
leal 0xc(,%rbx,4), %r12d
movq %r12, 0x28(%rsp)
movl 0x400(%r14), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movzbl (%r14,%rcx), %eax
cmpl $0x3e, %eax
jne 0xa9ac
cmpl %r12d, 0x404(%r14)
jae 0xa36d
movq %r14, %rdi
movl %ebp, %esi
callq 0xef52
testl %eax, %eax
jg 0xa33a
cmpl $-0x1, %eax
jne 0xa365
callq 0x4040
cmpl $0x4, (%rax)
je 0xa33a
jmp 0xaa1e
testl %eax, %eax
je 0xaa0c
movl 0x400(%r14), %r15d
leal 0x1(%r15), %eax
leal -0x3ff(%r15), %ecx
movl $0x400, %esi            # imm = 0x400
cmpl %esi, %eax
cmovbl %eax, %ecx
movzbl (%r14,%rcx), %r12d
leal 0x2(%r15), %eax
leal -0x3fe(%r15), %ecx
cmpl %esi, %eax
cmovbl %eax, %ecx
movb (%r14,%rcx), %al
movb %al, 0x9(%rsp)
leal 0x3(%r15), %eax
movl $0xfffffc03, %edi       # imm = 0xFFFFFC03
leal (%r15,%rdi), %ecx
cmpl %esi, %eax
cmovbl %eax, %ecx
movb (%r14,%rcx), %al
movb %al, 0xa(%rsp)
leal 0x4(%r15), %eax
leal -0x3fc(%r15), %ecx
cmpl %esi, %eax
cmovbl %eax, %ecx
leal 0x5(%r15), %eax
leal -0x3fb(%r15), %edx
cmpl %esi, %eax
cmovbl %eax, %edx
leal 0x8(%r15), %eax
leal -0x3f8(%r15), %r13d
cmpl %esi, %eax
cmovbl %eax, %r13d
movzbl (%r14,%rcx), %ecx
leal (%rdi,%r13), %eax
movb (%r14,%rdx), %bl
movl %ecx, 0xc(%rsp)
testb $0x10, %cl
movb %bl, 0xb(%rsp)
jne 0xa444
cmpw $0x4, 0x14(%rsp)
ja 0xa481
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa4cc
movl $0x400, %r15d           # imm = 0x400
subl %r13d, %r15d
movl %r13d, %esi
addq %r14, %rsi
leaq 0x1c(%rsp), %rdi
movq %r15, %rdx
callq 0x4270
leaq (%rsp,%r15), %rdi
addq $0x1c, %rdi
jmp 0xa46f
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa4bf
movl $0x400, %r15d           # imm = 0x400
subl %r13d, %r15d
movl %r13d, %esi
addq %r14, %rsi
leaq 0x20(%rsp), %rdi
movq %r15, %rdx
callq 0x4270
leaq (%rsp,%r15), %rdi
addq $0x20, %rdi
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r13, %rdx
jmp 0xa53b
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa4d9
movl $0x400, %edx            # imm = 0x400
subl %r13d, %edx
movl %r13d, %esi
addq %r14, %rsi
leaq 0x18(%rsp), %rdi
movq %rdx, %rbx
callq 0x4270
leaq (%rsp,%rbx), %rdi
addq $0x18, %rdi
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r13, %rdx
callq 0x4270
jmp 0xa4e4
movl %r13d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x20(%rsp)
jmp 0xa540
movl %r13d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x1c(%rsp)
jmp 0xa540
movl %r13d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x18(%rsp)
leal 0xc(%r15), %eax
addl $0xfffffc0c, %r15d      # imm = 0xFFFFFC0C
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa773
movl $0x400, %r13d           # imm = 0x400
subl %r15d, %r13d
movl %r15d, %esi
addq %r14, %rsi
leaq 0x10(%rsp), %rdi
movq %r13, %rdx
callq 0x4270
leaq (%rsp,%r13), %rdi
addq $0x10, %rdi
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r15, %rdx
callq 0x4270
movq 0x28(%rsp), %rax
leal (%rax,%r12), %r13d
movl 0x404(%r14), %eax
cmpl %r13d, %eax
jae 0xa586
movq %r14, %rdi
movl %ebp, %esi
callq 0xef52
testl %eax, %eax
jg 0xa549
cmpl $-0x1, %eax
jne 0xa577
callq 0x4040
cmpl $0x4, (%rax)
je 0xa549
jmp 0xaa2f
testl %eax, %eax
je 0xaa0c
movl 0x404(%r14), %eax
movl 0x400(%r14), %edx
movq 0x28(%rsp), %rcx
leal (%rdx,%rcx), %esi
cmpl $0x400, %esi            # imm = 0x400
leal -0x400(%rdx,%rcx), %ecx
cmovbl %esi, %ecx
leaq (%rcx,%r14), %rsi
testl %eax, %eax
je 0xa5d1
leal (%rax,%rdx), %edi
cmpl $0x400, %edi            # imm = 0x400
leal -0x400(%rax,%rdx), %eax
cmovbl %edi, %eax
leal (%r14,%rax), %r15d
cmpl %ecx, %eax
cmovbel 0x50(%rsp), %r15d
subl %esi, %r15d
jmp 0xa5d4
xorl %r15d, %r15d
cmpl %r12d, %r15d
cmovael %r12d, %r15d
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq (%rsi,%r15), %rdx
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0xec34
subl %r15d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4070
movq 0x1299a(%rip), %r15     # 0x1cfa8
movl $0x1, %edx
movq %r15, %rdi
leaq 0xc0b5(%rip), %rsi      # 0x166d2
callq 0x4330
cmpb $0x2, 0xa(%rsp)
movl $0x20, %eax
movl $0x7b, %ecx
cmovel %ecx, %eax
movl $0x20, %ebx
movl $0x7d, %ecx
cmovel %ecx, %ebx
testb $0x8, 0xc(%rsp)
movl $0x5b, %ecx
cmovnel %ecx, %eax
movb %al, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
movq 0x28(%rsp), %r12
cmpb $0x2, 0x9(%rsp)
jne 0xa69a
testb $0x4, 0xc(%rsp)
movl $0x73, %eax
movl $0x2b, %ecx
cmovel %ecx, %eax
movb %al, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
jmp 0xa6b1
movb $0x20, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
testb $0x8, 0xc(%rsp)
movzbl %bl, %eax
movl $0x5d, %ecx
cmovnel %ecx, %eax
movb %al, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
movb 0x9(%rsp), %cl
cmpb $0x3, %cl
leaq 0xbff5(%rip), %rsi      # 0x166da
leaq 0xbfeb(%rip), %rax      # 0x166d7
cmoveq %rax, %rsi
cmpb $0x1, %cl
leaq 0xbfda(%rip), %rax      # 0x166d4
cmoveq %rax, %rsi
movl $0x2, %edx
movq %r15, %rdi
callq 0x4330
cmpb $0x0, 0xa(%rsp)
movl $0x20, %eax
movl $0x7b, %ecx
cmovel %ecx, %eax
movb %al, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
cmpb $0x0, 0x9(%rsp)
je 0xa753
movb $0x20, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
jmp 0xa7b6
movb 0xb(%rsp), %al
cmpb $0x6, %al
jne 0xa76f
cmpw $0x4, 0x14(%rsp)
ja 0xa783
movzwl 0x1c(%rsp), %eax
testl $0xff7f, %eax           # imm = 0xFF7F
jmp 0xa78e
testb %al, %al
jmp 0xa78e
movl %r15d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0xa540
movl 0x18(%rsp), %eax
xorl $0x1, %eax
orl 0x10(%rsp), %eax
setne %al
testb %al, %al
movl $0x2d, %eax
movl $0x58, %ecx
cmovnel %ecx, %eax
movb %al, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
cmpb $0x0, 0xa(%rsp)
movl $0x20, %eax
movl $0x7d, %ecx
cmovel %ecx, %eax
movb %al, 0x8(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4330
movl $0x2, %edx
movq %r15, %rdi
leaq 0xbef0(%rip), %rsi      # 0x166dd
callq 0x4330
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rdi
callq 0x4330
cmpb $0x0, 0x9(%rsp)
je 0xa8f2
movl 0xc(%rsp), %eax
andl $0x10, %eax
je 0xa8f2
movl $0x7, %edx
movq %r15, %rdi
leaq 0xbeb5(%rip), %rsi      # 0x166e0
callq 0x4330
movl 0x20(%rsp), %ebx
movq %r15, %rdi
movl %ebx, %esi
callq 0x44e0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc621(%rip), %rsi      # 0x16e6e
callq 0x4330
testb $0x3, 0xc(%rsp)
je 0xa885
xorl %eax, %eax
testb $0x2, 0xc(%rsp)
sete %al
leaq 0xe(,%rax,8), %rdx
leaq 0xbe91(%rip), %rsi      # 0x16703
leaq 0xbe99(%rip), %rax      # 0x16712
cmoveq %rax, %rsi
movq %r15, %rdi
callq 0x4330
movl $0x1, %edx
movq %r15, %rdi
leaq 0xc26c(%rip), %rsi      # 0x16b00
callq 0x4330
movl 0x400(%r14), %eax
leal (%rax,%r13), %ecx
cmpl $0x400, %ecx            # imm = 0x400
leal -0x400(%rax,%r13), %eax
cmovbl %ecx, %eax
movl %eax, 0x400(%r14)
subl %r13d, 0x404(%r14)
movq %r14, %rdi
movl %ebp, %esi
callq 0xed47
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xa317
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42f0
jmp 0xa317
cmpb $0x0, 0x9(%rsp)
jne 0xa852
cmpb $0x6, 0xb(%rsp)
jne 0xa852
cmpw $0x4, 0x14(%rsp)
ja 0xa949
movl 0x1c(%rsp), %eax
movl %eax, %ebx
andl $0x7f, %ebx
je 0xa971
movl %ebx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0xa852
movl $0xa, %edx
movq %r15, %rdi
leaq 0xbdb9(%rip), %rsi      # 0x166f8
callq 0x4330
jmp 0xa834
movl 0x18(%rsp), %eax
cmpl $0x2, %eax
je 0xa98f
cmpl $0x1, %eax
jne 0xa852
movl $0xf, %edx
movq %r15, %rdi
leaq 0xbd7e(%rip), %rsi      # 0x166e8
callq 0x4330
jmp 0xa9a3
movl %eax, %ebx
movl $0xf, %edx
movq %r15, %rdi
leaq 0xbd66(%rip), %rsi      # 0x166e8
callq 0x4330
movzbl %bh, %ebx
jmp 0xa834
movl $0xa, %edx
movq %r15, %rdi
leaq 0xbd5a(%rip), %rsi      # 0x166f8
callq 0x4330
movl 0x10(%rsp), %ebx
jmp 0xa834
cmpl $0x3f, %eax
jne 0xa9b5
xorl %eax, %eax
jmp 0xa9d2
movq 0x1262c(%rip), %rdi     # 0x1cfe8
leaq 0xbd66(%rip), %rsi      # 0x16729
movl $0x28, %edx
callq 0x4330
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
jg 0xa2e9
movl $0x4, %edi
callq 0x4190
movq %rax, %rbx
callq 0x4040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x12149(%rip), %rsi     # 0x1cb50
movq %rbx, %rdi
jmp 0xaa4b
movl $0x4, %edi
callq 0x4190
movl $0x0, (%rax)
jmp 0xaa41
movq %rax, %rbx
movl $0x4, %edi
callq 0x4190
movl (%rbx), %ecx
jmp 0xaa3f
movq %rax, %r15
movl $0x4, %edi
callq 0x4190
movl (%r15), %ecx
movl %ecx, (%rax)
leaq 0x120f8(%rip), %rsi     # 0x1cb40
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0
jmp 0xaa56
jmp 0xaa56
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xaa75
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42f0
movq %rbx, %rdi
callq 0x44f0