#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 */
struct iso_dir {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ get_abs_path(char *, char *);
int /*<<< orphan*/ iso_dclose(struct iso_dir *);
struct iso_dir *iso_dopen(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_cur_path(char *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strtok(char *, char *);
void cmd_line_cd(const char *arg_line) {
  if (strcmp(arg_line, "--HELP") == 0) {
    printf(
        "Change the current working directory\nusage: cd <directory path>\n");
    return;
  }
  char line_copy[strlen(arg_line) + 1];
  strcpy(line_copy, arg_line);
  char *first_word = strtok(line_copy, " ");
  if (strlen(first_word) == 0 || strcmp(first_word, "/") == 0) {
    set_cur_path("/");
    return;
  }
  char abs_path[256];
  get_abs_path(first_word, abs_path);
  if (!abs_path[0]) {
    printf("cd: root has no parent\n");
    return;
  }
  struct iso_dir *dir = iso_dopen(abs_path, 3);
  if (!dir) {
    printf("cd: no such path %s\n", abs_path);
  } else {
    set_cur_path(abs_path);
  }
  iso_dclose(dir);
  return;
}


x86 assembly (clang, -O0):
<cmd_line_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    11cc <cmd_line_cd+0x3c>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1304 <cmd_line_cd+0x174>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
add    $0x10,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x140(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x140(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
je     124f <cmd_line_cd+0xbf>
mov    -0x20(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1267 <cmd_line_cd+0xd7>
lea    0xdf4(%rip),%rdi
call   1070 <set_cur_path@plt>
movl   $0x1,-0x24(%rbp)
jmp    12fd <cmd_line_cd+0x16d>
mov    -0x20(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   10b0 <get_abs_path@plt>
cmpb   $0x0,-0x130(%rbp)
jne    129e <cmd_line_cd+0x10e>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    12fd <cmd_line_cd+0x16d>
lea    -0x130(%rbp),%rdi
mov    $0x3,%esi
call   1030 <iso_dopen@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    12de <cmd_line_cd+0x14e>
lea    -0x130(%rbp),%rsi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ea <cmd_line_cd+0x15a>
lea    -0x130(%rbp),%rdi
call   1070 <set_cur_path@plt>
mov    -0x138(%rbp),%rdi
call   1080 <iso_dclose@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_line_cd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
lea    0xe46(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1267 <cmd_line_cd+0xc7>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rsp,%r15
mov    %rsp,%rbx
add    $0x10,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0xe11(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strtok@plt>
cmpb   $0x0,(%rax)
je     1259 <cmd_line_cd+0xb9>
mov    %rax,%rbx
lea    0xdfc(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1259 <cmd_line_cd+0xb9>
lea    -0x120(%rbp),%rsi
mov    %rbx,%rdi
call   10c0 <get_abs_path@plt>
cmpb   $0x0,-0x120(%rbp)
je     127d <cmd_line_cd+0xdd>
lea    -0x120(%rbp),%rdi
mov    $0x3,%esi
call   1030 <iso_dopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     128b <cmd_line_cd+0xeb>
lea    -0x120(%rbp),%rdi
call   1080 <set_cur_path@plt>
jmp    12a0 <cmd_line_cd+0x100>
lea    0xda9(%rip),%rdi
call   1080 <set_cur_path@plt>
jmp    12a8 <cmd_line_cd+0x108>
lea    0xdc9(%rip),%rdi
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <puts@plt>
lea    0xd9c(%rip),%rdi
call   1050 <puts@plt>
jmp    12a8 <cmd_line_cd+0x108>
lea    0xd79(%rip),%rdi
lea    -0x120(%rbp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1090 <iso_dclose@plt>
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iso_dir = type { i32 }
@.str = [7 x i8] c"--HELP\00"
@.str.1 = [65 x i8] c"Change the current working directory\0Ausage: cd <directory path>\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [24 x i8] c"cd: root has no parent\0A\00"
@.str.5 = [21 x i8] c"cd: no such path %s\0A\00"
define void @cmd_line_cd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca %struct.iso_dir*
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
br label %57
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %16, 1
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %3
%19 = alloca i8, i64 %17
store i64 %17, i64* %4
%20 = load i8*, i8** %2
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = call i64 @strlen(i8* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %14
%27 = load i8*, i8** %5
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26, %14
%31 = call i32 @set_cur_path(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
br label %55
32: ; preds = %26
%33 = load i8*, i8** %5
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%35 = call i32 @get_abs_path(i8* %33, i8* %34)
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %6
br label %55
41: ; preds = %32
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%43 = call %struct.iso_dir* @iso_dopen(i8* %42, i32 3)
store %struct.iso_dir* %43, %struct.iso_dir** %8
%44 = load %struct.iso_dir*, %struct.iso_dir** %8
%45 = icmp ne %struct.iso_dir* %44, null
br i1 %45, label %49, label %46
46: ; preds = %41
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %47)
br label %52
49: ; preds = %41
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%51 = call i32 @set_cur_path(i8* %50)
br label %52
52: ; preds = %49, %46
%53 = load %struct.iso_dir*, %struct.iso_dir** %8
%54 = call i32 @iso_dclose(%struct.iso_dir* %53)
store i32 1, i32* %6
br label %55
55: ; preds = %52, %39, %30
%56 = load i8*, i8** %3
br label %57
57: ; preds = %55, %12
ret void
}

LLVM IR (-O2):
%struct.iso_dir = type { i32 }
@.str = [7 x i8] c"--HELP\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"/\00"
@.str.5 = [21 x i8] c"cd: no such path %s\0A\00"
@str = [23 x i8] c"cd: root has no parent\00"
@str.6 = [64 x i8] c"Change the current working directory\0Ausage: cd <directory path>\00"
define void @cmd_line_cd(i8* %0) {
%2 = alloca [256 x i8]
%3 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.6, i64 0, i64 0))
br label %39
7: ; preds = %1
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = add nsw i64 %8, 1
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = call i32 @strcpy(i8* %11, i8* %0)
%13 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16, %7
%20 = call i32 @set_cur_path(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %38
21: ; preds = %16
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%23 = call i32 @get_abs_path(i8* %13, i8* %22)
%24 = load i8, i8* %22
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %37
28: ; preds = %21
%29 = call %struct.iso_dir* @iso_dopen(i8* %22, i32 3)
%30 = icmp eq %struct.iso_dir* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %22)
br label %35
33: ; preds = %28
%34 = call i32 @set_cur_path(i8* %22)
br label %35
35: ; preds = %33, %31
%36 = call i32 @iso_dclose(%struct.iso_dir* %29)
br label %37
37: ; preds = %35, %26
br label %38
38: ; preds = %37, %19
br label %39
39: ; preds = %38, %5
ret void
}