#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 int LONG_MAX;
unsigned int LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
unsigned int strtoul(char const *, char **, int);
unsigned int read_unsigned_int(const char *str, bool *success) {
  char *endptr;
  unsigned int val;
  val = strtoul(str, &endptr, 10);
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
      (errno != 0 && val == 0)) {
    fprintf(stderr, "Invalid value: %s\n", str);
    *success = false;
    return 0;
  }
  if (endptr == str) {
    fprintf(stderr, "Invalid value: %s\n", str);
    *success = false;
    return 0;
  }
  *success = true;
  return val;
}


x86 assembly (clang, -O0):
<read_unsigned_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e8a(%rip),%rcx
cmp    (%rcx),%rax
jne    1183 <read_unsigned_int+0x63>
mov    -0x24(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
je     119e <read_unsigned_int+0x7e>
mov    -0x24(%rbp),%eax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%eax
je     119e <read_unsigned_int+0x7e>
mov    0x2e36(%rip),%rax
cmpq   $0x0,(%rax)
je     11cd <read_unsigned_int+0xad>
cmpl   $0x0,-0x24(%rbp)
jne    11cd <read_unsigned_int+0xad>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4e(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    121a <read_unsigned_int+0xfa>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    120a <read_unsigned_int+0xea>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    121a <read_unsigned_int+0xfa>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_unsigned_int>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    0x2e82(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e98(%rip),%rdx
cmp    (%rdx),%rcx
jne    1163 <read_unsigned_int+0x43>
mov    0x2e74(%rip),%rdx
cmp    (%rdx),%eax
je     117d <read_unsigned_int+0x5d>
mov    0x2e89(%rip),%rdx
cmp    (%rdx),%eax
je     117d <read_unsigned_int+0x5d>
test   %rcx,%rcx
setne  %cl
test   %eax,%eax
sete   %dl
test   %dl,%cl
jne    117d <read_unsigned_int+0x5d>
mov    $0x1,%ecx
cmp    %rbx,(%rsp)
jne    1199 <read_unsigned_int+0x79>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
xor    %ecx,%ecx
xor    %eax,%eax
mov    %ecx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i32 0
@LONG_MIN = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Invalid value: %s\0A\00"
define i32 @read_unsigned_int(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32 @strtoul(i8* %8, i8** %6, i32 10)
store i32 %9, i32* %7
%10 = load i64, i64* @errno
%11 = load i64, i64* @ERANGE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* %7
%15 = load i32, i32* @LONG_MAX
%16 = icmp eq i32 %14, %15
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = load i32, i32* %7
%19 = load i32, i32* @LONG_MIN
%20 = icmp eq i32 %18, %19
br i1 %20, label %27, label %21
21: ; preds = %17, %2
%22 = load i64, i64* @errno
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24, %17, %13
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %5
store i32 0, i32* %31
store i32 0, i32* %3
br label %44
32: ; preds = %24, %21
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %4
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %5
store i32 0, i32* %40
store i32 0, i32* %3
br label %44
41: ; preds = %32
%42 = load i32*, i32** %5
store i32 1, i32* %42
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %44
44: ; preds = %41, %36, %27
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i32 0
@LONG_MIN = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Invalid value: %s\0A\00"
define i32 @read_unsigned_int(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @strtoul(i8* %0, i8** %3, i32 10)
%6 = load i64, i64* @errno
%7 = load i64, i64* @ERANGE
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @LONG_MAX
%11 = icmp eq i32 %5, %10
%12 = load i32, i32* @LONG_MIN
%13 = icmp eq i32 %5, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %22, label %15
15: ; preds = %9, %2
%16 = icmp ne i64 %6, 0
%17 = icmp eq i32 %5, 0
%18 = select i1 %16, i1 %17, i1 false
%19 = load i8*, i8** %3
%20 = icmp eq i8* %19, %0
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %22, label %25
22: ; preds = %9, %15
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %15, %22
%26 = phi i32 [ 0, %22 ], [ 1, %15 ]
%27 = phi i32 [ 0, %22 ], [ %5, %15 ]
store i32 %26, i32* %1
ret i32 %27
}