// File: /workspace/github_c_makefile_star2_O2/qpfiffer[P]lair/src/lair_std.c
// Function: lair_builtin_operator_plus
const struct _lair_type *_lair_builtin_operator_plus(LAIR_FUNCTION_SIG) {
	check(r, argc == 2, ERR_RUNTIME, "Incorrect number of arguments to `+` function.");
	check(r, argv[0] != NULL, ERR_RUNTIME, "Argument to `+` function was NULL.");
	check(r, argv[1] != NULL, ERR_RUNTIME, "Argument to `+` function was NULL.");
	check(r, argv[0]->type == argv[1]->type, ERR_RUNTIME, "Cannot add variables of different types together.");

	/* We already know (probably) that the types are the same. */
	const LAIR_TOKEN first_arg_type = argv[0]->type;
	switch (first_arg_type) {
		/* Integer addition. */
		case LR_NUM: {
			struct _lair_type _stack = {
				.type = LR_NUM,
				.value = {
					.num = argv[0]->value.num + argv[1]->value.num
				}
			};
			struct _lair_type *to_return = calloc(1, sizeof(struct _lair_type));
			memcpy(to_return, &_stack, sizeof(struct _lair_type));
			return to_return;
		}
		/* Append two strings together. */
		case LR_STRING: {
			const char *str0 = argv[0]->value.str;
			const char *str1 = argv[1]->value.str;
			const size_t str_siz = strlen(str0) + strlen(str1);

			struct _lair_type _stack = {
				.type = LR_STRING,
				.value = {
					.str = calloc(1, str_siz)
				}
			};

			struct _lair_type *to_return = calloc(1, sizeof(struct _lair_type) + 1);
			memcpy(_stack.value.str, str0, strlen(str0));
			memcpy(_stack.value.str + strlen(str0), str1, strlen(str1));
			memcpy(to_return, &_stack, sizeof(struct _lair_type));

			return to_return;
		}
		default:
			throw_exception(r, ERR_RUNTIME, "Don't know how to add these things together.");
	}
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rdi, %r14
xorl %eax, %eax
cmpl $0x2, %esi
sete %al
leaq 0x29ec(%rip), %rcx      # 0x40b8
pushq $0x2
popq %rbp
movl %eax, %esi
movl %ebp, %edx
callq 0x15c8
xorl %esi, %esi
cmpq $0x0, (%rbx)
setne %sil
leaq 0x29fe(%rip), %r15      # 0x40e7
movq %r14, %rdi
movl %ebp, %edx
movq %r15, %rcx
callq 0x15c8
xorl %esi, %esi
cmpq $0x0, 0x8(%rbx)
setne %sil
movq %r14, %rdi
movl %ebp, %edx
movq %r15, %rcx
callq 0x15c8
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movl (%rax), %eax
xorl %esi, %esi
cmpl (%rcx), %eax
sete %sil
leaq 0x29e4(%rip), %rcx      # 0x410a
movq %r14, %rdi
movl %ebp, %edx
callq 0x15c8
movq (%rbx), %rax
movl (%rax), %ecx
cmpl $0xa, %ecx
je 0x1769
cmpl $0xf, %ecx
jne 0x17e6
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %ebx
addl 0x8(%rax), %ebx
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x1130
movq %rax, %r14
movl $0xf, (%rax)
movl %ebx, 0x8(%rax)
jmp 0x17fb
movq 0x8(%rax), %r15
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rbx
movq %r15, %rdi
callq 0x1080
movq %rax, %r13
movq %rbx, %rdi
callq 0x1080
leaq (%rax,%r13), %rsi
pushq $0x1
popq %r14
movq %r14, %rdi
callq 0x1130
movq %rax, %r12
pushq $0x11
popq %rsi
movq %r14, %rdi
callq 0x1130
movq %rax, %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1160
movq %r15, %rdi
callq 0x1080
leaq (%rax,%r12), %r15
movq %rbx, %rdi
callq 0x1080
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x1160
movl $0xa, (%r14)
movq %r12, 0x8(%r14)
jmp 0x17fb
leaq 0x294f(%rip), %rdx      # 0x413c
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x15d8
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq