// File: /workspace/github_c_makefile_star2_O3/heltilda[P]cicada/ccmain.c
// Function: cicadaMain
ccInt cicadaMain(ccInt argc, char **argv)
{
    ccInt mainErrorCode = 0, startupFileNameLength, loopCompiler, rtrn;
    char *firstChar, *startupFileName;
    const char *defaultStartupFileName = "start.cicada";
    linkedlist sourceCode;
    
#ifdef CicadaTest_ON
    if (argc == 0)  TestCicada();
#endif
    
    
        // Initialize the random number generator.
    
    srand((int) time(0));
    rand();  rand();  rand();  rand();      // otherwise the first couple of random numbers are suspiciously consistent
    
    
        // initialize the compiler
    
    rtrn = newLinkedList(&allCompilers, 1, sizeof(compiler_type *), 1., ccFalse);
    if (rtrn == passed)  {
        whichCompiler = 1;
        currentCompiler = newCompiler(cicadaLanguage, cicadaLanguageNumCommands,
                        cicadaLanguageAssociativity, cicadaNumPrecedenceLevels, &rtrn);
        *(compiler_type **) element(&allCompilers, 1) = currentCompiler;         }
    
    if (rtrn != passed)  {
        printf("Error loading compiler (error code %i on command %i)\n", (int) errCode, (int) errPosition);
        return rtrn;       }
    
    
        // Set up a buffer for loading the script
    
    rtrn = newLinkedList(&sourceCode, 0, sizeof(char), 0, ccFalse);
    if (rtrn != passed)  {  printf("cicada:  %s\n", errorStrings[rtrn]);  return 1;  }
    
    
        // Open the file specified in the first argument, if given; otherwise, open start.cicada
    
    if (argc > 1)  {  printf("usage: cicada [source_file]\n");  return 1;  }
    
    if (argc == 0)  startupFileName = (char *) defaultStartupFileName;
    else  startupFileName = (char *) argv[0];
    
    startupFileNameLength = 0;
    while (startupFileName[startupFileNameLength] != 0)  startupFileNameLength++;
    
    if (loadFile(startupFileName, &sourceCode, ccTrue) != passed)  {
        printf("Error:  Could not open %s\n", startupFileName);
        return 1;      }
    
    rtrn = defragmentLinkedList(&sourceCode);
    if (rtrn != passed)  {  printf("cicada:  %s\n", errorStrings[rtrn]);  return 1;  }
    
    
        // (compile and) transform the starting script.
    
    firstChar = (char *) element(&sourceCode, 1);
    rtrn = compile(currentCompiler, firstChar);
    
    if (rtrn != passed)  {
        errCode = rtrn;
        printError(startupFileName, startupFileNameLength, firstChar, errPosition-1, ccFalse);
        return rtrn;    }
    else if (compilerWarning != passed)  {
        warningCode = compilerWarning;
        printError(startupFileName, startupFileNameLength, firstChar, errPosition-1, ccFalse);     }
    
    defragmentLinkedList(&(currentCompiler->opCharNum));
    
    
        // initialize all the global variables used by Cicada
    
    rtrn = initCicada(LL_int(&(currentCompiler->bytecode), 1), currentCompiler->bytecode.elementNum,
            startupFileName, startupFileNameLength, firstChar, LL_int(&(currentCompiler->opCharNum), 1), sourceCode.elementNum);
    if (rtrn != passed)  {  printf("Cicada error:  initialization returned \"%s\"\n", (char *) errorStrings[rtrn]);  return 1;  }
    
    
        // check the bytecode of the script (should be OK if we're using the 'default' Cicada language)
    
    startCodePtr = PCCodeRef.code_ptr;
    endCodePtr = startCodePtr + currentCompiler->bytecode.elementNum;
    
    pcCodePtr = startCodePtr;
    checkBytecode();
    
    if ((errCode != passed) || (warningCode != passed))  {
        
        printError(startupFileName, startupFileNameLength, firstChar, *LL_int(&(currentCompiler->opCharNum), errIndex)-1, ccFalse);
        
        if (errCode != passed)  return errCode;         }
    
    
        // run the script
    
    beginExecution(&PCCodeRef, ccFalse, 0, 1, 0);
    if (errCode == return_flag)  errCode = passed;
    
    
        // if there was an error in the script, print out the line that caused it (if possible)
    
    if ((errCode != passed) && (errCode != finished_signal))  {
        
        storedCodeType *errorBaseScript = storedCode(errScript.PLL_index);
        ccInt errCharNum = 0;
        
        if (errorBaseScript->opCharNum != NULL)  errCharNum = errorBaseScript->opCharNum[
                    errIndex + (ccInt) (errScript.code_ptr - errorBaseScript->bytecode) - 1] - 1;
        
        printError(errorBaseScript->fileName, -1, errorBaseScript->sourceCode, errCharNum, ccFalse);
        
        mainErrorCode = 1;         }
    
    
        // clean up after ourselves and exit
    
#ifdef CicadaTest_ON
test_MM_CountRefs();
#endif
    
    cleanUp();
    
    for (loopCompiler = 1; loopCompiler <= allCompilers.elementNum; loopCompiler++)
        freeCompiler(*(compiler_type **) element(&allCompilers, loopCompiler));
    deleteLinkedList(&allCompilers);
    
    deleteLinkedList(&sourceCode);
    
    return mainErrorCode;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movl %edi, %r15d
xorl %edi, %edi
callq 0x8130
movl %eax, %edi
callq 0x8150
callq 0x80b0
callq 0x80b0
callq 0x80b0
callq 0x80b0
movl $0x288, %edi            # imm = 0x288
addq 0x8195(%rip), %rdi      # 0x21fe0
movsd 0x11b5(%rip), %xmm0     # 0x1b008
movl $0x1, %esi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x83e1
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x19eda
leaq 0xb8ed(%rip), %rbp      # 0x25760
movl $0x1, 0x1f0(%rbp)
leaq 0x97fc(%rip), %rax      # 0x23680
movl (%rax), %esi
leaq 0x97f7(%rip), %rax      # 0x23684
movl (%rax), %ecx
leaq 0x872a(%rip), %rdi      # 0x225c0
leaq 0x86e3(%rip), %rdx      # 0x22580
movq %rbx, 0x18(%rsp)
leaq 0x14(%rsp), %r12
movq %r12, %r8
callq 0x95fc
movq %rax, %r13
leaq 0xb3e7(%rip), %rbx      # 0x252a0
movq %rax, (%rbx)
leaq 0x288(%rbp), %r14
movq %r14, %rdi
movl $0x1, %esi
callq 0x91d8
movq %r13, (%rax)
cmpl $0x0, (%r12)
je 0x19f11
leaq 0xb87f(%rip), %rax      # 0x25760
movl 0x18(%rax), %esi
leaq 0xb3b5(%rip), %rax      # 0x252a0
movl 0x8(%rax), %edx
leaq 0x22f0(%rip), %rdi      # 0x1c1e5
xorl %eax, %eax
callq 0x8050
movl 0x14(%rsp), %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %r13
movq %rbx, %r12
movq %rbp, %rbx
leaq 0x28(%rsp), %rdi
movl $0x1, %ebp
xorps %xmm0, %xmm0
xorl %esi, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x83e1
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x19f5a
cltq
leaq 0xb1ba(%rip), %rcx      # 0x25100
movq (%rcx,%rax,8), %rsi
leaq 0x22ca(%rip), %rdi      # 0x1c21b
xorl %eax, %eax
callq 0x8050
jmp 0x19f00
cmpl $0x2, %r15d
jl 0x19f6e
leaq 0x239f(%rip), %rdi      # 0x1c306
callq 0x8200
jmp 0x19f00
testl %r15d, %r15d
je 0x19f7d
movq 0x18(%rsp), %rax
movq (%rax), %r14
jmp 0x19f84
leaq 0x2254(%rip), %r14      # 0x1c1d8
xorl %r15d, %r15d
cmpb $0x0, (%r14,%r15)
leaq 0x1(%r15), %r15
jne 0x19f87
leaq 0x28(%rsp), %rsi
movl $0x1, %ebp
movq %r14, %rdi
movl $0x1, %edx
callq 0x194ec
testl %eax, %eax
je 0x19fb9
leaq 0x2274(%rip), %rdi      # 0x1c228
movq %r14, %rsi
jmp 0x19f51
leaq 0x28(%rsp), %rdi
callq 0x8c00
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x19f3d
decq %r15
leaq 0x28(%rsp), %rdi
movl $0x1, %esi
callq 0x91d8
movq %rax, %rbp
movq (%r12), %rdi
movq %rax, %rsi
callq 0xb7fe
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x1a018
movl %eax, 0x18(%rbx)
movl 0x8(%r12), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x1a237
jmp 0x19efc
movl 0xc(%r12), %eax
testl %eax, %eax
je 0x1a03c
movl %eax, 0x20(%rbx)
movl 0x8(%r12), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x1a237
movl $0x1480, %eax           # imm = 0x1480
movq (%r12), %rdi
addq %rax, %rdi
callq 0x8c00
movl $0x1460, %edi           # imm = 0x1460
addq (%r12), %rdi
movq %rbp, %r12
movl $0x1, %ebp
movl $0x1, %esi
callq 0x91d8
movq %rax, 0x18(%rsp)
leaq 0xb22c(%rip), %rax      # 0x252a0
movq (%rax), %rdi
movl 0x1460(%rdi), %eax
movl %eax, 0x24(%rsp)
movl $0x1480, %eax           # imm = 0x1480
addq %rax, %rdi
movl $0x1, %esi
callq 0x91d8
movl 0x28(%rsp), %ecx
movl %ecx, (%rsp)
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
movq %r14, %rdx
movl %r15d, %ecx
movq %r12, %r8
movq %rax, %r9
callq 0x1a51d
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x1a0d5
cltq
leaq 0xb03b(%rip), %rcx      # 0x25100
movq (%rcx,%rax,8), %rsi
leaq 0x2173(%rip), %rdi      # 0x1c243
jmp 0x19f51
movq %r12, 0x18(%rsp)
movq 0x218(%rbx), %rax
movq %rax, 0xc0(%rbx)
leaq 0xb1b1(%rip), %rbp      # 0x252a0
movq (%rbp), %rcx
movslq 0x1460(%rcx), %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0xc8(%rbx)
movq %rax, 0xb8(%rbx)
callq 0x15af5
movl 0x20(%rbx), %eax
orl 0x18(%rbx), %eax
je 0x1a157
movl $0x1480, %edi           # imm = 0x1480
addq (%rbp), %rdi
leaq 0xb637(%rip), %r12      # 0x25760
movl 0x1c(%r12), %esi
callq 0x91d8
movl (%rax), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq 0x18(%rsp), %rdx
xorl %r8d, %r8d
callq 0x1a237
movl 0x18(%r12), %ebp
testl %ebp, %ebp
jne 0x19f00
leaq 0xb602(%rip), %r14      # 0x25760
leaq 0x208(%r14), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
xorl %edx, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x1029f
movl 0x18(%r14), %eax
testl %eax, %eax
je 0x1a1e7
cmpl $0x33, %eax
je 0x1a1e7
cmpl $0x32, %eax
jne 0x1a193
movl $0x0, 0x18(%rbx)
jmp 0x1a1e7
movq %rbx, %r14
movl 0x28(%rbx), %esi
leaq 0xb508(%rip), %rdi      # 0x256a8
callq 0x91d8
movq 0x18(%rax), %rcx
testq %rcx, %rcx
je 0x1a1cc
movslq 0x1c(%r14), %rdx
movq 0x38(%r14), %rsi
subq 0x10(%rax), %rsi
shrq $0x2, %rsi
movslq %esi, %rsi
addq %rdx, %rsi
movl -0x4(%rcx,%rsi,4), %ecx
decl %ecx
jmp 0x1a1ce
xorl %ecx, %ecx
movq (%rax), %rdi
movq 0x8(%rax), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x1a237
movl $0x1, %ebp
callq 0x1a7d0
cmpl $0x0, 0x288(%rbx)
jle 0x1a217
xorl %r14d, %r14d
incl %r14d
movq %r13, %rdi
movl %r14d, %esi
callq 0x91d8
movq (%rax), %rdi
callq 0xa407
cmpl 0x288(%rbx), %r14d
jl 0x1a1f8
movl $0x288, %edi            # imm = 0x288
addq 0x7dbd(%rip), %rdi      # 0x21fe0
callq 0x847e
leaq 0x28(%rsp), %rdi
callq 0x847e
jmp 0x19f00