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

/* Variables and functions */
int EINVAL;
int ERANGE;
int errno;
scalar_t__ read_string_safe(int, char *, int);
int /*<<< orphan*/ strtoull(char *, char **, int /*<<< orphan*/);
int read_u64(int fd, uint64_t *val) {
  char buf[24];
  char *end;
  if (!val) {
    errno = EINVAL;
  } else if (read_string_safe(fd, buf, sizeof(buf)) > 0) {
    errno = 0;
    *val = strtoull(buf, &end, 0);
    if (buf != end && errno != ERANGE) {
      return 0;
    }
  }
  return -errno;
}


x86 assembly (clang, -O0):
<read_u64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1151 <read_u64+0x31>
mov    0x2e8f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8e(%rip),%rax
mov    %ecx,(%rax)
jmp    11cc <read_u64+0xac>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1040 <read_string_safe@plt>
cmp    $0x0,%rax
jle    11c7 <read_u64+0xa7>
mov    0x2e65(%rip),%rax
movl   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     11c2 <read_u64+0xa2>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%eax
je     11c2 <read_u64+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    11da <read_u64+0xba>
jmp    11c7 <read_u64+0xa7>
jmp    11cc <read_u64+0xac>
xor    %eax,%eax
mov    0x2e03(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_u64>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
test   %rsi,%rsi
je     1181 <read_u64+0x61>
mov    %rsi,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x18,%edx
call   1040 <read_string_safe@plt>
test   %rax,%rax
jle    1193 <read_u64+0x73>
mov    0x2e88(%rip),%r15
movl   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %eax,(%rbx)
cmp    0x8(%rsp),%r14
je     1193 <read_u64+0x73>
mov    (%r15),%eax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%eax
je     1193 <read_u64+0x73>
jmp    119e <read_u64+0x7e>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    0x2e47(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e3e(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@ERANGE = global i32 0
define i32 @read_u64(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [24 x i8]
%7 = alloca i8*
store i32 %0, i32* %4
store i32* %1, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %31
12: ; preds = %2
%13 = load i32, i32* %4
%14 = getelementptr inbounds [24 x i8], [24 x i8]* %6, i64 0, i64 0
%15 = call i64 @read_string_safe(i32 %13, i8* %14, i32 24)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %12
store i32 0, i32* @errno
%18 = getelementptr inbounds [24 x i8], [24 x i8]* %6, i64 0, i64 0
%19 = call i32 @strtoull(i8* %18, i8** %7, i32 0)
%20 = load i32*, i32** %5
store i32 %19, i32* %20
%21 = getelementptr inbounds [24 x i8], [24 x i8]* %6, i64 0, i64 0
%22 = load i8*, i8** %7
%23 = icmp ne i8* %21, %22
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = load i32, i32* @errno
%26 = load i32, i32* @ERANGE
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 0, i32* %3
br label %34
29: ; preds = %24, %17
br label %30
30: ; preds = %29, %12
br label %31
31: ; preds = %30, %10
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
store i32 %33, i32* %3
br label %34
34: ; preds = %31, %28
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ERANGE = global i32 0
define i32 @read_u64(i32 %0, i32* writeonly %1) {
%3 = alloca [24 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = icmp eq i32* %1, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %21
10: ; preds = %2
%11 = call i64 @read_string_safe(i32 %0, i8* %5, i32 24)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %10
store i32 0, i32* @errno
%14 = call i32 @strtoull(i8* %5, i8** %4, i32 0)
store i32 %14, i32* %1
%15 = load i8*, i8** %4
%16 = icmp eq i8* %5, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = load i32, i32* @ERANGE
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %10, %17, %13, %8
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
br label %24
24: ; preds = %17, %21
%25 = phi i32 [ %23, %21 ], [ 0, %17 ]
ret i32 %25
}