// File: /workspace/github_c_makefile_star2_O3/mist64[P]cbm6502asm/src/symbol.c
// Function: insert_symbol
void insert_symbol(char *sym, long value, int def, int known, int local) {
	int i, k;

	/* see if symbol already exists and find parent of new entry if not */

	level = -1;
	sym[31] = 0; /* truncate symbol if longer than 31 chars */
	ptr = root_symtab;
	while (ptr) {
		i = strcmp(ptr->name, sym);
		savepath(ptr, i < 0);
		if (i)
			ptr = ptr->link[i < 0];
		else {
			if (!local) addref(&ptr->xref, def);
			if (def && ptr->defined && (!equ || !ptr->equate) &&
			    pass == 1)
				ptr->multiple =
				        1; /* symbol is multiply defined */
			ptr->defined |= def;
			if (def && known && !ptr->multiple) {
				ptr->value = value;
				ptr->known = 1;
			}
			return;
		}
	}

	/* create a new symbol table entry */

	k = strlen(sym);
	if (k > 31) k = 31;
	ptr = (struct smbl *)allocsym(k);
	for (i = 0; (i < k) && (ptr->name[i] = sym[i]); i++)
		;                           /* copy symbol to table entry */
	ptr->name[k] = 0;                   /* terminating null character */
	ptr->value = value;                 /* value of symbol */
	ptr->link[0] = ptr->link[1] = NULL; /* no child nodes yet */
	ptr->xref = NULL;                   /* set up for xref list */
	ptr->weight = BALANCED;
	ptr->defined = def;
	ptr->known = known;
	ptr->label = ptr->equate = ptr->section = ptr->relocate =
	        ptr->multiple = ptr->external = 0;
	if (!local) addref(&ptr->xref, def);
	savepath(ptr, BALANCED);
	head(level); /* link the new symbol entry into the tree */

	/* balance the binary tree */

	while (level--) {
		i = path[level]->weight;
		if (i == direction[level]) {
			move();
			break;
		} else {
			path[level]->weight = direction[level];
			if (i != BALANCED) {
				path[level]->weight = BALANCED;
				break;
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r13
movl $0xffffffff, 0x5931(%rip) # imm = 0xFFFFFFFF
movb $0x0, 0x1f(%rdi)
leaq 0x4f06(%rip), %rax      # 0xdab0
movq (%rax), %r12
leaq 0x508c(%rip), %rbx      # 0xdc40
movq %r12, (%rbx)
testq %r12, %r12
je 0x8c0a
leaq 0x5a0d(%rip), %r15      # 0xe5d0
leaq 0x5916(%rip), %r14      # 0xe4e0
xorl %ebp, %ebp
leaq 0x22(%r12), %rdi
movq %r13, %rsi
callq 0x1130
movl %eax, %ecx
shrl $0x1f, %ecx
movl %ebp, 0x58ec(%rip)      # 0xe4d0
movl %ecx, (%r15)
movq %r12, (%r14)
testl %eax, %eax
je 0x8d68
movl %ecx, %eax
movq (%r12,%rax,8), %r12
movq %r12, (%rbx)
addq $0x4, %r15
addq $0x8, %r14
incl %ebp
testq %r12, %r12
jne 0x8bcc
movq %r13, %rdi
callq 0x10b0
movq %rax, %r15
cmpl $0x1f, %r15d
movl $0x1f, %r12d
cmovll %r15d, %r12d
movl %r12d, %edi
callq 0x8768
movq %rax, (%rbx)
testl %r15d, %r15d
jle 0x8c64
cmpl $0x2, %r12d
movl $0x1, %eax
cmovgel %r12d, %eax
decq %rax
xorl %ecx, %ecx
movb (%r13,%rcx), %dl
movq (%rbx), %rsi
movb %dl, 0x22(%rsi,%rcx)
testb %dl, %dl
je 0x8c61
leaq 0x1(%rcx), %rdx
cmpq %rcx, %rax
movq %rdx, %rcx
jne 0x8c45
movq (%rbx), %rax
movq 0x10(%rsp), %rsi
movslq %r12d, %rcx
movb $0x0, 0x22(%rax,%rcx)
movq (%rbx), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq (%rbx), %rdi
movq $0x0, 0x10(%rdi)
movl $0xfffffc00, %eax       # imm = 0xFFFFFC00
andl 0x20(%rdi), %eax
movl %esi, %ecx
andl $0x1, %ecx
movq 0x20(%rsp), %rdx
andl $0x1, %edx
shll $0x8, %edx
leal (%rdx,%rcx,4), %ecx
addl %ecx, %eax
addl $0x2, %eax
movw %ax, 0x20(%rdi)
cmpl $0x0, 0xc(%rsp)
jne 0x8cc5
addq $0x10, %rdi
callq 0x8a4e
movq (%rbx), %rdi
leaq 0x4de4(%rip), %r9       # 0xdab0
movslq 0x57fd(%rip), %rax      # 0xe4d0
movl %eax, %ecx
leaq 0x1(%rax), %rdx
leaq 0x58f0(%rip), %r8       # 0xe5d0
movl $0x2, 0x4(%r8,%rax,4)
leaq 0x57f0(%rip), %rsi      # 0xe4e0
movq %rdi, 0x8(%rsi,%rax,8)
testl %edx, %edx
je 0x8d05
movslq (%r8,%rax,4), %r9
shlq $0x3, %r9
addq (%rsi,%rax,8), %r9
movq %rdi, (%r9)
shlq $0x2, %rax
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x8d76
movq (%rsi,%rax,2), %r10
movzwl 0x20(%r10), %r11d
movl %r11d, %r9d
andl $0x3, %r9d
movl (%rax,%r8), %ebx
cmpl %r9d, %ebx
je 0x8d85
andl $0x3, %ebx
andl $-0x4, %r11d
orl %ebx, %r11d
movw %r11w, 0x20(%r10)
incq %rdi
addq $-0x4, %rax
cmpw $0x2, %r9w
je 0x8d0e
subl %edi, %edx
movl %edx, 0x577f(%rip)      # 0xe4d0
movq 0x8(%rsi,%rax,2), %rax
movl 0x20(%rax), %ecx
andl $-0x4, %ecx
orl $0x2, %ecx
movw %cx, 0x20(%rax)
jmp 0x8e22
cmpl $0x0, 0xc(%rsp)
je 0x8da0
movq 0x10(%rsp), %r14
jmp 0x8db7
movl $0xffffffff, 0x5750(%rip) # imm = 0xFFFFFFFF
jmp 0x8e22
subl %edi, %ecx
movl %ecx, 0x5743(%rip)      # 0xe4d0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8883
addq $0x10, %r12
movq %r12, %rdi
movq 0x10(%rsp), %r14
movl %r14d, %esi
callq 0x8a4e
movq (%rbx), %r12
movzwl 0x20(%r12), %eax
testl %r14d, %r14d
je 0x8de8
movl %eax, %ecx
andl $0x4, %ecx
je 0x8de8
leaq 0x54f2(%rip), %rcx      # 0xe2c2
cmpb $0x0, (%rcx)
je 0x8dd9
testb $0x20, %al
jne 0x8de8
leaq 0x4a14(%rip), %rcx      # 0xd7f4
cmpl $0x1, (%rcx)
jne 0x8de8
orl $0x8, %eax
leal (,%r14,4), %ecx
andl $0x4, %ecx
orl %eax, %ecx
movw %cx, 0x20(%r12)
testl %r14d, %r14d
je 0x8e22
cmpl $0x0, 0x20(%rsp)
je 0x8e22
andl $0x8, %eax
jne 0x8e22
movq 0x18(%rsp), %rax
movq %rax, 0x18(%r12)
orl $0x100, %ecx            # imm = 0x100
movw %cx, 0x20(%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq