#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;
  scalar_t__ st_gid;
};

/* Variables and functions */
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ getgid();
scalar_t__ getuid();
int stat(char *, struct stat *);
bool check_file_perm(char *filename) {
  struct stat statbuf;
  int retval = stat(filename, &statbuf);
  if (!retval) {
    if (statbuf.st_uid == getuid() && (S_IXUSR & statbuf.st_mode)) {
      return true;
    } else if (statbuf.st_gid == getgid() && (S_IXGRP & statbuf.st_mode)) {
      return true;
    } else if (S_IXOTH & statbuf.st_mode) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}


x86 assembly (clang, -O0):
<check_file_perm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1203 <check_file_perm+0xd3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jne    1196 <check_file_perm+0x66>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
and    -0x20(%rbp),%eax
cmp    $0x0,%eax
je     1196 <check_file_perm+0x66>
movl   $0x1,-0x4(%rbp)
jmp    120a <check_file_perm+0xda>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <getgid@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    11d6 <check_file_perm+0xa6>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
and    -0x20(%rbp),%eax
cmp    $0x0,%eax
je     11d6 <check_file_perm+0xa6>
movl   $0x1,-0x4(%rbp)
jmp    120a <check_file_perm+0xda>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
and    -0x20(%rbp),%eax
cmp    $0x0,%eax
je     11f7 <check_file_perm+0xc7>
movl   $0x1,-0x4(%rbp)
jmp    120a <check_file_perm+0xda>
movl   $0x0,-0x4(%rbp)
jmp    120a <check_file_perm+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_file_perm>:
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11a5 <check_file_perm+0x75>
mov    0x8(%rsp),%rbx
xor    %eax,%eax
call   1030 <getuid@plt>
cmp    %rax,%rbx
jne    116c <check_file_perm+0x3c>
mov    0x2e81(%rip),%rcx
mov    0x10(%rsp),%edx
mov    $0x1,%eax
test   %edx,(%rcx)
jne    11a5 <check_file_perm+0x75>
mov    0x18(%rsp),%rbx
xor    %eax,%eax
call   1050 <getgid@plt>
cmp    %rax,%rbx
jne    1193 <check_file_perm+0x63>
mov    0x2e54(%rip),%rdx
mov    0x10(%rsp),%ecx
mov    $0x1,%eax
test   %ecx,(%rdx)
jne    11a5 <check_file_perm+0x75>
jmp    1197 <check_file_perm+0x67>
mov    0x10(%rsp),%ecx
mov    0x2e4a(%rip),%rdx
xor    %eax,%eax
test   %ecx,(%rdx)
setne  %al
add    $0x20,%rsp
pop    %rbx
ret

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

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