// File: /workspace/github_cpp_makefile_star2_O2/BruceHu1005[P]np-assignment1/clientmain.cpp
// Function: main
int main(int argc, char *argv[]){
  
  /* Do magic */
  int sock_fd, send_num, recv_num;
  struct addrinfo hints, *servinfo, *p;
  char send_buf[BUFF_LEN];
  char recv_buf[BUFF_LEN];

  if (argc != 2) {
	  fprintf(stderr,"usage: %s hostname (%d)\n",argv[0],argc);
	  exit(1);
	}
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  

  if ((recv_num = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(recv_num));
        return 1;
  }

  for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sock_fd = socket(p->ai_family, p->ai_socktype,
      p->ai_protocol)) == -1) {
      perror("client: socket");
      continue;
    }

    if (connect(sock_fd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sock_fd);
      perror("client: connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "client: failed to connect\n");
    return 2;
  }
  recv_num = recv(sock_fd, recv_buf, sizeof(recv_buf), 0);
  if(recv_num<0 ){
  perror("recvfrom error:");
  exit(1);
  }
  else {
    recv_buf[recv_num] = '\0';
    printf("client receive %d bytes: %s\n", recv_num, recv_buf);
  } 
  
  std::cin >> send_buf;
  send_num=send(sock_fd,send_buf, strlen(send_buf), 0);
  recv_num = recv(sock_fd, recv_buf, sizeof(recv_buf), 0);
  char dis[recv_num];
  sprintf(dis, "%s", "disconnected");
  recv_buf[recv_num] = '\0';
  printf("client receive %d bytes: %s\n", recv_num, recv_buf);
  
  if (strcmp(recv_buf, dis) == 0) {
    close(sock_fd);
    return 0;
  }
  
  string temp = recv_buf;
  string calc[3];
  istringstream is(temp);
  is>>calc[0]>>calc[1]>>calc[2];
  string ptr = calc[0];
  string num1 = calc[1];
  string num2 = calc[2]; 
 
  if(ptr[0]=='f'){
    if (ptr == "fadd")
    {
      sprintf(send_buf, "%8.8g\n", stod(num1) + stod(num2));
    }
    else if (ptr == "fsub")
    {
      sprintf(send_buf, "%8.8g\n", stod(num1) - stod(num2));
    }
    else if (ptr == "fmul")
    {
      sprintf(send_buf, "%8.8g\n", stod(num1) * stod(num2));
    }
    else if (ptr == "fdiv")
    {
      sprintf(send_buf, "%8.8g\n", stod(num1) / stod(num2));
    }
  }
  else{
    if (ptr == "add")
    {
      sprintf(send_buf, "%d\n", stoi(num1) + stoi(num2));
    }
    else if (ptr == "sub")
    {
      sprintf(send_buf, "%d\n", stoi(num1) - stoi(num2));
    }
    else if (ptr == "mul")
    {
      sprintf(send_buf, "%d\n", stoi(num1) * stoi(num2));
    }
    else if (ptr == "div")
    {
      sprintf(send_buf, "%d\n", stoi(num1) / stoi(num2));
    }
  }
  printf("Answer of Client is %s\n", send_buf);
  send_num=send(sock_fd, send_buf, strlen(send_buf), 0);
  recv_num = recv(sock_fd, recv_buf, sizeof(recv_buf), 0);
  recv_buf[recv_num] = '\0';
  printf("client receive %d bytes: %s\n", recv_num, recv_buf);

  close(sock_fd);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6a8, %rsp            # imm = 0x6A8
cmpl $0x2, %edi
jne 0x2a10
xorpd %xmm0, %xmm0
leaq -0x150(%rbp), %rdx
movapd %xmm0, (%rdx)
movapd %xmm0, 0x20(%rdx)
movapd %xmm0, 0x10(%rdx)
movl $0x1, 0x8(%rdx)
movq 0x8(%rsi), %rdi
leaq 0xbd8(%rip), %rsi       # 0x303d
leaq -0xc0(%rbp), %r14
movq %r14, %rcx
callq 0x2170
testl %eax, %eax
jne 0x284f
leaq 0xbd0(%rip), %r15       # 0x3053
leaq 0xbd8(%rip), %r12       # 0x3062
movq (%r14), %r14
testq %r14, %r14
je 0x29ed
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x2060
movq %r15, %rdi
cmpl $-0x1, %eax
je 0x24cf
movl %eax, %ebx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %eax, %edi
callq 0x2110
cmpl $-0x1, %eax
jne 0x24da
movl %ebx, %edi
callq 0x2240
movq %r12, %rdi
callq 0x2120
addq $0x28, %r14
jmp 0x248a
leaq -0x550(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2090
testl %eax, %eax
js 0x2a2f
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r13d, %r13d
movb %r13b, -0x550(%rbp,%rcx)
leaq 0xb8c(%rip), %r15       # 0x309d
leaq -0x550(%rbp), %r14
movq %r15, %rdi
movl %eax, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2040
movq 0x2ab2(%rip), %rdi      # 0x4fe0
leaq -0x350(%rbp), %r12
movq %r12, %rsi
callq 0x2ab8
movq %r12, %rdi
callq 0x20a0
movl %ebx, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20d0
movl $0x200, %edx            # imm = 0x200
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2090
movq %rsp, -0x98(%rbp)
movl %eax, %ecx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdx
movq %rdx, %r12
subq %rcx, %r12
negq %rcx
movq %r12, %rsp
movabsq $0x656e6e6f63736964, %rsi # imm = 0x656E6E6F63736964
movq %rsi, (%rdx,%rcx)
movabsq $0x64657463656e6e, %rcx # imm = 0x64657463656E6E
movq %rcx, 0x5(%r12)
movslq %eax, %rsi
movb %r13b, -0x550(%rbp,%rsi)
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
movq %r12, %rsi
callq 0x21b0
testl %eax, %eax
je 0x26d0
leaq -0xb8(%rbp), %rdi
leaq -0x550(%rbp), %rsi
leaq -0x6d0(%rbp), %rdx
callq 0x2ada
pushq $0x10
popq %rax
leaq (%rax,%rbp), %rcx
addq $-0x120, %rcx           # imm = 0xFEE0
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq $0x20, %rax
cmpq $0x70, %rax
jne 0x25ea
leaq -0x6d0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x21f0
leaq -0x6d0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x2180
leaq -0x100(%rbp), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
leaq -0xe0(%rbp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
leaq -0x50(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x2190
leaq -0x90(%rbp), %rdi
movq %r14, %rsi
callq 0x2190
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
callq 0x2190
movq -0x50(%rbp), %rax
cmpb $0x66, (%rax)
jne 0x26dc
leaq 0x982(%rip), %rsi       # 0x3015
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x272a
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd %xmm0, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
jmp 0x28b7
movl %ebx, %edi
callq 0x2240
jmp 0x298e
leaq 0x933(%rip), %rsi       # 0x3016
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x276e
leaq -0x90(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
movl %eax, %r14d
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
addl %r14d, %eax
leaq 0x9a5(%rip), %rsi       # 0x30c1
leaq -0x350(%rbp), %rdi
movl %eax, %edx
jmp 0x27bc
leaq 0x8df(%rip), %rsi       # 0x3010
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x27c8
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd %xmm0, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd -0x30(%rbp), %xmm1
subsd %xmm0, %xmm1
jmp 0x28b7
leaq 0x89c(%rip), %rsi       # 0x3011
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x280c
leaq -0x90(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
movl %eax, %r14d
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
subl %eax, %r14d
leaq 0x90f(%rip), %rsi       # 0x30c1
leaq -0x350(%rbp), %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2050
jmp 0x28d0
leaq 0x850(%rip), %rsi       # 0x301f
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x287c
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd %xmm0, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd -0x30(%rbp), %xmm1
mulsd %xmm0, %xmm1
jmp 0x28b7
leaq 0x80d(%rip), %rsi       # 0x3020
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x29a6
leaq -0x90(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
movl %eax, %r14d
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
imull %r14d, %eax
jmp 0x2715
movq 0x2772(%rip), %rcx      # 0x4fc8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x21a0
leaq 0x7db(%rip), %rsi       # 0x3042
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
pushq $0x1
popq %rax
jmp 0x2997
leaq 0x797(%rip), %rsi       # 0x301a
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x28d0
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd %xmm0, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x2b25
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
leaq 0x7fc(%rip), %rsi       # 0x30ba
leaq -0x350(%rbp), %rdi
movapd %xmm1, %xmm0
movb $0x1, %al
callq 0x2050
leaq 0x7ee(%rip), %rdi       # 0x30c5
leaq -0x350(%rbp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x20a0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20d0
leaq -0x550(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2090
movslq %eax, %rsi
movb $0x0, -0x550(%rbp,%rsi)
leaq 0x777(%rip), %rdi       # 0x309d
leaq -0x550(%rbp), %rdx
xorl %eax, %eax
callq 0x2040
movl %ebx, %edi
callq 0x2240
leaq -0x70(%rbp), %rdi
callq 0x20b0
leaq -0x90(%rbp), %rdi
callq 0x20b0
leaq -0x50(%rbp), %rdi
callq 0x20b0
leaq -0x6d0(%rbp), %rdi
callq 0x20c0
pushq $0x40
popq %rbx
leaq (%rbx,%rbp), %rdi
addq $-0x120, %rdi           # imm = 0xFEE0
callq 0x20b0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x2968
leaq -0xb8(%rbp), %rdi
callq 0x20b0
movq -0x98(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66e(%rip), %rsi       # 0x301b
leaq -0x50(%rbp), %rdi
callq 0x2b15
testb %al, %al
je 0x28d0
leaq -0x90(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
movl %eax, %r14d
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2b43
movl %eax, %ecx
movl %r14d, %eax
cltd
idivl %ecx
jmp 0x2715
movq 0x25d4(%rip), %rax      # 0x4fc8
movq (%rax), %rcx
leaq 0x674(%rip), %rdi       # 0x3072
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
pushq $0x2
jmp 0x2876
movl %edi, %ecx
movq 0x25af(%rip), %rax      # 0x4fc8
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0x5fe(%rip), %rsi       # 0x3024
xorl %eax, %eax
callq 0x21c0
jmp 0x2a3b
leaq 0x657(%rip), %rdi       # 0x308d
callq 0x2120
pushq $0x1
popq %rdi
callq 0x2160
movq %rax, %rbx
jmp 0x2a65
movq %rax, %rbx
jmp 0x2a71
jmp 0x2a54
movq %rax, %rbx
jmp 0x2a86
movq %rax, %rbx
jmp 0x2a7a
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x20b0
leaq -0x90(%rbp), %rdi
callq 0x20b0
leaq -0x50(%rbp), %rdi
callq 0x20b0
leaq -0x6d0(%rbp), %rdi
callq 0x20c0
pushq $0x40
popq %r14
leaq (%r14,%rbp), %rdi
addq $-0x120, %rdi           # imm = 0xFEE0
callq 0x20b0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x2a8a
leaq -0xb8(%rbp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2210