#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 */
typedef unsigned long long uint64_t;

/* Variables and functions */
scalar_t__ ERANGE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ isspace(char);
int /*<<< orphan*/ *strchr(char const *, char);
unsigned long long strtoull(char const *, char **, int);
int safe_strtoull(const char *str, uint64_t *out) {
  assert(out != NULL);
  errno = 0;
  *out = 0;
  char *endptr;
  unsigned long long ull = strtoull(str, &endptr, 10);
  if (errno == ERANGE)
    return 0;
  if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
    if ((long long)ull < 0) {
      if (strchr(str, '-') != NULL) {
        return 0;
      }
    }
    *out = ull;
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<safe_strtoull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2e6f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtoull@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    11b7 <safe_strtoull+0x77>
movl   $0x0,-0x4(%rbp)
jmp    123d <safe_strtoull+0xfd>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
jne    11eb <safe_strtoull+0xab>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1236 <safe_strtoull+0xf6>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1236 <safe_strtoull+0xf6>
cmpq   $0x0,-0x28(%rbp)
jge    121f <safe_strtoull+0xdf>
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     121a <safe_strtoull+0xda>
movl   $0x0,-0x4(%rbp)
jmp    123d <safe_strtoull+0xfd>
jmp    121f <safe_strtoull+0xdf>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    123d <safe_strtoull+0xfd>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_strtoull>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r13
mov    %rdi,%r14
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1060 <assert@plt>
mov    0x2e70(%rip),%rbx
movq   $0x0,(%rbx)
movq   $0x0,0x0(%r13)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0xa,%edx
call   1050 <strtoull@plt>
mov    %rax,%r15
mov    (%rbx),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     11db <safe_strtoull+0x9b>
mov    0x8(%rsp),%r12
movsbl (%r12),%edi
call   1040 <isspace@plt>
test   %rax,%rax
jne    11c0 <safe_strtoull+0x80>
mov    $0x1,%eax
cmp    %r14,%r12
je     11dd <safe_strtoull+0x9d>
cmpb   $0x0,(%r12)
jne    11dd <safe_strtoull+0x9d>
test   %r15,%r15
jns    11d7 <safe_strtoull+0x97>
mov    %r14,%rdi
mov    $0x2d,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    11db <safe_strtoull+0x9b>
mov    %r15,0x0(%r13)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i32 @safe_strtoull(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%8 = load i64*, i64** %5
%9 = icmp ne i64* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
store i64 0, i64* @errno
%12 = load i64*, i64** %5
store i64 0, i64* %12
%13 = load i8*, i8** %4
%14 = call i64 @strtoull(i8* %13, i8** %6, i32 10)
store i64 %14, i64* %7
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 0, i32* %3
br label %46
19: ; preds = %2
%20 = load i8*, i8** %6
%21 = load i8, i8* %20
%22 = call i64 @isspace(i8 signext %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %45
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = load i8*, i8** %4
%32 = icmp ne i8* %30, %31
br i1 %32, label %33, label %45
33: ; preds = %29, %19
%34 = load i64, i64* %7
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i8*, i8** %4
%38 = call i32* @strchr(i8* %37, i8 signext 45)
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %3
br label %46
41: ; preds = %36
br label %42
42: ; preds = %41, %33
%43 = load i64, i64* %7
%44 = load i64*, i64** %5
store i64 %43, i64* %44
store i32 0, i32* %3
br label %46
45: ; preds = %29, %24
store i32 1, i32* %3
br label %46
46: ; preds = %45, %42, %40, %18
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i32 @safe_strtoull(i8* %0, i64* writeonly %1) {
%3 = alloca i8*
%4 = icmp ne i64* %1, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
store i64 0, i64* @errno
store i64 0, i64* %1
%7 = bitcast i8** %3 to i8*
%8 = call i64 @strtoull(i8* %0, i8** %3, i32 10)
%9 = load i64, i64* @errno
%10 = load i64, i64* @ERANGE
%11 = icmp eq i64 %9, %10
br i1 %11, label %28, label %12
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i8, i8* %13
%15 = tail call i64 @isspace(i8 signext %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i8, i8* %13
%19 = icmp ne i8 %18, 0
%20 = icmp eq i8* %13, %0
%21 = or i1 %20, %19
br i1 %21, label %28, label %22
22: ; preds = %17, %12
%23 = icmp slt i64 %8, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = tail call i32* @strchr(i8* %0, i8 signext 45)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %28
27: ; preds = %24, %22
store i64 %8, i64* %1
br label %28
28: ; preds = %17, %24, %2, %27
%29 = phi i32 [ 0, %27 ], [ 0, %2 ], [ 0, %24 ], [ 1, %17 ]
ret i32 %29
}