#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*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_IRUSR;
int allowedAccess(int, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getuid();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int main(int argc, char **argv) {
  int isAllowed;
  int userId = getuid();
  if (argc != 2) {
    printf("Usage: ./run <file name> \n");
    exit(1);
  }
  isAllowed = allowedAccess(userId, argv[1]);
  if (isAllowed) {
    ssize_t bytesRead;
    int fd;
    char *fileName = argv[1];
    char readBuf[512];
    fd = open(fileName, O_RDONLY, S_IRUSR);
    bytesRead = read(fd, readBuf, 512);
    close(fd);
    printf("****** File Contents *******\n%s", readBuf);
  } else {
    printf("Permission denied.\n");
    exit(1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b5 <main+0x45>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1090 <allowedAccess@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1235 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
lea    -0x230(%rbp),%rsi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124d <main+0xdd>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    $0x2,%ebp
jne    11fc <main+0x7c>
mov    0x8(%rbx),%rsi
mov    %eax,%edi
call   10a0 <allowedAccess@plt>
test   %eax,%eax
je     1205 <main+0x85>
mov    0x8(%rbx),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe1a(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe30(%rip),%rdi
jmp    120c <main+0x8c>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: ./run <file name> \0A\00"
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [32 x i8] c"****** File Contents *******\0A%s\00"
@.str.2 = [20 x i8] c"Permission denied.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (...) @getuid()
store i32 %12, i32* %7
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %7
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @allowedAccess(i32 %19, i8* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
store i8* %29, i8** %10
%30 = load i8*, i8** %10
%31 = load i32, i32* @O_RDONLY
%32 = load i32, i32* @S_IRUSR
%33 = call i32 @open(i8* %30, i32 %31, i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 512)
store i32 %36, i32* %8
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %44
41: ; preds = %18
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [32 x i8] c"****** File Contents *******\0A%s\00"
@str = [19 x i8] c"Permission denied.\00"
@str.3 = [26 x i8] c"Usage: ./run <file name> \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = tail call i32 (...) @getuid()
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.3, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @allowedAccess(i32 %4, i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %9
%15 = load i8*, i8** %10
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @S_IRUSR
%19 = tail call i32 @open(i8* %15, i32 %17, i32 %18)
%20 = call i32 @read(i32 %19, i8* %16, i32 512)
%21 = tail call i32 @close(i32 %19)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %16)
ret i32 0
23: ; preds = %9
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
}