#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 */
int /*<<< orphan*/ O_RDWR;
unsigned int access_type_to_int(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ int_to_access_type_k(unsigned int, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int snprintf(char *, int, char *, char const *, char const *, char *);
int write(int, char *, int);
int smack_have_access(const char *subject, const char *object,
                      const char *access_type) {
  char buf[(2 * (23 + 1) + 5) + 1];
  char access_type_k[5 + 1];
  unsigned access_code;
  int ret;
  int fd;
  access_code = access_type_to_int(access_type);
  int_to_access_type_k(access_code, access_type_k);
  ret = snprintf(buf, (2 * (23 + 1) + 5) + 1, "%-23s %-23s %5s", subject,
                 object, access_type_k);
  if (ret < 0)
    return -1;
  fd = open("/smack"
            "/access",
            O_RDWR);
  if (fd < 0)
    return -1;
  ret = write(fd, buf, (2 * (23 + 1) + 5));
  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    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <access_type_to_int@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x6c(%rbp),%edi
lea    -0x66(%rbp),%rsi
call   1070 <int_to_access_type_k@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x66(%rbp),%r9
mov    $0x36,%esi
lea    0xe45(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jge    11d9 <smack_have_access+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <smack_have_access+0x10d>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    1207 <smack_have_access+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <smack_have_access+0x10d>
mov    -0x74(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x35,%edx
call   1030 <write@plt>
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jge    1239 <smack_have_access+0xc9>
mov    -0x74(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <smack_have_access+0x10d>
mov    -0x74(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x70(%rbp)
mov    -0x74(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x70(%rbp)
jge    126b <smack_have_access+0xfb>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <smack_have_access+0x10d>
movsbl -0x60(%rbp),%eax
cmp    $0x31,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_have_access>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1080 <access_type_to_int@plt>
lea    0xa(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <int_to_access_type_k@plt>
lea    0xe64(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x36,%esi
mov    %rbp,%rcx
mov    %rbx,%r8
mov    %r14,%r9
call   1040 <snprintf@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1220 <smack_have_access+0xb0>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1220 <smack_have_access+0xb0>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x35,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1219 <smack_have_access+0xa9>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <close@plt>
test   %r14d,%r14d
js     1220 <smack_have_access+0xb0>
xor    %ebx,%ebx
cmpb   $0x31,0x10(%rsp)
sete   %bl
jmp    1220 <smack_have_access+0xb0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [16 x i8] c"%-23s %-23s %5s\00"
@.str.1 = [14 x i8] c"/smack/access\00"
@O_RDWR = global i32 0
define i32 @smack_have_access(i8* %0, i8* %1, i8* %2) {
%4 = alloca [54 x i8]
%5 = alloca [6 x i8]
%6 = getelementptr inbounds [54 x i8], [54 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%8 = tail call i32 @access_type_to_int(i8* %2)
%9 = call i32 @int_to_access_type_k(i32 %8, i8* %7)
%10 = call i32 @snprintf(i8* %6, i32 54, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %7)
%11 = icmp slt i32 %10, 0
br i1 %11, label %29, label %12
12: ; preds = %3
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %29, label %16
16: ; preds = %12
%17 = call i32 @write(i32 %14, i8* %6, i32 53)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @close(i32 %14)
br label %29
21: ; preds = %16
%22 = call i32 @read(i32 %14, i8* %6, i32 1)
%23 = call i32 @close(i32 %14)
%24 = icmp slt i32 %22, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i8, i8* %6
%27 = icmp eq i8 %26, 49
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %21, %12, %3, %25, %19
%30 = phi i32 [ -1, %19 ], [ %28, %25 ], [ -1, %3 ], [ -1, %12 ], [ -1, %21 ]
ret i32 %30
}