// File: /workspace/github_c_makefile_star2_O0/MaoKo[P]FGFX/fgfp/src/lr.c
// Function: build_lalr1_states
vector_t*
build_lalr1_states(cfg_t const* cfg) {
	if (!cfg)
		{ return (NULL_VECT); }

	bitset_t* start_bitset = new_bitset();

	production_t* start_prod = BACK_VECTOR(cfg->productions);
	ADD_BITSET(start_bitset, (size_t)new_lr1_item(start_prod,
									start_prod->rhs_element, NULL_BITSET));

	vector_t* lr1_states = new_vector();
	lr1_state_t* start_state = new_lr1_state(closure(cfg, start_bitset));

	PUSH_BACK_VECTOR(lr1_states, start_state);

	vector_t* stack_todo_state = new_vector();
	PUSH_BACK_VECTOR(stack_todo_state, start_state);

	while (!EMPTY_VECTOR(stack_todo_state)) {
//		printf("SIZE VECTOR %zu\n", SIZE_VECTOR(lr1_states));

		lr1_state_t* state = (lr1_state_t*)BACK_VECTOR(stack_todo_state);
		POP_BACK_VECTOR(stack_todo_state);

		bitset_t* target_item = dup_bitset(state->items);
		bool self_merging = false;

		int j;
		while ((j = IT_NEXT(target_item)) != IT_NULL) {
			bitset_t* next = move_dot_next(cfg, state, j);
			if (next == NULL_BITSET)
				{ continue; }

			size_t hash_test = hash_lr1_item(next);
			size_t index;

			lr1_state_t* cmp_item = NULL;
			bool find = false;

			for (index = 0; index < SIZE_VECTOR(lr1_states); ++index) {
				cmp_item = (lr1_state_t*)AT_VECTOR(lr1_states, index);
				if (cmp_item->hash_items == hash_test
							&& !cmp_lr0_state(cmp_item->items, next)) {
					find = true;
					break;
				}
			}

			lr1_state_t* next_state = new_lr1_state(next);
			if (find) {
				if (cmp_lr1_state(cmp_item->items, next_state->items)) {
					if (merge_lr1_state(cmp_item, next_state)) {
						cmp_item->merged = true;
						self_merging = (cmp_item == state);
					}
					PUSH_BACK_VECTOR(stack_todo_state, cmp_item);
				}
				del_lr1_state(next_state);
			}
			else {
				PUSH_BACK_VECTOR(stack_todo_state, next_state);
				PUSH_BACK_VECTOR(lr1_states, next_state);
			}

			if (state->first_reach)
				{ (*(state->last_move))->state = index; }
		}
		del_bitset(target_item);

		state->merged = self_merging;
		state->first_reach = false;
	}

#if 0
	puts("== KERNEL ==");
	for (size_t i = 0; i < SIZE_VECTOR(record_kernel); ++i) {
		kernel_t* kernel = AT_VECTOR(record_kernel, i);
		print_state(cfg, kernel->kernel_item);
		puts("");
	}
#endif

	return (lr1_states);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x9313
movq $0x0, -0x8(%rbp)
jmp 0x964c
callq 0x1290
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x8670
cltq
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jb 0x9391
movq -0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x8670
movq -0x88(%rbp), %rdi
movslq %eax, %rsi
callq 0x1420
jmp 0x93ff
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x8670
movslq %eax, %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x8670
movq -0x98(%rbp), %rdx
movl %eax, %ecx
movq -0x90(%rbp), %rax
movslq %ecx, %rcx
shrq $0x6, %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
callq 0x1ee0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8b20
movq %rax, %rdi
callq 0x9660
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x2070
callq 0x1ee0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x2070
movq -0x38(%rbp), %rax
cmpq $0x0, 0x10(%rax)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x946d
jmp 0x9644
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
subq $0x1, %rsi
callq 0x21a0
movq -0x40(%rbp), %rax
movq (%rax), %rdi
callq 0x1350
movq %rax, -0x48(%rbp)
movb $0x0, -0x49(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1aa0
movl %eax, -0x50(%rbp)
cmpl $-0x1, %eax
je 0x9621
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x50(%rbp), %edx
callq 0x96e0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x94e3
jmp 0x94b1
movq -0x58(%rbp), %rdi
callq 0x98d0
movq %rax, -0x60(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x71(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x955e
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x60(%rbp), %rax
jne 0x954e
movq -0x70(%rbp), %rax
movq (%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x8350
cmpl $0x0, %eax
jne 0x954e
movb $0x1, -0x71(%rbp)
jmp 0x955e
jmp 0x9550
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x9504
movq -0x58(%rbp), %rdi
callq 0x9660
movq %rax, -0x80(%rbp)
testb $0x1, -0x71(%rbp)
je 0x95d4
movq -0x70(%rbp), %rax
movq (%rax), %rdi
movq -0x80(%rbp), %rax
movq (%rax), %rsi
callq 0x8500
cmpl $0x0, %eax
je 0x95c9
movq -0x70(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x87b0
testb $0x1, %al
jne 0x959c
jmp 0x95b4
movq -0x70(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x70(%rbp), %rax
cmpq -0x40(%rbp), %rax
sete %al
andb $0x1, %al
movb %al, -0x49(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x70(%rbp), %rdx
callq 0x2070
movq -0x80(%rbp), %rdi
callq 0x82e0
jmp 0x95fe
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x80(%rbp), %rdx
callq 0x2070
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x80(%rbp), %rdx
callq 0x2070
movq -0x40(%rbp), %rax
testb $0x1, 0x38(%rax)
je 0x961c
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq (%rax), %rax
movl %ecx, 0xc(%rax)
jmp 0x94b1
movq -0x48(%rbp), %rdi
callq 0x1300
movb -0x49(%rbp), %cl
movq -0x40(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x28(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x38(%rax)
jmp 0x9454
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)