#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;
long UINT_MAX;
scalar_t__ errno;
long strtol(char const *, char **, int);
unsigned strtou(const char *s, int *err_or_overflow) {
  char *end;
  long sl;
  errno = 0;
  sl = strtol(s, &end, 10);
  if (end == s || *end != '\0' || errno == ERANGE || sl < 0 || sl > UINT_MAX) {
    *err_or_overflow = 1;
    return 0;
  }
  *err_or_overflow = 0;
  return sl;
}


x86 assembly (clang, -O0):
<strtou>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     119b <strtou+0x8b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    119b <strtou+0x8b>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
je     119b <strtou+0x8b>
cmpq   $0x0,-0x28(%rbp)
jl     119b <strtou+0x8b>
mov    -0x28(%rbp),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
jle    11b1 <strtou+0xa1>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11c2 <strtou+0xb2>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtou>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaa(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     114b <strtou+0x3b>
cmpb   $0x0,(%rcx)
je     115e <strtou+0x4e>
movl   $0x1,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%r15),%rcx
mov    0x2e78(%rip),%rdx
cmp    (%rdx),%rcx
je     114b <strtou+0x3b>
test   %rax,%rax
js     114b <strtou+0x3b>
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jg     114b <strtou+0x3b>
movl   $0x0,(%r14)
jmp    1154 <strtou+0x44>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@UINT_MAX = global i64 0
define i32 @strtou(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
store i64 0, i64* @errno
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i8** %6, i32 10)
store i64 %9, i64* %7
%10 = load i8*, i8** %6
%11 = load i8*, i8** %4
%12 = icmp eq i8* %10, %11
br i1 %12, label %29, label %13
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
br i1 %21, label %29, label %22
22: ; preds = %18
%23 = load i64, i64* %7
%24 = icmp slt i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i64, i64* %7
%27 = load i64, i64* @UINT_MAX
%28 = icmp sgt i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25, %22, %18, %13, %2
%30 = load i32*, i32** %5
store i32 1, i32* %30
store i32 0, i32* %3
br label %35
31: ; preds = %25
%32 = load i32*, i32** %5
store i32 0, i32* %32
%33 = load i64, i64* %7
%34 = trunc i64 %33 to i32
store i32 %34, i32* %3
br label %35
35: ; preds = %31, %29
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@UINT_MAX = global i64 0
define i32 @strtou(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, %0
br i1 %7, label %20, label %8
8: ; preds = %2
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp eq i64 %12, %13
%15 = icmp slt i64 %5, 0
%16 = select i1 %14, i1 true, i1 %15
%17 = load i64, i64* @UINT_MAX
%18 = icmp sgt i64 %5, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %20, label %21
20: ; preds = %11, %8, %2
store i32 1, i32* %1
br label %23
21: ; preds = %11
store i32 0, i32* %1
%22 = trunc i64 %5 to i32
br label %23
23: ; preds = %21, %20
%24 = phi i32 [ 0, %20 ], [ %22, %21 ]
ret i32 %24
}