#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 */

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ error(char *, char const *);
scalar_t__ isspace(unsigned char);
long strtol(char const *, char **, int);
int getn(const char *s) {
  char *p;
  long r;
  errno = 0;
  r = strtol(s, &p, 10);
  if (s == p)
    error("%s: bad number", s);
  if (errno != 0)
    error((errno == EINVAL) ? "%s: bad number" : "%s: out of range", s);
  while (isspace((unsigned char)*p))
    p++;
  if (*p)
    error("%s: bad number", s);
  return (int)r;
}


x86 assembly (clang, -O0):
<getn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    117e <getn+0x4e>
mov    -0x8(%rbp),%rsi
lea    0xe87(%rip),%rdi
call   1050 <error@plt>
mov    0x2e5b(%rip),%rax
cmpq   $0x0,(%rax)
je     11c1 <getn+0x91>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rcx
mov    0x2e38(%rip),%rax
mov    (%rax),%rdx
lea    0xe65(%rip),%rdi
lea    0xe4f(%rip),%rax
cmp    %rdx,%rcx
cmove  %rax,%rdi
mov    -0x8(%rbp),%rsi
call   1050 <error@plt>
jmp    11c6 <getn+0x96>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
je     11ed <getn+0xbd>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11c6 <getn+0x96>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     120a <getn+0xda>
mov    -0x8(%rbp),%rsi
lea    0xdfb(%rip),%rdi
call   1050 <error@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getn>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e9e(%rip),%r12
movq   $0x0,(%r12)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r14
mov    (%rsp),%rbx
cmp    %r15,%rbx
jne    1172 <getn+0x42>
lea    0xe96(%rip),%rdi
mov    %r15,%rsi
call   1050 <error@plt>
mov    (%r12),%rax
test   %rax,%rax
je     11a0 <getn+0x70>
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
lea    0xe74(%rip),%rax
lea    0xe7c(%rip),%rdi
cmove  %rax,%rdi
mov    %r15,%rsi
call   1050 <error@plt>
nop
movzbl (%rbx),%edi
call   1030 <isspace@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    11a0 <getn+0x70>
cmpb   $0x0,-0x1(%rbx)
je     11c6 <getn+0x96>
lea    0xe42(%rip),%rdi
mov    %r15,%rsi
call   1050 <error@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [15 x i8] c"%s: bad number\00"
@EINVAL = global i64 0
@.str.1 = [17 x i8] c"%s: out of range\00"
define i32 @getn(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtol(i8* %5, i8** %3, i32 10)
store i64 %6, i64* %4
%7 = load i8*, i8** %2
%8 = load i8*, i8** %3
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %10, %1
%14 = load i64, i64* @errno
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINVAL
%19 = icmp eq i64 %17, %18
%20 = zext i1 %19 to i64
%21 = select i1 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0)
%22 = load i8*, i8** %2
%23 = call i32 @error(i8* %21, i8* %22)
br label %24
24: ; preds = %16, %13
br label %25
25: ; preds = %30, %24
%26 = load i8*, i8** %3
%27 = load i8, i8* %26
%28 = call i64 @isspace(i8 zeroext %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %3
br label %25
33: ; preds = %25
%34 = load i8*, i8** %3
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %2
%39 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %37, %33
%41 = load i64, i64* %4
%42 = trunc i64 %41 to i32
ret i32 %42
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [15 x i8] c"%s: bad number\00"
@EINVAL = global i64 0
@.str.1 = [17 x i8] c"%s: out of range\00"
define i32 @getn(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %1
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = load i64, i64* @EINVAL
%14 = icmp eq i64 %10, %13
%15 = select i1 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0)
%16 = tail call i32 @error(i8* %15, i8* %0)
br label %17
17: ; preds = %12, %9
br label %18
18: ; preds = %17, %18
%19 = phi i8* [ %23, %18 ], [ %5, %17 ]
%20 = load i8, i8* %19
%21 = tail call i64 @isspace(i8 zeroext %20)
%22 = icmp eq i64 %21, 0
%23 = getelementptr inbounds i8, i8* %19, i64 1
br i1 %22, label %24, label %18
24: ; preds = %18
%25 = load i8, i8* %19
%26 = icmp eq i8 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %27, %24
%30 = trunc i64 %4 to i32
ret i32 %30
}