// File: /workspace/github_c_makefile_star2_O3/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 0xe6c(%rip), %r12       # 0x2010
leaq 0xebf(%rip), %r13       # 0x206a
leaq 0xedb(%rip), %rbp       # 0x208d
movq %rbx, %rdi
callq 0x14de
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x1222
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movl 0x2e8f(%rip), %ecx      # 0x4060
testl %ecx, %ecx
jle 0x132b
leal -0x1(%rcx), %eax
movl %eax, 0x2e7e(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
ucomisd 0xe18(%rip), %xmm0      # 0x2008
jne 0x11f8
jnp 0x1333
cmpl $0x1, %ecx
jne 0x1485
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorl %eax, %eax
movl 0x2e43(%rip), %ecx      # 0x4060
jmp 0x1494
cmpl $0xa, %eax
je 0x1340
cmpl $-0x1, %eax
je 0x14cd
leaq 0xe13(%rip), %rdi       # 0x204e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
jmp 0x11b2
movl 0x2e10(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1361
decl %eax
movl %eax, 0x2e00(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x1373
movl 0x2def(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13ae
decl %eax
movl %eax, 0x2ddf(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x13c0
movl 0x2dce(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13f8
decl %eax
movl %eax, 0x2dbe(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x140a
movl 0x2dad(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x132b
leal -0x1(%rax), %ecx
movl %ecx, 0x2da0(%rip)      # 0x4060
movsd (%r14,%rcx,8), %xmm1
ucomisd 0xd3a(%rip), %xmm1      # 0x2008
jne 0x12d2
jnp 0x1333
cmpl $0x1, %eax
jne 0x1457
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2d67(%rip), %eax      # 0x4060
jmp 0x1466
movq %rbx, %rdi
callq 0x1040
movslq 0x2d53(%rip), %rax      # 0x4060
cmpq $0x63, %rax
jg 0x14be
leal 0x1(%rax), %ecx
movl %ecx, 0x2d40(%rip)      # 0x4060
movsd %xmm0, (%r14,%rax,8)
jmp 0x11b2
movq %r15, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
jmp 0x11b2
movl 0x2d1a(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1442
decl %eax
movl %eax, 0x2d0a(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x144e
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
movl 0x2ced(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x1387
decl %eax
movl %eax, 0x2ce1(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x13a5
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
movl 0x2cbb(%rip), %eax      # 0x4060
addsd %xmm1, %xmm0
jmp 0x146a
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2ca0(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x13d4
decl %eax
movl %eax, 0x2c94(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
jmp 0x13f2
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2c6e(%rip), %eax      # 0x4060
subsd %xmm1, %xmm0
jmp 0x146a
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
movl 0x2c56(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x141e
decl %eax
movl %eax, 0x2c4a(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm1
jmp 0x143c
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
movl 0x2c24(%rip), %eax      # 0x4060
mulsd %xmm1, %xmm0
jmp 0x146a
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
leaq 0xbeb(%rip), %rdi       # 0x2040
jmp 0x14c1
addl $-0x2, %eax
movl %eax, 0x2c00(%rip)      # 0x4060
movsd (%r14,%rax,8), %xmm0
divsd %xmm1, %xmm0
cmpl $0x63, %eax
jg 0x14be
leal 0x1(%rax), %ecx
movl %ecx, 0x2be8(%rip)      # 0x4060
cltq
movsd %xmm0, (%r14,%rax,8)
jmp 0x11b2
addl $-0x2, %ecx
movl %ecx, 0x2bd2(%rip)      # 0x4060
cvttsd2si (%r14,%rcx,8), %eax
cvttsd2si %xmm0, %esi
cltd
idivl %esi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
cmpl $0x63, %ecx
jg 0x14be
leal 0x1(%rcx), %eax
movl %eax, 0x2bb0(%rip)      # 0x4060
movslq %ecx, %rax
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