#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;
unsigned long ULONG_MAX;
scalar_t__ errno;
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
int xstrtoul(const char *str, unsigned long *valuep) {
  unsigned long value;
  char *endp;
  value = strtoul(str, &endp, 0);
  if (*endp || (value == ULONG_MAX && errno == ERANGE))
    return -1;
  *valuep = value;
  return 0;
}


x86 assembly (clang, -O0):
<xstrtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1171 <xstrtoul+0x61>
mov    -0x20(%rbp),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
jne    117d <xstrtoul+0x6d>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
jne    117d <xstrtoul+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    118f <xstrtoul+0x7f>
mov    -0x20(%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):
<xstrtoul>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,%rcx
mov    0x8(%rsp),%rdx
mov    $0xffffffff,%eax
cmpb   $0x0,(%rdx)
jne    115d <xstrtoul+0x4d>
mov    0x2e8b(%rip),%rdx
cmp    (%rdx),%rcx
jne    1158 <xstrtoul+0x48>
mov    0x2e8f(%rip),%rdx
mov    (%rdx),%rdx
mov    0x2e95(%rip),%rsi
cmp    (%rsi),%rdx
je     115d <xstrtoul+0x4d>
mov    %rcx,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@ULONG_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
define i32 @xstrtoul(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i64 @strtoul(i8* %0, i8** %3, i32 0)
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i64, i64* @ULONG_MAX
%11 = icmp eq i64 %5, %10
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @ERANGE
%15 = icmp eq i64 %13, %14
br i1 %15, label %17, label %16
16: ; preds = %12, %9
store i64 %5, i64* %1
br label %17
17: ; preds = %2, %12, %16
%18 = phi i32 [ 0, %16 ], [ -1, %12 ], [ -1, %2 ]
ret i32 %18
}