// File: /workspace/github_c_makefile_star2_O1/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 0x17b4
testl %eax, %eax
je 0x17a0
leaq 0x10(%rsp), %rbx
leaq 0x329e(%rip), %rbp      # 0x4490
leaq 0xe17(%rip), %r14       # 0x2010
leaq 0xece(%rip), %r13       # 0x20ce
leaq 0xeda(%rip), %r12       # 0x20e1
leaq 0xe32(%rip), %r15       # 0x2040
movl $0x0, 0xc(%rsp)
movl $0x0, 0x2e70(%rip)      # 0x4090
movq %rbx, %rdi
callq 0x1812
cmpl $0x5d, %eax
jle 0x125f
leal -0x5e(%rax), %ecx
cmpl $0xa, %ecx
ja 0x1291
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movl 0x3244(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x165d
decl %eax
movl %eax, 0x3234(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm1
jmp 0x1669
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x12c4
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movl 0x3212(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x15fd
decl %eax
movl %eax, 0x3202(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x1609
cmpl $0x73, %eax
je 0x146d
cmpl $0x7e, %eax
jne 0x149c
movl 0x31df(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x16d8
decl %eax
movl %eax, 0x31cf(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x16e4
cmpl $0xa, %eax
jne 0x1494
movl 0x31b5(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1220
decl %eax
movl %eax, 0x31a5(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
leaq 0xd8f(%rip), %rdi       # 0x207f
movb $0x1, %al
callq 0x1060
jmp 0x1220
movl 0x3186(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x14b2
decl %eax
movl %eax, 0x3176(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm1
jmp 0x14be
movl 0x3165(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x14ef
decl %eax
movl %eax, 0x3155(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm1
jmp 0x14fb
movl 0x3144(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1545
decl %eax
movl %eax, 0x3134(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm1
jmp 0x1551
movl 0x3123(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1598
decl %eax
movl %eax, 0x3113(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm1
jmp 0x15a4
movq %r14, %r15
movq %r12, %r14
movq %r13, %r12
movslq 0x30f8(%rip), %r13      # 0x4488
cmpq $0x63, %r13
jg 0x1477
movq %rbx, %rdi
callq 0x1040
leal 0x1(%r13), %eax
movl %eax, 0x30dc(%rip)      # 0x4488
movsd %xmm0, (%rbp,%r13,8)
movq %r12, %r13
jmp 0x1482
movl 0x30c7(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1647
decl %eax
movl %eax, 0x30b7(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x1653
movslq 0x30a5(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x177a
movsbq 0x2c7b(%rip), %rcx      # 0x4070
movsd -0x288(%rsp,%rcx,8), %xmm0
jmp 0x16fa
callq 0x1a0e
jmp 0x1220
movl 0x3075(%rip), %eax      # 0x4488
movslq %eax, %rcx
subq %rax, %rcx
leaq (,%rcx,8), %rdi
addq %rbp, %rdi
leaq 0x8(,%rax,8), %rdx
xorl %esi, %esi
callq 0x1070
movl $0xffffffff, 0x304b(%rip) # imm = 0xFFFFFFFF
jmp 0x1220
movl 0x3040(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x16a0
decl %eax
movl %eax, 0x3030(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x16ac
callq 0x1a41
jmp 0x1220
callq 0x1adc
jmp 0x1220
movq %r12, %r13
movq %r12, %rdi
callq 0x1030
movq %r14, %r12
movq %r15, %r14
leaq 0xbb1(%rip), %r15       # 0x2040
jmp 0x1220
testl %eax, %eax
je 0x1787
leaq 0xbea(%rip), %rdi       # 0x208d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
ucomisd 0xb42(%rip), %xmm1      # 0x2008
jne 0x14ce
jnp 0x1636
movl 0x2fb4(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x170e
decl %eax
movl %eax, 0x2fa4(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x1724
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2f87(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1515
decl %eax
movl %eax, 0x2f7b(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x152b
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2f56(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x177a
subsd %xmm1, %xmm0
jmp 0x16fa
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2f31(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x156b
decl %eax
movl %eax, 0x2f25(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x1581
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2f00(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x177a
mulsd %xmm0, %xmm1
jmp 0x15e9
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2ede(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x15be
decl %eax
movl %eax, 0x2ed2(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x15d4
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2ead(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x177a
addsd %xmm0, %xmm1
leal 0x1(%rax), %ecx
movl %ecx, 0x2e96(%rip)      # 0x4488
movsd %xmm1, (%rbp,%rax,8)
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
ucomisd 0x9f7(%rip), %xmm0      # 0x2008
jne 0x1615
jnp 0x1636
movl 0x2e6d(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1737
decl %eax
movl %eax, 0x2e5d(%rip)      # 0x4488
cvttsd2si (%rbp,%rax,8), %eax
jmp 0x174b
leaq 0xa7c(%rip), %rdi       # 0x20b9
callq 0x1030
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x10a0
jmp 0x16e9
movq %r12, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2e19(%rip), %eax      # 0x4488
testl %eax, %eax
jle 0x1683
decl %eax
movl %eax, 0x2e0d(%rip)      # 0x4488
movsd (%rbp,%rax,8), %xmm0
jmp 0x1699
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
callq 0x1050
jmp 0x16e9
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 0x9a0(%rip), %rdi       # 0x206c
movb $0x1, %al
callq 0x1060
jmp 0x1220
movq %r12, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x1090
movslq 0x2d98(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x177a
leal 0x1(%rax), %ecx
movl %ecx, 0x2d85(%rip)      # 0x4488
movsd %xmm0, (%rbp,%rax,8)
jmp 0x1220
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x1030
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2d5d(%rip), %rax      # 0x4488
cmpq $0x63, %rax
jg 0x177a
divsd %xmm1, %xmm0
jmp 0x16fa
movq %r12, %rdi
movsd %xmm0, (%rsp)
callq 0x1030
movsd (%rsp), %xmm0
xorl %eax, %eax
movslq 0x2d36(%rip), %rcx      # 0x4488
cmpq $0x63, %rcx
jg 0x177a
cvttsd2si %xmm0, %esi
cltd
idivl %esi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
leal 0x1(%rcx), %eax
movl %eax, 0x2d19(%rip)      # 0x4488
movsd %xmm0, (%rbp,%rcx,8)
jmp 0x1220
movq %r13, %rdi
callq 0x1030
jmp 0x1220
leaq 0x2912(%rip), %rdi      # 0x40a0
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x17b4
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