#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 ssize_t;

/* Variables and functions */
scalar_t__ MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ err(int, char *, unsigned long, unsigned long,
                       unsigned long);
scalar_t__ mmap(void *, unsigned long, int, int, int, unsigned long);
unsigned long pread(int, void *, unsigned long, unsigned long);
void map_at(int fd, void *addr, unsigned long offset, unsigned long len) {
  ssize_t r;
  if (mmap(addr, len, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, fd,
           offset) != MAP_FAILED)
    return;
  r = pread(fd, addr, len, offset);
  if (r != len)
    err(1, "Reading offset %lu len %lu gave %zi", offset, len, r);
}


x86 assembly (clang, -O0):
<map_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2ea2(%rip),%rax
mov    (%rax),%edx
mov    0x2e79(%rip),%rax
or     (%rax),%edx
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5f(%rip),%rax
or     (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x18(%rbp),%r9
call   1030 <mmap@plt>
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
je     1195 <map_at+0x65>
jmp    11d8 <map_at+0xa8>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1050 <pread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11d8 <map_at+0xa8>
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1,%edi
lea    0xe2d(%rip),%rsi
call   1040 <err@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_at>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eaf(%rip),%rax
mov    0x2e88(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6d(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rsi,%rdi
mov    %r15,%rsi
mov    %ebp,%r8d
mov    %r14,%r9
call   1030 <mmap@plt>
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    1198 <map_at+0x68>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <pread@plt>
cmp    %r15,%rax
jne    11a3 <map_at+0x73>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe56(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rax,%r8
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <err@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64 0
@.str = [36 x i8] c"Reading offset %lu len %lu gave %zi\00"
define void @map_at(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%10 = load i8*, i8** %6
%11 = load i64, i64* %8
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_FIXED
%16 = load i32, i32* @MAP_PRIVATE
%17 = or i32 %15, %16
%18 = load i32, i32* %5
%19 = load i64, i64* %7
%20 = call i64 @mmap(i8* %10, i64 %11, i32 %14, i32 %17, i32 %18, i64 %19)
%21 = load i64, i64* @MAP_FAILED
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %4
br label %38
24: ; preds = %4
%25 = load i32, i32* %5
%26 = load i8*, i8** %6
%27 = load i64, i64* %8
%28 = load i64, i64* %7
%29 = call i64 @pread(i32 %25, i8* %26, i64 %27, i64 %28)
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = load i64, i64* %8
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %24
%34 = load i64, i64* %7
%35 = load i64, i64* %8
%36 = load i64, i64* %9
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %34, i64 %35, i64 %36)
br label %38
38: ; preds = %23, %33, %24
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64 0
@.str = [36 x i8] c"Reading offset %lu len %lu gave %zi\00"
define void @map_at(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_FIXED
%9 = load i32, i32* @MAP_PRIVATE
%10 = or i32 %9, %8
%11 = tail call i64 @mmap(i8* %1, i64 %3, i32 %7, i32 %10, i32 %0, i64 %2)
%12 = load i64, i64* @MAP_FAILED
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = tail call i64 @pread(i32 %0, i8* %1, i64 %3, i64 %2)
%16 = icmp eq i64 %15, %3
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %2, i64 %3, i64 %15)
br label %19
19: ; preds = %14, %17, %4
ret void
}