// File: /workspace/github_c_makefile_star2_O2/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:
testq %rdi, %rdi
je 0x6e9a
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r13
callq 0x22cc
movq %rax, %r15
movq (%r13), %rax
movq (%rax), %rcx
movq 0x10(%rax), %rax
movq -0x8(%rcx,%rax,8), %r14
movq 0x10(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x63a8
movslq %eax, %rbx
movl 0x4(%r15), %r12d
movq 0x10(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x63a8
cmpq %r12, %rbx
jae 0x6e9d
pushq $0x1
popq %rbx
movl %eax, %ecx
shlq %cl, %rbx
movq 0x8(%r15), %r12
movq 0x10(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x63a8
cltq
shrq $0x6, %rax
orq %rbx, (%r12,%rax,8)
jmp 0x6ea8
xorl %eax, %eax
retq
movslq %eax, %rsi
movq %r15, %rdi
callq 0x23b9
callq 0x28e4
movq %rax, %r14
movq %r13, %rdi
movq %r15, %rsi
callq 0x6622
movq %rax, %rdi
callq 0x7121
movq %rax, %rbx
movq 0x10(%r14), %rsi
movq %r14, -0x38(%rbp)
movq %r14, %rdi
movq %rax, %rdx
callq 0x29d6
callq 0x28e4
movq %rax, %r12
movq 0x10(%rax), %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x29d6
movq %r13, -0x40(%rbp)
movq %r12, -0x48(%rbp)
movq 0x10(%r12), %rsi
testq %rsi, %rsi
je 0x710e
movq (%r12), %rax
movq -0x8(%rax,%rsi,8), %r14
decq %rsi
movq %r12, %rdi
callq 0x2a66
movq (%r14), %rdi
callq 0x233f
movq %rax, -0x50(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x50(%rbp), %rdi
callq 0x26fa
cmpl $-0x1, %eax
je 0x70f2
movq 0x15f6a(%rip), %rcx     # 0x1ceb0
movq (%rcx), %rcx
cltq
movq (%rcx,%rax,8), %r15
movq 0x8(%r15), %rax
cmpb $0x0, 0x10(%rax)
jne 0x6f2d
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movl $0x2000, %ebx           # imm = 0x2000
cmpl $0x2d, 0x8(%rax)
pushq $0x18
popq %rcx
jne 0x6f84
movl $0x1000, %ebx           # imm = 0x1000
cmpb $0x1, 0x18(%rax)
pushq $0x10
popq %rcx
jne 0x6f84
movb $0x1, 0x29(%r14)
jmp 0x6f2d
orl (%rax), %ebx
leaq (%r14,%rcx), %rax
movq %rax, 0x30(%r14)
movq (%r14,%rcx), %rdi
movl %ebx, %esi
callq 0x3884
cmpl $-0x1, %eax
je 0x6fa7
cmpb $0x1, 0x28(%r14)
jne 0x6f2d
jmp 0x6fbe
movq 0x30(%r14), %rax
movq (%rax), %rdx
movl %ebx, %edi
xorl %esi, %esi
callq 0x378a
movq 0x30(%r14), %rcx
movq %rax, (%rcx)
movq (%r14), %rsi
movl 0x50(%rsi), %ebx
xorl %eax, %eax
cmpb $0x1, 0x54(%rsi)
jne 0x6fd1
movl 0x4(%rsi), %eax
decl %eax
movl %eax, 0x50(%rsi)
movq 0x8(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdx
movq %r13, %rdi
callq 0x6d22
movq %rax, %r15
movq (%r14), %rax
movl %ebx, 0x50(%rax)
testq %r15, %r15
je 0x6f2d
movq %r15, %rdi
callq 0x7169
movq %rax, %rdx
xorl %ebx, %ebx
xorl %r13d, %r13d
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %r12
cmpq %r12, %rbx
jae 0x7051
movq (%rcx), %rax
movq (%rax,%rbx,8), %r13
cmpq %rdx, 0x8(%r13)
jne 0x704c
movq (%r13), %rdi
movq %r15, %rsi
movq %rbx, -0x58(%rbp)
movq %r13, %rbx
movq %rdx, %r13
callq 0x620c
movq %r13, %rdx
movq %rbx, %r13
movq -0x58(%rbp), %rbx
movq -0x38(%rbp), %rcx
testl %eax, %eax
je 0x7051
incq %rbx
jmp 0x700e
movq %r15, %rdi
callq 0x7121
movq %rax, %r15
cmpq %r12, %rbx
jae 0x70b0
movq (%r13), %rdi
movq (%r15), %rsi
callq 0x62e5
testl %eax, %eax
movq -0x48(%rbp), %r12
je 0x70a2
movq %r13, %rdi
movq %r15, %rsi
callq 0x6431
testb %al, %al
je 0x7092
movb $0x1, 0x28(%r13)
cmpq %r14, %r13
sete %al
movl %eax, -0x2c(%rbp)
movq 0x10(%r12), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x29d6
movq -0x40(%rbp), %r13
movq %r15, %rdi
callq 0x61c4
jmp 0x70d8
movq -0x48(%rbp), %r12
movq 0x10(%r12), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x29d6
movq -0x38(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq %r15, %rdx
callq 0x29d6
movq -0x40(%rbp), %r13
cmpb $0x1, 0x38(%r14)
jne 0x6f2d
movq 0x30(%r14), %rax
movq (%rax), %rax
movl %ebx, 0xc(%rax)
jmp 0x6f2d
movq -0x50(%rbp), %rdi
callq 0x2311
movl -0x2c(%rbp), %eax
andb $0x1, %al
movb %al, 0x28(%r14)
movb $0x0, 0x38(%r14)
jmp 0x6ef8
movq -0x38(%rbp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq