// File: /workspace/github_c_makefile_star2_O0/s3lase[P]v6shell/osh.c
// Function: do_chdir
static void
do_chdir(char **av)
{
	const char *emsg, *home;
	int cwd;
	static int pwd = -1;

	emsg = ERR_BADDIR;

	cwd = open(".", O_RDONLY | O_NONBLOCK);

	if (av[1] == NULL) {
		home = getenv("HOME");
		if (home == NULL || *home == EOS) {
			emsg = ERR_NOHOMEDIR;
			goto chdirerr;
		}
		if (chdir(home) == -1)
			goto chdirerr;
	} else if (EQUAL(av[1], "-")) {
		if (pwd == -1) {
			emsg = ERR_NOPWD;
			goto chdirerr;
		}
		if (!fd_type(pwd, FD_ISDIROK) || fchdir(pwd) == -1) {
			if (close(pwd) != -1)
				pwd = -1;
			goto chdirerr;
		}
	} else if (chdir(atrim(UCPTR(av[1]))) == -1)
		goto chdirerr;

	if (cwd != -1) {
		if (cwd != PWD && (pwd = dup2(cwd, PWD)) == PWD)
			(void)fcntl(pwd, F_SETFD, FD_CLOEXEC);
		(void)close(cwd);
	} else if (close(pwd) != -1)
		pwd = -1;
	status = SH_TRUE;
	return;

chdirerr:
	if (cwd != -1)
		(void)close(cwd);
	err(-1, FMT3S, getmyname(), av[0], emsg);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
leaq 0x3dec(%rip), %rax      # 0xb89f
movq %rax, -0x10(%rbp)
leaq 0x41bf(%rip), %rdi      # 0xbc7d
movl $0x800, %esi            # imm = 0x800
movb $0x0, %al
callq 0x2320
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x7b23
leaq 0x3c37(%rip), %rdi      # 0xb716
callq 0x2030
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x7afb
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x7b0b
leaq 0x3dab(%rip), %rax      # 0xb8ad
movq %rax, -0x10(%rbp)
jmp 0x7c3f
movq -0x18(%rbp), %rdi
callq 0x2140
cmpl $-0x1, %eax
jne 0x7b1e
jmp 0x7c3f
jmp 0x7bd2
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movsbl (%rax), %eax
movsbl 0x3d8a(%rip), %ecx      # 0xb8bf
cmpl %ecx, %eax
jne 0x7bb2
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3d77(%rip), %rsi      # 0xb8bf
callq 0x2230
cmpl $0x0, %eax
jne 0x7bb2
cmpl $-0x1, 0x66a7(%rip)     # 0xe200
jne 0x7b6b
leaq 0x3d5f(%rip), %rax      # 0xb8c1
movq %rax, -0x10(%rbp)
jmp 0x7c3f
movl 0x668f(%rip), %edi      # 0xe200
movl $0x2, %esi
callq 0x2b90
testb $0x1, %al
jne 0x7b81
jmp 0x7b91
movl 0x6679(%rip), %edi      # 0xe200
callq 0x22f0
cmpl $-0x1, %eax
jne 0x7bb0
movl 0x6669(%rip), %edi      # 0xe200
callq 0x21d0
cmpl $-0x1, %eax
je 0x7bab
movl $0xffffffff, 0x6655(%rip) # imm = 0xFFFFFFFF
jmp 0x7c3f
jmp 0x7bd0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x6f20
movq %rax, %rdi
callq 0x2140
cmpl $-0x1, %eax
jne 0x7bce
jmp 0x7c3f
jmp 0x7bd0
jmp 0x7bd2
cmpl $-0x1, -0x1c(%rbp)
je 0x7c17
cmpl $0xc, -0x1c(%rbp)
je 0x7c0d
movl -0x1c(%rbp), %edi
movl $0xc, %esi
callq 0x2160
movl %eax, 0x660f(%rip)      # 0xe200
cmpl $0xc, %eax
jne 0x7c0d
movl 0x6604(%rip), %edi      # 0xe200
movl $0x2, %esi
movl $0x1, %edx
movb $0x0, %al
callq 0x20d0
movl -0x1c(%rbp), %edi
callq 0x21d0
jmp 0x7c33
movl 0x65e3(%rip), %edi      # 0xe200
callq 0x21d0
cmpl $-0x1, %eax
je 0x7c31
movl $0xffffffff, 0x65cf(%rip) # imm = 0xFFFFFFFF
jmp 0x7c33
movl $0x0, 0x662b(%rip)      # 0xe268
jmp 0x7c73
cmpl $-0x1, -0x1c(%rbp)
je 0x7c4d
movl -0x1c(%rbp), %edi
callq 0x21d0
callq 0x8df0
movq %rax, %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %r8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x3b31(%rip), %rsi      # 0xb79d
movb $0x0, %al
callq 0x8af0
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)