#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();
  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)
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     1213 <main+0xa3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%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    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122b <main+0xbb>
lea    0xe06(%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   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    0x8(%rbx),%rsi
mov    %eax,%edi
call   10a0 <allowedAccess@plt>
test   %eax,%eax
je     11f7 <main+0x77>
mov    0x8(%rbx),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe20(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str = [32 x i8] c"****** File Contents *******\0A%s\00"
@.str.1 = [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* %7
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @allowedAccess(i32 %13, i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %35
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = load i32, i32* @O_RDONLY
%26 = load i32, i32* @S_IRUSR
%27 = call i32 @open(i8* %24, i32 %25, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 512)
store i32 %30, i32* %8
%31 = load i32, i32* %9
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %38
35: ; preds = %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
ret i32 0
}

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