// File: /workspace/github_cpp_makefile_star2_O2/Lartu[P]coraldb/source/execute.cpp
// Function: execute_command(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, unsigned int)
int execute_command(std::string &command, unsigned int socket_fd)
{
    std::vector<std::string> tokens;
    std::string current_token = "";
    bool string_open = false;
    for (unsigned int i = 0; i < command.length(); ++i)
    {
        if (command[i] == '\\' && i < command.length() - 1 && command[i + 1] == '"')
        {
            current_token += command[i + 1];
            ++i;
        }
        else if (command[i] == '"')
        {
            string_open = !string_open;
            if (!string_open)
            {
                tokens.push_back(current_token);
                current_token.clear();
            }
        }
        else if (command[i] == ' ' && !string_open)
        {
            if (!current_token.empty())
            {
                tokens.push_back(current_token);
                current_token.clear();
            }
        }
        else
        {
            current_token += command[i];
        }
    }
    if (!current_token.empty())
    {
        tokens.push_back(current_token);
        current_token.clear();
    }

    if (display_tokens)
    {
        std::cerr << "\x1B[97;42m Message Received: " << command.length() << " bytes + \\n \033[0m" << std::endl;
        std::cerr << "\x1B[97;100m" << command << "\033[0m" << std::endl;
        std::cerr << "\x1B[33mThe message contains " << tokens.size() << " tokens.\033[0m" << std::endl;
        for (unsigned int i = 0; i < tokens.size(); ++i)
        {
            std::cerr << "\x1B[32mToken Number " << (i + 1) << ":\033[0m ";
            std::cerr << "\x1B[97;100m" << tokens[i] << "\033[0m" << std::endl;
        }
    }

    // Check for authentication key
    bool authenticated = false;
    if (tokens[0] == "KEY")
    {
        if (tokens.size() < 2)
            return 1;
        std::string received_key = tokens[1];
        std::string comparing_key = database_key;
        bool is_key_equal = true;
        // Fill buffers for full-buffer comparison
        while (received_key.length() < comparing_key.length())
        {
            received_key += ' ';
        }
        while (comparing_key.length() < received_key.length())
        {
            comparing_key += ' ';
        }
        // Compare buffers
        for (size_t i = 0; i < comparing_key.length(); ++i)
        {
            if (comparing_key[i] != received_key[i])
            {
                is_key_equal = false;
            }
        }
        if (is_key_equal)
        {
            authenticated = true;
            tokens.erase(tokens.begin()); // Remove KEY command
            tokens.erase(tokens.begin()); // Remove key
        }
    }

    // If not authenticated, throw error
    if (!authenticated && !database_key.empty())
    {
        srand(time(NULL));
        sleep((rand() % (MAX_WAIT * 10)) * 0.1 + MIN_WAIT); // Wait a random number of seconds
        return 3;
    }

    // If no command was sent, throw error
    if (tokens.empty())
    {
        return 1;
    }

    // Execute command
    if (tokens[0] == "SET")
    {
        if (tokens.size() < 3)
            return 1;
        else
            set_key(tokens[1], tokens[2]);
        return 0;
    }
    else if (tokens[0] == "GET")
    {
        if (tokens.size() < 2)
            return 1;
        std::string value;
        if (read_key(tokens[1], value))
        {
            send(socket_fd, "\"", 1, 0);
            send(socket_fd, value.c_str(), value.length(), 0);
            send(socket_fd, "\"\r\n", 3, 0);
            return 2;
        }
        else
        {
            return 1;
        }
    }
    else if (tokens[0] == "PROBE")
    {
        if (tokens.size() < 2)
            return 1;
        if (probe_key(tokens[1]))
        {
            send(socket_fd, FOUND_MSG, strlen(FOUND_MSG), 0);
        }
        else
        {
            send(socket_fd, NOT_FOUND_MSG, strlen(NOT_FOUND_MSG), 0);
        }
        return 2;
    }
    else if (tokens[0] == "DROP")
    {
        if (tokens.size() < 2)
            return 1;
        drop_key(tokens[1]);
        return 0;
    }
    else if (tokens[0] == "PING")
    {
        return 0;
    }
    else if (tokens[0] == "CHECKPOINT")
    {
        save_datafile();
        return 0;
    }
    else if (tokens[0] == "DEBUG")
    {
        if (tokens.size() < 2)
            return 1;
        if (tokens[1] == "ON")
        {
            display_tokens = true;
            return 0;
        }
        else if (tokens[1] == "OFF")
        {
            display_tokens = false;
            return 0;
        }
        else
        {
            return 1;
        }
    }
    else if (tokens[0] == "SETKEY")
    {
        if (tokens.size() < 2)
            return 1;
        database_key = tokens[1];
        return 0;
    }
    else if (tokens[0] == "DELKEY")
    {
        database_key = "";
        return 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %esi, -0x44(%rbp)
movq %rdi, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
leaq 0x27f0(%rip), %rsi      # 0x61b4
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x3120
xorl %r13d, %r13d
leaq -0x88(%rbp), %r12
xorl %ebx, %ebx
movl %ebx, %ecx
movq 0x8(%r14), %rsi
cmpq %rcx, %rsi
jbe 0x3a85
movq (%r14), %rdx
movzbl (%rdx,%rcx), %eax
cmpl $0x20, %eax
je 0x3a43
cmpl $0x22, %eax
je 0x3a28
cmpl $0x5c, %eax
jne 0x3a49
decq %rsi
cmpq %rcx, %rsi
jbe 0x3a49
leal 0x1(%rbx), %r15d
cmpb $0x22, (%rdx,%r15)
jne 0x3a49
movq %r12, %rdi
pushq $0x22
popq %rsi
callq 0x3250
movl %r15d, %ebx
jmp 0x3a7e
movl %r13d, %r15d
xorb $0x1, %r15b
testb $0x1, %r13b
je 0x3a7b
leaq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x4bfa
jmp 0x3a6c
testb $0x1, %r13b
je 0x3a56
movsbl %al, %esi
movq %r12, %rdi
callq 0x3250
jmp 0x3a7e
cmpq $0x0, -0x80(%rbp)
je 0x3a7e
leaq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x4bfa
movl %r13d, %r15d
andq $0x0, -0x80(%rbp)
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
movl %r15d, %r13d
incl %ebx
jmp 0x39e0
cmpq $0x0, -0x80(%rbp)
je 0x3aab
leaq -0x40(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x4bfa
andq $0x0, -0x80(%rbp)
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
cmpb $0x0, 0x5866(%rip)      # 0x9318
je 0x3bf2
movq 0x5531(%rip), %rdi      # 0x8ff0
leaq 0x266a(%rip), %rsi      # 0x6130
callq 0x32e0
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0x3200
leaq 0x266e(%rip), %rsi      # 0x614c
movq %rax, %rdi
callq 0x32e0
movq %rax, %rdi
callq 0x3180
movq 0x54fb(%rip), %rdi      # 0x8ff0
leaq 0x2661(%rip), %rsi      # 0x615d
callq 0x32e0
movq %rax, %rdi
movq %r14, %rsi
callq 0x3270
leaq 0x2645(%rip), %rsi      # 0x6158
movq %rax, %rdi
callq 0x32e0
movq %rax, %rdi
callq 0x3180
movq 0x54c6(%rip), %rdi      # 0x8ff0
leaq 0x2636(%rip), %rsi      # 0x6167
callq 0x32e0
movq -0x38(%rbp), %rsi
subq -0x40(%rbp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x3200
leaq 0x2631(%rip), %rsi      # 0x6182
movq %rax, %rdi
callq 0x32e0
movq %rax, %rdi
callq 0x3180
pushq $0x1
popq %r12
movq 0x5484(%rip), %r14      # 0x8ff0
leaq 0x25ea(%rip), %r13      # 0x615d
leaq 0x25de(%rip), %rbx      # 0x6158
leal -0x1(%r12), %r15d
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x3bf6
movq %r14, %rdi
leaq 0x25f2(%rip), %rsi      # 0x618f
callq 0x32e0
movl %r12d, %esi
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
leaq 0x25eb(%rip), %rsi      # 0x61a2
callq 0x32e0
movq %r14, %rdi
movq %r13, %rsi
callq 0x32e0
shlq $0x5, %r15
addq -0x40(%rbp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x3270
movq %rax, %rdi
movq %rbx, %rsi
callq 0x32e0
incl %r12d
movq %rax, %rdi
callq 0x3180
jmp 0x3b7a
movq -0x40(%rbp), %rdi
pushq $0x1
popq %r14
leaq 0x25e6(%rip), %rsi      # 0x61e7
callq 0x4c35
testb %al, %al
je 0x3cfb
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x21, %rax
jb 0x3ef7
addq $0x20, %rsi
leaq -0x68(%rbp), %rdi
callq 0x33a0
leaq 0x56e9(%rip), %rsi      # 0x9320
leaq -0xa8(%rbp), %rdi
callq 0x33a0
leaq -0x68(%rbp), %rbx
pushq $0x20
popq %r15
movq -0x60(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x3c65
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3250
jmp 0x3c4b
leaq -0xa8(%rbp), %rbx
pushq $0x20
popq %r15
movq -0xa0(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x3c8a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3250
jmp 0x3c70
movq -0xa8(%rbp), %rdx
movb $0x1, %cl
movq -0x68(%rbp), %rsi
xorl %edi, %edi
cmpq %rdi, %rax
je 0x3cb2
movb (%rdx,%rdi), %r8b
cmpb (%rsi,%rdi), %r8b
sete %r8b
andb %r8b, %cl
incq %rdi
jmp 0x3c99
testb $0x1, %cl
je 0x3ce6
leaq -0x40(%rbp), %rdi
movq (%rdi), %rsi
callq 0x4c46
leaq -0x40(%rbp), %rdi
movq (%rdi), %rsi
callq 0x4c46
leaq -0xa8(%rbp), %rdi
callq 0x3498
leaq -0x68(%rbp), %rdi
callq 0x3498
jmp 0x3d45
leaq -0xa8(%rbp), %rdi
callq 0x3498
leaq -0x68(%rbp), %rdi
callq 0x3498
cmpq $0x0, 0x5625(%rip)      # 0x9328
je 0x3d45
xorl %edi, %edi
callq 0x3290
movl %eax, %edi
callq 0x32d0
callq 0x30a0
pushq $0x28
popq %rcx
cltd
idivl %ecx
cvtsi2sd %edx, %xmm0
mulsd 0x22de(%rip), %xmm0     # 0x6008
addsd 0x22de(%rip), %xmm0     # 0x6010
cvttsd2si %xmm0, %rdi
callq 0x3220
pushq $0x3
popq %r14
jmp 0x3ef7
movq -0x40(%rbp), %rdi
cmpq -0x38(%rbp), %rdi
je 0x3ef7
leaq 0x244f(%rip), %rsi      # 0x61a9
callq 0x4c35
testb %al, %al
je 0x3d8d
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x41, %rax
jb 0x3ef7
leaq 0x20(%rsi), %rdi
addq $0x40, %rsi
callq 0x386e
xorl %r14d, %r14d
jmp 0x3ef7
movq -0x40(%rbp), %rdi
leaq 0x2415(%rip), %rsi      # 0x61ad
callq 0x4c35
testb %al, %al
je 0x3e1c
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x3ef7
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
addq $0x20, %rdi
leaq -0x68(%rbp), %rsi
callq 0x38cf
movl -0x44(%rbp), %ebx
testb %al, %al
je 0x3eea
leaq 0x24cb(%rip), %rsi      # 0x62b0
pushq $0x1
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x31a0
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x31a0
leaq 0x23a8(%rip), %rsi      # 0x61b1
pushq $0x3
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x31a0
pushq $0x2
jmp 0x3eec
movq -0x40(%rbp), %rdi
leaq 0x238e(%rip), %rsi      # 0x61b5
callq 0x4c35
testb %al, %al
je 0x3e81
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x3ef7
addq $0x20, %rdi
callq 0x3958
leaq 0x5403(%rip), %rcx      # 0x9258
leaq 0x5405(%rip), %rbx      # 0x9261
testb %al, %al
cmovneq %rcx, %rbx
movq %rbx, %rdi
callq 0x3140
movl -0x44(%rbp), %edi
movq %rbx, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x31a0
pushq $0x2
jmp 0x3d3e
movq -0x40(%rbp), %rdi
leaq 0x232f(%rip), %rsi      # 0x61bb
callq 0x4c35
testb %al, %al
je 0x3eb4
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x3ef7
addq $0x20, %rdi
callq 0x3924
jmp 0x3d85
movq -0x40(%rbp), %rdi
leaq 0x2301(%rip), %rsi      # 0x61c0
callq 0x4c35
testb %al, %al
jne 0x3d85
movq -0x40(%rbp), %rdi
leaq 0x22ee(%rip), %rsi      # 0x61c5
callq 0x4c35
testb %al, %al
je 0x3f21
callq 0x405a
xorl %r14d, %r14d
jmp 0x3ef7
pushq $0x1
popq %r14
leaq -0x68(%rbp), %rdi
callq 0x3498
leaq -0x88(%rbp), %rdi
callq 0x3498
leaq -0x40(%rbp), %rdi
callq 0x4c50
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rdi
leaq 0x22a4(%rip), %rsi      # 0x61d0
callq 0x4c35
testb %al, %al
je 0x3f6a
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x3ef7
addq $0x20, %rdi
leaq 0x2285(%rip), %rsi      # 0x61d6
callq 0x4c35
testb %al, %al
je 0x3fde
movb $0x1, 0x53b3(%rip)      # 0x9318
jmp 0x3d85
movq -0x40(%rbp), %rdi
leaq 0x2268(%rip), %rsi      # 0x61dd
callq 0x4c35
testb %al, %al
je 0x3fab
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x21, %rax
jb 0x3ef7
addq $0x20, %rsi
leaq 0x5382(%rip), %rdi      # 0x9320
callq 0x31c0
xorl %r14d, %r14d
jmp 0x3ef7
movq -0x40(%rbp), %rdi
leaq 0x222e(%rip), %rsi      # 0x61e4
callq 0x4c35
testb %al, %al
je 0x3ef7
leaq 0x5356(%rip), %rdi      # 0x9320
leaq 0x21e3(%rip), %rsi      # 0x61b4
callq 0x3470
xorl %r14d, %r14d
jmp 0x3ef7
movq -0x40(%rbp), %rdi
addq $0x20, %rdi
leaq 0x21ec(%rip), %rsi      # 0x61d9
callq 0x4c35
testb %al, %al
je 0x3ef7
movb $0x0, 0x5317(%rip)      # 0x9318
jmp 0x3d85
jmp 0x400c
jmp 0x401e
jmp 0x401e
movq %rax, %rbx
jmp 0x402d
jmp 0x403a
jmp 0x403a
movq %rax, %rbx
jmp 0x4049
jmp 0x403a
jmp 0x401e
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x3498
leaq -0x68(%rbp), %rdi
callq 0x3498
jmp 0x403d
jmp 0x403a
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x3498
leaq -0x40(%rbp), %rdi
callq 0x4c50
movq %rbx, %rdi
callq 0x3450