#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 */
struct stat {
  scalar_t__ st_uid;
  int st_mode;
};

/* Variables and functions */
int S_IWGRP;
int S_IWOTH;
int /*<<< orphan*/ printf(char *, char *);
int stat(char *, struct stat *);
int check_directory_access(char *directory) {
  struct stat statbuf;
  printf("Checking %s\n", directory);
  if (stat(directory, &statbuf) == -1) {
    printf("FAIL: %s. Could not obtain directory status\n", directory);
    return 1;
  }
  if (statbuf.st_uid != 0) {
    printf("FAIL: %s. Invalid owner\n", directory);
    return 1;
  }
  if ((statbuf.st_mode & S_IWGRP) || (statbuf.st_mode & S_IWOTH)) {
    printf("FAIL: %s. Invalid write access\n", directory);
    return 1;
  }
  printf("PASS: %s\n", directory);
  return 0;
}


x86 assembly (clang, -O0):
<check_directory_access>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xec9(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    116e <check_directory_access+0x4e>
mov    -0x10(%rbp),%rsi
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <check_directory_access+0xd2>
cmpq   $0x0,-0x20(%rbp)
je     1195 <check_directory_access+0x75>
mov    -0x10(%rbp),%rsi
lea    0xeb6(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <check_directory_access+0xd2>
mov    -0x18(%rbp),%eax
mov    0x2e49(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11bf <check_directory_access+0x9f>
mov    -0x18(%rbp),%eax
mov    0x2e1c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11db <check_directory_access+0xbb>
mov    -0x10(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <check_directory_access+0xd2>
mov    -0x10(%rbp),%rsi
lea    0xe8d(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_directory_access>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xed0(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     1160 <check_directory_access+0x40>
cmpq   $0x0,0x8(%rsp)
je     1169 <check_directory_access+0x49>
lea    0xedc(%rip),%rdi
jmp    11a2 <check_directory_access+0x82>
lea    0xea6(%rip),%rdi
jmp    11a2 <check_directory_access+0x82>
mov    0x10(%rsp),%eax
mov    0x2e74(%rip),%rcx
test   %eax,(%rcx)
je     1181 <check_directory_access+0x61>
lea    0xed4(%rip),%rdi
jmp    11a2 <check_directory_access+0x82>
mov    0x2e48(%rip),%rcx
xor    %ebp,%ebp
test   %eax,(%rcx)
setne  %bpl
lea    0xedc(%rip),%rax
lea    0xeb5(%rip),%rdi
cmove  %rax,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [13 x i8] c"Checking %s\0A\00"
@.str.1 = [45 x i8] c"FAIL: %s. Could not obtain directory status\0A\00"
@.str.2 = [25 x i8] c"FAIL: %s. Invalid owner\0A\00"
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.3 = [32 x i8] c"FAIL: %s. Invalid write access\0A\00"
@.str.4 = [10 x i8] c"PASS: %s\0A\00"
define i32 @check_directory_access(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %3
%8 = call i32 @stat(i8* %7, %struct.stat* %4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 1, i32* %2
br label %38
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i64, i64* %14
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 1, i32* %2
br label %38
20: ; preds = %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @S_IWGRP
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %20
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i32, i32* @S_IWOTH
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26, %20
%33 = load i8*, i8** %3
%34 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %33)
store i32 1, i32* %2
br label %38
35: ; preds = %26
%36 = load i8*, i8** %3
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %36)
store i32 0, i32* %2
br label %38
38: ; preds = %35, %32, %17, %10
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [13 x i8] c"Checking %s\0A\00"
@.str.1 = [45 x i8] c"FAIL: %s. Could not obtain directory status\0A\00"
@.str.2 = [25 x i8] c"FAIL: %s. Invalid owner\0A\00"
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.3 = [32 x i8] c"FAIL: %s. Invalid write access\0A\00"
@.str.4 = [10 x i8] c"PASS: %s\0A\00"
define i32 @check_directory_access(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32 @stat(i8* %0, %struct.stat* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %24, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IWGRP
%15 = and i32 %14, %13
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %11
%18 = load i32, i32* @S_IWOTH
%19 = and i32 %18, %13
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0)
%22 = xor i1 %20, true
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %17, %11, %7, %1
%25 = phi i8* [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %7 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), %11 ], [ %21, %17 ]
%26 = phi i32 [ 1, %1 ], [ 1, %7 ], [ 1, %11 ], [ %23, %17 ]
%27 = tail call i32 @printf(i8* %25, i8* %0)
ret i32 %26
}