// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl $0xffffffff, 0x515c(%rip) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movb $0x0, 0x1f(%rax)
leaq 0x472d(%rip), %rax      # 0x10aa0
movq (%rax), %rcx
leaq 0x48b3(%rip), %rax      # 0x10c30
movq %rcx, (%rax)
leaq 0x48a9(%rip), %rax      # 0x10c30
cmpq $0x0, (%rax)
je 0xc523
leaq 0x4898(%rip), %rax      # 0x10c30
movq (%rax), %rdi
addq $0x22, %rdi
movq -0x8(%rbp), %rsi
callq 0x1110
movl %eax, -0x20(%rbp)
leaq 0x487e(%rip), %rax      # 0x10c30
movq (%rax), %rdi
cmpl $0x0, -0x20(%rbp)
setl %al
andb $0x1, %al
movzbl %al, %esi
callq 0xbe80
cmpl $0x0, -0x20(%rbp)
je 0xc3f9
leaq 0x485d(%rip), %rax      # 0x10c30
movq (%rax), %rax
cmpl $0x0, -0x20(%rbp)
setl %cl
andb $0x1, %cl
movzbl %cl, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
leaq 0x483f(%rip), %rax      # 0x10c30
movq %rcx, (%rax)
jmp 0xc51e
cmpl $0x0, -0x1c(%rbp)
jne 0xc415
leaq 0x482a(%rip), %rax      # 0x10c30
movq (%rax), %rdi
addq $0x10, %rdi
movl -0x14(%rbp), %esi
callq 0xc150
cmpl $0x0, -0x14(%rbp)
je 0xc489
leaq 0x480e(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %ax
shrw $0x2, %ax
andw $0x1, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
je 0xc489
leaq 0x4e72(%rip), %rax      # 0x112b2
cmpb $0x0, (%rax)
je 0xc463
leaq 0x47e4(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %ax
shrw $0x5, %ax
andw $0x1, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0xc489
leaq 0x437a(%rip), %rax      # 0x107e4
cmpl $0x1, (%rax)
jne 0xc489
leaq 0x47ba(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $-0x9, %cx
orw $0x8, %cx
movw %cx, 0x20(%rax)
movl -0x14(%rbp), %edx
leaq 0x479d(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
shrw $0x2, %cx
andw $0x1, %cx
movzwl %cx, %ecx
orl %edx, %ecx
movw %cx, %dx
movw 0x20(%rax), %cx
andw $0x1, %dx
shlw $0x2, %dx
andw $-0x5, %cx
orw %dx, %cx
movw %cx, 0x20(%rax)
cmpl $0x0, -0x14(%rbp)
je 0xc519
cmpl $0x0, -0x18(%rbp)
je 0xc519
leaq 0x475c(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %ax
shrw $0x3, %ax
andw $0x1, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0xc519
movq -0x10(%rbp), %rcx
leaq 0x473a(%rip), %rax      # 0x10c30
movq (%rax), %rax
movq %rcx, 0x18(%rax)
leaq 0x472c(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $0xfeff, %cx            # imm = 0xFEFF
orw $0x100, %cx             # imm = 0x100
movw %cx, 0x20(%rax)
jmp 0xc801
jmp 0xc380
movq -0x8(%rbp), %rdi
callq 0x10a0
movl %eax, -0x24(%rbp)
cmpl $0x1f, -0x24(%rbp)
jle 0xc53c
movl $0x1f, -0x24(%rbp)
movl -0x24(%rbp), %edi
callq 0xbc90
movq %rax, %rcx
leaq 0x46e2(%rip), %rax      # 0x10c30
movq %rcx, (%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x24(%rbp), %ecx
movb %al, -0x25(%rbp)
jge 0xc58e
movq -0x8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movb (%rax,%rcx), %al
leaq 0x46b9(%rip), %rcx      # 0x10c30
movq (%rcx), %rcx
movslq -0x20(%rbp), %rdx
movb %al, 0x22(%rcx,%rdx)
movsbl %al, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x25(%rbp)
movb -0x25(%rbp), %al
testb $0x1, %al
jne 0xc597
jmp 0xc5a4
jmp 0xc599
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xc558
leaq 0x4685(%rip), %rax      # 0x10c30
movq (%rax), %rax
movslq -0x24(%rbp), %rcx
movb $0x0, 0x22(%rax,%rcx)
movq -0x10(%rbp), %rcx
leaq 0x466e(%rip), %rax      # 0x10c30
movq (%rax), %rax
movq %rcx, 0x18(%rax)
leaq 0x4660(%rip), %rax      # 0x10c30
movq (%rax), %rax
movq $0x0, 0x8(%rax)
leaq 0x464e(%rip), %rax      # 0x10c30
movq (%rax), %rax
movq $0x0, (%rax)
leaq 0x463d(%rip), %rax      # 0x10c30
movq (%rax), %rax
movq $0x0, 0x10(%rax)
leaq 0x462b(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $-0x4, %cx
orw $0x2, %cx
movw %cx, 0x20(%rax)
movl -0x14(%rbp), %ecx
leaq 0x460e(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw %cx, %dx
movw 0x20(%rax), %cx
andw $0x1, %dx
shlw $0x2, %dx
andw $-0x5, %cx
orw %dx, %cx
movw %cx, 0x20(%rax)
movl -0x18(%rbp), %ecx
leaq 0x45e7(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw %cx, %dx
movw 0x20(%rax), %cx
andw $0x1, %dx
shlw $0x8, %dx
andw $0xfeff, %cx            # imm = 0xFEFF
orw %dx, %cx
movw %cx, 0x20(%rax)
leaq 0x45c2(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $0xff7f, %cx            # imm = 0xFF7F
orw $0x0, %cx
movw %cx, 0x20(%rax)
leaq 0x45a7(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $-0x9, %cx
orw $0x0, %cx
movw %cx, 0x20(%rax)
leaq 0x458d(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $-0x41, %cx
orw $0x0, %cx
movw %cx, 0x20(%rax)
leaq 0x4573(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $-0x11, %cx
orw $0x0, %cx
movw %cx, 0x20(%rax)
leaq 0x4559(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $-0x21, %cx
orw $0x0, %cx
movw %cx, 0x20(%rax)
leaq 0x453f(%rip), %rax      # 0x10c30
movq (%rax), %rax
movw 0x20(%rax), %cx
andw $0xfdff, %cx            # imm = 0xFDFF
orw $0x0, %cx
movw %cx, 0x20(%rax)
cmpl $0x0, -0x1c(%rbp)
jne 0xc721
leaq 0x451e(%rip), %rax      # 0x10c30
movq (%rax), %rdi
addq $0x10, %rdi
movl -0x14(%rbp), %esi
callq 0xc150
leaq 0x4508(%rip), %rax      # 0x10c30
movq (%rax), %rdi
movl $0x2, %esi
callq 0xbe80
movl 0x4d85(%rip), %edi      # 0x114c0
callq 0xbd40
movl 0x4d7a(%rip), %eax      # 0x114c0
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x4d6f(%rip)      # 0x114c0
cmpl $0x0, %eax
je 0xc801
movslq 0x4d5f(%rip), %rcx      # 0x114c0
leaq 0x4d68(%rip), %rax      # 0x114d0
movq (%rax,%rcx,8), %rax
movw 0x20(%rax), %ax
andw $0x3, %ax
movzwl %ax, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movslq 0x4d3c(%rip), %rdx      # 0x114c0
leaq 0x4e35(%rip), %rcx      # 0x115c0
cmpl (%rcx,%rdx,4), %eax
jne 0xc797
callq 0xbed0
jmp 0xc801
movslq 0x4d22(%rip), %rcx      # 0x114c0
leaq 0x4e1b(%rip), %rax      # 0x115c0
movl (%rax,%rcx,4), %ecx
movslq 0x4d11(%rip), %rdx      # 0x114c0
leaq 0x4d1a(%rip), %rax      # 0x114d0
movq (%rax,%rdx,8), %rax
movw %cx, %dx
movw 0x20(%rax), %cx
andw $0x3, %dx
andw $-0x4, %cx
orw %dx, %cx
movw %cx, 0x20(%rax)
cmpl $0x2, -0x20(%rbp)
je 0xc7fa
movslq 0x4ce3(%rip), %rcx      # 0x114c0
leaq 0x4cec(%rip), %rax      # 0x114d0
movq (%rax,%rcx,8), %rax
movw 0x20(%rax), %cx
andw $-0x4, %cx
orw $0x2, %cx
movw %cx, 0x20(%rax)
jmp 0xc801
jmp 0xc7fc
jmp 0xc740
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)