#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__ ERANGE;
long LONG_MAX;
long LONG_MIN;
int atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
void print(char *buf) {
  int result = atoi(buf);
  printf("atoi(\"%s\") --> %d\n", buf, result);
  errno = 0;
  char *endptr;
  long int lresult = strtol(buf, &endptr, 10);
  printf("strtol(\"%s\", NULL, 10) --> %ld\n", buf, lresult);
  if (errno == ERANGE && (lresult == LONG_MAX || lresult == LONG_MIN))
    perror("strtol overflow/underflow");
  else if (errno == EINVAL)
    perror("strtol base contains unsupported value");
  else if (errno != 0)
    perror("strtol other error");
  if (endptr == buf)
    fprintf(stderr, "strtol didn't find any digits\n");
  printf("\n");
}


x86 assembly (clang, -O0):
<print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e3c(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <print+0xba>
mov    -0x20(%rbp),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
je     11f9 <print+0xa9>
mov    -0x20(%rbp),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <print+0xba>
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
jmp    125c <print+0x10c>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1235 <print+0xe5>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
jmp    1257 <print+0x107>
mov    0x2d84(%rip),%rax
cmpq   $0x0,(%rax)
je     1252 <print+0x102>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
jmp    1257 <print+0x107>
jmp    125c <print+0x10c>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    127f <print+0x12f>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1080 <atoi@plt>
lea    0xe88(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e35(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%rbx
lea    0xe65(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11e9 <print+0x89>
mov    0x2df7(%rip),%rcx
lea    0xe5b(%rip),%rdi
cmp    (%rcx),%rbx
je     1239 <print+0xd9>
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rbx
je     1239 <print+0xd9>
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
je     1229 <print+0xc9>
test   %rax,%rax
jne    1232 <print+0xd2>
cmp    %r14,0x8(%rsp)
jne    1216 <print+0xb6>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xa,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>
lea    0xe1d(%rip),%rdi
jmp    1239 <print+0xd9>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
cmp    %r14,0x8(%rsp)
jne    1216 <print+0xb6>
jmp    1201 <print+0xa1>

LLVM IR (-O0):
@.str = [19 x i8] c"atoi(\22%s\22) --> %d\0A\00"
@errno = global i64 0
@.str.1 = [32 x i8] c"strtol(\22%s\22, NULL, 10) --> %ld\0A\00"
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str.2 = [26 x i8] c"strtol overflow/underflow\00"
@EINVAL = global i64 0
@.str.3 = [39 x i8] c"strtol base contains unsupported value\00"
@.str.4 = [19 x i8] c"strtol other error\00"
@stderr = global i32 0
@.str.5 = [31 x i8] c"strtol didn't find any digits\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
define void @print(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @atoi(i8* %6)
store i32 %7, i32* %3
%8 = load i8*, i8** %2
%9 = load i32, i32* %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
store i64 0, i64* @errno
%11 = load i8*, i8** %2
%12 = call i64 @strtol(i8* %11, i8** %4, i32 10)
store i64 %12, i64* %5
%13 = load i8*, i8** %2
%14 = load i64, i64* %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %13, i64 %14)
%16 = load i64, i64* @errno
%17 = load i64, i64* @ERANGE
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %29
19: ; preds = %1
%20 = load i64, i64* %5
%21 = load i64, i64* @LONG_MAX
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* %5
%25 = load i64, i64* @LONG_MIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23, %19
%28 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %42
29: ; preds = %23, %1
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINVAL
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %41
35: ; preds = %29
%36 = load i64, i64* @errno
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
br label %41
41: ; preds = %40, %33
br label %42
42: ; preds = %41, %27
%43 = load i8*, i8** %4
%44 = load i8*, i8** %2
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %46, %42
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"atoi(\22%s\22) --> %d\0A\00"
@errno = global i64 0
@.str.1 = [32 x i8] c"strtol(\22%s\22, NULL, 10) --> %ld\0A\00"
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str.2 = [26 x i8] c"strtol overflow/underflow\00"
@EINVAL = global i64 0
@.str.3 = [39 x i8] c"strtol base contains unsupported value\00"
@.str.4 = [19 x i8] c"strtol other error\00"
@stderr = global i32 0
@.str.5 = [31 x i8] c"strtol didn't find any digits\0A\00"
define void @print(i8* %0) {
%2 = alloca i8*
%3 = tail call i32 @atoi(i8* %0)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %3)
store i64 0, i64* @errno
%5 = bitcast i8** %2 to i8*
%6 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %6)
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i64, i64* @LONG_MAX
%13 = icmp eq i64 %6, %12
%14 = load i64, i64* @LONG_MIN
%15 = icmp eq i64 %6, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %22, label %17
17: ; preds = %11, %1
%18 = load i64, i64* @EINVAL
%19 = icmp eq i64 %8, %18
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = icmp eq i64 %8, 0
br i1 %21, label %25, label %22
22: ; preds = %20, %17, %11
%23 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %17 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %20 ]
%24 = tail call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %20
%26 = load i8*, i8** %2
%27 = icmp eq i8* %26, %0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %28, %25
%32 = tail call i32 @putchar(i32 10)
ret void
}