#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 symlink(char const *, char const *);
int xsymlinkat(const char *oldpath, int newdirfd, const char *newpath) {
  int cfd, ret, error;
  cfd = open(".", O_RDONLY);
  if (cfd == -1)
    return (-1);
  if (fchdir(newdirfd) == -1) {
    error = errno;
    (void)close(cfd);
    errno = error;
    return (-1);
  }
  ret = symlink(oldpath, newpath);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return ret;
}


x86 assembly (clang, -O0):
<xsymlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1181 <xsymlinkat+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <xsymlinkat+0xbc>
mov    -0x14(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11be <xsymlinkat+0x7e>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2e28(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <xsymlinkat+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <symlink@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e03(%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    0x2de4(%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):
<xsymlinkat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1197 <xsymlinkat+0x57>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     119e <xsymlinkat+0x5e>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <symlink@plt>
mov    %eax,%ebp
mov    0x2e4d(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1050 <fchdir@plt>
jmp    11ad <xsymlinkat+0x6d>
mov    $0xffffffff,%ebp
jmp    11be <xsymlinkat+0x7e>
mov    0x2e33(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e1d(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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 @xsymlinkat(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %36
16: ; preds = %3
%17 = load i32, i32* %6
%18 = call i32 @fchdir(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* @errno
store i32 %21, i32* %10
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %10
store i32 %24, i32* @errno
store i32 -1, i32* %4
br label %36
25: ; preds = %16
%26 = load i8*, i8** %5
%27 = load i8*, i8** %7
%28 = call i32 @symlink(i8* %26, i8* %27)
store i32 %28, i32* %9
%29 = load i32, i32* @errno
store i32 %29, i32* %10
%30 = load i32, i32* %8
%31 = call i32 @fchdir(i32 %30)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %10
store i32 %34, i32* @errno
%35 = load i32, i32* %9
store i32 %35, i32* %4
br label %36
36: ; preds = %25, %20, %15
%37 = load i32, i32* %4
ret i32 %37
}

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