// File: /workspace/github_c_makefile_star2_O3/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, 0x221c4(%rip)  # imm = 0x1F3F
jge 0x3d51
movl %edx, %r12d
movq %rsi, %rbp
leaq 0x816dd(%rip), %r15     # 0x84f60
movq %r15, %rsi
callq 0x2a9c
movq %rax, %rbx
xorl %r14d, %r14d
cmpb $0x3a, (%rax)
sete %r14b
movq %r15, %rdi
callq 0x2110
movq %rax, 0x10(%rsp)
cmpb $0x0, (%rbx,%r14)
je 0x38d5
callq 0x2320
movq (%rax), %rax
addq %r14, %rbx
decq %rbx
movsbq 0x1(%rbx), %rcx
incq %rbx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x38ba
movl %r12d, %r15d
cmpb $0x3d, %cl
sete %r14b
jmp 0x38de
movl %r12d, %r15d
addq %r14, %rbx
xorl %r14d, %r14d
xorl %r12d, %r12d
leaq 0x10e58(%rip), %r13     # 0x14740
testb %r14b, %r14b
jne 0x3968
leaq (%r12,%r13), %rsi
movq %rbx, %rdi
callq 0x2cd8
testl %eax, %eax
je 0x3968
addq $0x10, %r12
cmpq $0x40, %r12
jne 0x38e8
leaq 0x8ace(%rip), %rsi      # 0xc3dc
movq %rbx, %rdi
callq 0x2cd8
testl %eax, %eax
je 0x3b44
leaq 0x8163b(%rip), %rdi     # 0x84f60
callq 0x35bd
testl %eax, %eax
js 0x3b88
movl %eax, %r12d
movl %eax, %eax
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
leaq 0x220fa(%rip), %rdx     # 0x25a40
leaq (%rdx,%rcx), %rax
movl 0xc(%rcx,%rdx), %edx
movl 0x10de0(%rip), %ecx     # 0x14734
cmpl $0xff0000, %edx         # imm = 0xFF0000
jne 0x3c12
movl %ecx, 0xc(%rax)
jmp 0x3d12
movslq 0x8(%r12,%r13), %rax
addq %rax, %rbx
leaq 0x815e9(%rip), %rdi     # 0x84f60
callq 0x35bd
testl %eax, %eax
js 0x399c
movl %eax, %r14d
leaq (%r14,%r14,2), %rax
shlq $0x4, %rax
leaq 0x220ae(%rip), %rcx     # 0x25a40
movq 0x28(%rax,%rcx), %r15
jmp 0x3a39
movslq 0x22091(%rip), %r14     # 0x25a34
leaq 0x815b6(%rip), %rdi     # 0x84f60
movq 0x10(%rsp), %rsi
callq 0x2878
movq %rbp, 0x10(%rsp)
leaq (%r14,%r14,2), %rbp
shlq $0x4, %rbp
leaq 0x22078(%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 0x2203d(%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 0x22017(%rip), %rax     # 0x25a40
movq %r15, 0x28(%rbp,%rax)
movq 0x10(%rsp), %rbp
incl 0x21ffb(%rip)           # 0x25a34
movl 0x21319(%rip), %eax     # 0x24d58
leaq (%r14,%r14,2), %rcx
shlq $0x4, %rcx
leaq 0x21ff2(%rip), %rdx     # 0x25a40
leaq (%rdx,%rcx), %r14
movq 0x20(%rcx,%rdx), %rcx
movl %eax, (%rcx)
movl $0x1, (%r15)
movq %rbx, %rdi
callq 0x36db
cmpb $0x0, 0x21531(%rip)     # 0x24fa0
je 0x3ae3
movq %rbx, %rdi
callq 0x2110
movq %rax, %r15
leaq 0x2151d(%rip), %rdi     # 0x24fa0
leaq 0xc(%rsp), %rsi
xorl %edx, %edx
callq 0x3e17
cmpb $0x0, (%rax)
jne 0x3d6c
addq %r15, %rbx
movl 0xc(%r14), %eax
cmpl $0xff0000, %eax         # imm = 0xFF0000
je 0x3ac8
cmpl $0x0, 0xc(%r12,%r13)
je 0x3ac8
movl 0xc(%rsp), %r15d
cmpl %r15d, %eax
je 0x3ad0
cmpl $0x0, 0x14(%r14)
je 0x3dba
movl %r15d, %eax
jmp 0x3ad0
movl 0xc(%rsp), %eax
movl %eax, 0xc(%r14)
movl %eax, (%rbp)
cmpl $0x0, 0xc(%r12,%r13)
jle 0x3b1a
movl %eax, 0x10c43(%rip)     # 0x14724
jmp 0x3b1a
cmpl $0x0, 0xc(%r12,%r13)
jle 0x3d7d
movl 0x10c2f(%rip), %eax     # 0x14724
incl %eax
movl %eax, 0x10c27(%rip)     # 0x14724
movl %eax, (%rbp)
movl 0xc(%r14), %ecx
cmpl $0xff0000, %ecx         # imm = 0xFF0000
jne 0x3b12
movl %eax, 0xc(%r14)
jmp 0x3b1a
cmpl %eax, %ecx
jne 0x3d94
movq 0x21eef(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x3d3f
movq (%r14), %rdx
movl 0xc(%r14), %ecx
leaq 0x8893(%rip), %rsi      # 0xc3cb
xorl %eax, %eax
callq 0x2210
jmp 0x3d3f
addq $0x4, %rbx
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x352f
movq %rax, %rbx
leaq 0x81401(%rip), %rdi     # 0x84f60
callq 0x35bd
testl %eax, %eax
js 0x3c21
movl %eax, %r14d
leaq (%r14,%r14,2), %rax
shlq $0x4, %rax
leaq 0x21ec2(%rip), %rcx     # 0x25a40
movq 0x28(%rax,%rcx), %r15
jmp 0x3c9f
movslq 0x21ea5(%rip), %r12     # 0x25a34
leaq 0x813ca(%rip), %rdi     # 0x84f60
movq 0x10(%rsp), %rsi
callq 0x2878
leaq (%r12,%r12,2), %r14
shlq $0x4, %r14
leaq 0x21e91(%rip), %r13     # 0x25a40
movq %rax, (%r14,%r13)
movb %r15b, 0x8(%r14,%r13)
movl 0x10b76(%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 0x21e2d(%rip)           # 0x25a34
movl 0x10b27(%rip), %ecx     # 0x14734
jmp 0x3d12
cmpl %ecx, %edx
jne 0x3cf6
movl %edx, %ecx
jmp 0x3d12
movslq 0x21e0c(%rip), %r14     # 0x25a34
leaq 0x81331(%rip), %rdi     # 0x84f60
movq 0x10(%rsp), %rsi
callq 0x2878
leaq (%r14,%r14,2), %r12
shlq $0x4, %r12
leaq 0x21df8(%rip), %r13     # 0x25a40
movq %rax, (%r12,%r13)
movb $0x42, 0x8(%r12,%r13)
movl $0xff0000, 0xc(%r12,%r13) # imm = 0xFF0000
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
movq %r15, 0x20(%r12,%r13)
movl $0x4, %edi
movl $0x1, %esi
callq 0x21e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2834
movq %r15, 0x28(%r12,%r13)
incl 0x21d95(%rip)           # 0x25a34
movl 0x210b3(%rip), %eax     # 0x24d58
leaq (%r14,%r14,2), %rcx
shlq $0x4, %rcx
leaq 0x21d8c(%rip), %rdx     # 0x25a40
leaq (%rdx,%rcx), %r14
movq 0x20(%rcx,%rdx), %rsi
movl %eax, (%rsi)
movl $0x2, (%r15)
movl 0xc(%rcx,%rdx), %eax
movl 0x21d60(%rip), %ecx     # 0x25a30
cmpl $0xff0000, %eax         # imm = 0xFF0000
jl 0x3cdf
movl %ecx, 0xc(%r14)
movl %ecx, %eax
jmp 0x3ce7
cmpl %ecx, %eax
jne 0x3ddf
movl %eax, (%rbp)
movl 0xc(%rsp), %eax
addl %eax, 0x21d3c(%rip)     # 0x25a30
jmp 0x3d3f
cmpl $0x0, 0x14(%rax)
jne 0x3d12
cmpl $0x0, 0x21d15(%rip)     # 0x25a18
je 0x3e0e
movl %ecx, 0xc(%rax)
incl 0x8134e(%rip)           # 0x85060
movl %ecx, (%rbp)
movl 0x2103d(%rip), %eax     # 0x24d58
movslq %r12d, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
leaq 0x21d13(%rip), %rdx     # 0x25a40
movq 0x20(%rcx,%rdx), %rsi
movl %eax, (%rsi)
movq 0x28(%rcx,%rdx), %rax
movl $0x3, (%rax)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incl 0x21cd5(%rip)           # 0x25a2c
leaq 0x85bf(%rip), %rdi      # 0xc31d
movl $0x1f40, %esi           # imm = 0x1F40
xorl %eax, %eax
callq 0x2e72
jmp 0x3db0
movq %rax, %rdi
callq 0x2dde
leaq 0x85ba(%rip), %rdi      # 0xc335
jmp 0x3da9
incl 0x21ca9(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dde
leaq 0x8628(%rip), %rdi      # 0xc3ba
jmp 0x3da9
incl 0x21c92(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dde
leaq 0x85ff(%rip), %rdi      # 0xc3a8
xorl %eax, %eax
callq 0x2e72
movl $0x1, %edi
callq 0x22f0
incl 0x21c6c(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dde
movl 0xc(%r14), %edx
leaq 0x8585(%rip), %rdi      # 0xc358
leaq 0x81186(%rip), %rsi     # 0x84f60
movl %r15d, %ecx
jmp 0x3e05
incl 0x21c47(%rip)           # 0x25a2c
movq %rbx, %rdi
callq 0x2dde
movl 0xc(%r14), %edx
movl 0x21c39(%rip), %ecx     # 0x25a30
leaq 0x85e2(%rip), %rdi      # 0xc3e0
leaq 0x8115b(%rip), %rsi     # 0x84f60
xorl %eax, %eax
callq 0x2e72
jmp 0x3db0
leaq 0x861e(%rip), %rdi      # 0xc433
jmp 0x3dfe