#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_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int smack_map_label(pid_t pid, const char *label, const char *label_ns) {
  char path[255];
  char buf[(2 * 255 + 2)];
  int ret, fd;
  snprintf(path, 255,
           "/proc/%d/attr/"
           "label_map",
           pid);
  fd = open(path, O_WRONLY);
  if (fd == -1)
    return -1;
  snprintf(buf, (2 * 255 + 2), "%s %s", label, label_ns);
  ret = write(fd, buf, strlen(buf));
  close(fd);
  if (ret == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<smack_map_label>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe84(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x328(%rbp)
cmpl   $0xffffffff,-0x328(%rbp)
jne    11b7 <smack_map_label+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <smack_map_label+0xf4>
lea    -0x320(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x200,%esi
lea    0xe46(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x328(%rbp),%eax
mov    %eax,-0x334(%rbp)
lea    -0x320(%rbp),%rax
mov    %rax,-0x330(%rbp)
lea    -0x320(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x334(%rbp),%edi
mov    -0x330(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x324(%rbp)
mov    -0x328(%rbp),%edi
call   1060 <close@plt>
cmpl   $0xffffffff,-0x324(%rbp)
jne    123d <smack_map_label+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <smack_map_label+0xf4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_map_label>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x308,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ecx
lea    0xe94(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e3 <smack_map_label+0x93>
mov    %eax,%ebp
lea    0xe7b(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebx
sete   %al
neg    %eax
jmp    11e8 <smack_map_label+0x98>
mov    $0xffffffff,%eax
add    $0x308,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [24 x i8] c"/proc/%d/attr/label_map\00"
@O_WRONLY = global i32 0
@.str.1 = [6 x i8] c"%s %s\00"
define i32 @smack_map_label(i32 %0, i8* %1, i8* %2) {
%4 = alloca [255 x i8]
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %19, label %12
12: ; preds = %3
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %2)
%14 = call i32 @strlen(i8* %7)
%15 = call i32 @write(i32 %10, i8* %7, i32 %14)
%16 = call i32 @close(i32 %10)
%17 = icmp eq i32 %15, -1
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %12, %3
%20 = phi i32 [ -1, %3 ], [ %18, %12 ]
ret i32 %20
}