// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movl $0x0, -0x4(%rbp)
movl $0x0, -0x84(%rbp)
leaq 0x2ecd(%rip), %rdi      # 0x4090
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x1460
cmpl $0x0, %eax
je 0x1451
movl $0x0, 0x2ea0(%rip)      # 0x4080
leaq -0x80(%rbp), %rdi
callq 0x1500
movl %eax, -0x8(%rbp)
cmpl $0x0, %eax
je 0x144c
movl -0x8(%rbp), %eax
addl $-0xa, %eax
movl %eax, %ecx
movq %rcx, -0x168(%rbp)
subl $0x74, %eax
ja 0x1435
movq -0x168(%rbp), %rax
leaq 0xde9(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x80(%rbp), %rdi
callq 0x1030
callq 0x1780
jmp 0x1447
callq 0x17e0
movsd %xmm0, -0x170(%rbp)
callq 0x17e0
movaps %xmm0, %xmm1
movsd -0x170(%rbp), %xmm0
addsd %xmm1, %xmm0
callq 0x1780
jmp 0x1447
callq 0x17e0
movsd %xmm0, -0x10(%rbp)
callq 0x17e0
subsd -0x10(%rbp), %xmm0
callq 0x1780
jmp 0x1447
callq 0x17e0
movsd %xmm0, -0x178(%rbp)
callq 0x17e0
movaps %xmm0, %xmm1
movsd -0x178(%rbp), %xmm0
mulsd %xmm1, %xmm0
callq 0x1780
jmp 0x1447
callq 0x17e0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x12c7
jp 0x12c7
jmp 0x12d8
callq 0x17e0
divsd -0x10(%rbp), %xmm0
callq 0x1780
jmp 0x12e6
leaq 0xef9(%rip), %rdi       # 0x21d8
movb $0x0, %al
callq 0x1050
jmp 0x1447
callq 0x17e0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1307
jp 0x1307
jmp 0x1323
callq 0x17e0
cvttsd2si %xmm0, %eax
cvttsd2si -0x10(%rbp), %ecx
cltd
idivl %ecx
cvtsi2sd %edx, %xmm0
callq 0x1780
jmp 0x1331
leaq 0xeae(%rip), %rdi       # 0x21d8
movb $0x0, %al
callq 0x1050
jmp 0x1447
callq 0x17e0
movsd %xmm0, -0x10(%rbp)
callq 0x17e0
movsd -0x10(%rbp), %xmm1
callq 0x1040
callq 0x1780
jmp 0x1447
callq 0x17e0
callq 0x1070
callq 0x1780
jmp 0x1447
callq 0x17e0
callq 0x1080
callq 0x1780
jmp 0x1447
callq 0x1840
jmp 0x1447
callq 0x1890
jmp 0x1447
callq 0x18c0
jmp 0x1447
callq 0x1900
jmp 0x1447
callq 0x17e0
movl -0x84(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x84(%rbp)
cltq
movsd %xmm0, -0x160(%rbp,%rax,8)
movl -0x84(%rbp), %esi
addl $0x61, %esi
subl $0x1, %esi
movl -0x84(%rbp), %eax
subl $0x1, %eax
cltq
movsd -0x160(%rbp,%rax,8), %xmm0
leaq 0xdfd(%rip), %rdi       # 0x21ee
movb $0x1, %al
callq 0x1050
jmp 0x1447
movsbl 0x2c5f(%rip), %eax      # 0x4060
subl $0x61, %eax
cltq
movsd -0x160(%rbp,%rax,8), %xmm0
callq 0x1780
jmp 0x1447
callq 0x1940
cmpl $0x0, %eax
jne 0x1433
callq 0x17e0
leaq 0xdd5(%rip), %rdi       # 0x2201
movb $0x1, %al
callq 0x1050
jmp 0x1447
leaq -0x80(%rbp), %rsi
leaq 0xdcf(%rip), %rdi       # 0x220f
movb $0x0, %al
callq 0x1050
jmp 0x11e0
jmp 0x11bc
xorl %eax, %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)