// File: /workspace/github_c_makefile_star2_O1/schroeder-lvb[P]sissm/src/api.c
// Function: apiInit
int apiInit( void )
{
    cfsPtr cP;
    char   myIP[API_LINE_STRING_MAX], myRconPassword[API_LINE_STRING_MAX];
    int    i, j, numMutators, myPort, mapsCount;
    char   serverName[API_LINE_STRING_MAX];
    char   mutListTemp1[CFS_FETCH_MAX], mutListTemp2[CFS_FETCH_MAX], *u;
    char   tmpStr[ API_MUT_MAXCHAR ];

    // Read the "sissm" systems configuration variables
    //
    cP = cfsCreate( sissmGetConfigPath() );

    myPort = (int) cfsFetchNum( cP, "sissm.RconPort", 27015 );            
    strlcpy( myIP, cfsFetchStr( cP, "sissm.RconIP", "127.0.0.1"), API_LINE_STRING_MAX );
    strlcpy( myRconPassword, cfsFetchStr( cP, "sissm.RconPassword", ""), API_LINE_STRING_MAX );
    // strlcpy( adminListFilePath, cfsFetchStr( cP, "sissm.adminListFilePath", "Admins.txt"), API_LINE_STRING_MAX );

    strlcpy( serverName, cfsFetchStr( cP, "sissm.ServerName", "Unknown Server" ), API_LINE_STRING_MAX );

    // read the Bad Words filename
    //
    strlcpy( badWordsFilePath, cfsFetchStr( cP, "sissm.badWordsFilePath", "" ), CFS_FETCH_MAX );

    // read the Mapcycle filepath
    //
    strlcpy( mapcycleFilePath, cfsFetchStr( cP, "sissm.mapcycleFilePath", "" ), CFS_FETCH_MAX );

    // read the Mutator lists allowed by this system
    //
    strlcpy( mutListTemp1, cfsFetchStr( cP, "sissm.MutatorsStock", "" ), CFS_FETCH_MAX );
    strlcpy( mutListTemp2, cfsFetchStr( cP, "sissm.MutatorsMods",  "" ), CFS_FETCH_MAX );
    strlcat( mutListTemp2, " ", CFS_FETCH_MAX );
    if (( strlen( mutListTemp2 ) + strlen( mutListTemp1 ) ) < CFS_FETCH_MAX )
        strlcat( mutListTemp2, mutListTemp1, CFS_FETCH_MAX );
    else
        logPrintf( LOG_LEVEL_CRITICAL, "api", "String overflow reading mutator list sissm.MutatorStock/Mods" );

    // parse the Mutator list into separate array element
    //
    j = 0;
    strclr( mutAllowedList[ 0 ] );
    while ( 1 == 1 ) {
        u = getWord( mutListTemp2, j, " ,=:\012\015" );
        if ( u == NULL )              break;
        if ( 0 == strlen( u ) )       break;
        strlcpy( mutAllowedList[ j ], u, API_MUT_MAXCHAR );
        strclr( mutAllowedList[ ++j ] );
        if ( j >= API_MUT_MAXLIST ) break;
    }
    j = 0;
    while ( 1 == 1 ) {
        if ( 0 == strlen( mutAllowedList[j] )) break;
        logPrintf( LOG_LEVEL_INFO, "api", "Mutator read %d ::%s::", j, mutAllowedList[j] );
        j++;
    }

    // create a sorted Mutator list by size of string (for lookup match priority) 
    //
    numMutators = j - 1;
    for ( j = 0; j<numMutators + 1; j++ ) {   // include one extra array w/ terminating ""
        strlcpy( mutAllowedListSorted[ j ], mutAllowedList[ j ], API_MUT_MAXCHAR );
    }
    for (j = 0; j<numMutators-1; j++ ) {
        for ( i = j+1; i<numMutators; i++ ) {
            if (  strlen( mutAllowedListSorted[i] ) > strlen( mutAllowedListSorted[j] ) ) {
                strlcpy( tmpStr, mutAllowedListSorted[i], API_MUT_MAXCHAR );
                strlcpy( mutAllowedListSorted[i], mutAllowedListSorted[j], API_MUT_MAXCHAR );
                strlcpy( mutAllowedListSorted[j], tmpStr, API_MUT_MAXCHAR );
            }
        }
    }
    j = 0;
    while ( 1 == 1 ) {
        if ( 0 == strlen( mutAllowedListSorted[j] )) break;
        logPrintf( LOG_LEVEL_INFO, "api", "Mutator sort %d ::%s::", j, mutAllowedListSorted[j] );
        j++;
    }

    // read the AUTH (permissions) data
    //
    apiInitAuth();

    // Used with Circleus' "Advanced Chat" mod
    //
    strlcpy( removeCodes, cfsFetchStr( cP, "sissm.removeCodes", ""), API_LINE_STRING_MAX );
    strlcpy( sayPrefix,   cfsFetchStr( cP, "sissm.sayPrefix",  ""),  API_LINE_STRING_MAX );
    strlcpy( sayPostfix,  cfsFetchStr( cP, "sissm.sayPostfix", ""),  API_LINE_STRING_MAX );

#if FINALCOUNTER_REDUCE
    // Checkpoint/Hardcorecheckpoint Crash Protection Limiter 
    //
    maxBotsCounterAttack = (int) cfsFetchNum( cP, "sissm.MaxBotsCounterAttack", 30.0 ); 
    finalCacheBotQuotaMultiplier = (double) cfsFetchNum( cP, "sissm.FinalCacheBotQuotaMultiplier", 1.2 );
#endif

    //  Close the configuration file reader
    //
    cfsDestroy( cP );

    // Set map to unknown
    //
    rosterSetMapName( "Unknown" );

    // Initialize the RCON TCP/IP interface
    //
    _rPtr = rdrvInit( myIP, myPort, myRconPassword );

    // Setup callbacks for player entering and leaving
    // 
    eventsRegister( SISSM_EV_CLIENT_ADD,  _apiPlayerConnectedCB );
    eventsRegister( SISSM_EV_CLIENT_DEL,  _apiPlayerDisconnectedCB );
    eventsRegister( SISSM_EV_MAPCHANGE,   _apiMapChangeCB );
    eventsRegister( SISSM_EV_TRAVEL,      _apiTravelCB );
    eventsRegister( SISSM_EV_SESSIONLOG,  _apiSessionLogCB );
    eventsRegister( SISSM_EV_ROUND_START, _apiRoundStartCB );

    // These callbacks are used for detecting players entering
    // and leaving territorial capture zone (piantirush)
    //
    eventsRegister( SISSM_EV_BP_PLAYER_CONN,    _apiBPCharNameCB );
    eventsRegister( SISSM_EV_BP_PLAYER_DISCONN, _apiBPCharDisconnectCB );
    eventsRegister( SISSM_EV_BP_CHARNAME,       _apiBPCharPlayerCB );
    eventsRegister( SISSM_EV_BP_TOUCHED_OBJ,    _apiBPCharTouchCB );
    eventsRegister( SISSM_EV_BP_UNTOUCHED_OBJ,  _apiBPCharUntouchCB );

    // Event to handle caching of map objective list 
    //
    eventsRegister( SISSM_EV_MAP_OBJECTIVE, _apiMapObjectiveCB );

    // This hook is mostly used for DEBUG use, non-production
    //
    eventsRegister( SISSM_EV_CHAT,                _apiChatCB );

    // for detecting if the game is in Counterattack (Checkpoint/Hardcore)
    //
    eventsRegister( SISSM_EV_ACTIVITY,         _apiCounterStartCB );
    eventsRegister( SISSM_EV_CA_STOP,          _apiCounterStopCB  );

    // Setup Alarm (periodic callbacks) for fetching roster from RCON
    // 
    _apiPollAlarmPtr = alarmCreate( _apiPollAlarmCB );
    alarmReset( _apiPollAlarmPtr, API_LISTPLAYERS_PERIOD );

    // Clear the Roster module that keeps track of players
    //
    rosterInit();
    rosterSetServerName( serverName );
    lastRosterSuccessTime = apiTimeGet();      
    rosterReset();

    // Read the admin list
    //
    // adminCount = apiIdListRead( adminListFilePath, adminIdList );
    // logPrintf( LOG_LEVEL_CRITICAL, "api", "Admin list %d clients from file %s", adminCount, adminListFilePath );

    // Read the Bad Words list
    //
    // badWordsCount = apiWordListRead( badWordsFilePath, badWordsList );
    // logPrintf( LOG_LEVEL_CRITICAL, "api", "BadWords list %d words from file %s", badWordsCount, badWordsFilePath );
    // 
    apiInitBadWords();

    // Read the Mapcycle.txt
    //
    mapsCount = apiMapcycleRead( mapcycleFilePath );
    logPrintf( LOG_LEVEL_CRITICAL, "api", "Maplist read %d maps from file %s", mapsCount, mapcycleFilePath);


    // Clear the internal roster & gamestate tracking variables
    //
    strclr( rosterPrevious );
    strclr( rosterCurrent  );
    strclr( gameStateCurrent );
    strclr( gameStatePrevious );

    // Clear the last reboot reason/time 
    //
    lastRebootTime = apiTimeGet();
    strlcpy( lastRebootReason, "Restart", 256 );

    // Clear the BPChar translation table
    //
    _apiBPCharInitCB( "" );

    // on init clear the cache of objectives names
    //
    for (i=0; i<API_OBJECTIVES_MAX; i++) strclr( _objectiveListCache[i]) ;

    // Clear the clan list
    //
    apiIdListClear( clanIdList );

    return( _rPtr == NULL );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb88, %rsp            # imm = 0xB88
callq 0x8969
movq %rax, %rdi
callq 0x9004
movq %rax, %rbx
leaq 0xc152(%rip), %rsi      # 0x1e2af
movsd 0x8eb3(%rip), %xmm0     # 0x1b018
movq %rax, %rdi
callq 0x9276
movsd %xmm0, (%rsp)
leaq 0xc145(%rip), %rsi      # 0x1e2be
leaq 0x8f8c(%rip), %rdx      # 0x1b10c
movq %rbx, %rdi
callq 0x907b
leaq 0x280(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf038
leaq 0xc127(%rip), %rsi      # 0x1e2cb
leaq 0x9566(%rip), %r14      # 0x1b711
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x180(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf038
leaq 0xc10c(%rip), %rsi      # 0x1e2de
leaq 0x8ef2(%rip), %rdx      # 0x1b0cb
movq %rbx, %rdi
callq 0x907b
leaq 0x80(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf038
leaq 0xc0f2(%rip), %rsi      # 0x1e2ef
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x142bb1(%rip), %rdi    # 0x154dc0
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
callq 0xf038
leaq 0xc0e3(%rip), %rsi      # 0x1e306
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x142c9b(%rip), %rdi    # 0x154ed0
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
callq 0xf038
leaq 0xc0d4(%rip), %rsi      # 0x1e31d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x780(%rsp), %r15
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %rax, %rsi
callq 0xf038
leaq 0xc0be(%rip), %rsi      # 0x1e331
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x380(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rax, %rsi
callq 0xf038
leaq 0x9eb4(%rip), %rsi      # 0x1c156
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
callq 0xefc4
movq %r14, %rdi
callq 0x8120
movq %rax, %r14
movq %r15, %rdi
callq 0x8120
addq %r14, %rax
cmpq $0x3ff, %rax            # imm = 0x3FF
ja 0x122e9
leaq 0x380(%rsp), %rdi
leaq 0x780(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0xefc4
jmp 0x12300
leaq 0xbb9f(%rip), %rsi      # 0x1de8f
leaq 0xc04d(%rip), %rdx      # 0x1e344
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a227
cvttsd2si (%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x142cc0(%rip), %r14    # 0x154fd0
movq %r14, %rdi
callq 0xf4dd
xorl %r15d, %r15d
leaq 0xc05f(%rip), %r12      # 0x1e381
leaq 0x380(%rsp), %r13
movq %r13, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0xf4e1
testq %rax, %rax
je 0x12367
cmpb $0x0, (%rax)
je 0x12367
movl $0x50, %edx
movq %r14, %rdi
movq %rax, %rsi
callq 0xf038
incq %r15
addq $0x50, %r14
movq %r14, %rdi
callq 0xf4dd
cmpq $0x40, %r15
jne 0x1232a
cmpb $0x0, 0x142c62(%rip)    # 0x154fd0
je 0x123af
leaq 0x142c59(%rip), %r12    # 0x154fd0
leaq 0xbb11(%rip), %r15      # 0x1de8f
leaq 0xc003(%rip), %r13      # 0x1e388
xorl %ebp, %ebp
movl $0x2, %edi
movq %r15, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0x1a227
incl %ebp
cmpb $0x0, 0x50(%r12)
leaq 0x50(%r12), %r12
jne 0x12387
jmp 0x123b1
xorl %ebp, %ebp
movl %ebp, %ebx
subl $0x1, %ebx
jb 0x123e6
movl %ebp, %r14d
leaq 0x142c0e(%rip), %r15    # 0x154fd0
leaq 0x144007(%rip), %r12    # 0x1563d0
movl $0x50, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0xf038
addq $0x50, %r15
addq $0x50, %r12
decq %r14
jne 0x123c9
cmpl $0x3, %ebp
jl 0x124a6
addl $-0x2, %ebp
movl %ebx, %eax
movq %rax, 0x18(%rsp)
movl $0x1, %ebx
leaq 0x14401b(%rip), %r13    # 0x156420
xorl %eax, %eax
leaq 0x30(%rsp), %r15
movq %rbp, 0x20(%rsp)
leaq (%rax,%rax,4), %r12
incq %rax
movq %rax, 0x28(%rsp)
shlq $0x4, %r12
leaq 0x143fa8(%rip), %rax    # 0x1563d0
addq %rax, %r12
movq 0x18(%rsp), %r14
movq %r13, (%rsp)
movq %r13, %rdi
callq 0x8120
movq %rax, %rbp
movq %r12, %rdi
callq 0x8120
cmpq %rax, %rbp
jbe 0x1247c
movl $0x50, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0xf038
movl $0x50, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0xf038
movl $0x50, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0xf038
addq $0x50, %r13
decq %r14
cmpq %r14, %rbx
jne 0x12434
incq %rbx
movq (%rsp), %r13
addq $0x50, %r13
movq 0x20(%rsp), %rbp
movq 0x28(%rsp), %rax
cmpq %rbp, %rax
jne 0x12411
cmpb $0x0, 0x143f23(%rip)    # 0x1563d0
je 0x124ed
leaq 0x143f1a(%rip), %r15    # 0x1563d0
leaq 0xb9d2(%rip), %r14      # 0x1de8f
leaq 0xbedb(%rip), %r12      # 0x1e39f
xorl %r13d, %r13d
movl $0x2, %edi
movq %r14, %rsi
movq %r12, %rdx
movl %r13d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x1a227
incl %r13d
cmpb $0x0, 0x50(%r15)
leaq 0x50(%r15), %r15
jne 0x124c7
callq 0x11cff
leaq 0xbebd(%rip), %rsi      # 0x1e3b6
leaq 0x9211(%rip), %r14      # 0x1b711
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0xbed69(%rip), %rdi     # 0xd1280
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf038
leaq 0xbe9d(%rip), %rsi      # 0x1e3c8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x145293(%rip), %rdi    # 0x1577d0
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf038
leaq 0xbe87(%rip), %rsi      # 0x1e3d8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x907b
leaq 0x14536d(%rip), %rdi    # 0x1578d0
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf038
movq %rbx, %rdi
callq 0x905c
leaq 0xbe6a(%rip), %rdi      # 0x1e3e9
callq 0x1424e
leaq 0x280(%rsp), %rdi
leaq 0x180(%rsp), %rdx
movl 0xc(%rsp), %esi
callq 0x9617
movq %rax, 0xc6ee4(%rip)     # 0xd9488
leaq -0xf9e(%rip), %rsi      # 0x1160d
movl $0x3, %edi
callq 0xd08c
leaq -0xf77(%rip), %rsi      # 0x11645
movl $0x4, %edi
callq 0xd08c
leaq -0xbb8(%rip), %rsi      # 0x11a15
movl $0x5, %edi
callq 0xd08c
leaq -0xf6b(%rip), %rsi      # 0x11673
movl $0x11, %edi
callq 0xd08c
leaq -0xf72(%rip), %rsi      # 0x1167d
movl $0x12, %edi
callq 0xd08c
leaq -0x936(%rip), %rsi      # 0x11cca
movl $0x8, %edi
callq 0xd08c
leaq -0xea5(%rip), %rsi      # 0x1176c
movl $0x15, %edi
callq 0xd08c
leaq -0xcd8(%rip), %rsi      # 0x1194a
movl $0x16, %edi
callq 0xd08c
leaq -0xe4c(%rip), %rsi      # 0x117e7
movl $0x17, %edi
callq 0xd08c
leaq -0xc35(%rip), %rsi      # 0x11a0f
movl $0x18, %edi
callq 0xd08c
leaq -0xc43(%rip), %rsi      # 0x11a12
movl $0x19, %edi
callq 0xd08c
leaq -0xa8f(%rip), %rsi      # 0x11bd7
movl $0x1a, %edi
callq 0xd08c
leaq -0x106d(%rip), %rsi     # 0x1160a
xorl %r15d, %r15d
xorl %edi, %edi
callq 0xd08c
leaq -0x601(%rip), %rsi      # 0x12087
movl $0x1c, %edi
callq 0xd08c
leaq -0x5a6(%rip), %rsi      # 0x120f3
movl $0x1e, %edi
callq 0xd08c
leaq -0x133b(%rip), %rdi     # 0x1136f
callq 0xf8d7
movq %rax, 0xcedda(%rip)     # 0xe1490
movl $0xa, %esi
movq %rax, %rdi
callq 0xf953
callq 0x144be
leaq 0x80(%rsp), %rdi
callq 0x14448
xorl %edi, %edi
callq 0x82a0
movq %rax, 0xc6d9d(%rip)     # 0xd9480
callq 0x14464
leaq 0x1426d1(%rip), %rbx    # 0x154dc0
leaq 0xbed8a(%rip), %rsi     # 0xd1480
movq %rbx, %rdi
callq 0x10d42
leaq 0xb78a(%rip), %r14      # 0x1de8f
leaq 0xbb12(%rip), %rdx      # 0x1e21e
xorl %edi, %edi
movq %r14, %rsi
movl %eax, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1a227
leaq 0x1427ac(%rip), %rbx    # 0x154ed0
movq %rbx, %rdi
callq 0x127f9
leaq 0xbcbe(%rip), %rdx      # 0x1e3f1
xorl %edi, %edi
movq %r14, %rsi
movl %eax, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1a227
leaq 0xcad45(%rip), %rdi     # 0xdd490
callq 0xf4dd
leaq 0xc6d39(%rip), %rdi     # 0xd9490
callq 0xf4dd
leaq 0x14526d(%rip), %rdi    # 0x1579d0
callq 0xf4dd
leaq 0xdae41(%rip), %rdi     # 0xed5b0
callq 0xf4dd
xorl %edi, %edi
callq 0x82a0
movq %rax, 0x14924e(%rip)    # 0x15b9d0
movabsq $0x74726174736552, %rax # imm = 0x74726174736552
movq %rax, 0x14924d(%rip)    # 0x15b9e0
callq 0x116c5
leaq 0xd6e11(%rip), %rbx     # 0xe95b0
leaq (%rbx,%r15), %rdi
callq 0xf4dd
addq $0x100, %r15            # imm = 0x100
cmpq $0x4000, %r15           # imm = 0x4000
jne 0x1279f
xorl %ebx, %ebx
leaq 0x14931f(%rip), %r14    # 0x15bae0
leaq (%r14,%rbx), %rdi
callq 0xf4dd
addq $0x100, %rbx            # imm = 0x100
cmpq $0x10000, %rbx          # imm = 0x10000
jne 0x127c1
xorl %eax, %eax
cmpq $0x0, 0xc6ca4(%rip)     # 0xd9488
sete %al
addq $0xb88, %rsp            # imm = 0xB88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq