// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/crossassemblers/macro11/symbols.c
// Function: add_sym
SYMBOL         *add_sym(
    char *labelraw,
    unsigned value,
    unsigned flags,
    SECTION *section,
    SYMBOL_TABLE *table)
{
    SYMBOL         *sym;
    char            label[SYMMAX_MAX + 1];      // big size

    if (isdigit((unsigned char)labelraw[0])) {
        // Don't truncate local labels
        strncpy(label, labelraw, SYMMAX_MAX);
        label[SYMMAX_MAX] = 0;
    } else {
        //JH: truncate symbol to SYMMAX
        strncpy(label, labelraw, symbol_len);
        label[symbol_len] = 0;
    }

    sym = lookup_sym(label, table);
    if (sym != NULL) {
        // A symbol registered as "undefined" can be changed.

        if ((sym->flags & SYMBOLFLAG_UNDEFINED) && !(flags & SYMBOLFLAG_UNDEFINED)) {
            sym->flags &= ~(SYMBOLFLAG_PERMANENT | SYMBOLFLAG_UNDEFINED);
        }

        /* Check for compatible definition */
        else if (sym->section == section && sym->value == value) {
            sym->flags |= flags;       /* Merge flags quietly */
            return sym;                /* 's okay */
        }

        if (!(sym->flags & SYMBOLFLAG_PERMANENT)) {
            /* permit redefinition */
            sym->value = value;
            sym->flags |= flags;
            sym->section = section;
            return sym;
        }

        return NULL;                   /* Bad symbol redefinition */
    }

    sym = new_sym(label);
    sym->flags = flags;
    sym->stmtno = stmtno;
    sym->section = section;
    sym->value = value;

    add_table(sym, table);

    return sym;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r14
movq %rcx, %r15
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %rbx
callq 0x2300
movq (%rax), %rax
movzbl (%rbx), %ecx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x9fc1
movslq 0xbb3c(%rip), %r13      # 0x15ae8
movq %rsp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2080
movb $0x0, (%rsp,%r13)
jmp 0x9fd9
movq %rsp, %r13
movl $0x40, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2080
movb $0x0, 0x40(%r13)
movq %rsp, %rdi
movq %r14, %rsi
callq 0x9e32
testq %rax, %rax
je 0xa00f
movq %rax, %rbx
movl 0x10(%rax), %eax
testb $0x10, %bpl
jne 0xa09a
movl %eax, %ecx
andl $0x10, %ecx
je 0xa09a
andl $-0x12, %eax
movl %eax, 0x10(%rbx)
jmp 0xa0ad
movl $0x28, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe094
movq %rax, %rbx
movq %rsp, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe094
movq %rax, (%rbx)
movl %ebp, 0x10(%rbx)
leaq 0xbb23(%rip), %rcx      # 0x15b64
movl (%rcx), %ecx
movl %ecx, 0xc(%rbx)
movq %r15, 0x18(%rbx)
movl %r12d, 0x8(%rbx)
movb (%rax), %dl
xorl %ecx, %ecx
testb %dl, %dl
je 0xa08c
incq %rax
movl %ecx, %esi
addl %ecx, %esi
movsbl %dl, %ecx
xorl %esi, %ecx
movb (%rax), %dl
incq %rax
testb %dl, %dl
jne 0xa059
movl %ecx, %eax
imulq $0x401005, %rax, %rax   # imm = 0x401005
shrq $0x20, %rax
movl %ecx, %edx
subl %eax, %edx
shrl %edx
addl %eax, %edx
shrl $0x9, %edx
movl %edx, %eax
shll $0xa, %eax
subl %eax, %edx
addl %edx, %ecx
movq (%r14,%rcx,8), %rax
movq %rax, 0x20(%rbx)
movq %rbx, (%r14,%rcx,8)
jmp 0xa0c2
cmpq %r15, 0x18(%rbx)
jne 0xa0ad
cmpl %r12d, 0x8(%rbx)
jne 0xa0ad
orl %ebp, %eax
movl %eax, 0x10(%rbx)
jmp 0xa0c2
testb $0x1, %al
jne 0xa0c0
movl %r12d, 0x8(%rbx)
orl %ebp, %eax
movl %eax, 0x10(%rbx)
movq %r15, 0x18(%rbx)
jmp 0xa0c2
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq