// File: 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
movq %rsp, %rbp
subq $0x1190, %rsp           # imm = 0x1190
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6610
movl -0xc(%rbp), %edi
callq 0x3e2c0
movl $0x1, %edi
callq 0x64f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x1140(%rbp)
callq 0x38230
jmp 0x3ddd7
movq -0x1140(%rbp), %rax
movq %rax, -0x1020(%rbp)
leaq -0x1010(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62d0
movl -0xc(%rbp), %edi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6730
movl %eax, -0x1014(%rbp)
cmpl $0x0, %eax
jle 0x3e24d
movl -0x1014(%rbp), %edi
leaq 0x3857(%rip), %rsi      # 0x4167f
callq 0x3dd50
leaq -0x1051(%rbp), %rdi
movq %rdi, -0x1148(%rbp)
callq 0x68d0
movq -0x1148(%rbp), %rdx
leaq -0x1050(%rbp), %rdi
leaq -0x1010(%rbp), %rsi
callq 0x9970
jmp 0x3de5c
leaq -0x1051(%rbp), %rdi
callq 0x6560
movq -0x1020(%rbp), %rdi
movq 0x21bf2(%rip), %rsi     # 0x5fa68
movl -0xc(%rbp), %ecx
leaq -0x1050(%rbp), %rdx
callq 0x38240
movl %eax, -0x114c(%rbp)
jmp 0x3de8d
movl -0x114c(%rbp), %eax
movl %eax, -0x1058(%rbp)
cmpl $0x4, -0x1058(%rbp)
jne 0x3df13
movl $0x3, -0x105c(%rbp)
jmp 0x3e223
movq -0x1140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
movl $0x1, %esi
callq 0x6500
jmp 0x3e2ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x1051(%rbp), %rdi
callq 0x6560
jmp 0x3e2ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
jmp 0x3e23f
cmpl $0x1, -0x1058(%rbp)
jne 0x3e017
leaq -0x1081(%rbp), %rdi
movq %rdi, -0x1158(%rbp)
callq 0x68d0
movq -0x1158(%rbp), %rdx
leaq 0x3749(%rip), %rsi      # 0x4168a
leaq -0x1080(%rbp), %rdi
callq 0x9970
jmp 0x3df4f
leaq -0x1081(%rbp), %rdi
callq 0x6560
movl -0xc(%rbp), %eax
movl %eax, -0x115c(%rbp)
leaq -0x10a8(%rbp), %rdi
leaq -0x1080(%rbp), %rsi
callq 0x6210
jmp 0x3df79
movl -0x115c(%rbp), %edi
leaq -0x10a8(%rbp), %rsi
callq 0x3e3a0
jmp 0x3df8d
leaq -0x10a8(%rbp), %rdi
callq 0x6340
movl -0xc(%rbp), %edi
callq 0x3e410
jmp 0x3dfa3
leaq -0x1080(%rbp), %rdi
callq 0x6340
jmp 0x3e017
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x1081(%rbp), %rdi
callq 0x6560
jmp 0x3e23f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
jmp 0x3e006
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x10a8(%rbp), %rdi
callq 0x6340
leaq -0x1080(%rbp), %rdi
callq 0x6340
jmp 0x3e23f
cmpl $0x3, -0x1058(%rbp)
jne 0x3e11b
leaq -0x10c9(%rbp), %rdi
movq %rdi, -0x1168(%rbp)
callq 0x68d0
movq -0x1168(%rbp), %rdx
leaq 0x3658(%rip), %rsi      # 0x4169d
leaq -0x10c8(%rbp), %rdi
callq 0x9970
jmp 0x3e053
leaq -0x10c9(%rbp), %rdi
callq 0x6560
movl -0xc(%rbp), %eax
movl %eax, -0x116c(%rbp)
leaq -0x10f0(%rbp), %rdi
leaq -0x10c8(%rbp), %rsi
callq 0x6210
jmp 0x3e07d
movl -0x116c(%rbp), %edi
leaq -0x10f0(%rbp), %rsi
callq 0x3e3a0
jmp 0x3e091
leaq -0x10f0(%rbp), %rdi
callq 0x6340
movl -0xc(%rbp), %edi
callq 0x3e410
jmp 0x3e0a7
leaq -0x10c8(%rbp), %rdi
callq 0x6340
jmp 0x3e11b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x10c9(%rbp), %rdi
callq 0x6560
jmp 0x3e23f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
jmp 0x3e10a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x10f0(%rbp), %rdi
callq 0x6340
leaq -0x10c8(%rbp), %rdi
callq 0x6340
jmp 0x3e23f
cmpl $0x2, -0x1058(%rbp)
jne 0x3e219
leaq -0x1111(%rbp), %rdi
movq %rdi, -0x1178(%rbp)
callq 0x68d0
movq -0x1178(%rbp), %rdx
leaq 0x3566(%rip), %rsi      # 0x416af
leaq -0x1110(%rbp), %rdi
callq 0x9970
jmp 0x3e157
leaq -0x1111(%rbp), %rdi
callq 0x6560
movl -0xc(%rbp), %eax
movl %eax, -0x117c(%rbp)
leaq -0x1138(%rbp), %rdi
leaq -0x1110(%rbp), %rsi
callq 0x6210
jmp 0x3e181
movl -0x117c(%rbp), %edi
leaq -0x1138(%rbp), %rsi
callq 0x3e3a0
jmp 0x3e195
leaq -0x1138(%rbp), %rdi
callq 0x6340
movl -0xc(%rbp), %edi
callq 0x3e410
jmp 0x3e1ab
leaq -0x1110(%rbp), %rdi
callq 0x6340
jmp 0x3e219
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x1111(%rbp), %rdi
callq 0x6560
jmp 0x3e23f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
jmp 0x3e20b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1028(%rbp)
movl %eax, -0x102c(%rbp)
leaq -0x1138(%rbp), %rdi
callq 0x6340
leaq -0x1110(%rbp), %rdi
callq 0x6340
jmp 0x3e23f
movl $0x0, -0x105c(%rbp)
leaq -0x1050(%rbp), %rdi
callq 0x6340
movl -0x105c(%rbp), %eax
testl %eax, %eax
je 0x3e23d
jmp 0x3e23b
jmp 0x3e252
jmp 0x3e24d
leaq -0x1050(%rbp), %rdi
callq 0x6340
jmp 0x3e2ac
jmp 0x3dde5
movl -0xc(%rbp), %edi
callq 0x3e4a0
movl -0xc(%rbp), %edi
callq 0x6990
movq -0x1020(%rbp), %rax
movq %rax, -0x1188(%rbp)
cmpq $0x0, %rax
je 0x3e293
movq -0x1188(%rbp), %rdi
callq 0x382f0
movq -0x1188(%rbp), %rdi
movl $0x1, %esi
callq 0x6500
leaq 0x3424(%rip), %rdi      # 0x416be
movb $0x0, %al
callq 0x6080
xorl %eax, %eax
addq $0x1190, %rsp           # imm = 0x1190
popq %rbp
retq
movq -0x1028(%rbp), %rdi
callq 0x68c0
nopl (%rax,%rax)