#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 */
#define EINVAL 129
#define ERANGE 128
int errno;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strtol(char const *, char **, int);
int main() {
  const char *pNum[] = {"376",  "+123", "-123", "+ 123", "- 123", "abc",
                        "-abc", "567",  "-765", "0x87a", "032",   NULL};
  int i = 0;
  for (i = 0; pNum[i] != NULL; i++) {
    int j = 0;
    for (j = 0; j <= 33; j++) {
      char *p = NULL;
      errno = 0;
      printf("strtol(%s, &p, %d) = [%ld]\t", pNum[i], j,
             strtol(pNum[i], &p, j));
      printf("p = [%s]\t", NULL == p ? "NULL" : p);
      switch (errno) {
      case EINVAL:
        printf("errno = [EINVAL]");
        break;
      case ERANGE:
        printf("errno = [ERANGE]");
        break;
      default:
        printf("errno = [%d]", errno);
        break;
      }
      printf("\n");
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
lea    0x2c63(%rip),%rsi
mov    $0x60,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x74(%rbp)
movl   $0x0,-0x74(%rbp)
movslq -0x74(%rbp),%rax
cmpq   $0x0,-0x70(%rbp,%rax,8)
je     12d6 <main+0x1a6>
movl   $0x0,-0x78(%rbp)
movl   $0x0,-0x78(%rbp)
cmpl   $0x21,-0x78(%rbp)
jg     12ba <main+0x18a>
movq   $0x0,-0x80(%rbp)
mov    0x2e3c(%rip),%rax
movl   $0x0,(%rax)
movslq -0x74(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rax
mov    %rax,-0x90(%rbp)
mov    -0x78(%rbp),%eax
mov    %eax,-0x84(%rbp)
movslq -0x74(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdi
mov    -0x78(%rbp),%edx
lea    -0x80(%rbp),%rsi
call   1040 <strtol@plt>
mov    -0x90(%rbp),%rsi
mov    -0x84(%rbp),%edx
mov    %eax,%ecx
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
cmp    -0x80(%rbp),%rax
jne    120c <main+0xdc>
lea    0xe58(%rip),%rax
mov    %rax,-0x98(%rbp)
jmp    1217 <main+0xe7>
mov    -0x80(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rsi
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x9c(%rbp)
sub    $0x80,%eax
je     1274 <main+0x144>
jmp    124b <main+0x11b>
mov    -0x9c(%rbp),%eax
sub    $0x81,%eax
jne    1287 <main+0x157>
jmp    1261 <main+0x131>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129e <main+0x16e>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129e <main+0x16e>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1183 <main+0x53>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1165 <main+0x35>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x2c6b(%rip),%rax
mov    0x2e8c(%rip),%rbp
lea    0xedf(%rip),%r15
lea    0xefe(%rip),%r14
lea    0xeed(%rip),%rbx
xor    %ecx,%ecx
jmp    119c <main+0x6c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x2c2f(%rip),%rax
mov    0x10(%rsp),%rcx
lea    (%rax,%rcx,8),%rax
add    $0x8,%rax
add    $0x1,%rcx
cmp    $0xb,%rcx
je     124c <main+0x11c>
mov    %rcx,0x10(%rsp)
mov    (%rax),%r12
xor    %r13d,%r13d
jmp    11d2 <main+0xa2>
nopl   0x0(%rax)
lea    0xea6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r13d
cmp    $0x22,%r13d
je     1170 <main+0x40>
movq   $0x0,0x8(%rsp)
movl   $0x0,0x0(%rbp)
mov    %r12,%rdi
lea    0x8(%rsp),%rsi
mov    %r13d,%edx
call   1050 <strtol@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
cmove  %r14,%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x0(%rbp),%esi
cmp    $0x80,%esi
je     1240 <main+0x110>
cmp    $0x81,%esi
je     11b0 <main+0x80>
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11be <main+0x8e>
nopl   (%rax)
lea    0xe27(%rip),%rdi
jmp    11b7 <main+0x87>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"376\00"
@.str.1 = [5 x i8] c"+123\00"
@.str.2 = [5 x i8] c"-123\00"
@.str.3 = [6 x i8] c"+ 123\00"
@.str.4 = [6 x i8] c"- 123\00"
@.str.5 = [4 x i8] c"abc\00"
@.str.6 = [5 x i8] c"-abc\00"
@.str.7 = [4 x i8] c"567\00"
@.str.8 = [5 x i8] c"-765\00"
@.str.9 = [6 x i8] c"0x87a\00"
@.str.10 = [4 x i8] c"032\00"
@__const.main.pNum = [12 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i32 0, i32 0), i8* null]
@errno = global i32 0
@.str.11 = [28 x i8] c"strtol(%s, &p, %d) = [%ld]\09\00"
@.str.12 = [10 x i8] c"p = [%s]\09\00"
@.str.13 = [5 x i8] c"NULL\00"
@.str.14 = [17 x i8] c"errno = [EINVAL]\00"
@.str.15 = [17 x i8] c"errno = [ERANGE]\00"
@.str.16 = [13 x i8] c"errno = [%d]\00"
@.str.17 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8*]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
%6 = bitcast [12 x i8*]* %2 to i8*
store i32 0, i32* %3
store i32 0, i32* %3
br label %7
7: ; preds = %53, %0
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [12 x i8*], [12 x i8*]* %2, i64 0, i64 %9
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %56
13: ; preds = %7
store i32 0, i32* %4
store i32 0, i32* %4
br label %14
14: ; preds = %48, %13
%15 = load i32, i32* %4
%16 = icmp sle i32 %15, 33
br i1 %16, label %17, label %51
17: ; preds = %14
store i8* null, i8** %5
store i32 0, i32* @errno
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [12 x i8*], [12 x i8*]* %2, i64 0, i64 %19
%21 = load i8*, i8** %20
%22 = load i32, i32* %4
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [12 x i8*], [12 x i8*]* %2, i64 0, i64 %24
%26 = load i8*, i8** %25
%27 = load i32, i32* %4
%28 = call i32 @strtol(i8* %26, i8** %5, i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0), i8* %21, i32 %22, i32 %28)
%30 = load i8*, i8** %5
%31 = icmp eq i8* null, %30
br i1 %31, label %32, label %33
32: ; preds = %17
br label %35
33: ; preds = %17
%34 = load i8*, i8** %5
br label %35
35: ; preds = %33, %32
%36 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0), %32 ], [ %34, %33 ]
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @errno
switch i32 %38, label %43 [
i32 129, label %39
i32 128, label %41
]
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.14, i64 0, i64 0))
br label %46
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.15, i64 0, i64 0))
br label %46
43: ; preds = %35
%44 = load i32, i32* @errno
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.16, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %41, %39
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.17, i64 0, i64 0))
br label %48
48: ; preds = %46
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
br label %14
51: ; preds = %14
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.17, i64 0, i64 0))
br label %53
53: ; preds = %51
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 1
store i32 %55, i32* %3
br label %7
56: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"376\00"
@.str.1 = [5 x i8] c"+123\00"
@.str.2 = [5 x i8] c"-123\00"
@.str.3 = [6 x i8] c"+ 123\00"
@.str.4 = [6 x i8] c"- 123\00"
@.str.5 = [4 x i8] c"abc\00"
@.str.6 = [5 x i8] c"-abc\00"
@.str.7 = [4 x i8] c"567\00"
@.str.8 = [5 x i8] c"-765\00"
@.str.9 = [6 x i8] c"0x87a\00"
@.str.10 = [4 x i8] c"032\00"
@__const.main.pNum = [12 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i32 0, i32 0), i8* null]
@errno = global i32 0
@.str.11 = [28 x i8] c"strtol(%s, &p, %d) = [%ld]\09\00"
@.str.12 = [10 x i8] c"p = [%s]\09\00"
@.str.13 = [5 x i8] c"NULL\00"
@.str.14 = [17 x i8] c"errno = [EINVAL]\00"
@.str.15 = [17 x i8] c"errno = [ERANGE]\00"
@.str.16 = [13 x i8] c"errno = [%d]\00"
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
br label %3
3: ; preds = %0, %26
%4 = phi i64 [ 0, %0 ], [ %28, %26 ]
%5 = phi i8** [ getelementptr inbounds ([12 x i8*], [12 x i8*]* @__const.main.pNum, i64 0, i64 0), %0 ], [ %29, %26 ]
%6 = load i8*, i8** %5
br label %7
7: ; preds = %3, %22
%8 = phi i32 [ 0, %3 ], [ %24, %22 ]
store i8* null, i8** %1
store i32 0, i32* @errno
%9 = call i32 @strtol(i8* %6, i8** %1, i32 %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0), i8* %6, i32 %8, i32 %9)
%11 = load i8*, i8** %1
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0), i8* %11
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @errno
switch i32 %15, label %20 [
i32 129, label %16
i32 128, label %18
]
16: ; preds = %7
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.14, i64 0, i64 0))
br label %22
18: ; preds = %7
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.15, i64 0, i64 0))
br label %22
20: ; preds = %7
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.16, i64 0, i64 0), i32 %15)
br label %22
22: ; preds = %20, %18, %16
%23 = tail call i32 @putchar(i32 10)
%24 = add nuw nsw i32 %8, 1
%25 = icmp eq i32 %24, 34
br i1 %25, label %26, label %7
26: ; preds = %22
%27 = tail call i32 @putchar(i32 10)
%28 = add nuw nsw i64 %4, 1
%29 = getelementptr inbounds [12 x i8*], [12 x i8*]* @__const.main.pNum, i64 0, i64 %28
%30 = icmp eq i64 %28, 11
br i1 %30, label %31, label %3
31: ; preds = %26
ret i32 0
}