// File: /workspace/github_c_makefile_star2_O1/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), %r14      # 0x40a0
leaq 0xe30(%rip), %rbp       # 0x2010
leaq 0xeee(%rip), %r12       # 0x20d5
leaq 0xe52(%rip), %r15       # 0x2040
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x17e5
cmpl $0x42, %eax
jle 0x1243
cmpl $0x5d, %eax
jg 0x127a
leal -0x43(%rax), %ecx
cmpl $0x5, %ecx
ja 0x131d
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movl 0x2e72(%rip), %eax      # 0x4090
movslq %eax, %rcx
subq %rax, %rcx
leaq (%r14,%rcx,8), %rdi
leaq 0x8(,%rax,8), %rdx
xorl %esi, %esi
callq 0x1070
movl $0xffffffff, 0x2e4f(%rip) # imm = 0xFFFFFFFF
jmp 0x11f1
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
ja 0x12e5
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movl 0x2e31(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1619
decl %eax
movl %eax, 0x2e21(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1629
cmpl $0x60, %eax
jg 0x12b2
cmpl $0x5e, %eax
je 0x141f
cmpl $0x5f, %eax
jne 0x1496
movl 0x2df9(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1681
decl %eax
movl %eax, 0x2de9(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1691
cmpl $0x61, %eax
je 0x1440
cmpl $0x7e, %eax
jne 0x1496
movl 0x2dc6(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x16c5
decl %eax
movl %eax, 0x2db6(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x16d5
cmpl $0xa, %eax
jne 0x148d
movl 0x2d9c(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x11f1
decl %eax
movl %eax, 0x2d8c(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
leaq 0xd5a(%rip), %rdi       # 0x206b
movb $0x1, %al
callq 0x1060
jmp 0x11f1
cmpl $0x53, %eax
jne 0x1496
callq 0x1b4b
jmp 0x11f1
movl 0x2d5a(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x14ac
decl %eax
movl %eax, 0x2d4a(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm1
jmp 0x14bc
movl 0x2d39(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x14ed
decl %eax
movl %eax, 0x2d29(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm1
jmp 0x14fd
movl 0x2d18(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x154d
decl %eax
movl %eax, 0x2d08(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm1
jmp 0x155d
movl 0x2cf7(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x15aa
decl %eax
movl %eax, 0x2ce7(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm1
jmp 0x15ba
movslq 0x2cd5(%rip), %r12      # 0x4090
cmpq $0x63, %r12
jg 0x1470
movq %rbx, %rdi
callq 0x1040
leal 0x1(%r12), %eax
movl %eax, 0x2cb8(%rip)      # 0x4090
movsd %xmm0, (%r14,%r12,8)
leaq 0xcf0(%rip), %r12       # 0x20d5
jmp 0x11f1
callq 0x1a7d
jmp 0x11f1
callq 0x1ab0
jmp 0x11f1
movl 0x2c8c(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1667
decl %eax
movl %eax, 0x2c7c(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1677
movl 0x2c6b(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x16dc
decl %eax
movl %eax, 0x2c5b(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm1
jmp 0x16ec
movsbl 0x2c29(%rip), %eax      # 0x4070
leal -0x61(%rax), %ecx
cmpl %r13d, %ecx
jge 0x147c
movslq 0x2c3a(%rip), %rcx      # 0x4090
cmpq $0x63, %rcx
jg 0x17c4
cltq
movsd -0x288(%rsp,%rax,8), %xmm0
jmp 0x17b0
leaq 0xc5e(%rip), %r12       # 0x20d5
jmp 0x17c4
leaq 0xc22(%rip), %rdi       # 0x20a5
callq 0x1030
jmp 0x11f1
cmpl $-0x1, %eax
je 0x17d1
leaq 0xbdc(%rip), %rdi       # 0x2079
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x11f1
leaq 0xc35(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm1, %xmm1
ucomisd 0xb44(%rip), %xmm1      # 0x2008
jne 0x14cc
jnp 0x1656
movl 0x2bbe(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x174c
decl %eax
movl %eax, 0x2bae(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1768
leaq 0xbf4(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2b8d(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1517
decl %eax
movl %eax, 0x2b81(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1533
leaq 0xbca(%rip), %rdi       # 0x20e8
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2b56(%rip), %rax      # 0x4090
cmpq $0x63, %rax
jg 0x17c4
subsd %xmm1, %xmm0
jmp 0x1738
leaq 0xb94(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2b2d(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1577
decl %eax
movl %eax, 0x2b21(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1593
leaq 0xb6a(%rip), %rdi       # 0x20e8
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2af6(%rip), %rax      # 0x4090
cmpq $0x63, %rax
jg 0x17c4
mulsd %xmm0, %xmm1
jmp 0x1605
leaq 0xb37(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x2ad0(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x15d4
decl %eax
movl %eax, 0x2ac4(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x15f0
leaq 0xb0d(%rip), %rdi       # 0x20e8
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2a99(%rip), %rax      # 0x4090
cmpq $0x63, %rax
jg 0x17c4
addsd %xmm0, %xmm1
leal 0x1(%rax), %ecx
movl %ecx, 0x2a82(%rip)      # 0x4090
movsd %xmm1, (%r14,%rax,8)
jmp 0x11f1
leaq 0xac8(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm0, %xmm0
ucomisd 0x9d7(%rip), %xmm0      # 0x2008
jne 0x1635
jnp 0x1656
movl 0x2a55(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x177b
decl %eax
movl %eax, 0x2a45(%rip)      # 0x4090
cvttsd2si (%r14,%rax,8), %eax
jmp 0x1795
leaq 0xa63(%rip), %rdi       # 0x20c0
callq 0x1030
jmp 0x11f1
leaq 0xa7a(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x10a0
jmp 0x1727
leaq 0xa60(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm0, %xmm0
leal 0x1(%r13), %r12d
movslq %r13d, %rax
movsd %xmm0, 0x80(%rsp,%rax,8)
addl $0x61, %r13d
leaq 0x9ac(%rip), %rdi       # 0x2058
movl %r13d, %esi
movb $0x1, %al
callq 0x1060
movl %r12d, %r13d
leaq 0xa15(%rip), %r12       # 0x20d5
jmp 0x11f1
leaq 0xa1c(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm0, %xmm0
callq 0x1090
jmp 0x1727
leaq 0xa05(%rip), %rdi       # 0x20e8
callq 0x1030
xorpd %xmm1, %xmm1
movl 0x299e(%rip), %eax      # 0x4090
testl %eax, %eax
jle 0x1706
decl %eax
movl %eax, 0x2992(%rip)      # 0x4090
movsd (%r14,%rax,8), %xmm0
jmp 0x1722
leaq 0x9db(%rip), %rdi       # 0x20e8
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
callq 0x1050
movslq 0x2962(%rip), %rax      # 0x4090
cmpq $0x63, %rax
jg 0x17c4
leal 0x1(%rax), %ecx
movl %ecx, 0x294f(%rip)      # 0x4090
movsd %xmm0, (%r14,%rax,8)
jmp 0x11f1
leaq 0x995(%rip), %rdi       # 0x20e8
movsd %xmm1, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm1
xorpd %xmm0, %xmm0
movslq 0x2921(%rip), %rax      # 0x4090
cmpq $0x63, %rax
jg 0x17c4
divsd %xmm1, %xmm0
jmp 0x1738
leaq 0x966(%rip), %rdi       # 0x20e8
movsd %xmm0, 0x8(%rsp)
callq 0x1030
movsd 0x8(%rsp), %xmm0
xorl %eax, %eax
movslq 0x28f4(%rip), %rcx      # 0x4090
cmpq $0x63, %rcx
jg 0x17c4
cvttsd2si %xmm0, %esi
cltd
idivl %esi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
leal 0x1(%rcx), %eax
movl %eax, 0x28d7(%rip)      # 0x4090
movsd %xmm0, (%r14,%rcx,8)
jmp 0x11f1
movq %r12, %rdi
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