#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 int intmax_t;

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ errno;
int strtoimax(char const *, char **, int);
int str2int(const char *s, int min, int max, int def) {
  char *endptr;
  errno = 0;
  intmax_t x = strtoimax(s, &endptr, 10);
  if (errno == ERANGE) {
    return def;
  }
  if (x < min || x > max) {
    return def;
  }
  return x;
}


x86 assembly (clang, -O0):
<str2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2eac(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoimax@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    0x2e8f(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <str2int+0x5d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <str2int+0x86>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1185 <str2int+0x75>
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    1190 <str2int+0x80>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <str2int+0x86>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r14d
mov    0x2eb3(%rip),%r15
movq   $0x0,(%r15)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtoimax@plt>
mov    (%r15),%rdx
cmp    %ebp,%eax
mov    %eax,%ecx
cmovg  %ebx,%ecx
mov    0x2e9e(%rip),%rsi
cmp    %r14d,%eax
cmovl  %ebx,%ecx
cmp    (%rsi),%rdx
cmove  %ebx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i32 @str2int(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
store i64 0, i64* @errno
%7 = call i32 @strtoimax(i8* %0, i8** %5, i32 10)
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
%11 = icmp slt i32 %7, %1
%12 = icmp sgt i32 %7, %2
%13 = or i1 %11, %12
%14 = select i1 %10, i1 true, i1 %13
%15 = select i1 %14, i32 %3, i32 %7
ret i32 %15
}