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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fatal(char *, ...);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int);
void *map_peripheral(uint32_t base, uint32_t len) {
  int fd = open("/dev/mem", O_RDWR | O_SYNC);
  void *vaddr;
  if (fd < 0)
    fatal("pi-blaster: Failed to open /dev/mem: %m\n");
  vaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
  if (vaddr == MAP_FAILED)
    fatal("pi-blaster: Failed to map peripheral at 0x%08x: %m\n", base);
  close(fd);
  return vaddr;
}


x86 assembly (clang, -O0):
<map_peripheral>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
lea    0xe99(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1187 <map_peripheral+0x47>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x8(%rbp),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x4(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    11df <map_peripheral+0x9f>
mov    -0x4(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_peripheral>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
mov    0x2e88(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    117b <map_peripheral+0x3b>
lea    0xe95(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    0x2e76(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e09(%rip),%rax
cmp    (%rax),%rbx
jne    11c5 <map_peripheral+0x85>
lea    0xe77(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    %ebp,%edi
call   1040 <close@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
@O_SYNC = global i32 0
@.str.1 = [41 x i8] c"pi-blaster: Failed to open /dev/mem: %m\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [52 x i8] c"pi-blaster: Failed to map peripheral at 0x%08x: %m\0A\00"
define i8* @map_peripheral(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* %4
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %5
%22 = load i32, i32* %3
%23 = call i8* @mmap(i32* null, i32 %16, i32 %19, i32 %20, i32 %21, i32 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %27, %15
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
%33 = load i8*, i8** %6
ret i8* %33
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [41 x i8] c"pi-blaster: Failed to open /dev/mem: %m\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [52 x i8] c"pi-blaster: Failed to map peripheral at 0x%08x: %m\0A\00"
define i8* @map_peripheral(i32 %0, i32 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32* null, i32 %1, i32 %13, i32 %14, i32 %6, i32 %0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %20
20: ; preds = %18, %10
%21 = tail call i32 @close(i32 %6)
ret i8* %15
}