// File: /workspace/github_c_makefile_star2_O3/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_5/exercise_5_01/getint.c
// Function: getint
int getint(int *pn)
{
  int c, sign;

  while (isspace(c = getch()))
    ;

  if (!isdigit(c) && c != EOF && c != '+' && c != '-')
  {
    ungetch(c);
    return 0;
  }

  sign = (c == '-') ? -1 : 1;

  if (c == '+' || c == '-')
  {
    if (!isdigit(c = getch()))
    {
      ungetch(c);
      ungetch(sign == 1 ? '+' : '-');
      return 0;
    }
  }

  for (*pn = 0; isdigit(c); c = getch())
  {
    *pn = 10 * *pn + (c - '0');
  }

  *pn = *pn * sign;

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

  return c;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
callq 0x1060
movq %rax, %r15
movl 0x2ea3(%rip), %eax      # 0x4060
leaq 0x2eac(%rip), %r13      # 0x4070
movq (%r15), %r12
testl %eax, %eax
jle 0x11da
decl %eax
movl %eax, 0x2e8d(%rip)      # 0x4060
movl (%r13,%rax,4), %ebx
jmp 0x11e7
callq 0x1050
movl %eax, %ebx
movl 0x2e79(%rip), %eax      # 0x4060
movslq %ebx, %rcx
testb $0x20, 0x1(%r12,%rcx,2)
jne 0x11c4
movq (%r15), %rbp
movzwl (%rbp,%rcx,2), %ecx
btl $0xb, %ecx
jb 0x1220
leal 0x1(%rbx), %edx
cmpl $0x2e, %edx
ja 0x134e
movabsq $0x500000000001, %rsi   # imm = 0x500000000001
btq %rdx, %rsi
jae 0x134e
cmpl $0x2d, %ebx
je 0x122a
cmpl $0x2b, %ebx
jne 0x123d
testl %eax, %eax
jle 0x1242
decl %eax
movl %eax, 0x2e2a(%rip)      # 0x4060
movl (%r13,%rax,4), %r12d
jmp 0x124a
movl %ebx, %r12d
jmp 0x1284
callq 0x1050
movl %eax, %r12d
movslq %r12d, %rax
testb $0x8, 0x1(%rbp,%rax,2)
jne 0x127c
movslq 0x2e05(%rip), %rcx      # 0x4060
cmpq $0x64, %rcx
jl 0x131a
leaq 0xdb6(%rip), %rdi       # 0x2022
callq 0x1030
movl 0x2de9(%rip), %eax      # 0x4060
jmp 0x1328
movq (%r15), %rbp
movzwl (%rbp,%rax,2), %ecx
movl $0x0, (%r14)
xorl %eax, %eax
btl $0xb, %ecx
jae 0x12d4
movl (%r14), %eax
leal (%rax,%rax,4), %eax
leal (%r12,%rax,2), %eax
addl $-0x30, %eax
movl %eax, (%r14)
movl 0x2db7(%rip), %eax      # 0x4060
testl %eax, %eax
jle 0x12bc
decl %eax
movl %eax, 0x2dab(%rip)      # 0x4060
movl (%r13,%rax,4), %r12d
jmp 0x12c7
callq 0x1050
movl %eax, %r12d
movq (%r15), %rbp
movslq %r12d, %rax
testb $0x8, 0x1(%rbp,%rax,2)
jne 0x1293
movl (%r14), %eax
movl %eax, %ecx
negl %ecx
cmpl $0x2d, %ebx
cmovnel %eax, %ecx
movl %ecx, (%r14)
cmpl $-0x1, %r12d
je 0x1302
movslq 0x2d72(%rip), %rax      # 0x4060
cmpq $0x64, %rax
jl 0x130a
leaq 0xd27(%rip), %rdi       # 0x2022
callq 0x1030
jmp 0x1374
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x1374
leal 0x1(%rax), %ecx
movl %ecx, 0x2d4d(%rip)      # 0x4060
movl %r12d, (%r13,%rax,4)
jmp 0x1374
leal 0x1(%rcx), %eax
movl %eax, 0x2d3d(%rip)      # 0x4060
movl %r12d, (%r13,%rcx,4)
cmpl $0x64, %eax
jge 0x1353
xorl %ecx, %ecx
cmpl $0x2d, %ebx
sete %cl
leal 0x2b(,%rcx,2), %ecx
leal 0x1(%rax), %edx
movl %edx, 0x2d1b(%rip)      # 0x4060
cltq
movl %ecx, (%r13,%rax,4)
jmp 0x1371
cmpl $0x64, %eax
jl 0x1361
leaq 0xcc8(%rip), %rdi       # 0x2022
callq 0x1030
jmp 0x1371
leal 0x1(%rax), %ecx
movl %ecx, 0x2cf6(%rip)      # 0x4060
cltq
movl %ebx, (%r13,%rax,4)
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq