// File: /workspace/github_cpp_makefile_star2_O1/awbrenn[P]robot-controller/Client.cpp
// Function: main
int main (int argc, char *argv[])
{

   int len = atoi(argv[3]);
   int num = atoi(argv[4]);
   port = atoi(argv[2]);
   ip = argv[1];
   double moveSpeed = 0.2;
   double turnSpeed = 0.2;
   double moveSleep, turnSleep;
   size_t found;
   
   unsigned int TIMEOUT_SECONDS = 5;
   struct sigaction timeoutAction;
  
   cout << "Port: " << port << " IP: " << ip << endl;
   
   //Open file for output
   FILE *output;
   output = fopen("output.txt", "w");
   
   //Set timeout
   timeoutAction.sa_handler = catchAlarm;
   if (sigfillset(&timeoutAction.sa_mask) < 0)
      cout << "Sigfillset() failed" << endl;
   timeoutAction.sa_flags = 0;
   if (sigaction(SIGALRM, &timeoutAction, 0) < 0)
      cout << "Sigaction() failed" << endl;
   
   //Call function to populate the vector
   createVector(len, num);
     
   //Call function to start connection
   initalizeMiddleware();   
   
   //Call function to add header to vector components
//   shape1 = addHeaderToCommands(shape1);
//   shape2 = addHeaderToCommands(shape2);
   
   printf("Len: %d, Num: %d, AngleFirst: %f, AngleSecond: %f\n", len, 
      num, angleFirst, angleSecond);
      
   //Print content of vector
   vector<string>::iterator it;
   // for (it = v.begin(); it != v.end(); it++)
   //    cout << *it << endl;

   int i = 0;
   //Run loop until all of shape1 has been sent 
   for (it = shape1.begin(); it != shape1.end(); it++)
   {
      memset (recvBuffer, 0, sizeof(recvBuffer));
      clntAddrLen = sizeof(clntAddr);
       
      //Send command
      if (sendto(sock, shape1.at(i).c_str(), shape1.at(i).length(), 0, 
         (struct sockaddr *) &servAddr, sizeof(servAddr)) != shape1.at(i).length())
	      cout << "Error on sendto" << endl; 

      alarm(TIMEOUT_SECONDS);

      //Recv ack
      recieveAckFromMiddleware();

      cout << PROXY_RESPONSE <<  endl << endl;


      //Shape1 sleep for move
      found = shape1.at(i).find("MOVE");
      if (found != std::string::npos)
      { 
         cout << "Sleeping for shape1 move" << endl;
         moveSleep = len / moveSpeed * 1000000;
         usleep(moveSleep);
      }
      
      //Shape1 sleep for turn
      found = shape1.at(i).find("TURN");
      if (found != std::string::npos)
      {
         cout << "Sleeping for shape1 turn" << endl;
         turnSleep = angleFirst / turnSpeed * 1000000;
         usleep(turnSleep);
      }
      
      i++;
   }

   // test

  // string get_lasers("GET LASERS");

  // if (sendto(sock, get_lasers.c_str(), get_lasers.length(), 0, 
  //       (struct sockaddr *) &servAddr, sizeof(servAddr)) != get_lasers.length())
  //       cout << "Error on sendto" << endl;

  // //Recv ack
  // recieveAckFromMiddleware();

  // cout << PROXY_RESPONSE << endl;
   
   i = 0;
   //Run loop until all of shape2 has been sent 
   for (it = shape2.begin(); it != shape2.end(); it++)
   {
      memset (recvBuffer, 0, sizeof(recvBuffer));
      clntAddrLen = sizeof(clntAddr);
       
      //Send command
      if (sendto(sock, shape2.at(i).c_str(), shape2.at(i).length(), 0, 
         (struct sockaddr *) &servAddr, sizeof(servAddr)) != shape2.at(i).length())
	      cout << "Error on sendto" << endl; 
         
      alarm(TIMEOUT_SECONDS);

      //Recv ack
      recieveAckFromMiddleware();
      
      //Shape2 sleep for move
      found = shape2.at(i).find("MOVE");
      if (found != std::string::npos)
      { 
         cout << "Sleeping for shape2 move" << endl;
         moveSleep = len / moveSpeed * 1000000;
         usleep(moveSleep);
      }
      //Shape2 sleep for turn
      found = shape2.at(i).find("TURN");
      if (found != std::string::npos)
      {
         cout << "Sleeping for shape2 turn" << endl;
         turnSleep = angleSecond / turnSpeed * 1000000;
         usleep(turnSleep);
      }
      
      i++;
   }
   
   fclose(output);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq 0x18(%rsi), %rdi
callq 0x22a0
movl %eax, %r15d
movq 0x20(%rbx), %rdi
callq 0x22a0
movl %eax, %r14d
movq 0x10(%rbx), %rdi
callq 0x22a0
movl %eax, 0x4cd2(%rip)      # 0x724c
movq 0x8(%rbx), %rax
movq %rax, 0x4ccb(%rip)      # 0x7250
movq 0x4a44(%rip), %rbx      # 0x6fd0
leaq 0x2aa3(%rip), %rsi      # 0x5036
movl $0x6, %edx
movq %rbx, %rdi
callq 0x21f0
movl 0x4ca6(%rip), %esi      # 0x724c
movq %rbx, %rdi
callq 0x22d0
movq %rax, %r12
leaq 0x2a85(%rip), %rsi      # 0x503d
movl $0x5, %edx
movq %rax, %rdi
callq 0x21f0
movq 0x4c84(%rip), %r13      # 0x7250
testq %r13, %r13
je 0x25e9
movq %r13, %rdi
callq 0x20e0
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x21f0
jmp 0x2604
movq (%r12), %rax
movq -0x18(%rax), %rax
movq %r12, %rdi
addq %rax, %rdi
movl 0x20(%r12,%rax), %esi
orl $0x1, %esi
callq 0x22c0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0x2a10(%rip), %rdi      # 0x5043
leaq 0x2a14(%rip), %rsi      # 0x504e
callq 0x2200
movq %rax, -0x40(%rbp)
leaq 0x618(%rip), %rax       # 0x2c62
leaq -0xd0(%rbp), %rdi
movq %rax, -0x8(%rdi)
callq 0x20b0
testl %eax, %eax
jns 0x2699
leaq 0x29eb(%rip), %rsi      # 0x5050
movl $0x13, %edx
movq %rbx, %rdi
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq -0xd8(%rbp), %rsi
movl $0x0, 0x88(%rsi)
movl $0xe, %edi
xorl %edx, %edx
callq 0x2230
testl %eax, %eax
jns 0x26f5
leaq 0x29a3(%rip), %rsi      # 0x5064
movl $0x12, %edx
movq %rbx, %rdi
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl %r15d, %edi
movl %r14d, %esi
callq 0x2c68
callq 0x31ef
cvtss2sd 0xf8db3(%rip), %xmm0    # 0xfb4c0
cvtss2sd 0xf8daf(%rip), %xmm1    # 0xfb4c4
leaq 0x295b(%rip), %rdi      # 0x5077
movl %r15d, %esi
movl %r14d, %edx
movb $0x2, %al
callq 0x2050
movq 0x4ab8(%rip), %r14      # 0x71e8
movq 0x4ab9(%rip), %r13      # 0x71f0
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, -0x38(%rbp)
cmpq %r13, %r14
je 0x29d5
movsd -0x38(%rbp), %xmm0
divsd 0x28ae(%rip), %xmm0     # 0x5008
mulsd 0x28ae(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movl $0xf4240, %edx          # imm = 0xF4240
leaq 0x4ae3(%rip), %rdi      # 0x7260
xorl %esi, %esi
callq 0x2120
movl $0x10, 0x4aba(%rip)     # 0x7248
movq 0x4a53(%rip), %rax      # 0x71e8
subq %rax, %r13
sarq $0x5, %r13
cmpq %r15, %r13
jbe 0x2c3f
movl 0x4a6d(%rip), %edi      # 0x7218
movq (%rax,%r12), %rsi
movq 0x8(%rax,%r12), %rdx
xorl %ecx, %ecx
leaq 0x4a5f(%rip), %r8       # 0x721c
movl $0x10, %r9d
callq 0x2060
movq 0x4a21(%rip), %rdx      # 0x71f0
movq 0x4a12(%rip), %rcx      # 0x71e8
subq %rcx, %rdx
sarq $0x5, %rdx
cmpq %r15, %rdx
jbe 0x2c27
cmpq 0x8(%rcx,%r12), %rax
je 0x2828
movl $0xf, %edx
movq %rbx, %rdi
leaq 0x28ae(%rip), %rsi      # 0x50aa
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x5, %edi
callq 0x2310
callq 0x328b
movq 0x498a(%rip), %rsi      # 0x71c8
movq 0x498b(%rip), %rdx      # 0x71d0
movq %rbx, %rdi
callq 0x21f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq 0x4948(%rip), %rdx      # 0x71f0
movq 0x4939(%rip), %rdi      # 0x71e8
subq %rdi, %rdx
sarq $0x5, %rdx
cmpq %r15, %rdx
jbe 0x2c27
addq %r12, %rdi
movl $0x4, %ecx
leaq 0x27ec(%rip), %rsi      # 0x50ba
xorl %edx, %edx
callq 0x22e0
cmpq $-0x1, %rax
je 0x291f
movl $0x18, %edx
movq %rbx, %rdi
leaq 0x27d5(%rip), %rsi      # 0x50bf
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq -0x30(%rbp), %rdi
callq 0x2240
movq 0x48ca(%rip), %rdx      # 0x71f0
movq 0x48bb(%rip), %rdi      # 0x71e8
subq %rdi, %rdx
sarq $0x5, %rdx
cmpq %r15, %rdx
jbe 0x2c27
addq %r12, %rdi
movl $0x4, %ecx
leaq 0x278c(%rip), %rsi      # 0x50d8
xorl %edx, %edx
callq 0x22e0
cmpq $-0x1, %rax
je 0x29b6
movl $0x18, %edx
movq %rbx, %rdi
leaq 0x2775(%rip), %rsi      # 0x50dd
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
cvtss2sd 0xf8b24(%rip), %xmm0    # 0xfb4c0
divsd 0x2664(%rip), %xmm0     # 0x5008
mulsd 0x2664(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rdi
callq 0x2240
incq %r15
movq 0x4830(%rip), %r13      # 0x71f0
leaq (%r14,%r12), %rax
addq $0x20, %rax
addq $0x20, %r12
cmpq %r13, %rax
jne 0x2771
movq 0x4824(%rip), %r13      # 0x7200
movq 0x4825(%rip), %r12      # 0x7208
cmpq %r12, %r13
je 0x2c0a
movsd -0x38(%rbp), %xmm0
divsd 0x260f(%rip), %xmm0     # 0x5008
mulsd 0x260f(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movl $0xf4240, %edx          # imm = 0xF4240
leaq 0x4844(%rip), %rdi      # 0x7260
xorl %esi, %esi
callq 0x2120
movl $0x10, 0x481b(%rip)     # 0x7248
movq 0x47cc(%rip), %rax      # 0x7200
subq %rax, %r12
sarq $0x5, %r12
cmpq %r14, %r12
jbe 0x2c4e
movl 0x47ce(%rip), %edi      # 0x7218
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
xorl %ecx, %ecx
leaq 0x47c0(%rip), %r8       # 0x721c
movl $0x10, %r9d
callq 0x2060
movq 0x479a(%rip), %rdx      # 0x7208
movq 0x478b(%rip), %rcx      # 0x7200
subq %rcx, %rdx
sarq $0x5, %rdx
cmpq %r14, %rdx
jbe 0x2c33
cmpq 0x8(%rcx,%r15), %rax
je 0x2ac7
movl $0xf, %edx
movq %rbx, %rdi
leaq 0x260f(%rip), %rsi      # 0x50aa
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x5, %edi
callq 0x2310
callq 0x328b
movq 0x472b(%rip), %rdx      # 0x7208
movq 0x471c(%rip), %rdi      # 0x7200
subq %rdi, %rdx
sarq $0x5, %rdx
cmpq %r14, %rdx
jbe 0x2c33
addq %r15, %rdi
movl $0x4, %ecx
leaq 0x25b7(%rip), %rsi      # 0x50ba
xorl %edx, %edx
callq 0x22e0
cmpq $-0x1, %rax
je 0x2b54
movl $0x18, %edx
movq %rbx, %rdi
leaq 0x25d7(%rip), %rsi      # 0x50f6
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq -0x30(%rbp), %rdi
callq 0x2240
movq 0x46ad(%rip), %rdx      # 0x7208
movq 0x469e(%rip), %rdi      # 0x7200
subq %rdi, %rdx
sarq $0x5, %rdx
cmpq %r14, %rdx
jbe 0x2c33
addq %r15, %rdi
movl $0x4, %ecx
leaq 0x2557(%rip), %rsi      # 0x50d8
xorl %edx, %edx
callq 0x22e0
cmpq $-0x1, %rax
je 0x2beb
movl $0x18, %edx
movq %rbx, %rdi
leaq 0x2572(%rip), %rsi      # 0x510f
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
cvtss2sd 0xf88f3(%rip), %xmm0    # 0xfb4c4
divsd 0x242f(%rip), %xmm0     # 0x5008
mulsd 0x242f(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rdi
callq 0x2240
incq %r14
movq 0x4613(%rip), %r12      # 0x7208
leaq (%r15,%r13), %rax
addq $0x20, %rax
addq $0x20, %r15
cmpq %r12, %rax
jne 0x2a10
movq -0x40(%rbp), %rdi
callq 0x21c0
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25e8(%rip), %rdi      # 0x5216
movq %r15, %rsi
jmp 0x2c5b
leaq 0x25dc(%rip), %rdi      # 0x5216
movq %r14, %rsi
jmp 0x2c5b
leaq 0x25d0(%rip), %rdi      # 0x5216
movq %r15, %rsi
movq %r13, %rdx
jmp 0x2c5b
leaq 0x25c1(%rip), %rdi      # 0x5216
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2270