// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movabsq $0x100000000, %r15      # imm = 0x100000000
movq (%rdi), %rdi
movq 0x8(%rbx), %rsi
leaq 0x16ae(%rip), %rdx      # 0x6926
callq 0x3c81
cmpq 0x8(%rbx), %rax
je 0x52d3
movq %rax, %r14
movq (%rbx), %rax
movq %r14, %rcx
subq %rax, %rcx
shlq $0x1b, %rcx
addq %r15, %rcx
sarq $0x1b, %rcx
movq (%rax,%rcx), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2190
movl %eax, %edi
xorl %esi, %esi
callq 0x2250
cmpl $-0x1, %eax
jne 0x52c1
leaq 0xf65(%rip), %rdi       # 0x6221
callq 0x2210
movq %r14, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x569c
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x1647(%rip), %rdx      # 0x6928
callq 0x3c81
cmpq 0x8(%rbx), %rax
je 0x534c
movq %rax, %r14
movq (%rbx), %rax
movq %r14, %rcx
subq %rax, %rcx
shlq $0x1b, %rcx
addq %r15, %rcx
sarq $0x1b, %rcx
movq (%rax,%rcx), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b0, %edx            # imm = 0x1B0
movl $0x1a4, %ecx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2190
movl %eax, %edi
movl $0x1, %esi
callq 0x2250
cmpl $-0x1, %eax
jne 0x533a
leaq 0xeec(%rip), %rdi       # 0x6221
callq 0x2210
movq %r14, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x569c
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rdx, %rdi
subq %rax, %rdi
movq %rdi, %rcx
sarq $0x5, %rcx
movq %rsp, %rsi
sarq $0x2, %rdi
addq $0x17, %rdi
andq $-0x10, %rdi
subq %rdi, %rsi
movq %rsi, %rsp
cmpq %rax, %rdx
je 0x539d
cmpq $0x1, %rcx
movq %rcx, %rdx
adcq $0x0, %rdx
xorl %edi, %edi
movq %rax, %r8
movq (%r8), %r9
movq %r9, (%rsi,%rdi,8)
incq %rdi
addq $0x20, %r8
cmpq %rdi, %rdx
jne 0x538a
movq $0x0, (%rsi,%rcx,8)
movq (%rax), %rdi
callq 0x21c0
cmpl $-0x1, %eax
jne 0x53c6
movq (%rbx), %rax
movq (%rax), %rsi
leaq 0x156b(%rip), %rdi      # 0x692a
xorl %eax, %eax
callq 0x2030
movl $0x1, %edi
callq 0x2310