#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 */
int EINVAL;
int ERANGE;
unsigned long long ULLONG_MAX;
int errno;
unsigned long long strtoll(char const *, char **, int);
bool kcn_strtoull(const char *s, unsigned long long min, unsigned long long max,
                  unsigned long long *p) {
  char *ep;
  int oerrno;
  unsigned long long llval;
  oerrno = errno;
  errno = 0;
  llval = strtoll(s, &ep, 10);
  if (s[0] == '\0' || *ep != '\0') {
    errno = EINVAL;
    return false;
  }
  if (errno == ERANGE && (llval == ULLONG_MAX || llval == 0))
    return false;
  if (llval < min || llval > max) {
    errno = ERANGE;
    return false;
  }
  errno = oerrno;
  *p = llval;
  return true;
}


x86 assembly (clang, -O0):
<kcn_strtoull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e95(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoll@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1177 <kcn_strtoull+0x67>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1195 <kcn_strtoull+0x85>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e49(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1230 <kcn_strtoull+0x120>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
jne    11d8 <kcn_strtoull+0xc8>
mov    -0x40(%rbp),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
je     11cc <kcn_strtoull+0xbc>
cmpq   $0x0,-0x40(%rbp)
jne    11d8 <kcn_strtoull+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    1230 <kcn_strtoull+0x120>
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11f4 <kcn_strtoull+0xe4>
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1212 <kcn_strtoull+0x102>
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcc(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1230 <kcn_strtoull+0x120>
mov    -0x34(%rbp),%ecx
mov    0x2db4(%rip),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kcn_strtoull>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2ea2(%rip),%r13
mov    0x0(%r13),%ebp
movl   $0x0,0x0(%r13)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtoll@plt>
cmpb   $0x0,(%rbx)
je     1155 <kcn_strtoull+0x45>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1175 <kcn_strtoull+0x65>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x0(%r13)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e64(%rip),%rcx
mov    (%rcx),%edx
cmp    %edx,0x0(%r13)
jne    1197 <kcn_strtoull+0x87>
xor    %ecx,%ecx
test   %rax,%rax
je     1164 <kcn_strtoull+0x54>
mov    0x2e56(%rip),%rsi
cmp    (%rsi),%rax
je     1164 <kcn_strtoull+0x54>
cmp    %r12,%rax
jb     11af <kcn_strtoull+0x9f>
cmp    %r15,%rax
ja     11af <kcn_strtoull+0x9f>
mov    %ebp,0x0(%r13)
mov    %rax,(%r14)
mov    $0x1,%ecx
jmp    1164 <kcn_strtoull+0x54>
mov    %edx,0x0(%r13)
jmp    1162 <kcn_strtoull+0x52>

LLVM IR (-O0):
@errno = global i32 0
@EINVAL = global i32 0
@ERANGE = global i32 0
@ULLONG_MAX = global i64 0
define i32 @kcn_strtoull(i8* %0, i64 %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%13 = load i32, i32* @errno
store i32 %13, i32* %11
store i32 0, i32* @errno
%14 = load i8*, i8** %6
%15 = call i64 @strtoll(i8* %14, i8** %10, i32 10)
store i64 %15, i64* %12
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %4
%22 = load i8*, i8** %10
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21, %4
%27 = load i32, i32* @EINVAL
store i32 %27, i32* @errno
store i32 0, i32* %5
br label %54
28: ; preds = %21
%29 = load i32, i32* @errno
%30 = load i32, i32* @ERANGE
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i64, i64* %12
%34 = load i64, i64* @ULLONG_MAX
%35 = icmp eq i64 %33, %34
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = load i64, i64* %12
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36, %32
store i32 0, i32* %5
br label %54
40: ; preds = %36, %28
%41 = load i64, i64* %12
%42 = load i64, i64* %7
%43 = icmp ult i64 %41, %42
br i1 %43, label %48, label %44
44: ; preds = %40
%45 = load i64, i64* %12
%46 = load i64, i64* %8
%47 = icmp ugt i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44, %40
%49 = load i32, i32* @ERANGE
store i32 %49, i32* @errno
store i32 0, i32* %5
br label %54
50: ; preds = %44
%51 = load i32, i32* %11
store i32 %51, i32* @errno
%52 = load i64, i64* %12
%53 = load i64*, i64** %9
store i64 %52, i64* %53
store i32 1, i32* %5
br label %54
54: ; preds = %50, %48, %39, %26
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
@ERANGE = global i32 0
@ULLONG_MAX = global i64 0
define i32 @kcn_strtoull(i8* %0, i64 %1, i64 %2, i64* nocapture writeonly %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = load i32, i32* @errno
store i32 0, i32* @errno
%8 = call i64 @strtoll(i8* %0, i8** %5, i32 10)
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %4
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11, %4
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
br label %32
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = load i32, i32* @ERANGE
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i64, i64* @ULLONG_MAX
%23 = icmp eq i64 %8, %22
%24 = icmp eq i64 %8, 0
%25 = or i1 %24, %23
br i1 %25, label %32, label %26
26: ; preds = %21, %17
%27 = icmp ult i64 %8, %1
%28 = icmp ugt i64 %8, %2
%29 = or i1 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 %19, i32* @errno
br label %32
31: ; preds = %26
store i32 %7, i32* @errno
store i64 %8, i64* %3
br label %32
32: ; preds = %21, %31, %30, %15
%33 = phi i32 [ 0, %15 ], [ 0, %30 ], [ 1, %31 ], [ 0, %21 ]
ret i32 %33
}