#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;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
unsigned long strtoul(char const *, char **, int);
unsigned long xstrtoul(const char *s) {
  char *endp = NULL;
  errno = 0;
  unsigned long val = strtoul(s, &endp, 10);
  if ((errno == ERANGE && (val == ULONG_MAX || val == 0)) ||
      (errno != 0 && val == 0)) {
    perror("strtoul");
    exit(-1);
  }
  if (endp == s) {
    fprintf(stderr, "xstrtoul: strtoul error: %s\n", s);
    exit(-1);
  }
  return val;
}


x86 assembly (clang, -O0):
<xstrtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e75(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    11b1 <xstrtoul+0x71>
mov    -0x18(%rbp),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     11cd <xstrtoul+0x8d>
cmpq   $0x0,-0x18(%rbp)
je     11cd <xstrtoul+0x8d>
mov    0x2e18(%rip),%rax
cmpq   $0x0,(%rax)
je     11e3 <xstrtoul+0xa3>
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <xstrtoul+0xa3>
lea    0xe2c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1214 <xstrtoul+0xd4>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrtoul>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    0x2e7a(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    (%r14),%rcx
mov    0x2e74(%rip),%rdx
cmp    (%rdx),%rcx
jne    118c <xstrtoul+0x4c>
test   %rax,%rax
je     11a4 <xstrtoul+0x64>
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <xstrtoul+0x56>
jmp    11a4 <xstrtoul+0x64>
test   %rcx,%rcx
je     1196 <xstrtoul+0x56>
test   %rax,%rax
je     11a4 <xstrtoul+0x64>
cmp    %rbx,(%rsp)
je     11ba <xstrtoul+0x7a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe55(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
@.str = [8 x i8] c"strtoul\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"xstrtoul: strtoul error: %s\0A\00"
define i64 @xstrtoul(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %3
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtoul(i8* %5, i8** %3, i32 10)
store i64 %6, i64* %4
%7 = load i64, i64* @errno
%8 = load i64, i64* @ERANGE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i64, i64* %4
%12 = load i64, i64* @ULONG_MAX
%13 = icmp eq i64 %11, %12
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = load i64, i64* %4
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %14, %1
%18 = load i64, i64* @errno
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i64, i64* %4
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20, %14, %10
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %20, %17
%27 = load i8*, i8** %3
%28 = load i8*, i8** %2
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %2
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %26
%36 = load i64, i64* %4
ret i64 %36
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
@.str = [8 x i8] c"strtoul\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"xstrtoul: strtoul error: %s\0A\00"
define i64 @xstrtoul(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* null, i8** %2
store i64 0, i64* @errno
%4 = call i64 @strtoul(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i64, i64* @ULONG_MAX
%10 = icmp eq i64 %4, %9
%11 = icmp eq i64 %4, 0
%12 = or i1 %11, %10
br i1 %12, label %17, label %20
13: ; preds = %1
%14 = icmp ne i64 %5, 0
%15 = icmp eq i64 %4, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %20
17: ; preds = %13, %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %8, %13
%21 = load i8*, i8** %2
%22 = icmp eq i8* %21, %0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %20
ret i64 %4
}