// File: /workspace/github_c_makefile_star2_O3/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_7/exercise_7_05/calculator.c
// Function: main
int main(int argc, char *argv[])
{
  char c;
  double op2;
  char str[MAX_STR_LEN];

  while (scanf("%s", str) != EOF)
  {
    if (sscanf(str, "%lf", &op2) == 1)
    {
      push(op2);
    }
    else if (sscanf(str, "%c", &c) == 1)
    {
      switch (c)
      {
      case '+':
        push(pop() + pop());
        break;

      case '-':
        op2 = pop();
        push(pop() - op2);
        break;

      case '*':
        push(pop() * pop());
        break;

      case '/':
        op2 = pop();

        if (op2 != 0.0)
        {
          push(pop() / op2);
        }
        else
        {
          printf("Error: zero divisor.\n");
        }
        break;

      case '%':
        op2 = pop();

        if (op2 != 0.0)
        {
          push((int)pop() % (int)op2);
        }
        else
        {
          printf("Error: zero divisor.\n");
        }
        break;

      default:
        printf("Error: unknown command.\n");
        break;
      }
    }
  }

  printf("result: %.8g\n", pop());

  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
leaq 0xeb8(%rip), %rdi       # 0x203c
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1060
cmpl $-0x1, %eax
je 0x14f1
leaq 0xe9f(%rip), %rbx       # 0x203f
leaq 0x20(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0x2ebf(%rip), %rbp      # 0x4070
leaq 0xe84(%rip), %r13       # 0x203c
leaq 0xf(%rsp), %r12
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1050
cmpl $0x1, %eax
jne 0x11f7
movsd 0x10(%rsp), %xmm0
movslq 0x2e81(%rip), %rax      # 0x4060
cmpq $0x63, %rax
jg 0x14cd
leal 0x1(%rax), %ecx
movl %ecx, 0x2e6e(%rip)      # 0x4060
jmp 0x14c5
movq %r14, %rdi
leaq 0xe42(%rip), %rsi       # 0x2043
movq %r12, %rdx
xorl %eax, %eax
callq 0x1050
cmpl $0x1, %eax
jne 0x14db
movzbl 0xf(%rsp), %eax
addl $-0x25, %eax
cmpl $0xa, %eax
ja 0x128c
leaq 0xde8(%rip), %rcx       # 0x2010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x2e29(%rip), %ecx      # 0x4060
testl %ecx, %ecx
jle 0x1350
leal -0x1(%rcx), %eax
movl %eax, 0x2e18(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rsp)
ucomisd 0xdac(%rip), %xmm0      # 0x2008
jne 0x1264
jnp 0x1365
cmpl $0x1, %ecx
jne 0x1472
leaq 0xe30(%rip), %rdi       # 0x20a4
callq 0x1030
movl 0x2de1(%rip), %ecx      # 0x4060
xorl %eax, %eax
movsd 0x10(%rsp), %xmm0
jmp 0x1481
leaq 0xdf9(%rip), %rdi       # 0x208c
jmp 0x136c
movl 0x2dc2(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1376
decl %eax
movl %eax, 0x2db2(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm0
jmp 0x138c
movl 0x2da1(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13cb
decl %eax
movl %eax, 0x2d91(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm0
jmp 0x13e1
movl 0x2d80(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1420
decl %eax
movl %eax, 0x2d70(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm1
jmp 0x1436
movl 0x2d5f(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1350
leal -0x1(%rax), %ecx
movl %ecx, 0x2d52(%rip)      # 0x4060
movsd (%rbp,%rcx,8), %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd 0xce6(%rip), %xmm1      # 0x2008
jne 0x1326
jnp 0x1365
cmpl $0x1, %eax
jne 0x14a2
leaq 0xd6e(%rip), %rdi       # 0x20a4
callq 0x1030
movl 0x2d1f(%rip), %eax      # 0x4060
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
jmp 0x14b1
leaq 0xd4d(%rip), %rdi       # 0x20a4
callq 0x1030
movq $0x0, 0x10(%rsp)
leaq 0xd0b(%rip), %rdi       # 0x2077
callq 0x1030
jmp 0x14db
leaq 0xd27(%rip), %rdi       # 0x20a4
callq 0x1030
xorpd %xmm0, %xmm0
movl 0x2cd4(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13a0
decl %eax
movl %eax, 0x2cc8(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm1
jmp 0x13c2
leaq 0xcfd(%rip), %rdi       # 0x20a4
movsd %xmm0, 0x18(%rsp)
callq 0x1030
movsd 0x18(%rsp), %xmm0
xorpd %xmm1, %xmm1
movl 0x2c9e(%rip), %eax      # 0x4060
addsd %xmm1, %xmm0
jmp 0x14b5
leaq 0xcd2(%rip), %rdi       # 0x20a4
callq 0x1030
xorpd %xmm0, %xmm0
movl 0x2c7f(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13f5
decl %eax
movl %eax, 0x2c73(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm1
jmp 0x1417
leaq 0xca8(%rip), %rdi       # 0x20a4
movsd %xmm0, 0x18(%rsp)
callq 0x1030
movsd 0x18(%rsp), %xmm0
xorpd %xmm1, %xmm1
movl 0x2c49(%rip), %eax      # 0x4060
mulsd %xmm1, %xmm0
jmp 0x14b5
leaq 0xc7d(%rip), %rdi       # 0x20a4
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2c2a(%rip), %eax      # 0x4060
movsd %xmm1, 0x10(%rsp)
testl %eax, %eax
jle 0x1450
decl %eax
movl %eax, 0x2c18(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm0
jmp 0x146c
leaq 0xc4d(%rip), %rdi       # 0x20a4
callq 0x1030
movl 0x2bfe(%rip), %eax      # 0x4060
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
subsd %xmm1, %xmm0
jmp 0x14b5
addl $-0x2, %ecx
movl %ecx, 0x2be5(%rip)      # 0x4060
cvttsd2si (%rbp,%rcx,8), %eax
cvttsd2si %xmm0, %esi
cltd
idivl %esi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
cmpl $0x63, %ecx
jg 0x14cd
leal 0x1(%rcx), %eax
movl %eax, 0x2bc3(%rip)      # 0x4060
movslq %ecx, %rax
jmp 0x14c5
addl $-0x2, %eax
movl %eax, 0x2bb5(%rip)      # 0x4060
movsd (%rbp,%rax,8), %xmm0
divsd %xmm1, %xmm0
cmpl $0x63, %eax
jg 0x14cd
leal 0x1(%rax), %ecx
movl %ecx, 0x2b9d(%rip)      # 0x4060
cltq
movsd %xmm0, (%rbp,%rax,8)
jmp 0x14db
leaq 0xb80(%rip), %rdi       # 0x2054
movb $0x1, %al
callq 0x1040
movq %r13, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1060
cmpl $-0x1, %eax
jne 0x11bd
movl 0x2b69(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1511
decl %eax
movl %eax, 0x2b5d(%rip)      # 0x4060
leaq 0x2b66(%rip), %rcx      # 0x4070
movsd (%rcx,%rax,8), %xmm0
jmp 0x1521
leaq 0xb8c(%rip), %rdi       # 0x20a4
callq 0x1030
xorpd %xmm0, %xmm0
leaq 0xb1e(%rip), %rdi       # 0x2046
movb $0x1, %al
callq 0x1040
xorl %eax, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq