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

/* Variables and functions */
scalar_t__ fstat(int, struct stat *);
scalar_t__ geteuid();
scalar_t__ hasopperm(int /*<<< orphan*/, int, int);
scalar_t__ userhasgid(int /*<<< orphan*/);
bool fhasaccess(int fd, int op) {
  struct stat stat;
  if (geteuid() == 0) {
    return true;
  }
  if (fstat(fd, &stat) != 0) {
    return false;
  }
  if (stat.st_uid == geteuid()) {
    return true;
  }
  if (hasopperm(stat.st_mode, op, 0)) {
    return true;
  }
  if (hasopperm(stat.st_mode, op, 1) && userhasgid(stat.st_gid)) {
    return true;
  }
  return false;
}


x86 assembly (clang, -O0):
<fhasaccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    $0x0,%rax
jne    116b <fhasaccess+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    121a <fhasaccess+0xda>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
je     118d <fhasaccess+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    121a <fhasaccess+0xda>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jne    11b8 <fhasaccess+0x78>
movl   $0x1,-0x4(%rbp)
jmp    121a <fhasaccess+0xda>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1040 <hasopperm@plt>
cmp    $0x0,%rax
je     11db <fhasaccess+0x9b>
movl   $0x1,-0x4(%rbp)
jmp    121a <fhasaccess+0xda>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
call   1040 <hasopperm@plt>
cmp    $0x0,%rax
je     1213 <fhasaccess+0xd3>
mov    -0x18(%rbp),%edi
call   1060 <userhasgid@plt>
cmp    $0x0,%rax
je     1213 <fhasaccess+0xd3>
movl   $0x1,-0x4(%rbp)
jmp    121a <fhasaccess+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fhasaccess>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <geteuid@plt>
test   %rax,%rax
je     117f <fhasaccess+0x3f>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <fstat@plt>
test   %rax,%rax
jne    1184 <fhasaccess+0x44>
mov    0x8(%rsp),%rbx
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    %rax,%rbx
jne    1191 <fhasaccess+0x51>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x14(%rsp),%r14d
mov    %r14d,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1040 <hasopperm@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1184 <fhasaccess+0x44>
mov    $0x1,%ebp
mov    %r14d,%edi
mov    %r15d,%esi
mov    $0x1,%edx
call   1040 <hasopperm@plt>
test   %rax,%rax
je     11d5 <fhasaccess+0x95>
mov    0x10(%rsp),%edi
call   1060 <userhasgid@plt>
test   %rax,%rax
jne    1184 <fhasaccess+0x44>
xor    %ebp,%ebp
jmp    1184 <fhasaccess+0x44>

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

LLVM IR (-O2):
%struct.stat = type { i64, i32, i32 }
define i32 @fhasaccess(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i64 (...) @geteuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %2
%8 = call i64 @fstat(i32 %0, %struct.stat* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i64, i64* %11
%13 = tail call i64 (...) @geteuid()
%14 = icmp eq i64 %12, %13
br i1 %14, label %29, label %15
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%17 = load i32, i32* %16
%18 = tail call i64 @hasopperm(i32 %17, i32 %1, i32 0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = tail call i64 @hasopperm(i32 %17, i32 %1, i32 1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%25 = load i32, i32* %24
%26 = tail call i64 @userhasgid(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23, %20
br label %29
29: ; preds = %23, %15, %10, %7, %2, %28
%30 = phi i32 [ 0, %28 ], [ 1, %2 ], [ 0, %7 ], [ 1, %10 ], [ 1, %15 ], [ 1, %23 ]
ret i32 %30
}