// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/parser_rec.c
// Function: prv_rec_reset
static void prv_rec_reset(subtilis_parser_t *p, subtilis_token_t *t,
			  const subtilis_type_t *type, size_t mem_reg,
			  size_t loc, subtilis_error_t *err)
{
	const char *tbuf;
	size_t id;
	const subtilis_type_rec_t *rec;
	const subtilis_type_t *field_type;
	uint32_t offset;
	subtilis_exp_t *e;

	tbuf = subtilis_token_get_text(t);
	if ((t->type == SUBTILIS_TOKEN_OPERATOR) && !strcmp(tbuf, ")")) {
		subtilis_rec_type_deref(p, type, mem_reg, loc, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;

		subtilis_rec_type_zero(p, type, mem_reg, loc, false, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;

		subtilis_lexer_get(p->l, t, err);
		return;
	}

	rec = &type->params.rec;

	for (id = 0; id < rec->num_fields; id++) {
		field_type = &rec->field_types[id];
		offset = subtilis_type_rec_field_offset_id(rec, id);
		if (subtilis_type_if_is_numeric(field_type) ||
		    field_type->type == SUBTILIS_TYPE_FN) {
			e = subtilis_parser_priority7(p, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			e = subtilis_type_if_coerce_type(p, e, field_type, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			subtilis_type_if_assign_to_mem(p, mem_reg, loc + offset,
						       e, err);
		} else if (field_type->type == SUBTILIS_TYPE_STRING) {
			e = subtilis_parser_priority7(p, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			subtilis_type_if_assign_to_mem(p, mem_reg, loc + offset,
						       e, err);
		} else if (subtilis_type_if_is_array(field_type) ||
			   subtilis_type_if_is_vector(field_type)) {
			prv_array_field_reset(p, t, &rec->fields[id],
					      field_type, mem_reg, loc + offset,
					      err);
		} else if (field_type->type == SUBTILIS_TYPE_REC) {
			tbuf = subtilis_token_get_text(t);
			if ((t->type != SUBTILIS_TOKEN_OPERATOR) ||
			    strcmp(tbuf, "(")) {
				subtilis_error_set_expected(err, "(", tbuf,
							    p->l->stream->name,
							    p->l->line);
				return;
			}
			subtilis_lexer_get(p->l, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;

			prv_rec_reset(p, t, field_type, mem_reg, loc + offset,
				      err);
		} else {
			subtilis_error_set_assertion_failed(err);
			return;
		}

		if (err->type != SUBTILIS_ERROR_OK)
			return;

		tbuf = subtilis_token_get_text(t);
		if (t->type == SUBTILIS_TOKEN_OPERATOR) {
			if (!strcmp(tbuf, ",")) {
				if (id < rec->num_fields - 1) {
					subtilis_lexer_get(p->l, t, err);
					if (err->type != SUBTILIS_ERROR_OK)
						return;
				}
				continue;
			}
			if (!strcmp(tbuf, ")"))
				break;
		}
		subtilis_error_set_expected(err, ", or )", tbuf,
					    p->l->stream->name, p->l->line);
		return;
	}

	if (strcmp(tbuf, ")")) {
		subtilis_error_set_expected(err, ")", tbuf, p->l->stream->name,
					    p->l->line);
		return;
	}

	/*
	 * I could add a type_if interface for this, a reset method.
	 * The main motivation would be to avoid de-allocing and re-allocing
	 * arrays, or vectors when their size hasn't changed, that have a
	 * single owner.  We'd have to do a ref count check though and since
	 * we know the heap will have a block of the correct size, allocation
	 * should be fast, so it's not worth the effort.
	 */

	for (id = id + 1; id < rec->num_fields; id++) {
		field_type = &rec->field_types[id];
		offset = subtilis_type_rec_field_offset_id(rec, id);
		if (subtilis_type_if_is_reference(field_type)) {
			subtilis_reference_type_deref(p, mem_reg, offset + loc,
						      err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
		} else if (field_type->type == SUBTILIS_TYPE_REC) {
			subtilis_rec_type_deref(p, field_type, mem_reg,
						offset + loc, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
		}
		subtilis_rec_type_init_field(p, field_type, &rec->fields[id],
					     mem_reg, offset + loc, false, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
	}

	subtilis_lexer_get(p->l, t, err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbx
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x12ad0
movq %rax, %r12
movq %r14, 0x10(%rsp)
cmpl $0x2, (%r14)
jne 0x2a7d4
leaq 0x49329(%rip), %rsi     # 0x73aed
movq %r12, %rdi
callq 0x12160
testl %eax, %eax
je 0x2ab7a
movq 0x28(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x38(%rsp)
cmpq $0x0, 0x28(%rax)
je 0x2ab2d
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x18(%rax), %r12
addq %r14, %r12
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x30118
movl %eax, %r15d
movq %r12, %rdi
callq 0x37676
testb %al, %al
jne 0x2a82a
movl (%r12), %eax
cmpl $0x7, %eax
je 0x2a85a
cmpl $0x9, %eax
jne 0x2a91d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1e874
cmpl $0x0, (%rbx)
jne 0x2ad29
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x379c3
jmp 0x2a86c
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1e874
cmpl $0x0, (%rbx)
jne 0x2ad29
movl %r15d, %edx
addq 0x20(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rcx
movq %rbx, %r8
callq 0x36416
cmpl $0x0, (%rbx)
jne 0x2ad29
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x12ad0
movq %rax, %r12
cmpl $0x2, (%r15)
jne 0x2acf1
movq %r12, %rdi
leaq 0x48cd9(%rip), %rsi     # 0x73598
callq 0x12160
testl %eax, %eax
jne 0x2acda
movq 0x28(%rsp), %rax
movq 0x28(%rax), %rax
decq %rax
cmpq %rax, %r13
jae 0x2a8fb
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12eda
cmpl $0x0, (%rbx)
jne 0x2ad29
incq %r13
addq $0x18, %rbp
addq $0x148, %r14            # imm = 0x148
movq 0x28(%rsp), %rax
cmpq 0x28(%rax), %r13
jb 0x2a7f5
jmp 0x2ab30
movq %r12, %rdi
callq 0x377f7
testb %al, %al
jne 0x2a939
movq %r12, %rdi
callq 0x37815
testb %al, %al
je 0x2a9bb
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x40(%rsp)
movl %r15d, %eax
addq 0x20(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x2a97e
movq %rax, %rdi
leaq 0x498a1(%rip), %rsi     # 0x74212
callq 0x12160
testl %eax, %eax
je 0x2aa47
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1e874
cmpl $0x0, (%rbx)
jne 0x2a892
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x4335c
jmp 0x2a892
cmpl $0x16, (%r12)
jne 0x2ad75
movq 0x10(%rsp), %rdi
callq 0x12ad0
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x2ad4f
movq %rcx, %rdi
leaq 0x49827(%rip), %rsi     # 0x74212
movq %rcx, 0x30(%rsp)
callq 0x12160
movq 0x30(%rsp), %rcx
testl %eax, %eax
jne 0x2ad4f
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12eda
cmpl $0x0, (%rbx)
jne 0x2ad29
movl %r15d, %r8d
addq 0x20(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r9
callq 0x2a77f
jmp 0x2a892
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1eb88
cmpl $0x0, (%rbx)
jne 0x2a892
movq 0x40(%rsp), %rcx
movq (%rcx,%rbp), %rcx
movq %rcx, 0x48(%rsp)
movq %r12, 0x50(%rsp)
movq 0x18(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq 0x30(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %rax, %rcx
movq %rbx, %r8
callq 0x16719
cmpl $0x0, (%rbx)
jne 0x2a892
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x12ad0
movq %rax, %r15
cmpl $0x2, (%r12)
jne 0x2aad6
movq %r15, %rdi
leaq 0x49020(%rip), %rsi     # 0x73aed
callq 0x12160
testl %eax, %eax
je 0x2ab13
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
movq %rbx, %rdi
movl $0x1d, %esi
leaq 0x48ff8(%rip), %rdx     # 0x73aed
movq %r15, %rcx
pushq $0x204                  # imm = 0x204
leaq 0x48fce(%rip), %rax     # 0x73ad2
pushq %rax
callq 0x1442a
addq $0x10, %rsp
jmp 0x2a892
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12eda
jmp 0x2a892
xorl %r13d, %r13d
leaq 0x48fb6(%rip), %rsi     # 0x73aed
movq %r12, %rdi
callq 0x12160
testl %eax, %eax
je 0x2abea
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x48f74(%rip), %rax     # 0x73ad2
leaq 0x48f88(%rip), %rdx     # 0x73aed
movq %rbx, %rdi
movl $0x1d, %esi
movq %r12, %rcx
pushq $0x341                  # imm = 0x341
jmp 0x2ad1f
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %rbx, %r8
callq 0x46313
cmpl $0x0, (%rbx)
jne 0x2ad29
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x45cd5
cmpl $0x0, (%rbx)
jne 0x2ad29
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12eda
leaq 0x1(%r13), %r15
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0x18(,%rax,8), %r14
imulq $0x148, %r13, %rbp      # imm = 0x148
addq $0x148, %rbp            # imm = 0x148
movq 0x28(%rsp), %rax
cmpq 0x28(%rax), %r15
jae 0x2ad38
movq 0x18(%rax), %r12
addq %rbp, %r12
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x30118
movl %eax, %r13d
movq %r12, %rdi
callq 0x37833
movl %r13d, %r13d
testb %al, %al
je 0x2ac61
movq 0x20(%rsp), %rax
leaq (%rax,%r13), %rdx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rcx
callq 0x4db70
jmp 0x2ac86
cmpl $0x16, (%r12)
jne 0x2ac8f
movq 0x20(%rsp), %rax
leaq (%rax,%r13), %rcx
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %r8
callq 0x46313
cmpl $0x0, (%rbx)
jne 0x2ad29
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdx
addq %r14, %rdx
addq 0x20(%rsp), %r13
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rcx
movq %r13, %r8
xorl %r9d, %r9d
pushq %rbx
callq 0x459be
addq $0x10, %rsp
incq %r15
addq $0x18, %r14
addq $0x148, %rbp            # imm = 0x148
cmpl $0x0, (%rbx)
je 0x2ac0f
jmp 0x2ad29
leaq 0x48e0c(%rip), %rsi     # 0x73aed
movq %r12, %rdi
callq 0x12160
testl %eax, %eax
je 0x2ab30
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x48de0(%rip), %rdx     # 0x73ae8
movq %rbx, %rdi
movl $0x1d, %esi
movq %r12, %rcx
pushq $0x33b                  # imm = 0x33B
leaq 0x48db3(%rip), %rax     # 0x73ad2
pushq %rax
callq 0x1442a
addq $0x10, %rsp
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12eda
jmp 0x2ad29
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x494ac(%rip), %rdx     # 0x74212
movq %rbx, %rdi
movl $0x1d, %esi
pushq $0x31c                  # imm = 0x31C
jmp 0x2ad18
leaq 0x48d56(%rip), %rdx     # 0x73ad2
movq %rbx, %rdi
movl $0xf, %esi
movl $0x326, %ecx            # imm = 0x326
callq 0x145f4
jmp 0x2ad29