// File: /workspace/github_cpp_makefile_star2_O2/xelia-it[P]mustache/src/mustache-light.cpp
// Function: mustache::Mustache::partialSubstitute(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>>>>, 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 Mustache::partialSubstitute(const Tokens partialParams, Tokens& newTokens) {
        Variables partialVariables;
        LOG_END("===============================================");

        for (std::vector<string>::size_type i = 1; i != partialParams.size(); i++) {
                LOG("Token: |");
                const string& token = partialParams.at(i);
                LOG(token);
                LOG_END("|");
                if (token.find_first_of("=") == std::string::npos) {
                        error("Bad substitution string: missing '=' in " + token);
                }
                vector<string> pair = split(token, '=');
                if (pair.size() != 2) {
                        error("Bad substitution string: missing separator in " + token);
                }

                VariableConstIterator lb = partialSearchVariable(partialVariables, pair.at(1));

                // map<string, string>::iterator lb = partialVariables.lower_bound(pair.at(0));
                // if (lb != partialVariables.end() && !(partialVariables.key_comp()(pair.at(0), lb->first))) {
                if (lb != partialVariables.end()) {
                        // key already exists
                        // update lb->second if you care to
                        partialVariables.insert(lb, map<string, string>::value_type(pair.at(0), lb->second ));
                } else {
                        // the key does not exist in the map
                        // add it to the map
                        partialVariables.insert(lb, map<string, string>::value_type(pair.at(0), pair.at(1)));
                        // Use lb as a hint to insert,
                        // so it can avoid another lookup
                }
        }

        LOG_END("===============================================");
        for (Tokens::size_type i = 1; i != newTokens.size(); i++) {
                //LOG("  New token: ");
                string tokenToCheck = newTokens.at(i);
                string tokenToCheckPrev = newTokens.at(i-1);
                //LOG_END(tokenToCheck);
                if ((tokenToCheck != TOKEN_START_VARIABLE) &&
                    (tokenToCheck != TOKEN_START_BEGIN_SECTION) &&
                    (tokenToCheck != TOKEN_START_END_SECTION) &&
                    (tokenToCheck != TOKEN_START_IF) &&
                    (tokenToCheck != TOKEN_START_UNLESS) &&
                    (tokenToCheck != TOKEN_START_PARTIAL) &&
                    (tokenToCheck != TOKEN_START_TEMPLATE) &&
                    (tokenToCheck != TOKEN_END)) {

                        // Searching
                        LOG("  Searching: ");
                        LOG(tokenToCheck);

                        VariableIterator lb = partialVariables.find(tokenToCheck);
                        if (lb == partialVariables.end()) {
                                LOG_END(" -> Not Found");
                                continue;
                        }
                        LOG(" -> { ");
                        LOG(lb->first);
                        LOG(" : ");
                        LOG(lb->second);
                        LOG_END(" }");

                        // Substitution
                        LOG("    Substitute: ");
                        string valueToSubstitute = lb->second;
                        LOG(newTokens.at(i));
                        LOG("    at position ");
                        LOG(i);

                        if (valueToSubstitute[0] != '\'' && valueToSubstitute[0] != '\"') {
                                // Simple substitution
                                LOG_END("    with nothing");
                                // newTokens.at(i) = valueToSubstitute;
                        } else {
                                // Substitute literal

                                if (tokenToCheckPrev != TOKEN_START_VARIABLE) {
                                        LOG_END("");
                                        LOG_END("NOT A VARIABLE SKIP LITERAL SUBSTITUTION!");
                                        continue;
                                }

                                if (valueToSubstitute[valueToSubstitute.size() - 1 ] != '\'' && valueToSubstitute[valueToSubstitute.size() - 1] != '\"') {
                                        error("Substitution string " + valueToSubstitute + " not properly closed");
                                }
                                LOG(" with literal ");
                                valueToSubstitute = valueToSubstitute.substr(1, valueToSubstitute.size() - 2);
                                LOG_END(valueToSubstitute);

                                // LOG_END("DUMP BEFORE:");
                                // for (std::vector<string>::size_type j = 0; j != newTokens.size(); j++) {
                                //     LOG(j);
                                //     LOG(" ");
                                //     LOG_END(newTokens.at(j));
                                // }

                                i = i - 1;
                                newTokens.erase(newTokens.begin() + i);
                                newTokens.erase(newTokens.begin() + i);

                                LOG_END("DUMP AFTER:");
                                for (std::vector<string>::size_type j = 0; j != newTokens.size(); j++) {
                                        LOG(j);
                                        LOG(" ");
                                        LOG_END(newTokens.at(j));
                                }

                                LOG("NOW i = ");
                                LOG_END(i);
                        }
                        newTokens.at(i) = valueToSubstitute;
                }
        }
        //partialVariables.clear();
        LOG_END("===============================================");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, -0x30(%rbp)
