#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 int /*<<< orphan*/ DIR;

/* Variables and functions */
int AT_FDCWD;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
int fchdir(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
char *xgetcwd();
DIR *fdopendir(int dirfd) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "fdopendir");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  DIR *dir;
  if (fchdir_err) {
    perror("fdopendir: ");
  }
  dir = opendir(".");
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return dir;
}


x86 assembly (clang, -O0):
<fdopendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1090 <xgetcwd@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ae <fdopendir+0x3e>
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%eax
je     11e6 <fdopendir+0x76>
mov    -0x4(%rbp),%edi
call   1060 <fchdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e1 <fdopendir+0x71>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
jmp    11e6 <fdopendir+0x76>
cmpl   $0x0,-0x8(%rbp)
je     11fc <fdopendir+0x8c>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
lea    0xe25(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
je     1227 <fdopendir+0xb7>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdopendir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <xgetcwd@plt>
test   %rax,%rax
je     11f0 <fdopendir+0x80>
mov    %rax,%r14
mov    0x2e49(%rip),%r15
cmp    %ebp,(%r15)
je     119f <fdopendir+0x2f>
mov    %ebp,%edi
call   1060 <fchdir@plt>
test   %eax,%eax
jne    11d1 <fdopendir+0x61>
lea    0xe82(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
cmp    %ebp,(%r15)
je     11bb <fdopendir+0x4b>
mov    %r14,%rdi
call   1050 <chdir@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0xffffffff,%eax
jne    11e2 <fdopendir+0x72>
lea    0xe38(%rip),%rdi
call   1070 <perror@plt>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
jmp    119f <fdopendir+0x2f>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

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