#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 */

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ parse_access_type(char const *, char *);
int read(int, char *, int);
int snprintf(char *, int, char *, char const *, char const *, char *);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int smack_have_access(const char *subject, const char *object,
                      const char *access_type) {
  char buf[(2 * (255 + 1) + 7) + 1];
  char access_type_kernel[7 + 1];
  int ret;
  int fd;
  int access2 = 1;
  fd = open("/smack/"
            "access2",
            O_RDWR);
  if (fd == -1) {
    if (errno != ENOENT)
      return -1;
    fd = open("/smack/"
              "access",
              O_RDWR);
    if (fd == -1)
      return -1;
    access2 = 0;
  }
  parse_access_type(access_type, access_type_kernel);
  if (access2)
    ret = snprintf(buf, (2 * (255 + 1) + 7) + 1, "%s %s %s", subject, object,
                   access_type_kernel);
  else
    ret = snprintf(buf, (2 * (255 + 1) + 7) + 1, "%-23s %-23s %5.5s", subject,
                   object, access_type_kernel);
  if (ret < 0) {
    close(fd);
    return -1;
  }
  ret = write(fd, buf, strlen(buf));
  if (ret == -1) {
    close(fd);
    return -1;
  }
  ret = read(fd, buf, 1);
  close(fd);
  if (ret == -1)
    return -1;
  return buf[0] == '1';
}


