#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 */

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int);
char *mmap(int /*<<< orphan*/, unsigned long, int /*<<< orphan*/,
           int /*<<< orphan*/, int, unsigned long);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char const *, unsigned long, unsigned long,
                          char *);
char *mapdev(const char *dev, unsigned long offset, unsigned long size) {
  char *addr;
  int fd = open(dev, O_RDONLY);
  if (fd < 0) {
    perror(dev);
    exit(1);
  }
  addr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, offset);
  if (addr == MAP_FAILED) {
    perror(dev);
    exit(1);
  }
  printf("Mapped %s (%lu @ %lx) at %p\n", dev, size, offset, addr);
  return (addr);
}


x86 assembly (clang, -O0):
<mapdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1196 <mapdev+0x46>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x10(%rbp),%r9
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <mapdev+0x96>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapdev>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11cb <mapdev+0x7b>
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %eax,%r8d
mov    %r12,%r9
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e29(%rip),%rax
cmp    (%rax),%rbx
je     11cb <mapdev+0x7b>
lea    0xe55(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %rbx,%r8
call   1040 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [29 x i8] c"Mapped %s (%lu @ %lx) at %p\0A\00"
define i8* @mapdev(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 @perror(i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i64, i64* %6
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_PRIVATE
%22 = load i32, i32* %8
%23 = load i64, i64* %5
%24 = call i8* @mmap(i32 0, i64 %19, i32 %20, i32 %21, i32 %22, i64 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = load i8*, i8** %4
%30 = call i32 @perror(i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = load i8*, i8** %4
%34 = load i64, i64* %6
%35 = load i64, i64* %5
%36 = load i8*, i8** %7
%37 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %33, i64 %34, i64 %35, i8* %36)
%38 = load i8*, i8** %7
ret i8* %38
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [29 x i8] c"Mapped %s (%lu @ %lx) at %p\0A\00"
define i8* @mapdev(i8* %0, i64 %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @perror(i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @MAP_PRIVATE
%13 = tail call i8* @mmap(i32 0, i64 %2, i32 %11, i32 %12, i32 %5, i64 %1)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @perror(i8* %0)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %2, i64 %1, i8* %13)
ret i8* %13
}