// File: /workspace/github_c_makefile_star2_O3/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 0x2ecc(%rip), %rdi      # 0x40a0
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x179c
testl %eax, %eax
je 0x1788
leaq 0x10(%rsp), %rbx
leaq 0x329e(%rip), %r14      # 0x4490
leaq 0xe17(%rip), %r15       # 0x2010
leaq 0xee1(%rip), %r12       # 0x20e1
leaq 0xec7(%rip), %r13       # 0x20ce
leaq 0xe32(%rip), %rbp       # 0x2040
movl $0x0, 0xc(%rsp)
movl $0x0, 0x2e70(%rip)      # 0x4090
movq %rbx, %rdi
callq 0x17f5
cmpl $0x5d, %eax
jle 0x1264
leal -0x5e(%rax), %ecx
cmpl $0xa, %ecx
ja 0x12ce
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movl 0x323f(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x164e
decl %eax
movl %eax, 0x322f(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm1
jmp 0x1660
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x1301
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movl 0x3209(%rip), %ecx      # 0x4488
testl %ecx, %ecx
jle 0x14dd
leal -0x1(%rcx), %eax
movl %eax, 0x31f8(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
ucomisd 0xd6a(%rip), %xmm0      # 0x2008
jne 0x12a6
jnp 0x14e5
cmpl $0x1, %ecx
jne 0x1729
movq %r12, %rdi
movsd %xmm0, (%rsp)
callq 0x1030
movsd (%rsp), %xmm0
xorl %eax, %eax
movl 0x31bf(%rip), %ecx      # 0x4488
jmp 0x1738
cmpl $0x73, %eax
je 0x14d3
cmpl $0x7e, %eax
jne 0x1518
movl 0x31a2(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x16c9
decl %eax
movl %eax, 0x3192(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x16d5
cmpl $0xa, %eax
jne 0x1510
movl 0x3178(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1220
decl %eax
movl %eax, 0x3168(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
leaq 0xd52(%rip), %rdi       # 0x207f
movb $0x1, %al
callq 0x1060
jmp 0x1220
movl 0x3149(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x14dd
leal -0x1(%rax), %ecx
movl %ecx, 0x3138(%rip)      # 0x4488
movsd (%r14,%rcx,8), %xmm1
ucomisd 0xcaa(%rip), %xmm1      # 0x2008
jne 0x1366
jnp 0x14e5
cmpl $0x1, %eax
jne 0x16fb
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x30fd(%rip), %eax      # 0x4488
jmp 0x170a
movl 0x30f2(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x152e
decl %eax
movl %eax, 0x30e2(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm1
jmp 0x1540
movl 0x30d1(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1582
decl %eax
movl %eax, 0x30c1(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm1
jmp 0x1594
movl 0x30b0(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x15d3
decl %eax
movl %eax, 0x30a0(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm1
jmp 0x15e5
movq %r15, %rbp
movq %r12, %r15
movslq 0x3088(%rip), %r12      # 0x4488
cmpq $0x63, %r12
jg 0x14f6
movq %rbx, %rdi
callq 0x1040
leal 0x1(%r12), %eax
movl %eax, 0x306b(%rip)      # 0x4488
movsd %xmm0, (%r14,%r12,8)
jmp 0x14fe
movl 0x305a(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1638
decl %eax
movl %eax, 0x304a(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x1644
movslq 0x3038(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x1762
movsbq 0x2c0e(%rip), %rcx      # 0x4070
movsd -0x288(%rsp,%rcx,8), %xmm0
jmp 0x16e7
callq 0x19be
jmp 0x1220
movl 0x3008(%rip), %eax      # 0x4488
movslq %eax, %rcx
subq %rax, %rcx
leaq (%r14,%rcx,8), %rdi
leaq 0x8(,%rax,8), %rdx
xorl %esi, %esi
callq 0x1070
movl $0xffffffff, 0x2fe5(%rip) # imm = 0xFFFFFFFF
jmp 0x1220
movl 0x2fda(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1691
decl %eax
movl %eax, 0x2fca(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x169d
callq 0x19f1
jmp 0x1220
callq 0x1a8d
jmp 0x1220
movq %r12, %rdi
callq 0x1030
leaq 0xbcd(%rip), %rdi       # 0x20b9
callq 0x1030
jmp 0x1220
movq %r13, %rdi
callq 0x1030
movq %r15, %r12
movq %rbp, %r15
leaq 0xb35(%rip), %rbp       # 0x2040
jmp 0x1220
testl %eax, %eax
je 0x176f
leaq 0xb6e(%rip), %rdi       # 0x208d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2f48(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1554
decl %eax
movl %eax, 0x2f3c(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x1570
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2f18(%rip), %eax      # 0x4488
cmpl $0x63, %eax
jg 0x1762
subsd %xmm1, %xmm0
jmp 0x1713
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2ef4(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x15a8
decl %eax
movl %eax, 0x2ee8(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x15c4
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2ec4(%rip), %eax      # 0x4488
cmpl $0x63, %eax
jg 0x1762
mulsd %xmm0, %xmm1
jmp 0x1622
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2ea3(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x15f9
decl %eax
movl %eax, 0x2e97(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x1615
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2e73(%rip), %eax      # 0x4488
cmpl $0x63, %eax
jg 0x1762
addsd %xmm0, %xmm1
leal 0x1(%rax), %ecx
movl %ecx, 0x2e5d(%rip)      # 0x4488
cltq
movsd %xmm1, (%r14,%rax,8)
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x10a0
jmp 0x16da
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2e28(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1674
decl %eax
movl %eax, 0x2e1c(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
jmp 0x168a
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
callq 0x1050
jmp 0x16da
movq %r12, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
movl 0xc(%rsp), %eax
movslq %eax, %rsi
incl %eax
movl %eax, 0xc(%rsp)
movsd %xmm0, 0x80(%rsp,%rsi,8)
addl $0x61, %esi
leaq 0x9af(%rip), %rdi       # 0x206c
movb $0x1, %al
callq 0x1060
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x1090
movslq 0x2da7(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x1762
leal 0x1(%rax), %ecx
movl %ecx, 0x2d98(%rip)      # 0x4488
movsd %xmm0, (%r14,%rax,8)
jmp 0x1220
addl $-0x2, %eax
movl %eax, 0x2d84(%rip)      # 0x4488
movsd (%r14,%rax,8), %xmm0
cmpl $0x63, %eax
jg 0x1762
divsd %xmm1, %xmm0
leal 0x1(%rax), %ecx
movl %ecx, 0x2d6c(%rip)      # 0x4488
cltq
movsd %xmm0, (%r14,%rax,8)
jmp 0x1220
addl $-0x2, %ecx
movl %ecx, 0x2d56(%rip)      # 0x4488
cvttsd2si (%r14,%rcx,8), %eax
cmpl $0x63, %ecx
jg 0x1762
cvttsd2si %xmm0, %esi
cltd
idivl %esi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
leal 0x1(%rcx), %eax
movl %eax, 0x2d34(%rip)      # 0x4488
movslq %ecx, %rax
movsd %xmm0, (%r14,%rax,8)
jmp 0x1220
movq %r13, %rdi
callq 0x1030
jmp 0x1220
leaq 0x292a(%rip), %rdi      # 0x40a0
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x179c
testl %eax, %eax
jne 0x1216
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq