// File: hinrikHelga[P]Botnet/client.cpp
// Function: main
int main(int argc, char* argv[])
{
   struct addrinfo hints, *svr;              // Network host entry for server
   struct sockaddr_in server_addr;           // Socket address for server
   int serverSocket;                         // Socket used for server
   int nwrite;                               // No. bytes written to server
   char buffer[1025];                        // buffer for writing to server
   bool finished;
   int set = 1;                              // Toggle for setsockopt

   if(argc != 3)
   {
        printf("Usage: chat_client <ip  port>\n");
        printf("Ctrl-C to terminate\n");
        exit(0);
   }

   hints.ai_family   = AF_INET;            // IPv4 only addresses
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags    = AI_PASSIVE;

   memset(&hints,   0, sizeof(hints));

   if(getaddrinfo(argv[1], argv[2], &hints, &svr) != 0)
   {
       perror("getaddrinfo failed: ");
       exit(0);
   }

   serverSocket = socket(svr->ai_family, svr->ai_socktype, svr->ai_protocol);

   // Turn on SO_REUSEADDR to allow socket to be quickly reused after
   // program exit.

   if(setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)) < 0)
   {
       printf("Failed to set SO_REUSEADDR for port %s\n", argv[2]);
       perror("setsockopt failed: ");
   }

   if(connect(serverSocket, svr->ai_addr, svr->ai_addrlen )< 0)
   {
       printf("Failed to open socket to server: %s\n", argv[1]);
       perror("Connect failed: ");
       exit(0);
   }

    // Listen and print replies from server
   std::thread serverThread(listenServer, serverSocket);

   finished = false;
   while(!finished)
   {
       bzero(buffer, sizeof(buffer));

       fgets(buffer, sizeof(buffer), stdin);

       nwrite = send(serverSocket, buffer, strlen(buffer),0);

       if(nwrite  == -1)
       {
           perror("send() to server failed: ");
           finished = true;
       }

   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x478(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x138c
leaq 0xca2(%rip), %rdi       # 0x2012
movb $0x0, %al
callq 0x1030
leaq 0xcb3(%rip), %rdi       # 0x2031
movb $0x0, %al
callq 0x1030
xorl %edi, %edi
callq 0x1110
movl $0x2, -0x3c(%rbp)
movl $0x1, -0x38(%rbp)
movl $0x1, -0x40(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x10a0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x1120
cmpl $0x0, %eax
je 0x13e6
leaq 0xc6c(%rip), %rdi       # 0x2046
callq 0x10c0
xorl %edi, %edi
callq 0x1110
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x48(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x1040
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x478(%rbp), %rcx
movl $0x4, %r8d
callq 0x1170
cmpl $0x0, %eax
jge 0x1449
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0xc25(%rip), %rdi       # 0x205b
movb $0x0, %al
callq 0x1030
leaq 0xc3f(%rip), %rdi       # 0x2083
callq 0x10c0
movl -0x5c(%rbp), %edi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x10b0
cmpl $0x0, %eax
jge 0x148e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xc23(%rip), %rdi       # 0x2097
movb $0x0, %al
callq 0x1030
leaq 0xc3a(%rip), %rdi       # 0x20bc
callq 0x10c0
xorl %edi, %edi
callq 0x1110
leaq -0x480(%rbp), %rdi
leaq -0x1dc(%rip), %rsi      # 0x12c0
leaq -0x5c(%rbp), %rdx
callq 0x15a0
movb $0x0, -0x471(%rbp)
movb -0x471(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x14bd
jmp 0x157c
leaq -0x470(%rbp), %rdi
movq %rdi, -0x498(%rbp)
xorl %esi, %esi
movl $0x401, %edx            # imm = 0x401
callq 0x10a0
movq -0x498(%rbp), %rdi
movq 0x2af3(%rip), %rax      # 0x3fd8
movq (%rax), %rdx
movl $0x401, %esi            # imm = 0x401
callq 0x1150
jmp 0x14f4
movl -0x5c(%rbp), %eax
movl %eax, -0x4ac(%rbp)
leaq -0x470(%rbp), %rdi
movq %rdi, -0x4a8(%rbp)
callq 0x1070
movl -0x4ac(%rbp), %edi
movq -0x4a8(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x1090
movq %rax, -0x4a0(%rbp)
jmp 0x1530
movq -0x4a0(%rbp), %rax
movl %eax, -0x60(%rbp)
cmpl $-0x1, -0x60(%rbp)
jne 0x1577
leaq 0xb86(%rip), %rdi       # 0x20cd
callq 0x10c0
jmp 0x154e
movb $0x1, -0x471(%rbp)
jmp 0x1577
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x488(%rbp)
movl %eax, -0x48c(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x1660
jmp 0x1594
jmp 0x14ac
leaq -0x480(%rbp), %rdi
callq 0x1660
movl -0x4(%rbp), %eax
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
movq -0x488(%rbp), %rdi
callq 0x1160