#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
int fchdir(int);
int open(char const *, int /*<<< orphan*/);
void dofchdir(const char *path, const char *errmsg) {
  int fd;
  fd = open(path, O_RDONLY);
  if (fd == -1)
    err(EXIT_FAILURE, "open %s", errmsg);
  if (fchdir(fd) == -1)
    err(EXIT_FAILURE, "fchdir %s", errmsg);
  close(fd);
}


x86 assembly (clang, -O0):
<dofchdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1188 <dofchdir+0x48>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7d(%rip),%rsi
call   1040 <err@plt>
mov    -0x14(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11b2 <dofchdir+0x72>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1040 <err@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dofchdir>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1173 <dofchdir+0x33>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
mov    %rbx,%rdx
call   1040 <err@plt>
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    1197 <dofchdir+0x57>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
mov    %rbx,%rdx
call   1040 <err@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <close@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [8 x i8] c"open %s\00"
@.str.1 = [10 x i8] c"fchdir %s\00"
define void @dofchdir(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @EXIT_FAILURE
%13 = load i8*, i8** %4
%14 = call i32 @err(i32 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %11, %2
%16 = load i32, i32* %5
%17 = call i32 @fchdir(i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @EXIT_FAILURE
%21 = load i8*, i8** %4
%22 = call i32 @err(i32 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %15
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [8 x i8] c"open %s\00"
@.str.1 = [10 x i8] c"fchdir %s\00"
define void @dofchdir(i8* nocapture %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @err(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %9
9: ; preds = %6, %2
%10 = tail call i32 @fchdir(i32 %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @err(i32 %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %15
15: ; preds = %12, %9
%16 = tail call i32 @close(i32 %4)
ret void
}