// File: /workspace/github_c_makefile_star2_O2/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 0xe67(%rip), %r14       # 0x2040
leaq 0xe99(%rip), %r13       # 0x2079
leaq 0xe29(%rip), %rbp       # 0x2010
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x146b
leal -0x25(%rax), %ecx
cmpl $0xb, %ecx
jbe 0x1254
leal -0x43(%rax), %ecx
cmpl $0x5, %ecx
jbe 0x1299
cmpl $0xa, %eax
je 0x13b3
cmpl $0x53, %eax
je 0x13a9
cmpl $0x5e, %eax
je 0x1410
cmpl $0x5f, %eax
je 0x1376
cmpl $0x61, %eax
je 0x13d8
cmpl $0x7e, %eax
je 0x13fc
cmpl $-0x1, %eax
je 0x1457
movq %r13, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x11ea
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
callq 0x15de
ucomisd 0xd9d(%rip), %xmm0      # 0x2008
jne 0x1273
jnp 0x1435
movsd %xmm0, 0x8(%rsp)
callq 0x15de
cvttsd2si %xmm0, %eax
cvttsd2si 0x8(%rsp), %ecx
cltd
idivl %ecx
cvtsi2sd %edx, %xmm0
callq 0x15af
jmp 0x11ea
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
callq 0x1694
jmp 0x11ea
callq 0x15de
movsd %xmm0, 0x8(%rsp)
callq 0x15de
addsd 0x8(%rsp), %xmm0
callq 0x15af
jmp 0x11ea
movq %rbx, %rdi
callq 0x1040
callq 0x15af
jmp 0x11ea
callq 0x15de
movsd %xmm0, 0x8(%rsp)
callq 0x15de
subsd 0x8(%rsp), %xmm0
callq 0x15af
jmp 0x11ea
callq 0x15de
ucomisd 0xcfd(%rip), %xmm0      # 0x2008
jne 0x1313
jnp 0x1435
movsd %xmm0, 0x8(%rsp)
callq 0x15de
divsd 0x8(%rsp), %xmm0
callq 0x15af
jmp 0x11ea
callq 0x15de
movsd %xmm0, 0x8(%rsp)
callq 0x15de
mulsd 0x8(%rsp), %xmm0
callq 0x15af
jmp 0x11ea
callq 0x15de
callq 0x10a0
callq 0x15af
jmp 0x11ea
callq 0x1612
jmp 0x11ea
callq 0x1645
jmp 0x11ea
callq 0x15de
leal 0x1(%r12), %r15d
movslq %r12d, %rax
movsd %xmm0, 0x80(%rsp,%rax,8)
addl $0x61, %r12d
leaq 0xcc1(%rip), %rdi       # 0x2058
movl %r12d, %esi
movb $0x1, %al
callq 0x1060
movl %r15d, %r12d
jmp 0x11ea
callq 0x1660
jmp 0x11ea
cmpl $0x0, 0x2cd6(%rip)      # 0x4090
jle 0x11ea
callq 0x15de
leaq 0xc9f(%rip), %rdi       # 0x206b
movb $0x1, %al
callq 0x1060
jmp 0x11ea
movsbl 0x2c91(%rip), %eax      # 0x4070
leal -0x61(%rax), %ecx
cmpl %r12d, %ecx
jge 0x1446
cltq
movsd -0x288(%rsp,%rax,8), %xmm0
callq 0x15af
jmp 0x11ea
callq 0x15de
callq 0x1090
callq 0x15af
jmp 0x11ea
callq 0x15de
movsd %xmm0, 0x8(%rsp)
callq 0x15de
movsd 0x8(%rsp), %xmm1
callq 0x1050
callq 0x15af
jmp 0x11ea
leaq 0xc84(%rip), %rdi       # 0x20c0
callq 0x1030
jmp 0x11ea
leaq 0xc58(%rip), %rdi       # 0x20a5
callq 0x1030
jmp 0x11ea
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq