#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int open(char *, int /*<<< orphan*/);
int readlink(char const *, char *, size_t);
int xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
  int cfd, ret, error;
  cfd = open(".", O_RDONLY);
  if (cfd == -1)
    return (-1);
  if (fchdir(dirfd) == -1) {
    error = errno;
    (void)close(cfd);
    errno = error;
    return (-1);
  }
  ret = readlink(pathname, buf, bufsiz);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return ret;
}


x86 assembly (clang, -O0):
<xreadlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1185 <xreadlinkat+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <xreadlinkat+0xc4>
mov    -0x8(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11c2 <xreadlinkat+0x82>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2e24(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <xreadlinkat+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <readlink@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <fchdir@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2ddc(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xreadlinkat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     119e <xreadlinkat+0x5e>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11a5 <xreadlinkat+0x65>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <readlink@plt>
mov    %eax,%ebp
mov    0x2e46(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1050 <fchdir@plt>
jmp    11b4 <xreadlinkat+0x74>
mov    $0xffffffff,%ebp
jmp    11c5 <xreadlinkat+0x85>
mov    0x2e2c(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e16(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @xreadlinkat(i32 %0, i8* nocapture %1, i8* nocapture %2, i64 %3) {
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %21, label %8
8: ; preds = %4
%9 = tail call i32 @fchdir(i32 %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32, i32* @errno
br label %17
13: ; preds = %8
%14 = tail call i32 @readlink(i8* %1, i8* %2, i64 %3)
%15 = load i32, i32* @errno
%16 = tail call i32 @fchdir(i32 %6)
br label %17
17: ; preds = %11, %13
%18 = phi i32 [ %15, %13 ], [ %12, %11 ]
%19 = phi i32 [ %14, %13 ], [ -1, %11 ]
%20 = tail call i32 @close(i32 %6)
store i32 %18, i32* @errno
br label %21
21: ; preds = %17, %4
%22 = phi i32 [ -1, %4 ], [ %19, %17 ]
ret i32 %22
}