#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;
  int /*<<< orphan*/ st_gid;
};
typedef int mode_t;

/* Variables and functions */
int _file_rwx_g(int /*<<< orphan*/);
scalar_t__ geteuid();
scalar_t__ stat(char *, struct stat *);
int _is_file_rwx(char *pathname, mode_t u, mode_t g, mode_t o) {
  struct stat st;
  if (stat(pathname, &st) != 0)
    return 0;
  if (geteuid() == st.st_uid)
    if ((st.st_mode & u) != 0)
      return 1;
  if ((st.st_mode & g) != 0) {
    if (_file_rwx_g(st.st_gid) == 1)
      return 1;
    if (geteuid() == st.st_uid)
      return 0;
  }
  return (st.st_mode & o) != 0 ? 1 : 0;
}


x86 assembly (clang, -O0):
<_is_file_rwx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1168 <_is_file_rwx+0x38>
movl   $0x0,-0x4(%rbp)
jmp    11fd <_is_file_rwx+0xcd>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    -0x30(%rbp),%rax
jne    1199 <_is_file_rwx+0x69>
mov    -0x28(%rbp),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     1194 <_is_file_rwx+0x64>
movl   $0x1,-0x4(%rbp)
jmp    11fd <_is_file_rwx+0xcd>
jmp    1199 <_is_file_rwx+0x69>
mov    -0x28(%rbp),%eax
and    -0x18(%rbp),%eax
cmp    $0x0,%eax
je     11e7 <_is_file_rwx+0xb7>
mov    -0x24(%rbp),%edi
call   1050 <_file_rwx_g@plt>
cmp    $0x1,%eax
jne    11c5 <_is_file_rwx+0x95>
movl   $0x1,-0x4(%rbp)
jmp    11fd <_is_file_rwx+0xcd>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    -0x30(%rbp),%rax
jne    11e2 <_is_file_rwx+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    11fd <_is_file_rwx+0xcd>
jmp    11e7 <_is_file_rwx+0xb7>
mov    -0x28(%rbp),%edx
and    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_is_file_rwx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    119e <_is_file_rwx+0x6e>
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    0x10(%rsp),%ecx
mov    $0x1,%ebp
cmp    0x8(%rsp),%rax
jne    116e <_is_file_rwx+0x3e>
and    %ecx,%ebx
jne    119e <_is_file_rwx+0x6e>
test   %r15d,%ecx
je     1195 <_is_file_rwx+0x65>
mov    0x14(%rsp),%edi
call   1050 <_file_rwx_g@plt>
cmp    $0x1,%eax
je     119e <_is_file_rwx+0x6e>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    0x8(%rsp),%rax
je     119e <_is_file_rwx+0x6e>
mov    0x10(%rsp),%ecx
xor    %ebp,%ebp
test   %r14d,%ecx
setne  %bpl
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32, i32 }
define i32 @_is_file_rwx(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = call i64 @stat(i8* %11, %struct.stat* %10)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 0, i32* %5
br label %55
15: ; preds = %4
%16 = call i64 (...) @geteuid()
%17 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%18 = load i64, i64* %17
%19 = icmp eq i64 %16, %18
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* %7
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 1, i32* %5
br label %55
27: ; preds = %20
br label %28
28: ; preds = %27, %15
%29 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i32, i32* %8
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %28
%35 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @_file_rwx_g(i32 %36)
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 1, i32* %5
br label %55
40: ; preds = %34
%41 = call i64 (...) @geteuid()
%42 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%43 = load i64, i64* %42
%44 = icmp eq i64 %41, %43
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 0, i32* %5
br label %55
46: ; preds = %40
br label %47
47: ; preds = %46, %28
%48 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load i32, i32* %9
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
%53 = zext i1 %52 to i64
%54 = select i1 %52, i32 1, i32 0
store i32 %54, i32* %5
br label %55
55: ; preds = %47, %45, %39, %26, %14
%56 = load i32, i32* %5
ret i32 %56
}

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