#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 {
  int st_mode;
  scalar_t__ st_uid;
  scalar_t__ st_gid;
};

/* Variables and functions */
scalar_t__ S_ISDIR(int);
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ stat(char const *, struct stat *);
int isWriteableDir(const char *tmp) {
  struct stat st;
  if (stat(tmp, &st) == 0 && S_ISDIR(st.st_mode)) {
    if ((geteuid() != st.st_uid || (st.st_mode & S_IWUSR) == 0) &&
        (getegid() != st.st_gid || (st.st_mode & S_IWGRP) == 0) &&
        ((st.st_mode & S_IWOTH) == 0)) {
      return 0;
    } else {
      return 1;
    }
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<isWriteableDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11ee <isWriteableDir+0xae>
mov    -0x28(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11ee <isWriteableDir+0xae>
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    -0x20(%rbp),%rax
jne    119b <isWriteableDir+0x5b>
mov    -0x28(%rbp),%eax
mov    0x2e50(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11e2 <isWriteableDir+0xa2>
mov    $0x0,%al
call   1060 <getegid@plt>
cmp    -0x18(%rbp),%rax
jne    11c1 <isWriteableDir+0x81>
mov    -0x28(%rbp),%eax
mov    0x2e32(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11e2 <isWriteableDir+0xa2>
mov    -0x28(%rbp),%eax
mov    0x2dfd(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11e2 <isWriteableDir+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    11f5 <isWriteableDir+0xb5>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <isWriteableDir+0xb5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isWriteableDir>:
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11b0 <isWriteableDir+0x70>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     11b0 <isWriteableDir+0x70>
xor    %eax,%eax
call   1040 <geteuid@plt>
cmp    0x10(%rsp),%rax
jne    1181 <isWriteableDir+0x41>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
jne    11ab <isWriteableDir+0x6b>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getegid@plt>
mov    0x8(%rsp),%ecx
cmp    0x18(%rsp),%rax
jne    11a0 <isWriteableDir+0x60>
mov    0x2e4c(%rip),%rax
test   %ecx,(%rax)
jne    11ab <isWriteableDir+0x6b>
mov    0x2e21(%rip),%rax
test   %ecx,(%rax)
je     11b0 <isWriteableDir+0x70>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i64, i64 }
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32 @isWriteableDir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %43
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @S_ISDIR(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %43
13: ; preds = %8
%14 = call i64 (...) @geteuid()
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%16 = load i64, i64* %15
%17 = icmp ne i64 %14, %16
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @S_IWUSR
%22 = and i32 %20, %21
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %18, %13
%25 = call i64 (...) @getegid()
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 2
%27 = load i64, i64* %26
%28 = icmp ne i64 %25, %27
br i1 %28, label %35, label %29
29: ; preds = %24
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @S_IWGRP
%33 = and i32 %31, %32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %29, %24
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @S_IWOTH
%39 = and i32 %37, %38
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
store i32 0, i32* %2
br label %44
42: ; preds = %35, %29, %18
store i32 1, i32* %2
br label %44
43: ; preds = %8, %1
store i32 0, i32* %2
br label %44
44: ; preds = %43, %42, %41
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i64 }
@S_IWUSR = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32 @isWriteableDir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %36
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %36, label %11
11: ; preds = %6
%12 = call i64 (...) @geteuid()
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%14 = load i64, i64* %13
%15 = icmp eq i64 %12, %14
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* %7
%18 = load i32, i32* @S_IWUSR
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %16, %11
%22 = call i64 (...) @getegid()
%23 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%24 = load i64, i64* %23
%25 = icmp eq i64 %22, %24
%26 = load i32, i32* %7
br i1 %25, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @S_IWGRP
%29 = and i32 %28, %26
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21, %27
%32 = load i32, i32* @S_IWOTH
%33 = and i32 %32, %26
%34 = icmp eq i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %31, %27, %16
br label %36
36: ; preds = %1, %6, %31, %35
%37 = phi i32 [ 1, %35 ], [ 0, %31 ], [ 0, %6 ], [ 0, %1 ]
ret i32 %37
}