// File: /workspace/github_c_makefile_star2_O2/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_4/exercise_4_10/calculator.c
// Function: main
int main(void)
{
  int type;
  double op2;
  char s[MAXOP];

  int varindex = 0;
  double var_buff[VARNUM];

  while (get_line(line, MAXLEN) != 0)
  {
    line_i = 0;
    while ((type = getop(s)) != '\0')
    {
      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 '^':
        op2 = pop();
        push(pow(pop(), op2));
        break;

      case '~':
        push(sin(pop()));
        break;

      case 'e':
        push(exp(pop()));
        break;

      case 'h':
        view_head();
        break;

      case 'd':
        duplicate();
        break;

      case 's':
        swap();
        break;

      case 'c':
        clear();
        break;

      case VARSET:
        var_buff[varindex++] = pop();
        printf("variable %c: %.3f\n", 'a' + varindex - 1, var_buff[varindex - 1]);
        break;

      case VARGET:
        push(var_buff[var - 'a']);
        break;

      case '\n':
        if (!is_empty())
        {
          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 $0x158, %rsp            # imm = 0x158
leaq 0x10(%rsp), %r14
leaq 0xe37(%rip), %rbx       # 0x2010
leaq 0xead(%rip), %r13       # 0x208d
leaq 0xe59(%rip), %r15       # 0x2040
leaq 0xe7e(%rip), %rbp       # 0x206c
xorl %r12d, %r12d
leaq 0x2ea8(%rip), %rdi      # 0x40a0
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x1446
testl %eax, %eax
je 0x1432
andl $0x0, 0x2e7f(%rip)      # 0x4090
movq %r14, %rdi
callq 0x1493
leal -0x5e(%rax), %ecx
cmpl $0xa, %ecx
jbe 0x126a
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x1298
movslq (%rbx,%rcx,4), %rax
addq %rbx, %rax
jmpq *%rax
callq 0x161e
ucomisd 0xdc9(%rip), %xmm0      # 0x2008
jne 0x1247
jnp 0x1421
movsd %xmm0, 0x8(%rsp)
callq 0x161e
cvttsd2si %xmm0, %eax
cvttsd2si 0x8(%rsp), %ecx
cltd
idivl %ecx
cvtsi2sd %edx, %xmm0
callq 0x15ef
jmp 0x1211
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
callq 0x161e
movsd %xmm0, 0x8(%rsp)
callq 0x161e
movsd 0x8(%rsp), %xmm1
callq 0x1050
callq 0x15ef
jmp 0x1211
cmpl $0xa, %eax
je 0x13de
cmpl $0x73, %eax
je 0x1403
cmpl $0x7e, %eax
je 0x140d
testl %eax, %eax
je 0x11f1
movq %r13, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x1211
callq 0x16d4
jmp 0x1211
callq 0x1685
jmp 0x1211
movsbq 0x2d87(%rip), %rax      # 0x4070
movsd -0x288(%rsp,%rax,8), %xmm0
callq 0x15ef
jmp 0x1211
callq 0x1652
jmp 0x1211
callq 0x161e
movslq %r12d, %rsi
incl %r12d
movsd %xmm0, 0x80(%rsp,%rsi,8)
addl $0x61, %esi
movq %rbp, %rdi
movb $0x1, %al
callq 0x1060
jmp 0x1211
callq 0x161e
callq 0x10a0
callq 0x15ef
jmp 0x1211
callq 0x161e
movsd %xmm0, 0x8(%rsp)
callq 0x161e
addsd 0x8(%rsp), %xmm0
callq 0x15ef
jmp 0x1211
movq %r14, %rdi
callq 0x1040
callq 0x15ef
jmp 0x1211
callq 0x161e
movsd %xmm0, 0x8(%rsp)
callq 0x161e
subsd 0x8(%rsp), %xmm0
callq 0x15ef
jmp 0x1211
callq 0x161e
ucomisd 0xc69(%rip), %xmm0      # 0x2008
jne 0x13a3
jnp 0x1421
movsd %xmm0, 0x8(%rsp)
callq 0x161e
divsd 0x8(%rsp), %xmm0
callq 0x15ef
jmp 0x1211
callq 0x161e
movsd %xmm0, 0x8(%rsp)
callq 0x161e
mulsd 0x8(%rsp), %xmm0
callq 0x15ef
jmp 0x1211
cmpl $0x0, 0x30a3(%rip)      # 0x4488
jle 0x1211
callq 0x161e
leaq 0xc88(%rip), %rdi       # 0x207f
movb $0x1, %al
callq 0x1060
jmp 0x1211
callq 0x16a0
jmp 0x1211
callq 0x161e
callq 0x1090
callq 0x15ef
jmp 0x1211
leaq 0xc91(%rip), %rdi       # 0x20b9
callq 0x1030
jmp 0x1211
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq