#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 */
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
scalar_t__ getegid();
scalar_t__ geteuid();
int i_have_permission(const struct stat *st, int op) {
  if (st->st_mode & S_IRWXU & op) {
    if (geteuid() == st->st_uid) {
      return 1;
    }
  }
  if (st->st_mode & S_IRWXG & op) {
    if (getegid() == st->st_gid) {
      return 1;
    }
  }
  if (st->st_mode & S_IRWXO & op) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i_have_permission>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2ea4(%rip),%rcx
and    (%rcx),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     1170 <i_have_permission+0x50>
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
jne    116b <i_have_permission+0x4b>
movl   $0x1,-0x4(%rbp)
jmp    11df <i_have_permission+0xbf>
jmp    1170 <i_have_permission+0x50>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e5b(%rip),%rcx
and    (%rcx),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     11b1 <i_have_permission+0x91>
mov    $0x0,%al
call   1040 <getegid@plt>
mov    -0x10(%rbp),%rcx
cmp    0x10(%rcx),%rax
jne    11ac <i_have_permission+0x8c>
movl   $0x1,-0x4(%rbp)
jmp    11df <i_have_permission+0xbf>
jmp    11b1 <i_have_permission+0x91>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e32(%rip),%rcx
and    (%rcx),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     11d8 <i_have_permission+0xb8>
movl   $0x1,-0x4(%rbp)
jmp    11df <i_have_permission+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i_have_permission>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2eb1(%rip),%rax
mov    (%rdi),%ecx
and    %esi,%ecx
test   %ecx,(%rax)
je     1150 <i_have_permission+0x30>
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    $0x1,%eax
cmp    0x8(%rbx),%rcx
je     1182 <i_have_permission+0x62>
mov    (%rbx),%ecx
and    %ebp,%ecx
mov    0x2e81(%rip),%rax
test   %ecx,(%rax)
je     1174 <i_have_permission+0x54>
xor    %eax,%eax
call   1040 <getegid@plt>
mov    %rax,%rcx
mov    $0x1,%eax
cmp    0x10(%rbx),%rcx
je     1182 <i_have_permission+0x62>
and    (%rbx),%ebp
mov    %ebp,%ecx
mov    0x2e75(%rip),%rdx
xor    %eax,%eax
test   %ecx,(%rdx)
setne  %al
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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