#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 */
typedef unsigned long uint32_t;

/* Variables and functions */
scalar_t__ ERANGE;
int /*<<< orphan*/ EX_USAGE;
unsigned long UINT32_MAX;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
uint32_t strtou32(const char *str) {
  unsigned long v;
  char *cp;
  errno = 0;
  v = strtoul(str, &cp, 0);
  if (cp == str || *cp != '\0')
    errx(EX_USAGE, "Invalid number '%s'", str);
  if (v > UINT32_MAX)
    errno = ERANGE;
  if (errno == ERANGE)
    err(EX_USAGE, "Invalid number '%s'", str);
  return (v);
}


x86 assembly (clang, -O0):
<strtou32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     117b <strtou32+0x4b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1194 <strtou32+0x64>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe71(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jbe    11bc <strtou32+0x8c>
mov    0x2e29(%rip),%rax
mov    (%rax),%rcx
mov    0x2e0f(%rip),%rax
mov    %rcx,(%rax)
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <strtou32+0xbf>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe16(%rip),%rsi
call   1040 <err@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtou32>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e85(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
je     1168 <strtou32+0x38>
cmpb   $0x0,(%rax)
je     1180 <strtou32+0x50>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
mov    %rbx,%rdx
call   1030 <errx@plt>
mov    0x2e71(%rip),%rax
cmp    (%rax),%r14
jbe    119b <strtou32+0x6b>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
jmp    11aa <strtou32+0x7a>
mov    (%r15),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jne    11c2 <strtou32+0x92>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    %rbx,%rdx
call   1040 <err@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EX_USAGE = global i32 0
@.str = [20 x i8] c"Invalid number '%s'\00"
@UINT32_MAX = global i64 0
@ERANGE = global i64 0
define i64 @strtou32(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtoul(i8* %5, i8** %4, i32 0)
store i64 %6, i64* %3
%7 = load i8*, i8** %4
%8 = load i8*, i8** %2
%9 = icmp eq i8* %7, %8
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10, %1
%16 = load i32, i32* @EX_USAGE
%17 = load i8*, i8** %2
%18 = call i32 @errx(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %10
%20 = load i64, i64* %3
%21 = load i64, i64* @UINT32_MAX
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i64, i64* @ERANGE
store i64 %24, i64* @errno
br label %25
25: ; preds = %23, %19
%26 = load i64, i64* @errno
%27 = load i64, i64* @ERANGE
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @EX_USAGE
%31 = load i8*, i8** %2
%32 = call i32 @err(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %29, %25
%34 = load i64, i64* %3
ret i64 %34
}

LLVM IR (-O2):
@errno = global i64 0
@EX_USAGE = global i32 0
@.str = [20 x i8] c"Invalid number '%s'\00"
@UINT32_MAX = global i64 0
@ERANGE = global i64 0
define i64 @strtou32(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtoul(i8* %0, i8** %2, i32 0)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7, %1
%11 = load i32, i32* @EX_USAGE
%12 = tail call i32 @errx(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %13
13: ; preds = %10, %7
%14 = load i64, i64* @UINT32_MAX
%15 = icmp ugt i64 %4, %14
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i64, i64* @ERANGE
store i64 %17, i64* @errno
br label %22
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %16, %18
%23 = load i32, i32* @EX_USAGE
%24 = tail call i32 @err(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %22, %18
ret i64 %4
}