// File: /workspace/github_c_makefile_star2_O3/42Bastian[P]lyxass/label.c
// Function: DefineLabel
label_t * DefineLabel(label_t *l, int *solved)
{
  int hashvalue;
  label_t *ptr;
  label_t *_this;
//->  extern int mac_mode;

  *solved = 0;

  //mesg("DefineLabel");

//->   printf("Type:%02x\n",l->type);

  if ( (l->type & GLOBAL) || (l->type & NORMAL) ){

    if ( n_global == MAX_GLOBAL_LABELS ){
      Error(TOOMANY_ERR,"");
      return NULL;
    }

    // clear local-labels
    if ( (l->type & GLOBAL) /*|| mac_mode*/ ){

      CheckForUnsolvedLocals();
      next_local = local_labels;
      local_hook = NULL;
      memset((char *)local_labels, 0, sizeof(label_t)*MAX_LOCAL_LABELS);

    }
    // get hash-value

    hashvalue = ComputeHash(l->name);

    //printf("Hashvalue: %d %p ",hashvalue,hash[hashvalue]);

    // check first hash-tab.
    if ( (ptr = hash[hashvalue]) == NULL ){
      hash[hashvalue] = next_global;
    }

    // go through all lables, check if _this one is already used
    // if not, go until the end is reached.

    else {
      do{
        if ( ptr->len == l->len && ! strcmp(l->name, ptr->name) ){
          if ( (ptr->type & REGISTER) ){
            l->type |= REGISTER;
            l->file = Current.File;
            return ptr;
          } else
            if ( (ptr->type & VARIABLE) ){
            //printf("LABEL:(%s)%d\n",next->name,next->value);
            l->type |= VARIABLE;
            return ptr;
          } else if ( ptr->type & UNSOLVED ){
            l->next = ptr->next;
            *ptr = *l;
            *solved = 1;
            return ptr;
          } else {
            Error(REDEF_ERR,(char *) ptr);
            return NULL;
          }
        }
        if ( ! ptr->next ) break;
        ptr = ptr->next;
      } while ( 1 );
      ptr->next = next_global;
    }

    // copy temp. data into buffer
    _this = next_global;

    *next_global++ = *l;
    ++n_global;

    //    printf("next_global:%p\n",next_global);
    return _this;
  } // global or normal label

  /* define a macro-label : .\xxx */

  if ( (l->type & (MACRO|LOCAL)) == (MACRO|LOCAL) ){

    //mesg("macro-label");

    if ( n_localmacro == MAX_MACRO_LABELS ){
      Error(LMTOOMANY_ERR,"");
      return NULL;
    }

    if ( (ptr = macro_hook) == NULL){
      macro_hook = next_macro;
    } else {
      do{
        if ( (ptr->len == l->len) &&
             ! strcmp(l->name, ptr->name) ){
          if ( ptr->type & VARIABLE ){
            //      printf("LABEL:(%s)%d\n",ptr->name,ptr->value);
            l->type |= VARIABLE;
            return ptr;
          } else if ( ptr->type & UNSOLVED ){
            l->next = ptr->next;
            *ptr = *l;
            *solved = 1;
            return ptr;
          } else {
            // re-definition allowed
            ptr->line = l->line;
            ptr->file = l->file;
            ptr->value = l->value;
            return ptr;
            //      Error(REDEF_ERR,(char *) ptr);
            //      return NULL;
          }

        }
        if ( !ptr->next ) break;
        ptr = ptr->next;
      } while ( 1 );
      ptr->next = next_macro;
    }

    l->next = (label_t *)NULL;
    _this = next_macro;
    *next_macro++ = *l;
    ++n_localmacro;
    //    printf("macro %s %02x",_this->name,_this->type);
    return _this;
  }

  /* define a local label : .xxx */

  if ( (l->type & LOCAL) == LOCAL  ){

    //mesg("local label");

    if ( n_local == MAX_LOCAL_LABELS ){
      Error(LTOOMANY_ERR,"");
      return NULL;
    }

    if ( (ptr = local_hook) == NULL){
      local_hook = next_local;
    } else {
      do{
        if ( (ptr->len == l->len) &&
             ! strcmp(l->name, ptr->name) ){
          if ( ptr->type & VARIABLE ){
            //printf("LABEL:(%s)%d\n",ptr->name,ptr->value);
            l->type |= VARIABLE;
            return ptr;
          } else if ( ptr->type & UNSOLVED ){
            l->next = ptr->next;
            *ptr = *l;
            *solved = 1;
            return ptr;
          } else {
            Error(REDEF_ERR,(char *) ptr);
            return NULL;
          }
        }
        if ( !ptr->next ) break;
        ptr = ptr->next;
      } while ( 1 );

      ptr->next = next_local;
    }

    l->next = (label_t *)NULL;
    _this = next_local;
    *next_local++ = *l;
    ++n_local;

    //    printf("%s (%d,%d)-%s-\n ",l->name,l->line,l->value,srcLine);
    return _this;
  }

  /* define a macro-name */

  if ( l->type == MACRO  ){

    // mesg("macro - name");

    if ( n_macro == MAX_MACROS ){
      Error(MTOOMANY_ERR,"");
      return NULL;
    }

    if ( (ptr = macroname_hook) == NULL){
      macroname_hook = next_macroname;
    } else {

      for ( ; ptr->next ; ptr = ptr->next ){
        if ( (ptr->len == l->len) &&
             ! strcmp(l->name, ptr->name) ){
          Error(REDEF_ERR,(char *) ptr);
          return NULL;
        }
      }
      ptr->next = next_macroname;
    }

    l->next = (label_t *)NULL;
    _this = next_macroname;
    *next_macroname++ = *l;
    ++n_macro;

    return _this;
  }
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movl $0x0, (%rsi)
movl 0x4(%rdi), %ebp
testb $0x9, %bpl
je 0x5d36
cmpl $0x2000, 0x17239(%rip)  # imm = 0x2000
jne 0x5d65
leaq 0x7266(%rip), %rsi      # 0xcf92
movl $0xb, %edi
jmp 0x6055
movl %ebp, %eax
notl %eax
testb $0x6, %al
jne 0x5dfc
movl 0x17218(%rip), %edx     # 0x1cf60
cmpl $0x200, %edx            # imm = 0x200
jne 0x5e64
leaq 0x7237(%rip), %rsi      # 0xcf92
movl $0xc, %edi
jmp 0x6055
testb $0x8, %bpl
je 0x5d97
xorl %eax, %eax
callq 0x44e7
movq 0x179f7(%rip), %rdi     # 0x1d770
movq %rdi, 0x179f8(%rip)     # 0x1d778
movq $0x0, 0x171b5(%rip) # 0x1cf40
movl $0xc000, %edx           # imm = 0xC000
xorl %esi, %esi
callq 0x30d0
movb 0x38(%rbx), %cl
xorl %eax, %eax
testb %cl, %cl
je 0x5db9
xorl %edx, %edx
movl %edx, %esi
xorl %eax, %esi
movzbl %cl, %eax
addl %esi, %eax
movb 0x39(%rbx,%rdx), %cl
incq %rdx
testb %cl, %cl
jne 0x5da2
movzbl %al, %eax
leaq 0x171b0(%rip), %rdx     # 0x1cf70
movq (%rdx,%rax,8), %rcx
testq %rcx, %rcx
je 0x5e32
leaq 0x38(%rbx), %r12
movl (%rbx), %ebp
movq %rcx, %r14
cmpl %ebp, (%rcx)
jne 0x5dea
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x5ed3
movq 0x28(%r14), %rcx
testq %rcx, %rcx
jne 0x5dcf
addq $0x28, %r14
movq %r14, %rax
jmp 0x5e36
testb $0x2, %bpl
jne 0x5eb4
cmpl $0x4, %ebp
jne 0x605a
movl 0x1714f(%rip), %ebp     # 0x1cf64
cmpl $0x400, %ebp            # imm = 0x400
jne 0x5f95
leaq 0x716a(%rip), %rsi      # 0xcf92
movl $0xe, %edi
jmp 0x6055
leaq (%rdx,%rax,8), %rax
movq 0x1794b(%rip), %r14     # 0x1d788
movq %r14, (%rax)
leaq 0x60(%r14), %rax
movq %rax, 0x1793d(%rip)     # 0x1d788
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl 0x170fd(%rip)           # 0x1cf5c
jmp 0x605d
movq 0x170dd(%rip), %rax     # 0x1cf48
testq %rax, %rax
je 0x5f40
movl %edx, 0x4(%rsp)
movl (%rbx), %r13d
leaq 0x38(%rbx), %r12
movq %rax, %r14
cmpl %r13d, (%rax)
jne 0x5e9b
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x5fe3
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x5e7f
addq $0x28, %r14
movq %r14, %rax
movl 0x4(%rsp), %edx
jmp 0x5f47
movl 0x1709e(%rip), %edx     # 0x1cf58
cmpl $0x200, %edx            # imm = 0x200
jne 0x5ef0
leaq 0x70c9(%rip), %rsi      # 0xcf92
movl $0xd, %edi
jmp 0x6055
movl 0x4(%r14), %eax
testb $0x40, %al
jne 0x5f7f
testb $0x10, %al
je 0x6049
orb $0x10, 0x4(%rbx)
jmp 0x605d
movq 0x17049(%rip), %rax     # 0x1cf40
testq %rax, %rax
je 0x6005
movl %edx, 0x4(%rsp)
movl (%rbx), %r13d
leaq 0x38(%rbx), %r12
movq %rax, %r14
cmpl %r13d, (%rax)
jne 0x5f27
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x6041
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x5f0b
addq $0x28, %r14
movq %r14, %rax
movl 0x4(%rsp), %edx
jmp 0x600c
leaq 0x17001(%rip), %rax     # 0x1cf48
movq 0x1784a(%rip), %r14     # 0x1d798
movq %r14, (%rax)
movq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x17834(%rip)     # 0x1d798
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %edx
movl %edx, 0x16fe6(%rip)     # 0x1cf60
jmp 0x605d
orb $0x40, 0x4(%rbx)
leaq 0xe67e(%rip), %rax      # 0x14608
movl 0x4(%rax), %eax
movl %eax, 0x14(%rbx)
jmp 0x605d
movq 0x16fb4(%rip), %r14     # 0x1cf50
testq %r14, %r14
je 0x6096
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x609f
movl (%rbx), %r13d
leaq 0x38(%rbx), %r15
movq %rax, %r12
cmpl %r13d, (%r14)
jne 0x5fd1
leaq 0x38(%r14), %rsi
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
je 0x604d
movq 0x28(%r12), %rax
movq %r12, %r14
testq %rax, %rax
jne 0x5fb9
jmp 0x60a2
movl 0x4(%r14), %eax
testb $0x10, %al
jne 0x608e
testb %al, %al
js 0x606f
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r14)
jmp 0x605d
leaq 0x16f34(%rip), %rax     # 0x1cf40
movq 0x17765(%rip), %r14     # 0x1d778
movq %r14, (%rax)
movq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x1774f(%rip)     # 0x1d778
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %edx
movl %edx, 0x16f19(%rip)     # 0x1cf58
jmp 0x605d
movl 0x4(%r14), %eax
testb $0x10, %al
jne 0x608e
testb %al, %al
js 0x606f
movl $0x7, %edi
movq %r14, %rsi
callq 0xa1d8
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%r14), %rax
movq %rax, 0x28(%rbx)
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x1, (%r15)
jmp 0x605d
orl $0x10, %ebp
movl %ebp, 0x4(%rbx)
jmp 0x605d
leaq 0x16eb3(%rip), %r12     # 0x1cf50
jmp 0x60a6
movq %r14, %r12
addq $0x28, %r12
movq 0x176fb(%rip), %r14     # 0x1d7a8
movq %r14, (%r12)
movq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x176e4(%rip)     # 0x1d7a8
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %ebp
movl %ebp, 0x16e8a(%rip)     # 0x1cf64
jmp 0x605d