#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 scalar_t__ ssize_t;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ S_IWUSR;
int allowedAccess(int, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getuid();
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int isAllowed;
  int userId = getuid();
  isAllowed = allowedAccess(userId, argv[1]);
  if (isAllowed) {
    ssize_t bytesWritten;
    int fd;
    char *fileName = argv[1];
    char writeString[29] = "Test string to write to file\n";
    fd = open(fileName, O_WRONLY | O_CREAT | O_APPEND, S_IWUSR);
    bytesWritten = write(fd, writeString, 29);
    printf("Bytes written: %d\n", (int)bytesWritten);
    close(fd);
  } else {
    printf("Permission denied.\n");
    exit(1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <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     1252 <main+0xe2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0xe40(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe3d(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe3a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe38(%rip),%eax
mov    %eax,-0x38(%rbp)
mov    0xe33(%rip),%al
mov    %al,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1d,%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
jmp    126a <main+0xfa>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    0x8(%rbx),%rsi
mov    %eax,%edi
call   10a0 <allowedAccess@plt>
test   %eax,%eax
je     1212 <main+0x92>
mov    0x8(%rbx),%rdi
movups 0xe64(%rip),%xmm0
movups %xmm0,0xd(%rsp)
movaps 0xe4b(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e08(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1d,%edx
call   1040 <write@plt>
lea    0xe23(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.main.writeString = [29 x i8] c"Test string to write to file\0A"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IWUSR = global i32 0
@.str = [19 x i8] c"Bytes written: %d\0A\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 i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [29 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 %41
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 = bitcast [29 x i8]* %11 to i8*
%25 = load i8*, i8** %10
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = load i32, i32* @O_APPEND
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IWUSR
%32 = call i32 @open(i8* %25, i32 %30, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = getelementptr inbounds [29 x i8], [29 x i8]* %11, i64 0, i64 0
%35 = call i64 @write(i32 %33, i8* %34, i32 29)
store i64 %35, i64* %8
%36 = load i64, i64* %8
%37 = trunc i64 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
br label %44
41: ; preds = %2
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
@__const.main.writeString = [29 x i8] c"Test string to write to file\0A"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IWUSR = global i32 0
@.str = [19 x i8] c"Bytes written: %d\0A\00"
@str = [19 x i8] c"Permission denied.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [29 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 %23, label %9
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = getelementptr inbounds [29 x i8], [29 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_APPEND
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IWUSR
%18 = tail call i32 @open(i8* %10, i32 %16, i32 %17)
%19 = call i64 @write(i32 %18, i8* %11, i32 29)
%20 = trunc i64 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = tail call i32 @close(i32 %18)
ret i32 0
23: ; preds = %2
%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
}