// File: /workspace/github_c_makefile_star2_O2/been-jamming[P]MinimalCC/expression.c
// Function: compile_function_call
void compile_function_call(char **c, value *func, FILE *output_file){
	reg_list reg_state;
	data_entry return_data;
	data_entry return_address_data;
	type current_argument_type;
	value current_argument_value;
	unsigned int label_num;
	int func_stack_pos = 0;
	int any_args = 0;

	if(peek_type(&(func->data_type)) == type_pointer){
		pop_type(&(func->data_type));
	}
	if(pop_type(&(func->data_type)) != type_function){
		snprintf(error_message, sizeof(error_message), "Can't call non-function type");
		do_error(1);
	}

	label_num = num_labels;
	num_labels++;
	reg_state = push_registers(output_file);

	if(func->data.type == data_register){
		func_stack_pos = get_reg_stack_pos(reg_state, func->data.reg);
	}
	return_address_data = allocate(1);
	return_data = allocate(1);
	fileprint(output_file, "la $t0, __L%d\n", label_num);
	fileprint(output_file, "sw $t0, %d($sp)\n", get_stack_pos(return_address_data));
	if(peek_type(&(func->data_type)) == type_returns){
		skip_whitespace(c);
		if(**c != ')'){
			snprintf(error_message, sizeof(error_message), "Expected ')'");
			do_error(1);
		}
		++*c;
	}
	while(peek_type(&(func->data_type)) != type_returns){
		any_args = 1;
		current_argument_type = get_argument_type(&(func->data_type));
		compile_expression(&current_argument_value, c, 1, 1, output_file);
		if(**c != ',' && peek_type(&(func->data_type)) != type_returns){
			snprintf(error_message, sizeof(error_message), "Expected ','");
			do_error(1);
		} else if(**c != ')' && peek_type(&(func->data_type)) == type_returns){
			snprintf(error_message, sizeof(error_message), "Expected ')'");
			do_error(1);
		}
		++*c;
		cast(&current_argument_value, current_argument_type, 1, output_file);
	}

	pop_type(&(func->data_type));
	if(func->data.type == data_register){
		fileprint(output_file, "lw $t0, %d($sp)\n", func_stack_pos);
		if(any_args)
			fileprint(output_file, "addi $sp, $sp, %d\n", get_stack_pos(current_argument_value.data));
		else
			fileprint(output_file, "addi $sp, $sp, %d\n", get_stack_pos(return_data));
		fileprint(output_file, "jr $t0\n\n");
	} else if(func->data.type == data_stack){
		fileprint(output_file, "lw $t0, %d($sp)\n", get_stack_pos(func->data));
		if(any_args)
			fileprint(output_file, "addi $sp, $sp, %d\n", get_stack_pos(current_argument_value.data));
		else
			fileprint(output_file, "addi $sp, $sp, %d\n", get_stack_pos(return_data));
		fileprint(output_file, "jr $t0\n\n");
	}
	fileprint(output_file, "__L%d:\n", label_num);
	if(any_args)
		fileprint(output_file, "addi $sp, $sp, %d\n", -get_stack_pos(current_argument_value.data));
	else
		fileprint(output_file, "addi $sp, $sp, %d\n", -get_stack_pos(return_data));
	fileprint(output_file, "lw $t0, %d($sp)\n", get_stack_pos(return_data));
	deallocate(return_data);
	deallocate(return_address_data);
	pull_registers(reg_state, output_file);
	if(func->data.type == data_register){
		fileprint(output_file, "move $s%d, $t0\n", func->data.reg);
	} else if(func->data.type == data_stack){
		fileprint(output_file, "sw $t0, %d($sp)\n", get_stack_pos(func->data));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
leaq 0x10(%rsi), %rbp
movq %rbp, %rdi
callq 0x7603
cmpl $0x4, %eax
jne 0x360d
movq %rbp, %rdi
callq 0x761b
movq %rbp, %rdi
callq 0x761b
cmpl $0x5, %eax
je 0x363e
movups 0x516a(%rip), %xmm0     # 0x878b
leaq 0x8eb8(%rip), %rax      # 0xc4e0
movups %xmm0, 0xd(%rax)
movups 0x514b(%rip), %xmm0     # 0x877e
movaps %xmm0, (%rax)
pushq $0x1
popq %rdi
callq 0x56c5
movl 0x8c2c(%rip), %eax      # 0xc270
movq %rax, 0x60(%rsp)
incl %eax
movl %eax, 0x8c1f(%rip)      # 0xc270
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0x13af
xorl %eax, %eax
cmpl $0x0, (%r14)
jne 0x3684
movzbl 0x4(%r14), %edi
movl 0x88(%rsp), %eax
movl %eax, 0x10(%rsp)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1458
movl %eax, 0x50(%rsp)
movq %r14, 0x70(%rsp)
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0x12e0
movq %rax, %r15
movl %edx, %r13d
movl %r14d, %edi
callq 0x12e0
movq %rax, 0x58(%rsp)
movl %edx, 0x4c(%rsp)
leaq 0x50e4(%rip), %rsi      # 0x879b
movq %rbx, %rdi
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x14e8
movq %r15, 0x68(%rsp)
movq %r15, %rdi
movl %r13d, 0x54(%rsp)
movl %r13d, %esi
callq 0x1393
leaq 0x4ed3(%rip), %rsi      # 0x85b5
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x14e8
movq %rbp, %rdi
callq 0x7603
cmpl $0x6, %eax
jne 0x3741
movq %r12, %rdi
callq 0x7549
movq (%r12), %rax
cmpb $0x29, (%rax)
je 0x373a
leaq 0x8dcd(%rip), %rax      # 0xc4e0
movabsq $0x27292720646574, %rcx # imm = 0x27292720646574
movq %rcx, 0x5(%rax)
movabsq $0x6465746365707845, %rcx # imm = 0x6465746365707845
movq %rcx, (%rax)
pushq $0x1
popq %rdi
callq 0x56c5
movq (%r12), %rax
incq %rax
movq %rax, (%r12)
movq %rbx, 0x40(%rsp)
movb $0x1, %r13b
leaq 0xd0(%rsp), %r15
pushq $0x1
popq %r14
leaq 0x8d84(%rip), %rbx      # 0xc4e0
movq %rbp, %rdi
callq 0x7603
cmpl $0x6, %eax
je 0x3880
leaq 0x128(%rsp), %rdi
movq %rbp, %rsi
callq 0x7755
movups 0x128(%rsp), %xmm0
movups 0x138(%rsp), %xmm1
movups 0x148(%rsp), %xmm2
movups 0x158(%rsp), %xmm3
movaps %xmm3, 0xc0(%rsp)
movaps %xmm2, 0xb0(%rsp)
movaps %xmm1, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movl %r14d, %edx
movl %r14d, %ecx
movq 0x40(%rsp), %r13
movq %r13, %r8
callq 0x3a29
movq (%r12), %rax
cmpb $0x2c, (%rax)
jne 0x37f8
movq %rbp, %rdi
callq 0x7603
cmpl $0x6, %eax
jne 0x3833
movabsq $0x27292720646574, %rax # imm = 0x27292720646574
jmp 0x381a
movq %rbp, %rdi
callq 0x7603
cmpl $0x6, %eax
jne 0x3810
movq (%r12), %rax
cmpb $0x29, (%rax)
jne 0x37df
jmp 0x3833
movabsq $0x272c2720646574, %rax # imm = 0x272C2720646574
movq %rax, 0x5(%rbx)
movabsq $0x6465746365707845, %rax # imm = 0x6465746365707845
movq %rax, (%rbx)
movl %r14d, %edi
callq 0x56c5
incq (%r12)
movaps 0x90(%rsp), %xmm0
movaps 0xa0(%rsp), %xmm1
movaps 0xb0(%rsp), %xmm2
movaps 0xc0(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rdx
callq 0x312b
xorl %r13d, %r13d
jmp 0x375c
movq %rbp, %rdi
callq 0x761b
movq 0x70(%rsp), %r14
movl (%r14), %eax
cmpl $0x1, %eax
je 0x38cd
testl %eax, %eax
movq 0x40(%rsp), %rbx
movq 0x58(%rsp), %r15
movl 0x4c(%rsp), %ebp
jne 0x393a
leaq 0x4d94(%rip), %rsi      # 0x8646
movq %rbx, %rdi
movl 0x50(%rsp), %edx
xorl %eax, %eax
callq 0x14e8
testb $0x1, %r13b
je 0x38fd
movq %r15, %rdi
movl %ebp, %esi
jmp 0x390c
movq (%r14), %rdi
movl 0x8(%r14), %esi
callq 0x1393
leaq 0x4d66(%rip), %rsi      # 0x8646
movq 0x40(%rsp), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x14e8
testb $0x1, %r13b
movq 0x58(%rsp), %r15
movl 0x4c(%rsp), %ebp
jne 0x38c6
movq 0xd0(%rsp), %rdi
movl 0xd8(%rsp), %esi
callq 0x1393
leaq 0x4eac(%rip), %rsi      # 0x87c4
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x14e8
leaq 0x4ea7(%rip), %rsi      # 0x87d7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x14e8
leaq 0x5431(%rip), %rsi      # 0x8d72
movq %rbx, %rdi
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x14e8
testb $0x1, %r13b
je 0x395d
movq %r15, %rdi
movl %ebp, %esi
jmp 0x396c
movq 0xd0(%rsp), %rdi
movl 0xd8(%rsp), %esi
callq 0x1393
negl %eax
leaq 0x4e4a(%rip), %rsi      # 0x87c4
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x14e8
movq %r15, %rdi
movl %ebp, %esi
callq 0x1393
leaq 0x4caa(%rip), %rsi      # 0x8646
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x14e8
movq %r15, %rdi
movl %ebp, %esi
callq 0x1343
movq 0x68(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x1343
movl 0x88(%rsp), %eax
movl %eax, 0x10(%rsp)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x146f
movl (%r14), %eax
cmpl $0x1, %eax
je 0x39f8
testl %eax, %eax
jne 0x3a17
movl 0x4(%r14), %edx
leaq 0x4ded(%rip), %rsi      # 0x87e0
movq %rbx, %rdi
jmp 0x3a10
movq (%r14), %rdi
movl 0x8(%r14), %esi
callq 0x1393
leaq 0x4baa(%rip), %rsi      # 0x85b5
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x14e8
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq