// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
movl $0x98, %edi
callq 0x1180
testq %rax, %rax
je 0x53a5
movq %rax, %rbx
pushq $0x40
popq %rdi
callq 0x1180
movq %rax, (%rbx)
testq %rax, %rax
je 0x53a5
andq $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 0x53a5
movq $0x20, 0x60(%rbx)
andl $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
andq $0x0, 0x18(%rbx)
movq $0x8, 0x20(%rbx)
pushq $0x1
popq %r14
pushq $0x40
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1130
movq %rax, 0x28(%rbx)
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
callq 0x1130
movq %rax, 0x38(%rbx)
movq %rax, 0x40(%rbx)
movq $0x10, 0x30(%rbx)
andq $0x0, 0x58(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1130
movq %rax, 0x90(%rbx)
testq %rax, %rax
je 0x53a5
movq $0x40, 0x80(%rbx)
andq $0x0, 0x88(%rbx)
jmp 0x53ba
leaq 0x51e4(%rip), %rdi      # 0xa590
leaq 0x5fcd(%rip), %rsi      # 0xb380
callq 0x9287
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r14
popq %r15
retq