// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/string_type.c
// Function: subtilis_string_type_add
subtilis_exp_t *subtilis_string_type_add(subtilis_parser_t *p,
					 subtilis_exp_t *a1, subtilis_exp_t *a2,
					 bool swapped, subtilis_error_t *err)
{
	subtilis_ir_operand_t a1_size;
	subtilis_ir_operand_t a2_size;
	subtilis_ir_operand_t new_size;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t a1_data;
	size_t a1_tmp_heap;
	subtilis_ir_operand_t a2_data;
	const subtilis_symbol_t *s;
	size_t tmp;
	bool a2_gt_0;
	char *a1_tmp = NULL;
	char *a2_tmp = NULL;
	size_t dest_reg = SIZE_MAX;
	char *tmp_name = NULL;

	a2_data.reg = SIZE_MAX;
	a2_size.reg = SIZE_MAX;

	op2.integer = SUBTIILIS_STRING_SIZE_OFF;
	a1_size.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I32, a1->exp.ir_op, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	a1_data.reg = subtilis_reference_get_data(p, a1->exp.ir_op.reg, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	a2_gt_0 =
	    prv_get_string_details(p, a2, &a2_size, &a2_data, &a2_tmp, err);
	a2 = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (!a2_gt_0)
		return a1;

	new_size.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_ADD_I32, a1_size, a2_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (swapped) {
		tmp = a1_size.reg;
		a1_size.reg = a2_size.reg;
		a2_size.reg = tmp;
		tmp = a1_data.reg;
		a1_data.reg = a2_data.reg;
		a2_data.reg = tmp;
		a1_tmp = a2_tmp;
	} else {
		a1_tmp = a1->temporary;
	}

	if (a1_tmp) {
		s = subtilis_symbol_table_lookup(p->local_st, a1_tmp);
		if (!s) {
			subtilis_error_set_assertion_failed(err);
			goto cleanup;
		}
		a1_tmp_heap = subtilis_reference_get_heap(
		    p, SUBTILIS_IR_REG_LOCAL, s->loc, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		dest_reg = subtilis_reference_type_realloc(
		    p, s->loc, SUBTILIS_IR_REG_LOCAL, a1_tmp_heap, a1_data.reg,
		    a1_size.reg, new_size.reg, a2_size.reg, SIZE_MAX, err);
	} else {
		s = subtilis_symbol_table_insert_tmp(
		    p->local_st, &subtilis_type_string, &tmp_name, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		a1->temporary = tmp_name;

		dest_reg = subtilis_reference_type_alloc(
		    p, &subtilis_type_string, s->loc, SUBTILIS_IR_REG_LOCAL,
		    new_size.reg, true, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		subtilis_reference_type_memcpy_dest(p, dest_reg, a1_data.reg,
						    a1_size.reg, err);
	}
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	op1.reg = dest_reg;
	dest_reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_ADD_I32, op1, a1_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_reference_type_memcpy_dest(p, dest_reg, a2_data.reg,
					    a2_size.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	a1->exp.ir_op.reg = subtilis_reference_get_pointer(
	    p, SUBTILIS_IR_REG_LOCAL, s->loc, err);

	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	return a1;

cleanup:

	subtilis_exp_delete(a1);
	subtilis_exp_delete(a2);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r14
movl %ecx, %r12d
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq $-0x1, %rax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0x48(%rdi), %rdi
movq 0x148(%rsi), %rdx
movl $0x12, %esi
xorl %ecx, %ecx
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x4bbc6
movq %rax, %r13
movq 0x148(%rbx), %rsi
movq %r15, %rdi
xorl %edx, %edx
movq %r14, %rcx
callq 0x4d29c
cmpl $0x0, (%r14)
jne 0x4bbc6
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x28(%rsp), %r8
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %r9
callq 0x4bdcc
cmpl $0x0, (%r14)
je 0x4bbea
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x2b4c3
movq %rbp, %rdi
callq 0x2b4c3
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %al, %al
je 0x4bbd8
movq 0x48(%r15), %rdi
movq 0x10(%rsp), %rcx
xorl %ebp, %ebp
xorl %esi, %esi
movq %r13, %rdx
movq %rcx, 0x30(%rsp)
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x4bbc6
movq 0x30(%rsp), %rcx
testb %r12b, %r12b
je 0x4bc41
movq %rax, %r12
movq %r13, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %rcx, %r13
leaq 0x28(%rsp), %rax
jmp 0x4bc4b
movq %rax, %r12
leaq 0x158(%rbx), %rax
movq (%rax), %rsi
movq 0x68(%r15), %rdi
testq %rsi, %rsi
je 0x4bcb4
callq 0x2eec8
testq %rax, %rax
je 0x4bdae
movq %rax, %rbp
movq (%rax), %rdx
movl $0x2, %esi
movq %r15, %rdi
movq %r14, %rcx
callq 0x4d2b6
cmpl $0x0, (%r14)
jne 0x4bbc4
movq (%rbp), %rsi
movl $0x2, %edx
movq %r15, %rdi
movq %rax, %rcx
movq 0x8(%rsp), %r8
movq %r13, %r9
pushq %r14
pushq $-0x1
pushq 0x20(%rsp)
pushq %r12
callq 0x4df02
addq $0x20, %rsp
movq %rax, %r12
jmp 0x4bd2f
leaq 0x28ecd(%rip), %rsi     # 0x74b88
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x2ea12
cmpl $0x0, (%r14)
jne 0x4bbc4
movq %rax, %rbp
movq 0x20(%rsp), %rax
movq %rax, 0x158(%rbx)
movq (%rbp), %rdx
subq $0x8, %rsp
leaq 0x28e98(%rip), %rsi     # 0x74b88
movl $0x2, %ecx
movq %r15, %rdi
movq %r12, %r8
movl $0x1, %r9d
pushq %r14
callq 0x4ddee
addq $0x10, %rsp
cmpl $0x0, (%r14)
jne 0x4bbc4
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x4d830
cmpl $0x0, (%r14)
jne 0x4bbc4
movq %rbp, 0x8(%rsp)
movq 0x48(%r15), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x4bbc6
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r15, %rdi
movq %rax, %rsi
movq %r14, %r8
callq 0x4d830
cmpl $0x0, (%r14)
jne 0x4bbc4
movq 0x8(%rsp), %rax
movq (%rax), %rdx
movl $0x2, %esi
movq %r15, %rdi
movq %r14, %rcx
callq 0x4d763
movq %rax, 0x148(%rbx)
cmpl $0x0, (%r14)
jne 0x4bbc4
jmp 0x4bbd8
leaq 0x29ce7(%rip), %rdx     # 0x75a9c
movq %r14, %rdi
movl $0xf, %esi
movl $0xd81, %ecx            # imm = 0xD81
callq 0x145f4
jmp 0x4bbc4