#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 scalar_t__ uint64_t;
typedef scalar_t__ uint32_t;
typedef int ssize_t;
typedef int /*<<< orphan*/ scratch;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ file32_to_cpu(scalar_t__);
scalar_t__ file64_to_cpu(scalar_t__);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long long,
                           int /*<<< orphan*/);
int machine_pointer_bits();
int pread(int, scalar_t__ *, int, scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
uint64_t read_file_pointer(int fd, uint64_t addr) {
  uint64_t result;
  ssize_t ret;
  if (machine_pointer_bits() == 64) {
    uint64_t scratch;
    ret = pread(fd, &scratch, sizeof(scratch), addr);
    if (ret != sizeof(scratch)) {
      fprintf(stderr, "Failed to read pointer @ 0x%llx: %s\n",
              (unsigned long long)addr, strerror(errno));
      exit(40);
    }
    result = file64_to_cpu(scratch);
  } else {
    uint32_t scratch;
    ret = pread(fd, &scratch, sizeof(scratch), addr);
    if (ret != sizeof(scratch)) {
      fprintf(stderr, "Failed to read pointer @ 0x%llx: %s\n",
              (unsigned long long)addr, strerror(errno));
      exit(40);
    }
    result = file32_to_cpu(scratch);
  }
  return result;
}


x86 assembly (clang, -O0):
<read_file_pointer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <machine_pointer_bits@plt>
cmp    $0x40,%eax
jne    1208 <read_file_pointer+0x98>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rcx
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1060 <pread@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
je     11f6 <read_file_pointer+0x86>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xe19(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x28,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1030 <file64_to_cpu@plt>
mov    %rax,-0x18(%rbp)
jmp    127c <read_file_pointer+0x10c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rcx
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1060 <pread@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
je     126f <read_file_pointer+0xff>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
lea    0xda0(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x28,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1050 <file32_to_cpu@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_pointer>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <machine_pointer_bits@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
mov    %rbx,%rcx
cmp    $0x40,%eax
jne    11aa <read_file_pointer+0x3a>
call   1060 <pread@plt>
cmp    $0x8,%eax
jne    11c3 <read_file_pointer+0x53>
mov    (%rsp),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <file64_to_cpu@plt>
call   1060 <pread@plt>
cmp    $0x8,%eax
jne    11c3 <read_file_pointer+0x53>
mov    (%rsp),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <file32_to_cpu@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe1f(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1040 <fprintf@plt>
mov    $0x28,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Failed to read pointer @ 0x%llx: %s\0A\00"
@errno = global i32 0
define i64 @read_file_pointer(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
%9 = call i32 (...) @machine_pointer_bits()
%10 = icmp eq i32 %9, 64
br i1 %10, label %11, label %28
11: ; preds = %2
%12 = load i32, i32* %3
%13 = load i64, i64* %4
%14 = call i32 @pread(i32 %12, i64* %7, i32 8, i64 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = icmp ne i64 %16, 8
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = load i64, i64* %4
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %20, i32 %22)
%24 = call i32 @exit(i32 40)
unreachable
25: ; preds = %11
%26 = load i64, i64* %7
%27 = call i64 @file64_to_cpu(i64 %26)
store i64 %27, i64* %5
br label %45
28: ; preds = %2
%29 = load i32, i32* %3
%30 = load i64, i64* %4
%31 = call i32 @pread(i32 %29, i64* %8, i32 8, i64 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 8
br i1 %34, label %35, label %42
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i64, i64* %4
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %37, i32 %39)
%41 = call i32 @exit(i32 40)
unreachable
42: ; preds = %28
%43 = load i64, i64* %8
%44 = call i64 @file32_to_cpu(i64 %43)
store i64 %44, i64* %5
br label %45
45: ; preds = %42, %25
%46 = load i64, i64* %5
ret i64 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Failed to read pointer @ 0x%llx: %s\0A\00"
@errno = global i32 0
define i64 @read_file_pointer(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = tail call i32 (...) @machine_pointer_bits()
%6 = icmp eq i32 %5, 64
br i1 %6, label %7, label %20
7: ; preds = %2
%8 = bitcast i64* %3 to i8*
%9 = call i32 @pread(i32 %0, i64* %3, i32 8, i64 %1)
%10 = icmp eq i32 %9, 8
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %1, i32 %14)
%16 = tail call i32 @exit(i32 40)
unreachable
17: ; preds = %7
%18 = load i64, i64* %3
%19 = tail call i64 @file64_to_cpu(i64 %18)
br label %33
20: ; preds = %2
%21 = bitcast i64* %4 to i8*
%22 = call i32 @pread(i32 %0, i64* %4, i32 8, i64 %1)
%23 = icmp eq i32 %22, 8
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i32 @strerror(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %1, i32 %27)
%29 = tail call i32 @exit(i32 40)
unreachable
30: ; preds = %20
%31 = load i64, i64* %4
%32 = tail call i64 @file32_to_cpu(i64 %31)
br label %33
33: ; preds = %30, %17
%34 = phi i64 [ %19, %17 ], [ %32, %30 ]
ret i64 %34
}