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

	bool outputRedirection = false, inputRedirection = false;

	for (int i = 0; i < tokens.size(); i++) { // detecting if output/input redirection required

		if (tokens[i].size() == 1) {

			if (tokens[i][0] == '>')
				outputRedirection = true;

			else if (tokens[i][0] == '<')
				inputRedirection = true;
		}
	}

	string cmd = tokens[0];
	int noOfArguments = 0;
	string outputFile, inputFile;
	char** arg;

	if (!inputRedirection and outputRedirection) { 
		for (int i = 1; i < tokens.size(); i++) {
			if (tokens[i].size() == 1 and tokens[i][0] == '>') {
				if (i == tokens.size() - 1) {
					cout << "\033[1;31m" << "Error: ";
					cout << "\033[0;38m" << "Output file not provided.\n";
					return;
				}

				else {
					outputFile = tokens[i + 1];
					break;
				}
			}

			else {
				noOfArguments++;
			}
		}

		arg = new char*[noOfArguments + 2];
		arg[0] = new char[20];

		strcpy(arg[0], cmd.c_str());
		arg[noOfArguments + 1] = nullptr;

		for (int i = 1; i <= noOfArguments; i++) {
			arg[i] = new char[20];
			strcpy(arg[i], tokens[i].c_str());
		}

		int file_desc = open(outputFile.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777);
		output = dup(1);

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

		oRedirect = true;
	}

	else if (inputRedirection and outputRedirection) {
		int i;
		for (i = 1; i < tokens.size(); i++) {
			if (tokens[i].size() == 1 and tokens[i][0] == '<') {
				if (i == tokens.size() - 1) {
					cout << "\033[1;31m" << "Error: ";
					cout << "\033[0;38m" << "Input file not provided.\n";
					return;
				}

				else {
					i++;
					inputFile = tokens[i];
					i++;
					break;
				}
			}

			else {
				noOfArguments++;
			}
		}

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

				else {
					outputFile = tokens[i + 1];
					break;
				}
			}
		}

		arg = new char*[noOfArguments + 2];
		arg[0] = new char[20];

		strcpy(arg[0], cmd.c_str());
		arg[noOfArguments + 1] = nullptr;

		for (int j = 1; j <= noOfArguments; j++) {
			arg[j] = new char[20];
			strcpy(arg[j], tokens[j].c_str());
		}

		int file_desc = open(outputFile.c_str(), O_WRONLY | O_CREAT |  O_TRUNC, 0777);
		output = dup(1);

		close(1);
		dup(file_desc);
		close(file_desc);
		
		oRedirect = true;

		int file_desc2 = open(inputFile.c_str(), O_WRONLY | 0777);
		input = dup(0);

		close(0);
		dup(file_desc2);
		close(file_desc2);

		iRedirect = true;
	}

	else if (!inputRedirection and !outputRedirection) {
		noOfArguments = tokens.size() - 1;
		char* arg[noOfArguments + 2];

		for (int i = 0; i <= noOfArguments; i++)
			arg[i] = new char[20];
			
		strcpy(arg[0], cmd.c_str());

		int i;
		for (i = 1; i <= noOfArguments; i++) {
			strcpy(arg[i], tokens[i].c_str());
		}

		arg[i] = NULL;
	}

	if (cmd == "top") {
		char path[50];
		getcwd(path, 50);

		chdir("/usr/bin");
		execl("top", "top", NULL);
		chdir(path);
	}

	else if (cmd == "man") {
		char path[50];
		getcwd(path, 50);

		chdir("/usr/bin");

		if (tokens.size() > 1)
			execl("man", "man", tokens[1].c_str(), NULL);
		else
			execl("man", "man", NULL);

		chdir(path);
	}

	else if (cmd == "ps") {
		char path[50];
		getcwd(path, 50);

		chdir("/usr/bin");

		if (tokens.size() > 1)
			execl("ps", "ps", tokens[1].c_str(), NULL);
		else
			execl("ps", "ps", NULL);

		chdir(path);
	}

	else {
		if (execvp(arg[0], arg) < 0) {
			cout << "\033[1;31m" << "Error: ";
			cout << "\033[0;38m" << "'" << arg[0] << "' not found.\n";
			
			for (int i = 0; i < noOfArguments + 2; i++)
				delete []arg[i];

			exit(0);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
subq %rax, %rcx
je 0x3ba2
sarq $0x5, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
leaq 0x8(%rax), %rdx
xorl %r14d, %r14d
movl $0x1, %esi
xorl %r15d, %r15d
cmpq $0x1, (%rdx)
jne 0x3b97
movq -0x8(%rdx), %rdi
movb (%rdi), %dil
cmpb $0x3e, %dil
jne 0x3b8b
movb $0x1, %r14b
jmp 0x3b97
cmpb $0x3c, %dil
movzbl %r15b, %r15d
cmovel %esi, %r15d
addq $0x20, %rdx
decq %rcx
jne 0x3b73
jmp 0x3ba8
xorl %r15d, %r15d
xorl %r14d, %r14d
leaq -0x40(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x454e
leaq -0x60(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x80(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
testb $0x1, %r15b
je 0x3c40
testb $0x1, %r14b
je 0x3c95
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x5, %rax
xorl %r12d, %r12d
cmpq $0x2, %rax
jb 0x3e4f
leal -0x1(%rax), %r12d
addq $0x40, %rsi
leaq -0x1(%rax), %rcx
xorl %r14d, %r14d
cmpq $0x1, -0x18(%rsi)
jne 0x3c2f
movq -0x20(%rsi), %rdx
cmpb $0x3c, (%rdx)
je 0x3d57
addq $0x20, %rsi
incq %r14
cmpq %r14, %rcx
jne 0x3c1b
jmp 0x3e4f
testb $0x1, %r14b
je 0x3ca6
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x5, %rax
xorl %r13d, %r13d
cmpq $0x2, %rax
jb 0x3f1d
leal -0x1(%rax), %r13d
addq $0x40, %rsi
leaq -0x1(%rax), %rcx
xorl %r14d, %r14d
cmpq $0x1, -0x18(%rsi)
jne 0x3c84
movq -0x20(%rsi), %rdx
cmpb $0x3e, (%rdx)
je 0x3dce
addq $0x20, %rsi
incq %r14
cmpq %r14, %rcx
jne 0x3c70
jmp 0x3f1d
orb %r14b, %r15b
testb $0x1, %r15b
je 0x3ca6
xorl %r12d, %r12d
jmp 0x4100
movq %rsp, -0x98(%rbp)
movq 0x8(%rbx), %r13
subq (%rbx), %r13
sarq $0x5, %r13
leal 0x1(%r13), %eax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movl %r13d, %eax
decl %eax
movq %rax, -0x30(%rbp)
js 0x3cf8
movl %r13d, %r12d
xorl %r14d, %r14d
movl $0x14, %edi
callq 0x2030
movq %rax, (%r15,%r14,8)
incq %r14
cmpq %r14, %r12
jne 0x3ce2
movq (%r15), %rdi
movq -0x50(%rbp), %rsi
callq 0x2160
cmpl $0x0, -0x30(%rbp)
jle 0x3d3a
movl %r13d, %r12d
movl $0x1, %r13d
movl $0x8, %r14d
movq (%r15,%r14), %rdi
movq (%rbx), %rax
movq (%rax,%r14,4), %rsi
callq 0x2160
incq %r13
addq $0x8, %r14
cmpq %r13, %r12
jne 0x3d19
movl %r13d, %eax
jmp 0x3d3f
movl $0x1, %eax
movq $0x0, (%r15,%rax,8)
movq -0x98(%rbp), %rsp
movq -0x30(%rbp), %r12
jmp 0x4100
addq $-0x2, %rax
cmpq %r14, %rax
jne 0x3e40
movq 0x325d(%rip), %rdi      # 0x6fc8
leaq 0x130f(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x3245(%rip), %rdi      # 0x6fc8
leaq 0x12ff(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x322d(%rip), %rdi      # 0x6fc8
leaq 0x12ef(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movq 0x3215(%rip), %rdi      # 0x6fc8
leaq 0x13cd(%rip), %rsi      # 0x5187
movl $0x19, %edx
callq 0x21b0
xorl %eax, %eax
movl %r14d, %r12d
jmp 0x3f0c
addq $-0x2, %rax
cmpq %r14, %rax
jne 0x3f11
movq 0x31e6(%rip), %rdi      # 0x6fc8
leaq 0x1298(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x31ce(%rip), %rdi      # 0x6fc8
leaq 0x1288(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x31b6(%rip), %rdi      # 0x6fc8
leaq 0x1278(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movq 0x319e(%rip), %rdi      # 0x6fc8
leaq 0x1268(%rip), %rsi      # 0x5099
movl $0x1a, %edx
callq 0x21b0
jmp 0x42ea
leaq -0x90(%rbp), %rdi
callq 0x20c0
movl %r14d, %r12d
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq $0x2, %rcx
jb 0x3ff3
leaq 0x28(%rax), %rsi
movq %rcx, %rdi
negq %rdi
movl $0x1, %edx
cmpq $0x1, (%rsi)
jne 0x3e86
movq -0x8(%rsi), %r8
cmpb $0x3e, (%r8)
je 0x3e9a
incq %rdx
addq $0x20, %rsi
movq %rdi, %r8
addq %rdx, %r8
jne 0x3e76
jmp 0x3ff3
movq %rdx, %rsi
subq %rcx, %rsi
cmpq $-0x1, %rsi
jne 0x3fdc
movq 0x3117(%rip), %rdi      # 0x6fc8
leaq 0x11c9(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x30ff(%rip), %rdi      # 0x6fc8
leaq 0x11b9(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x30e7(%rip), %rdi      # 0x6fc8
leaq 0x11a9(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movq 0x30cf(%rip), %rdi      # 0x6fc8
leaq 0x1199(%rip), %rsi      # 0x5099
movl $0x1a, %edx
callq 0x21b0
xorl %eax, %eax
jmp 0x40f8
leaq -0x70(%rbp), %rdi
callq 0x20c0
movl %r14d, %r13d
movl %r13d, %r15d
leaq 0x10(,%r15,8), %rdi
callq 0x2030
movq %rax, %r14
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14)
movq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
movq $0x0, 0x8(%r14,%r15,8)
movq %r13, %r12
testl %r13d, %r13d
je 0x3f8e
leal 0x1(%r12), %r15d
decq %r15
movl $0x8, %r13d
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14,%r13)
movq (%rbx), %rcx
movq (%rcx,%r13,4), %rsi
movq %rax, %rdi
callq 0x2160
addq $0x8, %r13
decq %r15
jne 0x3f68
movq -0x70(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x20e0
movl %eax, %r15d
movl $0x1, %edi
callq 0x2280
movl %eax, 0x3202(%rip)      # 0x71b8
movl $0x1, %edi
callq 0x2320
movl %r15d, %edi
callq 0x2280
movl %r15d, %edi
callq 0x2320
movb $0x1, 0x31e9(%rip)      # 0x71c0
jmp 0x4100
movl %edx, %ecx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rsi
addq $0x20, %rsi
leaq -0x70(%rbp), %rdi
callq 0x20c0
movl %r12d, %r15d
leaq 0x10(,%r15,8), %rdi
callq 0x2030
movq %rax, %r14
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14)
movq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
movq $0x0, 0x8(%r14,%r15,8)
movq %r12, -0x30(%rbp)
testl %r12d, %r12d
je 0x4068
movq -0x30(%rbp), %rax
leal 0x1(%rax), %r15d
decq %r15
movl $0x8, %r13d
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14,%r13)
movq (%rbx), %rcx
movq (%rcx,%r13,4), %rsi
movq %rax, %rdi
callq 0x2160
addq $0x8, %r13
decq %r15
jne 0x4042
movq -0x70(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x20e0
movl %eax, %r15d
movl $0x1, %edi
callq 0x2280
movl %eax, 0x3128(%rip)      # 0x71b8
movl $0x1, %edi
callq 0x2320
movl %r15d, %edi
callq 0x2280
movl %r15d, %edi
callq 0x2320
movb $0x1, 0x310f(%rip)      # 0x71c0
movq -0x90(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
xorl %eax, %eax
callq 0x20e0
movq -0x30(%rbp), %r12
movl %eax, %r15d
xorl %edi, %edi
callq 0x2280
movl %eax, 0x30e4(%rip)      # 0x71bc
xorl %edi, %edi
callq 0x2320
movl %r15d, %edi
callq 0x2280
movl %r15d, %edi
callq 0x2320
movb $0x1, 0x30cb(%rip)      # 0x71c1
movb $0x1, %al
testb %al, %al
je 0x42ea
leaq 0x109a(%rip), %rsi      # 0x51a1
leaq -0x50(%rbp), %rdi
callq 0x2060
testl %eax, %eax
je 0x4207
leaq 0x108f(%rip), %rsi      # 0x51ae
leaq -0x50(%rbp), %rdi
callq 0x2060
testl %eax, %eax
je 0x4242
leaq 0x107b(%rip), %rsi      # 0x51b2
leaq -0x50(%rbp), %rdi
callq 0x2060
testl %eax, %eax
je 0x427c
movq (%r14), %rdi
movq %r14, %rsi
callq 0x20f0
testl %eax, %eax
jns 0x42ea
movq 0x2e66(%rip), %rdi      # 0x6fc8
leaq 0xf18(%rip), %rsi       # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x2e4e(%rip), %rdi      # 0x6fc8
leaq 0xf08(%rip), %rsi       # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x2e36(%rip), %rdi      # 0x6fc8
leaq 0xef8(%rip), %rsi       # 0x5091
movl $0x7, %edx
callq 0x21b0
movq 0x2e1e(%rip), %rdi      # 0x6fc8
leaq 0xf9a(%rip), %rsi       # 0x514b
movl $0x1, %edx
callq 0x21b0
movq (%r14), %rsi
movq 0x2e03(%rip), %rdi      # 0x6fc8
callq 0x2180
leaq 0xfe4(%rip), %rsi       # 0x51b5
movl $0xd, %edx
movq %rax, %rdi
callq 0x21b0
cmpl $-0x1, %r12d
jl 0x4200
addl $0x2, %r12d
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x41f8
callq 0x2210
incq %rbx
cmpq %rbx, %r12
jne 0x41ea
xorl %edi, %edi
callq 0x21e0
leaq -0xd0(%rbp), %rbx
movl $0x32, %esi
movq %rbx, %rdi
callq 0x20a0
leaq 0xf83(%rip), %rdi       # 0x51a5
callq 0x2240
leaq 0xf73(%rip), %rdi       # 0x51a1
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2200
movq %rbx, %rdi
jmp 0x42e5
leaq -0xd0(%rbp), %rdi
movl $0x32, %esi
callq 0x20a0
leaq 0xf4b(%rip), %rdi       # 0x51a5
callq 0x2240
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
cmpq $0x21, %rcx
jb 0x42c2
movq 0x20(%rax), %rdx
leaq 0xf34(%rip), %rdi       # 0x51ae
jmp 0x42b4
leaq -0xd0(%rbp), %rdi
movl $0x32, %esi
callq 0x20a0
leaq 0xf11(%rip), %rdi       # 0x51a5
callq 0x2240
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
cmpq $0x21, %rcx
jb 0x42cb
movq 0x20(%rax), %rdx
leaq 0xefe(%rip), %rdi       # 0x51b2
movq %rdi, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2200
jmp 0x42de
leaq 0xee5(%rip), %rdi       # 0x51ae
jmp 0x42d2
leaq 0xee0(%rip), %rdi       # 0x51b2
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2200
leaq -0xd0(%rbp), %rdi
callq 0x2240
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x4306
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x431f
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x4338
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4357
jmp 0x4357
jmp 0x4357
jmp 0x4357
jmp 0x4357
jmp 0x4357
jmp 0x4357
jmp 0x4357
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x4376
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x438f
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x43a8
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0