#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 *);
int /*<<< orphan*/ echoerr(char *, char *, char *);
int /*<<< orphan*/ parse_dir(char *, char *);
int pathtype(char *);
int _cd(int argc, char *argv[]) {
  char abs_path[512];
  int path_type;
  int is_success;
  if (argc == 1) {
    parse_dir(abs_path, "");
  } else {
    parse_dir(abs_path, argv[1]);
  }
  path_type = pathtype(abs_path);
  if (path_type == -1) {
    echoerr("SBUsh", abs_path, "No such file or directory");
    return -1;
  }
  if (path_type == 0) {
    echoerr("SBUsh", abs_path, "Not a directory");
    return -1;
  }
  if ((is_success = chdir(abs_path)) < 0) {
    echoerr("SBUsh", abs_path, "Unknown error");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1174 <_cd+0x34>
lea    -0x210(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <parse_dir@plt>
jmp    1188 <_cd+0x48>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <parse_dir@plt>
lea    -0x210(%rbp),%rdi
call   1030 <pathtype@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0xffffffff,-0x214(%rbp)
jne    11cd <_cd+0x8d>
lea    -0x210(%rbp),%rsi
lea    0xe4b(%rip),%rdi
lea    0xe4a(%rip),%rdx
call   1060 <echoerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <_cd+0x108>
cmpl   $0x0,-0x214(%rbp)
jne    1200 <_cd+0xc0>
lea    -0x210(%rbp),%rsi
lea    0xe18(%rip),%rdi
lea    0xe31(%rip),%rdx
call   1060 <echoerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <_cd+0x108>
lea    -0x210(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x218(%rbp)
cmp    $0x0,%eax
jge    1241 <_cd+0x101>
lea    -0x210(%rbp),%rsi
lea    0xdd7(%rip),%rdi
lea    0xe00(%rip),%rdx
call   1060 <echoerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <_cd+0x108>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_cd>:
push   %rbx
sub    $0x200,%rsp
cmp    $0x1,%edi
jne    1156 <_cd+0x16>
lea    0xeb1(%rip),%rsi
jmp    115a <_cd+0x1a>
mov    0x8(%rsi),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <parse_dir@plt>
mov    %rbx,%rdi
call   1030 <pathtype@plt>
cmp    $0xffffffff,%eax
je     117f <_cd+0x3f>
test   %eax,%eax
jne    1188 <_cd+0x48>
lea    0xea3(%rip),%rdx
jmp    119f <_cd+0x5f>
lea    0xe80(%rip),%rdx
jmp    119f <_cd+0x5f>
mov    %rsp,%rdi
call   1040 <chdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    11b3 <_cd+0x73>
lea    0xe91(%rip),%rdx
lea    0xe5a(%rip),%rdi
mov    %rsp,%rsi
call   1060 <echoerr@plt>
mov    $0xffffffff,%eax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [6 x i8] c"SBUsh\00"
@.str.2 = [26 x i8] c"No such file or directory\00"
@.str.3 = [16 x i8] c"Not a directory\00"
@.str.4 = [14 x i8] c"Unknown error\00"
define i32 @_cd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%13 = call i32 @parse_dir(i8* %12, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %20
14: ; preds = %2
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @parse_dir(i8* %15, i8* %18)
br label %20
20: ; preds = %14, %11
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%22 = call i32 @pathtype(i8* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%27 = call i32 @echoerr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
28: ; preds = %20
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%33 = call i32 @echoerr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
34: ; preds = %28
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%36 = call i32 @chdir(i8* %35)
store i32 %36, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%40 = call i32 @echoerr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
41: ; preds = %34
store i32 0, i32* %3
br label %42
42: ; preds = %41, %38, %31, %25
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [6 x i8] c"SBUsh\00"
@.str.2 = [26 x i8] c"No such file or directory\00"
@.str.3 = [16 x i8] c"Not a directory\00"
@.str.4 = [14 x i8] c"Unknown error\00"
define i32 @_cd(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = call i32 @parse_dir(i8* %4, i8* %10)
%12 = call i32 @pathtype(i8* %4)
switch i32 %12, label %14 [
i32 -1, label %17
i32 0, label %13
]
13: ; preds = %9
br label %17
14: ; preds = %9
%15 = call i32 @chdir(i8* %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14, %9, %13
%18 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), %14 ]
%19 = call i32 @echoerr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %18)
br label %20
20: ; preds = %17, %14
%21 = phi i32 [ 0, %14 ], [ -1, %17 ]
ret i32 %21
}