// File: rofl0r[P]ascc/cs_parser.cpp
// Function: remove_locals(int, int, ccCompiledScript*)
int remove_locals(int from_level, int just_count, ccCompiledScript *scrip) {
    int cc, totalsub = 0;
    int zeroPtrCmd = SCMD_MEMZEROPTR;
    if (from_level == 0)
        zeroPtrCmd = SCMD_MEMZEROPTRND;

    for (cc=0; (size_t)cc<sym.entries.size();cc++) {
        if ((sym.entries[cc].sscope > from_level) && (sym.entries[cc].stype == SYM_LOCALVAR)) {
            // caller will sort out stack, so ignore parameters
            if ((sym.entries[cc].flags & SFLG_PARAMETER)==0) {
                if (sym.entries[cc].flags & SFLG_DYNAMICARRAY)
                    totalsub += 4;
                else
                {
                    // remove all elements if array
                    if (sym.entries[cc].flags & SFLG_ARRAY) {
                        if(sym.entries[cc].arrsize == 0) /* raw pointer */
                          totalsub += 4;
                        else
                          totalsub += (sym.entries[cc].arrsize) * sym.entries[cc].ssize;
                    } else
                        totalsub += sym.entries[cc].ssize;
                }
                if (sym.entries[cc].flags & SFLG_STRBUFFER)
                    totalsub += STRING_LENGTH;
            }
            // release the pointer reference if applicable
            if (sym.entries[cc].flags & SFLG_THISPTR) { }
            else if (((sym.entries[cc].flags & SFLG_POINTER) != 0) ||
                ((sym.entries[cc].flags & SFLG_DYNAMICARRAY) != 0))
            {
                free_pointer(scrip->cur_sp - sym.entries[cc].soffs, zeroPtrCmd, cc, scrip);
            }
            else if (sym.entries[sym.entries[cc].vartype].flags & SFLG_STRUCTTYPE) {
                // a struct -- free any pointers it contains
                free_pointers_from_struct(cc, scrip);
            }

            if (just_count == 0) {
                sym.entries[cc].stype = 0;
                sym.entries[cc].sscope = 0;
                sym.entries[cc].flags = 0;
            }
        }
    }
    return totalsub;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x31, -0x1c(%rbp)
cmpl $0x0, -0x4(%rbp)
jne 0x757d
movl $0x45, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq 0x3b2dd(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e80
movq %rax, %rcx
movq -0x28(%rbp), %rax
cmpq %rcx, %rax
jae 0x788c
movslq -0x14(%rbp), %rsi
leaq 0x3b2b9(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movswl 0x40(%rax), %eax
cmpl -0x4(%rbp), %eax
jle 0x787c
movslq -0x14(%rbp), %rsi
leaq 0x3b298(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movswl 0x20(%rax), %eax
cmpl $0x2, %eax
jne 0x787c
movslq -0x14(%rbp), %rsi
leaq 0x3b277(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x1, %rax
cmpq $0x0, %rax
jne 0x7726
movslq -0x14(%rbp), %rsi
leaq 0x3b251(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x40000, %rax          # imm = 0x40000
cmpq $0x0, %rax
je 0x7646
movl -0x18(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x18(%rbp)
jmp 0x76f7
movslq -0x14(%rbp), %rsi
leaq 0x3b21f(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x2, %rax
cmpq $0x0, %rax
je 0x76d3
movslq -0x14(%rbp), %rsi
leaq 0x3b1fd(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
cmpq $0x0, 0x48(%rax)
jne 0x768e
movl -0x18(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x18(%rbp)
jmp 0x76d1
movslq -0x14(%rbp), %rsi
leaq 0x3b1d7(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x48(%rax), %rax
movq %rax, -0x30(%rbp)
movslq -0x14(%rbp), %rsi
leaq 0x3b1bb(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq -0x30(%rbp), %rcx
imulq 0x38(%rax), %rcx
movslq -0x18(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x18(%rbp)
jmp 0x76f5
movslq -0x14(%rbp), %rsi
leaq 0x3b192(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x38(%rax), %rcx
movslq -0x18(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x18(%rbp)
jmp 0x76f7
movslq -0x14(%rbp), %rsi
leaq 0x3b16e(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x10, %rax
cmpq $0x0, %rax
je 0x7724
movl -0x18(%rbp), %eax
addl $0xc8, %eax
movl %eax, -0x18(%rbp)
jmp 0x7726
movslq -0x14(%rbp), %rsi
leaq 0x3b13f(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x800, %rax            # imm = 0x800
cmpq $0x0, %rax
je 0x774f
jmp 0x7824
movslq -0x14(%rbp), %rsi
leaq 0x3b116(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x100, %rax            # imm = 0x100
cmpq $0x0, %rax
jne 0x7797
movslq -0x14(%rbp), %rsi
leaq 0x3b0f2(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x40000, %rax          # imm = 0x40000
cmpq $0x0, %rax
je 0x77db
movq -0x10(%rbp), %rax
movq 0x8d40(%rax), %rax
movq %rax, -0x38(%rbp)
movslq -0x14(%rbp), %rsi
leaq 0x3b0bf(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq %rax, %rcx
movq -0x38(%rbp), %rax
movslq 0x34(%rcx), %rcx
subq %rcx, %rax
movl %eax, %edi
movl -0x1c(%rbp), %esi
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rcx
callq 0x7200
jmp 0x7822
movslq -0x14(%rbp), %rsi
leaq 0x3b08a(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movswq 0x30(%rax), %rsi
leaq 0x3b075(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x400, %rax            # imm = 0x400
cmpq $0x0, %rax
je 0x7820
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x7320
jmp 0x7822
jmp 0x7824
cmpl $0x0, -0x8(%rbp)
jne 0x787a
movslq -0x14(%rbp), %rsi
leaq 0x3b03b(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movw $0x0, 0x20(%rax)
movslq -0x14(%rbp), %rsi
leaq 0x3b021(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movw $0x0, 0x40(%rax)
movslq -0x14(%rbp), %rsi
leaq 0x3b007(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq $0x0, 0x28(%rax)
jmp 0x787c
jmp 0x787e
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x7584
movl -0x18(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)