// File: /workspace/github_cpp_makefile_star2_O3/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 0x747b
movq 0xab9b(%rip), %rbx      # 0x11fd0
leaq 0x7492(%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 0x7a02
movq %rdi, %r14
leaq -0xb8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x7681(%rip), %rdx      # 0xeb12
leaq -0xc8(%rbp), %rdi
movq %rdx, %rsi
callq 0x9ff0
leaq 0x5b9d(%rip), %rdi      # 0xd044
callq 0x3340
movq %rax, %rbx
testq %rax, %rax
jne 0x74c4
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 0x750b
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 0x7588
xorl %r14d, %r14d
cmpq $0x1, %rcx
jne 0x79e7
movq %rbx, %rdi
callq 0x3420
cmpl $-0x1, %eax
jne 0x79e7
callq 0x3060
movl (%rax), %r14d
movq 0xaa84(%rip), %r15      # 0x11fd0
leaq 0x7394(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
movq %r15, %rdi
callq 0x3300
testq %rbx, %rbx
je 0x77c3
movq %rbx, %rdi
callq 0x3130
movq 0xaa58(%rip), %rdi      # 0x11fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3300
jmp 0x77db
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x9f52
leaq 0x62bb(%rip), %rsi      # 0xd866
leaq -0x48(%rbp), %rdi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x3520
cmpq $-0x1, %rax
je 0x75e0
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 0x7523(%rip), %rdx      # 0xeb12
leaq -0x68(%rbp), %rdi
movq %rdx, %rsi
callq 0x9ff0
leaq -0x10d0(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3160
testq %rax, %rax
je 0x76f7
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 0x9ff0
leaq -0x68(%rbp), %rdi
leaq -0xa8(%rbp), %rbx
movq %rbx, %rsi
callq 0x3350
movq (%rbx), %rdi
cmpq %r14, %rdi
je 0x7673
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
cmpb $0x2f, (%rdi)
jne 0x774d
callq 0x3420
cmpl $-0x1, %eax
jne 0x7921
callq 0x3060
movl (%rax), %ebx
movq 0xa934(%rip), %rdi      # 0x11fd0
leaq 0x7244(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
callq 0x3300
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0xa914(%rip), %rdi      # 0x11fd0
callq 0x3300
movq %rax, %r14
leaq 0x7226(%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 0x79b7
callq 0x3060
movl (%rax), %r14d
movq 0xa8ca(%rip), %rbx      # 0x11fd0
leaq 0x71da(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
movq %rbx, %rdi
callq 0x3300
movl %r14d, %edi
callq 0x30f0
testq %rax, %rax
je 0x786f
movq %rax, %r14
movq %rax, %rdi
callq 0x3130
movq 0xa893(%rip), %rdi      # 0x11fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3300
jmp 0x7886
leaq -0x78(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x88(%rbp), %rdi
callq 0x9f52
leaq 0x7181(%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 0x78b6
movq %rdx, -0xa8(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x98(%rbp)
jmp 0x78bd
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x3500
movq 0xa7ee(%rip), %rbx      # 0x11fd0
leaq 0x7108(%rip), %rsi      # 0xe8f1
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movl %r14d, %edi
callq 0x30f0
testq %rax, %rax
je 0x7822
movq %rax, %r14
movq %rax, %rdi
callq 0x3130
movq 0xa7bb(%rip), %rdi      # 0x11fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3300
jmp 0x7839
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 0xa779(%rip), %rdi      # 0x11fd0
callq 0x3040
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x31b0
jmp 0x79e7
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 0xa72c(%rip), %rdi      # 0x11fd0
callq 0x3040
movq %rax, %rdi
callq 0x31b0
jmp 0x79b7
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 0x78f7
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x88(%rbp), %rdi
cmpq %r15, %rdi
je 0x790f
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x68(%rbp), %rdi
callq 0x3420
cmpl $-0x1, %eax
leaq -0x38(%rbp), %r15
je 0x7953
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x7936
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x794b
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x32c0
xorl %r14d, %r14d
jmp 0x79e7
callq 0x3060
movl (%rax), %ebx
movq 0xa66f(%rip), %rdi      # 0x11fd0
leaq 0x6f7f(%rip), %rsi      # 0xe8e7
movl $0xc, %edx
callq 0x3300
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0xa64f(%rip), %rdi      # 0x11fd0
callq 0x3300
movq %rax, %r14
leaq 0x6f61(%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 0x79cc
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x79e1
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x32c0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x7a02
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 0x7a19
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r15, %rdi
je 0x7a4c
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x32c0
jmp 0x7a4c
jmp 0x7a49
jmp 0x7a49
jmp 0x7a49
jmp 0x7a49
jmp 0x7a40
movq %rax, %rbx
jmp 0x7a61
jmp 0x7a7e
jmp 0x7a7e
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x7a61
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x32c0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x7a81
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x32c0
jmp 0x7a81
jmp 0x7a7e
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x7a9c
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x32c0
movq %rbx, %rdi
callq 0x3530