// File: /workspace/github_cpp_makefile_star2_O2/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 $0x10b8, %rsp           # imm = 0x10B8
movl (%rdi), %ebp
callq 0x5490
movl %ebp, %edi
callq 0x166cc
pushq $0x1
popq %rdi
callq 0x53c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x1451c
leaq 0xb0(%rsp), %r14
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %r13
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x5270
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rsi
callq 0x5580
testl %eax, %eax
jle 0x164d8
movl %eax, %edi
leaq 0x2107(%rip), %rsi      # 0x1860a
callq 0x1647b
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x6ac6
movq 0xf42b(%rip), %rsi      # 0x25948
movq %rbx, %rdi
movq %r12, %rdx
movl %ebp, %ecx
callq 0x1451e
decl %eax
cmpl $0x3, %eax
ja 0x1660b
leaq 0x207c(%rip), %rcx      # 0x185b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r13, %rdi
leaq 0x20c6(%rip), %rsi      # 0x18615
leaq 0xf(%rsp), %rdx
callq 0x6ac6
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5540
movl %ebp, %edi
movq %r15, %rsi
callq 0x1677c
movq %r15, %rdi
callq 0x51d0
movl %ebp, %edi
callq 0x167ab
jmp 0x16603
movq %r13, %rdi
leaq 0x2097(%rip), %rsi      # 0x18628
leaq 0xf(%rsp), %rdx
callq 0x6ac6
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5540
movl %ebp, %edi
movq %r15, %rsi
callq 0x1677c
movq %r15, %rdi
callq 0x51d0
movl %ebp, %edi
callq 0x167ab
jmp 0x16603
movq %r13, %rdi
leaq 0x206a(%rip), %rsi      # 0x1863a
leaq 0xf(%rsp), %rdx
callq 0x6ac6
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5540
movl %ebp, %edi
movq %r15, %rsi
callq 0x1677c
movq %r15, %rdi
callq 0x51d0
movl %ebp, %edi
callq 0x167ab
movq %r13, %rdi
callq 0x51d0
movq %r12, %rdi
callq 0x51d0
jmp 0x164d8
leaq 0x30(%rsp), %rdi
callq 0x51d0
movl %ebp, %edi
callq 0x167fe
movl %ebp, %edi
callq 0x5770
movq %rbx, %rdi
callq 0x14596
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x53d0
leaq 0x216a(%rip), %rdi      # 0x187b4
callq 0x5600
xorl %eax, %eax
addq $0x10b8, %rsp           # imm = 0x10B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x53d0
jmp 0x166c4
movq %rax, %r14
leaq 0x50(%rsp), %rdi
jmp 0x16696
movq %rax, %r14
leaq 0x70(%rsp), %rdi
jmp 0x16696
jmp 0x166b2
jmp 0x166b2
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x51d0
jmp 0x166a6
jmp 0x166b2
jmp 0x166a3
jmp 0x166a3
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x51d0
jmp 0x166b5
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x51d0
jmp 0x166c4
movq %rax, %r14
movq %r14, %rdi
callq 0x56c0