#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 /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int do_cd(char **arguments) {
  char *homedir;
  if ((arguments[0] != NULL) && !strcmp(arguments[0], "cd")) {
    if (arguments[1] != NULL) {
      if (chdir(arguments[1]) == -1) {
        perror("Could not change directory");
      }
    } else {
      homedir = getenv("HOME");
      if (homedir) {
        if (chdir(homedir) == -1) {
          perror("Could not change directory");
        }
      } else {
        printf("Could not read %s environment variable\n", "HOME");
      }
    }
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     121e <do_cd+0xce>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe88(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    121e <do_cd+0xce>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11bc <do_cd+0x6c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11b7 <do_cd+0x67>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
jmp    1212 <do_cd+0xc2>
lea    0xe5b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fa <do_cd+0xaa>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11f5 <do_cd+0xa5>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
jmp    120d <do_cd+0xbd>
lea    0xe22(%rip),%rdi
lea    0xe16(%rip),%rsi
call   1050 <printf@plt>
jmp    1212 <do_cd+0xc2>
movl   $0x1,-0x4(%rbp)
jmp    1225 <do_cd+0xd5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cd>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%rdi
xor    %ebp,%ebp
test   %rdi,%rdi
je     119c <do_cd+0x4c>
lea    0xe99(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    119c <do_cd+0x4c>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
jne    118d <do_cd+0x3d>
lea    0xe9e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a5 <do_cd+0x55>
mov    %rax,%rdi
call   1040 <chdir@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     11bf <do_cd+0x6f>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe77(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    119c <do_cd+0x4c>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
jmp    119c <do_cd+0x4c>

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
@.str.1 = [27 x i8] c"Could not change directory\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [40 x i8] c"Could not read %s environment variable\0A\00"
define i32 @do_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 0
%7 = load i8*, i8** %6
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %44
9: ; preds = %1
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 @strcmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne i32 %13, 0
br i1 %14, label %44, label %15
15: ; preds = %9
%16 = load i8**, i8*** %3
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = load i8**, i8*** %3
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @chdir(i8* %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
br label %43
29: ; preds = %15
%30 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = call i32 @chdir(i8* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
br label %42
40: ; preds = %29
%41 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %39
br label %43
43: ; preds = %42, %28
store i32 1, i32* %2
br label %45
44: ; preds = %9, %1
store i32 0, i32* %2
br label %45
45: ; preds = %44, %43
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
@.str.1 = [27 x i8] c"Could not change directory\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [40 x i8] c"Could not read %s environment variable\0A\00"
define i32 @do_cd(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = icmp eq i8* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = tail call i32 @strcmp(i8* %2, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %4
%8 = getelementptr inbounds i8*, i8** %0, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = tail call i32 @chdir(i8* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %26
16: ; preds = %7
%17 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = tail call i32 @chdir(i8* %17)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %26
24: ; preds = %16
%25 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %1, %4, %14, %11, %19, %22, %24
%27 = phi i32 [ 1, %24 ], [ 1, %22 ], [ 1, %19 ], [ 1, %11 ], [ 1, %14 ], [ 0, %4 ], [ 0, %1 ]
ret i32 %27
}