// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rsi, %rbx
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq $-0x1, %rax
movq %rax, 0x10(%rsp)
movq %rax, 0x30(%rsp)
movq 0x48(%rdi), %rdi
movq 0x148(%rsi), %rdx
movl $0x12, %esi
xorl %ecx, %ecx
callq 0x2bc8f
cmpl $0x0, (%r14)
jne 0x4bb79
movq %rax, %rbp
movq 0x148(%rbx), %rsi
movq %r15, %rdi
xorl %edx, %edx
movq %r14, %rcx
callq 0x4d258
cmpl $0x0, (%r14)
jne 0x4bb79
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x28(%rsp), %r8
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %r9
callq 0x4bd89
cmpl $0x0, (%r14)
je 0x4bb9d
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x2b166
movq %r13, %rdi
callq 0x2b166
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 0x4bb8b
movq 0x48(%r15), %rdi
movq 0x30(%rsp), %rcx
xorl %r13d, %r13d
xorl %esi, %esi
movq %rbp, %rdx
movq %rcx, (%rsp)
movq %r14, %r8
callq 0x2bc8f
cmpl $0x0, (%r14)
jne 0x4bb79
movq (%rsp), %rcx
movq %rax, %r13
testb %r12b, %r12b
je 0x4bbf3
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %rcx, %rbp
leaq 0x28(%rsp), %rax
jmp 0x4bbff
leaq 0x158(%rbx), %rax
movq %rcx, 0x18(%rsp)
movq (%rax), %rsi
movq 0x68(%r15), %rdi
testq %rsi, %rsi
je 0x4bc6f
callq 0x2ebbc
testq %rax, %rax
je 0x4bd6b
movq %rax, %r12
movq (%rax), %rdx
movl $0x2, %esi
movq %r15, %rdi
movq %r14, %rcx
callq 0x4d272
cmpl $0x0, (%r14)
jne 0x4bb76
movq %r12, (%rsp)
movq (%r12), %rsi
movl $0x2, %edx
movq %r15, %rdi
movq %rax, %rcx
movq 0x8(%rsp), %r8
movq %rbp, %r9
pushq %r14
pushq $-0x1
pushq 0x28(%rsp)
pushq %r13
callq 0x4debe
addq $0x20, %rsp
movq %rax, %r12
jmp 0x4bcf1
leaq 0x28f32(%rip), %rsi     # 0x74ba8
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x2e6f1
cmpl $0x0, (%r14)
jne 0x4bb76
movq %rax, %r12
movq 0x20(%rsp), %rax
movq %rax, 0x158(%rbx)
movq (%r12), %rdx
subq $0x8, %rsp
leaq 0x28efd(%rip), %rsi     # 0x74ba8
movl $0x2, %ecx
movq %r15, %rdi
movq %r13, %r8
movl $0x1, %r9d
pushq %r14
callq 0x4ddaa
addq $0x10, %rsp
cmpl $0x0, (%r14)
jne 0x4bb76
movq %r12, %rcx
movq %rax, %r12
movq %rcx, (%rsp)
movq %r15, %rdi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movq %rbp, %rcx
movq %r14, %r8
callq 0x4d7ec
cmpl $0x0, (%r14)
jne 0x4bb76
movq 0x48(%r15), %rdi
xorl %r13d, %r13d
xorl %esi, %esi
movq %r12, %rdx
movq %rbp, %rcx
movq %r14, %r8
callq 0x2bc8f
cmpl $0x0, (%r14)
jne 0x4bb79
movq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rcx
movq %r14, %r8
callq 0x4d7ec
cmpl $0x0, (%r14)
jne 0x4bb76
movq (%rsp), %rax
movq (%rax), %rdx
movl $0x2, %esi
movq %r15, %rdi
movq %r14, %rcx
callq 0x4d71f
movq %rax, 0x148(%rbx)
cmpl $0x0, (%r14)
jne 0x4bb76
jmp 0x4bb8b
leaq 0x29d6e(%rip), %rdx     # 0x75ae0
movq %r14, %rdi
movl $0xf, %esi
movl $0xd81, %ecx            # imm = 0xD81
callq 0x145a4
jmp 0x4bb76