#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_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(void *, int, int, int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fd = open("/dev/mem", O_RDWR);
  if (fd < 0) {
    printf("Open memory error!\n");
    return -1;
  }
  int base = 0;
  int size = 10;
  char *mem;
  mem = (char *)mmap((void *)base, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                     0);
  if (mem == MAP_FAILED) {
    printf("Mmap error!\n");
    return 1;
  }
  for (int i = 0; i < size; i++)
    printf("mem[%d]: %#x\n", i, (unsigned char)mem[i]);
  munmap(mem, size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119b <main+0x4b>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <main+0x112>
movl   $0x0,-0xc(%rbp)
movl   $0xa,-0x10(%rbp)
movslq -0xc(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <main+0xb9>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1262 <main+0x112>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1247 <main+0xf7>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1210 <main+0xc0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12a2 <main+0x142>
mov    %eax,%r15d
mov    0x2e6a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0xa,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dff(%rip),%rax
cmp    %rbx,(%rax)
je     12b6 <main+0x156>
movzbl (%rbx),%edx
lea    0xe35(%rip),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x1(%rbx),%edx
mov    %rbp,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x2(%rbx),%edx
mov    %rbp,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x3(%rbx),%edx
mov    %rbp,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x4(%rbx),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x5(%rbx),%edx
mov    %rbp,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x6(%rbx),%edx
mov    %rbp,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x7(%rbx),%edx
mov    %rbp,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x8(%rbx),%edx
mov    %rbp,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x9(%rbx),%edx
mov    %rbp,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1070 <munmap@plt>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    12c8 <main+0x168>
lea    0xd7a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    12c8 <main+0x168>
lea    0xd5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Open memory error!\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [13 x i8] c"Mmap error!\0A\00"
@.str.3 = [14 x i8] c"mem[%d]: %#x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %53
13: ; preds = %0
store i32 0, i32* %3
store i32 10, i32* %4
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
%17 = load i32, i32* %4
%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* %2
%23 = call i64 @mmap(i8* %16, i32 %17, i32 %20, i32 %21, i32 %22, i32 0)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %13
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %53
30: ; preds = %13
store i32 0, i32* %6
br label %31
31: ; preds = %44, %30
%32 = load i32, i32* %6
%33 = load i32, i32* %4
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %47
35: ; preds = %31
%36 = load i32, i32* %6
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %42)
br label %44
44: ; preds = %35
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
br label %31
47: ; preds = %31
%48 = load i8*, i8** %5
%49 = load i32, i32* %4
%50 = call i32 @munmap(i8* %48, i32 %49)
%51 = load i32, i32* %2
%52 = call i32 @close(i32 %51)
store i32 0, i32* %1
br label %53
53: ; preds = %47, %28, %11
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [14 x i8] c"mem[%d]: %#x\0A\00"
@str = [12 x i8] c"Mmap error!\00"
@str.4 = [19 x i8] c"Open memory error!\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
br label %59
6: ; preds = %0
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %8, %7
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i64 @mmap(i8* null, i32 10, i32 %9, i32 %10, i32 %2, i32 0)
%12 = inttoptr i64 %11 to i8*
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %13, %12
br i1 %14, label %57, label %15
15: ; preds = %6
%16 = load i8, i8* %12
%17 = zext i8 %16 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %17)
%19 = getelementptr inbounds i8, i8* %12, i64 1
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %21)
%23 = getelementptr inbounds i8, i8* %12, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 2, i32 %25)
%27 = getelementptr inbounds i8, i8* %12, i64 3
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 3, i32 %29)
%31 = getelementptr inbounds i8, i8* %12, i64 4
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 %33)
%35 = getelementptr inbounds i8, i8* %12, i64 5
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 5, i32 %37)
%39 = getelementptr inbounds i8, i8* %12, i64 6
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 6, i32 %41)
%43 = getelementptr inbounds i8, i8* %12, i64 7
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 7, i32 %45)
%47 = getelementptr inbounds i8, i8* %12, i64 8
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 8, i32 %49)
%51 = getelementptr inbounds i8, i8* %12, i64 9
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 9, i32 %53)
%55 = tail call i32 @munmap(i8* %12, i32 10)
%56 = tail call i32 @close(i32 %2)
br label %59
57: ; preds = %6
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %59
59: ; preds = %57, %15, %4
%60 = phi i32 [ -1, %4 ], [ 1, %57 ], [ 0, %15 ]
ret i32 %60
}