// File: /workspace/github_c_makefile_star2_O1/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 0x7587
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
callq 0x243c
movq %rax, %rbx
movq %r14, -0x50(%rbp)
movq (%r14), %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 0x69f6
movslq %eax, %r15
movl 0x4(%rbx), %r12d
movq 0x10(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x69f6
cmpq %r12, %r15
jae 0x758a
movl $0x1, %r15d
movl %eax, %ecx
shlq %cl, %r15
movq 0x8(%rbx), %r12
movq 0x10(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x69f6
cltq
shrq $0x6, %rax
orq %r15, (%r12,%rax,8)
jmp 0x7595
xorl %eax, %eax
retq
movslq %eax, %rsi
movq %rbx, %rdi
callq 0x2568
callq 0x2afc
movq %rax, %r14
movq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x6c92
movq %rax, %rdi
callq 0x784b
movq %rax, %rbx
movq 0x10(%r14), %rsi
movq %r14, -0x38(%rbp)
movq %r14, %rdi
movq %rax, %rdx
callq 0x2bf1
callq 0x2afc
movq %rax, %r14
movq 0x10(%rax), %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x2bf1
movq 0x10(%r14), %rsi
testq %rsi, %rsi
je 0x7838
movq %r14, %rdi
movq %r14, -0x48(%rbp)
movq (%rdi), %rax
movq -0x8(%rax,%rsi,8), %r13
decq %rsi
callq 0x2c7d
movq (%r13), %rdi
callq 0x24b8
movq %rax, -0x58(%rbp)
movq %rax, %rdi
callq 0x28eb
cmpl $-0x1, %eax
je 0x7812
leaq 0x18(%r13), %rcx
movq %rcx, -0x68(%rbp)
leaq 0x10(%r13), %rcx
movq %rcx, -0x60(%rbp)
xorl %r12d, %r12d
movq %r13, -0x40(%rbp)
movq 0x15862(%rip), %rcx     # 0x1cea0
movq (%rcx), %rcx
cltq
movq (%rcx,%rax,8), %r15
movq 0x8(%r15), %rax
cmpb $0x0, 0x10(%rax)
je 0x7658
xorl %ebx, %ebx
jmp 0x7703
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x2d, 0x8(%rax)
jne 0x7673
cmpb $0x1, 0x18(%rax)
jne 0x7682
movb $0x1, 0x29(%r13)
jmp 0x7651
movl (%rax), %ebx
movl $0x2000, %eax           # imm = 0x2000
orl %eax, %ebx
movq -0x68(%rbp), %rax
jmp 0x768f
movl (%rax), %ebx
movl $0x1000, %eax           # imm = 0x1000
orl %eax, %ebx
movq -0x60(%rbp), %rax
movq %rax, 0x30(%r13)
movq 0x30(%r13), %rax
movq (%rax), %rdi
movl %ebx, %esi
callq 0x3cb7
cmpl $-0x1, %eax
je 0x76af
cmpb $0x1, 0x28(%r13)
jne 0x7651
jmp 0x76c6
movq 0x30(%r13), %rax
movq (%rax), %rdx
movl %ebx, %edi
xorl %esi, %esi
callq 0x3bab
movq 0x30(%r13), %rcx
movq %rax, (%rcx)
movq (%r13), %rsi
movl 0x50(%rsi), %r14d
testq %rsi, %rsi
movq -0x50(%rbp), %rdi
je 0x76e7
xorl %eax, %eax
cmpb $0x1, 0x54(%rsi)
jne 0x76e4
movl 0x4(%rsi), %eax
decl %eax
movl %eax, 0x50(%rsi)
movq 0x8(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdx
callq 0x7416
movq %rax, %rbx
movq (%r13), %rax
movl %r14d, 0x50(%rax)
testq %rbx, %rbx
je 0x77fa
movl %r12d, -0x2c(%rbp)
movq %rbx, %rdi
callq 0x78a0
movq -0x38(%rbp), %rcx
cmpq $0x0, 0x10(%rcx)
setne %r14b
je 0x775d
movq %rax, %r15
xorl %r12d, %r12d
movq (%rcx), %rax
movq (%rax,%r12,8), %r13
cmpq %r15, 0x8(%r13)
jne 0x774e
movq (%r13), %rdi
movq %rbx, %rsi
callq 0x6820
movq -0x38(%rbp), %rcx
testl %eax, %eax
je 0x7763
incq %r12
cmpq 0x10(%rcx), %r12
setb %r14b
jb 0x772d
jmp 0x7763
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x784b
movq %rax, %rbx
testb $0x1, %r14b
je 0x77c0
movq (%r13), %rdi
movq (%rbx), %rsi
callq 0x2726
testb %al, %al
jne 0x77b2
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6a8c
testb %al, %al
je 0x77a2
movb $0x1, 0x28(%r13)
cmpq -0x40(%rbp), %r13
sete %al
movl %eax, -0x2c(%rbp)
movq -0x48(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq %r13, %rdx
callq 0x2bf1
movq -0x40(%rbp), %r13
movq %rbx, %rdi
callq 0x67d8
jmp 0x77e4
movq -0x48(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq %rbx, %rdx
callq 0x2bf1
movq -0x38(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq %rbx, %rdx
callq 0x2bf1
movq -0x40(%rbp), %r13
cmpb $0x1, 0x38(%r13)
jne 0x77f6
movq 0x30(%r13), %rax
movq (%rax), %rax
movl %r12d, 0xc(%rax)
movl -0x2c(%rbp), %r12d
movq -0x58(%rbp), %rdi
callq 0x28eb
cmpl $-0x1, %eax
jne 0x7637
andb $0x1, %r12b
jmp 0x7815
xorl %r12d, %r12d
movq -0x58(%rbp), %rdi
callq 0x248a
movb %r12b, 0x28(%r13)
movb $0x0, 0x38(%r13)
movq -0x48(%rbp), %rdi
movq 0x10(%rdi), %rsi
testq %rsi, %rsi
jne 0x75f2
movq -0x38(%rbp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq