#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ access_code_to_str(int, char *);
int /*<<< orphan*/ close(int);
int get_label(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/ *);
scalar_t__ init_smackfs_mnt();
int open_smackfs_file(char *, char *, int /*<<< orphan*/, int *);
int read(int, char *, int);
int snprintf(char *, int, char *, char const *, char const *, char *);
int str_to_access_code(char const *);
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) + 2 * 6 + 1) + 1];
  char str[6 + 1];
  int code;
  int ret;
  int fd;
  int use_long = 1;
  ssize_t slen;
  ssize_t olen;
  if (init_smackfs_mnt())
    return -1;
  slen = get_label(NULL, subject, NULL);
  olen = get_label(NULL, object, NULL);
  if (slen < 0 || olen < 0)
    return -1;
  fd = open_smackfs_file("access2", "access", O_RDWR, &use_long);
  if (fd < 0)
    return -1;
  if (!use_long && (slen > 23 || olen > 23)) {
    close(fd);
    return -1;
  }
  if ((code = str_to_access_code(access_type)) < 0)
    return -1;
  access_code_to_str(code, str);
  if (use_long)
    ret = snprintf(buf, (2 * (255 + 1) + 2 * 6 + 1) + 1, "%s %s %s", subject,
                   object, str);
  else
    ret = snprintf(buf, (2 * (255 + 1) + 2 * 6 + 1) + 1, "%-23s %-23s %5.5s",
                   subject, object, str);
  if (ret < 0 || ret >= (2 * (255 + 1) + 2 * 6 + 1) + 1) {
    close(fd);
    return -1;
  }
  ret = write(fd, buf, strlen(buf));
  if (ret < 0) {
    close(fd);
    return -1;
  }
  ret = read(fd, buf, 1);
  close(fd);
  if (ret < 0)
    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,-0x248(%rbp)
mov    $0x0,%al
call   1030 <init_smackfs_mnt@plt>
cmp    $0x0,%rax
je     11de <smack_have_access+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   10b0 <get_label@plt>
mov    %eax,-0x24c(%rbp)
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   10b0 <get_label@plt>
mov    %eax,-0x250(%rbp)
cmpl   $0x0,-0x24c(%rbp)
jl     1224 <smack_have_access+0x84>
cmpl   $0x0,-0x250(%rbp)
jge    1230 <smack_have_access+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
lea    0xdc0(%rip),%rdi
lea    0xdc1(%rip),%rsi
lea    -0x248(%rbp),%rcx
call   1080 <open_smackfs_file@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jge    1272 <smack_have_access+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
cmpl   $0x0,-0x248(%rbp)
jne    12b0 <smack_have_access+0x110>
cmpl   $0x17,-0x24c(%rbp)
jg     1299 <smack_have_access+0xf9>
cmpl   $0x17,-0x250(%rbp)
jle    12b0 <smack_have_access+0x110>
mov    -0x244(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
mov    -0x20(%rbp),%rdi
call   10c0 <str_to_access_code@plt>
mov    %eax,-0x23c(%rbp)
cmp    $0x0,%eax
jge    12d4 <smack_have_access+0x134>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
mov    -0x23c(%rbp),%edi
lea    -0x237(%rbp),%rsi
call   10a0 <access_code_to_str@plt>
cmpl   $0x0,-0x248(%rbp)
je     1325 <smack_have_access+0x185>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x237(%rbp),%r9
mov    $0x20e,%esi
lea    0xcfa(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x240(%rbp)
jmp    1352 <smack_have_access+0x1b2>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x237(%rbp),%r9
mov    $0x20e,%esi
lea    0xcd1(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0x0,-0x240(%rbp)
jl     136f <smack_have_access+0x1cf>
cmpl   $0x20e,-0x240(%rbp)
jl     1386 <smack_have_access+0x1e6>
mov    -0x244(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
mov    -0x244(%rbp),%eax
mov    %eax,-0x25c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x258(%rbp)
lea    -0x230(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x25c(%rbp),%edi
mov    -0x258(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0x0,-0x240(%rbp)
jge    13ea <smack_have_access+0x24a>
mov    -0x244(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
mov    -0x244(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
mov    %eax,-0x240(%rbp)
mov    -0x244(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x240(%rbp)
jge    142b <smack_have_access+0x28b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1440 <smack_have_access+0x2a0>
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    $0x228,%rsp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbp
movl   $0x1,0x4(%rsp)
xor    %eax,%eax
call   1030 <init_smackfs_mnt@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11e9 <smack_have_access+0x49>
mov    %r14d,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   10b0 <get_label@plt>
mov    %eax,%ebx
xor    %edi,%edi
mov    %r13,%rsi
xor    %edx,%edx
call   10b0 <get_label@plt>
test   %ebx,%ebx
js     11d4 <smack_have_access+0x34>
mov    %eax,%r15d
test   %eax,%eax
js     11d4 <smack_have_access+0x34>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    0xde2(%rip),%rdi
lea    0xde3(%rip),%rsi
lea    0x4(%rsp),%rcx
call   1080 <open_smackfs_file@plt>
test   %eax,%eax
js     11d4 <smack_have_access+0x34>
cmpl   $0x0,0x4(%rsp)
jne    124d <smack_have_access+0xad>
cmp    $0x17,%ebx
ja     1302 <smack_have_access+0x162>
cmp    $0x18,%r15d
jae    1302 <smack_have_access+0x162>
mov    %eax,%ebx
mov    %r12,%rdi
call   10c0 <str_to_access_code@plt>
test   %eax,%eax
js     11d4 <smack_have_access+0x34>
lea    0x9(%rsp),%r15
mov    %eax,%edi
mov    %r15,%rsi
call   10a0 <access_code_to_str@plt>
cmpl   $0x0,0x4(%rsp)
lea    0xd9e(%rip),%rax
lea    0xd8e(%rip),%rdx
cmove  %rax,%rdx
lea    0x10(%rsp),%rdi
mov    $0x20e,%esi
mov    %rbp,%rcx
mov    %r13,%r8
mov    %r15,%r9
call   1060 <snprintf@plt>
cmp    $0x20e,%eax
jb     12b0 <smack_have_access+0x110>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    11d4 <smack_have_access+0x34>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
js     130e <smack_have_access+0x16e>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1090 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
test   %ebx,%ebx
js     11d4 <smack_have_access+0x34>
xor    %r14d,%r14d
cmpb   $0x31,0x10(%rsp)
sete   %r14b
jmp    11d4 <smack_have_access+0x34>
mov    %eax,%edi
call   1070 <close@plt>
jmp    11d4 <smack_have_access+0x34>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    11d4 <smack_have_access+0x34>

LLVM IR (-O0):
@.str = [8 x i8] c"access2\00"
@.str.1 = [7 x i8] c"access\00"
@O_RDWR = global i32 0
@.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 [526 x i8]
%9 = alloca [7 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 1, i32* %13
%16 = call i64 (...) @init_smackfs_mnt()
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %106
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = call i32 @get_label(i32* null, i8* %20, i32* null)
store i32 %21, i32* %14
%22 = load i8*, i8** %6
%23 = call i32 @get_label(i32* null, i8* %22, i32* null)
store i32 %23, i32* %15
%24 = load i32, i32* %14
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = load i32, i32* %15
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26, %19
store i32 -1, i32* %4
br label %106
30: ; preds = %26
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open_smackfs_file(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32* %13)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 -1, i32* %4
br label %106
36: ; preds = %30
%37 = load i32, i32* %13
%38 = icmp ne i32 %37, 0
br i1 %38, label %48, label %39
39: ; preds = %36
%40 = load i32, i32* %14
%41 = icmp sgt i32 %40, 23
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = load i32, i32* %15
%44 = icmp sgt i32 %43, 23
br i1 %44, label %45, label %48
45: ; preds = %42, %39
%46 = load i32, i32* %12
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %4
br label %106
48: ; preds = %42, %36
%49 = load i8*, i8** %7
%50 = call i32 @str_to_access_code(i8* %49)
store i32 %50, i32* %10
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 -1, i32* %4
br label %106
53: ; preds = %48
%54 = load i32, i32* %10
%55 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%56 = call i32 @access_code_to_str(i32 %54, i8* %55)
%57 = load i32, i32* %13
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %53
%60 = getelementptr inbounds [526 x i8], [526 x i8]* %8, i64 0, i64 0
%61 = load i8*, i8** %5
%62 = load i8*, i8** %6
%63 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%64 = call i32 @snprintf(i8* %60, i32 526, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %61, i8* %62, i8* %63)
store i32 %64, i32* %11
br label %71
65: ; preds = %53
%66 = getelementptr inbounds [526 x i8], [526 x i8]* %8, i64 0, i64 0
%67 = load i8*, i8** %5
%68 = load i8*, i8** %6
%69 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%70 = call i32 @snprintf(i8* %66, i32 526, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %67, i8* %68, i8* %69)
store i32 %70, i32* %11
br label %71
71: ; preds = %65, %59
%72 = load i32, i32* %11
%73 = icmp slt i32 %72, 0
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = load i32, i32* %11
%76 = icmp sge i32 %75, 526
br i1 %76, label %77, label %80
77: ; preds = %74, %71
%78 = load i32, i32* %12
%79 = call i32 @close(i32 %78)
store i32 -1, i32* %4
br label %106
80: ; preds = %74
%81 = load i32, i32* %12
%82 = getelementptr inbounds [526 x i8], [526 x i8]* %8, i64 0, i64 0
%83 = getelementptr inbounds [526 x i8], [526 x i8]* %8, i64 0, i64 0
%84 = call i32 @strlen(i8* %83)
%85 = call i32 @write(i32 %81, i8* %82, i32 %84)
store i32 %85, i32* %11
%86 = load i32, i32* %11
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %80
%89 = load i32, i32* %12
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %4
br label %106
91: ; preds = %80
%92 = load i32, i32* %12
%93 = getelementptr inbounds [526 x i8], [526 x i8]* %8, i64 0, i64 0
%94 = call i32 @read(i32 %92, i8* %93, i32 1)
store i32 %94, i32* %11
%95 = load i32, i32* %12
%96 = call i32 @close(i32 %95)
%97 = load i32, i32* %11
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %100
99: ; preds = %91
store i32 -1, i32* %4
br label %106
100: ; preds = %91
%101 = getelementptr inbounds [526 x i8], [526 x i8]* %8, i64 0, i64 0
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = icmp eq i32 %103, 49
%105 = zext i1 %104 to i32
store i32 %105, i32* %4
br label %106
106: ; preds = %100, %99, %88, %77, %52, %45, %35, %29, %18
%107 = load i32, i32* %4
ret i32 %107
}

LLVM IR (-O2):
@.str = [8 x i8] c"access2\00"
@.str.1 = [7 x i8] c"access\00"
@O_RDWR = global i32 0
@.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 [526 x i8]
%5 = alloca [7 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [526 x i8], [526 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%9 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%10 = tail call i64 (...) @init_smackfs_mnt()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %57
12: ; preds = %3
%13 = tail call i32 @get_label(i32* null, i8* %0, i32* null)
%14 = tail call i32 @get_label(i32* null, i8* %1, i32* null)
%15 = icmp slt i32 %13, 0
%16 = icmp slt i32 %14, 0
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %57, label %18
18: ; preds = %12
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open_smackfs_file(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32* %6)
%21 = icmp slt i32 %20, 0
br i1 %21, label %57, label %22
22: ; preds = %18
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = icmp ugt i32 %13, 23
%27 = icmp ugt i32 %14, 23
%28 = select i1 %26, i1 true, i1 %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @close(i32 %20)
br label %57
31: ; preds = %25, %22
%32 = call i32 @str_to_access_code(i8* %2)
%33 = icmp slt i32 %32, 0
br i1 %33, label %57, label %34
34: ; preds = %31
%35 = call i32 @access_code_to_str(i32 %32, i8* %8)
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 0
%38 = select i1 %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0)
%39 = call i32 @snprintf(i8* %7, i32 526, i8* %38, i8* %0, i8* %1, i8* %8)
%40 = icmp ugt i32 %39, 525
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @close(i32 %20)
br label %57
43: ; preds = %34
%44 = call i32 @strlen(i8* %7)
%45 = call i32 @write(i32 %20, i8* %7, i32 %44)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @close(i32 %20)
br label %57
49: ; preds = %43
%50 = call i32 @read(i32 %20, i8* %7, i32 1)
%51 = call i32 @close(i32 %20)
%52 = icmp slt i32 %50, 0
br i1 %52, label %57, label %53
53: ; preds = %49
%54 = load i8, i8* %7
%55 = icmp eq i8 %54, 49
%56 = zext i1 %55 to i32
br label %57
57: ; preds = %49, %31, %18, %12, %3, %53, %47, %41, %29
%58 = phi i32 [ -1, %41 ], [ -1, %47 ], [ %56, %53 ], [ -1, %29 ], [ -1, %3 ], [ -1, %12 ], [ -1, %18 ], [ -1, %31 ], [ -1, %49 ]
ret i32 %58
}