// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x1fac
testq %rax, %rax
je 0x2a06
movq %rax, %rbx
movq %r15, 0x18(%rsp)
movl 0x88(%rsp), %r15d
movl 0x78(%rsp), %ebp
movq 0x70(%rsp), %rsi
testl %ebp, %ebp
jle 0x28bc
leaq 0x18(%rbx), %rdi
leal (,%rbp,2), %edx
callq 0x1170
movq 0x80(%rsp), %rsi
movq 0x68(%rsp), %r12
movq 0x60(%rsp), %rcx
movl %ebp, 0x74(%rbx)
testl %r15d, %r15d
jle 0x28f1
movq %rbx, %rdi
addq $0x46, %rdi
leal (%r15,%r15), %edx
movq %rsi, %rbp
callq 0x1170
movq 0x60(%rsp), %rcx
movq %rbp, %rsi
movl %r15d, 0x78(%rbx)
movq %rcx, 0x80(%rbx)
movq %r12, 0x88(%rbx)
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x297f
movq 0x70(%rsp), %rax
movzwl (%rax), %eax
xorl %edx, %edx
divl %r14d
leaq (%rcx,%rdx,8), %r15
movq (%r15), %r14
testq %r14, %r14
je 0x297c
movl 0x90(%rsp), %r12d
xorl %ebp, %ebp
testq %r13, %r13
sete %bpl
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x2ba7
cmpl %r12d, %eax
jge 0x296b
movq %r14, %r15
movq 0x8(%r14), %r14
testq %r14, %r14
je 0x2967
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x2ba7
cmpl %r12d, %eax
jl 0x2949
addq $0x8, %r15
movq %r14, 0x8(%rbx)
movq 0x10(%rsp), %rcx
movq 0x80(%rsp), %rsi
movq %rbx, (%r15)
testq %r13, %r13
je 0x29ec
movzwl (%rsi), %eax
xorl %edx, %edx
divl 0xc(%rsp)
leaq (,%rdx,8), %r15
addq %r13, %r15
movq (%r15), %r14
testq %r14, %r14
je 0x29e9
movl 0x98(%rsp), %r12d
xorl %ebp, %ebp
testq %rcx, %rcx
sete %bpl
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x2bf5
cmpl %r12d, %eax
jge 0x29e5
movq %r14, %r15
movq 0x10(%r14), %r14
testq %r14, %r14
je 0x29e1
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x2bf5
cmpl %r12d, %eax
jl 0x29c3
addq $0x10, %r15
movq %r14, 0x10(%rbx)
movq %rbx, (%r15)
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x29f8
incl (%rax)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x2a08
incl (%rax)
jmp 0x2a08
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq