// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x12ad0
movq %rax, %r13
movq %r14, 0x10(%rsp)
cmpl $0x2, (%r14)
movq %r15, 0x28(%rsp)
jne 0x2a4c0
cmpb $0x29, (%r13)
jne 0x2a4c0
cmpb $0x0, 0x1(%r13)
je 0x2a889
leaq 0x8(%r15), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x28(%r15)
je 0x2a7f1
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x18(%r15), %r13
addq %rbp, %r13
movq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x2fe6a
movl %eax, %r15d
movq %r13, %rdi
callq 0x37534
testb %al, %al
jne 0x2a511
movl (%r13), %eax
cmpl $0x7, %eax
je 0x2a541
cmpl $0x9, %eax
jne 0x2a60a
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a87a
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x37860
jmp 0x2a553
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a87a
movl %r15d, %edx
addq 0x20(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rcx
movq %rbx, %r8
callq 0x362c6
cmpl $0x0, (%rbx)
jne 0x2a87a
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x12ad0
movq %rax, %r13
cmpl $0x2, (%r15)
movq 0x28(%rsp), %r15
jne 0x2a842
movzbl (%r13), %eax
cmpl $0x2c, %eax
jne 0x2a836
cmpb $0x0, 0x1(%r13)
jne 0x2a842
movq 0x28(%r15), %rax
leaq -0x1(%rax), %rcx
cmpq %rcx, %r12
jae 0x2a5ee
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12edc
cmpl $0x0, (%rbx)
jne 0x2a87a
movq 0x28(%rsp), %r15
movq 0x28(%r15), %rax
incq %r12
addq $0x18, %r14
addq $0x148, %rbp            # imm = 0x148
cmpq %rax, %r12
jb 0x2a4dc
jmp 0x2a7f4
movq %r13, %rdi
callq 0x376b5
testb %al, %al
jne 0x2a622
movq %r13, %rdi
callq 0x376d3
testb %al, %al
je 0x2a69c
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 0x2a65f
cmpb $0x28, (%rax)
jne 0x2a65f
cmpb $0x0, 0x1(%rax)
je 0x2a717
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1e938
cmpl $0x0, (%rbx)
jne 0x2a579
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x432af
jmp 0x2a579
cmpl $0x16, (%r13)
jne 0x2aa1b
movq 0x10(%rsp), %rdi
callq 0x12ad0
movq 0x10(%rsp), %rcx
cmpl $0x2, (%rcx)
jne 0x2a9ef
cmpb $0x28, (%rax)
jne 0x2a9ef
cmpb $0x0, 0x1(%rax)
jne 0x2a9ef
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12edc
cmpl $0x0, (%rbx)
jne 0x2a87a
movl %r15d, %r8d
addq 0x20(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r9
callq 0x2a46d
jmp 0x2a579
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1ec4c
cmpl $0x0, (%rbx)
jne 0x2a579
movq 0x40(%rsp), %rcx
movq (%rcx,%r14), %rcx
movq %rcx, 0x48(%rsp)
movq %r13, 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 0x16680
cmpl $0x0, (%rbx)
jne 0x2a579
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x12ad0
cmpl $0x2, (%r15)
jne 0x2a79a
cmpb $0x29, (%rax)
jne 0x2a79a
cmpb $0x0, 0x1(%rax)
je 0x2a7d7
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
movq %rbx, %rdi
movl $0x1d, %esi
leaq 0x4934c(%rip), %rdx     # 0x73b05
movq %rax, %rcx
pushq $0x204                  # imm = 0x204
leaq 0x49322(%rip), %rax     # 0x73aea
pushq %rax
callq 0x143da
addq $0x10, %rsp
jmp 0x2a579
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12edc
jmp 0x2a579
xorl %r12d, %r12d
cmpb $0x29, (%r13)
jne 0x2a806
cmpb $0x0, 0x1(%r13)
je 0x2a8ef
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x492cd(%rip), %rax     # 0x73aea
leaq 0x492e1(%rip), %rdx     # 0x73b05
movq %rbx, %rdi
movl $0x1d, %esi
movq %r13, %rcx
pushq $0x341                  # imm = 0x341
jmp 0x2a870
cmpl $0x29, %eax
jne 0x2a842
cmpb $0x0, 0x1(%r13)
je 0x2a7f4
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x492a7(%rip), %rdx     # 0x73b00
movq %rbx, %rdi
movl $0x1d, %esi
movq %r13, %rcx
pushq $0x33b                  # imm = 0x33B
leaq 0x4927a(%rip), %rax     # 0x73aea
pushq %rax
callq 0x143da
addq $0x10, %rsp
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %rbx, %r8
callq 0x4626d
cmpl $0x0, (%rbx)
jne 0x2a87a
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x45c2d
cmpl $0x0, (%rbx)
jne 0x2a87a
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 0x12edc
leaq 0x1(%r12), %r15
leaq (%r12,%r12,2), %rax
leaq 0x18(,%rax,8), %r14
imulq $0x148, %r12, %rbp      # imm = 0x148
addq $0x148, %rbp            # imm = 0x148
movq 0x28(%rsp), %rax
cmpq 0x28(%rax), %r15
jae 0x2a9d5
movq 0x18(%rax), %r12
addq %rbp, %r12
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x2fe6a
movl %eax, %r13d
movq %r12, %rdi
callq 0x376f1
movl %r13d, %r13d
testb %al, %al
je 0x2a95f
addq 0x20(%rsp), %r13
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x4db2c
jmp 0x2a983
addq 0x20(%rsp), %r13
cmpl $0x16, (%r12)
jne 0x2a98c
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
movq %rbx, %r8
callq 0x4626d
cmpl $0x0, (%rbx)
jne 0x2a87a
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdx
addq %r14, %rdx
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rcx
movq %r13, %r8
xorl %r9d, %r9d
pushq %rbx
callq 0x45916
addq $0x10, %rsp
incq %r15
addq $0x18, %r14
addq $0x148, %rbp            # imm = 0x148
cmpl $0x0, (%rbx)
je 0x2a90e
jmp 0x2a87a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x12edc
jmp 0x2a87a
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x4982c(%rip), %rdx     # 0x74232
movq %rbx, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0x31c                  # imm = 0x31C
jmp 0x2a869
leaq 0x490c8(%rip), %rdx     # 0x73aea
movq %rbx, %rdi
movl $0xf, %esi
movl $0x326, %ecx            # imm = 0x326
callq 0x145a4
jmp 0x2a87a