#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 */
long ULLONG_MAX;
long ULONG_MAX;
scalar_t__ errno;
int muggle_str_lstrip_idx(char *);
long strtoul(char const *, char **, int);
int muggle_str_tou(const char *str, unsigned int *pval, int base) {
  long ret;
  char *endptr;
  if (str == NULL || pval == NULL) {
    return 0;
  }
  errno = 0;
  ret = strtoul(str, &endptr, base);
  if (endptr == str) {
    return 0;
  } else if (*endptr != '\0') {
    if (muggle_str_lstrip_idx(endptr) != -1) {
      return 0;
    }
  }
  if (ret == ULONG_MAX || ret == ULLONG_MAX) {
    return 0;
  }
  *pval = (unsigned int)ret;
  return 1;
}


x86 assembly (clang, -O0):
<muggle_str_tou>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <muggle_str_tou+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1155 <muggle_str_tou+0x35>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_tou+0xf0>
mov    0x2e7c(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x30(%rbp),%rsi
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1191 <muggle_str_tou+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_tou+0xf0>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c4 <muggle_str_tou+0xa4>
mov    -0x30(%rbp),%rdi
call   1030 <muggle_str_lstrip_idx@plt>
cmp    $0xffffffff,%eax
je     11bf <muggle_str_tou+0x9f>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_tou+0xf0>
jmp    11c4 <muggle_str_tou+0xa4>
jmp    11c9 <muggle_str_tou+0xa9>
mov    -0x28(%rbp),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
je     11f1 <muggle_str_tou+0xd1>
mov    -0x28(%rbp),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11fd <muggle_str_tou+0xdd>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_tou+0xf0>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<muggle_str_tou>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     118c <muggle_str_tou+0x6c>
mov    %rsi,%rbx
test   %rsi,%rsi
je     118c <muggle_str_tou+0x6c>
mov    %rdi,%rbp
mov    0x2e97(%rip),%rax
movq   $0x0,(%rax)
mov    %rsp,%rsi
call   1040 <strtoul@plt>
mov    (%rsp),%rdi
cmp    %rbp,%rdi
je     118c <muggle_str_tou+0x6c>
mov    %rax,%r14
cmpb   $0x0,(%rdi)
je     116b <muggle_str_tou+0x4b>
call   1030 <muggle_str_lstrip_idx@plt>
cmp    $0xffffffff,%eax
jne    118c <muggle_str_tou+0x6c>
mov    0x2e56(%rip),%rax
cmp    (%rax),%r14
je     118c <muggle_str_tou+0x6c>
mov    0x2e6a(%rip),%rax
cmp    (%rax),%r14
je     118c <muggle_str_tou+0x6c>
mov    %r14d,(%rbx)
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ULONG_MAX = global i64 0
@ULLONG_MAX = global i64 0
define i32 @muggle_str_tou(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
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 0, i32* %4
br label %49
16: ; preds = %12
store i64 0, i64* @errno
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = call i64 @strtoul(i8* %17, i8** %9, i32 %18)
store i64 %19, i64* %8
%20 = load i8*, i8** %9
%21 = load i8*, i8** %5
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 0, i32* %4
br label %49
24: ; preds = %16
%25 = load i8*, i8** %9
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = load i8*, i8** %9
%31 = call i32 @muggle_str_lstrip_idx(i8* %30)
%32 = icmp ne i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 0, i32* %4
br label %49
34: ; preds = %29
br label %35
35: ; preds = %34, %24
br label %36
36: ; preds = %35
%37 = load i64, i64* %8
%38 = load i64, i64* @ULONG_MAX
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i64, i64* %8
%42 = load i64, i64* @ULLONG_MAX
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %36
store i32 0, i32* %4
br label %49
45: ; preds = %40
%46 = load i64, i64* %8
%47 = trunc i64 %46 to i32
%48 = load i32*, i32** %6
store i32 %47, i32* %48
store i32 1, i32* %4
br label %49
49: ; preds = %45, %44, %33, %23, %15
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@errno = global i64 0
@ULONG_MAX = global i64 0
@ULLONG_MAX = global i64 0
define i32 @muggle_str_tou(i8* %0, i32* writeonly %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp eq i8* %0, null
%7 = icmp eq i32* %1, null
%8 = or i1 %6, %7
br i1 %8, label %27, label %9
9: ; preds = %3
store i64 0, i64* @errno
%10 = call i64 @strtoul(i8* %0, i8** %4, i32 %2)
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, %0
br i1 %12, label %27, label %13
13: ; preds = %9
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @muggle_str_lstrip_idx(i8* %11)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %13, %16
%20 = load i64, i64* @ULONG_MAX
%21 = icmp eq i64 %10, %20
%22 = load i64, i64* @ULLONG_MAX
%23 = icmp eq i64 %10, %22
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = trunc i64 %10 to i32
store i32 %26, i32* %1
br label %27
27: ; preds = %19, %16, %9, %3, %25
%28 = phi i32 [ 1, %25 ], [ 0, %3 ], [ 0, %9 ], [ 0, %16 ], [ 0, %19 ]
ret i32 %28
}