// File: Fohlen[P]cubescript-standalone/command.cpp
// Function: compilelookup(vector<unsigned int>&, char const*&, int)
static void compilelookup(vector<uint> &code, const char *&p, int ltype)
{
    char *lookup = NULL;
    int lookuplen = 0;
    switch(*++p)
    {
        case '(':
        case '[':
            if(!compileword(code, p, VAL_STR, lookup, lookuplen)) goto invalid;
            break;
        case '$':
            compilelookup(code, p, VAL_STR);
            break;
        case '\"':
            lookup = cutstring(p, lookuplen);
            goto lookupid;
        default:
        {
            lookup = cutword(p, lookuplen);
            if(!lookup) goto invalid;
        lookupid:
            ident *id = newident(lookup, IDF_UNKNOWN);
            if(id) switch(id->type)
            {
                case ID_VAR: code.add(CODE_IVAR|((ltype >= VAL_ANY ? VAL_INT : ltype)<<CODE_RET)|(id->index<<8)); goto done;
                case ID_FVAR: code.add(CODE_FVAR|((ltype >= VAL_ANY ? VAL_FLOAT : ltype)<<CODE_RET)|(id->index<<8)); goto done;
                case ID_SVAR: code.add(CODE_SVAR|((ltype >= VAL_ANY ? VAL_STR : ltype)<<CODE_RET)|(id->index<<8)); goto done;
                case ID_ALIAS: code.add((id->index < MAXARGS ? CODE_LOOKUPARG : CODE_LOOKUP)|((ltype >= VAL_ANY ? VAL_STR : ltype)<<CODE_RET)|(id->index<<8)); goto done;
                case ID_COMMAND:
                {
                    int comtype = CODE_COM, numargs = 0;
                    code.add(CODE_ENTER);
                    for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt)
                    {
                        case 's': compilestr(code, NULL, 0, true); numargs++; break;
                        case 'i': compileint(code); numargs++; break;         
                        case 'b': compileint(code, INT_MIN); numargs++; break;
                        case 'f': compilefloat(code); numargs++; break;
                        case 't': compilenull(code); numargs++; break;
                        case 'e': compileblock(code); numargs++; break;
                        case 'r': compileident(code); numargs++; break;
                        case '$': compileident(code, id); numargs++; break;
                        case 'N': compileint(code, -1); numargs++; break;
                        case 'C': comtype = CODE_COMC; numargs = 1; goto endfmt;
                        case 'V': comtype = CODE_COMV; numargs = 2; goto endfmt;
                        case '1': case '2': case '3': case '4': break; 
                    }
                endfmt:
                    code.add(comtype|(ltype < VAL_ANY ? ltype<<CODE_RET : 0)|(id->index<<8));
                    code.add(CODE_EXIT|(ltype < VAL_ANY ? ltype<<CODE_RET : 0));
                    goto done;
                }
                default: goto invalid;
            }
            compilestr(code, lookup, lookuplen, true);
            break;
        }
    }
    code.add(CODE_LOOKUPU|((ltype < VAL_ANY ? ltype<<CODE_RET : 0)));
done:
    delete[] lookup;
    switch(ltype)
    {
        case VAL_CODE: code.add(CODE_COMPILE); break;
        case VAL_IDENT: code.add(CODE_IDENTU); break;
    }
    return;
