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

/* Variables and functions */
void volatile *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ err(int, char *);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
volatile void *mmap_perith(off_t addr, size_t size, int *fd) {
  volatile void *mem;
  *fd = open("/dev/mem", O_RDWR);
  if (*fd < 0)
    err(1, "open /dev/mem:");
  mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, addr);
  if (mem == MAP_FAILED)
    err(1, "mmap:");
  return mem;
}


x86 assembly (clang, -O0):
<mmap_perith>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    117e <mmap_perith+0x4e>
mov    $0x1,%edi
lea    0xe90(%rip),%rsi
call   1040 <err@plt>
mov    -0x10(%rbp),%rsi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8d
mov    -0x4(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11da <mmap_perith+0xaa>
mov    $0x1,%edi
lea    0xe43(%rip),%rsi
call   1040 <err@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_perith>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x0(%rbp)
test   %eax,%eax
jns    116d <mmap_perith+0x3d>
lea    0xea9(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
mov    0x0(%rbp),%eax
mov    0x2e84(%rip),%rcx
mov    0x2e4d(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e62(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e1e(%rip),%rax
cmp    (%rax),%rbx
jne    11b8 <mmap_perith+0x88>
lea    0xe6a(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"open /dev/mem:\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [6 x i8] c"mmap:\00"
define i8* @mmap_perith(i32 %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32*, i32** %6
store i32 %9, i32* %10
%11 = load i32*, i32** %6
%12 = load i32, i32* %11
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i64, i64* %5
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_SHARED
%22 = load i32*, i32** %6
%23 = load i32, i32* %22
%24 = load i32, i32* %4
%25 = call i8* @mmap(i32* null, i64 %17, i32 %20, i32 %21, i32 %23, i32 %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %16
%32 = load i8*, i8** %7
ret i8* %32
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"open /dev/mem:\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [6 x i8] c"mmap:\00"
define i8* @mmap_perith(i32 %0, i64 %1, i32* nocapture %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* %2
br label %10
10: ; preds = %7, %3
%11 = phi i32 [ %9, %7 ], [ %5, %3 ]
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32* null, i64 %1, i32 %14, i32 %15, i32 %11, i32 %0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %10
ret i8* %16
}