// File: /workspace/github_cpp_makefile_star2_O1/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 0x1d12(%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 0x346a
cmpq $0x1, 0x28(%rsi)
jne 0x33f2
movq 0x20(%rsi), %rcx
cmpb $0x3e, (%rcx)
jne 0x33f2
cmpq $0x3, %rax
je 0x3457
cmpq $0x2, %rax
jne 0x33f2
movq 0x3c25(%rip), %rdi      # 0x6fc8
leaq 0x1cd7(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x3c0d(%rip), %rdi      # 0x6fc8
leaq 0x1cc7(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x3bf5(%rip), %rdi      # 0x6fc8
leaq 0x1cb7(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movl $0x1f, %edx
leaq 0x1cf4(%rip), %rsi      # 0x50e4
jmp 0x3446
movq 0x3bcf(%rip), %rdi      # 0x6fc8
leaq 0x1c81(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x3bb7(%rip), %rdi      # 0x6fc8
leaq 0x1c71(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x3b9f(%rip), %rdi      # 0x6fc8
leaq 0x1c61(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movl $0x14, %edx
leaq 0x1c70(%rip), %rsi      # 0x50b6
movq 0x3b7b(%rip), %rdi      # 0x6fc8
callq 0x21b0
jmp 0x3549
addq $0x40, %rsi
movq %rsp, %rdi
callq 0x20c0
movb $0x1, 0x3d56(%rip)      # 0x71c0
cmpb $0x1, 0x3d4f(%rip)      # 0x71c0
jne 0x34b2
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, 0x3d1e(%rip)      # 0x71b8
movl $0x1, %edi
callq 0x2320
movl %ebx, %edi
callq 0x2280
movl %ebx, %edi
callq 0x2320
movq 0x3b17(%rip), %r13      # 0x6fd0
movq (%r13), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x350f
movl $0x8, %ebp
movq 0x3af7(%rip), %rbx      # 0x6fc8
leaq 0x1c71(%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 0x34d8
cmpb $0x1, 0x3caa(%rip)      # 0x71c0
jne 0x3549
movq 0x3a89(%rip), %rax      # 0x6fa8
movq (%rax), %rdi
callq 0x21c0
movl 0x3c8b(%rip), %edi      # 0x71b8
movl $0x1, %esi
callq 0x2150
movl 0x3c7b(%rip), %edi      # 0x71b8
callq 0x2320
movb $0x0, 0x3c77(%rip)      # 0x71c0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x355f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21a0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3577
jmp 0x3577
jmp 0x3577
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x3590
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0