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


x86 assembly (clang, -O0):
<muggle_str_toul>:
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_toul+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1155 <muggle_str_toul+0x35>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_toul+0xf0>
mov    0x2e7c(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1040 <strtoul@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1197 <muggle_str_toul+0x77>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_toul+0xf0>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ca <muggle_str_toul+0xaa>
mov    -0x28(%rbp),%rdi
call   1030 <muggle_str_lstrip_idx@plt>
cmp    $0xffffffff,%eax
je     11c5 <muggle_str_toul+0xa5>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_toul+0xf0>
jmp    11ca <muggle_str_toul+0xaa>
jmp    11cf <muggle_str_toul+0xaf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
je     11fd <muggle_str_toul+0xdd>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <muggle_str_toul+0xe9>
movl   $0x0,-0x4(%rbp)
jmp    1210 <muggle_str_toul+0xf0>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<muggle_str_toul>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %r14d,%r14d
test   %rdi,%rdi
je     1192 <muggle_str_toul+0x72>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1192 <muggle_str_toul+0x72>
mov    %rdi,%rbp
mov    0x2e96(%rip),%rax
movq   $0x0,(%rax)
lea    0x8(%rsp),%rsi
call   1040 <strtoul@plt>
mov    %rax,(%rbx)
mov    0x8(%rsp),%rdi
cmp    %rbp,%rdi
je     1192 <muggle_str_toul+0x72>
cmpb   $0x0,(%rdi)
je     1172 <muggle_str_toul+0x52>
call   1030 <muggle_str_lstrip_idx@plt>
cmp    $0xffffffff,%eax
jne    1192 <muggle_str_toul+0x72>
mov    (%rbx),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
setne  %cl
mov    0x2e62(%rip),%rdx
cmp    (%rdx),%rax
setne  %al
and    %cl,%al
movzbl %al,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@ULONG_MAX = global i64 0
@ULLONG_MAX = global i64 0
define i32 @muggle_str_toul(i8* %0, i64* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp eq i8* %0, null
%7 = icmp eq i64* %1, null
%8 = or i1 %6, %7
br i1 %8, label %29, label %9
9: ; preds = %3
store i64 0, i64* @errno
%10 = call i64 @strtoul(i8* %0, i8** %4, i32 %2)
store i64 %10, i64* %1
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, %0
br i1 %12, label %29, label %13
13: ; preds = %9
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 0
br i1 %15, label %21, 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 %29
19: ; preds = %16
%20 = load i64, i64* %1
br label %21
21: ; preds = %19, %13
%22 = phi i64 [ %20, %19 ], [ %10, %13 ]
%23 = load i64, i64* @ULONG_MAX
%24 = icmp ne i64 %22, %23
%25 = load i64, i64* @ULLONG_MAX
%26 = icmp ne i64 %22, %25
%27 = select i1 %24, i1 %26, i1 false
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %21, %16, %9, %3
%30 = phi i32 [ 0, %3 ], [ 0, %9 ], [ 0, %16 ], [ %28, %21 ]
ret i32 %30
}