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

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


x86 assembly (clang, -O0):
<read_file_u32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1050 <pread@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
je     11c6 <read_file_u32+0x76>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
lea    0xe49(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x29,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <file32_to_cpu@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_u32>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1050 <pread@plt>
cmp    $0x4,%eax
jne    117c <read_file_u32+0x2c>
mov    0x4(%rsp),%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <file32_to_cpu@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe66(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1030 <fprintf@plt>
mov    $0x29,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"Failed to read value @ 0x%llx: %s\0A\00"
@errno = global i32 0
define i32 @read_file_u32(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%7 = load i32, i32* %3
%8 = load i64, i64* %4
%9 = call i32 @pread(i32 %7, i32* %5, i32 4, i64 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = icmp ne i64 %11, 4
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i64, i64* %4
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %15, i32 %17)
%19 = call i32 @exit(i32 41)
unreachable
20: ; preds = %2
%21 = load i32, i32* %5
%22 = call i32 @file32_to_cpu(i32 %21)
ret i32 %22
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"Failed to read value @ 0x%llx: %s\0A\00"
@errno = global i32 0
define i32 @read_file_u32(i32 %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @pread(i32 %0, i32* %3, i32 4, i64 %1)
%6 = icmp eq i32 %5, 4
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %1, i32 %10)
%12 = tail call i32 @exit(i32 41)
unreachable
13: ; preds = %2
%14 = load i32, i32* %3
%15 = tail call i32 @file32_to_cpu(i32 %14)
ret i32 %15
}