#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 /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argn, char *argv[]) {
  int sysmem_fd;
  char *data;
  int length = 4096;
  int i;
  unsigned int *reg;
  if (argn < 2) {
    printf("filename required\n");
    return 1;
  }
  sysmem_fd = open(argv[1], O_RDWR);
  data = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED, sysmem_fd, 0x0);
  close(sysmem_fd);
  printf("current value: %d\n", (int)data[512]);
  reg = (int *)&data[512];
  *reg = 0;
  munmap(data, length);
  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)
movl   $0x1000,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1191 <main+0x41>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1227 <main+0xd7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    0x2e0b(%rip),%rax
or     (%rax),%edx
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
movsbl 0x200(%rax),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <munmap@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
cmp    $0x1,%edi
jg     117c <main+0x1c>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    11f7 <main+0x97>
mov    0x8(%rsi),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2e60(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    %r14d,%edi
call   1060 <close@plt>
movsbl 0x200(%rbp),%esi
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x0,0x200(%rbp)
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"filename required\0A\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [19 x i8] c"current value: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4096, i32* %8
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %42
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %8
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* %6
%27 = call i8* @mmap(i32 0, i32 %21, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %7
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 512
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 512
%37 = bitcast i8* %36 to i32*
store i32* %37, i32** %10
%38 = load i32*, i32** %10
store i32 0, i32* %38
%39 = load i8*, i8** %7
%40 = load i32, i32* %8
%41 = call i32 @munmap(i8* %39, i32 %40)
store i32 0, i32* %3
br label %42
42: ; preds = %15, %13
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [19 x i8] c"current value: %d\0A\00"
@str = [18 x i8] c"filename required\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 ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %23
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32 0, i32 4096, i32 %13, i32 %14, i32 %10, i32 0)
%16 = tail call i32 @close(i32 %10)
%17 = getelementptr inbounds i8, i8* %15, i64 512
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = bitcast i8* %17 to i32*
store i32 0, i32* %21
%22 = tail call i32 @munmap(i8* %15, i32 4096)
br label %23
23: ; preds = %6, %4
%24 = phi i32 [ 1, %4 ], [ 0, %6 ]
ret i32 %24
}