#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 O_APPEND;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getuid();
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int fd;
  fd = open("/etc/zzz", O_RDWR | O_APPEND);
  if (fd == -1) {
    printf("Cannot open /etc/zzz\n");
    exit(0);
  }
  sleep(1);
  setuid(getuid());
  if (fork()) {
    close(fd);
    exit(0);
  } else {
    write(fd, "Malicious Data\n", 15);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d6 <main+0x46>
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   1090 <setuid@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
je     120e <main+0x7e>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    0xe07(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e50(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0x65>
mov    %eax,%ebx
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1090 <setuid@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    1208 <main+0x78>
lea    0xe1f(%rip),%rsi
mov    %ebx,%edi
mov    $0xf,%edx
call   1040 <write@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1060 <close@plt>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/etc/zzz\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [22 x i8] c"Cannot open /etc/zzz\0A\00"
@.str.2 = [16 x i8] c"Malicious Data\0A\00"
define void @main() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_APPEND
%4 = or i32 %2, %3
%5 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = call i32 @sleep(i32 1)
%13 = call i32 (...) @getuid()
%14 = call i32 @setuid(i32 %13)
%15 = call i64 (...) @fork()
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %11
%22 = load i32, i32* %1
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 15)
%24 = load i32, i32* %1
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %21
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/etc/zzz\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.2 = [16 x i8] c"Malicious Data\0A\00"
@str = [21 x i8] c"Cannot open /etc/zzz\00"
define void @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_APPEND
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %0
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 (...) @getuid()
%12 = tail call i32 @setuid(i32 %11)
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = tail call i32 @close(i32 %4)
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %9
%19 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 15)
%20 = tail call i32 @close(i32 %4)
ret void
}