// File: /workspace/github_c_makefile_star2_O2/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 %edx, 0xc(%rsp)
movl $0xff0000, (%rsi)       # imm = 0xFF0000
cmpl $0x1f3f, 0x21744(%rip)  # imm = 0x1F3F
jge 0x3712
movq %rsi, %rbp
leaq 0x80c60(%rip), %rbx     # 0x83f60
movq %rbx, %rsi
callq 0x27d6
xorl %r12d, %r12d
cmpb $0x3a, (%rax)
sete %r12b
addq %rax, %r12
movq %rbx, %rdi
callq 0x2110
movq %rax, %r15
movq %r12, %rdi
callq 0x2675
movq %rax, %rbx
movb (%rax), %r14b
xorl %r12d, %r12d
leaq 0x10408(%rip), %r13     # 0x13740
cmpq $0x40, %r12
je 0x338f
cmpb $0x3d, %r14b
je 0x335a
leaq (%r12,%r13), %rsi
movq %rbx, %rdi
callq 0x2994
testl %eax, %eax
je 0x335a
addq $0x10, %r12
jmp 0x3338
movq %rbp, 0x10(%rsp)
movslq 0x8(%r12,%r13), %rax
addq %rax, %rbx
leaq 0x80bf2(%rip), %rdi     # 0x83f60
callq 0x30cc
testl %eax, %eax
js 0x33ec
movl %eax, %r14d
imulq $0x30, %r14, %rax
leaq 0x216bb(%rip), %rcx     # 0x24a40
movq 0x28(%rax,%rcx), %rax
jmp 0x344d
leaq 0x7fd6(%rip), %rsi      # 0xb36c
movq %rbx, %rdi
callq 0x2994
testl %eax, %eax
je 0x3528
leaq 0x80bb3(%rip), %rdi     # 0x83f60
callq 0x30cc
testl %eax, %eax
js 0x359a
movl %eax, %r12d
movl %eax, %eax
imulq $0x30, %rax, %rcx
leaq 0x21676(%rip), %rdx     # 0x24a40
leaq (%rdx,%rcx), %rax
movl 0xc(%rcx,%rdx), %edx
movl 0x1035c(%rip), %ecx     # 0x13734
cmpl $0xff0000, %edx         # imm = 0xFF0000
jne 0x3602
movl %ecx, 0xc(%rax)
jmp 0x36d7
movslq 0x21641(%rip), %r14     # 0x24a34
leaq 0x80b66(%rip), %rdi     # 0x83f60
movl %r15d, %esi
callq 0x258f
imulq $0x30, %r14, %rbp
leaq 0x21633(%rip), %r15     # 0x24a40
movq %rax, (%rbp,%r15)
movl 0xc(%rsp), %eax
movb %al, 0x8(%rbp,%r15)
movl $0xff0000, 0xc(%rbp,%r15) # imm = 0xFF0000
movl $0x1, 0x14(%rbp,%r15)
pushq $0x4
popq %rdi
callq 0x2554
movq %rax, 0x20(%rbp,%r15)
pushq $0x4
popq %rdi
callq 0x2554
movq %rax, 0x28(%rbp,%r15)
incl 0x215e7(%rip)           # 0x24a34
movl 0x20905(%rip), %ecx     # 0x23d58
imulq $0x30, %r14, %rdx
leaq 0x215e2(%rip), %rsi     # 0x24a40
leaq (%rsi,%rdx), %rbp
movq 0x20(%rdx,%rsi), %rdx
movl %ecx, (%rdx)
movl $0x1, (%rax)
movq %rbx, %rdi
callq 0x31b3
cmpb $0x0, 0x20b22(%rip)     # 0x23fa0
je 0x34f7
movq %rbx, %rdi
callq 0x2110
movq %rax, %r15
leaq 0x20b0e(%rip), %rdi     # 0x23fa0
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x37d5
cmpb $0x0, (%rax)
jne 0x372d
addq %r15, %rbx
movl 0xc(%rbp), %eax
cmpl $0xff0000, %eax         # imm = 0xFF0000
je 0x34d5
cmpl $0x0, 0xc(%r12,%r13)
je 0x34d5
movl 0x8(%rsp), %r15d
cmpl %r15d, %eax
je 0x34dc
cmpl $0x0, 0x14(%rbp)
je 0x37a1
movl %r15d, %eax
jmp 0x34dc
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rbp)
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
cmpl $0x0, 0xc(%r12,%r13)
jle 0x3570
movl %eax, 0x1022f(%rip)     # 0x13724
jmp 0x3570
cmpl $0x0, 0xc(%r12,%r13)
jle 0x373e
movl 0x1021b(%rip), %eax     # 0x13724
incl %eax
movl %eax, 0x10213(%rip)     # 0x13724
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl 0xc(%rbp), %ecx
cmpl $0xff0000, %ecx         # imm = 0xFF0000
jne 0x3568
movl %eax, 0xc(%rbp)
jmp 0x3570
addq $0x4, %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rbx
leaq 0x80a1d(%rip), %rdi     # 0x83f60
callq 0x30cc
testl %eax, %eax
js 0x3611
movl %eax, %r14d
imulq $0x30, %r14, %rax
leaq 0x214e2(%rip), %rcx     # 0x24a40
movq 0x28(%rax,%rcx), %rax
jmp 0x3669
cmpl %eax, %ecx
jne 0x3755
movq 0x21499(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x3700
movq (%rbp), %rdx
movl 0xc(%rbp), %ecx
leaq 0x7dcd(%rip), %rsi      # 0xb35b
xorl %eax, %eax
callq 0x2210
jmp 0x3700
movslq 0x21493(%rip), %r12     # 0x24a34
leaq 0x809b8(%rip), %rdi     # 0x83f60
movl %r15d, %esi
callq 0x258f
imulq $0x30, %r12, %r14
leaq 0x21485(%rip), %r13     # 0x24a40
movq %rax, (%r14,%r13)
movl 0xc(%rsp), %eax
movb %al, 0x8(%r14,%r13)
movl 0x10166(%rip), %eax     # 0x13734
movl %eax, 0xc(%r14,%r13)
pushq $0x4
popq %r15
movq %r15, %rdi
callq 0x2554
movq %rax, 0x20(%r14,%r13)
movq %r15, %rdi
callq 0x2554
movq %rax, 0x28(%r14,%r13)
incl 0x2143d(%rip)           # 0x24a34
movl 0x10137(%rip), %ecx     # 0x13734
jmp 0x36d7
cmpl %ecx, %edx
jne 0x36bb
movl %edx, %ecx
jmp 0x36d7
movslq 0x2141c(%rip), %r14     # 0x24a34
leaq 0x80941(%rip), %rdi     # 0x83f60
movl %r15d, %esi
callq 0x258f
imulq $0x30, %r14, %r12
leaq 0x2140e(%rip), %r13     # 0x24a40
movq %rax, (%r12,%r13)
movb $0x42, 0x8(%r12,%r13)
movl $0xff0000, 0xc(%r12,%r13) # imm = 0xFF0000
pushq $0x4
popq %r15
movq %r15, %rdi
callq 0x2554
movq %rax, 0x20(%r12,%r13)
movq %r15, %rdi
callq 0x2554
movq %rax, 0x28(%r12,%r13)
incl 0x213cb(%rip)           # 0x24a34
movl 0x206e9(%rip), %ecx     # 0x23d58
imulq $0x30, %r14, %rdx
leaq 0x213c6(%rip), %rsi     # 0x24a40
leaq (%rsi,%rdx), %r14
movq 0x20(%rdx,%rsi), %rdi
movl %ecx, (%rdi)
movl $0x2, (%rax)
movl 0xc(%rdx,%rsi), %eax
movl 0x2139b(%rip), %ecx     # 0x24a30
cmpl $0xff0000, %eax         # imm = 0xFF0000
jl 0x36a4
movl %ecx, 0xc(%r14)
movl %ecx, %eax
jmp 0x36ac
cmpl %ecx, %eax
jne 0x3779
movl %eax, (%rbp)
movl 0x8(%rsp), %eax
addl %eax, 0x21377(%rip)     # 0x24a30
jmp 0x3700
cmpl $0x0, 0x14(%rax)
jne 0x36d7
cmpl $0x0, 0x21350(%rip)     # 0x24a18
je 0x37cc
movl %ecx, 0xc(%rax)
incl 0x80989(%rip)           # 0x84060
movl %ecx, (%rbp)
movl 0x20678(%rip), %eax     # 0x23d58
movslq %r12d, %rcx
imulq $0x30, %rcx, %rcx
leaq 0x21352(%rip), %rdx     # 0x24a40
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 0x21314(%rip)           # 0x24a2c
leaq 0x7b8e(%rip), %rdi      # 0xb2ad
movl $0x1f40, %esi           # imm = 0x1F40
xorl %eax, %eax
callq 0x2af7
jmp 0x3771
movq %rax, %rdi
callq 0x2a71
leaq 0x7b89(%rip), %rdi      # 0xb2c5
jmp 0x376a
incl 0x212e8(%rip)           # 0x24a2c
movq %rbx, %rdi
callq 0x2a71
leaq 0x7bf7(%rip), %rdi      # 0xb34a
jmp 0x376a
incl 0x212d1(%rip)           # 0x24a2c
movq %rbx, %rdi
callq 0x2a71
leaq 0x7bce(%rip), %rdi      # 0xb338
xorl %eax, %eax
callq 0x2af7
pushq $0x1
popq %rdi
callq 0x22f0
incl 0x212ad(%rip)           # 0x24a2c
movq %rbx, %rdi
callq 0x2a71
movl 0xc(%r14), %edx
movl 0x2129f(%rip), %ecx     # 0x24a30
leaq 0x7bd8(%rip), %rdi      # 0xb370
leaq 0x807c1(%rip), %rsi     # 0x83f60
jmp 0x37c3
incl 0x21285(%rip)           # 0x24a2c
movq %rbx, %rdi
callq 0x2a71
movl 0xc(%rbp), %edx
leaq 0x7b2f(%rip), %rdi      # 0xb2e8
leaq 0x807a0(%rip), %rsi     # 0x83f60
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2af7
jmp 0x3771
leaq 0x7bf0(%rip), %rdi      # 0xb3c3
jmp 0x3798