#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 */
scalar_t__ chdir(char const *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int builtin_cd(char *const *argv) {
  const char *dest;
  if (*(argv + 1) == NULL) {
    if (getenv("HOME") == NULL) {
      fprintf(stderr, "Could not determine home directory\n");
      return -1;
    }
    dest = getenv("HOME");
  } else {
    dest = *(argv + 1);
  }
  if (*(argv + 2) != NULL) {
    fprintf(stderr, "Invalid arguments to cd\n");
    return -1;
  }
  if (chdir(dest) != 0) {
    perror("chdir");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<builtin_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11a7 <builtin_cd+0x67>
lea    0xe9e(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
jne    1192 <builtin_cd+0x52>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <builtin_cd+0xda>
lea    0xe67(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
jmp    11b3 <builtin_cd+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     11e3 <builtin_cd+0xa3>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <builtin_cd+0xda>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1213 <builtin_cd+0xd3>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121a <builtin_cd+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_cd>:
push   %rbx
mov    %rdi,%rbx
mov    0x8(%rdi),%rax
test   %rax,%rax
jne    115e <builtin_cd+0x1e>
lea    0xeac(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     118b <builtin_cd+0x4b>
cmpq   $0x0,0x10(%rbx)
je     1177 <builtin_cd+0x37>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xeb4(%rip),%rsi
jmp    119b <builtin_cd+0x5b>
mov    %rax,%rdi
call   1040 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11a7 <builtin_cd+0x67>
pop    %rbx
ret
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
lea    0xe94(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Could not determine home directory\0A\00"
@.str.2 = [25 x i8] c"Invalid arguments to cd\0A\00"
@.str.3 = [6 x i8] c"chdir\00"
@errno = global i32 0
define i32 @builtin_cd(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i8*
store i8** %0, i8*** %3
%5 = load i8**, i8*** %3
%6 = getelementptr inbounds i8*, i8** %5, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
15: ; preds = %9
%16 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %4
br label %21
17: ; preds = %1
%18 = load i8**, i8*** %3
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %4
br label %21
21: ; preds = %17, %15
%22 = load i8**, i8*** %3
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
29: ; preds = %21
%30 = load i8*, i8** %4
%31 = call i64 @chdir(i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @errno
store i32 %35, i32* %2
br label %37
36: ; preds = %29
store i32 0, i32* %2
br label %37
37: ; preds = %36, %33, %26, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Could not determine home directory\0A\00"
@.str.2 = [25 x i8] c"Invalid arguments to cd\0A\00"
@.str.3 = [6 x i8] c"chdir\00"
@errno = global i32 0
define i32 @builtin_cd(i8** nocapture %0) {
%2 = getelementptr inbounds i8*, i8** %0, i64 1
%3 = load i8*, i8** %2
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %25
11: ; preds = %1, %5
%12 = phi i8* [ %6, %5 ], [ %3, %1 ]
%13 = getelementptr inbounds i8*, i8** %0, i64 2
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %25
19: ; preds = %11
%20 = tail call i64 @chdir(i8* %12)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @errno
br label %25
25: ; preds = %19, %22, %16, %8
%26 = phi i32 [ -1, %8 ], [ -1, %16 ], [ %24, %22 ], [ 0, %19 ]
ret i32 %26
}