// File: rofl0r[P]ascc/cc_compiledscript.cpp
// Function: ccCompiledScript::add_new_export(char const*, int, long, int)
int ccCompiledScript::add_new_export(const char*namm,int etype,long eoffs, int numArgs)
{
    if (numexports >= exportsCapacity)
    {
        exportsCapacity += 1000;
        exports = (char**)realloc(exports, sizeof(char*) * exportsCapacity);
        export_addr = (int32_t*)realloc(export_addr, sizeof(int32_t) * exportsCapacity);
    }
    if (eoffs >= 0x00ffffff) {
        cc_error("export offset too high; script data size too large?");
        return -1;
    }
    char *newName = (char*)malloc(strlen(namm)+20);
    strcpy(newName, namm);
    // mangle the name for functions to record parameters
    if (etype == EXPORT_FUNCTION) {
        char tempAddition[20];
        sprintf(tempAddition, "$%d", numArgs);
        strcat(newName, tempAddition);
    }
    // Check if it's already exported
    for (int aa = 0; aa < numexports; aa++) {
        if (strcmp(exports[aa], newName) == 0) {
            free(newName);
            return aa;
        }
    }
    exports[numexports] = newName;

    export_addr[numexports] = eoffs | ((long)etype << 24L);
    numexports++;
    return numexports-1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x60(%rbp)
movl 0x70(%rcx), %eax
cmpl 0x5c(%rcx), %eax
jl 0x25ea3
movq -0x60(%rbp), %rax
movl 0x5c(%rax), %ecx
addl $0x3e8, %ecx            # imm = 0x3E8
movl %ecx, 0x5c(%rax)
movq 0x60(%rax), %rdi
movslq 0x5c(%rax), %rsi
shlq $0x3, %rsi
callq 0x4620
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x60(%rax)
movq 0x68(%rax), %rdi
movslq 0x5c(%rax), %rsi
shlq $0x2, %rsi
callq 0x4620
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x68(%rax)
cmpq $0xffffff, -0x28(%rbp)  # imm = 0xFFFFFF
jl 0x25ec7
leaq 0xdca2(%rip), %rdi      # 0x33b56
movb $0x0, %al
callq 0x1bac0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25fa6
movq -0x18(%rbp), %rdi
callq 0x41d0
movq %rax, %rdi
addq $0x14, %rdi
callq 0x44f0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x4350
cmpl $0x1, -0x1c(%rbp)
jne 0x25f15
leaq -0x50(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq 0xdc89(%rip), %rsi      # 0x33b8a
movb $0x0, %al
callq 0x40d0
movq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4690
movl $0x0, -0x54(%rbp)
movq -0x60(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x70(%rcx), %eax
jge 0x25f64
movq -0x60(%rbp), %rax
movq 0x60(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x38(%rbp), %rsi
callq 0x4510
cmpl $0x0, %eax
jne 0x25f57
movq -0x38(%rbp), %rdi
callq 0x4460
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25fa6
jmp 0x25f59
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x25f1c
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rsi
movq 0x60(%rax), %rcx
movslq 0x70(%rax), %rdx
movq %rsi, (%rcx,%rdx,8)
movq -0x28(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
shlq $0x18, %rdx
orq %rdx, %rcx
movl %ecx, %esi
movq 0x68(%rax), %rcx
movslq 0x70(%rax), %rdx
movl %esi, (%rcx,%rdx,4)
movl 0x70(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x70(%rax)
movl 0x70(%rax), %eax
subl $0x1, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop