// File: /workspace/github_cpp_makefile_star2_O3/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
leaq 0x130(%rsp), %rbx
movq $0x0, 0x8(%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 0x28a3
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 0x2a25
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 0x3056
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 0x28be
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 0x2f40
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
leaq (%rax,%rdx), %rcx
cmpq $0xfa1, %rcx            # imm = 0xFA1
jb 0x283a
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x2fc0
leaq 0x88(%rsp), %rdi
xorl %esi, %esi
callq 0x2dba
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2817
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x10(%rsp), %rdx
movl $0x7, %r8d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x185c(%rip), %rcx      # 0x408c
callq 0x2200
movq 0x50(%rsp), %rax
movq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdi
movq %rax, %rdx
callq 0x2080
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2863
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x287f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x289b
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
incl %r15d
jmp 0x260a
movl %eax, %r15d
movq 0x3723(%rip), %rdi      # 0x5fd0
leaq 0x176e(%rip), %rsi      # 0x4022
movl $0x1b, %edx
jmp 0x2bf0
leaq 0x120(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x110(%rsp), %rdi
callq 0x2fc0
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
callq 0x2dba
movq 0x110(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2910
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x2180
callq 0x2040
movq %rax, %r14
movq 0x36b1(%rip), %rdi      # 0x5fd0
leaq 0x176e(%rip), %rsi      # 0x4094
movl $0x14, %edx
callq 0x21b0
movq 0x3699(%rip), %rdi      # 0x5fd0
movl %r15d, %esi
callq 0x2230
movq %rax, %rbx
leaq 0x1760(%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 0x364c(%rip), %rdi      # 0x5fd0
leaq 0x1730(%rip), %rsi      # 0x40bb
movl $0xd, %edx
callq 0x21b0
subq 0xc8(%rsp), %r14
cvtsi2sd %r14, %xmm0
divsd 0x165e(%rip), %xmm0     # 0x4008
divsd 0x165e(%rip), %xmm0     # 0x4010
movq 0x3617(%rip), %rdi      # 0x5fd0
callq 0x2220
movq %rax, %rbx
leaq 0x1701(%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 0x2a18
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x140(%rsp), %rdi
callq 0x2060
leaq 0x150(%rsp), %r14
leaq 0x100(%rsp), %rbp
movq 0x35a4(%rip), %r15      # 0x5fe0
leaq 0x140(%rsp), %r12
leaq 0xf0(%rsp), %r13
movl $0x3, %edx
movq 0x3578(%rip), %rdi      # 0x5fd0
leaq 0x1674(%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 0x2fc0
movq %r13, %rdi
movl $0x1, %esi
callq 0x2dba
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2aef
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2180
movq %r12, %rdi
leaq 0x15de(%rip), %rsi      # 0x40d7
callq 0x2070
movl %eax, %ebx
testl %eax, %eax
jne 0x2b4e
movl $0x7, %edx
movq 0x34c0(%rip), %rdi      # 0x5fd0
leaq 0x15c5(%rip), %rsi      # 0x40dc
callq 0x21b0
movq 0x34ad(%rip), %rcx      # 0x5fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq 0x348f(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x140(%rsp), %rdi
cmpq %r14, %rdi
je 0x2b6b
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x2180
testl %ebx, %ebx
jne 0x2a4c
movl 0x4637(%rip), %edi      # 0x71b0
leaq 0x3630(%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 0x342f(%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 0x45d4(%rip), %edi      # 0x71b0
callq 0x2280
leaq 0x14fc(%rip), %rsi      # 0x40e4
movl $0x11, %edx
movq %r14, %rdi
callq 0x21b0
movq 0x33d4(%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 0x2d45
movq 0x120(%rsp), %rsi
jmp 0x2d38
jmp 0x2d42
jmp 0x2c60
movq %rax, %r15
jmp 0x2d61
jmp 0x2d42
movq %rax, %r15
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2cec
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2cec
movq %rax, %r15
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2cca
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2cca
jmp 0x2d42
jmp 0x2d21
jmp 0x2cc2
movq %rax, %r15
jmp 0x2d03
movq %rax, %r15
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x2d03
movq 0xe0(%rsp), %rsi
jmp 0x2cfb
movq %rax, %r15
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2d03
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2d24
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2d24
movq %rax, %r15
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2d45
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2d45
movq %rax, %r15
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2d61
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x140(%rsp), %rdi
callq 0x2060
jmp 0x2db2
movq %rax, %r15
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2d95
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x2d95
movq %rax, %r15
movq 0x140(%rsp), %rdi
cmpq %r14, %rdi
je 0x2db2
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x2180
movq %r15, %rdi
callq 0x2240