// File: /workspace/github_cpp_makefile_star2_O2/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 0x109a(%rip), %rdx      # 0x591e
callq 0x3603
movq 0x8(%rbx), %rsi
cmpq %rsi, %rax
je 0x48e6
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 0x21c0
movl %eax, %edi
xorl %esi, %esi
callq 0x2280
cmpl $-0x1, %eax
jne 0x48d0
leaq 0x965(%rip), %rdi       # 0x5230
callq 0x2240
movq %r14, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4bf4
movq 0x8(%rbx), %rsi
movq (%rbx), %rdi
leaq 0x1030(%rip), %rdx      # 0x5920
callq 0x3603
movq %rax, %r14
cmpq 0x8(%rbx), %rax
je 0x495d
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 0x21c0
pushq $0x1
popq %rsi
movl %eax, %edi
callq 0x2280
cmpl $-0x1, %eax
jne 0x4947
leaq 0x8ee(%rip), %rdi       # 0x5230
callq 0x2240
movq %r14, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4bf4
movq 0x8(%rbx), %r14
movq (%rbx), %rax
subq %rax, %r14
movq %r14, %rcx
sarq $0x5, %rcx
sarq $0x2, %r14
addq $0x17, %r14
andq $-0x10, %r14
movq %rsp, %rsi
subq %r14, %rsi
movq %rsi, %rsp
xorl %edx, %edx
movq %rcx, %rdi
subq $0x1, %rdi
jb 0x4998
movq (%rax,%rdx,4), %r8
movq %r8, (%rsi,%rdx)
addq $0x8, %rdx
jmp 0x4984
andq $0x0, (%rsi,%rcx,8)
movq (%rax), %rdi
callq 0x21f0
cmpl $-0x1, %eax
jne 0x49be
movq (%rbx), %rax
movq (%rax), %rsi
leaq 0xf6b(%rip), %rdi       # 0x5922
xorl %eax, %eax
callq 0x2030
pushq $0x1
popq %rdi
callq 0x2320