cmd_cd(str) char *str;
{
  if (str == 0)
    str = getenv("HOME");
  if (str == 0)
    error("Cannot find your home directory: $HOME not set.");
  if (chdir(str) == -1)
    uerror("chdir to %s failed.", str);
}


x86 assembly (clang, -O0):
<cmd_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <cmd_cd+0x2b>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <cmd_cd+0x44>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11aa <cmd_cd+0x6a>
mov    -0x10(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <uerror@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_cd>:
push   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1170 <cmd_cd+0x30>
lea    0xeb0(%rip),%rdi
xor    %eax,%eax
call   1030 <getenv@plt>
test   %eax,%eax
je     1160 <cmd_cd+0x20>
movslq %eax,%rbx
jmp    1170 <cmd_cd+0x30>
lea    0xe9e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <error@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     1181 <cmd_cd+0x41>
pop    %rbx
ret
lea    0xead(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <uerror@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [48 x i8] c"Cannot find your home directory: $HOME not set.\00"
@.str.2 = [20 x i8] c"chdir to %s failed.\00"
define i32 @cmd_cd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
store i8* %0, i8** %3
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = call i32 (i8*, ...) bitcast (i32 (...)* @getenv to i32 (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %3
br label %10
10: ; preds = %6, %1
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @uerror to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [48 x i8] c"Cannot find your home directory: $HOME not set.\00"
@.str.2 = [20 x i8] c"chdir to %s failed.\00"
define i32 @cmd_cd(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %10
3: ; preds = %1
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @getenv to i32 (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i32 %4, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %1, %8, %3
%11 = phi i8* [ null, %8 ], [ %6, %3 ], [ %0, %1 ]
%12 = tail call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @uerror to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %16
16: ; preds = %14, %10
ret i32 undef
}