#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__ ERANGE;
int EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
int str_to_int(char *p) {
  char *end;
  for (long i = strtol(p, &end, 10); p != end; i = strtol(p, &end, 10)) {
    p = end;
    if (errno == ERANGE) {
      fprintf(stderr, "range error, got ");
      errno = 0;
    }
    return i;
  }
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<str_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11a1 <str_to_int+0x81>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1195 <str_to_int+0x75>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e42(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11ad <str_to_int+0x8d>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_int>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    (%rsp),%r15
cmp    %rbx,%r15
je     1196 <str_to_int+0x76>
mov    %rax,%r14
mov    0x2e85(%rip),%r12
mov    (%r12),%rax
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%rax
jne    1178 <str_to_int+0x58>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,(%r12)
cmp    %rbx,%r15
jne    1187 <str_to_int+0x67>
mov    0x2e44(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
cmp    %rbx,%r15
je     117d <str_to_int+0x5d>
jmp    1187 <str_to_int+0x67>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [18 x i8] c"range error, got \00"
@EXIT_FAILURE = global i32 0
define i32 @str_to_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
br label %8
8: ; preds = %23, %1
%9 = load i8*, i8** %3
%10 = load i8*, i8** %4
%11 = icmp ne i8* %9, %10
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = load i8*, i8** %4
store i8* %13, i8** %3
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
br label %20
20: ; preds = %17, %12
%21 = load i64, i64* %5
%22 = trunc i64 %21 to i32
store i32 %22, i32* %2
br label %28
23: ; No predecessors!
%24 = load i8*, i8** %3
%25 = call i64 @strtol(i8* %24, i8** %4, i32 10)
store i64 %25, i64* %5
br label %8
26: ; preds = %8
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [18 x i8] c"range error, got \00"
@EXIT_FAILURE = global i32 0
define i32 @str_to_int(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%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 %16, label %7
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
br label %14
14: ; preds = %11, %7
%15 = trunc i64 %4 to i32
br label %16
16: ; preds = %1, %14
%17 = phi i32 [ %15, %14 ], [ undef, %1 ]
%18 = load i32, i32* @EXIT_FAILURE
%19 = select i1 %6, i32 %18, i32 %17
ret i32 %19
}