#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ ERANGE;
char *calloc(scalar_t__, int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ getxattr(char const *, char const *, char *, scalar_t__);
scalar_t__ lgetxattr(char const *, char const *, char *, scalar_t__);
ssize_t smack_new_label_from_path(const char *path, const char *xattr,
                                  int follow, char **label) {
  char *result;
  ssize_t ret = 0;
  ret =
      follow ? getxattr(path, xattr, NULL, 0) : lgetxattr(path, xattr, NULL, 0);
  if (ret < 0 && errno != ERANGE)
    return -1;
  result = calloc(ret + 1, 1);
  if (result == NULL)
    return -1;
  ret = follow ? getxattr(path, xattr, result, ret)
               : lgetxattr(path, xattr, result, ret);
  if (ret < 0) {
    free(result);
    return -1;
  }
  *label = result;
  return 0;
}


x86 assembly (clang, -O0):
<smack_new_label_from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1186 <smack_new_label_from_path+0x46>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <getxattr@plt>
mov    %rax,-0x40(%rbp)
jmp    119e <smack_new_label_from_path+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <lgetxattr@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11d8 <smack_new_label_from_path+0x98>
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
je     11d8 <smack_new_label_from_path+0x98>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1283 <smack_new_label_from_path+0x143>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1206 <smack_new_label_from_path+0xc6>
movq   $0xffffffffffffffff,-0x8(%rbp)
jmp    1283 <smack_new_label_from_path+0x143>
cmpl   $0x0,-0x1c(%rbp)
je     122e <smack_new_label_from_path+0xee>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1060 <getxattr@plt>
mov    %rax,-0x48(%rbp)
jmp    1247 <smack_new_label_from_path+0x107>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1040 <lgetxattr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1270 <smack_new_label_from_path+0x130>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1283 <smack_new_label_from_path+0x143>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_new_label_from_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%r12
test   %edx,%edx
je     1166 <smack_new_label_from_path+0x26>
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <getxattr@plt>
jmp    116f <smack_new_label_from_path+0x2f>
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <lgetxattr@plt>
mov    %rax,%rbp
test   %rax,%rax
jns    1194 <smack_new_label_from_path+0x54>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
mov    $0xffffffffffffffff,%rbx
cmp    (%rcx),%rax
jne    11ed <smack_new_label_from_path+0xad>
mov    %r14,(%rsp)
lea    0x1(%rbp),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
je     11ed <smack_new_label_from_path+0xad>
mov    %rax,%r14
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %rbp,%rcx
test   %r13d,%r13d
je     11db <smack_new_label_from_path+0x9b>
call   1060 <getxattr@plt>
test   %rax,%rax
js     11e5 <smack_new_label_from_path+0xa5>
mov    (%rsp),%rax
mov    %r14,(%rax)
xor    %ebx,%ebx
jmp    11ed <smack_new_label_from_path+0xad>
call   1040 <lgetxattr@plt>
test   %rax,%rax
jns    11d0 <smack_new_label_from_path+0x90>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i64 @smack_new_label_from_path(i8* %0, i8* %1, i32 %2, i8** %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8** %3, i8*** %9
store i64 0, i64* %11
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = call i64 @getxattr(i8* %15, i8* %16, i8* null, i64 0)
br label %22
18: ; preds = %4
%19 = load i8*, i8** %6
%20 = load i8*, i8** %7
%21 = call i64 @lgetxattr(i8* %19, i8* %20, i8* null, i64 0)
br label %22
22: ; preds = %18, %14
%23 = phi i64 [ %17, %14 ], [ %21, %18 ]
store i64 %23, i64* %11
%24 = load i64, i64* %11
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @ERANGE
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
store i64 -1, i64* %5
br label %63
31: ; preds = %26, %22
%32 = load i64, i64* %11
%33 = add nsw i64 %32, 1
%34 = call i8* @calloc(i64 %33, i32 1)
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %31
store i64 -1, i64* %5
br label %63
38: ; preds = %31
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i8*, i8** %6
%43 = load i8*, i8** %7
%44 = load i8*, i8** %10
%45 = load i64, i64* %11
%46 = call i64 @getxattr(i8* %42, i8* %43, i8* %44, i64 %45)
br label %53
47: ; preds = %38
%48 = load i8*, i8** %6
%49 = load i8*, i8** %7
%50 = load i8*, i8** %10
%51 = load i64, i64* %11
%52 = call i64 @lgetxattr(i8* %48, i8* %49, i8* %50, i64 %51)
br label %53
53: ; preds = %47, %41
%54 = phi i64 [ %46, %41 ], [ %52, %47 ]
store i64 %54, i64* %11
%55 = load i64, i64* %11
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i8*, i8** %10
%59 = call i32 @free(i8* %58)
store i64 -1, i64* %5
br label %63
60: ; preds = %53
%61 = load i8*, i8** %10
%62 = load i8**, i8*** %9
store i8* %61, i8** %62
store i64 0, i64* %5
br label %63
63: ; preds = %60, %57, %37, %30
%64 = load i64, i64* %5
ret i64 %64
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i64 @smack_new_label_from_path(i8* %0, i8* %1, i32 %2, i8** nocapture writeonly %3) {
%5 = icmp eq i32 %2, 0
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i64 @getxattr(i8* %0, i8* %1, i8* null, i64 0)
br label %10
8: ; preds = %4
%9 = tail call i64 @lgetxattr(i8* %0, i8* %1, i8* null, i64 0)
br label %10
10: ; preds = %8, %6
%11 = phi i64 [ %7, %6 ], [ %9, %8 ]
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %13, %10
%18 = add nsw i64 %11, 1
%19 = tail call i8* @calloc(i64 %18, i32 1)
%20 = icmp eq i8* %19, null
br i1 %20, label %32, label %21
21: ; preds = %17
br i1 %5, label %24, label %22
22: ; preds = %21
%23 = tail call i64 @getxattr(i8* %0, i8* %1, i8* %19, i64 %11)
br label %26
24: ; preds = %21
%25 = tail call i64 @lgetxattr(i8* %0, i8* %1, i8* %19, i64 %11)
br label %26
26: ; preds = %24, %22
%27 = phi i64 [ %23, %22 ], [ %25, %24 ]
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @free(i8* %19)
br label %32
31: ; preds = %26
store i8* %19, i8** %3
br label %32
32: ; preds = %17, %13, %31, %29
%33 = phi i64 [ -1, %29 ], [ 0, %31 ], [ -1, %13 ], [ -1, %17 ]
ret i64 %33
}