// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x12a25
movq %rax, %r15
cmpl $0x2, (%r14)
movq %rbp, 0x20(%rsp)
jne 0x29165
cmpb $0x29, (%r15)
jne 0x29165
cmpb $0x0, 0x1(%r15)
je 0x294aa
leaq 0x8(%rbp), %rax
movq %rax, 0x38(%rsp)
pushq $0x1
popq %r13
pushq $0x18
popq %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
leaq -0x1(%r13), %r12
cmpq 0x28(%rbp), %r12
jae 0x294a2
movq 0x18(%rbp), %rbp
movq %rax, 0x28(%rsp)
addq %rax, %rbp
movq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x2e515
movl %eax, %r15d
movq %rbp, %rdi
callq 0x35655
movl %r15d, %r15d
testb %al, %al
jne 0x291c6
movl (%rbp), %eax
cmpl $0x7, %eax
je 0x291f4
cmpl $0x9, %eax
jne 0x292ab
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x29552
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x35977
jmp 0x29204
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x29552
addq 0x18(%rsp), %r15
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %rax, %rcx
movq %rbx, %r8
callq 0x34300
cmpl $0x0, (%rbx)
jne 0x29552
movq %r14, %rdi
callq 0x12a25
movq %rax, %r15
cmpl $0x2, (%r14)
movq 0x20(%rsp), %rcx
jne 0x29478
movzbl (%r15), %eax
cmpl $0x2c, %eax
jne 0x29468
cmpb $0x0, 0x1(%r15)
jne 0x29478
movq 0x28(%rcx), %rax
decq %rax
cmpq %rax, %r12
jae 0x2928d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12e0e
cmpl $0x0, (%rbx)
jne 0x29552
incq %r13
addq $0x18, 0x30(%rsp)
movq 0x28(%rsp), %rax
addq $0x148, %rax            # imm = 0x148
movq 0x20(%rsp), %rbp
jmp 0x2917c
movq %rbp, %rdi
callq 0x357cc
testb %al, %al
jne 0x292c3
movq %rbp, %rdi
callq 0x357ea
testb %al, %al
je 0x2932c
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x40(%rsp)
addq 0x18(%rsp), %r15
movq %r14, %rdi
callq 0x12a25
cmpl $0x2, (%r14)
jne 0x292f3
cmpb $0x28, (%rax)
jne 0x292f3
cmpb $0x0, 0x1(%rax)
je 0x2939c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1dccf
cmpl $0x0, (%rbx)
jne 0x2922a
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq %r15, %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x40c7e
jmp 0x2922a
cmpl $0x16, (%rbp)
jne 0x2967f
movq %r14, %rdi
callq 0x12a25
cmpl $0x2, (%r14)
jne 0x29655
cmpb $0x28, (%rax)
jne 0x29655
cmpb $0x0, 0x1(%rax)
jne 0x29655
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12e0e
cmpl $0x0, (%rbx)
jne 0x29552
addq 0x18(%rsp), %r15
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0x29118
jmp 0x2922a
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1dfd5
cmpl $0x0, (%rbx)
jne 0x2922a
movq 0x30(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq -0x18(%rdx,%rcx), %rcx
movq %rcx, 0x48(%rsp)
movq %rbp, 0x50(%rsp)
movq 0x10(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq 0x8(%rsp), %rdi
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
movq %rax, %rcx
movq %rbx, %r8
callq 0x161d0
cmpl $0x0, (%rbx)
jne 0x2922a
movq %r14, %rdi
callq 0x12a25
cmpl $0x2, (%r14)
jne 0x29417
cmpb $0x29, (%rax)
jne 0x29417
cmpb $0x0, 0x1(%rax)
je 0x29450
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
movq %rbx, %rdi
pushq $0x1d
popq %rsi
leaq 0x44689(%rip), %rdx     # 0x6dabd
movq %rax, %rcx
pushq $0x204                  # imm = 0x204
leaq 0x4465f(%rip), %rax     # 0x6daa2
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
jmp 0x2922a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12e0e
jmp 0x2922a
cmpl $0x29, %eax
jne 0x29478
cmpb $0x0, 0x1(%r15)
je 0x29512
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x44629(%rip), %rdx     # 0x6dab8
pushq $0x1d
popq %rsi
movq %rbx, %rdi
movq %r15, %rcx
pushq $0x33b                  # imm = 0x33B
jmp 0x29543
cmpb $0x29, (%r15)
je 0x29517
jmp 0x2951e
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
callq 0x43a22
cmpl $0x0, (%rbx)
jne 0x29552
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x433f5
cmpl $0x0, (%rbx)
jne 0x29552
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rdx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12e0e
movq 0x28(%rsp), %rax
cmpb $0x0, 0x1(%r15)
je 0x29561
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x44588(%rip), %rdx     # 0x6dabd
pushq $0x1d
popq %rsi
movq %rbx, %rdi
movq %r15, %rcx
pushq $0x341                  # imm = 0x341
leaq 0x44558(%rip), %rax     # 0x6daa2
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x148, %rax            # imm = 0x148
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x28(%rax), %r13
jae 0x2963d
movq 0x18(%rax), %r15
addq 0x28(%rsp), %r15
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x2e515
movl %eax, %ebp
movq %r15, %rdi
callq 0x35808
movl %ebp, %r12d
testb %al, %al
je 0x295be
addq 0x18(%rsp), %r12
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x4aeaa
jmp 0x295e1
addq 0x18(%rsp), %r12
cmpl $0x16, (%r15)
jne 0x295ea
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0x43a22
cmpl $0x0, (%rbx)
jne 0x29552
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rdx
movq 0x30(%rsp), %rbp
addq %rbp, %rdx
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %rbx
callq 0x43100
popq %rax
popq %rcx
incq %r13
addq $0x18, %rbp
movq %rbp, 0x30(%rsp)
addq $0x148, 0x28(%rsp)      # imm = 0x148
cmpl $0x0, (%rbx)
je 0x2956c
jmp 0x29552
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12e0e
jmp 0x29552
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x44b76(%rip), %rdx     # 0x6e1e2
pushq $0x1d
popq %rsi
movq %rbx, %rdi
movq %rax, %rcx
pushq $0x31c                  # imm = 0x31C
jmp 0x29543
leaq 0x4441c(%rip), %rdx     # 0x6daa2
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0x326, %ecx            # imm = 0x326
callq 0x1429e
jmp 0x29552