// File: rofl0r[P]ascc/cc_compiledscript.cpp
// Function: ccCompiledScript::remove_any_import(char const*, SymbolDef*)
int ccCompiledScript::remove_any_import (const char*namm, SymbolDef *oldSym) {
    // Remove any import with the specified name
    int i, sidx;
    sidx = sym.find(namm);
    if (sidx < 0)
        return 0;
    if ((sym.entries[sidx].flags & SFLG_IMPORTED) == 0)
        return 0;
    // if this import has been referenced, flag an error
    if (sym.entries[sidx].flags & SFLG_ACCESSED) {
        cc_error("Already referenced name as import; you must define it before using it");
        return -1;
    }
    // if they set the No Override Imports flag, don't allow it
    if (ccGetOption(SCOPT_NOIMPORTOVERRIDE)) {
        cc_error("Variable '%s' is already imported", namm);
        return -1;
    }

    if (oldSym) {
        // Copy the import declaration to a backup struct
        // This allows a type comparison to be done
        // strip the imported flag, since it the real def won't be
        oldSym->flags = sym.entries[sidx].flags & ~SFLG_IMPORTED;
        oldSym->stype = sym.entries[sidx].stype;
        oldSym->sscope = sym.entries[sidx].sscope;
        // Return size may have been unknown at the time of forward declaration. Check the actual return type for those cases.
        if(sym.entries[sidx].stype == SYM_FUNCTION && sym.entries[sidx].ssize == 0) {
            oldSym->ssize = sym.entries[sym.entries[sidx].funcparamtypes[0] & ~(STYPE_POINTER | STYPE_DYNARRAY)].ssize;
        } else {
            oldSym->ssize = sym.entries[sidx].ssize;
        }
        oldSym->arrsize = sym.entries[sidx].arrsize;
        if (sym.entries[sidx].stype == SYM_FUNCTION) {
            // <= because of return type
            for (i = 0; i <= sym.entries[sidx].get_num_args(); i++) {
                oldSym->funcparamtypes[i] = sym.entries[sidx].funcparamtypes[i];
                oldSym->funcParamDefaultValues[i] = sym.entries[sidx].funcParamDefaultValues[i];
                oldSym->funcParamHasDefaultValues[i] = sym.entries[sidx].funcParamHasDefaultValues[i];
            }
        }
    }

    // remove its type so that it can be declared
    sym.entries[sidx].stype = 0;
    sym.entries[sidx].flags = 0;

    // check also for a number-of-parameters appended version
    char appended[200];
    sprintf(appended, "%s^", namm);
    int applen = strlen(appended);

    for (i = 0; i < numimports; i++) {
        if (strcmp(imports[i], namm) == 0) {
            // Just null the name of the import
            // DO NOT remove the import from the list, as some other
            // import indexes might already be referenced by the code
            // compiled so far.
            imports[i][0] = 0;
        }
        else if (strncmp(imports[i], appended, applen) == 0) {
            imports[i][0] = 0;
        }

    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x18(%rbp), %rsi
leaq 0x1ce63(%rip), %rdi     # 0x42870
callq 0x1f050
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0x25a27
movl $0x0, -0x4(%rbp)
jmp 0x25e22
movslq -0x28(%rbp), %rsi
leaq 0x1ce3e(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x4, %rax
cmpq $0x0, %rax
jne 0x25a55
movl $0x0, -0x4(%rbp)
jmp 0x25e22
movslq -0x28(%rbp), %rsi
leaq 0x1ce10(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x8, %rax
cmpq $0x0, %rax
je 0x25a91
leaq 0xe06c(%rip), %rdi      # 0x33aea
movb $0x0, %al
callq 0x1bac0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25e22
movl $0x20, %edi
callq 0x265a0
cmpl $0x0, %eax
je 0x25abe
movq -0x18(%rbp), %rsi
leaq 0xe085(%rip), %rdi      # 0x33b30
movb $0x0, %al
callq 0x1bac0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25e22
cmpq $0x0, -0x20(%rbp)
je 0x25d14
movslq -0x28(%rbp), %rsi
leaq 0x1cd9c(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rcx
andq $-0x5, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movslq -0x28(%rbp), %rsi
leaq 0x1cd78(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movw 0x20(%rax), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movslq -0x28(%rbp), %rsi
leaq 0x1cd59(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movw 0x40(%rax), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x18(%rax)
movslq -0x28(%rbp), %rsi
leaq 0x1cd39(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movswl 0x20(%rax), %eax
cmpl $0x4, %eax
jne 0x25bb0
movslq -0x28(%rbp), %rsi
leaq 0x1cd1c(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
cmpq $0x0, 0x38(%rax)
jne 0x25bb0
movslq -0x28(%rbp), %rsi
leaq 0x1cd01(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq %rax, %rdi
addq $0x58, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x18260
movq (%rax), %rsi
andq $-0x50000001, %rsi      # imm = 0xAFFFFFFF
leaq 0x1ccd7(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x38(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x25bd0
movslq -0x28(%rbp), %rsi
leaq 0x1ccb5(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x38(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movslq -0x28(%rbp), %rsi
leaq 0x1cc95(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x48(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movslq -0x28(%rbp), %rsi
leaq 0x1cc75(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movswl 0x20(%rax), %eax
cmpl $0x4, %eax
jne 0x25d12
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x114(%rbp)
movslq -0x28(%rbp), %rsi
leaq 0x1cc44(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq %rax, %rdi
callq 0x1e1c0
movl %eax, %ecx
movl -0x114(%rbp), %eax
cmpl %ecx, %eax
jg 0x25d10
movslq -0x28(%rbp), %rsi
leaq 0x1cc18(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq %rax, %rdi
addq $0x58, %rdi
movslq -0x24(%rbp), %rsi
callq 0x18260
movq (%rax), %rdx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, 0x28(%rax,%rcx,8)
movslq -0x28(%rbp), %rsi
leaq 0x1cbe4(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq %rax, %rdi
addq $0x70, %rdi
movslq -0x24(%rbp), %rsi
callq 0x17e00
movl (%rax), %edx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, 0xa8(%rax,%rcx,4)
movslq -0x28(%rbp), %rsi
leaq 0x1cbaf(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq %rax, %rdi
addq $0x88, %rdi
movslq -0x24(%rbp), %rsi
callq 0x181b0
movq %rax, -0x38(%rbp)
movq %rdx, -0x30(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x182f0
movb %al, %dl
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
andb $0x1, %dl
movb %dl, 0xe8(%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x25c18
jmp 0x25d12
jmp 0x25d14
movslq -0x28(%rbp), %rsi
leaq 0x1cb51(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movw $0x0, 0x20(%rax)
movslq -0x28(%rbp), %rsi
leaq 0x1cb37(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq $0x0, 0x28(%rax)
leaq -0x100(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0xddf6(%rip), %rsi      # 0x33b52
movb $0x0, %al
callq 0x40d0
leaq -0x100(%rbp), %rdi
callq 0x41d0
movl %eax, -0x104(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x110(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x58(%rcx), %eax
jge 0x25e1b
movq -0x110(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0x4510
cmpl $0x0, %eax
jne 0x25dc8
movq -0x110(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x0, (%rax)
jmp 0x25e0b
movq -0x110(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x100(%rbp), %rsi
movslq -0x104(%rbp), %rdx
callq 0x4210
cmpl $0x0, %eax
jne 0x25e09
movq -0x110(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x0, (%rax)
jmp 0x25e0b
jmp 0x25e0d
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x25d7c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop