#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 MAP_FIXED;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ mmap(char *, int, int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  char *c = (char *)0x94000000;
  int fd;
  open("/tmp/delme", O_RDWR);
  mmap(c, 4096, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, 3, 0);
  *c = 0;
  if (fork()) {
    while (1) {
      strcpy(c, "/etc/passwd");
      strcpy(c, "/etc/shadow");
    }
  } else {
    while (1)
      if ((fd = open(c, 0)) != -1)
        close(fd);
  }
  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)
mov    $0x94000000,%eax
mov    %rax,-0x18(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
mov    0x2e30(%rip),%rax
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3e(%rip),%rax
or     (%rax),%ecx
mov    $0x1000,%esi
mov    $0x3,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     1201 <main+0xb1>
jmp    11dc <main+0x8c>
mov    -0x18(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1030 <strcpy@plt>
jmp    11dc <main+0x8c>
jmp    1206 <main+0xb6>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     1225 <main+0xd5>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1206 <main+0xb6>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x94000000,%ebx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1060 <open@plt>
mov    0x2e83(%rip),%rax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e39(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x94000000,%edi
mov    $0x1000,%esi
mov    $0x3,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
movb   $0x0,(%rbx)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11f0 <main+0xa0>
lea    0xe4b(%rip),%r14
lea    0xe50(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0x94000000,%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    $0x94000000,%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    11d0 <main+0x80>
nopl   0x0(%rax)
mov    $0x94000000,%edi
xor    %esi,%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0xa0>
mov    %eax,%edi
call   1050 <close@plt>
jmp    11f0 <main+0xa0>

LLVM IR (-O0):
@.str = [11 x i8] c"/tmp/delme\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [12 x i8] c"/etc/passwd\00"
@.str.2 = [12 x i8] c"/etc/shadow\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* inttoptr (i64 2483027968 to i8*), i8** %6
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i8*, i8** %6
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_FIXED
%15 = load i32, i32* @MAP_SHARED
%16 = or i32 %14, %15
%17 = call i32 @mmap(i8* %10, i32 4096, i32 %13, i32 %16, i32 3, i32 0)
%18 = load i8*, i8** %6
store i8 0, i8* %18
%19 = call i64 (...) @fork()
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %2
br label %22
22: ; preds = %21, %22
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = load i8*, i8** %6
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %22
27: ; preds = %2
br label %28
28: ; preds = %27, %35
%29 = load i8*, i8** %6
%30 = call i32 @open(i8* %29, i32 0)
store i32 %30, i32* %7
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %28
br label %28
}

LLVM IR (-O2):
@.str = [11 x i8] c"/tmp/delme\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [12 x i8] c"/etc/passwd\00"
@.str.2 = [12 x i8] c"/etc/shadow\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_FIXED
%9 = load i32, i32* @MAP_SHARED
%10 = or i32 %9, %8
%11 = tail call i32 @mmap(i8* inttoptr (i64 2483027968 to i8*), i32 4096, i32 %7, i32 %10, i32 3, i32 0)
store i8 0, i8* inttoptr (i64 2483027968 to i8*)
%12 = tail call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %2, %14
%15 = tail call i32 @strcpy(i8* inttoptr (i64 2483027968 to i8*), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @strcpy(i8* inttoptr (i64 2483027968 to i8*), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %14
17: ; preds = %2, %22
%18 = tail call i32 @open(i8* inttoptr (i64 2483027968 to i8*), i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @close(i32 %18)
br label %22
22: ; preds = %20, %17
br label %17
}