#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 */
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*/ exit(int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
volatile unsigned int *setup_io(int reg_base) {
  int mem_fd;
  void *io_map;
  if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
    printf("can't open /dev/mem \n");
    exit(-1);
  }
  io_map =
      mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, reg_base);
  close(mem_fd);
  if (io_map == MAP_FAILED) {
    printf("mmap error %d\n", (int)io_map);
    exit(-1);
  }
  return (volatile unsigned *)io_map;
}


x86 assembly (clang, -O0):
<setup_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    119d <setup_io+0x4d>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    0x2e13(%rip),%rax
or     (%rax),%edx
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
mov    -0x4(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jne    120c <setup_io+0xbc>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_io>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e6c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe82(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11d6 <setup_io+0x76>
mov    %eax,%ebp
mov    0x2e68(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
mov    %ebx,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2df1(%rip),%rax
cmp    (%rax),%rbx
je     11ec <setup_io+0x8c>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xe16(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [16 x i8] c"mmap error\C2\A0%d\0A\00"
@str = [21 x i8] c"can't open /dev/mem \00"
define i32* @setup_io(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_SYNC
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %1
%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 4096, i32 %13, i32 %14, i32 %5, i32 %0)
%16 = tail call i32 @close(i32 %5)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %15, %17
br i1 %18, label %19, label %24
19: ; preds = %10
%20 = ptrtoint i8* %15 to i64
%21 = trunc i64 %20 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %10
%25 = bitcast i8* %15 to i32*
ret i32* %25
}