// File: /workspace/github_cpp_makefile_star2_O1/davmac314[P]dinit/src/includes/dinit-client.h
// Function: check_protocol_version(int, int, cpbuffer<1024u>&, int)
inline uint16_t check_protocol_version(int minversion, int version, cpbuffer_t &rbuffer, int fd)
{
    using namespace dinit_cptypes;
    constexpr int bufsize = 1;
    char buf[bufsize] = { (char)cp_cmd::QUERYVERSION };
    write_all_x(fd, buf, bufsize);

    wait_for_reply(rbuffer, fd);
    if (rbuffer[0] != (char)cp_rply::CPVERSION) {
        throw cp_read_exception{0};
    }

    // cp_rply::CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
    constexpr int rbufsize = 1 + 2 * sizeof(uint16_t);
    fill_buffer_to(rbuffer, fd, rbufsize);
    uint16_t rminversion;
    uint16_t cpversion;

    rbuffer.extract(reinterpret_cast<char *>(&rminversion), 1, sizeof(uint16_t));
    rbuffer.extract(reinterpret_cast<char *>(&cpversion), 1 + sizeof(uint16_t), sizeof(uint16_t));
    rbuffer.consume(rbufsize);

    if (rminversion > version) {
        // We are too old
        throw cp_old_client_exception();
    }
    if (cpversion < minversion) {
        // Server is too old
        throw cp_old_server_exception();
    }

    return cpversion;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r15d
movq %rdx, %r14
movl %esi, 0x14(%rsp)
movl %edi, 0x10(%rsp)
leaq 0xf(%rsp), %r12
movb $0x0, (%r12)
movl $0x1, %ebx
xorl %r13d, %r13d
movl %ebx, %edx
movl %r15d, %edi
movq %r12, %rsi
callq 0x30c0
cmpl $-0x1, %eax
je 0x4507
addl %eax, %r13d
movslq %eax, %rcx
addq %rcx, %r12
subl %eax, %ebx
xorl %ecx, %ecx
jmp 0x4527
callq 0x3040
movl $0x2, %ecx
cmpl $0x4, (%rax)
je 0x4527
testl %r13d, %r13d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovgl %r13d, %ebp
movl $0x1, %ecx
testb $0x1, %cl
jne 0x4532
testl %ebx, %ebx
jg 0x44e6
jmp 0x4535
movl %ebp, %r13d
cmpl $-0x1, %r13d
je 0x470c
movq %r14, %rdi
movl %r15d, %esi
callq 0x4754
movl 0x400(%r14), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
cmpb $0x3a, (%r14,%rcx)
jne 0x46d6
movl $0x1, %ebx
cmpl $0x4, 0x404(%r14)
ja 0x458d
movq %r14, %rdi
movl %r15d, %esi
callq 0x4a9e
testl %eax, %eax
cmovlel %eax, %ebp
movl %ebp, %ebx
jg 0x456a
cmpl $-0x1, %ebx
je 0x459b
testl %ebx, %ebx
jne 0x45a9
jmp 0x46d6
callq 0x3040
cmpl $0x4, (%rax)
jne 0x46e8
cmpl $-0x1, %ebx
je 0x456a
movl 0x400(%r14), %r15d
leal 0x1(%r15), %eax
movl $0xfffffc01, %ecx       # imm = 0xFFFFFC01
addl %ecx, %r15d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
addl %r15d, %ecx
cmpl $0xfffffbfe, %ecx       # imm = 0xFFFFFBFE
ja 0x460e
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0xc(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x31f0
addq %r13, %r12
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x31f0
jmp 0x461b
movl %r15d, %eax
movzwl (%r14,%rax), %eax
movw %ax, 0xc(%rsp)
movl 0x400(%r14), %r15d
leal 0x3(%r15), %eax
addl $0xfffffc03, %r15d      # imm = 0xFFFFFC03
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3ff(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x467d
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0xa(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x31f0
addq %r13, %r12
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x31f0
jmp 0x468a
movl %r15d, %eax
movzwl (%r14,%rax), %eax
movw %ax, 0xa(%rsp)
movl 0x400(%r14), %eax
leal 0x5(%rax), %ecx
addl $0xfffffc05, %eax       # imm = 0xFFFFFC05
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl %eax, 0x400(%r14)
addl $-0x5, 0x404(%r14)
movzwl 0xc(%rsp), %eax
cmpl 0x14(%rsp), %eax
jg 0x472e
movzwl 0xa(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x4741
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %edi
callq 0x3130
movl $0x0, (%rax)
jmp 0x46fb
movq %rax, %r12
movl $0x4, %edi
callq 0x3130
movl (%r12), %ecx
movl %ecx, (%rax)
leaq 0x931e(%rip), %rsi      # 0xda20
movq %rax, %rdi
xorl %edx, %edx
callq 0x3430
movl $0x4, %edi
callq 0x3130
movq %rax, %rbx
callq 0x3040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x9307(%rip), %rsi      # 0xda30
movq %rbx, %rdi
jmp 0x4705
movl $0x1, %edi
callq 0x3130
leaq 0x92b1(%rip), %rsi      # 0xd9f0
jmp 0x4702
movl $0x1, %edi
callq 0x3130
leaq 0x92b6(%rip), %rsi      # 0xda08
jmp 0x4702