// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, (%rsi)
movq %rdi, %rbx
movl 0x4(%rdi), %ebp
testb $0x9, %bpl
je 0x5634
cmpl $0x2000, 0x17938(%rip)  # imm = 0x2000
jne 0x5660
leaq 0x6965(%rip), %rsi      # 0xbf92
pushq $0xb
jmp 0x57a3
movl %ebp, %eax
notl %eax
testb $0x6, %al
jne 0x56dc
movl 0x1791a(%rip), %edx     # 0x1cf60
cmpl $0x200, %edx            # imm = 0x200
jne 0x573c
leaq 0x6939(%rip), %rsi      # 0xbf92
pushq $0xc
jmp 0x57a3
testb $0x8, %bpl
je 0x568f
xorl %eax, %eax
callq 0x440d
movq 0x180fc(%rip), %rdi     # 0x1d770
movq %rdi, 0x180fd(%rip)     # 0x1d778
andq $0x0, 0x178bd(%rip)     # 0x1cf40
movl $0xc000, %edx           # imm = 0xC000
xorl %esi, %esi
callq 0x30d0
leaq 0x38(%rbx), %r12
movq %r12, %rdi
callq 0x55b9
movl %eax, %ecx
leaq 0x178cc(%rip), %rdx     # 0x1cf70
movq (%rdx,%rcx,8), %rax
testq %rax, %rax
je 0x570f
movl (%rbx), %ebp
movq %rax, %r14
cmpl %ebp, (%rax)
jne 0x56ca
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x57be
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x56af
addq $0x28, %r14
movq %r14, %rax
jmp 0x5713
testb $0x2, %bpl
jne 0x578c
cmpl $0x4, %ebp
jne 0x57a9
movl 0x1786f(%rip), %ebp     # 0x1cf64
cmpl $0x400, %ebp            # imm = 0x400
jne 0x5878
leaq 0x688a(%rip), %rsi      # 0xbf92
pushq $0xe
jmp 0x57a3
leaq (%rdx,%rcx,8), %rax
movq 0x1806e(%rip), %r14     # 0x1d788
movq %r14, (%rax)
leaq 0x60(%r14), %rax
movq %rax, 0x18060(%rip)     # 0x1d788
pushq $0xc
popq %rcx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl 0x17822(%rip)           # 0x1cf5c
jmp 0x57ac
movq 0x17805(%rip), %rax     # 0x1cf48
testq %rax, %rax
je 0x5828
movl %edx, 0x4(%rsp)
movl (%rbx), %r13d
leaq 0x38(%rbx), %r12
movq %rax, %r14
cmpl %r13d, (%rax)
jne 0x5773
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x58bf
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x5757
addq $0x28, %r14
movq %r14, %rax
movl 0x4(%rsp), %edx
jmp 0x582f
movl 0x177c6(%rip), %edx     # 0x1cf58
cmpl $0x200, %edx            # imm = 0x200
jne 0x57d8
leaq 0x67f1(%rip), %rsi      # 0xbf92
pushq $0xd
popq %rdi
callq 0x96cc
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%r14), %eax
testb $0x40, %al
jne 0x5862
testb $0x10, %al
je 0x5926
orb $0x10, 0x4(%rbx)
jmp 0x57ac
movq 0x17761(%rip), %rax     # 0x1cf40
testq %rax, %rax
je 0x58e4
movl %edx, 0x4(%rsp)
movl (%rbx), %r13d
leaq 0x38(%rbx), %r12
movq %rax, %r14
cmpl %r13d, (%rax)
jne 0x580f
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x591e
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x57f3
addq $0x28, %r14
movq %r14, %rax
movl 0x4(%rsp), %edx
jmp 0x58eb
leaq 0x17719(%rip), %rax     # 0x1cf48
movq 0x17f62(%rip), %r14     # 0x1d798
movq %r14, (%rax)
andq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x17f4f(%rip)     # 0x1d798
pushq $0xc
popq %rcx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %edx
movl %edx, 0x17703(%rip)     # 0x1cf60
jmp 0x57ac
orb $0x40, 0x4(%rbx)
leaq 0xed9b(%rip), %rax      # 0x14608
movl 0x4(%rax), %eax
movl %eax, 0x14(%rbx)
jmp 0x57ac
movq 0x176d1(%rip), %r12     # 0x1cf50
testq %r12, %r12
je 0x5966
leaq 0x38(%rbx), %r14
movq %r12, %r15
movq 0x28(%r12), %r12
testq %r12, %r12
je 0x5960
movl (%r15), %eax
cmpl (%rbx), %eax
jne 0x588c
leaq 0x38(%r15), %rsi
movq %r14, %rdi
callq 0x3110
testl %eax, %eax
jne 0x588c
pushq $0x7
popq %rdi
movq %r15, %rsi
jmp 0x57a4
movl 0x4(%r14), %eax
testb $0x10, %al
jne 0x5955
testb %al, %al
js 0x5935
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r14)
jmp 0x57ac
leaq 0x17655(%rip), %rax     # 0x1cf40
movq 0x17e86(%rip), %r14     # 0x1d778
movq %r14, (%rax)
andq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x17e73(%rip)     # 0x1d778
pushq $0xc
popq %rcx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %edx
movl %edx, 0x1763f(%rip)     # 0x1cf58
jmp 0x57ac
movl 0x4(%r14), %eax
testb $0x10, %al
jne 0x5955
testb %al, %al
js 0x5935
pushq $0x7
popq %rdi
movq %r14, %rsi
jmp 0x57a4
movq 0x28(%r14), %rax
movq %rax, 0x28(%rbx)
pushq $0xc
popq %rcx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x1, (%r15)
jmp 0x57ac
orl $0x10, %ebp
movl %ebp, 0x4(%rbx)
jmp 0x57ac
addq $0x28, %r15
jmp 0x596d
leaq 0x175e3(%rip), %r15     # 0x1cf50
movq 0x17e34(%rip), %r14     # 0x1d7a8
movq %r14, (%r15)
andq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x17e21(%rip)     # 0x1d7a8
pushq $0xc
popq %rcx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %ebp
movl %ebp, 0x175c9(%rip)     # 0x1cf64
jmp 0x57ac