#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 PATH_MAX;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ realpath(char *, char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ stderr;
int cd(int argc, char **argv) {
  char *dir;
  char path[PATH_MAX];
  if (argc == 2) {
    dir = argv[1];
  } else {
    dir = getenv("HOME");
  }
  if (access(dir, R_OK) == 0) {
    setenv("OLDPWD", getenv("PWD"), 1);
    realpath(dir, path);
    chdir(path);
    setenv("PWD", path, 1);
  } else {
    fprintf(stderr, "cd: no such directory '%s'\n", dir);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11b6 <cd+0x56>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11c6 <cd+0x66>
lea    0xe43(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    1232 <cd+0xd2>
lea    0xe1f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0xe0d(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdi
call   1070 <realpath@plt>
mov    -0x38(%rbp),%rdi
call   1050 <chdir@plt>
mov    -0x38(%rbp),%rsi
lea    0xde5(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
jmp    125e <cd+0xfe>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdc6(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    126c <cd+0x10c>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
jne    118f <cd+0x2f>
mov    0x8(%rsi),%r14
jmp    119e <cd+0x3e>
lea    0xe6a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     11d3 <cd+0x73>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    121b <cd+0xbb>
lea    0xe2e(%rip),%r15
mov    %r15,%rdi
call   1030 <getenv@plt>
lea    0xe1c(%rip),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <realpath@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"HOME\00"
@R_OK = global i32 0
@.str.1 = [7 x i8] c"OLDPWD\00"
@.str.2 = [4 x i8] c"PWD\00"
@stderr = global i32 0
@.str.3 = [28 x i8] c"cd: no such directory '%s'\0A\00"
define i32 @cd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
br label %22
20: ; preds = %2
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %6
br label %22
22: ; preds = %20, %16
%23 = load i8*, i8** %6
%24 = load i32, i32* @R_OK
%25 = call i64 @access(i8* %23, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 1)
%30 = load i8*, i8** %6
%31 = call i32 @realpath(i8* %30, i8* %13)
%32 = call i32 @chdir(i8* %13)
%33 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 1)
br label %38
34: ; preds = %22
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %6
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %36)
store i32 -1, i32* %3
store i32 1, i32* %9
br label %39
38: ; preds = %27
store i32 0, i32* %3
store i32 1, i32* %9
br label %39
39: ; preds = %38, %34
%40 = load i8*, i8** %7
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"HOME\00"
@R_OK = global i32 0
@.str.1 = [7 x i8] c"OLDPWD\00"
@.str.2 = [4 x i8] c"PWD\00"
@stderr = global i32 0
@.str.3 = [28 x i8] c"cd: no such directory '%s'\0A\00"
define i32 @cd(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i32 %0, 2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %12
10: ; preds = %2
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = phi i8* [ %9, %7 ], [ %11, %10 ]
%14 = load i32, i32* @R_OK
%15 = tail call i64 @access(i8* %13, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 1)
%20 = call i32 @realpath(i8* %13, i8* %5)
%21 = call i32 @chdir(i8* %5)
%22 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %5, i32 1)
br label %26
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %26
26: ; preds = %17, %23
%27 = phi i32 [ 0, %17 ], [ -1, %23 ]
ret i32 %27
}