#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 strnlen(char const *, int);
int write(int, char const *, int);
int smack_set_self_label(const char *label) {
  int len, fd, ret;
  len = strnlen(label, 255 + 1);
  if (len > 255) {
    errno = EINVAL;
    return -1;
  }
  fd = open("/proc/self/attr/current", O_WRONLY);
  if (fd == -1)
    return -1;
  ret = write(fd, label, len);
  close(fd);
  if (ret == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<smack_set_self_label>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x100,%esi
call   1040 <strnlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xff,-0x14(%rbp)
jle    1188 <smack_set_self_label+0x48>
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    0x2e66(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <smack_set_self_label+0xad>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b6 <smack_set_self_label+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <smack_set_self_label+0xad>
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   1050 <close@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e6 <smack_set_self_label+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <smack_set_self_label+0xad>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_set_self_label>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x100,%esi
call   1040 <strnlen@plt>
cmp    $0x100,%eax
jl     116c <smack_set_self_label+0x2c>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    0x2e78(%rip),%rcx
mov    %eax,(%rcx)
jmp    11ad <smack_set_self_label+0x6d>
mov    %eax,%ebp
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11ad <smack_set_self_label+0x6d>
mov    %eax,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebx
sete   %al
neg    %eax
jmp    11b2 <smack_set_self_label+0x72>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [24 x i8] c"/proc/self/attr/current\00"
@O_WRONLY = global i32 0
define i32 @smack_set_self_label(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 @strnlen(i8* %7, i32 256)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 255
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i32 -1, i32* %2
br label %30
13: ; preds = %1
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %2
br label %30
19: ; preds = %13
%20 = load i32, i32* %5
%21 = load i8*, i8** %3
%22 = load i32, i32* %4
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %19
store i32 -1, i32* %2
br label %30
29: ; preds = %19
store i32 0, i32* %2
br label %30
30: ; preds = %29, %28, %18, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [24 x i8] c"/proc/self/attr/current\00"
@O_WRONLY = global i32 0
define i32 @smack_set_self_label(i8* %0) {
%2 = tail call i32 @strnlen(i8* %0, i32 256)
%3 = icmp sgt i32 %2, 255
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = load i32, i32* @EINVAL
store i32 %5, i32* @errno
br label %15
6: ; preds = %1
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %6
%11 = tail call i32 @write(i32 %8, i8* %0, i32 %2)
%12 = tail call i32 @close(i32 %8)
%13 = icmp eq i32 %11, -1
%14 = sext i1 %13 to i32
br label %15
15: ; preds = %10, %6, %4
%16 = phi i32 [ -1, %4 ], [ -1, %6 ], [ %14, %10 ]
ret i32 %16
}