int file_chdir(char *dir) {
  char *tmp = malloc(sizeof(char) * 4096);
  char *oldwd = getcwd(tmp, 4096);
  if (!oldwd)
    die(__func__);
  if (chdir(dir) == -1)
    die(__func__);
  free(oldwd);
  return 0;
}


x86 assembly (clang, -O0):
<file_chdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
mov    $0x0,%al
call   1050 <getcwd@plt>
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1199 <file_chdir+0x49>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11bb <file_chdir+0x6b>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_chdir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%rdi
mov    $0x1000,%esi
xor    %eax,%eax
call   1050 <getcwd@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1184 <file_chdir+0x34>
lea    0xe83(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11a1 <file_chdir+0x51>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
movslq %ebx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__func__.file_chdir = [11 x i8] c"file_chdir\00"
define i32 @file_chdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call i8* @malloc(i64 4096)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @getcwd to i32 (i8*, i32, ...)*)(i8* %6, i32 4096)
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__func__.file_chdir, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i8*, i8** %2
%16 = call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__func__.file_chdir, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i8*, i8** %4
ret i32 0
}

LLVM IR (-O2):
@__func__.file_chdir = [11 x i8] c"file_chdir\00"
define i32 @file_chdir(i8* %0) {
%2 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%3 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @getcwd to i32 (i8*, i32, ...)*)(i8* %2, i32 4096)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__func__.file_chdir, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = tail call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__func__.file_chdir, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = sext i32 %3 to i64
%14 = inttoptr i64 %13 to i8*
tail call void @free(i8* %14)
ret i32 0
}