// File: /workspace/github_cpp_makefile_star2_O1/prakhar2808[P]DBEngine/clientCode/client.cc
// Function: main
int main(int argc, char** argv) {

  struct hostent* host = gethostbyname(serverIp.c_str()); 
  sockaddr_in sendSockAddr;   
  bzero((char*)&sendSockAddr, sizeof(sendSockAddr)); 
  sendSockAddr.sin_family = AF_INET; 
  sendSockAddr.sin_addr.s_addr = 
    inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list));
  sendSockAddr.sin_port = htons(port);
  clientSd = socket(AF_INET, SOCK_STREAM, 0);
    
  int status = connect(clientSd,
                      (sockaddr*) &sendSockAddr, 
                      sizeof(sendSockAddr));
  if(status < 0) {
    std::cout << "Error connecting to socket!" 
              << std::endl; 
    return status;
  }
    
  std::cout << "Connected to the server!" 
            << std::endl;

  // First message
  recv(clientSd, buffer, sizeof(buffer), 0);
  std::cout << buffer << std::endl;
  
  // Check if the client has sent a file name to dump in the db
  if(argc == 2) {
    std::cout << "Please wait! Dumping input file to the database ... "
              << std::endl;
    auto t_start = std::chrono::high_resolution_clock::now();
    std::ifstream fd;
    fd.open(argv[1]);
    fd >> std::noskipws;
    unsigned char len;
    int count = 0;
    std::string sendBuffer = "FILEPUT";
    while(fd.peek() != EOF) {
      fd >> len;
      std::string key((int)len, '\0');
      fd.read(&key[0], (int)len);
      fd >> len;
      std::string value((int)len, '\0');
      fd.read(&value[0], (int)len);
      std::string data = (char)(key.length()) 
                         + key 
                         + (char)(value.length()) 
                         + value;
      if(sendBuffer.length() + data.length() > 4000) {
        sendToServerAndGetResponse(sendBuffer, false);
        //break;
        sendBuffer = "FILEPUT";
      }
      sendBuffer += data;
      ++count;
    }
    // Do the last time.
    sendToServerAndGetResponse(sendBuffer, false);
    // Calculate time elapsed.
    auto t_end = std::chrono::high_resolution_clock::now();
    double elapsed_time_ms = 
      std::chrono::duration<double, std::milli>(t_end-t_start).count();
    std::cout << "Successfully dumped "
              << count 
              << " key-value pairs!"
              << std::endl;
    std::cout << "Time taken : "
              << (elapsed_time_ms/1000)
              << " seconds."
              << std::endl;
  }


  while(true) {
    std::cout << ">> ";
    std::string data;
    getline(std::cin, data);
    
    sendToServerAndGetResponse(data);
    
    if(data == "exit") {
      std::cout << "Exiting"
                << std::endl;
      break;
    }
  }

  // Last message
  recv(clientSd, buffer, sizeof(buffer), 0);
  std::cout << buffer << std::endl;
  close(clientSd);
  std::cout << "Connection closed" 
            << std::endl;
  return 0;    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
