#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 chdir(char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int builtin_cd(size_t argc, char **argv) {
  char *dest = NULL;
  if (argc < 2) {
    dest = getenv("HOME");
  } else {
    dest = argv[1];
  }
  if (dest == NULL) {
    dest = "/";
  }
  if (chdir(dest) == -1) {
    perror("chdir");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<builtin_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x2,-0x10(%rbp)
jae    1168 <builtin_cd+0x38>
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
jmp    1174 <builtin_cd+0x44>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118a <builtin_cd+0x5a>
lea    0xe7f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11b4 <builtin_cd+0x84>
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <builtin_cd+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_cd>:
push   %rax
cmp    $0x1,%rdi
ja     1145 <builtin_cd+0x15>
lea    0xec2(%rip),%rdi
call   1030 <getenv@plt>
jmp    1149 <builtin_cd+0x19>
mov    0x8(%rsi),%rax
test   %rax,%rax
lea    0xeb2(%rip),%rdi
cmovne %rax,%rdi
call   1040 <chdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1167 <builtin_cd+0x37>
pop    %rcx
ret
lea    0xe99(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"chdir\00"
define i32 @builtin_cd(i64 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8**
%6 = alloca i8*
store i64 %0, i64* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
%7 = load i64, i64* %4
%8 = icmp ult i64 %7, 2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
br label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %6
br label %15
15: ; preds = %11, %9
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %15
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %19
19: ; preds = %18, %15
%20 = load i8*, i8** %6
%21 = call i32 @chdir(i8* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
25: ; preds = %19
store i32 0, i32* %3
br label %26
26: ; preds = %25, %23
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"chdir\00"
define i32 @builtin_cd(i64 %0, i8** nocapture %1) {
%3 = icmp ult i64 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %4
%10 = phi i8* [ %5, %4 ], [ %8, %6 ]
%11 = icmp eq i8* %10, null
%12 = select i1 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %10
%13 = tail call i32 @chdir(i8* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %9, %15
%18 = phi i32 [ -1, %15 ], [ 0, %9 ]
ret i32 %18
}