// File: /workspace/github_cpp_makefile_star2_O3/BruceHu1005[P]np-assignment1/main.cpp
// Function: main
int main(int argc, char *argv[]){

  /* Initialize the library, this is needed for this library. */
  initCalcLib();
  char *ptr;
  ptr=randomType(); // Get a random arithemtic operator. 

  double f1,f2,fresult;
  int i1,i2,iresult;
  /*
  printf("ptr = %p, \t", ptr );
  printf("string = %s, \n", ptr );
  */

  /* Act differently depending on what operator you got, judge type by first char in string. If 'f' then a float */
  
  if(ptr[0]=='f'){
    printf("Float\t");
    f1=randomFloat();
    f2=randomFloat();

    /* At this point, ptr holds operator, f1 and f2 the operands. Now we work to determine the reference result. */
   
    if(strcmp(ptr,"fadd")==0){
      fresult=f1+f2;
    } else if (strcmp(ptr, "fsub")==0){
      fresult=f1-f2;
    } else if (strcmp(ptr, "fmul")==0){
      fresult=f1*f2;
    } else if (strcmp(ptr, "fdiv")==0){
      fresult=f1/f2;
    }
    printf("%s %8.8g %8.8g = %8.8g\n",ptr,f1,f2,fresult);
  } else {
    printf("Int\t");
    i1=randomInt();
    i2=randomInt();

    if(strcmp(ptr,"add")==0){
      iresult=i1+i2;
    } else if (strcmp(ptr, "sub")==0){
      iresult=i1-i2;
    } else if (strcmp(ptr, "mul")==0){
      iresult=i1*i2;
    } else if (strcmp(ptr, "div")==0){
      iresult=i1/i2;
    }

    printf("%s %d %d = %d \n",ptr,i1,i2,iresult);
  }

  /* This section shows how to read a line from stdin, process and do a similar operation as above. */
  
  char *lineBuffer=NULL;
  size_t lenBuffer=0;
  ssize_t nread=0;
  printf("Print a command: ");
  nread=getline(&lineBuffer,&lenBuffer,stdin);
  
  printf("got:> %s \n",lineBuffer);

  int rv;
  char command[10];

  
  rv=sscanf(lineBuffer,"%s",command);

  printf("Command: |%s|\n",command);
  
  if(command[0]=='f'){
    printf("Float\t");
    rv=sscanf(lineBuffer,"%s %lg %lg",command,&f1,&f2);
    if(strcmp(command,"fadd")==0){
      fresult=f1+f2;
    } else if (strcmp(command, "fsub")==0){
      fresult=f1-f2;
    } else if (strcmp(command, "fmul")==0){
      fresult=f1*f2;
    } else if (strcmp(command, "fdiv")==0){
      fresult=f1/f2;
    }
    printf("%s %8.8g %8.8g = %8.8g\n",command,f1,f2,fresult);
  } else {
    printf("Int\t");
    rv=sscanf(lineBuffer,"%s %d %d",command,&i1,&i2);
    if(strcmp(command,"add")==0){
      iresult=i1+i2;
    } else if (strcmp(command, "sub")==0){
      iresult=i1-i2;
      printf("[%s %d %d = %d ]\n",command,i1,i2,iresult);
    } else if (strcmp(command, "mul")==0){
      iresult=i1*i2;
    } else if (strcmp(command, "div")==0){
      
      iresult=i1/i2;
    } else {
      printf("No match\n");
    }

    printf("%s %d %d = %d \n",command,i1,i2,iresult);
  }
  

  

  free(lineBuffer); // This is needed for the getline() as it will allocate memory (if the provided buffer is NUL).
  

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
callq 0x158c
callq 0x15c0
movq %rax, %r14
cmpb $0x66, (%rax)
jne 0x1269
leaq 0xe1c(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1030
callq 0x160b
movsd %xmm0, -0x40(%rbp)
movsd %xmm0, -0x50(%rbp)
callq 0x160b
movsd %xmm0, -0x58(%rbp)
movsd %xmm0, -0x48(%rbp)
leaq 0xdf7(%rip), %rsi       # 0x200b
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12d1
leaq 0xde5(%rip), %rsi       # 0x2010
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12ed
leaq 0xdd3(%rip), %rsi       # 0x2015
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x1309
movsd -0x40(%rbp), %xmm0
movapd %xmm0, %xmm2
movsd -0x58(%rbp), %xmm1
divsd %xmm1, %xmm2
jmp 0x131b
leaq 0xdc7(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1030
callq 0x15e5
movl %eax, %r15d
movl %eax, -0x28(%rbp)
callq 0x15e5
movl %eax, %r12d
movl %eax, -0x24(%rbp)
leaq 0xd78(%rip), %rsi       # 0x200c
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12e5
leaq 0xd6a(%rip), %rsi       # 0x2011
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x1301
leaq 0xd5c(%rip), %rsi       # 0x2016
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x1333
movl %r15d, %eax
cltd
idivl %r12d
movl %eax, %ebx
jmp 0x133a
movsd -0x58(%rbp), %xmm1
movapd %xmm1, %xmm2
movsd -0x40(%rbp), %xmm0
addsd %xmm0, %xmm2
jmp 0x131b
movl %r15d, %ebx
addl %r12d, %ebx
jmp 0x133a
movsd -0x40(%rbp), %xmm0
movapd %xmm0, %xmm2
movsd -0x58(%rbp), %xmm1
subsd %xmm1, %xmm2
jmp 0x131b
movl %r15d, %ebx
subl %r12d, %ebx
jmp 0x133a
movsd -0x58(%rbp), %xmm1
movapd %xmm1, %xmm2
movsd -0x40(%rbp), %xmm0
mulsd %xmm0, %xmm2
leaq 0xcfd(%rip), %rdi       # 0x201f
movq %r14, %rsi
movsd %xmm2, -0x40(%rbp)
movb $0x3, %al
callq 0x1030
jmp 0x1354
movl %r15d, %ebx
imull %r12d, %ebx
leaq 0xcfb(%rip), %rdi       # 0x203c
movq %r14, %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
xorl %eax, %eax
leaq -0x60(%rbp), %r14
movq %rax, (%r14)
leaq -0x68(%rbp), %r15
movq %rax, (%r15)
leaq 0xce1(%rip), %rdi       # 0x204c
xorl %eax, %eax
callq 0x1030
movq 0x2c67(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq (%r14), %rsi
leaq 0xccd(%rip), %rdi       # 0x205e
xorl %eax, %eax
callq 0x1030
movq (%r14), %rdi
leaq 0xcc7(%rip), %rsi       # 0x2069
leaq -0x32(%rbp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
leaq 0xcb5(%rip), %rdi       # 0x206c
movq %r14, %rsi
xorl %eax, %eax
callq 0x1030
cmpb $0x66, (%r14)
jne 0x145d
leaq 0xc32(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rdi
leaq 0xc97(%rip), %rsi       # 0x207b
leaq -0x32(%rbp), %rbx
leaq -0x50(%rbp), %rcx
leaq -0x48(%rbp), %r8
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
movl $0x64646166, %eax       # imm = 0x64646166
xorl (%rbx), %eax
movzbl 0x4(%rbx), %ecx
orl %eax, %ecx
je 0x14c5
movl $0x62757366, %eax       # imm = 0x62757366
xorl -0x32(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
orl %eax, %ecx
je 0x14e1
movl $0x6c756d66, %eax       # imm = 0x6C756D66
xorl -0x32(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
orl %eax, %ecx
je 0x1516
movl $0x76696466, %eax       # imm = 0x76696466
xorl -0x32(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
orl %eax, %ecx
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
je 0x1533
movsd -0x40(%rbp), %xmm2
jmp 0x153b
leaq 0xbd3(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rdi
leaq 0xc10(%rip), %rsi       # 0x2086
leaq -0x28(%rbp), %rcx
leaq -0x24(%rbp), %r8
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
cmpl $0x646461, (%r14)       # imm = 0x646461
je 0x14d9
cmpl $0x627573, -0x32(%rbp)  # imm = 0x627573
je 0x14f5
cmpl $0x6c756d, -0x32(%rbp)  # imm = 0x6C756D
je 0x152a
cmpl $0x766964, -0x32(%rbp)  # imm = 0x766964
je 0x154f
leaq 0xbe6(%rip), %rdi       # 0x20a1
callq 0x10b0
jmp 0x1558
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
addsd %xmm1, %xmm2
jmp 0x153b
movl -0x24(%rbp), %ebx
addl -0x28(%rbp), %ebx
jmp 0x1558
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
jmp 0x153b
movl -0x28(%rbp), %edx
movl -0x24(%rbp), %ecx
movl %edx, %ebx
subl %ecx, %ebx
leaq 0xb89(%rip), %rdi       # 0x208f
leaq -0x32(%rbp), %rsi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
jmp 0x1558
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
jmp 0x153b
movl -0x24(%rbp), %ebx
imull -0x28(%rbp), %ebx
jmp 0x1558
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
leaq 0xadd(%rip), %rdi       # 0x201f
leaq -0x32(%rbp), %rsi
movb $0x3, %al
callq 0x1030
jmp 0x1573
movl -0x28(%rbp), %eax
cltd
idivl -0x24(%rbp)
movl %eax, %ebx
movl -0x28(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0xad7(%rip), %rdi       # 0x203c
leaq -0x32(%rbp), %rsi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1080
xorl %eax, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop