#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 AT_FDCWD;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
int fchdir(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int symlink(char const *, char const *);
char *xgetcwd();
int symlinkat(const char *oldpath, int dirfd, const char *newpath) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "symlinkat");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  int result = symlink(oldpath, newpath);
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return result;
}


x86 assembly (clang, -O0):
<symlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <xgetcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b6 <symlinkat+0x46>
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%eax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
je     11ee <symlinkat+0x7e>
mov    -0xc(%rbp),%edi
call   1060 <fchdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e9 <symlinkat+0x79>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
jmp    11ee <symlinkat+0x7e>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <symlink@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%eax
je     1219 <symlinkat+0xa9>
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<symlinkat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
xor    %eax,%eax
call   1090 <xgetcwd@plt>
test   %rax,%rax
je     11e6 <symlinkat+0x76>
mov    %rax,%rbx
mov    0x2e42(%rip),%r12
cmp    %ebp,(%r12)
je     11a8 <symlinkat+0x38>
mov    %ebp,%edi
call   1060 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11d8 <symlinkat+0x68>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <symlink@plt>
mov    %eax,%r14d
cmp    %ebp,(%r12)
je     11c4 <symlinkat+0x54>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
jmp    11a8 <symlinkat+0x38>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [21 x i8] c"fd_wrapper_symlinkat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
define i32 @symlinkat(i8* %0, i32 %1, i8* %2) {
%4 = tail call i8* (...) @xgetcwd()
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = load i32, i32* @AT_FDCWD
%11 = icmp eq i32 %10, %1
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = tail call i32 @fchdir(i32 %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %12, %15, %9
%18 = tail call i32 @symlink(i8* %0, i8* %2)
%19 = load i32, i32* @AT_FDCWD
%20 = icmp eq i32 %19, %1
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @chdir(i8* %4)
br label %23
23: ; preds = %21, %17
%24 = tail call i32 @free(i8* %4)
ret i32 %18
}