movq %rsi, %r14
movl %edi, %ebp
movq 0x3d5f(%rip), %rdi      # 0x6188
callq 0x21a0
xorpd %xmm0, %xmm0
leaq 0x130(%rsp), %rbx
movapd %xmm0, (%rbx)
movw $0x2, (%rbx)
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %edi
callq 0x20c0
movq %rax, %rdi
callq 0x2170
movl %eax, 0x4(%rbx)
movzwl 0x3cf5(%rip), %eax      # 0x6158
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2050
movl %eax, 0x4d2e(%rip)      # 0x71b0
movl %eax, %edi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x2130
testl %eax, %eax
js 0x289e
movq 0x3b30(%rip), %r15      # 0x5fd0
leaq 0x1b97(%rip), %rsi      # 0x403e
movl $0x18, %edx
movq %r15, %rdi
callq 0x21b0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movl 0x4ccf(%rip), %edi      # 0x71b0
leaq 0x3cc8(%rip), %rbx      # 0x61b0
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2090
movq %rbx, %rdi
callq 0x20b0
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21b0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
cmpl $0x2, %ebp
jne 0x2a20
leaq 0x1b13(%rip), %rsi      # 0x4057
movl $0x34, %edx
movq 0x3a80(%rip), %rbx      # 0x5fd0
movq %rbx, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
callq 0x2040
movq %rax, 0xc8(%rsp)
leaq 0x140(%rsp), %rbx
movq %rbx, %rdi
callq 0x2190
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0x20f0
movq 0x140(%rsp), %rax
movq -0x18(%rax), %rax
andl $0xffffefff, 0x158(%rsp,%rax) # imm = 0xFFFFEFFF
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1ab8(%rip), %rsi      # 0x408c
leaq 0x1ab8(%rip), %rdx      # 0x4093
leaq 0x8(%rsp), %rdi
callq 0x304c
leaq 0xb8(%rsp), %r13
leaq 0x58(%rsp), %rbx
xorl %r15d, %r15d
leaq 0x140(%rsp), %r12
leaq 0x68(%rsp), %rbp
leaq 0xd0(%rsp), %r14
movq %r12, %rdi
callq 0x20a0
cmpl $-0x1, %eax
je 0x28b9
movq %r12, %rdi
leaq 0x7(%rsp), %rsi
callq 0x21e0
movzbl 0x7(%rsp), %esi
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rbp, %rdi
xorl %edx, %edx
callq 0x21c0
movq 0x68(%rsp), %rsi
movzbl 0x7(%rsp), %edx
movq %r12, %rdi
callq 0x21d0
movq %r12, %rdi
leaq 0x7(%rsp), %rsi
callq 0x21e0
movzbl 0x7(%rsp), %esi
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x21c0
movq 0x28(%rsp), %rsi
movzbl 0x7(%rsp), %edx
movq %r12, %rdi
callq 0x21d0
movsbl 0x70(%rsp), %esi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x2f3b
movsbl 0x30(%rsp), %r8d
movq 0xd8(%rsp), %rsi
movl $0x1, %ecx
movq %r14, %rdi
xorl %edx, %edx
callq 0x2270
movq %r13, 0xa8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x26e6
movq %rdx, 0xa8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xb8(%rsp)
jmp 0x26f0
movupd (%rcx), %xmm0
movupd %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0xb0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
callq 0x2080
movq %rbx, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2745
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x274d
movupd (%rcx), %xmm0
movupd %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2782
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x27a7
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x10(%rsp), %rdx
movq 0x50(%rsp), %rax
addq %rdx, %rax
cmpq $0xfa1, %rax            # imm = 0xFA1
jb 0x2833
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x2fbc
leaq 0x88(%rsp), %rdi
xorl %esi, %esi
callq 0x2db5
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2815
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x10(%rsp), %rdx
movl $0x7, %r8d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x185e(%rip), %rcx      # 0x408c
callq 0x2200
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rdi
callq 0x2080
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x285e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x287a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2896
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
incl %r15d
jmp 0x260a
movl %eax, %r15d
movq 0x3728(%rip), %rdi      # 0x5fd0
leaq 0x1773(%rip), %rsi      # 0x4022
movl $0x1b, %edx
jmp 0x2beb
leaq 0x120(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x110(%rsp), %rdi
callq 0x2fbc
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
callq 0x2db5
movq 0x110(%rsp), %rdi
cmpq %rbx, %rdi
je 0x290b
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x2180
callq 0x2040
movq %rax, %r14
movq 0x36b6(%rip), %rdi      # 0x5fd0
leaq 0x1773(%rip), %rsi      # 0x4094
movl $0x14, %edx
callq 0x21b0
movq 0x369e(%rip), %rdi      # 0x5fd0
movl %r15d, %esi
callq 0x2230
movq %rax, %rbx
leaq 0x1765(%rip), %rsi      # 0x40a9
movl $0x11, %edx
movq %rax, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x3651(%rip), %rdi      # 0x5fd0
leaq 0x1735(%rip), %rsi      # 0x40bb
movl $0xd, %edx
callq 0x21b0
subq 0xc8(%rsp), %r14
cvtsi2sd %r14, %xmm0
divsd 0x1663(%rip), %xmm0     # 0x4008
divsd 0x1663(%rip), %xmm0     # 0x4010
movq 0x361c(%rip), %rdi      # 0x5fd0
callq 0x2220
movq %rax, %rbx
leaq 0x1706(%rip), %rsi      # 0x40c9
movl $0x9, %edx
movq %rax, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2a13
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x140(%rsp), %rdi
callq 0x2060
leaq 0x150(%rsp), %r14
leaq 0x100(%rsp), %rbp
movq 0x35a9(%rip), %r15      # 0x5fe0
leaq 0x140(%rsp), %r12
leaq 0xf0(%rsp), %r13
movl $0x3, %edx
movq 0x357d(%rip), %rdi      # 0x5fd0
leaq 0x1679(%rip), %rsi      # 0x40d3
callq 0x21b0
movq %r14, 0x140(%rsp)
movq $0x0, 0x148(%rsp)
movb $0x0, 0x150(%rsp)
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2260
movq %rbp, 0xf0(%rsp)
movq 0x140(%rsp), %rsi
movq 0x148(%rsp), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x2fbc
movq %r13, %rdi
movl $0x1, %esi
callq 0x2db5
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2aea
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2180
movq %r12, %rdi
leaq 0x15e3(%rip), %rsi      # 0x40d7
callq 0x2070
movl %eax, %ebx
testl %eax, %eax
jne 0x2b49
movl $0x7, %edx
movq 0x34c5(%rip), %rdi      # 0x5fd0
leaq 0x15ca(%rip), %rsi      # 0x40dc
callq 0x21b0
movq 0x34b2(%rip), %rcx      # 0x5fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq 0x3494(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x140(%rsp), %rdi
cmpq %r14, %rdi
je 0x2b66
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x2180
testl %ebx, %ebx
jne 0x2a47
movl 0x463c(%rip), %edi      # 0x71b0
leaq 0x3635(%rip), %rbx      # 0x61b0
xorl %r15d, %r15d
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2090
movq %rbx, %rdi
callq 0x20b0
movq 0x3434(%rip), %r14      # 0x5fd0
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movl 0x45d9(%rip), %edi      # 0x71b0
callq 0x2280
leaq 0x1501(%rip), %rsi      # 0x40e4
movl $0x11, %edx
movq %r14, %rdi
callq 0x21b0
movq 0x33d9(%rip), %rbx      # 0x5fd0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movl %r15d, %eax
addq $0x348, %rsp            # imm = 0x348
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq 0x110(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2d40
movq 0x120(%rsp), %rsi
jmp 0x2d33
jmp 0x2d3d
jmp 0x2c5b
movq %rax, %r15
jmp 0x2d5c
jmp 0x2d3d
movq %rax, %r15
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2ce7
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2ce7
movq %rax, %r15
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2cc5
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2cc5
jmp 0x2d3d
jmp 0x2d1c
jmp 0x2cbd
movq %rax, %r15
jmp 0x2cfe
movq %rax, %r15
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x2cfe
movq 0xe0(%rsp), %rsi
jmp 0x2cf6
movq %rax, %r15
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2cfe
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2d1f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2d1f
movq %rax, %r15
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2d40
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2d40
movq %rax, %r15
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2d5c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x140(%rsp), %rdi
callq 0x2060
jmp 0x2dad
movq %rax, %r15
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2d90
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2d90
movq %rax, %r15
movq 0x140(%rsp), %rdi
cmpq %r14, %rdi
je 0x2dad
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x2180
movq %r15, %rdi
callq 0x2240