// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
je 0x72d8
movq %rdi, %rbx
callq 0x243c
movq %rax, %r15
movq %rbx, -0x48(%rbp)
movq (%rbx), %rax
movq (%rax), %rcx
movq 0x10(%rax), %rax
movq -0x8(%rcx,%rax,8), %rbx
movq 0x10(%rbx), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x67ee
movslq %eax, %r14
movl 0x4(%r15), %r12d
movq 0x10(%rbx), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x67ee
cmpq %r12, %r14
jae 0x72e0
movl $0x1, %r14d
movl %eax, %ecx
shlq %cl, %r14
movq 0x8(%r15), %r12
movq 0x10(%rbx), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x67ee
cltq
shrq $0x6, %rax
orq %r14, (%r12,%rax,8)
jmp 0x72eb
xorl %r14d, %r14d
jmp 0x7582
movslq %eax, %rsi
movq %r15, %rdi
callq 0x2570
callq 0x2aec
movq %rax, %r14
movq -0x48(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x6a6c
movq %rax, %rdi
callq 0x7594
movq %rax, %rbx
movq 0x10(%r14), %rsi
movq %r14, %rdi
movq %rax, %rdx
callq 0x2be1
callq 0x2aec
movq %rax, %r15
movq 0x10(%rax), %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x2be1
movq 0x10(%r15), %rsi
testq %rsi, %rsi
je 0x7582
movq %r15, %rdi
movq %r15, -0x50(%rbp)
movq (%rdi), %rax
movq -0x8(%rax,%rsi,8), %r13
decq %rsi
callq 0x2c71
movq (%r13), %rdi
callq 0x24b4
movq %rax, %rbx
movq %rax, %rdi
callq 0x28dc
xorl %r15d, %r15d
cmpl $-0x1, %eax
je 0x7560
movq %rbx, -0x38(%rbp)
movq %r13, -0x40(%rbp)
movl %r15d, -0x2c(%rbp)
movq 0x15b16(%rip), %rcx     # 0x1cea0
movq (%rcx), %rcx
cltq
movq (%rcx,%rax,8), %r15
movq 0x8(%r15), %rax
cmpb $0x0, 0x10(%rax)
jne 0x74cb
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x2d, 0x8(%rax)
jne 0x73bf
cmpb $0x1, 0x18(%rax)
jne 0x73cb
movb $0x1, 0x29(%r13)
jmp 0x74cb
movl $0x18, %ecx
movl $0x2000, %ebx           # imm = 0x2000
jmp 0x73d5
movl $0x10, %ecx
movl $0x1000, %ebx           # imm = 0x1000
orl (%rax), %ebx
leaq (%rcx,%r13), %rax
movq %rax, 0x30(%r13)
movq (%r13,%rcx), %rdi
movl %ebx, %esi
callq 0x3bbb
cmpl $-0x1, %eax
je 0x73fc
cmpb $0x1, 0x28(%r13)
je 0x7413
jmp 0x74c7
movq 0x30(%r13), %rax
movq (%rax), %rdx
movl %ebx, %edi
xorl %esi, %esi
callq 0x3aaf
movq 0x30(%r13), %rcx
movq %rax, (%rcx)
movq (%r13), %rsi
movl 0x50(%rsi), %ebx
xorl %eax, %eax
cmpb $0x1, 0x54(%rsi)
jne 0x7427
movl 0x4(%rsi), %eax
decl %eax
movl %eax, 0x50(%rsi)
movq 0x8(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdx
movq %r12, %rdi
callq 0x716b
movq %rax, %r13
movq -0x40(%rbp), %rax
movq (%rax), %rax
movl %ebx, 0x50(%rax)
testq %r13, %r13
je 0x74c3
movq %r13, %rdi
callq 0x75de
movq %rax, %r15
movq 0x10(%r14), %rax
xorl %r12d, %r12d
testq %rax, %rax
je 0x748f
movq (%r14), %rcx
movq (%rcx,%r12,8), %rbx
cmpq %r15, 0x8(%rbx)
jne 0x7487
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x6640
testl %eax, %eax
je 0x74e2
movq 0x10(%r14), %rax
incq %r12
cmpq %rax, %r12
jb 0x7467
movq %r13, %rdi
callq 0x7594
movq %rax, %rbx
movq -0x50(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq %rax, %rdx
callq 0x2be1
movq 0x10(%r14), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x2be1
movq -0x38(%rbp), %rbx
movl -0x2c(%rbp), %r15d
jmp 0x753d
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %rbx
movl -0x2c(%rbp), %r15d
movq %rbx, %rdi
callq 0x28dc
cmpl $-0x1, %eax
jne 0x737f
jmp 0x755c
movq %r13, %rdi
callq 0x7594
movq %rax, %r13
movq (%rbx), %rdi
movq (%rax), %rsi
callq 0x272e
testb %al, %al
je 0x7502
movl -0x2c(%rbp), %r15d
jmp 0x7531
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6884
testb %al, %al
movl -0x2c(%rbp), %r15d
je 0x7521
movb $0x1, 0x28(%rbx)
cmpq -0x40(%rbp), %rbx
sete %r15b
movq -0x50(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq %rbx, %rdx
callq 0x2be1
movq %r13, %rdi
callq 0x65f8
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %r13
cmpb $0x1, 0x38(%r13)
jne 0x7553
movq 0x30(%r13), %rax
movq (%rax), %rax
movl %r12d, 0xc(%rax)
movq -0x48(%rbp), %r12
jmp 0x74cf
andb $0x1, %r15b
movq %rbx, %rdi
callq 0x2486
movb %r15b, 0x28(%r13)
movb $0x0, 0x38(%r13)
movq -0x50(%rbp), %rdi
movq 0x10(%rdi), %rsi
testq %rsi, %rsi
jne 0x7347
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq