#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_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int i, fd;
  char *p;
  fd = open("/dev/simpler", O_RDWR);
  if (fd < 0) {
    perror("error in device");
    exit(1);
  }
  p = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (p == MAP_FAILED) {
    perror("failed to mmap device");
    exit(1);
  }
  printf("mmap device ok p = %p\n", p);
  for (i = 0; i < 1024; i++)
    p[i] = 0x55;
  for (i = 0; i < 1024; i++) {
    if (p[i] != 0x55) {
      printf(" memory write/read error! \n");
      return -1;
    }
  }
  printf(" memory write/read succeed\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a7 <main+0x47>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <main+0xa5>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x400,-0x8(%rbp)
jge    1245 <main+0xe5>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x55,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    121e <main+0xbe>
movl   $0x0,-0x8(%rbp)
cmpl   $0x400,-0x8(%rbp)
jge    129b <main+0x13b>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x55,%eax
je     1288 <main+0x128>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <main+0x158>
jmp    128d <main+0x12d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    124c <main+0xec>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%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   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1266 <main+0xe6>
mov    %eax,%ebp
mov    0x2e4f(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x400,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2de7(%rip),%rax
cmp    (%rax),%rbx
je     126f <main+0xef>
lea    0xe4a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x400,%edx
mov    %rbx,%rdi
mov    $0x55,%esi
call   1060 <memset@plt>
mov    $0x4,%eax
nopw   0x0(%rax,%rax,1)
cmpb   $0x55,-0x3(%rbx,%rax,1)
jne    1237 <main+0xb7>
cmpb   $0x55,-0x2(%rbx,%rax,1)
jne    1237 <main+0xb7>
cmpb   $0x55,-0x1(%rbx,%rax,1)
jne    1237 <main+0xb7>
cmp    $0x400,%rax
je     124f <main+0xcf>
cmpb   $0x55,(%rbx,%rax,1)
lea    0x4(%rax),%rax
je     1210 <main+0x90>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    1248 <main+0xc8>
lea    0xda0(%rip),%rdi
jmp    1276 <main+0xf6>
lea    0xda7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/simpler\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"error in device\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [22 x i8] c"failed to mmap device\00"
@.str.3 = [23 x i8] c"mmap device ok p = %p\0A\00"
@.str.4 = [28 x i8] c" memory write/read error! \0A\00"
@.str.5 = [28 x i8] c" memory write/read succeed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_PRIVATE
%17 = load i32, i32* %3
%18 = call i8* @mmap(i32* null, i32 1024, i32 %15, i32 %16, i32 %17, i32 0)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = load i8*, i8** %4
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %26)
store i32 0, i32* %2
br label %28
28: ; preds = %36, %25
%29 = load i32, i32* %2
%30 = icmp slt i32 %29, 1024
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = load i32, i32* %2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 85, i8* %35
br label %36
36: ; preds = %31
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %28
39: ; preds = %28
store i32 0, i32* %2
br label %40
40: ; preds = %54, %39
%41 = load i32, i32* %2
%42 = icmp slt i32 %41, 1024
br i1 %42, label %43, label %57
43: ; preds = %40
%44 = load i8*, i8** %4
%45 = load i32, i32* %2
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 85
br i1 %50, label %51, label %53
51: ; preds = %43
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %61
53: ; preds = %43
br label %54
54: ; preds = %53
%55 = load i32, i32* %2
%56 = add nsw i32 %55, 1
store i32 %56, i32* %2
br label %40
57: ; preds = %40
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* %3
%60 = call i32 @close(i32 %59)
store i32 0, i32* %1
br label %61
61: ; preds = %57, %51
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/simpler\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"error in device\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [22 x i8] c"failed to mmap device\00"
@.str.3 = [23 x i8] c"mmap device ok p = %p\0A\00"
@str = [27 x i8] c" memory write/read succeed\00"
@str.6 = [27 x i8] c" memory write/read error! \00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = tail call i8* @mmap(i32* null, i32 1024, i32 %10, i32 %11, i32 %2, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %7
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %20
20: ; preds = %39, %18
%21 = phi i64 [ 0, %18 ], [ %37, %39 ]
%22 = or i64 %21, 1
%23 = getelementptr inbounds i8, i8* %12, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 85
br i1 %25, label %26, label %43
26: ; preds = %20
%27 = or i64 %21, 2
%28 = getelementptr inbounds i8, i8* %12, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 85
br i1 %30, label %31, label %43
31: ; preds = %26
%32 = or i64 %21, 3
%33 = getelementptr inbounds i8, i8* %12, i64 %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 85
br i1 %35, label %36, label %43
36: ; preds = %31
%37 = add nuw nsw i64 %21, 4
%38 = icmp eq i64 %37, 1024
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = getelementptr inbounds i8, i8* %12, i64 %37
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 85
br i1 %42, label %20, label %43
43: ; preds = %39, %31, %26, %20
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %48
45: ; preds = %36
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%47 = tail call i32 @close(i32 %2)
br label %48
48: ; preds = %45, %43
%49 = phi i32 [ -1, %43 ], [ 0, %45 ]
ret i32 %49
}