// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_getallenv()
bool control_conn_t::process_getallenv()
{
    // 1 byte packet type
    // 1 byte reserved - must be 0

    constexpr int pkt_size = 2;
    if (rbuf.get_length() < pkt_size) {
        chklen = pkt_size;
        return true;
    }

    uint8_t reserved_byte = rbuf[1];
    if (reserved_byte != 0) {
        char badreqRep[] = { (char)cp_rply::BADREQ };
        if (!queue_packet(badreqRep, 1)) return false;
        bad_conn_close = true;
        return true;
    }

    // The reply looks like:
    // 1 byte - reply type
    // sizeof(size_t) - reply data size
    // n bytes - reply data (NAME=VALUE, separated by nul characters)

    std::vector<char> env_block;
    constexpr size_t env_block_hdr_size = sizeof(size_t) + 1;
    env_block.resize(env_block_hdr_size);

    rbuf.consume(pkt_size);
    auto env = main_env.build();
    for (const char *env_var : env.env_list) {
        if (env_var != nullptr) {
            env_block.insert(env_block.end(), env_var, env_var + strlen(env_var) + 1);
        }
    }

    env_block[0] = (char)cp_rply::ALLENV;
    size_t block_size = env_block.size() - env_block_hdr_size;
    memcpy(env_block.data() + 1, &block_size, sizeof(block_size));
    if (!queue_packet(std::move(env_block))) return false;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
movl $0x2, -0x14(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0x2, %eax
jae 0x5b119
movq -0xe8(%rbp), %rax
movl $0x2, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5b387
movq -0xe8(%rbp), %rdi
addq $0x80, %rdi
movl $0x1, %esi
callq 0x5e160
movb %al, -0x15(%rbp)
movzbl -0x15(%rbp), %eax
cmpl $0x0, %eax
je 0x5b17c
movq -0xe8(%rbp), %rdi
movb 0x1e30e(%rip), %al      # 0x79458
movb %al, -0x16(%rbp)
leaq -0x16(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5b168
movb $0x0, -0x1(%rbp)
jmp 0x5b387
movq -0xe8(%rbp), %rax
movb $0x1, 0x78(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5b387
leaq -0x30(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x30d60
movq -0xf0(%rbp), %rdi
movq $0x9, -0x38(%rbp)
movl $0x9, %esi
callq 0x31490
jmp 0x5b1a7
movq -0xe8(%rbp), %rdi
subq $-0x80, %rdi
movl $0x2, %esi
callq 0x5e1a0
leaq 0x4fd7d(%rip), %rsi     # 0xaaf40
leaq -0x98(%rbp), %rdi
callq 0x2e130
jmp 0x5b1d1
leaq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x15b90
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x15a20
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5f4a0
testb $0x1, %al
jne 0x5b221
jmp 0x5b2fb
leaq -0xa8(%rbp), %rdi
callq 0x38a90
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x5b2e8
leaq -0x30(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x5f580
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5f5b0
movq -0xb8(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0xa280
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rdx
leaq 0x1(%rdx,%rax), %rcx
movq -0xc0(%rbp), %rsi
callq 0x5f4e0
movq %rax, -0xf8(%rbp)
jmp 0x5b2aa
movq -0xf8(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x5b2e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5b37c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2efa0
jmp 0x5b37c
jmp 0x5b2ea
leaq -0xa8(%rbp), %rdi
callq 0x5f5f0
jmp 0x5b205
leaq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5e950
movb $0x4e, (%rax)
leaq -0x30(%rbp), %rdi
callq 0x31650
subq $0x9, %rax
movq %rax, -0xd8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x31180
movq -0xe8(%rbp), %rdi
movq -0xd8(%rbp), %rcx
movq %rcx, 0x1(%rax)
leaq -0x30(%rbp), %rsi
callq 0x5bc70
testb $0x1, %al
jne 0x5b357
movb $0x0, -0x1(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0x5b365
movb $0x1, -0x1(%rbp)
movl $0x1, -0xdc(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2efa0
leaq -0x30(%rbp), %rdi
callq 0x13f20
jmp 0x5b387
leaq -0x30(%rbp), %rdi
callq 0x13f20
jmp 0x5b395
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0xacc0
nop