// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x90, %rsp
movq %rdi, %rbx
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x20(%rsp), %r14
pushq $0x64
popq %rsi
movq %r14, %rdi
callq 0x20b0
leaq 0x2035(%rip), %rdi      # 0x5069
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0x2270
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq $0x2, %rax
jb 0x30c0
cmpq $0x1, 0x28(%rsi)
jne 0x3182
movq 0x20(%rsi), %rcx
cmpb $0x3e, (%rcx)
jne 0x3182
cmpq $0x3, %rax
je 0x31de
cmpq $0x2, %rax
jne 0x3182
movq 0x3f42(%rip), %rdi      # 0x6fc8
leaq 0x1ff4(%rip), %rsi      # 0x5081
callq 0x2170
leaq 0x1ff0(%rip), %rsi      # 0x5089
movq %rax, %rdi
callq 0x2170
movq 0x3f20(%rip), %rdi      # 0x6fc8
leaq 0x1fe2(%rip), %rsi      # 0x5091
callq 0x2170
leaq 0x2029(%rip), %rsi      # 0x50e4
jmp 0x31be
testb $0x1, 0x40e9(%rip)      # 0x71b0
je 0x3105
movq (%rsp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x20e0
movl %eax, %ebx
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x2280
movl %eax, 0x40b8(%rip)      # 0x71a8
movl %ebp, %edi
callq 0x2300
movl %ebx, %edi
callq 0x2280
movl %ebx, %edi
callq 0x2300
movq 0x3ec4(%rip), %r15      # 0x6fd0
movq (%r15), %rax
pushq $0x8
popq %r12
movq 0x3eae(%rip), %rbx      # 0x6fc8
leaq 0x2028(%rip), %r14      # 0x5149
movq (%rax), %rsi
testq %rsi, %rsi
je 0x3148
movq %rbx, %rdi
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %r12, %rax
addq $0x8, %r12
addq (%r15), %rax
jmp 0x3121
cmpb $0x1, 0x4061(%rip)      # 0x71b0
jne 0x31c6
movq 0x3e50(%rip), %rax      # 0x6fa8
movq (%rax), %rdi
callq 0x21a0
movl 0x4042(%rip), %edi      # 0x71a8
pushq $0x1
popq %rsi
callq 0x2150
movl 0x4034(%rip), %edi      # 0x71a8
callq 0x2300
movb $0x0, 0x4030(%rip)      # 0x71b0
jmp 0x31c6
movq 0x3e3f(%rip), %rdi      # 0x6fc8
leaq 0x1ef1(%rip), %rsi      # 0x5081
callq 0x2170
leaq 0x1eed(%rip), %rsi      # 0x5089
movq %rax, %rdi
callq 0x2170
movq 0x3e1d(%rip), %rdi      # 0x6fc8
leaq 0x1edf(%rip), %rsi      # 0x5091
callq 0x2170
leaq 0x1ef8(%rip), %rsi      # 0x50b6
movq %rax, %rdi
callq 0x2170
movq %rsp, %rdi
callq 0x20a0
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
addq $0x40, %rsi
movq %rsp, %rdi
callq 0x20d0
movb $0x1, 0x3fbf(%rip)      # 0x71b0
jmp 0x30c9
jmp 0x31fc
jmp 0x31fc
jmp 0x31fc
movq %rax, %rbx
movq %rsp, %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x22e0