// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdi, %r14
leaq 0x78c2(%rip), %rdi      # 0xf113
callq 0x5580
xorl %r15d, %r15d
movzbl (%rbx,%r15), %eax
testl %eax, %eax
je 0x786c
cmpl $0xa, %eax
je 0x786c
incq %r15
jmp 0x7859
movq %r15, %rdi
callq 0x54f0
testq %rax, %rax
je 0x78f1
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x52f0
movq %r12, %rdi
decq %rdi
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x7890
xorl %r15d, %r15d
movzbl (%rdi,%r15), %eax
testl %eax, %eax
je 0x78b0
cmpl $0xa, %eax
je 0x78b0
incq %r15
jmp 0x789d
movl $0x1, %ebx
movb $0x1, %bpl
cmpq $0x5, %r15
jb 0x790b
leaq 0x695c(%rip), %rsi      # 0xe221
movl $0x7, %edx
callq 0x54a0
testl %eax, %eax
jne 0x790b
movq 0x70(%r14), %rax
leaq 0x60(%r14), %rbx
cmpq %rbx, %rax
je 0x78fb
movl $0x1, 0x24(%rax)
movq %rax, %rdi
callq 0x5200
jmp 0x78db
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x7cf1
leaq 0x7838(%rip), %rdi      # 0xf13a
callq 0x5580
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpq $0xf, %r15
setb %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x7ccd
leaq 0x30(%rsp), %rax
movl $0x0, (%rax)
xorl %r13d, %r13d
movq %r13, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r13, 0x20(%rax)
movl $0x1, 0x10(%rsp)
cmpl $0x0, 0x20(%r14)
jle 0x7978
leaq 0x28(%rsp), %rbx
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x51f0
movl $0x1, (%rax)
movl 0x10(%rsp), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x10(%rsp)
cmpl 0x20(%r14), %eax
jl 0x7956
leaq 0x77d9(%rip), %rdi      # 0xf158
callq 0x5580
movl 0x20(%r14), %esi
leaq 0x6ca3(%rip), %rdi      # 0xe632
xorl %eax, %eax
callq 0x5060
leaq 0x6cc8(%rip), %rsi      # 0xe665
leaq 0x10(%rsp), %rdx
movq %r12, 0x18(%rsp)
movq %r12, %rdi
callq 0x5690
movq %rax, %rbx
testq %rax, %rax
jne 0x79c9
leaq 0x77d1(%rip), %rdi      # 0xf18f
callq 0x5580
movl $0x1, %r13d
leaq 0x20(%rsp), %r15
leaq 0x4(%rsp), %r12
testl %r13d, %r13d
jne 0x7c72
movq %rbx, %rdi
leaq 0x6ed0(%rip), %rsi      # 0xe8b6
leaq 0x10(%rsp), %rdx
callq 0x5690
testq %rax, %rax
je 0x7c6f
movq %rax, %rdi
leaq 0x6c65(%rip), %rsi      # 0xe668
movq %r15, %rdx
callq 0x5690
movq %rax, %rdi
xorl %r13d, %r13d
testl %r13d, %r13d
jne 0x7b94
leaq 0x6e25(%rip), %rsi      # 0xe846
movq %r15, %rdx
callq 0x5690
testq %rax, %rax
je 0x7b91
xorl %edx, %edx
movq %rax, %rcx
movzbl (%rcx), %esi
cmpl $0x2d, %esi
je 0x7a45
testl %esi, %esi
jne 0x7a4d
jmp 0x7a52
movb $0x20, (%rcx)
movl $0x1, %edx
incq %rcx
jmp 0x7a37
cmpl $0x1, %edx
jne 0x7aa8
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x4(%rsp)
movl %ecx, 0xc(%rsp)
xorl %ebp, %ebp
movq %rax, %rdi
leaq 0x6bfb(%rip), %rsi      # 0xe66b
movq %r12, %rdx
leaq 0xc(%rsp), %rcx
xorl %eax, %eax
callq 0x5350
movl $0x2, %r13d
movl $0x11, %ebx
cmpl $0x2, %eax
jne 0x7b7b
movl 0x4(%rsp), %ecx
movl 0x20(%r14), %eax
cmpl %eax, %ecx
jle 0x7b13
leaq 0x7725(%rip), %rdi      # 0xf1cb
jmp 0x7b04
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %rax, %rdi
leaq 0x6bb2(%rip), %rsi      # 0xe66e
movq %r12, %rdx
xorl %eax, %eax
callq 0x5350
movl $0x2, %r13d
movl $0x11, %ebx
cmpl $0x1, %eax
jne 0x7b7b
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x7afd
cmpl 0x20(%r14), %eax
jg 0x7afd
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x51f0
movl $0x63, (%rax)
jmp 0x7b73
leaq 0x76aa(%rip), %rdi      # 0xf1ae
callq 0x5580
movl $0x14, %r13d
xorl %ebp, %ebp
jmp 0x7b7b
movl 0xc(%rsp), %edx
testl %edx, %edx
je 0x7a9f
testl %ecx, %ecx
je 0x7a9f
cmpl %eax, %edx
jg 0x7a9f
movl $0x1, 0x8(%rsp)
cmpl $0x0, 0x20(%r14)
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %r13
jle 0x7b73
movl $0x1, %eax
cmpl %eax, 0x4(%rsp)
jg 0x7b62
cmpl 0xc(%rsp), %eax
jg 0x7b62
movq %rbx, %rdi
movq %r13, %rsi
callq 0x51f0
movl $0x63, (%rax)
movl 0x8(%rsp), %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x8(%rsp)
cmpl 0x20(%r14), %ecx
jl 0x7b45
xorl %ebx, %ebx
movb $0x1, %bpl
xorl %r13d, %r13d
testb %bpl, %bpl
je 0x7b82
xorl %ebx, %ebx
movl $0x0, %edi
testl %ebx, %ebx
je 0x7a11
jmp 0x7b94
xorl %r13d, %r13d
xorl %r15d, %r15d
testl %r13d, %r13d
je 0x7bb4
movl $0x0, %ebx
testb %r15b, %r15b
leaq 0x20(%rsp), %r15
jne 0x79d3
jmp 0x7c72
xorl %edi, %edi
leaq 0x6c89(%rip), %rsi      # 0xe846
leaq 0x10(%rsp), %rdx
callq 0x5690
testq %rax, %rax
je 0x7c64
movq %rax, %rbx
movzbl (%rbx), %eax
cmpl $0x3d, %eax
je 0x7be0
cmpl $0x20, %eax
jne 0x7be5
incq %rbx
jmp 0x7bd3
movq %rbx, %rdi
callq 0x5150
movq %rax, %r15
xorl %ebp, %ebp
cmpq $0x5, %rax
jb 0x7c14
movl $0x5, %edx
movq %rbx, %rdi
leaq 0x6a6a(%rip), %rsi      # 0xe671
callq 0x54a0
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
cmpq $0xc, %r15
jb 0x7c38
movl $0xc, %edx
movq %rbx, %rdi
leaq 0x6a4e(%rip), %rsi      # 0xe677
callq 0x54a0
testl %eax, %eax
movl $0x2, %eax
cmovel %eax, %ebp
movq 0x40(%rsp), %rax
xorl %r13d, %r13d
movb $0x1, %r15b
leaq 0x30(%rsp), %rcx
cmpq %rcx, %rax
je 0x7b9c
cmpl $0x63, 0x24(%rax)
jne 0x7c5a
movl %ebp, 0x24(%rax)
movq %rax, %rdi
callq 0x5200
jmp 0x7c43
movl $0x2, %r13d
jmp 0x7b9c
xorl %r13d, %r13d
movq 0x40(%rsp), %rbx
leaq 0x30(%rsp), %rax
cmpq %rax, %rbx
movq 0x18(%rsp), %r12
je 0x7cb0
addq $0x58, %r14
leaq 0x20(%rbx), %rsi
movl 0x24(%rbx), %ebp
movq %r14, %rdi
callq 0x51f0
movl %ebp, (%rax)
movq %rbx, %rdi
callq 0x5200
movq %rax, %rbx
leaq 0x30(%rsp), %rax
cmpq %rax, %rbx
jne 0x7c8a
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x5080
xorl %ebx, %ebx
testl %r13d, %r13d
setne %bl
movq %r14, %rdi
callq 0x5170
testl %ebx, %ebx
je 0x7cdd
leaq 0x7511(%rip), %rdi      # 0xf1e9
callq 0x5580
movq %r12, %rdi
callq 0x5430
leaq 0x7520(%rip), %rdi      # 0xf20c
callq 0x5580
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7d08
jmp 0x7d08
jmp 0x7d08
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5170
movq %rbx, %rdi
callq 0x5650
nop