#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 */
#define ENOENT 128
int chdir(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int cd_cmd(char *argv[], char *pwd) {
  int ret;
  if (strcmp(argv[0], "cd") == 0) {
    if (argv[1] == NULL) {
      ret = chdir(getenv("HOME"));
    } else if (strcmp(argv[1], "-") == 0) {
      ret = chdir(pwd);
    } else {
      getcwd(pwd, 50);
      ret = chdir(argv[1]);
      switch (ret) {
      case -1:
        fprintf(stdout, "\"%s\" is no valid path\n", argv[1]);
        break;
      case 0:
        break;
      case ENOENT:
        printf("dir not found\n");
        break;
      default:
        break;
      }
    }
  } else
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<cd_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe82(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1288 <cd_cmd+0x128>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11b8 <cd_cmd+0x58>
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
jmp    1283 <cd_cmd+0x123>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11e7 <cd_cmd+0x87>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
jmp    127e <cd_cmd+0x11e>
mov    -0x18(%rbp),%rdi
mov    $0x32,%esi
call   1060 <getcwd@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xffffffff,%eax
je     123c <cd_cmd+0xdc>
jmp    1219 <cd_cmd+0xb9>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     125e <cd_cmd+0xfe>
jmp    1229 <cd_cmd+0xc9>
mov    -0x20(%rbp),%eax
sub    $0x80,%eax
je     1263 <cd_cmd+0x103>
jmp    1274 <cd_cmd+0x114>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdb6(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1279 <cd_cmd+0x119>
jmp    1279 <cd_cmd+0x119>
lea    0xdb7(%rip),%rdi
call   1050 <printf@plt>
jmp    1279 <cd_cmd+0x119>
jmp    1279 <cd_cmd+0x119>
jmp    127e <cd_cmd+0x11e>
jmp    1283 <cd_cmd+0x123>
jmp    1294 <cd_cmd+0x134>
movl   $0x1,-0x4(%rbp)
jmp    129b <cd_cmd+0x13b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd_cmd>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <strcmp@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11f2 <cd_cmd+0x92>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11dc <cd_cmd+0x7c>
lea    0xe75(%rip),%rsi
call   1070 <strcmp@plt>
mov    %r14,%rdi
test   %rax,%rax
je     11eb <cd_cmd+0x8b>
mov    $0x32,%esi
call   1060 <getcwd@plt>
mov    0x8(%rbx),%rdi
call   1050 <chdir@plt>
xor    %ebp,%ebp
cmp    $0x80,%eax
je     11f9 <cd_cmd+0x99>
cmp    $0xffffffff,%eax
jne    11f2 <cd_cmd+0x92>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe35(%rip),%rsi
call   1080 <fprintf@plt>
jmp    11f2 <cd_cmd+0x92>
lea    0xe20(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1050 <chdir@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe21(%rip),%rdi
call   1040 <puts@plt>
jmp    11f2 <cd_cmd+0x92>

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [2 x i8] c"-\00"
@stdout = global i32 0
@.str.3 = [23 x i8] c"\22%s\22 is no valid path\0A\00"
@.str.4 = [15 x i8] c"dir not found\0A\00"
define i32 @cd_cmd(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
%7 = load i8**, i8*** %4
%8 = getelementptr inbounds i8*, i8** %7, i64 0
%9 = load i8*, i8** %8
%10 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %50
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @chdir(i8* %18)
store i32 %19, i32* %6
br label %49
20: ; preds = %12
%21 = load i8**, i8*** %4
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = call i32 @chdir(i8* %27)
store i32 %28, i32* %6
br label %48
29: ; preds = %20
%30 = load i8*, i8** %5
%31 = call i32 @getcwd(i8* %30, i32 50)
%32 = load i8**, i8*** %4
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @chdir(i8* %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
switch i32 %36, label %46 [
i32 -1, label %37
i32 0, label %43
i32 128, label %44
]
37: ; preds = %29
%38 = load i32, i32* @stdout
%39 = load i8**, i8*** %4
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %47
43: ; preds = %29
br label %47
44: ; preds = %29
%45 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %47
46: ; preds = %29
br label %47
47: ; preds = %46, %44, %43, %37
br label %48
48: ; preds = %47, %26
br label %49
49: ; preds = %48, %17
br label %51
50: ; preds = %2
store i32 1, i32* %3
br label %52
51: ; preds = %49
store i32 0, i32* %3
br label %52
52: ; preds = %51, %50
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [2 x i8] c"-\00"
@stdout = global i32 0
@.str.3 = [23 x i8] c"\22%s\22 is no valid path\0A\00"
@str = [14 x i8] c"dir not found\00"
define i32 @cd_cmd(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %28
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %0, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @chdir(i8* %11)
br label %28
13: ; preds = %6
%14 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @chdir(i8* %1)
br label %28
18: ; preds = %13
%19 = tail call i32 @getcwd(i8* %1, i32 50)
%20 = load i8*, i8** %7
%21 = tail call i32 @chdir(i8* %20)
switch i32 %21, label %28 [
i32 -1, label %22
i32 128, label %26
]
22: ; preds = %18
%23 = load i32, i32* @stdout
%24 = load i8*, i8** %7
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %24)
br label %28
26: ; preds = %18
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %16, %18, %26, %22, %10, %2
%29 = phi i32 [ 1, %2 ], [ 0, %10 ], [ 0, %22 ], [ 0, %26 ], [ 0, %18 ], [ 0, %16 ]
ret i32 %29
}