// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movl %r8d, (%rsp)
movq %rcx, 0x10(%rsp)
movl %edx, 0x4(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
movl $0xffffffff, 0x5945(%rip) # imm = 0xFFFFFFFF
movb $0x0, 0x1f(%rdi)
leaq 0x4f1a(%rip), %rax      # 0xdab0
movq (%rax), %r13
leaq 0x50a0(%rip), %rbp      # 0xdc40
movq %r13, (%rbp)
testq %r13, %r13
je 0x8bfb
leaq 0x5a20(%rip), %r15      # 0xe5d0
leaq 0x5929(%rip), %rbx      # 0xe4e0
xorl %r14d, %r14d
leaq 0x22(%r13), %rdi
movq %r12, %rsi
callq 0x1130
movl %eax, %ecx
shrl $0x1f, %ecx
movl %r14d, 0x58fe(%rip)     # 0xe4d0
movl %ecx, (%r15)
movq %r13, (%rbx)
testl %eax, %eax
je 0x8d65
movl %ecx, %eax
movq (%r13,%rax,8), %r13
movq %r13, (%rbp)
addq $0x4, %r15
addq $0x8, %rbx
incl %r14d
testq %r13, %r13
jne 0x8bba
movq %r12, %rdi
callq 0x10b0
movq %rax, %r15
cmpl $0x1f, %r15d
movl $0x1f, %r13d
cmovll %r15d, %r13d
movl %r13d, %edi
callq 0x8780
movq %rax, (%rbp)
testl %r15d, %r15d
jle 0x8c53
cmpl $0x2, %r13d
movl $0x1, %eax
cmovgel %r13d, %eax
decq %rax
xorl %ecx, %ecx
movb (%r12,%rcx), %dl
movq (%rbp), %rsi
movb %dl, 0x22(%rsi,%rcx)
testb %dl, %dl
je 0x8c53
leaq 0x1(%rcx), %rdx
cmpq %rcx, %rax
movq %rdx, %rcx
jne 0x8c37
movq (%rbp), %rax
movslq %r13d, %rcx
movb $0x0, 0x22(%rax,%rcx)
movq (%rbp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq (%rbp), %rdi
movq $0x0, 0x10(%rdi)
movl $0xfffffc00, %eax       # imm = 0xFFFFFC00
andl 0x20(%rdi), %eax
movl 0x4(%rsp), %esi
movl %esi, %ecx
andl $0x1, %ecx
movq 0x10(%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, (%rsp)
jne 0x8cb5
addq $0x10, %rdi
callq 0x8a36
movq (%rbp), %rax
movslq 0x5810(%rip), %rcx      # 0xe4d0
leaq 0x1(%rcx), %rdi
movl %edi, 0x5806(%rip)      # 0xe4d0
leaq 0x58ff(%rip), %rbx      # 0xe5d0
movl $0x2, 0x4(%rbx,%rcx,4)
leaq 0x5800(%rip), %r14      # 0xe4e0
movq %rax, 0x8(%r14,%rcx,8)
callq 0x87ec
movslq 0x57df(%rip), %rax      # 0xe4d0
movl %eax, %edx
movq %rax, %rdi
negq %rdi
leaq (%r14,%rax,8), %rsi
leaq (%rbx,%rax,4), %r8
addq $-0x4, %r8
xorl %ecx, %ecx
cmpq %rcx, %rdi
je 0x8dfa
movq -0x8(%rsi,%rcx,8), %r10
movzwl 0x20(%r10), %r11d
movl %r11d, %r9d
andl $0x3, %r9d
movl (%r8,%rcx,4), %ebx
cmpl %r9d, %ebx
je 0x8e13
andl $0x3, %ebx
andl $-0x4, %r11d
orl %ebx, %r11d
movw %r11w, 0x20(%r10)
decq %rcx
cmpw $0x2, %r9w
je 0x8d07
addl %ecx, %edx
movl %edx, 0x5781(%rip)      # 0xe4d0
movq (%rsi,%rcx,8), %rax
movl 0x20(%rax), %ecx
andl $-0x4, %ecx
orl $0x2, %ecx
movw %cx, 0x20(%rax)
jmp 0x8e04
cmpl $0x0, (%rsp)
movl 0x4(%rsp), %ebx
jne 0x8d7d
addq $0x10, %r13
movq %r13, %rdi
movl %ebx, %esi
callq 0x8a36
testl %ebx, %ebx
je 0x8db2
movq (%rbp), %rax
movzwl 0x20(%rax), %ecx
testb $0x4, %cl
je 0x8db2
leaq 0x552d(%rip), %rdx      # 0xe2c2
cmpb $0x0, (%rdx)
je 0x8d9f
testb $0x20, %cl
jne 0x8db2
leaq 0x4a4e(%rip), %rdx      # 0xd7f4
cmpl $0x1, (%rdx)
jne 0x8db2
orl $0x8, %ecx
movw %cx, 0x20(%rax)
testl %ebx, %ebx
setne %cl
movq (%rbp), %rax
movzwl 0x20(%rax), %edx
shll $0x2, %ebx
andl $0x4, %ebx
orl %edx, %ebx
movw %bx, 0x20(%rax)
cmpl $0x0, 0x10(%rsp)
setne %sil
andb %cl, %sil
testb $0x8, %dl
sete %cl
andb %sil, %cl
cmpb $0x1, %cl
jne 0x8e04
movq 0x8(%rsp), %rcx
movq %rcx, 0x18(%rax)
orl $0x100, %ebx            # imm = 0x100
movw %bx, 0x20(%rax)
jmp 0x8e04
movl $0xffffffff, 0x56cc(%rip) # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %ecx, %eax
decl %eax
movl %eax, 0x56b3(%rip)      # 0xe4d0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x889b