#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint64_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int LINE_MAX;
int /*<<< orphan*/ err(int, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ isblank(unsigned char);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strtoumax(char *, char **, int /*<<< orphan*/);
uint64_t read_num_buf(void) {
  uint64_t val;
  char *p, buf[LINE_MAX];
  for (;;) {
    if (fgets(buf, sizeof(buf), stdin) == NULL) {
      if (ferror(stdin))
        err(1, "stdin");
      exit(0);
    }
    for (p = buf; isblank((unsigned char)*p); ++p)
      ;
    if (*p == '\n' || *p == '\0')
      continue;
    if (*p == '-')
      errx(1, "negative numbers aren't permitted.");
    errno = 0;
    val = strtoumax(buf, &p, 0);
    if (errno)
      err(1, "%s", buf);
    if (*p != '\n')
      errx(1, "%s: illegal numeric format.", buf);
    return (val);
  }
}


x86 assembly (clang, -O0):
<read_num_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11fc <read_num_buf+0x8c>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11f5 <read_num_buf+0x85>
mov    $0x1,%edi
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1050 <isblank@plt>
cmp    $0x0,%rax
je     1230 <read_num_buf+0xc0>
jmp    121f <read_num_buf+0xaf>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1204 <read_num_buf+0x94>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1250 <read_num_buf+0xe0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1255 <read_num_buf+0xe5>
jmp    11a8 <read_num_buf+0x38>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    1278 <read_num_buf+0x108>
mov    $0x1,%edi
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d4d(%rip),%rax
movq   $0x0,(%rax)
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtoumax@plt>
mov    %eax,-0x4(%rbp)
mov    0x2d31(%rip),%rax
cmpq   $0x0,(%rax)
je     12c0 <read_num_buf+0x150>
mov    -0x28(%rbp),%rdx
mov    $0x1,%edi
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     12e7 <read_num_buf+0x177>
mov    -0x28(%rbp),%rdx
mov    $0x1,%edi
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x4(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_num_buf>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x38(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e3e(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     12af <read_num_buf+0x13f>
lea    0x1(%rbx),%r15
jmp    11e8 <read_num_buf+0x78>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     12af <read_num_buf+0x13f>
mov    %rbx,-0x30(%rbp)
movzbl (%rbx),%edi
call   1050 <isblank@plt>
mov    %r15,%r13
mov    %rbx,%rcx
test   %rax,%rax
je     121e <read_num_buf+0xae>
nop
mov    %r13,-0x30(%rbp)
movzbl 0x0(%r13),%edi
call   1050 <isblank@plt>
add    $0x1,%r13
test   %rax,%rax
jne    1200 <read_num_buf+0x90>
add    $0xffffffffffffffff,%r13
mov    %r13,%rcx
mov    (%rcx),%al
test   %al,%al
je     11d0 <read_num_buf+0x60>
cmp    $0xa,%al
je     11d0 <read_num_buf+0x60>
cmp    $0x2d,%al
jne    123f <read_num_buf+0xcf>
lea    0xdd3(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <errx@plt>
mov    0x2d8a(%rip),%r15
movq   $0x0,(%r15)
lea    -0x30(%rbp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <strtoumax@plt>
mov    %eax,%r14d
cmpq   $0x0,(%r15)
je     127a <read_num_buf+0x10a>
lea    0xdbe(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <err@plt>
mov    -0x30(%rbp),%rax
cmpb   $0xa,(%rax)
je     1299 <read_num_buf+0x129>
lea    0xda2(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <errx@plt>
mov    -0x38(%rbp),%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%edi
call   1030 <ferror@plt>
test   %rax,%rax
je     12d0 <read_num_buf+0x160>
lea    0xd3c(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <err@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@LINE_MAX = global i32 0
@stdin = global i32 0
@.str = [6 x i8] c"stdin\00"
@.str.1 = [35 x i8] c"negative numbers aren't permitted.\00"
@errno = global i64 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [28 x i8] c"%s: illegal numeric format.\00"
define i32 @read_num_buf() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = load i32, i32* @LINE_MAX
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %3
%8 = alloca i8, i64 %6
store i64 %6, i64* %4
br label %9
9: ; preds = %42, %0
%10 = trunc i64 %6 to i32
%11 = load i32, i32* @stdin
%12 = call i32* @fgets(i8* %8, i32 %10, i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load i32, i32* @stdin
%16 = call i64 @ferror(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %9
store i8* %8, i8** %2
br label %23
23: ; preds = %29, %22
%24 = load i8*, i8** %2
%25 = load i8, i8* %24
%26 = call i64 @isblank(i8 zeroext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
br label %29
29: ; preds = %28
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %2
br label %23
32: ; preds = %23
%33 = load i8*, i8** %2
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %42, label %37
37: ; preds = %32
%38 = load i8*, i8** %2
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37, %32
br label %9
43: ; preds = %37
%44 = load i8*, i8** %2
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 45
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
store i64 0, i64* @errno
%51 = call i32 @strtoumax(i8* %8, i8** %2, i32 0)
store i32 %51, i32* %1
%52 = load i64, i64* @errno
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %56
56: ; preds = %54, %50
%57 = load i8*, i8** %2
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %59, 10
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %63
63: ; preds = %61, %56
%64 = load i32, i32* %1
%65 = load i8*, i8** %3
ret i32 %64
}

LLVM IR (-O2):
@LINE_MAX = global i32 0
@stdin = global i32 0
@.str = [6 x i8] c"stdin\00"
@.str.1 = [35 x i8] c"negative numbers aren't permitted.\00"
@errno = global i64 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [28 x i8] c"%s: illegal numeric format.\00"
define i32 @read_num_buf() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = load i32, i32* @LINE_MAX
%4 = zext i32 %3 to i64
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %4
%7 = load i32, i32* @stdin
%8 = call i32* @fgets(i8* %6, i32 %3, i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %0, %31
store i8* %6, i8** %1
%11 = load i8, i8* %6
%12 = call i64 @isblank(i8 zeroext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %28, label %22
14: ; preds = %31, %0
%15 = load i32, i32* @stdin
%16 = call i64 @ferror(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %10, %22
%23 = phi i8* [ %24, %22 ], [ %6, %10 ]
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8* %24, i8** %1
%25 = load i8, i8* %24
%26 = call i64 @isblank(i8 zeroext %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %22
28: ; preds = %22, %10
%29 = phi i8* [ %6, %10 ], [ %24, %22 ]
%30 = load i8, i8* %29
switch i8 %30, label %37 [
i8 10, label %31
i8 0, label %31
i8 45, label %35
]
31: ; preds = %28, %28
%32 = load i32, i32* @stdin
%33 = call i32* @fgets(i8* %6, i32 %3, i32 %32)
%34 = icmp eq i32* %33, null
br i1 %34, label %14, label %10
35: ; preds = %28
%36 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %28, %35
store i64 0, i64* @errno
%38 = call i32 @strtoumax(i8* %6, i8** %1, i32 0)
%39 = load i64, i64* @errno
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %43
43: ; preds = %41, %37
%44 = load i8*, i8** %1
%45 = load i8, i8* %44
%46 = icmp eq i8 %45, 10
br i1 %46, label %49, label %47
47: ; preds = %43
%48 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %49
49: ; preds = %47, %43
ret i32 %38
}