// File: /workspace/github_c_makefile_star2_O0/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_4/exercise_4_04/stack.c
// Function: getop
int getop(char s[])
{
  int i = 0, c;

  while ((s[0] = c = getch()) == ' ' || c == '\t')
    ;

  s[1] = '\0';

  if (!isdigit(c) && c != '.' && c != '-')
  {
    return c;
  }

  if (c == '-')
  {
    int next = getch();
    if (next == '\n' || next == ' ' || next == '\t')
    {
      ungetch(next);
      return c; // return '-' as operator
    }
    else if (!isdigit(next) && next != '.')
    {
      return next; // not a number
    }
    else // number like "-5", "-.6" etc, next is digit or '.'
    {
      s[++i] = c = next;
    }
  }

  if (isdigit(c))
  {
    while (isdigit(s[++i] = c = getch()))
      ;
  }

  if (c == '.')
  {
    while (isdigit(s[++i] = c = getch()))
      ;
  }

  if (c != EOF)
  {
    ungetch(c);
  }

  return NUMBER;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
callq 0x16c0
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movb %al, (%rcx)
movsbl %al, %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x1d(%rbp)
je 0x1368
cmpl $0x9, -0x18(%rbp)
sete %al
movb %al, -0x1d(%rbp)
movb -0x1d(%rbp), %al
testb $0x1, %al
jne 0x1371
jmp 0x1373
jmp 0x1343
movq -0x10(%rbp), %rax
movb $0x0, 0x1(%rax)
callq 0x1060
movq (%rax), %rax
movslq -0x18(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
jne 0x13ac
cmpl $0x2e, -0x18(%rbp)
je 0x13ac
cmpl $0x2d, -0x18(%rbp)
je 0x13ac
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x14ec
cmpl $0x2d, -0x18(%rbp)
jne 0x1429
callq 0x16c0
movl %eax, -0x1c(%rbp)
cmpl $0xa, -0x1c(%rbp)
je 0x13cc
cmpl $0x20, -0x1c(%rbp)
je 0x13cc
cmpl $0x9, -0x1c(%rbp)
jne 0x13df
movl -0x1c(%rbp), %edi
callq 0x1710
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x14ec
callq 0x1060
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
jne 0x140a
cmpl $0x2e, -0x1c(%rbp)
je 0x140a
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x14ec
movl -0x1c(%rbp), %eax
movl %eax, -0x18(%rbp)
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x14(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1427
jmp 0x1429
callq 0x1060
movq (%rax), %rax
movslq -0x18(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x148a
jmp 0x1445
callq 0x1060
movq (%rax), %rax
movq %rax, -0x28(%rbp)
callq 0x16c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl %esi, -0x14(%rbp)
movslq %esi, %rsi
movb %cl, (%rdx,%rsi)
movsbl %cl, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x1488
jmp 0x1445
jmp 0x148a
cmpl $0x2e, -0x18(%rbp)
jne 0x14d7
jmp 0x1492
callq 0x1060
movq (%rax), %rax
movq %rax, -0x30(%rbp)
callq 0x16c0
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl %esi, -0x14(%rbp)
movslq %esi, %rsi
movb %cl, (%rdx,%rsi)
movsbl %cl, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x14d5
jmp 0x1492
jmp 0x14d7
cmpl $-0x1, -0x18(%rbp)
je 0x14e5
movl -0x18(%rbp), %edi
callq 0x1710
movl $0x30, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)