#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 UINT_MAX;
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
int str2uint(const char *val, unsigned int *result) {
  char *end;
  unsigned long int temp;
  if (*val == '\0')
    return -1;
  temp = strtoul(val, &end, 0);
  if (*end != '\0')
    return -1;
  if (temp > UINT_MAX)
    return -1;
  *result = temp;
  return 0;
}


x86 assembly (clang, -O0):
<str2uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    113c <str2uint+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <str2uint+0x8e>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116b <str2uint+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <str2uint+0x8e>
mov    -0x28(%rbp),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jbe    118b <str2uint+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <str2uint+0x8e>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2uint>:
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
cmpb   $0x0,(%rdi)
je     1145 <str2uint+0x35>
mov    %rsi,%r14
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1145 <str2uint+0x35>
mov    0x2ead(%rip),%rcx
cmp    (%rcx),%rax
ja     1145 <str2uint+0x35>
mov    %eax,(%r14)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@UINT_MAX = global i64 0
define i32 @str2uint(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = call i64 @strtoul(i8* %0, i8** %3, i32 0)
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
%12 = load i64, i64* @UINT_MAX
%13 = icmp ugt i64 %8, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = trunc i64 %8 to i32
store i32 %16, i32* %1
br label %17
17: ; preds = %7, %2, %15
%18 = phi i32 [ 0, %15 ], [ -1, %2 ], [ -1, %7 ]
ret i32 %18
}