#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__ errno;
int handle_errors(unsigned long, char **, char *);
int /*<<< orphan*/ isalnum(char const);
int ret_ERANGE();
unsigned long strtoul(char const *, char **, int);
unsigned bb_strtou(const char *arg, char **endp, int base) {
  unsigned long v;
  char *endptr;
  if (!isalnum(arg[0]))
    return ret_ERANGE();
  errno = 0;
  v = strtoul(arg, &endptr, base);
  if (v > (~0U))
    return ret_ERANGE();
  return handle_errors(v, endp, endptr);
}


x86 assembly (clang, -O0):
<bb_strtou>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%eax
jne    1177 <bb_strtou+0x37>
mov    $0x0,%al
call   1030 <ret_ERANGE@plt>
mov    %eax,-0x4(%rbp)
jmp    11cb <bb_strtou+0x8b>
mov    0x2e62(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x30(%rbp),%rsi
call   1050 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    %rax,-0x28(%rbp)
jbe    11b7 <bb_strtou+0x77>
mov    $0x0,%al
call   1030 <ret_ERANGE@plt>
mov    %eax,-0x4(%rbp)
jmp    11cb <bb_strtou+0x8b>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <handle_errors@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bb_strtou>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1040 <isalnum@plt>
test   %eax,%eax
je     1182 <bb_strtou+0x42>
mov    0x2e7d(%rip),%rax
movq   $0x0,(%rax)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <strtoul@plt>
mov    %rax,%rcx
shr    $0x20,%rcx
je     1191 <bb_strtou+0x51>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <ret_ERANGE@plt>
mov    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <handle_errors@plt>

LLVM IR (-O0):
@errno = global i64 0
define i32 @bb_strtou(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = call i32 @isalnum(i8 signext %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = call i32 (...) @ret_ERANGE()
store i32 %16, i32* %4
br label %30
17: ; preds = %3
store i64 0, i64* @errno
%18 = load i8*, i8** %5
%19 = load i32, i32* %7
%20 = call i64 @strtoul(i8* %18, i8** %9, i32 %19)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = icmp ugt i64 %21, 4294967295
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (...) @ret_ERANGE()
store i32 %24, i32* %4
br label %30
25: ; preds = %17
%26 = load i64, i64* %8
%27 = load i8**, i8*** %6
%28 = load i8*, i8** %9
%29 = call i32 @handle_errors(i64 %26, i8** %27, i8* %28)
store i32 %29, i32* %4
br label %30
30: ; preds = %25, %23, %15
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
define i32 @bb_strtou(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = load i8, i8* %0
%7 = tail call i32 @isalnum(i8 signext %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 (...) @ret_ERANGE()
br label %19
11: ; preds = %3
store i64 0, i64* @errno
%12 = call i64 @strtoul(i8* %0, i8** %4, i32 %2)
%13 = icmp ugt i64 %12, 4294967295
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (...) @ret_ERANGE()
br label %19
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = tail call i32 @handle_errors(i64 %12, i8** %1, i8* %17)
br label %19
19: ; preds = %16, %14, %9
%20 = phi i32 [ %15, %14 ], [ %18, %16 ], [ %10, %9 ]
ret i32 %20
}