#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 */
int /*<<< orphan*/ MAP_SHARED;
int O_NDELAY;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strtoul(char *, int /*<<< orphan*/, int);
int main(int argc, char *argv[]) {
  int fd;
  char *pdata;
  if (argc <= 1) {
    printf("Usage: main devfile pa_mapped\n");
    return -1;
  }
  fd = open(argv[1], O_RDWR | O_NDELAY);
  if (fd >= 0) {
    pdata = (char *)mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                         strtoul(argv[2], 0, 16));
    printf("UserAddr = %p, Data from kernel:%s\n", pdata, pdata);
    printf("Writing a string to the kernel space...");
    strcpy(pdata, "String changed from the User Space");
    printf("Done\n");
    munmap(pdata, 4096);
    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)
cmpl   $0x1,-0x8(%rbp)
jg     11aa <main+0x3a>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <main+0x11d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1286 <main+0x116>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    0x2dda(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    %eax,%r9d
xor    %edi,%edi
mov    $0x1000,%esi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    -0x14(%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   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11a2 <main+0x22>
lea    0xee1(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1269 <main+0xe9>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e28(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1267 <main+0xe7>
mov    %eax,%r14d
mov    0x2e26(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e04(%rip),%rax
mov    (%rax),%r15d
mov    0x10(%rbp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%edx
mov    %r15d,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
lea    0xde6(%rip),%rdi
mov    %rax,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe10(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe24(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1080 <munmap@plt>
mov    %r14d,%edi
call   1070 <close@plt>
jmp    1269 <main+0xe9>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Usage: main devfile pa_mapped\0A\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [36 x i8] c"UserAddr = %p, Data from kernel:%s\0A\00"
@.str.2 = [40 x i8] c"Writing a string to the kernel space...\00"
@.str.3 = [35 x i8] c"String changed from the User Space\00"
@.str.4 = [6 x i8] c"Done\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
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NDELAY
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %15, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %45
22: ; preds = %12
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %6
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strtoul(i8* %30, i32 0, i32 16)
%32 = call i64 @mmap(i32 0, i32 4096, i32 %25, i32 %26, i32 %27, i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = load i8*, i8** %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%38 = load i8*, i8** %7
%39 = call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%41 = load i8*, i8** %7
%42 = call i32 @munmap(i8* %41, i32 4096)
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %22, %12
store i32 0, i32* %3
br label %46
46: ; preds = %45, %10
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [36 x i8] c"UserAddr = %p, Data from kernel:%s\0A\00"
@.str.2 = [40 x i8] c"Writing a string to the kernel space...\00"
@.str.3 = [35 x i8] c"String changed from the User Space\00"
@str = [5 x i8] c"Done\00"
@str.5 = [30 x i8] c"Usage: main devfile pa_mapped\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
br label %30
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NDELAY
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %30
14: ; preds = %6
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32 @strtoul(i8* %20, i32 0, i32 16)
%22 = tail call i64 @mmap(i32 0, i32 4096, i32 %17, i32 %18, i32 %12, i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%28 = tail call i32 @munmap(i8* %23, i32 4096)
%29 = tail call i32 @close(i32 %12)
br label %30
30: ; preds = %6, %14, %4
%31 = phi i32 [ -1, %4 ], [ 0, %14 ], [ 0, %6 ]
ret i32 %31
}