#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;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ pread(int, char *, int, int /*<<< orphan*/);
int pwrite(int, char *, int, int /*<<< orphan*/);
int smaunch_fs_context_apply();
int /*<<< orphan*/ smaunch_has_prepared();
int /*<<< orphan*/ smaunch_is_ready();
int smaunch_smack_context_apply();
int /*<<< orphan*/ smaunch_smack_set_subject(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int smaunch_apply() {
  int result, length, file;
  char slabel[256];
  assert(smaunch_is_ready());
  assert(smaunch_has_prepared());
  file = open("/proc/self/attr/current", O_RDWR);
  if (file < 0)
    return -errno;
  length = (int)pread(file, slabel, sizeof slabel - 1, 0);
  if (length < 0) {
    result = -errno;
    close(file);
    return result;
  }
  if (length == 0) {
    strncpy(slabel, "User", 5);
    length = 4;
  } else {
    slabel[length] = 0;
  }
  result = pwrite(file, "_", 1, 0);
  if (result < 0) {
    result = -errno;
    close(file);
    return result;
  }
  smaunch_smack_set_subject(slabel);
  result = smaunch_smack_context_apply();
  if (!result) {
    result = smaunch_fs_context_apply();
  }
  length = pwrite(file, slabel, length, 0);
  if (length < 0 && !result)
    result = -errno;
  close(file);
  return result;
}


x86 assembly (clang, -O0):
<smaunch_apply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    $0x0,%al
call   1030 <smaunch_is_ready@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    $0x0,%al
call   1050 <smaunch_has_prepared@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0xe19(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    120c <smaunch_apply+0x5c>
xor    %eax,%eax
mov    0x2dd6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    133f <smaunch_apply+0x18f>
mov    -0x10(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1250 <smaunch_apply+0xa0>
xor    %eax,%eax
mov    0x2da0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133f <smaunch_apply+0x18f>
cmpl   $0x0,-0xc(%rbp)
jne    127e <smaunch_apply+0xce>
lea    -0x110(%rbp),%rdi
lea    0xdb0(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncpy@plt>
movl   $0x4,-0xc(%rbp)
jmp    128a <smaunch_apply+0xda>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x10(%rbp),%edi
lea    0xd89(%rip),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   10b0 <pwrite@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12ce <smaunch_apply+0x11e>
xor    %eax,%eax
mov    0x2d22(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133f <smaunch_apply+0x18f>
lea    -0x110(%rbp),%rdi
call   10a0 <smaunch_smack_set_subject@plt>
mov    $0x0,%al
call   1060 <smaunch_smack_context_apply@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    12f8 <smaunch_apply+0x148>
mov    $0x0,%al
call   1090 <smaunch_fs_context_apply@plt>
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0xc(%rbp),%edx
xor    %ecx,%ecx
call   10b0 <pwrite@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1331 <smaunch_apply+0x181>
cmpl   $0x0,-0x8(%rbp)
jne    1331 <smaunch_apply+0x181>
xor    %eax,%eax
mov    0x2cac(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smaunch_apply>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <smaunch_is_ready@plt>
mov    %eax,%edi
call   1080 <assert@plt>
xor    %eax,%eax
call   1050 <smaunch_has_prepared@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe15(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     1220 <smaunch_apply+0x70>
mov    %eax,%ebp
mov    %rsp,%rsi
xor    %ebx,%ebx
mov    %eax,%edi
mov    $0xff,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
mov    %rax,%r14
test   %r14d,%r14d
js     12ac <smaunch_apply+0xfc>
je     122e <smaunch_apply+0x7e>
movslq %r14d,%rax
movb   $0x0,(%rsp,%rax,1)
jmp    1248 <smaunch_apply+0x98>
mov    0x2db1(%rip),%rax
sub    (%rax),%ebx
jmp    12bc <smaunch_apply+0x10c>
lea    0xde3(%rip),%rsi
mov    %rsp,%rdi
mov    $0x5,%edx
call   1040 <strncpy@plt>
mov    $0x4,%r14d
lea    0xdce(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   10b0 <pwrite@plt>
test   %eax,%eax
js     12ac <smaunch_apply+0xfc>
mov    %rsp,%rdi
call   10a0 <smaunch_smack_set_subject@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <smaunch_smack_context_apply@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    1285 <smaunch_apply+0xd5>
xor    %eax,%eax
call   1090 <smaunch_fs_context_apply@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %r14d,%edx
xor    %ecx,%ecx
call   10b0 <pwrite@plt>
mov    0x2d3d(%rip),%rcx
sub    (%rcx),%ebx
test   %r15d,%r15d
cmovne %r15d,%ebx
test   %eax,%eax
cmovns %r15d,%ebx
jmp    12b5 <smaunch_apply+0x105>
mov    0x2d25(%rip),%rax
sub    (%rax),%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/proc/self/attr/current\00"
@O_RDWR = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"User\00"
@.str.2 = [2 x i8] c"_\00"
define i32 @smaunch_apply() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = call i32 (...) @smaunch_is_ready()
%7 = call i32 @assert(i32 %6)
%8 = call i32 (...) @smaunch_has_prepared()
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %1
br label %76
17: ; preds = %0
%18 = load i32, i32* %4
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = call i64 @pread(i32 %18, i8* %19, i32 255, i32 0)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
store i32 %26, i32* %2
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %2
store i32 %29, i32* %1
br label %76
30: ; preds = %17
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = call i32 @strncpy(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i32 4, i32* %3
br label %40
36: ; preds = %30
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %36, %33
%41 = load i32, i32* %4
%42 = call i32 @pwrite(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 0)
store i32 %42, i32* %2
%43 = load i32, i32* %2
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load i32, i32* @errno
%47 = sub nsw i32 0, %46
store i32 %47, i32* %2
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %2
store i32 %50, i32* %1
br label %76
51: ; preds = %40
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%53 = call i32 @smaunch_smack_set_subject(i8* %52)
%54 = call i32 (...) @smaunch_smack_context_apply()
store i32 %54, i32* %2
%55 = load i32, i32* %2
%56 = icmp ne i32 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %51
%58 = call i32 (...) @smaunch_fs_context_apply()
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %51
%60 = load i32, i32* %4
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%62 = load i32, i32* %3
%63 = call i32 @pwrite(i32 %60, i8* %61, i32 %62, i32 0)
store i32 %63, i32* %3
%64 = load i32, i32* %3
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %59
%67 = load i32, i32* %2
%68 = icmp ne i32 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %66
%70 = load i32, i32* @errno
%71 = sub nsw i32 0, %70
store i32 %71, i32* %2
br label %72
72: ; preds = %69, %66, %59
%73 = load i32, i32* %4
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %2
store i32 %75, i32* %1
br label %76
76: ; preds = %72, %45, %24, %14
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
@.str = [24 x i8] c"/proc/self/attr/current\00"
@O_RDWR = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"User\00"
@.str.2 = [2 x i8] c"_\00"
define i32 @smaunch_apply() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @smaunch_is_ready()
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 (...) @smaunch_has_prepared()
%6 = tail call i32 @assert(i32 %5)
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %53
13: ; preds = %0
%14 = call i64 @pread(i32 %8, i8* %2, i32 255, i32 0)
%15 = trunc i64 %14 to i32
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
%20 = tail call i32 @close(i32 %8)
br label %53
21: ; preds = %13
%22 = icmp eq i32 %15, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %29
25: ; preds = %21
%26 = shl i64 %14, 32
%27 = ashr exact i64 %26, 32
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %25, %23
%30 = phi i32 [ 4, %23 ], [ %15, %25 ]
%31 = call i32 @pwrite(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
%36 = call i32 @close(i32 %8)
br label %53
37: ; preds = %29
%38 = call i32 @smaunch_smack_set_subject(i8* %2)
%39 = call i32 (...) @smaunch_smack_context_apply()
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (...) @smaunch_fs_context_apply()
br label %43
43: ; preds = %41, %37
%44 = phi i32 [ %39, %37 ], [ %42, %41 ]
%45 = call i32 @pwrite(i32 %8, i8* %2, i32 %30, i32 0)
%46 = icmp sgt i32 %45, -1
%47 = icmp ne i32 %44, 0
%48 = select i1 %46, i1 true, i1 %47
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
%51 = select i1 %48, i32 %44, i32 %50
%52 = call i32 @close(i32 %8)
br label %53
53: ; preds = %43, %33, %17, %10
%54 = phi i32 [ %12, %10 ], [ %19, %17 ], [ %35, %33 ], [ %51, %43 ]
ret i32 %54
}