// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/crossassemblers/macro1/macro1.c
// Function: lookup
SYM_T *lookup( char *name, int type )
{
    int ix;				/* Insertion index */
    int lx;				/* Left index */
    int rx;				/* Right index */
    SYM_T *best;			/* best match */
    SYM_T *sym;

    /* YIKES!  Search dummies (and "R") before anything else!! */
    if (curmacro && curmacro->defn) {
	struct macdef *mdp = curmacro->defn;
	int i;

	for (i = 0, sym = mdp->args; i <= mdp->nargs; i++, sym++)
	    if (strcmp(name, sym->name) == 0)
		return sym;
    }

    lx = 0;
    rx = symbol_top - 1;
    best = NULL;
    while (lx <= rx) {
	int mx = (lx + rx) / 2;		/* Find center of search area. */
	int compare;

	sym = symtab + mx;

	compare = strcmp(name, sym->name);
	if (compare < 0)
	    rx = mx - 1;
	else if (compare > 0)
	    lx = mx + 1;
	else {				/* match */
	    if (overbar && !M_DEFINED(sym->type) && pass == 2) {
		sym->type = DEFINED;
		sym->val = vars_addr++;
		nvars++;
	    }
	    return sym;			/* return exact match */
	} /* match */

	/* save best non-exact match; MACRO returns last defined n-x match! */
	if ((M_PSEUDO(sym->type)||M_EPSEUDO(sym->type)||M_MACRO(sym->type)) &&
	    strncmp(name, sym->name, 3) == 0)
	    best = sym;
    } /* while */

    /* return best match (pseudo or macro) if any for lookups (not defns) */
    if (best && type == UNDEFINED)
	return best;

    /* Must put symbol in table if index is negative. */
    ix = lx;				/* insertion point */
    if( symbol_top + 1 >= SYMBOL_TABLE_SIZE ) {
	errorSymbol( &symbol_table_full, name, lexstart );
	exit( 1 );
    }

    for( rx = symbol_top; rx >= ix; rx-- )
	symtab[rx + 1] = symtab[rx];

    symbol_top++;

    /* Enter the symbol as UNDEFINED with a value of zero. */
    sym = symtab + ix;
    strcpy( sym->name, name );
    sym->type = UNDEFINED;
    sym->val  = 0;
    sym->xref_count = 0;
    if( xref && pass == 2 && sym->xref_index >= 0)
	xreftab[sym->xref_index] = 0;

    if (overbar)
	nvars++;

    return sym;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r14d
movq %rdi, 0x8(%rsp)
movq 0x7fd4(%rip), %rax      # 0xc0c0
testq %rax, %rax
je 0x4124
movq 0x70(%rax), %rbx
testq %rbx, %rbx
je 0x4124
movl (%rbx), %ebp
testl %ebp, %ebp
js 0x4124
addq $0x4, %rbx
incl %ebp
leaq 0x4(%rbx), %rsi
movq 0x8(%rsp), %rdi
callq 0x20f0
testl %eax, %eax
je 0x42fc
addq $0x18, %rbx
decl %ebp
jne 0x4106
movslq 0x5c25(%rip), %r8       # 0x9d50
testq %r8, %r8
jle 0x421a
movl %r14d, 0x14(%rsp)
movq %r8, 0x18(%rsp)
leal -0x1(%r8), %ebp
xorl %r13d, %r13d
movq 0x5bfc(%rip), %rax      # 0x9d48
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
leal (%r13,%rbp), %r12d
shrl %r12d
leaq (%r12,%r12,2), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
leaq (%rcx,%rax,8), %r15
addq $0x4, %r15
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x20f0
testl %eax, %eax
jns 0x4189
decl %r12d
movl %r12d, %ebp
jmp 0x4195
je 0x42c2
incl %r12d
movl %r12d, %r13d
movl (%r14), %eax
movl %eax, %ecx
notl %ecx
testl $0x101, %ecx            # imm = 0x101
je 0x41ba
movl %eax, %ecx
andl $0x43, %ecx
cmpl $0x43, %ecx
je 0x41ba
andl $0x83, %eax
cmpl $0x83, %eax
jne 0x41d2
movl $0x3, %edx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x2060
testl %eax, %eax
cmoveq %r14, %rbx
cmpl %ebp, %r13d
jle 0x4153
cmpl $0x0, 0x14(%rsp)
jne 0x41eb
testq %rbx, %rbx
jne 0x42fc
movq 0x18(%rsp), %r8
cmpl $0x1ffe, %r8d           # imm = 0x1FFE
jle 0x421d
movl 0x7eb1(%rip), %edx      # 0xc0b0
leaq 0x59ea(%rip), %rdi      # 0x9bf0
movq 0x8(%rsp), %rsi
callq 0x4353
movl $0x1, %edi
callq 0x2170
xorl %r13d, %r13d
movq 0x5b24(%rip), %rax      # 0x9d48
movl %r13d, %ecx
cmpl %r13d, %r8d
jl 0x4253
leaq 0x1(%r8), %rdx
leaq (%r8,%r8,2), %rsi
leaq (%rax,%rsi,8), %rsi
movq 0x10(%rsi), %rdi
movq %rdi, 0x28(%rsi)
movups (%rsi), %xmm0
movups %xmm0, 0x18(%rsi)
decq %rdx
addq $-0x18, %rsi
cmpq %rcx, %rdx
jg 0x4238
incl %r8d
movl %r8d, 0x5af3(%rip)      # 0x9d50
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbx
leaq (%rax,%rcx,8), %r15
addq $0x4, %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x2070
xorl %eax, %eax
movl %eax, -0x4(%r15)
movl %eax, 0x8(%r15)
movl %eax, 0x10(%r15)
cmpb $0x0, 0x5ada(%rip)      # 0x9d69
je 0x42b1
cmpl $0x2, 0x5ae8(%rip)      # 0x9d80
jne 0x42b1
movslq 0x10(%rbx), %rax
testq %rax, %rax
js 0x42b1
movq 0x5c0e(%rip), %rcx      # 0x9eb8
movl $0x0, (%rcx,%rax,4)
cmpl $0x0, 0x9100(%rip)      # 0xd3b8
je 0x42fc
incl 0x7e90(%rip)            # 0xc150
jmp 0x42fc
cmpl $0x0, 0x90ef(%rip)      # 0xd3b8
je 0x42f9
testb $0x1, (%r14)
jne 0x42f9
cmpl $0x2, 0x5aa8(%rip)      # 0x9d80
jne 0x42f9
movl $0x1, (%r14)
movl 0x90d5(%rip), %eax      # 0xd3bc
leal 0x1(%rax), %ecx
movl %ecx, 0x90cc(%rip)      # 0xd3bc
movl %eax, 0xc(%r14)
movq %r14, %rbx
jmp 0x42ba
movq %r14, %rbx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq