#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__ errno;
unsigned long strtoul(char const *, char **, int);
int strtoul_ui(char const *s, int base, unsigned int *result) {
  unsigned long ul;
  char *p;
  errno = 0;
  ul = strtoul(s, &p, base);
  if (errno || *p || p == s || (unsigned int)ul != ul)
    return -1;
  *result = ul;
  return 0;
}


x86 assembly (clang, -O0):
<strtoul_ui>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2eb6(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%rsi
call   1030 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e94(%rip),%rax
cmpq   $0x0,(%rax)
jne    1184 <strtoul_ui+0x74>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1184 <strtoul_ui+0x74>
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1184 <strtoul_ui+0x74>
mov    -0x28(%rbp),%rax
mov    %eax,%eax
cmp    -0x28(%rbp),%rax
je     1190 <strtoul_ui+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <strtoul_ui+0x93>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoul_ui>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%edx
mov    %rdi,%rbx
mov    0x2eb8(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
call   1030 <strtoul@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpq   $0x0,(%r15)
jne    1164 <strtoul_ui+0x54>
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     1164 <strtoul_ui+0x54>
cmpb   $0x0,(%rdx)
jne    1164 <strtoul_ui+0x54>
mov    %rcx,%rdx
shr    $0x20,%rdx
jne    1164 <strtoul_ui+0x54>
mov    %ecx,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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