#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;
long strtol(char const *, char **, int);
int strtosint(const char *s, int *err_or_overflow) {
  char *end;
  long sl;
  errno = 0;
  sl = strtol(s, &end, 10);
  if (end == s || *end != '\0' || errno == ERANGE || sl < INT_MIN ||
      sl > INT_MAX) {
    *err_or_overflow = 1;
    return 0;
  }
  *err_or_overflow = 0;
  return sl;
}


x86 assembly (clang, -O0):
<strtosint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11a4 <strtosint+0x94>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a4 <strtosint+0x94>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <strtosint+0x94>
mov    -0x28(%rbp),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jl     11a4 <strtosint+0x94>
mov    -0x28(%rbp),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jle    11ba <strtosint+0xaa>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11cb <strtosint+0xbb>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtosint>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea2(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     114b <strtosint+0x3b>
cmpb   $0x0,(%rcx)
je     115e <strtosint+0x4e>
movl   $0x1,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%r15),%rcx
mov    0x2e70(%rip),%rdx
cmp    (%rdx),%rcx
je     114b <strtosint+0x3b>
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jl     114b <strtosint+0x3b>
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jg     114b <strtosint+0x3b>
movl   $0x0,(%r14)
jmp    1154 <strtosint+0x44>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MIN = global i64 0
@INT_MAX = global i64 0
define i32 @strtosint(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 0, i64* @errno
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i8** %6, i32 10)
store i64 %9, i64* %7
%10 = load i8*, i8** %6
%11 = load i8*, i8** %4
%12 = icmp eq i8* %10, %11
br i1 %12, label %30, label %13
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
br i1 %21, label %30, label %22
22: ; preds = %18
%23 = load i64, i64* %7
%24 = load i64, i64* @INT_MIN
%25 = icmp slt i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i64, i64* %7
%28 = load i64, i64* @INT_MAX
%29 = icmp sgt i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26, %22, %18, %13, %2
%31 = load i32*, i32** %5
store i32 1, i32* %31
store i32 0, i32* %3
br label %36
32: ; preds = %26
%33 = load i32*, i32** %5
store i32 0, i32* %33
%34 = load i64, i64* %7
%35 = trunc i64 %34 to i32
store i32 %35, i32* %3
br label %36
36: ; preds = %32, %30
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MIN = global i64 0
@INT_MAX = global i64 0
define i32 @strtosint(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, %0
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @INT_MIN
%16 = icmp slt i64 %5, %15
%17 = select i1 %14, i1 true, i1 %16
%18 = load i64, i64* @INT_MAX
%19 = icmp sgt i64 %5, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %22
21: ; preds = %11, %8, %2
store i32 1, i32* %1
br label %24
22: ; preds = %11
store i32 0, i32* %1
%23 = trunc i64 %5 to i32
br label %24
24: ; preds = %22, %21
%25 = phi i32 [ 0, %21 ], [ %23, %22 ]
ret i32 %25
}