// File: /workspace/github_c_makefile_star2_O1/Encryptr[P]O-plus-plus/opp/ir/ir.c
// Function: init_oppir
struct OppIr* init_oppir()
{
	struct OppIr* ir = (struct OppIr*)malloc(sizeof(struct OppIr));

	if (ir == NULL)
		goto err;

	// Bytecode
	ir->code.bytes = (unsigned char*)malloc(INIT_BYTECODE_SIZE);

	if (ir->code.bytes == NULL)
		goto err;

	ir->code.idx = 0;
	ir->code.allocated = INIT_BYTECODE_SIZE;

	// Local Stack
	ir->local_stack.size = 0;
	ir->local_stack.pos = 0;
	ir->local_stack.frame_ptr = 0;

	// Offset Tables 
	ir->offsets.offset_table = (int32_t*)malloc(sizeof(int32_t)*DEFAULT_OFFSET_TABLE);
	ir->offsets.jmp_table = (struct Jmp_Item*)malloc(sizeof(struct Jmp_Item)*DEFAULT_OFFSET_TABLE);

	if (ir->offsets.offset_table == NULL || ir->offsets.jmp_table == NULL)
		goto err;

	ir->offsets.allocated = DEFAULT_OFFSET_TABLE;
	ir->offsets.jmp_idx = 0;
	memset(ir->offsets.offset_table, 0, sizeof(int32_t)*DEFAULT_OFFSET_TABLE);
	memset(ir->offsets.jmp_table, 0, sizeof(struct Jmp_Item)*DEFAULT_OFFSET_TABLE);

	// Register Allocation
	ir->regalloc.amount = 0;
	ir->regalloc.allocated = DEFAULT_SPILL;
	ir->regalloc.spills = (struct Spill*)
		malloc(sizeof(struct Spill)*DEFAULT_SPILL);
	memset(ir->regalloc.spills, 0, sizeof(struct Spill)*DEFAULT_SPILL);

	if (ir->regalloc.spills == NULL)
		goto err;

	// Reg Stack
	ir->reg_stack.stack = (struct Register*)
		malloc(sizeof(struct Register)*DEFAULT_REG_STACK);
	memset(ir->reg_stack.stack, 0, sizeof(struct Register)*DEFAULT_REG_STACK);

	if (ir->reg_stack.stack == NULL)
		goto err;

	ir->reg_stack.top = ir->reg_stack.stack;
	ir->reg_stack.allocated = DEFAULT_REG_STACK;
	ir->instr = NULL;

	// Data
	ir->data_seg.data = (unsigned char*)
		malloc(INIT_BYTECODE_SIZE);

	if (ir->data_seg.data == NULL)
		goto err;

	ir->data_seg.allocated = INIT_BYTECODE_SIZE;
	ir->data_seg.idx = 0;

	memset(ir->data_seg.data, 0, INIT_BYTECODE_SIZE);

	// End
	return ir;

	err:
		INTERNAL_ERROR("Malloc Fail");
		return NULL;
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movl $0x98, %edi
callq 0x1180
testq %rax, %rax
je 0x5780
movq %rax, %rbx
movl $0x40, %edi
callq 0x1180
movq %rax, (%rbx)
testq %rax, %rax
je 0x5780
movq $0x0, 0x8(%rbx)
movq $0x40, 0x10(%rbx)
movl $0x80, %edi
callq 0x1180
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rbx)
movq %rax, 0x70(%rbx)
movl $0x200, %edi            # imm = 0x200
callq 0x1180
movq %rax, 0x78(%rbx)
testq %r14, %r14
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x5780
movq $0x20, 0x60(%rbx)
movl $0x0, 0x68(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%r14)
movups %xmm0, 0x60(%r14)
movups %xmm0, 0x50(%r14)
movups %xmm0, 0x40(%r14)
movups %xmm0, 0x30(%r14)
movups %xmm0, 0x20(%r14)
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1110
movq $0x0, 0x18(%rbx)
movq $0x8, 0x20(%rbx)
movl $0x40, %edi
callq 0x1180
movq %rax, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
testq %rax, %rax
je 0x5780
movl $0x100, %edi            # imm = 0x100
callq 0x1180
movq %rax, %r14
movq %rax, 0x38(%rbx)
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x1110
testq %r14, %r14
je 0x5780
movq %r14, 0x40(%rbx)
movq $0x10, 0x30(%rbx)
movq $0x0, 0x58(%rbx)
movl $0x40, %edi
callq 0x1180
movq %rax, 0x90(%rbx)
testq %rax, %rax
je 0x5780
movq $0x40, 0x80(%rbx)
movq $0x0, 0x88(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
jmp 0x5795
leaq 0x5e09(%rip), %rdi      # 0xb590
leaq 0x6bd6(%rip), %rsi      # 0xc364
callq 0x98fb
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
retq