// File: /workspace/github_c_makefile_star2_O1/Edilbert[P]BS9/bs9.c
// Function: DefineLabel
char *DefineLabel(char *p, int *val, char Type)
{
   int i,j,l,v;
   char *rop;

   *val = UNDEF; // preset

   if (Labels > MAXLAB -2)
   {
      ++ErrNum;
      ErrorMsg("Too many labels (> %d)\n",MAXLAB);
      exit(1);
   }
   p = GetSymbol(p,Label);
   if (*p == ':') ++p; // Ignore colon after label
   l = strlen(Label);
   p = SkipSpace(p);

   // parse definition of constants or variables

   for (i=0 ; i < LABTYPES ; ++i)
   {
      if (*p == '=' || !strcmpword(p,LabDef[i].Name)) break;
   }

   if (i < LABTYPES) // label definition
   {
      p += LabDef[i].Length;   // add keyword length
      j = LabelIndex(Label);
      if (j < 0)
      {
         j = Labels;
         lab[j].Name = (char *)StrNDup(Label,l);
         lab[j].Type = Type;
         lab[j].Address = UNDEF;
         lab[j].Locked  = 1;
         lab[j].Ref = (int *)MallocOrDie(sizeof(int));
         lab[j].Att = (int *)MallocOrDie(sizeof(int));
         Labels++;
      }
      lab[j].Ref[0] = LiNo;
      lab[j].Att[0] = LDEF;

      ExtractOpText(p);
      if (OpText[0])
      {
         p += strlen(p);
         rop = EvalOperand(OpText,&v,0);
         if (*rop)
         {
            ErrorLine(rop);
            ErrorMsg("Extra text after label assignment\n");
            exit(1);
         }
         if (lab[j].Address == UNDEF || LabDef[i].Type == 0)
             lab[j].Address = v;
         else if (lab[j].Address != v && !lab[j].Locked)
         {
            ++ErrNum;
            ErrorLine(p);
            ErrorMsg("*Multiple assignments for label [%s]\n"
                     "1st. value = $%4.4x   2nd. value = $%4.4x\n",
                     Label, lab[j].Address, v);
            exit(1);
         }
         *val = v;
         if (LabDef[i].Type > 0) EnumValue = v;
      }
      else if (LabDef[i].Type > 0) // ENUM
      {
         *val = ++EnumValue;
         if (lab[j].Address == UNDEF) lab[j].Address = *val;
         else if (lab[j].Address != *val)
         {
            ++ErrNum;
            ErrorLine(p);
            ErrorMsg("ENUM phase error\n");
            exit(1);
         }
      }
      else
      {
         ++ErrNum;
         ErrorLine(p);
         ErrorMsg("Missing operand\n");
         exit(1);
      }
      if (df) fprintf(df,"Define:%s=%4.4X\n",lab[j].Name,lab[j].Address);
   }
   else if (!strcmpword(p,"BSS"))
   {
      p = ExtractValue(p+4,&v);
      j = LabelIndex(Label);
      if (j < 0)
      {
         j = Labels;
         lab[j].Name = (char *)StrNDup(Label,l);
         lab[j].Type = 'B';
         lab[j].Address = UNDEF;
         lab[j].Ref = (int *)MallocOrDie(sizeof(int));
         lab[j].Att = (int *)MallocOrDie(sizeof(int));
         Labels++;
      }
      lab[j].Ref[0] = LiNo;
      lab[j].Att[0] = LBSS;
      if (lab[j].Address >= UNDEF) lab[j].Address = bss;
      else if (lab[j].Address != bss)
      {
         ++ErrNum;
         ErrorLine(p);
         ErrorMsg("Multiple assignments for BSS label [%s]\n"
                  "1st. value = $%4.4x   2nd. value = $%4.4x\n",
                  Label,lab[j].Address,bss);
         exit(1);
      }
      *val = bss;
      bss += v;
   }
   else
   {
      j = LabelIndex(Label);
      if (j < 0)
      {
         j = Labels;
         lab[j].Name = (char *)StrNDup(Label,l);
         lab[j].Type = Type;
         lab[j].Address = pc;
         lab[j].Ref = (int *)MallocOrDie(sizeof(int));
         lab[j].Att = (int *)MallocOrDie(sizeof(int));
         Labels++;
      }
      else if (lab[j].Address == UNDEF) lab[j].Address = pc;
      else if (lab[j].Address != pc && !lab[j].Locked)
      {
         if (Pass)
         {
            lab[j].Address = pc;
            ++Changes;
         }
         else
         {
            ErrorMsg("Phase error label [%s]"
                     " phase 1: %4.4x   phase 2: %4.4x\n",
                     Label,lab[j].Address,pc);
            exit(1);
         }
      }
      *val = pc;
      lab[j].Ref[0] = LiNo;
      lab[j].Att[0] = LPOS;
   }
   return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0xff0000, (%rsi)       # imm = 0xFF0000
cmpl $0x1f3f, 0x22209(%rip)  # imm = 0x1F3F
jge 0x3d09
movl %edx, %ebp
movq %rsi, %r12
leaq 0x81723(%rip), %r15     # 0x84f60
movq %r15, %rsi
callq 0x2aa4
movq %rax, %rbx
xorl %r14d, %r14d
cmpb $0x3a, (%rax)
sete %r14b
movq %r15, %rdi
callq 0x2110
movq %rax, 0x8(%rsp)
cmpb $0x0, (%rbx,%r14)
je 0x3888
movl %ebp, %r15d
callq 0x2320
movq (%rax), %rax
addq %r14, %rbx
decq %rbx
movsbq 0x1(%rbx), %rcx
incq %rbx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x3877
jmp 0x388e
movl %ebp, %r15d
addq %r14, %rbx
movb (%rbx), %bpl
xorl %r13d, %r13d
leaq 0x10ea5(%rip), %r14     # 0x14740
cmpb $0x3d, %bpl
je 0x391f
leaq (%r14,%r13), %rsi
movq %rbx, %rdi
callq 0x2cc9
testl %eax, %eax
je 0x391f
addq $0x10, %r13
cmpq $0x40, %r13
jne 0x389b
movq %r12, %rbp
leaq 0x8b2b(%rip), %rsi      # 0xc3f0
movq %rbx, %rdi
callq 0x2cc9
testl %eax, %eax
je 0x3b15
leaq 0x81684(%rip), %rdi     # 0x84f60
callq 0x357d
testl %eax, %eax
js 0x3c06
movl %eax, %r12d
movl %eax, %eax
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
leaq 0x22143(%rip), %rdx     # 0x25a40
leaq (%rdx,%rcx), %rax
movl 0xc(%rcx,%rdx), %edx
movl 0x10e29(%rip), %ecx     # 0x14734
cmpl $0xff0000, %edx         # imm = 0xFF0000
jne 0x3c87
movl %ecx, 0xc(%rax)
jmp 0x3ca7
movq %r12, 0x10(%rsp)
movslq 0x8(%r13,%r14), %rax
addq %rax, %rbx
leaq 0x8162d(%rip), %rdi     # 0x84f60
callq 0x357d
movl %eax, %r12d
testl %eax, %eax
jns 0x39d6
movslq 0x220ea(%rip), %r12     # 0x25a34
leaq 0x8160f(%rip), %rdi     # 0x84f60
movq 0x8(%rsp), %rsi
callq 0x2878
leaq (%r12,%r12,2), %rbp
shlq $0x4, %rbp
leaq 0x220d6(%rip), %rcx     # 0x25a40
movq %rax, (%rbp,%rcx)
movb %r15b, 0x8(%rbp,%rcx)
movl $0xff0000, 0xc(%rbp,%rcx) # imm = 0xFF0000
movl $0x1, 0x14(%rbp,%rcx)
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
leaq 0x2209b(%rip), %rax     # 0x25a40
movq %r15, 0x20(%rbp,%rax)
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
leaq 0x22075(%rip), %rax     # 0x25a40
movq %r15, 0x28(%rbp,%rax)
incl 0x2205e(%rip)           # 0x25a34
movl 0x2137c(%rip), %eax     # 0x24d58
movslq %r12d, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
leaq 0x22052(%rip), %rdx     # 0x25a40
leaq (%rdx,%rcx), %r12
movq 0x20(%rcx,%rdx), %rsi
movl %eax, (%rsi)
movq 0x28(%rcx,%rdx), %rax
movl $0x1, (%rax)
movq %rbx, %rdi
callq 0x369b
cmpb $0x0, 0x2158d(%rip)     # 0x24fa0
je 0x3aac
movq %rbx, %rdi
callq 0x2110
movq %rax, %r15
leaq 0x21575(%rip), %rdi     # 0x24fa0
leaq 0x4(%rsp), %rsi
xorl %edx, %edx
callq 0x3daa
cmpb $0x0, (%rax)
jne 0x3d24
addq %r15, %rbx
movl 0xc(%r12), %eax
cmpl $0xff0000, %eax         # imm = 0xFF0000
je 0x3a88
cmpl $0x0, 0xc(%r13,%r14)
je 0x3a88
cmpl 0x4(%rsp), %eax
je 0x3a91
cmpl $0x0, 0x14(%r12)
jne 0x3a91
incl 0x21fc1(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dcf
movl 0xc(%r12), %edx
movl 0x4(%rsp), %ecx
leaq 0x88e9(%rip), %rdi      # 0xc36c
jmp 0x3d91
movl 0x4(%rsp), %eax
movl %eax, 0xc(%r12)
movl 0x4(%rsp), %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
cmpl $0x0, 0xc(%r13,%r14)
jle 0x3ae9
movl %eax, 0x10c7a(%rip)     # 0x14724
jmp 0x3ae9
cmpl $0x0, 0xc(%r13,%r14)
jle 0x3d35
movl 0x10c66(%rip), %eax     # 0x14724
incl %eax
movl %eax, 0x10c5e(%rip)     # 0x14724
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl 0xc(%r12), %ecx
cmpl $0xff0000, %ecx         # imm = 0xFF0000
jne 0x3ae1
movl %eax, 0xc(%r12)
jmp 0x3ae9
cmpl %eax, %ecx
jne 0x3d4c
movq 0x21f20(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x3cf7
movq (%r12), %rdx
movl 0xc(%r12), %ecx
leaq 0x88d6(%rip), %rsi      # 0xc3df
xorl %eax, %eax
callq 0x2210
jmp 0x3cf7
addq $0x4, %rbx
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x34ef
movq %rax, %rbx
leaq 0x81430(%rip), %rdi     # 0x84f60
callq 0x357d
movl %eax, %r12d
testl %eax, %eax
jns 0x3bba
movslq 0x21ef1(%rip), %r12     # 0x25a34
leaq 0x81416(%rip), %rdi     # 0x84f60
movq 0x8(%rsp), %rsi
callq 0x2878
leaq (%r12,%r12,2), %r14
shlq $0x4, %r14
leaq 0x21edd(%rip), %r13     # 0x25a40
movq %rax, (%r14,%r13)
movb $0x42, 0x8(%r14,%r13)
movl $0xff0000, 0xc(%r14,%r13) # imm = 0xFF0000
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
movq %r15, 0x20(%r14,%r13)
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
movq %r15, 0x28(%r14,%r13)
incl 0x21e7a(%rip)           # 0x25a34
movl 0x21198(%rip), %eax     # 0x24d58
movslq %r12d, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
leaq 0x21e6e(%rip), %rdx     # 0x25a40
leaq (%rdx,%rcx), %r14
movq 0x20(%rcx,%rdx), %rsi
movl %eax, (%rsi)
movq 0x28(%rcx,%rdx), %rax
movl $0x2, (%rax)
movl 0xc(%rcx,%rdx), %eax
movl 0x21e3e(%rip), %ecx     # 0x25a30
cmpl $0xff0000, %eax         # imm = 0xFF0000
jl 0x3cdc
movl %ecx, 0xc(%r14)
jmp 0x3ce4
movslq 0x21e27(%rip), %r12     # 0x25a34
leaq 0x8134c(%rip), %rdi     # 0x84f60
movq 0x8(%rsp), %rsi
callq 0x2878
leaq (%r12,%r12,2), %r14
shlq $0x4, %r14
leaq 0x21e13(%rip), %r13     # 0x25a40
movq %rax, (%r14,%r13)
movb %r15b, 0x8(%r14,%r13)
movl 0x10af8(%rip), %eax     # 0x14734
movl %eax, 0xc(%r14,%r13)
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
movq %r15, 0x20(%r14,%r13)
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
movq %r15, 0x28(%r14,%r13)
incl 0x21daf(%rip)           # 0x25a34
jmp 0x3ca7
cmpl %ecx, %edx
je 0x3ca7
cmpl $0x0, 0x14(%rax)
jne 0x3ca7
cmpl $0x0, 0x21d80(%rip)     # 0x25a18
je 0x3da1
movl %ecx, 0xc(%rax)
incl 0x813b9(%rip)           # 0x85060
movl 0x10a87(%rip), %eax     # 0x14734
movl %eax, (%rbp)
movl 0x210a2(%rip), %eax     # 0x24d58
movslq %r12d, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
leaq 0x21d78(%rip), %rdx     # 0x25a40
movq 0x20(%rcx,%rdx), %rsi
movl %eax, (%rsi)
movq 0x28(%rcx,%rdx), %rax
movl $0x3, (%rax)
jmp 0x3cf7
cmpl %ecx, %eax
jne 0x3d72
movl 0x21d46(%rip), %eax     # 0x25a30
movl %eax, (%rbp)
movl 0x4(%rsp), %eax
addl %eax, 0x21d39(%rip)     # 0x25a30
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incl 0x21d1d(%rip)           # 0x25a2c
leaq 0x861b(%rip), %rdi      # 0xc331
movl $0x1f40, %esi           # imm = 0x1F40
xorl %eax, %eax
callq 0x2e63
jmp 0x3d68
movq %rax, %rdi
callq 0x2dcf
leaq 0x8616(%rip), %rdi      # 0xc349
jmp 0x3d61
incl 0x21cf1(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dcf
leaq 0x8684(%rip), %rdi      # 0xc3ce
jmp 0x3d61
incl 0x21cda(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dcf
leaq 0x865b(%rip), %rdi      # 0xc3bc
xorl %eax, %eax
callq 0x2e63
movl $0x1, %edi
callq 0x22f0
incl 0x21cb4(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dcf
movl 0xc(%r14), %edx
movl 0x21ca6(%rip), %ecx     # 0x25a30
leaq 0x8663(%rip), %rdi      # 0xc3f4
leaq 0x811c8(%rip), %rsi     # 0x84f60
xorl %eax, %eax
callq 0x2e63
jmp 0x3d68
leaq 0x869f(%rip), %rdi      # 0xc447
jmp 0x3d91