#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 ULONG_MAX;
unsigned long strtoul(char const *, char **, int);
int extract_uint(const char *s, void *dest, char **errp) {
  unsigned long int value;
  char *endptr = NULL;
  value = strtoul(s, &endptr, 10);
  if (value == ULONG_MAX || value > UINT_MAX) {
    *errp = "Value too large";
    return -1;
  }
  if (*endptr != '\0') {
    *errp = "Trailing garbage";
    return -1;
  }
  *(unsigned int *)dest = value;
  return 0;
}


x86 assembly (clang, -O0):
<extract_uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
je     116a <extract_uint+0x5a>
mov    -0x28(%rbp),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
jbe    1184 <extract_uint+0x74>
mov    -0x20(%rbp),%rax
lea    0xe8b(%rip),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <extract_uint+0xb1>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ae <extract_uint+0x9e>
mov    -0x20(%rbp),%rax
lea    0xe71(%rip),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <extract_uint+0xb1>
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):
<extract_uint>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%rax
je     1159 <extract_uint+0x49>
mov    0x2ea6(%rip),%rcx
cmp    (%rcx),%rax
ja     1159 <extract_uint+0x49>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1170 <extract_uint+0x60>
lea    0xeb9(%rip),%rax
jmp    1160 <extract_uint+0x50>
lea    0xea0(%rip),%rax
mov    %rax,(%rbx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,(%r14)
xor    %eax,%eax
jmp    1168 <extract_uint+0x58>

LLVM IR (-O0):
@ULONG_MAX = global i64 0
@UINT_MAX = global i64 0
@.str = [16 x i8] c"Value too large\00"
@.str.1 = [17 x i8] c"Trailing garbage\00"
define i32 @extract_uint(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = call i64 @strtoul(i8* %10, i8** %9, i32 10)
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = load i64, i64* @ULONG_MAX
%14 = icmp eq i64 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i64, i64* %8
%17 = load i64, i64* @UINT_MAX
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15, %3
%20 = load i8**, i8*** %7
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %20
store i32 -1, i32* %4
br label %33
21: ; preds = %15
%22 = load i8*, i8** %9
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i8**, i8*** %7
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %27
store i32 -1, i32* %4
br label %33
28: ; preds = %21
%29 = load i64, i64* %8
%30 = trunc i64 %29 to i32
%31 = load i8*, i8** %6
%32 = bitcast i8* %31 to i32*
store i32 %30, i32* %32
store i32 0, i32* %4
br label %33
33: ; preds = %28, %26, %19
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@ULONG_MAX = global i64 0
@UINT_MAX = global i64 0
@.str = [16 x i8] c"Value too large\00"
@.str.1 = [17 x i8] c"Trailing garbage\00"
define i32 @extract_uint(i8* %0, i8* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%6 = call i64 @strtoul(i8* %0, i8** %4, i32 10)
%7 = load i64, i64* @ULONG_MAX
%8 = icmp eq i64 %6, %7
%9 = load i64, i64* @UINT_MAX
%10 = icmp ugt i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %2
br label %21
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %13
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %21
18: ; preds = %13
%19 = trunc i64 %6 to i32
%20 = bitcast i8* %1 to i32*
store i32 %19, i32* %20
br label %21
21: ; preds = %18, %17, %12
%22 = phi i32 [ -1, %12 ], [ -1, %17 ], [ 0, %18 ]
ret i32 %22
}