// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, 0x18(%rsp)
movl %edx, 0x24(%rsp)
movl %esi, %r14d
movq %rdi, %r15
movl $0x1, %ebx
cmpl $0x0, 0x404(%r15)
je 0xf0d7
cmpl $-0x1, %ebx
je 0xf0ed
testl %ebx, %ebx
jne 0xf0fb
jmp 0xf43d
movq %r15, %rdi
movl %r14d, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %ebp
movl %ebp, %ebx
jg 0xf0ba
jmp 0xf0c9
callq 0x4040
cmpl $0x4, (%rax)
jne 0xf486
cmpl $-0x1, %ebx
je 0xf0ba
movl 0x400(%r15), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movb (%r15,%rcx), %bl
cmpb $0x64, %bl
jl 0xf42e
movl $0x1, %ebp
cmpl $0x1, 0x404(%r15)
ja 0xf147
movq %r15, %rdi
movl %r14d, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %r12d
movl %r12d, %ebp
jg 0xf122
cmpl $-0x1, %ebp
je 0xf155
testl %ebp, %ebp
jne 0xf163
jmp 0xf43d
callq 0x4040
cmpl $0x4, (%rax)
jne 0xf44f
cmpl $-0x1, %ebp
je 0xf122
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
movl %eax, 0x400(%r15)
decl 0x404(%r15)
movl $0x1, %r12d
cmpl %ebp, 0x404(%r15)
jae 0xf1b8
movq %r15, %rdi
movl %r14d, %esi
callq 0xeffc
testl %eax, %eax
cmovlel %eax, %r13d
movl %r13d, %r12d
jg 0xf193
cmpl $-0x1, %r12d
je 0xf1c8
testl %r12d, %r12d
jne 0xf1d6
jmp 0xf43d
callq 0x4040
cmpl $0x4, (%rax)
jne 0xf44f
cmpl $-0x1, %r12d
je 0xf193
cmpq $0x0, 0x18(%rsp)
je 0xf3ee
movl %ebx, %eax
andb $0x7e, %al
cmpb $0x64, %al
jne 0xf3ee
cmpb $0x6, %bpl
jbe 0xf473
movl 0x400(%r15), %r13d
leal 0x1(%r13), %eax
addl $0xfffffc01, %r13d      # imm = 0xFFFFFC01
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
leal -0x3fd(%r13), %eax
movl %r13d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xf262
movl $0x400, %r12d           # imm = 0x400
subl %r13d, %r12d
addq %r15, %rsi
leaq 0x14(%rsp), %rdi
movq %r12, %rdx
callq 0x4270
leaq 0x14(%rsp), %rax
addq %rax, %r12
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x4270
jmp 0xf26a
movl (%r15,%rsi), %eax
movl %eax, 0x14(%rsp)
movl 0x24(%rsp), %eax
cmpl %eax, 0x14(%rsp)
jne 0xf3ee
movl 0x400(%r15), %r13d
leal 0x5(%r13), %eax
leal -0x3fb(%r13), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movzbl (%r15,%rcx), %eax
testl %eax, %eax
je 0xf33b
cmpl $0x2, %eax
je 0xf2bb
cmpl $0x1, %eax
jne 0xf3ee
movq 0x18(%rsp), %rax
movb $0x1, 0x1(%rax)
jmp 0xf3ee
leal 0x9(%r13), %eax
leal -0x3f7(%r13), %ecx
movl $0x400, %esi            # imm = 0x400
cmpl %esi, %eax
cmovbl %eax, %ecx
movsbl (%r15,%rcx), %eax
movq 0x18(%rsp), %rdx
movb $0x1, 0x2(%rdx)
leal 0xc(%r13), %ecx
addl $0xfffffc0c, %r13d      # imm = 0xFFFFFC0C
cmpl %esi, %ecx
cmovbl %ecx, %r13d
movl %eax, 0x4(%rdx)
leal -0x3fd(%r13), %eax
movl %r13d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xf348
movl $0x400, %r12d           # imm = 0x400
subl %r13d, %r12d
addq %r15, %rsi
leaq 0xc(%rsp), %rdi
movq %r12, %rdx
callq 0x4270
leaq 0xc(%rsp), %rax
addq %rax, %r12
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x4270
jmp 0xf350
movq 0x18(%rsp), %rax
movb $0x1, (%rax)
jmp 0xf3ee
movl (%r15,%rsi), %eax
movl %eax, 0xc(%rsp)
cmpb $0x65, %bl
jne 0xf3e2
cmpb $0x14, %bpl
jbe 0xf473
movl 0x400(%r15), %r13d
leal 0x10(%r13), %eax
addl $0xfffffc10, %r13d      # imm = 0xFFFFFC10
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r13d
movl 0xc(%rsp), %ebx
leal -0x3fd(%r13), %eax
movl %r13d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0xf3cb
movl $0x400, %r12d           # imm = 0x400
subl %r13d, %r12d
addq %r15, %rsi
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x4270
leaq 0x10(%rsp), %rax
addq %rax, %r12
addl $0xfffffc04, %r13d      # imm = 0xFFFFFC04
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x4270
jmp 0xf3d3
movl (%r15,%rsi), %eax
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rcx
movl %ebx, 0xc(%rcx)
movl 0x10(%rsp), %eax
movl %eax, 0x10(%rcx)
movl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x8(%rcx)
decl %ebp
movl 0x400(%r15), %eax
addl %ebp, %eax
movl $0xfffffc00, %esi       # imm = 0xFFFFFC00
leal (%rax,%rsi), %ecx
movl $0x400, %edx            # imm = 0x400
cmpl %edx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x400(%r15)
subl %ebp, 0x404(%r15)
leal (%rcx,%rsi), %eax
cmpl %edx, %ecx
cmovbl %ecx, %eax
movb (%r15,%rax), %bl
cmpb $0x63, %bl
jg 0xf122
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x4190
movl $0x0, (%rax)
jmp 0xf462
movq %rax, %r13
movl $0x4, %edi
callq 0x4190
movl (%r13), %ecx
movl %ecx, (%rax)
leaq 0xd6d7(%rip), %rsi      # 0x1cb40
movq %rax, %rdi
xorl %edx, %edx
callq 0x44d0
movl $0x1, %edi
callq 0x4190
leaq 0xd67c(%rip), %rsi      # 0x1cb00
jmp 0xf469
movq %rax, %r12
movl $0x4, %edi
callq 0x4190
movl (%r12), %ecx
jmp 0xf460
nop