// File: /workspace/github_cpp_makefile_star2_O2/brandoncanaday[P]1730sh/1730sh.cpp
// Function: cd_builtin(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&)
int cd_builtin(const vector<string> & args) {
  if(args.size() > 2) { // too many args
    cout << "1730sh: Usage: cd [PATH]" << endl;
    return -1;
  } // if/else
  // home dir retrieval
  const char * homedir = nullptr;
  string s_homedir = "";
  if((homedir = getenv("HOME")) == nullptr) {
    homedir = getpwuid(getuid())->pw_dir;
  } // if
  s_homedir = string(homedir);
  // actual chdir stuff
  if(args.size() == 1) { // 'cd'
    if(chdir(homedir) == -1) { 
      int err = errno; 
      cout << "1730sh: cd: " << homedir << ": " << strerror(err) << endl;
      return -1;
    } // if
  } else if(args.size() == 2) { // cd [PATH]
    string dest = args[1];
    // replaces "~" with full path of HOME
    size_t pos;
    if((pos = dest.find("~")) != string::npos) {
      dest.replace(pos,1,s_homedir);
    } // if    
    // current dir retrieval
    char cwd[PATH_MAX];
    string s_cwd = "";
    if(getcwd(cwd,sizeof(cwd)) == nullptr) { 
      int err = errno; 
      cout << "1730sh: cd: " << strerror(err) << endl;
      return -1;      
    } // if
    s_cwd = string(cwd);
    if(dest[0] == '/') { // if dest already started with '/', no need to append to cwd
      if(chdir(dest.c_str()) == -1) { 
	int err = errno; 
	cout << "1730sh: cd: " << dest << ": " << strerror(err) << endl;
	return -1;
      } // if
    } else { // dest was a relative path, so need to append '/' + dest to cwd
      s_cwd = s_cwd + "/" + dest;
      if(chdir(s_cwd.c_str()) == -1) { 
	int err = errno; 
	cout << "1730sh: cd: " << dest << ": " << strerror(err) << endl;
	return -1;
      } // if
    } // if/else
  } // if/else
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x10a8, %rsp           # imm = 0x10A8
movq 0x8(%rdi), %rax
subq (%rdi), %rax
cmpq $0x41, %rax
jb 0x5fae
movq 0xa037(%rip), %rdi      # 0xffc8
leaq 0x5936(%rip), %rsi      # 0xb8ce
callq 0x3280
movq %rax, %rdi
callq 0x3130
pushq $-0x1
popq %r14
jmp 0x62dd
movq %rdi, %r14
leaq 0x5b2a(%rip), %rsi      # 0xbae2
leaq -0xb8(%rbp), %rdi
leaq -0x10c0(%rbp), %rdx
callq 0x3100
leaq 0x4072(%rip), %rdi      # 0xa044
callq 0x3300
movq %rax, %rbx
testq %rax, %rax
jne 0x5fef
callq 0x3140
movl %eax, %edi
callq 0x32d0
movq 0x20(%rax), %rbx
leaq -0x10c0(%rbp), %rdi
leaq -0x38(%rbp), %rdx
movq %rbx, %rsi
callq 0x3100
leaq -0xb8(%rbp), %rdi
leaq -0x10c0(%rbp), %r15
movq %r15, %rsi
callq 0x3310
movq %r15, %rdi
callq 0x3120
movq (%r14), %rsi
movq 0x8(%r14), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq $0x2, %rax
je 0x60aa
xorl %r14d, %r14d
cmpq $0x1, %rax
jne 0x62d1
movq %rbx, %rdi
callq 0x33f0
cmpl $-0x1, %eax
jne 0x62d1
callq 0x3050
movl (%rax), %r14d
movq 0x9f67(%rip), %rdi      # 0xffc8
leaq 0x587f(%rip), %rsi      # 0xb8e7
callq 0x3280
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3280
leaq 0x5872(%rip), %rsi      # 0xb8f1
movq %rax, %rdi
callq 0x3280
movq %rax, %rbx
movl %r14d, %edi
callq 0x30d0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, %rdi
callq 0x3130
jmp 0x62cd
addq $0x20, %rsi
leaq -0x38(%rbp), %rdi
callq 0x3380
leaq 0x47a8(%rip), %rsi      # 0xa866
leaq -0x38(%rbp), %rdi
xorl %edx, %edx
callq 0x3520
cmpq $-0x1, %rax
je 0x60e5
leaq -0x38(%rbp), %rdi
pushq $0x1
popq %rdx
leaq -0xb8(%rbp), %rcx
movq %rax, %rsi
callq 0x30f0
leaq 0x59f6(%rip), %rsi      # 0xbae2
leaq -0x58(%rbp), %rdi
leaq -0x98(%rbp), %rdx
callq 0x3100
leaq -0x10c0(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3150
testq %rax, %rax
je 0x61ba
leaq -0x98(%rbp), %rdi
leaq -0x10c0(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0x3100
leaq -0x58(%rbp), %rdi
leaq -0x98(%rbp), %rbx
movq %rbx, %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3120
movq -0x38(%rbp), %rdi
cmpb $0x2f, (%rdi)
jne 0x61f6
callq 0x33f0
cmpl $-0x1, %eax
jne 0x6252
callq 0x3050
movl (%rax), %ebx
movq 0x9e57(%rip), %rdi      # 0xffc8
leaq 0x576f(%rip), %rsi      # 0xb8e7
callq 0x3280
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
callq 0x3250
leaq 0x5761(%rip), %rsi      # 0xb8f1
movq %rax, %rdi
callq 0x3280
movq %rax, %r14
movl %ebx, %edi
callq 0x30d0
movq %r14, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, %rdi
callq 0x3130
jmp 0x62bb
callq 0x3050
movl (%rax), %ebx
movq 0x9e00(%rip), %rdi      # 0xffc8
leaq 0x5718(%rip), %rsi      # 0xb8e7
callq 0x3280
movq %rax, %r14
movl %ebx, %edi
callq 0x30d0
movq %r14, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, %rdi
callq 0x3130
jmp 0x62bb
leaq 0x56f7(%rip), %rdx      # 0xb8f4
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x761b
leaq -0x98(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x75f2
leaq -0x58(%rbp), %rbx
leaq -0x98(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3310
movq %r14, %rdi
callq 0x3120
leaq -0x78(%rbp), %rdi
callq 0x3120
movq (%rbx), %rdi
callq 0x33f0
cmpl $-0x1, %eax
je 0x6269
leaq -0x58(%rbp), %rdi
callq 0x3120
leaq -0x38(%rbp), %rdi
callq 0x3120
xorl %r14d, %r14d
jmp 0x62d1
callq 0x3050
movl (%rax), %ebx
movq 0x9d51(%rip), %rdi      # 0xffc8
leaq 0x5669(%rip), %rsi      # 0xb8e7
callq 0x3280
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
callq 0x3250
leaq 0x565b(%rip), %rsi      # 0xb8f1
movq %rax, %rdi
callq 0x3280
movq %rax, %r14
movl %ebx, %edi
callq 0x30d0
movq %r14, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, %rdi
callq 0x3130
leaq -0x58(%rbp), %rdi
callq 0x3120
leaq -0x38(%rbp), %rdi
callq 0x3120
pushq $-0x1
popq %r14
leaq -0xb8(%rbp), %rdi
callq 0x3120
movl %r14d, %eax
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x3120
jmp 0x6310
jmp 0x630d
jmp 0x630d
jmp 0x630d
jmp 0x630d
jmp 0x6306
movq %rax, %rbx
jmp 0x6319
jmp 0x6328
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x3120
leaq -0x38(%rbp), %rdi
callq 0x3120
jmp 0x632b
jmp 0x6328
jmp 0x6328
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x3120
movq %rbx, %rdi
callq 0x34b0