// File: /workspace/github_c_makefile_star2_O2/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)
orl $-0x1, 0x638f(%rip)     # 0xe4e0
movq %rsi, 0x18(%rsp)
movq %rdi, %r13
movb $0x0, 0x1f(%rdi)
leaq 0x595c(%rip), %rax      # 0xdac0
leaq 0x6475(%rip), %rbp      # 0xe5e0
leaq 0x637e(%rip), %rbx      # 0xe4f0
xorl %r14d, %r14d
leaq 0x5ad4(%rip), %r15      # 0xdc50
movq (%rax), %r12
movq %r12, (%r15)
testq %r12, %r12
je 0x81c1
leaq 0x22(%r12), %rdi
movq %r13, %rsi
callq 0x1130
movl %eax, %ecx
shrl $0x1f, %ecx
movl %r14d, 0x6340(%rip)     # 0xe4e0
movl %ecx, (%rbp)
movq %r12, (%rbx)
testl %eax, %eax
je 0x8316
movl %ecx, %eax
leaq (%r12,%rax,8), %rax
addq $0x4, %rbp
addq $0x8, %rbx
incl %r14d
jmp 0x817c
movq %r13, %rdi
callq 0x10b0
cmpl $0x1f, %eax
pushq $0x1f
popq %r12
cmovll %eax, %r12d
movl %r12d, %edi
callq 0x7dec
movq %rax, (%r15)
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
movq 0x10(%rsp), %rsi
movq (%r15), %rdx
cmpq %rax, %rcx
je 0x820f
movb (%r13,%rax), %dil
movb %dil, 0x22(%rdx,%rax)
incq %rax
testb %dil, %dil
jne 0x81f2
movq (%r15), %rdx
movslq %r12d, %rax
movb $0x0, 0x22(%rdx,%rax)
movq (%r15), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq (%r15), %rdi
andq $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 0x8268
addq $0x10, %rdi
callq 0x800f
movq (%r15), %rdi
movslq 0x6271(%rip), %rcx      # 0xe4e0
leaq 0x1(%rcx), %rax
movl %eax, 0x6267(%rip)      # 0xe4e0
leaq 0x6360(%rip), %rbx      # 0xe5e0
movl $0x2, 0x4(%rbx,%rcx,4)
leaq 0x6261(%rip), %r14      # 0xe4f0
movq %rdi, 0x8(%r14,%rcx,8)
movl %eax, %edi
callq 0x7e54
movslq 0x623e(%rip), %rax      # 0xe4e0
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 0x83a4
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 0x83ba
andl $0x3, %ebx
andl $-0x4, %r11d
orl %ebx, %r11d
movw %r11w, 0x20(%r10)
decq %rcx
cmpw $0x2, %r9w
je 0x82b8
addl %ecx, %edx
movl %edx, 0x61e0(%rip)      # 0xe4e0
movq (%rsi,%rcx,8), %rax
movl 0x20(%rax), %ecx
andl $-0x4, %ecx
orl $0x2, %ecx
movw %cx, 0x20(%rax)
jmp 0x83ab
cmpl $0x0, 0xc(%rsp)
je 0x8324
movq 0x10(%rsp), %rbx
jmp 0x833a
addq $0x10, %r12
movq %r12, %rdi
movq 0x10(%rsp), %rbx
movl %ebx, %esi
callq 0x800f
movq (%r15), %r12
movzwl 0x20(%r12), %eax
testl %ebx, %ebx
je 0x836a
movl %eax, %ecx
andl $0x4, %ecx
je 0x836a
leaq 0x5f80(%rip), %rcx      # 0xe2d2
cmpb $0x0, (%rcx)
je 0x835b
testb $0x20, %al
jne 0x836a
leaq 0x54a2(%rip), %rcx      # 0xd804
cmpl $0x1, (%rcx)
jne 0x836a
orl $0x8, %eax
leal (,%rbx,4), %ecx
andl $0x4, %ecx
orl %eax, %ecx
movw %cx, 0x20(%r12)
testl %ebx, %ebx
je 0x83ab
cmpl $0x0, 0x20(%rsp)
je 0x83ab
andl $0x8, %eax
jne 0x83ab
movq 0x18(%rsp), %rax
movq %rax, 0x18(%r12)
orl $0x100, %ecx            # imm = 0x100
movw %cx, 0x20(%r12)
jmp 0x83ab
orl $-0x1, 0x6135(%rip)     # 0xe4e0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %ecx, %eax
decl %eax
movl %eax, 0x611c(%rip)      # 0xe4e0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7f03