// File: /workspace/github_c_makefile_star2_O3/MikeGray-APH[P]LibLouisAPH/source/table.c
// Function: rule_add_new
static struct rule* rule_add_new(
	struct rule **chars_hash,
	int *chars_hash_cnt,
	const unsigned int chars_hash_size,
	struct rule **dots_hash,
	int *dots_hash_cnt,
	const unsigned int dots_hash_size,
	struct filter *filter_forward,
	struct filter *filter_backward,
	const unichar *chars,
	const int chars_len,
	const unichar *dots,
	const int dots_len,
	const int chars_weight,
	const int dots_weight)
{
	struct rule *rule_new, *rule_crs;
	int hash;
	int i;

	rule_new = rule_allocate();
	if(!rule_new)
		return NULL;

	for(i = 0; i < chars_len; i++)
		rule_new->chars[i] = chars[i];
	rule_new->chars_len = chars_len;

	for(i = 0; i < dots_len; i++)
		rule_new->dots[i] = dots[i];
	rule_new->dots_len = dots_len;

	/*   must be set before adding rule to chains   */
	rule_new->filter_forward = filter_forward;
	rule_new->filter_backward = filter_backward;

	/*   add to chars_hash   */

	if(!chars_hash)
		goto add_to_dots_hash;

	hash = uchar_get_hash(chars[0], chars_hash_size);
	rule_crs = chars_hash[hash];

	/*   empty chain   */
	if(!rule_crs)
	{
		chars_hash[hash] = rule_new;
		goto add_to_dots_hash;
	}

	/*   check first rule in chain   */
	if(rule_chars_compare(rule_crs, rule_new, dots_hash == NULL) >= chars_weight)
	{
		rule_new->chars_nxt = rule_crs;
		chars_hash[hash] = rule_new;
		goto add_to_dots_hash;
	}

	/*   find rule position in chain   */
	while(rule_crs->chars_nxt && rule_chars_compare(rule_crs->chars_nxt, rule_new, dots_hash == NULL) < chars_weight)
		rule_crs = rule_crs->chars_nxt;

	rule_new->chars_nxt = rule_crs->chars_nxt;
	rule_crs->chars_nxt = rule_new;

	/*   add to dots_hash   */
	add_to_dots_hash:

	if(!dots_hash)
		goto return_rule;

	hash = uchar_get_hash(dots[0], dots_hash_size);
	rule_crs = dots_hash[hash];

	/*   empty chain   */
	if(!rule_crs)
	{
		dots_hash[hash] = rule_new;
		goto return_rule;
	}

	/*   check first rule in chain   */
	if(rule_dots_compare(rule_crs, rule_new, chars_hash == NULL) >= dots_weight)
	{
		rule_new->dots_nxt = rule_crs;
		dots_hash[hash] = rule_new;
		goto return_rule;
	}

	/*   find rule position in chain   */
	while(rule_crs->dots_nxt && rule_dots_compare(rule_crs->dots_nxt, rule_new, chars_hash == NULL) < dots_weight)
		rule_crs = rule_crs->dots_nxt;

	rule_new->dots_nxt = rule_crs->dots_nxt;
	rule_crs->dots_nxt = rule_new;

	return_rule:

	if(chars_hash_cnt)
		*chars_hash_cnt = *chars_hash_cnt + 1;
	if(dots_hash_cnt)
		*dots_hash_cnt = *dots_hash_cnt + 1;

	return rule_new;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0xc(%rsp)
movq %r8, %r15
movq %rcx, %r13
movl %edx, %ebx
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x1fe6
movq %rax, %r14
testq %rax, %rax
je 0x2937
movq %r15, 0x18(%rsp)
movl 0x88(%rsp), %r15d
movl 0x78(%rsp), %ebp
movq 0x70(%rsp), %rsi
testl %ebp, %ebp
jle 0x27ef
leaq 0x18(%r14), %rdi
leal (,%rbp,2), %edx
callq 0x1180
movq 0x80(%rsp), %rsi
movq 0x68(%rsp), %r12
movq 0x60(%rsp), %rcx
movl %ebp, 0x74(%r14)
testl %r15d, %r15d
jle 0x2825
movq %r14, %rdi
addq $0x46, %rdi
leal (%r15,%r15), %edx
movq %rsi, %rbp
callq 0x1180
movq 0x60(%rsp), %rcx
movq %rbp, %rsi
movl %r15d, 0x78(%r14)
movq %rcx, 0x80(%r14)
movq %r12, 0x88(%r14)
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x28b2
movq 0x70(%rsp), %rax
movzwl (%rax), %eax
xorl %edx, %edx
divl %ebx
leaq (%rcx,%rdx,8), %r15
movq (%r15), %rbx
testq %rbx, %rbx
je 0x28af
movl 0x90(%rsp), %r12d
xorl %ebp, %ebp
testq %r13, %r13
sete %bpl
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x2ad6
cmpl %r12d, %eax
jge 0x289e
movq %rbx, %r15
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
je 0x289a
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x2ad6
cmpl %r12d, %eax
jl 0x287c
addq $0x8, %r15
movq %rbx, 0x8(%r14)
movq 0x10(%rsp), %rcx
movq 0x80(%rsp), %rsi
movq %r14, (%r15)
testq %r13, %r13
je 0x291f
movzwl (%rsi), %eax
xorl %edx, %edx
divl 0xc(%rsp)
leaq (,%rdx,8), %r15
addq %r13, %r15
movq (%r15), %rbx
testq %rbx, %rbx
je 0x291c
movl 0x98(%rsp), %r12d
xorl %ebp, %ebp
testq %rcx, %rcx
sete %bpl
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x2b24
cmpl %r12d, %eax
jge 0x2918
movq %rbx, %r15
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
je 0x2914
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x2b24
cmpl %r12d, %eax
jl 0x28f6
addq $0x10, %r15
movq %rbx, 0x10(%r14)
movq %r14, (%r15)
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x292b
incl (%rax)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x2937
incl (%rax)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq