// File: /workspace/github_c_makefile_star2_O1/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 0x8172(%rip), %rdi      # 0x21fe0
movsd 0x1192(%rip), %xmm0     # 0x1b008
movl $0x1, %esi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x83e1
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x19eee
leaq 0xb8ca(%rip), %rbx      # 0x25760
movl $0x1, 0x1f0(%rbx)
leaq 0x97d9(%rip), %rax      # 0x23680
movl (%rax), %esi
leaq 0x97d4(%rip), %rax      # 0x23684
movl (%rax), %ecx
leaq 0x8707(%rip), %rdi      # 0x225c0
leaq 0x86c0(%rip), %rdx      # 0x22580
leaq 0x14(%rsp), %r8
callq 0x9550
movq %rax, %r15
leaq 0xb3cc(%rip), %rax      # 0x252a0
movq %r15, (%rax)
addq $0x288, %rbx            # imm = 0x288
movq %rbx, %rdi
movl $0x1, %esi
callq 0x911c
movq %r15, (%rax)
cmpl $0x0, 0x14(%rsp)
je 0x19f1d
leaq 0xb864(%rip), %rax      # 0x25760
movl 0x18(%rax), %esi
leaq 0xb39a(%rip), %rax      # 0x252a0
movl 0x8(%rax), %edx
leaq 0x22f5(%rip), %rdi      # 0x1c205
xorl %eax, %eax
callq 0x8050
movl 0x14(%rsp), %ebx
jmp 0x19f5b
leaq 0x18(%rsp), %rdi
movl $0x1, %ebx
xorps %xmm0, %xmm0
xorl %esi, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x83e1
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x19f6c
cltq
leaq 0xb1b7(%rip), %rcx      # 0x25100
movq (%rcx,%rax,8), %rsi
leaq 0x22e7(%rip), %rdi      # 0x1c23b
xorl %eax, %eax
callq 0x8050
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, %ebp
jl 0x19f7f
leaq 0x23ae(%rip), %rdi      # 0x1c326
callq 0x8200
jmp 0x19f5b
testl %ebp, %ebp
je 0x19f88
movq (%r14), %r14
jmp 0x19f8f
leaq 0x2269(%rip), %r14      # 0x1c1f8
xorl %r15d, %r15d
cmpb $0x0, (%r14,%r15)
leaq 0x1(%r15), %r15
jne 0x19f92
leaq 0x18(%rsp), %rsi
movl $0x1, %ebx
movq %r14, %rdi
movl $0x1, %edx
callq 0x19507
testl %eax, %eax
je 0x19fc4
leaq 0x2289(%rip), %rdi      # 0x1c248
movq %r14, %rsi
jmp 0x19f54
leaq 0x18(%rsp), %rdi
callq 0x8bb7
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x19f40
decq %r15
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x911c
movq %rax, %r12
leaq 0xb2aa(%rip), %rbp      # 0x252a0
movq (%rbp), %rdi
movq %rax, %rsi
callq 0xb945
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x1a02f
leaq 0xb74f(%rip), %rcx      # 0x25760
movl %eax, 0x18(%rcx)
movl 0x8(%rbp), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0x1a232
jmp 0x19f17
movl 0xc(%rbp), %eax
testl %eax, %eax
je 0x1a056
leaq 0xb723(%rip), %rcx      # 0x25760
movl %eax, 0x20(%rcx)
movl 0x8(%rbp), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0x1a232
movl $0x1480, %eax           # imm = 0x1480
movq (%rbp), %rdi
addq %rax, %rdi
callq 0x8bb7
movl $0x1460, %edi           # imm = 0x1460
addq (%rbp), %rdi
movl $0x1, %ebx
movl $0x1, %esi
callq 0x911c
movq %rax, %r13
movq (%rbp), %rdi
movl 0x1460(%rdi), %ebp
movl $0x1480, %eax           # imm = 0x1480
addq %rax, %rdi
movl $0x1, %esi
callq 0x911c
movl 0x18(%rsp), %ecx
movl %ecx, (%rsp)
movq %r13, %rdi
movl %ebp, %esi
movq %r14, %rdx
movl %r15d, %ecx
movq %r12, %r8
movq %rax, %r9
callq 0x1a515
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x1a0dc
cltq
leaq 0xb034(%rip), %rcx      # 0x25100
movq (%rcx,%rax,8), %rsi
leaq 0x218c(%rip), %rdi      # 0x1c263
jmp 0x19f54
leaq 0xb67d(%rip), %r13      # 0x25760
movq 0x218(%r13), %rax
movq %rax, 0xc0(%r13)
leaq 0xb1a8(%rip), %rbx      # 0x252a0
movq (%rbx), %rcx
movslq 0x1460(%rcx), %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0xc8(%r13)
movq %rax, 0xb8(%r13)
callq 0x15b42
movl 0x20(%r13), %eax
orl 0x18(%r13), %eax
je 0x1a155
movl $0x1480, %edi           # imm = 0x1480
addq (%rbx), %rdi
movl 0x1c(%r13), %esi
callq 0x911c
movl (%rax), %ecx
decl %ecx
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0x1a232
movl 0x18(%r13), %ebx
testl %ebx, %ebx
jne 0x19f5b
leaq 0x208(%r13), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x10502
cmpl $0x32, 0x18(%r13)
jne 0x1a17e
movl $0x0, 0x18(%r13)
movl 0x18(%r13), %eax
testl %eax, %eax
je 0x1a1dd
cmpl $0x33, %eax
je 0x1a1dd
movl 0x28(%r13), %esi
leaq 0xb512(%rip), %rdi      # 0x256a8
callq 0x911c
movq 0x18(%rax), %rcx
testq %rcx, %rcx
je 0x1a1c2
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 0x1a1c4
xorl %ecx, %ecx
movq (%rax), %rdi
movq 0x8(%rax), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x1a232
movl $0x1, %ebx
callq 0x1a7c8
cmpl $0x0, 0x288(%r13)
jle 0x1a212
xorl %ebp, %ebp
leaq 0x288(%r13), %r14
incl %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x911c
movq (%rax), %rdi
callq 0xa394
cmpl 0x288(%r13), %ebp
jl 0x1a1f5
movl $0x288, %edi            # imm = 0x288
addq 0x7dc2(%rip), %rdi      # 0x21fe0
callq 0x847e
leaq 0x18(%rsp), %rdi
callq 0x847e
jmp 0x19f5b