// File: xelia-it[P]mustache/src/mustache-light.cpp
// Function: mustache::Mustache::searchVariableInContext(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
json Mustache::searchVariableInContext(const string& key) {
        // TODO: better to use a constant
        if (key == "@index") {
                json value = currentListCounter_;
                return value;
        }
        if (key == "@first") {
                json value = (currentListCounter_ == 0);
                return value;
        }
        // Get the current context
        json top = stack_.top();
        LOG_END("SEARCH CONTEXT:");
        LOG_END(top.dump(2));
        if (top.is_null()) {
                return "null"_json;
        }

        string::size_type start;
        string::size_type stop;
        string::size_type index = string::npos;
        string newKey = key;

        if ((start = key.find_first_of("[")) != string::npos) {
                if ((stop = key.find_first_of("]")) == string::npos) {
                        error("Missing ] in array selection");
                }
                if (start + 1 == stop) {
                        error("Index is empty");
                }
                string array = key.substr(0, start);
                string indexAsString = key.substr(start + 1, stop - 1);

                newKey = array;
                index = std::stoul(indexAsString);
        }

        json::const_iterator it = top.find(newKey);
        if (it == top.end()) {
                LOG_END("NOT FOUND:");
                throw std::invalid_argument("Variable " + key + " not found");
        } else if (index == string::npos) {
                LOG_END("NORMAL USE *it:");
                json value = *it;
                return value;
        } else {
                LOG_END("USE INDEX:");
                // top exists at this point
                if (it->is_array()) {
                    LOG_END("USE ARRAY:");
                    if (index >= it->size()) {
                      LOG_END("OUT OF RANGE:");
                      throw std::out_of_range("Index " + std::to_string(index) + " is out of range");
                    } else {
                      LOG_END("IN RANGE:");
                    }
                }
                json value = (*it)[index];
                return value;
        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x230(%rbp)
movq %rdi, %rax
movq %rax, -0x228(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x220(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x34c1c(%rip), %rsi     # 0xa97aa
callq 0x6e390
testb $0x1, %al
jne 0x74b99
jmp 0x74bd2
movq -0x230(%rbp), %rdi
movq -0x220(%rbp), %rsi
movb $0x0, -0x19(%rbp)
addq $0xf0, %rsi
callq 0x6dad0
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x74bcd
movq -0x230(%rbp), %rdi
callq 0x6b680
jmp 0x75292
movq -0x18(%rbp), %rdi
leaq 0x34bd4(%rip), %rsi     # 0xa97b1
callq 0x6e390
testb $0x1, %al
jne 0x74be8
jmp 0x74c2e
movq -0x230(%rbp), %rdi
movq -0x220(%rbp), %rax
movb $0x0, -0x1a(%rbp)
cmpq $0x0, 0xf0(%rax)
sete %al
andb $0x1, %al
movb %al, -0x1b(%rbp)
leaq -0x1b(%rbp), %rsi
callq 0x69550
movb $0x1, -0x1a(%rbp)
testb $0x1, -0x1a(%rbp)
jne 0x74c29
movq -0x230(%rbp), %rdi
callq 0x6b680
jmp 0x75292
movq -0x220(%rbp), %rdi
addq $0xf8, %rdi
callq 0x6d5f0
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x67550
leaq -0x30(%rbp), %rdi
callq 0x6b930
testb $0x1, %al
jne 0x74c5c
jmp 0x74c93
movq -0x230(%rbp), %rdi
leaq 0x34b4e(%rip), %rsi     # 0xa97b8
movl $0x4, %edx
callq 0x69840
jmp 0x74c76
movl $0x1, -0x40(%rbp)
jmp 0x7527c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x75287
movq $-0x1, -0x58(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x685c0
jmp 0x74caa
movq -0x18(%rbp), %rdi
leaq 0x394f9(%rip), %rsi     # 0xae1ae
xorl %eax, %eax
movl %eax, %edx
callq 0x688f0
movq %rax, -0x48(%rbp)
cmpq $-0x1, %rax
je 0x74ec5
movq -0x18(%rbp), %rdi
leaq 0x37c58(%rip), %rsi     # 0xac92f
xorl %eax, %eax
movl %eax, %edx
callq 0x688f0
movq %rax, -0x50(%rbp)
cmpq $-0x1, %rax
jne 0x74d65
leaq -0x99(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x6d7d0
movq -0x238(%rbp), %rdx
leaq 0x34ab2(%rip), %rsi     # 0xa97bd
leaq -0x98(%rbp), %rdi
callq 0x6bd90
jmp 0x74d19
movq -0x220(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x6ba10
jmp 0x74d2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x74d54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x6e458
leaq -0x99(%rbp), %rdi
callq 0x6ae90
jmp 0x75271
movq -0x48(%rbp), %rax
addq $0x1, %rax
cmpq -0x50(%rbp), %rax
jne 0x74dee
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x6d7d0
movq -0x240(%rbp), %rdx
leaq 0x34a46(%rip), %rsi     # 0xa97da
leaq -0xc0(%rbp), %rdi
callq 0x6bd90
jmp 0x74da2
movq -0x220(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x6ba10
jmp 0x74db7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x74ddd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x6e458
leaq -0xc1(%rbp), %rdi
callq 0x6ae90
jmp 0x75271
movq -0x18(%rbp), %rsi
movq -0x48(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0xe8(%rbp), %rdi
callq 0x6ac50
jmp 0x74e08
movq -0x18(%rbp), %rsi
movq -0x48(%rbp), %rdx
incq %rdx
movq -0x50(%rbp), %rcx
decq %rcx
leaq -0x108(%rbp), %rdi
callq 0x6ac50
jmp 0x74e28
leaq -0x78(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x67190
jmp 0x74e3a
xorl %eax, %eax
movl %eax, %esi
leaq -0x108(%rbp), %rdi
movl $0xa, %edx
callq 0x67a50
movq %rax, -0x248(%rbp)
jmp 0x74e58
movq -0x248(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x6e458
leaq -0xe8(%rbp), %rdi
callq 0x6e458
jmp 0x74ec5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x75271
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x74eb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x6e458
leaq -0xe8(%rbp), %rdi
callq 0x6e458
jmp 0x75271
leaq -0x148(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0x682a0
jmp 0x74edb
leaq -0x128(%rbp), %rdi
movq %rdi, -0x260(%rbp)
leaq -0x148(%rbp), %rsi
callq 0x6b750
leaq -0x168(%rbp), %rdi
movq %rdi, -0x258(%rbp)
leaq -0x30(%rbp), %rsi
callq 0x68b00
movq -0x260(%rbp), %rdi
movq -0x258(%rbp), %rsi
callq 0x6abe0
movb %al, -0x249(%rbp)
jmp 0x74f27
movb -0x249(%rbp), %al
testb $0x1, %al
jne 0x74f36
jmp 0x7501a
movb $0x1, -0x1a9(%rbp)
movl $0x10, %edi
callq 0x687d0
movq %rax, -0x268(%rbp)
movq -0x18(%rbp), %rdx
leaq 0x34890(%rip), %rsi     # 0xa97e9
leaq -0x1a8(%rbp), %rdi
callq 0x6a3b0
jmp 0x74f67
leaq 0x34885(%rip), %rdx     # 0xa97f3
leaq -0x188(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x678d0
jmp 0x74f83
movq -0x268(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x6d570
jmp 0x74f98
movq -0x268(%rbp), %rdi
movb $0x0, -0x1a9(%rbp)
movq 0x51023(%rip), %rsi     # 0xc5fd0
movq 0x50fec(%rip), %rdx     # 0xc5fa0
callq 0x6d470
jmp 0x752ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x74ffe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x74ff2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x6e458
leaq -0x1a8(%rbp), %rdi
callq 0x6e458
testb $0x1, -0x1a9(%rbp)
jne 0x75009
jmp 0x75015
movq -0x268(%rbp), %rdi
callq 0x69830
jmp 0x75271
cmpq $-0x1, -0x58(%rbp)
jne 0x7507a
movb $0x0, -0x1aa(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x68900
movq %rax, -0x270(%rbp)
jmp 0x7503d
movq -0x270(%rbp), %rsi
movq -0x230(%rbp), %rdi
callq 0x67550
jmp 0x75052
movb $0x1, -0x1aa(%rbp)
movl $0x1, -0x40(%rbp)
testb $0x1, -0x1aa(%rbp)
jne 0x75075
movq -0x230(%rbp), %rdi
callq 0x6b680
jmp 0x75266
leaq -0x128(%rbp), %rdi
callq 0x6cc20
movq %rax, -0x278(%rbp)
jmp 0x7508f
movq -0x278(%rbp), %rdi
callq 0x69d90
testb $0x1, %al
jne 0x750a4
jmp 0x751f7
movq -0x58(%rbp), %rax
movq %rax, -0x288(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x6cc20
movq %rax, -0x280(%rbp)
jmp 0x750c4
movq -0x280(%rbp), %rdi
callq 0x6c630
movq %rax, %rcx
movq -0x288(%rbp), %rax
cmpq %rcx, %rax
jb 0x751f3
movb $0x1, -0x211(%rbp)
movl $0x10, %edi
callq 0x687d0
movq %rax, -0x290(%rbp)
movq -0x58(%rbp), %rsi
leaq -0x210(%rbp), %rdi
callq 0x68070
jmp 0x7510d
leaq 0x346ea(%rip), %rsi     # 0xa97fe
leaq -0x1f0(%rbp), %rdi
leaq -0x210(%rbp), %rdx
callq 0x6d330
jmp 0x75129
leaq 0x346d5(%rip), %rdx     # 0xa9805
leaq -0x1d0(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x678d0
jmp 0x75145
movq -0x290(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x6c6b0
jmp 0x7515a
movq -0x290(%rbp), %rdi
movb $0x0, -0x211(%rbp)
movq 0x50e49(%rip), %rsi     # 0xc5fb8
movq 0x50dfa(%rip), %rdx     # 0xc5f70
callq 0x6d470
jmp 0x752ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x751da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x751ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x751c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x6e458
leaq -0x1f0(%rbp), %rdi
callq 0x6e458
leaq -0x210(%rbp), %rdi
callq 0x6e458
testb $0x1, -0x211(%rbp)
jne 0x751e5
jmp 0x751f1
movq -0x290(%rbp), %rdi
callq 0x69830
jmp 0x75271
jmp 0x751f5
jmp 0x751f7
movb $0x0, -0x212(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x68900
movq %rax, -0x298(%rbp)
jmp 0x75213
movq -0x298(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x6d000
movq %rax, -0x2a0(%rbp)
jmp 0x7522c
movq -0x2a0(%rbp), %rsi
movq -0x230(%rbp), %rdi
callq 0x67550
jmp 0x75241
movb $0x1, -0x212(%rbp)
movl $0x1, -0x40(%rbp)
testb $0x1, -0x212(%rbp)
jne 0x75264
movq -0x230(%rbp), %rdi
callq 0x6b680
jmp 0x75266
leaq -0x78(%rbp), %rdi
callq 0x6e458
jmp 0x7527c
leaq -0x78(%rbp), %rdi
callq 0x6e458
jmp 0x75287
leaq -0x30(%rbp), %rdi
callq 0x6b680
jmp 0x75292
leaq -0x30(%rbp), %rdi
callq 0x6b680
jmp 0x752a2
movq -0x228(%rbp), %rax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x6d7b0
nopl (%rax,%rax)