#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 /*<<< orphan*/ assert(int /*<<< orphan*/);
int dirfd(int /*<<< orphan*/ *const);
int fchdir(int);
bool xchdir(DIR *const d, const char **const err_msg) {
  assert(d != NULL);
  assert(err_msg != NULL);
  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)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <dirfd@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    119a <xchdir+0x6a>
mov    -0x18(%rbp),%rax
lea    0xe75(%rip),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11cc <xchdir+0x9c>
mov    -0x1c(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11c5 <xchdir+0x95>
mov    -0x18(%rbp),%rax
lea    0xe72(%rip),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11cc <xchdir+0x9c>
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
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <dirfd@plt>
cmp    $0xffffffff,%eax
je     117f <xchdir+0x4f>
mov    %eax,%edi
call   1050 <fchdir@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0xffffffff,%ecx
jne    118b <xchdir+0x5b>
lea    0xeab(%rip),%rax
jmp    1186 <xchdir+0x56>
lea    0xe7a(%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(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8**, i8*** %5
%12 = icmp ne i8** %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
store i32 -1, i32* %6
%15 = load i32*, i32** %4
%16 = call i32 @dirfd(i32* %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i8**, i8*** %5
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8** %19
store i32 0, i32* %3
br label %27
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i32 @fchdir(i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i8**, i8*** %5
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8** %25
store i32 0, i32* %3
br label %27
26: ; preds = %20
store i32 1, i32* %3
br label %27
27: ; preds = %26, %24, %18
%28 = load i32, i32* %3
ret i32 %28
}

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(i32* %0, i8** writeonly %1) {
%3 = icmp ne i32* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8** %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @dirfd(i32* %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = tail call i32 @fchdir(i32 %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11, %2
%15 = 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), %11 ]
store i8* %15, i8** %1
br label %16
16: ; preds = %14, %11
%17 = phi i32 [ 1, %11 ], [ 0, %14 ]
ret i32 %17
}