// File: /workspace/github_cpp_makefile_star2_O3/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 0x186e(%rip), %rdx      # 0x6926
callq 0x3b01
movq 0x8(%rbx), %rsi
cmpq %rsi, %rax
je 0x511a
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 0x5104
leaq 0x1122(%rip), %rdi      # 0x6221
callq 0x2210
movq %r14, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x54e0
movq 0x8(%rbx), %rsi
movq (%rbx), %rdi
leaq 0x1804(%rip), %rdx      # 0x6928
callq 0x3b01
movq %rax, %r14
cmpq 0x8(%rbx), %rax
je 0x5193
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 0x517d
leaq 0x10a9(%rip), %rdi      # 0x6221
callq 0x2210
movq %r14, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x54e0
movq 0x8(%rbx), %r14
movq (%rbx), %rax
movq %r14, %rdx
subq %rax, %rdx
movq %rdx, %rcx
sarq $0x5, %rcx
sarq $0x2, %rdx
addq $0x17, %rdx
andq $-0x10, %rdx
movq %rsp, %rsi
subq %rdx, %rsi
movq %rsi, %rsp
cmpq %rax, %r14
je 0x51e0
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 0x51cd
movq $0x0, (%rsi,%rcx,8)
movq (%rax), %rdi
callq 0x21c0
cmpl $-0x1, %eax
jne 0x5209
movq (%rbx), %rax
movq (%rax), %rsi
leaq 0x1728(%rip), %rdi      # 0x692a
xorl %eax, %eax
callq 0x2030
movl $0x1, %edi
callq 0x2310