#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 int /*<<< orphan*/ uint64_t;

/* Variables and functions */
scalar_t__ ERANGE;
int /*<<< orphan*/ ULLONG_MAX;
scalar_t__ errno;
int /*<<< orphan*/ strtoull(char const *, char **, int /*<<< orphan*/);
int atou64(const char *str, uint64_t *val) {
  char *endptr;
  errno = 0;
  *val = strtoull(str, &endptr, 0);
  if ((errno == ERANGE && *val == ULLONG_MAX) || (errno != 0 && *val == 0) ||
      endptr == str || *endptr != '\0')
    return -1;
  return 1;
}


x86 assembly (clang, -O0):
<atou64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e8a(%rip),%rcx
cmp    (%rcx),%rax
jne    1174 <atou64+0x64>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%eax
je     11b0 <atou64+0xa0>
mov    0x2e55(%rip),%rax
cmpq   $0x0,(%rax)
je     1192 <atou64+0x82>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
je     11b0 <atou64+0xa0>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11b0 <atou64+0xa0>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11bc <atou64+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <atou64+0xb3>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atou64>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2eaa(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %eax,%ecx
mov    %eax,(%rbx)
mov    (%r15),%rdx
mov    0x2e99(%rip),%rsi
mov    $0xffffffff,%eax
cmp    (%rsi),%rdx
jne    115c <atou64+0x4c>
mov    0x2e90(%rip),%rsi
cmp    (%rsi),%ecx
je     1178 <atou64+0x68>
test   %rdx,%rdx
je     1165 <atou64+0x55>
test   %ecx,%ecx
je     1178 <atou64+0x68>
mov    0x8(%rsp),%rcx
cmp    %r14,%rcx
je     1178 <atou64+0x68>
xor    %eax,%eax
cmp    (%rcx),%al
sbb    %eax,%eax
or     $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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