// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %r14
movl %edi, %ebp
xorl %edi, %edi
callq 0x8130
movl %eax, %edi
callq 0x8150
callq 0x80b0
callq 0x80b0
callq 0x80b0
callq 0x80b0
movl $0x288, %edi            # imm = 0x288
addq 0x93f6(%rip), %rdi      # 0x20fe0
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movsd 0x1410(%rip), %xmm0     # 0x19008
xorl %ecx, %ecx
callq 0x83d5
movl %eax, 0x8(%rsp)
testl %eax, %eax
jne 0x17c6b
leaq 0xcb52(%rip), %r13      # 0x24760
movl $0x1, 0x1f0(%r13)
leaq 0xaa60(%rip), %rax      # 0x22680
movl (%rax), %esi
leaq 0xaa5b(%rip), %rax      # 0x22684
movl (%rax), %ecx
leaq 0x998e(%rip), %rdi      # 0x215c0
leaq 0x9947(%rip), %rdx      # 0x21580
leaq 0x8(%rsp), %rbx
movq %rbx, %r8
callq 0x9310
movq %rax, %r15
leaq 0xc650(%rip), %r12      # 0x242a0
movq %rax, (%r12)
leaq 0x288(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x9088
movq %r15, (%rax)
cmpl $0x0, (%rbx)
je 0x17ca2
leaq 0xcaee(%rip), %rax      # 0x24760
movl 0x18(%rax), %esi
leaq 0xc624(%rip), %rax      # 0x242a0
movl 0x8(%rax), %edx
leaq 0x251f(%rip), %rdi      # 0x1a1a5
xorl %eax, %eax
callq 0x8050
movl 0x8(%rsp), %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rsp), %rdi
pushq $0x1
popq %rbx
xorps %xmm0, %xmm0
xorl %esi, %esi
movl %ebx, %edx
xorl %ecx, %ecx
callq 0x83d5
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x17cdd
cltq
leaq 0xc437(%rip), %rcx      # 0x24100
movq (%rcx,%rax,8), %rsi
leaq 0x2507(%rip), %rdi      # 0x1a1db
xorl %eax, %eax
callq 0x8050
jmp 0x17c91
cmpl $0x2, %ebp
jl 0x17cf0
leaq 0x25dd(%rip), %rdi      # 0x1a2c6
callq 0x8200
jmp 0x17c91
testl %ebp, %ebp
je 0x17cf9
movq (%r14), %r14
jmp 0x17d00
leaq 0x2498(%rip), %r14      # 0x1a198
xorl %r15d, %r15d
cmpb $0x0, (%r14,%r15)
leaq 0x1(%r15), %r15
jne 0x17d03
leaq 0x18(%rsp), %rsi
pushq $0x1
popq %rbx
movq %r14, %rdi
movl %ebx, %edx
callq 0x17326
testl %eax, %eax
je 0x17d30
leaq 0x24bd(%rip), %rdi      # 0x1a1e8
movq %r14, %rsi
jmp 0x17cd4
leaq 0x18(%rsp), %rdi
callq 0x8b50
movl %eax, 0x8(%rsp)
testl %eax, %eax
jne 0x17cc0
decq %r15
leaq 0x18(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x9088
movq %rax, %rbp
movq (%r12), %rdi
movq %rax, %rsi
callq 0xb2a8
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x17d8e
movl %eax, 0x18(%r13)
movl 0x8(%r12), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x17f79
jmp 0x17c8d
movl 0xc(%r12), %eax
testl %eax, %eax
je 0x17db3
movl %eax, 0x20(%r13)
movl 0x8(%r12), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x17f79
movl $0x1480, %eax           # imm = 0x1480
movq (%r12), %rdi
addq %rax, %rdi
callq 0x8b50
movl $0x1460, %edi           # imm = 0x1460
addq (%r12), %rdi
pushq $0x1
popq %rbx
movl %ebx, %esi
callq 0x9088
movq %rax, 0x10(%rsp)
movq (%r12), %rdi
movl 0x1460(%rdi), %eax
movl %eax, 0xc(%rsp)
movl $0x1480, %eax           # imm = 0x1480
addq %rax, %rdi
movl %ebx, %esi
callq 0x9088
movl 0x18(%rsp), %ecx
movl %ecx, (%rsp)
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movq %r14, %rdx
movl %r15d, %ecx
movq %rbp, %r8
movq %rax, %r9
callq 0x18210
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x17e3b
cltq
leaq 0xc2d5(%rip), %rcx      # 0x24100
movq (%rcx,%rax,8), %rsi
leaq 0x23cd(%rip), %rdi      # 0x1a203
jmp 0x17cd4
movq 0x218(%r13), %rax
movq %rax, 0xc0(%r13)
movq (%r12), %rcx
movslq 0x1460(%rcx), %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0xc8(%r13)
movq %rax, 0xb8(%r13)
callq 0x13e09
movl 0x20(%r13), %eax
orl 0x18(%r13), %eax
je 0x17ea8
movl $0x1480, %edi           # imm = 0x1480
addq (%r12), %rdi
movl 0x1c(%r13), %esi
callq 0x9088
movl (%rax), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x17f79
movl 0x18(%r13), %ebx
testl %ebx, %ebx
jne 0x17c91
leaq 0x208(%r13), %rdi
pushq $0x1
popq %rbp
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0xf66d
movl 0x18(%r13), %eax
testl %eax, %eax
je 0x17f28
cmpl $0x33, %eax
je 0x17f28
cmpl $0x32, %eax
jne 0x17edb
andl $0x0, 0x18(%r13)
jmp 0x17f28
movl 0x28(%r13), %esi
leaq 0xc7c2(%rip), %rdi      # 0x246a8
callq 0x9088
movq 0x18(%rax), %rcx
testq %rcx, %rcx
je 0x17f12
movslq 0x1c(%r13), %rdx
movq 0x38(%r13), %rsi
subq 0x10(%rax), %rsi
shrq $0x2, %rsi
movslq %esi, %rsi
addq %rdx, %rsi
movl -0x4(%rcx,%rsi,4), %ecx
decl %ecx
jmp 0x17f14
xorl %ecx, %ecx
movq (%rax), %rdi
movq 0x8(%rax), %rdx
pushq $-0x1
popq %rsi
xorl %r8d, %r8d
callq 0x17f79
movl %ebp, %ebx
callq 0x184ab
movl $0x288, %r14d           # imm = 0x288
addq 0x90a6(%rip), %r14      # 0x20fe0
cmpl 0x288(%r13), %ebp
jg 0x17f59
movq %r14, %rdi
movl %ebp, %esi
callq 0x9088
movq (%rax), %rdi
callq 0x9fbe
incl %ebp
jmp 0x17f3a
movl $0x288, %edi            # imm = 0x288
addq 0x907b(%rip), %rdi      # 0x20fe0
callq 0x846b
leaq 0x18(%rsp), %rdi
callq 0x846b
jmp 0x17c91