#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_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *strchr(char *, char);
int smack_self(char *smack) {
  int fd;
  int i;
  char *cp;
  fd = open("/proc/self/attr/smack.current", O_RDONLY);
  if (fd < 0)
    fd = open("/proc/self/attr/current", O_RDONLY);
  if (fd < 0)
    return -1;
  i = read(fd, smack, 256);
  if (i >= 0)
    smack[i] = '\0';
  if ((cp = strchr(smack, '\n')) != NULL)
    *cp = '\0';
  close(fd);
  return i;
}


x86 assembly (clang, -O0):
<smack_self>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1186 <smack_self+0x46>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119c <smack_self+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <smack_self+0xb7>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11c6 <smack_self+0x86>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11e9 <smack_self+0xa9>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_self>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e9a(%rip),%rbx
mov    (%rbx),%esi
lea    0xea9(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1176 <smack_self+0x36>
mov    (%rbx),%esi
lea    0xeb3(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11b5 <smack_self+0x75>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1192 <smack_self+0x52>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11a7 <smack_self+0x67>
movb   $0x0,(%rax)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%ebx
jmp    11ae <smack_self+0x6e>

LLVM IR (-O0):
@.str = [30 x i8] c"/proc/self/attr/smack.current\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"/proc/self/attr/current\00"
define i32 @smack_self(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %4
br label %14
14: ; preds = %11, %1
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 -1, i32* %2
br label %39
18: ; preds = %14
%19 = load i32, i32* %4
%20 = load i8*, i8** %3
%21 = call i32 @read(i32 %19, i8* %20, i32 256)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %24, %18
%30 = load i8*, i8** %3
%31 = call i8* @strchr(i8* %30, i8 signext 10)
store i8* %31, i8** %6
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i8*, i8** %6
store i8 0, i8* %34
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %5
store i32 %38, i32* %2
br label %39
39: ; preds = %35, %17
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [30 x i8] c"/proc/self/attr/smack.current\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"/proc/self/attr/current\00"
define i32 @smack_self(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %1, %5
%10 = phi i32 [ %7, %5 ], [ %3, %1 ]
%11 = tail call i32 @read(i32 %10, i8* %0, i32 256)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = zext i32 %11 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %13, %9
%17 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 10)
%18 = icmp eq i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16
store i8 0, i8* %17
br label %20
20: ; preds = %19, %16
%21 = tail call i32 @close(i32 %10)
br label %22
22: ; preds = %5, %20
%23 = phi i32 [ %11, %20 ], [ -1, %5 ]
ret i32 %23
}