// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x2df1(%rip), %r12      # 0x43b0
movl 0x2ddb(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x15d8
decl %eax
movl %eax, 0x2dcf(%rip)      # 0x43a0
movsbl (%rax,%r12), %r15d
jmp 0x15e0
callq 0x1070
movl %eax, %r15d
movb %r15b, (%rbx)
cmpb $0x20, %r15b
sete %al
cmpl $0x9, %r15d
sete %cl
orb %al, %cl
jne 0x15bf
movb $0x0, 0x1(%rbx)
callq 0x1080
movq %rax, %r14
movq (%rax), %rax
movslq %r15d, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
leal -0x2f(%r15), %ecx
cmpl $-0x3, %ecx
ja 0x1622
testw %ax, %ax
je 0x1784
xorl %r13d, %r13d
cmpl $0x2d, %r15d
jne 0x16ce
movl 0x2d6b(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x1648
decl %eax
movl %eax, 0x2d5f(%rip)      # 0x43a0
movsbl (%rax,%r12), %r15d
jmp 0x1650
callq 0x1070
movl %eax, %r15d
cmpl $0x20, %r15d
ja 0x169c
movl %r15d, %eax
movabsq $0x100000600, %rcx      # imm = 0x100000600
btq %rax, %rcx
jae 0x169c
movslq 0x2d30(%rip), %rax      # 0x43a0
cmpq $0x64, %rax
jl 0x1684
leaq 0xa39(%rip), %rdi       # 0x20b6
callq 0x1030
jmp 0x1691
leal 0x1(%rax), %ecx
movl %ecx, 0x2d13(%rip)      # 0x43a0
movb %r15b, (%rax,%r12)
movl $0x2d, %r15d
jmp 0x1784
movslq %r15d, %rax
cmpl $0x2e, %r15d
je 0x16b8
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %ecx
andl $0x800, %ecx            # imm = 0x800
je 0x1784
movb %r15b, 0x1(%rbx)
movq (%r14), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x800, %eax            # imm = 0x800
movl $0x1, %r13d
testw %ax, %ax
je 0x170d
movl %r13d, %r13d
movq (%r14), %rbp
movl 0x2cc1(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x16f2
decl %eax
movl %eax, 0x2cb5(%rip)      # 0x43a0
movsbl (%rax,%r12), %r15d
jmp 0x16fa
callq 0x1070
movl %eax, %r15d
movb %r15b, 0x1(%rbx,%r13)
incq %r13
movsbq %r15b, %rax
testb $0x8, 0x1(%rbp,%rax,2)
jne 0x16d6
movl %r15d, %eax
cmpl $0x2e, %r15d
jne 0x1751
movslq %r13d, %rax
addq %rax, %rbx
incq %rbx
movq (%r14), %r15
movl 0x2c78(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x173b
decl %eax
movl %eax, 0x2c6c(%rip)      # 0x43a0
movsbl (%rax,%r12), %eax
jmp 0x1740
callq 0x1070
movb %al, (%rbx)
movsbq %al, %rcx
incq %rbx
testb $0x8, 0x1(%r15,%rcx,2)
jne 0x171f
movl $0x30, %r15d
cmpl $-0x1, %eax
je 0x1784
movslq 0x2c3d(%rip), %rcx      # 0x43a0
cmpq $0x64, %rcx
jl 0x1777
leaq 0x946(%rip), %rdi       # 0x20b6
callq 0x1030
jmp 0x1784
leal 0x1(%rcx), %edx
movl %edx, 0x2c20(%rip)      # 0x43a0
movb %al, (%rcx,%r12)
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq