// File: 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
subq $0x1200, %rsp           # imm = 0x1200
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xb7a0
cmpq $0x2, %rax
jbe 0x87fc
movq 0x137f3(%rip), %rdi     # 0x1bfc8
leaq 0xd0f2(%rip), %rsi      # 0x158ce
callq 0x32e0
movq %rax, %rdi
movq 0x137bd(%rip), %rsi     # 0x1bfa8
callq 0x3330
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8e7d
movq $0x0, -0x18(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x1150(%rbp)
callq 0x3590
movq -0x1150(%rbp), %rdx
leaq 0xd2ac(%rip), %rsi      # 0x15ace
leaq -0x38(%rbp), %rdi
callq 0x3440
jmp 0x882d
leaq -0x39(%rbp), %rdi
callq 0x3340
leaq 0xb807(%rip), %rdi      # 0x14044
callq 0x33a0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x889d
callq 0x3150
movl %eax, %edi
callq 0x3370
movq %rax, -0x1158(%rbp)
jmp 0x8861
movq -0x1158(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x889d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x3340
jmp 0x8e89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8e72
movq -0x18(%rbp), %rax
movq %rax, -0x1168(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x1160(%rbp)
callq 0x3590
movq -0x1168(%rbp), %rsi
movq -0x1160(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x3440
jmp 0x88d1
leaq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x33b0
leaq -0x70(%rbp), %rdi
callq 0x31e0
leaq -0x71(%rbp), %rdi
callq 0x3340
movq -0x10(%rbp), %rdi
callq 0xb7a0
cmpq $0x1, %rax
jne 0x89d7
movq -0x18(%rbp), %rdi
callq 0x34c0
cmpl $-0x1, %eax
jne 0x89d2
callq 0x3060
movl (%rax), %eax
movl %eax, -0x78(%rbp)
movq 0x136a2(%rip), %rdi     # 0x1bfc8
leaq 0xcfba(%rip), %rsi      # 0x158e7
callq 0x32e0
movq %rax, -0x1170(%rbp)
jmp 0x893b
movq -0x1170(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x32e0
movq %rax, -0x1178(%rbp)
jmp 0x8954
movq -0x1178(%rbp), %rdi
leaq 0xcf8f(%rip), %rsi      # 0x158f1
callq 0x32e0
movq %rax, -0x1180(%rbp)
jmp 0x8970
movl -0x78(%rbp), %edi
callq 0x30e0
movq -0x1180(%rbp), %rdi
movq %rax, %rsi
callq 0x32e0
movq %rax, -0x1188(%rbp)
jmp 0x8990
movq -0x1188(%rbp), %rdi
movq 0x1360a(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x89a5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x7c(%rbp)
jmp 0x8e67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x3340
jmp 0x8e72
jmp 0x8e59
movq -0x10(%rbp), %rdi
callq 0xb7a0
cmpq $0x2, %rax
jne 0x8e57
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xbb00
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3140
jmp 0x8a09
leaq -0xa0(%rbp), %rdi
leaq 0xbe4f(%rip), %rsi      # 0x14866
xorl %eax, %eax
movl %eax, %edx
callq 0x3600
movq %rax, -0xa8(%rbp)
cmpq $-0x1, %rax
je 0x8a5e
movq -0xa8(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
movl $0x1, %edx
leaq -0x38(%rbp), %rcx
callq 0x3110
jmp 0x8a4b
jmp 0x8a5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8e49
leaq -0x10d1(%rbp), %rdi
movq %rdi, -0x1190(%rbp)
callq 0x3590
movq -0x1190(%rbp), %rdx
leaq 0xd04f(%rip), %rsi      # 0x15ace
leaq -0x10d0(%rbp), %rdi
callq 0x3440
jmp 0x8a8d
leaq -0x10d1(%rbp), %rdi
callq 0x3340
leaq -0x10b0(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3160
cmpq $0x0, %rax
jne 0x8b56
callq 0x3060
movl (%rax), %eax
movl %eax, -0x10d8(%rbp)
movq 0x13500(%rip), %rdi     # 0x1bfc8
leaq 0xce18(%rip), %rsi      # 0x158e7
callq 0x32e0
movq %rax, -0x1198(%rbp)
jmp 0x8add
movl -0x10d8(%rbp), %edi
callq 0x30e0
movq -0x1198(%rbp), %rdi
movq %rax, %rsi
callq 0x32e0
movq %rax, -0x11a0(%rbp)
jmp 0x8b00
movq -0x11a0(%rbp), %rdi
movq 0x1349a(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x8b15
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x7c(%rbp)
jmp 0x8e1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x10d1(%rbp), %rdi
callq 0x3340
jmp 0x8e49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8e3d
leaq -0x10f9(%rbp), %rdi
movq %rdi, -0x11a8(%rbp)
callq 0x3590
movq -0x11a8(%rbp), %rdx
leaq -0x10f8(%rbp), %rdi
leaq -0x10b0(%rbp), %rsi
callq 0x3440
jmp 0x8b85
leaq -0x10d0(%rbp), %rdi
leaq -0x10f8(%rbp), %rsi
movq %rsi, -0x11b8(%rbp)
callq 0x33b0
movq -0x11b8(%rbp), %rdi
callq 0x31e0
leaq -0x10f9(%rbp), %rdi
callq 0x3340
xorl %eax, %eax
movl %eax, %esi
leaq -0xa0(%rbp), %rdi
callq 0x3610
movq %rax, -0x11b0(%rbp)
jmp 0x8bd0
movq -0x11b0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x8cce
leaq -0xa0(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
callq 0x34c0
cmpl $-0x1, %eax
jne 0x8cc9
callq 0x3060
movl (%rax), %eax
movl %eax, -0x1100(%rbp)
movq 0x133b4(%rip), %rdi     # 0x1bfc8
leaq 0xcccc(%rip), %rsi      # 0x158e7
callq 0x32e0
movq %rax, -0x11c0(%rbp)
jmp 0x8c29
movq -0x11c0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x32a0
movq %rax, -0x11c8(%rbp)
jmp 0x8c45
movq -0x11c8(%rbp), %rdi
leaq 0xcc9e(%rip), %rsi      # 0x158f1
callq 0x32e0
movq %rax, -0x11d0(%rbp)
jmp 0x8c61
movl -0x1100(%rbp), %edi
callq 0x30e0
movq -0x11d0(%rbp), %rdi
movq %rax, %rsi
callq 0x32e0
movq %rax, -0x11d8(%rbp)
jmp 0x8c84
movq -0x11d8(%rbp), %rdi
movq 0x13316(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x8c99
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x7c(%rbp)
jmp 0x8e1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x10f9(%rbp), %rdi
callq 0x3340
jmp 0x8e3d
jmp 0x8e13
leaq 0xcc1f(%rip), %rdx      # 0x158f4
leaq -0x1140(%rbp), %rdi
leaq -0x10d0(%rbp), %rsi
callq 0xb310
jmp 0x8cea
leaq -0x1120(%rbp), %rdi
leaq -0x1140(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0xb2c0
jmp 0x8d06
leaq -0x10d0(%rbp), %rdi
leaq -0x1120(%rbp), %rsi
callq 0x33b0
leaq -0x1120(%rbp), %rdi
callq 0x31e0
leaq -0x1140(%rbp), %rdi
callq 0x31e0
leaq -0x10d0(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
callq 0x34c0
cmpl $-0x1, %eax
jne 0x8e11
callq 0x3060
movl (%rax), %eax
movl %eax, -0x1144(%rbp)
movq 0x13266(%rip), %rdi     # 0x1bfc8
leaq 0xcb7e(%rip), %rsi      # 0x158e7
callq 0x32e0
movq %rax, -0x11e0(%rbp)
jmp 0x8d77
movq -0x11e0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x32a0
movq %rax, -0x11e8(%rbp)
jmp 0x8d93
movq -0x11e8(%rbp), %rdi
leaq 0xcb50(%rip), %rsi      # 0x158f1
callq 0x32e0
movq %rax, -0x11f0(%rbp)
jmp 0x8daf
movl -0x1144(%rbp), %edi
callq 0x30e0
movq -0x11f0(%rbp), %rdi
movq %rax, %rsi
callq 0x32e0
movq %rax, -0x11f8(%rbp)
jmp 0x8dd2
movq -0x11f8(%rbp), %rdi
movq 0x131c8(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x8de7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x7c(%rbp)
jmp 0x8e1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1140(%rbp), %rdi
callq 0x31e0
jmp 0x8e3d
jmp 0x8e13
movl $0x0, -0x7c(%rbp)
leaq -0x10d0(%rbp), %rdi
callq 0x31e0
leaq -0xa0(%rbp), %rdi
callq 0x31e0
movl -0x7c(%rbp), %eax
testl %eax, %eax
jne 0x8e67
jmp 0x8e3b
jmp 0x8e57
leaq -0x10d0(%rbp), %rdi
callq 0x31e0
leaq -0xa0(%rbp), %rdi
callq 0x31e0
jmp 0x8e72
jmp 0x8e59
movl $0x0, -0x4(%rbp)
movl $0x1, -0x7c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31e0
jmp 0x8e7d
leaq -0x38(%rbp), %rdi
callq 0x31e0
jmp 0x8e89
movl -0x4(%rbp), %eax
addq $0x1200, %rsp           # imm = 0x1200
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3580
nopw %cs:(%rax,%rax)