// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/dinitctl.cc
// Function: process_service_event(cpbuffer<1024u>&, unsigned int, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool, bool)
static int process_service_event(cpbuffer_t &rbuffer, unsigned pktlen, handle_t handle,
        const std::string &service_name, bool do_stop, bool verbose)
{
    using std::cout;

    // earlier versions do not include status info, the size in that case is
    // base_pkt_size:
    constexpr unsigned base_pkt_size = 2 + sizeof(handle_t) + 1;
    if (pktlen < base_pkt_size) {
        throw dinit_protocol_error();
    }

    // version 5 packets include extended staus info:
    if (rbuffer[0] == (char)cp_info::SERVICEEVENT5 && pktlen < (base_pkt_size + STATUS_BUFFER5_SIZE)) {
        throw dinit_protocol_error();
    }

    service_event_t completion_event;
    service_event_t cancelled_event;
    if (do_stop) {
        completion_event = service_event_t::STOPPED;
        cancelled_event = service_event_t::STOPCANCELLED;
    }
    else {
        completion_event = service_event_t::STARTED;
        cancelled_event = service_event_t::STARTCANCELLED;
    }

    handle_t ev_handle;
    rbuffer.extract((char *)&ev_handle, 2, sizeof(ev_handle));
    service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
    if (ev_handle == handle) {
        if (event == completion_event) {
            if (verbose) {
                cout << "Service '" << service_name << "' " << describe_state(do_stop)
                        << ".\n";
            }
            rbuffer.consume(pktlen);
            return 0;
        }
        else if (event == cancelled_event) {
            if (verbose) {
                cout << "Service '" << service_name << "' " << describe_verb(do_stop)
                        << " cancelled.\n";
            }
            rbuffer.consume(pktlen);
            return 1;
        }
        else if (!do_stop && event == service_event_t::FAILEDSTART) {
            if (verbose) {
                cout << "Service '" << service_name << "' failed to start.\n";
                if (pktlen >= base_pkt_size + STATUS_BUFFER_SIZE) {
                    uint16_t launch_stage;
                    rbuffer.extract((char *)&launch_stage, base_pkt_size + 4,
                                    sizeof(uint16_t));

                    stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[base_pkt_size + 3]);
                    int exit_status;
                    int exit_si_code = 0;
                    int exit_si_status = 0;
                    rbuffer.extract((char *)&exit_status, base_pkt_size + 6, sizeof(exit_status));
                    if (rbuffer[0] == (char)cp_info::SERVICEEVENT5) {
                        exit_si_code = exit_status;
                        rbuffer.extract((char *)&exit_si_status,
                                base_pkt_size + 6 + sizeof(exit_si_code), sizeof(exit_si_status));
                    }

                    print_failure_details(stop_reason, launch_stage, exit_status, exit_si_code, exit_si_status);
                }
            }
            rbuffer.consume(pktlen);
            return 1;
        }
    }
    rbuffer.consume(pktlen);
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpl $0x6, %esi
jbe 0xa7e9
movl %r9d, %r13d
movl %r8d, %r15d
movl %edx, %ebp
movl %esi, %ebx
movq %rdi, %r14
movq %rcx, 0x8(%rsp)
movl 0x400(%rdi), %ecx
leal -0x400(%rcx), %eax
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
cmpl $0x14, %esi
ja 0xa5b8
cmpb $0x65, (%r14,%rax)
je 0xa7e9
leaq 0x14(%rsp), %r12
pushq $0x2
popq %rdx
pushq $0x4
popq %rcx
movq %r14, %rdi
movq %r12, %rsi
callq 0xb3a0
movl 0x400(%r14), %eax
leal 0x6(%rax), %ecx
addl $0xfffffc06, %eax       # imm = 0xFFFFFC06
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
cmpl %ebp, (%r12)
jne 0xa659
movzbl %r15b, %ecx
movsbl (%r14,%rax), %eax
cmpl %eax, %ecx
jne 0xa661
xorl %ebp, %ebp
testb %r13b, %r13b
je 0xa7ce
movq 0x1099d(%rip), %rdi     # 0x1afa8
leaq 0x7a5b(%rip), %rsi      # 0x1206d
callq 0x42f0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x42d0
leaq 0x7d07(%rip), %rsi      # 0x12332
movq %rax, %rdi
callq 0x42f0
leaq 0x7cb7(%rip), %rcx      # 0x122f1
leaq 0x6c43(%rip), %rsi      # 0x11284
testb %r15b, %r15b
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x42f0
leaq 0x7b3e(%rip), %rsi      # 0x12195
jmp 0xa6c7
pushq $-0x1
popq %rbp
jmp 0xa7ce
addl $0x3, %ecx
cmpl %eax, %ecx
jne 0xa6d4
pushq $0x1
popq %rbp
testb %r13b, %r13b
je 0xa7ce
movq 0x1092d(%rip), %rdi     # 0x1afa8
leaq 0x79eb(%rip), %rsi      # 0x1206d
callq 0x42f0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x42d0
leaq 0x7c97(%rip), %rsi      # 0x12332
movq %rax, %rdi
callq 0x42f0
leaq 0x6ba7(%rip), %rcx      # 0x11251
leaq 0x811a(%rip), %rsi      # 0x127cb
testb %r15b, %r15b
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x42f0
leaq 0x7c6e(%rip), %rsi      # 0x12335
movq %rax, %rdi
callq 0x42f0
jmp 0xa7ce
cmpb $0x2, %al
setne %al
orb %r15b, %al
xorb $0x1, %r13b
orb %al, %r13b
je 0xa6f2
movzbl %al, %ebp
negl %ebp
orl $0x1, %ebp
jmp 0xa7ce
movq 0x108af(%rip), %rdi     # 0x1afa8
leaq 0x796d(%rip), %rsi      # 0x1206d
callq 0x42f0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x42d0
leaq 0x7a97(%rip), %rsi      # 0x121b0
movq %rax, %rdi
callq 0x42f0
pushq $0x1
popq %rbp
cmpl $0x11, %ebx
jb 0xa7ce
leaq 0x2(%rsp), %rsi
pushq $0xb
popq %rdx
pushq $0x2
popq %rcx
movq %r14, %rdi
callq 0xb3a0
movl 0x400(%r14), %eax
leal 0xa(%rax), %ecx
addl $0xfffffc0a, %eax       # imm = 0xFFFFFC0A
movl $0x400, %r12d           # imm = 0x400
cmpl %r12d, %ecx
cmovbl %ecx, %eax
movsbl (%r14,%rax), %r15d
andl $0x0, 0x10(%rsp)
leaq 0x4(%rsp), %rsi
pushq $0xd
popq %rdx
pushq $0x4
popq %rcx
movq %r14, %rdi
callq 0xb3a0
movl 0x400(%r14), %eax
leal -0x400(%rax), %ecx
cmpl %r12d, %eax
cmovbl %eax, %ecx
xorl %r8d, %r8d
cmpb $0x65, (%r14,%rcx)
movl $0x0, %r12d
jne 0xa7ba
movl 0x4(%rsp), %r12d
leaq 0x10(%rsp), %r13
pushq $0x11
popq %rdx
pushq $0x4
popq %rcx
movq %r14, %rdi
movq %r13, %rsi
callq 0xb3a0
movl (%r13), %r8d
movzwl 0x2(%rsp), %esi
movl 0x4(%rsp), %edx
movl %r15d, %edi
movl %r12d, %ecx
callq 0xa29a
movq %r14, %rdi
movl %ebx, %esi
callq 0xb41c
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x4190
leaq 0x10308(%rip), %rsi     # 0x1ab00
movq %rax, %rdi
xorl %edx, %edx
callq 0x44f0