void docd(argc, argv) int argc;
char *argv[];
{
  if (argc == 2) {
    if (chdir(argv[1]) != 0)
      printf("Failed to change directory %s\n", argv[1]);
  } else {
    if (argc == 1) {
      system("/usr/bin/pwd");
    } else
      printf("Usage: cd [<directory>]\n");
  }
}


x86 assembly (clang, -O0):
<docd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
jne    117c <docd+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x0,%al
call   1030 <chdir@plt>
cmp    $0x0,%eax
je     1177 <docd+0x47>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ac <docd+0x7c>
cmpl   $0x1,-0x4(%rbp)
jne    1199 <docd+0x69>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <system@plt>
jmp    11a7 <docd+0x77>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ac <docd+0x7c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<docd>:
push   %rbx
cmp    $0x1,%edi
je     1170 <docd+0x30>
cmp    $0x2,%edi
jne    117f <docd+0x3f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
xor    %eax,%eax
call   1040 <chdir@plt>
test   %eax,%eax
je     118c <docd+0x4c>
mov    0x8(%rbx),%rsi
lea    0xe98(%rip),%rdi
xor    %eax,%eax
pop    %rbx
jmp    1060 <printf@plt>
lea    0xea8(%rip),%rdi
xor    %eax,%eax
pop    %rbx
jmp    1050 <system@plt>
lea    0xea6(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Failed to change directory %s\0A\00"
@.str.1 = [13 x i8] c"/usr/bin/pwd\00"
@.str.2 = [25 x i8] c"Usage: cd [<directory>]\0A\00"
define void @docd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
store i32 %0, i32* %3
store i8** %1, i8*** %4
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 2
br i1 %6, label %7, label %19
7: ; preds = %2
%8 = load i8**, i8*** %4
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i8**, i8*** %4
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %13, %7
br label %27
19: ; preds = %2
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 (i8*, ...) bitcast (i32 (...)* @system to i32 (i8*, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %22
br label %27
27: ; preds = %26, %18
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"Failed to change directory %s\0A\00"
@.str.1 = [13 x i8] c"/usr/bin/pwd\00"
@str = [24 x i8] c"Usage: cd [<directory>]\00"
define void @docd(i32 %0, i8** nocapture %1) {
switch i32 %0, label %13 [
i32 2, label %3
i32 1, label %11
]
3: ; preds = %2
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %15
11: ; preds = %2
%12 = tail call i32 (i8*, ...) bitcast (i32 (...)* @system to i32 (i8*, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %11, %13, %3, %8
ret void
}