#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);
int /*<<< orphan*/ memcpy(char *, char *, 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 *, char *);
int main(int argc, char *argv[]) {
  int configfd;
  char *address = NULL;
  configfd = open("/sys/kernel/debug/mmap_example", O_RDWR);
  if (configfd < 0) {
    perror("Open call failed");
    return -1;
  }
  address = mmap(NULL, (4096), PROT_READ | PROT_WRITE, MAP_SHARED, configfd, 0);
  if (address == MAP_FAILED) {
    perror("mmap operation failed");
    return -1;
  }
  printf("\nInitial message: %s\n", address);
  memcpy(address + 11, "*user*", 6);
  printf("Changed message: %s\n", address);
  close(configfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b8 <main+0x58>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <main+0x100>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
or     (%rax),%edx
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1218 <main+0xb8>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <main+0x100>
mov    -0x20(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0xb,%rdi
lea    0xe25(%rip),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%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   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1207 <main+0xa7>
mov    %eax,%ebp
mov    0x2e6e(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e03(%rip),%rax
cmp    (%rax),%rbx
je     1210 <main+0xb0>
lea    0xe7d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
add    $0xb,%rdi
lea    0xe7d(%rip),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
lea    0xe73(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe11(%rip),%rdi
jmp    1217 <main+0xb7>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11ff <main+0x9f>

LLVM IR (-O0):
@.str = [31 x i8] c"/sys/kernel/debug/mmap_example\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"Open call failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [22 x i8] c"mmap operation failed\00"
@.str.3 = [22 x i8] c"\0AInitial message: %s\0A\00"
@.str.4 = [7 x i8] c"*user*\00"
@.str.5 = [21 x i8] c"Changed message: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 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 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
14: ; preds = %2
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %6
%20 = call i8* @mmap(i32* null, i32 4096, i32 %17, i32 %18, i32 %19, i32 0)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
26: ; preds = %14
%27 = load i8*, i8** %7
%28 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 11
%31 = call i32 @memcpy(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%32 = load i8*, i8** %7
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %26, %24, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [31 x i8] c"/sys/kernel/debug/mmap_example\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"Open call failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [22 x i8] c"mmap operation failed\00"
@.str.3 = [22 x i8] c"\0AInitial message: %s\0A\00"
@.str.4 = [7 x i8] c"*user*\00"
@.str.5 = [21 x i8] c"Changed message: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 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 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %24
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 4096, 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 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %24
18: ; preds = %8
%19 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%20 = getelementptr inbounds i8, i8* %13, i64 11
%21 = tail call i32 @memcpy(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %13)
%23 = tail call i32 @close(i32 %4)
br label %24
24: ; preds = %18, %16, %6
%25 = phi i32 [ -1, %6 ], [ -1, %16 ], [ 0, %18 ]
ret i32 %25
}