#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 *,
             char const *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int smack_change_rule(const char *subject, const char *object,
                      const char *allow, const char *deny) {
  char buf[(2 * (255 + 1) + 2 * 7 + 1) + 1];
  int ret;
  int fd;
  if (allow == NULL || strlen(allow) == 0)
    allow = "-";
  if (deny == NULL || strlen(deny) == 0)
    deny = "-";
  fd = open("/smack/"
            "change-rule",
            O_WRONLY);
  if (fd == -1)
    return -1;
  ret = snprintf(buf, (2 * (255 + 1) + 2 * 7 + 1) + 1, "%s %s %s %s", subject,
                 object, allow, deny);
  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_change_rule>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1188 <smack_change_rule+0x38>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    1193 <smack_change_rule+0x43>
lea    0xe71(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b0 <smack_change_rule+0x60>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    11bb <smack_change_rule+0x6b>
lea    0xe49(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x248(%rbp)
cmpl   $0xffffffff,-0x248(%rbp)
jne    11ef <smack_change_rule+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <smack_change_rule+0x164>
lea    -0x240(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    -0x28(%rbp),%rax
mov    $0x210,%esi
lea    0xe03(%rip),%rdx
mov    %rax,(%rsp)
call   1050 <snprintf@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jge    124f <smack_change_rule+0xff>
mov    0x2da3(%rip),%rax
mov    (%rax),%ecx
mov    0x2da2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x244(%rbp)
ff ff ff
jmp    12a0 <smack_change_rule+0x150>
mov    -0x248(%rbp),%eax
mov    %eax,-0x254(%rbp)
lea    -0x240(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x240(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%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,-0x244(%rbp)
mov    -0x248(%rbp),%edi
call   1060 <close@plt>
mov    -0x244(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_change_rule>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x220,%rsp
mov    %rcx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdx,%rdx
je     1183 <smack_change_rule+0x33>
mov    %rdx,%r12
mov    %rdx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     1183 <smack_change_rule+0x33>
test   %rbx,%rbx
jne    118f <smack_change_rule+0x3f>
jmp    119b <smack_change_rule+0x4b>
lea    0xe76(%rip),%r12
test   %rbx,%rbx
je     119b <smack_change_rule+0x4b>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jne    11a2 <smack_change_rule+0x52>
lea    0xe5e(%rip),%rbx
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe50(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1207 <smack_change_rule+0xb7>
mov    %eax,%ebp
mov    %rbx,(%rsp)
lea    0xe4c(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x210,%esi
mov    %r15,%rcx
mov    %r14,%r8
mov    %r12,%r9
call   1050 <snprintf@plt>
test   %eax,%eax
js     120e <smack_change_rule+0xbe>
lea    0x10(%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    1225 <smack_change_rule+0xd5>
mov    $0xffffffff,%ebx
jmp    122c <smack_change_rule+0xdc>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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