// File: /workspace/github_cpp_makefile_star2_O3/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 0x3bc3
sarq $0x5, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
leaq 0x8(%rax), %rdx
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq $0x1, (%rdx)
jne 0x3bb8
movq -0x8(%rdx), %rsi
movb (%rsi), %sil
cmpb $0x3e, %sil
jne 0x3bad
movb $0x1, %r15b
jmp 0x3bb8
cmpb $0x3c, %sil
sete %sil
orb %sil, %r14b
addq $0x20, %rdx
decq %rcx
jne 0x3b95
jmp 0x3bc9
xorl %r14d, %r14d
xorl %r15d, %r15d
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x4532
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
xorl %r12d, %r12d
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
testb $0x1, %r14b
je 0x3c64
testb $0x1, %r15b
je 0x40e3
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
xorl %r12d, %r12d
cmpq $0x2, %rcx
jb 0x3fe2
leal -0x1(%rcx), %r12d
leaq 0x40(%rax), %rsi
leaq -0x1(%rcx), %rdx
xorl %r14d, %r14d
cmpq $0x1, -0x18(%rsi)
jne 0x3c53
movq -0x20(%rsi), %rdi
cmpb $0x3c, (%rdi)
je 0x3d57
addq $0x20, %rsi
incq %r14
cmpq %r14, %rdx
jne 0x3c3f
jmp 0x3e4c
movq (%rbx), %rsi
movq 0x8(%rbx), %r14
subq %rsi, %r14
sarq $0x5, %r14
testb $0x1, %r15b
je 0x3cb9
xorl %r12d, %r12d
cmpq $0x2, %r14
jb 0x3e99
leal -0x1(%r14), %r12d
addq $0x40, %rsi
leaq -0x1(%r14), %rax
xorl %r15d, %r15d
cmpq $0x1, -0x18(%rsi)
jne 0x3ca8
movq -0x20(%rsi), %rcx
cmpb $0x3e, (%rcx)
je 0x3dbd
addq $0x20, %rsi
incq %r15
cmpq %r15, %rax
jne 0x3c94
jmp 0x3e99
movq %rsp, -0x98(%rbp)
leal 0x1(%r14), %eax
movq %rsp, %r12
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movl %r14d, %eax
decl %eax
movq %rax, -0x50(%rbp)
js 0x3d3e
movl %r14d, %r15d
xorl %r13d, %r13d
movl $0x14, %edi
callq 0x2030
movq %rax, (%r12,%r13,8)
incq %r13
cmpq %r13, %r15
jne 0x3cea
movq (%r12), %rdi
movq -0x48(%rbp), %rsi
callq 0x2160
cmpl $0x0, -0x50(%rbp)
leaq -0x38(%rbp), %r13
je 0x3d47
movl %r14d, %r14d
decq %r14
movl $0x8, %r15d
movq (%r12,%r15), %rdi
movq (%rbx), %rax
movq (%rax,%r15,4), %rsi
callq 0x2160
addq $0x8, %r15
decq %r14
jne 0x3d23
jmp 0x3d47
movq -0x48(%rbp), %rsi
callq 0x2160
movq -0x98(%rbp), %rsp
movq -0x50(%rbp), %r12
jmp 0x40e3
addq $-0x2, %rcx
cmpq %r14, %rcx
jne 0x3e2f
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
movl $0x19, %edx
leaq 0x13cf(%rip), %rsi      # 0x5187
jmp 0x3fba
addq $-0x2, %r14
cmpq %r15, %r14
jne 0x3e8d
movq 0x31f7(%rip), %rdi      # 0x6fc8
leaq 0x12a9(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x31df(%rip), %rdi      # 0x6fc8
leaq 0x1299(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x31c7(%rip), %rdi      # 0x6fc8
leaq 0x1289(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movq 0x31af(%rip), %rdi      # 0x6fc8
leaq 0x1279(%rip), %rsi      # 0x5099
movl $0x1a, %edx
callq 0x21b0
jmp 0x42cd
leaq -0x90(%rbp), %rdi
callq 0x20c0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
movl %r14d, %r12d
cmpq $0x2, %rcx
jb 0x3fe2
leaq 0x28(%rax), %rsi
movq %rcx, %rdi
negq %rdi
movl $0x1, %edx
cmpq $0x1, (%rsi)
jne 0x3e79
movq -0x8(%rsi), %r8
cmpb $0x3e, (%r8)
je 0x3f5a
incq %rdx
addq $0x20, %rsi
movq %rdi, %r8
addq %rdx, %r8
jne 0x3e65
jmp 0x3fe2
leaq -0x70(%rbp), %rdi
callq 0x20c0
movl %r15d, %r12d
movl %r12d, %r15d
leaq 0x10(,%r15,8), %rdi
callq 0x2030
movq %rax, %r14
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14)
movq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
movq $0x0, 0x8(%r14,%r15,8)
movq %r12, %r13
testl %r12d, %r12d
je 0x3f09
leal 0x1(%r13), %r15d
decq %r15
movl $0x8, %r12d
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14,%r12)
movq (%rbx), %rcx
movq (%rcx,%r12,4), %rsi
movq %rax, %rdi
callq 0x2160
addq $0x8, %r12
decq %r15
jne 0x3ee3
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, 0x3287(%rip)      # 0x71b8
movl $0x1, %edi
callq 0x2320
movl %r15d, %edi
callq 0x2280
movl %r15d, %edi
callq 0x2320
movb $0x1, 0x326e(%rip)      # 0x71c0
movq %r13, %r12
jmp 0x40df
movq %rdx, %rsi
subq %rcx, %rsi
cmpq $-0x1, %rsi
jne 0x3fcb
movq 0x305b(%rip), %rdi      # 0x6fc8
leaq 0x110d(%rip), %rsi      # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x3043(%rip), %rdi      # 0x6fc8
leaq 0x10fd(%rip), %rsi      # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x302b(%rip), %rdi      # 0x6fc8
leaq 0x10ed(%rip), %rsi      # 0x5091
movl $0x7, %edx
callq 0x21b0
movl $0x1a, %edx
leaq 0x10df(%rip), %rsi      # 0x5099
movq 0x3007(%rip), %rdi      # 0x6fc8
callq 0x21b0
jmp 0x42cd
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 -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
movq $0x0, 0x8(%r14,%r15,8)
movq %r12, %r13
testl %r12d, %r12d
je 0x4052
leal 0x1(%r13), %r15d
decq %r15
movl $0x8, %r12d
movl $0x14, %edi
callq 0x2030
movq %rax, (%r14,%r12)
movq (%rbx), %rcx
movq (%rcx,%r12,4), %rsi
movq %rax, %rdi
callq 0x2160
addq $0x8, %r12
decq %r15
jne 0x402c
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, 0x313e(%rip)      # 0x71b8
movl $0x1, %edi
callq 0x2320
movl %r15d, %edi
callq 0x2280
movl %r15d, %edi
callq 0x2320
movb $0x1, 0x3125(%rip)      # 0x71c0
movq -0x90(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
xorl %eax, %eax
callq 0x20e0
movl %eax, %r15d
movq %r13, %r12
xorl %edi, %edi
callq 0x2280
movl %eax, 0x30fb(%rip)      # 0x71bc
xorl %edi, %edi
callq 0x2320
movl %r15d, %edi
callq 0x2280
movl %r15d, %edi
callq 0x2320
movb $0x1, 0x30e2(%rip)      # 0x71c1
leaq -0x38(%rbp), %r13
leaq 0x10b7(%rip), %rsi      # 0x51a1
leaq -0x48(%rbp), %rdi
callq 0x2060
testl %eax, %eax
je 0x41ea
leaq 0x10ac(%rip), %rsi      # 0x51ae
leaq -0x48(%rbp), %rdi
callq 0x2060
testl %eax, %eax
je 0x4225
leaq 0x1098(%rip), %rsi      # 0x51b2
leaq -0x48(%rbp), %rdi
callq 0x2060
testl %eax, %eax
je 0x425f
movq (%r14), %rdi
movq %r14, %rsi
callq 0x20f0
testl %eax, %eax
jns 0x42cd
movq 0x2e83(%rip), %rdi      # 0x6fc8
leaq 0xf35(%rip), %rsi       # 0x5081
movl $0x7, %edx
callq 0x21b0
movq 0x2e6b(%rip), %rdi      # 0x6fc8
leaq 0xf25(%rip), %rsi       # 0x5089
movl $0x7, %edx
callq 0x21b0
movq 0x2e53(%rip), %rdi      # 0x6fc8
leaq 0xf15(%rip), %rsi       # 0x5091
movl $0x7, %edx
callq 0x21b0
movq 0x2e3b(%rip), %rdi      # 0x6fc8
leaq 0xfb7(%rip), %rsi       # 0x514b
movl $0x1, %edx
callq 0x21b0
movq (%r14), %rsi
movq 0x2e20(%rip), %rdi      # 0x6fc8
callq 0x2180
leaq 0x1001(%rip), %rsi      # 0x51b5
movl $0xd, %edx
movq %rax, %rdi
callq 0x21b0
cmpl $-0x1, %r12d
jl 0x41e3
addl $0x2, %r12d
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x41db
callq 0x2210
incq %rbx
cmpq %rbx, %r12
jne 0x41cd
xorl %edi, %edi
callq 0x21e0
leaq -0xd0(%rbp), %rbx
movl $0x32, %esi
movq %rbx, %rdi
callq 0x20a0
leaq 0xfa0(%rip), %rdi       # 0x51a5
callq 0x2240
leaq 0xf90(%rip), %rdi       # 0x51a1
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2200
movq %rbx, %rdi
jmp 0x42c8
leaq -0xd0(%rbp), %rdi
movl $0x32, %esi
callq 0x20a0
leaq 0xf68(%rip), %rdi       # 0x51a5
callq 0x2240
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
cmpq $0x21, %rcx
jb 0x42a5
movq 0x20(%rax), %rdx
leaq 0xf51(%rip), %rdi       # 0x51ae
jmp 0x4297
leaq -0xd0(%rbp), %rdi
movl $0x32, %esi
callq 0x20a0
leaq 0xf2e(%rip), %rdi       # 0x51a5
callq 0x2240
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
cmpq $0x21, %rcx
jb 0x42ae
movq 0x20(%rax), %rdx
leaq 0xf1b(%rip), %rdi       # 0x51b2
movq %rdi, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2200
jmp 0x42c1
leaq 0xf02(%rip), %rdi       # 0x51ae
jmp 0x42b5
leaq 0xefd(%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 0x42e9
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x4302
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x4317
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x21a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4336
jmp 0x4336
jmp 0x4336
jmp 0x4336
jmp 0x4336
jmp 0x4336
jmp 0x4336
jmp 0x4336
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x4355
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x436e
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21a0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x4387
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0
nop