// File: /workspace/github_c_makefile_star2_O1/gregtour[P]minimal-compiler/typechecker.c
// Function: LinkFunctionRoot
void LinkFunctionRoot(TYPE type, const char* identifier, SYNTAX_TREE* parameters, SYNTAX_TREE* source)
{
    VARLIST* paramList = NULL;
    printf("Linking function %s %s.\n", TypeString(type), identifier);

    // parse function parameters
    if (parameters && parameters->production != PROD_PARAM_STAR)
        // <param*> ::= <epsilon>
    {
        while (parameters)
        {
            const char* identifier;
            TYPE type;
            int result;

            Assert(parameters->children && parameters->numChildren >= 3);
            Assert(parameters->children[0] && parameters->children[0]->string);
            Assert(parameters->children[2]->token == SYMBOL_TYPE);

            identifier = parameters->children[0]->string;
            type = DeriveType(parameters->children[2]);

            result = AddVariableToList(&paramList, type, identifier);
            if (result == 0)
            {
                CompilerError("Parameter variable redefined.");
            }

            // (a) <param*> ::= <identifier> : <type>
            // (b) <param*> ::= <identifier> : <type> , <param*>
            if (parameters->production == PROD_PARAM_STAR_IDENTIFIER_B)
            {   parameters = parameters->children[4]; }
            else 
            {   parameters = NULL; }
        }
    }

    Assert(source->token == SYMBOL_BLOCK);
    // check for redundant functions and link global methods
    if (AddFunctionToList(&gPrgrmFunctions, type, identifier, paramList, (SYNTAX_BLOCK*)source) == 0)
    {
        if (paramList)
        {
            FreeVariableList(&paramList);
        }

        CompilerError("Function redefined.");
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, -0x40(%rbp)
movq %rdx, %r12
movq %rsi, %rdx
movq %rdi, %r15
movq $0x0, -0x30(%rbp)
movq %rdi, %rax
shrq $0x22, %rax
jne 0x66d2
movq %r15, %rax
shrq $0x20, %rax
leaq 0x570c(%rip), %rcx      # 0xbdd8
movq (%rcx,%rax,8), %rsi
jmp 0x66d9
leaq 0x2342(%rip), %rsi      # 0x8a1b
leaq 0x233f(%rip), %rdi      # 0x8a1f
movq %rdx, -0x38(%rbp)
xorl %eax, %eax
callq 0x2090
testq %r12, %r12
je 0x6757
cmpl $0xe, 0x4(%r12)
je 0x6757
leaq -0x30(%rbp), %r13
movabsq $0x100000004, %rbx      # imm = 0x100000004
leaq 0x2349(%rip), %r14      # 0x8a56
movq 0x28(%r12), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x611f
testl %eax, %eax
jne 0x673c
leaq 0x2309(%rip), %rdi      # 0x8a38
movq %r14, %rsi
movl $0x2c, %edx
callq 0x4a54
cmpl $0xd, 0x4(%r12)
jne 0x674f
movq 0x28(%r12), %rax
movq 0x20(%rax), %r12
jmp 0x6752
xorl %r12d, %r12d
testq %r12, %r12
jne 0x670d
movq -0x30(%rbp), %rcx
leaq 0x12c16(%rip), %rdi     # 0x19378
movq %r15, %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %r8
callq 0x619f
testl %eax, %eax
jne 0x679e
cmpq $0x0, -0x30(%rbp)
je 0x6786
leaq -0x30(%rbp), %rdi
callq 0x626c
leaq 0x22d7(%rip), %rdi      # 0x8a64
leaq 0x22c2(%rip), %rsi      # 0x8a56
movl $0x41, %edx
callq 0x4a54
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq