#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 open(char const *, int);
int /*<<< orphan*/ perror(char *);
char *xgetcwd();
int openat(int dirfd, const char *pathname, int flags, ...) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "openat");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  int fd = open(pathname, flags);
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return fd;
}


x86 assembly (clang, -O0):
<openat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1090 <xgetcwd@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <openat+0x45>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%eax
je     11ed <openat+0x7d>
mov    -0x4(%rbp),%edi
call   1050 <fchdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e8 <openat+0x78>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
jmp    11ed <openat+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <openat+0xa7>
mov    -0x20(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <xgetcwd@plt>
test   %rax,%rax
je     11e6 <openat+0x76>
mov    %rax,%rbx
mov    0x2e42(%rip),%r12
cmp    %ebp,(%r12)
je     11a8 <openat+0x38>
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11d8 <openat+0x68>
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <open@plt>
mov    %eax,%r14d
cmp    %ebp,(%r12)
je     11c4 <openat+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    0xe33(%rip),%rdi
call   1070 <perror@plt>
jmp    11a8 <openat+0x38>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"fd_wrapper_openat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
define i32 @openat(i32 %0, i8* %1, i32 %2, ...) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %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 ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = load i32, i32* %4
%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* %4
%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** %5
%30 = load i32, i32* %6
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %4
%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 = [18 x i8] c"fd_wrapper_openat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
define i32 @openat(i32 %0, i8* nocapture %1, i32 %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 ([18 x i8], [18 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, %0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = tail call i32 @fchdir(i32 %0)
%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 @open(i8* %1, i32 %2)
%19 = load i32, i32* @AT_FDCWD
%20 = icmp eq i32 %19, %0
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
}