#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;
scalar_t__ strchr(char const *, char);
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;
  if (strchr(s, '-'))
    return -1;
  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    0x2ea6(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     1165 <strtoul_ui+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <strtoul_ui+0xb7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%rsi
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e60(%rip),%rax
cmpq   $0x0,(%rax)
jne    11b8 <strtoul_ui+0x98>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b8 <strtoul_ui+0x98>
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11b8 <strtoul_ui+0x98>
mov    -0x28(%rbp),%rax
mov    %eax,%eax
cmp    -0x28(%rbp),%rax
je     11c4 <strtoul_ui+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <strtoul_ui+0xb7>
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   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2ea5(%rip),%r12
movq   $0x0,(%r12)
mov    $0x2d,%esi
call   1030 <strchr@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
jne    118c <strtoul_ui+0x6c>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <strtoul@plt>
cmpq   $0x0,(%r12)
jne    118c <strtoul_ui+0x6c>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     118c <strtoul_ui+0x6c>
cmpb   $0x0,(%rcx)
jne    118c <strtoul_ui+0x6c>
mov    %rax,%rcx
shr    $0x20,%rcx
jne    118c <strtoul_ui+0x6c>
mov    %eax,(%r14)
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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 = call i64 @strchr(i8* %10, i8 signext 45)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %40
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = call i64 @strtoul(i8* %15, i8** %9, i32 %16)
store i64 %17, i64* %8
%18 = load i64, i64* @errno
%19 = icmp ne i64 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %14
%21 = load i8*, i8** %9
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %35, label %25
25: ; preds = %20
%26 = load i8*, i8** %9
%27 = load i8*, i8** %5
%28 = icmp eq i8* %26, %27
br i1 %28, label %35, label %29
29: ; preds = %25
%30 = load i64, i64* %8
%31 = trunc i64 %30 to i32
%32 = zext i32 %31 to i64
%33 = load i64, i64* %8
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %29, %25, %20, %14
store i32 -1, i32* %4
br label %40
36: ; preds = %29
%37 = load i64, i64* %8
%38 = trunc i64 %37 to i32
%39 = load i32*, i32** %7
store i32 %38, i32* %39
store i32 0, i32* %4
br label %40
40: ; preds = %36, %35, %13
%41 = load i32, i32* %4
ret i32 %41
}

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 = tail call i64 @strchr(i8* %0, i8 signext 45)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %3
%9 = call i64 @strtoul(i8* %0, i8** %4, i32 %1)
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
%16 = icmp ne i8* %13, %0
%17 = and i1 %16, %15
%18 = icmp ult i64 %9, 4294967296
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = trunc i64 %9 to i32
store i32 %21, i32* %2
br label %22
22: ; preds = %8, %12, %3, %20
%23 = phi i32 [ 0, %20 ], [ -1, %3 ], [ -1, %12 ], [ -1, %8 ]
ret i32 %23
}