// File: /workspace/github_cpp_makefile_star2_O3/sanaa-khan[P]mini-linux-shell/gbsh.cpp
// Function: listEnvVars(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>>>>)
void listEnvVars(vector<string> tokens) {

	string filename;

	char cwd[100];
	getcwd(cwd, 100);

	setenv("SHELL", cwd, 1);

	// for output redirection
	if (tokens.size() > 1) {
		if (tokens[1].size() == 1 and tokens[1][0] == '>') {
			if (tokens.size() == 2) {
				cout << "\033[1;31m" << "Error: ";
				cout << "\033[0;38m" << "Output file name not provided.\n";
				return;
			}

			else if (tokens.size() == 3) {
				filename = tokens[2];
				oRedirect = true;
			}

			else {
				cout << "\033[1;31m" << "Error: ";
				cout << "\033[0;38m" << "Too many arguments.\n";
				return;
			}
		}

		else {
			cout << "\033[1;31m" << "Error: ";
			cout << "\033[0;38m" << "Too many arguments.\n";
			return;
		}
	}

	if (oRedirect) {
		int file_desc = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777);
		output = dup(1);

		close(1);
		dup(file_desc);
		close(file_desc);
	}

	// outputting variables
	int index = 0;
	char* current = environ[index];

	while (current != NULL) {
		cout << current << "\n";
		current = environ[++index];
	}

	// redirecting output back
	if (oRedirect) {
		fflush(stdout); 

		dup2(output, 1);
		close(output);

		oRedirect = false;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x20(%rsp), %r14
movl $0x64, %esi
movq %r14, %rdi
callq 0x20a0
leaq 0x1d00(%rip), %rdi      # 0x5069
movq %r14, %rsi
movl $0x1, %edx
callq 0x2270
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq $0x2, %rax
jb 0x3413
cmpq $0x1, 0x28(%rsi)
jne 0x34f8
movq 0x20(%rsi), %rcx
cmpb $0x3e, (%rcx)
jne 0x34f8
cmpq $0x3, %rax
je 0x3580
cmpq $0x2, %rax
jne 0x34f8
movq 0x3c07(%rip), %rdi      # 0x6fc8
leaq 0x1cb9(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x3bef(%rip), %rdi      # 0x6fc8
leaq 0x1ca9(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x3bd7(%rip), %rdi      # 0x6fc8
leaq 0x1c99(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movl $0x1f, %edx
leaq 0x1cd6(%rip), %rsi      # 0x50e4
jmp 0x354c
testb $0x1, 0x3da6(%rip)      # 0x71c0
je 0x345b
movq (%rsp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x20e0
movl %eax, %ebx
movl $0x1, %edi
callq 0x2280
movl %eax, 0x3d75(%rip)      # 0x71b8
movl $0x1, %edi
callq 0x2320
movl %ebx, %edi
callq 0x2280
movl %ebx, %edi
callq 0x2320
movq 0x3b6e(%rip), %r13      # 0x6fd0
movq (%r13), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x34b8
movl $0x8, %ebp
movq 0x3b4e(%rip), %rbx      # 0x6fc8
leaq 0x1cc8(%rip), %r14      # 0x5149
movq %r15, %rdi
callq 0x2090
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x21b0
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21b0
movq (%r13), %rax
movq (%rax,%rbp), %r15
addq $0x8, %rbp
testq %r15, %r15
jne 0x3481
cmpb $0x1, 0x3d01(%rip)      # 0x71c0
jne 0x3558
movq 0x3adc(%rip), %rax      # 0x6fa8
movq (%rax), %rdi
callq 0x21c0
movl 0x3cde(%rip), %edi      # 0x71b8
movl $0x1, %esi
callq 0x2150
movl 0x3cce(%rip), %edi      # 0x71b8
callq 0x2320
movb $0x0, 0x3cca(%rip)      # 0x71c0
jmp 0x3558
movq 0x3ac9(%rip), %rdi      # 0x6fc8
leaq 0x1b7b(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x3ab1(%rip), %rdi      # 0x6fc8
leaq 0x1b6b(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x3a99(%rip), %rdi      # 0x6fc8
leaq 0x1b5b(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movl $0x14, %edx
leaq 0x1b6a(%rip), %rsi      # 0x50b6
movq 0x3a75(%rip), %rdi      # 0x6fc8
callq 0x21b0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x356e
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21a0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x40, %rsi
movq %rsp, %rdi
callq 0x20c0
movb $0x1, 0x3c2d(%rip)      # 0x71c0
jmp 0x341c
jmp 0x359e
jmp 0x359e
jmp 0x359e
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x35b7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0