#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;
long INT_MAX;
long INT_MIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
int cw_atoi(char *s) {
  char *end = NULL;
  long temp = strtol(s, &end, 10);
  if (end != s && errno != ERANGE && temp >= INT_MIN && temp <= INT_MAX) {
    return (int)temp;
  }
  fprintf(stderr, "Warning: Invalid integer value '%s'\n", s);
  return -1;
}


x86 assembly (clang, -O0):
<cw_atoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11a6 <cw_atoi+0x86>
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
je     11a6 <cw_atoi+0x86>
mov    -0x20(%rbp),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jl     11a6 <cw_atoi+0x86>
mov    -0x20(%rbp),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
jg     11a6 <cw_atoi+0x86>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11c6 <cw_atoi+0xa6>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe46(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cw_atoi>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
cmp    %rbx,0x8(%rsp)
je     1175 <cw_atoi+0x55>
mov    0x2e72(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e80(%rip),%rdx
cmp    (%rdx),%rcx
je     1175 <cw_atoi+0x55>
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%rax
jl     1175 <cw_atoi+0x55>
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jle    1192 <cw_atoi+0x72>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@stderr = global i32 0
@.str = [37 x i8] c"Warning: Invalid integer value '%s'\0A\00"
define i32 @cw_atoi(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
%6 = load i8*, i8** %3
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
%8 = load i8*, i8** %4
%9 = load i8*, i8** %3
%10 = icmp ne i8* %8, %9
br i1 %10, label %11, label %26
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load i64, i64* %5
%17 = load i64, i64* @INT_MIN
%18 = icmp sge i64 %16, %17
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i64, i64* %5
%21 = load i64, i64* @INT_MAX
%22 = icmp sle i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i64, i64* %5
%25 = trunc i64 %24 to i32
store i32 %25, i32* %2
br label %30
26: ; preds = %19, %15, %11, %1
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %3
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32 -1, i32* %2
br label %30
30: ; preds = %26, %23
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@stderr = global i32 0
@.str = [37 x i8] c"Warning: Invalid integer value '%s'\0A\00"
define i32 @cw_atoi(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%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 %19, label %7
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @INT_MIN
%12 = icmp slt i64 %4, %11
%13 = select i1 %10, i1 true, i1 %12
%14 = load i64, i64* @INT_MAX
%15 = icmp sgt i64 %4, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %19, label %17
17: ; preds = %7
%18 = trunc i64 %4 to i32
br label %22
19: ; preds = %7, %1
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %19, %17
%23 = phi i32 [ %18, %17 ], [ -1, %19 ]
ret i32 %23
}