// File: /workspace/github_cpp_makefile_star2_O1/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 %rbx
subq $0x48, %rsp
callq 0x1558
callq 0x158c
movq %rax, %r14
cmpb $0x66, (%rax)
jne 0x125a
leaq 0xe22(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1030
callq 0x15d7
movsd %xmm0, -0x28(%rbp)
callq 0x15d7
movsd %xmm0, -0x40(%rbp)
movsd %xmm0, -0x48(%rbp)
leaq 0xe02(%rip), %rsi       # 0x200b
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12bf
leaq 0xdf0(%rip), %rsi       # 0x2010
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12d7
leaq 0xdde(%rip), %rsi       # 0x2015
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12ef
movsd -0x28(%rbp), %xmm2
movsd -0x40(%rbp), %xmm1
divsd %xmm1, %xmm2
jmp 0x12fd
leaq 0xdd6(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1030
callq 0x15b1
movl %eax, -0x1c(%rbp)
callq 0x15b1
movl %eax, %r15d
movl %eax, -0x20(%rbp)
leaq 0xd8a(%rip), %rsi       # 0x200c
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12cf
leaq 0xd7c(%rip), %rsi       # 0x2011
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x12e7
leaq 0xd6e(%rip), %rsi       # 0x2016
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x131a
movl -0x1c(%rbp), %eax
cltd
idivl %r15d
movl %eax, %ebx
jmp 0x1321
movsd -0x28(%rbp), %xmm2
movsd -0x40(%rbp), %xmm1
addsd %xmm1, %xmm2
jmp 0x12fd
movl -0x1c(%rbp), %ebx
addl %r15d, %ebx
jmp 0x1321
movsd -0x28(%rbp), %xmm2
movsd -0x40(%rbp), %xmm1
subsd %xmm1, %xmm2
jmp 0x12fd
movl -0x1c(%rbp), %ebx
subl %r15d, %ebx
jmp 0x1321
movsd -0x28(%rbp), %xmm2
movsd -0x40(%rbp), %xmm1
mulsd %xmm1, %xmm2
movsd -0x28(%rbp), %xmm0
leaq 0xd16(%rip), %rdi       # 0x201f
movq %r14, %rsi
movsd %xmm2, -0x40(%rbp)
movb $0x3, %al
callq 0x1030
jmp 0x133b
movl -0x1c(%rbp), %ebx
imull %r15d, %ebx
movl -0x1c(%rbp), %edx
leaq 0xd11(%rip), %rdi       # 0x203c
movq %r14, %rsi
movl %r15d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
xorl %eax, %eax
leaq -0x50(%rbp), %r14
movq %rax, (%r14)
leaq -0x58(%rbp), %r15
movq %rax, (%r15)
leaq 0xcfa(%rip), %rdi       # 0x204c
xorl %eax, %eax
callq 0x1030
movq 0x2c80(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq (%r14), %rsi
leaq 0xce6(%rip), %rdi       # 0x205e
xorl %eax, %eax
callq 0x1030
movq (%r14), %rdi
leaq 0xce0(%rip), %rsi       # 0x2069
leaq -0x32(%rbp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
leaq 0xcce(%rip), %rdi       # 0x206c
movq %r14, %rsi
xorl %eax, %eax
callq 0x1030
cmpb $0x66, (%r14)
jne 0x1444
leaq 0xc4b(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1030
movq -0x50(%rbp), %rdi
leaq 0xcb0(%rip), %rsi       # 0x207b
leaq -0x32(%rbp), %rbx
leaq -0x28(%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 0x14a9
movl $0x62757366, %eax       # imm = 0x62757366
xorl -0x32(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
orl %eax, %ecx
je 0x14bd
movl $0x6c756d66, %eax       # imm = 0x6C756D66
xorl -0x32(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
orl %eax, %ecx
je 0x14ea
movl $0x76696466, %eax       # imm = 0x76696466
xorl -0x32(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
orl %eax, %ecx
movsd -0x40(%rbp), %xmm2
jne 0x14f4
movsd -0x28(%rbp), %xmm2
divsd -0x48(%rbp), %xmm2
jmp 0x14f4
leaq 0xbec(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1030
movq -0x50(%rbp), %rdi
leaq 0xc29(%rip), %rsi       # 0x2086
leaq -0x1c(%rbp), %rcx
leaq -0x20(%rbp), %r8
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
cmpl $0x646461, (%r14)       # imm = 0x646461
je 0x14b5
cmpl $0x627573, -0x32(%rbp)  # imm = 0x627573
je 0x14c9
cmpl $0x6c756d, -0x32(%rbp)  # imm = 0x6C756D
je 0x1512
cmpl $0x766964, -0x32(%rbp)  # imm = 0x766964
je 0x151b
leaq 0xbff(%rip), %rdi       # 0x20a1
callq 0x10b0
jmp 0x1524
movsd -0x28(%rbp), %xmm2
addsd -0x48(%rbp), %xmm2
jmp 0x14f4
movl -0x20(%rbp), %ebx
addl -0x1c(%rbp), %ebx
jmp 0x1524
movsd -0x28(%rbp), %xmm2
subsd -0x48(%rbp), %xmm2
jmp 0x14f4
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl %edx, %ebx
subl %ecx, %ebx
leaq 0xbb5(%rip), %rdi       # 0x208f
leaq -0x32(%rbp), %rsi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
jmp 0x1524
movsd -0x28(%rbp), %xmm2
mulsd -0x48(%rbp), %xmm2
movsd -0x28(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq 0xb1a(%rip), %rdi       # 0x201f
leaq -0x32(%rbp), %rsi
movb $0x3, %al
callq 0x1030
jmp 0x153f
movl -0x20(%rbp), %ebx
imull -0x1c(%rbp), %ebx
jmp 0x1524
movl -0x1c(%rbp), %eax
cltd
idivl -0x20(%rbp)
movl %eax, %ebx
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
leaq 0xb0b(%rip), %rdi       # 0x203c
leaq -0x32(%rbp), %rsi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1030
movq -0x50(%rbp), %rdi
callq 0x1080
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)