// File: /workspace/github_cpp_makefile_star2_O3/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)
movq $0x0, -0x30(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x351f(%rip), %rdx      # 0x71b4
leaq -0x88(%rbp), %rdi
movq %rdx, %rsi
callq 0x60ac
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x3d62
xorl %r12d, %r12d
leaq -0x88(%rbp), %r15
xorl %r13d, %r13d
movq (%r14), %rdx
movzbl (%rdx,%r12), %ecx
cmpl $0x20, %ecx
je 0x3d14
cmpl $0x22, %ecx
je 0x3cfa
cmpl $0x5c, %ecx
jne 0x3d1a
decq %rax
cmpq %r12, %rax
jbe 0x3d1a
leal 0x1(%r12), %ebx
cmpb $0x22, (%rdx,%rbx)
jne 0x3d1a
movq %r15, %rdi
movl $0x22, %esi
callq 0x3270
movl %ebx, %r12d
jmp 0x3d52
movl %r13d, %ebx
xorb $0x1, %bl
testb $0x1, %r13b
je 0x3d4f
leaq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x55d2
jmp 0x3d3d
testb $0x1, %r13b
je 0x3d27
movsbl %cl, %esi
movq %r15, %rdi
callq 0x3270
jmp 0x3d52
cmpq $0x0, -0x80(%rbp)
je 0x3d52
leaq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x55d2
movl %r13d, %ebx
movq $0x0, -0x80(%rbp)
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
movl %ebx, %r13d
incl %r12d
movq 0x8(%r14), %rax
cmpq %r12, %rax
ja 0x3cbe
cmpq $0x0, -0x80(%rbp)
je 0x3d8b
leaq -0x40(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x55d2
movq $0x0, -0x80(%rbp)
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
cmpb $0x0, 0x65b6(%rip)      # 0xa348
je 0x3fab
movq 0x6251(%rip), %rdi      # 0x9ff0
leaq 0x338a(%rip), %rsi      # 0x7130
movl $0x1b, %edx
callq 0x3330
movq 0x8(%r14), %rsi
movq 0x6235(%rip), %rdi      # 0x9ff0
callq 0x31f0
movq %rax, %rbx
leaq 0x3382(%rip), %rsi      # 0x714c
movl $0x10, %edx
movq %rax, %rdi
callq 0x3330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movq 0x61eb(%rip), %rdi      # 0x9ff0
leaq 0x3351(%rip), %rsi      # 0x715d
movl $0x9, %edx
callq 0x3330
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x61cc(%rip), %rdi      # 0x9ff0
callq 0x3330
movq %rax, %rbx
leaq 0x3325(%rip), %rsi      # 0x7158
movl $0x4, %edx
movq %rax, %rdi
callq 0x3330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movq 0x6182(%rip), %rdi      # 0x9ff0
leaq 0x32f2(%rip), %rsi      # 0x7167
movl $0x1a, %edx
callq 0x3330
movq -0x38(%rbp), %rsi
subq -0x40(%rbp), %rsi
sarq $0x5, %rsi
movq 0x615e(%rip), %rdi      # 0x9ff0
callq 0x31f0
movq %rax, %rbx
leaq 0x32e1(%rip), %rsi      # 0x7182
movl $0xc, %edx
movq %rax, %rdi
callq 0x3330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movl $0x1, %r12d
movq 0x610e(%rip), %r14      # 0x9ff0
leaq 0x3274(%rip), %r13      # 0x715d
leaq 0x3268(%rip), %rbx      # 0x7158
leal -0x1(%r12), %r15d
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x3faf
movl $0x12, %edx
movq %r14, %rdi
leaq 0x3273(%rip), %rsi      # 0x718f
callq 0x3330
movl %r12d, %esi
movq %r14, %rdi
callq 0x31f0
movl $0x6, %edx
movq %rax, %rdi
leaq 0x3267(%rip), %rsi      # 0x71a2
callq 0x3330
movl $0x9, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x3330
movq -0x40(%rbp), %rax
shlq $0x5, %r15
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq %r14, %rdi
callq 0x3330
movq %rax, %r15
movl $0x4, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3330
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32f0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
incl %r12d
movq %rax, %rdi
callq 0x3200
jmp 0x3ef0
movq -0x40(%rbp), %rdi
leaq 0x3231(%rip), %rsi      # 0x71e7
callq 0x30c0
testl %eax, %eax
je 0x402a
cmpq $0x0, 0x6391(%rip)      # 0xa358
je 0x413a
xorl %edi, %edi
callq 0x32a0
movl %eax, %edi
callq 0x32e0
callq 0x30a0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x24, %rcx
addl %edx, %ecx
shll $0x3, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x2ffb(%rip), %xmm0     # 0x7008
addsd 0x2ffb(%rip), %xmm0     # 0x7010
cvttsd2si %xmm0, %rdi
movl $0x3, %r14d
callq 0x3220
jmp 0x440f
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rax, %rcx
movl $0x1, %r14d
cmpq $0x21, %rcx
jb 0x440f
leaq -0x58(%rbp), %r14
movq %r14, -0x10(%r14)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x58de
leaq -0x98(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x62dd(%rip), %rsi      # 0xa350
movq 0x62de(%rip), %rdx      # 0xa358
addq %rsi, %rdx
leaq -0xa8(%rbp), %rdi
callq 0x58de
leaq -0x68(%rbp), %rbx
movq -0xa0(%rbp), %rcx
cmpq %rcx, -0x60(%rbp)
jae 0x40a9
movq %rbx, %rdi
movl $0x20, %esi
callq 0x3270
jmp 0x408d
cmpq -0x60(%rbp), %rcx
jae 0x40d0
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
movl $0x20, %esi
callq 0x3270
movq -0xa0(%rbp), %rcx
cmpq -0x60(%rbp), %rcx
jb 0x40b6
testq %rcx, %rcx
je 0x4103
movq -0xa8(%rbp), %rax
movb $0x1, %dl
movq -0x68(%rbp), %rdi
xorl %esi, %esi
movb (%rax,%rsi), %r8b
cmpb (%rdi,%rsi), %r8b
sete %r8b
andb %r8b, %dl
incq %rsi
cmpq %rsi, %rcx
jne 0x40e4
testb %dl, %dl
je 0x4267
leaq -0x40(%rbp), %rdi
movq (%rdi), %rsi
callq 0x6358
leaq -0x40(%rbp), %rdi
movq (%rdi), %rsi
callq 0x6358
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x412c
callq 0x32c0
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x413a
callq 0x32c0
movq -0x40(%rbp), %rdi
movl $0x1, %r14d
cmpq -0x38(%rbp), %rdi
je 0x440f
leaq 0x3054(%rip), %rsi      # 0x71a9
callq 0x30c0
testl %eax, %eax
je 0x423d
movq -0x40(%rbp), %rdi
leaq 0x3040(%rip), %rsi      # 0x71ad
callq 0x30c0
testl %eax, %eax
je 0x428b
movq -0x40(%rbp), %rdi
leaq 0x3030(%rip), %rsi      # 0x71b5
callq 0x30c0
testl %eax, %eax
je 0x4320
movq -0x40(%rbp), %rdi
leaq 0x301e(%rip), %rsi      # 0x71bb
callq 0x30c0
testl %eax, %eax
je 0x4375
movq -0x40(%rbp), %rdi
leaq 0x300b(%rip), %rsi      # 0x71c0
callq 0x30c0
testl %eax, %eax
je 0x425f
movq -0x40(%rbp), %rdi
leaq 0x2ff8(%rip), %rsi      # 0x71c5
callq 0x30c0
testl %eax, %eax
je 0x4398
movq -0x40(%rbp), %rdi
leaq 0x2feb(%rip), %rsi      # 0x71d0
callq 0x30c0
testl %eax, %eax
je 0x43a2
movq -0x40(%rbp), %rdi
leaq 0x2fe0(%rip), %rsi      # 0x71dd
callq 0x30c0
testl %eax, %eax
je 0x43eb
movq -0x40(%rbp), %rdi
leaq 0x2fcf(%rip), %rsi      # 0x71e4
callq 0x30c0
testl %eax, %eax
jne 0x440f
leaq 0x6127(%rip), %rdi      # 0xa350
leaq 0x2f84(%rip), %rsi      # 0x71b4
callq 0x33b0
xorl %r14d, %r14d
jmp 0x440f
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x41, %rax
jb 0x440f
leaq 0x20(%rsi), %rdi
addq $0x40, %rsi
callq 0x3b04
xorl %r14d, %r14d
jmp 0x440f
cmpq %r15, %rax
je 0x4278
movq %rax, %rdi
callq 0x32c0
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x3fbf
callq 0x32c0
jmp 0x3fbf
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x440f
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
addq $0x20, %rdi
leaq -0x68(%rbp), %rsi
callq 0x3b70
testb %al, %al
je 0x4309
leaq 0x2fe4(%rip), %rsi      # 0x72b0
movl $0x1, %edx
movl -0x44(%rbp), %edi
xorl %ecx, %ecx
callq 0x3190
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x44(%rbp), %edi
xorl %ecx, %ecx
callq 0x3190
movl $0x2, %r14d
leaq 0x2eb7(%rip), %rsi      # 0x71b1
movl $0x3, %edx
movl -0x44(%rbp), %edi
xorl %ecx, %ecx
callq 0x3190
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x440f
callq 0x32c0
jmp 0x440f
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x440f
addq $0x20, %rdi
callq 0x3c11
leaq 0x5f43(%rip), %rcx      # 0xa288
leaq 0x5f45(%rip), %rbx      # 0xa291
testb %al, %al
cmovneq %rcx, %rbx
movq %rbx, %rdi
callq 0x3150
movl $0x2, %r14d
movl -0x44(%rbp), %edi
movq %rbx, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3190
jmp 0x440f
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x440f
addq $0x20, %rdi
callq 0x3bd0
jmp 0x425f
callq 0x44c1
xorl %r14d, %r14d
jmp 0x440f
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x440f
addq $0x20, %rdi
leaq 0x2e18(%rip), %rsi      # 0x71d6
callq 0x30c0
testl %eax, %eax
je 0x4442
movq -0x40(%rbp), %rdi
addq $0x20, %rdi
leaq 0x2e03(%rip), %rsi      # 0x71d9
callq 0x30c0
testl %eax, %eax
jne 0x440f
movb $0x0, 0x5f62(%rip)      # 0xa348
jmp 0x425f
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x21, %rax
jb 0x440f
addq $0x20, %rsi
leaq 0x5f49(%rip), %rdi      # 0xa350
callq 0x31b0
xorl %r14d, %r14d
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x4424
callq 0x32c0
leaq -0x40(%rbp), %rdi
callq 0x561e
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x5eff(%rip)      # 0xa348
jmp 0x425f
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
jne 0x448d
jmp 0x449b
jmp 0x4470
jmp 0x4470
movq %rax, %rbx
jmp 0x4484
jmp 0x4498
jmp 0x4498
movq %rax, %rbx
jmp 0x44b0
jmp 0x4470
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x4484
callq 0x32c0
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x449b
callq 0x32c0
jmp 0x449b
jmp 0x4498
jmp 0x4498
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x44b0
callq 0x32c0
leaq -0x40(%rbp), %rdi
callq 0x561e
movq %rbx, %rdi
callq 0x34a0