// File: /workspace/github_c_makefile_star2_O1/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 0x2ddc(%rip), %r12      # 0x43b0
movl 0x2dc6(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x15ed
decl %eax
movl %eax, 0x2dba(%rip)      # 0x43a0
movsbl (%rax,%r12), %r15d
jmp 0x15f5
callq 0x1070
movl %eax, %r15d
movb %r15b, (%rbx)
cmpb $0x20, %r15b
sete %al
cmpl $0x9, %r15d
sete %cl
orb %al, %cl
jne 0x15d4
movb $0x0, 0x1(%rbx)
callq 0x1080
movq %rax, %r14
leal -0x2f(%r15), %eax
cmpl $-0x3, %eax
ja 0x1638
movq (%r14), %rax
movslq %r15d, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
jne 0x1638
movl %r15d, %ebp
jmp 0x177f
xorl %r13d, %r13d
cmpl $0x2d, %r15d
jne 0x16c0
movl 0x2d59(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x165a
decl %eax
movl %eax, 0x2d4d(%rip)      # 0x43a0
movsbl (%rax,%r12), %ebp
jmp 0x1661
callq 0x1070
movl %eax, %ebp
cmpl $0x20, %ebp
ja 0x1790
movl %ebp, %eax
movabsq $0x100000600, %rcx      # imm = 0x100000600
btq %rax, %rcx
jae 0x1790
movslq 0x2d19(%rip), %rax      # 0x43a0
cmpq $0x64, %rax
jl 0x169b
leaq 0xa22(%rip), %rdi       # 0x20b6
callq 0x1030
jmp 0x16a8
leal 0x1(%rax), %ecx
movl %ecx, 0x2cfc(%rip)      # 0x43a0
movb %bpl, (%rax,%r12)
movl $0x2d, %r15d
xorl %r13d, %r13d
xorl %eax, %eax
movl $0x2d, %ebp
testb %al, %al
je 0x177f
movq (%r14), %rax
movslq %r15d, %rcx
testb $0x8, 0x1(%rax,%rcx,2)
je 0x1707
movl %r13d, %r13d
movq (%r14), %rbp
movl 0x2cc7(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x16ec
decl %eax
movl %eax, 0x2cbb(%rip)      # 0x43a0
movsbl (%rax,%r12), %r15d
jmp 0x16f4
callq 0x1070
movl %eax, %r15d
movb %r15b, 0x1(%rbx,%r13)
incq %r13
movsbq %r15b, %rax
testb $0x8, 0x1(%rbp,%rax,2)
jne 0x16d0
cmpl $0x2e, %r15d
jne 0x174c
movslq %r13d, %rax
addq %rax, %rbx
incq %rbx
movq (%r14), %r13
movl 0x2c81(%rip), %eax      # 0x43a0
testl %eax, %eax
jle 0x1732
decl %eax
movl %eax, 0x2c75(%rip)      # 0x43a0
movsbl (%rax,%r12), %r15d
jmp 0x173a
callq 0x1070
movl %eax, %r15d
movb %r15b, (%rbx)
movsbq %r15b, %rax
incq %rbx
testb $0x8, 0x1(%r13,%rax,2)
jne 0x1716
movl $0x30, %ebp
cmpl $-0x1, %r15d
je 0x177f
movslq 0x2c42(%rip), %rax      # 0x43a0
cmpq $0x64, %rax
jl 0x1772
leaq 0x94b(%rip), %rdi       # 0x20b6
callq 0x1030
jmp 0x177f
leal 0x1(%rax), %ecx
movl %ecx, 0x2c25(%rip)      # 0x43a0
movb %r15b, (%rax,%r12)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2e, %ebp
je 0x17b6
movq (%r14), %rax
movslq %ebp, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
jne 0x17b6
xorl %r13d, %r13d
movl $0x2d, %r15d
xorl %eax, %eax
jmp 0x16b8
movb %bpl, 0x1(%rbx)
movb $0x1, %al
movl $0x1, %r13d
movl %ebp, %r15d
jmp 0x16b8