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

  int varindex = 0;
  double var_buff[VARNUM];

  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 '^':
      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:
      // Check if the variable has been assigned previously
      if (var - 'a' <= (varindex - 1))
      {
        push(var_buff[var - 'a']);
      }
      else
      {
        printf("Error: undefined variable.\n");
      }
      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), %rbx
leaq 0x2ec7(%rip), %rbp      # 0x40a0
leaq 0xe30(%rip), %r13       # 0x2010
leaq 0xf01(%rip), %r15       # 0x20e8
leaq 0xe52(%rip), %r12       # 0x2040
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x17c2
cmpl $0x42, %eax
jle 0x124e
cmpl $0x5d, %eax
jg 0x12bb
leal -0x43(%rax), %ecx
cmpl $0x5, %ecx
ja 0x135e
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movl 0x2e6e(%rip), %eax      # 0x4090
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, 0x2e44(%rip) # imm = 0xFFFFFFFF
jmp 0x11f1
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x1326
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movl 0x2e26(%rip), %ecx      # 0x4090
testl %ecx, %ecx
jle 0x14f8
leal -0x1(%rcx), %eax
movl %eax, 0x2e15(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
ucomisd 0xd7f(%rip), %xmm0      # 0x2008
jne 0x1291
jnp 0x1500
cmpl $0x1, %ecx
jne 0x1764
movq %r15, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorl %eax, %eax
movl 0x2dda(%rip), %ecx      # 0x4090
jmp 0x1773
cmpl $0x60, %eax
jg 0x12f3
cmpl $0x5e, %eax
je 0x1498
cmpl $0x5f, %eax
jne 0x1543
movl 0x2db8(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x167f
decl %eax
movl %eax, 0x2da8(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x168b
cmpl $0x61, %eax
je 0x14b9
cmpl $0x7e, %eax
jne 0x1543
movl 0x2d85(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x16bf
decl %eax
movl %eax, 0x2d75(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x16cb
cmpl $0xa, %eax
jne 0x153a
movl 0x2d5b(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x11f1
decl %eax
movl %eax, 0x2d4b(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
leaq 0xd19(%rip), %rdi       # 0x206b
movb $0x1, %al
callq 0x1060
jmp 0x11f1
cmpl $0x53, %eax
jne 0x1543
callq 0x1b0f
jmp 0x11f1
movl 0x2d19(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x14f8
leal -0x1(%rax), %ecx
movl %ecx, 0x2d08(%rip)      # 0x4090
movsd (%rbp,%rcx,8), %xmm1
ucomisd 0xc72(%rip), %xmm1      # 0x2008
jne 0x139e
jnp 0x1500
cmpl $0x1, %eax
jne 0x1736
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2ccb(%rip), %eax      # 0x4090
jmp 0x1745
movl 0x2cc0(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1559
decl %eax
movl %eax, 0x2cb0(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm1
jmp 0x156b
movl 0x2c9f(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x15af
decl %eax
movl %eax, 0x2c8f(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm1
jmp 0x15c1
movl 0x2c7e(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1602
decl %eax
movl %eax, 0x2c6e(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm1
jmp 0x1614
movslq 0x2c5c(%rip), %r15      # 0x4090
cmpq $0x63, %r15
jg 0x1511
movq %rbx, %rdi
callq 0x1040
leal 0x1(%r15), %eax
movl %eax, 0x2c40(%rip)      # 0x4090
movsd %xmm0, (%rbp,%r15,8)
leaq 0xc8a(%rip), %r15       # 0x20e8
jmp 0x11f1
callq 0x1a40
jmp 0x11f1
callq 0x1a73
jmp 0x11f1
movl 0x2c13(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1669
decl %eax
movl %eax, 0x2c03(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x1675
movl 0x2bf2(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x16d2
decl %eax
movl %eax, 0x2be2(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm1
jmp 0x16e4
movsbl 0x2bb0(%rip), %eax      # 0x4070
leal -0x61(%rax), %ecx
cmpl %r14d, %ecx
jge 0x1529
movslq 0x2bc1(%rip), %rcx      # 0x4090
cmpq $0x63, %rcx
jg 0x179d
cltq
movsd -0x288(%rsp,%rax,8), %xmm0
leal 0x1(%rcx), %eax
movl %eax, 0x2ba3(%rip)      # 0x4090
movsd %xmm0, (%rbp,%rcx,8)
jmp 0x11f1
movq %r15, %rdi
callq 0x1030
leaq 0xbb9(%rip), %rdi       # 0x20c0
callq 0x1030
jmp 0x11f1
leaq 0xbbd(%rip), %rdi       # 0x20d5
callq 0x1030
leaq 0xbc4(%rip), %r15       # 0x20e8
jmp 0x11f1
leaq 0xb75(%rip), %rdi       # 0x20a5
callq 0x1030
jmp 0x11f1
cmpl $-0x1, %eax
je 0x17ae
leaq 0xb2f(%rip), %rdi       # 0x2079
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x11f1
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2b25(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x157f
decl %eax
movl %eax, 0x2b19(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x159d
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2af3(%rip), %eax      # 0x4090
cmpl $0x63, %eax
jg 0x179d
subsd %xmm1, %xmm0
jmp 0x174e
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2acf(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x15d5
decl %eax
movl %eax, 0x2ac3(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x15f3
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2a9d(%rip), %eax      # 0x4090
cmpl $0x63, %eax
jg 0x179d
mulsd %xmm0, %xmm1
jmp 0x1653
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2a7c(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1628
decl %eax
movl %eax, 0x2a70(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x1646
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movl 0x2a4a(%rip), %eax      # 0x4090
cmpl $0x63, %eax
jg 0x179d
addsd %xmm0, %xmm1
leal 0x1(%rax), %ecx
movl %ecx, 0x2a34(%rip)      # 0x4090
cltq
movsd %xmm1, (%rbp,%rax,8)
jmp 0x11f1
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x10a0
jmp 0x1715
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
leal 0x1(%r14), %r15d
movslq %r14d, %rax
movsd %xmm0, 0x80(%rsp,%rax,8)
addl $0x61, %r14d
leaq 0x9b2(%rip), %rdi       # 0x2058
movl %r14d, %esi
movb $0x1, %al
callq 0x1060
movl %r15d, %r14d
leaq 0xa2e(%rip), %r15       # 0x20e8
jmp 0x11f1
movq %r15, %rdi
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x1090
jmp 0x1715
movq %r15, %rdi
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x29ac(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x16f8
decl %eax
movl %eax, 0x29a0(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
jmp 0x1710
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
callq 0x1050
movslq 0x2974(%rip), %rax      # 0x4090
cmpq $0x63, %rax
jg 0x179d
leal 0x1(%rax), %ecx
movl %ecx, 0x2965(%rip)      # 0x4090
movsd %xmm0, (%rbp,%rax,8)
jmp 0x11f1
addl $-0x2, %eax
movl %eax, 0x2951(%rip)      # 0x4090
movsd (%rbp,%rax,8), %xmm0
cmpl $0x63, %eax
jg 0x179d
divsd %xmm1, %xmm0
leal 0x1(%rax), %ecx
movl %ecx, 0x2939(%rip)      # 0x4090
cltq
movsd %xmm0, (%rbp,%rax,8)
jmp 0x11f1
addl $-0x2, %ecx
movl %ecx, 0x2923(%rip)      # 0x4090
cvttsd2si (%rbp,%rcx,8), %eax
cmpl $0x63, %ecx
jg 0x179d
cvttsd2si %xmm0, %esi
cltd
idivl %esi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
leal 0x1(%rcx), %eax
movl %eax, 0x2901(%rip)      # 0x4090
movslq %ecx, %rax
movsd %xmm0, (%rbp,%rax,8)
jmp 0x11f1
leaq 0x931(%rip), %rdi       # 0x20d5
callq 0x1030
jmp 0x11f1
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq