#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 LONG_MAX;
scalar_t__ errno;
int /*<<< orphan*/ strcmp(char const *, char *);
scalar_t__ strtoul(char const *, char **, int);
int parse_ul(const char *str, unsigned long *val) {
  char *tail;
  if (!strcmp(str, "unlimited")) {
    *val = LONG_MAX;
    return 0;
  }
  errno = 0;
  *val = (int)strtoul(str, (char **)&tail, 10);
  if (*tail != '\0' || errno == ERANGE)
    return -2;
  return 0;
}


x86 assembly (clang, -O0):
<parse_ul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1166 <parse_ul+0x46>
mov    0x2e88(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11cd <parse_ul+0xad>
mov    0x2e63(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ba <parse_ul+0x9a>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <parse_ul+0xa6>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11cd <parse_ul+0xad>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ul>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeca(%rip),%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
je     118d <parse_ul+0x6d>
mov    0x2e8a(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtoul@plt>
cltq
mov    %rax,(%r14)
mov    0x8(%rsp),%rcx
mov    $0xfffffffe,%eax
cmpb   $0x0,(%rcx)
jne    119c <parse_ul+0x7c>
mov    (%r15),%rax
mov    0x2e6b(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
setne  %dl
lea    (%rdx,%rdx,1),%eax
add    $0xfffffffe,%eax
jmp    119c <parse_ul+0x7c>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"unlimited\00"
@LONG_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
define i32 @parse_ul(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%7 = load i8*, i8** %4
%8 = call i32 @strcmp(i8* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = icmp ne i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i64, i64* @LONG_MAX
%12 = load i64*, i64** %5
store i64 %11, i64* %12
store i32 0, i32* %3
br label %29
13: ; preds = %2
store i64 0, i64* @errno
%14 = load i8*, i8** %4
%15 = call i64 @strtoul(i8* %14, i8** %6, i32 10)
%16 = trunc i64 %15 to i32
%17 = sext i32 %16 to i64
%18 = load i64*, i64** %5
store i64 %17, i64* %18
%19 = load i8*, i8** %6
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %13
%24 = load i64, i64* @errno
%25 = load i64, i64* @ERANGE
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %13
store i32 -2, i32* %3
br label %29
28: ; preds = %23
store i32 0, i32* %3
br label %29
29: ; preds = %28, %27, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [10 x i8] c"unlimited\00"
@LONG_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
define i32 @parse_ul(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i64, i64* @LONG_MAX
store i64 %8, i64* %1
br label %21
9: ; preds = %2
store i64 0, i64* @errno
%10 = call i64 @strtoul(i8* %0, i8** %3, i32 10)
%11 = shl i64 %10, 32
%12 = ashr exact i64 %11, 32
store i64 %12, i64* %1
%13 = load i8*, i8** %3
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %9
%17 = load i64, i64* @errno
%18 = load i64, i64* @ERANGE
%19 = icmp eq i64 %17, %18
%20 = select i1 %19, i32 -2, i32 0
br label %21
21: ; preds = %16, %9, %7
%22 = phi i32 [ 0, %7 ], [ -2, %9 ], [ %20, %16 ]
ret i32 %22
}