// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/includes/dinit-client.h
// Function: wait_for_reply(cpbuffer<1024u>&, int, unsigned int, observed_states_t*)
inline void wait_for_reply(cpbuffer_t &rbuffer, int fd, dinit_cptypes::handle_t handle, observed_states_t *seen_states)
{
    fill_buffer_to(rbuffer, fd, 1);

    while (rbuffer[0] >= 100) {
        // Information packet; discard.
        cp_info pkt_type = (cp_info) rbuffer[0];
        fill_buffer_to(rbuffer, fd, 2);
        unsigned pktlen = (unsigned char) rbuffer[1];

        rbuffer.consume(1);  // Consume one byte so we'll read one byte of the next packet
        fill_buffer_to(rbuffer, fd, pktlen);

        if (value(pkt_type).is_in(cp_info::SERVICEEVENT, cp_info::SERVICEEVENT5)
                && seen_states != nullptr) {

            // earlier versions do not include status info, the size in that case is
            // base_pkt_size:
            constexpr unsigned base_pkt_size = 2 + sizeof(dinit_cptypes::handle_t) + 1;

            if (pktlen < base_pkt_size) {
                throw dinit_protocol_error();
            }

            // Extract handle, check for match
            dinit_cptypes::handle_t ev_handle;
            rbuffer.extract((char *)&ev_handle, 1, sizeof(ev_handle));
            service_event_t event = static_cast<service_event_t>(rbuffer[1 + sizeof(ev_handle)]);

            if (ev_handle == handle) {
                if (event == service_event_t::STOPPED) {
                    seen_states->stopped = true;
                }
                if (event == service_event_t::STARTED) {
                    seen_states->started = true;
                }
                if (event == service_event_t::FAILEDSTART) {
                    stopped_reason_t stop_reason =
                            static_cast<stopped_reason_t>(rbuffer[base_pkt_size + 2]);

                    seen_states->failed_start = true;
                    seen_states->stop_reason = stop_reason;

                    int exit_status;
                    int exit_si_code;
                    int exit_si_status;
                    rbuffer.extract((char *)&exit_status, base_pkt_size + 5, sizeof(exit_status));
                    if (pkt_type == cp_info::SERVICEEVENT5) {
                        if (pktlen < base_pkt_size + STATUS_BUFFER5_SIZE) {
                            throw dinit_protocol_error();
                        }
                        exit_si_code = exit_status;
                        rbuffer.extract((char *)&exit_si_status,
                                base_pkt_size + 5 + sizeof(exit_si_code), sizeof(exit_si_status));

                        seen_states->exit_si_code = exit_si_code;
                        seen_states->exit_si_status = exit_si_status;
                    }

                    seen_states->exit_status = exit_status;
                }
            }
        }

        rbuffer.consume(pktlen - 1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movl %edx, 0xc(%rsp)
movl %esi, %r14d
movq %rdi, %r15
pushq $0x1
popq %r12
movl %r12d, %edx
callq 0xb33a
movl 0x400(%r15), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movb (%r15,%rcx), %r13b
cmpb $0x64, %r13b
jl 0xb73d
movq %r15, %rdi
movl %r14d, %esi
pushq $0x2
popq %rdx
callq 0xb33a
movl 0x400(%r15), %eax
leal 0x1(%rax), %ecx
addl $0xfffffc01, %eax       # imm = 0xFFFFFC01
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movzbl (%r15,%rax), %ebp
movq %r15, %rdi
movl %r12d, %esi
callq 0xb41c
movq %r15, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0xb33a
testq %rbx, %rbx
je 0xb72c
movl %r13d, %eax
andb $0x7e, %al
cmpb $0x64, %al
jne 0xb72c
cmpb $0x6, %bpl
jbe 0xb74c
movq %r15, %rdi
leaq 0x14(%rsp), %rsi
movl %r12d, %edx
pushq $0x4
popq %rcx
callq 0xb3a0
movl 0xc(%rsp), %eax
cmpl %eax, 0x14(%rsp)
jne 0xb72c
movl 0x400(%r15), %eax
leal 0x5(%rax), %ecx
leal -0x3fb(%rax), %edx
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %edx
movzbl (%r15,%rdx), %ecx
testl %ecx, %ecx
je 0xb729
cmpl $0x2, %ecx
je 0xb6c1
cmpl $0x1, %ecx
jne 0xb72c
movb $0x1, 0x1(%rbx)
jmp 0xb72c
leal 0x9(%rax), %ecx
addl $0xfffffc09, %eax       # imm = 0xFFFFFC09
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movsbl (%r15,%rax), %eax
movb $0x1, 0x2(%rbx)
movl %eax, 0x4(%rbx)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xc
popq %rdx
pushq $0x4
popq %rcx
callq 0xb3a0
cmpb $0x65, %r13b
jne 0xb720
cmpb $0x14, %bpl
jbe 0xb74c
movl 0x8(%rsp), %r13d
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
pushq $0x10
popq %rdx
pushq $0x4
popq %rcx
callq 0xb3a0
movl %r13d, 0xc(%rbx)
movl 0x10(%rsp), %eax
movl %eax, 0x10(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
jmp 0xb72c
movb $0x1, (%rbx)
decl %ebp
movq %r15, %rdi
movl %ebp, %esi
callq 0xb41c
jmp 0xb5e8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x4190
leaq 0xf3a5(%rip), %rsi      # 0x1ab00
movq %rax, %rdi
xorl %edx, %edx
callq 0x44f0
nop