// File: /workspace/github_c_makefile_star2_O1/heltilda[P]cicada/cmpile.c
// Function: relinkBestToken
ccInt relinkBestToken(compiler_type *compiler, linkedlist *OoOblock, ccInt firstToken, ccInt lastToken, ccInt expectedType,
        ccInt **lastOrderedToken, ccBool **lastRtrnTypes, ccInt *lastTokenToFix)
{
    ccInt lowOoO = 0, bestToken = 0, lowOoOpos = 0, blockOpCounter, tokenNum = 0, rtrn;       // inits to suppress warning
    ccInt innerToken, adapter = 0, tokenToFix, *argPtr, loopArgType;
    ccBool **rtrnTypes = NULL;
    tokenSpecType *oneSpec, *bestSpec = NULL;
    scriptTokenType *oneScriptToken;
    
    
        // find the lowest-precedence token
    
    if (lastToken < firstToken)  {
        errPosition = 0;                        // temporary -- we will update errPosition later
        return arg_expected_err;        }
    
    for (blockOpCounter = firstToken; blockOpCounter <= lastToken; blockOpCounter++)  {
        
        tokenNum = *LL_int(OoOblock, blockOpCounter);
        oneScriptToken = scriptToken(tokenNum);
        oneSpec = tokenSpec(oneScriptToken->tokenID);
        
        if ((blockOpCounter == firstToken) || (oneSpec->precedence <= lowOoO))  {
            if ((blockOpCounter == firstToken) || ((oneSpec->leftArgType != -1)
                        && ((blockOpCounter == lastToken) || (oneSpec->rightArgType != -1))
                        && ((compiler->OoOdirections[oneSpec->precedence-1] == l_to_r) || (oneSpec->precedence < lowOoO))) )  {
                bestToken = tokenNum;
                bestSpec = oneSpec;
                rtrnTypes = &(oneScriptToken->rtrnTypes);
                lowOoO = oneSpec->precedence;
                lowOoOpos = blockOpCounter;         }
            
            else if (lowOoO == 1)  break;     // speeds up sentence-parsing (\n is highest-precedence)
    }   }
    
    
        // in rare cases we may have a type-X no-argument adapter whose type needs setting (if possible)
        // (it might be either this operator or else an enclosed operator)
        // 
        // 2 possibilities:  either we know what type we are expecting, in which case we 'fix' the adapter
        // type (or throw an error if we don't have an adapter of the appropriate type);
        // or else we don't know because it is an enclosed argument, so we feed the token # that needs fixing
        // back to the enclosing operator
    
    tokenToFix = scriptToken(bestToken)->adapterTokenToFix;
    if (tokenToFix != 0)  {
        if (expectedType == 10)  *lastTokenToFix = tokenToFix;
        else if (compiler->adapters[10][expectedType] != 0)  {
            scriptToken(tokenToFix)->adapterTokenToFix = compiler->adapters[10][expectedType];
            (*rtrnTypes)[expectedType] = ccTrue;     }
        else  {
            errPosition = scriptToken(tokenToFix)->tokenCharNum;
            return arg_expected_err;
    }   }
    
    
        // check to see if the best token is not of a type that we expected;
        // if possible, add an adapter to the argument
        //
        // Note:  the adapters are all added at the END of the bytecode
        // so as not to disrupt the positions of earlier tokens which may already be linked
    
    if (expectedType < (ccInt) (*typeXarg - *type0arg))  {
    if (!((*rtrnTypes)[expectedType]))  {
        
        for (loopArgType = 0; loopArgType < 10; loopArgType++)  {
        if (((*rtrnTypes)[loopArgType]) && (compiler->adapters[loopArgType][expectedType] != 0))  {
            
            rtrn = addScriptToken(compiler, compiler->adapters[loopArgType][expectedType], scriptToken(bestToken)->tokenCharNum, NULL, 0);
            if (rtrn != passed)  return rtrn;
            
            adapter = compiler->scriptTokens.elementNum;
            **lastOrderedToken = adapter;
            *lastOrderedToken = &(scriptToken(adapter)->firstArg);
            
            break;
        }}
        
            // otherwise throw a type-mismatch error
        
        if (adapter == 0)  {
            errPosition = scriptToken(bestToken)->tokenCharNum;
            return type_mismatch_err;
    }}  }
    
    
        // build the link to bestToken from some previous token
        // (lastOrderedToken is either the firstArg or nextExpression field of that prior token)
    
    **lastOrderedToken = bestToken;
    
    
        // now set bestToken's firstArg field to point to the lowest-OoO operator of its first argument
        // 
        // this low-OoO token's nextExpression field will point to the second argument of bestToken;
        // then that token's nextExpression will point to the third argument; etc.
        // 
        // the last argument's nextExpression field (or firstArg, if there are no arguments) is set to zero
    
    argPtr = &(scriptToken(bestToken)->firstArg);
    *lastOrderedToken = argPtr;
    **lastOrderedToken = 0;
    
    
        // link the left-hand argument
    
    if (bestSpec->leftArgType != -1)  {
        rtrn = relinkBestToken(compiler, OoOblock, firstToken, lowOoOpos-1, bestSpec->leftArgType, lastOrderedToken,
            rtrnTypes, &(scriptToken(bestToken)->adapterTokenToFix));
        if (rtrn != passed)  {
            if ((rtrn == arg_expected_err) && (errPosition == 0))  {
                rtrn = left_arg_expected_err;
                errPosition = scriptToken(bestToken)->tokenCharNum;     }
            return rtrn;
    }   }
    else if (lowOoOpos != firstToken)  {
        errPosition = scriptToken(bestToken)->tokenCharNum;
        return no_left_arg_allowed_err;     }
    
    
        // link any enclosed arguments -- these have already been chained via their nextExpression fields
    
    if (scriptToken(bestToken)->nextScriptToken != 0)  {
        
        ccInt tokenInExpression = bestToken;
        
        innerToken = scriptToken(bestToken)->enclosedArgs;
        **lastOrderedToken = innerToken;
        while (scriptToken(innerToken)->nextExpression != 0)  innerToken = scriptToken(innerToken)->nextExpression;
        *lastOrderedToken = &(scriptToken(innerToken)->nextExpression);
        
        
            // update bestSpec to get our final right-hand argument
        
        while (scriptToken(tokenInExpression)->nextScriptToken != 0)  {
            tokenInExpression = scriptToken(tokenInExpression)->nextScriptToken;
            bestSpec = tokenSpec(bestSpec->nextToken);
    }   }
    
    
        // link the right-hand argument
    
    if (bestSpec->rightArgType != -1)  {
        rtrn = relinkBestToken(compiler, OoOblock, lowOoOpos+1, lastToken, bestSpec->rightArgType, lastOrderedToken,
            rtrnTypes, &(scriptToken(bestToken)->adapterTokenToFix));
        if (rtrn != passed)  {
            if ((rtrn == arg_expected_err) && (errPosition == 0))  {
                rtrn = right_arg_expected_err;
                errPosition = scriptToken(bestToken)->tokenCharNum;     }
            return rtrn;
    }   }
    else if (lowOoOpos != lastToken)  {
        errPosition = scriptToken(bestToken)->tokenCharNum;
        return no_right_arg_allowed_err;     }
    
    
        // finally, prepare to link to the token of the next argument bestToken
        // if this token is an argument of another token, then this will be the next argument of that token
        // the linking is done one level up, so all we have to do is set the pointer here
    
    if (adapter == 0)  argPtr = &(scriptToken(bestToken)->nextExpression);
    else  argPtr = &(scriptToken(adapter)->nextExpression);
    
    *lastOrderedToken = argPtr;
    **lastOrderedToken = 0;
    
    
        // if the enclosing operator is, say, a ( .. ), feed the return type of this operator on back
    
    if (((*lastRtrnTypes)[10]) && (expectedType == 10))  {
        *lastRtrnTypes = scriptToken(bestToken)->rtrnTypes;       }
    
    return passed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, 0x30(%rsp)
movl %ecx, %esi
movq %rdx, 0x38(%rsp)
subl %edx, %esi
jge 0xc51e
movl $0x0, 0x18da0(%rip)     # 0x252a8
movl $0xc, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, 0x28(%rsp)
movq %r9, 0x40(%rsp)
movl %r8d, 0x8(%rsp)
leaq 0x1440(%rdi), %r15
movq %rdi, 0x10(%rsp)
leaq 0x1200(%rdi), %rax
movq %rax, 0x48(%rsp)
movq %rsi, 0x50(%rsp)
leal 0x1(%rsi), %eax
movl %eax, 0xc(%rsp)
xorl %ebx, %ebx
movq $0x0, (%rsp)
xorl %r12d, %r12d
movq $0x0, 0x20(%rsp)
movl $0x0, 0x2c(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x38(%rsp), %rax
leal (%rax,%rbx), %ebp
movq 0x30(%rsp), %rdi
movl %ebp, %esi
callq 0x911c
movl (%rax), %r13d
movq %r15, %r14
movq %r15, %rdi
movl %r13d, %esi
callq 0x911c
movq %rax, %r15
movl (%rax), %esi
movq 0x48(%rsp), %rdi
callq 0x911c
testl %ebx, %ebx
je 0xc5ed
movslq 0xc(%rax), %rcx
cmpl %r12d, %ecx
jg 0xc60a
cmpl $-0x1, 0x10(%rax)
je 0xc5e2
cmpl %ebx, 0x50(%rsp)
je 0xc5ca
cmpl $-0x1, 0x14(%rax)
je 0xc5e2
movq 0x10(%rsp), %rdx
movq 0x1240(%rdx), %rdx
cmpl $0x1, -0x4(%rdx,%rcx,4)
je 0xc5ed
cmpl %r12d, %ecx
jl 0xc5ed
cmpl $0x1, %r12d
movq %r14, %r15
jne 0xc60d
jmp 0xc619
addq $0x28, %r15
movl 0xc(%rax), %r12d
movl %ebp, %ecx
movq %rcx, 0x18(%rsp)
movl %r13d, 0x2c(%rsp)
movq %r15, 0x20(%rsp)
movq %rax, (%rsp)
movq %r14, %r15
incl %ebx
cmpl %ebx, 0xc(%rsp)
jne 0xc577
movq %r15, %rdi
movl 0x2c(%rsp), %r12d
movl %r12d, %esi
callq 0x911c
movl 0x30(%rax), %esi
testl %esi, %esi
movl 0x8(%rsp), %r13d
movq 0x40(%rsp), %rbx
je 0xc67f
cmpl $0xa, %r13d
jne 0xc64c
movq 0x98(%rsp), %rax
movl %esi, (%rax)
jmp 0xc67f
movslq %r13d, %rbx
movq 0x10(%rsp), %rax
movl 0x140c(%rax,%rbx,4), %ebp
movq %r15, %rdi
callq 0x911c
testl %ebp, %ebp
je 0xc969
movl %ebp, 0x30(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movb $0x1, (%rax,%rbx)
movq 0x40(%rsp), %rbx
xorl %ebp, %ebp
cmpl $0x9, %r13d
jg 0xc6f3
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq %r13d, %rcx
cmpb $0x0, (%rax,%rcx)
movq (%rsp), %r13
jne 0xc6f7
movq 0x10(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
addq $0x1254, %rcx           # imm = 0x1254
xorl %edx, %edx
cmpb $0x0, (%rax,%rdx)
je 0xc6be
movl (%rcx), %ebp
testl %ebp, %ebp
jne 0xc977
incq %rdx
addq $0x2c, %rcx
cmpq $0xa, %rdx
jne 0xc6ae
xorl %ebp, %ebp
testl %ebp, %ebp
movq (%rsp), %r13
jne 0xc6f7
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x4(%rax), %eax
movl %eax, 0x18bbf(%rip)     # 0x252a8
movl $0x11, %ebp
jmp 0xc50d
movq (%rsp), %r13
movl %ebp, 0xc(%rsp)
movq (%rbx), %rax
movl %r12d, (%rax)
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
leaq 0x8(%rax), %rcx
movq %rcx, (%rbx)
movl $0x0, 0x8(%rax)
movl 0x10(%r13), %ebp
cmpl $-0x1, %ebp
je 0xc79e
movq 0x18(%rsp), %rax
leal -0x1(%rax), %r15d
movq %r14, %rdi
movl %r12d, %esi
callq 0x911c
addq $0x30, %rax
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl %r15d, %ecx
movq %r14, %r15
movl %ebp, %r8d
movq %rbx, %r9
pushq %rax
pushq 0x28(%rsp)
callq 0xc4e0
addq $0x10, %rsp
testl %eax, %eax
je 0xc7ad
movl %eax, %ebp
cmpl $0xc, %eax
jne 0xc50d
cmpl $0x0, 0x18b2e(%rip)     # 0x252a8
jne 0xc50d
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x4(%rax), %eax
movl %eax, 0x18b14(%rip)     # 0x252a8
movl $0xd, %ebp
jmp 0xc50d
movq 0x38(%rsp), %rax
cmpl %eax, 0x18(%rsp)
jne 0xc8dc
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
cmpl $0x0, 0x14(%rax)
je 0xc850
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x10(%rax), %ebp
movq (%rbx), %rax
movl %ebp, (%rax)
movq %r15, %rdi
movl %ebp, %esi
callq 0x911c
cmpl $0x0, 0xc(%rax)
je 0xc7f4
movq %r15, %rdi
movl %ebp, %esi
callq 0x911c
movl 0xc(%rax), %ebp
jmp 0xc7d5
movq %r15, %rdi
movl %ebp, %esi
callq 0x911c
addq $0xc, %rax
movq %rax, (%rbx)
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
cmpl $0x0, 0x14(%rax)
je 0xc850
movl %r12d, %ebp
movq 0x48(%rsp), %r14
movq (%rsp), %r13
movq %r15, %rdi
movl %ebp, %esi
callq 0x911c
movl 0x14(%rax), %ebp
movl 0x28(%r13), %esi
movq %r14, %rdi
callq 0x911c
movq %rax, %r13
movq %r15, %rdi
movl %ebp, %esi
callq 0x911c
cmpl $0x0, 0x14(%rax)
jne 0xc822
jmp 0xc854
movq (%rsp), %r13
movl 0x14(%r13), %ebp
cmpl $-0x1, %ebp
je 0xc8fa
movq 0x18(%rsp), %r14
incl %r14d
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
addq $0x30, %rax
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl %r14d, %edx
movl 0x28(%rsp), %ecx
movl %ebp, %r8d
movq %rbx, %r9
pushq %rax
pushq 0x28(%rsp)
callq 0xc4e0
addq $0x10, %rsp
testl %eax, %eax
movl 0x8(%rsp), %r14d
je 0xc90e
movl %eax, %ebp
cmpl $0xc, %eax
jne 0xc50d
cmpl $0x0, 0x189f0(%rip)     # 0x252a8
jne 0xc50d
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x4(%rax), %eax
movl %eax, 0x189d6(%rip)     # 0x252a8
movl $0xe, %ebp
jmp 0xc50d
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x4(%rax), %eax
movl %eax, 0x189b8(%rip)     # 0x252a8
movl $0xf, %ebp
jmp 0xc50d
movq 0x18(%rsp), %rax
cmpl 0x28(%rsp), %eax
movl 0x8(%rsp), %r14d
jne 0xc9be
movl 0xc(%rsp), %esi
testl %esi, %esi
cmovel %r12d, %esi
movq %r15, %rdi
callq 0x911c
leaq 0xc(%rax), %rcx
movq %rcx, (%rbx)
movl $0x0, 0xc(%rax)
xorl %ebp, %ebp
cmpl $0xa, %r14d
jne 0xc50d
movq 0x90(%rsp), %rcx
movq (%rcx), %rax
cmpb $0x0, 0xa(%rax)
je 0xc50d
movq %r15, %rdi
movl %r12d, %esi
movq %rcx, %rbx
callq 0x911c
movq 0x28(%rax), %rax
movq %rax, (%rbx)
jmp 0xc50d
movl 0x4(%rax), %eax
movl %eax, 0x18936(%rip)     # 0x252a8
jmp 0xc508
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x4(%rax), %edx
movq 0x10(%rsp), %rdi
movl %ebp, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xb98d
movl %eax, %ebp
testl %eax, %eax
jne 0xc50d
movl (%r15), %ebp
movq (%rbx), %rax
movl %ebp, (%rax)
movq %r15, %rdi
movl %ebp, %esi
callq 0x911c
addq $0x8, %rax
movq %rax, (%rbx)
jmp 0xc6cd
movq %r15, %rdi
movl %r12d, %esi
callq 0x911c
movl 0x4(%rax), %eax
movl %eax, 0x188d6(%rip)     # 0x252a8
movl $0x10, %ebp
jmp 0xc50d