// File: /workspace/github_c_makefile_star2_O0/Yamsman[P]scc/src/parse.c
// Function: parse_sdef
s_type *parse_sdef(lexer *lx) {
	s_type *type = type_new(TYPE_UNDEF);
	switch (lex_peek(lx).type) {
		case TOK_KW_STRUCT:
			type->kind = TYPE_STRUCT;
			break;
		case TOK_KW_UNION:
			type->kind = TYPE_UNION;
			break;
		default:
			type_del(type);
			return NULL;
	}
	lex_adv(lx);

	//Get identifier and member list
	char *tname = NULL;
	s_pos nloc, aloc;
	if (lex_peek(lx).type == TOK_IDENT) {
		tname = lex_peek(lx).dat.sval;
		nloc = lex_peek(lx).loc;
		lex_adv(lx);
	}
	aloc = lex_peek(lx).loc;
	if (lex_peek(lx).type == TOK_LBR)
		type->param = parse_sdef_body(lx);

	/* 
	 * Four cases:
	 * struct is anonymous (list but no name)
	 * struct is already defined (name but no list)
	 * struct needs to be defined (name and list)
	 * invalid (no name and no list)
	 */
	if (tname == NULL) {
		if (type->param.len == VECTOR_EMPTY) {
			c_error(&aloc, "Expected '{' or identifier before ...\n");
			type_del(type);
			return NULL;
		} 
	} else {
		symbol *s = NULL;
		if (type->param.len == VECTOR_EMPTY) {
			//Search the symbol table for an existing definition
			s = symtable_search(lx->stb, tname);
			type_del(type);
			if (s == NULL) {
				c_error(&nloc, "Undefined struct '%s'\n", tname);
				free(tname);
				return NULL;
			}
			free(tname);
			type = type_clone(s->type);
		} else {
			s = symtable_def(lx->stb, tname, type_clone(type), &nloc);
		}
	}

	return type;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x10(%rbp)
xorl %edi, %edi
callq 0xf720
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x9790
movl -0x48(%rbp), %eax
movl %eax, -0x18c(%rbp)
subl $0x49, %eax
je 0xaef4
jmp 0xaee7
movl -0x18c(%rbp), %eax
subl $0x4a, %eax
je 0xaf00
jmp 0xaf0c
movq -0x18(%rbp), %rax
movl $0x8, (%rax)
jmp 0xaf22
movq -0x18(%rbp), %rax
movl $0x9, (%rax)
jmp 0xaf22
movq -0x18(%rbp), %rdi
callq 0xfca0
movq $0x0, -0x8(%rbp)
jmp 0xb141
movq -0x10(%rbp), %rdi
callq 0x97e0
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x9790
cmpl $0x35, -0xb0(%rbp)
jne 0xafa1
movq -0x10(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x9790
movq -0xc0(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x9790
movq -0x108(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x97e0
movq -0x10(%rbp), %rsi
leaq -0x140(%rbp), %rdi
callq 0x9790
movq -0x138(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x9790
cmpl $0x5, -0x170(%rbp)
jne 0xb02d
movq -0x18(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x10(%rbp), %rdi
callq 0xbd80
movq %rax, %rcx
movq -0x198(%rbp), %rax
movq %rcx, -0x180(%rbp)
movq %rdx, -0x178(%rbp)
movq -0x180(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x178(%rbp), %rcx
movq %rcx, 0x30(%rax)
cmpq $0x0, -0x50(%rbp)
jne 0xb06b
movq -0x18(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0xb066
leaq -0x80(%rbp), %rdi
leaq 0x7ea3(%rip), %rsi      # 0x12eec
movb $0x0, %al
callq 0x3880
movq -0x18(%rbp), %rdi
callq 0xfca0
movq $0x0, -0x8(%rbp)
jmp 0xb141
jmp 0xb139
movq $0x0, -0x188(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0xb0f3
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0xfb60
movq %rax, -0x188(%rbp)
movq -0x18(%rbp), %rdi
callq 0xfca0
cmpq $0x0, -0x188(%rbp)
jne 0xb0d4
movq -0x50(%rbp), %rdx
leaq -0x68(%rbp), %rdi
leaq 0x7e59(%rip), %rsi      # 0x12f13
movb $0x0, %al
callq 0x3880
movq -0x50(%rbp), %rdi
callq 0x3030
movq $0x0, -0x8(%rbp)
jmp 0xb141
movq -0x50(%rbp), %rdi
callq 0x3030
movq -0x188(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xfdc0
movq %rax, -0x18(%rbp)
jmp 0xb137
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x1a8(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x18(%rbp), %rdi
callq 0xfdc0
movq -0x1a8(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
movq %rax, %rdx
leaq -0x68(%rbp), %rcx
callq 0xf5f0
movq %rax, -0x188(%rbp)
jmp 0xb139
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
nop