#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 ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EIO;
int errno;
int getxattr(char const *, char *, char *, int);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ setxattr(char const *, char *, char *, int, int /*<<< orphan*/);
int check_xattr_support_impl(const char *fname) {
  ssize_t res;
  char buf[(sizeof("my_xattr_val") - 1)] = {0};
  if (setxattr(fname, "user.my_xattr", "my_xattr_val",
               (sizeof("my_xattr_val") - 1), 0) < 0) {
    printf("setxattr got %d\n", errno);
    return -errno;
  }
  res = getxattr(fname, "user.my_xattr", buf, sizeof(buf));
  if (res < 0)
    return -errno;
  if (res != (sizeof("my_xattr_val") - 1))
    return -EIO;
  if (memcmp("my_xattr_val", buf, (sizeof("my_xattr_val") - 1)))
    return -EIO;
  return 0;
}


x86 assembly (clang, -O0):
<check_xattr_support_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
lea    0xe90(%rip),%rdx
mov    $0xc,%ecx
xor    %r8d,%r8d
call   1070 <setxattr@plt>
cmp    $0x0,%rax
jge    11bd <check_xattr_support_impl+0x6d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    0x2e25(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <check_xattr_support_impl+0x100>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0xc,%ecx
call   1060 <getxattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f6 <check_xattr_support_impl+0xa6>
xor    %eax,%eax
mov    0x2dec(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <check_xattr_support_impl+0x100>
movslq -0x14(%rbp),%rax
cmp    $0xc,%rax
je     1217 <check_xattr_support_impl+0xc7>
xor    %eax,%eax
mov    0x2dd3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <check_xattr_support_impl+0x100>
lea    -0x20(%rbp),%rsi
lea    0xdec(%rip),%rdi
mov    $0xc,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     1249 <check_xattr_support_impl+0xf9>
xor    %eax,%eax
mov    0x2da1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <check_xattr_support_impl+0x100>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_xattr_support_impl>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe9e(%rip),%rsi
lea    0xea5(%rip),%rdx
xor    %ebx,%ebx
mov    $0xc,%ecx
xor    %r8d,%r8d
call   1060 <setxattr@plt>
test   %rax,%rax
js     11c3 <check_xattr_support_impl+0x83>
lea    0xe7c(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    $0xc,%ecx
call   1050 <getxattr@plt>
test   %eax,%eax
js     11de <check_xattr_support_impl+0x9e>
cmp    $0xc,%eax
jne    11ba <check_xattr_support_impl+0x7a>
lea    0xe68(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11f3 <check_xattr_support_impl+0xb3>
mov    0x2e1f(%rip),%rax
jmp    11e5 <check_xattr_support_impl+0xa5>
mov    0x2e0e(%rip),%r14
mov    (%r14),%esi
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
sub    (%r14),%ebx
jmp    11e9 <check_xattr_support_impl+0xa9>
mov    0x2df3(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
jmp    11e9 <check_xattr_support_impl+0xa9>

LLVM IR (-O0):
@.str = [14 x i8] c"user.my_xattr\00"
@.str.1 = [13 x i8] c"my_xattr_val\00"
@.str.2 = [17 x i8] c"setxattr got %d\0A\00"
@errno = global i32 0
@EIO = global i32 0
define i32 @check_xattr_support_impl(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [12 x i8]
store i8* %0, i8** %3
%6 = bitcast [12 x i8]* %5 to i8*
%7 = load i8*, i8** %3
%8 = call i64 @setxattr(i8* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 12, i32 0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %39
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%18 = call i32 @getxattr(i8* %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %17, i32 12)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %2
br label %39
24: ; preds = %15
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = icmp ne i64 %26, 12
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @EIO
%30 = sub nsw i32 0, %29
store i32 %30, i32* %2
br label %39
31: ; preds = %24
%32 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%33 = call i64 @memcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 12)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @EIO
%37 = sub nsw i32 0, %36
store i32 %37, i32* %2
br label %39
38: ; preds = %31
store i32 0, i32* %2
br label %39
39: ; preds = %38, %35, %28, %21, %10
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [14 x i8] c"user.my_xattr\00"
@.str.1 = [13 x i8] c"my_xattr_val\00"
@.str.2 = [17 x i8] c"setxattr got %d\0A\00"
@errno = global i32 0
@EIO = global i32 0
define i32 @check_xattr_support_impl(i8* %0) {
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @setxattr(i8* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 12, i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
br label %28
11: ; preds = %1
%12 = call i32 @getxattr(i8* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %3, i32 12)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %28
17: ; preds = %11
%18 = icmp eq i32 %12, 12
br i1 %18, label %22, label %19
19: ; preds = %17
%20 = load i32, i32* @EIO
%21 = sub nsw i32 0, %20
br label %28
22: ; preds = %17
%23 = call i64 @memcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %3, i32 12)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* @EIO
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %22, %25, %19, %14, %6
%29 = phi i32 [ %10, %6 ], [ %16, %14 ], [ %21, %19 ], [ %27, %25 ], [ 0, %22 ]
ret i32 %29
}