x86 assembly (clang, -O0):
<smack_have_access>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x244(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0xffffffff,-0x240(%rbp)
jne    121d <smack_have_access+0xad>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     11df <smack_have_access+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <smack_have_access+0x207>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0xffffffff,-0x240(%rbp)
jne    1213 <smack_have_access+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <smack_have_access+0x207>
movl   $0x0,-0x244(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x238(%rbp),%rsi
call   1060 <parse_access_type@plt>
cmpl   $0x0,-0x244(%rbp)
je     126c <smack_have_access+0xfc>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x238(%rbp),%r9
mov    $0x208,%esi
lea    0xdc1(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x23c(%rbp)
jmp    1299 <smack_have_access+0x129>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x238(%rbp),%r9
mov    $0x208,%esi
lea    0xd98(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0x0,-0x23c(%rbp)
jge    12bd <smack_have_access+0x14d>
mov    -0x240(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <smack_have_access+0x207>
mov    -0x240(%rbp),%eax
mov    %eax,-0x254(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0xffffffff,-0x23c(%rbp)
jne    1321 <smack_have_access+0x1b1>
mov    -0x240(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <smack_have_access+0x207>
mov    -0x240(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %eax,-0x23c(%rbp)
mov    -0x240(%rbp),%edi
call   1070 <close@plt>
cmpl   $0xffffffff,-0x23c(%rbp)
jne    1362 <smack_have_access+0x1f2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <smack_have_access+0x207>
movsbl -0x230(%rbp),%eax
cmp    $0x31,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_have_access>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e57(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe64(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     123a <smack_have_access+0xca>
mov    %eax,%ebx
lea    0xe6a(%rip),%rbp
lea    0x8(%rsp),%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <parse_access_type@plt>
lea    0x10(%rsp),%rdi
mov    $0x208,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    %r14,%r8
mov    %r13,%r9
call   1050 <snprintf@plt>
test   %eax,%eax
js     122c <smack_have_access+0xbc>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122c <smack_have_access+0xbc>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%ebp
je     1233 <smack_have_access+0xc3>
xor    %ebp,%ebp
cmpb   $0x31,0x10(%rsp)
sete   %bpl
jmp    1278 <smack_have_access+0x108>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebp
jmp    1278 <smack_have_access+0x108>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d7d(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    1278 <smack_have_access+0x108>
mov    0x0(%r13),%esi
lea    0xdaf(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1278 <smack_have_access+0x108>
mov    %eax,%ebx
lea    0xdb3(%rip),%rbp
jmp    11b3 <smack_have_access+0x43>
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/smack/access2\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"/smack/access\00"
@.str.2 = [9 x i8] c"%s %s %s\00"
@.str.3 = [18 x i8] c"%-23s %-23s %5.5s\00"
define i32 @smack_have_access(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [520 x i8]
%9 = alloca [8 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 1, i32* %12
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %29
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %4
br label %79
22: ; preds = %17
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %4
br label %79
28: ; preds = %22
store i32 0, i32* %12
br label %29
29: ; preds = %28, %3
%30 = load i8*, i8** %7
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%32 = call i32 @parse_access_type(i8* %30, i8* %31)
%33 = load i32, i32* %12
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = getelementptr inbounds [520 x i8], [520 x i8]* %8, i64 0, i64 0
%37 = load i8*, i8** %5
%38 = load i8*, i8** %6
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%40 = call i32 @snprintf(i8* %36, i32 520, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %37, i8* %38, i8* %39)
store i32 %40, i32* %10
br label %47
41: ; preds = %29
%42 = getelementptr inbounds [520 x i8], [520 x i8]* %8, i64 0, i64 0
%43 = load i8*, i8** %5
%44 = load i8*, i8** %6
%45 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%46 = call i32 @snprintf(i8* %42, i32 520, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %43, i8* %44, i8* %45)
store i32 %46, i32* %10
br label %47
47: ; preds = %41, %35
%48 = load i32, i32* %10
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %11
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %4
br label %79
53: ; preds = %47
%54 = load i32, i32* %11
%55 = getelementptr inbounds [520 x i8], [520 x i8]* %8, i64 0, i64 0
%56 = getelementptr inbounds [520 x i8], [520 x i8]* %8, i64 0, i64 0
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @write(i32 %54, i8* %55, i32 %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %53
%62 = load i32, i32* %11
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %4
br label %79
64: ; preds = %53
%65 = load i32, i32* %11
%66 = getelementptr inbounds [520 x i8], [520 x i8]* %8, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 1)
store i32 %67, i32* %10
%68 = load i32, i32* %11
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %10
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %73
72: ; preds = %64
store i32 -1, i32* %4
br label %79
73: ; preds = %64
%74 = getelementptr inbounds [520 x i8], [520 x i8]* %8, i64 0, i64 0
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 49
%78 = zext i1 %77 to i32
store i32 %78, i32* %4
br label %79
79: ; preds = %73, %72, %61, %50, %27, %21
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
@.str = [15 x i8] c"/smack/access2\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"/smack/access\00"
@.str.2 = [9 x i8] c"%s %s %s\00"
@.str.3 = [18 x i8] c"%-23s %-23s %5.5s\00"
define i32 @smack_have_access(i8* %0, i8* %1, i8* %2) {
%4 = alloca [520 x i8]
%5 = alloca [8 x i8]
%6 = getelementptr inbounds [520 x i8], [520 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %41
15: ; preds = %11
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %41, label %19
19: ; preds = %15, %3
%20 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %3 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %15 ]
%21 = phi i32 [ %9, %3 ], [ %17, %15 ]
%22 = call i32 @parse_access_type(i8* %2, i8* %7)
%23 = call i32 @snprintf(i8* %6, i32 520, i8* %20, i8* %0, i8* %1, i8* %7)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @close(i32 %21)
br label %41
27: ; preds = %19
%28 = call i32 @strlen(i8* %6)
%29 = call i32 @write(i32 %21, i8* %6, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @close(i32 %21)
br label %41
33: ; preds = %27
%34 = call i32 @read(i32 %21, i8* %6, i32 1)
%35 = call i32 @close(i32 %21)
%36 = icmp eq i32 %34, -1
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = load i8, i8* %6
%39 = icmp eq i8 %38, 49
%40 = zext i1 %39 to i32
br label %41
41: ; preds = %33, %15, %11, %37, %31, %25
%42 = phi i32 [ -1, %25 ], [ -1, %31 ], [ %40, %37 ], [ -1, %11 ], [ -1, %15 ], [ -1, %33 ]
ret i32 %42
}