// File: /workspace/github_cpp_makefile_star2_O1/prakhar2808[P]DBEngine/serverCode/server.cc
// Function: handle_connection(void*)
void* handle_connection(void* pclient_socket) {
  int client_socket = *((int*)pclient_socket);
  free(pclient_socket);
  char buffer[BUFSIZE];
  int bytes_read;
  // Sending welcome message.
  sendWelcomeMessage(client_socket);
  // Getting a rpel object for the client.
  rpel* rpelObjRef = new rpel();
  while(true) {
    memset(buffer, 0, sizeof(buffer));
    // Reading the message.
    if((bytes_read = read(client_socket,
                          buffer,
                          sizeof(buffer))) > 0) {
      check(bytes_read, "recv error");

      // Send to DB engine.
      std::string req(buffer);
      opStatus response = rpelObjRef->execute(dbObjectRef, req, client_socket);
      
      if(response == opStatus::opExit) {
        break;
      }

      if(response == opStatus::opInvalid) {
        std::string responseStr = "Invalid operation!";
        sendToClient(client_socket, responseStr);
        sendEndMsgToClient(client_socket);
      }

      if(response == opStatus::opFail) {
        std::string responseStr = "Operation failed!";
        sendToClient(client_socket, responseStr);
        sendEndMsgToClient(client_socket);
      }

      if(response == opStatus::opKeyNotFound) {
        std::string responseStr = "Key not found!";
        sendToClient(client_socket, responseStr);
        sendEndMsgToClient(client_socket);
      }
    }
  }

  sendGoodByeMessage(client_socket);
  close(client_socket);
  delete rpelObjRef;
  printf("Closing connection\n");
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10a8, %rsp           # imm = 0x10A8
movl (%rdi), %ebp
callq 0x5460
movl %ebp, %edi
callq 0x1a93b
movl $0x1, %edi
callq 0x5380
movq %rax, %rbx
movq %rax, %rdi
callq 0x174b8
leaq 0xa0(%rsp), %r14
leaq 0x60(%rsp), %r15
leaq 0x1fe4(%rip), %r12      # 0x1c5b8
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x5230
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rsi
callq 0x5530
testl %eax, %eax
jle 0x1a5d4
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq %r14, %rdi
callq 0x51c0
leaq (%rsp,%rax), %rdx
addq $0xa0, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x7326
movq 0xc2f3(%rip), %rsi      # 0x26918
movq %rbx, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x174ba
movl %eax, %r13d
leal -0x1(%r13), %eax
cmpl $0x3, %eax
ja 0x1a811
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x1fac(%rip), %rsi      # 0x1c60a
leaq 0x1fb7(%rip), %rdx      # 0x1c61c
callq 0x7326
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x6924
movl %ebp, %edi
leaq 0x40(%rsp), %rsi
callq 0x1aa0a
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x1a6b2
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %edi
callq 0x1aa44
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a6d4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x5390
cmpl $0x2, %r13d
je 0x1a77b
cmpl $0x3, %r13d
jne 0x1a811
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x1f22(%rip), %rsi      # 0x1c61d
leaq 0x1f2c(%rip), %rdx      # 0x1c62e
callq 0x7326
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x6924
movl %ebp, %edi
leaq 0x20(%rsp), %rsi
callq 0x1aa0a
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1a74f
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %edi
callq 0x1aa44
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a771
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x5390
cmpl $0x2, %r13d
jne 0x1a811
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x1ea1(%rip), %rsi      # 0x1c62f
leaq 0x1ea8(%rip), %rdx      # 0x1c63d
callq 0x7326
leaq 0x90(%rsp), %r13
movq %r13, 0x80(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %rdi
callq 0x6924
movl %ebp, %edi
leaq 0x80(%rsp), %rsi
callq 0x1aa0a
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x1a7ef
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %edi
callq 0x1aa44
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a811
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x5390
movb $0x1, %r13b
jmp 0x1a819
xorl %r13d, %r13d
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x1a835
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5390
testb %r13b, %r13b
jne 0x1a5d4
movl %ebp, %edi
callq 0x1aabc
movl %ebp, %edi
callq 0x5710
movq %rbx, %rdi
callq 0x1757a
movl $0x1, %esi
movq %rbx, %rdi
callq 0x5390
leaq 0x1f41(%rip), %rdi      # 0x1c7a9
callq 0x55a0
xorl %eax, %eax
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x1, %esi
movq %rbx, %rdi
jmp 0x1a92e
movq %rax, %r14
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x1a8f2
movq 0x50(%rsp), %rsi
jmp 0x1a8e3
jmp 0x1a914
jmp 0x1a914
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1a8f2
movq 0x30(%rsp), %rsi
jmp 0x1a8e3
jmp 0x1a914
jmp 0x1a8ef
movq %rax, %r14
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x1a8f2
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x1a8f2
jmp 0x1a8ef
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a917
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x1a917
movq %rax, %r14
jmp 0x1a933
movq %rax, %r14
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x1a933
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5390
movq %r14, %rdi
callq 0x5670