// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movq $0x0, -0x30(%rbp)
movq %rdi, %rax
shrq $0x22, %rax
jne 0x67dd
movq %r15, %rax
shrq $0x20, %rax
leaq 0x5601(%rip), %rcx      # 0xbdd8
movq (%rcx,%rax,8), %rsi
jmp 0x67e4
leaq 0x21d7(%rip), %rsi      # 0x89bb
leaq 0x21d4(%rip), %rdi      # 0x89bf
xorl %ebx, %ebx
movq %r14, %rdx
xorl %eax, %eax
callq 0x2090
testq %r12, %r12
je 0x686b
cmpl $0xe, 0x4(%r12)
jne 0x6808
xorl %ebx, %ebx
jmp 0x686b
movq %r13, -0x38(%rbp)
leaq -0x30(%rbp), %r13
movabsq $0x100000004, %rbx      # imm = 0x100000004
movq 0x28(%r12), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6233
testl %eax, %eax
jne 0x684d
leaq 0x219c(%rip), %rdi      # 0x89d8
leaq 0x21b3(%rip), %rsi      # 0x89f6
movl $0x2c, %edx
callq 0x4a64
cmpl $0xd, 0x4(%r12)
jne 0x6863
movq 0x28(%r12), %rax
movq 0x20(%rax), %r12
testq %r12, %r12
jne 0x681a
movq -0x30(%rbp), %rbx
movq -0x38(%rbp), %r13
leaq 0x12b06(%rip), %rdi     # 0x19378
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x62b0
testl %eax, %eax
jne 0x68af
cmpq $0x0, -0x30(%rbp)
je 0x6897
leaq -0x30(%rbp), %rdi
callq 0x6379
leaq 0x2166(%rip), %rdi      # 0x8a04
leaq 0x2151(%rip), %rsi      # 0x89f6
movl $0x41, %edx
callq 0x4a64
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq