#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_WRONLY;
int /*<<< orphan*/ close(int);
int get_label(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int write(int, char const *, int);
int smack_set_label_for_self(const char *label) {
  int len;
  int fd;
  int ret;
  len = get_label(NULL, label, NULL);
  if (len < 0)
    return -1;
  fd = open("/proc/self/attr/current", O_WRONLY);
  if (fd < 0)
    return -1;
  ret = write(fd, label, len);
  close(fd);
  return (ret < 0) ? -1 : 0;
}


x86 assembly (clang, -O0):
<smack_set_label_for_self>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1060 <get_label@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1175 <smack_set_label_for_self+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <smack_set_label_for_self+0x90>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a3 <smack_set_label_for_self+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <smack_set_label_for_self+0x90>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_set_label_for_self>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <get_label@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1196 <smack_set_label_for_self+0x56>
mov    %eax,%r15d
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1196 <smack_set_label_for_self+0x56>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
sar    $0x1f,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/proc/self/attr/current\00"
@O_WRONLY = global i32 0
define i32 @smack_set_label_for_self(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @get_label(i32* null, i8* %7, i32* null)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %29
12: ; preds = %1
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %29
18: ; preds = %12
%19 = load i32, i32* %5
%20 = load i8*, i8** %3
%21 = load i32, i32* %4
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %5
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
%27 = zext i1 %26 to i64
%28 = select i1 %26, i32 -1, i32 0
store i32 %28, i32* %2
br label %29
29: ; preds = %18, %17, %11
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [24 x i8] c"/proc/self/attr/current\00"
@O_WRONLY = global i32 0
define i32 @smack_set_label_for_self(i8* %0) {
%2 = tail call i32 @get_label(i32* null, i8* %0, i32* null)
%3 = icmp slt i32 %2, 0
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = tail call i32 @write(i32 %6, i8* %0, i32 %2)
%10 = tail call i32 @close(i32 %6)
%11 = ashr i32 %9, 31
br label %12
12: ; preds = %4, %1, %8
%13 = phi i32 [ %11, %8 ], [ -1, %1 ], [ -1, %4 ]
ret i32 %13
}