// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x10a8, %rsp           # imm = 0x10A8
movq 0x8(%rdi), %rax
subq (%rdi), %rax
cmpq $0x41, %rax
jb 0x7461
movq 0xabb5(%rip), %rbx      # 0x11fd0
leaq 0x74ac(%rip), %rsi      # 0xe8ce
movl $0x18, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x79e8
movq %rdi, %r14
leaq -0xb8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x769b(%rip), %rdx      # 0xeb12
leaq -0xc8(%rbp), %rdi
movq %rdx, %rsi
callq 0xa038
leaq 0x5bb7(%rip), %rdi      # 0xd044
callq 0x3340
movq %rax, %rbx
testq %rax, %rax
jne 0x74aa
callq 0x3150
movl %eax, %edi
callq 0x3310
movq 0x20(%rax), %rbx
leaq -0x10d0(%rbp), %rdi
leaq -0x48(%rbp), %rdx
movq %rbx, %rsi
callq 0x3110
leaq -0xc8(%rbp), %rdi
leaq -0x10d0(%rbp), %r15
movq %r15, %rsi
callq 0x3350
movq (%r15), %rdi
leaq -0x10c0(%rbp), %rax
cmpq %rax, %rdi
je 0x74f1
movq -0x10c0(%rbp), %rsi
incq %rsi
callq 0x32c0
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq $0x2, %rcx
je 0x756e
xorl %r14d, %r14d
cmpq $0x1, %rcx
jne 0x79cd
movq %rbx, %rdi
callq 0x3420
cmpl $-0x1, %eax
jne 0x79cd
callq 0x3060
movl (%rax), %r14d
movq 0xaa9e(%rip), %r15      # 0x11fd0
leaq 0x73ae(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
movq %r15, %rdi
callq 0x3300
testq %rbx, %rbx
je 0x77a9
movq %rbx, %rdi
callq 0x3130
movq 0xaa72(%rip), %rdi      # 0x11fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3300
jmp 0x77c1
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x9f9e
leaq 0x62d5(%rip), %rsi      # 0xd866
leaq -0x48(%rbp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x3520
cmpq $-0x1, %rax
je 0x75c6
movq -0xc8(%rbp), %rcx
movq -0xc0(%rbp), %r8
leaq -0x48(%rbp), %rdi
movl $0x1, %edx
movq %rax, %rsi
callq 0x32f0
leaq -0x58(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x753d(%rip), %rdx      # 0xeb12
leaq -0x68(%rbp), %rdi
movq %rdx, %rsi
callq 0xa038
leaq -0x10d0(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3160
testq %rax, %rax
je 0x76dd
leaq -0x98(%rbp), %r14
movq %r14, -0x10(%r14)
leaq -0x10d0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3130
leaq (%rax,%rbp), %rdx
addq $-0x10d0, %rdx          # imm = 0xEF30
leaq -0xa8(%rbp), %rdi
movq %rbx, %rsi
callq 0xa038
leaq -0x68(%rbp), %rdi
leaq -0xa8(%rbp), %rbx
movq %rbx, %rsi
callq 0x3350
movq (%rbx), %rdi
cmpq %r14, %rdi
je 0x7659
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
cmpb $0x2f, (%rdi)
jne 0x7733
callq 0x3420
cmpl $-0x1, %eax
jne 0x7907
callq 0x3060
movl (%rax), %ebx
movq 0xa94e(%rip), %rdi      # 0x11fd0
leaq 0x725e(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
callq 0x3300
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0xa92e(%rip), %rdi      # 0x11fd0
callq 0x3300
movq %rax, %r14
leaq 0x7240(%rip), %rsi      # 0xe8f1
movl $0x2, %edx
movq %rax, %rdi
callq 0x3300
movl %ebx, %edi
callq 0x30f0
movq %r14, %rdi
movq %rax, %rsi
callq 0x32a0
movq %rax, %rdi
callq 0x3140
jmp 0x799d
callq 0x3060
movl (%rax), %r14d
movq 0xa8e4(%rip), %rbx      # 0x11fd0
leaq 0x71f4(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
movq %rbx, %rdi
callq 0x3300
movl %r14d, %edi
callq 0x30f0
testq %rax, %rax
je 0x7855
movq %rax, %r14
movq %rax, %rdi
callq 0x3130
movq 0xa8ad(%rip), %rdi      # 0x11fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3300
jmp 0x786c
leaq -0x78(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x88(%rbp), %rdi
callq 0x9f9e
leaq 0x719b(%rip), %rsi      # 0xe8f4
leaq -0x88(%rbp), %rdi
callq 0x3570
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x30c0
movq %r14, -0xa8(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x789c
movq %rdx, -0xa8(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x98(%rbp)
jmp 0x78a3
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x3500
movq 0xa808(%rip), %rbx      # 0x11fd0
leaq 0x7122(%rip), %rsi      # 0xe8f1
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movl %r14d, %edi
callq 0x30f0
testq %rax, %rax
je 0x7808
movq %rax, %r14
movq %rax, %rdi
callq 0x3130
movq 0xa7d5(%rip), %rdi      # 0x11fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3300
jmp 0x781f
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3500
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq 0xa793(%rip), %rdi      # 0x11fd0
callq 0x3040
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x31b0
jmp 0x79cd
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3500
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq 0xa746(%rip), %rdi      # 0x11fd0
callq 0x3040
movq %rax, %rdi
callq 0x31b0
jmp 0x799d
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rbx)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0x68(%rbp), %rdi
movq %rbx, %rsi
callq 0x3350
movq (%rbx), %rdi
cmpq %r14, %rdi
je 0x78dd
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x88(%rbp), %rdi
cmpq %r15, %rdi
je 0x78f5
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x68(%rbp), %rdi
callq 0x3420
cmpl $-0x1, %eax
leaq -0x38(%rbp), %r15
je 0x7939
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x791c
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x7931
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x32c0
xorl %r14d, %r14d
jmp 0x79cd
callq 0x3060
movl (%rax), %ebx
movq 0xa689(%rip), %rdi      # 0x11fd0
leaq 0x6f99(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
callq 0x3300
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0xa669(%rip), %rdi      # 0x11fd0
callq 0x3300
movq %rax, %r14
leaq 0x6f7b(%rip), %rsi      # 0xe8f1
movl $0x2, %edx
movq %rax, %rdi
callq 0x3300
movl %ebx, %edi
callq 0x30f0
movq %r14, %rdi
movq %rax, %rsi
callq 0x32a0
movq %rax, %rdi
callq 0x3140
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x79b2
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x79c7
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x32c0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x79e8
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x32c0
movl %r14d, %eax
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x79ff
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r15, %rdi
je 0x7a32
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x32c0
jmp 0x7a32
jmp 0x7a2f
jmp 0x7a2f
jmp 0x7a2f
jmp 0x7a2f
jmp 0x7a26
movq %rax, %rbx
jmp 0x7a47
jmp 0x7a64
jmp 0x7a64
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x7a47
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x7a67
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x32c0
jmp 0x7a67
jmp 0x7a64
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x7a82
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x32c0
movq %rbx, %rdi
callq 0x3530