#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 /*<<< orphan*/ EXIT_FAILURE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
void testStrtol() {
  char *str = "12abc";
  char *endPtr;
  long val = strtol(str, &endPtr, 10);
  if (endPtr == str) {
    fprintf(stderr, "No digits were found\n");
    exit(EXIT_FAILURE);
  }
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))) {
    fprintf(stderr, "ERROR: number out of range for LONG\n");
    exit(EXIT_FAILURE);
  }
  if (errno != 0 && val == 0) {
    perror("strtol");
    exit(EXIT_FAILURE);
  }
  printf("success, ret = %ld and endPrt = %s", val, endPtr);
}


x86 assembly (clang, -O0):
<testStrtol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11aa <testStrtol+0x5a>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <testStrtol+0xbf>
mov    -0x18(%rbp),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     11ec <testStrtol+0x9c>
mov    -0x18(%rbp),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <testStrtol+0xbf>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2daa(%rip),%rax
cmpq   $0x0,(%rax)
je     1245 <testStrtol+0xf5>
cmpq   $0x0,-0x18(%rbp)
jne    1245 <testStrtol+0xf5>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testStrtol>:
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     11c4 <testStrtol+0x74>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e57(%rip),%rsi
cmp    (%rsi),%rcx
jne    11a6 <testStrtol+0x56>
mov    0x2e33(%rip),%rsi
cmp    (%rsi),%rax
je     11e4 <testStrtol+0x94>
mov    0x2e47(%rip),%rsi
cmp    (%rsi),%rax
je     11e4 <testStrtol+0x94>
test   %rcx,%rcx
je     11b0 <testStrtol+0x60>
test   %rax,%rax
je     11d6 <testStrtol+0x86>
lea    0xe91(%rip),%rdi
mov    %rax,%rsi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
jmp    11f4 <testStrtol+0xa4>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
jmp    11f9 <testStrtol+0xa9>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"12abc\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"No digits were found\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str.2 = [37 x i8] c"ERROR: number out of range for LONG\0A\00"
@.str.3 = [7 x i8] c"strtol\00"
@.str.4 = [35 x i8] c"success, ret = %ld and endPrt = %s\00"
define void @testStrtol() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %1
%4 = load i8*, i8** %1
%5 = call i64 @strtol(i8* %4, i8** %2, i32 10)
store i64 %5, i64* %3
%6 = load i8*, i8** %2
%7 = load i8*, i8** %1
%8 = icmp eq i8* %6, %7
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i64, i64* %3
%20 = load i64, i64* @LONG_MAX
%21 = icmp eq i64 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i64, i64* %3
%24 = load i64, i64* @LONG_MIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %22, %18
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %22, %14
%32 = load i64, i64* @errno
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i64, i64* %3
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %34, %31
%42 = load i64, i64* %3
%43 = load i8*, i8** %2
%44 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i64 %42, i8* %43)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"12abc\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"No digits were found\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str.2 = [37 x i8] c"ERROR: number out of range for LONG\0A\00"
@.str.3 = [7 x i8] c"strtol\00"
@.str.4 = [35 x i8] c"success, ret = %ld and endPrt = %s\00"
define void @testStrtol() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = call i64 @strtol(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %1, i32 10)
%4 = load i8*, i8** %1
%5 = icmp eq i8* %4, getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0)
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load i64, i64* @LONG_MAX
%17 = icmp eq i64 %3, %16
%18 = load i64, i64* @LONG_MIN
%19 = icmp eq i64 %3, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %15, %11
%27 = icmp ne i64 %12, 0
%28 = icmp eq i64 %3, 0
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %26
%35 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i64 %3, i8* %4)
ret void
}