#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;
scalar_t__ ERANGE;
scalar_t__ errno;
unsigned long strtoul(char *, char **, int);
int libres_ulong(char *numstr, unsigned long *converted) {
  char *endptr = NULL;
  unsigned long uli;
  errno = 0;
  uli = strtoul(numstr, &endptr, 10);
  if (errno == ERANGE || endptr == numstr ||
      (*endptr != '\0' && *endptr != '\n')) {
    return -EINVAL;
  }
  *converted = uli;
  return 0;
}


x86 assembly (clang, -O0):
<libres_ulong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2ea9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%rax
je     1194 <libres_ulong+0x84>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1194 <libres_ulong+0x84>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a7 <libres_ulong+0x97>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     11a7 <libres_ulong+0x97>
xor    %eax,%eax
mov    0x2e33(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <libres_ulong+0xa9>
mov    -0x28(%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):
<libres_ulong>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    0x2ea9(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    (%r15),%rcx
mov    0x2e99(%rip),%rdx
cmp    (%rdx),%rcx
je     1170 <libres_ulong+0x60>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     1170 <libres_ulong+0x60>
mov    (%rcx),%cl
test   %cl,%cl
je     1169 <libres_ulong+0x59>
cmp    $0xa,%cl
jne    1170 <libres_ulong+0x60>
mov    %rax,(%r14)
xor    %eax,%eax
jmp    117b <libres_ulong+0x6b>
mov    0x2e59(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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