// File: /workspace/github_c_makefile_star2_O0/schroeder-lvb[P]sissm/src/sissm.c
// Function: sissmMainLoop
int sissmMainLoop( void )
{
    int errCode = 0;
    int masterState = SM_STARTUP;
    ftrackObj *fPtr = NULL;
    char strBuffer[4096];
    unsigned long timePrev;          // for tracking periodic 1.0 Hz call


    // Log some general info
    //
    logPrintf(LOG_LEVEL_INFO, "sissm", "Server ID: ::%s::", sissmConfig.serverName );
    if ( sissmConfig.gracefulExit ) 
        logPrintf(LOG_LEVEL_CRITICAL, "sissm", "** Warning: Console '^C' may take several seconds to process");
    logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_STARTUP" );

    timePrev = (unsigned long) time( NULL );

    for ( ;; ) {

        //  0. Synchronized SIGTERM exit process
        //
        if ( gracefulKill ) {
            logPrintf( LOG_LEVEL_CRITICAL, "sissm", "######## SIGTERM Exit #######" );
            break;
        }

	//  1. Server restart supercedure
	//
        if (sissmServerRestartPending()) masterState = SM_SYS_RESTART;     // restart supercedure

        //  2. Logfile tracking state machine
        //   
        switch ( masterState ) {
        case SM_STARTUP:
            logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_STARTUP");
            if ( fPtr == NULL ) {
                sleep( 1 );
                fPtr = ftrackOpen( sissmConfig.gameLogFile );
            }
            if (fPtr != NULL)  {
                logPrintf( LOG_LEVEL_CRITICAL, "sissm", "Tracking game logfile ::%s::", 
                    sissmConfig.gameLogFile );
                ftrackTailOfFile( fPtr, strBuffer, sizeof( strBuffer ), 1 );       // seek to end
                masterState = SM_POLLING_INIT;
            }
            else {
                logPrintf( LOG_LEVEL_CRITICAL, "sissm", "Trying to open game logfile ::%s::", 
                    sissmConfig.gameLogFile );
            }
            break;
        case SM_POLLING_INIT:
            logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_POLLING_INIT->TRACKING");
            eventsDispatch( "~INIT~" );
            ftrackResync( fPtr );      //      check for log file rotate and follow
            masterState = SM_POLLING_TRACKING;
            break;
        case SM_POLLING_TRACKING:
            if ( 0 == ftrackTailOfFile( fPtr, strBuffer, sizeof( strBuffer ), 0 ) ) {
                logPrintf(LOG_LEVEL_RAWDUMP, "sissm", "::%s::", strBuffer);
                eventsDispatch( strBuffer );
            }
            else { 
                usleep( SISSM_POLLING_INTERVAL_MICROSEC );
            }
            break;
        case SM_SYS_RESTART:
            logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_SYS_RESTART");
            eventsDispatch( "~RESTART~" );
            ftrackClose( fPtr );
            fPtr = NULL;
            sissmRestartServer();
            masterState = SM_STARTUP;
            break;
        default:
            ;;
        }

        // 3. periodic callbacks and alarms processing
	// currently at 1.0Hz polling rate
        //   
        if ( timePrev != (unsigned long int) time( NULL ) ) {
	    alarmDispatch();
            eventsDispatch( "~PERIODIC~" );
            timePrev = (unsigned int) time( NULL );
            ftrackResync( fPtr );                          // check for log file rotate and follow
        }
    }

    // 4. graceful exit
    // This only happens if "sissm.gracefulExit" flag is set in the .cfg file.
    // Before SISSM shuts down, generate a synthetic event to all plugins so that 
    // they can take actions to leave the server in a playable state without SISSM.
    //
    eventsDispatch( "~SIGTERM~" );

    return errCode;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1030, %rsp           # imm = 0x1030
movl $0x0, -0x4(%rbp)
movl $0x3e9, -0x8(%rbp)      # imm = 0x3E9
movq $0x0, -0x10(%rbp)
movl $0x2, %edi
leaq 0x1a377(%rip), %rsi     # 0x231a4
leaq 0x1a55b(%rip), %rdx     # 0x2338f
leaq 0x290d5(%rip), %rcx     # 0x31f10
addq $0x408, %rcx            # imm = 0x408
movb $0x0, %al
callq 0x21bf0
cmpl $0x0, 0x2acd0(%rip)     # 0x33b20
je 0x8e69
xorl %edi, %edi
leaq 0x1a349(%rip), %rsi     # 0x231a4
leaq 0x1a53f(%rip), %rdx     # 0x233a1
movb $0x0, %al
callq 0x21bf0
movl $0x2, %edi
leaq 0x1a32f(%rip), %rsi     # 0x231a4
leaq 0x1a562(%rip), %rdx     # 0x233de
movb $0x0, %al
callq 0x21bf0
xorl %eax, %eax
movl %eax, %edi
callq 0x8280
movq %rax, -0x1018(%rbp)
movl 0x29067(%rip), %eax     # 0x31f00
cmpl $0x0, %eax
je 0x8eba
xorl %edi, %edi
leaq 0x1a2fd(%rip), %rsi     # 0x231a4
leaq 0x1a543(%rip), %rdx     # 0x233f1
movb $0x0, %al
callq 0x21bf0
jmp 0x9107
callq 0x8b40
cmpl $0x0, %eax
je 0x8ecb
movl $0xbb9, -0x8(%rbp)      # imm = 0xBB9
movl -0x8(%rbp), %eax
movl %eax, -0x101c(%rbp)
subl $0x3e9, %eax            # imm = 0x3E9
je 0x8f19
jmp 0x8edd
movl -0x101c(%rbp), %eax
subl $0xbb9, %eax            # imm = 0xBB9
je 0x9069
jmp 0x8ef0
movl -0x101c(%rbp), %eax
subl $0xfa1, %eax            # imm = 0xFA1
je 0x8fd4
jmp 0x8f03
movl -0x101c(%rbp), %eax
subl $0xfa2, %eax            # imm = 0xFA2
je 0x900f
jmp 0x90ae
movl $0x2, %edi
leaq 0x1a27f(%rip), %rsi     # 0x231a4
leaq 0x1a4b2(%rip), %rdx     # 0x233de
movb $0x0, %al
callq 0x21bf0
cmpq $0x0, -0x10(%rbp)
jne 0x8f5b
movl $0x1, %edi
callq 0x8380
leaq 0x28fc5(%rip), %rdi     # 0x31f10
addq $0x100c, %rdi           # imm = 0x100C
callq 0x11c90
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x8faa
xorl %edi, %edi
leaq 0x1a239(%rip), %rsi     # 0x231a4
leaq 0x1a49d(%rip), %rdx     # 0x2340f
leaq 0x28f97(%rip), %rcx     # 0x31f10
addq $0x100c, %rcx           # imm = 0x100C
movb $0x0, %al
callq 0x21bf0
movq -0x10(%rbp), %rdi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movl $0x1, %ecx
callq 0x12040
movl $0xfa1, -0x8(%rbp)      # imm = 0xFA1
jmp 0x8fcf
xorl %edi, %edi
leaq 0x1a1f1(%rip), %rsi     # 0x231a4
leaq 0x1a472(%rip), %rdx     # 0x2342c
leaq 0x28f4f(%rip), %rcx     # 0x31f10
addq $0x100c, %rcx           # imm = 0x100C
movb $0x0, %al
callq 0x21bf0
jmp 0x90b0
movl $0x2, %edi
leaq 0x1a1c4(%rip), %rsi     # 0x231a4
leaq 0x1a468(%rip), %rdx     # 0x2344f
movb $0x0, %al
callq 0x21bf0
leaq 0x1a47c(%rip), %rdi     # 0x23471
callq 0xf070
movq -0x10(%rbp), %rdi
callq 0x11e60
movl $0xfa2, -0x8(%rbp)      # imm = 0xFA2
jmp 0x90b0
movq -0x10(%rbp), %rdi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0x12040
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x905d
leaq -0x1010(%rbp), %rcx
movl $0x4, %edi
leaq 0x1a163(%rip), %rsi     # 0x231a4
leaq 0x1ac96(%rip), %rdx     # 0x23cde
movb $0x0, %al
callq 0x21bf0
leaq -0x1010(%rbp), %rdi
callq 0xf070
jmp 0x9067
movl $0x2710, %edi           # imm = 0x2710
callq 0x83c0
jmp 0x90b0
movl $0x2, %edi
leaq 0x1a12f(%rip), %rsi     # 0x231a4
leaq 0x1a3fc(%rip), %rdx     # 0x23478
movb $0x0, %al
callq 0x21bf0
leaq 0x1a405(%rip), %rdi     # 0x2348f
callq 0xf070
movq -0x10(%rbp), %rdi
callq 0x12000
movq $0x0, -0x10(%rbp)
callq 0x8ba0
movl $0x3e9, -0x8(%rbp)      # imm = 0x3E9
jmp 0x90b0
jmp 0x90b0
movq -0x1018(%rbp), %rax
movq %rax, -0x1028(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x8280
movq %rax, %rcx
movq -0x1028(%rbp), %rax
cmpq %rcx, %rax
je 0x9102
callq 0x12af0
leaq 0x1a3b7(%rip), %rdi     # 0x23499
callq 0xf070
xorl %eax, %eax
movl %eax, %edi
callq 0x8280
movl %eax, %eax
movq %rax, -0x1018(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11e60
jmp 0x8e93
leaq 0x1a396(%rip), %rdi     # 0x234a4
callq 0xf070
movl -0x4(%rbp), %eax
addq $0x1030, %rsp           # imm = 0x1030
popq %rbp
retq
nop