#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 char const *DIR;

/* Variables and functions */
int /*<<< orphan*/ assert(char const **const);
int dirfd(char const **const);
int fchdir(int);
bool xchdir(DIR *const d, const char **const err_msg) {
  assert(d);
  assert(err_msg);
  int fd = -1;
  if ((fd = dirfd(d)) == -1) {
    *err_msg = "Could not get directory file descriptor";
    return false;
  }
  if (fchdir(fd) == -1) {
    *err_msg = "Could not change into directory";
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<xchdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1040 <assert@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <dirfd@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1188 <xchdir+0x58>
mov    -0x18(%rbp),%rax
lea    0xe87(%rip),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11ba <xchdir+0x8a>
mov    -0x1c(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11b3 <xchdir+0x83>
mov    -0x18(%rbp),%rax
lea    0xe84(%rip),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11ba <xchdir+0x8a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xchdir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <assert@plt>
mov    %r14,%rdi
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <dirfd@plt>
cmp    $0xffffffff,%eax
je     1170 <xchdir+0x40>
mov    %eax,%edi
call   1050 <fchdir@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0xffffffff,%ecx
jne    117c <xchdir+0x4c>
lea    0xeba(%rip),%rax
jmp    1177 <xchdir+0x47>
lea    0xe89(%rip),%rax
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Could not get directory file descriptor\00"
@.str.1 = [32 x i8] c"Could not change into directory\00"
define i32 @xchdir(i8** %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca i32
store i8** %0, i8*** %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %4
%8 = call i32 @assert(i8** %7)
%9 = load i8**, i8*** %5
%10 = call i32 @assert(i8** %9)
store i32 -1, i32* %6
%11 = load i8**, i8*** %4
%12 = call i32 @dirfd(i8** %11)
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i8**, i8*** %5
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8** %15
store i32 0, i32* %3
br label %23
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @fchdir(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i8**, i8*** %5
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8** %21
store i32 0, i32* %3
br label %23
22: ; preds = %16
store i32 1, i32* %3
br label %23
23: ; preds = %22, %20, %14
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@.str = [40 x i8] c"Could not get directory file descriptor\00"
@.str.1 = [32 x i8] c"Could not change into directory\00"
define i32 @xchdir(i8** %0, i8** %1) {
%3 = tail call i32 @assert(i8** %0)
%4 = tail call i32 @assert(i8** %1)
%5 = tail call i32 @dirfd(i8** %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @fchdir(i32 %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7, %2
%11 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %7 ]
store i8* %11, i8** %1
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ 1, %7 ], [ 0, %10 ]
ret i32 %13
}