// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_setenv()
bool control_conn_t::process_setenv()
{
    using std::string;

    string envVar;
    typename string::size_type eq;

    constexpr int pkt_size = 4;
    char badreqRep[] = { (char)cp_rply::BADREQ };
    char okRep[] = { (char)cp_rply::ACK };

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

    envvar_len_t envvar_len;
    rbuf.extract(&envvar_len, 1, sizeof(envvar_len));
    if (envvar_len <= 0 || envvar_len > (1024 - 3)) {
        goto badreq;
    }
    chklen = envvar_len + 1 + sizeof(envvar_len); // packet type + (2 byte) length + envvar

    if (rbuf.get_length() < chklen) {
        // packet not complete yet; read more
        return true;
    }

    envVar = rbuf.extract_string(3, envvar_len);

    eq = envVar.find('=');
    if (eq == envVar.npos) {
        // Unset the env var
        main_env.undefine_var(std::move(envVar), true);
    }
    else if (eq) {
        // Regular set
        main_env.set_var(std::move(envVar), true);
    }
    else {
        // At the beginning of the string
        goto badreq;
    }

    // Success response
    if (!queue_packet(okRep, 1)) return false;

    // Clear the packet from the buffer
    rbuf.consume(chklen);
    chklen = 0;
    return true;

badreq:
    // Queue error response / mark connection bad
    if (!queue_packet(badreqRep, 1)) return false;
    bad_conn_close = true;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xaa40
movq -0x80(%rbp), %rdi
movl $0x4, -0x3c(%rbp)
movb 0x1e585(%rip), %al      # 0x79456
movb %al, -0x3d(%rbp)
movb 0x1e57d(%rip), %al      # 0x79457
movb %al, -0x3e(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0x4, %eax
jae 0x5af09
movq -0x80(%rbp), %rax
movl $0x4, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x5b0a6
movq -0x80(%rbp), %rdi
addq $0x80, %rdi
leaq -0x46(%rbp), %rsi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x5e220
movzwl -0x46(%rbp), %eax
cmpl $0x0, %eax
jle 0x5af3b
movzwl -0x46(%rbp), %eax
cmpl $0x3fd, %eax            # imm = 0x3FD
jle 0x5af40
jmp 0x5b070
movq -0x80(%rbp), %rdi
movzwl -0x46(%rbp), %eax
addl $0x1, %eax
cltq
addq $0x2, %rax
movl %eax, 0x7c(%rdi)
addq $0x80, %rdi
callq 0x5e200
movq -0x80(%rbp), %rcx
cmpl 0x7c(%rcx), %eax
jae 0x5af79
movb $0x1, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x5b0a6
movq -0x80(%rbp), %rsi
subq $-0x80, %rsi
movzwl -0x46(%rbp), %ecx
leaq -0x68(%rbp), %rdi
movl $0x3, %edx
callq 0x5e2d0
jmp 0x5af95
leaq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xa8b0
leaq -0x68(%rbp), %rdi
callq 0xade8
leaq -0x30(%rbp), %rdi
movl $0x3d, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0xa990
movq %rax, -0x38(%rbp)
cmpq $-0x1, -0x38(%rbp)
jne 0x5affb
leaq 0x4ff71(%rip), %rdi     # 0xaaf40
leaq -0x30(%rbp), %rsi
movl $0x1, %edx
callq 0x5f3a0
jmp 0x5afdf
jmp 0x5b01f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xade8
jmp 0x5b0bd
cmpq $0x0, -0x38(%rbp)
je 0x5b01b
leaq 0x4ff37(%rip), %rdi     # 0xaaf40
leaq -0x30(%rbp), %rsi
movl $0x1, %edx
callq 0x2e6d0
jmp 0x5b019
jmp 0x5b01d
jmp 0x5b070
jmp 0x5b01f
movq -0x80(%rbp), %rdi
leaq -0x3e(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5b042
movb $0x0, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x5b0a6
movq -0x80(%rbp), %rax
movq %rax, %rdi
addq $0x80, %rdi
movl 0x7c(%rax), %esi
callq 0x5e1a0
movq -0x80(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x5b0a6
movq -0x80(%rbp), %rdi
leaq -0x3d(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5b093
movb $0x0, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x5b0a6
movq -0x80(%rbp), %rax
movb $0x1, 0x78(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xade8
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0xacc0
nopw %cs:(%rax,%rax)