#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 errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int sh_cd(char *path) {
  if (!path) {
    char *res = getenv("HOME");
    if (res) {
      path = res;
    } else {
      return 0;
    }
  }
  int cd_res = chdir(path);
  if (cd_res == -1) {
    fprintf(stderr, "cd: %s: %s\n", strerror(errno), path);
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sh_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <sh_cd+0x50>
lea    0xea2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117f <sh_cd+0x3f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    118b <sh_cd+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    11ee <sh_cd+0xae>
jmp    1190 <sh_cd+0x50>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e7 <sh_cd+0xa7>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
lea    0xe34(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <sh_cd+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh_cd>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1160 <sh_cd+0x20>
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a2 <sh_cd+0x62>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11a4 <sh_cd+0x64>
mov    0x2e68(%rip),%rax
mov    (%rax),%ebp
mov    0x2e57(%rip),%r14
mov    (%r14),%edi
call   1060 <strerror@plt>
lea    0xe75(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1050 <fprintf@plt>
mov    (%r14),%eax
jmp    11a4 <sh_cd+0x64>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"cd: %s: %s\0A\00"
@errno = global i32 0
define i32 @sh_cd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %16, label %8
8: ; preds = %1
%9 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = load i8*, i8** %4
store i8* %13, i8** %3
br label %15
14: ; preds = %8
store i32 0, i32* %2
br label %29
15: ; preds = %12
br label %16
16: ; preds = %15, %1
%17 = load i8*, i8** %3
%18 = call i32 @chdir(i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = load i8*, i8** %3
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %25)
%27 = load i32, i32* @errno
store i32 %27, i32* %2
br label %29
28: ; preds = %16
store i32 0, i32* %2
br label %29
29: ; preds = %28, %21, %14
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"cd: %s: %s\0A\00"
@errno = global i32 0
define i32 @sh_cd(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3, %1
%7 = phi i8* [ %0, %1 ], [ %4, %3 ]
%8 = tail call i32 @chdir(i8* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %7)
%15 = load i32, i32* @errno
br label %16
16: ; preds = %3, %10, %6
%17 = phi i32 [ 0, %3 ], [ %15, %10 ], [ 0, %6 ]
ret i32 %17
}