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

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


x86 assembly (clang, -O0):
<map_peripheral>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    0xea2(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118b <map_peripheral+0x4b>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1200 <map_peripheral+0xc0>
mov    -0x10(%rbp),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0xc(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    11f0 <map_peripheral+0xb0>
mov    -0xc(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1200 <map_peripheral+0xc0>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%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    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a6 <map_peripheral+0x66>
mov    %eax,%ebp
mov    0x2e8d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6b(%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    0x2e28(%rip),%rax
cmp    (%rax),%rbx
je     11b8 <map_peripheral+0x78>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11cb <map_peripheral+0x8b>
lea    0xe5c(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11cb <map_peripheral+0x8b>
lea    0xe70(%rip),%rdi
xor    %ebx,%ebx
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@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 = [38 x i8] c"i2c-map: 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 = [49 x i8] c"i2c-map: Failed to map peripheral at 0x%08x: %m\0A\00"
define i8* @map_peripheral(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
%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)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %33
14: ; preds = %2
%15 = load i32, i32* %5
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_SHARED
%20 = load i32, i32* %6
%21 = load i32, i32* %4
%22 = call i8* @mmap(i32* null, i32 %15, i32 %18, i32 %19, i32 %20, i32 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = load i32, i32* %4
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %27)
store i8* null, i8** %3
br label %33
29: ; preds = %14
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
%32 = load i8*, i8** %7
store i8* %32, i8** %3
br label %33
33: ; preds = %29, %26, %12
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [38 x i8] c"i2c-map: 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 = [49 x i8] c"i2c-map: 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 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %20
8: ; preds = %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i8* @mmap(i32* null, i32 %1, i32 %11, i32 %12, i32 %4, i32 %0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %20
18: ; preds = %8
%19 = tail call i32 @close(i32 %4)
br label %20
20: ; preds = %18, %16, %6
%21 = phi i8* [ null, %6 ], [ null, %16 ], [ %13, %18 ]
ret i8* %21
}