#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;
size_t atox(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, size_t);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int args, char *arg[]) {
  int i;
  int fd;
  char *mem;
  char *buff = "HELLO";
  if ((fd = open("/dev/mem", O_RDWR)) < 0) {
    perror("open error");
    return -1;
  }
  printf("open ok\n");
  size_t addr = (size_t)(0x11000000);
  if (args >= 3) {
    addr = atox(arg[2]);
  }
  mem = mmap(0, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr);
  if (mem == MAP_FAILED) {
    perror("mmap error:");
    return 1;
  }
  if (arg[1][0] == 'r') {
    for (i = 0; i < 5; i++) {
      printf("\nold mem[%d]:%d", i, mem[i]);
    }
    printf("The value is 0x%x\n", *((int *)mem));
  } else if (arg[1][0] == 'w') {
    memcpy(mem, buff, 5);
    for (i = 0; i < 5; i++) {
      printf("\nnew mem[%d]:%c", i, mem[i]);
    }
  }
  printf("\n");
  munmap(mem, 10);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x5a>
lea    0xe46(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x1ea>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x11000000,-0x30(%rbp)
11
cmpl   $0x3,-0x8(%rbp)
jl     120b <main+0x8b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <atox@plt>
mov    %rax,-0x30(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    0x2dad(%rip),%rax
or     (%rax),%edx
mov    0x2dc4(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x30(%rbp),%r9
xor    %edi,%edi
mov    $0xa,%esi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <main+0xea>
lea    0xdca(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136a <main+0x1ea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    12d3 <main+0x153>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    12ba <main+0x13a>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1285 <main+0x105>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    133f <main+0x1bf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    133a <main+0x1ba>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    1335 <main+0x1b5>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1300 <main+0x180>
jmp    133a <main+0x1ba>
jmp    133f <main+0x1bf>
lea    0xcdb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1080 <munmap@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe4d(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     134b <main+0x1ab>
mov    %eax,%r14d
lea    0xe89(%rip),%rdi
call   1040 <puts@plt>
mov    $0x11000000,%r9d
cmp    $0x3,%ebx
jl     11ec <main+0x4c>
mov    0x10(%rbp),%rdi
call   1050 <atox@plt>
mov    %rax,%r9
mov    0x2e05(%rip),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0xa,%esi
mov    %r14d,%r8d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2da0(%rip),%rax
cmp    (%rax),%rbx
je     135e <main+0x1be>
mov    0x8(%rbp),%rax
mov    (%rax),%al
cmp    $0x77,%al
je     12af <main+0x10f>
cmp    $0x72,%al
jne    1325 <main+0x185>
movsbl (%rbx),%edx
lea    0xde1(%rip),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %rbp,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %rbp,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %rbp,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%rbx),%esi
lea    0xd90(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1325 <main+0x185>
lea    0xd4a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
call   1090 <memcpy@plt>
movsbl (%rbx),%edx
lea    0xd7c(%rip),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %rbp,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %rbp,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %rbp,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   10a0 <munmap@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcbd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1346 <main+0x1a6>
lea    0xcb5(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
jmp    1346 <main+0x1a6>

LLVM IR (-O0):
@.str = [6 x i8] c"HELLO\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.2 = [11 x i8] c"open error\00"
@.str.3 = [9 x i8] c"open ok\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"mmap error:\00"
@.str.5 = [16 x i8] c"\0Aold mem[%d]:%d\00"
@.str.6 = [19 x i8] c"The value is 0x%x\0A\00"
@.str.7 = [16 x i8] c"\0Anew mem[%d]:%c\00"
@.str.8 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %9
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %102
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i64 285212672, i64* %10
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 3
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i64 @atox(i8* %23)
store i64 %24, i64* %10
br label %25
25: ; preds = %20, %16
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %7
%31 = load i64, i64* %10
%32 = call i8* @mmap(i32 0, i32 10, i32 %28, i32 %29, i32 %30, i64 %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %102
38: ; preds = %25
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 114
br i1 %45, label %46, label %67
46: ; preds = %38
store i32 0, i32* %6
br label %47
47: ; preds = %59, %46
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 5
br i1 %49, label %50, label %62
50: ; preds = %47
%51 = load i32, i32* %6
%52 = load i8*, i8** %8
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %51, i32 %57)
br label %59
59: ; preds = %50
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %47
62: ; preds = %47
%63 = load i8*, i8** %8
%64 = bitcast i8* %63 to i32*
%65 = load i32, i32* %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %65)
br label %96
67: ; preds = %38
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = getelementptr inbounds i8, i8* %70, i64 0
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 119
br i1 %74, label %75, label %95
75: ; preds = %67
%76 = load i8*, i8** %8
%77 = load i8*, i8** %9
%78 = call i32 @memcpy(i8* %76, i8* %77, i32 5)
store i32 0, i32* %6
br label %79
79: ; preds = %91, %75
%80 = load i32, i32* %6
%81 = icmp slt i32 %80, 5
br i1 %81, label %82, label %94
82: ; preds = %79
%83 = load i32, i32* %6
%84 = load i8*, i8** %8
%85 = load i32, i32* %6
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %83, i32 %89)
br label %91
91: ; preds = %82
%92 = load i32, i32* %6
%93 = add nsw i32 %92, 1
store i32 %93, i32* %6
br label %79
94: ; preds = %79
br label %95
95: ; preds = %94, %67
br label %96
96: ; preds = %95, %62
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%98 = load i8*, i8** %8
%99 = call i32 @munmap(i8* %98, i32 10)
%100 = load i32, i32* %7
%101 = call i32 @close(i32 %100)
store i32 0, i32* %3
br label %102
102: ; preds = %96, %36, %14
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
@.str = [6 x i8] c"HELLO\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.2 = [11 x i8] c"open error\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"mmap error:\00"
@.str.5 = [16 x i8] c"\0Aold mem[%d]:%d\00"
@.str.6 = [19 x i8] c"The value is 0x%x\0A\00"
@.str.7 = [16 x i8] c"\0Anew mem[%d]:%c\00"
@str = [8 x i8] c"open ok\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, 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 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %78
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%10 = icmp sgt i32 %0, 2
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i64 @atox(i8* %13)
br label %15
15: ; preds = %11, %8
%16 = phi i64 [ %14, %11 ], [ 285212672, %8 ]
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32 0, i32 10, i32 %19, i32 %20, i32 %4, i64 %16)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %78
26: ; preds = %15
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = load i8, i8* %28
switch i8 %29, label %74 [
i8 114, label %30
i8 119, label %53
]
30: ; preds = %26
%31 = load i8, i8* %21
%32 = sext i8 %31 to i32
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 %32)
%34 = getelementptr inbounds i8, i8* %21, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 %36)
%38 = getelementptr inbounds i8, i8* %21, i64 2
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 2, i32 %40)
%42 = getelementptr inbounds i8, i8* %21, i64 3
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 3, i32 %44)
%46 = getelementptr inbounds i8, i8* %21, i64 4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 4, i32 %48)
%50 = bitcast i8* %21 to i32*
%51 = load i32, i32* %50
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %51)
br label %74
53: ; preds = %26
%54 = tail call i32 @memcpy(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%55 = load i8, i8* %21
%56 = sext i8 %55 to i32
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 0, i32 %56)
%58 = getelementptr inbounds i8, i8* %21, i64 1
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 1, i32 %60)
%62 = getelementptr inbounds i8, i8* %21, i64 2
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 2, i32 %64)
%66 = getelementptr inbounds i8, i8* %21, i64 3
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 3, i32 %68)
%70 = getelementptr inbounds i8, i8* %21, i64 4
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 4, i32 %72)
br label %74
74: ; preds = %53, %26, %30
%75 = tail call i32 @putchar(i32 10)
%76 = tail call i32 @munmap(i8* %21, i32 10)
%77 = tail call i32 @close(i32 %4)
br label %78
78: ; preds = %24, %74, %6
%79 = phi i32 [ -1, %6 ], [ 1, %24 ], [ 0, %74 ]
ret i32 %79
}