// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rcx, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
cmpl $0x6, %esi
jbe 0xdd5b
movl %r8d, %r12d
movl %edx, %ebx
movq %rdi, %rbp
movl 0x400(%rdi), %r15d
leal -0x400(%r15), %eax
cmpl $0x400, %r15d           # imm = 0x400
cmovbl %r15d, %eax
cmpl $0x14, 0x28(%rsp)
ja 0xd932
cmpb $0x65, (%rbp,%rax)
je 0xdd5b
leal 0x2(%r15), %eax
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xd99d
movl %r12d, 0x34(%rsp)
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %rbp, %rsi
leaq 0x24(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
movl %r9d, %r14d
callq 0x4270
addq %r13, %r12
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r12, %rdi
movl 0x34(%rsp), %r12d
movq %rbp, %rsi
movq %r15, %rdx
callq 0x4270
movl %r14d, %r9d
jmp 0xd9a8
movl %r15d, %eax
movl (%rbp,%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x400(%rbp), %ecx
leal 0x6(%rcx), %eax
leal -0x3fa(%rcx), %edx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ebx, 0x24(%rsp)
jne 0xdd28
movzbl %r12b, %r13d
movsbl (%rbp,%rdx), %edx
cmpl %edx, %r13d
jne 0xda6e
testb %r9b, %r9b
je 0xda61
movq 0xf5bc(%rip), %r15      # 0x1cfa8
leaq 0x869f(%rip), %rsi      # 0x16092
movl $0x9, %edx
movq %r15, %rdi
callq 0x4330
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r15, %rdi
callq 0x4330
movq %rax, %r15
leaq 0x8939(%rip), %rsi      # 0x16357
movl $0x2, %edx
movq %rax, %rdi
callq 0x4330
leaq 0x88e4(%rip), %rax      # 0x16316
leaq 0x7870(%rip), %rsi      # 0x152a9
testb %r12b, %r12b
cmovneq %rax, %rsi
movl $0x7, %edx
movq %r15, %rdi
callq 0x4330
leaq 0x8766(%rip), %rsi      # 0x161ba
movl $0x2, %edx
movq %r15, %rdi
callq 0x4330
movl 0x400(%rbp), %ecx
xorl %eax, %eax
jmp 0xdd28
leal 0x3(%r13), %esi
cmpl %edx, %esi
jne 0xdb06
testb %r9b, %r9b
je 0xdd1d
movq 0xf51e(%rip), %r15      # 0x1cfa8
leaq 0x8601(%rip), %rsi      # 0x16092
movl $0x9, %edx
movq %r15, %rdi
callq 0x4330
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r15, %rdi
callq 0x4330
movq %rax, %r15
leaq 0x889b(%rip), %rsi      # 0x16357
movl $0x2, %edx
movq %rax, %rdi
callq 0x4330
leaq 0x77a6(%rip), %rax      # 0x15276
leaq 0x8d19(%rip), %rsi      # 0x167f0
testb %r13b, %r13b
cmovneq %rax, %rsi
xorq $0x5, %r13
movq %r15, %rdi
movq %r13, %rdx
callq 0x4330
leaq 0x8866(%rip), %rsi      # 0x1635a
movl $0xc, %edx
movq %r15, %rdi
callq 0x4330
jmp 0xdd1d
cmpb $0x2, %dl
setne %dl
orb %r12b, %dl
jne 0xdd28
testb %r9b, %r9b
je 0xdd1d
movq 0xf483(%rip), %r15      # 0x1cfa8
leaq 0x8566(%rip), %rsi      # 0x16092
movl $0x9, %edx
movq %r15, %rdi
callq 0x4330
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r15, %rdi
callq 0x4330
leaq 0x8681(%rip), %rsi      # 0x161d5
movl $0x13, %edx
movq %rax, %rdi
callq 0x4330
cmpl $0x11, 0x28(%rsp)
jb 0xdd1d
movl 0x400(%rbp), %r15d
leal 0xb(%r15), %eax
addl $0xfffffc0b, %r15d      # imm = 0xFFFFFC0B
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3ff(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xdbce
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %rbp, %rsi
leaq 0xe(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x4270
addq %r13, %r12
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x4270
jmp 0xdbdb
movl %r15d, %eax
movzwl (%rbp,%rax), %eax
movw %ax, 0xe(%rsp)
movl 0x400(%rbp), %r15d
leal 0xa(%r15), %eax
leal -0x3f6(%r15), %ecx
movl $0x400, %r12d           # imm = 0x400
cmpl %r12d, %eax
cmovbl %eax, %ecx
movq %rbp, %r14
movsbl (%rbp,%rcx), %edi
movl $0x0, 0x10(%rsp)
leal 0xd(%r15), %eax
addl $0xfffffc0d, %r15d      # imm = 0xFFFFFC0D
cmpl %r12d, %eax
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xdc60
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0x14(%rsp), %r13
movl %edi, %ebp
movq %r13, %rdi
movq %r12, %rdx
callq 0x4270
addq %r13, %r12
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4270
movl %ebp, %edi
jmp 0xdc6b
movl %r15d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x14(%rsp)
movl 0x400(%r14), %r12d
leal -0x400(%r12), %eax
cmpl $0x400, %r12d           # imm = 0x400
cmovbl %r12d, %eax
xorl %r15d, %r15d
movq %r14, %rbp
cmpb $0x65, (%r14,%rax)
jne 0xdd07
movl 0x14(%rsp), %r15d
leal 0x11(%r12), %eax
addl $0xfffffc11, %r12d      # imm = 0xFFFFFC11
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r12d
leal -0x3fd(%r12), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xdcfc
movl $0x400, %r13d           # imm = 0x400
subl %r12d, %r13d
movl %r12d, %esi
addq %rbp, %rsi
leaq 0x10(%rsp), %rbx
movl %edi, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rdx
callq 0x4270
addq %rbx, %r13
addl $0xfffffc04, %r12d      # imm = 0xFFFFFC04
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x4270
movl 0x18(%rsp), %edi
jmp 0xdd07
movl %r12d, %eax
movl (%rbp,%rax), %eax
movl %eax, 0x10(%rsp)
movzwl 0xe(%rsp), %esi
movl 0x14(%rsp), %edx
movl 0x10(%rsp), %r8d
movl %r15d, %ecx
callq 0xd3d4
movl 0x400(%rbp), %ecx
movl $0x1, %eax
movq 0x28(%rsp), %rsi
leal (%rcx,%rsi), %edx
cmpl $0x400, %edx            # imm = 0x400
leal -0x400(%rcx,%rsi), %ecx
cmovbl %edx, %ecx
movl %ecx, 0x400(%rbp)
subl %esi, 0x404(%rbp)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x4190
leaq 0xed94(%rip), %rsi      # 0x1cb00
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0