// File: /workspace/github_cpp_makefile_star2_O2/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 0x15b8
callq 0x15ec
movq %rax, %r14
cmpb $0x66, (%rax)
jne 0x1279
leaq 0xe0c(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1030
callq 0x161e
movsd %xmm0, -0x40(%rbp)
movsd %xmm0, -0x50(%rbp)
callq 0x161e
movsd %xmm0, -0x58(%rbp)
movsd %xmm0, -0x48(%rbp)
leaq 0xde7(%rip), %rsi       # 0x200b
movq %r14, %rdi
callq 0x10b0
testl %eax, %eax
je 0x12e1
leaq 0xdd5(%rip), %rsi       # 0x2010
movq %r14, %rdi
callq 0x10b0
testl %eax, %eax
je 0x12fd
leaq 0xdc3(%rip), %rsi       # 0x2015
movq %r14, %rdi
callq 0x10b0
testl %eax, %eax
je 0x1319
movsd -0x40(%rbp), %xmm0
movapd %xmm0, %xmm2
movsd -0x58(%rbp), %xmm1
divsd %xmm1, %xmm2
jmp 0x132b
leaq 0xdb7(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1030
callq 0x160b
movl %eax, %r15d
movl %eax, -0x28(%rbp)
callq 0x160b
movl %eax, %r12d
movl %eax, -0x24(%rbp)
leaq 0xd68(%rip), %rsi       # 0x200c
movq %r14, %rdi
callq 0x10b0
testl %eax, %eax
je 0x12f5
leaq 0xd5a(%rip), %rsi       # 0x2011
movq %r14, %rdi
callq 0x10b0
testl %eax, %eax
je 0x1311
leaq 0xd4c(%rip), %rsi       # 0x2016
movq %r14, %rdi
callq 0x10b0
testl %eax, %eax
je 0x1343
movl %r15d, %eax
cltd
idivl %r12d
movl %eax, %ebx
jmp 0x134a
movsd -0x58(%rbp), %xmm1
movapd %xmm1, %xmm2
movsd -0x40(%rbp), %xmm0
addsd %xmm0, %xmm2
jmp 0x132b
movl %r15d, %ebx
addl %r12d, %ebx
jmp 0x134a
movsd -0x40(%rbp), %xmm0
movapd %xmm0, %xmm2
movsd -0x58(%rbp), %xmm1
subsd %xmm1, %xmm2
jmp 0x132b
movl %r15d, %ebx
subl %r12d, %ebx
jmp 0x134a
movsd -0x58(%rbp), %xmm1
movapd %xmm1, %xmm2
movsd -0x40(%rbp), %xmm0
mulsd %xmm0, %xmm2
leaq 0xced(%rip), %rdi       # 0x201f
movq %r14, %rsi
movsd %xmm2, -0x40(%rbp)
movb $0x3, %al
callq 0x1030
jmp 0x1364
movl %r15d, %ebx
imull %r12d, %ebx
leaq 0xceb(%rip), %rdi       # 0x203c
movq %r14, %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
leaq -0x60(%rbp), %r14
andq $0x0, (%r14)
leaq -0x68(%rbp), %r15
andq $0x0, (%r15)
leaq 0xcd1(%rip), %rdi       # 0x204c
xorl %eax, %eax
callq 0x1030
movq 0x2c57(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x10a0
movq (%r14), %rsi
leaq 0xcbd(%rip), %rdi       # 0x205e
xorl %eax, %eax
callq 0x1030
movq (%r14), %rdi
leaq 0xcb7(%rip), %rsi       # 0x2069
leaq -0x32(%rbp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
leaq 0xca5(%rip), %rdi       # 0x206c
movq %r14, %rsi
xorl %eax, %eax
callq 0x1030
cmpb $0x66, (%r14)
jne 0x1489
leaq 0xc22(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rdi
leaq 0xc87(%rip), %rsi       # 0x207b
leaq -0x32(%rbp), %rbx
leaq -0x50(%rbp), %rcx
leaq -0x48(%rbp), %r8
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
leaq 0xbfa(%rip), %rsi       # 0x200b
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x1080
testl %eax, %eax
je 0x14f1
leaq 0xbe5(%rip), %rsi       # 0x2010
leaq -0x32(%rbp), %rdi
pushq $0x5
popq %rdx
callq 0x1080
testl %eax, %eax
je 0x150d
leaq 0xbcf(%rip), %rsi       # 0x2015
leaq -0x32(%rbp), %rdi
pushq $0x5
popq %rdx
callq 0x1080
testl %eax, %eax
je 0x1542
leaq 0xbb9(%rip), %rsi       # 0x201a
leaq -0x32(%rbp), %rdi
pushq $0x5
popq %rdx
callq 0x1080
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
testl %eax, %eax
je 0x155f
movsd -0x40(%rbp), %xmm2
jmp 0x1567
leaq 0xba7(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rdi
leaq 0xbe4(%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 0x1505
cmpl $0x627573, -0x32(%rbp)  # imm = 0x627573
je 0x1521
cmpl $0x6c756d, -0x32(%rbp)  # imm = 0x6C756D
je 0x1556
cmpl $0x766964, -0x32(%rbp)  # imm = 0x766964
je 0x157b
leaq 0xbba(%rip), %rdi       # 0x20a1
callq 0x10c0
jmp 0x1584
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
addsd %xmm1, %xmm2
jmp 0x1567
movl -0x24(%rbp), %ebx
addl -0x28(%rbp), %ebx
jmp 0x1584
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
jmp 0x1567
movl -0x28(%rbp), %edx
movl -0x24(%rbp), %ecx
movl %edx, %ebx
subl %ecx, %ebx
leaq 0xb5d(%rip), %rdi       # 0x208f
leaq -0x32(%rbp), %rsi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
jmp 0x1584
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
jmp 0x1567
movl -0x24(%rbp), %ebx
imull -0x28(%rbp), %ebx
jmp 0x1584
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
leaq 0xab1(%rip), %rdi       # 0x201f
leaq -0x32(%rbp), %rsi
movb $0x3, %al
callq 0x1030
jmp 0x159f
movl -0x28(%rbp), %eax
cltd
idivl -0x24(%rbp)
movl %eax, %ebx
movl -0x28(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0xaab(%rip), %rdi       # 0x203c
leaq -0x32(%rbp), %rsi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1090
xorl %eax, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop