// File: /workspace/github_c_makefile_star2_O1/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 0x5d8c
cmpl $0x2000, 0x171e3(%rip)  # imm = 0x2000
jne 0x5dbb
leaq 0x7210(%rip), %rsi      # 0xcf92
movl $0xb, %edi
jmp 0x60b9
movl %ebp, %eax
notl %eax
testb $0x6, %al
jne 0x5e52
movl 0x171c2(%rip), %edx     # 0x1cf60
cmpl $0x200, %edx            # imm = 0x200
jne 0x5eba
leaq 0x71e1(%rip), %rsi      # 0xcf92
movl $0xc, %edi
jmp 0x60b9
testb $0x8, %bpl
je 0x5ded
xorl %eax, %eax
callq 0x44f7
movq 0x179a1(%rip), %rdi     # 0x1d770
movq %rdi, 0x179a2(%rip)     # 0x1d778
movq $0x0, 0x1715f(%rip) # 0x1cf40
movl $0xc000, %edx           # imm = 0xC000
xorl %esi, %esi
callq 0x30d0
movb 0x38(%rbx), %cl
xorl %eax, %eax
testb %cl, %cl
je 0x5e0f
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 0x5df8
movzbl %al, %eax
leaq 0x1715a(%rip), %rdx     # 0x1cf70
movq (%rdx,%rax,8), %rcx
testq %rcx, %rcx
je 0x5e88
leaq 0x38(%rbx), %r12
movl (%rbx), %ebp
movq %rcx, %r14
cmpl %ebp, (%rcx)
jne 0x5e40
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x5f29
movq 0x28(%r14), %rcx
testq %rcx, %rcx
jne 0x5e25
addq $0x28, %r14
movq %r14, %rax
jmp 0x5e8c
testb $0x2, %bpl
jne 0x5f0a
cmpl $0x4, %ebp
jne 0x60be
movl 0x170f9(%rip), %ebp     # 0x1cf64
cmpl $0x400, %ebp            # imm = 0x400
jne 0x5feb
leaq 0x7114(%rip), %rsi      # 0xcf92
movl $0xe, %edi
jmp 0x60b9
leaq (%rdx,%rax,8), %rax
movq 0x178f5(%rip), %r14     # 0x1d788
movq %r14, (%rax)
leaq 0x60(%r14), %rax
movq %rax, 0x178e7(%rip)     # 0x1d788
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl 0x170a7(%rip)           # 0x1cf5c
jmp 0x60c1
movq 0x17087(%rip), %rax     # 0x1cf48
testq %rax, %rax
je 0x5f96
movl %edx, 0x4(%rsp)
movl (%rbx), %r13d
leaq 0x38(%rbx), %r12
movq %rax, %r14
cmpl %r13d, (%rax)
jne 0x5ef1
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x603d
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x5ed5
addq $0x28, %r14
movq %r14, %rax
movl 0x4(%rsp), %edx
jmp 0x5f9d
movl 0x17048(%rip), %edx     # 0x1cf58
cmpl $0x200, %edx            # imm = 0x200
jne 0x5f46
leaq 0x7073(%rip), %rsi      # 0xcf92
movl $0xd, %edi
jmp 0x60b9
movl 0x4(%r14), %eax
testb $0x40, %al
jne 0x5fd5
testb $0x10, %al
je 0x60ad
orb $0x10, 0x4(%rbx)
jmp 0x60c1
movq 0x16ff3(%rip), %rax     # 0x1cf40
testq %rax, %rax
je 0x6069
movl %edx, 0x4(%rsp)
movl (%rbx), %r13d
leaq 0x38(%rbx), %r12
movq %rax, %r14
cmpl %r13d, (%rax)
jne 0x5f7d
leaq 0x38(%r14), %rsi
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
je 0x60a5
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x5f61
addq $0x28, %r14
movq %r14, %rax
movl 0x4(%rsp), %edx
jmp 0x6070
leaq 0x16fab(%rip), %rax     # 0x1cf48
movq 0x177f4(%rip), %r14     # 0x1d798
movq %r14, (%rax)
movq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x177de(%rip)     # 0x1d798
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %edx
movl %edx, 0x16f90(%rip)     # 0x1cf60
jmp 0x60c1
orb $0x40, 0x4(%rbx)
leaq 0xe628(%rip), %rax      # 0x14608
movl 0x4(%rax), %eax
movl %eax, 0x14(%rbx)
jmp 0x60c1
movq 0x16f5e(%rip), %r14     # 0x1cf50
testq %r14, %r14
je 0x60fa
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x6103
movl (%rbx), %r13d
leaq 0x38(%rbx), %r15
movq %rax, %r12
cmpl %r13d, (%r14)
jne 0x602b
leaq 0x38(%r14), %rsi
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
je 0x60b1
movq 0x28(%r12), %rax
movq %r12, %r14
testq %rax, %rax
jne 0x600f
jmp 0x6106
movl 0x4(%r14), %eax
testb $0x10, %al
jne 0x60f2
testb %al, %al
js 0x60d3
movl 0x10(%rbx), %eax
movl %eax, 0x10(%r14)
movl 0x14(%rbx), %eax
movl %eax, 0x14(%r14)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r14)
jmp 0x60c1
leaq 0x16ed0(%rip), %rax     # 0x1cf40
movq 0x17701(%rip), %r14     # 0x1d778
movq %r14, (%rax)
movq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x176eb(%rip)     # 0x1d778
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %edx
movl %edx, 0x16eb5(%rip)     # 0x1cf58
jmp 0x60c1
movl 0x4(%r14), %eax
testb $0x10, %al
jne 0x60f2
testb %al, %al
js 0x60d3
movl $0x7, %edi
movq %r14, %rsi
callq 0xa2c8
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 0x60c1
orl $0x10, %ebp
movl %ebp, 0x4(%rbx)
jmp 0x60c1
leaq 0x16e4f(%rip), %r12     # 0x1cf50
jmp 0x610a
movq %r14, %r12
addq $0x28, %r12
movq 0x17697(%rip), %r14     # 0x1d7a8
movq %r14, (%r12)
movq $0x0, 0x28(%rbx)
leaq 0x60(%r14), %rax
movq %rax, 0x17680(%rip)     # 0x1d7a8
movl $0xc, %ecx
movq %r14, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl %ebp
movl %ebp, 0x16e26(%rip)     # 0x1cf64
jmp 0x60c1