// File: /workspace/github_cpp_makefile_star2_O3/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 0x1a908
movl $0x1, %edi
callq 0x5380
movq %rax, %rbx
movq %rax, %rdi
callq 0x17504
leaq 0xa0(%rsp), %r14
leaq 0x80(%rsp), %r15
leaq 0x1fd2(%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 0x1a5e6
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %r14, %rdi
callq 0x51c0
leaq (%rsp,%rax), %rdx
addq $0xa0, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x736c
movq 0xc2db(%rip), %rsi      # 0x26918
movq %rbx, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x17506
decl %eax
cmpl $0x3, %eax
ja 0x1a7d2
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x1f99(%rip), %rsi      # 0x1c60a
leaq 0x1fa4(%rip), %rdx      # 0x1c61c
callq 0x736c
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x6924
movl %ebp, %edi
leaq 0x60(%rsp), %rsi
callq 0x1a9d7
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x1a6c5
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %edi
callq 0x1aa11
jmp 0x1a7b7
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x1f39(%rip), %rsi      # 0x1c61d
leaq 0x1f43(%rip), %rdx      # 0x1c62e
callq 0x736c
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 0x1a9d7
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x1a738
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %edi
callq 0x1aa11
jmp 0x1a7b7
xorl %r13d, %r13d
jmp 0x1a7d5
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x1ed3(%rip), %rsi      # 0x1c62f
leaq 0x1eda(%rip), %rdx      # 0x1c63d
callq 0x736c
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 0x1a9d7
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1a7b0
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %edi
callq 0x1aa11
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a7d2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x5390
movb $0x1, %r13b
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x1a7fa
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5390
testb %r13b, %r13b
jne 0x1a5e6
movl %ebp, %edi
callq 0x1aa89
movl %ebp, %edi
callq 0x5710
movq %rbx, %rdi
callq 0x175c6
movl $0x1, %esi
movq %rbx, %rdi
callq 0x5390
leaq 0x1f7c(%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 0x1a8fb
movq %rax, %r14
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x1a8b6
movq 0x50(%rsp), %rsi
jmp 0x1a8a3
jmp 0x1a8d8
movq %rax, %r14
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x1a8b6
movq 0x70(%rsp), %rsi
jmp 0x1a8a3
jmp 0x1a8d8
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1a8b6
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x1a8b6
jmp 0x1a8d8
jmp 0x1a8b3
jmp 0x1a8b3
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a8db
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x1a8db
movq %rax, %r14
jmp 0x1a900
movq %rax, %r14
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x1a900
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5390
movq %r14, %rdi
callq 0x5670