// File: mukul-mehta[P]meta/builtins.cc
// Function: metash_execute(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>>>>)
int metash_execute(vector<string> tokens) {
    /*
		Check if there is a < token in the token vector. If found, the next token of < will be the filename
		Remove this token and the filename since the execvp call needs only the arguments passed
		Open the file and use the `dup2` system call to set appropriate file descriptor entry
	*/

    vector<string>::iterator inpIter = find(tokens.begin(), tokens.end(), "<");
    if (inpIter != tokens.end()) {
        int index = inpIter - tokens.begin();
        int inputFD = open(tokens[index + 1].c_str(), O_RDONLY);
        int status = dup2(inputFD, READ_FLAGS);
        if (status == -1)
            perror("dup2() failed");

        tokens.erase(inpIter, inpIter + 2);
    }

    // Same as above, check for existence of > token and use the `dup2` call with the output file
    vector<string>::iterator outIter = find(tokens.begin(), tokens.end(), ">");
    if (outIter != tokens.end()) {
        int index = outIter - tokens.begin();
        int outputFD = open(tokens[index + 1].c_str(), WRITE_FLAGS, 0644);
        int status = dup2(outputFD, STDOUT_FILENO);
        if (status == -1)
            perror("dup2() failed");

        tokens.erase(outIter, outIter + 2);
    }

    size_t num_tokens = tokens.size();
    // `execvp` requires a char array with the last element set to NULL
    char* args[num_tokens + 1];
    for (size_t i = 0; i < tokens.size(); i++)
        args[i] = (char*)(tokens[i].c_str());

    args[num_tokens] = NULL;

    int ret = execvp(tokens[0].c_str(), args);
    if (ret == -1) {
        printf("execvp() failed: Command not found: %s\n", tokens[0].c_str());
    }
    exit(EXIT_FAILURE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xe8(%rbp)
movq %rdi, -0x8(%rbp)
callq 0x54f0
movq -0xe8(%rbp), %rdi
movq %rax, -0x18(%rbp)
callq 0x5520
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x2263(%rip), %rdx      # 0xb961
callq 0x54a0
movq -0xe8(%rbp), %rdi
movq %rax, -0x10(%rbp)
callq 0x5520
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x5550
testb $0x1, %al
jne 0x972d
jmp 0x97dc
movq -0xe8(%rbp), %rdi
callq 0x54f0
movq %rax, -0x38(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x6c30
movq -0xe8(%rbp), %rdi
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x50c0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x3220
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %edi
xorl %esi, %esi
callq 0x32f0
movl %eax, -0x40(%rbp)
cmpl $-0x1, -0x40(%rbp)
jne 0x9798
leaq 0x1aad(%rip), %rdi      # 0xb240
callq 0x32a0
leaq -0x48(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x9ea0
leaq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x9ee0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x9ea0
movq -0xe8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x9e10
movq %rax, -0x60(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x54f0
movq -0xe8(%rbp), %rdi
movq %rax, -0x70(%rbp)
callq 0x5520
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq 0x2158(%rip), %rdx      # 0xb963
callq 0x54a0
movq -0xe8(%rbp), %rdi
movq %rax, -0x68(%rbp)
callq 0x5520
movq %rax, -0x80(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x5550
testb $0x1, %al
jne 0x983a
jmp 0x9926
movq -0xe8(%rbp), %rdi
callq 0x54f0
movq %rax, -0x90(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x6c30
movq -0xe8(%rbp), %rdi
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x50c0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b0, %edx            # imm = 0x1B0
movl $0x1a4, %ecx            # imm = 0x1A4
movb $0x0, %al
callq 0x3220
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %edi
movl $0x1, %esi
callq 0x32f0
movl %eax, -0x98(%rbp)
cmpl $-0x1, -0x98(%rbp)
jne 0x98cd
leaq 0x1978(%rip), %rdi      # 0xb240
callq 0x32a0
leaq -0xa0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x9ea0
leaq -0x68(%rbp), %rdi
movl $0x2, %esi
callq 0x9ee0
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x9ea0
movq -0xe8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
callq 0x9e10
movq %rax, -0xb8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x5450
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rcx
movq %rcx, %rax
incq %rax
movq %rsp, %rdx
movq %rdx, -0xc8(%rbp)
leaq 0x17(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf0(%rbp)
movq %rcx, %rsp
movq %rax, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xd8(%rbp), %rax
movq %rax, -0xf8(%rbp)
callq 0x5450
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0x99eb
movq -0xe8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdx
movq -0xf0(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x997e
movq -0xf0(%rbp), %rax
movq -0xe8(%rbp), %rdi
movq -0xc0(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
xorl %eax, %eax
movl %eax, %esi
callq 0x50c0
movq %rax, %rdi
callq 0x3120
movq -0xf0(%rbp), %rsi
movq %rax, %rdi
callq 0x3250
movl %eax, -0xdc(%rbp)
cmpl $-0x1, -0xdc(%rbp)
jne 0x9a60
movq -0xe8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x50c0
movq %rax, %rdi
callq 0x3120
movq %rax, %rsi
leaq 0x1f0c(%rip), %rdi      # 0xb965
movb $0x0, %al
callq 0x3040
movl $0x1, %edi
callq 0x33d0
nopw (%rax,%rax)