#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*/ EINVAL;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, char const *, char const *, char const *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int smack_set_rule_ex(const char *subject, const char *object,
                      const char *access, int for_self) {
  char buf[(2 * (255 + 1) + 7) + 1];
  int ret;
  int fd;
  int load2 = 1;
  if (for_self)
    fd = open("/smack/"
              "load-self2",
              O_WRONLY);
  else
    fd = open("/smack/"
              "load2",
              O_WRONLY);
  if (fd < 0) {
    if (for_self)
      fd = open("/smack/"
                "load-self",
                O_WRONLY);
    else
      fd = open("/smack/"
                "load",
                O_WRONLY);
    if (fd < 0)
      return -1;
    load2 = 0;
  }
  if (load2)
    ret = snprintf(buf, (2 * (255 + 1) + 7) + 1, "%s %s %s", subject, object,
                   access);
  else
    ret = snprintf(buf, (2 * (255 + 1) + 7) + 1, "%-23s %-23s %5.5s", subject,
                   object, access);
  if (ret < 0) {
    errno = EINVAL;
    ret = -1;
  } else
    ret = (write(fd, buf, strlen(buf)) >= 0) ? 0 : -1;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<smack_set_rule_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x1,-0x23c(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     119e <smack_set_rule_ex+0x4e>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x238(%rbp)
jmp    11b9 <smack_set_rule_ex+0x69>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0x0,-0x238(%rbp)
jge    122e <smack_set_rule_ex+0xde>
cmpl   $0x0,-0x24(%rbp)
je     11f0 <smack_set_rule_ex+0xa0>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x238(%rbp)
jmp    120b <smack_set_rule_ex+0xbb>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0x0,-0x238(%rbp)
jge    1224 <smack_set_rule_ex+0xd4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <smack_set_rule_ex+0x1d7>
movl   $0x0,-0x23c(%rbp)
cmpl   $0x0,-0x23c(%rbp)
je     126a <smack_set_rule_ex+0x11a>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x208,%esi
lea    0xde2(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x234(%rbp)
jmp    1294 <smack_set_rule_ex+0x144>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x208,%esi
lea    0xdbc(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jge    12c2 <smack_set_rule_ex+0x172>
mov    0x2d30(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x234(%rbp)
ff ff ff
jmp    1313 <smack_set_rule_ex+0x1c3>
mov    -0x238(%rbp),%eax
mov    %eax,-0x24c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24c(%rbp),%edi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovge %ecx,%eax
mov    %eax,-0x234(%rbp)
mov    -0x238(%rbp),%edi
call   1060 <close@plt>
mov    -0x234(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_set_rule_ex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x210,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e59(%rip),%rbx
mov    (%rbx),%esi
test   %ecx,%ecx
je     118e <smack_set_rule_ex+0x3e>
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    119e <smack_set_rule_ex+0x4e>
lea    0xe93(%rip),%rdi
jmp    11b0 <smack_set_rule_ex+0x60>
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11a9 <smack_set_rule_ex+0x59>
mov    %eax,%ebp
lea    0xe95(%rip),%rdx
jmp    11c4 <smack_set_rule_ex+0x74>
lea    0xe80(%rip),%rdi
mov    (%rbx),%esi
call   1070 <open@plt>
test   %eax,%eax
js     122e <smack_set_rule_ex+0xde>
mov    %eax,%ebp
lea    0xe81(%rip),%rdx
mov    %rsp,%rdi
mov    $0x208,%esi
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14,%r9
call   1050 <snprintf@plt>
test   %eax,%eax
js     11fe <smack_set_rule_ex+0xae>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
jmp    1215 <smack_set_rule_ex+0xc5>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    0x2dd2(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
jmp    121c <smack_set_rule_ex+0xcc>

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

LLVM IR (-O2):
@.str = [18 x i8] c"/smack/load-self2\00"
@O_WRONLY = global i32 0
@.str.1 = [13 x i8] c"/smack/load2\00"
@.str.2 = [17 x i8] c"/smack/load-self\00"
@.str.3 = [12 x i8] c"/smack/load\00"
@.str.4 = [9 x i8] c"%s %s %s\00"
@.str.5 = [18 x i8] c"%-23s %-23s %5.5s\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @smack_set_rule_ex(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [520 x i8]
%6 = getelementptr inbounds [520 x i8], [520 x i8]* %5, i64 0, i64 0
%7 = icmp eq i32 %3, 0
%8 = load i32, i32* @O_WRONLY
br i1 %7, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %20
12: ; preds = %4
%13 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %9, %12
%16 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %12 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %9 ]
%17 = load i32, i32* @O_WRONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %15, %12, %9
%21 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), %9 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), %12 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), %15 ]
%22 = phi i32 [ %10, %9 ], [ %13, %12 ], [ %18, %15 ]
%23 = call i32 @snprintf(i8* %6, i32 520, i8* %21, i8* %0, i8* %1, i8* %2)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i32, i32* @EINVAL
store i32 %26, i32* @errno
br label %32
27: ; preds = %20
%28 = call i32 @strlen(i8* %6)
%29 = call i64 @write(i32 %22, i8* %6, i32 %28)
%30 = ashr i64 %29, 63
%31 = trunc i64 %30 to i32
br label %32
32: ; preds = %27, %25
%33 = phi i32 [ -1, %25 ], [ %31, %27 ]
%34 = call i32 @close(i32 %22)
br label %35
35: ; preds = %15, %32
%36 = phi i32 [ %33, %32 ], [ -1, %15 ]
ret i32 %36
}