// File: 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
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x1620
callq 0x1670
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x66, %eax
jne 0x12c8
leaq 0xe19(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1030
callq 0x16c0
movsd %xmm0, -0x20(%rbp)
callq 0x16c0
movsd %xmm0, -0x28(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xdfa(%rip), %rsi       # 0x200b
callq 0x10a0
cmpl $0x0, %eax
jne 0x122c
movsd -0x20(%rbp), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x12a2
movq -0x18(%rbp), %rdi
leaq 0xdd9(%rip), %rsi       # 0x2010
callq 0x10a0
cmpl $0x0, %eax
jne 0x1252
movsd -0x20(%rbp), %xmm0
subsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x12a0
movq -0x18(%rbp), %rdi
leaq 0xdb8(%rip), %rsi       # 0x2015
callq 0x10a0
cmpl $0x0, %eax
jne 0x1278
movsd -0x20(%rbp), %xmm0
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x129e
movq -0x18(%rbp), %rdi
leaq 0xd97(%rip), %rsi       # 0x201a
callq 0x10a0
cmpl $0x0, %eax
jne 0x129c
movsd -0x20(%rbp), %xmm0
divsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x129e
jmp 0x12a0
jmp 0x12a2
movq -0x18(%rbp), %rsi
movsd -0x20(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
movsd -0x30(%rbp), %xmm2
leaq 0xd63(%rip), %rdi       # 0x201f
movb $0x3, %al
callq 0x1030
jmp 0x1388
leaq 0xd68(%rip), %rdi       # 0x2037
movb $0x0, %al
callq 0x1030
callq 0x16a0
movl %eax, -0x34(%rbp)
callq 0x16a0
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xd1b(%rip), %rsi       # 0x200c
callq 0x10a0
cmpl $0x0, %eax
jne 0x1306
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x136c
movq -0x18(%rbp), %rdi
leaq 0xd00(%rip), %rsi       # 0x2011
callq 0x10a0
cmpl $0x0, %eax
jne 0x1326
movl -0x34(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x136a
movq -0x18(%rbp), %rdi
leaq 0xce5(%rip), %rsi       # 0x2016
callq 0x10a0
cmpl $0x0, %eax
jne 0x1347
movl -0x34(%rbp), %eax
imull -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x1368
movq -0x18(%rbp), %rdi
leaq 0xcc9(%rip), %rsi       # 0x201b
callq 0x10a0
cmpl $0x0, %eax
jne 0x1366
movl -0x34(%rbp), %eax
cltd
idivl -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1368
jmp 0x136a
jmp 0x136c
movq -0x18(%rbp), %rsi
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
movl -0x3c(%rbp), %r8d
leaq 0xcbb(%rip), %rdi       # 0x203c
movb $0x0, %al
callq 0x1030
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
leaq 0xca5(%rip), %rdi       # 0x204c
movb $0x0, %al
callq 0x1030
movq 0x2c2b(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1090
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rsi
leaq 0xc8a(%rip), %rdi       # 0x205e
movb $0x0, %al
callq 0x1030
movq -0x48(%rbp), %rdi
leaq -0x66(%rbp), %rdx
leaq 0xc7f(%rip), %rsi       # 0x2069
movb $0x0, %al
callq 0x1070
movl %eax, -0x5c(%rbp)
leaq -0x66(%rbp), %rsi
leaq 0xc6d(%rip), %rdi       # 0x206c
movb $0x0, %al
callq 0x1030
movsbl -0x66(%rbp), %eax
cmpl $0x66, %eax
jne 0x1504
leaq 0xbea(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1030
movq -0x48(%rbp), %rdi
leaq -0x66(%rbp), %rdx
leaq 0xc4b(%rip), %rsi       # 0x207b
leaq -0x20(%rbp), %rcx
leaq -0x28(%rbp), %r8
movb $0x0, %al
callq 0x1070
movl %eax, -0x5c(%rbp)
leaq -0x66(%rbp), %rdi
leaq 0xbbe(%rip), %rsi       # 0x200b
callq 0x10a0
cmpl $0x0, %eax
jne 0x1468
movsd -0x20(%rbp), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x14de
leaq -0x66(%rbp), %rdi
leaq 0xb9d(%rip), %rsi       # 0x2010
callq 0x10a0
cmpl $0x0, %eax
jne 0x148e
movsd -0x20(%rbp), %xmm0
subsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x14dc
leaq -0x66(%rbp), %rdi
leaq 0xb7c(%rip), %rsi       # 0x2015
callq 0x10a0
cmpl $0x0, %eax
jne 0x14b4
movsd -0x20(%rbp), %xmm0
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x14da
leaq -0x66(%rbp), %rdi
leaq 0xb5b(%rip), %rsi       # 0x201a
callq 0x10a0
cmpl $0x0, %eax
jne 0x14d8
movsd -0x20(%rbp), %xmm0
divsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x14da
jmp 0x14dc
jmp 0x14de
leaq -0x66(%rbp), %rsi
movsd -0x20(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
movsd -0x30(%rbp), %xmm2
leaq 0xb27(%rip), %rdi       # 0x201f
movb $0x3, %al
callq 0x1030
jmp 0x1604
leaq 0xb2c(%rip), %rdi       # 0x2037
movb $0x0, %al
callq 0x1030
movq -0x48(%rbp), %rdi
leaq -0x66(%rbp), %rdx
leaq 0xb65(%rip), %rsi       # 0x2086
leaq -0x34(%rbp), %rcx
leaq -0x38(%rbp), %r8
movb $0x0, %al
callq 0x1070
movl %eax, -0x5c(%rbp)
leaq -0x66(%rbp), %rdi
leaq 0xace(%rip), %rsi       # 0x200c
callq 0x10a0
cmpl $0x0, %eax
jne 0x1556
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x15e8
leaq -0x66(%rbp), %rdi
leaq 0xab0(%rip), %rsi       # 0x2011
callq 0x10a0
cmpl $0x0, %eax
jne 0x1592
movl -0x34(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
leaq -0x66(%rbp), %rsi
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
movl -0x3c(%rbp), %r8d
leaq 0xb06(%rip), %rdi       # 0x208f
movb $0x0, %al
callq 0x1030
jmp 0x15e6
leaq -0x66(%rbp), %rdi
leaq 0xa79(%rip), %rsi       # 0x2016
callq 0x10a0
cmpl $0x0, %eax
jne 0x15b3
movl -0x34(%rbp), %eax
imull -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x15e4
leaq -0x66(%rbp), %rdi
leaq 0xa5d(%rip), %rsi       # 0x201b
callq 0x10a0
cmpl $0x0, %eax
jne 0x15d4
movl -0x34(%rbp), %eax
cltd
idivl -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x15e2
leaq 0xac6(%rip), %rdi       # 0x20a1
movb $0x0, %al
callq 0x1030
jmp 0x15e4
jmp 0x15e6
jmp 0x15e8
leaq -0x66(%rbp), %rsi
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
movl -0x3c(%rbp), %r8d
leaq 0xa3f(%rip), %rdi       # 0x203c
movb $0x0, %al
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1080
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)