// File: /workspace/github_cpp_makefile_star2_O2/nompelis[P]INTecplotLib/intecplot.cpp
// Function: inTec_Zone::HandleKeyword_Varlocation(char const*)
int inTec_Zone::HandleKeyword_Varlocation( const char *string )
{
#ifdef _DEBUG_
   printf(" i Inside HandleKeyword_Varlocation() \n");
#endif
   int iret=1;

   // make a duplicate buffer
   size_t is=0;
   while( string[is] != '\0' && string[is] != '\n' ) ++is;
   char *buf = (char *) malloc(is*sizeof(char));
   if( buf == NULL ) return(-1);

   // shift passed spaces
   memcpy( buf, string, is );
   char *s = buf;
   while( s[0] == ' ' ) s++;
   is = 0;
   while( s[is] != '\0' && s[is] != '\n' ) ++is;

   // the case where all variables are nodal, which is the default
   if( iret != 0 && is >= 5 ) {
      if( strncasecmp( s, "(NODAL)", 7 ) == 0 ) {
         // perhaps do something to mark all variables as nodal
         std::map< int, int > :: iterator it;
         for( it= ivar_loc.begin(); it != ivar_loc.end(); ++it ) {
            (*it).second = 1;
         }
         iret = 0;
#ifdef _DEBUG_
         printf(" --- All variables are nodal \n");
#endif
      }
   }

   // the case where variables are individualluy separated
   int ierror=0;
   if( iret != 0 && is >= 15 ) {    // the check may be different...

      std::map< int, int > itmp_loc;
      std::map< int, int > :: iterator it;
      for(int n=1;n<=num_var;++n) itmp_loc[n] = 1;

#ifdef _DEBUG_
     printf(" --- Parsing variable locations (nodal/cell-centered) \n");
     printf(" --- Number of variables in zone( from file): %d \n", num_var);
#endif
      char *s,*sr;
      char *token;
      const char *delim;

      // first pass is the parentheses and we assume it will not be seen again
#ifdef _DEBUG3_
     printf(" --- Searching for parentheses\n");
#endif
      delim = (const char *) "()";
      token = strtok_r( buf, delim, &sr );
      if( token == NULL ) {
         // this is in error
         printf(" e Malformed varlocation line!\n");
         ierror = 1;
      }
#ifdef _DEBUG3_
      printf(" --- Token in parentheses --->|%s|<---\n",token);
#endif

      int k;
      for( k=0,s=token; ierror==0 ; s=NULL,++k ) {
#ifdef _DEBUG3_
         printf("LOOP START: ");
         printf("S  -->|%s|<-- \n",s);
#endif
         // look for first delimiter (a group before an equal sign)
         delim = (const char *) "= ";
      // printf("DELIM1 -->|%s|<-- \n",delim);
         token = strtok_r( s, delim, &sr );
      // printf("TOKEN1 -->|%s|<-- \n",token);
         // this is the proper termination condition
         if( token == NULL ) break;

         // look for variable numbers
         char *s2=token,*sr2;
         token = strtok_r( s2, "[]", &sr2 );
      // printf("TOKEN+ -->|%s|<-- \n",token);
         int kk;
         for( kk=0,s2=token; ierror==0 ; s2=NULL,++kk ) {
            token = strtok_r( s2, ",", &sr2 );
            if( token == NULL ) break;
      //    printf("TOKEN++ -->|%s|<-- \n", token);

            // look for a minus sign and take action
            int isign=0;
            is=0;
            while( token[is] != '\0' ) {
               if( token[is] == '-' ) {
                  isign = 1;         // indicate this is a range group
                  token[is] = ' ';   // turn sign to a space for easy parsing
               }
               ++is;
            }
      //    printf("TOKEN++ changed as range/number -->|%s|<-- \n", token);

            // parse the number range or variable number
            if( isign == 1 ) {
               int i1=-1,i2=-1;
               is = sscanf( token, "%d %d", &i1,&i2);
#ifdef _DEBUG2_
               printf(" --- Variable range: %d - %d \n", i1,i2);
#endif
               if( is != 2 ) {
#ifdef _DEBUG2_
                  printf(" --- Failed parsing range! \n");
#endif
                  ierror = 2;
                  break;
               } else {
                  if( i1 > num_var || i2 > num_var ||
                      i1 == 0      || i2 == 0 ) {
#ifdef _DEBUG_
                     printf(" --- Variables out of range! \n");
#endif
                     ierror = 20;
                     break;
                  } else {
                     for(int n=1;n<=num_var;++n)
                        if( i1 <= n && n <= i2 ) itmp_loc[n] = 99;
                  }
               }
            } else {
               int i0=-1;
               is = sscanf( token, "%d", &i0);
#ifdef _DEBUG2_
               printf(" --- Variable: %d \n", i0);
#endif
               if( is != 1 ) {
#ifdef _DEBUG2_
                  printf(" --- Failed parsing variable slot! \n");
#endif
                  ierror = 2;
                  break;
               } else {
                  if( i0 > num_var || i0 == 0 ) {
#ifdef _DEBUG_
                     printf(" --- Variable out of range! \n");
#endif
                     ierror = 20;
                     break;
                  } else {
                     itmp_loc[i0] = 99;
                  }
               }
            }
#ifdef _DEBUG3_
            printf(" --- Inner cycling... n");
#endif
         }
         if( ierror != 0 ) break;

         // look for second delimiter (a group before a comma)
         s = NULL;
         delim = (const char *) ",";
      // printf("DELIM2 -->|%s|<-- \n",delim);
         token = strtok_r( s, delim, &sr );
         if( token == NULL ) {
            ierror = 2;
            break;
         }
      // printf("TOKEN2 -->|%s|<-- \n",token);

         // skip the first few spaces if any
         while( token[0] == ' ' || token[0] == '=' ) token++;
      // printf("CLEAN TOKEN2 -->|%s|<-- \n",token);

         // determine the "variable location" based on the second string
         int iloc=0;    // this would eventually result in an error...
         if( strlen( token ) >= 5 ) {
            if( strncasecmp( token, "NODAL", 5 ) == 0 ) iloc = 1;
         }
         if( strlen( token ) >= 12 ) {
            if( strncasecmp( token, "CELLCENTERED", 12 ) == 0 ) iloc = 2;
         }
         for( it= itmp_loc.begin(); it != itmp_loc.end(); ++it ) {
            if( (*it).second == 99 ) (*it).second = iloc;
         }

#ifdef _DEBUG3_
         printf(" --- Outer cycling... n");
#endif
      }

#ifdef _DEBUG3_
      printf(" --- Variable locations (temporary) \n");
#endif
      for( it= itmp_loc.begin(); it != itmp_loc.end(); ++it ) {
#ifdef _DEBUG3_
         printf("      ==> var. %d location %d \n", (*it).first, (*it).second );
#endif
         ivar_loc[ (*it).first ] = (*it).second;
      }

      // drop contents of this array (althought it would have been cleaned)
      itmp_loc.clear();

      // report on errors trapped
      if( ierror == 0 ) {
         iret = 0;
      } else {
         // provide some output
      }

#ifdef _DEBUG2_
      printf(" --- Variable locations \n");
      for( it= ivar_loc.begin(); it != ivar_loc.end(); ++it ) {
         printf("      ==> var. %d location %d \n", (*it).first, (*it).second );
      }
#endif
   }

#ifdef _DEBUG_
   if( iret != 0 ) printf(" --- Problematic VARLOCATION line \n");
#endif

   // drop the buffer
   free( buf );

#ifdef _DEBUG_
   printf(" i Exiting HandleKeyword_Varlocation() \n");
#endif
   return( iret );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x6c60(%rip), %rdi      # 0x101ab
callq 0x77b0
xorl %r12d, %r12d
movzbl (%r15,%r12), %eax
testl %eax, %eax
je 0x9566
cmpl $0xa, %eax
je 0x9566
incq %r12
jmp 0x9553
movq %r12, %rdi
callq 0x76e0
testq %rax, %rax
je 0x9620
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x73e0
movq %r13, %rdi
decq %rdi
movb 0x1(%rdi), %al
incq %rdi
cmpb $0x20, %al
je 0x958e
xorl %ebx, %ebx
testb %al, %al
je 0x95af
movzbl %al, %eax
cmpl $0xa, %eax
je 0x95af
movb 0x1(%rdi,%rbx), %al
incq %rbx
jmp 0x959a
cmpq $0x5, %rbx
jb 0x9949
leaq 0x5cf9(%rip), %rsi      # 0xf2b9
pushq $0x7
popq %rdx
callq 0x7650
testl %eax, %eax
je 0x9628
cmpq $0xf, %rbx
jb 0x9949
leaq 0x38(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
pushq $0x1
popq %rax
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r12
movl %eax, 0x10(%rsp)
cmpl 0x20(%r14), %eax
jg 0x9659
movq %r15, %rdi
movq %r12, %rsi
callq 0x72d0
movl $0x1, (%rax)
movl 0x10(%rsp), %eax
incl %eax
jmp 0x95fd
pushq $-0x1
popq %rbp
jmp 0x996c
movq 0x70(%r14), %rax
addq $0x60, %r14
cmpq %r14, %rax
je 0x9646
movl $0x1, 0x24(%rax)
movq %rax, %rdi
callq 0x72f0
jmp 0x9630
leaq 0x6b85(%rip), %rdi      # 0x101d2
callq 0x77b0
xorl %ebp, %ebp
jmp 0x9958
leaq 0x6b90(%rip), %rdi      # 0x101f0
callq 0x77b0
movl 0x20(%r14), %esi
leaq 0x605a(%rip), %rdi      # 0xf6ca
xorl %eax, %eax
callq 0x7060
leaq 0x607f(%rip), %rsi      # 0xf6fd
leaq 0x10(%rsp), %rdx
movq %r13, 0x28(%rsp)
movq %r13, %rdi
callq 0x7910
movq %rax, %rbp
testq %rax, %rax
setne %al
jne 0x96ab
leaq 0x6b85(%rip), %rdi      # 0x10227
movl %eax, %ebx
callq 0x77b0
movl %ebx, %eax
leaq 0x629c(%rip), %rsi      # 0xf94e
leaq 0x10(%rsp), %rbx
pushq $0x1
popq %r12
leaq 0x30(%rsp), %r15
leaq 0x1c(%rsp), %r13
movl %eax, 0x20(%rsp)
testb $0x1, %al
je 0x98f4
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x7910
testq %rax, %rax
je 0x98f4
movq %rax, %rdi
leaq 0x6011(%rip), %rsi      # 0xf700
leaq 0x60(%rsp), %rbp
movq %rbp, %rdx
callq 0x7910
movq %rax, %rdi
leaq 0x61d8(%rip), %rsi      # 0xf8de
movq %rbp, %rdx
callq 0x7910
testq %rax, %rax
je 0x9820
xorl %edx, %edx
movq %rax, %rcx
movzbl (%rcx), %esi
cmpl $0x2d, %esi
je 0x972a
testl %esi, %esi
jne 0x9730
jmp 0x9735
movb $0x20, (%rcx)
movl %r12d, %edx
incq %rcx
jmp 0x971c
cmpl $0x1, %edx
jne 0x97cc
orl $-0x1, 0x8(%rsp)
orl $-0x1, 0xc(%rsp)
movq %rax, %rdi
leaq 0x5fb1(%rip), %rsi      # 0xf703
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
xorl %eax, %eax
callq 0x74a0
cmpl $0x2, %eax
jne 0x98ec
movl 0x8(%rsp), %ecx
movl 0x20(%r14), %eax
cmpl %eax, %ecx
jg 0x98d7
movl 0xc(%rsp), %edx
testl %edx, %edx
je 0x98d7
testl %ecx, %ecx
je 0x98d7
cmpl %eax, %edx
jg 0x98d7
movl %r12d, %ecx
movl %ecx, 0x1c(%rsp)
cmpl %eax, %ecx
jg 0x9819
cmpl %ecx, 0x8(%rsp)
jg 0x97c8
cmpl 0xc(%rsp), %ecx
jg 0x97c8
movq %r15, %rdi
movq %r13, %rsi
callq 0x72d0
movl $0x63, (%rax)
movl 0x1c(%rsp), %ecx
movl 0x20(%r14), %eax
incl %ecx
jmp 0x979b
orl $-0x1, 0x8(%rsp)
movq %rax, %rdi
leaq 0x5f2b(%rip), %rsi      # 0xf706
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x74a0
cmpl $0x1, %eax
jne 0x98ec
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x98e0
cmpl 0x20(%r14), %eax
jg 0x98e0
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x72d0
movl $0x63, (%rax)
xorl %edi, %edi
jmp 0x96ff
xorl %edi, %edi
leaq 0x60b5(%rip), %rsi      # 0xf8de
movq %rbx, %rdx
callq 0x7910
testq %rax, %rax
je 0x98ec
movq %rax, %rbp
movzbl (%rbp), %eax
cmpl $0x3d, %eax
je 0x984b
cmpl $0x20, %eax
jne 0x9850
incq %rbp
jmp 0x983d
movq %rbp, %rdi
callq 0x71f0
xorl %ebx, %ebx
cmpq $0x5, %rax
jb 0x98a0
movq %rbp, %rdi
leaq 0x5e9f(%rip), %rsi      # 0xf709
pushq $0x5
popq %rdx
movq %rax, 0x20(%rsp)
callq 0x7650
xorl %ebx, %ebx
testl %eax, %eax
sete %bl
cmpq $0xc, 0x20(%rsp)
jb 0x98a0
movq %rbp, %rdi
leaq 0x5e7f(%rip), %rsi      # 0xf70f
pushq $0xc
popq %rdx
callq 0x7650
testl %eax, %eax
pushq $0x2
popq %rax
cmovel %eax, %ebx
movq 0x48(%rsp), %rax
leaq 0x38(%rsp), %rcx
cmpq %rcx, %rax
je 0x98c2
cmpl $0x63, 0x24(%rax)
jne 0x98b8
movl %ebx, 0x24(%rax)
movq %rax, %rdi
callq 0x72f0
jmp 0x98a5
movb $0x1, %al
xorl %ebp, %ebp
leaq 0x6081(%rip), %rsi      # 0xf94e
leaq 0x10(%rsp), %rbx
jmp 0x96c5
leaq 0x6985(%rip), %rdi      # 0x10263
jmp 0x98e7
leaq 0x695f(%rip), %rdi      # 0x10246
callq 0x77b0
movl $0x0, 0x20(%rsp)
movq 0x48(%rsp), %r15
addq $0x58, %r14
movq 0x28(%rsp), %r13
leaq 0x38(%rsp), %r12
cmpq %r12, %r15
je 0x992b
leaq 0x20(%r15), %rsi
movl 0x24(%r15), %ebx
movq %r14, %rdi
callq 0x72d0
movl %ebx, (%rax)
movq %r15, %rdi
callq 0x72f0
movq %rax, %r15
jmp 0x9907
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x70b0
movq %r14, %rdi
callq 0x7220
xorl %ebp, %ebp
testb $0x1, 0x20(%rsp)
jne 0x9958
pushq $0x1
popq %rbp
leaq 0x692e(%rip), %rdi      # 0x10281
callq 0x77b0
movq %r13, %rdi
callq 0x75c0
leaq 0x693d(%rip), %rdi      # 0x102a4
callq 0x77b0
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9983
jmp 0x9983
jmp 0x9983
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x7220
movq %rbx, %rdi
callq 0x78d0