#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 const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char const *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strrep(char *, char *, char const *);
int cd(char *args[]) {
  if (args[1] == NULL) {
    chdir(getenv("HOME"));
    return 0;
  } else {
    if (chdir((const char *)strrep(args[1], "~", getenv("HOME"))) == -1) {
      fprintf(stderr, "-feshell: %s: %s: ", args[0], args[1]);
      perror("");
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    118b <cd+0x3b>
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
movl   $0x0,-0x4(%rbp)
jmp    120f <cd+0xbf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xe62(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x18(%rbp),%rdi
mov    %rax,%rdx
lea    0xe54(%rip),%rsi
call   1060 <strrep@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1203 <cd+0xb3>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe21(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120f <cd+0xbf>
jmp    1208 <cd+0xb8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x8(%rdi),%rbx
lea    0xe9e(%rip),%rdi
call   1030 <getenv@plt>
test   %rbx,%rbx
je     11be <cd+0x6e>
lea    0xe92(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1060 <strrep@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11c8 <cd+0x78>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x8(%r14),%rcx
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11c8 <cd+0x78>
mov    %rax,%rdi
call   1040 <chdir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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