#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct passwd {
  char *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int chdir(char *);
int /*<<< orphan*/ getcwd(char *, int);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putenv(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int cd(char *args[], int size) {
  int ret;
  struct stat buf;
  struct passwd *user;
  char value[1000];
  char pwd[1000];
  if (size == 0) {
    user = getpwuid(getuid());
    ret = chdir(user->pw_dir);
    if (ret == -1) {
      printf("myshell: cd: %s: 打开目录失败\n", user->pw_dir);
      return 0;
    }
    getcwd(pwd, 999);
    strcpy(value, "PWD=");
    strcat(value, pwd);
    putenv(value);
    return 1;
  }
  ret = stat(args[0], &buf);
  if (ret == -1) {
    printf("myshell: cd: %s: 没有该目录或文件\n", args[0]);
    return 0;
  }
  if (!S_ISDIR(buf.st_mode)) {
    printf("myshell: cd: %s: 不是一个目录\n", args[0]);
    return 0;
  }
  ret = chdir(args[0]);
  if (ret == -1) {
    printf("myshell: cd: %s: 打开目录失败\n", args[0]);
    return 0;
  }
  getcwd(pwd, 999);
  strcpy(value, "PWD=");
  strcat(value, pwd);
  putenv(value);
  return 1;
}


x86 assembly (clang, -O0):
<cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x800,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1255 <cd+0xb5>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1050 <chdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1206 <cd+0x66>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0b(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1343 <cd+0x1a3>
lea    -0x800(%rbp),%rdi
mov    $0x3e7,%esi
call   1090 <getcwd@plt>
lea    -0x410(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
lea    -0x800(%rbp),%rsi
call   10c0 <strcat@plt>
lea    -0x410(%rbp),%rdi
call   10a0 <putenv@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <cd+0x1a3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1291 <cd+0xf1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdaa(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1343 <cd+0x1a3>
mov    -0x20(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12c1 <cd+0x121>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xda5(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1343 <cd+0x1a3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <chdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12f9 <cd+0x159>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd18(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1343 <cd+0x1a3>
lea    -0x800(%rbp),%rdi
mov    $0x3e7,%esi
call   1090 <getcwd@plt>
lea    -0x410(%rbp),%rdi
lea    0xd0d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
lea    -0x800(%rbp),%rsi
call   10c0 <strcat@plt>
lea    -0x410(%rbp),%rdi
call   10a0 <putenv@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x800,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd>:
push   %r14
push   %rbx
sub    $0x7e8,%rsp
test   %esi,%esi
je     11f4 <cd+0x54>
mov    %rdi,%rbx
mov    (%rdi),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
je     1257 <cd+0xb7>
mov    0x8(%rsp),%edi
call   1080 <S_ISDIR@plt>
mov    (%rbx),%rsi
test   %eax,%eax
je     1263 <cd+0xc3>
mov    %rsi,%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1214 <cd+0x74>
mov    (%rbx),%rsi
lea    0xe0e(%rip),%rdi
jmp    126a <cd+0xca>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,%r14
mov    (%rax),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     1273 <cd+0xd3>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x3e7,%esi
call   1090 <getcwd@plt>
lea    0xdf8(%rip),%rsi
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <strcat@plt>
mov    %rbx,%rdi
call   10a0 <putenv@plt>
mov    $0x1,%ebx
jmp    1282 <cd+0xe2>
mov    (%rbx),%rsi
lea    0xdc9(%rip),%rdi
jmp    126a <cd+0xca>
lea    0xdeb(%rip),%rdi
call   1070 <printf@plt>
xor    %ebx,%ebx
jmp    1282 <cd+0xe2>
mov    (%r14),%rsi
lea    0xd83(%rip),%rdi
call   1070 <printf@plt>
mov    %ebx,%eax
add    $0x7e8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.passwd = type { i8* }
@.str = [37 x i8] c"myshell: cd: %s: \E6\89\93\E5\BC\80\E7\9B\AE\E5\BD\95\E5\A4\B1\E8\B4\A5\0A\00"
@.str.1 = [5 x i8] c"PWD=\00"
@.str.2 = [43 x i8] c"myshell: cd: %s: \E6\B2\A1\E6\9C\89\E8\AF\A5\E7\9B\AE\E5\BD\95\E6\88\96\E6\96\87\E4\BB\B6\0A\00"
@.str.3 = [37 x i8] c"myshell: cd: %s: \E4\B8\8D\E6\98\AF\E4\B8\80\E4\B8\AA\E7\9B\AE\E5\BD\95\0A\00"
define i32 @cd(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca %struct.passwd*
%9 = alloca [1000 x i8]
%10 = alloca [1000 x i8]
store i8** %0, i8*** %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %2
%14 = call i32 (...) @getuid()
%15 = call %struct.passwd* @getpwuid(i32 %14)
store %struct.passwd* %15, %struct.passwd** %8
%16 = load %struct.passwd*, %struct.passwd** %8
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @chdir(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load %struct.passwd*, %struct.passwd** %8
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 0, i32* %3
br label %81
27: ; preds = %13
%28 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%29 = call i32 @getcwd(i8* %28, i32 999)
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%31 = call i32 @strcpy(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%33 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%34 = call i32 @strcat(i8* %32, i8* %33)
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%36 = call i32 @putenv(i8* %35)
store i32 1, i32* %3
br label %81
37: ; preds = %2
%38 = load i8**, i8*** %4
%39 = getelementptr inbounds i8*, i8** %38, i64 0
%40 = load i8*, i8** %39
%41 = call i32 @stat(i8* %40, %struct.stat* %7)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %37
%45 = load i8**, i8*** %4
%46 = getelementptr inbounds i8*, i8** %45, i64 0
%47 = load i8*, i8** %46
%48 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %47)
store i32 0, i32* %3
br label %81
49: ; preds = %37
%50 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @S_ISDIR(i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %49
%55 = load i8**, i8*** %4
%56 = getelementptr inbounds i8*, i8** %55, i64 0
%57 = load i8*, i8** %56
%58 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %57)
store i32 0, i32* %3
br label %81
59: ; preds = %49
%60 = load i8**, i8*** %4
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load i8*, i8** %61
%63 = call i32 @chdir(i8* %62)
store i32 %63, i32* %6
%64 = load i32, i32* %6
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %71
66: ; preds = %59
%67 = load i8**, i8*** %4
%68 = getelementptr inbounds i8*, i8** %67, i64 0
%69 = load i8*, i8** %68
%70 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %69)
store i32 0, i32* %3
br label %81
71: ; preds = %59
%72 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%73 = call i32 @getcwd(i8* %72, i32 999)
%74 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%75 = call i32 @strcpy(i8* %74, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%76 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%77 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%78 = call i32 @strcat(i8* %76, i8* %77)
%79 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%80 = call i32 @putenv(i8* %79)
store i32 1, i32* %3
br label %81
81: ; preds = %71, %66, %54, %44, %27, %22
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.passwd = type { i8* }
@.str = [37 x i8] c"myshell: cd: %s: \E6\89\93\E5\BC\80\E7\9B\AE\E5\BD\95\E5\A4\B1\E8\B4\A5\0A\00"
@.str.1 = [5 x i8] c"PWD=\00"
@.str.2 = [43 x i8] c"myshell: cd: %s: \E6\B2\A1\E6\9C\89\E8\AF\A5\E7\9B\AE\E5\BD\95\E6\88\96\E6\96\87\E4\BB\B6\0A\00"
@.str.3 = [37 x i8] c"myshell: cd: %s: \E4\B8\8D\E6\98\AF\E4\B8\80\E4\B8\AA\E7\9B\AE\E5\BD\95\0A\00"
define i32 @cd(i8** nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca [1000 x i8]
%5 = alloca [1000 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %1, 0
br i1 %9, label %10, label %25
10: ; preds = %2
%11 = tail call i32 (...) @getuid()
%12 = tail call %struct.passwd* @getpwuid(i32 %11)
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i32 @chdir(i8* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i8*, i8** %13
%19 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %51
20: ; preds = %10
%21 = call i32 @getcwd(i8* %8, i32 999)
%22 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @strcat(i8* %7, i8* %8)
%24 = call i32 @putenv(i8* %7)
br label %51
25: ; preds = %2
%26 = load i8*, i8** %0
%27 = call i32 @stat(i8* %26, %struct.stat* %3)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %0
%31 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %51
32: ; preds = %25
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i32, i32* %33
%35 = tail call i32 @S_ISDIR(i32 %34)
%36 = icmp eq i32 %35, 0
%37 = load i8*, i8** %0
br i1 %36, label %38, label %40
38: ; preds = %32
%39 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %51
40: ; preds = %32
%41 = tail call i32 @chdir(i8* %37)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** %0
%45 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %44)
br label %51
46: ; preds = %40
%47 = call i32 @getcwd(i8* %8, i32 999)
%48 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @strcat(i8* %7, i8* %8)
%50 = call i32 @putenv(i8* %7)
br label %51
51: ; preds = %46, %43, %38, %29, %20, %17
%52 = phi i32 [ 0, %17 ], [ 1, %20 ], [ 0, %29 ], [ 0, %43 ], [ 1, %46 ], [ 0, %38 ]
ret i32 %52
}