LCOV - code coverage report
Current view: top level - src - ichinorm.c (source / functions) Coverage Total Hit
Test: InChI Unit Test Coverage Lines: 3.9 % 2768 109
Test Date: 2026-10-14 12:14:55 Functions: 1.5 % 65 1
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2.1 % 3193 66

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * SPDX-License-Identifier: MIT
       3                 :             :  * Copyright (c) 2024 IUPAC and InChI Trust
       4                 :             :  */
       5                 :             : 
       6                 :             : 
       7                 :             : /*
       8                 :             : Underivatization, ring-chain tautomerism, OriGAtData edits, etc.
       9                 :             : */
      10                 :             : #include <stdlib.h>
      11                 :             : #include <string.h>
      12                 :             : 
      13                 :             : #include "mode.h"
      14                 :             : #include "ichinorm.h"
      15                 :             : #include "aromaticity.h"
      16                 :             : #include "ichierr.h"
      17                 :             : 
      18                 :             : #include "bcf_s.h"
      19                 :             : 
      20                 :             : #if ( FIND_RING_SYSTEMS == 1 ) /* { */
      21                 :             : 
      22                 :             : 
      23                 :             : 
      24                 :             : /****************************************************************************/
      25                 :         211 : int MarkRingSystemsInp( inp_ATOM *at, int num_atoms, int start )
      26                 :             : {
      27                 :         211 :     AT_NUMB   *nStackAtom = NULL;
      28                 :         211 :     int        nTopStackAtom = -1;
      29                 :         211 :     AT_NUMB   *nRingStack = NULL;
      30                 :         211 :     int        nTopRingStack = -1; /* was AT_NUMB */
      31                 :         211 :     AT_NUMB   *nDfsNumber = NULL;
      32                 :         211 :     AT_NUMB   *nLowNumber = NULL;
      33                 :         211 :     S_CHAR    *cNeighNumb = NULL;
      34                 :             :     AT_NUMB    nDfs;
      35                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
      36                 :             :     AT_NUMB    nRs, *nRsConnect = NULL;
      37                 :             :     int        k;
      38                 :             :     AT_NUMB   *tree = NULL;
      39                 :             :     int        nNumConnect, nMaxNumConnect, nLenConnect;
      40                 :             : #endif
      41                 :             :     AT_NUMB    nNumAtInRingSystem;
      42                 :             :     int        i, j, u, /*start,*/ nNumRingSystems, nNumStartChildren;
      43                 :             : 
      44                 :             :     /*  allocate arrays */
      45                 :         211 :     nStackAtom = (AT_NUMB *) inchi_malloc( num_atoms * sizeof( nStackAtom[0] ) );
      46                 :         211 :     nRingStack = (AT_NUMB *) inchi_malloc( num_atoms * sizeof( nRingStack[0] ) );
      47                 :         211 :     nDfsNumber = (AT_NUMB *) inchi_malloc( num_atoms * sizeof( nDfsNumber[0] ) );
      48                 :         211 :     nLowNumber = (AT_NUMB *) inchi_malloc( num_atoms * sizeof( nLowNumber[0] ) );
      49                 :         211 :     cNeighNumb = (S_CHAR  *) inchi_malloc( num_atoms * sizeof( cNeighNumb[0] ) );
      50                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
      51                 :             :     nRsConnect = (AT_NUMB *) inchi_calloc( 3 * num_atoms + 3, sizeof( nRsConnect[0] ) );
      52                 :             : #endif
      53                 :             :     /*  check allocation */
      54   [ +  -  +  -  :         211 :     if (!nStackAtom || !nRingStack || !nDfsNumber || !nLowNumber || !cNeighNumb
          +  -  +  -  -  
                      + ]
      55                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
      56                 :             :          || !nRsConnect
      57                 :             : #endif
      58                 :             :          )
      59                 :             :     {
      60                 :           0 :         nNumRingSystems = CT_OUT_OF_RAM;  /*  program error */ /*   <BRKPT> */
      61                 :           0 :         goto exit_function;
      62                 :             :     }
      63                 :             : 
      64                 :             :     /********************************************
      65                 :             :     *
      66                 :             :     * Find Cut-vertices & Blocks
      67                 :             :     *
      68                 :             :     ********************************************/
      69                 :             : 
      70                 :             :     /*  initiation */
      71                 :             :     /*start           = 0;*/
      72                 :         211 :     nNumRingSystems = 0;
      73                 :         211 :     u = start; /*  start atom */
      74                 :         211 :     nDfs = 0;
      75                 :         211 :     nTopStackAtom = -1;
      76                 :         211 :     nTopRingStack = -1;
      77                 :         211 :     memset( nDfsNumber, 0, num_atoms * sizeof( nDfsNumber[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
      78                 :         211 :     memset( cNeighNumb, 0, num_atoms * sizeof( cNeighNumb[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
      79                 :             :     /*  push the start atom on the stack */
      80                 :             :     /* djb-rwth: fixing oss-fuzz issue #66720 */
      81         [ +  - ]:         211 :     if (u <= num_atoms - 1)
      82                 :             :     {
      83                 :         211 :         nLowNumber[u] = nDfsNumber[u] = ++nDfs;
      84                 :         211 :         nStackAtom[++nTopStackAtom] = (AT_NUMB)u;
      85                 :         211 :         nRingStack[++nTopRingStack] = (AT_NUMB)u;
      86                 :             :     }
      87                 :             :     else
      88                 :             :     {
      89                 :           0 :         nNumRingSystems = CT_OVERFLOW;  /*  program error */ /*   <BRKPT> */
      90                 :           0 :         goto exit_function;
      91                 :             :     }
      92                 :             : 
      93                 :         211 :     nNumStartChildren = 0;
      94                 :             : 
      95                 :             :     do
      96                 :             :     {
      97                 :             : 
      98                 :             :         /* advance */
      99                 :        2850 :     advance_block:
     100                 :             : 
     101                 :             :         /*if ( (int)at[i=nStackAtom[nTopStackAtom]].valence > (j = (int)cNeighNumb[i]) )*/
     102                 :             :         /* replaced due to missing sequence point */
     103         [ +  + ]:        3061 :         if (i = (int) nStackAtom[nTopStackAtom], j = (int) cNeighNumb[i], (int) at[i].valence > j)
     104                 :             :         {
     105                 :        1936 :             cNeighNumb[i] ++;
     106                 :        1936 :             u = (int) at[i].neighbor[j];
     107         [ +  + ]:        1936 :             if (!nDfsNumber[u])
     108                 :             :             {
     109                 :             :                 /* tree edge, 1st visit -- advance */
     110                 :         914 :                 nStackAtom[++nTopStackAtom] = (AT_NUMB) u;
     111                 :         914 :                 nRingStack[++nTopRingStack] = (AT_NUMB) u;
     112                 :         914 :                 nLowNumber[u] = nDfsNumber[u] = ++nDfs;
     113                 :         914 :                 nNumStartChildren += ( i == start );
     114                 :             :             }
     115                 :             :             else
     116                 :             :             {
     117   [ +  +  +  + ]:        1022 :                 if (!nTopStackAtom || u != (int) nStackAtom[nTopStackAtom - 1])
     118                 :             :                 {
     119                 :             :                     /*  may comment out ? */
     120                 :             :                     /* back edge: u is not a predecessor of i */
     121         [ +  + ]:         108 :                     if (nDfsNumber[u] < nDfsNumber[i])
     122                 :             :                     {
     123                 :             :                         /* Back edge, 1st visit: u is an ancestor of i. Compare */
     124         [ +  - ]:          54 :                         if (nLowNumber[i] > nDfsNumber[u])
     125                 :             :                         {
     126                 :          54 :                             nLowNumber[i] = nDfsNumber[u];
     127                 :             :                         }
     128                 :             :                     }
     129                 :             :                 } /*  may comment out ? */
     130                 :             :             }
     131                 :        1936 :             goto advance_block;
     132                 :             :         }
     133                 :             :         else
     134                 :             :         {
     135                 :        1125 :             cNeighNumb[i] = 0;
     136                 :             :         }
     137                 :             : 
     138                 :             :         /* back up */
     139         [ +  + ]:        1125 :         if (i != start)
     140                 :             :         {
     141                 :         914 :             u = (int) nStackAtom[nTopStackAtom - 1]; /* predecessor of i */
     142         [ +  + ]:         914 :             if (nLowNumber[i] >= nDfsNumber[u])
     143                 :             :             {
     144                 :             :                 /* output the block */
     145                 :         724 :                 nNumRingSystems++;
     146                 :         724 :                 at[u].nBlockSystem = nNumRingSystems;
     147   [ +  +  +  + ]:         724 :                 if (u != start || nNumStartChildren > 1)
     148                 :             :                 {
     149                 :         606 :                     at[u].bCutVertex += 1;
     150                 :             :                 }
     151         [ +  - ]:         914 :                 while (nTopRingStack >= 0)
     152                 :             :                 {
     153                 :         914 :                     j = nRingStack[nTopRingStack--];
     154                 :         914 :                     at[j].nBlockSystem = nNumRingSystems; /*  mark the atom */
     155         [ +  + ]:         914 :                     if (i == j)
     156                 :             :                     {
     157                 :         724 :                         break;
     158                 :             :                     }
     159                 :             :                 }
     160                 :             :             }
     161                 :             :             else
     162                 :             :             {
     163         [ +  + ]:         190 :                 if (nLowNumber[u] > nLowNumber[i])
     164                 :             :                 {
     165                 :             :                     /* inherit */
     166                 :         178 :                     nLowNumber[u] = nLowNumber[i];
     167                 :             :                 }
     168                 :             :             }
     169                 :             :         }
     170         [ +  + ]:        1125 :     } while (--nTopStackAtom >= 0);
     171                 :             : 
     172                 :             :     /****************************************************************************
     173                 :             :     *
     174                 :             :     * Find Ring Systems
     175                 :             :     * Including chain atoms X: A-X-B, where the bonds (of any kind) are bridges.
     176                 :             :     *
     177                 :             :     ****************************************************************************/
     178                 :             : 
     179                 :             : 
     180                 :             :     /*  initiation */
     181                 :             :     /* start           = 0;*/
     182                 :         211 :     nNumRingSystems = 0;
     183                 :         211 :     u = start; /*  start atom */
     184                 :         211 :     nDfs = 0;
     185                 :         211 :     nTopStackAtom = -1;
     186                 :         211 :     nTopRingStack = -1;
     187                 :         211 :     memset( nDfsNumber, 0, num_atoms * sizeof( nDfsNumber[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
     188                 :         211 :     memset( cNeighNumb, 0, num_atoms * sizeof( cNeighNumb[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
     189                 :             :     /*  push the start atom on the stack */
     190                 :         211 :     nLowNumber[u] = nDfsNumber[u] = ++nDfs;
     191                 :         211 :     nStackAtom[++nTopStackAtom] = (AT_NUMB) u;
     192                 :         211 :     nRingStack[++nTopRingStack] = (AT_NUMB) u;
     193                 :             : 
     194                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
     195                 :             :     nNumConnect = nLenConnect = nMaxNumConnect = 0;
     196                 :             : #endif
     197                 :             : 
     198                 :             :     do
     199                 :             :     {
     200                 :             :         /* advance */
     201                 :        2850 :     advance_ring:
     202                 :             :         /*if ( (int)at[i=nStackAtom[nTopStackAtom]].valence > (j = (int)cNeighNumb[i]) )*/
     203                 :             :         /* replaced due to missing sequence point */
     204         [ +  + ]:        3061 :         if (i = (int) nStackAtom[nTopStackAtom], j = (int) cNeighNumb[i], (int) at[i].valence > j)
     205                 :             :         {
     206                 :        1936 :             cNeighNumb[i] ++;
     207                 :        1936 :             u = (int) at[i].neighbor[j];
     208         [ +  + ]:        1936 :             if (!nDfsNumber[u])
     209                 :             :             {
     210                 :             :                 /* tree edge, 1st visit -- advance */
     211                 :         914 :                 nStackAtom[++nTopStackAtom] = (AT_NUMB) u;
     212                 :         914 :                 nRingStack[++nTopRingStack] = (AT_NUMB) u;
     213                 :         914 :                 nLowNumber[u] = nDfsNumber[u] = ++nDfs;
     214                 :             :             }
     215                 :             :             else
     216                 :             :             {
     217   [ +  +  +  + ]:        1022 :                 if (!nTopStackAtom || u != (int) nStackAtom[nTopStackAtom - 1])
     218                 :             :                 {
     219                 :             :                     /* back edge: u is not a predecessor of i */
     220         [ +  + ]:         108 :                     if (nDfsNumber[u] < nDfsNumber[i])
     221                 :             :                     {
     222                 :             :                         /* Back edge, 1st visit: u is ancestor of i. Compare */
     223         [ +  - ]:          54 :                         if (nLowNumber[i] > nDfsNumber[u])
     224                 :             :                         {
     225                 :          54 :                             nLowNumber[i] = nDfsNumber[u];
     226                 :             :                         }
     227                 :             :                     }
     228                 :             :                 }
     229                 :             :             }
     230                 :        1936 :             goto advance_ring;
     231                 :             :         }
     232                 :             :         else
     233                 :             :         {
     234                 :        1125 :             cNeighNumb[i] = 0;
     235                 :             :         }
     236                 :             : 
     237                 :             :         /* back up */
     238         [ +  + ]:        1125 :         if (nDfsNumber[i] == nLowNumber[i])
     239                 :             :         {
     240                 :             :             /*  found a ring system */
     241                 :         893 :             nNumRingSystems++;
     242                 :             :             /*  unwind nRingStack[] down to i */
     243                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
     244                 :             :             nNumConnect = 2;
     245                 :             :             /* data structure: for each ring system nRsConnect[] contains:
     246                 :             :             * 1) nNumConnect+1 = (number of already discovered neighboring "ring systems" + 1)+1
     247                 :             :             * 2) nNumAtInRingSystem
     248                 :             :             * 3) (nNumConnect-1) numbers (IDs) of neighboring ring systems.
     249                 :             :             * BFS guarantees that each neighboring ring system is encountered only one time
     250                 :             :             * Number of all neighboring ring systems = (nNumConnect-1)+1 = nNumConnect
     251                 :             :             * (One additional ring system is where the BFS retracts from the vertex #i,
     252                 :             :             * except when i=DFS root node. In the latter case there is/are only (nNumConnect-1)
     253                 :             :             * neighboring ring system(s).
     254                 :             :             */
     255                 :             : #endif
     256                 :             :             /*  count atoms in a ring system */
     257         [ +  - ]:        1125 :             for (nNumAtInRingSystem = 0, j = nTopRingStack; 0 <= j; j--)
     258                 :             :             {
     259                 :        1125 :                 nNumAtInRingSystem++;
     260         [ +  + ]:        1125 :                 if (i == (int) nRingStack[j])
     261                 :             :                 {
     262                 :         893 :                     break;
     263                 :             :                 }
     264                 :             :             }
     265         [ +  - ]:        1125 :             while (nTopRingStack >= 0)
     266                 :             :             {
     267                 :        1125 :                 j = (int) nRingStack[nTopRingStack--];
     268                 :        1125 :                 at[j].nRingSystem = (AT_NUMB) nNumRingSystems; /*  ring system id */
     269                 :        1125 :                 at[j].nNumAtInRingSystem = nNumAtInRingSystem;
     270                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
     271                 :             :                 for (k = 0; k < at[j].valence; k++)
     272                 :             :                 {
     273                 :             :                     if (( nRs = at[at[j].neighbor[k]].nRingSystem ) && (int) nRs != nNumRingSystems)
     274                 :             :                     {
     275                 :             :                         nRsConnect[nLenConnect + ( nNumConnect++ )] = nRs; /*  adjacent ring system id */
     276                 :             :                     }
     277                 :             :                 }
     278                 :             : #endif
     279         [ +  + ]:        1125 :                 if (i == j)
     280                 :             :                 {
     281                 :             :                     /*  reached atom on the top of nStackAtom[] stack  */
     282                 :         893 :                     break;
     283                 :             :                 }
     284                 :             :             }
     285                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
     286                 :             :             nRsConnect[nLenConnect] = nNumConnect;
     287                 :             :             nRsConnect[nLenConnect + 1] = nNumAtInRingSystem;
     288                 :             :             nLenConnect += nNumConnect;
     289                 :             :             if (nMaxNumConnect < nNumConnect)
     290                 :             :             {
     291                 :             :                 /*  max number of neighboring ring systems */
     292                 :             :                 nMaxNumConnect = nNumConnect;
     293                 :             :             }
     294                 :             : #endif
     295                 :             :         }
     296                 :             :         else
     297                 :             :         {
     298         [ +  - ]:         232 :             if (nTopStackAtom > 0)
     299                 :             :             {
     300                 :         232 :                 j = (int) nStackAtom[nTopStackAtom - 1];
     301                 :             :                 /* inherit nLowNumber */
     302         [ +  + ]:         232 :                 if (nLowNumber[j] > nLowNumber[i])
     303                 :             :                 {
     304                 :         178 :                     nLowNumber[j] = nLowNumber[i];
     305                 :             :                 }
     306                 :             :             }
     307                 :             :         }
     308         [ +  + ]:        1125 :     } while (--nTopStackAtom >= 0);
     309                 :             : 
     310                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 ) /*  normally disabled */
     311                 :             :     nMaxNumConnect++;
     312                 :             :     if (nNumRingSystems > 1)
     313                 :             :     {
     314                 :             :         int nCol = nMaxNumConnect + 1;
     315                 :             :         int nNumInSyst = nMaxNumConnect;
     316                 :             :         int nMaxNeigh = nMaxNumConnect - 1;
     317                 :             : #define T(a,b) tree[(a)*nCol+b]
     318                 :             :         if (tree = (AT_NUMB *) inchi_calloc( nCol * ( nNumRingSystems + 1 ), sizeof( tree[0] ) ))
     319                 :             :         {
     320                 :             :             int len, neigh;
     321                 :             :             /*  reuse previous allocations */
     322                 :             :             AT_NUMB *nNumVisitedNeighbors = nStackAtom;
     323                 :             :             AT_NUMB *nDistanceFromTerminal = nRingStack;
     324                 :             :             AT_NUMB *nCurrActiveRingSystem = nDfsNumber;
     325                 :             :             AT_NUMB *nNextActiveRingSystem = nLowNumber;
     326                 :             :             int        nNumCurrActiveRingSystems, nNumNextActiveRingSystems, pass;
     327                 :             :             /* build a "condensation graph (actually, a tree)" in which
     328                 :             :             * each vertex corresponds to a ring system T(row, col) = T(ring syst, neighbors)
     329                 :             :             * Number of rows = column length = max. number of ring system neighbors + 2
     330                 :             :             * Number of cols = row length    = number of ring systems + 1
     331                 :             :             * Neighboring ring systems are contiguously stored in a row
     332                 :             :             * T(i,0) = number of neighbors,  1 <= i <= nNumRingSystems;
     333                 :             :             * T(i,k) = number of a neighboring ring system, 1 <= k <= T(i,0)
     334                 :             :             * T(i,nCol-1) = number of atoms in the system #i
     335                 :             :             */
     336                 :             :             for (i = 1, j = 0; len = nRsConnect[j]; i++)
     337                 :             :             {
     338                 :             :                 T( i, nNumInSyst ) = nRsConnect[j + 1];
     339                 :             :                 for (k = 2; k < len; k++)
     340                 :             :                 {
     341                 :             :                     neigh = nRsConnect[j + k];
     342                 :             :                     if (T( i, 0 ) < nMaxNeigh && T( neigh, 0 ) < nMaxNeigh)
     343                 :             :                     {
     344                 :             :                         T( i, 0 )++;
     345                 :             :                         T( neigh, 0 )++;
     346                 :             :                         T( i, T( i, 0 ) ) = neigh;
     347                 :             :                         T( neigh, T( neigh, 0 ) ) = i;
     348                 :             :                     }
     349                 :             :                     else
     350                 :             :                     {
     351                 :             :                         nNumRingSystems = CT_OVERFLOW;  /*  program error */ /*   <BRKPT> */
     352                 :             :                         goto exit_function;
     353                 :             :                     }
     354                 :             :                 }
     355                 :             :                 j += len;
     356                 :             :             }
     357                 :             :             /*  clear memory */
     358                 :             :             memset( nNumVisitedNeighbors, 0, nNumRingSystems * sizeof( nNumVisitedNeighbors[0] ) );
     359                 :             :             memset( nDistanceFromTerminal, 0, nNumRingSystems * sizeof( nDistanceFromTerminal[0] ) );
     360                 :             :             memset( nCurrActiveRingSystem, 0, nNumRingSystems * sizeof( nCurrActiveRingSystem[0] ) );
     361                 :             :             memset( nNextActiveRingSystem, 0, nNumRingSystems * sizeof( nNextActiveRingSystem[0] ) );
     362                 :             :             nNumNextActiveRingSystems = 0;
     363                 :             :             for (i = 0; i < nNumRingSystems; i++)
     364                 :             :             {
     365                 :             :                 if (1 == T( i + 1, 0 ))
     366                 :             :                 {
     367                 :             :                     nNextActiveRingSystem[i] = 1; /*  number of traversed neighbors + 1 */
     368                 :             :                     nDistanceFromTerminal[i] = 1;
     369                 :             :                     nNumNextActiveRingSystems++;
     370                 :             :                 }
     371                 :             :                 else
     372                 :             :                 {
     373                 :             :                     nNextActiveRingSystem[i] = 0;
     374                 :             :                     nDistanceFromTerminal[i] = 0;
     375                 :             :                 }
     376                 :             :                 nNumVisitedNeighbors[i] = 0;
     377                 :             :             }
     378                 :             : 
     379                 :             :             /* nCurrActiveRingSystem[i] = a sum of:
     380                 :             :             * 1) +1 if it is or was active
     381                 :             :             * 2) +(number of neighbors from which it was reached)
     382                 :             :             * 3) +1 if it was left and not active anymore
     383                 :             :             */
     384                 :             :             pass = 0;
     385                 :             :             do
     386                 :             :             {
     387                 :             :                 nNumCurrActiveRingSystems = nNumNextActiveRingSystems;
     388                 :             :                 nNumNextActiveRingSystems = 0;
     389                 :             :                 memcpy( nCurrActiveRingSystem, nNextActiveRingSystem,
     390                 :             :                         nNumRingSystems * sizeof( nNextActiveRingSystem[0] ) );
     391                 :             :                 for (i = 0; i < nNumRingSystems; i++)
     392                 :             :                 {
     393                 :             :                     if (T( i + 1, 0 ) == nCurrActiveRingSystem[i])
     394                 :             :                     {
     395                 :             :                         /* on the previous pass currently active ring system i+1 bas been reached
     396                 :             :                         * from all neighbors except one;
     397                 :             :                         * the neighbors from which it was reached have
     398                 :             :                         * T(neigh,0)+1 == nCurrActiveRingSystem[i]
     399                 :             :                         * this ring system has not been left yet
     400                 :             :                         */
     401                 :             :                         for (k = 1, len = T( i + 1, 0 ); k <= len; k++)
     402                 :             :                         {
     403                 :             :                             neigh = (int) T( i + 1, k );
     404                 :             :                             if (T( neigh, 0 ) >= nCurrActiveRingSystem[neigh - 1])
     405                 :             :                             {
     406                 :             :                                 if (0 == pass)
     407                 :             :                                 {
     408                 :             :                                     nDistanceFromTerminal[i] = 1;
     409                 :             :                                 }
     410                 :             :                                 break;
     411                 :             :                             }
     412                 :             :                         }
     413                 :             :                         if (k <= len)
     414                 :             :                         {
     415                 :             :                             /* neigh was not reached from at least 2 neighbors
     416                 :             :                             * walk along -R- chain (T(neigh,0)==2) up to
     417                 :             :                             * 1)  a terminal system, not including it or
     418                 :             :                             * 2)  a branching point.
     419                 :             :                             *
     420                 :             :                             * pass = 0: started from terminal systems:
     421                 :             :                             *     reach the branching point.
     422                 :             :                             * If chain system next to a terminal system has already been reached
     423                 :             :                             * then walk along it according to Note below
     424                 :             :                             *
     425                 :             :                             * pass > 0: started from branching points
     426                 :             :                             * 2a) If the branching point has not been reached from 2 or more neighbors,
     427                 :             :                             *     then include it
     428                 :             :                             * 2b) If the branching point has not been reached from 1 neighbor only,
     429                 :             :                             *     then do not include it: it will be a starting point later
     430                 :             :                             * Note: if a chain atom already has nDistanceFromTerminal[i] > 0, then
     431                 :             :                             *     the last atom should be the one such that
     432                 :             :                             *     its nDistanceFromTerminal[]+1>= nDistanceFromTerminal[] of the
     433                 :             :                             *     next in the chain
     434                 :             :                             */
     435                 :             :                             int bOk = 0;
     436                 :             :                             k = i + 1; /*  starting point */
     437                 :             :                             if (0 == pass && T( k, nNumInSyst ) > 1)
     438                 :             :                             {
     439                 :             :                                 nNumNextActiveRingSystems++; /*  request next pass */
     440                 :             :                                 continue; /*  stop a the terminal ring system */
     441                 :             :                             }
     442                 :             :                             while (2 == T( neigh, 0 ))
     443                 :             :                             {
     444                 :             :                                 /*  walk along a chain */
     445                 :             :                                 if (!nNextActiveRingSystem[neigh - 1])
     446                 :             :                                 {
     447                 :             :                                     nNextActiveRingSystem[neigh - 1] = 1; /*  make neighbor active */
     448                 :             :                                 }
     449                 :             :                                 else
     450                 :             :                                     if (nDistanceFromTerminal[k - 1] + 1 <= nDistanceFromTerminal[neigh - 1])
     451                 :             :                                     {
     452                 :             :                                         /*  walking along the chain; already have had a walk */
     453                 :             :                                         /*  in the opposite direction at this pass */
     454                 :             :                                     }
     455                 :             :                                     else
     456                 :             :                                     {
     457                 :             :                                         /*  k is the last; neigh (it is a bridge -X-) has not been reached */
     458                 :             :                                         bOk = 1;
     459                 :             :                                         break;
     460                 :             :                                     }
     461                 :             :                                 nNextActiveRingSystem[k - 1] ++; /*  leave system k */
     462                 :             :                                 if (nNextActiveRingSystem[neigh - 1] < T( neigh, 0 ))
     463                 :             :                                 {
     464                 :             :                                     nNextActiveRingSystem[neigh - 1] ++; /*  add one connection to neigh */
     465                 :             :                                 }
     466                 :             :                                 nDistanceFromTerminal[neigh - 1] = nDistanceFromTerminal[k - 1] + 1;
     467                 :             :                                 j = ( T( neigh, 1 ) == k ) ? 2 : 1;
     468                 :             :                                 k = neigh;
     469                 :             :                                 neigh = T( k, j ); /*  next in the chain */
     470                 :             :                                 nNumNextActiveRingSystems++;
     471                 :             :                                 if (T( k, nNumInSyst ) > 1)
     472                 :             :                                 {
     473                 :             :                                     bOk = 1;
     474                 :             :                                     break; /*  stop on a ring system */
     475                 :             :                                 }
     476                 :             :                             }
     477                 :             :                             /*  neigh is a terminal or a bridge or a branching point */
     478                 :             :                             if (2 > T( neigh, 0 ))
     479                 :             :                             {
     480                 :             :                                 /*  neighbor is a terminal atom */
     481                 :             :                                 if (1 < pass)
     482                 :             :                                 {
     483                 :             :                                     nNumRingSystems = CT_UNKNOWN_ERR; /*  error (debug only) */ /*   <BRKPT> */
     484                 :             :                                     goto exit_function;
     485                 :             :                                 }
     486                 :             :                                 continue;
     487                 :             :                             }
     488                 :             :                             if (2 == T( neigh, 0 ))
     489                 :             :                             {
     490                 :             :                                 /*  neighbor is a bridge */
     491                 :             :                                 continue;
     492                 :             :                             }
     493                 :             :                             /*  neighbor is a branching point */
     494                 :             :                             if (T( neigh, 0 ) > nCurrActiveRingSystem[neigh - 1])
     495                 :             :                             {
     496                 :             :                                 /*  move to the neigh (make neigh active): on previous pass it */
     497                 :             :                                 /*  has not been reached from 2 or more neighbors */
     498                 :             :                                 if (!nNextActiveRingSystem[neigh - 1])
     499                 :             :                                 {
     500                 :             :                                     nNextActiveRingSystem[neigh - 1] = 1;
     501                 :             :                                 }
     502                 :             :                                 if (nDistanceFromTerminal[neigh - 1] < nDistanceFromTerminal[k - 1] + 1)
     503                 :             :                                 {
     504                 :             :                                     nDistanceFromTerminal[neigh - 1] = nDistanceFromTerminal[k - 1] + 1;
     505                 :             :                                 }
     506                 :             :                                 nNextActiveRingSystem[k - 1] ++; /*  leave system k */
     507                 :             :                                 if (nNextActiveRingSystem[neigh - 1] < T( neigh, 0 ))
     508                 :             :                                 {
     509                 :             :                                     nNextActiveRingSystem[neigh - 1] ++; /*  add one connection to neigh */
     510                 :             :                                 }
     511                 :             :                                 nNumNextActiveRingSystems++;
     512                 :             :                             }
     513                 :             :                         }
     514                 :             :                     }
     515                 :             :                 }
     516                 :             :                 pass++;
     517                 :             :             } while (nNumNextActiveRingSystems);
     518                 :             : 
     519                 :             :             for (i = 0; i < num_atoms; i++)
     520                 :             :             {
     521                 :             :                 at[i].nDistanceFromTerminal = nDistanceFromTerminal[(int) at[i].nRingSystem - 1];
     522                 :             :             }
     523                 :             : 
     524                 :             :             inchi_free( tree );
     525                 :             :             tree = NULL;
     526                 :             : #undef T
     527                 :             :         }
     528                 :             :         else
     529                 :             :         {
     530                 :             :             nNumRingSystems = CT_OUT_OF_RAM; /*  error */ /*   <BRKPT> */
     531                 :             :             goto exit_function;
     532                 :             :         }
     533                 :             :     }
     534                 :             : #endif
     535                 :             : 
     536                 :         211 : exit_function:
     537                 :             : 
     538         [ +  - ]:         211 :     if (nStackAtom)
     539                 :             :     {
     540         [ +  - ]:         211 :         inchi_free( nStackAtom );
     541                 :             :     }
     542         [ +  - ]:         211 :     if (nRingStack)
     543                 :             :     {
     544         [ +  - ]:         211 :         inchi_free( nRingStack );
     545                 :             :     }
     546         [ +  - ]:         211 :     if (nDfsNumber)
     547                 :             :     {
     548         [ +  - ]:         211 :         inchi_free( nDfsNumber );
     549                 :             :     }
     550         [ +  - ]:         211 :     if (nLowNumber)
     551                 :             :     {
     552         [ +  - ]:         211 :         inchi_free( nLowNumber );
     553                 :             :     }
     554         [ +  - ]:         211 :     if (cNeighNumb)
     555                 :             :     {
     556         [ +  - ]:         211 :         inchi_free( cNeighNumb );
     557                 :             :     }
     558                 :             : 
     559                 :             : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
     560                 :             :     if (nRsConnect)
     561                 :             :         inchi_free( nRsConnect );
     562                 :             :     if (tree)
     563                 :             :         inchi_free( tree );
     564                 :             : #endif
     565                 :             : 
     566                 :         211 :     return nNumRingSystems;
     567                 :             : }
     568                 :             : 
     569                 :             : 
     570                 :             : #endif /* } FIND_RING_SYSTEMS */
     571                 :             : 
     572                 :             : 
     573                 :             : /****************************************************************************
     574                 :             : 
     575                 :             : InChI post-version 1.01 features implementation
     576                 :             : (v. 1.06+ : underivatize is still an experiment available in engineering mode)
     577                 :             : 
     578                 :             : ****************************************************************************/
     579                 :             : 
     580                 :             : #if ( RING2CHAIN == 1 || UNDERIVATIZE == 1 )
     581                 :             : 
     582                 :             : typedef struct tagAtPair
     583                 :             : {
     584                 :             :     AT_NUMB at[2];  /* at[0] < at[1] */
     585                 :             :     AT_NUMB atno;   /* atom marked with derivative type */
     586                 :             : } R2C_ATPAIR;
     587                 :             : 
     588                 :             : 
     589                 :             : /* Local functions */
     590                 :             : void set_R2C_el_numbers( void );
     591                 :             : int subtract_DT_from_num_H( int num_atoms, inp_ATOM *at );
     592                 :             : int add_inp_ATOM( inp_ATOM *at, int len_at, int len_cur,
     593                 :             :                   inp_ATOM *add, int len_add );
     594                 :             : int cmp_r2c_atpair( const void *p1, const void *p2 );
     595                 :             : int has_atom_pair( R2C_ATPAIR *ap, int num_ap, AT_NUMB at1, AT_NUMB at2 );
     596                 :             : int mark_atoms_ap( inp_ATOM *at, AT_NUMB start, R2C_ATPAIR *ap,
     597                 :             :                    int num_ap, int num, AT_NUMB cFlags );
     598                 :             : 
     599                 :             : int UnMarkDisconnectedComponents( ORIG_ATOM_DATA *orig_inp_data );
     600                 :             : int UnMarkOtherIndicators( inp_ATOM *at, int num_atoms );
     601                 :             : int UnMarkOneComponent( inp_ATOM *at, int num_atoms );
     602                 :             : 
     603                 :             : /* Other functions */
     604                 :             : int DisconnectInpAtBond( inp_ATOM *at, AT_NUMB *nOldCompNumber,
     605                 :             :                          int iat, int neigh_ord );
     606                 :             : int ExtractConnectedComponent( inp_ATOM *at, int num_at,
     607                 :             :                                int component_number, inp_ATOM *component_at );
     608                 :             : int UnMarkRingSystemsInp( inp_ATOM *at, int num_atoms );
     609                 :             : 
     610                 :             : 
     611                 :             : 
     612                 :             : /****************************************************************************
     613                 :             : Clear the (disconnected) components info in  ORIG_ATOM_DATA
     614                 :             : ****************************************************************************/
     615                 :           0 : int UnMarkDisconnectedComponents( ORIG_ATOM_DATA *orig_inp_data )
     616                 :             : {
     617                 :             :     int i;
     618                 :             : 
     619         [ #  # ]:           0 :     for (i = 0; i < orig_inp_data->num_inp_atoms; i++)
     620                 :             :     {
     621                 :           0 :         orig_inp_data->at[i].orig_compt_at_numb = 0;
     622                 :           0 :         orig_inp_data->at[i].component = 0;
     623                 :             :     }
     624                 :             : 
     625         [ #  # ]:           0 :     if (orig_inp_data->nCurAtLen)
     626                 :             :     {
     627         [ #  # ]:           0 :         inchi_free( orig_inp_data->nCurAtLen );
     628                 :           0 :         orig_inp_data->nCurAtLen = NULL;
     629                 :             :     }
     630                 :             : 
     631         [ #  # ]:           0 :     if (orig_inp_data->nOldCompNumber)
     632                 :             :     {
     633         [ #  # ]:           0 :         inchi_free( orig_inp_data->nOldCompNumber );
     634                 :           0 :         orig_inp_data->nOldCompNumber = NULL;
     635                 :             :     }
     636                 :             : 
     637                 :           0 :     orig_inp_data->num_components = 0;
     638                 :             : 
     639                 :           0 :     return 0;
     640                 :             : }
     641                 :             : 
     642                 :             : 
     643                 :             : /****************************************************************************/
     644                 :           0 : int UnMarkOtherIndicators( inp_ATOM *at, int num_atoms )
     645                 :             : {
     646                 :             :     int i;
     647         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
     648                 :             :     {
     649                 :           0 :         at[i].at_type = 0;
     650                 :           0 :         at[i].cFlags = 0;
     651                 :             :     }
     652                 :             : 
     653                 :           0 :     return 0;
     654                 :             : }
     655                 :             : 
     656                 :             : 
     657                 :             : /****************************************************************************
     658                 :             : Clear the (disconnected) component numbers in atoms of inp_ATOM structure
     659                 :             : (which typicall came from INP_ATOM_DATA)
     660                 :             : ****************************************************************************/
     661                 :           0 : int UnMarkOneComponent( inp_ATOM *at, int num_atoms )
     662                 :             : {
     663                 :             :     int i;
     664         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
     665                 :             :     {
     666                 :           0 :         at[i].orig_compt_at_numb = 0;
     667                 :           0 :         at[i].component = 0;
     668                 :             :     }
     669                 :             : 
     670                 :           0 :     return 0;
     671                 :             : }
     672                 :             : 
     673                 :             : 
     674                 :             : /****************************************************************************/
     675                 :           0 : void set_R2C_el_numbers( void )
     676                 :             : {
     677                 :             :     /*
     678                 :             :     if (!el_number_O)
     679                 :             :     {
     680                 :             :     el_number_O = EL_NUMBER_O;
     681                 :             :     el_number_C = EL_NUMBER_C;
     682                 :             :     el_number_N = EL_NUMBER_N;
     683                 :             :     el_number_P = EL_NUMBER_P;
     684                 :             :     el_number_S = EL_NUMBER_S;
     685                 :             :     el_number_Si = EL_NUMBER_SI;
     686                 :             :     el_number_F = EL_NUMBER_F;
     687                 :             :     el_number_Cl = EL_NUMBER_CL;
     688                 :             :     el_number_Br = EL_NUMBER_BR;
     689                 :             :     el_number_I = EL_NUMBER_I;
     690                 :             :     el_number_H = EL_NUMBER_H;
     691                 :             :     }
     692                 :             :     */
     693                 :           0 : }
     694                 :             : 
     695                 :             : 
     696                 :             : /****************************************************************************/
     697                 :           0 : int subtract_DT_from_num_H( int num_atoms, inp_ATOM *at )
     698                 :             : /*  assume num_1H, num_D and num_T are included in num_H */
     699                 :             : {
     700                 :             :     int i, j;
     701         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
     702                 :             :     {
     703         [ #  # ]:           0 :         for (j = 0; j < NUM_H_ISOTOPES; j++)
     704                 :           0 :             at[i].num_H -= at[i].num_iso_H[j];
     705                 :             :     }
     706                 :             : 
     707                 :           0 :     return 0;
     708                 :             : }
     709                 :             : 
     710                 :             : 
     711                 :             : /****************************************************************************/
     712                 :           0 : int add_inp_ATOM( inp_ATOM *at,
     713                 :             :                   int len_at,
     714                 :             :                   int len_cur,
     715                 :             :                   inp_ATOM *add,
     716                 :             :                   int len_add )
     717                 :             : {
     718                 :             :     int i, j;
     719                 :             :     inp_ATOM *a;
     720                 :             :     /* chack correctness */
     721         [ #  # ]:           0 :     if (len_cur < 0)
     722                 :           0 :         return len_cur;
     723         [ #  # ]:           0 :     if (len_add < 0)
     724                 :           0 :         return len_add;
     725         [ #  # ]:           0 :     if (len_cur + len_add > len_at)
     726                 :           0 :         return -1;
     727                 :             :     /* copy */
     728                 :           0 :     memcpy(at + len_cur, add, len_add * sizeof(at[0]));
     729                 :             :     /* modify */
     730         [ #  # ]:           0 :     if (len_cur)
     731                 :             :     {
     732                 :           0 :         a = at + len_cur;
     733         [ #  # ]:           0 :         for (i = 0; i < len_add; i++)
     734                 :             :         {
     735         [ #  # ]:           0 :             for (j = 0; j < a[i].valence; j++)
     736                 :             :             {
     737                 :           0 :                 a[i].neighbor[j] += len_cur;
     738                 :             :             }
     739                 :             :         }
     740                 :             :     }
     741                 :             : 
     742                 :           0 :     return len_cur + len_add;
     743                 :             : }
     744                 :             : 
     745                 :             : 
     746                 :             : /****************************************************************************/
     747                 :           0 : int cmp_r2c_atpair( const void *p1, const void *p2 )
     748                 :             : {
     749                 :           0 :     const R2C_ATPAIR *ap1 = (const R2C_ATPAIR *) p1;
     750                 :           0 :     const R2C_ATPAIR *ap2 = (const R2C_ATPAIR *) p2;
     751                 :           0 :     int diff = (int) ap1->at[0] - (int) ap2->at[0];
     752         [ #  # ]:           0 :     if (!diff)
     753                 :             :     {
     754                 :           0 :         diff = (int) ap1->at[1] - (int) ap2->at[1];
     755                 :             :     }
     756                 :             : 
     757                 :           0 :     return diff;
     758                 :             : }
     759                 :             : 
     760                 :             : 
     761                 :             : /****************************************************************************/
     762                 :           0 : int has_atom_pair_seq( R2C_ATPAIR *ap, int num_ap, AT_NUMB at1, AT_NUMB at2 )
     763                 :             : {
     764                 :             :     R2C_ATPAIR ap1;
     765                 :             :     int i1;
     766                 :           0 :     int n = at1 > at2;
     767                 :             : 
     768                 :           0 :     ap1.at[n] = at1;
     769                 :           0 :     ap1.at[1 - n] = at2;
     770         [ #  # ]:           0 :     for (i1 = 0; i1 < num_ap; i1++)
     771                 :             :     {
     772         [ #  # ]:           0 :         if (ap[i1].at[0] == ap1.at[0] &&
     773         [ #  # ]:           0 :              ap[i1].at[1] == ap1.at[1])
     774                 :           0 :             return i1 + 1;
     775                 :             :     }
     776                 :             : 
     777                 :           0 :     return 0; /* not found */
     778                 :             : }
     779                 :             : 
     780                 :             : 
     781                 :             : /****************************************************************************/
     782                 :           0 : int has_atom_pair( R2C_ATPAIR *ap, int num_ap, AT_NUMB at1, AT_NUMB at2 )
     783                 :             : {
     784                 :             :     R2C_ATPAIR ap1;
     785                 :             :     int i1, i2, i3, diff;
     786                 :           0 :     int n = at1 > at2;
     787                 :             : 
     788                 :           0 :     ap1.at[n] = at1;
     789                 :           0 :     ap1.at[1 - n] = at2;
     790                 :           0 :     i1 = 0;
     791                 :           0 :     i2 = num_ap - 1;
     792                 :             :     /* search for ap1 by simple bisections */
     793                 :             :     do
     794                 :             :     {
     795                 :           0 :         i3 = ( i1 + i2 ) / 2;
     796         [ #  # ]:           0 :         if (!( diff = cmp_r2c_atpair( &ap1, ap + i3 ) ))
     797                 :             :         {
     798                 :           0 :             return i3 + 1;  /* found => positive number */
     799                 :             :         }
     800                 :             :         else
     801         [ #  # ]:           0 :             if (diff > 0)
     802                 :             :             {
     803                 :           0 :                 i1 = i3 + 1;
     804                 :             :             }
     805                 :             :             else
     806                 :             :             {
     807                 :           0 :                 i2 = i3 - 1;
     808                 :             :             }
     809         [ #  # ]:           0 :     } while (i2 >= i1);
     810                 :             : 
     811                 :           0 :     return 0; /* not found */
     812                 :             : }
     813                 :             : 
     814                 :             : 
     815                 :             : /****************************************************************************
     816                 :             : DFS search for atoms that do not have a flag
     817                 :             : ****************************************************************************/
     818                 :           0 : int mark_atoms_ap( inp_ATOM *at,
     819                 :             :                    AT_NUMB start,
     820                 :             :                    R2C_ATPAIR *ap,
     821                 :             :                    int num_ap, int num,
     822                 :             :                    AT_NUMB cFlags )
     823                 :             : {
     824         [ #  # ]:           0 :     if (!at[start].at_type)
     825                 :             :     {
     826                 :             :         int i;
     827                 :             :         AT_NUMB neigh;
     828                 :           0 :         at[start].at_type = cFlags;
     829                 :           0 :         num++;
     830                 :             : 
     831         [ #  # ]:           0 :         for (i = 0; i < at[start].valence; i++)
     832                 :             :         {
     833                 :           0 :             neigh = at[start].neighbor[i];
     834         [ #  # ]:           0 :             if (has_atom_pair_seq( ap, num_ap, start, neigh ))
     835                 :             :             {
     836                 :           0 :                 continue;
     837                 :             :             }
     838                 :           0 :             num = mark_atoms_ap( at, neigh, ap, num_ap, num, cFlags );
     839                 :             :         }
     840                 :             :     }
     841                 :             : 
     842                 :           0 :     return num; /* number of atoms traversed forward from at[start] */
     843                 :             : }
     844                 :             : 
     845                 :             : #endif /* RING2CHAIN || UNDERIVATIZE */
     846                 :             : 
     847                 :             : 
     848                 :             : 
     849                 :             : #if ( UNDERIVATIZE == 1 )
     850                 :             : 
     851                 :             : /****************************************************************************/
     852                 :             : 
     853                 :             : #ifdef NEVER
     854                 :             : typedef struct tagAtTypeBitmap {
     855                 :             :     AT_NUMB ord1 : 5; /* up to 2^5-1 = 31 = 0x0037 */
     856                 :             :     AT_NUMB ord2 : 5;
     857                 :             :     AT_NUMB type : 6; /* up to 2^6-1 = 63 = 0x0077 */
     858                 :             : } AtTypeBitmap;
     859                 :             : typedef union tagAtTypeUnion {
     860                 :             :     AT_NUMB num;
     861                 :             :     AtTypeBitmap bits;
     862                 :             : } AtTypeUnion;
     863                 :             : #endif
     864                 :             : 
     865                 :             : 
     866                 :             : /* Underivatize settings */
     867                 :             : /* DERIV_AT::typ begin */
     868                 :             : #define DERIV_BRIDGE_O  0x0001   /* R1-O-R2 => R1-OH + HO-R2 */
     869                 :             : #define DERIV_BRIDGE_NH 0x0002   /* R1-NH-R2  amine */
     870                 :             : #define DERIV_AMINE_tN  0x0004   /* R1-N(-R2)-R3  tertiary amine */
     871                 :             : #define DERIV_RING_O_OUTSIDE_PRECURSOR    0x0008   /* -O- in a ring */
     872                 :             : #define DERIV_RING_NH_OUTSIDE_PRECURSOR   0x0010   /* -NH- in a ring */
     873                 :             : /* MOX_EtOX Underiv: R2-(R3-)C=N-O-R => >C=O (ketone, aldehide only);
     874                 :             : -R: -CH3, -CH2-CH3, -Si(CH3)3, -CH2-Phenyl
     875                 :             : DERIV_X_OXIME        R3- may be H or any C, R2- is any C
     876                 :             : precursor: R2-(R3-)C=O  (note: N replaced with O)-- 2013-08-23 DT */
     877                 :             : #define DERIV_X_OXIME   0x0020   /* comment out to disable */
     878                 :             : /*  */
     879                 :             : #define DERIV_UNMARK    0x0040   /* unmark the cut */
     880                 :             : #define DERIV_DUPLIC    0x0080   /* duplicated disconnection */
     881                 :             : /* comment out to disable */
     882                 :             : #define DERIV_RO_COX    0x0100   /* alcohol derivatives: R-O--C(=O)C[n]F[2n+1] 0<n<4, R-O--C(=O)CH3, R-O--C(=O)-Phenyl */
     883                 :             : /* comment out next 2 to disable; DMOX=Dimethyloxazoline DEOX=Diethyloxazoline */
     884                 :             : #define DERIV_RING_DMOX_DEOX_N   0x0200   /* =N- in a ring:    /-O--CH2-\      DMOX, DEOX  */
     885                 :             : #define DERIV_RING_DMOX_DEOX_O   0x0400   /* -O- in a ring: R-C=N--------C<2Me or 2Et */
     886                 :             : 
     887                 :             : #ifdef UNDERIV_PYRROLIDIDES
     888                 :             : #define DERIV_RING2_PRRLDD_OUTSIDE_PRECUR 0x0800  /* alcohol derivatives: R(=O)-N<C4H4 5-memb ring Pyrrolidides; replace -N< with -OH */
     889                 :             : #endif
     890                 :             : 
     891                 :             : #define DERIV_RING2_PPRDN_OUTSIDE_PRECUR  0x1000  /* alcohol derivatives: R(=O)-N<C5H5 6-memb ring Piperidines; replace -N< with -OH */
     892                 :             : #define DERIV_DANSYL                      0x2000  /* alcohol derivatives: R-O--SO2-C10H5-N(CH3)2 => R-OH */
     893                 :             : /* DERIV_AT::typ end */
     894                 :             : 
     895                 :             : /* derivative types, which cannot be absorbed into greater derivatization agents */
     896                 :             : static const int DERIV_UNEXPADABLE = 0
     897                 :             : #ifdef DERIV_X_OXIME
     898                 :             : | DERIV_X_OXIME
     899                 :             : #endif
     900                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
     901                 :             : | ( DERIV_RING_DMOX_DEOX_O | DERIV_RING_DMOX_DEOX_N )
     902                 :             : #endif
     903                 :             : /*
     904                 :             : #ifdef DERIV_RO_COX
     905                 :             : |DERIV_RO_COX
     906                 :             : #endif
     907                 :             : */
     908                 :             : #ifdef DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
     909                 :             : | DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
     910                 :             : #endif
     911                 :             : #ifdef DERIV_RING2_PPRDN_OUTSIDE_PRECUR
     912                 :             : | DERIV_RING2_PPRDN_OUTSIDE_PRECUR
     913                 :             : #endif
     914                 :             : #ifdef DERIV_DANSYL
     915                 :             : | DERIV_DANSYL
     916                 :             : #endif
     917                 :             : ;
     918                 :             : /* derivative precursor types in which Precur=N- is replaced with Precur=O */
     919                 :             : static const int DERIV_REPL_N_WITH_O = 0
     920                 :             : #ifdef DERIV_X_OXIME
     921                 :             : | DERIV_X_OXIME
     922                 :             : #endif
     923                 :             : #ifdef DERIV_RING_DMOX_DEOX_N
     924                 :             : | DERIV_RING_DMOX_DEOX_N
     925                 :             : #endif
     926                 :             : ;
     927                 :             : 
     928                 :             : /* derivative precursor types in which Precur-N< is replaced with Precur-OH */
     929                 :             : static const int DERIV_REPL_N_WITH_OH = 0
     930                 :             : #ifdef DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
     931                 :             : | DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
     932                 :             : #endif
     933                 :             : #ifdef DERIV_RING2_PPRDN_OUTSIDE_PRECUR
     934                 :             : | DERIV_RING2_PPRDN_OUTSIDE_PRECUR
     935                 :             : #endif
     936                 :             : ;
     937                 :             : 
     938                 :             : /* combined DERIV_AT::typ */
     939                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
     940                 :             : #define DERIV_RING_DMOX_DEOX     (DERIV_RING_DMOX_DEOX_O | DERIV_RING_DMOX_DEOX_N)
     941                 :             : #endif
     942                 :             : 
     943                 :             : #if( defined(DERIV_RING_O_OUTSIDE_PRECURSOR) && defined(DERIV_RING_NH_OUTSIDE_PRECURSOR) )
     944                 :             : #define DERIV_RING_OUTSIDE_PRECURSOR     (DERIV_RING_O_OUTSIDE_PRECURSOR | DERIV_RING_NH_OUTSIDE_PRECURSOR)
     945                 :             : #elif( defined(DERIV_RING_O_OUTSIDE_PRECURSOR) )
     946                 :             : #define DERIV_RING_OUTSIDE_PRECURSOR   DERIV_RING_O_OUTSIDE_PRECURSOR
     947                 :             : #elif( defined(DERIV_RING_NH_OUTSIDE_PRECURSOR) )
     948                 :             : #define DERIV_RING_OUTSIDE_PRECURSOR   DERIV_RING_NH_OUTSIDE_PRECURSOR
     949                 :             : #else
     950                 :             : #define DERIV_RING_OUTSIDE_PRECURSOR 0
     951                 :             : #endif
     952                 :             : 
     953                 :             : #if( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) && defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
     954                 :             : #define DERIV_RING2_OUTSIDE_PRECUR  (DERIV_RING2_PRRLDD_OUTSIDE_PRECUR | DERIV_RING2_PPRDN_OUTSIDE_PRECUR)
     955                 :             : #elif ( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) )
     956                 :             : #define DERIV_RING2_OUTSIDE_PRECUR  DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
     957                 :             : #elif ( defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
     958                 :             : #define DERIV_RING2_OUTSIDE_PRECUR DERIV_RING2_PPRDN_OUTSIDE_PRECUR
     959                 :             : #endif
     960                 :             : /* combined DERIV_AT::typ end */
     961                 :             : 
     962                 :             : #define DERIV_AT_LEN  4
     963                 :             : typedef struct tagDerivAttachment {
     964                 :             :     short typ[DERIV_AT_LEN]; /* changed from char to short on 2013-11-13 DT */
     965                 :             :     char  ord[DERIV_AT_LEN]; /* ring: neighbor in precursor; chain: neighbor in derivatizing agent */
     966                 :             :     char  num[DERIV_AT_LEN]; /* num. atoms to remove */
     967                 :             : #ifdef DERIV_RING_DMOX_DEOX
     968                 :             :     AT_NUMB other_atom;      /* other atno+1; for DERIV_RING_DMOX_DEOX */
     969                 :             : #endif
     970                 :             : } DERIV_AT;
     971                 :             : 
     972                 :             : /* return value */
     973                 :             : #define DERIV_NOT       0x1000   /* cannot be a derivatization agent atom */
     974                 :             : 
     975                 :             : #define MAX_AT_DERIV      13              /* max. num of heavy atoms in removed derivatizing agent: RO-C(O)PheF5 */
     976                 :             : #define NOT_AT_DERIV      99              /* DERIV_AT::num; rejected as > MAX_AT_DERIV */
     977                 :             : #define MIN_AT_LEFT_DERIV 2               /* was 3 before 2013-11-12; min num heavy atoms in derivative precursor = MIN_AT_LEFT_DERIV-1 */
     978                 :             : 
     979                 :             : #define NO_ORD_VAL        0x0037          /* not used */
     980                 :             : 
     981                 :             : #define CFLAG_MARK_BRANCH      1          /* for main derivative traversal */
     982                 :             : #define CFLAG_MARK_BLOCK       2          /* for block detection */
     983                 :             : #define CFLAG_MARK_BLOCK_INV   ((char)~(CFLAG_MARK_BLOCK)) /* for block detection */
     984                 :             : #define COUNT_ALL_NOT_DERIV    1      /* 1=> count ALL atoms that are not in deriv. agents */
     985                 :             : /* 0=> only atoms that are not in DERIV_RING_OUTSIDE_PRECURSOR */
     986                 :             : #define IS_DA_NUM_LE(DA, I, MX)  (((DA)->typ[I] && ((DA)->typ[I] & DERIV_UNEXPADABLE) == (DA)->typ[I]) || (DA)->num[I] <= (MX))
     987                 :             : 
     988                 :             : 
     989                 :             : /* derivative classes */
     990                 :             : typedef enum tagDerivId {
     991                 :             :     DERIV_ID_Acentonate,
     992                 :             :     DERIV_ID_Benzlidene,
     993                 :             :     DERIV_ID_BenzOX,
     994                 :             :     DERIV_ID_BuBorate,
     995                 :             :     DERIV_ID_Dansyl,
     996                 :             :     DERIV_ID_DEOX,
     997                 :             :     DERIV_ID_DMOX,
     998                 :             :     DERIV_ID_EtBorate,
     999                 :             :     DERIV_ID_EtOX,
    1000                 :             :     DERIV_ID_HFB,
    1001                 :             :     DERIV_ID_MeBorate,
    1002                 :             :     DERIV_ID_MOX,
    1003                 :             :     DERIV_ID_PFB,
    1004                 :             :     DERIV_ID_PFP,
    1005                 :             :     DERIV_ID_Piperidine,
    1006                 :             :     DERIV_ID_Pyrrolidide,
    1007                 :             :     DERIV_ID_TBDMS,
    1008                 :             :     DERIV_ID_TFA,
    1009                 :             :     DERIV_ID_TMS,
    1010                 :             :     DERIV_ID_Unknown,
    1011                 :             : 
    1012                 :             : 
    1013                 :             : #if defined(UNDERIV_RN_AcMe) || defined(UNDERIV_RNH_AcMe) || defined(UNDERIV_RO_COX_Me)
    1014                 :             :     DERIV_ID_Acetate,
    1015                 :             : #endif
    1016                 :             : #if defined(UNDERIV_RO_COX_BENZOATES)
    1017                 :             :     DERIV_ID_Benzoate,
    1018                 :             : #endif
    1019                 :             : 
    1020                 :             : 
    1021                 :             : #ifdef UNDERIV_ACETATE_Me
    1022                 :             :     DERIV_ID_Methylation,
    1023                 :             : #endif
    1024                 :             : #ifdef UNDERIV_ACETATE_Et
    1025                 :             :     DERIV_ID_Ethylation,
    1026                 :             : #endif
    1027                 :             : #if defined(UNDERIV_RN_AcEt) || defined(UNDERIV_RNH_AcEt) || defined(UNDERIV_RO_COX_Et)
    1028                 :             :     DERIV_ID_Propanoate,
    1029                 :             : #endif
    1030                 :             : 
    1031                 :             : 
    1032                 :             : } DerivId;
    1033                 :             : 
    1034                 :             : #ifdef PTR_TO_DERIV_NAMES
    1035                 :             : extern const char **pszDerivName;
    1036                 :             : #else
    1037                 :             : const char *pszDerivName[] = {
    1038                 :             :     "Acentonate",      /*  DERIV_ID_Acentonate, 00 */
    1039                 :             :     "Benzlidene",      /*  DERIV_ID_Benzlidene, 01 */
    1040                 :             :     "BenzOX",          /*  DERIV_ID_BenzOX,     02 */
    1041                 :             :     "BuBorate",        /*  DERIV_ID_BuBorate,   03 */
    1042                 :             :     "Dansyl",          /*  DERIV_ID_Dansyl,     04 */
    1043                 :             :     "DEOX",            /*  DERIV_ID_DEOX,       05 */
    1044                 :             :     "DMOX",            /*  DERIV_ID_DMOX,       06 */
    1045                 :             :     "EtBorate",        /*  DERIV_ID_EtBorate,   07 */
    1046                 :             :     "EtOX",            /*  DERIV_ID_EtOX,       08 */
    1047                 :             :     "HFB",             /*  DERIV_ID_HFB,        09 */
    1048                 :             :     "MeBorate",        /*  DERIV_ID_MeBorate,   10 */
    1049                 :             :     "MOX",             /*  DERIV_ID_MOX,        11 */
    1050                 :             :     "PFB",             /*  DERIV_ID_PFB,        12 */
    1051                 :             :     "PFP",             /*  DERIV_ID_PFP,        13 */
    1052                 :             :     "Piperidine",      /*  DERIV_ID_Piperidine, 14 */
    1053                 :             :     "Pyrrolidide",     /*  DERIV_ID_Pyrrolidide,15 */
    1054                 :             :     "TBDMS",           /*  DERIV_ID_TBDMS,      16 */
    1055                 :             :     "TFA",             /*  DERIV_ID_TFA,        17 */
    1056                 :             :     "TMS",             /*  DERIV_ID_TMS,        18 */
    1057                 :             :     "???",             /*  DERIV_ID_Unknown,    19 */
    1058                 :             : 
    1059                 :             : 
    1060                 :             : #if defined(UNDERIV_RN_AcMe) || defined(UNDERIV_RNH_AcMe) || defined(UNDERIV_RO_COX_Me)
    1061                 :             :     "Acetate",         /* DERIV_ID_Acetate,     20 */
    1062                 :             : #endif
    1063                 :             : #if defined(UNDERIV_RO_COX_BENZOATES)
    1064                 :             :     "Benzoate",        /* DERIV_ID_Benzoate,    21 */
    1065                 :             : #endif
    1066                 :             : 
    1067                 :             :                        /************* disabled ***********************/
    1068                 :             : 
    1069                 :             : #ifdef UNDERIV_ACETATE_Me
    1070                 :             :                        "Methylation",     /* DERIV_ID_Methylation   */
    1071                 :             : #endif
    1072                 :             : #ifdef UNDERIV_ACETATE_Et
    1073                 :             :                        "Ethylation",      /* DERIV_ID_Ethylation    */
    1074                 :             : #endif
    1075                 :             : #if defined(UNDERIV_RN_AcEt) || defined(UNDERIV_RNH_AcEt) || defined(UNDERIV_RO_COX_Et)
    1076                 :             :                        "Propanoate",      /* DERIV_ID_Propanoate    */
    1077                 :             : #endif
    1078                 :             : 
    1079                 :             :                        "",  /* for linear searching */
    1080                 :             : };
    1081                 :             : #endif /* PTR_TO_DERIV_NAMES */
    1082                 :             : 
    1083                 :             : typedef enum tagDerivBit {
    1084                 :             :     DERIV_BIT_Acentonate = 1 << DERIV_ID_Acentonate,
    1085                 :             :     DERIV_BIT_Benzlidene = 1 << DERIV_ID_Benzlidene,
    1086                 :             :     DERIV_BIT_BenzOX = 1 << DERIV_ID_BenzOX,
    1087                 :             :     DERIV_BIT_BuBorate = 1 << DERIV_ID_BuBorate,
    1088                 :             :     DERIV_BIT_Dansyl = 1 << DERIV_ID_Dansyl,
    1089                 :             :     DERIV_BIT_DEOX = 1 << DERIV_ID_DEOX,
    1090                 :             :     DERIV_BIT_DMOX = 1 << DERIV_ID_DMOX,
    1091                 :             :     DERIV_BIT_EtBorate = 1 << DERIV_ID_EtBorate,
    1092                 :             :     DERIV_BIT_EtOX = 1 << DERIV_ID_EtOX,
    1093                 :             :     DERIV_BIT_HFB = 1 << DERIV_ID_HFB,
    1094                 :             :     DERIV_BIT_MeBorate = 1 << DERIV_ID_MeBorate,
    1095                 :             :     DERIV_BIT_MOX = 1 << DERIV_ID_MOX,
    1096                 :             :     DERIV_BIT_PFB = 1 << DERIV_ID_PFB,
    1097                 :             :     DERIV_BIT_PFP = 1 << DERIV_ID_PFP,
    1098                 :             :     DERIV_BIT_Piperidine = 1 << DERIV_ID_Piperidine,
    1099                 :             :     DERIV_BIT_Pyrrolidide = 1 << DERIV_ID_Pyrrolidide,
    1100                 :             :     DERIV_BIT_TBDMS = 1 << DERIV_ID_TBDMS,
    1101                 :             :     DERIV_BIT_TFA = 1 << DERIV_ID_TFA,
    1102                 :             :     DERIV_BIT_TMS = 1 << DERIV_ID_TMS,
    1103                 :             :     DERIV_BIT_Unknown = 1 << DERIV_ID_Unknown,
    1104                 :             : 
    1105                 :             : #if defined(UNDERIV_RN_AcMe) || defined(UNDERIV_RNH_AcMe) || defined(UNDERIV_RO_COX_Me)
    1106                 :             :     DERIV_BIT_Acetate = 1 << DERIV_ID_Acetate,
    1107                 :             : #endif
    1108                 :             : #if defined(UNDERIV_RO_COX_BENZOATES)
    1109                 :             :     DERIV_BIT_Benzoate = 1 << DERIV_ID_Benzoate,
    1110                 :             : #endif
    1111                 :             : 
    1112                 :             : 
    1113                 :             : #ifdef UNDERIV_ACETATE_Me
    1114                 :             :     DERIV_BIT_Methylation = 1 << DERIV_ID_Methylation,
    1115                 :             : #endif
    1116                 :             : #ifdef UNDERIV_ACETATE_Et
    1117                 :             :     DERIV_BIT_Ethylation = 1 << DERIV_ID_Ethylation,
    1118                 :             : #endif
    1119                 :             : #if defined(UNDERIV_RN_AcEt) || defined(UNDERIV_RNH_AcEt) || defined(UNDERIV_RO_COX_Et)
    1120                 :             :     DERIV_BIT_Propanoate = 1 << DERIV_ID_Propanoate,
    1121                 :             : #endif
    1122                 :             : 
    1123                 :             : 
    1124                 :             :     DERIV_ID_NUMBER,
    1125                 :             : } DerivBit;
    1126                 :             : 
    1127                 :             : typedef int BIT_UNDERIV;
    1128                 :             : 
    1129                 :             : int mark_atoms_cFlags( inp_ATOM *at,
    1130                 :             :                        int start,
    1131                 :             :                        int num,
    1132                 :             :                        char cFlags );
    1133                 :             : int unmark_atoms_cFlags( inp_ATOM *at,
    1134                 :             :                          int start,
    1135                 :             :                          int num,
    1136                 :             :                          char cFlags,
    1137                 :             :                          char cInvFlags );
    1138                 :             : 
    1139                 :             : int is_C_or_S_DB_O( inp_ATOM *at, int i );
    1140                 :             : int is_C_DB_O( inp_ATOM *at, int i );
    1141                 :             : int is_Saturated_C( inp_ATOM *at, int attachment_pont );
    1142                 :             : int is_C_Alk( inp_ATOM *at, int i, char cFlags );
    1143                 :             : 
    1144                 :             : #ifdef DERIV_X_OXIME
    1145                 :             : int is_Phenyl( inp_ATOM *at,
    1146                 :             :                int outside_point,
    1147                 :             :                int attachment_point );
    1148                 :             : int is_PentaFluoroPhenyl( inp_ATOM *at,
    1149                 :             :                           int outside_point,
    1150                 :             :                           int attachment_point );
    1151                 :             : #endif
    1152                 :             : 
    1153                 :             : int is_Methyl( inp_ATOM *at,
    1154                 :             :                int attachment_point );
    1155                 :             : int is_Ethyl( inp_ATOM *at,
    1156                 :             :               int outside_point,
    1157                 :             :               int attachment_point );
    1158                 :             : int is_Methyl_or_Etyl( inp_ATOM *at,
    1159                 :             :                        int outside_point,
    1160                 :             :                        int attachment_point );
    1161                 :             : int is_Si_IV( inp_ATOM *at, int i );
    1162                 :             : int is_P_TB_N( inp_ATOM *at, int i );
    1163                 :             : 
    1164                 :             : #if( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) || defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
    1165                 :             : int is_DERIV_RING2_PRRLDD_PPRDN( inp_ATOM *at,
    1166                 :             :                                  int cur_atom,
    1167                 :             :                                  int from_ord,
    1168                 :             :                                  DERIV_AT *da,
    1169                 :             :                                  DERIV_AT *da1 );
    1170                 :             : #endif
    1171                 :             : int is_Dansyl( inp_ATOM *at,
    1172                 :             :                int cur_atom,
    1173                 :             :                int from_ord,
    1174                 :             :                DERIV_AT *da,
    1175                 :             :                DERIV_AT *da1 );
    1176                 :             : int is_possibly_deriv_neigh( inp_ATOM *at,
    1177                 :             :                              int iat,
    1178                 :             :                              int iord,
    1179                 :             :                              int type,
    1180                 :             :                              char cFlags );
    1181                 :             : int get_traversed_deriv_type( inp_ATOM *at,
    1182                 :             :                               DERIV_AT *da,
    1183                 :             :                               int k, DERIV_AT *da1,
    1184                 :             :                               char cFlags );
    1185                 :             : int add_to_da( DERIV_AT *da, DERIV_AT *add );
    1186                 :             : int mark_atoms_deriv( inp_ATOM *at,
    1187                 :             :                       DERIV_AT *da,
    1188                 :             :                       int start,
    1189                 :             :                       int num,
    1190                 :             :                       char cFlags,
    1191                 :             :                       int *pbFound );
    1192                 :             : int count_one_bond_atoms( inp_ATOM *at,
    1193                 :             :                           DERIV_AT *da,
    1194                 :             :                           int start,
    1195                 :             :                           int ord,
    1196                 :             :                           char cFlags,
    1197                 :             :                           int *bFound );
    1198                 :             : int is_silyl( inp_ATOM *at, int start, int ord_prev );
    1199                 :             : int is_silyl2( inp_ATOM *at, int start, int from_at );
    1200                 :             : int is_Me_or_Et( inp_ATOM *at, int start, int ord_prev );
    1201                 :             : int is_nButyl( inp_ATOM *at, int start, int ord_prev );
    1202                 :             : int is_CF3_or_linC3F7a( inp_ATOM *at, int start, int iat_prev );
    1203                 :             : int is_CF3_or_linC3F7( inp_ATOM *at, int start, int ord_prev );
    1204                 :             : int is_phenyl( inp_ATOM *at, int start, int ord_prev );
    1205                 :             : int is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR( inp_ATOM *at,
    1206                 :             :                                              int start,
    1207                 :             :                                              int num_atoms,
    1208                 :             :                                              DERIV_AT *da1,
    1209                 :             :                                              int idrv,
    1210                 :             :                                              char *szUnderiv,
    1211                 :             :                                              int lenUnderiv,
    1212                 :             :                                              char *szUnderiv2,
    1213                 :             :                                              int lenUnderiv2,
    1214                 :             :                                              BIT_UNDERIV *bitUnderiv );
    1215                 :             : int is_deriv_chain( inp_ATOM *at,
    1216                 :             :                     int start,
    1217                 :             :                     int num_atoms,
    1218                 :             :                     DERIV_AT *da1,
    1219                 :             :                     int idrv,
    1220                 :             :                     char *szUnderiv,
    1221                 :             :                     int lenUnderiv,
    1222                 :             :                     char *szUnderiv2,
    1223                 :             :                     int lenUnderiv2,
    1224                 :             :                     BIT_UNDERIV *bitUnderiv );
    1225                 :             : int is_deriv_chain2( inp_ATOM *at,
    1226                 :             :                      int start,
    1227                 :             :                      int type,
    1228                 :             :                      int num,
    1229                 :             :                      int ord,
    1230                 :             :                      int idrv,
    1231                 :             :                      char *szUnderiv,
    1232                 :             :                      int lenUnderiv,
    1233                 :             :                      char *szUnderiv2,
    1234                 :             :                      int lenUnderiv2,
    1235                 :             :                      BIT_UNDERIV *bitUnderiv );
    1236                 :             : int is_deriv_chain_or_ring( inp_ATOM *at,
    1237                 :             :                             int start,
    1238                 :             :                             int num_atoms,
    1239                 :             :                             DERIV_AT *da1,
    1240                 :             :                             int *idrv );
    1241                 :             : 
    1242                 :             : int remove_deriv( DERIV_AT *da1, int idrv );
    1243                 :             : int remove_deriv_mark( DERIV_AT *da1, int idrv );
    1244                 :             : int underiv_compare( const void *p1, const void *p2 );
    1245                 :             : int underiv_list_add_two_cuts( char *szUnderivList,
    1246                 :             :                                int lenUnderivList,
    1247                 :             :                                char *szUnderiv,
    1248                 :             :                                const char cDelim );
    1249                 :             : int sort_merge_underiv( char *pSdfValue,
    1250                 :             :                         int bOutputSdf,
    1251                 :             :                         char *szUnderivList,
    1252                 :             :                         char cDerivSeparator,
    1253                 :             :                         const char *pszUnderivPrefix,
    1254                 :             :                         const char *pszUnderivPostfix );
    1255                 :             : int eliminate_deriv_not_in_list( inp_ATOM *at,
    1256                 :             :                                  DERIV_AT *da,
    1257                 :             :                                  int num_atoms,
    1258                 :             :                                  char *szUnderivList,
    1259                 :             :                                  int lenUnderivList,
    1260                 :             :                                  char *szUnderivList2,
    1261                 :             :                                  int lenUnderivList2,
    1262                 :             :                                  BIT_UNDERIV *bitUnderivList );
    1263                 :             : void underiv_buf_clear( char *szUnderiv );
    1264                 :             : int underiv_list_add( char *szUnderivList,
    1265                 :             :                       int lenUnderivList,
    1266                 :             :                       const char *szUnderiv,
    1267                 :             :                       char cDelimiter );
    1268                 :             : const char* underiv_list_get_last( const char *szUnderivList,
    1269                 :             :                                    char cDelimiter );
    1270                 :             : int make_single_cut( inp_ATOM *at,
    1271                 :             :                      DERIV_AT *da,
    1272                 :             :                      int iat,
    1273                 :             :                      int icut );
    1274                 :             : int fill_out_bond_cuts( inp_ATOM *at,
    1275                 :             :                         DERIV_AT *da,
    1276                 :             :                         int num_atoms,
    1277                 :             :                         R2C_ATPAIR *ap,
    1278                 :             :                         int num_cuts_to_check );
    1279                 :             : int mark_deriv_agents( inp_ATOM *at,
    1280                 :             :                        DERIV_AT *da,
    1281                 :             :                        int num_atoms,
    1282                 :             :                        R2C_ATPAIR *ap,
    1283                 :             :                        int num_cuts_to_check,
    1284                 :             :                        AT_NUMB *pnum_comp,
    1285                 :             :                        int *pcur_num_at );
    1286                 :             : int add_explicit_H( INP_ATOM_DATA *inp_cur_data );
    1287                 :             : void free_underiv_temp_data( R2C_ATPAIR *ap,
    1288                 :             :                              DERIV_AT *da,
    1289                 :             :                              inp_ATOM *at2,
    1290                 :             :                              INP_ATOM_DATA *inp_cur_data,
    1291                 :             :                              int num_components );
    1292                 :             : void remove_cut_derivs( int num_atoms,
    1293                 :             :                         inp_ATOM *at,
    1294                 :             :                         INP_ATOM_DATA *inp_cur_data,
    1295                 :             :                         int i_component,
    1296                 :             :                         int *errcode );
    1297                 :             : 
    1298                 :             : 
    1299                 :             : /****************************************************************************
    1300                 :             : DFS search for atoms that do not have a flag
    1301                 :             : ****************************************************************************/
    1302                 :           0 : int mark_atoms_cFlags( inp_ATOM *at, int start, int num, char cFlags )
    1303                 :             : {
    1304         [ #  # ]:           0 :     if (!( at[start].cFlags & cFlags ))
    1305                 :             :     {
    1306                 :             :         int i;
    1307                 :           0 :         at[start].cFlags |= cFlags;
    1308                 :           0 :         num++;
    1309         [ #  # ]:           0 :         for (i = 0; i < at[start].valence; i++)
    1310                 :             :         {
    1311                 :           0 :             num = mark_atoms_cFlags( at, at[start].neighbor[i], num, cFlags );
    1312                 :             :         }
    1313                 :             :     }
    1314                 :             : 
    1315                 :           0 :     return num; /* number of atoms traversed forward from at[start] */
    1316                 :             : }
    1317                 :             : 
    1318                 :             : 
    1319                 :             : /****************************************************************************
    1320                 :             : DFS search for atoms that do have a flag
    1321                 :             : ****************************************************************************/
    1322                 :           0 : int unmark_atoms_cFlags( inp_ATOM *at,
    1323                 :             :                          int start,
    1324                 :             :                          int num,
    1325                 :             :                          char cFlags,
    1326                 :             :                          char cInvFlags )
    1327                 :             : {
    1328         [ #  # ]:           0 :     if (at[start].cFlags & cFlags)
    1329                 :             :     {
    1330                 :             :         int i;
    1331                 :           0 :         at[start].cFlags &= cInvFlags;
    1332                 :           0 :         num++;
    1333         [ #  # ]:           0 :         for (i = 0; i < at[start].valence; i++)
    1334                 :             :         {
    1335                 :           0 :             num = unmark_atoms_cFlags( at, at[start].neighbor[i], num, cFlags, cInvFlags );
    1336                 :             :         }
    1337                 :             :     }
    1338                 :             : 
    1339                 :           0 :     return num; /* number of atoms traversed forward from at[start] */
    1340                 :             : }
    1341                 :             : 
    1342                 :             : 
    1343                 :             : /****************************************************************************/
    1344                 :           0 : int is_C_or_S_DB_O( inp_ATOM *at, int i )
    1345                 :             : {
    1346                 :             :     int j, neigh;
    1347         [ #  # ]:           0 :     if ((at[i].el_number != EL_NUMBER_C &&
    1348         [ #  # ]:           0 :          at[i].el_number != EL_NUMBER_S) ||
    1349   [ #  #  #  # ]:           0 :          at[i].charge || at[i].radical) /* djb-rwth: addressing LLVM warning */
    1350                 :           0 :         return 0;
    1351         [ #  # ]:           0 :     for (j = 0; j < at[i].valence; j++)
    1352                 :             :     {
    1353                 :           0 :         neigh = at[i].neighbor[j];
    1354         [ #  # ]:           0 :         if (( at[neigh].el_number == EL_NUMBER_O ||
    1355         [ #  # ]:           0 :               at[neigh].el_number == EL_NUMBER_S ) &&
    1356   [ #  #  #  # ]:           0 :              !at[neigh].num_H && 1 == at[neigh].valence &&
    1357         [ #  # ]:           0 :              2 == at[neigh].chem_bonds_valence)
    1358                 :             :         {
    1359                 :           0 :             return 1;
    1360                 :             :         }
    1361                 :             :     }
    1362                 :             : 
    1363                 :           0 :     return 0;
    1364                 :             : }
    1365                 :             : 
    1366                 :             : 
    1367                 :             : /****************************************************************************/
    1368                 :           0 : int is_C_DB_O( inp_ATOM *at, int i )
    1369                 :             : {
    1370                 :             :     int j, neigh;
    1371         [ #  # ]:           0 :     if (at[i].el_number != EL_NUMBER_C ||
    1372   [ #  #  #  # ]:           0 :          at[i].charge || at[i].radical ||
    1373   [ #  #  #  # ]:           0 :          at[i].valence != 3 || at[i].chem_bonds_valence != 4)
    1374                 :           0 :         return 0;
    1375         [ #  # ]:           0 :     for (j = 0; j < at[i].valence; j++)
    1376                 :             :     {
    1377                 :           0 :         neigh = at[i].neighbor[j];
    1378         [ #  # ]:           0 :         if (( at[neigh].el_number == EL_NUMBER_O ) &&
    1379   [ #  #  #  # ]:           0 :              !at[neigh].num_H && 1 == at[neigh].valence &&
    1380         [ #  # ]:           0 :              2 == at[neigh].chem_bonds_valence)
    1381                 :             :         {
    1382                 :           0 :             return j + 1; /* =O ord */
    1383                 :             :         }
    1384                 :             :     }
    1385                 :             : 
    1386                 :           0 :     return 0;
    1387                 :             : }
    1388                 :             : 
    1389                 :             : 
    1390                 :             : /****************************************************************************/
    1391                 :           0 : int is_Saturated_C( inp_ATOM *at, int attachment_pont )
    1392                 :             : {
    1393         [ #  # ]:           0 :     return ( at[attachment_pont].el_number == EL_NUMBER_C &&
    1394         [ #  # ]:           0 :              at[attachment_pont].valence == at[attachment_pont].chem_bonds_valence );
    1395                 :             : }
    1396                 :             : 
    1397                 :             : 
    1398                 :             : /****************************************************************************/
    1399                 :           0 : int is_C_Alk( inp_ATOM *at, int i, char cFlags )
    1400                 :             : {
    1401         [ #  # ]:           0 :     if (at[i].el_number == EL_NUMBER_C &&
    1402         [ #  # ]:           0 :          at[i].valence == at[i].chem_bonds_valence)
    1403                 :             :     {
    1404                 :             :         int j, k;
    1405                 :             :         U_CHAR el;
    1406         [ #  # ]:           0 :         for (j = 0; j < at[i].valence; j++)
    1407                 :             :         {
    1408                 :           0 :             k = at[i].neighbor[j];
    1409         [ #  # ]:           0 :             if (at[k].cFlags & cFlags)
    1410                 :           0 :                 continue;
    1411                 :           0 :             el = at[k].el_number;
    1412   [ #  #  #  # ]:           0 :             if (el != EL_NUMBER_C &&
    1413         [ #  # ]:           0 :                  el != EL_NUMBER_F &&
    1414         [ #  # ]:           0 :                  el != EL_NUMBER_CL &&
    1415         [ #  # ]:           0 :                  el != EL_NUMBER_BR &&
    1416                 :             :                  el != EL_NUMBER_I)
    1417                 :             :             {
    1418                 :           0 :                 return 0;
    1419                 :             :             }
    1420                 :             :         }
    1421                 :           0 :         return 1;
    1422                 :             :     }
    1423                 :             : 
    1424                 :           0 :     return 0;
    1425                 :             : }
    1426                 :             : 
    1427                 :             : 
    1428                 :             : #if( defined(DERIV_X_OXIME) || defined(DERIV_RO_COX) || defined(DERIV_DANSYL) )
    1429                 :             : 
    1430                 :             : 
    1431                 :             : /****************************************************************************
    1432                 :             : CH -- CH
    1433                 :             : /        \
    1434                 :             : (outside point)-(attachment point, C)          CH
    1435                 :             : \        /
    1436                 :             : CH -- CH
    1437                 :             : note: bond types in the ring are not checked,
    1438                 :             : we check num_H, valence, charge, radical, RingSystem,
    1439                 :             : nNumAtInRingSystem, bCutVertex
    1440                 :             : ****************************************************************************/
    1441                 :           0 : int is_Phenyl( inp_ATOM *at, int outside_point, int attachment_point )
    1442                 :             : {
    1443                 :             :     int iNext, iCur, iNewNext, k;
    1444                 :             : 
    1445         [ #  # ]:           0 :     if (at[attachment_point].el_number == EL_NUMBER_C &&
    1446         [ #  # ]:           0 :          at[attachment_point].valence == 3 && /*at[attachment_point].chem_bonds_valence == 4 &&*/
    1447   [ #  #  #  #  :           0 :          !at[attachment_point].num_H && !at[attachment_point].charge && !at[attachment_point].radical &&
                   #  # ]
    1448         [ #  # ]:           0 :          at[attachment_point].nRingSystem != at[outside_point].nRingSystem &&
    1449   [ #  #  #  # ]:           0 :          at[attachment_point].bCutVertex && at[attachment_point].nNumAtInRingSystem == 6)
    1450                 :             :     {
    1451                 :             : 
    1452         [ #  # ]:           0 :         for (iNext = 0; iNext < at[attachment_point].valence; iNext++)
    1453                 :             :         {
    1454         [ #  # ]:           0 :             if (at[attachment_point].neighbor[iNext] != outside_point)
    1455                 :             :             {
    1456                 :           0 :                 break;
    1457                 :             :             }
    1458                 :             :         }
    1459         [ #  # ]:           0 :         if (iNext == at[attachment_point].valence)
    1460                 :             :         {
    1461                 :           0 :             return 0; /* program error*/
    1462                 :             :         }
    1463                 :           0 :         iCur = attachment_point;
    1464                 :           0 :         iNext = at[attachment_point].neighbor[iNext];
    1465         [ #  # ]:           0 :         for (k = 0; k < 5; k++)
    1466                 :             :         {
    1467                 :             :             /* here we do not check bond type in the aromatic ring */
    1468   [ #  #  #  #  :           0 :             if (at[iNext].el_number != EL_NUMBER_C || at[iNext].valence != 2 || at[iNext].num_H != 1 || at[iNext].charge || at[iNext].radical)
          #  #  #  #  #  
                      # ]
    1469                 :             :             {
    1470                 :           0 :                 return 0;
    1471                 :             :             }
    1472                 :           0 :             iNewNext = at[iNext].neighbor[at[iNext].neighbor[0] == iCur];
    1473                 :           0 :             iCur = iNext;
    1474                 :           0 :             iNext = iNewNext;
    1475                 :             :         }
    1476                 :           0 :         return ( iNext == attachment_point );
    1477                 :             :     }
    1478                 :             : 
    1479                 :           0 :     return 0;
    1480                 :             : }
    1481                 :             : 
    1482                 :             : 
    1483                 :             : /****************************************************************************
    1484                 :             : F     F
    1485                 :             : |     |
    1486                 :             : C --- C
    1487                 :             : /        \
    1488                 :             : (outside point)-(attachment point, C)          C---F
    1489                 :             : \        /
    1490                 :             : C --- C
    1491                 :             : |     |
    1492                 :             : F     F
    1493                 :             : note: bond types in the ring are not checked,
    1494                 :             : we check num_H, valence, charge, radical, RingSystem,
    1495                 :             : nNumAtInRingSystem, bCutVertex
    1496                 :             : ****************************************************************************/
    1497                 :           0 : int is_PentaFluoroPhenyl( inp_ATOM *at,
    1498                 :             :                           int outside_point,
    1499                 :             :                           int attachment_point )
    1500                 :             : {
    1501                 :             :     int iNext, iCur, iNewNext, nF, k, i, neigh;
    1502                 :             : 
    1503         [ #  # ]:           0 :     if (at[attachment_point].el_number == EL_NUMBER_C &&
    1504         [ #  # ]:           0 :          at[attachment_point].valence == 3 && /*at[attachment_point].chem_bonds_valence == 4 &&*/
    1505   [ #  #  #  #  :           0 :          !at[attachment_point].num_H && !at[attachment_point].charge && !at[attachment_point].radical &&
                   #  # ]
    1506         [ #  # ]:           0 :          at[attachment_point].nRingSystem != at[outside_point].nRingSystem &&
    1507   [ #  #  #  # ]:           0 :          at[attachment_point].bCutVertex && at[attachment_point].nNumAtInRingSystem == 6)
    1508                 :             :     {
    1509                 :             : 
    1510         [ #  # ]:           0 :         for (iNext = 0; iNext < at[attachment_point].valence; iNext++)
    1511                 :             :         {
    1512         [ #  # ]:           0 :             if (at[attachment_point].neighbor[iNext] != outside_point)
    1513                 :           0 :                 break;
    1514                 :             :         }
    1515         [ #  # ]:           0 :         if (iNext == at[attachment_point].valence)
    1516                 :           0 :             return 0; /* program error*/
    1517                 :           0 :         iCur = attachment_point;
    1518                 :           0 :         iNext = at[attachment_point].neighbor[iNext];
    1519         [ #  # ]:           0 :         for (k = 0; k < 5; k++)
    1520                 :             :         {
    1521                 :             :             /* here we do not check bond type in the aromatic ring */
    1522   [ #  #  #  #  :           0 :             if (at[iNext].el_number != EL_NUMBER_C || at[iNext].valence != 3 || at[iNext].num_H != 0 || at[iNext].charge || at[iNext].radical)
          #  #  #  #  #  
                      # ]
    1523                 :           0 :                 return 0;
    1524         [ #  # ]:           0 :             for (i = 0, nF = 0, iNewNext = -1; i < at[iNext].valence; i++)
    1525                 :             :             {
    1526                 :           0 :                 neigh = at[iNext].neighbor[i];
    1527         [ #  # ]:           0 :                 if (neigh == iCur)
    1528                 :             :                 {
    1529                 :             :                     ;
    1530                 :             :                 }
    1531                 :             :                 else
    1532   [ #  #  #  #  :           0 :                     if (at[neigh].el_number == EL_NUMBER_F && at[neigh].chem_bonds_valence == 1 && !at[neigh].charge && !at[neigh].radical && !at[neigh].num_H)
          #  #  #  #  #  
                      # ]
    1533                 :             :                     {
    1534                 :           0 :                         nF++; /* terminal flourine */
    1535                 :             :                     }
    1536                 :             :                     else
    1537         [ #  # ]:           0 :                         if (iNewNext == -1)
    1538                 :             :                         {
    1539                 :           0 :                             iNewNext = neigh; /* Carbon will be checked on the next pass */
    1540                 :             :                         }
    1541                 :             :                         else
    1542                 :             :                         {
    1543                 :           0 :                             return 0;
    1544                 :             :                         }
    1545                 :             :             }
    1546   [ #  #  #  # ]:           0 :             if (iNewNext == -1 || nF != 1)
    1547                 :             :             {
    1548                 :           0 :                 return 0;
    1549                 :             :             }
    1550                 :           0 :             iCur = iNext;
    1551                 :           0 :             iNext = iNewNext;
    1552                 :             :         }
    1553                 :           0 :         return ( iNext == attachment_point );
    1554                 :             :     }
    1555                 :             : 
    1556                 :           0 :     return 0;
    1557                 :             : }
    1558                 :             : 
    1559                 :             : 
    1560                 :             : /****************************************************************************/
    1561                 :           0 : int is_Methyl( inp_ATOM *at, int attachment_point )
    1562                 :             : {
    1563   [ #  #  #  # ]:           0 :     if (at[attachment_point].valence == 1 && at[attachment_point].chem_bonds_valence == 1 &&
    1564   [ #  #  #  # ]:           0 :          at[attachment_point].el_number == EL_NUMBER_C && at[attachment_point].num_H == 3 &&
    1565   [ #  #  #  # ]:           0 :          !at[attachment_point].charge && !at[attachment_point].radical)
    1566                 :             :     {
    1567                 :             :         /* methyl */
    1568                 :           0 :         return 1;
    1569                 :             :     }
    1570                 :             : 
    1571                 :           0 :     return 0;
    1572                 :             : }
    1573                 :             : 
    1574                 :             : 
    1575                 :             : /****************************************************************************/
    1576                 :           0 : int is_Ethyl( inp_ATOM *at, int outside_point, int attachment_point )
    1577                 :             : {
    1578   [ #  #  #  # ]:           0 :     if (at[attachment_point].valence == 2 && at[attachment_point].chem_bonds_valence == 2 &&
    1579   [ #  #  #  # ]:           0 :          at[attachment_point].el_number == EL_NUMBER_C && at[attachment_point].num_H == 2 &&
    1580   [ #  #  #  # ]:           0 :          !at[attachment_point].charge && !at[attachment_point].radical)
    1581                 :             :     {
    1582                 :             :         /* methanediyl */
    1583                 :           0 :         int iat_methyl = at[attachment_point].neighbor[( at[attachment_point].neighbor[0] == outside_point )];
    1584                 :           0 :         return is_Methyl( at, iat_methyl );
    1585                 :             :     }
    1586                 :             : 
    1587                 :           0 :     return 0;
    1588                 :             : }
    1589                 :             : 
    1590                 :             : /*****************************************************************************/
    1591                 :           0 : int is_Methyl_or_Etyl( inp_ATOM *at, int outside_point, int attachment_point )
    1592                 :             : {
    1593         [ #  # ]:           0 :     if (is_Methyl( at, attachment_point ))
    1594                 :             :     {
    1595                 :           0 :         return 1;
    1596                 :             :     }
    1597         [ #  # ]:           0 :     if (is_Ethyl( at, outside_point, attachment_point ))
    1598                 :             :     {
    1599                 :           0 :         return 2;
    1600                 :             :     }
    1601                 :             : 
    1602                 :           0 :     return 0;
    1603                 :             : }
    1604                 :             : 
    1605                 :             : #endif /* ( defined(DERIV_X_OXIME) || defined(DERIV_RO_COX) || defined(DERIV_DANSYL) ) */
    1606                 :             : 
    1607                 :             : 
    1608                 :             : /****************************************************************************/
    1609                 :           0 : int is_Si_IV( inp_ATOM *at, int i )
    1610                 :             : {
    1611         [ #  # ]:           0 :     if (at[i].el_number != EL_NUMBER_SI ||
    1612   [ #  #  #  #  :           0 :          at[i].charge || at[i].radical || at[i].valence != 4 || at[i].chem_bonds_valence != 4)
             #  #  #  # ]
    1613                 :             :     {
    1614                 :           0 :         return 0;
    1615                 :             :     }
    1616                 :             : 
    1617                 :           0 :     return 1;
    1618                 :             : }
    1619                 :             : 
    1620                 :             : 
    1621                 :             : /****************************************************************************/
    1622                 :           0 : int is_P_TB_N( inp_ATOM *at, int i )
    1623                 :             : {
    1624                 :             :     int j, k;
    1625   [ #  #  #  # ]:           0 :     if (at[i].el_number != EL_NUMBER_P || at[i].chem_bonds_valence - at[i].valence != 2)
    1626                 :           0 :         return 0;
    1627         [ #  # ]:           0 :     for (j = 0; j < at[i].valence; j++)
    1628                 :             :     {
    1629                 :           0 :         k = at[i].neighbor[j];
    1630   [ #  #  #  #  :           0 :         if (at[k].el_number == EL_NUMBER_N && at[k].valence == 1 && at[k].chem_bonds_valence == 3)
                   #  # ]
    1631                 :             :         {
    1632                 :           0 :             return 1;
    1633                 :             :         }
    1634                 :             :     }
    1635                 :             : 
    1636                 :           0 :     return 0;
    1637                 :             : }
    1638                 :             : 
    1639                 :             : 
    1640                 :             : /****************************************************************************
    1641                 :             : X
    1642                 :             : ||
    1643                 :             : [iat](iord)--C--(iord_opposite)[iat_oppposite]
    1644                 :             : ****************************************************************************/
    1645                 :           0 : int get_CO_opposite( inp_ATOM *at,
    1646                 :             :                      int iat,
    1647                 :             :                      int iord,
    1648                 :             :                      int *iat_opposite,
    1649                 :             :                      int *iord_opposite )
    1650                 :             : {
    1651                 :           0 :     int i, iOpp, iC = at[iat].neighbor[iord];
    1652   [ #  #  #  #  :           0 :     if (at[iat].bond_type[iord] == BOND_SINGLE && 3 == at[iC].valence && 4 == at[iC].chem_bonds_valence)
                   #  # ]
    1653                 :             :     {
    1654                 :             :         /* scan neighbors of iC */
    1655         [ #  # ]:           0 :         for (i = 0; i < at[iC].valence; i++)
    1656                 :             :         {
    1657   [ #  #  #  # ]:           0 :             if (iat != at[iC].neighbor[i] && BOND_SINGLE == at[iC].bond_type[i])
    1658                 :             :             {
    1659                 :           0 :                 iOpp = *iat_opposite = at[iC].neighbor[i];
    1660                 :           0 :                 goto scan_opposite_atom;
    1661                 :             :             }
    1662                 :             :         }
    1663                 :           0 :         return 0; /* failed */
    1664                 :           0 :     scan_opposite_atom:
    1665         [ #  # ]:           0 :         for (i = 0; i < at[iOpp].valence; i++)
    1666                 :             :         {
    1667         [ #  # ]:           0 :             if (iC == at[iOpp].neighbor[i])
    1668                 :             :             {
    1669                 :           0 :                 *iord_opposite = i;
    1670                 :           0 :                 return 1; /* success */
    1671                 :             :             }
    1672                 :             :         }
    1673                 :             :     }
    1674                 :             : 
    1675                 :           0 :     return 0; /* failed */
    1676                 :             : }
    1677                 :             : 
    1678                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    1679                 :             : 
    1680                 :             : #define OX_RING_SIZE 5
    1681                 :             : 
    1682                 :             : 
    1683                 :             : /****************************************************************************/
    1684                 :           0 : int is_DERIV_RING_DMOX_DEOX_O( inp_ATOM *at,
    1685                 :             :                                int cur_atom,
    1686                 :             :                                int from_ord,
    1687                 :             :                                DERIV_AT *da,
    1688                 :             :                                DERIV_AT *da1 )
    1689                 :             : {
    1690                 :             :     /*-----------------------
    1691                 :             :     <-
    1692                 :             :     #4 #3
    1693                 :             :     R--C==N   Me or Et
    1694                 :             :     |   \ /
    1695                 :             :     |    C #2
    1696                 :             :     |   / \
    1697                 :             :     at[k]:O--CH2 Me or ET
    1698                 :             :     #0  #1
    1699                 :             :     ->
    1700                 :             :     --------------------------*/
    1701                 :             :     /*            #0           #1           #2           #3           #4 */
    1702                 :             :     static const U_CHAR bond_type[OX_RING_SIZE] = { BOND_SINGLE, BOND_SINGLE, BOND_SINGLE, BOND_DOUBLE, BOND_SINGLE };
    1703                 :             :     static const S_CHAR valence[OX_RING_SIZE] = { 2,           2,           4,           2,           3 };
    1704                 :             :     static const S_CHAR bonds_valence[OX_RING_SIZE] = { 2,           2,           4,           3,           4 };
    1705                 :             :     static const S_CHAR num_H[OX_RING_SIZE] = { 0,           2,           0,           0,           0 };
    1706                 :             : 
    1707                 :             :     AT_NUMB from, curr, next, nRingSystem, at_no[OX_RING_SIZE];
    1708                 :             :     S_CHAR  bond_no[OX_RING_SIZE];
    1709                 :             :     int     i, n0, n1, attach1, attach2, neigh;
    1710                 :             : 
    1711   [ #  #  #  # ]:           0 :     if (at[cur_atom].el_number == EL_NUMBER_O && at[cur_atom].nNumAtInRingSystem == OX_RING_SIZE)
    1712                 :             :     {
    1713                 :           0 :         AT_NUMB attype[OX_RING_SIZE] = { (AT_NUMB) EL_NUMBER_O, (AT_NUMB) EL_NUMBER_C, (AT_NUMB) EL_NUMBER_C, (AT_NUMB) EL_NUMBER_N, (AT_NUMB) EL_NUMBER_C };
    1714                 :             : 
    1715                 :           0 :         curr = cur_atom;
    1716                 :           0 :         from = at[curr].neighbor[from_ord];
    1717                 :           0 :         nRingSystem = at[curr].nRingSystem;
    1718                 :           0 :         n0 = 0;
    1719                 :             : 
    1720                 :             :         do
    1721                 :             :         {
    1722                 :             :             /* find next atom in a simple ring */
    1723         [ #  # ]:           0 :             for (i = 0; i < at[curr].valence &&
    1724         [ #  # ]:           0 :                 ( from == ( next = at[curr].neighbor[i] ) ||
    1725         [ #  # ]:           0 :                   nRingSystem != at[next].nRingSystem ); i++)
    1726                 :             :             {
    1727                 :             :                 ;
    1728                 :             :             }
    1729         [ #  # ]:           0 :             if (i == at[curr].valence)
    1730                 :             :             {
    1731                 :           0 :                 goto check_next_derivative2;
    1732                 :             :             }
    1733                 :             :             /* check curr atom */
    1734   [ #  #  #  # ]:           0 :             if (at[curr].charge || at[curr].radical)
    1735                 :             :             {
    1736                 :           0 :                 goto check_next_derivative2;
    1737                 :             :             }
    1738         [ #  # ]:           0 :             if (at[curr].bond_type[i] != bond_type[n0] ||
    1739         [ #  # ]:           0 :                  at[curr].valence != valence[n0] ||
    1740         [ #  # ]:           0 :                  at[curr].chem_bonds_valence != bonds_valence[n0] ||
    1741         [ #  # ]:           0 :                  at[curr].num_H != num_H[n0] ||
    1742         [ #  # ]:           0 :                  at[curr].el_number != (U_CHAR) attype[n0])
    1743                 :             :             {
    1744                 :           0 :                 goto check_next_derivative2;
    1745                 :             :             }
    1746                 :             :             /* save current atom */
    1747                 :           0 :             at_no[n0] = curr;
    1748                 :           0 :             bond_no[n0] = i;
    1749                 :             :             /* prepare for the next */
    1750                 :           0 :             from = curr;
    1751                 :           0 :             curr = next;
    1752                 :           0 :             n0++;
    1753   [ #  #  #  # ]:           0 :         } while (n0 < OX_RING_SIZE && curr != cur_atom);
    1754                 :             :         /* check completion */
    1755   [ #  #  #  # ]:           0 :         if (OX_RING_SIZE != n0 || curr != cur_atom)
    1756                 :             :         {
    1757                 :           0 :             goto check_next_derivative2;
    1758                 :             :         }
    1759                 :             :         /* check if R is C */
    1760                 :           0 :         n1 = at_no[4];
    1761         [ #  # ]:           0 :         for (i = 0; i < at[n1].valence; i++)
    1762                 :             :         {
    1763                 :           0 :             neigh = at[n1].neighbor[i];
    1764   [ #  #  #  # ]:           0 :             if (neigh != at_no[0] && neigh != at_no[3])
    1765                 :             :             {
    1766         [ #  # ]:           0 :                 if (at[neigh].el_number != EL_NUMBER_C)
    1767                 :             :                 {
    1768                 :           0 :                     goto check_next_derivative2;
    1769                 :             :                 }
    1770                 :             :                 else
    1771                 :             :                 {
    1772                 :           0 :                     break; /* checked */
    1773                 :             :                 }
    1774                 :             :             }
    1775                 :             :         }
    1776                 :             :         /* check >C< attachments */
    1777                 :           0 :         n1 = at_no[2];
    1778                 :           0 :         attach1 = attach2 = 0;
    1779         [ #  # ]:           0 :         for (i = 0; i < at[n1].valence; i++)
    1780                 :             :         {
    1781         [ #  # ]:           0 :             if (at[n1].neighbor[i] != at_no[1] &&
    1782         [ #  # ]:           0 :                  at[n1].neighbor[i] != at_no[3])
    1783                 :             :             {
    1784         [ #  # ]:           0 :                 if (!attach1)
    1785                 :             :                 {
    1786                 :           0 :                     attach1 = is_Methyl_or_Etyl( at, n1, at[n1].neighbor[i] );
    1787                 :             :                 }
    1788                 :             :                 else
    1789                 :             :                 {
    1790         [ #  # ]:           0 :                     if (!attach2)
    1791                 :             :                     {
    1792                 :           0 :                         attach2 = is_Methyl_or_Etyl( at, n1, at[n1].neighbor[i] );
    1793                 :             :                     }
    1794                 :             :                     else
    1795                 :             :                     {
    1796                 :           0 :                         goto check_next_derivative2;
    1797                 :             :                     }
    1798                 :             :                 }
    1799                 :             :             }
    1800                 :             :         }
    1801   [ #  #  #  # ]:           0 :         if (!attach2 || attach2 != attach1)
    1802                 :             :         {
    1803                 :           0 :             goto check_next_derivative2;
    1804                 :             :         }
    1805                 :             :         /* all checks are done */
    1806         [ #  # ]:           0 :         if ( /*da &&*/ da1)
    1807                 :             :         {
    1808                 :           0 :             short ord_O = bond_no[0];
    1809                 :             :             /*short ord_N = !bond_no[3];*/
    1810                 :           0 :             AT_NUMB iN = at_no[3];
    1811                 :             :             /*AT_NUMB iO  = at_no[0];*/
    1812                 :           0 :             char num_2remove = 2 + attach1 + attach2;
    1813                 :           0 :             da1->typ[0]    /* = da1->typ[1] */ = DERIV_RING_DMOX_DEOX_O;
    1814                 :           0 :             da1->ord[0]    /* = da1->ord[1] */ = ord_O;
    1815                 :           0 :             da1->num[0]    /* = da1->num[1] */ = num_2remove;
    1816                 :           0 :             da1->other_atom = iN + 1;
    1817                 :             :             /*
    1818                 :             :             if ( da1->typ[0] ) {
    1819                 :             :             if ( da1->typ[0]     != DERIV_RING_DMOX_DEOX_O ||
    1820                 :             :             da1->ord[0]     != ord_O ||
    1821                 :             :             da1->num[0]     != num_2remove ||
    1822                 :             :             da1->other_atom != iN ) {
    1823                 :             :             goto check_next_derivative2;
    1824                 :             :             }
    1825                 :             :             } else {
    1826                 :             :             da1->typ[0]     = DERIV_RING_DMOX_DEOX_O;
    1827                 :             :             da1->ord[0]     = ord_O;
    1828                 :             :             da1->num[0]     = num_2remove;
    1829                 :             :             da1->other_atom = iN;
    1830                 :             :             }
    1831                 :             :             */
    1832                 :             :         }
    1833                 :             : 
    1834                 :           0 :         return DERIV_RING_DMOX_DEOX_O;
    1835                 :             :     }
    1836                 :             : 
    1837                 :           0 : check_next_derivative2:;
    1838                 :             : 
    1839                 :           0 :     return 0;
    1840                 :             : }
    1841                 :             : 
    1842                 :             : 
    1843                 :             : /****************************************************************************/
    1844                 :           0 : int is_DERIV_RING_DMOX_DEOX_N( inp_ATOM *at,
    1845                 :             :                                int cur_atom,
    1846                 :             :                                int from_ord,
    1847                 :             :                                DERIV_AT *da,
    1848                 :             :                                DERIV_AT *da1 )
    1849                 :             : {
    1850                 :             :     /*-----------------------
    1851                 :             :     ->
    1852                 :             :     #4 #0
    1853                 :             :     R--C==N   Me or Et
    1854                 :             :     |   \ /
    1855                 :             :     |    C #1
    1856                 :             :     |   / \
    1857                 :             :     at[k]:O--CH2 Me or ET
    1858                 :             :     #3  #2
    1859                 :             :     <-
    1860                 :             :     --------------------------*/
    1861                 :             :     /*            #0           #1           #2           #3           #4 */
    1862                 :             :     static const U_CHAR bond_type[OX_RING_SIZE] = { BOND_SINGLE, BOND_SINGLE, BOND_SINGLE, BOND_SINGLE, BOND_DOUBLE };
    1863                 :             :     static const S_CHAR valence[OX_RING_SIZE] = { 2,           4,           2,           2,           3 };
    1864                 :             :     static const S_CHAR bonds_valence[OX_RING_SIZE] = { 3,           4,           2,           2,           4 };
    1865                 :             :     static const S_CHAR num_H[OX_RING_SIZE] = { 0,           0,           2,           0,           0 };
    1866                 :             : 
    1867                 :             :     AT_NUMB from, curr, next, nRingSystem, at_no[OX_RING_SIZE];
    1868                 :             :     S_CHAR  bond_no[OX_RING_SIZE];
    1869                 :             :     int     i, n0, n1, attach1, attach2, neigh;
    1870                 :             : 
    1871   [ #  #  #  # ]:           0 :     if (at[cur_atom].el_number == EL_NUMBER_N && at[cur_atom].nNumAtInRingSystem == OX_RING_SIZE &&
    1872   [ #  #  #  # ]:           0 :          at[cur_atom].valence == 2 && at[cur_atom].chem_bonds_valence == 3)
    1873                 :             :     {
    1874                 :           0 :         AT_NUMB attype[OX_RING_SIZE] = { (AT_NUMB) EL_NUMBER_N, (AT_NUMB) EL_NUMBER_C, (AT_NUMB) EL_NUMBER_C, (AT_NUMB) EL_NUMBER_O, (AT_NUMB) EL_NUMBER_C };
    1875                 :             : 
    1876                 :           0 :         curr = cur_atom;
    1877                 :           0 :         from = at[curr].neighbor[from_ord];
    1878                 :           0 :         nRingSystem = at[curr].nRingSystem;
    1879                 :           0 :         n0 = 0;
    1880                 :             : 
    1881                 :             :         do
    1882                 :             :         {
    1883                 :             :             /* find next atom in a simple ring */
    1884         [ #  # ]:           0 :             for (i = 0; i < at[curr].valence &&
    1885         [ #  # ]:           0 :                 ( from == ( next = at[curr].neighbor[i] ) ||
    1886         [ #  # ]:           0 :                   nRingSystem != at[next].nRingSystem ); i++)
    1887                 :             :             {
    1888                 :             :                 ;
    1889                 :             :             }
    1890         [ #  # ]:           0 :             if (i == at[curr].valence)
    1891                 :             :             {
    1892                 :           0 :                 goto check_next_derivative2;
    1893                 :             :             }
    1894                 :             :             /* check curr atom */
    1895   [ #  #  #  # ]:           0 :             if (at[curr].charge || at[curr].radical)
    1896                 :             :             {
    1897                 :           0 :                 goto check_next_derivative2;
    1898                 :             :             }
    1899         [ #  # ]:           0 :             if (at[curr].bond_type[i] != bond_type[n0] ||
    1900         [ #  # ]:           0 :                  at[curr].valence != valence[n0] ||
    1901         [ #  # ]:           0 :                  at[curr].chem_bonds_valence != bonds_valence[n0] ||
    1902         [ #  # ]:           0 :                  at[curr].num_H != num_H[n0] ||
    1903         [ #  # ]:           0 :                  at[curr].el_number != (U_CHAR) attype[n0])
    1904                 :             :             {
    1905                 :           0 :                 goto check_next_derivative2;
    1906                 :             :             }
    1907                 :             :             /* save current atom */
    1908                 :           0 :             at_no[n0] = curr;
    1909                 :           0 :             bond_no[n0] = i;
    1910                 :             :             /* prepare for the next */
    1911                 :           0 :             from = curr;
    1912                 :           0 :             curr = next;
    1913                 :           0 :             n0++;
    1914   [ #  #  #  # ]:           0 :         } while (n0 < OX_RING_SIZE && curr != cur_atom);
    1915                 :             : 
    1916                 :             :         /* check completion */
    1917   [ #  #  #  # ]:           0 :         if (OX_RING_SIZE != n0 || curr != cur_atom)
    1918                 :             :         {
    1919                 :           0 :             goto check_next_derivative2;
    1920                 :             :         }
    1921                 :             : 
    1922                 :             :         /* check if R is C */
    1923                 :           0 :         n1 = at_no[4];
    1924         [ #  # ]:           0 :         for (i = 0; i < at[n1].valence; i++)
    1925                 :             :         {
    1926                 :           0 :             neigh = at[n1].neighbor[i];
    1927   [ #  #  #  # ]:           0 :             if (neigh != at_no[0] && neigh != at_no[3])
    1928                 :             :             {
    1929         [ #  # ]:           0 :                 if (at[neigh].el_number != EL_NUMBER_C)
    1930                 :             :                 {
    1931                 :           0 :                     goto check_next_derivative2;
    1932                 :             :                 }
    1933                 :             :                 else
    1934                 :             :                 {
    1935                 :           0 :                     break; /* checked */
    1936                 :             :                 }
    1937                 :             :             }
    1938                 :             :         }
    1939                 :             : 
    1940                 :             :         /* check >C< attachments */
    1941                 :           0 :         n1 = at_no[1];
    1942                 :           0 :         attach1 = attach2 = 0;
    1943         [ #  # ]:           0 :         for (i = 0; i < at[n1].valence; i++)
    1944                 :             :         {
    1945         [ #  # ]:           0 :             if (at[n1].neighbor[i] != at_no[0] &&
    1946         [ #  # ]:           0 :                  at[n1].neighbor[i] != at_no[2])
    1947                 :             :             {
    1948         [ #  # ]:           0 :                 if (!attach1)
    1949                 :             :                 {
    1950                 :           0 :                     attach1 = is_Methyl_or_Etyl( at, n1, at[n1].neighbor[i] );
    1951                 :             :                 }
    1952                 :             :                 else
    1953                 :             :                 {
    1954         [ #  # ]:           0 :                     if (!attach2)
    1955                 :             :                     {
    1956                 :           0 :                         attach2 = is_Methyl_or_Etyl( at, n1, at[n1].neighbor[i] );
    1957                 :             :                     }
    1958                 :             :                     else
    1959                 :             :                     {
    1960                 :           0 :                         goto check_next_derivative2;
    1961                 :             :                     }
    1962                 :             :                 }
    1963                 :             :             }
    1964                 :             :         }
    1965   [ #  #  #  # ]:           0 :         if (!attach2 || attach2 != attach1)
    1966                 :             :         {
    1967                 :           0 :             goto check_next_derivative2;
    1968                 :             :         }
    1969                 :             : 
    1970                 :             :         /* all checks are done */
    1971         [ #  # ]:           0 :         if ( /*da &&*/ da1)
    1972                 :             :         {
    1973                 :             :             /*short ord_O = !bond_no[3];*/
    1974                 :           0 :             short ord_N = bond_no[0];
    1975                 :             :             /*AT_NUMB iN  = at_no[0];*/
    1976                 :           0 :             AT_NUMB iO = at_no[3];
    1977                 :           0 :             char num_2remove = 2 + attach1 + attach2;
    1978                 :           0 :             da1->typ[0]    /* = da1->typ[1] */ = DERIV_RING_DMOX_DEOX_N;
    1979                 :           0 :             da1->ord[0]    /* = da1->ord[1] */ = ord_N;
    1980                 :           0 :             da1->num[0]    /* = da1->num[1] */ = num_2remove;
    1981                 :           0 :             da1->other_atom = iO + 1;
    1982                 :             :             /*
    1983                 :             :             if ( da1->typ[0] ) {
    1984                 :             :             if ( da1->typ[0]     != DERIV_RING_DMOX_DEOX_O ||
    1985                 :             :             da1->ord[0]     != ord_O ||
    1986                 :             :             da1->num[0]     != num_2remove ||
    1987                 :             :             da1->other_atom != iN ) {
    1988                 :             :             goto check_next_derivative2;
    1989                 :             :             }
    1990                 :             :             } else {
    1991                 :             :             da1->typ[0]     = DERIV_RING_DMOX_DEOX_O;
    1992                 :             :             da1->ord[0]     = ord_O;
    1993                 :             :             da1->num[0]     = num_2remove;
    1994                 :             :             da1->other_atom = iN;
    1995                 :             :             }
    1996                 :             :             */
    1997                 :             :         }
    1998                 :             : 
    1999                 :           0 :         return DERIV_RING_DMOX_DEOX_N;
    2000                 :             :     }
    2001                 :             : 
    2002                 :           0 : check_next_derivative2:;
    2003                 :           0 :     return 0;
    2004                 :             : }
    2005                 :             : 
    2006                 :             : 
    2007                 :             : #endif  /* defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) */
    2008                 :             : 
    2009                 :             : #if( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) || defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
    2010                 :             : 
    2011                 :             : #define PRRLDD_RING_SIZE  5
    2012                 :             : #define PPRDN_RING_SIZE   6
    2013                 :             : #if( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) && defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
    2014                 :             : #define MIN_PRRLDD_PPRDN_RING_SIZE PRRLDD_RING_SIZE
    2015                 :             : #define MAX_PRRLDD_PPRDN_RING_SIZE PPRDN_RING_SIZE
    2016                 :             : #elif ( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) )
    2017                 :             : #define MIN_PRRLDD_PPRDN_RING_SIZE PRRLDD_RING_SIZE
    2018                 :             : #define MAX_PRRLDD_PPRDN_RING_SIZE PRRLDD_RING_SIZE
    2019                 :             : #elif ( defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
    2020                 :             : #define MIN_PRRLDD_PPRDN_RING_SIZE PPRDN_RING_SIZE
    2021                 :             : #define MAX_PRRLDD_PPRDN_RING_SIZE PPRDN_RING_SIZE
    2022                 :             : #else
    2023                 :             : #define MIN_PRRLDD_PPRDN_RING_SIZE (-1)
    2024                 :             : #define MAX_PRRLDD_PPRDN_RING_SIZE (-1)
    2025                 :             : #endif
    2026                 :             : 
    2027                 :             : 
    2028                 :             : /****************************************************************************/
    2029                 :           0 : int is_DERIV_RING2_PRRLDD_PPRDN( inp_ATOM *at,
    2030                 :             :                                  int cur_atom,
    2031                 :             :                                  int from_ord,
    2032                 :             :                                  DERIV_AT *da,
    2033                 :             :                                  DERIV_AT *da1 )
    2034                 :             : {
    2035                 :             :     /*
    2036                 :             :     #1   #2                                      #1   #2
    2037                 :             :     O   CH2--CH2                                 O   CH2--CH2            O
    2038                 :             :     ||  /     |                                  ||  /     |             ||
    2039                 :             :     R--C--N #0   |     N is cur_atom             R--C--N #0  CH2 #3  =>  R--C--OH
    2040                 :             :     from  \     |     C(IV) is from atom         from  \     |
    2041                 :             :     CH2--CH2                                     CH2--CH2
    2042                 :             :     #4   #3                                      #5   #4
    2043                 :             : 
    2044                 :             :     Pyrrolidides: Replace -N< with -OH           Piperidines: Replace -N< with -OH
    2045                 :             :     DERIV_RING2_PRRLDD_OUTSIDE_PRECUR            DERIV_RING2_PPRDN_OUTSIDE_PRECUR
    2046                 :             :     */
    2047                 :             :     int iat_from, i, neigh, k;
    2048                 :             :     char ord[2];
    2049                 :             : 
    2050                 :             :     /* check cur atom */
    2051                 :           0 :     if (
    2052         [ #  # ]:           0 :         at[cur_atom].el_number == EL_NUMBER_N &&
    2053         [ #  # ]:           0 :         MIN_PRRLDD_PPRDN_RING_SIZE <= at[cur_atom].nNumAtInRingSystem &&
    2054         [ #  # ]:           0 :         at[cur_atom].nNumAtInRingSystem <= MAX_PRRLDD_PPRDN_RING_SIZE &&
    2055   [ #  #  #  # ]:           0 :         at[cur_atom].valence == 3 && at[cur_atom].chem_bonds_valence == 3 &&
    2056   [ #  #  #  #  :           0 :         !at[cur_atom].charge && !at[cur_atom].radical && !at[cur_atom].num_H &&
                   #  # ]
    2057                 :             :         /* check the "from" atom (on the left from cur atom) */
    2058         [ #  # ]:           0 :         at[iat_from = at[cur_atom].neighbor[from_ord]].el_number == EL_NUMBER_C &&
    2059         [ #  # ]:           0 :         at[iat_from].nNumAtInRingSystem == 1 &&
    2060         [ #  # ]:           0 :         at[iat_from].valence == 3 &&
    2061         [ #  # ]:           0 :         at[iat_from].chem_bonds_valence == 4 &&
    2062   [ #  #  #  #  :           0 :         !at[iat_from].charge && !at[iat_from].radical && !at[iat_from].num_H)
                   #  # ]
    2063                 :             :     {
    2064                 :             :         /* check neighbors of the "from" atom (on the left from cur atom) */
    2065         [ #  # ]:           0 :         for (i = 0; i < at[iat_from].valence; i++)
    2066                 :             :         {
    2067                 :           0 :             neigh = at[iat_from].neighbor[i];
    2068         [ #  # ]:           0 :             if (neigh == cur_atom)
    2069                 :             :             {
    2070                 :             :                 ;
    2071                 :             :             }
    2072                 :             :             else
    2073                 :             :             {
    2074         [ #  # ]:           0 :                 if (at[iat_from].bond_type[i] == BOND_SINGLE)
    2075                 :             :                 {
    2076         [ #  # ]:           0 :                     if (at[neigh].el_number != EL_NUMBER_C)
    2077                 :             :                     {
    2078                 :           0 :                         goto check_next_derivative;
    2079                 :             :                     }
    2080                 :             :                 }
    2081                 :             :                 else
    2082                 :             :                 {
    2083         [ #  # ]:           0 :                     if (at[iat_from].bond_type[i] == BOND_DOUBLE)
    2084                 :             :                     {
    2085         [ #  # ]:           0 :                         if (at[neigh].el_number != EL_NUMBER_O ||
    2086   [ #  #  #  # ]:           0 :                              at[neigh].valence != 1 || at[neigh].chem_bonds_valence != 2 ||
    2087   [ #  #  #  #  :           0 :                              at[neigh].charge || at[neigh].radical || at[neigh].num_H)
                   #  # ]
    2088                 :             :                         {
    2089                 :           0 :                             goto check_next_derivative;
    2090                 :             :                         }
    2091                 :             :                     }
    2092                 :             :                     else
    2093                 :             :                     {
    2094                 :           0 :                         goto check_next_derivative;
    2095                 :             :                     }
    2096                 :             :                 }
    2097                 :             :             }
    2098                 :             :         }
    2099                 :             : 
    2100                 :             :         /* check the ring */
    2101                 :           0 :         iat_from = cur_atom;
    2102                 :           0 :         neigh = at[cur_atom].neighbor[!from_ord]; /* any except "from" atom */
    2103                 :           0 :         i = 1;
    2104         [ #  # ]:           0 :         if (at[cur_atom].nRingSystem != at[neigh].nRingSystem)
    2105                 :             :         {
    2106                 :           0 :             goto check_next_derivative;
    2107                 :             :         }
    2108                 :             :         do
    2109                 :             :         {
    2110         [ #  # ]:           0 :             if (at[neigh].el_number != EL_NUMBER_C ||
    2111         [ #  # ]:           0 :                  at[neigh].valence != 2 ||
    2112         [ #  # ]:           0 :                  at[neigh].chem_bonds_valence != 2 ||
    2113         [ #  # ]:           0 :                  at[neigh].num_H != 2 ||
    2114   [ #  #  #  # ]:           0 :                  at[neigh].charge || at[neigh].radical)
    2115                 :             :             {
    2116                 :           0 :                 goto check_next_derivative;
    2117                 :             :             }
    2118                 :           0 :             i++; /* at[neigh] satisfied the conditions */
    2119                 :           0 :             k = at[neigh].neighbor[at[neigh].neighbor[0] == iat_from];
    2120                 :           0 :             iat_from = neigh;
    2121                 :           0 :             neigh = k;
    2122   [ #  #  #  # ]:           0 :         } while (neigh != cur_atom && i <= MAX_PRRLDD_PPRDN_RING_SIZE);
    2123                 :             : 
    2124   [ #  #  #  # ]:           0 :         if (neigh == cur_atom &&
    2125         [ #  # ]:           0 :              MIN_PRRLDD_PPRDN_RING_SIZE <= i && i <= MAX_PRRLDD_PPRDN_RING_SIZE)
    2126                 :             :         {
    2127         [ #  # ]:           0 :             if (da1)
    2128                 :             :             {
    2129                 :             : #ifdef DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
    2130         [ #  # ]:           0 :                 if (i == PRRLDD_RING_SIZE)
    2131                 :             :                 {
    2132                 :           0 :                     da1->typ[0] = da1->typ[1] = DERIV_RING2_PRRLDD_OUTSIDE_PRECUR;
    2133                 :             :                 }
    2134                 :             : #endif
    2135                 :             : #ifdef DERIV_RING2_PPRDN_OUTSIDE_PRECUR
    2136         [ #  # ]:           0 :                 if (i == PPRDN_RING_SIZE)
    2137                 :             :                 {
    2138                 :           0 :                     da1->typ[0] = da1->typ[1] = DERIV_RING2_PPRDN_OUTSIDE_PRECUR;
    2139                 :             :                 }
    2140                 :             : #endif
    2141                 :           0 :                 ord[0] = !from_ord;
    2142         [ #  # ]:           0 :                 ord[1] = ( ( !from_ord + 1 ) ^ ( from_ord + 1 ) ) - 1; /* the 3rd possible index out of (0,1,2) */
    2143                 :           0 :                 k = ( ord[1] < ord[0] );
    2144                 :           0 :                 da1->ord[0] = ord[k];  /* smaller */
    2145                 :           0 :                 da1->ord[1] = ord[!k]; /* greater */
    2146                 :           0 :                 /*da1->num[0] = */da1->num[0] = i - 1; /* djb-rwth: unresolved issue -- revision required? / da1->num[1] = i-1? */
    2147                 :             :             }
    2148                 :           0 :             return i;
    2149                 :             :         }
    2150                 :             :     }
    2151                 :             : 
    2152                 :           0 : check_next_derivative:
    2153                 :             : 
    2154                 :           0 :     return 0;
    2155                 :             : }
    2156                 :             : #endif /* defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) || defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) */
    2157                 :             : 
    2158                 :             : 
    2159                 :             : #ifdef DERIV_DANSYL
    2160                 :             : 
    2161                 :             : 
    2162                 :             : /****************************************************************************
    2163                 :             : 
    2164                 :             : O        CH--CH
    2165                 :             : ||       //     \\
    2166                 :             : R--O[cur]--S[iS]--C[a]     CH                    => R--OH
    2167                 :             : ||       \      /cj bond from c
    2168                 :             : O     [b]C====C[c]   CH3
    2169                 :             : /      \    /
    2170                 :             : CH    [d]C--N[iN]-CH3
    2171                 :             : \\    //dj bond from d
    2172                 :             : CH--CH
    2173                 :             : 
    2174                 :             : -O[cur]=  -O- or -S- or -NH-
    2175                 :             : 
    2176                 :             : ****************************************************************************/
    2177                 :           0 : int is_Dansyl( inp_ATOM *at,
    2178                 :             :                int cur_atom,
    2179                 :             :                int to_ord, DERIV_AT *da,
    2180                 :             :                DERIV_AT *da1 )
    2181                 :             : {
    2182                 :           0 :     int i, a, b, c, d, cj = -1, dj = -1, neigh, k, iS /* S */, iN /* N */;
    2183   [ #  #  #  #  :           0 :     if (( (( at[cur_atom].el_number == EL_NUMBER_O || at[cur_atom].el_number == EL_NUMBER_S || at[cur_atom].el_number == EL_NUMBER_N ) &&
                   #  # ]
    2184   [ #  #  #  # ]:           0 :           at[cur_atom].valence == 2 && at[cur_atom].num_H == ( at[cur_atom].el_number == EL_NUMBER_N ) &&
    2185         [ #  # ]:           0 :           at[cur_atom].nNumAtInRingSystem == 1) ||
    2186   [ #  #  #  #  :           0 :           (at[cur_atom].el_number == EL_NUMBER_N && at[cur_atom].valence == 3 && at[cur_atom].num_H == 0) ) &&
                   #  # ]
    2187         [ #  # ]:           0 :          at[cur_atom].valence == at[cur_atom].chem_bonds_valence &&
    2188         [ #  # ]:           0 :          at[iS /* S */ = at[cur_atom].neighbor[to_ord]].el_number == EL_NUMBER_S &&
    2189   [ #  #  #  # ]:           0 :          at[iS].valence == 4 && at[iS].chem_bonds_valence == 6) /* djb-rwth: addressing LLVM warning */
    2190                 :             :     {
    2191                 :             :         /* neighbors of S; 6=1+1+2+2, 1+1+1+3 only. Therefore, we do not need to count (=O) neighbors */
    2192         [ #  # ]:           0 :         for (i = 0, a = -1; i < at[iS].valence; i++)
    2193                 :             :         {
    2194         [ #  # ]:           0 :             if (cur_atom == ( neigh = at[iS].neighbor[i] ))
    2195                 :             :             {
    2196                 :             :                 ;
    2197                 :             :             }
    2198                 :             :             else
    2199                 :             :             {
    2200         [ #  # ]:           0 :                 if (at[iS].bond_type[i] == BOND_DOUBLE)
    2201                 :             :                 {
    2202         [ #  # ]:           0 :                     if (at[neigh].el_number != EL_NUMBER_O ||
    2203         [ #  # ]:           0 :                          at[neigh].valence != 1 ||
    2204         [ #  # ]:           0 :                          at[neigh].chem_bonds_valence != 2 ||
    2205   [ #  #  #  #  :           0 :                          at[neigh].num_H || at[neigh].charge || at[neigh].radical)
                   #  # ]
    2206                 :             :                     {
    2207                 :           0 :                         goto check_next_derivative;
    2208                 :             :                     }
    2209                 :             :                 }
    2210                 :             :                 else
    2211                 :             :                 {
    2212   [ #  #  #  # ]:           0 :                     if (at[iS].bond_type[i] == BOND_SINGLE && a == -1)
    2213                 :             :                     {
    2214         [ #  # ]:           0 :                         if (at[neigh].el_number != EL_NUMBER_C ||
    2215         [ #  # ]:           0 :                              at[neigh].nNumAtInRingSystem != 10 ||
    2216         [ #  # ]:           0 :                              at[neigh].valence != 3 ||
    2217         [ #  # ]:           0 :                              at[neigh].chem_bonds_valence != 4 ||
    2218   [ #  #  #  #  :           0 :                              at[neigh].num_H || at[neigh].charge || at[neigh].radical ||
                   #  # ]
    2219         [ #  # ]:           0 :                              at[neigh].bond_type[at[neigh].neighbor[0] == iS] != BOND_ALTERN)
    2220                 :             :                         {
    2221                 :           0 :                             goto check_next_derivative;
    2222                 :             :                         }
    2223                 :           0 :                         a = neigh;
    2224                 :             :                     }
    2225                 :             :                     else
    2226                 :             :                     {
    2227                 :           0 :                         goto check_next_derivative;
    2228                 :             :                     }
    2229                 :             :                 }
    2230                 :             :             }
    2231                 :             :         }
    2232                 :             : 
    2233         [ #  # ]:           0 :         if (a < 0)
    2234                 :             :         {
    2235                 :           0 :             goto check_next_derivative;
    2236                 :             :         }
    2237                 :             : 
    2238                 :             :         /* at[a] - aromatic, j1 - index of its bond to its aromatic neighbor */
    2239                 :             :         /* find at[b] */
    2240         [ #  # ]:           0 :         for (i = k = 0, b = -1; i < at[a].valence; i++)
    2241                 :             :         {
    2242                 :           0 :             neigh = at[a].neighbor[i];
    2243         [ #  # ]:           0 :             if (neigh == iS)
    2244                 :             :             {
    2245                 :           0 :                 k += 1;
    2246                 :             :             }
    2247                 :             :             else
    2248                 :             :             {
    2249         [ #  # ]:           0 :                 if (at[a].bond_type[i] != BOND_ALTERN)
    2250                 :             :                 {
    2251                 :           0 :                     goto check_next_derivative; /* not Dansyl */
    2252                 :             :                 }
    2253   [ #  #  #  # ]:           0 :                 if (at[neigh].valence == 3 && b == -1)
    2254                 :             :                 {
    2255                 :           0 :                     b = neigh;
    2256                 :           0 :                     k += 10;
    2257                 :             :                 }
    2258                 :             :                 else
    2259                 :             :                 {
    2260   [ #  #  #  # ]:           0 :                     if (at[neigh].valence == 2 && at[neigh].chem_bonds_valence == 3)
    2261                 :             :                     {
    2262                 :           0 :                         k += 100;
    2263                 :             :                     }
    2264                 :             :                     else
    2265                 :             :                     {
    2266                 :           0 :                         goto check_next_derivative; /* not Dansyl */
    2267                 :             :                     }
    2268                 :             :                 }
    2269                 :             :             }
    2270                 :             :         }
    2271                 :             : 
    2272                 :             :         /* structure check: C[b] */
    2273         [ #  # ]:           0 :         if (k != 111)
    2274                 :             :         {
    2275                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2276                 :             :         }
    2277         [ #  # ]:           0 :         if (at[b].el_number != EL_NUMBER_C ||
    2278         [ #  # ]:           0 :              at[b].valence != 3 ||
    2279         [ #  # ]:           0 :              at[b].chem_bonds_valence != 4 ||
    2280   [ #  #  #  #  :           0 :              at[b].charge || at[b].radical || at[b].num_H)
                   #  # ]
    2281                 :             :         {
    2282                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2283                 :             :         }
    2284                 :             : 
    2285                 :             :         /* find at[c] */
    2286         [ #  # ]:           0 :         for (i = k = 0, c = -1; i < at[b].valence; i++)
    2287                 :             :         {
    2288         [ #  # ]:           0 :             if (at[b].bond_type[i] != BOND_ALTERN)
    2289                 :             :             {
    2290                 :           0 :                 goto check_next_derivative; /* not Dansyl */
    2291                 :             :             }
    2292                 :           0 :             neigh = at[b].neighbor[i];
    2293         [ #  # ]:           0 :             if (neigh == a)
    2294                 :             :             {
    2295                 :           0 :                 k += 1;
    2296                 :             :             }
    2297                 :             :             else
    2298                 :             :             {
    2299   [ #  #  #  # ]:           0 :                 if (at[neigh].valence == 3 && c == -1)
    2300                 :             :                 {
    2301                 :           0 :                     c = neigh;
    2302                 :           0 :                     k += 10;
    2303                 :             :                 }
    2304                 :             :                 else
    2305                 :             :                 {
    2306   [ #  #  #  # ]:           0 :                     if (at[neigh].valence == 2 && at[neigh].chem_bonds_valence == 3)
    2307                 :             :                     {
    2308                 :           0 :                         k += 100;
    2309                 :             :                     }
    2310                 :             :                     else
    2311                 :             :                     {
    2312                 :           0 :                         goto check_next_derivative; /* not Dansyl */
    2313                 :             :                     }
    2314                 :             :                 }
    2315                 :             :             }
    2316                 :             :         }
    2317                 :             : 
    2318         [ #  # ]:           0 :         if (k != 111)
    2319                 :             :         {
    2320                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2321                 :             :         }
    2322                 :             : 
    2323                 :             :         /* structure check: C[c] */
    2324         [ #  # ]:           0 :         if (at[c].el_number != EL_NUMBER_C ||
    2325         [ #  # ]:           0 :              at[c].valence != 3 ||
    2326         [ #  # ]:           0 :              at[c].chem_bonds_valence != 4 ||
    2327   [ #  #  #  #  :           0 :              at[c].charge || at[c].radical || at[c].num_H)
                   #  # ]
    2328                 :             :         {
    2329                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2330                 :             :         }
    2331                 :             : 
    2332                 :             :         /* find at[d] */
    2333         [ #  # ]:           0 :         for (i = k = 0, d = -1; i < at[c].valence; i++)
    2334                 :             :         {
    2335         [ #  # ]:           0 :             if (at[c].bond_type[i] != BOND_ALTERN)
    2336                 :             :             {
    2337                 :           0 :                 goto check_next_derivative; /* not Dansyl */
    2338                 :             :             }
    2339                 :           0 :             neigh = at[c].neighbor[i];
    2340         [ #  # ]:           0 :             if (neigh == b)
    2341                 :             :             {
    2342                 :           0 :                 k += 1;
    2343                 :             :             }
    2344                 :             :             else
    2345                 :             :             {
    2346   [ #  #  #  # ]:           0 :                 if (at[neigh].valence == 3 && d == -1)
    2347                 :             :                 {
    2348                 :           0 :                     d = neigh;
    2349                 :           0 :                     k += 10;
    2350                 :             :                 }
    2351                 :             :                 else
    2352                 :             :                 {
    2353         [ #  # ]:           0 :                     if (at[neigh].valence == 2)
    2354                 :             :                     {
    2355                 :           0 :                         cj = i;
    2356                 :           0 :                         k += 100;
    2357                 :             :                     }
    2358                 :             :                     else
    2359                 :             :                     {
    2360                 :           0 :                         goto check_next_derivative; /* not Dansyl */
    2361                 :             :                     }
    2362                 :             :                 }
    2363                 :             :             }
    2364                 :             :         }
    2365         [ #  # ]:           0 :         if (k != 111)
    2366                 :             :         {
    2367                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2368                 :             :         }
    2369                 :             : 
    2370                 :             :         /* structure check: C[d] */
    2371         [ #  # ]:           0 :         if (at[d].el_number != EL_NUMBER_C ||
    2372         [ #  # ]:           0 :              at[d].valence != 3 ||
    2373         [ #  # ]:           0 :              at[d].chem_bonds_valence != 4 ||
    2374   [ #  #  #  #  :           0 :              at[d].charge || at[d].radical || at[d].num_H)
                   #  # ]
    2375                 :             :         {
    2376                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2377                 :             :         }
    2378                 :             :         /* find at[iN]  */
    2379         [ #  # ]:           0 :         for (i = k = 0, iN = -1; i < at[d].valence; i++)
    2380                 :             :         {
    2381                 :           0 :             neigh = at[d].neighbor[i];
    2382         [ #  # ]:           0 :             if (neigh == c)
    2383                 :             :             {
    2384                 :           0 :                 k += 1;
    2385                 :             :             }
    2386                 :             :             else
    2387                 :             :             {
    2388   [ #  #  #  # ]:           0 :                 if (at[neigh].valence == 3 && iN == -1)
    2389                 :             :                 {
    2390                 :           0 :                     iN = neigh;
    2391                 :           0 :                     k += 10;
    2392                 :             :                 }
    2393                 :             :                 else
    2394                 :             :                 {
    2395   [ #  #  #  #  :           0 :                     if (at[neigh].valence == 2 && at[neigh].chem_bonds_valence == 3 && at[d].bond_type[i] == BOND_ALTERN)
                   #  # ]
    2396                 :             :                     {
    2397                 :           0 :                         dj = i;
    2398                 :           0 :                         k += 100;
    2399                 :             :                     }
    2400                 :             :                     else
    2401                 :             :                     {
    2402                 :           0 :                         goto check_next_derivative; /* not Dansyl */
    2403                 :             :                     }
    2404                 :             :                 }
    2405                 :             :             }
    2406                 :             :         }
    2407                 :             : 
    2408                 :             :         /* structure check: at[iN] */
    2409         [ #  # ]:           0 :         if (k != 111)
    2410                 :             :         {
    2411                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2412                 :             :         }
    2413         [ #  # ]:           0 :         if (at[iN].el_number != EL_NUMBER_N ||
    2414         [ #  # ]:           0 :              at[iN].valence != 3 ||
    2415         [ #  # ]:           0 :              at[iN].chem_bonds_valence != 3 ||
    2416   [ #  #  #  #  :           0 :              at[iN].charge || at[d].radical || at[d].num_H)
                   #  # ]
    2417                 :             :         {
    2418                 :           0 :             goto check_next_derivative; /* not Dansyl */
    2419                 :             :         }
    2420                 :             : 
    2421                 :             :         /* find attached to N 2 methyls */
    2422         [ #  # ]:           0 :         for (i = 0; i < at[iN].valence; i++)
    2423                 :             :         {
    2424   [ #  #  #  # ]:           0 :             if (( neigh = at[iN].neighbor[i] ) != d &&
    2425                 :           0 :                  !is_Methyl( at, neigh ))
    2426                 :             :             {
    2427                 :           0 :                 goto check_next_derivative; /* not Dansyl */
    2428                 :             :             }
    2429                 :             :         }
    2430                 :             : 
    2431                 :             :         /* check aromatic chain d-dj-...b and c-cj-...a */
    2432   [ #  #  #  # ]:           0 :         if (check_arom_chain( at, at[d].neighbor[dj] /* first*/, d /*from*/, b /*to*/, 4 ) &&
    2433                 :           0 :              check_arom_chain( at, at[c].neighbor[cj] /* first*/, c /*from*/, a /*to*/, 4 ))
    2434                 :             :         {
    2435         [ #  # ]:           0 :             if (da1)
    2436                 :             :             {
    2437                 :           0 :                 da1->typ[0] = DERIV_DANSYL;
    2438                 :           0 :                 da1->ord[0] = to_ord;
    2439                 :           0 :                 da1->num[0] = 16;
    2440                 :             :             }
    2441                 :           0 :             return DERIV_DANSYL;
    2442                 :             :         }
    2443                 :             :     }
    2444                 :             : 
    2445                 :           0 : check_next_derivative:
    2446                 :             : 
    2447                 :           0 :     return 0;
    2448                 :             : }
    2449                 :             : #endif /* DERIV_DANSYL */
    2450                 :             : 
    2451                 :             : 
    2452                 :             : /****************************************************************************/
    2453                 :           0 : int is_possibly_deriv_neigh( inp_ATOM *at,
    2454                 :             :                              int iat,
    2455                 :             :                              int iord,
    2456                 :             :                              int type,
    2457                 :             :                              char cFlags )
    2458                 :             : {
    2459                 :           0 :     int neigh = at[iat].neighbor[iord]; /* inside derivatizing agent */
    2460                 :           0 :     int neigh_from = -1;
    2461                 :           0 :     U_CHAR el = at[neigh].el_number;
    2462                 :           0 :     int    bOk = 0;
    2463   [ #  #  #  #  :           0 :     switch (type)
                      # ]
    2464                 :             :     {
    2465                 :           0 :         case DERIV_BRIDGE_O:
    2466                 :           0 :             neigh_from = at[iat].neighbor[!iord]; /* inside precursor
    2467                 :             :                                                   neigh_from  iat
    2468                 :             :                                                          -> A--O--B -> traversing from A(neigh_from) to B(neigh); may we cut O--B bond? */
    2469                 :             :                                                   /* do not cut bond "---" in A=Si(IV), B(=O), B=C: Si(IV)-O---B(=O) */
    2470   [ #  #  #  #  :           0 :             if (!( is_C_or_S_DB_O( at, neigh ) && is_Si_IV( at, neigh_from ) ) &&
                   #  # ]
    2471                 :           0 :                  !is_C_unsat_not_arom( at, neigh ))
    2472                 :             :             {
    2473         [ #  # ]:           0 :                 bOk = ( el == EL_NUMBER_C ||
    2474         [ #  # ]:           0 :                         el == EL_NUMBER_SI ||
    2475         [ #  # ]:           0 :                         el == EL_NUMBER_S ||
    2476   [ #  #  #  # ]:           0 :                         el == EL_NUMBER_P ) &&
    2477                 :           0 :                     is_deriv_chain2( at, iat, DERIV_BRIDGE_O, -1, iord, 0, NULL, 0, NULL, 0, NULL );
    2478                 :             :             }
    2479                 :           0 :             break;
    2480                 :             : 
    2481                 :             : #ifdef DERIV_RO_COX
    2482                 :           0 :         case DERIV_RO_COX:
    2483                 :             :             /*           iord
    2484                 :             :             -> R-O--[C(=O)-B]; -B: -CH3, C[n]F[2n+1] 0 < n < 4; may we cut O--C bond? */
    2485                 :           0 :             neigh_from = at[iat].neighbor[!iord];
    2486         [ #  # ]:           0 :             if (at[neigh_from].el_number == EL_NUMBER_C &&
    2487         [ #  # ]:           0 :                  at[iat].el_number == EL_NUMBER_O &&
    2488   [ #  #  #  # ]:           0 :                  at[neigh].el_number == EL_NUMBER_C &&
    2489                 :           0 :                  is_C_or_S_DB_O( at, neigh ))
    2490                 :             :             {
    2491                 :           0 :                 bOk = 1; /*is_deriv_chain2( at, iat, DERIV_RO_COX, iord, 0 ); does nothing */
    2492                 :             :             }
    2493                 :           0 :             break;
    2494                 :             : #endif
    2495                 :             : 
    2496                 :           0 :         case DERIV_BRIDGE_NH:
    2497                 :             :             /* -> A--NH--B -> traversing from A(neigh_from) to B(neigh); may we cut NH--B bond? */
    2498         [ #  # ]:           0 :             bOk = ( is_C_or_S_DB_O( at, neigh ) ||
    2499                 :             :                     /*is_C_Alk( at, neigh, cFlags ) ||*/
    2500                 :           0 :                     is_Si_IV( at, neigh ) /*||
    2501   [ #  #  #  #  :           0 :                                           is_P_TB_N( at, neigh )*/ ) && !( is_C_unsat_not_arom( at, neigh ) ) &&
                   #  # ]
    2502                 :           0 :                 is_deriv_chain2( at, iat, DERIV_BRIDGE_NH, -1, iord, 0, NULL, 0, NULL, 0, NULL );
    2503                 :           0 :             break;
    2504                 :           0 :         case DERIV_AMINE_tN:
    2505         [ #  # ]:           0 :             bOk = ( is_C_or_S_DB_O( at, neigh ) ||
    2506                 :             :                     /*is_C_Alk( at, neigh, cFlags ) ||*/
    2507                 :           0 :                     is_Si_IV( at, neigh ) /*||
    2508   [ #  #  #  #  :           0 :                                           is_P_TB_N( at, neigh )*/ ) && !( is_C_unsat_not_arom( at, neigh ) ) &&
                   #  # ]
    2509                 :           0 :                 is_deriv_chain2( at, iat, DERIV_AMINE_tN, -1, iord, 0, NULL, 0, NULL, 0, NULL );
    2510                 :           0 :             break;
    2511                 :             :     }
    2512                 :             : 
    2513                 :           0 :     return bOk;
    2514                 :             : }
    2515                 :             : 
    2516                 :             : 
    2517                 :             : /****************************************************************************
    2518                 :             : Determines derivative type on the forward step of the DFS
    2519                 :             : ****************************************************************************/
    2520                 :           0 : int get_traversed_deriv_type( inp_ATOM *at,
    2521                 :             :                               DERIV_AT *da,
    2522                 :             :                               int k,
    2523                 :             :                               DERIV_AT *da1,
    2524                 :             :                               char cFlags )
    2525                 :             : {
    2526                 :             :     /* at[k] is attachment point of the precursor */
    2527                 :             :     /* at[(int)at[k].neighbor[m]] is inside precursor */
    2528                 :             :     /* at[(int)at[k].neighbor[!m]] is inside derivatizing agent */
    2529                 :             :     /* !!! Except DERIV_RING_O_OUTSIDE_PRECURSOR, DERIV_RING_NH_OUTSIDE_PRECURSOR !!! */
    2530                 :             :     /* when at[k] is B or C attached to two atoms of the precursor */
    2531                 :             :     int i, j, m, n1, nBlockSystemFrom, nOrdBack1, nOrdBack2, nOrdBack3, nBackType1, nBackType2;
    2532                 :             : 
    2533                 :             : #if( defined(DERIV_X_OXIME) || defined(DERIV_RO_COX) || defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    2534                 :             :     int n0, n2, n3;
    2535                 :             : #endif
    2536                 :             : 
    2537                 :           0 :     memset( da1, 0, sizeof( *da1 ) ); /* djb-rwth: memset_s C11/Annex K variant? */
    2538         [ #  # ]:           0 :     if (at[k].cFlags & cFlags)
    2539                 :             :     {
    2540                 :           0 :         return 0;
    2541                 :             :     }
    2542                 :             : 
    2543   [ #  #  #  # ]:           0 :     for (m = 0; m < at[k].valence && !( at[(int) at[k].neighbor[m]].cFlags & cFlags ); m++)
    2544                 :             :     {
    2545                 :             :         ;
    2546                 :             :     }
    2547         [ #  # ]:           0 :     if (m == at[k].valence)
    2548                 :             :     {
    2549                 :           0 :         return -1;  /* error: at least one neighbor must have cFlags */
    2550                 :             :                     /* traversing at[k] from at[(int)at[k].neighbor[m]] */
    2551                 :             :     }
    2552   [ #  #  #  # ]:           0 :     if (at[k].valence == 1 && at[k].num_H && (
    2553         [ #  # ]:           0 :         at[k].el_number == EL_NUMBER_O ||
    2554         [ #  # ]:           0 :         at[k].el_number == EL_NUMBER_N ||
    2555         [ #  # ]:           0 :         at[k].el_number == EL_NUMBER_S ||
    2556         [ #  # ]:           0 :         at[k].el_number == EL_NUMBER_P ))
    2557                 :             :     {
    2558                 :           0 :         return DERIV_NOT;
    2559                 :             :     }
    2560         [ #  # ]:           0 :     if (is_el_a_metal( at[k].el_number ))
    2561                 :             :     {
    2562                 :           0 :         return DERIV_NOT;
    2563                 :             :     }
    2564                 :             : #ifdef NEVER
    2565                 :             :     if (at[k].el_number == EL_NUMBER_N && at[k].valence >= 2 && at[k].chem_bonds_valence <= 3)
    2566                 :             :     {
    2567                 :             :         return DERIV_NOT; /* prohibit -N-, -N=, allow -N# as in isocyano -N#C or NO2 */
    2568                 :             :     }
    2569                 :             : #endif
    2570                 :             :     /* m is the ord of the bond from which at[k] was reached first time */
    2571   [ #  #  #  # ]:           0 :     if (da[k].typ[0] && ( da[k].typ[0] & DERIV_UNEXPADABLE ) == da[k].typ[0])
    2572                 :             :     {
    2573                 :           0 :         return 0;
    2574                 :             :     }
    2575                 :             : #ifdef DERIV_X_OXIME
    2576   [ #  #  #  # ]:           0 :     if (at[k].nNumAtInRingSystem == 1 && at[k].el_number == EL_NUMBER_N &&
    2577   [ #  #  #  # ]:           0 :          at[k].valence == 2 && at[k].chem_bonds_valence == 3 &&
    2578   [ #  #  #  #  :           0 :          !at[k].num_H && !at[k].charge && !at[k].radical &&
                   #  # ]
    2579         [ #  # ]:           0 :          at[n0 = at[k].neighbor[m]].el_number == EL_NUMBER_C &&  /* inside precursor */
    2580         [ #  # ]:           0 :          at[n1 = at[k].neighbor[!m]].el_number == EL_NUMBER_O && /* inside derivatizing agent */
    2581   [ #  #  #  # ]:           0 :          at[k].bond_type[m] == BOND_DOUBLE && at[k].bond_type[!m] == BOND_SINGLE &&
    2582   [ #  #  #  #  :           0 :          at[n0].valence == 3 - at[n0].num_H && at[n0].chem_bonds_valence == 4 - at[n0].num_H && !at[n0].charge && !at[n0].radical && /* C */
             #  #  #  # ]
    2583   [ #  #  #  #  :           0 :          at[n1].valence == 2 && at[n1].chem_bonds_valence == 2 && !at[n1].charge && !at[n1].radical                   /* O */
             #  #  #  # ]
    2584                 :             :          )
    2585                 :             :     {
    2586                 :             :         /* found C==N--O */
    2587                 :             :         /* traversing from C to O; C(at[neighbor[m]]) has cFlag; N is at[k]; N-O bond is to be broken */
    2588                 :             :         /* m     !m
    2589                 :             :            n0 k  n1 n2       n2    n2  n3   n2  n3      n2  n3...
    2590                 :             :            C==N--O--R; -R: -CH3, -CH2-CH3, -Si(CH3)3, -CH2-C6H5; cut N-O and replace =N- with =O 2013-08-22 DT */
    2591                 :             :         /* check other neighbors of C: they should be C,H or C,C */
    2592         [ #  # ]:           0 :         for (i = 0; i < at[n0].valence; i++)
    2593                 :             :         {
    2594   [ #  #  #  # ]:           0 :             if (at[n0].neighbor[i] != k && at[at[n0].neighbor[i]].el_number != EL_NUMBER_C)
    2595                 :             :             {
    2596                 :           0 :                 goto check_next_derivative; /* wrong neighbor */
    2597                 :             :             }
    2598                 :             :         }
    2599                 :             :         /* found    C
    2600                 :             :         |
    2601                 :             :         C==N==O
    2602                 :             :         |
    2603                 :             :         C
    2604                 :             :         */
    2605                 :             :         /* find other neighbor of O */
    2606                 :           0 :         n2 = at[n1].neighbor[at[n1].neighbor[0] == k];
    2607         [ #  # ]:           0 :         if (is_Si_IV( at, n2 ))
    2608                 :             :         {
    2609                 :           0 :             int n4 = -1;
    2610         [ #  # ]:           0 :             for (i = 0; i < at[n2].valence; i++)
    2611                 :             :             {
    2612                 :           0 :                 int n3 = at[n2].neighbor[i];
    2613         [ #  # ]:           0 :                 if (n3 == n1)
    2614                 :             :                 {
    2615                 :           0 :                     continue; /* atom O */
    2616                 :             :                 }
    2617   [ #  #  #  # ]:           0 :                 if (at[n3].el_number != EL_NUMBER_C || at[n3].charge ||
    2618   [ #  #  #  # ]:           0 :                      at[n3].radical || at[n3].chem_bonds_valence != at[n3].valence)
    2619                 :           0 :                     goto check_next_derivative; /* wrong neighbor */
    2620   [ #  #  #  #  :           0 :                 if (n4 == -1 && at[n3].valence == 4 && !at[n3].num_H)
                   #  # ]
    2621                 :             :                 {
    2622                 :           0 :                     n4 = n3; /* possibly tret-butyl */
    2623                 :             :                 }
    2624                 :             :                 else
    2625                 :             :                 {
    2626   [ #  #  #  # ]:           0 :                     if (at[n3].chem_bonds_valence != 1 || at[n3].num_H != 3)
    2627                 :             :                     {
    2628                 :           0 :                         goto check_next_derivative; /* wrong neighbor */
    2629                 :             :                                                     /* methyl identified */
    2630                 :             :                     }
    2631                 :             :                 }
    2632                 :             :             }
    2633         [ #  # ]:           0 :             if (n4 == -1)
    2634                 :             :             {
    2635                 :             : #ifdef UNDERIV_X_OXIME_TMS
    2636                 :             :                 /* found    C
    2637                 :             :                 |        n2
    2638                 :             :                 C==N==O--Si(CH3)3
    2639                 :             :                 |
    2640                 :             :                 C
    2641                 :             :                 */
    2642                 :           0 :                 da1->ord[0] = !m;         /* ord of neighbor O, already checked */
    2643                 :           0 :                 da1->typ[0] = DERIV_X_OXIME;   /* type */
    2644                 :           0 :                 da1->num[0] = 5; /* 5 atoms: -O-Si(CH3)3 -O-TMS*/
    2645                 :           0 :                 return DERIV_X_OXIME;   /* >C=N-O-Si(CH3)3 */
    2646                 :             : #else
    2647                 :             :                 goto check_next_derivative; /* wrong neighbor */
    2648                 :             : #endif
    2649                 :             :             }
    2650                 :             : #ifndef UNDERIV_X_OXIME_TBDMS
    2651                 :             :             goto check_next_derivative; /* do not include TBDMS */
    2652                 :             : #endif
    2653                 :             : 
    2654         [ #  # ]:           0 :             for (i = 0; i < at[n4].valence; i++)
    2655                 :             :             {
    2656                 :           0 :                 int n3 = at[n4].neighbor[i];
    2657         [ #  # ]:           0 :                 if (n3 == n2)
    2658                 :             :                 {
    2659                 :           0 :                     continue; /* atom Si */
    2660                 :             :                 }
    2661   [ #  #  #  # ]:           0 :                 if (at[n3].el_number != EL_NUMBER_C || at[n3].charge ||
    2662   [ #  #  #  # ]:           0 :                      at[n3].radical || at[n3].chem_bonds_valence != at[n3].valence)
    2663                 :             :                 {
    2664                 :           0 :                     goto check_next_derivative; /* wrong neighbor */
    2665                 :             :                 }
    2666   [ #  #  #  # ]:           0 :                 if (at[n3].chem_bonds_valence != 1 || at[n3].num_H != 3)
    2667                 :             :                 {
    2668                 :           0 :                     goto check_next_derivative; /* wrong neighbor */
    2669                 :             :                                                 /* methyl identified */
    2670                 :             :                 }
    2671                 :             :             }
    2672                 :           0 :             da1->ord[0] = !m;         /* ord of neighbor O, already checked */
    2673                 :           0 :             da1->typ[0] = DERIV_X_OXIME;   /* type */
    2674                 :           0 :             da1->num[0] = 8; /* 8 atoms: -O-Si(CH3)2-C(CH3)3 -O-TBDMS  */
    2675                 :           0 :             return DERIV_X_OXIME;   /* >C=N-O-Si(CH3)2-C(CH3)3 */
    2676                 :             : 
    2677                 :             :         }
    2678         [ #  # ]:           0 :         if (at[n2].el_number != EL_NUMBER_C)
    2679                 :             :         {
    2680                 :           0 :             goto check_next_derivative; /* wrong neighbor */
    2681                 :             :         }
    2682   [ #  #  #  #  :           0 :         if (at[n2].chem_bonds_valence == 1 && at[n2].num_H == 3 && !at[n2].charge && !at[n2].radical)
             #  #  #  # ]
    2683                 :             :         {
    2684                 :           0 :             da1->ord[0] = !m;         /* ord of neighbor O, already checked */
    2685                 :           0 :             da1->typ[0] = DERIV_X_OXIME;   /* type */
    2686                 :           0 :             da1->num[0] = 2; /* 2 atoms: -O-CH3 */
    2687                 :           0 :             return DERIV_X_OXIME;   /* >C=N-O-CH3 */
    2688                 :             :         }
    2689   [ #  #  #  #  :           0 :         if (at[n2].valence != 2 || at[n2].chem_bonds_valence != 2 || at[n2].num_H != 2 || at[n2].charge || at[n2].radical)
          #  #  #  #  #  
                      # ]
    2690                 :             :         {
    2691                 :           0 :             goto check_next_derivative; /* wrong neighbor */
    2692                 :             :         }
    2693                 :           0 :         n3 = at[n2].neighbor[at[n2].neighbor[0] == n1];
    2694   [ #  #  #  #  :           0 :         if (at[n3].chem_bonds_valence == 1 && at[n3].num_H == 3 && !at[n3].charge && !at[n3].radical)
             #  #  #  # ]
    2695                 :             :         {
    2696                 :           0 :             da1->ord[0] = !m;         /* ord of neighbor O, already checked */
    2697                 :           0 :             da1->typ[0] = DERIV_X_OXIME;   /* type */
    2698                 :           0 :             da1->num[0] = 3; /* 3 atoms: -O-CH2-CH3 */
    2699                 :           0 :             return DERIV_X_OXIME;   /* >C=N-O-CH2-CH3 */
    2700                 :             :         }
    2701   [ #  #  #  #  :           0 :         if (at[n3].valence == 3 && at[n3].chem_bonds_valence == 4 && !at[n3].num_H && !at[n3].charge && !at[n3].radical &&
          #  #  #  #  #  
                      # ]
    2702   [ #  #  #  #  :           0 :              at[n3].nRingSystem != at[n2].nRingSystem && at[n3].bCutVertex && at[n3].nNumAtInRingSystem == 6)
                   #  # ]
    2703                 :             :         {
    2704         [ #  # ]:           0 :             if (is_Phenyl( at, n2, n3 ))
    2705                 :             :             {
    2706                 :           0 :                 da1->ord[0] = !m;         /* ord of neighbor O, already checked */
    2707                 :           0 :                 da1->typ[0] = DERIV_X_OXIME;   /* type */
    2708                 :           0 :                 da1->num[0] = 8; /* 8 atoms: O--CH2-C6H5 */
    2709                 :           0 :                 return DERIV_X_OXIME;   /* >C=N-O-CH2-C6H5 */
    2710                 :             :             }
    2711                 :             :         }
    2712                 :             :     }
    2713                 :             : 
    2714                 :           0 : check_next_derivative:
    2715                 :             : #endif  /* DERIV_X_OXIME */
    2716                 :             : #ifdef DERIV_DANSYL
    2717         [ #  # ]:           0 :     if (at[k].nNumAtInRingSystem == 1 &&
    2718   [ #  #  #  #  :           0 :         ( (( at[k].el_number == EL_NUMBER_O || at[k].el_number == EL_NUMBER_S ) && at[k].valence == 2) ||
                   #  # ]
    2719   [ #  #  #  #  :           0 :           (at[k].el_number == EL_NUMBER_N && at[k].valence == 2 && at[k].num_H == 1) || (at[k].valence == 3 && at[k].num_H == 2) )) /* djb-rwth: addressing LLVM warnings */
          #  #  #  #  #  
                      # ]
    2720                 :             :     {
    2721                 :             :         DERIV_AT da2;
    2722                 :           0 :         memset( &da2, 0, sizeof( da2 ) ); /* djb-rwth: memset_s C11/Annex K variant? */
    2723         [ #  # ]:           0 :         for (j = 0, n1 = 0; j < at[k].valence; j++)
    2724                 :             :         {
    2725         [ #  # ]:           0 :             if (j == m)
    2726                 :             :             {
    2727                 :           0 :                 continue;
    2728                 :             :             }
    2729         [ #  # ]:           0 :             if (at[i = at[k].neighbor[j]].el_number == EL_NUMBER_S &&
    2730   [ #  #  #  #  :           0 :                  at[i].valence == 4 && at[i].chem_bonds_valence == 6 &&
                   #  # ]
    2731                 :           0 :                  is_Dansyl( at, k, j, da, &da2 ))
    2732                 :             :             {
    2733                 :           0 :                 n1++;
    2734                 :             :             }
    2735                 :             :         }
    2736         [ #  # ]:           0 :         if (n1 == 1)
    2737                 :             :         {
    2738                 :           0 :             *da1 = da2;
    2739                 :           0 :             return DERIV_DANSYL;
    2740                 :             :         }
    2741                 :             :     }
    2742                 :             : #endif
    2743                 :             : 
    2744   [ #  #  #  #  :           0 :     if (at[k].nNumAtInRingSystem == 1 && ( at[k].el_number == EL_NUMBER_O || at[k].el_number == EL_NUMBER_S ) &&
                   #  # ]
    2745   [ #  #  #  # ]:           0 :          at[k].valence == 2 && at[k].chem_bonds_valence == 2 &&
    2746   [ #  #  #  #  :           0 :          !at[k].num_H && !at[k].charge && !at[k].radical)
                   #  # ]
    2747                 :             :     {
    2748                 :             :         /*   at[k].neighbor[m] k n1==at[k].neighbor[!m]  */
    2749                 :             :         /*               -> A--O--B -> traversing from A to B; cut O--B */
    2750                 :             :         /* check for carboxy A(=O)-O-B and A--O--B(=O) */
    2751                 :             :         /*int has_A_CO   = is_C_or_S_DB_O( at, at[k].neighbor[m] );*/
    2752                 :           0 :         int has_B_CO = is_C_or_S_DB_O( at, n1 = at[k].neighbor[!m] );/* B is C(=o) or S(=O) */
    2753                 :           0 :         int is_A_Si_IV = is_Si_IV( at, at[k].neighbor[m] ); /* A is >Si< */
    2754                 :             :                                                             /* int is_B_Si_IV = is_Si_IV( at, at[k].neighbor[!m] );*/
    2755                 :             : 
    2756                 :             : #ifdef DERIV_RO_COX
    2757                 :             :                                                             /*               n3
    2758                 :             :                                                             at[k].neighbor[m]  k n1 n2
    2759                 :             :                                                             R--O--C--X; -X = -CH3,  -Phenyl,  -C[n]F[2n+1] 0 < n < 4
    2760                 :             :                                                             ||        (acetate)(benzoate)
    2761                 :             :                                                             O
    2762                 :             :                                                             */
    2763                 :           0 :         n3 = at[k].neighbor[m];  /* R */
    2764   [ #  #  #  #  :           0 :         if (has_B_CO && is_C_DB_O( at, n1 ) &&  /* B:n1 is >C=O  */
                   #  # ]
    2765         [ #  # ]:           0 :              !is_silyl2( at, n3, k ) &&
    2766                 :           0 :              !is_el_a_metal( at[n3].el_number ))
    2767                 :             :         {
    2768         [ #  # ]:           0 :             for (j = 0; j < at[n1].valence; j++)
    2769                 :             :             {
    2770   [ #  #  #  # ]:           0 :                 if (at[n1].neighbor[j] != k && at[n1].bond_type[j] == BOND_SINGLE)
    2771                 :             :                 {
    2772                 :             :                     /* the only suspected neighbor */
    2773                 :           0 :                     n2 = at[n1].neighbor[j]; /* X */
    2774                 :           0 :                     n0 = is_CF3_or_linC3F7a(at, n2, n1); /* djb-rwth: addressing LLVM warning */
    2775         [ #  # ]:           0 :                     if (n0) 
    2776                 :             :                     {
    2777                 :           0 :                         n0 = 2 + 3 * n0 + 1; /* (6,9,12 atoms) -C(=O)C[n]F[2n+1]; is_CF3_or_linC3F7a returns n */
    2778                 :             :                     }
    2779                 :             : #ifdef UNDERIV_RO_COX_Me
    2780                 :             :                     else
    2781         [ #  # ]:           0 :                         if (is_Methyl( at, n2 ))
    2782                 :             :                         {
    2783                 :             :                             /* methyl */
    2784   [ #  #  #  # ]:           0 :                             if (!is_Methyl( at, n3 ) && !is_Ethyl( at, k, n3 ))
    2785                 :             :                             {
    2786                 :           0 :                                 n0 = 3; /* 3 atoms: -C(=O)-CH3 */
    2787                 :             :                             }
    2788                 :             :                         }
    2789                 :             : #endif /* UNDERIV_RO_COX_Me */
    2790                 :             : #ifdef UNDERIV_RO_COX_Et
    2791                 :             :                         else
    2792                 :             :                             if (is_Ethyl( at, n1, n2 ))
    2793                 :             :                             { /* ethyl */
    2794                 :             :                                 if (!is_Methyl( at, n3 ) && !is_Ethyl( at, k, n3 ))
    2795                 :             :                                 {
    2796                 :             :                                     n0 = 4; /* 4 atoms: -C(=O)-CH2-CH3 */
    2797                 :             :                                 }
    2798                 :             :                             }
    2799                 :             : #endif /* UNDERIV_RO_COX_Et */
    2800                 :             : #ifdef UNDERIV_RO_COX_BENZOATES
    2801                 :             :                             else
    2802         [ #  # ]:           0 :                                 if (is_Phenyl( at, n1, n2 ))
    2803                 :             :                                 {
    2804   [ #  #  #  # ]:           0 :                                     if (!is_Methyl( at, n3 ) && !is_Ethyl( at, k, n3 ))
    2805                 :             :                                     {
    2806                 :           0 :                                         n0 = 2 + 6; /* 8  atoms -C(=O)-C6H5 */
    2807                 :             :                                     }
    2808                 :             :                                 }
    2809                 :             : #endif /* UNDERIV_RO_COX_BENZOATES */
    2810                 :             : #ifdef UNDERIV_RO_COX_PENTAFLOUROBENZOATES
    2811                 :             :                                 else
    2812         [ #  # ]:           0 :                                     if (is_PentaFluoroPhenyl( at, n1, n2 ))
    2813                 :             :                                     {
    2814   [ #  #  #  # ]:           0 :                                         if (!is_Methyl( at, n3 ) && !is_Ethyl( at, k, n3 ))
    2815                 :             :                                         {
    2816                 :           0 :                                             n0 = 13; /* 13  atoms -C(=O)-C6F5 */
    2817                 :             :                                         }
    2818                 :             :                                     }
    2819                 :             : #endif  /* UNDERIV_RO_COX_PENTAFLOUROBENZOATES */
    2820         [ #  # ]:           0 :                     if (n0)
    2821                 :             :                     {
    2822                 :           0 :                         da1->ord[0] = !m;         /* ord of at[k]'s, that is, -O-'s neighbor X in C(=O)-X */
    2823                 :           0 :                         da1->typ[0] = DERIV_RO_COX;   /* type */
    2824                 :           0 :                         da1->num[0] = n0; /* num atoms in derivatizing attachement */
    2825                 :           0 :                         return DERIV_RO_COX;   /* R--O--C(=O)--X; -X = -CH3, -C[n]F[2n+1] 0 < n < 4, -Phenyl, -C6F5 */
    2826                 :             :                     }
    2827                 :           0 :                     break;
    2828                 :             :                 }
    2829                 :             :             }
    2830                 :             :         }
    2831                 :             : #endif   /* DERIV_RO_COX */
    2832                 :             : 
    2833   [ #  #  #  # ]:           0 :         if (is_A_Si_IV && has_B_CO)
    2834                 :             :         {
    2835                 :             :             /*                                             precursor | deriv.agent */
    2836                 :             :             ; /* do not cut bond --- in A=>Si<, B(=O), B=C,S: Si(IV)-O---B(=O) */
    2837                 :             :         }
    2838                 :             :         else
    2839                 :             :         {
    2840                 :             :             /* at[k] is precursor's attachment point;
    2841                 :             :             at[at[k].neighbor[!m]] belongs to drivatizing agent,
    2842                 :             :             at[at[k].neighbor[m]]  was marked (from_atom) */
    2843         [ #  # ]:           0 :             if (is_possibly_deriv_neigh( at, k, !m, DERIV_BRIDGE_O, cFlags ))
    2844                 :             :             {
    2845                 :           0 :                 da1->ord[0] = !m;         /* ord of neighbor B, not traversed yet */
    2846                 :           0 :                 da1->typ[0] = DERIV_BRIDGE_O;   /* type */
    2847                 :           0 :                 return DERIV_BRIDGE_O;   /* Representative: R-C(=O)-O(!m)--[D]  */
    2848                 :             :             }
    2849                 :             :         }
    2850                 :             :     }
    2851                 :             : 
    2852                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    2853                 :             :     /*
    2854                 :             :     R--C==N   Me or Et
    2855                 :             :     |   \ /
    2856                 :             :     |    C
    2857                 :             :     |   / \
    2858                 :             :     O--CH2 Me or ET
    2859                 :             :     */
    2860   [ #  #  #  #  :           0 :     if (at[k].el_number == EL_NUMBER_O && at[k].nNumAtInRingSystem == 5 &&
                   #  # ]
    2861                 :           0 :          is_DERIV_RING_DMOX_DEOX_O( at, k, m, da, da1 ))
    2862                 :             :     {
    2863                 :           0 :         return DERIV_RING_DMOX_DEOX_O;
    2864                 :             :     }
    2865   [ #  #  #  # ]:           0 :     if (at[k].el_number == EL_NUMBER_N && at[k].nNumAtInRingSystem == 5 &&
    2866   [ #  #  #  #  :           0 :          at[k].valence == 2 && at[k].chem_bonds_valence == 3 &&
                   #  # ]
    2867                 :           0 :          is_DERIV_RING_DMOX_DEOX_N( at, k, m, da, da1 ))
    2868                 :             :     {
    2869                 :           0 :         return DERIV_RING_DMOX_DEOX_N;
    2870                 :             :     }
    2871                 :             : #endif
    2872                 :             : 
    2873                 :             :     /* R1--NH--R2 */
    2874   [ #  #  #  # ]:           0 :     if (at[k].bCutVertex && at[k].el_number == EL_NUMBER_N &&
    2875   [ #  #  #  # ]:           0 :          at[k].valence == 2 && at[k].chem_bonds_valence == at[k].valence &&
    2876   [ #  #  #  #  :           0 :          at[k].valence + at[k].num_H == 3 && !at[k].charge && !at[k].radical)
                   #  # ]
    2877                 :             :     {
    2878                 :           0 :         da1->ord[0] = !m;         /* ord of neighbor B, not traversed yet */
    2879                 :           0 :         da1->typ[0] = DERIV_BRIDGE_NH;   /* type */
    2880                 :           0 :         return DERIV_BRIDGE_NH;   /* R1-NH-R2  amine */
    2881                 :             :     }
    2882                 :             :     /*
    2883                 :             :     R2
    2884                 :             :     /
    2885                 :             :     R1----N
    2886                 :             :     \
    2887                 :             :     R3
    2888                 :             :     */
    2889                 :             : 
    2890                 :             : 
    2891   [ #  #  #  # ]:           0 :     if (at[k].bCutVertex && at[k].el_number == EL_NUMBER_N &&
    2892   [ #  #  #  # ]:           0 :          at[k].valence == 3 && at[k].chem_bonds_valence == at[k].valence &&
    2893   [ #  #  #  #  :           0 :          at[k].valence + at[k].num_H == 3 && !at[k].charge && !at[k].radical)
                   #  # ]
    2894                 :             :     {
    2895                 :             : #if( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) || defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) )
    2896         [ #  # ]:           0 :         if (at[j = at[k].neighbor[m]].el_number == EL_NUMBER_C &&
    2897   [ #  #  #  #  :           0 :              at[j].valence == 3 && at[j].chem_bonds_valence == 4 &&
                   #  # ]
    2898                 :           0 :              ( i = is_DERIV_RING2_PRRLDD_PPRDN( at, k, m, da, da1 ) ))
    2899                 :             :         {
    2900                 :           0 :             return i;
    2901                 :             :         }
    2902                 :             :         else
    2903                 :             : #endif
    2904                 :             :         {
    2905                 :           0 :             int rm1 = ( at[at[k].neighbor[m]].nRingSystem == at[at[k].neighbor[( m + 1 ) % 3]].nRingSystem );
    2906                 :           0 :             int rm2 = ( at[at[k].neighbor[m]].nRingSystem == at[at[k].neighbor[( m + 2 ) % 3]].nRingSystem );
    2907                 :           0 :             int r12 = ( at[at[k].neighbor[( m + 1 ) % 3]].nRingSystem == at[at[k].neighbor[( m + 2 ) % 3]].nRingSystem );
    2908                 :           0 :             int ord[2] = { -1, -1 };
    2909                 :           0 :             i = 0; /* type: tertriary amine: DERIV_AMINE_tN */
    2910      [ #  #  # ]:           0 :             switch (rm1 + rm2 + r12)
    2911                 :             :             {
    2912                 :           0 :                 case 0:
    2913                 :             :                     /* -N< has no ring bonds */
    2914         [ #  # ]:           0 :                     if (is_possibly_deriv_neigh( at, k, ( m + 1 ) % 3, DERIV_AMINE_tN, cFlags ))
    2915                 :             :                     {
    2916                 :           0 :                         ord[i++] = ( m + 1 ) % 3; /* ord of a non-ring neighbor, not traversed yet */
    2917                 :             :                     }
    2918         [ #  # ]:           0 :                     if (is_possibly_deriv_neigh( at, k, ( m + 2 ) % 3, DERIV_AMINE_tN, cFlags ))
    2919                 :             :                     {
    2920                 :           0 :                         ord[i++] = ( m + 2 ) % 3; /* ord of another non-ring neighbor, not traversed yet */
    2921                 :             :                     }
    2922   [ #  #  #  # ]:           0 :                     if (i == 2 && ord[0] > ord[1])
    2923                 :             :                     {
    2924                 :           0 :                         int tmp = ord[0];
    2925                 :           0 :                         ord[0] = ord[1];
    2926                 :           0 :                         ord[1] = tmp;
    2927                 :             :                     }
    2928                 :           0 :                     break;
    2929                 :             : 
    2930                 :           0 :                 case 1:
    2931                 :             :                     /* -N< has one non-ring bond; do not consider [m] because it is "from" bond */
    2932   [ #  #  #  # ]:           0 :                     if (rm1 && is_possibly_deriv_neigh( at, k, ( m + 2 ) % 3, DERIV_AMINE_tN, cFlags ))
    2933                 :             :                     {
    2934                 :           0 :                         ord[i++] = ( m + 2 ) % 3;   /* ord of a single non-ring neighbor, not traversed yet */
    2935                 :             :                     }
    2936                 :             :                     else
    2937   [ #  #  #  # ]:           0 :                         if (rm2 && is_possibly_deriv_neigh( at, k, ( m + 1 ) % 3, DERIV_AMINE_tN, cFlags ))
    2938                 :             :                         {
    2939                 :           0 :                             ord[i++] = ( m + 1 ) % 3; /* ord of a single non-ring neighbor, not traversed yet */
    2940                 :             :                         }
    2941                 :             :                     /* r12 != 0 <=> at[k]neighbor[m] is the only non-ring bond; ignore it because it is "from" bond */
    2942                 :             :             }
    2943         [ #  # ]:           0 :             for (j = 0; j < i; j++)
    2944                 :             :             {
    2945                 :           0 :                 da1->ord[j] = ord[j];
    2946                 :           0 :                 da1->typ[j] = DERIV_AMINE_tN;
    2947                 :             :             }
    2948         [ #  # ]:           0 :             if (i)
    2949                 :             :             {
    2950                 :           0 :                 return DERIV_AMINE_tN;
    2951                 :             :             }
    2952                 :           0 :             return 0; /* all neighbors or two untraversed edges are in one ring system */
    2953                 :             :         }
    2954                 :             :     }
    2955                 :             : 
    2956                 :             :     /*-----------------------------------------------------------------*/
    2957                 :             :     /* DERIV_RING_O_OUTSIDE_PRECURSOR, DERIV_RING_NH_OUTSIDE_PRECURSOR */
    2958         [ #  # ]:           0 :     if (at[k].bCutVertex && /* DD */
    2959         [ #  # ]:           0 :          at[k].valence == at[k].chem_bonds_valence &&
    2960   [ #  #  #  #  :           0 :          ( !at[k].num_H || (at[k].el_number == EL_NUMBER_C && 1 == at[k].num_H) ) &&
                   #  # ]
    2961   [ #  #  #  # ]:           0 :          !at[k].charge && !at[k].radical &&
    2962   [ #  #  #  # ]:           0 :          ( (at[k].el_number == EL_NUMBER_C  && at[k].valence + at[k].num_H == 4) ||
    2963   [ #  #  #  # ]:           0 :            (at[k].el_number == EL_NUMBER_SI && at[k].valence == 4) ||
    2964   [ #  #  #  # ]:           0 :            (at[k].el_number == EL_NUMBER_B  && at[k].valence == 3) )) /* djb-rwth: addressing LLVM warning */
    2965                 :             :     {
    2966                 :             : 
    2967                 :             :         /*-->    j \        entering path: ->X--O--DD
    2968                 :             :         --X--O  v
    2969                 :             :         |    \ k /    DD = C, CH, Si, B
    2970                 :             :         |     DD
    2971                 :             :         |    /   \     O = O, S, NH  = at[j], going from DD
    2972                 :             :         --Y--O
    2973                 :             :         X, Y -- must be C
    2974                 :             :         */
    2975                 :           0 :         nBlockSystemFrom = 0;
    2976                 :           0 :         nBackType1 = nBackType2 = 0;
    2977                 :           0 :         nOrdBack1 = nOrdBack2 = nOrdBack3 = -1;
    2978                 :           0 :         j = (int) at[k].neighbor[m];
    2979                 :             :         ; /* X */
    2980   [ #  #  #  #  :           0 :         if (( at[j].el_number == EL_NUMBER_O || at[j].el_number == EL_NUMBER_S ) && at[j].valence == 2 &&
                   #  # ]
    2981         [ #  # ]:           0 :              at[j].chem_bonds_valence == at[j].valence &&
    2982         [ #  # ]:           0 :              at[j].nNumAtInRingSystem >= 5 &&
    2983         [ #  # ]:           0 :              at[n1 = at[j].neighbor[at[j].neighbor[0] == k]].el_number == EL_NUMBER_C && /* X is C */ 
    2984   [ #  #  #  #  :           0 :              !at[j].num_H && !at[j].charge && !at[j].radical) /* djb-rwth: ignoring LLVM warning: variable used */
                   #  # ]
    2985                 :             :         {
    2986                 :           0 :             nBackType1 = DERIV_RING_O_OUTSIDE_PRECURSOR;
    2987                 :           0 :             nBlockSystemFrom = at[j].nBlockSystem;
    2988                 :           0 :             nOrdBack1 = m; /* predecessor */
    2989                 :             :         }
    2990                 :             :         else
    2991   [ #  #  #  # ]:           0 :             if (at[j].el_number == EL_NUMBER_N && at[j].valence == 2 &&
    2992         [ #  # ]:           0 :                  at[j].chem_bonds_valence == at[j].valence &&
    2993         [ #  # ]:           0 :                  at[j].nNumAtInRingSystem >= 5 &&
    2994         [ #  # ]:           0 :                  at[n1 = at[j].neighbor[at[j].neighbor[0] == k]].el_number == EL_NUMBER_C && /* X is C */ 
    2995   [ #  #  #  #  :           0 :                  1 == at[j].num_H && !at[j].charge && !at[j].radical) /* djb-rwth: ignoring LLVM warning: variable used */
                   #  # ]
    2996                 :             :             {
    2997                 :           0 :                 nBackType1 = DERIV_RING_NH_OUTSIDE_PRECURSOR;
    2998                 :           0 :                 nBlockSystemFrom = at[j].nBlockSystem;
    2999                 :           0 :                 nOrdBack1 = m; /* predecessor */
    3000                 :             :             }
    3001         [ #  # ]:           0 :         if (nBlockSystemFrom)
    3002                 :             :         {
    3003                 :             :             int num1, num2, bFound;
    3004                 :           0 :             at[k].cFlags |= CFLAG_MARK_BLOCK;
    3005                 :             :             /* mark precursor atoms + at[k] */
    3006                 :           0 :             num1 = mark_atoms_cFlags( at, at[k].neighbor[nOrdBack1], 1, CFLAG_MARK_BLOCK );
    3007         [ #  # ]:           0 :             for (i = 0; i < at[k].valence; i++)
    3008                 :             :             {
    3009         [ #  # ]:           0 :                 if (i == nOrdBack1)
    3010                 :           0 :                     continue;
    3011                 :           0 :                 j = (int) at[k].neighbor[i];
    3012                 :           0 :                 bFound = 0;
    3013         [ #  # ]:           0 :                 if (at[j].cFlags & CFLAG_MARK_BLOCK)
    3014                 :             :                 {
    3015   [ #  #  #  #  :           0 :                     if (( at[j].el_number == EL_NUMBER_O || at[j].el_number == EL_NUMBER_S ) && at[j].valence == 2 &&
                   #  # ]
    3016         [ #  # ]:           0 :                          at[j].chem_bonds_valence == at[j].valence &&
    3017         [ #  # ]:           0 :                          at[j].nNumAtInRingSystem >= 5 &&
    3018         [ #  # ]:           0 :                          at[n1 = at[j].neighbor[at[j].neighbor[0] == k]].el_number == EL_NUMBER_C && /* Y is C */ 
    3019   [ #  #  #  #  :           0 :                          !at[j].num_H && !at[j].charge && !at[j].radical) /* djb-rwth: ignoring LLVM warning: variable used */
                   #  # ]
    3020                 :             :                     {
    3021                 :           0 :                         bFound = 1;
    3022         [ #  # ]:           0 :                         if (nOrdBack2 < 0)
    3023                 :             :                         {
    3024                 :           0 :                             nOrdBack2 = i; /* predecessor #2 */
    3025                 :           0 :                             nBackType2 = DERIV_RING_O_OUTSIDE_PRECURSOR;
    3026                 :             :                         }
    3027                 :             :                         else
    3028                 :             :                         {
    3029                 :           0 :                             nOrdBack3 = i; /* predecessor #3 -- should not happen */
    3030                 :             :                         }
    3031                 :             :                     }
    3032   [ #  #  #  # ]:           0 :                     if (at[j].el_number == EL_NUMBER_N && at[j].valence == 2 &&
    3033         [ #  # ]:           0 :                          at[j].chem_bonds_valence == at[j].valence &&
    3034         [ #  # ]:           0 :                          at[j].nNumAtInRingSystem >= 5 &&
    3035         [ #  # ]:           0 :                          at[n1 = at[j].neighbor[at[j].neighbor[0] == k]].el_number == EL_NUMBER_C && /* Y is C */
    3036   [ #  #  #  #  :           0 :                          1 == at[j].num_H && !at[j].charge && !at[j].radical) /* djb-rwth: ignoring LLVM warning: variable used */
                   #  # ]
    3037                 :             :                     {
    3038                 :           0 :                         bFound = 1;
    3039         [ #  # ]:           0 :                         if (nOrdBack2 < 0)
    3040                 :             :                         {
    3041                 :           0 :                             nOrdBack2 = i; /* predecessor #2 */
    3042                 :           0 :                             nBackType2 = DERIV_RING_NH_OUTSIDE_PRECURSOR;
    3043                 :             :                         }
    3044                 :             :                         else
    3045                 :             :                         {
    3046                 :           0 :                             nOrdBack3 = i; /* predecessor #3 -- should not happen */
    3047                 :             :                         }
    3048                 :             :                     }
    3049         [ #  # ]:           0 :                     if (!bFound)
    3050                 :             :                     {
    3051                 :           0 :                         nOrdBack3 = 99; /* reject: wrong neighboring atom in the same block */
    3052                 :           0 :                         break;
    3053                 :             :                     }
    3054                 :             :                 }
    3055                 :             :             }
    3056                 :           0 :             num2 = unmark_atoms_cFlags( at, k, 0, CFLAG_MARK_BLOCK, CFLAG_MARK_BLOCK_INV );
    3057         [ #  # ]:           0 :             if (num1 != num2)
    3058                 :             :             {
    3059                 :           0 :                 return -1; /* mark_atoms_cFlags() program error */
    3060                 :             :             }
    3061   [ #  #  #  # ]:           0 :             if (nOrdBack2 >= 0 && nOrdBack3 < 0)
    3062                 :             :             {
    3063                 :             :                 /* note: da1 refers to DD, which is a neighbor of 2 precursor atoms; ord point to precursor attachment points, O */
    3064         [ #  # ]:           0 :                 if (nOrdBack1 < nOrdBack2)
    3065                 :             :                 {
    3066                 :           0 :                     da1->ord[0] = nOrdBack1;  /* ord of a ring neighbor, traversed */
    3067                 :           0 :                     da1->typ[0] = nBackType1;
    3068                 :           0 :                     da1->ord[1] = nOrdBack2;  /* ord of another ring neighbor, not traversed yet */
    3069                 :           0 :                     da1->typ[1] = nBackType2;
    3070                 :             :                 }
    3071                 :             :                 else
    3072                 :             :                 {
    3073                 :           0 :                     da1->ord[0] = nOrdBack2;  /* ord of a ring neighbor, traversed */
    3074                 :           0 :                     da1->typ[0] = nBackType2;
    3075                 :           0 :                     da1->ord[1] = nOrdBack1;  /* ord of another ring neighbor, not traversed yet */
    3076                 :           0 :                     da1->typ[1] = nBackType1;
    3077                 :             :                 }
    3078                 :           0 :                 return nBackType1 | nBackType2;
    3079                 :             :             }
    3080                 :             :         }
    3081                 :             :     }
    3082                 :             : 
    3083                 :           0 :     return 0;
    3084                 :             : }
    3085                 :             : 
    3086                 :             : 
    3087                 :             : /****************************************************************************/
    3088                 :           0 : int add_to_da( DERIV_AT *da, DERIV_AT *add )
    3089                 :             : {
    3090                 :             :     /* if add has more than 1 element the elements are in ascending add.ord[] order */
    3091                 :             :     int i, j, len_da, len_add, numAddHiPri, numDaHiPri;
    3092                 :             : 
    3093   [ #  #  #  # ]:           0 :     for (len_da = 0, numDaHiPri = 0; len_da < DERIV_AT_LEN && da->typ[len_da]; len_da++)
    3094                 :             :     {
    3095                 :           0 :         numDaHiPri += ( 0 != ( da->typ[len_da] & DERIV_UNEXPADABLE ) );
    3096                 :             :     }
    3097   [ #  #  #  # ]:           0 :     for (len_add = 0, numAddHiPri = 0; len_add < DERIV_AT_LEN && da->typ[len_add]; len_add++) /* djb-rwth: addressing coverity ID #499516 -- definitely not a copy-paste error */
    3098                 :             :     {
    3099                 :           0 :         numAddHiPri += ( 0 != ( add->typ[len_add] & DERIV_UNEXPADABLE ) );
    3100                 :             :     }
    3101                 :             : 
    3102                 :             :     /* HiPri replaces non-HiPri derivatives */
    3103   [ #  #  #  # ]:           0 :     if (numAddHiPri && !numDaHiPri)
    3104                 :             :     {
    3105                 :             :         /* no harm if already len_da=0 */
    3106                 :           0 :         memset( da, 0, sizeof( *da ) ); /* djb-rwth: memset_s C11/Annex K variant? */
    3107                 :           0 :         len_da = 0;
    3108                 :             :     }
    3109                 :             :     else
    3110                 :             :     {
    3111                 :             :         /* non-HiPri derivatives cannot be added to HiPri */
    3112   [ #  #  #  # ]:           0 :         if (!numAddHiPri && numDaHiPri)
    3113                 :             :         {
    3114                 :           0 :             return 0;
    3115                 :             :         }
    3116                 :             :     }
    3117                 :             : 
    3118   [ #  #  #  # ]:           0 :     for (j = 0; j < DERIV_AT_LEN && add->typ[j]; j++)
    3119                 :             :     {
    3120         [ #  # ]:           0 :         for (i = 0; i < len_da; i++)
    3121                 :             :         {
    3122         [ #  # ]:           0 :             if (add->ord[j] == da->ord[i])
    3123                 :             :             {
    3124         [ #  # ]:           0 :                 if (add->typ[j] != da->typ[i])
    3125                 :             :                 {
    3126                 :           0 :                     return -1; /* error, should not happen */
    3127                 :             :                 }
    3128         [ #  # ]:           0 :                 if (add->num[j] != da->num[i])
    3129                 :             :                 {
    3130                 :           0 :                     return -2; /* error, should not happen */
    3131                 :             :                 }
    3132                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3133   [ #  #  #  #  :           0 :                 if ((( len_da>1 || j ) && ( add->other_atom || da->other_atom )) || (1 == len_da && add->other_atom != da->other_atom)) /* djb-rwth: addressing LLVM warning */
          #  #  #  #  #  
                #  #  # ]
    3134                 :             :                 {
    3135                 :           0 :                     return -3; /* other_atom implies single bond to cut */
    3136                 :             :                 }
    3137                 :             : #endif
    3138                 :           0 :                 break;
    3139                 :             :             }
    3140                 :             :         }
    3141                 :             : 
    3142         [ #  # ]:           0 :         if (i == len_da)
    3143                 :             :         {
    3144                 :             :             /* add->ord[j] has different ord values from all da->ord[]; add or replace */
    3145         [ #  # ]:           0 :             if (len_da < DERIV_AT_LEN)
    3146                 :             :             {
    3147                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3148   [ #  #  #  #  :           0 :                 if (( i || j ) && add->other_atom)
                   #  # ]
    3149                 :             :                 {
    3150                 :           0 :                     return -3; /* other_atom implies single bond to cut */
    3151                 :             :                 }
    3152                 :             : #endif
    3153                 :           0 :                 da->ord[i] = add->ord[j];
    3154                 :           0 :                 da->typ[i] = add->typ[j];
    3155                 :           0 :                 da->num[i] = add->num[j];
    3156                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3157                 :           0 :                 da->other_atom = add->other_atom;
    3158                 :             : #endif
    3159                 :           0 :                 len_da++;
    3160                 :             :             }
    3161                 :             :             else
    3162                 :             :             {
    3163                 :           0 :                 return -4; /* overflow, should not happen */
    3164                 :             :             }
    3165                 :             :         }
    3166                 :             :     }
    3167                 :             : 
    3168                 :           0 :     return 0;
    3169                 :             : }
    3170                 :             : 
    3171                 :             : 
    3172                 :             : /****************************************************************************
    3173                 :             : DFS search for atoms that do not have a flag
    3174                 :             : ****************************************************************************/
    3175                 :           0 : int mark_atoms_deriv( inp_ATOM *at,
    3176                 :             :                       DERIV_AT *da,
    3177                 :             :                       int start,
    3178                 :             :                       int num,
    3179                 :             :                       char cFlags,
    3180                 :             :                       int *pbFound )
    3181                 :             : {
    3182                 :           0 :     int i, nFound = 0, ret; /* djb-rwth: removing redundant variables */
    3183                 :             :     DERIV_AT da1;
    3184                 :             :     int      ret2;   /* moved from below 2024-09-01 DT */
    3185                 :             :     DERIV_AT da2;    /* moved from below 2024-09-01 DT */
    3186                 :           0 :     da1.other_atom = 0; /* djb-rwth: initialisation needed for if conditons */
    3187                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3188                 :             :     /* djb-rwth: initialisation needed to avoid garbage values in add_to_da function call; fixing coverity ID #499492 */
    3189                 :           0 :     memset(da2.typ, 0, DERIV_AT_LEN * sizeof(da2.typ[0]));
    3190                 :           0 :     memset(da2.ord, '\0', DERIV_AT_LEN * sizeof(da2.ord[0]));
    3191                 :           0 :     memset(da2.num, '\0', DERIV_AT_LEN * sizeof(da2.num[0]));
    3192                 :           0 :     da2.other_atom = 0; /* djb-rwth: initialisation needed for if conditons */
    3193                 :             : #endif
    3194         [ #  # ]:           0 :     if (!( at[start].cFlags & cFlags ))
    3195                 :             :     {
    3196         [ #  # ]:           0 :         if (DERIV_NOT == ( ret = get_traversed_deriv_type( at, da, start, &da1, cFlags ) ))
    3197                 :             :         {
    3198                 :           0 :             nFound++; /* at[start] cannot belong to a derivatizing agent */
    3199                 :             :         }
    3200                 :           0 :         num++;
    3201                 :           0 :         at[start].cFlags |= cFlags;
    3202         [ #  # ]:           0 :         if (da1.typ[0])
    3203                 :             :         {
    3204                 :             :             /* possibly a derivatization agent attachment point. */
    3205                 :             :             /* check neighbors that have not been traversed yet */
    3206                 :           0 :             int n1 = 0, n2 = 0, i1 = -1, i2 = -1, nFound1 = 0, nFound2 = 0;
    3207   [ #  #  #  #  :           0 :             switch (da1.typ[0])
             #  #  #  # ]
    3208                 :             :             {
    3209                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3210                 :           0 :                 case DERIV_RING_DMOX_DEOX_N:
    3211                 :             :                 case DERIV_RING_DMOX_DEOX_O:
    3212                 :           0 :                     ret2 = get_traversed_deriv_type( at, da, da1.other_atom - 1, &da2, cFlags );
    3213         [ #  # ]:           0 :                     if (ret != ( ret2 ^ DERIV_RING_DMOX_DEOX ))
    3214                 :             :                     {
    3215                 :             :                         /* bug */
    3216                 :           0 :                         nFound++; /* at[start] cannot belong to a derivatizing agent */
    3217                 :           0 :                         goto check_neighbors; /* bypass add_to_da( da+start, &da1 ) */
    3218                 :             :                     }
    3219                 :             :                     /*at[da1.other_atom-1].cFlags |= cFlags;*/
    3220                 :           0 :                     n1 = da1.num[0]; /* terminal fragment has been identified; don't search subfragments */
    3221                 :           0 :                     nFound++;
    3222                 :           0 :                     break;
    3223                 :             : #endif
    3224                 :             : #ifdef DERIV_X_OXIME
    3225                 :           0 :                 case DERIV_X_OXIME:
    3226                 :           0 :                     n1 = da1.num[0]; /* terminal fragment has been identified; don't search subfragments */
    3227                 :           0 :                     nFound++;
    3228                 :           0 :                     break;
    3229                 :             : #endif
    3230                 :             : #ifdef DERIV_RO_COX
    3231                 :           0 :                 case DERIV_RO_COX:
    3232                 :           0 :                     n1 = da1.num[0]; /* terminal fragment has been identified; don't search subfragments */
    3233                 :           0 :                     nFound++;
    3234                 :           0 :                     break;
    3235                 :             : #endif
    3236                 :             : #if( defined(DERIV_RING2_PRRLDD_OUTSIDE_PRECUR) || defined(DERIV_RING2_PPRDN_OUTSIDE_PRECUR) || defined(DERIV_DANSYL) )
    3237                 :             : #ifdef DERIV_RING2_PRRLDD_OUTSIDE_PRECUR
    3238                 :           0 :                 case DERIV_RING2_PRRLDD_OUTSIDE_PRECUR:
    3239                 :             : #endif
    3240                 :             : #ifdef DERIV_RING2_PPRDN_OUTSIDE_PRECUR
    3241                 :             :                 case DERIV_RING2_PPRDN_OUTSIDE_PRECUR:
    3242                 :             : #endif
    3243                 :             : #ifdef DERIV_DANSYL
    3244                 :             :                 case DERIV_DANSYL:
    3245                 :             : #endif
    3246                 :           0 :                     n1 = da1.num[0]; /* terminal fragment has been identified; don't search subfragments */
    3247                 :           0 :                     nFound++;
    3248                 :           0 :                     break;
    3249                 :             : #endif
    3250                 :             : 
    3251                 :           0 :                 case DERIV_BRIDGE_O:
    3252                 :             :                 case DERIV_BRIDGE_NH:
    3253                 :           0 :                     n1 = mark_atoms_deriv( at, da, at[start].neighbor[(int) da1.ord[0]], 0, cFlags, &nFound1 );
    3254   [ #  #  #  # ]:           0 :                     if (n1 > MAX_AT_DERIV || nFound1)
    3255                 :             :                     {
    3256                 :           0 :                         da1.typ[0] = 0;
    3257                 :             :                     }
    3258                 :             :                     else
    3259                 :             :                     {
    3260                 :           0 :                         da1.num[0] = n1;
    3261                 :           0 :                         nFound++;
    3262                 :             :                     }
    3263                 :           0 :                     break;
    3264                 :           0 :                 case DERIV_AMINE_tN:
    3265                 :           0 :                     n1 = mark_atoms_deriv( at, da, at[start].neighbor[i1 = da1.ord[0]], 0, cFlags, &nFound1 ); /* djb-rwth: ignoring LLVM warning: variable used */
    3266         [ #  # ]:           0 :                     if (da1.typ[1])
    3267                 :             :                     {
    3268                 :           0 :                         n2 = mark_atoms_deriv( at, da, at[start].neighbor[i2 = da1.ord[1]], 0, cFlags, &nFound2 ); /* djb-rwth: ignoring LLVM warning: variable used */
    3269                 :             :                     }
    3270   [ #  #  #  #  :           0 :                     if (0 < n1 && n1 <= MAX_AT_DERIV && !nFound1)
                   #  # ]
    3271                 :             :                     {
    3272                 :           0 :                         da1.num[0] = n1;
    3273                 :           0 :                         i = 1;
    3274                 :           0 :                         nFound++;
    3275                 :             :                     }
    3276                 :             :                     else
    3277                 :             :                     {
    3278                 :           0 :                         da1.ord[0] = da1.ord[1];
    3279                 :           0 :                         da1.num[0] = da1.num[1];
    3280                 :           0 :                         da1.typ[0] = da1.typ[1];
    3281                 :           0 :                         da1.typ[1] = 0;
    3282                 :           0 :                         i = 0;
    3283                 :             :                     }
    3284   [ #  #  #  #  :           0 :                     if (0 < n2 && n2 <= MAX_AT_DERIV && !nFound2)
                   #  # ]
    3285                 :             :                     {
    3286                 :           0 :                         da1.num[i] = n2;
    3287                 :           0 :                         nFound++;
    3288                 :             :                     }
    3289                 :             :                     else
    3290                 :             :                     {
    3291                 :           0 :                         da1.typ[i] = 0;
    3292                 :             :                     }
    3293                 :           0 :                     break;
    3294                 :           0 :                 case DERIV_RING_O_OUTSIDE_PRECURSOR:
    3295                 :             :                 case DERIV_RING_NH_OUTSIDE_PRECURSOR:
    3296         [ #  # ]:           0 :                     for (i = 0; i < at[start].valence; i++)
    3297                 :             :                     {
    3298   [ #  #  #  #  :           0 :                         if (i != da1.ord[0] && i != da1.ord[1] && !( at[at[start].neighbor[i]].cFlags & cFlags ))
                   #  # ]
    3299                 :             :                         {
    3300         [ #  # ]:           0 :                             if (!n1)
    3301                 :             :                             {
    3302                 :           0 :                                 n1 = mark_atoms_deriv( at, da, at[start].neighbor[i1 = i], 0, cFlags, &nFound1 ); /* djb-rwth: ignoring LLVM warning: variable used */
    3303                 :             :                             }
    3304                 :             :                             else
    3305                 :             :                             {
    3306                 :           0 :                                 n2 = mark_atoms_deriv( at, da, at[start].neighbor[i2 = i], 0, cFlags, &nFound2 ); /* djb-rwth: ignoring LLVM warning: variable used */
    3307                 :             :                             }
    3308                 :             :                         }
    3309                 :             :                     }
    3310   [ #  #  #  #  :           0 :                     if (n1 + n2 + 1 > MAX_AT_DERIV || nFound1 || nFound2)
                   #  # ]
    3311                 :             :                     {
    3312                 :           0 :                         da1.typ[1] = da1.typ[0] = 0;
    3313                 :             :                     }
    3314                 :             :                     else
    3315                 :             :                     {
    3316                 :           0 :                         da1.num[0] = n1;
    3317                 :           0 :                         da1.num[1] = n2;
    3318                 :           0 :                         nFound++;
    3319                 :             :                     }
    3320                 :           0 :                     break;
    3321                 :             :             }
    3322                 :             : 
    3323                 :             :             /* --------- end of switch( da1.typ[0] ) ------- */
    3324         [ #  # ]:           0 :             if (n1 < 0)
    3325                 :             :             {
    3326                 :           0 :                 return n1;
    3327                 :             :             }
    3328         [ #  # ]:           0 :             if (n2 < 0)
    3329                 :             :             {
    3330                 :           0 :                 return n2; /* errors */
    3331                 :             :             }
    3332                 :             : 
    3333                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3334         [ #  # ]:           0 :             if (da1.other_atom)
    3335                 :             :             {
    3336         [ #  # ]:           0 :                 if (da2.other_atom == start + 1)
    3337                 :             :                 {
    3338         [ #  # ]:           0 :                     if ((i = add_to_da( da + da1.other_atom - 1, &da2 ))) /* djb-rwth: addressing LLVM warning */
    3339                 :             :                     {
    3340                 :           0 :                         return i;  /* error */
    3341                 :             :                     }
    3342                 :             :                 }
    3343                 :             :                 else
    3344                 :             :                 {
    3345                 :           0 :                     return -4; /* error */
    3346                 :             :                 }
    3347                 :             :             }
    3348                 :             : #endif
    3349         [ #  # ]:           0 :             if ((i = add_to_da( da + start, &da1 ))) /* djb-rwth: addressing LLVM warning */
    3350                 :             :             {
    3351                 :           0 :                 return i;  /* error */
    3352                 :             :             }
    3353                 :           0 :             *pbFound += nFound1 + nFound2 + nFound;
    3354                 :           0 :             num += n1 + n2;
    3355                 :             :         }
    3356                 :             :         else
    3357                 :             :         {
    3358                 :           0 :             *pbFound += nFound;
    3359                 :             :         }
    3360                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    3361                 :           0 :         check_neighbors:
    3362                 :             : #endif
    3363         [ #  # ]:           0 :                        for (i = 0; i < at[start].valence; i++)
    3364                 :             :                        {
    3365                 :           0 :                            num = mark_atoms_deriv( at, da, at[start].neighbor[i], num, cFlags, pbFound );
    3366         [ #  # ]:           0 :                            if (num < 0)
    3367                 :             :                            {
    3368                 :           0 :                                return num;
    3369                 :             :                            }
    3370                 :             :                        }
    3371                 :             :     }
    3372                 :             :     /* *pbFound =  number of derivatizer attachment points traversed forward from at[start] */
    3373                 :             : 
    3374                 :           0 :     return num; /* number of atoms traversed forward from at[start] */
    3375                 :             : }
    3376                 :             : 
    3377                 :             : 
    3378                 :             : /****************************************************************************/
    3379                 :           0 : int count_one_bond_atoms( inp_ATOM *at,
    3380                 :             :                           DERIV_AT *da,
    3381                 :             :                           int start,
    3382                 :             :                           int ord,
    3383                 :             :                           char cFlags,
    3384                 :             :                           int *bFound )
    3385                 :             : {
    3386                 :           0 :     int num = 0;
    3387         [ #  # ]:           0 :     if (!( at[at[start].neighbor[ord]].cFlags & cFlags ))
    3388                 :             :     {
    3389                 :           0 :         at[at[start].neighbor[ord]].cFlags |= cFlags;
    3390                 :           0 :         num++;
    3391                 :           0 :         num = mark_atoms_deriv( at, da, start, num, cFlags, bFound );
    3392                 :             :     }
    3393                 :             : 
    3394                 :           0 :     return num;
    3395                 :             : }
    3396                 :             : 
    3397                 :             : 
    3398                 :             : /****************************************************************************
    3399                 :             : List of allowed derivatives
    3400                 :             : 
    3401                 :             : Legend:
    3402                 :             : 
    3403                 :             : ->- marks the bond to be disconnexted: X->-Y => XD + TY
    3404                 :             : where TY is a derivatizing agent eventually to be discarded
    3405                 :             : 
    3406                 :             : Allowed Derivative Types List
    3407                 :             : =============================
    3408                 :             : 
    3409                 :             : DERIV_BRIDGE_O, DERIV_BRIDGE_NH, DERIV_AMINE_tN
    3410                 :             : -----------------------------------------------
    3411                 :             : CH3                CH3  CH3           CH3   CH3
    3412                 :             : |                  |    |             |     |
    3413                 :             : X->-Si--CH3        X->-Si---Si--CH3   X->-Si----C--CH3  X= O, NH, N
    3414                 :             : |                  |    |             |     |
    3415                 :             : CH3                CH3  CH3           CH3   CH3
    3416                 :             : 
    3417                 :             : 4 atoms           7 atoms             7 atoms        is_silyl()
    3418                 :             : - eliminated
    3419                 :             : 
    3420                 :             : 
    3421                 :             : 
    3422                 :             : O                 O                     O      F         O                  O
    3423                 :             : ||                ||                    ||     |         ||                 ||
    3424                 :             : R--C--O->-CH3     R--C--O->-CH2--CH3    R--C--O->-C--F   R--C--O->-CF2-CF3  R--C--O->-CF2-CF2-CF3
    3425                 :             : |
    3426                 :             : F
    3427                 :             : 
    3428                 :             : 
    3429                 :             : 
    3430                 :             : 1 atom             2 atoms            4 atoms     7 atoms          10 atoms
    3431                 :             : is_Me_or_Et()      is_Me_or_Et()         is_CF3_or_linC3F7()
    3432                 :             : 
    3433                 :             : 
    3434                 :             : A. DERIV_BRIDGE_NH, DERIV_AMINE_tN
    3435                 :             : -----------------------------------
    3436                 :             : 
    3437                 :             : 
    3438                 :             : O                 O                     O   F             O                O
    3439                 :             : ||                ||                    ||  |             ||               ||
    3440                 :             : N->-C--CH3        N->-C--CH2--CH3       N->-C---C--F      N->-C--CF2-CF3   N->-C--CF2-CF2-CF3
    3441                 :             : |
    3442                 :             : F
    3443                 :             : 
    3444                 :             : 
    3445                 :             : 
    3446                 :             : 3 atoms           5 atoms              8 atoms           7 atoms         12 atoms
    3447                 :             : is_Me_or_Et()     is_Me_or_Et()              is_CF3_or_linC3F7()
    3448                 :             : 
    3449                 :             : DERIV_RING_O_OUTSIDE_PRECURSOR (da contains >B- or >C< or >CH- atom)
    3450                 :             : ------------
    3451                 :             : 
    3452                 :             : C----O               R----O                  R----O
    3453                 :             : |     \              |     \     CH3         |     \
    3454                 :             : |      >             |      >   /            |      >
    3455                 :             : |       \            |       \ /             |       \
    3456                 :             : |        B--X        |        C              |        CH--Ph
    3457                 :             : |       /            |       / \             |       /
    3458                 :             : |      >             |      >   \            |      >
    3459                 :             : |     /              |     /     CH3         |     /
    3460                 :             : C----O               R----O                  R----O
    3461                 :             : 
    3462                 :             : 5-member             5 or 6-member           5 or 6-member
    3463                 :             : X=Me,Et, n-Butyl
    3464                 :             : 
    3465                 :             : 2 atoms              3 atoms                 7 atoms
    3466                 :             : 
    3467                 :             : DERIV_RING_NH_OUTSIDE_PRECURSOR
    3468                 :             : ------------
    3469                 :             : 
    3470                 :             : None in the list
    3471                 :             : 
    3472                 :             : ****************************************************************************/
    3473                 :             : 
    3474                 :             : 
    3475                 :             : /****************************************************************************/
    3476                 :           0 : int is_silyl( inp_ATOM *at, int start, int ord_prev )
    3477                 :             : {
    3478                 :           0 :     int i, neigh, num_Me = 0, iC_IV = -1, iSi_IV = -1, i_C_or_Si_IV;
    3479                 :             : 
    3480   [ #  #  #  # ]:           0 :     if (at[start].el_number != EL_NUMBER_SI || at[start].valence != 4 ||
    3481         [ #  # ]:           0 :          at[start].valence != at[start].chem_bonds_valence ||
    3482   [ #  #  #  # ]:           0 :          at[start].charge || at[start].radical)
    3483                 :             :     {
    3484                 :           0 :         return 0;
    3485                 :             :     }
    3486                 :             : 
    3487         [ #  # ]:           0 :     for (i = 0; i < at[start].valence; i++)
    3488                 :             :     {
    3489         [ #  # ]:           0 :         if (i == ord_prev)
    3490                 :             :         {
    3491                 :           0 :             continue;
    3492                 :             :         }
    3493                 :           0 :         neigh = at[start].neighbor[i];
    3494   [ #  #  #  # ]:           0 :         if (at[neigh].charge || at[neigh].radical ||
    3495         [ #  # ]:           0 :              at[neigh].valence != at[neigh].chem_bonds_valence)
    3496                 :             :         {
    3497                 :           0 :             return 0;
    3498                 :             :         }
    3499         [ #  # ]:           0 :         if (at[neigh].valence == 4)
    3500                 :             :         {
    3501   [ #  #  #  #  :           0 :             if (at[neigh].el_number == EL_NUMBER_C && iC_IV < 0 && iSi_IV < 0)
                   #  # ]
    3502                 :             :             {
    3503                 :           0 :                 iC_IV = neigh;
    3504                 :             :             }
    3505                 :             :             else
    3506                 :             :             {
    3507   [ #  #  #  #  :           0 :                 if (at[neigh].el_number == EL_NUMBER_SI && iC_IV < 0 && iSi_IV < 0)
                   #  # ]
    3508                 :             :                 {
    3509                 :           0 :                     iSi_IV = neigh;
    3510                 :             :                 }
    3511                 :             :                 else
    3512                 :             :                 {
    3513                 :           0 :                     return 0;
    3514                 :             :                 }
    3515                 :             :             }
    3516                 :             :         }
    3517                 :             :         else
    3518                 :             :         {
    3519         [ #  # ]:           0 :             if (at[neigh].valence == 1 &&
    3520         [ #  # ]:           0 :                  at[neigh].valence == at[neigh].chem_bonds_valence &&
    3521   [ #  #  #  # ]:           0 :                  at[neigh].el_number == EL_NUMBER_C && at[neigh].num_H == 3)
    3522                 :             :             {
    3523                 :           0 :                 num_Me++;
    3524                 :             :             }
    3525                 :             :             else
    3526                 :             :             {
    3527                 :           0 :                 return 0;
    3528                 :             :             }
    3529                 :             :         }
    3530                 :             :     }
    3531                 :             : 
    3532   [ #  #  #  #  :           0 :     if (num_Me == 3 && iC_IV < 0 && iSi_IV < 0)
                   #  # ]
    3533                 :             :     {
    3534                 :           0 :         return 1; /* Si(CH3)3 */
    3535                 :             :     }
    3536                 :             : 
    3537                 :             :     /* next C(IV) or Si(IV) */
    3538                 :             :     /* this is a fix requested by Anz. and suggested by Gary 09/21/2011
    3539                 :             :     it rejects -Si(CH3)2-Si(CH3)3 and allows only -Si(CH3)2-C(CH3)3
    3540                 :             :     */
    3541                 :           0 :     i_C_or_Si_IV = iC_IV >= 0 ? iC_IV : -1;
    3542   [ #  #  #  # ]:           0 :     if (num_Me != 2 || i_C_or_Si_IV < 0)
    3543                 :             :     {
    3544                 :           0 :         return 0;
    3545                 :             :     }
    3546                 :             : 
    3547                 :           0 :     num_Me = 0;
    3548         [ #  # ]:           0 :     for (i = 0; i < at[i_C_or_Si_IV].valence; i++)
    3549                 :             :     {
    3550                 :           0 :         neigh = at[i_C_or_Si_IV].neighbor[i];
    3551         [ #  # ]:           0 :         if (neigh == start)
    3552                 :             :         {
    3553                 :           0 :             continue;
    3554                 :             :         }
    3555   [ #  #  #  # ]:           0 :         if (at[neigh].charge || at[neigh].radical ||
    3556         [ #  # ]:           0 :              at[neigh].valence != at[neigh].chem_bonds_valence)
    3557                 :             :         {
    3558                 :           0 :             return 0;
    3559                 :             :         }
    3560         [ #  # ]:           0 :         if (at[neigh].valence == 1 &&
    3561         [ #  # ]:           0 :              at[neigh].valence == at[neigh].chem_bonds_valence &&
    3562   [ #  #  #  # ]:           0 :              at[neigh].el_number == EL_NUMBER_C && at[neigh].num_H == 3)
    3563                 :             :         {
    3564                 :           0 :             num_Me++;
    3565                 :             :         }
    3566                 :             :         else
    3567                 :             :         {
    3568                 :           0 :             return 0;
    3569                 :             :         }
    3570                 :             :     }
    3571         [ #  # ]:           0 :     if (num_Me == 3)
    3572                 :             :     {
    3573                 :           0 :         return 2; /* Si(CH3)2Si/C(CH3)3 */
    3574                 :             :     }
    3575                 :             : 
    3576                 :           0 :     return 0;
    3577                 :             : }
    3578                 :             : 
    3579                 :             : 
    3580                 :             : /****************************************************************************/
    3581                 :           0 : int is_silyl2( inp_ATOM *at, int start, int from_at )
    3582                 :             : {
    3583                 :           0 :     int i, neigh, num_Me = 0, iC_IV = -1;
    3584                 :             : 
    3585   [ #  #  #  # ]:           0 :     if (at[start].el_number != EL_NUMBER_SI || at[start].valence != 4 ||
    3586         [ #  # ]:           0 :          at[start].valence != at[start].chem_bonds_valence ||
    3587   [ #  #  #  # ]:           0 :          at[start].charge || at[start].radical)
    3588                 :             :     {
    3589                 :           0 :         return 0;
    3590                 :             :     }
    3591                 :             : 
    3592         [ #  # ]:           0 :     for (i = 0; i < at[start].valence; i++)
    3593                 :             :     {
    3594                 :           0 :         neigh = at[start].neighbor[i];
    3595         [ #  # ]:           0 :         if (neigh == from_at)
    3596                 :             :         {
    3597                 :           0 :             continue;
    3598                 :             :         }
    3599   [ #  #  #  # ]:           0 :         if (at[neigh].charge || at[neigh].radical ||
    3600         [ #  # ]:           0 :              at[neigh].valence != at[neigh].chem_bonds_valence)
    3601                 :             :         {
    3602                 :           0 :             return 0;
    3603                 :             :         }
    3604         [ #  # ]:           0 :         if (at[neigh].valence == 4)
    3605                 :             :         {
    3606   [ #  #  #  # ]:           0 :             if (at[neigh].el_number == EL_NUMBER_C && iC_IV < 0)
    3607                 :             :             {
    3608                 :           0 :                 iC_IV = neigh;
    3609                 :             :             }
    3610                 :             :             else
    3611                 :             :             {
    3612                 :           0 :                 return 0;
    3613                 :             :             }
    3614                 :             :         }
    3615                 :             :         else
    3616                 :             :         {
    3617         [ #  # ]:           0 :             if (at[neigh].valence == 1 &&
    3618         [ #  # ]:           0 :                  at[neigh].valence == at[neigh].chem_bonds_valence &&
    3619   [ #  #  #  # ]:           0 :                  at[neigh].el_number == EL_NUMBER_C && at[neigh].num_H == 3)
    3620                 :             :             {
    3621                 :           0 :                 num_Me++;
    3622                 :             :             }
    3623                 :             :             else
    3624                 :             :             {
    3625                 :           0 :                 return 0;
    3626                 :             :             }
    3627                 :             :         }
    3628                 :             :     }
    3629   [ #  #  #  # ]:           0 :     if (num_Me == 3 && iC_IV < 0)
    3630                 :             :     {
    3631                 :           0 :         return 1; /* Si(CH3)3 */
    3632                 :             :     }
    3633                 :             : 
    3634                 :             :     /* next C(IV) */
    3635   [ #  #  #  # ]:           0 :     if (num_Me != 2 || iC_IV < 0)
    3636                 :             :     {
    3637                 :           0 :         return 0;
    3638                 :             :     }
    3639                 :             : 
    3640                 :           0 :     num_Me = 0;
    3641         [ #  # ]:           0 :     for (i = 0; i < at[iC_IV].valence; i++)
    3642                 :             :     {
    3643                 :           0 :         neigh = at[iC_IV].neighbor[i];
    3644         [ #  # ]:           0 :         if (neigh == start)
    3645                 :             :         {
    3646                 :           0 :             continue;
    3647                 :             :         }
    3648   [ #  #  #  # ]:           0 :         if (at[neigh].charge || at[neigh].radical ||
    3649         [ #  # ]:           0 :              at[neigh].valence != at[neigh].chem_bonds_valence)
    3650                 :             :         {
    3651                 :           0 :             return 0;
    3652                 :             :         }
    3653         [ #  # ]:           0 :         if (at[neigh].valence == 1 &&
    3654         [ #  # ]:           0 :              at[neigh].valence == at[neigh].chem_bonds_valence &&
    3655   [ #  #  #  # ]:           0 :              at[neigh].el_number == EL_NUMBER_C && at[neigh].num_H == 3)
    3656                 :             :         {
    3657                 :           0 :             num_Me++;
    3658                 :             :         }
    3659                 :             :         else
    3660                 :             :         {
    3661                 :           0 :             return 0;
    3662                 :             :         }
    3663                 :             :     }
    3664         [ #  # ]:           0 :     if (num_Me == 3)
    3665                 :             :     {
    3666                 :           0 :         return 2; /* Si(CH3)2-Si/C(CH3)3, not Si(CH3)2-Si(CH3)3 */
    3667                 :             :     }
    3668                 :             : 
    3669                 :           0 :     return 0;
    3670                 :             : }
    3671                 :             : 
    3672                 :             : 
    3673                 :             : /****************************************************************************/
    3674                 :           0 : int is_nButyl( inp_ATOM *at, int start, int ord_prev )
    3675                 :             : {
    3676                 :           0 :     int i, next, curr = start;
    3677                 :           0 :     int prev = at[curr].neighbor[ord_prev];
    3678                 :           0 :     const int len = 4;
    3679         [ #  # ]:           0 :     for (i = 0; i < len; i++)
    3680                 :             :     {
    3681   [ #  #  #  # ]:           0 :         if (at[curr].el_number != EL_NUMBER_C || at[curr].valence > 2 ||
    3682         [ #  # ]:           0 :              at[curr].valence != at[curr].chem_bonds_valence ||
    3683         [ #  # ]:           0 :              at[curr].chem_bonds_valence + at[curr].num_H != 4 ||
    3684   [ #  #  #  # ]:           0 :              at[curr].charge || at[curr].radical)
    3685                 :             :         {
    3686                 :           0 :             return 0;
    3687                 :             :         }
    3688         [ #  # ]:           0 :         if (at[curr].valence == 2)
    3689                 :             :         {
    3690                 :           0 :             next = at[curr].neighbor[prev == at[curr].neighbor[0]];
    3691                 :           0 :             prev = curr;
    3692                 :           0 :             curr = next;
    3693                 :             :         }
    3694                 :             :         else
    3695                 :             :         {
    3696                 :           0 :             return i + 1 == len;
    3697                 :             :         }
    3698                 :             :     }
    3699                 :             : 
    3700                 :           0 :     return 0;
    3701                 :             : }
    3702                 :             : 
    3703                 :             : 
    3704                 :             : /****************************************************************************/
    3705                 :           0 : int is_Me_or_Et( inp_ATOM *at, int start, int ord_prev )
    3706                 :             : {
    3707                 :           0 :     int i, neigh = -1;
    3708   [ #  #  #  # ]:           0 :     if (at[start].el_number != EL_NUMBER_C || at[start].valence > 2 ||
    3709         [ #  # ]:           0 :          at[start].valence != at[start].chem_bonds_valence ||
    3710         [ #  # ]:           0 :          at[start].chem_bonds_valence + at[start].num_H != 4 ||
    3711   [ #  #  #  # ]:           0 :          at[start].charge || at[start].radical)
    3712                 :           0 :         return 0;
    3713         [ #  # ]:           0 :     for (i = 0; i < at[start].valence; i++)
    3714                 :             :     {
    3715         [ #  # ]:           0 :         if (i == ord_prev)
    3716                 :             :         {
    3717                 :           0 :             continue;
    3718                 :             :         }
    3719         [ #  # ]:           0 :         if (neigh >= 0)
    3720                 :             :         {
    3721                 :           0 :             return 0;
    3722                 :             :         }
    3723                 :             : 
    3724                 :           0 :         neigh = at[start].neighbor[i];
    3725   [ #  #  #  # ]:           0 :         if (at[neigh].el_number != EL_NUMBER_C || at[neigh].valence > 1 ||
    3726         [ #  # ]:           0 :              at[neigh].valence != at[neigh].chem_bonds_valence ||
    3727         [ #  # ]:           0 :              at[neigh].chem_bonds_valence + at[neigh].num_H != 4 ||
    3728   [ #  #  #  # ]:           0 :              at[neigh].charge || at[neigh].radical)
    3729                 :             :         {
    3730                 :           0 :             return 0;
    3731                 :             :         }
    3732                 :             :     }
    3733                 :             : 
    3734         [ #  # ]:           0 :     return 1 + ( neigh >= 0 );
    3735                 :             : }
    3736                 :             : 
    3737                 :             : 
    3738                 :             : #ifdef NEVER
    3739                 :             : 
    3740                 :             : /****************************************************************************
    3741                 :             : CF3
    3742                 :             : -CF3  or -CF<
    3743                 :             : CF3
    3744                 :             : ****************************************************************************/
    3745                 :             : 
    3746                 :             : /****************************************************************************/
    3747                 :             : int is_CF3_or_isoC3F7( inp_ATOM *at, int start, int ord_prev )
    3748                 :             : {
    3749                 :             :     int i, k, num_C_IV, iC_IV[2], neigh, num_F, iC;
    3750                 :             :     if (at[start].el_number != EL_NUMBER_C || at[start].valence != 4 ||
    3751                 :             :          at[start].valence != at[start].chem_bonds_valence ||
    3752                 :             :          at[start].chem_bonds_valence + at[start].num_H != 4 ||
    3753                 :             :          at[start].charge || at[start].radical)
    3754                 :             :         return 0;
    3755                 :             : 
    3756                 :             :     iC_IV[0] = iC_IV[1] = num_F = 0;
    3757                 :             : 
    3758                 :             :     for (i = num_C_IV = 0; i < at[start].valence; i++)
    3759                 :             :     {
    3760                 :             :         if (i == ord_prev)
    3761                 :             :         {
    3762                 :             :             continue;
    3763                 :             :         }
    3764                 :             : 
    3765                 :             :         neigh = at[start].neighbor[i];
    3766                 :             :         if (at[neigh].valence != at[neigh].chem_bonds_valence ||
    3767                 :             :              at[neigh].charge || at[neigh].radical)
    3768                 :             :             return 0;
    3769                 :             :         if (at[neigh].el_number == EL_NUMBER_F)
    3770                 :             :         {
    3771                 :             :             if (at[neigh].chem_bonds_valence + at[neigh].num_H != 1)
    3772                 :             :             {
    3773                 :             :                 return 0;
    3774                 :             :             }
    3775                 :             :             num_F++;
    3776                 :             :         }
    3777                 :             :         else
    3778                 :             :         {
    3779                 :             :             if (at[neigh].el_number == EL_NUMBER_C &&
    3780                 :             :                  at[neigh].valence == 4 &&
    3781                 :             :                  !at[neigh].num_H && !at[neigh].charge && !at[neigh].radical && num_C_IV < 2)
    3782                 :             :             {
    3783                 :             : 
    3784                 :             :                 if (num_C_IV > 1)
    3785                 :             :                     return 0;
    3786                 :             : 
    3787                 :             :                 iC_IV[num_C_IV++] = neigh;
    3788                 :             :             }
    3789                 :             :         }
    3790                 :             :     }
    3791                 :             :     if (!num_C_IV && 3 == num_F)
    3792                 :             :     {
    3793                 :             :         return 1; /* -CF3 */
    3794                 :             :     }
    3795                 :             :     if (2 != num_C_IV || 1 != num_F)
    3796                 :             :     {
    3797                 :             :         return 0;
    3798                 :             :     }
    3799                 :             : 
    3800                 :             :     /* detect iso-C3F7 */
    3801                 :             :     for (k = 0; k < num_C_IV; k++)
    3802                 :             :     {
    3803                 :             :         iC = iC_IV[k];
    3804                 :             :         num_F = 0;
    3805                 :             :         for (i = 0; i < at[iC].valence; i++)
    3806                 :             :         {
    3807                 :             :             neigh = at[iC].neighbor[i];
    3808                 :             :             if (neigh == start)
    3809                 :             :             {
    3810                 :             :                 continue;
    3811                 :             :             }
    3812                 :             :             if (at[neigh].valence != at[neigh].chem_bonds_valence ||
    3813                 :             :                  at[neigh].charge || at[neigh].radical)
    3814                 :             :             {
    3815                 :             :                 return 0;
    3816                 :             :             }
    3817                 :             :             if (at[neigh].el_number == EL_NUMBER_F)
    3818                 :             :             {
    3819                 :             :                 if (at[neigh].chem_bonds_valence + at[neigh].num_H != 1)
    3820                 :             :                 {
    3821                 :             :                     return 0;
    3822                 :             :                 }
    3823                 :             :                 num_F++;
    3824                 :             :             }
    3825                 :             :             else
    3826                 :             :             {
    3827                 :             :                 return 0;
    3828                 :             :             }
    3829                 :             :         }
    3830                 :             :         if (num_F != 3)
    3831                 :             :         {
    3832                 :             :             return 0;
    3833                 :             :         }
    3834                 :             :     }
    3835                 :             : 
    3836                 :             :     return 2; /* iso-C3F7 */
    3837                 :             : }
    3838                 :             : 
    3839                 :             : #endif
    3840                 :             : 
    3841                 :             : 
    3842                 :             : /****************************************************************************/
    3843                 :           0 : int is_CF3_or_linC3F7a( inp_ATOM *at, int start, int iat_prev )
    3844                 :             : {
    3845                 :             :     int i;
    3846                 :             : 
    3847         [ #  # ]:           0 :     for (i = 0; i < at[start].valence; i++)
    3848                 :             :     {
    3849         [ #  # ]:           0 :         if (iat_prev == at[start].neighbor[i])
    3850                 :             :         {
    3851                 :           0 :             return is_CF3_or_linC3F7( at, start, i );
    3852                 :             :         }
    3853                 :             :     }
    3854                 :             : 
    3855                 :           0 :     return 0;
    3856                 :             : }
    3857                 :             : 
    3858                 :             : 
    3859                 :             : /****************************************************************************/
    3860                 :           0 : int is_CF3_or_linC3F7( inp_ATOM *at, int start, int ord_prev )
    3861                 :             : {
    3862                 :           0 :     int i, num_C_IV, iC_IV, neigh, num_F, num_C = 0;
    3863                 :             :     AT_NUMB *p;
    3864                 :             : 
    3865         [ #  # ]:           0 :     while (num_C < 4)
    3866                 :             :     {
    3867                 :             : 
    3868   [ #  #  #  # ]:           0 :         if (at[start].el_number != EL_NUMBER_C || at[start].valence != 4 ||
    3869         [ #  # ]:           0 :              at[start].valence != at[start].chem_bonds_valence ||
    3870         [ #  # ]:           0 :              at[start].chem_bonds_valence + at[start].num_H != 4 ||
    3871   [ #  #  #  # ]:           0 :              at[start].charge || at[start].radical)
    3872                 :             :         {
    3873                 :           0 :             return 0;
    3874                 :             :         }
    3875                 :             : 
    3876                 :           0 :         iC_IV = num_F = 0;
    3877                 :             : 
    3878         [ #  # ]:           0 :         for (i = num_C_IV = 0; i < at[start].valence; i++)
    3879                 :             :         {
    3880         [ #  # ]:           0 :             if (i == ord_prev)
    3881                 :             :             {
    3882                 :           0 :                 continue;
    3883                 :             :             }
    3884                 :             : 
    3885                 :           0 :             neigh = at[start].neighbor[i];
    3886         [ #  # ]:           0 :             if (at[neigh].valence != at[neigh].chem_bonds_valence ||
    3887   [ #  #  #  # ]:           0 :                  at[neigh].charge || at[neigh].radical)
    3888                 :             :             {
    3889                 :           0 :                 return 0;
    3890                 :             :             }
    3891         [ #  # ]:           0 :             if (at[neigh].el_number == EL_NUMBER_F)
    3892                 :             :             {
    3893         [ #  # ]:           0 :                 if (at[neigh].chem_bonds_valence + at[neigh].num_H != 1)
    3894                 :             :                 {
    3895                 :           0 :                     return 0;
    3896                 :             :                 }
    3897                 :           0 :                 num_F++;
    3898                 :             :             }
    3899                 :             :             else
    3900                 :             :             {
    3901         [ #  # ]:           0 :                 if (at[neigh].el_number == EL_NUMBER_C &&
    3902         [ #  # ]:           0 :                      at[neigh].valence == 4 &&
    3903   [ #  #  #  #  :           0 :                      !at[neigh].num_H && !at[neigh].charge && !at[neigh].radical && num_C_IV < 2)
             #  #  #  # ]
    3904                 :             :                 {
    3905                 :             : 
    3906         [ #  # ]:           0 :                     if (num_C_IV)
    3907                 :             :                     {
    3908                 :           0 :                         return 0;
    3909                 :             :                     }
    3910                 :             : 
    3911                 :           0 :                     iC_IV = neigh;
    3912                 :           0 :                     num_C_IV++;
    3913                 :             :                 }
    3914                 :             :             }
    3915                 :             :         }
    3916                 :             : 
    3917         [ #  # ]:           0 :         if (num_C_IV + num_F != 3)
    3918                 :             :         {
    3919                 :           0 :             return 0;
    3920                 :             :         }
    3921                 :             : 
    3922                 :           0 :         num_C++; /* found -CF2-C or -CF3 */
    3923         [ #  # ]:           0 :         if (!num_C_IV)
    3924                 :             :         {
    3925                 :           0 :             break; /* -CF3 */
    3926                 :             :         }
    3927                 :             : 
    3928                 :             :         /* treat next C(IV) as a new start atom */
    3929         [ #  # ]:           0 :         if ((p = is_in_the_list( at[iC_IV].neighbor, (AT_NUMB) start, at[iC_IV].valence ))) /* djb-rwth: addressing LLVM warning */
    3930                 :             :         {
    3931                 :           0 :             start = iC_IV;
    3932                 :           0 :             ord_prev = p - at[iC_IV].neighbor;
    3933                 :             :         }
    3934                 :             :         else
    3935                 :             :         {
    3936                 :           0 :             return -1; /* program error */
    3937                 :             :         }
    3938                 :             :     }
    3939                 :             : 
    3940                 :             :     /* Corrected by DT below - ? was - return num_C == 1 ? 1 : num_C == 3 ? 2 : 0;*/
    3941   [ #  #  #  #  :           0 :     return num_C == 1 ? 1 : num_C == 2 ? 2 : num_C == 3 ? 3 : 0;
                   #  # ]
    3942                 :             : }
    3943                 :             : 
    3944                 :             : 
    3945                 :             : /****************************************************************************/
    3946                 :           0 : int is_phenyl( inp_ATOM *at, int start, int ord_prev )
    3947                 :             : {
    3948                 :             :     int k, neigh, cur_at, ord;
    3949   [ #  #  #  # ]:           0 :     if (at[start].el_number != EL_NUMBER_C || at[start].valence != 3 ||
    3950         [ #  # ]:           0 :          at[start].valence + 1 != at[start].chem_bonds_valence ||
    3951         [ #  # ]:           0 :          at[start].chem_bonds_valence + at[start].num_H != 4 ||
    3952   [ #  #  #  # ]:           0 :          at[start].charge || at[start].radical)
    3953                 :             :     {
    3954                 :           0 :         return 0;
    3955                 :             :     }
    3956                 :             : 
    3957                 :           0 :     ord = ( ord_prev + 1 ) % 3;
    3958                 :           0 :     cur_at = start;
    3959                 :             : 
    3960         [ #  # ]:           0 :     for (k = 0; k < 5; k++)
    3961                 :             :     {
    3962                 :           0 :         neigh = at[cur_at].neighbor[ord];
    3963   [ #  #  #  # ]:           0 :         if (at[neigh].el_number != EL_NUMBER_C || at[neigh].valence != 2 ||
    3964         [ #  # ]:           0 :              at[neigh].valence + 1 != at[neigh].chem_bonds_valence ||
    3965         [ #  # ]:           0 :              at[neigh].chem_bonds_valence + at[neigh].num_H != 4 ||
    3966   [ #  #  #  # ]:           0 :              at[neigh].charge || at[neigh].radical)
    3967                 :             :         {
    3968                 :           0 :             return 0;
    3969                 :             :         }
    3970                 :           0 :         ord = ( at[neigh].neighbor[0] == cur_at );
    3971                 :           0 :         cur_at = neigh;
    3972                 :             :     }
    3973                 :             : 
    3974                 :           0 :     return ( at[cur_at].neighbor[ord] == start );
    3975                 :             : }
    3976                 :             : 
    3977                 :             : 
    3978                 :             : /****************************************************************************/
    3979                 :           0 : int is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR( inp_ATOM *at,
    3980                 :             :                                              int start,
    3981                 :             :                                              int num_atoms,
    3982                 :             :                                              DERIV_AT *da1,
    3983                 :             :                                              int idrv,
    3984                 :             :                                              char *szUnderiv,
    3985                 :             :                                              int lenUnderiv,
    3986                 :             :                                              char *szUnderiv2,
    3987                 :             :                                              int lenUnderiv2,
    3988                 :             :                                              BIT_UNDERIV *bitUnderiv )
    3989                 :             : {
    3990                 :           0 :     int i, j, k, neigh_at[2], prev_ord[2], neigh, is_B = 0, is_C = 0, n0, n1, n2, n3, n[4] = {0}, nFound, ind1, ind2; /* djb-rwth: adding variables for char -> int conversion of subscripts */
    3991                 :             :     AT_NUMB *p;
    3992                 :             :     const char *pUnk;
    3993                 :           0 :     char str[16] = { '\0' };
    3994                 :             : #if( defined(DERIV_RING_O_OUTSIDE_PRECURSOR) )
    3995                 :           0 :     char strO[8] = { '\0' };
    3996                 :             : #endif
    3997                 :             : #if( defined(DERIV_RING_NH_OUTSIDE_PRECURSOR) )
    3998                 :           0 :     char strN[8] = { '\0' };
    3999                 :             : #endif
    4000                 :             : 
    4001   [ #  #  #  # ]:           0 :     if (da1->typ[idrv] && ( da1->typ[idrv] & DERIV_RING_OUTSIDE_PRECURSOR ) == da1->typ[idrv] &&
    4002   [ #  #  #  # ]:           0 :          da1->typ[idrv + 1] && ( da1->typ[idrv + 1] & DERIV_RING_OUTSIDE_PRECURSOR ) == da1->typ[idrv + 1])
    4003                 :             :     {
    4004                 :             :         ;
    4005                 :             :     }
    4006                 :             :     else
    4007                 :             :     {
    4008                 :           0 :         return 0;
    4009                 :             :     }
    4010                 :             : 
    4011                 :             :     /*
    4012                 :             :     if ( (da1->typ[idrv] & DERIV_RING_O_OUTSIDE_PRECURSOR  || da1->typ[idrv+1] != DERIV_RING_O_OUTSIDE_PRECURSOR) &&
    4013                 :             :     (da1->typ[idrv] != DERIV_RING_NH_OUTSIDE_PRECURSOR || da1->typ[idrv+1] != DERIV_RING_NH_OUTSIDE_PRECURSOR) )
    4014                 :             :     return 0;
    4015                 :             :     */
    4016   [ #  #  #  #  :           0 :     if (at[start].charge || at[start].radical || at[start].valence != at[start].chem_bonds_valence)
                   #  # ]
    4017                 :             :     {
    4018                 :           0 :         return 0; /* check bond types start-n0 and start-n3 */
    4019                 :             :     }
    4020                 :             : 
    4021                 :             :     /* check
    4022                 :             :     n1    n0
    4023                 :             :     R1---O
    4024                 :             :     |     \
    4025                 :             :     |      B [start]
    4026                 :             :     |     /
    4027                 :             :     R2---O
    4028                 :             :     n2    n3
    4029                 :             : 
    4030                 :             :     All bond are single except n1-n2 (R1-R2), which may be either single or aromatic
    4031                 :             : 
    4032                 :             :     */
    4033                 :           0 :     nFound = 0;
    4034                 :           0 :     ind1 = da1->ord[0] - '0'; /* djb-rwth: converting char to int for subscript use */
    4035                 :           0 :     ind2 = da1->ord[1] - '0'; /* djb-rwth: converting char to int for subscript use */
    4036                 :           0 :     n0 = at[start].neighbor[ind1];
    4037                 :           0 :     n3 = at[start].neighbor[ind2];
    4038                 :             :     /* search for i, j, k such that at[at[n1]neighbor[i]].neighbor[k]= at[n2]neighbor[j] */
    4039         [ #  # ]:           0 :     for (i = 0; i < at[n0].valence; i++)
    4040                 :             :     {
    4041         [ #  # ]:           0 :         if (( n1 = at[n0].neighbor[i] ) == start)
    4042                 :             :         {
    4043                 :           0 :             continue; /* don't go back */
    4044                 :             :         }
    4045         [ #  # ]:           0 :         if (BOND_SINGLE != at[n0].bond_type[i])
    4046                 :             :         {
    4047                 :             :             /* check bond type n0-n1 */
    4048                 :           0 :             continue;
    4049                 :             :         }
    4050         [ #  # ]:           0 :         for (j = 0; j < at[n1].valence; j++)
    4051                 :             :         {
    4052         [ #  # ]:           0 :             if (( n2 = at[n1].neighbor[j] ) == n0)
    4053                 :             :             {
    4054                 :           0 :                 continue; /* don't go back */
    4055                 :             :             }
    4056         [ #  # ]:           0 :             if ((p = is_in_the_list( at[n3].neighbor, (AT_NUMB) n2, at[n3].valence ))) /* djb-rwth: addressing LLVM warning */
    4057                 :             :             {
    4058   [ #  #  #  # ]:           0 :                 if (( BOND_SINGLE == at[n1].bond_type[j] || BOND_ALTERN == at[n1].bond_type[j] ) && /* check bond type n1-n2 */
    4059         [ #  # ]:           0 :                      BOND_SINGLE == at[n3].bond_type[p - at[n3].neighbor] && /* check bond type n3-n2 */
    4060         [ #  # ]:           0 :                      !nFound++)
    4061                 :             :                 {
    4062                 :           0 :                     n[0] = n0;
    4063                 :           0 :                     n[1] = n1;
    4064                 :           0 :                     n[2] = n2;
    4065                 :           0 :                     n[3] = n3;
    4066                 :             :                 }
    4067                 :             :             }
    4068                 :             :         }
    4069                 :             :     }
    4070                 :             : 
    4071   [ #  #  #  #  :           0 :     if (nFound != 1 || at[n[1]].el_number != EL_NUMBER_C || at[n[2]].el_number != EL_NUMBER_C)
                   #  # ]
    4072                 :             :     {
    4073                 :           0 :         return 0;
    4074                 :             :     }
    4075                 :             : 
    4076                 :             :     /* n[1] and n[2] cannot have 3 neighboring heteroatoms */
    4077         [ #  # ]:           0 :     for (i = 1; i <= 2; i++)
    4078                 :             :     {
    4079         [ #  # ]:           0 :         if (at[n1 = n[i]].valence > 3)
    4080                 :             :         {
    4081         [ #  # ]:           0 :             for (k = 0, j = 0; j < at[n1].valence; j++)
    4082                 :             :             {
    4083                 :           0 :                 k += ( at[at[n1].neighbor[j]].el_number != EL_NUMBER_C );
    4084                 :             :             }
    4085         [ #  # ]:           0 :             if (k >= 3)
    4086                 :             :             {
    4087                 :           0 :                 return 0;
    4088                 :             :             }
    4089                 :             :         }
    4090                 :             :     }
    4091                 :             : 
    4092                 :           0 :     n0 = n[0];
    4093                 :           0 :     n3 = n[3];
    4094                 :             : 
    4095         [ #  # ]:           0 :     if (NULL != szUnderiv)
    4096                 :             :     {
    4097                 :             : #if( defined(DERIV_RING_O_OUTSIDE_PRECURSOR) )
    4098   [ #  #  #  # ]:           0 :         if (da1->typ[idrv] == DERIV_RING_O_OUTSIDE_PRECURSOR && da1->typ[idrv + 1] == DERIV_RING_O_OUTSIDE_PRECURSOR)
    4099                 :             :         {
    4100         [ #  # ]:           0 :             if (at[n0].el_number <= at[n3].el_number)
    4101                 :             :             {
    4102                 :           0 :                 strcat(strO, at[n0].elname);
    4103                 :           0 :                 strcat(strO, at[n3].elname);
    4104                 :             :             }
    4105                 :             :             else
    4106                 :             :             {
    4107                 :           0 :                 strcat(strO, at[n3].elname);
    4108                 :           0 :                 strcat(strO, at[n0].elname);
    4109                 :             :             }
    4110                 :             :         }
    4111                 :             :         else
    4112                 :             :         {
    4113         [ #  # ]:           0 :             if (da1->typ[idrv] == DERIV_RING_O_OUTSIDE_PRECURSOR)
    4114                 :             :             {
    4115                 :           0 :                 strcat(strO, at[n0].elname);
    4116                 :             :             }
    4117                 :             :             else
    4118                 :             :             {
    4119         [ #  # ]:           0 :                 if (da1->typ[idrv + 1] == DERIV_RING_O_OUTSIDE_PRECURSOR)
    4120                 :             :                 {
    4121                 :           0 :                     strcat(strO, at[n3].elname);
    4122                 :             :                 }
    4123                 :             :             }
    4124                 :             :         }
    4125                 :             : #endif
    4126                 :             : 
    4127                 :             : #if( defined(DERIV_RING_NH_OUTSIDE_PRECURSOR) )
    4128   [ #  #  #  # ]:           0 :         if (da1->typ[idrv] == DERIV_RING_NH_OUTSIDE_PRECURSOR && da1->typ[idrv + 1] == DERIV_RING_NH_OUTSIDE_PRECURSOR)
    4129                 :             :         {
    4130   [ #  #  #  # ]:           0 :             if (1 == at[n0].num_H && 1 == at[n3].num_H)
    4131                 :             :             {
    4132                 :           0 :                 strcat(strN, "(NH)2");
    4133                 :             :             }
    4134                 :             :             else
    4135                 :             :             {
    4136   [ #  #  #  # ]:           0 :                 if (1 == at[n0].num_H || 1 == at[n3].num_H)
    4137                 :             :                 {
    4138                 :           0 :                     strcat(strN, "(NH)N");
    4139                 :             :                 }
    4140                 :             :                 else
    4141                 :             :                 {
    4142                 :           0 :                     strcat(strN, "NN");
    4143                 :             :                 }
    4144                 :             :             }
    4145                 :             :         }
    4146                 :             :         else
    4147                 :             :         {
    4148         [ #  # ]:           0 :             if (da1->typ[idrv] == DERIV_RING_NH_OUTSIDE_PRECURSOR)
    4149                 :             :             {
    4150         [ #  # ]:           0 :                 strcat(strN, 1 == at[n0].num_H ? "(NH)" : "N");
    4151                 :             :             }
    4152                 :             :             else
    4153                 :             :             {
    4154         [ #  # ]:           0 :                 if (da1->typ[idrv + 1] == DERIV_RING_NH_OUTSIDE_PRECURSOR)
    4155                 :             :                 {
    4156         [ #  # ]:           0 :                     strcat(strN, 1 == at[n3].num_H ? "(NH)" : "N");
    4157                 :             :                 }
    4158                 :             :             }
    4159                 :             :         }
    4160                 :             : #endif
    4161                 :             : 
    4162                 :             : 
    4163   [ #  #  #  # ]:           0 :         switch (da1->typ[idrv] | da1->typ[idrv + 1])
    4164                 :             :         {
    4165                 :             : #if( defined(DERIV_RING_O_OUTSIDE_PRECURSOR) && defined(DERIV_RING_NH_OUTSIDE_PRECURSOR) )
    4166                 :           0 :             case ( DERIV_RING_O_OUTSIDE_PRECURSOR | DERIV_RING_NH_OUTSIDE_PRECURSOR ):
    4167                 :           0 :                 strcat(str, strN);
    4168                 :           0 :                 strcat(str, strO); /* "(NH)O" or "(NH)S" */
    4169                 :           0 :                 break;
    4170                 :             : #endif
    4171                 :             : #if( defined(DERIV_RING_O_OUTSIDE_PRECURSOR) )
    4172                 :           0 :             case ( DERIV_RING_O_OUTSIDE_PRECURSOR ):
    4173                 :           0 :                 strcat(str, strO); /* "OO" or "OS" or "SS" */
    4174                 :           0 :                 break;
    4175                 :             : #endif
    4176                 :             : #if( defined(DERIV_RING_NH_OUTSIDE_PRECURSOR) )
    4177                 :           0 :             case ( DERIV_RING_NH_OUTSIDE_PRECURSOR ):
    4178                 :           0 :                 strcat(str, strN);
    4179                 :           0 :                 break;
    4180                 :             : #endif
    4181                 :           0 :             default:
    4182                 :           0 :                 strcat(str, "???");
    4183                 :           0 :                 break;
    4184                 :             :         }
    4185                 :           0 :         strcat(str, "-");
    4186                 :             :     }
    4187                 :           0 :     underiv_list_add( szUnderiv, lenUnderiv, str, 0 );
    4188                 :             : 
    4189                 :             :     /*underiv_list_add( szUnderiv, lenUnderiv, da1->typ[idrv] == DERIV_RING_O_OUTSIDE_PRECURSOR? "OO-" : "(NH)2-", 0 );*/
    4190   [ #  #  #  # ]:           0 :     if (at[start].el_number == EL_NUMBER_B && at[start].valence == 3)
    4191                 :             :     {
    4192                 :           0 :         is_B = 1;
    4193                 :           0 :         underiv_list_add( szUnderiv, lenUnderiv, "B", 0 );
    4194                 :             :     }
    4195                 :             :     else
    4196                 :             :     {
    4197   [ #  #  #  #  :           0 :         if (at[start].el_number == EL_NUMBER_C && ( at[start].valence == 3 || at[start].valence == 4 ) &&
                   #  # ]
    4198         [ #  # ]:           0 :              at[start].chem_bonds_valence == at[start].valence &&
    4199         [ #  # ]:           0 :              at[start].num_H + at[start].chem_bonds_valence == 4)
    4200                 :             :         {
    4201                 :           0 :             is_C = 1;
    4202         [ #  # ]:           0 :             underiv_list_add( szUnderiv, lenUnderiv, at[start].valence == 3 ? "CH" : "C", 0 );
    4203                 :             :         }
    4204                 :             :         else
    4205                 :             :         {
    4206                 :           0 :             return 0;
    4207                 :             :         }
    4208                 :             :     }
    4209                 :             : 
    4210                 :             :     /* locate bonds connecting >B- or >C< or >C- to the rest of the derivative */
    4211         [ #  # ]:           0 :     for (i = k = 0; i < at[start].valence; i++)
    4212                 :             :     {
    4213   [ #  #  #  # ]:           0 :         if (i != da1->ord[idrv] && i != da1->ord[idrv + 1])
    4214                 :             :         {
    4215                 :           0 :             neigh = at[start].neighbor[i];
    4216   [ #  #  #  # ]:           0 :             if (k < 2 && ( p = is_in_the_list( at[neigh].neighbor, (AT_NUMB) start, at[neigh].valence ) ))
    4217                 :             :             {
    4218                 :           0 :                 neigh_at[k] = neigh;
    4219                 :           0 :                 prev_ord[k] = p - at[neigh].neighbor;
    4220                 :           0 :                 k++;
    4221                 :             :             }
    4222                 :             :             else
    4223                 :             :             {
    4224                 :           0 :                 return -1; /* program error */
    4225                 :             :             }
    4226                 :             :         }
    4227                 :             :     }
    4228                 :             : 
    4229                 :           0 :     n1 = n2 = 0; /* djb-rwth: ignoring LLVM warning: variables used */
    4230   [ #  #  #  #  :           0 :     if (is_B && k == 1 && da1->typ[idrv] == DERIV_RING_O_OUTSIDE_PRECURSOR)
                   #  # ]
    4231                 :             :     {
    4232         [ #  # ]:           0 :         if (0 < ( n1 = is_Me_or_Et( at, neigh_at[0], prev_ord[0] ) )
    4233                 :             : #ifdef UNDERIV_OOB_nButyl
    4234         [ #  # ]:           0 :              || 0 < ( n2 = is_nButyl( at, neigh_at[0], prev_ord[0] ) )
    4235                 :             : #endif
    4236                 :             :              )
    4237                 :             :         {
    4238   [ #  #  #  #  :           0 :             underiv_list_add( szUnderiv, lenUnderiv, n1 == 1 ? "Me" : n1 == 2 ? "Et" : n2 ? "nButyl" : "???", 0 );
                   #  # ]
    4239                 :             :             /* is_B */
    4240         [ #  # ]:           0 :             underiv_list_add( szUnderiv2, lenUnderiv2, n1 == 1 ? pszDerivName[DERIV_ID_MeBorate] :
    4241         [ #  # ]:           0 :                               n1 == 2 ? pszDerivName[DERIV_ID_EtBorate] :
    4242         [ #  # ]:           0 :                               n2 ? pszDerivName[DERIV_ID_BuBorate] : "???", ' ' );
    4243         [ #  # ]:           0 :             *bitUnderiv |= n1 == 1 ? DERIV_BIT_MeBorate :
    4244         [ #  # ]:           0 :                 n1 == 2 ? DERIV_BIT_EtBorate :
    4245         [ #  # ]:           0 :                 n2 ? DERIV_BIT_BuBorate : DERIV_BIT_Unknown;
    4246                 :           0 :             return 1;
    4247                 :             :         }
    4248                 :             :     }
    4249                 :             :     else
    4250                 :             :     {
    4251         [ #  # ]:           0 :         if (is_C)
    4252                 :             :         {
    4253   [ #  #  #  #  :           0 :             if (k == 1 && at[start].num_H == 1 && is_phenyl( at, neigh_at[0], prev_ord[0] ))
                   #  # ]
    4254                 :             :             {
    4255                 :           0 :                 underiv_list_add( szUnderiv, lenUnderiv, "Phe", 0 );
    4256         [ #  # ]:           0 :                 if (strN[0])
    4257                 :             :                 {
    4258         [ #  # ]:           0 :                     if ((pUnk = underiv_list_get_last( szUnderiv, ' ' ))) /* djb-rwth: addressing LLVM warning */
    4259                 :             :                     {
    4260                 :           0 :                         underiv_list_add( szUnderiv2, lenUnderiv2, pUnk, ' ' );
    4261                 :           0 :                         *bitUnderiv |= DERIV_BIT_Unknown;
    4262                 :             :                     }
    4263                 :             :                 }
    4264                 :             :                 else
    4265                 :             :                 {
    4266                 :           0 :                     underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[DERIV_ID_Benzlidene], ' ' );
    4267                 :           0 :                     *bitUnderiv |= DERIV_BIT_Benzlidene;
    4268                 :             :                 }
    4269                 :           0 :                 return 1;
    4270                 :             :             }
    4271   [ #  #  #  #  :           0 :             if (k == 2 && 0 < ( n1 = is_Me_or_Et( at, neigh_at[0], prev_ord[0] ) ) &&
                   #  # ]
    4272                 :           0 :                  0 < ( n2 = is_Me_or_Et( at, neigh_at[1], prev_ord[1] ) ))
    4273                 :             :             {
    4274         [ #  # ]:           0 :                 if (n1 != n2)
    4275                 :             :                 {
    4276                 :           0 :                     underiv_list_add( szUnderiv, lenUnderiv, "MeEt", 0 );
    4277         [ #  # ]:           0 :                     if ((pUnk = underiv_list_get_last( szUnderiv, ' ' ))) /* djb-rwth: addressing LLVM warning */
    4278                 :             :                     {
    4279                 :           0 :                         underiv_list_add( szUnderiv2, lenUnderiv2, pUnk, ' ' );
    4280                 :           0 :                         *bitUnderiv |= DERIV_BIT_Unknown;
    4281                 :             :                     }
    4282                 :             :                 }
    4283                 :             :                 else
    4284                 :             :                 {
    4285   [ #  #  #  # ]:           0 :                     if (n1 == 1 || n1 == 2)
    4286                 :             :                     {
    4287         [ #  # ]:           0 :                         underiv_list_add( szUnderiv, lenUnderiv, n1 == 1 ? "Me2" : "Et2", 0 );
    4288   [ #  #  #  # ]:           0 :                         if (strN[0] || n1 != 1)
    4289                 :             :                         {
    4290         [ #  # ]:           0 :                             if ((pUnk = underiv_list_get_last( szUnderiv, ' ' ))) /* djb-rwth: addressing LLVM warning */
    4291                 :             :                             {
    4292                 :           0 :                                 underiv_list_add( szUnderiv2, lenUnderiv2, pUnk, ' ' );
    4293                 :           0 :                                 *bitUnderiv |= DERIV_BIT_Unknown;
    4294                 :             :                             }
    4295                 :             :                         }
    4296                 :             :                         else
    4297                 :             :                         {
    4298         [ #  # ]:           0 :                             underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[n1 == 1 ? DERIV_ID_Acentonate : DERIV_ID_Unknown], ' ' );
    4299         [ #  # ]:           0 :                             *bitUnderiv |= ( n1 == 1 ? DERIV_BIT_Acentonate : DERIV_BIT_Unknown );
    4300                 :             :                         }
    4301                 :             :                     }
    4302                 :             :                 }
    4303                 :             : 
    4304                 :           0 :                 return 1;
    4305                 :             :             }
    4306                 :             :         }
    4307                 :             :     }
    4308                 :             : 
    4309                 :             :     /*
    4310                 :             :     if ( is_B && k == 1 && is_Me_or_Et( at, neigh_at[0], prev_ord[0]) )
    4311                 :             :     return 1;
    4312                 :             :     if ( is_B && k == 1 && is_nButyl( at, neigh_at[0], prev_ord[0]) )
    4313                 :             :     return 1;
    4314                 :             :     if ( is_C && k == 1 && at[start].num_H == 1 && is_phenyl( at, neigh_at[0], prev_ord[0]) )
    4315                 :             :     return 1;
    4316                 :             :     if ( is_C && k == 2 && is_Me_or_Et( at, neigh_at[0], prev_ord[0]) &&
    4317                 :             :     is_Me_or_Et( at, neigh_at[1], prev_ord[1]) )
    4318                 :             :     return 1;
    4319                 :             :     */
    4320                 :             : 
    4321                 :           0 :     return 0;
    4322                 :             : }
    4323                 :             : 
    4324                 :             : 
    4325                 :             : /****************************************************************************/
    4326                 :           0 : int is_deriv_chain2( inp_ATOM *at,
    4327                 :             :                      int start,
    4328                 :             :                      int type,
    4329                 :             :                      int num,
    4330                 :             :                      int ord,
    4331                 :             :                      int idrv,
    4332                 :             :                      char *szUnderiv,
    4333                 :             :                      int lenUnderiv,
    4334                 :             :                      char *szUnderiv2,
    4335                 :             :                      int lenUnderiv2,
    4336                 :             :                      BIT_UNDERIV *bitUnderiv )
    4337                 :             : {
    4338                 :           0 :     int i, k, prev_ord, neigh, iC, iO /* O or N */, iNxt, n1 = 0, n2 = 0;
    4339                 :             :     AT_NUMB *p;
    4340                 :             : 
    4341   [ #  #  #  # ]:           0 :     if (!type || ( type & DERIV_RING_OUTSIDE_PRECURSOR ))
    4342                 :             :     {
    4343                 :           0 :         return 0;
    4344                 :             :     }
    4345                 :             :     /*
    4346                 :             :     #ifdef DERIV_RING2_OUTSIDE_PRECUR
    4347                 :             :     if (type & DERIV_RING2_OUTSIDE_PRECUR)
    4348                 :             :     {
    4349                 :             :     return 1;
    4350                 :             :     }
    4351                 :             :     #endif
    4352                 :             :     */
    4353                 :             :     /* reject unexpected unsaturated */
    4354   [ #  #  #  #  :           0 :     if (at[start].charge || at[start].radical || (at[start].valence != at[start].chem_bonds_valence
                   #  # ]
    4355                 :             : #ifdef DERIV_X_OXIME
    4356         [ #  # ]:           0 :          && type != DERIV_X_OXIME
    4357                 :             : #endif
    4358                 :             : #ifdef DERIV_RO_COX
    4359         [ #  # ]:           0 :          && type != DERIV_RO_COX
    4360                 :             : #endif
    4361                 :             : #ifdef DERIV_RING_DMOX_DEOX_N
    4362         [ #  # ]:           0 :          && type != DERIV_RING_DMOX_DEOX_N
    4363                 :             : #endif
    4364                 :             :          )) /* djb-rwth: addressing LLVM warning */
    4365                 :             :     {
    4366                 :           0 :         return 0;
    4367                 :             :     }
    4368                 :             : 
    4369                 :           0 :     neigh = at[start].neighbor[(int) ord];
    4370                 :           0 :     p = is_in_the_list( at[neigh].neighbor, (AT_NUMB) start, at[neigh].valence );
    4371         [ #  # ]:           0 :     if (!p)
    4372                 :             :     {
    4373                 :           0 :         return -1; /* program error */
    4374                 :             :     }
    4375                 :             : 
    4376                 :           0 :     prev_ord = p - at[neigh].neighbor;
    4377                 :             : 
    4378                 :             :     /* eliminate silyl possibility */
    4379   [ #  #  #  #  :           0 :     if (type == DERIV_BRIDGE_O || type == DERIV_BRIDGE_NH || type == DERIV_AMINE_tN)
                   #  # ]
    4380                 :             :     {
    4381         [ #  # ]:           0 :         if ((n1 = is_silyl( at, neigh, prev_ord ))) /* djb-rwth: addressing LLVM warning */
    4382                 :             :         {
    4383         [ #  # ]:           0 :             if (at[start].valence != 2 || /* amine ? */
    4384                 :             :                                           /*at[start].el_number == EL_NUMBER_O && */
    4385         [ #  # ]:           0 :                  at[at[start].neighbor[!ord]].el_number != EL_NUMBER_SI)
    4386                 :             :             {
    4387                 :             :                 /* Gari's request: disconnect only from C-O->-Si..., not Si-O->-Si... (e.g.  CASr.n.= 141-63-9 ). */
    4388                 :             :                 /* ???? in case of type == DERIV_AMINE_tN why don't we check more neighbors ???? */
    4389   [ #  #  #  # ]:           0 :                 if (NULL != szUnderiv && 0 < lenUnderiv)
    4390                 :             :                 {
    4391                 :           0 :                     char szPrecur[16] = "R";
    4392   [ #  #  #  # ]:           0 :                     switch (type)
    4393                 :             :                     {
    4394                 :           0 :                         case DERIV_BRIDGE_O:
    4395                 :           0 :                             strcat(szPrecur, at[start].elname);
    4396                 :           0 :                             break;
    4397                 :           0 :                         case DERIV_BRIDGE_NH:
    4398                 :           0 :                             strcat(szPrecur, "NH");
    4399                 :           0 :                             break;
    4400                 :           0 :                         case DERIV_AMINE_tN:
    4401                 :           0 :                             strcat(szPrecur, "N");
    4402                 :           0 :                             break;
    4403                 :           0 :                         default:
    4404                 :           0 :                             strcat(szPrecur, "??");
    4405                 :           0 :                             break;
    4406                 :             :                     }
    4407                 :           0 :                     strcat(szPrecur, "-");
    4408      [ #  #  # ]:           0 :                     switch (n1)
    4409                 :             :                     {
    4410                 :           0 :                         case 1:
    4411                 :           0 :                             strcat(szPrecur, "TMS");
    4412                 :           0 :                             break;
    4413                 :           0 :                         case 2:
    4414                 :           0 :                             strcat(szPrecur, "TBDMS");
    4415                 :           0 :                             break;
    4416                 :           0 :                         default:
    4417                 :           0 :                             strcat(szPrecur, "???");
    4418                 :           0 :                             break;
    4419                 :             :                     }
    4420                 :           0 :                     underiv_list_add( szUnderiv, lenUnderiv, szPrecur, ' ' );
    4421   [ #  #  #  # ]:           0 :                     underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[n1 == 1 ? DERIV_ID_TMS : n1 == 2 ? DERIV_ID_TBDMS : DERIV_ID_Unknown], ' ' );
    4422   [ #  #  #  # ]:           0 :                     *bitUnderiv |= n1 == 1 ? DERIV_BIT_TMS : n1 == 2 ? DERIV_BIT_TBDMS : DERIV_BIT_Unknown;
    4423                 :             : 
    4424                 :             :                     /*
    4425                 :             :                     underiv_list_add( szUnderiv, lenUnderiv, type == DERIV_BRIDGE_O? "RO-" : type == DERIV_BRIDGE_NH? "RNH-" : "RN-", ' ' );
    4426                 :             :                     underiv_list_add( szUnderiv, lenUnderiv, n1==1?"TMS" : n1==2? "TBDMS" : "???", 0 );
    4427                 :             :                     */
    4428                 :             :                 }
    4429                 :           0 :                 return 1;
    4430                 :             :             }
    4431                 :             :             else
    4432                 :             :             {
    4433                 :           0 :                 return 0;
    4434                 :             :             }
    4435                 :             :         }
    4436                 :             :     }
    4437                 :             : #ifdef UNDERIV_SYLYL_ONLY
    4438                 :             :     return 0; /* if it is not Sylyl then it is not a derivative */
    4439                 :             : #endif
    4440                 :             : 
    4441                 :           0 :     n1 = n2 = 0; /* djb-rwth: ignoring LLVM warning: variables used */
    4442         [ #  # ]:           0 :     if (type == DERIV_BRIDGE_O)
    4443                 :             :     {
    4444                 :             :         /* check acetyl */
    4445                 :             : #if ( !defined(UNDERIV_ACETATE_Me) && !defined(UNDERIV_ACETATE_Et) && !defined(UNDERIV_ACETATE_CnF2np1) )
    4446                 :           0 :         return 0;
    4447                 :             : #else
    4448                 :             :         iC = at[start].neighbor[!ord];
    4449                 :             :         if (at[iC].charge || at[iC].radical || at[iC].num_H ||
    4450                 :             :              at[iC].el_number != EL_NUMBER_C || at[iC].valence != 3 ||
    4451                 :             :              at[iC].valence + 1 != at[iC].chem_bonds_valence)
    4452                 :             :             return 0;
    4453                 :             :         for (i = k = 0; i < at[iC].valence; i++)
    4454                 :             :         {
    4455                 :             :             iO = at[iC].neighbor[i];
    4456                 :             :             if (at[iO].charge || at[iO].radical || at[iO].num_H ||
    4457                 :             :                  at[iO].el_number != EL_NUMBER_O || at[iO].valence != 1 ||
    4458                 :             :                  at[iO].valence + 1 != at[iO].chem_bonds_valence)
    4459                 :             :                 continue;
    4460                 :             :             k++; /* number of =O */
    4461                 :             :         }
    4462                 :             :         if (k != 1)
    4463                 :             :             return 0;
    4464                 :             :         /* check derivative */
    4465                 :             : #if defined(UNDERIV_ACETATE_Et) || defined(UNDERIV_ACETATE_Me)
    4466                 :             :         n1 = is_Me_or_Et( at, neigh, prev_ord );
    4467                 :             :         if (
    4468                 :             : #if defined(UNDERIV_ACETATE_Et) && defined(UNDERIV_ACETATE_Me)
    4469                 :             :             0 !=
    4470                 :             : #elif defined(UNDERIV_ACETATE_Et)
    4471                 :             :             2 ==
    4472                 :             : #elif defined(UNDERIV_ACETATE_Me)
    4473                 :             :             1 ==
    4474                 :             : #endif
    4475                 :             :             n1)
    4476                 :             :             ;
    4477                 :             :         else
    4478                 :             :             n1 = 0;
    4479                 :             : #endif /* defined(UNDERIV_ACETATE_Et) || defined(UNDERIV_ACETATE_Me) */
    4480                 :             : #ifdef UNDERIV_ACETATE_CnF2np1
    4481                 :             :         if (n1 <= 0)
    4482                 :             :             n2 = is_CF3_or_linC3F7( at, neigh, prev_ord );
    4483                 :             : #endif
    4484                 :             :         if (n1 || n2)
    4485                 :             :         {
    4486                 :             :             if (szUnderiv)
    4487                 :             :             {
    4488                 :             :                 underiv_list_add( szUnderiv, lenUnderiv, "RCOO-", ' ' );
    4489                 :             :                 underiv_list_add( szUnderiv, lenUnderiv,
    4490                 :             :                                   n1 == 1 ? "Me" :
    4491                 :             :                                   n1 == 2 ? "Et" :
    4492                 :             :                                   n2 == 1 ? "CF3" :
    4493                 :             :                                   n2 == 2 ? "C2F5" :
    4494                 :             :                                   n2 == 3 ? "C3F7" :
    4495                 :             :                                   "C?F?", 0 );
    4496                 :             :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[
    4497                 :             : #if  defined(UNDERIV_ACETATE_Me)
    4498                 :             :                     n1 == 1 ? DERIV_ID_Methylation :
    4499                 :             : #endif
    4500                 :             : #if  defined(UNDERIV_ACETATE_Et)
    4501                 :             :                         n1 == 2 ? DERIV_ID_Ethylation :
    4502                 :             : #endif
    4503                 :             :                         n2 == 1 ? DERIV_ID_TFA :
    4504                 :             :                         n2 == 2 ? DERIV_ID_PFP :
    4505                 :             :                         n2 == 3 ? DERIV_ID_HFB :
    4506                 :             :                         DERIV_ID_Unknown], ' ' );
    4507                 :             :                 *bitUnderiv |=
    4508                 :             : #if  defined(UNDERIV_ACETATE_Me)
    4509                 :             :                     n1 == 1 ? DERIV_BIT_Methylation :
    4510                 :             : #endif
    4511                 :             : #if  defined(UNDERIV_ACETATE_Et)
    4512                 :             :                     n1 == 2 ? DERIV_BIT_Ethylation :
    4513                 :             : #endif
    4514                 :             :                     n2 == 1 ? DERIV_BIT_TFA :
    4515                 :             :                     n2 == 2 ? DERIV_BIT_PFP :
    4516                 :             :                     n2 == 3 ? DERIV_BIT_HFB :
    4517                 :             :                     DERIV_BIT_Unknown;
    4518                 :             :             }
    4519                 :             :             return 1;
    4520                 :             :         }
    4521                 :             :         return 0;
    4522                 :             : #endif /* !defined(UNDERIV_ACETATE_Me) && !defined(UNDERIV_ACETATE_Et) && !defined(UNDERIV_ACETATE_CnF2np1) */
    4523                 :             :     }
    4524                 :             : 
    4525                 :           0 :     n1 = n2 = 0;
    4526   [ #  #  #  # ]:           0 :     if (type == DERIV_BRIDGE_NH || type == DERIV_AMINE_tN)
    4527                 :             :     {
    4528                 :             :         /* check acetyl */
    4529                 :           0 :         iNxt = -1;
    4530                 :           0 :         iC = at[start].neighbor[(int) ord];
    4531   [ #  #  #  #  :           0 :         if (at[iC].charge || at[iC].radical || at[iC].num_H ||
                   #  # ]
    4532   [ #  #  #  # ]:           0 :              at[iC].el_number != EL_NUMBER_C || at[iC].valence != 3 ||
    4533         [ #  # ]:           0 :              at[iC].valence + 1 != at[iC].chem_bonds_valence)
    4534                 :             :         {
    4535                 :           0 :             return 0;
    4536                 :             :         }
    4537         [ #  # ]:           0 :         for (i = k = 0; i < at[iC].valence; i++)
    4538                 :             :         {
    4539                 :           0 :             iO = at[iC].neighbor[i];
    4540   [ #  #  #  #  :           0 :             if (at[iO].charge || at[iO].radical || at[iO].num_H ||
                   #  # ]
    4541   [ #  #  #  # ]:           0 :                  at[iO].el_number != EL_NUMBER_O || at[iO].valence != 1 ||
    4542         [ #  # ]:           0 :                  at[iO].valence + 1 != at[iO].chem_bonds_valence)
    4543                 :             :             {
    4544         [ #  # ]:           0 :                 if (iO != start)
    4545                 :             :                 {
    4546         [ #  # ]:           0 :                     if (iNxt < 0)
    4547                 :             :                     {
    4548                 :           0 :                         iNxt = iO;
    4549                 :             :                     }
    4550                 :             :                     else
    4551                 :             :                     {
    4552                 :           0 :                         return 0;
    4553                 :             :                     }
    4554                 :             :                 }
    4555                 :           0 :                 continue;
    4556                 :             :             }
    4557                 :           0 :             k++; /* number of =O */
    4558                 :             :         }
    4559   [ #  #  #  # ]:           0 :         if (k != 1 || iNxt < 0)
    4560                 :             :         {
    4561                 :           0 :             return 0;
    4562                 :             :         }
    4563                 :             :         /* find bond from iNxt to iC */
    4564                 :           0 :         p = is_in_the_list( at[iNxt].neighbor, (AT_NUMB) iC, at[iNxt].valence );
    4565         [ #  # ]:           0 :         if (!p)
    4566                 :             :         {
    4567                 :           0 :             return -1; /* program error */
    4568                 :             :         }
    4569                 :           0 :         prev_ord = p - at[iNxt].neighbor;
    4570                 :             :         /* check derivative */
    4571                 :             : #if ( defined( UNDERIV_RN_AcEt ) || defined( UNDERIV_RNH_AcEt )  )
    4572                 :             :         n1 = is_Me_or_Et( at, iNxt, prev_ord );
    4573                 :             : #endif
    4574         [ #  # ]:           0 :         if (!n1)
    4575                 :             :         {
    4576                 :           0 :             n2 = is_CF3_or_linC3F7( at, iNxt, prev_ord );
    4577                 :             :         }
    4578                 :             :         else
    4579                 :             :         {
    4580         [ #  # ]:           0 :             if (n1 == 1)
    4581                 :             :             {
    4582                 :             : #if ( !defined(UNDERIV_RN_AcMe) && defined(DERIV_BRIDGE_tN) )
    4583                 :             :                 if (type == DERIV_BRIDGE_tN) n1 = 0;
    4584                 :             : #elif ( !defined(UNDERIV_RNH_AcMe) && defined(DERIV_BRIDGE_NH) )
    4585                 :             :                 if (type == DERIV_BRIDGE_NH) n1 = 0;
    4586                 :             : #endif
    4587                 :             :                 ; /* keep C-compiler happy */
    4588                 :             :             }
    4589                 :             :             else
    4590                 :             :             {
    4591         [ #  # ]:           0 :                 if (n1 == 2)
    4592                 :             :                 {
    4593                 :             : #if ( !defined(UNDERIV_RN_AcEt) && defined(DERIV_BRIDGE_tN) )
    4594                 :             :                     if (type == DERIV_BRIDGE_tN) n1 = 0;
    4595                 :             : #elif ( !defined(UNDERIV_RNH_AcEt) && defined(DERIV_BRIDGE_NH) )
    4596         [ #  # ]:           0 :                     if (type == DERIV_BRIDGE_NH) n1 = 0;
    4597                 :             : #endif
    4598                 :             :                     ; /* keep C-compiler happy */
    4599                 :             :                 }
    4600                 :             :                 else
    4601                 :             :                 {
    4602                 :           0 :                     n1 = 0;
    4603                 :             :                 }
    4604                 :             :             }
    4605                 :             :         }
    4606                 :             : 
    4607   [ #  #  #  # ]:           0 :         if (n1 || n2)
    4608                 :             :         {
    4609         [ #  # ]:           0 :             if (szUnderiv)
    4610                 :             :             {
    4611         [ #  # ]:           0 :                 underiv_list_add( szUnderiv, lenUnderiv, type == DERIV_AMINE_tN ? "RN-C(O)" : "RNH-C(O)", ' ' );
    4612         [ #  # ]:           0 :                 underiv_list_add( szUnderiv, lenUnderiv,
    4613   [ #  #  #  #  :           0 :                                   n1 == 1 ? "Me" :
             #  #  #  # ]
    4614                 :             :                                   n1 == 2 ? "Et" :
    4615                 :             :                                   n2 == 1 ? "CF3" :
    4616                 :             :                                   n2 == 2 ? "C2F5" :
    4617                 :             :                                   n2 == 3 ? "C3F7" :
    4618                 :             :                                   "C?F?", 0 );
    4619                 :             :                 /* djb-rwth: addressing coverity ID #499506 -- condition is correct for n1 != 1 */
    4620                 :           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[
    4621                 :             : #if defined(UNDERIV_RN_AcMe) || defined(UNDERIV_RNH_AcMe)
    4622         [ #  # ]:           0 :                     n1 == 1 ? DERIV_ID_Acetate :
    4623                 :             : #endif
    4624                 :             : #if defined(UNDERIV_RN_AcEt) || defined(UNDERIV_RNH_AcEt)
    4625                 :             :                         n1 == 2 ? DERIV_ID_Propanoate :
    4626                 :             : #endif
    4627         [ #  # ]:           0 :                         n2 == 1 ? DERIV_ID_TFA :
    4628         [ #  # ]:           0 :                         n2 == 2 ? DERIV_ID_PFP :
    4629         [ #  # ]:           0 :                         n2 == 3 ? DERIV_ID_HFB :
    4630                 :             :                         DERIV_ID_Unknown], ' ' );
    4631                 :           0 :                 *bitUnderiv |=
    4632                 :             : #if defined(UNDERIV_RN_AcMe) || defined(UNDERIV_RNH_AcMe)
    4633         [ #  # ]:           0 :                     n1 == 1 ? DERIV_BIT_Acetate :
    4634                 :             : #endif
    4635                 :             : #if defined(UNDERIV_RN_AcEt) || defined(UNDERIV_RNH_AcEt)
    4636                 :             :                     n1 == 2 ? DERIV_BIT_Propanoate :
    4637                 :             : #endif
    4638         [ #  # ]:           0 :                     n2 == 1 ? DERIV_BIT_TFA :
    4639         [ #  # ]:           0 :                     n2 == 2 ? DERIV_BIT_PFP :
    4640         [ #  # ]:           0 :                     n2 == 3 ? DERIV_BIT_HFB :
    4641                 :             :                     DERIV_BIT_Unknown;
    4642                 :             :             }
    4643                 :           0 :             return 1;
    4644                 :             :         }
    4645                 :             : 
    4646                 :           0 :         return 0;
    4647                 :             :     }
    4648                 :             : 
    4649                 :             :     /*underiv_buf_clear( szUnderiv );*/
    4650                 :             : #ifdef DERIV_X_OXIME
    4651         [ #  # ]:           0 :     if (type == DERIV_X_OXIME)
    4652                 :             :     {
    4653         [ #  # ]:           0 :         if (szUnderiv)
    4654                 :             :         {
    4655                 :           0 :             iO = 0;
    4656         [ #  # ]:           0 :             if (num == 2)
    4657                 :             :             {
    4658                 :           0 :                 underiv_list_add( szUnderiv, lenUnderiv, "MOX", ' ' );
    4659                 :           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[DERIV_ID_MOX], ' ' );
    4660                 :           0 :                 *bitUnderiv |= DERIV_BIT_MOX;
    4661                 :             :             }
    4662                 :             :             else
    4663                 :             :             {
    4664         [ #  # ]:           0 :                 if (num == 3)
    4665                 :             :                 {
    4666                 :           0 :                     underiv_list_add( szUnderiv, lenUnderiv, "EtOX", ' ' );
    4667                 :           0 :                     underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[DERIV_ID_EtOX], ' ' );
    4668                 :           0 :                     *bitUnderiv |= DERIV_BIT_EtOX;
    4669                 :             :                 }
    4670                 :             :                 else
    4671                 :             :                 {
    4672         [ #  # ]:           0 :                     if (num == 8)
    4673                 :             :                     {
    4674                 :           0 :                         neigh = at[start].neighbor[ord];
    4675                 :           0 :                         iC = at[neigh].neighbor[start == at[neigh].neighbor[0]];
    4676                 :           0 :                         iO = at[iC].el_number == EL_NUMBER_SI;
    4677                 :             :                     }
    4678                 :           0 :                     underiv_list_add( szUnderiv, lenUnderiv, "OX-", ' ' );
    4679   [ #  #  #  #  :           0 :                     underiv_list_add( szUnderiv, lenUnderiv, num == 5 ? "TMS" : num == 8 && iO ? "TBDMS" : num == 8 && !iO ? "CH2Phe" : "???", 0 );
          #  #  #  #  #  
                      # ]
    4680   [ #  #  #  #  :           0 :                     underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[num == 5 ? DERIV_ID_TMS : num == 8 && iO ? DERIV_ID_TBDMS : num == 8 && !iO ? DERIV_ID_BenzOX : DERIV_ID_Unknown], ' ' );
          #  #  #  #  #  
                      # ]
    4681   [ #  #  #  #  :           0 :                     *bitUnderiv |= num == 5 ? DERIV_BIT_TMS : num == 8 && iO ? DERIV_BIT_TBDMS : num == 8 && !iO ? DERIV_BIT_BenzOX : DERIV_BIT_Unknown;
          #  #  #  #  #  
                      # ]
    4682                 :             :                 }
    4683                 :             :             }
    4684                 :             :         }
    4685                 :             : 
    4686                 :           0 :         return 1;
    4687                 :             :     }
    4688                 :             : #endif
    4689                 :             : 
    4690                 :             : #ifdef DERIV_RO_COX
    4691         [ #  # ]:           0 :     if (type == DERIV_RO_COX)
    4692                 :             :     {
    4693         [ #  # ]:           0 :         if (szUnderiv)
    4694                 :             :         {
    4695         [ #  # ]:           0 :             underiv_list_add( szUnderiv, lenUnderiv, at[start].el_number == EL_NUMBER_O ? "RO-C(O)" :
    4696         [ #  # ]:           0 :                               at[start].el_number == EL_NUMBER_S ? "RS-C(O)" : "R?-C(O)", ' ' );
    4697   [ #  #  #  #  :           0 :             underiv_list_add( szUnderiv, lenUnderiv, num == 3 ? "Me" :
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4698                 :             :                               num == 4 ? "Et" :
    4699                 :             :                               num == 6 ? "CF3" :
    4700                 :             :                               num == 8 ? "Phe" :
    4701                 :             :                               num == 9 ? "C2F5" :
    4702                 :             :                               num == 12 ? "C3F7" :
    4703                 :             :                               num == 13 ? "PheF5" :
    4704                 :             :                               "???", 0 );
    4705         [ #  # ]:           0 :             if (num == 13)
    4706                 :             :             {
    4707                 :           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, underiv_list_get_last( szUnderiv, ' ' ), ' ' );
    4708                 :           0 :                 *bitUnderiv |= DERIV_BIT_Unknown;
    4709                 :             :             }
    4710                 :             :             else
    4711                 :             :             {
    4712                 :           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[
    4713                 :             : #if  defined(UNDERIV_RO_COX_Me)
    4714         [ #  # ]:           0 :                     num == 3 ? DERIV_ID_Acetate :
    4715                 :             : #endif
    4716                 :             : #if  defined(UNDERIV_RO_COX_Et)
    4717                 :             :                         num == 4 ? DERIV_ID_Propanoate :
    4718                 :             : #endif
    4719         [ #  # ]:           0 :                         num == 6 ? DERIV_ID_TFA :
    4720                 :             : #if  defined(UNDERIV_RO_COX_BENZOATES)
    4721         [ #  # ]:           0 :                         num == 8 ? DERIV_ID_Benzoate :
    4722                 :             : #endif
    4723         [ #  # ]:           0 :                         num == 9 ? DERIV_ID_PFP :
    4724         [ #  # ]:           0 :                         num == 12 ? DERIV_ID_HFB :
    4725         [ #  # ]:           0 :                         num == 13 ? DERIV_ID_PFB :
    4726                 :             :                         DERIV_ID_Unknown], ' ' );
    4727                 :           0 :                 *bitUnderiv |=
    4728                 :             : #if  defined(UNDERIV_RO_COX_Me)
    4729         [ #  # ]:           0 :                     num == 3 ? DERIV_BIT_Acetate :
    4730                 :             : #endif
    4731                 :             : #if  defined(UNDERIV_RO_COX_Et)
    4732                 :             :                     num == 4 ? DERIV_BIT_Propanoate :
    4733                 :             : #endif
    4734         [ #  # ]:           0 :                     num == 6 ? DERIV_BIT_TFA :
    4735                 :             : #if  defined(UNDERIV_RO_COX_BENZOATES)
    4736         [ #  # ]:           0 :                     num == 8 ? DERIV_BIT_Benzoate :
    4737                 :             : #endif
    4738         [ #  # ]:           0 :                     num == 9 ? DERIV_BIT_PFP :
    4739         [ #  # ]:           0 :                     num == 12 ? DERIV_BIT_HFB :
    4740         [ #  # ]:           0 :                     num == 13 ? DERIV_BIT_PFB :
    4741                 :             :                     DERIV_ID_Unknown;
    4742                 :             :             }
    4743                 :             :         }
    4744                 :             : 
    4745                 :           0 :         return 1;
    4746                 :             :     }
    4747                 :             : #endif
    4748                 :             : 
    4749                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    4750   [ #  #  #  #  :           0 :     if (!idrv && ( type == DERIV_RING_DMOX_DEOX_N ||
                   #  # ]
    4751                 :             :                    type == DERIV_RING_DMOX_DEOX_O ))
    4752                 :             :     {
    4753   [ #  #  #  # ]:           0 :         if (szUnderiv && type == DERIV_RING_DMOX_DEOX_N)
    4754                 :             :         {
    4755                 :             :             /* add only once; do not add upon DERIV_RING_DMOX_DEOX_O */
    4756   [ #  #  #  #  :           0 :             underiv_list_add( szUnderiv, lenUnderiv, num == 4 ? "DMOX" : num == 6 ? "DEOX" : num ? "D?OX" : "???", ' ' );
                   #  # ]
    4757   [ #  #  #  # ]:           0 :             if (num == 4 || num == 6)
    4758                 :             :             {
    4759   [ #  #  #  # ]:           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[num == 4 ? DERIV_ID_DMOX : num == 6 ? DERIV_ID_DEOX : DERIV_ID_Unknown], ' ' );
    4760   [ #  #  #  # ]:           0 :                 *bitUnderiv |= num == 4 ? DERIV_BIT_DMOX : num == 6 ? DERIV_BIT_DEOX : DERIV_BIT_Unknown;
    4761                 :             :             }
    4762                 :             :             else
    4763                 :             :             {
    4764                 :           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, underiv_list_get_last( szUnderiv, ' ' ), ' ' );
    4765                 :           0 :                 *bitUnderiv |= DERIV_BIT_Unknown;
    4766                 :             :             }
    4767                 :             :         }
    4768                 :             : 
    4769                 :           0 :         return 1;
    4770                 :             :     }
    4771                 :             : #endif
    4772                 :             : 
    4773                 :             : #ifdef DERIV_RING2_OUTSIDE_PRECUR
    4774   [ #  #  #  # ]:           0 :     if (type && ( type & DERIV_RING2_OUTSIDE_PRECUR ) == type)
    4775                 :             :     {
    4776   [ #  #  #  # ]:           0 :         if (szUnderiv && !idrv)
    4777                 :             :         {
    4778   [ #  #  #  # ]:           0 :             if (num == 4 || num == 5)
    4779                 :             :             {
    4780   [ #  #  #  # ]:           0 :                 underiv_list_add( szUnderiv, lenUnderiv, num == 4 ? "Pyrrolidide" : num == 5 ? "Piperidine" : "???", ' ' );
    4781   [ #  #  #  # ]:           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[num == 4 ? DERIV_ID_Pyrrolidide : num == 5 ? DERIV_ID_Piperidine : DERIV_ID_Unknown], ' ' ); /* djb-rwth: addressing coverity ID #499491 -- working correctly for num == 5 */
    4782   [ #  #  #  # ]:           0 :                 *bitUnderiv |= num == 4 ? DERIV_BIT_Pyrrolidide : num == 5 ? DERIV_BIT_Piperidine : DERIV_BIT_Unknown;
    4783                 :             :             }
    4784                 :             :             else
    4785                 :             :             {
    4786                 :             : #ifdef _DEBUG
    4787                 :             :                 int stop_DERIV_RING2_OUTSIDE_PRECUR = 1; /* debug only */
    4788                 :             : #endif
    4789         [ #  # ]:           0 :                 underiv_list_add( szUnderiv, lenUnderiv, num ? "Pyrrol?Piper?" : "???", ' ' );
    4790                 :           0 :                 underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[DERIV_ID_Unknown], ' ' );
    4791                 :           0 :                 *bitUnderiv |= DERIV_BIT_Unknown;
    4792                 :             :             }
    4793                 :             :         }
    4794                 :           0 :         return 1;
    4795                 :             :     }
    4796                 :             : #endif
    4797                 :             : 
    4798                 :             : #ifdef DERIV_DANSYL
    4799   [ #  #  #  #  :           0 :     if (!idrv && type && ( type & DERIV_DANSYL ) == type)
                   #  # ]
    4800                 :             :     {
    4801         [ #  # ]:           0 :         if (szUnderiv)
    4802                 :             :         {
    4803                 :           0 :             char szRO[16] = "R";
    4804                 :           0 :             strcat(szRO, at[start].elname);
    4805         [ #  # ]:           0 :             if (at[start].num_H == 1)
    4806                 :             :             {
    4807                 :           0 :                 strcat(szRO, "H");
    4808                 :             :             }
    4809                 :           0 :             strcat(szRO, "-Dansyl");
    4810                 :           0 :             underiv_list_add( szUnderiv, lenUnderiv, szRO, ' ' );
    4811                 :           0 :             underiv_list_add( szUnderiv2, lenUnderiv2, pszDerivName[DERIV_ID_Dansyl], ' ' );
    4812                 :           0 :             *bitUnderiv |= DERIV_BIT_Dansyl;
    4813                 :             :         }
    4814                 :           0 :         return 1;
    4815                 :             :     }
    4816                 :             : #endif
    4817                 :             : 
    4818                 :           0 :     return 0;
    4819                 :             : }
    4820                 :             : 
    4821                 :             : 
    4822                 :             : /****************************************************************************/
    4823                 :           0 : int is_deriv_chain( inp_ATOM *at,
    4824                 :             :                     int start,
    4825                 :             :                     int num_atoms,
    4826                 :             :                     DERIV_AT *da1,
    4827                 :             :                     int idrv,
    4828                 :             :                     char *szUnderiv,
    4829                 :             :                     int lenUnderiv,
    4830                 :             :                     char *szUnderiv2,
    4831                 :             :                     int lenUnderiv2,
    4832                 :             :                     BIT_UNDERIV *bitUnderiv )
    4833                 :             : {
    4834                 :           0 :     return is_deriv_chain2( at, start, da1->typ[idrv], da1->num[idrv], da1->ord[idrv], idrv, szUnderiv, lenUnderiv, szUnderiv2, lenUnderiv2, bitUnderiv );
    4835                 :             : }
    4836                 :             : 
    4837                 :             : 
    4838                 :             : /****************************************************************************/
    4839                 :           0 : int is_deriv_chain_or_ring( inp_ATOM *at,
    4840                 :             :                             int start,
    4841                 :             :                             int num_atoms,
    4842                 :             :                             DERIV_AT *da1,
    4843                 :             :                             int *idrv )
    4844                 :             : {
    4845                 :           0 :     int i, ret = -1;
    4846         [ #  # ]:           0 :     if (da1->typ[*idrv] & DERIV_RING_OUTSIDE_PRECURSOR)
    4847                 :             :     {
    4848                 :             :         /* find the first ord of this derivative; ord of ring derivatives are in pairs */
    4849                 :           0 :         int j = -1;
    4850   [ #  #  #  # ]:           0 :         for (i = 0; i < DERIV_AT_LEN && da1->typ[i]; i++)
    4851                 :             :         {
    4852         [ #  # ]:           0 :             if (da1->typ[i] & DERIV_RING_OUTSIDE_PRECURSOR)
    4853                 :             :             {
    4854   [ #  #  #  # ]:           0 :                 if (i == *idrv || i + 1 == *idrv)
    4855                 :             :                 {
    4856                 :           0 :                     *idrv = j = i;
    4857                 :           0 :                     break;
    4858                 :             :                 }
    4859                 :           0 :                 i++; /* bypass the second bond to the same derivatization agent */
    4860                 :             :             }
    4861                 :             :         }
    4862                 :             :         /* check consistency */
    4863   [ #  #  #  # ]:           0 :         if (j == -1 || j + 1 >= DERIV_AT_LEN ||
    4864   [ #  #  #  # ]:           0 :              !( da1->typ[j] & DERIV_RING_OUTSIDE_PRECURSOR ) || !( da1->typ[j + 1] & DERIV_RING_OUTSIDE_PRECURSOR ))
    4865                 :             :         {
    4866                 :           0 :             ret = -1; /* program error */
    4867                 :             :         }
    4868                 :             :         else
    4869                 :             :         {
    4870                 :           0 :             ret = is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR( at, start, num_atoms, da1, j, NULL, 0, NULL, 0, NULL );
    4871                 :             :         }
    4872                 :             :     }
    4873                 :             :     else
    4874                 :             :     {
    4875         [ #  # ]:           0 :         if (da1->typ[*idrv])
    4876                 :             :         {
    4877                 :           0 :             ret = is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR( at, start, num_atoms, da1, *idrv, NULL, 0, NULL, 0, NULL );
    4878                 :             :         }
    4879                 :             :     }
    4880                 :             : 
    4881                 :           0 :     return ret;
    4882                 :             : }
    4883                 :             : 
    4884                 :             : 
    4885                 :             : #define DERIV_RING DERIV_RING_OUTSIDE_PRECURSOR
    4886                 :             : 
    4887                 :             : 
    4888                 :             : /****************************************************************************/
    4889                 :           0 : int remove_deriv( DERIV_AT *da1, int idrv )
    4890                 :             : {
    4891                 :           0 :     int i, j, ret = -1;
    4892                 :             : 
    4893         [ #  # ]:           0 :     if (da1->typ[idrv] & DERIV_RING)
    4894                 :             :     {
    4895                 :             :         /* find the first ord of this derivative; ord of ring derivatives are in pairs */
    4896                 :           0 :         j = -1;
    4897   [ #  #  #  # ]:           0 :         for (i = 0; i < DERIV_AT_LEN && da1->typ[i]; i++)
    4898                 :             :         {
    4899         [ #  # ]:           0 :             if (da1->typ[i] & DERIV_RING)
    4900                 :             :             {
    4901   [ #  #  #  # ]:           0 :                 if (i == idrv || i + 1 == idrv)
    4902                 :             :                 {
    4903                 :           0 :                     j = i;
    4904                 :           0 :                     break;
    4905                 :             :                 }
    4906                 :           0 :                 i++; /* bypass the second bond to the same derivatization agent */
    4907                 :             :             }
    4908                 :             :         }
    4909                 :             : 
    4910                 :             :         /* delete if data are consistent */
    4911   [ #  #  #  #  :           0 :         if (j >= 0 && j + 1 < DERIV_AT_LEN && ( da1->typ[j] & DERIV_RING ) && ( da1->typ[j + 1] & DERIV_RING ))
             #  #  #  # ]
    4912                 :             :         {
    4913   [ #  #  #  # ]:           0 :             for (; j < DERIV_AT_LEN - 2 && da1->typ[j + 2]; j++)
    4914                 :             :             {
    4915                 :           0 :                 da1->typ[j] = da1->typ[j + 2];
    4916                 :           0 :                 da1->num[j] = da1->num[j + 2];
    4917                 :           0 :                 da1->ord[j] = da1->ord[j + 2];
    4918                 :             :             }
    4919         [ #  # ]:           0 :             for (; j < DERIV_AT_LEN; j++)
    4920                 :             :             {
    4921                 :           0 :                 da1->typ[j] = 0;
    4922                 :           0 :                 da1->num[j] = 0;
    4923                 :           0 :                 da1->ord[j] = 0;
    4924                 :             :             }
    4925                 :           0 :             ret = 0;
    4926                 :             :         }
    4927                 :             :     }
    4928                 :             :     else
    4929                 :             :     {
    4930                 :           0 :         j = idrv;
    4931   [ #  #  #  # ]:           0 :         for (; j < DERIV_AT_LEN - 1 && da1->typ[j + 1]; j++)
    4932                 :             :         {
    4933                 :           0 :             da1->typ[j] = da1->typ[j + 1];
    4934                 :           0 :             da1->num[j] = da1->num[j + 1];
    4935                 :           0 :             da1->ord[j] = da1->ord[j + 1];
    4936                 :             :         }
    4937         [ #  # ]:           0 :         for (; j < DERIV_AT_LEN; j++)
    4938                 :             :         {
    4939                 :           0 :             da1->typ[j] = 0;
    4940                 :           0 :             da1->num[j] = 0;
    4941                 :           0 :             da1->ord[j] = 0;
    4942                 :             :         }
    4943                 :           0 :         ret = 0;
    4944                 :             :     }
    4945                 :             : 
    4946                 :           0 :     return ret;
    4947                 :             : }
    4948                 :             : 
    4949                 :             : 
    4950                 :             : /****************************************************************************/
    4951                 :           0 : int remove_deriv_mark( DERIV_AT *da1, int idrv )
    4952                 :             : {
    4953                 :           0 :     int i, j, ret = -1;
    4954         [ #  # ]:           0 :     if (da1->typ[idrv] & DERIV_RING)
    4955                 :             :     {
    4956                 :             :         /* find the first ord of this derivative; ord of ring derivatives are in pairs */
    4957                 :           0 :         j = -1;
    4958   [ #  #  #  # ]:           0 :         for (i = 0; i < DERIV_AT_LEN && da1->typ[i]; i++)
    4959                 :             :         {
    4960         [ #  # ]:           0 :             if (da1->typ[i] & DERIV_RING)
    4961                 :             :             {
    4962   [ #  #  #  # ]:           0 :                 if (i == idrv || i + 1 == idrv)
    4963                 :             :                 {
    4964                 :           0 :                     j = i;
    4965                 :           0 :                     break;
    4966                 :             :                 }
    4967                 :           0 :                 i++; /* bypass the second bond to the same derivatization agent */
    4968                 :             :             }
    4969                 :             :         }
    4970                 :             :         /* delete if data are consistent */
    4971   [ #  #  #  #  :           0 :         if (j >= 0 && j + 1 < DERIV_AT_LEN && ( da1->typ[j] & DERIV_RING ) && ( da1->typ[j + 1] & DERIV_RING ))
             #  #  #  # ]
    4972                 :             :         {
    4973                 :           0 :             da1->typ[j] |= DERIV_DUPLIC;
    4974                 :           0 :             da1->typ[j + 1] |= DERIV_DUPLIC;
    4975                 :           0 :             ret = 0;
    4976                 :             :         }
    4977                 :             :     }
    4978                 :             :     else
    4979                 :             :     {
    4980                 :           0 :         j = idrv;
    4981                 :           0 :         da1->typ[j] |= DERIV_DUPLIC;
    4982                 :           0 :         ret = 0;
    4983                 :             :     }
    4984                 :             : 
    4985                 :           0 :     return ret;
    4986                 :             : }
    4987                 :             : 
    4988                 :             : 
    4989                 :             : /****************************************************************************/
    4990                 :           0 : void underiv_buf_clear( char *szUnderiv )
    4991                 :             : {
    4992         [ #  # ]:           0 :     if (NULL != szUnderiv)
    4993                 :             :     {
    4994                 :           0 :         szUnderiv[0] = '\0';
    4995                 :             :     }
    4996                 :           0 : }
    4997                 :             : 
    4998                 :             : /****************************************************************************/
    4999                 :           0 : int underiv_list_add( char *szUnderivList, int lenUnderivList, const char *szUnderiv, char cDelimiter )
    5000                 :             : {
    5001   [ #  #  #  #  :           0 :     if (NULL != szUnderivList && lenUnderivList > 0 && NULL != szUnderiv && *szUnderiv)
             #  #  #  # ]
    5002                 :             :     {
    5003                 :           0 :         int lenList = strlen( szUnderivList );
    5004                 :           0 :         int lenAdd = strlen( szUnderiv );
    5005                 :           0 :         int bDelimiter = cDelimiter ? 1 : 0;
    5006         [ #  # ]:           0 :         if (lenList + lenAdd + bDelimiter < lenUnderivList)
    5007                 :             :         {
    5008   [ #  #  #  # ]:           0 :             if (lenList && bDelimiter)
    5009                 :             :             {
    5010                 :           0 :                 szUnderivList[lenList++] = cDelimiter;
    5011                 :             :             }
    5012                 :           0 :             memcpy(szUnderivList + lenList, szUnderiv, (long long)lenAdd + 1); /* +1 adds zero termination */ /* djb-rwth: cast operator added */
    5013                 :           0 :             return lenList + lenAdd;
    5014                 :             :         }
    5015                 :             :     }
    5016                 :             : 
    5017                 :           0 :     return 0;
    5018                 :             : }
    5019                 :             : 
    5020                 :             : 
    5021                 :             : /****************************************************************************/
    5022                 :           0 : const char* underiv_list_get_last( const char *szUnderivList,
    5023                 :             :                                    char cDelimiter )
    5024                 :             : {
    5025   [ #  #  #  # ]:           0 :     if (szUnderivList && cDelimiter)
    5026                 :             :     {
    5027                 :           0 :         const char *p = strrchr( szUnderivList, cDelimiter );
    5028         [ #  # ]:           0 :         if (NULL == p)
    5029                 :             :         {
    5030                 :           0 :             p = szUnderivList;
    5031                 :             :         }
    5032                 :           0 :         return p;
    5033                 :             :     }
    5034                 :             : 
    5035                 :           0 :     return NULL;
    5036                 :             : }
    5037                 :             : 
    5038                 :             : 
    5039                 :             : /****************************************************************************/
    5040                 :           0 : int underiv_compare( const void *p1, const void *p2 )
    5041                 :             : {
    5042                 :           0 :     return strcmp( *(const char **) p1, *(const char **) p2 );
    5043                 :             : }
    5044                 :             : 
    5045                 :             : 
    5046                 :             : /****************************************************************************/
    5047                 :           0 : int underiv_list_add_two_cuts( char *szUnderivList,
    5048                 :             :                                int lenUnderivList,
    5049                 :             :                                char *szUnderiv,
    5050                 :             :                                const char cDelim )
    5051                 :             : {
    5052                 :             :     /* may happen only in RN- case, DERIV_AMINE_tN, but not always  */
    5053                 :           0 :     const char szDelim[] = { cDelim, 0 };
    5054                 :           0 :     char *p1 = strtok( szUnderiv, szDelim );
    5055         [ #  # ]:           0 :     char *p2 = p1 ? strtok( NULL, szDelim ) : NULL;
    5056         [ #  # ]:           0 :     char *p1m = p1 ? strchr( p1, '-' ) : NULL;
    5057         [ #  # ]:           0 :     char *p2m = p2 ? strchr( p2, '-' ) : NULL;
    5058                 :             :     char *pm;
    5059                 :             : 
    5060   [ #  #  #  # ]:           0 :     if (p1m && p2m)
    5061                 :             :     {
    5062   [ #  #  #  # ]:           0 :         if (p1m - p1 == p2m - p2 && !memcmp( p1, p2, p1m - p1 ))
    5063                 :           0 :         {
    5064                 :             :             /* found common prefix */
    5065                 :             :             int diff;
    5066                 :           0 :             *p1m++ = '\0';
    5067                 :           0 :             *p2m++ = '\0';
    5068                 :             :             /* output
    5069                 :             :             [common prefix]-(suffix1)+(suffix2): (suffix1)<(suffix2)  or
    5070                 :             :             [common prefix]-2(suffix1):          (suffix1)==(suffix2)
    5071                 :             :             */
    5072                 :           0 :             underiv_list_add( szUnderivList, lenUnderivList, p1, cDelim ); /* [common prefix] */
    5073                 :           0 :             underiv_list_add( szUnderivList, lenUnderivList, "-", 0 );  /* - */
    5074                 :           0 :             diff = strcmp( p1m, p2m );
    5075         [ #  # ]:           0 :             if (diff > 0)
    5076                 :             :             {
    5077                 :           0 :                 pm = p1m;
    5078                 :           0 :                 p1m = p2m; /* (suffix1) - smaller */
    5079                 :           0 :                 p2m = pm;  /* (suffix2) - greater */
    5080                 :             :             }
    5081         [ #  # ]:           0 :             if (diff)
    5082                 :             :             {
    5083                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, p1m, 0 );  /* (suffix1) */
    5084                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, "+", 0 );  /* - */
    5085                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, p2m, 0 );  /* (suffix2) */
    5086                 :             :             }
    5087                 :             :             else
    5088                 :             :             {
    5089                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, "2", 0 );  /* 2 */
    5090                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, p1m, 0 );  /* (suffix1) */
    5091                 :             :             }
    5092                 :             :         }
    5093                 :             :         else
    5094                 :             :         {
    5095                 :             :             /* should not happen */
    5096                 :           0 :             underiv_list_add( szUnderivList, lenUnderivList, p1, cDelim );
    5097                 :           0 :             underiv_list_add( szUnderivList, lenUnderivList, p2, cDelim );
    5098                 :             :         }
    5099                 :             :     }
    5100                 :             :     else
    5101                 :             :     {
    5102   [ #  #  #  # ]:           0 :         if (p1 && p2)
    5103                 :             :         {
    5104                 :             :             /* should not happen */
    5105                 :           0 :             underiv_list_add( szUnderivList, lenUnderivList, p1, cDelim );
    5106                 :           0 :             underiv_list_add( szUnderivList, lenUnderivList, p2, cDelim );
    5107                 :             :         }
    5108                 :             :         else
    5109                 :             :         {
    5110         [ #  # ]:           0 :             if (p1)
    5111                 :             :             {
    5112                 :             :                 /* happens in case only one num[] is not zero, namely, DERIV_RING2_OUTSIDE_PRECUR */
    5113                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, p1, cDelim );
    5114                 :             :             }
    5115                 :             :         }
    5116                 :             :     }
    5117                 :             : 
    5118                 :           0 :     return 0;
    5119                 :             : }
    5120                 :             : 
    5121                 :             : 
    5122                 :             : /****************************************************************************/
    5123                 :             : #if( UNDERIVATIZE_REPORT == 1 )
    5124                 :           0 : int sort_merge_underiv( char *pSdfValue,
    5125                 :             :                         int bOutputSdf,
    5126                 :             :                         char *szUnderivList,
    5127                 :             :                         char cDerivSeparator,
    5128                 :             :                         const char *pszUnderivPrefix,
    5129                 :             :                         const char *pszUnderivPostfix )
    5130                 :             : {
    5131                 :             : #define UNDERIV_MAX_NUM  512   /*max. number of records in szUnderivList */
    5132                 :           0 :     int num, numUnderiv = 0, i, j, k, m, n;
    5133                 :             :     char *q;
    5134                 :             :     char coeff[16];
    5135                 :             :     char *pszUnderiv[UNDERIV_MAX_NUM];
    5136                 :             : 
    5137                 :           0 :     num = strlen( pSdfValue );
    5138                 :           0 :     n = num + strlen( pszUnderivPrefix ) + 1;
    5139                 :             : 
    5140         [ #  # ]:           0 :     if (n + strlen( pszUnderivPostfix ) + 1 + strlen( szUnderivList ) < MAX_SDF_VALUE)
    5141                 :             :     {
    5142   [ #  #  #  # ]:           0 :         for (numUnderiv = 0, q = strtok( szUnderivList, " " ); numUnderiv < UNDERIV_MAX_NUM && q; q = strtok( NULL, " " ), numUnderiv++)
    5143                 :             :         {
    5144                 :           0 :             pszUnderiv[numUnderiv] = q;
    5145                 :             :         }
    5146                 :             :         /*if ( !bOutputSdf || num ) {*/
    5147                 :           0 :         n = underiv_list_add( pSdfValue, MAX_SDF_VALUE, pszUnderivPrefix, 0 );
    5148                 :             :         /*}*/
    5149         [ #  # ]:           0 :         if (numUnderiv > 1)
    5150                 :             :         {
    5151                 :           0 :             qsort( pszUnderiv, numUnderiv, sizeof( pszUnderiv[0] ), underiv_compare );
    5152                 :             :         }
    5153         [ #  # ]:           0 :         for (i = 0; i < numUnderiv; i = j)
    5154                 :             :         {
    5155   [ #  #  #  # ]:           0 :             for (j = i + 1; j < numUnderiv && !underiv_compare( pszUnderiv + i, pszUnderiv + j ); j++)
    5156                 :             :             {
    5157                 :             :                 ; /* find identical derivatives */
    5158                 :             :             }
    5159                 :           0 :             k = strlen( pszUnderiv[i] );
    5160         [ #  # ]:           0 :             if (1 < ( num = j - i ))
    5161                 :             :             {
    5162                 :           0 :                 k = sprintf(coeff, "%d", num);
    5163                 :             :             }
    5164         [ #  # ]:           0 :             if ((long long)n + (long long)k + sizeof( pszUnderivPostfix ) < MAX_SDF_VALUE) /* djb-rwth: cast operators added */
    5165                 :             :             {
    5166         [ #  # ]:           0 :                 m = i ? cDerivSeparator : 0;
    5167         [ #  # ]:           0 :                 if (num > 1)
    5168                 :             :                 {
    5169                 :           0 :                     underiv_list_add( pSdfValue, MAX_SDF_VALUE, coeff, m );
    5170                 :           0 :                     m = 0;
    5171                 :             :                 }
    5172                 :           0 :                 n = underiv_list_add( pSdfValue, MAX_SDF_VALUE, pszUnderiv[i], m );
    5173                 :             :             }
    5174                 :             :             else
    5175                 :             :             {
    5176                 :           0 :                 underiv_list_add( pSdfValue, MAX_SDF_VALUE, "!", 0 ); /* overflow indicator */
    5177                 :           0 :                 numUnderiv = -( 1 + numUnderiv );
    5178                 :           0 :                 break;
    5179                 :             :             }
    5180                 :             :         }
    5181   [ #  #  #  # ]:           0 :         if (!bOutputSdf || num)
    5182                 :             :         {
    5183                 :           0 :             underiv_list_add( pSdfValue, MAX_SDF_VALUE, pszUnderivPostfix, 0 );
    5184                 :             :         }
    5185                 :             :     }
    5186                 :             : 
    5187                 :           0 :     return numUnderiv;
    5188                 :             : #undef UNDERIV_MAX_NUM
    5189                 :             : }
    5190                 :             : #endif  /* UNDERIVATIZE_REPORT == 1 */
    5191                 :             : 
    5192                 :             : 
    5193                 :             : /****************************************************************************/
    5194                 :           0 : int eliminate_deriv_not_in_list( inp_ATOM *at,
    5195                 :             :                                  DERIV_AT *da,
    5196                 :             :                                  int num_atoms,
    5197                 :             :                                  char *szUnderivList,
    5198                 :             :                                  int lenUnderivList,
    5199                 :             :                                  char *szUnderivList2,
    5200                 :             :                                  int lenUnderivList2,
    5201                 :             :                                  BIT_UNDERIV *bitUnderivList )
    5202                 :             : {
    5203                 :           0 :     int i, j, num_da, num_cuts = 0, num_cuts_this_atom, ret = 0;
    5204                 :             : #if( UNDERIVATIZE_REPORT == 1 )
    5205                 :             : #define UNDERIV_LEN 128
    5206                 :             : #define UNDERIV_LEN2 128
    5207                 :             :     char szUnderiv[UNDERIV_LEN];
    5208                 :             :     char szUnderiv2[UNDERIV_LEN2];
    5209                 :             :     BIT_UNDERIV  bitUnderiv;
    5210                 :             : #else
    5211                 :             : #define UNDERIV_LEN 0
    5212                 :             : #define UNDERIV_LEN2 0
    5213                 :             :     char *szUnderiv = NULL;
    5214                 :             :     char *szUnderiv2 = NULL;
    5215                 :             :     BIT_UNDERIV  bitUnderiv;
    5216                 :             : #endif
    5217         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    5218                 :             :     {
    5219         [ #  # ]:           0 :         if (!da[i].typ[0])
    5220                 :             :         {
    5221                 :           0 :             continue;
    5222                 :             :         }
    5223                 :             :         /* count deriative attachments */
    5224   [ #  #  #  # ]:           0 :         for (num_da = 0; num_da < DERIV_AT_LEN && da[i].typ[num_da]; num_da++)
    5225                 :             :         {
    5226                 :             :             ;
    5227                 :             :         }
    5228         [ #  # ]:           0 :         if (num_da > 2)
    5229                 :             :         {
    5230                 :           0 :             return -1; /* should not happen */
    5231                 :             :         }
    5232   [ #  #  #  # ]:           0 :         if (num_da == 2 && da[i].typ[0] != da[i].typ[1])
    5233                 :             :         {
    5234                 :           0 :             da[i].typ[0] = da[i].typ[1] = 0; /* do not allow */
    5235                 :           0 :             continue;
    5236                 :             :         }
    5237                 :             : 
    5238                 :           0 :         underiv_buf_clear( szUnderiv );
    5239                 :           0 :         underiv_buf_clear( szUnderiv2 );
    5240                 :           0 :         bitUnderiv = 0;
    5241                 :             : 
    5242                 :           0 :         num_cuts_this_atom = 0;
    5243         [ #  # ]:           0 :         if (da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR)
    5244                 :             :         {
    5245                 :           0 :             ret = 0;
    5246                 :             : #ifndef UNDERIV_SYLYL_ONLY
    5247   [ #  #  #  #  :           0 :             if (num_da == 2 && 1 + da[i].num[0] + da[i].num[1] <= MAX_AT_DERIV &&
                   #  # ]
    5248                 :           0 :                  0 < ( ret = is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR( at, i, num_atoms, da + i, 0, szUnderiv, UNDERIV_LEN, szUnderiv2, UNDERIV_LEN2, &bitUnderiv ) ))
    5249                 :             :             {
    5250                 :           0 :                 num_cuts += 2;
    5251                 :           0 :                 underiv_list_add( szUnderivList, lenUnderivList, szUnderiv, ' ' );
    5252                 :           0 :                 underiv_list_add( szUnderivList2, lenUnderivList2, szUnderiv2, ' ' );
    5253                 :           0 :                 *bitUnderivList |= bitUnderiv;
    5254                 :             :             }
    5255                 :             :             else
    5256                 :             : #endif
    5257                 :             :             {
    5258         [ #  # ]:           0 :                 if (ret < 0)
    5259                 :             :                 {
    5260                 :           0 :                     return ret;
    5261                 :             :                 }
    5262                 :             :                 else
    5263                 :             :                 {
    5264                 :           0 :                     da[i].typ[0] = da[i].typ[1] = 0; /* not a derivative */
    5265                 :             :                 }
    5266                 :             :             }
    5267                 :             :         }
    5268                 :             :         else
    5269                 :             :         {
    5270                 :           0 :             ret = 0;
    5271                 :             :             /*if ( da[i].num[0] <= MAX_AT_DERIV && 0 < (ret = is_deriv_chain( at, i, num_atoms, da+i, 0 )) )*/
    5272   [ #  #  #  #  :           0 :             if (IS_DA_NUM_LE( da + i, 0, MAX_AT_DERIV ) && 0 < ( ret = is_deriv_chain( at, i, num_atoms, da + i, 0, szUnderiv, UNDERIV_LEN, szUnderiv2, UNDERIV_LEN2, &bitUnderiv ) ))
             #  #  #  # ]
    5273                 :             :             {
    5274                 :           0 :                 num_cuts++;
    5275                 :           0 :                 num_cuts_this_atom++;
    5276                 :           0 :                 j = 1;
    5277                 :             :                 /*underiv_list_add( szUnderivList, lenUnderivList, szUnderiv, ' ' );*/
    5278                 :             :             }
    5279                 :             :             else
    5280                 :             :             {
    5281         [ #  # ]:           0 :                 if (ret < 0)
    5282                 :             :                 {
    5283                 :           0 :                     return ret;
    5284                 :             :                 }
    5285                 :             :                 else
    5286                 :             :                 {
    5287                 :           0 :                     da[i].ord[0] = da[i].ord[1];
    5288                 :           0 :                     da[i].num[0] = da[i].num[1];
    5289                 :           0 :                     da[i].typ[0] = da[i].typ[1];
    5290                 :           0 :                     da[i].typ[1] = 0;
    5291                 :           0 :                     j = 0;
    5292                 :             :                 }
    5293                 :             :             }
    5294                 :             :             /*underiv_buf_clear( szUnderiv );*/
    5295                 :             :             /*if ( da[i].typ[j] && da[i].num[j] <= MAX_AT_DERIV &&*/
    5296   [ #  #  #  #  :           0 :             if (IS_DA_NUM_LE( da + i, j, MAX_AT_DERIV ) &&
             #  #  #  # ]
    5297                 :           0 :                  0 < ( ret = is_deriv_chain( at, i, num_atoms, da + i, j, szUnderiv, UNDERIV_LEN, szUnderiv2, UNDERIV_LEN2, &bitUnderiv ) ))
    5298                 :             :             {
    5299                 :           0 :                 num_cuts++;
    5300                 :           0 :                 num_cuts_this_atom++;
    5301                 :             :                 /*underiv_list_add( szUnderivList, lenUnderivList, szUnderiv, ' ' );*/
    5302                 :             :             }
    5303                 :             :             else
    5304                 :             :             {
    5305         [ #  # ]:           0 :                 if (ret < 0)
    5306                 :             :                 {
    5307                 :           0 :                     return ret;
    5308                 :             :                 }
    5309                 :             :                 else
    5310                 :             :                 {
    5311                 :           0 :                     da[i].typ[j] = 0;
    5312                 :             :                 }
    5313                 :             :             }
    5314         [ #  # ]:           0 :             if (num_cuts_this_atom == 2)
    5315                 :             :             {
    5316                 :             :                 /* may happen only in RN- case, DERIV_AMINE_tN, but not always  */
    5317                 :           0 :                 underiv_list_add_two_cuts( szUnderivList, lenUnderivList, szUnderiv, ' ' );
    5318                 :           0 :                 underiv_list_add( szUnderivList2, lenUnderivList2, szUnderiv2, ' ' );
    5319                 :           0 :                 *bitUnderivList |= bitUnderiv;
    5320                 :             :             }
    5321                 :             :             else
    5322                 :             :             {
    5323         [ #  # ]:           0 :                 if (num_cuts_this_atom == 1)
    5324                 :             :                 {
    5325                 :           0 :                     underiv_list_add( szUnderivList, lenUnderivList, szUnderiv, ' ' );
    5326                 :           0 :                     underiv_list_add( szUnderivList2, lenUnderivList2, szUnderiv2, ' ' );
    5327                 :           0 :                     *bitUnderivList |= bitUnderiv;
    5328                 :             :                 }
    5329                 :             :             }
    5330                 :             :         }
    5331                 :             :     }
    5332                 :             : 
    5333                 :           0 :     return num_cuts;
    5334                 :             : }
    5335                 :             : 
    5336                 :             : 
    5337                 :             : /****************************************************************************/
    5338                 :           0 : int make_single_cut( inp_ATOM *at, DERIV_AT *da, int iat, int icut )
    5339                 :             : {
    5340                 :           0 :     int ret = -1; /* error flag */
    5341                 :           0 :     int iord = (int) da[iat].ord[icut]; /* ord of the bond in iat */
    5342                 :             : 
    5343         [ #  # ]:           0 :     if (da[iat].typ[icut] & DERIV_DUPLIC)
    5344                 :             :     {
    5345                 :           0 :         return 0;
    5346                 :             :     }
    5347                 :             : 
    5348         [ #  # ]:           0 :     if (iord < 0)
    5349                 :             :     {
    5350                 :           0 :         return -1; /* program error */
    5351                 :             :     }
    5352                 :             : 
    5353                 :             :     else
    5354                 :             :     {
    5355                 :             :         /* find other da[] that affect at[iat] */
    5356                 :             : 
    5357                 :           0 :         int jat = at[iat].neighbor[iord];  /* opposite atom */
    5358                 :           0 :         AT_NUMB *p = is_in_the_list( at[jat].neighbor, (AT_NUMB) iat, at[jat].valence );
    5359         [ #  # ]:           0 :         int jord = p ? ( p - at[jat].neighbor ) : -1;
    5360                 :             :         int i;
    5361                 :           0 :         const int iT = 2;
    5362                 :             : #ifdef UNDERIV_ADD_D_TO_PRECURSOR
    5363                 :             :         const int iD = 1;
    5364                 :             : #endif
    5365                 :             : 
    5366         [ #  # ]:           0 :         if (jord < 0)
    5367                 :             :         {
    5368                 :           0 :             return -1;  /* program error */
    5369                 :             :         }
    5370                 :             : 
    5371                 :           0 :         ret = DisconnectInpAtBond( at, NULL, iat, iord );
    5372                 :             : 
    5373         [ #  # ]:           0 :         if (ret == 1)
    5374                 :             :         {
    5375         [ #  # ]:           0 :             if (da[iat].typ[icut] & DERIV_RING_OUTSIDE_PRECURSOR)
    5376                 :             :             {
    5377                 :             :                 /* at[jat] belongs to the main structure */
    5378                 :           0 :                 at[jat].num_H++;
    5379                 :             : #ifdef UNDERIV_ADD_D_TO_PRECURSOR
    5380                 :             :                 at[jat].num_iso_H[iD] ++; /* add D to the main structure */
    5381                 :             : #endif
    5382                 :           0 :                 at[iat].num_H++;        /* add T to the derivatizing fragment */
    5383                 :           0 :                 at[iat].num_iso_H[iT] ++;
    5384                 :             :             }
    5385                 :             :             else
    5386                 :             :             {
    5387   [ #  #  #  # ]:           0 :                 if (da[iat].typ[icut] && ( da[iat].typ[icut] & DERIV_REPL_N_WITH_O ) == da[iat].typ[icut])
    5388                 :             :                 {
    5389                 :           0 :                     at[jat].num_H++;
    5390                 :           0 :                     at[jat].num_iso_H[iT] ++; /* add T to the derivatizing fragment ??? */
    5391                 :             :                                               /* replace R=N-DerivAgent with R=O H-DerivAgent */
    5392                 :           0 :                     at[iat].elname[0] = 'O';
    5393                 :           0 :                     at[iat].el_number = EL_NUMBER_O;   /* since N replaced with O, do not add H */
    5394                 :             :                 }
    5395                 :             :                 else
    5396                 :             :                 {
    5397   [ #  #  #  #  :           0 :                     if (!icut && da[iat].typ[icut] && ( da[iat].typ[icut] & DERIV_REPL_N_WITH_OH ) == da[iat].typ[icut])
                   #  # ]
    5398                 :             :                     {
    5399                 :             :                         /* cut #0 is the second; in case of DERIV_RING2_OUTSIDE_PRECUR on the 1st cut H has already been added */
    5400                 :           0 :                         at[jat].num_H++;
    5401                 :           0 :                         at[jat].num_iso_H[iT] ++; /* add T to the derivatizing fragment ??? */
    5402                 :             :                                                   /* replace R=N-DerivAgent with R=O H-DerivAgent */
    5403                 :           0 :                         at[iat].elname[0] = 'O';
    5404                 :           0 :                         at[iat].el_number = EL_NUMBER_O;   /* since N replaced with O, do not add H */
    5405                 :             : #ifdef DERIV_RING2_OUTSIDE_PRECUR
    5406         [ #  # ]:           0 :                         if (!( da[iat].typ[icut] & DERIV_RING2_OUTSIDE_PRECUR ))
    5407                 :             :                         {
    5408                 :           0 :                             at[iat].num_H++;
    5409                 :             :                         }
    5410                 :             : #endif
    5411                 :             :                     }
    5412                 :             :                     else
    5413                 :             :                     {
    5414                 :           0 :                         at[iat].num_H++;
    5415                 :             : #ifdef UNDERIV_ADD_D_TO_PRECURSOR
    5416                 :             :                         at[iat].num_iso_H[iD] ++; /* add D to the main structure */
    5417                 :             : #endif
    5418                 :           0 :                         at[jat].num_H++;        /* add T to the derivatizing fragment */
    5419                 :           0 :                         at[jat].num_iso_H[iT] ++;
    5420                 :             :                     }
    5421                 :             :                 }
    5422                 :             :             }
    5423                 :             : 
    5424                 :             :             /* adjust ord for other bonds */
    5425   [ #  #  #  # ]:           0 :             for (i = 0; i < DERIV_AT_LEN && da[iat].typ[i]; i++)
    5426                 :             :             {
    5427         [ #  # ]:           0 :                 if (da[iat].ord[i] == iord)
    5428                 :             :                 {
    5429                 :           0 :                     da[iat].ord[i] = -( 1 + da[iat].ord[i] ); /* mark the bond being disconnected */
    5430                 :             :                 }
    5431         [ #  # ]:           0 :                 else if (da[iat].ord[i] > iord)
    5432                 :             :                 {
    5433                 :           0 :                     da[iat].ord[i] --;
    5434                 :             :                 }
    5435                 :             :             }
    5436   [ #  #  #  # ]:           0 :             for (i = 0; i < DERIV_AT_LEN && da[jat].typ[i]; i++)
    5437                 :             :             {
    5438         [ #  # ]:           0 :                 if (da[jat].ord[i] == jord)
    5439                 :             :                 {
    5440                 :             :                     /* opposite atom needs the same bond to be disconnected */
    5441                 :             : #ifdef NEVER        /* not needed */
    5442                 :             :                     if (da[iat].num[icut] == da[jat].num[i])
    5443                 :             :                     {
    5444                 :             :                         iD = 2; /* mark both as fragments */
    5445                 :             :                     }
    5446                 :             :                     else
    5447                 :             :                         if (da[iat].num[icut] > da[jat].num[i])
    5448                 :             :                         {
    5449                 :             :                             iD = 2; /* greater as a main structure */
    5450                 :             :                             iT = 1; /* mark smaller as a derivatizing fragment */
    5451                 :             :                         }
    5452                 :             : #endif
    5453                 :           0 :                     da[jat].ord[i] = -( 1 + da[jat].ord[i] );
    5454                 :           0 :                     da[jat].typ[i] |= DERIV_DUPLIC; /* do not cut again */
    5455                 :             :                 }
    5456         [ #  # ]:           0 :                 else if (da[jat].ord[i] > jord)
    5457                 :             :                 {
    5458                 :           0 :                     da[jat].ord[i] --;
    5459                 :             :                 }
    5460                 :             : 
    5461                 :             :             }
    5462                 :             :         }
    5463                 :             :     }
    5464                 :             : 
    5465                 :             : 
    5466                 :           0 :     return ret;
    5467                 :             : }
    5468                 :             : 
    5469                 :             : 
    5470                 :             : /****************************************************************************/
    5471                 :           0 : int fill_out_bond_cuts( inp_ATOM *at,
    5472                 :             :                         DERIV_AT *da,
    5473                 :             :                         int num_atoms,
    5474                 :             :                         R2C_ATPAIR *ap,
    5475                 :             :                         int num_cuts_to_check )
    5476                 :             : {
    5477                 :             :     int i, j, k, n;
    5478                 :             :     AT_NUMB at1, at2;
    5479                 :           0 :     int ret = 0;
    5480                 :             :     /* fill out the array of bonds to be cut */
    5481         [ #  # ]:           0 :     for (i = j = 0; i < num_atoms; i++)
    5482                 :             :     {
    5483   [ #  #  #  # ]:           0 :         if (( da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR ) && ( da[i].typ[1] & DERIV_RING_OUTSIDE_PRECURSOR ) &&
    5484   [ #  #  #  # ]:           0 :              da[i].num[0] <= MAX_AT_DERIV && da[i].num[1] <= MAX_AT_DERIV)
    5485                 :             :         {
    5486         [ #  # ]:           0 :             if (j + 1 >= num_cuts_to_check)
    5487                 :             :             {
    5488                 :           0 :                 ret = -2;
    5489                 :           0 :                 goto exit_r2c_num; /* wrong number of cuts = num */
    5490                 :             :             }
    5491         [ #  # ]:           0 :             for (k = 0; k < 2; k++)
    5492                 :             :             {
    5493                 :           0 :                 at1 = i;
    5494                 :           0 :                 at2 = at[at1].neighbor[(int) da[at1].ord[k]];
    5495                 :           0 :                 n = ( at1 > at2 );
    5496                 :           0 :                 ap[j].at[n] = at1;
    5497                 :           0 :                 ap[j].at[1 - n] = at2; /* ap[j].at[0] < ap[j].at[1] */
    5498                 :           0 :                 ap[j].atno = i;
    5499                 :           0 :                 j++;
    5500                 :             :             }
    5501         [ #  # ]:           0 :             if (0 < cmp_r2c_atpair( ap + j - 2, ap + j - 1 ))
    5502                 :             :             {
    5503                 :           0 :                 R2C_ATPAIR ap1 = ap[j - 2];
    5504                 :           0 :                 ap[j - 2] = ap[j - 1];
    5505                 :           0 :                 ap[j - 1] = ap1; /* sort each pair */
    5506                 :             :             }
    5507                 :             :         }
    5508                 :             :         else
    5509                 :             :         {
    5510                 :             :             /* 2013-12-04 DT */
    5511         [ #  # ]:           0 :             if (da[i].typ[0] & DERIV_RING_DMOX_DEOX)
    5512                 :             :             {
    5513                 :           0 :                 int other_atom = (int) da[i].other_atom - 1;
    5514   [ #  #  #  #  :           0 :                 if (da[i].typ[1] || other_atom < 0 || i == other_atom || da[other_atom].other_atom != i + 1 ||
             #  #  #  # ]
    5515   [ #  #  #  # ]:           0 :                      !( da[other_atom].typ[0] & DERIV_RING_DMOX_DEOX ) || da[other_atom].typ[1])
    5516                 :             :                 {
    5517                 :           0 :                     ret = -3;
    5518                 :           0 :                     goto exit_r2c_num;
    5519                 :             :                     /* no other cut may be at the atom in addition to DERIV_RING_DMOX_DEOX
    5520                 :             :                     or no other_atom or other_atom has wrong deriv. type */
    5521                 :             :                 }
    5522                 :             :                 /* make sure the ap[] for the two cuts in the same ring are adjacent */
    5523         [ #  # ]:           0 :                 if (other_atom > i)
    5524                 :             :                 {
    5525         [ #  # ]:           0 :                     if (j + 1 >= num_cuts_to_check)
    5526                 :             :                     {
    5527                 :           0 :                         ret = -2;
    5528                 :           0 :                         goto exit_r2c_num; /* wrong number of cuts = num */
    5529                 :             :                     }
    5530                 :             :                     /* cut #1 */
    5531                 :           0 :                     at1 = i;
    5532                 :           0 :                     at2 = at[at1].neighbor[(int) da[at1].ord[0]];
    5533                 :           0 :                     n = ( at1 > at2 );
    5534                 :           0 :                     ap[j].at[n] = at1;
    5535                 :           0 :                     ap[j].at[1 - n] = at2; /* ap[j].at[0] < ap[j].at[1] */
    5536                 :           0 :                     ap[j].atno = i;
    5537                 :           0 :                     j++;
    5538                 :             :                     /* cut #2 */
    5539                 :           0 :                     at1 = other_atom;
    5540                 :           0 :                     at2 = at[at1].neighbor[(int) da[at1].ord[0]];
    5541                 :           0 :                     n = ( at1 > at2 );
    5542                 :           0 :                     ap[j].at[n] = at1;
    5543                 :           0 :                     ap[j].at[1 - n] = at2; /* ap[j].at[0] < ap[j].at[1] */
    5544                 :           0 :                     ap[j].atno = i;
    5545                 :           0 :                     j++;
    5546                 :             :                 }
    5547                 :             :                 else
    5548                 :             :                 {
    5549                 :             :                     /* add each pair of cuts only once */
    5550                 :           0 :                     continue;
    5551                 :             :                 }
    5552                 :             :             }
    5553                 :             : #if ( COUNT_ALL_NOT_DERIV == 1 )
    5554                 :             :             else
    5555                 :             :             {
    5556   [ #  #  #  # ]:           0 :                 for (k = 0; k < DERIV_AT_LEN && da[i].typ[k]; k++)
    5557                 :             :                 {
    5558   [ #  #  #  # ]:           0 :                     if (j >= num_cuts_to_check || ( da[i].typ[k] & DERIV_RING_OUTSIDE_PRECURSOR ))
    5559                 :             :                     {
    5560                 :           0 :                         ret = -2;
    5561                 :           0 :                         goto exit_r2c_num; /* wrong number of cuts = num or wrong type */
    5562                 :             :                     }
    5563                 :           0 :                     at1 = i;
    5564                 :           0 :                     at2 = at[i].neighbor[(int) da[i].ord[k]];
    5565                 :           0 :                     n = ( at1 > at2 );
    5566                 :             :                     /* pair of atoms possibly to be disconnected */
    5567                 :           0 :                     ap[j].at[n] = at1;
    5568                 :           0 :                     ap[j].at[1 - n] = at2; /* ap[j].at[0] < ap[j].at[1] */
    5569                 :             :                                            /* precursor's atom */
    5570                 :           0 :                     ap[j].atno = i;
    5571                 :           0 :                     j++;
    5572                 :             :                 }
    5573                 :             :             }
    5574                 :             : #endif
    5575                 :             :         }
    5576                 :             :     }
    5577                 :           0 :     return j;
    5578                 :             : 
    5579                 :           0 : exit_r2c_num:
    5580                 :             : 
    5581                 :           0 :     return ret;
    5582                 :             : }
    5583                 :             : 
    5584                 :             : 
    5585                 :             : /****************************************************************************/
    5586                 :           0 : int mark_deriv_agents( inp_ATOM *at,
    5587                 :             :                        DERIV_AT *da,
    5588                 :             :                        int num_atoms,
    5589                 :             :                        R2C_ATPAIR *ap,
    5590                 :             :                        int num_cuts_to_check,
    5591                 :             :                        AT_NUMB *pnum_comp,
    5592                 :             :                        int *pcur_num_at )
    5593                 :             : {
    5594                 :             :     /* mark components to be disconnected */
    5595                 :           0 :     int comp_num = 0;   /* number of components */
    5596                 :           0 :     int cur_num_at = 0; /* number of atoms left after disconnecting the derivatizing agent */
    5597                 :           0 :     int ret = 0;
    5598                 :           0 :     int i, j, k = -1, n;
    5599                 :           0 :     *pnum_comp = 0;
    5600                 :           0 :     *pcur_num_at = 0;
    5601                 :           0 :     UnMarkOtherIndicators( at, num_atoms );
    5602         [ #  # ]:           0 :     for (i = 0; i < num_cuts_to_check; i++)
    5603                 :             :     {
    5604                 :           0 :         n = 0;
    5605         [ #  # ]:           0 :         for (j = 0; j < 2; j++)
    5606                 :             :         {
    5607         [ #  # ]:           0 :             if (da[(int) ap[i].at[j]].typ[0])
    5608                 :             :             {
    5609                 :           0 :                 k = j;
    5610                 :           0 :                 n++;
    5611                 :             :             }
    5612                 :             :         }
    5613         [ #  # ]:           0 :         if (n != 1)
    5614                 :             :         {
    5615                 :           0 :             ret = -3;
    5616                 :           0 :             goto exit_r2c_num; /* wrong atom pair */
    5617                 :             :         }
    5618                 :           0 :         n = ap[i].at[k]; /* marked atom */
    5619         [ #  # ]:           0 :         if (( da[n].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR ))
    5620                 :             :         {
    5621                 :           0 :             n = ap[i].at[1 - k];
    5622                 :             :         }
    5623                 :             :         /* at[n] belongs to the precursor */
    5624         [ #  # ]:           0 :         if (!at[n].at_type)
    5625                 :             :         {
    5626                 :           0 :             comp_num++;
    5627                 :           0 :             cur_num_at = mark_atoms_ap( at, n, ap, num_cuts_to_check, cur_num_at, comp_num );
    5628                 :             :         }
    5629                 :             :     }
    5630                 :             : 
    5631                 :           0 :     *pnum_comp = comp_num;
    5632                 :           0 :     *pcur_num_at = cur_num_at;
    5633                 :             : 
    5634                 :           0 : exit_r2c_num:
    5635                 :             : 
    5636                 :           0 :     return ret;
    5637                 :             : }
    5638                 :             : 
    5639                 :             : 
    5640                 :             : #ifdef UNDERIV_ADD_EXPLICIT_H
    5641                 :             : 
    5642                 :             : 
    5643                 :             : /*****************************************************************************/
    5644                 :           0 : int add_explicit_H( INP_ATOM_DATA *inp_cur_data )
    5645                 :             : {
    5646                 :             :     /* do not care about stereo parities for now */
    5647                 :           0 :     int curRemovedH, num_added_explicit_H, iat, num_removed_H, m, num_H, num_atoms = inp_cur_data->num_at;
    5648         [ #  # ]:           0 :     if (( num_removed_H = inp_cur_data->num_removed_H ) > 0)
    5649                 :             :     {
    5650                 :           0 :         inp_ATOM *at_H = (inp_ATOM *) inchi_calloc( num_removed_H, sizeof( inp_ATOM ) );
    5651                 :           0 :         inp_ATOM *at = inp_cur_data->at;
    5652         [ #  # ]:           0 :         for (curRemovedH = num_atoms, num_added_explicit_H = 0; curRemovedH < num_atoms + num_removed_H; curRemovedH++)
    5653                 :             :         {
    5654         [ #  # ]:           0 :             if (at[curRemovedH].el_number == EL_NUMBER_H &&
    5655         [ #  # ]:           0 :                  1 == at[curRemovedH].valence &&
    5656         [ #  # ]:           0 :                  ( iat = (int) at[curRemovedH].neighbor[0] ) < num_atoms &&
    5657   [ #  #  #  # ]:           0 :                  0 <= ( m = at[curRemovedH].iso_atw_diff ) &&
    5658                 :             :                  m <= NUM_H_ISOTOPES)
    5659                 :             :             {
    5660                 :             :                 /* num_H is the total number of all implicit H including isotopic H */
    5661   [ #  #  #  #  :           0 :                 if (at_H && 0 < at[iat].num_H && 0 <= ( num_H = at[iat].num_H - NUM_ISO_H( at, iat ) ) && ( (!m && num_H) || (m && at[iat].num_iso_H[m - 1]) )) /* djb-rwth: addressing LLVM warning; fixing a NULL pointer dereference */
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5662                 :             :                 { /* number of implicit H > 0 */
    5663                 :           0 :                     int val = at[iat].valence;
    5664                 :             :                     /* set hydrogen atom */
    5665                 :           0 :                     at_H[num_added_explicit_H] = at[curRemovedH];
    5666                 :           0 :                     at_H[num_added_explicit_H].neighbor[0] = iat;
    5667                 :           0 :                     at_H[num_added_explicit_H].valence = 1;
    5668                 :           0 :                     at_H[num_added_explicit_H].chem_bonds_valence = at_H[num_added_explicit_H].bond_type[0];
    5669                 :             :                     /* set heavy atom */
    5670                 :           0 :                     at[iat].neighbor[val] = num_atoms + num_added_explicit_H;
    5671                 :           0 :                     at[iat].bond_type[val] = at_H[num_added_explicit_H].bond_type[0];
    5672                 :           0 :                     at[iat].bond_stereo[val] = -at_H[num_added_explicit_H].bond_stereo[0];
    5673                 :           0 :                     at[iat].valence++;
    5674   [ #  #  #  # ]:           0 :                     if (BOND_SINGLE <= at[iat].bond_type[val] && at[iat].bond_type[val] <= BOND_TRIPLE)
    5675                 :             :                     {
    5676                 :           0 :                         at[iat].chem_bonds_valence += at[iat].bond_type[val];
    5677                 :             :                     }
    5678                 :             :                     else
    5679                 :             :                     {
    5680                 :             :                         /* should not happen */
    5681                 :           0 :                         at_H[num_added_explicit_H].bond_type[0] = at[iat].bond_type[val] = BOND_SINGLE;
    5682                 :           0 :                         at[iat].chem_bonds_valence += BOND_SINGLE;
    5683                 :           0 :                         at_H[num_added_explicit_H].chem_bonds_valence = BOND_SINGLE;
    5684                 :             :                     }
    5685                 :           0 :                     at[iat].num_H--;
    5686         [ #  # ]:           0 :                     if (m)
    5687                 :             :                     {
    5688                 :           0 :                         at[iat].num_iso_H[m - 1] --;
    5689                 :             :                     }
    5690                 :           0 :                     num_added_explicit_H++;
    5691                 :             :                 }
    5692                 :             :             }
    5693                 :             :         }
    5694   [ #  #  #  # ]:           0 :         if (0 < num_added_explicit_H && num_added_explicit_H <= num_removed_H)
    5695                 :             :         {
    5696                 :           0 :             memcpy(at + num_atoms, at_H, num_added_explicit_H * sizeof(at));
    5697                 :           0 :             inp_cur_data->num_removed_H = 0;
    5698                 :           0 :             inp_cur_data->num_at = ( num_atoms += num_added_explicit_H );
    5699                 :             :         }
    5700         [ #  # ]:           0 :         inchi_free( at_H );
    5701                 :             :     }
    5702                 :           0 :     return num_atoms;
    5703                 :             : }
    5704                 :             : #endif  /* UNDERIV_ADD_EXPLICIT_H */
    5705                 :             : 
    5706                 :             : 
    5707                 :             : /****************************************************************************
    5708                 :             : Main underivatization procedure
    5709                 :             : ****************************************************************************/
    5710                 :           0 : int OAD_Edit_Underivatize( struct tagINCHI_CLOCK *ic,
    5711                 :             :                            struct tagCANON_GLOBALS *pCG,
    5712                 :             :                            ORIG_ATOM_DATA *orig_inp_data,
    5713                 :             :                            int bOutputSdf,
    5714                 :             :                            int bOutputReport,
    5715                 :             :                            char *pSdfValue )
    5716                 :             : {
    5717                 :             : 
    5718                 :             : #define ALLOC_AP \
    5719                 :             :         if ( 0 < num_cuts_to_check && (lenAllocated_ap < num_cuts_to_check || !ap) ) {\
    5720                 :             :             if ( ap )\
    5721                 :             :                 inchi_free( ap );\
    5722                 :             :             ap = (R2C_ATPAIR *) inchi_malloc( num_cuts_to_check * sizeof(ap[0]) );\
    5723                 :             :             if ( !ap ) {\
    5724                 :             :                 ret = -1;\
    5725                 :             :                 goto exit_function; /* malloc failure */\
    5726                 :             :             }\
    5727                 :             :             lenAllocated_ap = num_cuts_to_check;\
    5728                 :             :         }
    5729                 :             : 
    5730                 :           0 :     int ret = 0, i, j, k, m, n, num_atoms, num_components, i_component, nFound, num, cur_num_at = 0, len, ind1, ind2, ind3; /* djb-rwth: adding variables for char -> int conversion of subscripts; initialisation added */
    5731                 :             :     int num_cuts, num_ring_cuts, num_cut_pieces, num_cuts_to_check;
    5732                 :           0 :     inp_ATOM *at = orig_inp_data->at; /* djb-rwth: ignoring LLVM warning: value used */
    5733                 :           0 :     INP_ATOM_DATA *inp_cur_data = NULL;
    5734                 :           0 :     DERIV_AT      *da = NULL;
    5735                 :           0 :     R2C_ATPAIR    *ap = NULL;
    5736                 :           0 :     int            lenAllocated_ap = 0;
    5737                 :           0 :     int  nTotNumCuts = 0;
    5738                 :           0 :     int  num_removed_H = 0; /* djb-rwth: ignoring LLVM warning: variable used */
    5739                 :             : #ifdef FIX_UNDERIV_TO_SDF
    5740                 :           0 :     inp_ATOM *at2 = NULL;
    5741                 :             : #endif
    5742                 :             : #if ( UNDERIVATIZE_REPORT == 1 )
    5743                 :             : #define UNDERIV_LIST_LEN 2048
    5744                 :             : #define UNDERIV_LIST_LEN2 2048
    5745                 :           0 :     char szUnderivList[UNDERIV_LIST_LEN] = "";
    5746                 :           0 :     char szUnderivList2[UNDERIV_LIST_LEN2] = "";
    5747                 :           0 :     char underivPrefix[] = "\tDeriv=";
    5748                 :           0 :     char underivPostfix[] = "";
    5749                 :           0 :     char underivPrefix2[] = "\tDeriv2=";
    5750                 :           0 :     char underivPostfix2[] = "";
    5751                 :           0 :     char underivPrefix3[] = "\tDerivBits=";
    5752                 :           0 :     char underivPostfix3[] = "";
    5753                 :           0 :     char cDerivSeparator = ',';
    5754                 :           0 :     int numUnderiv = 0, numUnderiv2 = 0, numUnderiv3 = 0; /* djb-rwth: ignoring LLVM warning: variables used to store function return value */
    5755                 :           0 :     BIT_UNDERIV bitUnderivList = 0;
    5756                 :             :     char szbitUnderivList[16]; /* int32 has at most 32/4=8 hexadecimal digits + 0x prefix + zero termination = 8+2+1=11 */
    5757                 :             : #else
    5758                 :             : #define UNDERIV_LIST_LEN 0
    5759                 :             : #define UNDERIV_LIST_LEN2 0
    5760                 :             : #define UNDERIV_MAX_NUM  0   /*max. number of records in szUnderivList */
    5761                 :             :     char *szUnderivList = NULL;
    5762                 :             :     char *szUnderivList2 = NULL;
    5763                 :             :     char **pszUnderiv = NULL;
    5764                 :             :     BIT_UNDERIV bitUnderivList = 0;
    5765                 :             : #endif
    5766                 :             : 
    5767                 :             : 
    5768                 :             :     /* Prepare */
    5769                 :             : 
    5770                 :             :     /*set_R2C_el_numbers( );*/
    5771                 :             : 
    5772                 :             : #ifndef UNDERIV_ADD_EXPLICIT_H
    5773                 :             :     num_atoms = remove_terminal_HDT( orig_inp_data->num_inp_atoms, at, 1, NULL );
    5774                 :             :     /*^^^^^ always accomodate accomodate FIX_TERM_H_CHRG_BUG - IPl, July 2008*/
    5775                 :             :     num_removed_H = orig_inp_data->num_inp_atoms - num_atoms;
    5776                 :             :     orig_inp_data->num_inp_atoms = num_atoms;
    5777                 :             : #endif
    5778                 :             : 
    5779                 :             :     /* Initialize */
    5780                 :           0 :     UnMarkDisconnectedComponents( orig_inp_data );
    5781                 :             :     /* djb-rwth: removing redundant code */
    5782                 :             : 
    5783                 :             :     /* Mark */
    5784                 :           0 :     num_components = MarkDisconnectedComponents( orig_inp_data, 0 );
    5785                 :           0 :     inp_cur_data = (INP_ATOM_DATA *) inchi_calloc( num_components, sizeof( inp_cur_data[0] ) );
    5786                 :             : 
    5787         [ #  # ]:           0 :     for (i_component = 0; i_component < num_components; i_component++)
    5788                 :             :     {
    5789                 :           0 :         CreateInpAtomData(inp_cur_data + i_component, orig_inp_data->nCurAtLen[i_component], 0);
    5790                 :             : 
    5791                 :           0 :         inp_cur_data[i_component].num_at = ExtractConnectedComponent(orig_inp_data->at, orig_inp_data->num_inp_atoms, i_component + 1, inp_cur_data[i_component].at);
    5792                 :             : 
    5793                 :             :         /*  error processing */
    5794   [ #  #  #  # ]:           0 :         if (inp_cur_data[i_component].num_at <= 0 || orig_inp_data->nCurAtLen[i_component] != inp_cur_data[i_component].num_at)
    5795                 :             :         {
    5796                 :           0 :             ret = -(i_component + 1); /* severe error */
    5797                 :           0 :             goto exit_function;
    5798                 :             :         }
    5799                 :             : 
    5800                 :             : #ifdef UNDERIV_ADD_EXPLICIT_H
    5801                 :           0 :         num_atoms = remove_terminal_HDT(inp_cur_data[i_component].num_at, inp_cur_data[i_component].at, 1, NULL);
    5802                 :           0 :         inp_cur_data[i_component].num_removed_H = inp_cur_data[i_component].num_at - num_atoms;
    5803                 :           0 :         inp_cur_data[i_component].num_at = num_atoms;
    5804                 :             : #endif
    5805                 :             : 
    5806                 :             :         /* Initialize */
    5807                 :           0 :         num_atoms = inp_cur_data[i_component].num_at;
    5808                 :           0 :         at = inp_cur_data[i_component].at;
    5809                 :           0 :         add_DT_to_num_H(num_atoms, at);
    5810                 :             : 
    5811                 :           0 :         UnMarkRingSystemsInp(at, num_atoms);
    5812                 :           0 :         UnMarkOtherIndicators(at, num_atoms);
    5813                 :           0 :         UnMarkOneComponent(at, num_atoms);
    5814                 :           0 :         MarkRingSystemsInp(at, num_atoms, 0);
    5815                 :             : #ifdef FIX_UNDERIV_TO_SDF
    5816         [ #  # ]:           0 :         if (bOutputSdf)
    5817                 :             :         {
    5818                 :             :             /* save orig. bond types to restore them after replacing them with aromatic */
    5819         [ #  # ]:           0 :             if (at2)
    5820                 :             :             {
    5821         [ #  # ]:           0 :                 inchi_free(at2);
    5822                 :             :             }
    5823         [ #  # ]:           0 :             if ((at2 = (inp_ATOM*)inchi_malloc(num_atoms * sizeof(inp_ATOM)))) /* djb-rwth: addressing LLVM warning */
    5824                 :             :             {
    5825                 :           0 :                 memcpy(at2, at, num_atoms * sizeof(inp_ATOM));
    5826                 :             :             }
    5827                 :             :         }
    5828                 :             : #endif
    5829                 :             : 
    5830                 :             :         /* Mark aromatic bonds */
    5831                 :           0 :         ret = mark_arom_bonds(ic, pCG, at, num_atoms);
    5832         [ #  # ]:           0 :         if (ret < 0)
    5833                 :             :         {
    5834                 :           0 :             goto exit_function;
    5835                 :             :         }
    5836                 :           0 :         ret = 0;
    5837                 :             : 
    5838         [ #  # ]:           0 :         if (da)
    5839                 :             :         {
    5840         [ #  # ]:           0 :             inchi_free(da);
    5841                 :             :         }
    5842                 :           0 :         da = (DERIV_AT*)inchi_calloc(num_atoms, sizeof(da[0]));
    5843                 :             : 
    5844                 :             :         /* Detect derivatives */
    5845                 :           0 :         nFound = 0;
    5846         [ #  # ]:           0 :         for (i = 0; i < num_atoms; i++)
    5847                 :             :         {
    5848   [ #  #  #  # ]:           0 :             if (at[i].bCutVertex && !da[i].typ[inchi_min(at[i].valence, DERIV_AT_LEN) - 1])
    5849                 :             :             {
    5850         [ #  # ]:           0 :                 for (k = 0; k < at[i].valence; k++)
    5851                 :             :                 {
    5852                 :           0 :                     num = count_one_bond_atoms(at, da, i, k, CFLAG_MARK_BRANCH, &nFound);
    5853                 :           0 :                     UnMarkOtherIndicators(at, num_atoms);
    5854         [ #  # ]:           0 :                     if (num < 0)
    5855                 :             :                     {
    5856                 :           0 :                         ret = num; /* severe error */
    5857                 :           0 :                         goto exit_function;
    5858                 :             :                     }
    5859                 :             :                 }
    5860                 :             :             }
    5861                 :             :         }
    5862                 :             : 
    5863                 :             :         /* Prepare cuts: remove cuts that are not to be done */
    5864                 :             :         /* in addition, count ring cuts DERIV_RING_OUTSIDE_PRECURSOR */
    5865                 :           0 :         num_ring_cuts = 0;
    5866                 :           0 :         num_cuts = 0;
    5867                 :           0 :         num_cut_pieces = 0;
    5868         [ #  # ]:           0 :         if (da) /* djb-rwth: fixing a NULL pointer dereference */
    5869                 :             :         {
    5870         [ #  # ]:           0 :             for (i = num = 0; i < num_atoms; i++) /* djb-rwth: ignoring LLVM warning: variable used */
    5871                 :             :             {
    5872                 :             :                 /*for ( len = 0; len < MAX_AT_DERIV && da[i].typ[len]; len ++ ) -- bug fixed 2013-11-07 DCh */
    5873   [ #  #  #  # ]:           0 :                 for (len = 0; len < DERIV_AT_LEN && da[i].typ[len]; len++)
    5874                 :             :                 {
    5875                 :             :                     ;
    5876                 :             :                 }
    5877   [ #  #  #  #  :           0 :                 switch (len)
                   #  # ]
    5878                 :             :                 {
    5879                 :             : 
    5880                 :           0 :                 case 0:
    5881                 :           0 :                     continue;
    5882                 :             : 
    5883                 :           0 :                 case 1:
    5884                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    5885         [ #  # ]:           0 :                     if (da[i].typ[0] & DERIV_RING_DMOX_DEOX)
    5886                 :             :                     {
    5887   [ #  #  #  # ]:           0 :                         if (!da[i].other_atom || (j = da[i].other_atom - 1) >= num_atoms ||
    5888         [ #  # ]:           0 :                             (da[j].typ[0] ^ DERIV_RING_DMOX_DEOX) != da[i].typ[0] ||
    5889         [ #  # ]:           0 :                             da[j].other_atom - 1 != i)
    5890                 :             :                         {
    5891                 :             :                             /* program error */
    5892                 :           0 :                             da[i].num[0] = NOT_AT_DERIV;
    5893                 :             :                         }
    5894                 :             :                         else
    5895                 :             :                         {
    5896                 :             :                             /* one of 2 ring cuts */
    5897                 :           0 :                             num_cuts += 1;
    5898                 :           0 :                             num_ring_cuts += 1;
    5899                 :           0 :                             num_cut_pieces += (i > j); /* count pieces only once */
    5900                 :             :                         }
    5901                 :             :                     }
    5902                 :             :                     else
    5903                 :             : #endif
    5904                 :             :                     {
    5905                 :             :                         /* single cut: unconditional */
    5906                 :           0 :                         num_cuts += 1;
    5907                 :           0 :                         num_cut_pieces += 1;
    5908                 :             :                     }
    5909                 :           0 :                     continue;
    5910                 :             : 
    5911                 :           0 :                 case 2:
    5912   [ #  #  #  # ]:           0 :                     if ((da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR) && (da[i].typ[1] & DERIV_RING_OUTSIDE_PRECURSOR))
    5913                 :             :                     {
    5914                 :             :                         /* double cut, unconditional */
    5915                 :           0 :                         num_ring_cuts += 2;
    5916                 :           0 :                         num_cuts += 2;
    5917                 :           0 :                         num_cut_pieces += 1;
    5918                 :           0 :                         continue;
    5919                 :             :                     }
    5920                 :             :                     else
    5921                 :             : #ifdef DERIV_RING2_OUTSIDE_PRECUR
    5922   [ #  #  #  #  :           0 :                         if (da[i].typ[0] && (da[i].typ[0] & DERIV_RING2_OUTSIDE_PRECUR) == da[i].typ[0] && da[i].typ[1] == da[i].typ[0])
                   #  # ]
    5923                 :             :                         {
    5924                 :             :                             /* double cut, unconditional */
    5925                 :           0 :                             num_ring_cuts += 2;
    5926                 :           0 :                             num_cuts += 2;
    5927                 :           0 :                             num_cut_pieces += 1;
    5928                 :           0 :                             continue;
    5929                 :             :                         }
    5930                 :             :                         else
    5931                 :             : #endif
    5932   [ #  #  #  # ]:           0 :                             if (da[i].typ[0] == DERIV_AMINE_tN && da[i].typ[1] == DERIV_AMINE_tN)
    5933                 :             :                             {
    5934                 :             :                                 /* double cut, unconditional */
    5935                 :           0 :                                 num_cuts += 2;
    5936                 :           0 :                                 num_cut_pieces += 2;
    5937                 :           0 :                                 continue;
    5938                 :             :                             }
    5939                 :             :                             else
    5940                 :             : #ifdef DERIV_RO_COX
    5941   [ #  #  #  # ]:           0 :                                 if (da[i].typ[0] == DERIV_RO_COX && da[i].typ[1] == DERIV_RO_COX)
    5942                 :             :                                 {
    5943         [ #  # ]:           0 :                                     if (da[i].num[0] == da[i].num[1])
    5944                 :             :                                     {
    5945                 :           0 :                                         memset(da + i, 0, sizeof(da[0])); /* don't remove if the two agents are identical */ /* djb-rwth: memset_s C11/Annex K variant? */
    5946                 :           0 :                                         continue;
    5947                 :             :                                     }
    5948                 :             :                                     else
    5949                 :             :                                     {
    5950   [ #  #  #  # ]:           0 :                                         if (da[i].num[0] && da[i].num[1])
    5951                 :           0 :                                         {
    5952                 :             :                                             static char pref_RO_COX[] = {/*likely deriv.agent*/12, 9, 6, 13, 3, 8/*likely precursor*/, 0 };
    5953                 :           0 :                                             char* p0 = strchr(pref_RO_COX, da[i].num[0]);
    5954                 :           0 :                                             char* p1 = strchr(pref_RO_COX, da[i].num[1]);
    5955   [ #  #  #  # ]:           0 :                                             if (p0 && p1)
    5956                 :             :                                             {
    5957                 :           0 :                                                 j = p1 < p0; /* j=1 => deriv. agent num[1] has higher priority */
    5958                 :             :                                             }
    5959                 :             :                                             else
    5960                 :             :                                             {
    5961   [ #  #  #  # ]:           0 :                                                 j = p0 ? 0 : p1 ? 1 : -1; /* we are here if there is a program error */
    5962                 :             :                                             }
    5963                 :             :                                         }
    5964                 :             :                                         else
    5965                 :             :                                         {
    5966   [ #  #  #  # ]:           0 :                                             j = da[i].num[0] ? 0 : da[i].num[1] ? 1 : -1; /* we are here if there is a program error */
    5967                 :             :                                         }
    5968                 :             :                                     }
    5969                 :             : 
    5970   [ #  #  #  # ]:           0 :                                     switch (j)
    5971                 :             :                                     {
    5972                 :           0 :                                     case 1:
    5973                 :           0 :                                         da[i].num[0] = da[i].num[1];
    5974                 :           0 :                                         da[i].ord[0] = da[i].ord[1];
    5975                 :             :                                         /* fall through */
    5976                 :           0 :                                     case 0:
    5977                 :           0 :                                         da[i].typ[1] = 0;
    5978                 :           0 :                                         da[i].num[1] = 0;
    5979                 :           0 :                                         da[i].ord[1] = 0;
    5980                 :           0 :                                         num_cuts += 1;
    5981                 :           0 :                                         num_cut_pieces += 1;
    5982                 :           0 :                                         continue;
    5983                 :           0 :                                     case -1:
    5984                 :           0 :                                         memset(da + i, 0, sizeof(da[0])); /* djb-rwth: memset_s C11/Annex K variant? */
    5985                 :           0 :                                         break; /* will produce error */
    5986                 :             :                                     }
    5987                 :             :                                 }
    5988                 :             : #endif  /* DERIV_RO_COX */
    5989                 :             :                                 else
    5990         [ #  # ]:           0 :                                     if (da[i].typ[0] == da[i].typ[1])
    5991                 :           0 :                                     {
    5992                 :           0 :                                         int sy0 = 0, sy1 = 0;
    5993                 :             :                                         /* DERIV_BRIDGE_O or DERIV_BRIDGE_NH ; cut off the smallest */
    5994         [ #  # ]:           0 :                                         if (0 == is_deriv_chain(at, i, num_atoms, da + i, 0, NULL, 0, NULL, 0, NULL))
    5995                 :             :                                         {
    5996                 :           0 :                                             da[i].num[0] = NOT_AT_DERIV;
    5997                 :             :                                         }
    5998                 :             :                                         else
    5999                 :             :                                         {
    6000                 :           0 :                                             ind1 = da[i].ord[0] - '0'; /* djb-rwth: converting char to int for subscript use */
    6001                 :           0 :                                             sy0 = is_silyl2(at, at[i].neighbor[ind1], i);
    6002                 :             :                                         }
    6003         [ #  # ]:           0 :                                         if (0 == is_deriv_chain(at, i, num_atoms, da + i, 1, NULL, 0, NULL, 0, NULL))
    6004                 :             :                                         {
    6005                 :           0 :                                             da[i].num[1] = NOT_AT_DERIV;
    6006                 :             :                                         }
    6007                 :             :                                         else
    6008                 :             :                                         {
    6009                 :           0 :                                             ind2 = da[i].ord[1] - '0'; /* djb-rwth: converting char to int for subscript use */
    6010                 :           0 :                                             sy1 = is_silyl2(at, at[i].neighbor[ind2], i);
    6011                 :             :                                         }
    6012   [ #  #  #  #  :           0 :                                         if ((sy1 && (!sy0 || sy1 < sy0)) || (!(sy0 || sy1) && da[i].num[0] > da[i].num[1])) /* djb-rwth: addressing LLVM warnings */
          #  #  #  #  #  
                #  #  # ]
    6013                 :             :                                         {
    6014                 :           0 :                                             da[i].num[0] = da[i].num[1];
    6015                 :           0 :                                             da[i].ord[0] = da[i].ord[1];
    6016                 :           0 :                                             da[i].typ[0] = da[i].typ[1];
    6017                 :           0 :                                             da[i].typ[1] = 0;
    6018                 :           0 :                                             num_cuts += 1;
    6019                 :           0 :                                             num_cut_pieces += 1;
    6020                 :             :                                         }
    6021                 :             :                                         else
    6022                 :             :                                         {
    6023   [ #  #  #  #  :           0 :                                             if ((sy0 && (!sy1 || sy0 < sy1)) || (!(sy0 || sy1) && da[i].num[0] < da[i].num[1])) /* djb-rwth: addressing LLVM warning */
          #  #  #  #  #  
                #  #  # ]
    6024                 :             :                                             {
    6025                 :           0 :                                                 da[i].typ[1] = 0;
    6026                 :           0 :                                                 num_cuts += 1;
    6027                 :           0 :                                                 num_cut_pieces += 1;
    6028                 :             :                                             }
    6029                 :             :                                             else
    6030                 :             :                                             {
    6031                 :             :                                                 /* attachments have same size: ignore both */
    6032                 :             :                                                 /* ??? check for standard derivatizations ??? */
    6033                 :           0 :                                                 da[i].typ[0] = 0;
    6034                 :           0 :                                                 da[i].typ[1] = 0;
    6035                 :             :                                             }
    6036                 :             :                                         }
    6037                 :           0 :                                         continue;
    6038                 :             :                                     }
    6039                 :             : #ifdef DERIV_RO_COX
    6040                 :             :                                     else
    6041   [ #  #  #  # ]:           0 :                                         if ((da[i].typ[0] & (DERIV_RO_COX | DERIV_BRIDGE_O)) && (da[i].typ[1] & (DERIV_RO_COX | DERIV_BRIDGE_O)))
    6042                 :             :                                         {
    6043                 :             :                                             /*static char pref_RO_COX2[] = {13, 3, 8*/
    6044                 :             :                                             /*likely precursor*/
    6045                 :             :                                             /*, 0};*/
    6046                 :           0 :                                             j = (da[i].typ[0] == DERIV_BRIDGE_O);  /* da[i].typ[j] == DERIV_RO_COX */
    6047                 :             :                                             /*------------------------------------------------------------------------*
    6048                 :             :                                             * discard DERIV_RO_COX only in case [CH3-C(=O)]-O-[CH3]                  *
    6049                 :             :                                             *                      precursor DERIV_BRIDGE_O   DERIV_RO_COX precursor *
    6050                 :             :                                             * has already been done in finding DERIV_RO_COX                          *
    6051                 :             :                                             *------------------------------------------------------------------------*/
    6052                 :             : #ifdef NEVER
    6053                 :             :                                             /* methyl/ethyl alcoholes have already been excluded in get_traversed_deriv_type(...)
    6054                 :             :                                             for etyl/methyl acetate/benzoate. Only acetate/benzoic acids may their precursors */
    6055                 :             :                                             if (da[i].num[j] == 3 &&  /* -O-[C(=O)-CH3]  : DERIV_RO_COX*/
    6056                 :             :                                                 da[i].num[1 - j] == 1)
    6057                 :             :                                             { /* [CH3]-O-C(=O)-R : DERIV_BRIDGE_O; derivatizin agent in [] */
    6058                 :             :                                                 j = 1 - j; /* remove da[i].xxx[j] */
    6059                 :             :                                         }
    6060                 :             : #endif
    6061                 :             :                                             /*
    6062                 :             :                                             O
    6063                 :             :                                             ||
    6064                 :             :                                             R----O----C---X
    6065                 :             : 
    6066                 :             :                                             R----O---OC---X
    6067                 :             :                                             DERIV_BRIDGE_O \___/\____precursor__/
    6068                 :             : 
    6069                 :             :                                             R----O----COX
    6070                 :             :                                             \___precursor__/ \___/ DERIV_RO_COX
    6071                 :             : 
    6072                 :             :                                             rule: If R is >Si< then select DERIV_BRIDGE_O
    6073                 :             : 
    6074                 :             :                                             */
    6075                 :             : 
    6076         [ #  # ]:           0 :                                             if (j /* choose DERIV_RO_COX */)
    6077                 :             :                                             {
    6078                 :             :                                                 /* da[i].typ[1]=DERIV_RO_COX is not likely a precursor */
    6079                 :           0 :                                                 da[i].typ[0] = da[i].typ[1];
    6080                 :           0 :                                                 da[i].num[0] = da[i].num[1];
    6081                 :           0 :                                                 da[i].ord[0] = da[i].ord[1];
    6082                 :             :                                             }
    6083                 :           0 :                                             da[i].typ[1] = 0;
    6084                 :           0 :                                             da[i].num[1] = 0;
    6085                 :           0 :                                             da[i].ord[1] = 0;
    6086                 :             : 
    6087                 :           0 :                                             num_cuts += 1;
    6088                 :           0 :                                             num_cut_pieces += 1;
    6089                 :           0 :                                             continue;
    6090                 :             :                 }
    6091                 :             : #endif
    6092                 :           0 :                     ret = -88;
    6093                 :           0 :                     goto exit_function; /* unexpected */
    6094                 :             : 
    6095                 :           0 :                 case 3:
    6096         [ #  # ]:           0 :                     if (da[i].typ[0] == da[i].typ[1] &&
    6097         [ #  # ]:           0 :                         da[i].typ[0] == da[i].typ[2] &&
    6098         [ #  # ]:           0 :                         da[i].typ[0] == DERIV_AMINE_tN)
    6099                 :           0 :                     {
    6100                 :             :                         int x, y, z;
    6101                 :           0 :                         int sy[3] = { 0, 0, 0 }; /* silyl */
    6102                 :             : 
    6103         [ #  # ]:           0 :                         if (0 == is_deriv_chain(at, i, num_atoms, da + i, 0, NULL, 0, NULL, 0, NULL))
    6104                 :             :                         {
    6105                 :           0 :                             da[i].num[0] = NOT_AT_DERIV;
    6106                 :             :                         }
    6107                 :             :                         else
    6108                 :             :                         {
    6109                 :           0 :                             ind1 = da[i].ord[0] - '0'; /* djb-rwth: converting char to int for subscript use */
    6110                 :           0 :                             sy[0] = is_silyl2(at, at[i].neighbor[ind1], i);
    6111                 :             :                         }
    6112                 :             : 
    6113         [ #  # ]:           0 :                         if (0 == is_deriv_chain(at, i, num_atoms, da + i, 1, NULL, 0, NULL, 0, NULL))
    6114                 :             :                         {
    6115                 :           0 :                             da[i].num[1] = NOT_AT_DERIV;
    6116                 :             :                         }
    6117                 :             :                         else
    6118                 :             :                         {
    6119                 :           0 :                             ind2 = da[i].ord[1] - '0'; /* djb-rwth: converting char to int for subscript use */
    6120                 :           0 :                             sy[1] = is_silyl2(at, at[i].neighbor[ind2], i);
    6121                 :             :                         }
    6122                 :             : 
    6123         [ #  # ]:           0 :                         if (0 == is_deriv_chain(at, i, num_atoms, da + i, 2, NULL, 0, NULL, 0, NULL))
    6124                 :             :                         {
    6125                 :           0 :                             da[i].num[2] = NOT_AT_DERIV;
    6126                 :             :                         }
    6127                 :             :                         else
    6128                 :             :                         {
    6129                 :           0 :                             ind3 = da[i].ord[2] - '0'; /* djb-rwth: converting char to int for subscript use */
    6130                 :           0 :                             sy[2] = is_silyl2(at, at[i].neighbor[ind3], i);
    6131                 :             :                         }
    6132                 :             : 
    6133                 :             :                         /* djb-rwth: removing redundant code */
    6134                 :             : 
    6135   [ #  #  #  #  :           0 :                         x = ((sy[0] && (!sy[1] || sy[0] < sy[1])) || (!(sy[0] || sy[1]) && da[i].num[0] < da[i].num[1])) ? 0 : 1; /* djb-rwth: addressing LLVM warning */
          #  #  #  #  #  
                #  #  # ]
    6136                 :           0 :                         z = !x;
    6137   [ #  #  #  #  :           0 :                         x = ((sy[x] && (!sy[2] || sy[x] < sy[2])) || (!(sy[x] || sy[2]) && da[i].num[x] < da[i].num[2])) ? x : 2; /* min */ /* djb-rwth: addressing LLVM warning */
          #  #  #  #  #  
                #  #  # ]
    6138                 :             :                         /*z = (da[i].num[0] < da[i].num[1])? 1 : 0;*/
    6139                 :             :                         /*z = (da[i].num[x] < da[i].num[2])? 2 : z;*/ /* max */
    6140   [ #  #  #  #  :           0 :                         z = ((sy[x] && (!sy[2] || sy[x] < sy[2])) || (!(sy[x] || sy[2]) && da[i].num[x] < da[i].num[2])) ? 2 : z; /* max */ /* djb-rwth: addressing LLVM warning */
          #  #  #  #  #  
                #  #  # ]
    6141                 :           0 :                         y = ((x + 1) ^ (z + 1)) - 1;                      /* median */
    6142                 :             : 
    6143   [ #  #  #  # ]:           0 :                         if (da[i].num[x] == da[i].num[z] && sy[x] == sy[z])
    6144                 :             :                         {
    6145                 :             :                             /* no cuts */
    6146                 :           0 :                             da[i].typ[0] = 0;
    6147                 :           0 :                             da[i].typ[1] = 0;
    6148                 :           0 :                             da[i].typ[2] = 0;
    6149                 :           0 :                             continue; /* all deriv. agents have same size, ignore */
    6150                 :             :                             /* ??? check for standard derivatizations ??? */
    6151                 :             :                         }
    6152                 :             :                         else
    6153                 :             :                         {
    6154   [ #  #  #  #  :           0 :                             if ((da[i].num[x] == da[i].num[y] && sy[x] == sy[y]) || (sy[x] && sy[y] && !sy[z])) /* djb-rwth: addressing LLVM warning */
          #  #  #  #  #  
                      # ]
    6155                 :             :                             {
    6156                 :             :                                 /* two smallest */
    6157   [ #  #  #  # ]:           0 :                                 switch (z)
    6158                 :             :                                 {
    6159                 :           0 :                                 case 0:
    6160                 :           0 :                                     da[i].num[0] = da[i].num[1];
    6161                 :           0 :                                     da[i].ord[0] = da[i].ord[1];
    6162                 :           0 :                                     da[i].typ[0] = da[i].typ[1];
    6163                 :             : 
    6164                 :           0 :                                     da[i].num[1] = da[i].num[2];
    6165                 :           0 :                                     da[i].ord[1] = da[i].ord[2];
    6166                 :           0 :                                     da[i].typ[1] = da[i].typ[2];
    6167                 :           0 :                                     break;
    6168                 :           0 :                                 case 1:
    6169                 :           0 :                                     da[i].num[1] = da[i].num[2];
    6170                 :           0 :                                     da[i].ord[1] = da[i].ord[2];
    6171                 :           0 :                                     da[i].typ[1] = da[i].typ[2];
    6172                 :           0 :                                     break;
    6173                 :           0 :                                 case 2:
    6174                 :           0 :                                     break;
    6175                 :             :                                 }
    6176                 :           0 :                                 da[i].typ[2] = 0;
    6177                 :           0 :                                 num_cuts += 2;
    6178                 :           0 :                                 num_cut_pieces += 2;
    6179                 :             :                             }
    6180                 :             :                             else
    6181                 :             :                             {
    6182                 :             :                                 /* one smallest */
    6183         [ #  # ]:           0 :                                 if (x)
    6184                 :             :                                 {
    6185                 :           0 :                                     da[i].num[0] = da[i].num[x];
    6186                 :           0 :                                     da[i].ord[0] = da[i].ord[x];
    6187                 :           0 :                                     da[i].typ[0] = da[i].typ[x];
    6188                 :             :                                 }
    6189                 :           0 :                                 da[i].typ[1] = 0;
    6190                 :           0 :                                 da[i].typ[2] = 0;
    6191                 :           0 :                                 num_cuts += 1;
    6192                 :           0 :                                 num_cut_pieces += 1;
    6193                 :             :                             }
    6194                 :             :                         }
    6195                 :           0 :                         continue;
    6196                 :             :                     }
    6197                 :           0 :                     ret = -88;
    6198                 :           0 :                     goto exit_function; /* unexpected */
    6199                 :             : 
    6200                 :           0 :                 case 4:
    6201   [ #  #  #  # ]:           0 :                     if ((da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR) && (da[i].typ[1] & DERIV_RING_OUTSIDE_PRECURSOR) &&
    6202   [ #  #  #  # ]:           0 :                         (da[i].typ[2] & DERIV_RING_OUTSIDE_PRECURSOR) && (da[i].typ[3] & DERIV_RING_OUTSIDE_PRECURSOR))
    6203                 :           0 :                     {
    6204                 :           0 :                         int n01 = inchi_max(da[i].num[0], da[i].num[1]);
    6205                 :           0 :                         int n23 = inchi_max(da[i].num[2], da[i].num[3]);
    6206   [ #  #  #  # ]:           0 :                         if (n01 < n23 && 0 < is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR(at, i, num_atoms, da + i, 0, NULL, 0, NULL, 0, NULL))
    6207                 :             :                         {
    6208                 :           0 :                             da[i].typ[2] = 0;
    6209                 :           0 :                             da[i].typ[3] = 0;
    6210                 :           0 :                             num_cuts += 2;
    6211                 :           0 :                             num_ring_cuts += 2;
    6212                 :           0 :                             num_cut_pieces += 1;
    6213                 :             :                         }
    6214                 :             :                         else
    6215                 :             :                         {
    6216   [ #  #  #  # ]:           0 :                             if (n01 > n23 && 0 < is_DERIV_RING_O_or_NH_OUTSIDE_PRECURSOR(at, i, num_atoms, da + i, 2, NULL, 0, NULL, 0, NULL))
    6217                 :             :                             {
    6218                 :           0 :                                 da[i].num[0] = da[i].num[2];
    6219                 :           0 :                                 da[i].ord[0] = da[i].ord[2];
    6220                 :           0 :                                 da[i].typ[0] = da[i].typ[2];
    6221                 :             : 
    6222                 :           0 :                                 da[i].num[1] = da[i].num[3];
    6223                 :           0 :                                 da[i].ord[1] = da[i].ord[3];
    6224                 :           0 :                                 da[i].typ[1] = da[i].typ[3];
    6225                 :             : 
    6226                 :           0 :                                 da[i].typ[2] = 0;
    6227                 :           0 :                                 da[i].typ[3] = 0;
    6228                 :           0 :                                 num_cuts += 2;
    6229                 :           0 :                                 num_ring_cuts += 2;
    6230                 :           0 :                                 num_cut_pieces += 1;
    6231                 :             :                             }
    6232                 :             :                             else
    6233                 :             :                             {
    6234                 :           0 :                                 da[i].typ[0] = 0;
    6235                 :           0 :                                 da[i].typ[1] = 0;
    6236                 :           0 :                                 da[i].typ[2] = 0;
    6237                 :           0 :                                 da[i].typ[3] = 0;
    6238                 :             :                             }
    6239                 :             :                         }
    6240                 :           0 :                         continue;
    6241                 :             :                     }
    6242                 :           0 :                     ret = -88;
    6243                 :           0 :                     goto exit_function; /* unexpected */
    6244                 :             :             }
    6245                 :             :         }
    6246                 :             : 
    6247                 :             :             /*
    6248                 :             :             Eliminate cases when
    6249                 :             :             da[i1].typ[j1] && da[i2].typ[j2] &&
    6250                 :             :             at[i1].neighbor[da[i1].ord[j1]] == i2 && at[i2].neighbor[da[i2].ord[j2]] == i1
    6251                 :             :             that is, same bond is in the da[] elements of the adjacent atoms
    6252                 :             :             */
    6253                 :             : 
    6254                 :           0 :             nFound = 0; /* number of cuts to remove */
    6255         [ #  # ]:           0 :             for (i = 0; i < num_atoms; i++)
    6256                 :             :             {
    6257                 :             :                 /*for ( j = 0; j < MAX_AT_DERIV && da[i].typ[j]; j ++ ) -- bug fixed 2013-11-07 DCh */
    6258   [ #  #  #  # ]:           0 :                 for (j = 0; j < DERIV_AT_LEN && da[i].typ[j]; j++)
    6259                 :             :                 {
    6260         [ #  # ]:           0 :                     if (da[i].typ[j] & DERIV_DUPLIC)
    6261                 :             :                     {
    6262                 :           0 :                         continue;
    6263                 :             :                     }
    6264                 :           0 :                     n = at[i].neighbor[(int)da[i].ord[j]];
    6265         [ #  # ]:           0 :                     if (n < i)
    6266                 :             :                     {
    6267                 :           0 :                         continue;
    6268                 :             :                     }
    6269                 :             :                     /*for ( k = 0; k < MAX_AT_DERIV && da[n].typ[k]; k ++ ) -- bug fixed 2013-11-07 DCh */
    6270   [ #  #  #  #  :           0 :                     for (k = 0; k < DERIV_AT_LEN && n< num_atoms && da[n].typ[k]; k++)
                   #  # ]
    6271                 :             :                     {
    6272         [ #  # ]:           0 :                         if (da[n].typ[k] & DERIV_DUPLIC)
    6273                 :             :                         {
    6274                 :           0 :                             continue;
    6275                 :             :                         }
    6276                 :           0 :                         m = at[n].neighbor[(int)da[n].ord[k]];
    6277         [ #  # ]:           0 :                         if (m == i)
    6278                 :             :                         {
    6279                 :             :                             /* same bond in da[i].typ[j] and da[n].typ[k] */
    6280                 :             :                             /* check whether both derivatives are acceptable */
    6281                 :           0 :                             int k1 = k, j1 = j;
    6282                 :           0 :                             int ret_i = is_deriv_chain_or_ring(at, i, num_atoms, da + i, &j1);
    6283                 :           0 :                             int ret_n = is_deriv_chain_or_ring(at, n, num_atoms, da + n, &k1);
    6284         [ #  # ]:           0 :                             if (ret_i < 0)
    6285                 :             :                             {
    6286                 :           0 :                                 ret = ret_i;
    6287                 :           0 :                                 goto exit_function;
    6288                 :             :                             }
    6289         [ #  # ]:           0 :                             if (ret_n < 0)
    6290                 :             :                             {
    6291                 :           0 :                                 ret = ret_n;
    6292                 :           0 :                                 goto exit_function;
    6293                 :             :                             }
    6294   [ #  #  #  #  :           0 :                             if (!ret_i || (ret_i && ret_n)) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    6295                 :             :                             {
    6296         [ #  # ]:           0 :                                 if (da[i].typ[j1] & DERIV_RING_OUTSIDE_PRECURSOR)
    6297                 :             :                                 {
    6298                 :           0 :                                     num_cuts -= 2;
    6299                 :           0 :                                     num_ring_cuts -= 2;
    6300                 :             :                                 }
    6301                 :             :                                 else
    6302                 :             :                                 {
    6303                 :           0 :                                     num_cuts -= 1;
    6304                 :             :                                 }
    6305                 :           0 :                                 num_cut_pieces -= 1;
    6306         [ #  # ]:           0 :                                 if ((ret = remove_deriv_mark(da + i, j1))) /* djb-rwth: addressing LLVM warning */
    6307                 :             :                                 {
    6308                 :           0 :                                     goto exit_function;
    6309                 :             :                                 }
    6310                 :           0 :                                 nFound++;
    6311                 :             :                             }
    6312   [ #  #  #  #  :           0 :                             if (!ret_n || (ret_i && ret_n)) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    6313                 :             :                             {
    6314         [ #  # ]:           0 :                                 if (da[n].typ[k1] & DERIV_RING_OUTSIDE_PRECURSOR)
    6315                 :             :                                 {
    6316                 :           0 :                                     num_cuts -= 2;
    6317                 :           0 :                                     num_ring_cuts -= 2;
    6318                 :             :                                 }
    6319                 :             :                                 else
    6320                 :             :                                 {
    6321                 :           0 :                                     num_cuts -= 1;
    6322                 :             :                                 }
    6323                 :           0 :                                 num_cut_pieces -= 1;
    6324         [ #  # ]:           0 :                                 if ((ret = remove_deriv_mark(da + n, k1))) /* djb-rwth: addressing LLVM warning */
    6325                 :             :                                 {
    6326                 :           0 :                                     goto exit_function;
    6327                 :             :                                 }
    6328                 :           0 :                                 nFound++;
    6329                 :             :                             }
    6330                 :             :                         }
    6331                 :             :                     }
    6332                 :             :                 }
    6333                 :             :             }
    6334                 :             : 
    6335         [ #  # ]:           0 :             if (nFound)
    6336                 :             :             {
    6337         [ #  # ]:           0 :                 for (i = 0; i < num_atoms; i++)
    6338                 :             :                 {
    6339                 :             :                     /*for ( j = 0; j < MAX_AT_DERIV && da[i].typ[j]; j ++ ) -- bug fixed 2013-11-07 DCh */
    6340   [ #  #  #  # ]:           0 :                     for (j = 0; j < DERIV_AT_LEN && da[i].typ[j]; j++)
    6341                 :             :                     {
    6342                 :             :                         /* attn: j is changed inside the cycle body */
    6343         [ #  # ]:           0 :                         if (da[i].typ[j] & DERIV_DUPLIC)
    6344                 :             :                         {
    6345         [ #  # ]:           0 :                             if ((ret = remove_deriv(da + i, j))) /* djb-rwth: addressing LLVM warning */
    6346                 :             :                             {
    6347                 :           0 :                                 goto exit_function;
    6348                 :             :                             }
    6349                 :           0 :                             j--;
    6350                 :             :                         }
    6351                 :             :                     }
    6352                 :             :                 }
    6353                 :             :             }
    6354                 :             :         }
    6355                 :             : 
    6356                 :             :         /* make sure DERIV_RING_OUTSIDE_PRECURSOR type disconnections increase */
    6357                 :             :         /* number of components by the number of disconnected derivateves */
    6358                 :             :         /* Avoid cases like these:
    6359                 :             : 
    6360                 :             :         O--R--O             DO--R--OD
    6361                 :             :         /       \
    6362                 :             :         R--X         Y--R => R--XT2     T2Y--R
    6363                 :             :         \       /
    6364                 :             :         O--R--O             DO--R--OD
    6365                 :             : 
    6366                 :             : 
    6367                 :             : 
    6368                 :             :         O--O                 DO--OD
    6369                 :             :         /    \
    6370                 :             :         R--X--O---Y--R    =>  R--X  OD2 Y--R
    6371                 :             :         T2     T2
    6372                 :             : 
    6373                 :             :         */
    6374                 :             :         /* count DERIV_RING_OUTSIDE_PRECURSOR-type attachments */
    6375                 :             : 
    6376                 :             : #if ( COUNT_ALL_NOT_DERIV == 1 )
    6377                 :           0 :         num_cuts_to_check = num_cuts; /* STOP HERE */
    6378                 :             : #else
    6379                 :             :         num_cuts_to_check = num_ring_cuts;
    6380                 :             : #endif
    6381   [ #  #  #  #  :           0 :         ALLOC_AP
          #  #  #  #  #  
                #  #  # ]
    6382                 :             : 
    6383         [ #  # ]:           0 :             if (num_cuts_to_check >= 2)
    6384                 :             :             {
    6385                 :             :                 /* check */
    6386                 :             :                 AT_NUMB    comp_num;
    6387                 :             :                 int        /*n,*/ m_at, m_ord;
    6388                 :             :                 /*AT_NUMB at1, at2;*/
    6389                 :             : 
    6390                 :           0 :             repeat_without_deriv_ring:
    6391                 :             : 
    6392   [ #  #  #  #  :           0 :                 ALLOC_AP
          #  #  #  #  #  
                #  #  # ]
    6393                 :             : 
    6394                 :           0 :                     comp_num = 0;
    6395                 :             :                 /* fill out the array of bonds to be cut */
    6396                 :           0 :                 j = fill_out_bond_cuts( at, da, num_atoms, ap, num_cuts_to_check );
    6397         [ #  # ]:           0 :                 if (j < 0)
    6398                 :             :                 {
    6399                 :           0 :                     ret = j;
    6400                 :           0 :                     goto exit_r2c_num; /* wrong number of cuts = num */
    6401                 :             :                 }
    6402         [ #  # ]:           0 :                 if (j != num_cuts_to_check)
    6403                 :             :                 {
    6404                 :           0 :                     ret = -3;
    6405                 :           0 :                     goto exit_r2c_num; /* wrong number of cuts = num */
    6406                 :             :                 }
    6407                 :             :                 /* can't sort the bonds for subsequent searching by bisections in
    6408                 :             :                 mark_atoms_ap() -> has_atom_pair() 2013-08-48 DCh */
    6409                 :             :                 /* !!!!!!!! check that there are no derivatives inside a derivative */
    6410                 :           0 :                 comp_num = 0; /* here it is the number of removed cuts */
    6411         [ #  # ]:           0 :                 for (i = 0; i < num_cuts_to_check; i += j)
    6412                 :             :                 {
    6413         [ #  # ]:           0 :                     for (j = n = 0; j < 2; j++)
    6414                 :             :                     {
    6415                 :           0 :                         int atj = (int) ap[i].at[j];
    6416   [ #  #  #  # ]:           0 :                         if (da[atj].typ[0] && at[atj].neighbor[(int) da[atj].ord[0]] == ap[i].at[1 - j])
    6417                 :             :                         {
    6418                 :           0 :                             k = j;      /* ap[i].at[k] is precursor atom */
    6419                 :           0 :                             n++;
    6420                 :           0 :                             m_at = atj; /* precursor atom at[m_at], da[m_at] */
    6421                 :           0 :                             m_ord = 0;  /* da[m_at].typ[m_ord] - type of the deriv.bond to break  */
    6422                 :             :                         }
    6423                 :             :                         else
    6424                 :             :                         {
    6425   [ #  #  #  # ]:           0 :                             if (da[atj].typ[1] && at[atj].neighbor[(int) da[atj].ord[1]] == ap[i].at[1 - j])
    6426                 :             :                             {
    6427                 :           0 :                                 k = j;
    6428                 :           0 :                                 n++;
    6429                 :           0 :                                 m_at = atj;
    6430                 :           0 :                                 m_ord = 1;
    6431                 :             :                             }
    6432                 :             :                         }
    6433                 :             :                     }
    6434                 :             : 
    6435         [ #  # ]:           0 :                     if (n != 1)
    6436                 :             :                     {
    6437                 :           0 :                         ret = -3;
    6438                 :           0 :                         goto exit_r2c_num; /* wrong atom pair */
    6439                 :             :                     }
    6440                 :             : 
    6441         [ #  # ]:           0 :                     if (( da[m_at].typ[m_ord] & DERIV_RING_OUTSIDE_PRECURSOR ))
    6442                 :             :                     {
    6443                 :           0 :                         n = (int) ap[i].at[k];   /* atom inside the derivation attachment */
    6444                 :           0 :                         j = 2;             /* number of bonds to cut */
    6445   [ #  #  #  #  :           0 :                         if (i + j > num_cuts_to_check || ((int) ap[i + 1].at[0] != n && (int) ap[i + 1].at[1] != n)) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    6446                 :             :                         {
    6447                 :           0 :                             ret = -3;
    6448                 :           0 :                             goto exit_r2c_num; /* wrong atom pair */
    6449                 :             :                         }
    6450                 :             :                     }
    6451                 :             :                     else
    6452                 :             :                     {
    6453                 :           0 :                         n = ap[i].at[1 - k]; /* atom inside the tentative derivation attachment */
    6454                 :           0 :                         j = 1;             /* number of bonds to cut */
    6455                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    6456                 :             :                                            /*j += (0 != (da[m_at].typ[m_ord] & DERIV_RING_DMOX_DEOX)); */
    6457                 :             :                                            /* these 2 cuts are always adjacent */
    6458   [ #  #  #  # ]:           0 :                         j += ( da[m_at].typ[m_ord] && da[m_at].typ[m_ord] == ( da[m_at].typ[m_ord] & DERIV_RING_DMOX_DEOX ) ); /* these 2 cuts are always adjacent */
    6459                 :             : #endif
    6460                 :             : #ifdef DERIV_RING2_OUTSIDE_PRECUR
    6461   [ #  #  #  # ]:           0 :                         j += ( da[m_at].typ[m_ord] && da[m_at].typ[m_ord] == ( da[m_at].typ[m_ord] & DERIV_RING2_OUTSIDE_PRECUR ) ); /* these 2 cuts are always adjacent */
    6462                 :             : #endif
    6463                 :             :                     }
    6464                 :             : 
    6465                 :             :                     /* at[n] belongs to the derivation agent  */
    6466                 :           0 :                     cur_num_at = mark_atoms_ap( at, n, ap + i, j, 0, 1 );
    6467         [ #  # ]:           0 :                     for (k = 0; k < num_cuts_to_check; k++)
    6468                 :             :                     {
    6469   [ #  #  #  # ]:           0 :                         if (k == i || k == i + j - 1) /* not more than 2 cuts per derivatization agent */
    6470                 :             :                         {
    6471                 :           0 :                             continue; /* skip current 1 or 2 cuts */
    6472                 :             :                         }
    6473   [ #  #  #  # ]:           0 :                         if (at[(int) ap[k].at[0]].at_type || at[(int) ap[k].at[1]].at_type)
    6474                 :             :                         {
    6475                 :             :                             /*#ifdef DERIV_X_OXIME*/
    6476         [ #  # ]:           0 :                             if (( da[m_at].typ[m_ord] & DERIV_UNEXPADABLE ) == da[m_at].typ[m_ord])
    6477                 :             :                             {
    6478                 :             :                                 /* this derivatization agent cannot be inside another, larger deriv. agent */
    6479                 :             :                                 /* disable cuts of the larger derivatization agent */
    6480         [ #  # ]:           0 :                                 if (ap[k].atno != m_at)
    6481                 :             :                                 {
    6482   [ #  #  #  # ]:           0 :                                     for (j = 0; j < 2 && da[ap[k].atno].typ[j]; j++)
    6483                 :             :                                     {
    6484                 :           0 :                                         da[ap[k].atno].typ[j] |= DERIV_UNMARK;
    6485                 :             :                                     }
    6486                 :           0 :                                     num_cuts -= 1;
    6487                 :           0 :                                     num_cut_pieces -= 1;
    6488         [ #  # ]:           0 :                                     if (j == 2)
    6489                 :             :                                     {
    6490                 :           0 :                                         num_cuts -= 1;
    6491                 :           0 :                                         num_ring_cuts -= 2;
    6492                 :             :                                     }
    6493                 :           0 :                                     comp_num++;
    6494                 :             :                                 }
    6495                 :             :                                 /* djb-rwth: removing redundant code */
    6496                 :             :                             }
    6497                 :             :                             else
    6498                 :             :                                 /* #endif */
    6499                 :             :                                 /* unmark the cut: found a cut inside the derivatizing agent */
    6500                 :             :                             {
    6501                 :           0 :                                 da[m_at].typ[m_ord] |= DERIV_UNMARK;
    6502                 :           0 :                                 num_cuts -= 1;
    6503                 :           0 :                                 num_cut_pieces -= 1;
    6504         [ #  # ]:           0 :                                 if (j == 2)
    6505                 :             :                                 {
    6506                 :           0 :                                     da[m_at].typ[1 - m_ord] |= DERIV_UNMARK;
    6507                 :           0 :                                     num_cuts -= 1;
    6508                 :           0 :                                     num_ring_cuts -= 2;
    6509                 :             :                                 }
    6510                 :           0 :                                 comp_num++;
    6511                 :             :                             }
    6512                 :           0 :                             break;
    6513                 :             :                         }
    6514                 :             :                     }
    6515                 :           0 :                     UnMarkOtherIndicators( at, num_atoms );
    6516                 :             :                 }
    6517                 :             : 
    6518         [ #  # ]:           0 :                 if (comp_num)
    6519                 :             :                 {
    6520         [ #  # ]:           0 :                     for (i = 0; i < num_atoms; i++)
    6521                 :             :                     {
    6522         [ #  # ]:           0 :                         if (da[i].typ[0] & DERIV_UNMARK)
    6523                 :             :                         {
    6524                 :           0 :                             da[i].num[0] = da[i].num[1];
    6525                 :           0 :                             da[i].ord[0] = da[i].ord[1];
    6526                 :           0 :                             da[i].typ[0] = da[i].typ[1];
    6527                 :           0 :                             da[i].typ[1] = 0;
    6528                 :           0 :                             j = 0;
    6529                 :             :                         }
    6530                 :             :                         else
    6531                 :             :                         {
    6532                 :           0 :                             j = 1;
    6533                 :             :                         }
    6534         [ #  # ]:           0 :                         if (da[i].typ[j] & DERIV_UNMARK)
    6535                 :             :                         {
    6536                 :           0 :                             da[i].typ[j] = 0;
    6537                 :             :                         }
    6538                 :             :                     }
    6539                 :             : 
    6540                 :             : #if ( COUNT_ALL_NOT_DERIV == 1 )
    6541                 :           0 :                     num_cuts_to_check = num_cuts;
    6542                 :             : #else
    6543                 :             :                     num_cuts_to_check = num_ring_cuts;
    6544                 :             : #endif
    6545   [ #  #  #  #  :           0 :                     if (num_cuts < 0 || num_ring_cuts < 0 || num_cut_pieces < 0)
                   #  # ]
    6546                 :             :                     {
    6547                 :           0 :                         ret = -3;
    6548                 :           0 :                         goto exit_r2c_num; /* wrong number of cuts = num */
    6549                 :             :                     }
    6550                 :             : #if( defined(DERIV_X_OXIME) || defined(DERIV_RO_COX) )
    6551         [ #  # ]:           0 :                     if (num_cuts_to_check > 0)
    6552                 :             : #endif
    6553                 :             :                     {
    6554                 :           0 :                         goto repeat_without_deriv_ring;
    6555                 :             :                     }
    6556                 :             :                 }
    6557                 :             : 
    6558                 :             :                 /* Sort the bonds for subsequent searching by bisections
    6559                 :             :                 -- disabled because DERIV_RING_DMOX_DEOX have to be adjacent in ap
    6560                 :             :                 if ( num_cuts_to_check > 1 ) {
    6561                 :             :                 qsort( ap, num_cuts_to_check, sizeof(ap[0]), cmp_r2c_atpair);
    6562                 :             :                 }
    6563                 :             :                 */
    6564         [ #  # ]:           0 :                 if ((ret = mark_deriv_agents( at, da, num_atoms, ap, num_cuts_to_check, &comp_num, &cur_num_at ))) /* djb-rwth: addressing LLVM warning */
    6565                 :             :                 {
    6566                 :           0 :                     goto exit_r2c_num; /* wrong atom pair */
    6567                 :             :                 }
    6568         [ #  # ]:           0 :                 if (comp_num > 1)
    6569                 :             :                 {
    6570                 :             :                     /* eliminate offending DERIV_RING_OUTSIDE_PRECURSOR type derivatives */
    6571         [ #  # ]:           0 :                     if (num_ring_cuts <= 2)
    6572                 :             :                     {
    6573                 :           0 :                         ret = -99;
    6574                 :           0 :                         goto exit_r2c_num;
    6575                 :             :                     }
    6576                 :           0 :                     n = 0;
    6577         [ #  # ]:           0 :                     for (i = j = 0; i < num_atoms; i++) /* djb-rwth: ignoring LLVM warning: variable used */
    6578                 :             :                     {
    6579   [ #  #  #  # ]:           0 :                         if (( da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR ) && ( da[i].typ[1] & DERIV_RING_OUTSIDE_PRECURSOR ))
    6580                 :             :                         {
    6581                 :           0 :                             int at1a = at[i].neighbor[(int) da[i].ord[0]];
    6582                 :           0 :                             int at2a = at[i].neighbor[(int) da[i].ord[1]];
    6583         [ #  # ]:           0 :                             if (at[at1a].at_type != at[at2a].at_type)
    6584                 :             :                             {
    6585                 :           0 :                                 da[i].typ[0] = 0; /* eliminate this cut */
    6586                 :           0 :                                 da[i].typ[1] = 0;
    6587                 :           0 :                                 n++;
    6588                 :           0 :                                 num_cuts_to_check -= 2;
    6589                 :           0 :                                 num_cuts -= 2;
    6590                 :           0 :                                 num_ring_cuts -= 2;
    6591                 :           0 :                                 num_cut_pieces -= 1;
    6592                 :             :                             }
    6593                 :             :                         }
    6594                 :             :                     }
    6595   [ #  #  #  # ]:           0 :                     if (n > 0 && num_cuts_to_check > 2)
    6596                 :             :                     {
    6597                 :           0 :                         UnMarkOtherIndicators( at, num_atoms );
    6598                 :           0 :                         goto repeat_without_deriv_ring;
    6599                 :             :                     }
    6600                 :             :                 }
    6601                 :           0 :                 ret = 0;
    6602                 :             : 
    6603                 :           0 :             exit_r2c_num:
    6604                 :             :                 /*inchi_free( ap );*/
    6605                 :           0 :                 UnMarkOtherIndicators( at, num_atoms );
    6606                 :             :                 /*if ( ret < 0 || num_cuts_to_check >= 2 && cur_num_at < MIN_AT_LEFT_DERIV ) */
    6607                 :             :                 /* -- bug: cur_num_at may include later rejected deriv. agents 2013-11-08 DCh */
    6608         [ #  # ]:           0 :                 if (ret < 0)
    6609                 :             :                 {
    6610                 :           0 :                     goto exit_function; /* unexpected  error or nothing left */
    6611                 :             :                 }
    6612                 :             :             }
    6613                 :             : 
    6614         [ #  # ]:           0 :         if (!num_cuts)
    6615                 :             :         {
    6616                 :           0 :             continue; /*goto exit_function;*/
    6617                 :             :         }
    6618                 :             : 
    6619                 :             :         /* Eliminate derivatives that are not in the list */
    6620                 :           0 :         num_cuts = eliminate_deriv_not_in_list( at, da, num_atoms, szUnderivList, UNDERIV_LIST_LEN, szUnderivList2, UNDERIV_LIST_LEN2, &bitUnderivList );
    6621         [ #  # ]:           0 :         if (num_cuts < 0)
    6622                 :             :         {
    6623                 :           0 :             ret = num_cuts;
    6624                 :           0 :             goto exit_function;
    6625                 :             :         }
    6626                 :             : 
    6627                 :             :         /* Check how many atoms was left in the precursor - begin - 2013-11-12 DT */
    6628         [ #  # ]:           0 :         if (( num_cuts_to_check = num_cuts ) >= 1)
    6629                 :             :         {
    6630                 :           0 :             AT_NUMB comp_num = 0; /* here it is the number of removed cuts */
    6631                 :             :             int        /*n,*/ m_at, m_ord;
    6632                 :             : 
    6633   [ #  #  #  #  :           0 :             ALLOC_AP
          #  #  #  #  #  
                #  #  # ]
    6634                 :             : 
    6635                 :             :                 /* Fill out the array of bonds to be cut */
    6636                 :           0 :                 j = fill_out_bond_cuts( at, da, num_atoms, ap, num_cuts_to_check );
    6637         [ #  # ]:           0 :             if (j < 0)
    6638                 :             :             {
    6639                 :           0 :                 ret = j;
    6640                 :           0 :                 goto exit_r2c_num2; /* wrong number of cuts = num */
    6641                 :             :             }
    6642         [ #  # ]:           0 :             if (j != num_cuts_to_check)
    6643                 :             :             {
    6644                 :           0 :                 ret = -3;
    6645                 :           0 :                 goto exit_r2c_num2; /* wrong number of cuts = num */
    6646                 :             :             }
    6647                 :             : 
    6648                 :             :             /* The following code was copied from above to mark removed */
    6649         [ #  # ]:           0 :             for (i = 0; i < num_cuts_to_check; i += j)
    6650                 :             :             {
    6651         [ #  # ]:           0 :                 for (j = n = 0; j < 2; j++)
    6652                 :             :                 {
    6653                 :           0 :                     int atj = (int) ap[i].at[j];
    6654   [ #  #  #  #  :           0 :                     if (atj < num_atoms && da[atj].typ[0] && at[atj].neighbor[(int) da[atj].ord[0]] == ap[i].at[1 - j])
                   #  # ]
    6655                 :             :                     {
    6656                 :           0 :                         k = j;      /* ap[i].at[k] is precursor atom */
    6657                 :           0 :                         n++;
    6658                 :           0 :                         m_at = atj; /* precursor atom at[m_at], da[m_at] */
    6659                 :           0 :                         m_ord = 0;  /* da[m_at].typ[m_ord] - type of the deriv.bond to break  */
    6660                 :             :                     }
    6661                 :             :                     else
    6662                 :             :                     {
    6663   [ #  #  #  #  :           0 :                         if (atj < num_atoms && da[atj].typ[1] && at[atj].neighbor[(int) da[atj].ord[1]] == ap[i].at[1 - j])
                   #  # ]
    6664                 :             :                         {
    6665                 :           0 :                             k = j;
    6666                 :           0 :                             n++;
    6667                 :           0 :                             m_at = atj;
    6668                 :           0 :                             m_ord = 1;
    6669                 :             :                         }
    6670                 :             :                     }
    6671                 :             :                 }
    6672         [ #  # ]:           0 :                 if (n != 1)
    6673                 :             :                 {
    6674                 :           0 :                     ret = -3;
    6675                 :           0 :                     goto exit_r2c_num2; /* wrong atom pair */
    6676                 :             :                 }
    6677         [ #  # ]:           0 :                 if (( da[m_at].typ[m_ord] & DERIV_RING_OUTSIDE_PRECURSOR ))
    6678                 :             :                 {
    6679                 :           0 :                     n = (int) ap[i].at[k];   /* atom inside the derivation attachment */
    6680                 :           0 :                     j = 2;             /* number of bonds to cut */
    6681   [ #  #  #  #  :           0 :                     if (i + j > num_cuts_to_check || ((int) ap[i + 1].at[0] != n && (int) ap[i + 1].at[1] != n)) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    6682                 :             :                     {
    6683                 :           0 :                         ret = -3;
    6684                 :           0 :                         goto exit_r2c_num2; /* wrong atom pair */
    6685                 :             :                     }
    6686                 :             :                 }
    6687                 :             :                 else
    6688                 :             :                 {
    6689                 :           0 :                     n = ap[i].at[1 - k];    /* atom inside the tentative derivation attachment */
    6690                 :           0 :                     j = 1;                  /* number of bonds to cut */
    6691                 :             : #if( defined(DERIV_RING_DMOX_DEOX_N) && defined(DERIV_RING_DMOX_DEOX_O) )
    6692                 :             :                                             /*j += (0 != (da[m_at].typ[m_ord] & DERIV_RING_DMOX_DEOX));*/
    6693                 :             :                                             /* these 2 cuts are always adjacent */
    6694   [ #  #  #  # ]:           0 :                     j += ( da[m_at].typ[m_ord] && da[m_at].typ[m_ord] == ( da[m_at].typ[m_ord] & DERIV_RING_DMOX_DEOX ) ); /* these 2 cuts are always adjacent */
    6695                 :             : #endif
    6696                 :             : #ifdef DERIV_RING2_OUTSIDE_PRECUR
    6697   [ #  #  #  # ]:           0 :                     j += ( da[m_at].typ[m_ord] && da[m_at].typ[m_ord] == ( da[m_at].typ[m_ord] & DERIV_RING2_OUTSIDE_PRECUR ) ); /* these 2 cuts are always adjacent */
    6698                 :             : #endif
    6699                 :             :                 }
    6700                 :             : 
    6701                 :             :                 /* at[n] belongs to the derivation agent  */
    6702                 :           0 :                 cur_num_at = mark_atoms_ap( at, n, ap + i, j, 0, 1 );
    6703                 :           0 :                 UnMarkOtherIndicators( at, num_atoms );
    6704                 :             :             }
    6705                 :           0 :             ret = mark_deriv_agents( at, da, num_atoms, ap, num_cuts_to_check, &comp_num, &cur_num_at );
    6706                 :           0 :             UnMarkOtherIndicators( at, num_atoms );
    6707         [ #  # ]:           0 :             if (ap)
    6708                 :             :             {
    6709         [ #  # ]:           0 :                 inchi_free( ap );
    6710                 :           0 :                 ap = NULL;
    6711                 :             :             }
    6712         [ #  # ]:           0 :             if (ret)
    6713                 :             :             {
    6714                 :           0 :                 goto exit_r2c_num2; /* wrong atom pair */
    6715                 :             :             }
    6716                 :             :         }
    6717                 :             : 
    6718                 :           0 :     exit_r2c_num2:
    6719                 :             : 
    6720         [ #  # ]:           0 :         if (ap)
    6721                 :             :         {
    6722         [ #  # ]:           0 :             inchi_free( ap );
    6723                 :           0 :             ap = NULL;
    6724                 :             :         }
    6725   [ #  #  #  #  :           0 :         if (ret < 0 || (num_cuts_to_check >= 2 && cur_num_at < MIN_AT_LEFT_DERIV)) /* -- bug: cur_num_at may include later rejected deriv. agents 2013-11-08 DCh */ /* djb-rwth: addressing LLVM warning */
                   #  # ]
    6726                 :             :         {
    6727                 :           0 :             goto exit_function; /* unexpected  error or nothing left */
    6728                 :             :         }
    6729                 :             : 
    6730                 :             :         /* Check how many atoms was left in the precursor - end - 2013-11-12 DT */
    6731                 :             : 
    6732                 :             :         /* make cuts */
    6733                 :           0 :         num_cuts = 0;
    6734         [ #  # ]:           0 :         for (i = num = 0; i < num_atoms; i++) /* djb-rwth: ignoring LLVM warning: variable used */
    6735                 :             :         {
    6736                 :             :             /*for ( len = 0; len < MAX_AT_DERIV && da[i].typ[len]; len ++ ) -- bug fixed 2013-11-07 DCh */
    6737   [ #  #  #  # ]:           0 :             for (len = 0; len < DERIV_AT_LEN && da[i].typ[len]; len++)
    6738                 :             :             {
    6739                 :             :                 ;
    6740                 :             :             }
    6741   [ #  #  #  #  :           0 :             switch (len)
                   #  # ]
    6742                 :             :             {
    6743                 :           0 :                 case 0:
    6744                 :           0 :                     continue;
    6745                 :           0 :                 case 1:
    6746                 :             :                     /* single cut: unconditional */
    6747                 :           0 :                     make_single_cut( at, da, i, 0 );
    6748                 :           0 :                     num_cuts += 1;
    6749                 :           0 :                     continue;
    6750                 :           0 :                 case 2:
    6751   [ #  #  #  # ]:           0 :                     if (( (da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR ) && ( da[i].typ[1] & DERIV_RING_OUTSIDE_PRECURSOR )) ||
    6752   [ #  #  #  # ]:           0 :                          (da[i].typ[0] == DERIV_AMINE_tN && da[i].typ[1] == DERIV_AMINE_tN)
    6753                 :             : #ifdef DERIV_RING2_OUTSIDE_PRECUR
    6754   [ #  #  #  # ]:           0 :                          || (da[i].typ[0] && da[i].typ[0] == ( da[i].typ[0] & DERIV_RING2_OUTSIDE_PRECUR ) &&
    6755         [ #  # ]:           0 :                          da[i].typ[1] == da[i].typ[0])
    6756                 :             : #endif
    6757                 :             :                          ) /* djb-rwth: addressing LLVM warning */
    6758                 :             :                     {
    6759                 :             :                         /* double cut, unconditional */
    6760                 :           0 :                         make_single_cut( at, da, i, 1 );
    6761                 :           0 :                         make_single_cut( at, da, i, 0 );
    6762                 :           0 :                         num_cuts += 1;
    6763                 :           0 :                         continue;
    6764                 :             :                     }
    6765         [ #  # ]:           0 :                     if (da[i].typ[0] == da[i].typ[1])
    6766                 :             :                     {
    6767                 :             :                         /* DERIV_BRIDGE_O or DERIV_BRIDGE_NH; cut off the smallest */
    6768         [ #  # ]:           0 :                         if (da[i].num[0] > da[i].num[1])
    6769                 :             :                         {
    6770                 :           0 :                             make_single_cut( at, da, i, 1 );
    6771                 :           0 :                             num_cuts += 1;
    6772                 :             :                         }
    6773                 :             :                         else
    6774                 :             :                         {
    6775         [ #  # ]:           0 :                             if (da[i].num[0] < da[i].num[1])
    6776                 :             :                             {
    6777                 :           0 :                                 make_single_cut( at, da, i, 0 );
    6778                 :           0 :                                 num_cuts += 1;
    6779                 :             :                             }
    6780                 :             :                         }
    6781                 :           0 :                         continue;
    6782                 :             :                     }
    6783                 :           0 :                     ret = -88;
    6784                 :           0 :                     goto exit_function; /* unexpected */
    6785                 :           0 :                 case 3:
    6786         [ #  # ]:           0 :                     if (da[i].typ[0] == da[i].typ[1] &&
    6787         [ #  # ]:           0 :                          da[i].typ[0] == da[i].typ[2] &&
    6788         [ #  # ]:           0 :                          da[i].typ[0] == DERIV_AMINE_tN)
    6789                 :           0 :                     {
    6790                 :             :                         int x, y, z;
    6791                 :           0 :                         x = ( da[i].num[0] < da[i].num[1] ) ? 0 : 1;
    6792         [ #  # ]:           0 :                         x = ( da[i].num[x] < da[i].num[2] ) ? x : 2; /* min */
    6793                 :           0 :                         z = ( da[i].num[0] < da[i].num[1] ) ? 1 : 0;
    6794         [ #  # ]:           0 :                         z = ( da[i].num[x] < da[i].num[2] ) ? 2 : z; /* max */
    6795                 :           0 :                         y = ( ( x + 1 ) ^ ( z + 1 ) ) - 1;                      /* median */
    6796         [ #  # ]:           0 :                         if (da[i].num[x] == da[i].num[z])
    6797                 :           0 :                             continue; /* all deriv. agents have same size */
    6798                 :             :                                       /* two smallest */
    6799   [ #  #  #  # ]:           0 :                         if (da[i].num[x] == da[i].num[y] && x < y)
    6800                 :             :                         {
    6801                 :           0 :                             int t = x; /* first cut x > y */
    6802                 :           0 :                             x = y;
    6803                 :           0 :                             y = t;
    6804                 :             :                         }
    6805                 :           0 :                         make_single_cut( at, da, i, x );
    6806                 :           0 :                         num_cuts += 1;
    6807         [ #  # ]:           0 :                         if (da[i].num[x] == da[i].num[y])
    6808                 :             :                         {
    6809                 :             :                             /* equally small */
    6810                 :           0 :                             make_single_cut( at, da, i, y );
    6811                 :           0 :                             num_cuts += 1;
    6812                 :             :                         }
    6813                 :           0 :                         continue;
    6814                 :             :                     }
    6815                 :           0 :                     ret = -88;
    6816                 :           0 :                     goto exit_function; /* unexpected */
    6817                 :           0 :                 case 4:
    6818   [ #  #  #  # ]:           0 :                     if (( da[i].typ[0] & DERIV_RING_OUTSIDE_PRECURSOR ) && ( da[i].typ[1] & DERIV_RING_OUTSIDE_PRECURSOR ) &&
    6819   [ #  #  #  # ]:           0 :                         ( da[i].typ[2] & DERIV_RING_OUTSIDE_PRECURSOR ) && ( da[i].typ[3] & DERIV_RING_OUTSIDE_PRECURSOR ))
    6820                 :           0 :                     {
    6821                 :           0 :                         int n01 = inchi_max( da[i].num[0], da[i].num[1] );
    6822                 :           0 :                         int n23 = inchi_max( da[i].num[2], da[i].num[3] );
    6823         [ #  # ]:           0 :                         if (n01 < n23)
    6824                 :             :                         {
    6825                 :           0 :                             make_single_cut( at, da, i, 1 );
    6826                 :           0 :                             make_single_cut( at, da, i, 0 );
    6827                 :           0 :                             num_cuts += 1;
    6828                 :             :                         }
    6829                 :             :                         else
    6830                 :             :                         {
    6831         [ #  # ]:           0 :                             if (n01 > n23)
    6832                 :             :                             {
    6833                 :           0 :                                 make_single_cut( at, da, i, 3 );
    6834                 :           0 :                                 make_single_cut( at, da, i, 2 );
    6835                 :           0 :                                 num_cuts += 1;
    6836                 :             :                             }
    6837                 :             :                         }
    6838                 :           0 :                         continue;
    6839                 :             :                     }
    6840                 :             :             }
    6841                 :             :         }
    6842                 :             : 
    6843                 :           0 :         nTotNumCuts += num_cuts;
    6844                 :             : 
    6845                 :             : #ifdef FIX_UNDERIV_TO_SDF
    6846   [ #  #  #  # ]:           0 :         if (bOutputSdf && at2)
    6847                 :             :         {
    6848                 :             :             /* replace arom bonds with original */
    6849                 :           0 :             replace_arom_bonds( at, num_atoms, at2, num_atoms );
    6850         [ #  # ]:           0 :             if (at2)
    6851                 :             :             {
    6852         [ #  # ]:           0 :                 inchi_free( at2 );
    6853                 :           0 :                 at2 = NULL;
    6854                 :             :             }
    6855                 :             :         }
    6856                 :             : #endif
    6857                 :             : 
    6858                 :             : #ifdef UNDERIV_ADD_EXPLICIT_H
    6859                 :             :         /**********  Add explicit hydrogens ************************/
    6860                 :           0 :         num_atoms = add_explicit_H( inp_cur_data + i_component );
    6861                 :             : #endif
    6862                 :             : 
    6863         [ #  # ]:           0 :         if (num_cuts)
    6864                 :             :         {
    6865                 :           0 :             remove_cut_derivs( num_atoms, at, inp_cur_data, i_component, &ret );
    6866                 :             :         }
    6867                 :             : 
    6868                 :             : 
    6869                 :             :     } /* for (i_component = 0; i_component < num_components; i_component++) */
    6870                 :             : 
    6871                 :             : 
    6872         [ #  # ]:           0 :     if (nTotNumCuts)
    6873                 :             :     {
    6874                 :           0 :         OAD_Edit_MergeComponentsAndRecreateOAD( orig_inp_data, inp_cur_data, num_components, &ret );
    6875                 :             :     }
    6876                 :             : 
    6877                 :           0 : exit_function:
    6878                 :             : 
    6879                 :           0 :     free_underiv_temp_data( ap, da, at2, inp_cur_data, num_components );
    6880                 :             : 
    6881                 :             : #if( UNDERIVATIZE_REPORT == 1 )
    6882   [ #  #  #  #  :           0 :     if (!ret && nTotNumCuts && pSdfValue && bOutputReport)
             #  #  #  # ]
    6883                 :             :     {
    6884                 :           0 :         numUnderiv = sort_merge_underiv( pSdfValue, bOutputSdf, szUnderivList, cDerivSeparator, underivPrefix, underivPostfix ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    6885                 :           0 :         numUnderiv2 = sort_merge_underiv( pSdfValue, bOutputSdf, szUnderivList2, cDerivSeparator, underivPrefix2, underivPostfix2 ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    6886                 :           0 :         sprintf(szbitUnderivList, "0x%.8X", bitUnderivList); /* djb-rwth: addressing GCC warning about 0 flag being ignored */
    6887                 :           0 :         numUnderiv3 = sort_merge_underiv( pSdfValue, bOutputSdf, szbitUnderivList, cDerivSeparator, underivPrefix3, underivPostfix3 ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    6888                 :             :     }
    6889                 :             : #endif
    6890                 :             : 
    6891         [ #  # ]:           0 :     return ret ? ret : nTotNumCuts;
    6892                 :             : }
    6893                 :             : 
    6894                 :             : 
    6895                 :             : #endif /* UNDERIVATIZE */
    6896                 :             : 
    6897                 :             : 
    6898                 :             : /****************************************************************************/
    6899                 :             : #if ( RING2CHAIN == 1 )
    6900                 :             : /*
    6901                 :             : type=1  (incl sugars: W=O, A=C(sat), Z=C(sat), Y=O, B=C(sat)-OH
    6902                 :             : 
    6903                 :             : A---W               A---WH
    6904                 :             : /    |              /
    6905                 :             : |     |        ---> |
    6906                 :             : \    |              \
    6907                 :             : B---Z---YH          B---Z===Y
    6908                 :             : |                   |
    6909                 :             : |                   |
    6910                 :             : C(opt)              C(opt)
    6911                 :             : 
    6912                 :             : type=2 [not implemented]
    6913                 :             : 
    6914                 :             : R---W               R---WH
    6915                 :             : /     \             /
    6916                 :             : |       Z      ---> |        Z
    6917                 :             : \     /             \     //
    6918                 :             : R---YH              R---Y
    6919                 :             : 
    6920                 :             : */
    6921                 :             : #define R2C_EMPTY  127
    6922                 :             : typedef struct tagRing2Chain {  /* atom Z */
    6923                 :             :     char type; /* 1 => sugar-like */
    6924                 :             :     char ordW; /* ordering number of W-neighbor; bond to break; H to add */
    6925                 :             :     char ordY; /* ordering number of YH-neighbor; bond to increment; H to remove */
    6926                 :             :     char ordC; /* atom B = C(sat) */
    6927                 :             :     char ordCopt; /* if exists, saturated C connected by a chain-bond to Z */
    6928                 :             : } R2C_AT;
    6929                 :             : 
    6930                 :             : int detect_r2c_Zatom( inp_ATOM *at, R2C_AT *da, int iZ );
    6931                 :             : int cut_ring_to_chain( inp_ATOM *at, R2C_AT *da, int iZ );
    6932                 :             : 
    6933                 :             : 
    6934                 :             : /****************************************************************************/
    6935                 :           0 : int detect_r2c_Zatom( inp_ATOM *at, R2C_AT *da, int iZ )
    6936                 :             : {
    6937                 :             :     int i, j, neigh, neighneigh, nRingSystem, num_found;
    6938                 :             :     R2C_AT da1;
    6939                 :             : 
    6940         [ #  # ]:           0 :     if (at[iZ].valence > 4)
    6941                 :             :     {
    6942                 :           0 :         return 0;
    6943                 :             :     }
    6944         [ #  # ]:           0 :     if (at[iZ].valence != at[iZ].chem_bonds_valence)
    6945                 :             :     {
    6946                 :           0 :         return 0; /* approach limitation: no double bonds */
    6947                 :             :     }
    6948         [ #  # ]:           0 :     if (at[iZ].el_number != EL_NUMBER_C)
    6949                 :             :     {
    6950                 :           0 :         return 0; /* sugar-specific */
    6951                 :             :     }
    6952         [ #  # ]:           0 :     if (at[iZ].nNumAtInRingSystem < 5)
    6953                 :             :     {
    6954                 :           0 :         return 0; /* not in a suitable ring */
    6955                 :             :     }
    6956         [ #  # ]:           0 :     if (!at[iZ].bCutVertex)
    6957                 :             :     {
    6958                 :           0 :         return 0;  /* recognize only type 1 for now */
    6959                 :             :     }
    6960                 :             : 
    6961                 :           0 :     nRingSystem = at[iZ].nRingSystem;
    6962                 :           0 :     memset( &da1, R2C_EMPTY, sizeof( da1 ) ); /* djb-rwth: memset_s C11/Annex K variant? */
    6963                 :             : 
    6964         [ #  # ]:           0 :     for (i = 0, num_found = 0; i < at[iZ].valence; i++)
    6965                 :             :     {
    6966                 :           0 :         neigh = at[iZ].neighbor[i];
    6967   [ #  #  #  # ]:           0 :         if (at[neigh].charge || at[neigh].radical)
    6968                 :             :         {
    6969                 :           0 :             return 0;
    6970                 :             :         }
    6971         [ #  # ]:           0 :         if (at[neigh].el_number == EL_NUMBER_O &&
    6972         [ #  # ]:           0 :              at[neigh].valence == 1 &&
    6973         [ #  # ]:           0 :              at[neigh].chem_bonds_valence == 1 &&
    6974         [ #  # ]:           0 :              at[neigh].num_H == 1)
    6975                 :             :         {
    6976                 :             :             /* found Z-OH, i.e. Z-YH */
    6977         [ #  # ]:           0 :             if (da1.ordY == R2C_EMPTY)
    6978                 :             :             {
    6979                 :           0 :                 da1.ordY = i;
    6980                 :           0 :                 num_found++;
    6981                 :           0 :                 continue;
    6982                 :             :             }
    6983                 :             :             else
    6984                 :             :             {
    6985                 :           0 :                 return 0;
    6986                 :             :             }
    6987                 :             :         }
    6988         [ #  # ]:           0 :         if (at[neigh].el_number == EL_NUMBER_O &&
    6989         [ #  # ]:           0 :              at[neigh].valence == 2 &&
    6990         [ #  # ]:           0 :              at[neigh].chem_bonds_valence == 2 &&
    6991         [ #  # ]:           0 :              at[neigh].num_H == 0 &&
    6992         [ #  # ]:           0 :              at[neigh].nRingSystem == nRingSystem)
    6993                 :             :         {
    6994                 :             :             /* found Z-O-, i.e. Z-W- */
    6995         [ #  # ]:           0 :             if (da1.ordW == R2C_EMPTY)
    6996                 :             :             {
    6997                 :             :                 /* j = index of the oppozite to at[iZ] neighbor of at[neigh] */
    6998                 :           0 :                 j = ( at[neigh].neighbor[0] == iZ );
    6999                 :           0 :                 neighneigh = at[neigh].neighbor[j];
    7000         [ #  # ]:           0 :                 if (at[neighneigh].valence != at[neighneigh].chem_bonds_valence ||
    7001         [ #  # ]:           0 :                      at[neighneigh].el_number != EL_NUMBER_C)
    7002                 :           0 :                     return 0; /* sugar-specific */
    7003                 :           0 :                 da1.ordW = i;
    7004                 :           0 :                 num_found++;
    7005                 :           0 :                 continue;
    7006                 :             :             }
    7007                 :             :             else
    7008                 :             :             {
    7009                 :           0 :                 return 0;
    7010                 :             :             }
    7011                 :             :         }
    7012         [ #  # ]:           0 :         if (at[neigh].el_number == EL_NUMBER_C &&
    7013         [ #  # ]:           0 :              at[neigh].valence > 2 &&
    7014         [ #  # ]:           0 :              at[neigh].chem_bonds_valence == at[neigh].valence &&
    7015         [ #  # ]:           0 :              at[neigh].num_H <= 1 &&
    7016         [ #  # ]:           0 :              at[neigh].nRingSystem == nRingSystem)
    7017                 :             :         {
    7018                 :             :             /* sugar-specfic: carbon in the ring should have -OH neighbor */
    7019                 :             :             int iOH;
    7020         [ #  # ]:           0 :             for (j = 0; j < at[neigh].valence; j++)
    7021                 :             :             {
    7022                 :           0 :                 iOH = at[neigh].neighbor[j];
    7023         [ #  # ]:           0 :                 if (at[iOH].el_number == EL_NUMBER_O &&
    7024         [ #  # ]:           0 :                      at[iOH].valence == 1 &&
    7025         [ #  # ]:           0 :                      at[iOH].chem_bonds_valence == 1 &&
    7026         [ #  # ]:           0 :                      at[iOH].num_H == 1 &&
    7027   [ #  #  #  # ]:           0 :                      !at[iOH].charge && !at[iOH].radical)
    7028                 :             :                 {
    7029         [ #  # ]:           0 :                     if (da1.ordC == R2C_EMPTY)
    7030                 :             :                     {
    7031                 :           0 :                         da1.ordC = i;
    7032                 :           0 :                         num_found++;
    7033                 :           0 :                         break;
    7034                 :             :                     }
    7035                 :             :                     else
    7036                 :             :                     {
    7037                 :           0 :                         return 0;
    7038                 :             :                     }
    7039                 :             :                 }
    7040                 :             :             }
    7041         [ #  # ]:           0 :             if (j < at[neigh].valence)
    7042                 :             :             {
    7043                 :           0 :                 continue;
    7044                 :             :             }
    7045                 :             :         }
    7046         [ #  # ]:           0 :         if (at[neigh].el_number == EL_NUMBER_C &&
    7047         [ #  # ]:           0 :              at[neigh].chem_bonds_valence == at[neigh].valence &&
    7048         [ #  # ]:           0 :              at[neigh].nRingSystem != nRingSystem)
    7049                 :             :         {
    7050                 :             :             /* extra carbon neighbor of Z */
    7051         [ #  # ]:           0 :             if (da1.ordCopt == R2C_EMPTY)
    7052                 :             :             {
    7053                 :           0 :                 da1.ordCopt = i;
    7054                 :           0 :                 continue;
    7055                 :             :             }
    7056                 :             :         }
    7057                 :           0 :         return 0; /* unexpectd neighbor */
    7058                 :             :     }
    7059                 :             : 
    7060         [ #  # ]:           0 :     if (num_found == 3)
    7061                 :             :     {
    7062                 :           0 :         da1.type = 1;
    7063                 :           0 :         da[iZ] = da1;
    7064                 :           0 :         return 1; /* disconnection found */
    7065                 :             :     }
    7066                 :             : 
    7067                 :           0 :     return 0;
    7068                 :             : }
    7069                 :             : 
    7070                 :             : 
    7071                 :             : /****************************************************************************/
    7072                 :           0 : int cut_ring_to_chain( inp_ATOM *at, R2C_AT *da, int iZ )
    7073                 :             : {
    7074                 :           0 :     int ret = -1; /* error flag */
    7075                 :           0 :     int iordW = (int) da[iZ].ordW; /* ord of the bond in iZ */
    7076                 :           0 :     int iordY = (int) da[iZ].ordY; /* ord of the bond in iZ */
    7077                 :           0 :     int iordC = (int) da[iZ].ordC;
    7078                 :             :     int iW, iY, num_iso_H, i, jordZ;
    7079                 :             :     AT_NUMB *p;
    7080                 :             : 
    7081         [ #  # ]:           0 :     if (da[iZ].type != 1)
    7082                 :             :     {
    7083                 :           0 :         return 0;
    7084                 :             :     }
    7085   [ #  #  #  #  :           0 :     if (0 > iordW || iordW >= at[iZ].valence ||
                   #  # ]
    7086   [ #  #  #  # ]:           0 :          0 > iordY || iordY >= at[iZ].valence ||
    7087         [ #  # ]:           0 :          0 > iordC || iordC >= at[iZ].valence /* suger-specific*/)
    7088                 :             :     {
    7089                 :           0 :         return -1; /* program error */
    7090                 :             :     }
    7091                 :             :     /* find other da[] that affect at[iZ] */
    7092                 :           0 :     iW = at[iZ].neighbor[iordW];  /* opposite atom to disconnect and add H */
    7093                 :           0 :     iY = at[iZ].neighbor[iordY];  /* opposite atom to increment the bond and remove H*/
    7094   [ #  #  #  # ]:           0 :     if (!at[iY].num_H || at[iZ].bond_type[iordY] != BOND_TYPE_SINGLE)
    7095                 :             :     {
    7096                 :           0 :         return -2; /* program error */
    7097                 :             :     }
    7098                 :             :     /* increment at[iZ]--at[iY] bond */
    7099                 :           0 :     p = is_in_the_list( at[iY].neighbor, (AT_NUMB) iZ, at[iY].valence );
    7100         [ #  # ]:           0 :     if (!p)
    7101                 :             :     {
    7102                 :           0 :         return -3; /* program error */
    7103                 :             :     }
    7104                 :           0 :     jordZ = p - at[iY].neighbor;
    7105                 :           0 :     at[iZ].bond_type[iordY] ++;
    7106                 :           0 :     at[iZ].chem_bonds_valence++;
    7107                 :           0 :     at[iY].bond_type[jordZ] ++;
    7108                 :           0 :     at[iY].chem_bonds_valence++;
    7109                 :             : 
    7110                 :             :     /* disconnect at[iZ]--at[iW] bond */
    7111                 :           0 :     ret = DisconnectInpAtBond( at, NULL, iZ, iordW );
    7112         [ #  # ]:           0 :     if (ret != 1)
    7113                 :             :     {
    7114                 :           0 :         return -4; /* program error */
    7115                 :             :     }
    7116                 :             :     /* disconnection succeeded */
    7117                 :             :     /* transfer H from at[iY] to at[iW] */
    7118                 :           0 :     num_iso_H = NUM_ISO_H( at, iY );
    7119         [ #  # ]:           0 :     if (at[iY].num_H == num_iso_H)
    7120                 :             :     {
    7121         [ #  # ]:           0 :         for (i = 0; i < NUM_H_ISOTOPES; i++)
    7122                 :             :         {
    7123         [ #  # ]:           0 :             if (at[iY].num_iso_H[i])
    7124                 :             :             {
    7125                 :           0 :                 at[iY].num_iso_H[i] --;
    7126                 :           0 :                 at[iW].num_iso_H[i] ++;
    7127                 :             :             }
    7128                 :             :         }
    7129                 :             :     }
    7130                 :           0 :     at[iY].num_H--;
    7131                 :           0 :     at[iW].num_H++;
    7132                 :             : 
    7133                 :           0 :     return 1;
    7134                 :             : }
    7135                 :             : 
    7136                 :             : 
    7137                 :             : /****************************************************************************/
    7138                 :           0 : int Ring2Chain( struct tagINCHI_CLOCK *ic,
    7139                 :             :                 struct tagCANON_GLOBALS *pCG,
    7140                 :             :                 ORIG_ATOM_DATA *orig_inp_data )
    7141                 :             : {
    7142                 :           0 :     int ret = 0, i, j, n, num_atoms, num_components, num, num_cuts, iZ; /* djb-rwth: removing redundant variables */
    7143                 :           0 :     inp_ATOM *at = orig_inp_data->at;
    7144                 :           0 :     INP_ATOM_DATA *inp_cur_data = NULL;
    7145                 :           0 :     R2C_AT        *da = NULL;
    7146                 :             : 
    7147                 :             : 
    7148                 :             :     /* prepare */
    7149                 :             : 
    7150                 :             :     /*set_R2C_el_numbers( );*/
    7151                 :             : 
    7152                 :           0 :     num_atoms = remove_terminal_HDT( orig_inp_data->num_inp_atoms, at, 1, NULL );
    7153                 :             :     /*^^^^^ always accomodate accomodate FIX_TERM_H_CHRG_BUG - IPl, July 2008*/
    7154                 :           0 :     orig_inp_data->num_inp_atoms = num_atoms;
    7155                 :             : 
    7156                 :             :     /* initialize */
    7157                 :           0 :     UnMarkDisconnectedComponents( orig_inp_data );
    7158                 :           0 :     num_cuts = 0;
    7159                 :             :     /* mark */
    7160                 :           0 :     num_components = MarkDisconnectedComponents( orig_inp_data, 0 );
    7161                 :           0 :     inp_cur_data = (INP_ATOM_DATA *) inchi_calloc( num_components, sizeof( inp_cur_data[0] ) );
    7162                 :           0 :     iZ = -1;
    7163         [ #  # ]:           0 :     for (j = 0; j < num_components; j++)
    7164                 :             :     {
    7165                 :           0 :         CreateInpAtomData( inp_cur_data + j, orig_inp_data->nCurAtLen[j], 0 );
    7166                 :           0 :         inp_cur_data[j].num_at = ExtractConnectedComponent( orig_inp_data->at, orig_inp_data->num_inp_atoms, j + 1, inp_cur_data[j].at );
    7167                 :             :         /*  error processing */
    7168   [ #  #  #  # ]:           0 :         if (inp_cur_data[j].num_at <= 0 || orig_inp_data->nCurAtLen[j] != inp_cur_data[j].num_at)
    7169                 :             :         {
    7170                 :           0 :             ret = -( j + 1 ); /* severe error */
    7171                 :           0 :             goto exit_function;
    7172                 :             :         }
    7173                 :             :         /* initialize */
    7174                 :           0 :         num_atoms = inp_cur_data[j].num_at;
    7175                 :           0 :         at = inp_cur_data[j].at;
    7176                 :           0 :         add_DT_to_num_H( num_atoms, at );
    7177                 :             : 
    7178                 :           0 :         UnMarkRingSystemsInp( at, num_atoms );
    7179                 :           0 :         UnMarkOtherIndicators( at, num_atoms );
    7180                 :           0 :         UnMarkOneComponent( at, num_atoms );
    7181                 :           0 :         MarkRingSystemsInp( at, num_atoms, 0 );
    7182                 :           0 :         ret = mark_arom_bonds( ic, pCG, at, num_atoms );
    7183         [ #  # ]:           0 :         if (ret < 0)
    7184                 :             :         {
    7185                 :           0 :             goto exit_function;
    7186                 :             :         }
    7187                 :           0 :         ret = 0;
    7188         [ #  # ]:           0 :         if (da)
    7189                 :             :         {
    7190         [ #  # ]:           0 :             inchi_free( da );
    7191                 :             :         }
    7192                 :           0 :         da = (R2C_AT *) inchi_calloc( num_atoms, sizeof( da[0] ) );
    7193                 :             : 
    7194                 :             :         /* detect ring-to-chain possibilities */
    7195                 :             :         /* djb-rwth: removing redundant code */
    7196         [ #  # ]:           0 :         for (i = 0, num = 0; i < num_atoms; i++)
    7197                 :             :         {
    7198   [ #  #  #  # ]:           0 :             if (at[i].bCutVertex /* type 1 specific*/ && !da[i].type)
    7199                 :             :             {
    7200                 :           0 :                 num += ( n = detect_r2c_Zatom( at, da, i ) );
    7201         [ #  # ]:           0 :                 if (n == 1)
    7202                 :             :                 {
    7203                 :           0 :                     iZ = i;
    7204                 :             :                 }
    7205                 :           0 :                 UnMarkOtherIndicators( at, num_atoms );
    7206                 :             :             }
    7207                 :             :         }
    7208                 :             : 
    7209         [ #  # ]:           0 :         if (num == 1)
    7210                 :             :         {
    7211                 :             :             /* convert ring to chain: make single cut */
    7212                 :           0 :             ret = cut_ring_to_chain( at, da, iZ );
    7213         [ #  # ]:           0 :             if (ret < 0)
    7214                 :             :             {
    7215                 :           0 :                 goto exit_function;
    7216                 :             :             }
    7217                 :           0 :             num_cuts += ( ret == 1 );
    7218                 :             :         }
    7219                 :             :         else
    7220                 :             :         {
    7221         [ #  # ]:           0 :             if (num)
    7222                 :             :             {
    7223                 :             :                 /* allocate an array of bonds to be cut */
    7224                 :           0 :                 R2C_ATPAIR *ap = (R2C_ATPAIR *) inchi_malloc( sizeof( ap[0] ) * num );
    7225                 :           0 :                 AT_NUMB    comp_num = 0;
    7226         [ #  # ]:           0 :                 if (!ap)
    7227                 :             :                 {
    7228                 :           0 :                     ret = -1; /* malloc failure */
    7229                 :           0 :                     goto exit_function;
    7230                 :             :                 }
    7231                 :             :                 /* fill out the array of bonds to be cut */
    7232         [ #  # ]:           0 :                 for (i = j = 0; i < num_atoms; i++)
    7233                 :             :                 {
    7234         [ #  # ]:           0 :                     if (da[i].type == 1)
    7235                 :             :                     {
    7236                 :           0 :                         AT_NUMB at1 = i;
    7237                 :           0 :                         AT_NUMB at2 = at[i].neighbor[(int) da[i].ordW];
    7238         [ #  # ]:           0 :                         if (j >= num)
    7239                 :             :                         {
    7240                 :           0 :                             ret = -2;
    7241                 :           0 :                             goto exit_r2c_num; /* wrong number of cuts = num */
    7242                 :             :                         }
    7243                 :           0 :                         n = ( at1 > at2 );
    7244                 :           0 :                         ap[j].at[n] = at1;
    7245                 :           0 :                         ap[j].at[1 - n] = at2; /* ap[j].at[0] < ap[j].at[1] */
    7246                 :           0 :                         j++;
    7247                 :             :                     }
    7248                 :             :                 }
    7249         [ #  # ]:           0 :                 if (j != num)
    7250                 :             :                 {
    7251                 :           0 :                     ret = -3;
    7252                 :           0 :                     goto exit_r2c_num; /* wrong number of cuts = num */
    7253                 :             :                 }
    7254                 :             :                 /* sort the bonds for subsequent searching by bisections */
    7255                 :           0 :                 qsort( ap, num, sizeof( ap[0] ), cmp_r2c_atpair );
    7256                 :             :                 /* mark components to be disconnected */
    7257         [ #  # ]:           0 :                 for (i = 0; i < num; i++)
    7258                 :             :                 {
    7259         [ #  # ]:           0 :                     for (j = 0; j < 2; j++)
    7260                 :             :                     {
    7261         [ #  # ]:           0 :                         if (!at[ap[i].at[j]].at_type)
    7262                 :             :                         {
    7263                 :           0 :                             comp_num++;
    7264                 :           0 :                             mark_atoms_ap( at, (int) ap[i].at[j], ap, num, 0, comp_num );
    7265                 :             :                         }
    7266                 :             :                     }
    7267                 :             :                 }
    7268                 :             :                 /* convert ring to chain */
    7269         [ #  # ]:           0 :                 for (i = 0; i < num; i++)
    7270                 :             :                 {
    7271                 :           0 :                     int i1 = ap[i].at[0];
    7272                 :           0 :                     int i2 = ap[i].at[1];
    7273                 :           0 :                     iZ = -1;
    7274         [ #  # ]:           0 :                     if (at[i1].at_type == at[i2].at_type)
    7275                 :             :                     {
    7276                 :             :                         /* by definition, there are no adjacent iZ atoms; one iZ atom per bond */
    7277   [ #  #  #  # ]:           0 :                         if (da[i1].type == 1 && at[i1].neighbor[(int) da[i1].ordW] == i2)
    7278                 :             :                         {
    7279                 :           0 :                             iZ = i1;
    7280                 :             :                         }
    7281                 :             :                         else
    7282   [ #  #  #  # ]:           0 :                             if (da[i2].type == 1 && at[i2].neighbor[(int) da[i2].ordW] == i1)
    7283                 :             :                             {
    7284                 :           0 :                                 iZ = i2;
    7285                 :             :                             }
    7286                 :             :                             else
    7287                 :             :                             {
    7288                 :           0 :                                 ret = -3;
    7289                 :           0 :                                 goto exit_r2c_num;
    7290                 :             :                             }
    7291                 :           0 :                         ret = cut_ring_to_chain( at, da, iZ );
    7292         [ #  # ]:           0 :                         if (ret < 0)
    7293                 :             :                         {
    7294                 :           0 :                             goto exit_r2c_num;
    7295                 :             :                         }
    7296                 :           0 :                         num_cuts += ( ret == 1 );
    7297                 :             :                     }
    7298                 :             :                 }
    7299                 :           0 :                 ret = 0;
    7300                 :           0 :             exit_r2c_num:
    7301         [ #  # ]:           0 :                 inchi_free( ap );
    7302                 :           0 :                 UnMarkOtherIndicators( at, num_atoms );
    7303         [ #  # ]:           0 :                 if (ret < 0)
    7304                 :             :                 {
    7305                 :           0 :                     goto exit_function;
    7306                 :             :                 }
    7307                 :             :             }
    7308                 :             :         }
    7309                 :             :     }
    7310                 :             : 
    7311         [ #  # ]:           0 :     if (num_cuts)
    7312                 :             :     {
    7313                 :           0 :         OAD_Edit_MergeComponentsAndRecreateOAD( orig_inp_data, inp_cur_data, num_components, &ret );
    7314                 :             :     }
    7315                 :             : 
    7316                 :           0 : exit_function:
    7317                 :             : 
    7318         [ #  # ]:           0 :     if (da)
    7319                 :             :     {
    7320         [ #  # ]:           0 :         inchi_free( da );
    7321                 :           0 :         da = NULL;
    7322                 :             :     }
    7323         [ #  # ]:           0 :     for (j = 0; j < num_components; j++)
    7324                 :             :     {
    7325                 :           0 :         FreeInpAtomData( inp_cur_data + j );
    7326                 :             :     }
    7327         [ #  # ]:           0 :     inchi_free( inp_cur_data );
    7328                 :           0 :     inp_cur_data = NULL;
    7329                 :             : 
    7330         [ #  # ]:           0 :     return ret ? ret : num_cuts;
    7331                 :             : }
    7332                 :             : 
    7333                 :             : 
    7334                 :             : #endif /* RING2CHAIN */
    7335                 :             : 
    7336                 :             : 
    7337                 :             : #if ( RING2CHAIN == 1 || UNDERIVATIZE == 1 )
    7338                 :             : /****************************************************************************/
    7339                 :           0 : void OAD_Edit_MergeComponentsAndRecreateOAD( ORIG_ATOM_DATA *orig_OrigAtomData,
    7340                 :             :                                              INP_ATOM_DATA *curr_InpAtomData,
    7341                 :             :                                              int num_components,
    7342                 :             :                                              int *errcode )
    7343                 :             : {
    7344                 :           0 :     int i, num_atoms = 0, cur_num_at = 0;
    7345                 :             :     inp_ATOM *at;
    7346                 :             : 
    7347         [ #  # ]:           0 :     if (num_components <= 0)
    7348                 :             :     {
    7349                 :           0 :         *errcode = -999; /* num atoms mismatch */
    7350                 :           0 :         return;
    7351                 :             :     }
    7352                 :             : 
    7353                 :             :     /* Merge kept components into 'at' */
    7354         [ #  # ]:           0 :     for (i = 0; i < num_components; i++)
    7355                 :             :     {
    7356                 :           0 :         num_atoms += curr_InpAtomData[i].num_at;
    7357                 :             :     }
    7358                 :             : 
    7359                 :           0 :     at = (inp_ATOM *) inchi_calloc( num_atoms, sizeof( at[0] ) );
    7360                 :           0 :     cur_num_at = 0;
    7361                 :             : 
    7362         [ #  # ]:           0 :     for (i = 0; i < num_components; i++)
    7363                 :             :     {
    7364                 :             :         /* Clean and prepare */
    7365                 :           0 :         UnMarkRingSystemsInp( curr_InpAtomData[i].at, curr_InpAtomData[i].num_at );
    7366                 :           0 :         UnMarkOtherIndicators( curr_InpAtomData[i].at, curr_InpAtomData[i].num_at );
    7367                 :           0 :         UnMarkOneComponent( curr_InpAtomData[i].at, curr_InpAtomData[i].num_at );
    7368                 :             : 
    7369                 :           0 :         subtract_DT_from_num_H( curr_InpAtomData[i].num_at, curr_InpAtomData[i].at );
    7370                 :             : 
    7371                 :             :         /* Merge one by one */
    7372                 :           0 :         cur_num_at = add_inp_ATOM( at, num_atoms, cur_num_at, curr_InpAtomData[i].at, curr_InpAtomData[i].num_at );
    7373                 :             :     }
    7374                 :             : 
    7375                 :             :     /* Replace original OrigAtomData structure */
    7376         [ #  # ]:           0 :     if (cur_num_at == num_atoms)
    7377                 :             :     {
    7378         [ #  # ]:           0 :         inchi_free( orig_OrigAtomData->at );
    7379                 :           0 :         orig_OrigAtomData->at = at;
    7380                 :             : 
    7381                 :           0 :         orig_OrigAtomData->num_inp_atoms = cur_num_at;
    7382                 :             : 
    7383                 :             :         /* Destroy original coordinates as we destroyed a part of original input structure */
    7384         [ #  # ]:           0 :         if (orig_OrigAtomData->szCoord)
    7385                 :             :         {
    7386         [ #  # ]:           0 :             inchi_free( orig_OrigAtomData->szCoord );
    7387                 :           0 :             orig_OrigAtomData->szCoord = NULL;
    7388                 :             :         }
    7389                 :             : 
    7390                 :           0 :         UnMarkDisconnectedComponents( orig_OrigAtomData );
    7391                 :             :     }
    7392                 :             :     else
    7393                 :             :     {
    7394                 :             :         /* Create copy error! */
    7395         [ #  # ]:           0 :         if (at)
    7396                 :             :         {
    7397         [ #  # ]:           0 :             inchi_free( at );
    7398                 :           0 :             at = NULL;
    7399                 :             :         }
    7400                 :           0 :         *errcode = -999; /* num atoms mismatch */
    7401                 :             :     }
    7402                 :             : 
    7403                 :           0 :     return;
    7404                 :             : }
    7405                 :             : 
    7406                 :             : 
    7407                 :             : /****************************************************************************/
    7408                 :           0 : void free_underiv_temp_data( R2C_ATPAIR *ap,
    7409                 :             :                              DERIV_AT *da,
    7410                 :             :                              inp_ATOM *at2,
    7411                 :             :                              INP_ATOM_DATA *inp_cur_data,
    7412                 :             :                              int num_components )
    7413                 :             : {
    7414                 :             :     int i_component;
    7415         [ #  # ]:           0 :     if (ap)
    7416                 :             :     {
    7417         [ #  # ]:           0 :         inchi_free( ap );
    7418                 :           0 :         ap = NULL;
    7419                 :             :     }
    7420         [ #  # ]:           0 :     if (da)
    7421                 :             :     {
    7422         [ #  # ]:           0 :         inchi_free( da );
    7423                 :           0 :         da = NULL;
    7424                 :             :     }
    7425                 :             : #ifdef FIX_UNDERIV_TO_SDF
    7426         [ #  # ]:           0 :     if (at2)
    7427                 :             :     {
    7428         [ #  # ]:           0 :         inchi_free( at2 );
    7429                 :           0 :         at2 = NULL;
    7430                 :             :     }
    7431                 :             : #endif
    7432         [ #  # ]:           0 :     for (i_component = 0; i_component < num_components; i_component++)
    7433                 :             :     {
    7434                 :           0 :         FreeInpAtomData( inp_cur_data + i_component );
    7435                 :             :     }
    7436         [ #  # ]:           0 :     inchi_free( inp_cur_data );
    7437                 :           0 :     inp_cur_data = NULL;
    7438                 :           0 : }
    7439                 :             : 
    7440                 :             : 
    7441                 :             : /****************************************************************************/
    7442                 :           0 : void remove_cut_derivs( int num_atoms,
    7443                 :             :                         inp_ATOM *at,
    7444                 :             :                         INP_ATOM_DATA *inp_cur_data,
    7445                 :             :                         int i_component,
    7446                 :             :                         int *errcode )
    7447                 :             : {
    7448                 :             :     /* Remove marked with Tritium disconnected derivative attachments */
    7449                 :             :     ORIG_ATOM_DATA Orig_inp_data1, *orig_inp_data1;
    7450                 :           0 :     INP_ATOM_DATA *inp_cur_data1 = NULL;
    7451                 :           0 :     int i, num_components1, i_component1, cur_num_at = 0; /* djb-rwth: removing redundant variables */
    7452                 :             : 
    7453                 :           0 :     orig_inp_data1 = &Orig_inp_data1;
    7454                 :           0 :     memset( orig_inp_data1, 0, sizeof( orig_inp_data1[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
    7455                 :             : 
    7456                 :           0 :     UnMarkRingSystemsInp( at, num_atoms );
    7457                 :           0 :     UnMarkOtherIndicators( at, num_atoms );
    7458                 :           0 :     UnMarkOneComponent( at, num_atoms );
    7459                 :             : 
    7460         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    7461                 :             :     {
    7462                 :           0 :         orig_inp_data1->num_inp_bonds += at[i].valence;
    7463                 :             :     }
    7464                 :           0 :     orig_inp_data1->num_inp_bonds /= 2;
    7465                 :           0 :     orig_inp_data1->num_inp_atoms = num_atoms;
    7466                 :             : 
    7467                 :           0 :     orig_inp_data1->at = at; /* = from inp_cur_data[i_component].at */
    7468                 :             : 
    7469                 :           0 :     num_components1 = MarkDisconnectedComponents( orig_inp_data1, 0 );
    7470                 :             : 
    7471                 :           0 :     inp_cur_data1 = (INP_ATOM_DATA *) inchi_calloc( num_components1, sizeof( inp_cur_data1[0] ) );
    7472                 :             : 
    7473   [ #  #  #  # ]:           0 :     if (inp_cur_data1 && (num_components1 > 0)) /* djb-rwth: fixing a NULL pointer dereference */
    7474                 :             :     {
    7475                 :             :     /* Extract components and discard disconnected derivatizing agents */
    7476         [ #  # ]:           0 :     for (i_component1 = 0; i_component1 < num_components1; i_component1++)
    7477                 :             :     {
    7478                 :           0 :         CreateInpAtomData( inp_cur_data1 + i_component1, orig_inp_data1->nCurAtLen[i_component1], 0 );
    7479                 :           0 :         inp_cur_data1[i_component1].num_at = ExtractConnectedComponent( orig_inp_data1->at, orig_inp_data1->num_inp_atoms,
    7480                 :           0 :                                                                         i_component1 + 1, inp_cur_data1[i_component1].at );
    7481                 :             :         /*  error processing */
    7482   [ #  #  #  # ]:           0 :         if (inp_cur_data1[i_component1].num_at <= 0 || orig_inp_data1->nCurAtLen[i_component1] != inp_cur_data1[i_component1].num_at)
    7483                 :             :         {
    7484                 :           0 :             *errcode = -( i_component1 + 1 ); /* severe error */
    7485                 :           0 :             break;
    7486                 :             :         }
    7487                 :             :         /* if the component has tritium then discard: it is a derivatizing agent */
    7488         [ #  # ]:           0 :         for (i = 0; i < inp_cur_data1[i_component1].num_at; i++)
    7489                 :             :         {
    7490                 :             : #ifdef UNDERIV_ADD_D_TO_PRECURSOR
    7491                 :             :             if (inp_cur_data1[i_component1].at[i].num_iso_H[1])
    7492                 :             :             {
    7493                 :             :                 inp_cur_data1[i_component1].at[i].num_iso_H[1] = 0; /* remove deuterium */
    7494                 :             :             }
    7495                 :             : #endif
    7496         [ #  # ]:           0 :             if (inp_cur_data1[i_component1].at[i].num_iso_H[2])
    7497                 :             :             {
    7498                 :           0 :                 FreeInpAtomData( inp_cur_data1 + i_component1 );
    7499                 :           0 :                 break;
    7500                 :             :             }
    7501                 :             :         }
    7502                 :             :     }
    7503                 :             :     /* Merge components into one -- must be only one */
    7504         [ #  # ]:           0 :     for (i_component1 = 0, num_atoms = 0; i_component1 < num_components1; i_component1++)
    7505                 :             :     {
    7506                 :           0 :         num_atoms += inp_cur_data1[i_component1].num_at;
    7507                 :             :     }
    7508                 :           0 :     at = (inp_ATOM *) inchi_calloc( num_atoms, sizeof( at[0] ) );
    7509                 :           0 :     cur_num_at = 0;
    7510         [ #  # ]:           0 :     for (i_component1 = 0; i_component1 < num_components1; i_component1++)
    7511                 :             :     {
    7512                 :             :         /* clean and prepare */
    7513         [ #  # ]:           0 :         if (!inp_cur_data1[i_component1].num_at)
    7514                 :             :         {
    7515                 :           0 :             continue; /* removed derivatizing object */
    7516                 :             :                       /*UnMarkOneComponent( inp_cur_data1[i_component1].at, inp_cur_data1[i_component1].num_at );*/
    7517                 :             :                       /* merge one by one */
    7518                 :             :         }
    7519                 :           0 :         cur_num_at = add_inp_ATOM( at, num_atoms, cur_num_at, inp_cur_data1[i_component1].at, inp_cur_data1[i_component1].num_at );
    7520                 :           0 :         FreeInpAtomData( inp_cur_data1 + i_component1 ); /* cleanup */
    7521                 :             :         /* djb-rwth: removing redundant code */
    7522                 :             :     }
    7523                 :             :     }
    7524                 :             : 
    7525                 :             :     /* Replace the component */
    7526                 :             :     /* Order of the following two statements is critically important */
    7527                 :             : 
    7528                 :           0 :     UnMarkDisconnectedComponents( orig_inp_data1 ); /* orig_inp_data1->at is same as inp_cur_data[i_component].at */
    7529                 :           0 :     FreeInpAtomData( inp_cur_data + i_component ); /* cleanup the original component */
    7530                 :             : 
    7531                 :           0 :     inp_cur_data[i_component].at = at;
    7532                 :           0 :     inp_cur_data[i_component].num_at = cur_num_at;
    7533         [ #  # ]:           0 :     inchi_free( inp_cur_data1 );
    7534                 :           0 : }
    7535                 :             : #endif /* #if ( RING2CHAIN == 1 || UNDERIVATIZE == 1 ) */
    7536                 :             : 
        

Generated by: LCOV version 2.0-1