// File: /workspace/github_c_makefile_star2_O0/Encryptr[P]O-plus-plus/opp/parser/parser.c
// Function: opp_parse_struct
static struct Opp_Node* opp_parse_struct(struct Opp_Parser* parser)
{
	struct Opp_Node* struct_node = opp_new_node(parser, STMT_STRUCT);

	opp_next(parser->lex);

	if (parser->lex->t.id != TIDENT)
		opp_error(parser->lex, "Expected a identifier after struct");

	struct_node->struct_stmt.name = (char*)malloc(strlen(parser->lex->t.buffer.buf)+1);
	strcpy(struct_node->struct_stmt.name, parser->lex->t.buffer.buf);

	struct Opp_Type_Entry* entry = add_type(&parser->tree, struct_node->struct_stmt.name);

	if (entry == NULL)
		opp_error(parser->lex, "Redefinition of type '%s'", struct_node->struct_stmt.name);
	entry->t_type = TYPE_STRUCT;	
	entry->s_elems = &struct_node->struct_stmt;

	opp_next(parser->lex);
	// add peek to see if non typedef declaration aka struct test a; for example

	if (parser->lex->t.id != TOPENC)
		opp_error(parser->lex, "Expected a '{' after 'struct %s'", struct_node->struct_stmt.name);

	struct_node->struct_stmt.elems = (struct Opp_Node**)
		malloc(sizeof(struct Opp_Node*)*16);

	memset(struct_node->struct_stmt.elems, 0, sizeof(struct Opp_Node*)*16);
	unsigned int allocated = 16;

	unsigned int i = 0;
	do {
		opp_next(parser->lex);
		if (parser->lex->t.id == FEND || parser->lex->t.id == TCLOSEC)
			break;

		if (i == allocated) {
			struct_node->struct_stmt.elems = (struct Opp_Node**)
				realloc(struct_node->struct_stmt.elems, (64+allocated) * sizeof(struct Opp_Node*));

			if (struct_node->struct_stmt.elems == NULL)
				INTERNAL_ERROR("Malloc Fail");

			allocated += 64;
		}

		struct_node->struct_stmt.elems[i] = opp_parse_type(parser);

		i++;

	} while (parser->lex->t.id != TCLOSEP);

	opp_next(parser->lex);
	if (parser->lex->t.id != TSEMICOLON)
		opp_error(parser->lex, "Expected ';' after struct '%s' declaration", struct_node->struct_stmt.name);

	struct_node->struct_stmt.len = i;

	return struct_node;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movl $0x1e, %esi
callq 0xf950
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x11bd0
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x1, 0x30(%rax)
je 0xf2ae
movq 0x20(%rsp), %rax
movq (%rax), %rdi
leaq 0x5c81(%rip), %rsi      # 0x14f28
movb $0x0, %al
callq 0x11370
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x38(%rax), %rdi
callq 0x1090
movq %rax, %rdi
addq $0x1, %rdi
callq 0x1160
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x38(%rax), %rsi
callq 0x1040
movq 0x20(%rsp), %rdi
addq $0x20, %rdi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0xee20
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0xf334
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdx
leaq 0x5c1e(%rip), %rsi      # 0x14f4b
movb $0x0, %al
callq 0x11370
movq 0x10(%rsp), %rax
movl $0x8, 0x8(%rax)
movq 0x18(%rsp), %rcx
addq $0x10, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x11bd0
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x27, 0x30(%rax)
je 0xf38c
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdx
leaq 0x5be0(%rip), %rsi      # 0x14f65
movb $0x0, %al
callq 0x11370
movl $0x80, %edi
callq 0x1160
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x10f0
movl $0x10, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x11bd0
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x40, 0x30(%rax)
je 0xf3f0
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x28, 0x30(%rax)
jne 0xf3f5
jmp 0xf48d
movl 0x8(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0xf452
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
movl 0xc(%rsp), %eax
addl $0x40, %eax
movl %eax, %eax
movl %eax, %esi
shlq $0x3, %rsi
callq 0x1180
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xf447
leaq 0x41cd(%rip), %rdi      # 0x13608
leaq 0x5b44(%rip), %rsi      # 0x14f86
callq 0x114a0
movl 0xc(%rsp), %eax
addl $0x40, %eax
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %rdi
callq 0xefb0
movq %rax, %rdx
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x8(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x23, 0x30(%rax)
jne 0xf3c7
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x11bd0
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x2a, 0x30(%rax)
je 0xf4c7
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdx
leaq 0x5ad7(%rip), %rsi      # 0x14f97
movb $0x0, %al
callq 0x11370
movl 0x8(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x18(%rsp), %rax
addq $0x28, %rsp
retq
nopl (%rax)