// File: /workspace/github_cpp_makefile_star2_O2/PepiJK[P]Mail-Server-Client/src/myclient.cpp
// Function: main
int main(int argc, char *argv[])
{
   MyHelper helper;
   helper.checkClientArguments(argc, argv);

   MySocket clientSocket(argv[1], stoi(argv[2]));
   clientSocket.createSocket();
   clientSocket.connectSocket();

   char buffer[BUF], command[BUF];

   cout << clientSocket.recvMessage() << endl;

   while (1)
   {
      fgets(command, BUF, stdin);
      buffer[strlen(command)] = '\0';
      clientSocket.sendMessage(command);

      if (strcmp(command, "SEND\n\0") == 0)
      {
         // sender, reveiver, subject
         for (int i = 0; i < 4; i++)
         {
            fgets(buffer, BUF, stdin);
            buffer[strlen(buffer)] = '\0';
            clientSocket.sendMessage(buffer);
         }
         // email message
         do
         {
            fgets(buffer, BUF, stdin);
            if (strncmp(buffer, ".\n", 2) == 0)
            {
               buffer[strlen(buffer)] = '\0';
            }
            clientSocket.sendMessage(buffer);
         } while (strcmp(buffer, ".\n\0") != 0);
         cout << clientSocket.recvMessage() << endl;
      }

      else if (strcmp(command, "LIST\n\0") == 0)
      {
         // user
         fgets(buffer, BUF, stdin);
         buffer[strlen(buffer)] = '\0';
         clientSocket.sendMessage(buffer);

         string mailSubject, numberOfMails;
         numberOfMails = clientSocket.recvMessage();
         cout << numberOfMails;

         int mailAmount = stoi(numberOfMails.substr(0, numberOfMails.find(' ')).c_str());
         if (mailAmount > 0)
         {
            for (int i = 1; i <= mailAmount; i++)
            {
               // tell the server to start sending the list of mails
               clientSocket.sendMessage("OK\0");
               mailSubject = clientSocket.recvMessage();
               mailSubject = mailSubject.substr(mailSubject.find('_') + 1, mailSubject.length()) + ' ' + to_string(i);
               cout << mailSubject << endl;
            }
         }
      }

      else if (strcmp(command, "READ\n\0") == 0)
      {
         // user
         fgets(buffer, BUF, stdin);
         buffer[strlen(buffer)] = '\0';
         clientSocket.sendMessage(buffer);

         // mail number
         fgets(buffer, BUF, stdin);
         buffer[strlen(buffer)] = '\0';
         clientSocket.sendMessage(buffer);

         string mailOutput;
         string resp = clientSocket.recvMessage();
         cout << resp << endl;

         if (resp == "OK")
         {
            while (1)
            {
               // send Server the ok to start receiving the mail
               clientSocket.sendMessage("OK\0");
               mailOutput = clientSocket.recvMessage();
               if (mailOutput == ".\n")
               {
                  break;
               }
               cout << mailOutput;
            }
         }
      }

      else if (strcmp(command, "DEL\n\0") == 0)
      {
         // user
         fgets(buffer, BUF, stdin);
         buffer[strlen(buffer)] = '\0';
         clientSocket.sendMessage(buffer);

         // mail number
         fgets(buffer, BUF, stdin);
         buffer[strlen(buffer)] = '\0';
         clientSocket.sendMessage(buffer);

         cout << clientSocket.recvMessage() << endl;
      }

      else if (strcmp(command, "QUIT\n\0") == 0)
      {
         break;
      }

      else
      {
         cout << "Unknown command!" << endl;
      }
   }
   return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd28, %rsp            # imm = 0xD28
movq %rsi, %r14
movl %edi, %ebx
leaq -0x120(%rbp), %r15
movq %r15, %rdi
callq 0x250c
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x26b4
movq 0x8(%r14), %rbx
movq 0x10(%r14), %rsi
leaq -0x520(%rbp), %rdi
leaq -0xd50(%rbp), %rdx
callq 0x20b0
leaq -0x520(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2a83
leaq -0x950(%rbp), %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0x2ec0
leaq -0x520(%rbp), %rdi
callq 0x20f0
leaq -0x950(%rbp), %rdi
callq 0x2ef6
leaq -0x950(%rbp), %rdi
callq 0x2f2e
leaq -0x950(%rbp), %rdi
pushq $-0x1
popq %rsi
callq 0x30d6
movq 0x3da4(%rip), %rdi      # 0x6fd0
movq %rax, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2100
leaq -0x950(%rbp), %r14
pushq $-0x1
popq %r15
movq 0x3d7a(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
leaq -0xd50(%rbp), %rbx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %rbx, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x312c
movq %rbx, %rdi
leaq 0xeee(%rip), %rsi       # 0x417b
pushq $0x6
popq %r13
movq %r13, %rdx
callq 0x2200
testl %eax, %eax
je 0x332c
movq %rbx, %rdi
leaq 0xed7(%rip), %rsi       # 0x4182
movq %r13, %rdx
callq 0x2200
testl %eax, %eax
je 0x35bb
movq %rbx, %rdi
leaq 0xec4(%rip), %rsi       # 0x4189
movq %r13, %rdx
callq 0x2200
testl %eax, %eax
je 0x3401
movq %rbx, %rdi
leaq 0xeb1(%rip), %rsi       # 0x4190
pushq $0x5
popq %rdx
callq 0x2200
testl %eax, %eax
leaq -0x950(%rbp), %r14
pushq $-0x1
popq %r15
je 0x3545
movq %rbx, %rdi
leaq 0xe92(%rip), %rsi       # 0x4196
pushq $0x6
popq %rdx
callq 0x2200
testl %eax, %eax
je 0x37de
movq 0x3cb5(%rip), %rdi      # 0x6fd0
leaq 0xe81(%rip), %rsi       # 0x41a3
callq 0x21d0
jmp 0x33f4
pushq $0x4
popq %rbx
leaq -0x950(%rbp), %r14
pushq $-0x1
popq %r15
subl $0x1, %ebx
jb 0x337d
movq 0x3c82(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
leaq -0x520(%rbp), %r12
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %r12, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
movq %r14, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x312c
jmp 0x333a
movq 0x3c44(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
leaq -0x520(%rbp), %rbx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
cmpw $0xa2e, -0x520(%rbp)    # imm = 0xA2E
jne 0x33b6
movq %rbx, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x312c
movq %rbx, %rdi
leaq 0xde6(%rip), %rsi       # 0x41b4
pushq $0x3
popq %rdx
callq 0x2200
testl %eax, %eax
jne 0x337d
movq %r14, %rdi
movl %r15d, %esi
callq 0x30d6
movq 0x3be4(%rip), %rdi      # 0x6fd0
movq %rax, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2100
jmp 0x3247
movq 0x3bc0(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
leaq -0x520(%rbp), %rbx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %rbx, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
leaq -0x950(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
pushq $-0x1
popq %r15
movl %r15d, %edx
callq 0x312c
movq 0x3b79(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %rbx, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x312c
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
movb $0x0, -0x38(%rbp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x30d6
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
leaq -0xd0(%rbp), %rdx
callq 0x20b0
movq 0x3b1a(%rip), %rdi      # 0x6fd0
movq %rbx, %rsi
callq 0x21b0
movq %rax, %rdi
callq 0x2100
leaq -0x68(%rbp), %rdi
leaq 0xccf(%rip), %rsi       # 0x41a0
callq 0x2c5b
testb %al, %al
leaq -0x48(%rbp), %rbx
je 0x352f
movq %r14, %rdi
leaq 0xcd0(%rip), %rsi       # 0x41b8
movl %r15d, %edx
callq 0x312c
movq %r14, %rdi
movl %r15d, %esi
callq 0x30d6
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x2360
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc88(%rip), %rsi       # 0x419d
callq 0x2c5b
testb %al, %al
jne 0x352f
movq 0x3aab(%rip), %rdi      # 0x6fd0
movq %rbx, %rsi
callq 0x21b0
jmp 0x34de
leaq -0x68(%rbp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x20f0
jmp 0x3247
movq 0x3a7c(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
leaq -0x520(%rbp), %rbx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %rbx, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x312c
movq 0x3a40(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %rbx, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x312c
jmp 0x33da
movq 0x3a06(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
leaq -0x520(%rbp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x22f0
movq %r14, %rdi
callq 0x20d0
movb $0x0, -0x520(%rbp,%rax)
leaq -0x950(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
pushq $-0x1
popq %r14
movl %r14d, %edx
callq 0x312c
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
xorl %ecx, %ecx
movb %cl, -0x38(%rbp)
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movb %cl, -0x58(%rbp)
movq %rbx, %rdi
movl %r14d, %esi
callq 0x30d6
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2360
movq 0x398b(%rip), %rdi      # 0x6fd0
movq %rbx, %rsi
callq 0x21b0
movq %rbx, %rdi
pushq $0x20
popq %rsi
xorl %edx, %edx
callq 0x2280
leaq -0x90(%rbp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x2210
movq -0x90(%rbp), %rsi
leaq -0xd0(%rbp), %r14
movq %r14, %rdi
leaq -0xb0(%rbp), %rdx
callq 0x20b0
leaq -0x48(%rbp), %r15
movq %r14, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2a83
movl %eax, %ebx
movq %r14, %rdi
callq 0x20f0
leaq -0x90(%rbp), %rdi
callq 0x20f0
testl %ebx, %ebx
jle 0x37c8
negl %ebx
pushq $0x1
popq %rax
movl %eax, %r13d
leal (%rbx,%r13), %eax
cmpl $0x1, %eax
je 0x37c8
leaq -0x950(%rbp), %rdi
leaq 0xadb(%rip), %rsi       # 0x41b8
pushq $-0x1
popq %rdx
callq 0x312c
leaq -0x950(%rbp), %rdi
pushq $-0x1
popq %rsi
callq 0x30d6
movq %r15, %rdi
movq %rax, %rsi
callq 0x2360
movq %r15, %rdi
pushq $0x5f
popq %rsi
xorl %edx, %edx
callq 0x2280
leaq 0x1(%rax), %rdx
movq -0x40(%rbp), %rcx
movq %r15, %rsi
leaq -0xb0(%rbp), %r15
movq %r15, %rdi
callq 0x2210
leaq -0x90(%rbp), %rdi
movq %r15, %rsi
pushq $0x20
popq %rdx
callq 0x393c
leaq -0xf0(%rbp), %r12
movq %r12, %rdi
movl %r13d, %esi
callq 0x3968
movq %r14, %rdi
movq %r14, %r15
leaq -0x90(%rbp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x38cb
movq %r13, -0x70(%rbp)
movq %rbx, %r13
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2260
movq %r15, %rdi
callq 0x20f0
movq %r12, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x20f0
leaq -0xb0(%rbp), %rdi
callq 0x20f0
movq 0x382d(%rip), %rdi      # 0x6fd0
movq %rbx, %rsi
callq 0x21b0
movq %r15, %r14
movq %rbx, %r15
movq %r13, %rbx
movq -0x70(%rbp), %r13
movq %rax, %rdi
callq 0x2100
incl %r13d
jmp 0x36c2
leaq -0x68(%rbp), %rdi
callq 0x20f0
movq %r15, %rdi
callq 0x20f0
jmp 0x323c
leaq -0x950(%rbp), %rdi
callq 0x316e
leaq -0x120(%rbp), %rdi
callq 0x2a3e
xorl %eax, %eax
addq $0xd28, %rsp            # imm = 0xD28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x380c
movq %rax, %rbx
jmp 0x38b7
jmp 0x3891
movq %rax, %rbx
leaq -0x520(%rbp), %rdi
callq 0x20f0
jmp 0x38b7
jmp 0x3891
jmp 0x382e
movq %rax, %rbx
jmp 0x38a2
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x20f0
jmp 0x3847
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
jmp 0x3886
jmp 0x3896
jmp 0x3896
jmp 0x3896
jmp 0x3891
jmp 0x3896
movq %rax, %rbx
jmp 0x387f
movq %rax, %rbx
jmp 0x3873
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x20f0
leaq -0x90(%rbp), %rdi
callq 0x20f0
leaq -0xb0(%rbp), %rdi
callq 0x20f0
jmp 0x3899
jmp 0x3896
jmp 0x3891
movq %rax, %rbx
jmp 0x38ab
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x20f0
leaq -0x48(%rbp), %rdi
callq 0x20f0
leaq -0x950(%rbp), %rdi
callq 0x316e
leaq -0x120(%rbp), %rdi
callq 0x2a3e
movq %rbx, %rdi
callq 0x2340