invalid:
    switch(ltype)
    {
        case VAL_NULL: case VAL_ANY: compilenull(code); break;
        default: compileval(code, ltype, NULL, 0); break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
incq %rdx
movq %rdx, (%rcx)
movsbl 0x1(%rax), %eax
movl %eax, -0x70(%rbp)
subl $0x22, %eax
je 0x11190
jmp 0x11133
movl -0x70(%rbp), %eax
subl $0x24, %eax
je 0x11179
jmp 0x1113d
movl -0x70(%rbp), %eax
subl $0x28, %eax
je 0x11151
jmp 0x11147
movl -0x70(%rbp), %eax
subl $0x5b, %eax
jne 0x111a3
jmp 0x11151
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x3, %edx
leaq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %r8
callq 0x10970
testb $0x1, %al
jne 0x11174
jmp 0x11674
jmp 0x115c9
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x3, %edx
callq 0x110f0
jmp 0x115c9
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x11040
movq %rax, -0x20(%rbp)
jmp 0x111c2
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x11c70
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x111c0
jmp 0x11674
jmp 0x111c2
movq -0x20(%rbp), %rdi
movl $0x20, %esi
callq 0x4e50
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x115b4
movq -0x30(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x78(%rbp)
subq $0x4, %rax
ja 0x115af
movq -0x78(%rbp), %rax
leaq 0xc5a6(%rip), %rcx      # 0x1d7a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpl $0x4, -0x14(%rbp)
jl 0x11222
movl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x1122b
movl -0x14(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %eax
shll $0x6, %eax
orl $0x1a, %eax
movq -0x30(%rbp), %rcx
movl 0x3c(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
leaq -0x34(%rbp), %rsi
callq 0x16f60
jmp 0x1160e
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpl $0x4, -0x14(%rbp)
jl 0x11276
movl $0x2, %eax
movl %eax, -0x94(%rbp)
jmp 0x1127f
movl -0x14(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x90(%rbp), %rdi
movl -0x94(%rbp), %eax
shll $0x6, %eax
orl $0x1e, %eax
movq -0x30(%rbp), %rcx
movl 0x3c(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x38(%rbp)
leaq -0x38(%rbp), %rsi
callq 0x16f60
jmp 0x1160e
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpl $0x4, -0x14(%rbp)
jl 0x112cd
movl $0x3, %eax
movl %eax, -0xa4(%rbp)
jmp 0x112d6
movl -0x14(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0xa0(%rbp), %rdi
movl -0xa4(%rbp), %eax
shll $0x6, %eax
orl $0x18, %eax
movq -0x30(%rbp), %rcx
movl 0x3c(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x3c(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0x16f60
jmp 0x1160e
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movl 0x3c(%rax), %edx
movl $0x20, %eax
movl $0x22, %ecx
cmpl $0x19, %edx
cmovll %ecx, %eax
movl %eax, -0xa8(%rbp)
cmpl $0x4, -0x14(%rbp)
jl 0x11341
movl $0x3, %eax
movl %eax, -0xb4(%rbp)
jmp 0x1134a
movl -0x14(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0xb0(%rbp), %rdi
movl -0xa8(%rbp), %eax
movl -0xb4(%rbp), %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movl 0x3c(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
callq 0x16f60
jmp 0x1160e
movl $0x10, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movl $0x3, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x16f60
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpb $0x0, (%rax)
je 0x11507
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
addl $-0x24, %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x50, %eax
ja 0x114f4
movq -0xc0(%rbp), %rax
leaq 0xc3d4(%rip), %rcx      # 0x1d7b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x10b10
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10d70
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
movl $0x80000000, %esi       # imm = 0x80000000
callq 0x10d00
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10dc0
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
callq 0x10e10
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
callq 0x10e40
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10ed0
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x10f30
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x10d00
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x114f4
movl $0x12, -0x44(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x11509
movl $0x13, -0x44(%rbp)
movl $0x2, -0x48(%rbp)
jmp 0x11509
jmp 0x114f4
jmp 0x114f6
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x113ad
jmp 0x11509
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0xc4(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x11531
movl -0x14(%rbp), %eax
shll $0x6, %eax
movl %eax, -0xd4(%rbp)
jmp 0x1153b
xorl %eax, %eax
movl %eax, -0xd4(%rbp)
jmp 0x1153b
movq -0xd0(%rbp), %rdi
movl -0xc4(%rbp), %eax
movl -0xd4(%rbp), %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movl 0x3c(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x5c(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0x16f60
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x11587
movl -0x14(%rbp), %eax
shll $0x6, %eax
movl %eax, -0xe4(%rbp)
jmp 0x11591
xorl %eax, %eax
movl %eax, -0xe4(%rbp)
jmp 0x11591
movq -0xe0(%rbp), %rdi
movl -0xe4(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x60(%rbp)
leaq -0x60(%rbp), %rsi
callq 0x16f60
jmp 0x1160e
jmp 0x11674
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
movl $0x1, %ecx
callq 0x10b10
movq -0x8(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x115e8
movl -0x14(%rbp), %eax
shll $0x6, %eax
movl %eax, -0xf4(%rbp)
jmp 0x115f2
xorl %eax, %eax
movl %eax, -0xf4(%rbp)
jmp 0x115f2
movq -0xf0(%rbp), %rdi
movl -0xf4(%rbp), %eax
orl $0x21, %eax
movl %eax, -0x64(%rbp)
leaq -0x64(%rbp), %rsi
callq 0x16f60
movq -0x20(%rbp), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0x1162b
movq -0x100(%rbp), %rdi
callq 0x1b110
movl -0x14(%rbp), %eax
movl %eax, -0x104(%rbp)
subl $0x5, %eax
je 0x11648
jmp 0x1163b
movl -0x104(%rbp), %eax
subl $0x7, %eax
je 0x1165e
jmp 0x11672
movq -0x8(%rbp), %rdi
movl $0xa, -0x68(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x16f60
jmp 0x11672
movq -0x8(%rbp), %rdi
movl $0xe, -0x6c(%rbp)
leaq -0x6c(%rbp), %rsi
callq 0x16f60
jmp 0x1169d
movl -0x14(%rbp), %eax
testl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
je 0x11680
jmp 0x1168b
movq -0x8(%rbp), %rdi
callq 0x10e10
jmp 0x1169d
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x11cf0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)