#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ system(char *);
void main(int argc, char *argv[]) {
  char *buff, *ptr, *egg, tmp_buf[256];
  long *addr_ptr, addr;
  int offset = 0, bsize = 512;
  int i, eggsize = 1024;
  FILE *fp = NULL;
  memset(tmp_buf, 0x00, 256);
  printf("We are gonna run a few tests for apparmor's policies!\n");
  fp = fopen("/etc/shadow", "r");
  if (fp == NULL)
    printf("[1] no READ permission of /etc/shadow\n");
  if (system("/bin/sh") != 0)
    printf("[2] Spawn shell failed!\n");
  fp = fopen("/home/shawn/hello", "r");
  if (fp != NULL)
    printf("[3] apparmor allows this program read from the file...\n");
  fp = fopen("/home/shawn/world", "w");
  if (fp != NULL)
    printf("[4] apparmor allows this program write to the file...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x144(%rbp)
movl   $0x200,-0x148(%rbp)
movl   $0x400,-0x150(%rbp)
movq   $0x0,-0x158(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
lea    0xe96(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
jne    11ce <main+0x8e>
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe97(%rip),%rdi
call   1030 <system@plt>
cmp    $0x0,%rax
je     11f0 <main+0xb0>
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
lea    0xe96(%rip),%rdi
lea    0xe45(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
je     1224 <main+0xe4>
lea    0xe80(%rip),%rdi
call   1040 <printf@plt>
lea    0xeac(%rip),%rdi
lea    0xe0f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
je     1258 <main+0x118>
lea    0xe96(%rip),%rdi
call   1040 <printf@plt>
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x108,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
lea    0xf9a(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
jne    1186 <main+0x46>
lea    0xf68(%rip),%rdi
call   1030 <puts@plt>
lea    0xe75(%rip),%rdi
call   1040 <system@plt>
test   %rax,%rax
je     11a3 <main+0x63>
lea    0xf33(%rip),%rdi
call   1030 <puts@plt>
lea    0xe60(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c7 <main+0x87>
lea    0xed8(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%rdi
lea    0xf38(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11eb <main+0xab>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
add    $0x108,%rsp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"We are gonna run a few tests for apparmor's policies!\0A\00"
@.str.1 = [12 x i8] c"/etc/shadow\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [39 x i8] c"[1] no READ permission of /etc/shadow\0A\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [25 x i8] c"[2] Spawn shell failed!\0A\00"
@.str.6 = [18 x i8] c"/home/shawn/hello\00"
@.str.7 = [56 x i8] c"[3] apparmor allows this program read from the file...\0A\00"
@.str.8 = [18 x i8] c"/home/shawn/world\00"
@.str.9 = [2 x i8] c"w\00"
@.str.10 = [55 x i8] c"[4] apparmor allows this program write to the file...\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %11
store i32 512, i32* %12
store i32 1024, i32* %14
store i32* null, i32** %15
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 256)
%18 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%19 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %15
%20 = load i32*, i32** %15
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %2
%25 = call i64 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %15
%31 = load i32*, i32** %15
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.7, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
store i32* %36, i32** %15
%37 = load i32*, i32** %15
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.10, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
ret void
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"/etc/shadow\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.6 = [18 x i8] c"/home/shawn/hello\00"
@.str.8 = [18 x i8] c"/home/shawn/world\00"
@.str.9 = [2 x i8] c"w\00"
@str = [54 x i8] c"We are gonna run a few tests for apparmor's policies!\00"
@str.11 = [54 x i8] c"[4] apparmor allows this program write to the file...\00"
@str.12 = [55 x i8] c"[3] apparmor allows this program read from the file...\00"
@str.13 = [24 x i8] c"[2] Spawn shell failed!\00"
@str.14 = [38 x i8] c"[1] no READ permission of /etc/shadow\00"
define void @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 256)
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%7 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.14, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i64 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.13, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.12, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.11, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
ret void
}