#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strdup(char *);
int smack_get_process_label(pid_t pid, char **label) {
  char result[255 + 1];
  int fd, ret;
  char path[255];
  snprintf(path, 255, "/proc/%d/attr/current", pid);
  fd = open(path, O_RDONLY);
  if (fd == -1) {
    *label = NULL;
    return -1;
  }
  ret = read(fd, result, 255);
  close(fd);
  if (ret == -1) {
    *label = NULL;
    return -1;
  }
  result[ret] = '\0';
  *label = strdup(result);
  return 0;
}


x86 assembly (clang, -O0):
<smack_get_process_label>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe88(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    11bc <smack_get_process_label+0x6c>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <smack_get_process_label+0xe4>
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x118(%rbp)
mov    -0x114(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x118(%rbp)
jne    1208 <smack_get_process_label+0xb8>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <smack_get_process_label+0xe4>
movslq -0x118(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_get_process_label>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xe97(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1030 <snprintf@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11cf <smack_get_process_label+0x7f>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
je     11d1 <smack_get_process_label+0x81>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1070 <strdup@plt>
xor    %r15d,%r15d
jmp    11d1 <smack_get_process_label+0x81>
xor    %eax,%eax
mov    %rax,(%r14)
mov    %r15d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [22 x i8] c"/proc/%d/attr/current\00"
@O_RDONLY = global i32 0
define i32 @smack_get_process_label(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %19, label %11
11: ; preds = %2
%12 = call i32 @read(i32 %9, i8* %5, i32 255)
%13 = call i32 @close(i32 %9)
%14 = icmp eq i32 %12, -1
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = sext i32 %12 to i64
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i8* @strdup(i8* %5)
br label %19
19: ; preds = %11, %2, %15
%20 = phi i8* [ %18, %15 ], [ null, %2 ], [ null, %11 ]
%21 = phi i32 [ 0, %15 ], [ -1, %2 ], [ -1, %11 ]
store i8* %20, i8** %1
ret i32 %21
}