// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/arch/arm32/arm_reg_alloc.c
// Function: prv_new_regs
static subtilis_arm_reg_class_t *prv_new_regs(
	size_t reg_count, size_t max_regs, size_t reg_start, size_t reg_size,
	size_t args, int32_t max_offset, subtilis_arm_reg_spill_imm_t spill_imm,
	subtilis_arm_reg_stran_imm_t stran_imm,
	subtilis_arm_instr_type_t store_type,
	subtilis_arm_instr_type_t load_type,
	subtilis_arm_fp_is_fixed_t is_fixed, subtilis_error_t *err)

/* clang-format on */
{
	size_t i;
	size_t index;
	subtilis_arm_reg_class_t *regs;
	size_t reg_args = 4;

	regs = calloc(sizeof(*regs), 1);
	if (!regs) {
		subtilis_error_set_oom(err);
		goto on_error;
	}

	regs->phys_to_virt = malloc(sizeof(*regs->phys_to_virt) * max_regs);
	if (!regs->phys_to_virt) {
		subtilis_error_set_oom(err);
		goto on_error;
	}

	regs->next = malloc(sizeof(*regs->next) * max_regs);
	if (!regs->next) {
		subtilis_error_set_oom(err);
		goto on_error;
	}

	if (args <= reg_args) {
		reg_args = args;
		regs->spill_top = 0;
	} else {
		regs->spill_top = args - reg_args;
	}

	regs->spilt_regs = malloc(reg_count * sizeof(int32_t));
	if (!regs->spilt_regs) {
		subtilis_error_set_oom(err);
		goto on_error;
	}
	regs->spill_stack = malloc(reg_count * sizeof(size_t));
	if (!regs->spill_stack) {
		subtilis_error_set_oom(err);
		goto on_error;
	}
	regs->max_regs = max_regs;
	regs->spill_max = regs->spill_top;
	regs->reg_size = reg_size;
	regs->spill_imm = spill_imm;
	regs->stran_imm = stran_imm;
	regs->store_type = store_type;
	regs->load_type = load_type;
	regs->max_offset = max_offset;
	regs->is_fixed = is_fixed;

	for (i = 0; i < reg_count; i++) {
		regs->spilt_regs[i] = -1;
		regs->spill_stack[i] = i * reg_size;
	}
	regs->vr_reg_count = reg_count;

	for (i = 0; i < reg_args; i++) {
		regs->next[i] = -1;
		regs->phys_to_virt[i] = i + reg_start;
	}

	for (; i < max_regs; i++) {
		regs->next[i] = -1;
		regs->phys_to_virt[i] = INT_MAX;
	}

	for (i = reg_args; i < args; i++) {
		index = i - reg_args;
		regs->spilt_regs[i + reg_start] = index * reg_size;
	}

	if (i > reg_args) {
		regs->spilt_args = i - reg_args;
		regs->spill_top = regs->spilt_args;
		regs->spill_max = regs->spill_top;
		for (i = 0; i < regs->spilt_args; i++)
			regs->spill_stack[i] = INT_MAX;
	} else {
		regs->spilt_args = 0;
	}

	regs->spill_points = NULL;
	regs->spill_points_count = 0;
	regs->spill_points_max = 0;

	return regs;

on_error:

	free(regs->spilt_regs);
	free(regs->next);
	free(regs->phys_to_virt);
	free(regs);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r14d
movq %r8, %r15
movq %rcx, %r12
movq %rdx, 0x30(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
pushq $0x1
popq %rsi
movl $0x2e0, %edi            # imm = 0x2E0
callq 0x12150
movq %rax, %rbp
testq %rax, %rax
je 0x51ea3
movl %r14d, 0xc(%rsp)
leaq (,%r13,8), %rdi
callq 0x121d0
movq %rax, 0x8(%rbp)
testq %rax, %rax
je 0x51eaa
movq %rax, %r14
movq %r15, 0x10(%rsp)
movq %r13, 0x28(%rsp)
leaq (,%r13,4), %rdi
callq 0x121d0
movq %rax, 0x10(%rbp)
testq %rax, %rax
je 0x51f02
movq %rax, %r13
movq 0x10(%rsp), %rax
leaq -0x4(%rax), %rdx
xorl %ecx, %ecx
cmpq $0x5, %rax
cmovbq %rcx, %rdx
pushq $0x4
popq %r15
cmovbq %rax, %r15
movq %rdx, 0x20(%rsp)
movq %rdx, 0x30(%rbp)
leaq (,%rbx,4), %rdi
callq 0x121d0
movq %rbp, %rdx
movq %rax, 0x20(%rbp)
testq %rax, %rax
je 0x51f97
movq %rax, %rbp
leaq (,%rbx,8), %rdi
movq %rdx, 0x18(%rsp)
callq 0x121d0
movq 0x18(%rsp), %r10
movq %rax, 0x28(%r10)
testq %rax, %rax
je 0x51fa4
movq 0x90(%rsp), %rcx
movl 0x88(%rsp), %edx
movl 0x80(%rsp), %esi
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %r8
movq 0x28(%rsp), %r9
movq %r9, (%r10)
movq 0x20(%rsp), %r11
movq %r11, 0x38(%r10)
movq %r12, 0x40(%r10)
movq %r8, 0x58(%r10)
movq %rdi, 0x60(%r10)
movl %esi, 0x68(%r10)
movl %edx, 0x6c(%r10)
movl 0xc(%rsp), %edx
movl %edx, 0x48(%r10)
movq %r10, %rdi
movq %rcx, 0x70(%r10)
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x30(%rsp), %rsi
cmpq %rdx, %rbx
je 0x51f09
orl $-0x1, (%rbp,%rdx,4)
movq %rcx, (%rax,%rdx,8)
incq %rdx
addq %r12, %rcx
jmp 0x51e8d
movl $0x153, %ecx            # imm = 0x153
jmp 0x51eaf
movl $0x159, %ecx            # imm = 0x159
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x98(%rsp), %rdi
leaq 0x1dce5(%rip), %rdx     # 0x6fbab
pushq $0x1
popq %rsi
callq 0x1429e
movq %rbx, %rdi
callq 0x122c0
movq %r13, %rdi
callq 0x122c0
movq %r14, %rdi
callq 0x122c0
movq %rbp, %rdi
callq 0x122c0
xorl %esi, %esi
movq %rsi, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x15f, %ecx            # imm = 0x15F
jmp 0x51eb2
movq %rbx, 0x18(%rdi)
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x51f27
orl $-0x1, (%r13,%rcx,4)
leaq (%rsi,%rcx), %rdx
movq %rdx, (%r14,%rcx,8)
incq %rcx
jmp 0x51f0f
movq %r15, %rcx
cmpq %r9, %rcx
jae 0x51f42
orl $-0x1, (%r13,%rcx,4)
movq $0x7fffffff, (%r14,%rcx,8) # imm = 0x7FFFFFFF
incq %rcx
jmp 0x51f2a
shlq $0x2, %rsi
leaq (%rsi,%r15,4), %rcx
addq %rcx, %rbp
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x10(%rsp), %r8
leaq (%r15,%rcx), %rsi
cmpq %r8, %rsi
jae 0x51f6b
movl %edx, (%rbp,%rcx,4)
incq %rcx
addl %r12d, %edx
jmp 0x51f56
cmpq %r15, %rsi
jbe 0x51fb4
movq %rdi, %rsi
movq %rcx, 0x50(%rdi)
movq %rcx, 0x30(%rdi)
movq %rcx, 0x38(%rdi)
xorl %edx, %edx
cmpq %rcx, %rdx
jae 0x51ef0
movq $0x7fffffff, (%rax,%rdx,8) # imm = 0x7FFFFFFF
incq %rdx
jmp 0x51f81
movl $0x16c, %ecx            # imm = 0x16C
movq %rdx, %rbp
jmp 0x51eb5
movl $0x171, %ecx            # imm = 0x171
movq %rbp, %rbx
movq %r10, %rbp
jmp 0x51eb7
movq %rdi, %rsi
jmp 0x51ef0