// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
cmpl $0x6, %esi
jbe 0xddf8
movl %esi, %ebp
movq %rdi, %r14
movl 0x400(%rdi), %r15d
leal -0x400(%r15), %eax
cmpl $0x400, %r15d           # imm = 0x400
cmovbl %r15d, %eax
cmpl $0x14, %esi
ja 0xd9c5
cmpb $0x65, (%r14,%rax)
je 0xddf8
leal 0x2(%r15), %eax
leal -0x3fe(%r15), %r13d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
leal -0x3fd(%r13), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
movq %rcx, 0x10(%rsp)
ja 0xda48
movq %rbp, 0x8(%rsp)
movl $0x400, %ebp            # imm = 0x400
subl %r13d, %ebp
movl %r13d, %esi
addq %r14, %rsi
leaq 0x24(%rsp), %rbx
movq %rbx, %rdi
movl %edx, 0x18(%rsp)
movq %rbp, %rdx
movl %r8d, 0x1c(%rsp)
movl %r9d, %r12d
callq 0x4270
leaq (%rsp,%rbp), %rdi
addq $0x24, %rdi
movq 0x8(%rsp), %rbp
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r13, %rdx
callq 0x4270
movl 0x18(%rsp), %edx
movl %r12d, %r9d
movl 0x1c(%rsp), %r8d
jmp 0xda4e
movl %r13d, %ebx
addq %r14, %rbx
leal 0x6(%r15), %ecx
leal -0x3fa(%r15), %eax
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %edx, (%rbx)
jne 0xddc4
movzbl %r8b, %r12d
movsbl (%r14,%rax), %eax
cmpl %eax, %r12d
jne 0xdb0a
xorl %r13d, %r13d
testb %r9b, %r9b
je 0xddc4
movq 0xf513(%rip), %rbx      # 0x1cfa8
leaq 0x85e1(%rip), %rsi      # 0x1607d
movl $0x9, %edx
movq %rbx, %rdi
movl %r8d, %r15d
callq 0x4330
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x4330
movq %rax, %rbx
leaq 0x8878(%rip), %rsi      # 0x16342
movl $0x2, %edx
movq %rax, %rdi
callq 0x4330
leaq 0x8823(%rip), %rax      # 0x16301
leaq 0x77af(%rip), %rsi      # 0x15294
testb %r15b, %r15b
cmovneq %rax, %rsi
movl $0x7, %edx
movq %rbx, %rdi
callq 0x4330
leaq 0x86a5(%rip), %rsi      # 0x161a5
movl $0x2, %edx
jmp 0xdb9c
leal 0x3(%r12), %ecx
cmpl %eax, %ecx
jne 0xdbb0
movl $0x1, %r13d
testb %r9b, %r9b
je 0xddc4
movq 0xf47b(%rip), %rbx      # 0x1cfa8
leaq 0x8549(%rip), %rsi      # 0x1607d
movl $0x9, %edx
movq %rbx, %rdi
callq 0x4330
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x4330
movq %rax, %rbx
leaq 0x87e3(%rip), %rsi      # 0x16342
movl $0x2, %edx
movq %rax, %rdi
callq 0x4330
leaq 0x76ee(%rip), %rax      # 0x15261
leaq 0x8c61(%rip), %rsi      # 0x167db
testb %r12b, %r12b
cmovneq %rax, %rsi
xorq $0x5, %r12
movq %rbx, %rdi
movq %r12, %rdx
callq 0x4330
leaq 0x87ae(%rip), %rsi      # 0x16345
movl $0xc, %edx
movq %rbx, %rdi
callq 0x4330
movl 0x400(%r14), %r15d
jmp 0xddc4
cmpb $0x2, %al
setne %al
orb %r8b, %al
xorb $0x1, %r9b
orb %al, %r9b
je 0xdbd1
movzbl %al, %r13d
negl %r13d
orl $0x1, %r13d
jmp 0xddc4
movq 0xf3d0(%rip), %rbx      # 0x1cfa8
leaq 0x849e(%rip), %rsi      # 0x1607d
movl $0x9, %edx
movq %rbx, %rdi
callq 0x4330
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x4330
leaq 0x85b9(%rip), %rsi      # 0x161c0
movl $0x13, %edx
movq %rax, %rdi
callq 0x4330
movl 0x400(%r14), %r15d
movl $0x1, %r13d
cmpl $0x11, %ebp
jb 0xddc4
leal 0xb(%r15), %eax
leal -0x3f5(%r15), %r12d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r12d
leal -0x3ff(%r12), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xdc84
movl $0x400, %ebx            # imm = 0x400
subl %r12d, %ebx
movl %r12d, %esi
addq %r14, %rsi
leaq 0x2(%rsp), %rdi
movq %rbx, %rdx
callq 0x4270
leaq (%rsp,%rbx), %rdi
addq $0x2, %rdi
addl $0xfffffc02, %r12d      # imm = 0xFFFFFC02
movq %r14, %rsi
movq %r12, %rdx
callq 0x4270
jmp 0xdc91
movl %r12d, %eax
movzwl (%r14,%rax), %eax
movw %ax, 0x2(%rsp)
movq %rbp, 0x8(%rsp)
leal 0xa(%r15), %eax
leal -0x3f6(%r15), %ecx
movl $0x400, %r13d           # imm = 0x400
cmpl %r13d, %eax
cmovbl %eax, %ecx
movsbl (%r14,%rcx), %r12d
movl $0x0, 0x4(%rsp)
leal 0xd(%r15), %eax
leal -0x3f3(%r15), %ebx
cmpl %r13d, %eax
cmovbl %eax, %ebx
leal -0x3fd(%rbx), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xdd0b
subl %ebx, %r13d
movl %ebx, %esi
addq %r14, %rsi
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
callq 0x4270
leaq (%rsp,%r13), %rdi
addq $0x20, %rdi
addl $0xfffffc04, %ebx       # imm = 0xFFFFFC04
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4270
jmp 0xdd10
movl %ebx, %ebp
addq %r14, %rbp
movl $0x1, %r13d
movl (%rbp), %ebp
leal -0x400(%r15), %eax
cmpl $0x400, %r15d           # imm = 0x400
cmovbl %r15d, %eax
xorl %r8d, %r8d
cmpb $0x65, (%r14,%rax)
movl $0x0, %ecx
jne 0xdda9
leal 0x11(%r15), %eax
addl $0xfffffc11, %r15d      # imm = 0xFFFFFC11
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xdda0
movl $0x400, %ebx            # imm = 0x400
subl %r15d, %ebx
movl %r15d, %esi
addq %r14, %rsi
leaq 0x4(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rdx
callq 0x4270
leaq (%rsp,%rbx), %rdi
addq $0x4, %rdi
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r15, %rdx
callq 0x4270
movl (%r13), %r8d
movl $0x1, %r13d
jmp 0xdda7
movl %r15d, %eax
movl (%r14,%rax), %r8d
movl %ebp, %ecx
movzwl 0x2(%rsp), %esi
movl %r12d, %edi
movl %ebp, %edx
callq 0xd4b4
movl 0x400(%r14), %r15d
movq 0x8(%rsp), %rbp
leal (%r15,%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
leal -0x400(%r15,%rbp), %ecx
cmovbl %eax, %ecx
movl %ecx, 0x400(%r14)
subl %ebp, 0x404(%r14)
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x4190
leaq 0xecf7(%rip), %rsi      # 0x1cb00
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0