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

/* Variables and functions */
int EINVAL;
int errno;
size_t fgetxattr(int, char const *, char *, int);
int str_to_int(char *, char *, int);
int fxgeti(int fd, const char *xname, int *x) {
  ssize_t res;
  int ret;
  char err[1] = {0}, buf[32];
  res = fgetxattr(fd, xname, buf, sizeof(buf) - 1);
  if (res < 0)
    return -errno;
  buf[res] = '\0';
  ret = str_to_int(buf, err, sizeof(err));
  if (err[0])
    return -EINVAL;
  *x = ret;
  return 0;
}


x86 assembly (clang, -O0):
<fxgeti>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x25(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x1f,%ecx
call   1050 <fgetxattr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    118a <fxgeti+0x5a>
xor    %eax,%eax
mov    0x2e60(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <fxgeti+0xa5>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1030 <str_to_int@plt>
mov    %eax,-0x24(%rbp)
cmpb   $0x0,-0x25(%rbp)
je     11c5 <fxgeti+0x95>
xor    %eax,%eax
mov    0x2e1d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <fxgeti+0xa5>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fxgeti>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
movb   $0x0,0xf(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdx
mov    $0x1f,%ecx
call   1040 <fgetxattr@plt>
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xf(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1030 <str_to_int@plt>
cmpb   $0x0,0xf(%rsp)
je     116c <fxgeti+0x4c>
mov    0x2e7a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1171 <fxgeti+0x51>
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @fxgeti(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [1 x i8]
%11 = alloca [32 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%12 = bitcast [1 x i8]* %10 to i8*
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%16 = call i64 @fgetxattr(i32 %13, i8* %14, i8* %15, i32 31)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %4
br label %37
22: ; preds = %3
%23 = load i64, i64* %8
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%26 = getelementptr inbounds [1 x i8], [1 x i8]* %10, i64 0, i64 0
%27 = call i32 @str_to_int(i8* %25, i8* %26, i32 1)
store i32 %27, i32* %9
%28 = getelementptr inbounds [1 x i8], [1 x i8]* %10, i64 0, i64 0
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i32, i32* @EINVAL
%33 = sub nsw i32 0, %32
store i32 %33, i32* %4
br label %37
34: ; preds = %22
%35 = load i32, i32* %9
%36 = load i32*, i32** %7
store i32 %35, i32* %36
store i32 0, i32* %4
br label %37
37: ; preds = %34, %31, %19
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @fxgeti(i32 %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [1 x i8]
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
store i8 0, i8* %6
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%8 = call i64 @fgetxattr(i32 %0, i8* %1, i8* %7, i32 31)
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %8
store i8 0, i8* %9
%10 = call i32 @str_to_int(i8* %7, i8* %6, i32 1)
%11 = load i8, i8* %6
%12 = icmp eq i8 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %17
16: ; preds = %3
store i32 %10, i32* %2
br label %17
17: ; preds = %16, %13
%18 = phi i32 [ %15, %13 ], [ 0, %16 ]
ret i32 %18
}