// File: /workspace/github_c_makefile_star2_O1/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_4/exercise_4_03/calculator.c
// Function: main
int main(void)
{
  int type;
  double op2;
  char s[MAXOP];

  while ((type = getop(s)) != EOF)
  {
    switch (type)
    {
    case NUMBER:
      push(atof(s));
      break;

    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;

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

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

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
leaq 0x10(%rsp), %rbx
leaq 0x2eda(%rip), %r14      # 0x4070
leaq 0xf05(%rip), %r15       # 0x20a2
leaq 0xeaa(%rip), %r12       # 0x204e
leaq 0xe65(%rip), %rbp       # 0x2010
leaq 0xeb8(%rip), %r13       # 0x206a
movq %rbx, %rdi
callq 0x14a2
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x11ed
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movl 0x2e8e(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13c7
decl %eax
movl %eax, 0x2e7e(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x13d3
cmpl $0xa, %eax
je 0x129f
cmpl $-0x1, %eax
je 0x1491
movq %r12, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
jmp 0x11b2
movl 0x2e4c(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x12c0
decl %eax
movl %eax, 0x2e3c(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x12cc
movl 0x2e2b(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1307
decl %eax
movl %eax, 0x2e1b(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x1313
movl 0x2e0a(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x134e
decl %eax
movl %eax, 0x2dfa(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x135a
movl 0x2de9(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1395
decl %eax
movl %eax, 0x2dd9(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x13a1
movq %rbx, %rdi
callq 0x1040
jmp 0x1461
movl 0x2dbb(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x140a
decl %eax
movl %eax, 0x2dab(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x1416
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
movl 0x2d8e(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x12e6
decl %eax
movl %eax, 0x2d82(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x12fe
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
addsd %xmm1, %xmm0
jmp 0x1461
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2d47(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x132d
decl %eax
movl %eax, 0x2d3b(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x1345
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
subsd %xmm1, %xmm0
jmp 0x1461
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
movl 0x2d00(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1374
decl %eax
movl %eax, 0x2cf4(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x138c
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
mulsd %xmm1, %xmm0
jmp 0x1461
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
ucomisd 0xc5f(%rip), %xmm1      # 0x2008
jne 0x13ad
jnp 0x13f9
movl 0x2cad(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x141f
decl %eax
movl %eax, 0x2ca1(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x1437
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
ucomisd 0xc2d(%rip), %xmm0      # 0x2008
jne 0x13df
jnp 0x13f9
movl 0x2c7b(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x143d
decl %eax
movl %eax, 0x2c6f(%rip)      # 0x4060
cvttsd2si (%r14,%rax,8), %eax
jmp 0x1453
leaq 0xc8d(%rip), %rdi       # 0x208d
callq 0x1030
jmp 0x11b2
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
leaq 0xc23(%rip), %rdi       # 0x2040
jmp 0x1485
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
divsd %xmm1, %xmm0
jmp 0x1461
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorl %eax, %eax
cvttsd2si %xmm0, %ecx
cltd
idivl %ecx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movslq 0x2bf8(%rip), %rax      # 0x4060
cmpq $0x63, %rax
jg 0x1482
leal 0x1(%rax), %ecx
movl %ecx, 0x2be9(%rip)      # 0x4060
movsd %xmm0, (%r14,%rax,8)
jmp 0x11b2
movq %r13, %rdi
movb $0x1, %al
callq 0x1050
jmp 0x11b2
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq