// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc(%rsp)
cmpw $0x5, %dx
movl $0x19, %eax
movl $0x8, %ecx
cmovael %eax, %ecx
leaq 0x1b(%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 0xa0c6
addl %eax, %r15d
movslq %eax, %rcx
addq %rcx, %rbx
subl %eax, %r12d
xorl %ecx, %ecx
jmp 0xa0e7
callq 0x4040
movl $0x2, %ecx
cmpl $0x4, (%rax)
je 0xa0e7
testl %r15d, %r15d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmovgl %r15d, %r13d
movl $0x1, %ecx
testb $0x1, %cl
jne 0xa0f3
testl %r12d, %r12d
jg 0xa0a4
jmp 0xa0f6
movl %r13d, %r15d
cmpl $-0x1, %r15d
je 0xa849
xorl %ebx, %ebx
cmpw $0x5, 0xc(%rsp)
setae %bl
movq %r14, %rdi
movl %ebp, %esi
callq 0xedbd
leaq 0x400(%r14), %rax
movq %rax, 0x30(%rsp)
leal 0xc(,%rbx,4), %r12d
movl %ebp, 0x2c(%rsp)
movq %r12, 0x20(%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 0xa7e0
movl $0x1, %r15d
cmpl %r12d, 0x404(%r14)
jae 0xa178
movq %r14, %rdi
movl %ebp, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %ebx
movl %ebx, %r15d
jg 0xa155
cmpl $-0x1, %r15d
je 0xa188
testl %r15d, %r15d
jne 0xa196
jmp 0xa815
callq 0x4040
cmpl $0x4, (%rax)
jne 0xa827
cmpl $-0x1, %r15d
je 0xa155
movl 0x400(%r14), %ebx
leal 0x1(%rbx), %eax
leal -0x3ff(%rbx), %ecx
movl $0x400, %esi            # imm = 0x400
cmpl %esi, %eax
cmovbl %eax, %ecx
movzbl (%r14,%rcx), %r12d
leal 0x2(%rbx), %eax
leal -0x3fe(%rbx), %ecx
cmpl %esi, %eax
cmovbl %eax, %ecx
movb (%r14,%rcx), %al
movb %al, 0x1(%rsp)
leal 0x3(%rbx), %eax
movl $0xfffffc03, %edi       # imm = 0xFFFFFC03
leal (%rbx,%rdi), %ecx
cmpl %esi, %eax
cmovbl %eax, %ecx
movb (%r14,%rcx), %al
movb %al, 0x2(%rsp)
leal 0x4(%rbx), %eax
leal -0x3fc(%rbx), %ecx
cmpl %esi, %eax
cmovbl %eax, %ecx
leal 0x5(%rbx), %eax
leal -0x3fb(%rbx), %edx
cmpl %esi, %eax
cmovbl %eax, %edx
leal 0x8(%rbx), %eax
addl $0xfffffc08, %ebx       # imm = 0xFFFFFC08
cmpl %esi, %eax
cmovbl %eax, %ebx
movzbl (%r14,%rcx), %ecx
leal (%rbx,%rdi), %eax
movb (%r14,%rdx), %dl
movb %dl, 0x3(%rsp)
movl %ecx, 0x4(%rsp)
testb $0x10, %cl
jne 0xa253
cmpw $0x4, 0xc(%rsp)
ja 0xa272
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa2ba
movl $0x400, %r13d           # imm = 0x400
subl %ebx, %r13d
movl %ebx, %esi
addq %r14, %rsi
leaq 0x8(%rsp), %r15
jmp 0xa30c
movl %ebx, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa2b0
movl $0x400, %r13d           # imm = 0x400
subl %ebx, %r13d
addq %r14, %rsi
leaq 0x1c(%rsp), %r15
jmp 0xa30c
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa2c6
movl $0x400, %r13d           # imm = 0x400
subl %ebx, %r13d
movl %ebx, %esi
addq %r14, %rsi
leaq 0x14(%rsp), %r15
movq %r15, %rdi
movq %r13, %rdx
callq 0x4270
addq %r15, %r13
addl $0xfffffc04, %ebx       # imm = 0xFFFFFC04
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4270
jmp 0xa2d0
movl (%r14,%rsi), %eax
movl %eax, 0x1c(%rsp)
jmp 0xa32e
movl %ebx, %eax
movl (%r14,%rax), %eax
movl %eax, 0x8(%rsp)
jmp 0xa32e
movl %ebx, %eax
movl (%r14,%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x30(%rsp), %rax
movl (%rax), %ebx
leal 0xc(%rbx), %eax
addl $0xfffffc0c, %ebx       # imm = 0xFFFFFC0C
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ebx
leal -0x3fd(%rbx), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xa579
movl $0x400, %r13d           # imm = 0x400
subl %ebx, %r13d
movl %ebx, %esi
addq %r14, %rsi
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r13, %rdx
callq 0x4270
addq %r15, %r13
addl $0xfffffc04, %ebx       # imm = 0xFFFFFC04
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4270
movq 0x20(%rsp), %rax
leal (%rax,%r12), %r13d
movl $0x1, %r15d
cmpl %r13d, 0x404(%r14)
jae 0xa35a
movq %r14, %rdi
movl %ebp, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %ebx
movl %ebx, %r15d
jg 0xa337
cmpl $-0x1, %r15d
je 0xa36a
testl %r15d, %r15d
jne 0xa378
jmp 0xa815
callq 0x4040
cmpl $0x4, (%rax)
jne 0xa827
cmpl $-0x1, %r15d
je 0xa337
movl 0x400(%r14), %ecx
movl 0x404(%r14), %edx
movq 0x20(%rsp), %rax
leal (%rcx,%rax), %esi
cmpl $0x400, %esi            # imm = 0x400
leal -0x400(%rcx,%rax), %eax
cmovbl %esi, %eax
leaq (%rax,%r14), %rsi
testl %edx, %edx
je 0xa3ce
leal (%rdx,%rcx), %edi
cmpl $0x400, %edi            # imm = 0x400
leal -0x400(%rdx,%rcx), %ecx
cmovbl %edi, %ecx
leal (%r14,%rcx), %ebx
cmpl %eax, %ecx
cmovbel 0x30(%rsp), %ebx
subl %esi, %ebx
jmp 0xa3d0
xorl %ebx, %ebx
leaq 0x48(%rsp), %r15
cmpl %r12d, %ebx
cmovael %r12d, %ebx
movq %r15, 0x38(%rsp)
leaq (%rsi,%rbx), %rdx
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0xec92
subl %ebx, %r12d
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4070
movq 0x12b9e(%rip), %rbp     # 0x1cfa8
movl $0x1, %edx
movq %rbp, %rdi
leaq 0xc2ce(%rip), %rsi      # 0x166e7
callq 0x4330
cmpb $0x2, 0x2(%rsp)
movl $0x20, %eax
movl $0x7b, %ecx
cmovel %ecx, %eax
movl $0x20, %ebx
movl $0x7d, %ecx
cmovel %ecx, %ebx
testb $0x8, 0x4(%rsp)
movl $0x5b, %ecx
cmovnel %ecx, %eax
movb %al, (%rsp)
movl $0x1, %edx
movq %rbp, %rdi
movq %rsp, %rsi
callq 0x4330
movl 0x2c(%rsp), %ebp
movq 0x20(%rsp), %r12
cmpb $0x2, 0x1(%rsp)
jne 0xa498
testb $0x4, 0x4(%rsp)
movl $0x73, %eax
movl $0x2b, %ecx
cmovel %ecx, %eax
movb %al, (%rsp)
movl $0x1, %edx
movq 0x12b1a(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
jmp 0xa4b0
movb $0x20, (%rsp)
movl $0x1, %edx
movq 0x12b00(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
testb $0x8, 0x4(%rsp)
movzbl %bl, %eax
movl $0x5d, %ecx
cmovnel %ecx, %eax
movb %al, (%rsp)
movl $0x1, %edx
movq 0x12ad9(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
movb 0x1(%rsp), %cl
cmpb $0x3, %cl
leaq 0xc20a(%rip), %rsi      # 0x166ef
leaq 0xc200(%rip), %rax      # 0x166ec
cmoveq %rax, %rsi
cmpb $0x1, %cl
leaq 0xc1ef(%rip), %rax      # 0x166e9
cmoveq %rax, %rsi
movl $0x2, %edx
movq 0x12a9e(%rip), %rdi     # 0x1cfa8
callq 0x4330
cmpb $0x0, 0x2(%rsp)
movl $0x20, %eax
movl $0x7b, %ecx
cmovel %ecx, %eax
movb %al, (%rsp)
movl $0x1, %edx
movq 0x12a78(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
cmpb $0x0, 0x1(%rsp)
je 0xa559
movb $0x20, (%rsp)
movl $0x1, %edx
movq 0x12a59(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
jmp 0xa5bc
movb 0x3(%rsp), %al
cmpb $0x6, %al
jne 0xa575
cmpw $0x4, 0xc(%rsp)
ja 0xa588
movzwl 0x8(%rsp), %eax
testl $0xff7f, %eax           # imm = 0xFF7F
jmp 0xa593
testb %al, %al
jmp 0xa593
movl %ebx, %eax
movl (%r14,%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0xa32e
movl 0x14(%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, (%rsp)
movl $0x1, %edx
movq 0x129f4(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
cmpb $0x0, 0x2(%rsp)
movl $0x20, %eax
movl $0x7d, %ecx
cmovel %ecx, %eax
movb %al, (%rsp)
movl $0x1, %edx
movq 0x129cb(%rip), %rdi     # 0x1cfa8
movq %rsp, %rsi
callq 0x4330
movl $0x2, %edx
movq 0x129b7(%rip), %rdi     # 0x1cfa8
leaq 0xc0fa(%rip), %rsi      # 0x166f2
callq 0x4330
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x1299a(%rip), %rdi     # 0x1cfa8
callq 0x4330
cmpb $0x0, 0x1(%rsp)
je 0xa65f
movl 0x4(%rsp), %eax
andl $0x10, %eax
je 0xa65f
movl $0x7, %edx
movq 0x12979(%rip), %rdi     # 0x1cfa8
leaq 0xc0bf(%rip), %rsi      # 0x166f5
callq 0x4330
movl 0x1c(%rsp), %esi
movq 0x12962(%rip), %rdi     # 0x1cfa8
callq 0x44e0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc829(%rip), %rsi      # 0x16e83
callq 0x4330
cmpb $0x0, 0x1(%rsp)
jne 0xa73d
cmpb $0x6, 0x3(%rsp)
jne 0xa73d
cmpw $0x4, 0xc(%rsp)
ja 0xa6ba
movl 0x8(%rsp), %eax
andl $0x7f, %eax
je 0xa6e2
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0xa73d
movl $0xa, %edx
movq 0x12903(%rip), %rdi     # 0x1cfa8
leaq 0xc061(%rip), %rsi      # 0x1670d
callq 0x4330
movl 0x8(%rsp), %esi
andl $0x7f, %esi
jmp 0xa71d
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
je 0xa701
cmpl $0x1, %eax
jne 0xa73d
movl $0xf, %edx
movq 0x128d4(%rip), %rdi     # 0x1cfa8
leaq 0xc022(%rip), %rsi      # 0x166fd
callq 0x4330
jmp 0xa719
movl $0xf, %edx
movq 0x128ba(%rip), %rdi     # 0x1cfa8
leaq 0xc008(%rip), %rsi      # 0x166fd
callq 0x4330
movzbl 0x9(%rsp), %esi
jmp 0xa71d
movl $0xa, %edx
movq 0x1289b(%rip), %rdi     # 0x1cfa8
leaq 0xbff9(%rip), %rsi      # 0x1670d
callq 0x4330
movl 0x10(%rsp), %esi
movq 0x12884(%rip), %rdi     # 0x1cfa8
callq 0x44e0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc74b(%rip), %rsi      # 0x16e83
callq 0x4330
testb $0x3, 0x4(%rsp)
je 0xa774
xorl %eax, %eax
testb $0x2, 0x4(%rsp)
sete %al
leaq 0xe(,%rax,8), %rdx
leaq 0xbfbb(%rip), %rsi      # 0x16718
leaq 0xbfc3(%rip), %rax      # 0x16727
cmoveq %rax, %rsi
movq 0x12839(%rip), %rdi     # 0x1cfa8
callq 0x4330
movl $0x1, %edx
movq 0x12828(%rip), %rdi     # 0x1cfa8
leaq 0xc38e(%rip), %rsi      # 0x16b15
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 0xedbd
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0xa132
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x42f0
jmp 0xa132
cmpl $0x3f, %eax
jne 0xa7e9
xorl %eax, %eax
jmp 0xa806
movq 0x127f8(%rip), %rdi     # 0x1cfe8
leaq 0xbf47(%rip), %rsi      # 0x1673e
movl $0x28, %edx
callq 0x4330
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x4190
movl $0x0, (%rax)
jmp 0xa838
movq %rax, %rbx
movl $0x4, %edi
callq 0x4190
movl (%rbx), %ecx
movl %ecx, (%rax)
leaq 0x12301(%rip), %rsi     # 0x1cb40
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0
movl $0x4, %edi
callq 0x4190
movq %rax, %rbx
callq 0x4040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x122ea(%rip), %rsi     # 0x1cb50
movq %rbx, %rdi
jmp 0xa842
jmp 0xa86f
jmp 0xa86f
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0xa889
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x42f0
movq %rbx, %rdi
callq 0x44f0