// File: Fohlen[P]cubescript-standalone/command.cpp
// Function: compileblock(vector<unsigned int>&, char const*&, int)
static void compileblock(vector<uint> &code, const char *&p, int wordtype)
{
    const char *line = p, *start = p;
    int concs = 0;
    for(int brak = 1; brak;)
    {
        p += strcspn(p, "@\"/[]\0");
        int c = *p++;
        switch(c)
        {
            case '\0':
                debugcodeline(line, "missing \"]\"");
                p--;
                goto done;
            case '\"':
                p = parsestring(p);
                if(*p=='\"') p++;
                break;
            case '/':
                if(*p=='/') p += strcspn(p, "\n\0");
                break;
            case '[': brak++; break;
            case ']': brak--; break;
            case '@': 
            {
                const char *esc = p;
                while(*p == '@') p++; 
                int level = p - (esc - 1);
                if(brak > level) continue;
                else if(brak < level) debugcodeline(line, "too many @s");
                if(!concs) code.add(CODE_ENTER);
                if(concs + 2 > MAXARGS)
                {
                    code.add(CODE_CONCW|RET_STR|(concs<<8));
                    concs = 1;
                }
                if(compileblockstr(code, start, esc-1, true)) concs++;
                if(compileblocksub(code, p)) concs++;
                if(!concs) code.pop();
                else start = p;
                break;
            }
        }
    }
done:
    if(p-1 > start) 
    {
        if(!concs) switch(wordtype)
        {
            case VAL_CODE:
            {
                p = start;
                int inst = code.length();
                code.add(CODE_BLOCK);
                code.add(CODE_OFFSET|((inst+2)<<8));
                compilestatements(code, p, VAL_ANY, ']');
                code.add(CODE_EXIT);
                code[inst] |= uint(code.length() - (inst + 1))<<8;
                return;
            }
            case VAL_IDENT:
            {
                char *name = newstring(start, p-1-start);
                compileident(code, name);
                delete[] name;
                return;
            }
        }
        compileblockstr(code, start, p-1, concs > 0);
        if(concs > 1) concs++;
    }        
    if(concs)
    {
        code.add(CODE_CONCM|(wordtype < VAL_ANY ? wordtype<<CODE_RET : RET_STR)|(concs<<8));
        code.add(CODE_EXIT|(wordtype < VAL_ANY ? wordtype<<CODE_RET : RET_STR));
    }
    switch(wordtype)
    {
        case VAL_CODE: if(!concs && p-1 <= start) compileblock(code); else code.add(CODE_COMPILE); break;
        case VAL_IDENT: if(!concs && p-1 <= start) compileident(code); else code.add(CODE_IDENTU); break;
        case VAL_STR: case VAL_NULL: case VAL_ANY:
            if(!concs && p-1 <= start) compilestr(code);
            break;
        default: 
            if(!concs) 
            {
                if(p-1 <= start) compileval(code, wordtype, NULL, 0);
                else code.add(CODE_FORCE|(wordtype<<CODE_RET));
            }
            break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x11942
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xc6aa(%rip), %rsi      # 0x1ddac
callq 0x2040
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq %rdx, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
incq %rdx
movq %rdx, (%rcx)
movsbl (%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x80(%rbp)
testl %eax, %eax
je 0x1177e
jmp 0x11739
movl -0x80(%rbp), %eax
subl $0x22, %eax
je 0x117a3
jmp 0x11743
movl -0x80(%rbp), %eax
subl $0x2f, %eax
je 0x117db
jmp 0x11751
movl -0x80(%rbp), %eax
subl $0x40, %eax
je 0x1182b
jmp 0x1175f
movl -0x80(%rbp), %eax
subl $0x5b, %eax
je 0x1180f
jmp 0x1176d
movl -0x80(%rbp), %eax
subl $0x5d, %eax
je 0x1181d
jmp 0x1193d
movq -0x20(%rbp), %rdi
leaq 0xcb80(%rip), %rsi      # 0x1e309
movb $0x0, %al
callq 0x10f80
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
jmp 0x11944
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x22, %eax
jne 0x117d6
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x1193d
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x1180a
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xc5b1(%rip), %rsi      # 0x1dda9
callq 0x2040
movq %rax, %rcx
movq -0x10(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x1193d
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1193d
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1193d
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x11855
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x11836
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
addq $-0x1, %rcx
subq %rcx, %rax
movl %eax, -0x44(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x44(%rbp), %eax
jle 0x11877
jmp 0x116ea
movl -0x30(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x11891
movq -0x20(%rbp), %rdi
leaq 0xca8b(%rip), %rsi      # 0x1e315
movb $0x0, %al
callq 0x10f80
jmp 0x11893
cmpl $0x0, -0x2c(%rbp)
jne 0x118ad
movq -0x8(%rbp), %rdi
movl $0x3, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
callq 0x16f60
movl -0x2c(%rbp), %eax
addl $0x2, %eax
cmpl $0x19, %eax
jle 0x118da
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %eax
shll $0x8, %eax
orl $0xd5, %eax
movl %eax, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x16f60
movl $0x1, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq $-0x1, %rdx
movl $0x1, %ecx
callq 0x11e40
testb $0x1, %al
jne 0x118fa
jmp 0x11903
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x120d0
testb $0x1, %al
jne 0x11916
jmp 0x1191f
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x11930
movq -0x8(%rbp), %rdi
callq 0x170a0
jmp 0x1193b
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x1193d
jmp 0x116ea
jmp 0x11944
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $-0x1, %rax
cmpq -0x28(%rbp), %rax
jbe 0x11ab4
cmpl $0x0, -0x2c(%rbp)
jne 0x11a7e
movl -0x14(%rbp), %eax
movl %eax, -0x84(%rbp)
subl $0x5, %eax
je 0x11987
jmp 0x11973
movl -0x84(%rbp), %eax
subl $0x7, %eax
je 0x11a2e
jmp 0x11a7c
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
callq 0x15e00
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl $0x9, -0x54(%rbp)
leaq -0x54(%rbp), %rsi
callq 0x16f60
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %eax
addl $0x2, %eax
shll $0x8, %eax
orl $0x1, %eax
movl %eax, -0x58(%rbp)
leaq -0x58(%rbp), %rsi
callq 0x16f60
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x4, %edx
movl $0x5d, %ecx
callq 0xfba0
movq -0x8(%rbp), %rdi
movl $0x4, -0x5c(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0x16f60
movq -0x8(%rbp), %rdi
callq 0x15e00
movl -0x50(%rbp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
shll $0x8, %eax
movl %eax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x15ea0
movl -0x88(%rbp), %ecx
orl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x11c66
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
addq $-0x1, %rsi
movq -0x28(%rbp), %rax
subq %rax, %rsi
callq 0x16150
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x10ed0
movq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x11a77
movq -0x90(%rbp), %rdi
callq 0x1b110
jmp 0x11c66
jmp 0x11a7e
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
addq $-0x1, %rdx
cmpl $0x0, -0x2c(%rbp)
setg %al
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x11e40
cmpl $0x1, -0x2c(%rbp)
jle 0x11ab2
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x11ab4
cmpl $0x0, -0x2c(%rbp)
je 0x11b56
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x11add
movl -0x14(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x9c(%rbp)
jmp 0x11aea
movl $0xc0, %eax
movl %eax, -0x9c(%rbp)
jmp 0x11aea
movq -0x98(%rbp), %rdi
movl -0x9c(%rbp), %eax
orl $0x16, %eax
movl -0x2c(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x6c(%rbp)
leaq -0x6c(%rbp), %rsi
callq 0x16f60
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x11b2d
movl -0x14(%rbp), %eax
shll $0x6, %eax
movl %eax, -0xac(%rbp)
jmp 0x11b3a
movl $0xc0, %eax
movl %eax, -0xac(%rbp)
jmp 0x11b3a
movq -0xa8(%rbp), %rdi
movl -0xac(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x70(%rbp)
leaq -0x70(%rbp), %rsi
callq 0x16f60
movl -0x14(%rbp), %eax
movq %rax, -0xb8(%rbp)
subq $0x7, %rax
ja 0x11c1e
movq -0xb8(%rbp), %rax
leaq 0xbd84(%rip), %rcx      # 0x1d8fc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, -0x2c(%rbp)
jne 0x11ba3
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $-0x1, %rax
cmpq -0x28(%rbp), %rax
ja 0x11ba3
movq -0x8(%rbp), %rdi
callq 0x10e40
jmp 0x11bb7
movq -0x8(%rbp), %rdi
movl $0xa, -0x74(%rbp)
leaq -0x74(%rbp), %rsi
callq 0x16f60
jmp 0x11c66
cmpl $0x0, -0x2c(%rbp)
jne 0x11be2
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $-0x1, %rax
cmpq -0x28(%rbp), %rax
ja 0x11be2
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10ed0
jmp 0x11bf6
movq -0x8(%rbp), %rdi
movl $0xe, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x16f60
jmp 0x11c66
cmpl $0x0, -0x2c(%rbp)
jne 0x11c1c
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $-0x1, %rax
cmpq -0x28(%rbp), %rax
ja 0x11c1c
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10ca0
jmp 0x11c66
cmpl $0x0, -0x2c(%rbp)
jne 0x11c64
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $-0x1, %rax
cmpq -0x28(%rbp), %rax
ja 0x11c49
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x11cf0
jmp 0x11c62
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %eax
shll $0x6, %eax
orl $0xb, %eax
movl %eax, -0x7c(%rbp)
leaq -0x7c(%rbp), %rsi
callq 0x16f60
jmp 0x11c64
jmp 0x11c66
addq $0xc0, %rsp
popq %rbp
retq
nop