#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 UINT32_MAX;
unsigned long strtoul(char const *, char **, int);
int parse_unsigned_int(unsigned int *value, const char *str) {
  unsigned long tmp;
  char *end;
  tmp = strtoul(str, &end, 10);
  if (!*str || *end || (tmp > UINT32_MAX && tmp != -1))
    return -1;
  *value = tmp == -1 ? UINT32_MAX : (unsigned int)tmp;
  return 0;
}


x86 assembly (clang, -O0):
<parse_unsigned_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1172 <parse_unsigned_int+0x62>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1172 <parse_unsigned_int+0x62>
mov    -0x20(%rbp),%rax
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%rax
jbe    117e <parse_unsigned_int+0x6e>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     117e <parse_unsigned_int+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <parse_unsigned_int+0xa9>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    119c <parse_unsigned_int+0x8c>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11a6 <parse_unsigned_int+0x96>
mov    -0x20(%rbp),%rax
mov    %eax,%eax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_unsigned_int>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpb   $0x0,(%rbx)
je     1161 <parse_unsigned_int+0x51>
mov    (%rsp),%rdx
cmpb   $0x0,(%rdx)
jne    1161 <parse_unsigned_int+0x51>
mov    0x2eb1(%rip),%rdx
mov    (%rdx),%rdx
cmp    $0xffffffffffffffff,%rcx
je     1155 <parse_unsigned_int+0x45>
cmp    %rdx,%rcx
ja     1161 <parse_unsigned_int+0x51>
cmp    $0xffffffffffffffff,%rcx
cmovne %ecx,%edx
mov    %edx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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