// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x7a60(%rip), %rdi      # 0xf1ab
callq 0x5570
xorl %r12d, %r12d
movzbl (%r15,%r12), %eax
testl %eax, %eax
je 0x7766
cmpl $0xa, %eax
je 0x7766
incq %r12
jmp 0x7753
movq %r12, %rdi
callq 0x54e0
testq %rax, %rax
je 0x7acf
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x52e0
movq %rbx, %rdi
decq %rdi
movb 0x1(%rdi), %al
incq %rdi
cmpb $0x20, %al
je 0x778e
xorl %r15d, %r15d
testb %al, %al
je 0x77b1
movzbl %al, %eax
cmpl $0xa, %eax
je 0x77b1
movb 0x1(%rdi,%r15), %al
incq %r15
jmp 0x779b
cmpq $0x5, %r15
jb 0x7b75
leaq 0x6af7(%rip), %rsi      # 0xe2b9
movl $0x7, %edx
callq 0x5490
testl %eax, %eax
je 0x7ad9
cmpq $0xf, %r15
jb 0x7b75
leaq 0x30(%rsp), %rbp
movl $0x0, (%rbp)
xorl %eax, %eax
movq %rax, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %rax, 0x20(%rbp)
movl $0x1, 0x8(%rsp)
cmpl $0x0, 0x20(%r14)
jle 0x7837
leaq 0x28(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x51f0
movl $0x1, (%rax)
movl 0x8(%rsp), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x8(%rsp)
cmpl 0x20(%r14), %eax
jl 0x7815
leaq 0x79b2(%rip), %rdi      # 0xf1f0
callq 0x5570
movl 0x20(%r14), %esi
leaq 0x6e7c(%rip), %rdi      # 0xe6ca
xorl %eax, %eax
callq 0x5060
leaq 0x6ea1(%rip), %rsi      # 0xe6fd
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x5680
testq %rax, %rax
je 0x7b07
leaq 0x70d5(%rip), %rsi      # 0xe94e
leaq 0x8(%rsp), %rdx
movq %rax, %rdi
callq 0x5680
movb $0x1, %r13b
testq %rax, %rax
je 0x7b28
leaq 0x6e67(%rip), %rsi      # 0xe700
leaq 0x20(%rsp), %r13
leaq 0x7039(%rip), %r12      # 0xe8de
leaq 0x28(%rsp), %r15
movq %rax, %rdi
movq %r13, %rdx
callq 0x5680
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5680
testq %rax, %rax
je 0x79f4
xorl %edx, %edx
movq %rax, %rcx
movzbl (%rcx), %esi
cmpl $0x2d, %esi
je 0x78df
testl %esi, %esi
jne 0x78e7
jmp 0x78ec
movb $0x20, (%rcx)
movl $0x1, %edx
incq %rcx
jmp 0x78d1
cmpl $0x1, %edx
jne 0x799d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x4(%rsp)
movl %ecx, 0x14(%rsp)
movq %rax, %rdi
leaq 0x6df7(%rip), %rsi      # 0xe703
leaq 0x4(%rsp), %rdx
leaq 0x14(%rsp), %rcx
xorl %eax, %eax
callq 0x5340
cmpl $0x2, %eax
jne 0x7b25
movl 0x4(%rsp), %ecx
movl 0x20(%r14), %eax
cmpl %eax, %ecx
jg 0x7b10
movl 0x14(%rsp), %edx
testl %edx, %edx
je 0x7b10
testl %ecx, %ecx
je 0x7b10
cmpl %eax, %edx
jg 0x7b10
movl $0x1, 0x10(%rsp)
testl %eax, %eax
jle 0x79ed
movl $0x1, %ecx
cmpl %ecx, 0x4(%rsp)
jg 0x798e
cmpl 0x14(%rsp), %ecx
jg 0x798e
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x51f0
movl $0x63, (%rax)
movl 0x10(%rsp), %ecx
movl 0x20(%r14), %eax
leal 0x1(%rcx), %edx
movl %edx, 0x10(%rsp)
cmpl %eax, %ecx
movl %edx, %ecx
jl 0x7967
jmp 0x79ed
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movq %rax, %rdi
leaq 0x6d57(%rip), %rsi      # 0xe706
leaq 0x4(%rsp), %rdx
xorl %eax, %eax
callq 0x5340
cmpl $0x1, %eax
jne 0x7b25
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x7b19
cmpl 0x20(%r14), %eax
jg 0x7b19
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
callq 0x51f0
movl $0x63, (%rax)
xorl %edi, %edi
jmp 0x78b8
xorl %edi, %edi
movq %r12, %rsi
leaq 0x8(%rsp), %rdx
callq 0x5680
testq %rax, %rax
je 0x7b25
movq %rax, %r12
movzbl (%r12), %eax
cmpl $0x3d, %eax
je 0x7a1e
cmpl $0x20, %eax
jne 0x7a23
incq %r12
jmp 0x7a0f
movq %r12, %rdi
callq 0x5150
xorl %r13d, %r13d
cmpq $0x5, %rax
jb 0x7a7d
movl $0x5, %edx
movq %r12, %rdi
leaq 0x6cc6(%rip), %rsi      # 0xe709
movq %rax, 0x18(%rsp)
callq 0x5490
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
cmpq $0xc, 0x18(%rsp)
jb 0x7a7d
movl $0xc, %edx
movq %r12, %rdi
leaq 0x6ca2(%rip), %rsi      # 0xe70f
callq 0x5490
testl %eax, %eax
movl $0x2, %eax
cmovel %eax, %r13d
movq 0x40(%rsp), %rax
cmpq %rbp, %rax
je 0x7a9b
cmpl $0x63, 0x24(%rax)
jne 0x7a91
movl %r13d, 0x24(%rax)
movq %rax, %rdi
callq 0x5200
jmp 0x7a82
xorl %edi, %edi
leaq 0x6eaa(%rip), %rsi      # 0xe94e
leaq 0x8(%rsp), %rdx
callq 0x5680
testq %rax, %rax
leaq 0x20(%rsp), %r13
leaq 0x6e21(%rip), %r12      # 0xe8de
leaq 0x6c3c(%rip), %rsi      # 0xe700
jne 0x78aa
movb $0x1, %r13b
jmp 0x7b28
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x7b9a
movq 0x70(%r14), %rax
addq $0x60, %r14
cmpq %r14, %rax
je 0x7af7
movl $0x1, 0x24(%rax)
movq %rax, %rdi
callq 0x5200
jmp 0x7ae1
leaq 0x76d4(%rip), %rdi      # 0xf1d2
callq 0x5570
xorl %ebp, %ebp
jmp 0x7b86
leaq 0x7719(%rip), %rdi      # 0xf227
jmp 0x7b20
leaq 0x774c(%rip), %rdi      # 0xf263
jmp 0x7b20
leaq 0x7726(%rip), %rdi      # 0xf246
callq 0x5570
xorl %r13d, %r13d
movq 0x40(%rsp), %r15
cmpq %rbp, %r15
je 0x7b59
addq $0x58, %r14
leaq 0x20(%r15), %rsi
movl 0x24(%r15), %r12d
movq %r14, %rdi
callq 0x51f0
movl %r12d, (%rax)
movq %r15, %rdi
callq 0x5200
movq %rax, %r15
cmpq %rbp, %rax
jne 0x7b36
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x5080
movq %r14, %rdi
callq 0x5170
xorl %ebp, %ebp
testb %r13b, %r13b
jne 0x7b86
leaq 0x7705(%rip), %rdi      # 0xf281
callq 0x5570
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x5420
leaq 0x770f(%rip), %rdi      # 0xf2a4
callq 0x5570
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7bb1
jmp 0x7bb1
jmp 0x7bb1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5170
movq %rbx, %rdi
callq 0x5640