// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x28(%rsp)
movq %r8, %r14
andq $0x0, 0x20(%rsp)
movl %ecx, %r12d
orq $-0x1, 0x10(%rsp)
movq %rdx, %r13
orq $-0x1, 0x30(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
movq 0x48(%rdi), %rdi
movq 0x148(%rsi), %rdx
pushq $0x12
popq %rsi
xorl %ecx, %ecx
callq 0x2a819
cmpl $0x0, (%r14)
jne 0x49083
movq %rax, %rbp
movq 0x148(%rbx), %rsi
movq %r15, %rdi
xorl %edx, %edx
movq %r14, %rcx
callq 0x4a690
cmpl $0x0, (%r14)
jne 0x49083
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 0x49284
cmpl $0x0, (%r14)
je 0x490a7
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x29d88
movq %r13, %rdi
callq 0x29d88
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 0x49095
movq 0x48(%r15), %rdi
movq 0x30(%rsp), %rcx
xorl %r13d, %r13d
xorl %esi, %esi
movq %rbp, %rdx
movq %rcx, (%rsp)
movq %r14, %r8
callq 0x2a819
cmpl $0x0, (%r14)
jne 0x49083
movq (%rsp), %rcx
movq %rax, %r13
testb %r12b, %r12b
je 0x490fd
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 0x49109
leaq 0x158(%rbx), %rax
movq %rcx, 0x18(%rsp)
movq (%rax), %rsi
movq 0x68(%r15), %rdi
testq %rsi, %rsi
je 0x49172
callq 0x2d376
testq %rax, %rax
je 0x49268
movq %rax, %r12
movq (%rax), %rdx
pushq $0x2
popq %rsi
movq %r15, %rdi
movq %r14, %rcx
callq 0x4a6a8
cmpl $0x0, (%r14)
jne 0x49080
movq %r12, (%rsp)
movq (%r12), %rsi
pushq $0x2
popq %rdx
movq %r15, %rdi
movq %rax, %rcx
movq 0x8(%rsp), %r8
movq %rbp, %r9
pushq %r14
pushq $-0x1
pushq 0x28(%rsp)
pushq %r13
callq 0x4b1f9
addq $0x20, %rsp
movq %rax, %r12
jmp 0x491f0
leaq 0x259df(%rip), %rsi     # 0x6eb58
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x2cee3
cmpl $0x0, (%r14)
jne 0x49080
movq %rax, %r12
movq 0x20(%rsp), %rax
movq %rax, 0x158(%rbx)
movq (%r12), %rdx
subq $0x8, %rsp
leaq 0x259aa(%rip), %rsi     # 0x6eb58
pushq $0x2
popq %rcx
pushq $0x1
popq %r9
movq %r15, %rdi
movq %r13, %r8
pushq %r14
callq 0x4b101
addq $0x10, %rsp
cmpl $0x0, (%r14)
jne 0x49080
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 0x4abdd
cmpl $0x0, (%r14)
jne 0x49080
movq 0x48(%r15), %rdi
xorl %r13d, %r13d
xorl %esi, %esi
movq %r12, %rdx
movq %rbp, %rcx
movq %r14, %r8
callq 0x2a819
cmpl $0x0, (%r14)
jne 0x49083
movq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rcx
movq %r14, %r8
callq 0x4abdd
cmpl $0x0, (%r14)
jne 0x49080
movq (%rsp), %rax
movq (%rax), %rdx
pushq $0x2
popq %rsi
movq %r15, %rdi
movq %r14, %rcx
callq 0x4ab28
movq %rax, 0x148(%rbx)
cmpl $0x0, (%r14)
jne 0x49080
jmp 0x49095
leaq 0x267fd(%rip), %rdx     # 0x6fa6c
pushq $0xf
popq %rsi
movq %r14, %rdi
movl $0xd81, %ecx            # imm = 0xD81
callq 0x1429e
jmp 0x49080