// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl 0x48(%rbp), %eax
movl 0x40(%rbp), %eax
movl 0x38(%rbp), %eax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
callq 0x2b60
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x3b58
movq $0x0, -0x8(%rbp)
jmp 0x3e10
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl 0x28(%rbp), %eax
jge 0x3b8b
movq 0x20(%rbp), %rax
movslq -0x50(%rbp), %rcx
movw (%rax,%rcx,2), %dx
movq -0x40(%rbp), %rax
movslq -0x50(%rbp), %rcx
movw %dx, 0x18(%rax,%rcx,2)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3b5f
movl 0x28(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x74(%rax)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl 0x38(%rbp), %eax
jge 0x3bc8
movq 0x30(%rbp), %rax
movslq -0x50(%rbp), %rcx
movw (%rax,%rcx,2), %dx
movq -0x40(%rbp), %rax
movslq -0x50(%rbp), %rcx
movw %dx, 0x46(%rax,%rcx,2)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3b9c
movl 0x38(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x78(%rax)
movq 0x10(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x80(%rax)
movq 0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x88(%rax)
cmpq $0x0, -0x10(%rbp)
jne 0x3bfc
jmp 0x3ce6
movq 0x20(%rbp), %rax
movw (%rax), %ax
movl -0x1c(%rbp), %esi
movzwl %ax, %edi
callq 0x2970
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x3c3d
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x3ce6
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
cmpq $0x0, -0x28(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %edx
callq 0x4260
cmpl 0x40(%rbp), %eax
jl 0x3c7a
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x3ce6
jmp 0x3c7c
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x8(%rcx)
movb %al, -0x51(%rbp)
je 0x3cb3
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x40(%rbp), %rsi
cmpq $0x0, -0x28(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %edx
callq 0x4260
cmpl 0x40(%rbp), %eax
setl %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x3cbc
jmp 0x3cca
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x3c7c
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x0, -0x28(%rbp)
jne 0x3cf2
jmp 0x3ddc
movq 0x30(%rbp), %rax
movw (%rax), %ax
movl -0x34(%rbp), %esi
movzwl %ax, %edi
callq 0x2970
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x3d33
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x3ddc
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
cmpq $0x0, -0x10(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %edx
callq 0x4390
cmpl 0x48(%rbp), %eax
jl 0x3d70
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x3ddc
jmp 0x3d72
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x10(%rcx)
movb %al, -0x52(%rbp)
je 0x3da9
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rsi
cmpq $0x0, -0x10(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %edx
callq 0x4390
cmpl 0x48(%rbp), %eax
setl %al
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
testb $0x1, %al
jne 0x3db2
jmp 0x3dc0
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x3d72
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, -0x18(%rbp)
je 0x3df2
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0x3e08
movq -0x30(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)