#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);
scalar_t__ fork();
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  char *c;
  int fd;
  fd = open("/tmp/delme", O_RDWR);
  c = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 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    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11f3 <main+0xa3>
jmp    11ce <main+0x7e>
mov    -0x18(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1030 <strcpy@plt>
jmp    11ce <main+0x7e>
jmp    11f8 <main+0xa8>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     1217 <main+0xc7>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    11f8 <main+0xa8>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    0x2e87(%rip),%rcx
mov    0x2e50(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e65(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11e0 <main+0x90>
lea    0xe5b(%rip),%r14
lea    0xe60(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
jmp    11c0 <main+0x70>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x90>
mov    %eax,%edi
call   1050 <close@plt>
jmp    11e0 <main+0x90>

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_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
%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)
store i32 %9, i32* %7
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_SHARED
%14 = load i32, i32* %7
%15 = call i8* @mmap(i32 0, i32 4096, i32 %12, i32 %13, i32 %14, i32 0)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
store i8 0, i8* %16
%17 = call i64 (...) @fork()
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %2
br label %20
20: ; preds = %19, %20
%21 = load i8*, i8** %6
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %20
25: ; preds = %2
br label %26
26: ; preds = %25, %33
%27 = load i8*, i8** %6
%28 = call i32 @open(i8* %27, i32 0)
store i32 %28, i32* %7
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %30, %26
br label %26
}

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_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_SHARED
%9 = tail call i8* @mmap(i32 0, i32 4096, i32 %7, i32 %8, i32 %4, i32 0)
store i8 0, i8* %9
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2, %12
%13 = tail call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %12
15: ; preds = %2, %20
%16 = tail call i32 @open(i8* %9, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @close(i32 %16)
br label %20
20: ; preds = %18, %15
br label %15
}