movq %rsi, %r14
leaq -0xe0(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %rdi, -0x58(%rbp)
pushq $0x1
popq %r12
leaq -0xb8(%rbp), %r13
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x5, %rax
cmpq %rax, %r12
je 0x2a3f0
movq %r14, %rdi
movq %r12, %rsi
callq 0x260e0
movq %rax, %r15
movq %rax, %rdi
leaq 0x12949(%rip), %rsi     # 0x3cc43
xorl %edx, %edx
callq 0x25890
cmpq $-0x1, %rax
je 0x2a638
movq %r13, %rdi
movq -0x58(%rbp), %rsi
movq %r15, %rdx
pushq $0x3d
popq %rcx
callq 0x266d0
movq -0xb0(%rbp), %rax
subq -0xb8(%rbp), %rax
cmpq $0x40, %rax
jne 0x2a65a
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x25490
leaq -0xe8(%rbp), %rsi
movq %rax, %rdx
callq 0x25a30
movq %rax, %r15
leaq -0xe0(%rbp), %rax
cmpq %rax, %r15
je 0x2a395
movq %r13, %rdi
xorl %esi, %esi
callq 0x25490
leaq -0x98(%rbp), %rbx
movq %r15, %rdx
addq $0x40, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x272a0
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x27170
jmp 0x2a3d8
movq %r13, %rdi
xorl %esi, %esi
callq 0x25490
movq %rax, %rbx
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x25490
leaq -0x98(%rbp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x261e0
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
leaq -0x98(%rbp), %rbx
movq %rbx, %rdx
callq 0x27170
movq %rbx, %rdi
callq 0x25230
movq %r13, %rdi
callq 0x25d90
incq %r12
jmp 0x2a2ce
pushq $0x1
popq %rbx
leaq -0x98(%rbp), %r15
leaq -0xb8(%rbp), %r12
movq %rbx, %r14
movq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
sarq $0x5, %rax
cmpq %rax, %r14
je 0x2a61a
movq %r14, %rsi
callq 0x25490
movq %r15, %rdi
movq %rax, %rsi
callq 0x26a70
leaq -0x1(%r14), %r13
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x25490
movq %r12, %rdi
movq %rax, %rsi
callq 0x26a70
movq %r15, %rdi
movq 0x21a94(%rip), %rsi     # 0x4bee8
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x21aed(%rip), %rsi     # 0x4bf58
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x21b2e(%rip), %rsi     # 0x4bfb0
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x21b0f(%rip), %rsi     # 0x4bfa8
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x219f8(%rip), %rsi     # 0x4bea8
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x21ab9(%rip), %rsi     # 0x4bf80
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x21a1a(%rip), %rsi     # 0x4bef8
callq 0x273f0
testb %al, %al
je 0x2a602
movq %r15, %rdi
movq 0x2198b(%rip), %rsi     # 0x4be80
callq 0x273f0
testb %al, %al
je 0x2a602
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
callq 0x25c80
leaq -0xe0(%rbp), %rcx
cmpq %rcx, %rax
je 0x2a602
addq $0x40, %rax
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x26a70
movq -0x50(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x22, %eax
je 0x2a546
cmpl $0x27, %eax
jne 0x2a5d8
movq %r12, %rdi
movq 0x21998(%rip), %rsi     # 0x4bee8
callq 0x273f0
testb %al, %al
jne 0x2a5f9
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
movzbl -0x1(%rax,%rcx), %eax
cmpl $0x22, %eax
je 0x2a578
cmpl $0x27, %eax
jne 0x2a67c
addq $-0x2, %rcx
leaq -0x108(%rbp), %rdi
leaq -0x50(%rbp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x26450
movq %r14, %rdi
leaq -0x108(%rbp), %rbx
movq %rbx, %rsi
callq 0x26740
movq %rbx, %rdi
callq 0x27788
movq %r13, %rbx
shlq $0x5, %rbx
movq -0x30(%rbp), %r14
movq (%r14), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x26420
addq (%r14), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x26420
pushq $0x1
popq %rbx
jmp 0x2a5df
movq %r14, %r13
movq -0x30(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x25490
movq %rax, %rdi
leaq -0x50(%rbp), %rsi
callq 0x259e0
movq %r13, %r14
leaq -0x50(%rbp), %rdi
callq 0x27788
movq %r12, %rdi
callq 0x27788
movq %r15, %rdi
callq 0x27788
incq %r14
jmp 0x2a404
leaq -0xe8(%rbp), %rdi
callq 0x26150
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x126d1(%rip), %rsi     # 0x3cd10
leaq -0x98(%rbp), %rdi
movq %r15, %rdx
callq 0x261d0
leaq -0x98(%rbp), %rsi
callq 0x26890
leaq 0x126d8(%rip), %rsi     # 0x3cd39
leaq -0x98(%rbp), %rdi
movq %r15, %rdx
callq 0x261d0
leaq -0x98(%rbp), %rsi
callq 0x26890
leaq 0x126e5(%rip), %rsi     # 0x3cd68
leaq -0x128(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x261d0
leaq 0x126e3(%rip), %rdx     # 0x3cd7d
leaq -0x108(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x25300
leaq -0x108(%rbp), %rsi
callq 0x26890
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x27788
jmp 0x2a6cd
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
callq 0x27788
jmp 0x2a6e8
jmp 0x2a6e5
jmp 0x2a6e5
jmp 0x2a6e5
jmp 0x2a6e5
jmp 0x2a6f3
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x27788
jmp 0x2a6f6
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x27788
jmp 0x2a739
jmp 0x2a71d
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x27788
jmp 0x2a74f
jmp 0x2a74c
jmp 0x2a736
jmp 0x2a747
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x25230
jmp 0x2a74f
jmp 0x2a747
jmp 0x2a747
jmp 0x2a74c
jmp 0x2a74c
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x27788
jmp 0x2a75b
movq %rax, %rbx
jmp 0x2a75b
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x25d90
leaq -0xe8(%rbp), %rdi
callq 0x26150
movq %rbx, %rdi
callq 0x27310
nop