#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 */
typedef scalar_t__ int32_t;

/* Variables and functions */
int EINVAL;
int ERANGE;
long long INT32_MAX;
long long INT32_MIN;
int errno;
long long strtoll(char const *, char **, int);
int _checkstrto32(const char *str, int32_t *ret, char **endptr) {
  long long ll;
  errno = 0;
  ll = strtoll(str, endptr, 10);
  if (*endptr == str)
    return -EINVAL;
  if (ll > INT32_MAX || ll < INT32_MIN)
    return -ERANGE;
  if (!ll && errno != 0)
    return -errno;
  *ret = (int32_t)ll;
  return 0;
}


x86 assembly (clang, -O0):
<_checkstrto32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2ead(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoll@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    116b <_checkstrto32+0x5b>
xor    %eax,%eax
mov    0x2e5f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <_checkstrto32+0xd6>
mov    -0x28(%rbp),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jg     1193 <_checkstrto32+0x83>
mov    -0x28(%rbp),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jge    11a6 <_checkstrto32+0x96>
xor    %eax,%eax
mov    0x2e4c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <_checkstrto32+0xd6>
cmpq   $0x0,-0x28(%rbp)
jne    11d4 <_checkstrto32+0xc4>
mov    0x2e20(%rip),%rax
cmpl   $0x0,(%rax)
je     11d4 <_checkstrto32+0xc4>
xor    %eax,%eax
mov    0x2e0e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <_checkstrto32+0xd6>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_checkstrto32>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb0(%rip),%r12
movl   $0x0,(%r12)
mov    %rdx,%rsi
mov    $0xa,%edx
call   1030 <strtoll@plt>
cmp    %rbx,(%r15)
je     116b <_checkstrto32+0x5b>
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
jg     1174 <_checkstrto32+0x64>
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
jl     1174 <_checkstrto32+0x64>
test   %rax,%rax
jne    118d <_checkstrto32+0x7d>
mov    (%r12),%ecx
test   %ecx,%ecx
je     118d <_checkstrto32+0x7d>
neg    %ecx
jmp    117f <_checkstrto32+0x6f>
mov    0x2e4e(%rip),%rax
jmp    117b <_checkstrto32+0x6b>
mov    0x2e6d(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rax,(%r14)
xor    %ecx,%ecx
jmp    117f <_checkstrto32+0x6f>

LLVM IR (-O0):
@errno = global i32 0
@EINVAL = global i32 0
@INT32_MAX = global i64 0
@INT32_MIN = global i64 0
@ERANGE = global i32 0
define i32 @_checkstrto32(i8* %0, i64* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8**
%8 = alloca i64
store i8* %0, i8** %5
store i64* %1, i64** %6
store i8** %2, i8*** %7
store i32 0, i32* @errno
%9 = load i8*, i8** %5
%10 = load i8**, i8*** %7
%11 = call i64 @strtoll(i8* %9, i8** %10, i32 10)
store i64 %11, i64* %8
%12 = load i8**, i8*** %7
%13 = load i8*, i8** %12
%14 = load i8*, i8** %5
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %4
br label %42
19: ; preds = %3
%20 = load i64, i64* %8
%21 = load i64, i64* @INT32_MAX
%22 = icmp sgt i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* %8
%25 = load i64, i64* @INT32_MIN
%26 = icmp slt i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23, %19
%28 = load i32, i32* @ERANGE
%29 = sub nsw i32 0, %28
store i32 %29, i32* %4
br label %42
30: ; preds = %23
%31 = load i64, i64* %8
%32 = icmp ne i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* @errno
%38 = sub nsw i32 0, %37
store i32 %38, i32* %4
br label %42
39: ; preds = %33, %30
%40 = load i64, i64* %8
%41 = load i64*, i64** %6
store i64 %40, i64* %41
store i32 0, i32* %4
br label %42
42: ; preds = %39, %36, %27, %16
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
@INT32_MAX = global i64 0
@INT32_MIN = global i64 0
@ERANGE = global i32 0
define i32 @_checkstrto32(i8* %0, i64* nocapture writeonly %1, i8** nocapture %2) {
store i32 0, i32* @errno
%4 = tail call i64 @strtoll(i8* %0, i8** %2, i32 10)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %27
10: ; preds = %3
%11 = load i64, i64* @INT32_MAX
%12 = icmp sgt i64 %4, %11
%13 = load i64, i64* @INT32_MIN
%14 = icmp slt i64 %4, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* @ERANGE
%18 = sub nsw i32 0, %17
br label %27
19: ; preds = %10
%20 = icmp eq i64 %4, 0
%21 = load i32, i32* @errno
%22 = icmp ne i32 %21, 0
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = sub nsw i32 0, %21
br label %27
26: ; preds = %19
store i64 %4, i64* %1
br label %27
27: ; preds = %26, %24, %16, %7
%28 = phi i32 [ %9, %7 ], [ %18, %16 ], [ %25, %24 ], [ 0, %26 ]
ret i32 %28
}