// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x9730
movq %rax, %rsi
movq -0x10(%rbp), %rdx
leaq 0x2113(%rip), %rdi      # 0xb8ea
movb $0x0, %al
callq 0x2070
cmpq $0x0, -0x18(%rbp)
je 0x9891
movq -0x18(%rbp), %rax
cmpl $0xe, 0x4(%rax)
je 0x9891
jmp 0x97f9
cmpq $0x0, -0x18(%rbp)
je 0x988f
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rdi
callq 0x98e0
movq %rax, -0x44(%rbp)
movq -0x44(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x8e30
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x9866
leaq 0x20ae(%rip), %rdi      # 0xb903
leaq 0x20c5(%rip), %rsi      # 0xb921
movl $0x2c, %edx
callq 0x6e20
movq -0x18(%rbp), %rax
cmpl $0xd, 0x4(%rax)
jne 0x9882
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x988a
movq $0x0, -0x18(%rbp)
jmp 0x97f9
jmp 0x9891
movq -0x10(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %r8
movq -0x8(%rbp), %rsi
leaq 0x12ac0(%rip), %rdi     # 0x1c368
callq 0x8f30
cmpl $0x0, %eax
jne 0x98da
cmpq $0x0, -0x28(%rbp)
je 0x98c2
leaq -0x28(%rbp), %rdi
callq 0x90b0
leaq 0x2066(%rip), %rdi      # 0xb92f
leaq 0x2051(%rip), %rsi      # 0xb921
movl $0x41, %edx
callq 0x6e20
addq $0x50, %rsp
popq %rbp
retq