#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 *, char **, int /*<<< orphan*/);
unsigned long int secureStr2ul(char *arg) {
  unsigned long int value = 0;
  char *fin;
  if (!arg)
    return 0;
  value = strtoul(arg, &fin, 0);
  if (fin == arg)
    return 0;
  if (((value == 0) || (value == ULONG_MAX)) && (errno == ERANGE))
    return 0;
  return value;
}


x86 assembly (clang, -O0):
<secureStr2ul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113c <secureStr2ul+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <secureStr2ul+0xa8>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    116a <secureStr2ul+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <secureStr2ul+0xa8>
cmpq   $0x0,-0x18(%rbp)
je     1189 <secureStr2ul+0x79>
mov    -0x18(%rbp),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <secureStr2ul+0xa0>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <secureStr2ul+0xa0>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <secureStr2ul+0xa8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secureStr2ul>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1157 <secureStr2ul+0x47>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
cmp    %rbx,0x8(%rsp)
je     1157 <secureStr2ul+0x47>
test   %rax,%rax
je     1141 <secureStr2ul+0x31>
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%rax
jne    1159 <secureStr2ul+0x49>
mov    0x2e90(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e96(%rip),%rdx
cmp    (%rdx),%rcx
jne    1159 <secureStr2ul+0x49>
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 i64 @secureStr2ul(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 0, i64* %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i64 0, i64* %2
br label %30
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strtoul(i8* %10, i8** %5, i32 0)
store i64 %11, i64* %4
%12 = load i8*, i8** %5
%13 = load i8*, i8** %3
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %16
15: ; preds = %9
store i64 0, i64* %2
br label %30
16: ; preds = %9
%17 = load i64, i64* %4
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i64, i64* %4
%21 = load i64, i64* @ULONG_MAX
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %19, %16
%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
store i64 0, i64* %2
br label %30
28: ; preds = %23, %19
%29 = load i64, i64* %4
store i64 %29, i64* %2
br label %30
30: ; preds = %28, %27, %15, %8
%31 = load i64, i64* %2
ret i64 %31
}

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