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

/* Variables and functions */
int ENOMEM;
int errno;
size_t fgetxattr(int, char const *, char *, size_t);
char *strdup(char *);
int fxgets(int fd, const char *xname, size_t buf_sz, char **x) {
  ssize_t res;
  char buf[buf_sz + 1];
  res = fgetxattr(fd, xname, buf, buf_sz);
  if (res < 0)
    return -errno;
  buf[res] = '\0';
  *x = strdup(buf);
  if (!*x)
    return -ENOMEM;
  return 0;
}


x86 assembly (clang, -O0):
<fxgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
add    $0x10,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
call   1030 <fgetxattr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    119b <fxgets+0x7b>
xor    %eax,%eax
mov    0x2e4e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11ec <fxgets+0xcc>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1040 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11de <fxgets+0xbe>
xor    %eax,%eax
mov    0x2e13(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11ec <fxgets+0xcc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fxgets>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rcx
mov    %rsp,%rbx
lea    0x10(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdx
call   1030 <fgetxattr@plt>
movb   $0x0,(%rax,%rbx,1)
mov    %rbx,%rdi
call   1040 <strdup@plt>
mov    %rax,(%r14)
mov    0x2e84(%rip),%rdx
xor    %ecx,%ecx
sub    (%rdx),%ecx
xor    %edx,%edx
test   %rax,%rax
cmovne %edx,%ecx
mov    %ecx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @fxgets(i32 %0, i8* %1, i64 %2, i8** nocapture writeonly %3) {
%5 = add i64 %2, 1
%6 = alloca i8, i64 %5
%7 = call i64 @fgetxattr(i32 %0, i8* %1, i8* %6, i64 %2)
%8 = getelementptr inbounds i8, i8* %6, i64 %7
store i8 0, i8* %8
%9 = call i8* @strdup(i8* %6)
store i8* %9, i8** %3
%10 = icmp eq i8* %9, null
%11 = load i32, i32* @ENOMEM
%12 = sub nsw i32 0, %11
%13 = select i1 %10, i32 %12, i32 0
ret i32 %13
}