// File: /workspace/github_c_makefile_star2_O2/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, %r15d
movq %r8, %r12
movq %rcx, %r13
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
callq 0x1f2f
movq %rax, %r14
testq %rax, %rax
je 0x285a
movl %r15d, 0xc(%rsp)
movl 0x88(%rsp), %ecx
movq 0x80(%rsp), %r15
movl 0x78(%rsp), %edi
movq 0x70(%rsp), %rax
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %rsi
xorl %r8d, %r8d
testl %edi, %edi
movl $0x0, %r9d
cmovgl %edi, %r9d
cmpq %r8, %r9
je 0x271b
movzwl (%rax,%r8,2), %r10d
movw %r10w, 0x18(%r14,%r8,2)
incq %r8
jmp 0x2706
movl %edi, 0x74(%r14)
xorl %edi, %edi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
cmpq %rdi, %r8
je 0x2742
movzwl (%r15,%rdi,2), %r9d
movw %r9w, 0x46(%r14,%rdi,2)
incq %rdi
jmp 0x272d
movl %ecx, 0x78(%r14)
movq %rsi, 0x80(%r14)
movq %rdx, 0x88(%r14)
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x27d7
movq %r12, 0x20(%rsp)
movzwl (%rax), %eax
xorl %edx, %edx
divl %ebp
leaq (%rcx,%rdx,8), %r12
movq (%r12), %rbp
testq %rbp, %rbp
je 0x27ce
movq %rbx, 0x18(%rsp)
xorl %ebx, %ebx
testq %r13, %r13
sete %bl
movq %rbp, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x2a0b
cmpl 0x90(%rsp), %eax
jge 0x27c0
movq %rbp, %r12
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
je 0x27bc
movq %rbp, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x2a0b
cmpl 0x90(%rsp), %eax
jl 0x279a
addq $0x8, %r12
movq %rbp, 0x8(%r14)
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rcx
movq %r14, (%r12)
movq 0x20(%rsp), %r12
testq %r13, %r13
je 0x284a
movzwl (%r15), %eax
xorl %edx, %edx
divl 0xc(%rsp)
leaq (,%rdx,8), %r15
addq %r13, %r15
movq (%r15), %r13
testq %r13, %r13
je 0x2847
movq %rbx, %rbp
xorl %ebx, %ebx
testq %rcx, %rcx
sete %bl
movq %r13, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x2a54
cmpl 0x98(%rsp), %eax
jge 0x2840
movq %r13, %r15
movq 0x10(%r13), %r13
testq %r13, %r13
je 0x283c
movq %r13, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x2a54
cmpl 0x98(%rsp), %eax
jl 0x281a
addq $0x10, %r15
movq %r13, 0x10(%r14)
movq %rbp, %rbx
movq %r14, (%r15)
testq %rbx, %rbx
je 0x2851
incl (%rbx)
testq %r12, %r12
je 0x285a
incl (%r12)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq