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


x86 assembly (clang, -O0):
<parse_ul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    0x2e90(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11be <parse_ul+0x9e>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a0 <parse_ul+0x80>
mov    -0x28(%rbp),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jbe    11ac <parse_ul+0x8c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11be <parse_ul+0x9e>
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):
<parse_ul>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xecf(%rip),%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
je     1166 <parse_ul+0x46>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
mov    $0xfffffffe,%ecx
jne    1175 <parse_ul+0x55>
mov    0x2e81(%rip),%rdx
cmp    (%rdx),%rax
jbe    1170 <parse_ul+0x50>
jmp    1175 <parse_ul+0x55>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"unlimited\00"
@LONG_MAX = 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
br label %17
9: ; preds = %2
%10 = call i64 @strtoul(i8* %0, i8** %3, i32 10)
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
%14 = load i64, i64* @LONG_MAX
%15 = icmp ugt i64 %10, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %19, label %17
17: ; preds = %9, %7
%18 = phi i64 [ %8, %7 ], [ %10, %9 ]
store i64 %18, i64* %1
br label %19
19: ; preds = %17, %9
%20 = phi i32 [ -2, %9 ], [ 0, %17 ]
ret i32 %20
}