// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/util.c
// Function: LogFileMaintain
void LogFileMaintain(int ForceLogSaveReboot)
{
    static char ThisLogTo[PATH_MAX];
    char NewLogTo[PATH_MAX];
    
    int HaveLogAlready;

    if (LogToFile[0] == 0){
        Log = stdout;
        return;
    }
    HaveLogAlready = (Log != NULL);
   
    if (MoveLogNames[0]){
        strftime(NewLogTo, PATH_MAX, MoveLogNames, localtime(&LastPic_mtime));
		if (ForceLogSaveReboot){
			// Just change the old name, so it gets backed up, and not overwritten
			// by new log after reboot.
			strcat(ThisLogTo, " reboot");
			printf("modified log name: %s\n",ThisLogTo);
		}
        if (strcmp(ThisLogTo, NewLogTo)){
            if (Log != NULL){
                if (BackupImageCount){
                    printf("Log rotate %s --> %s\n", ThisLogTo, NewLogTo);
                    fprintf(Log,"Log rotate %s --> %s\n", ThisLogTo, NewLogTo);
                    EnsurePathExists(ThisLogTo, 1);
                    fclose(Log);
                    Log = NULL;
                    CopyFile(LogToFile, ThisLogTo);
                    unlink(LogToFile);
                    BackupImageCount = 0;
                }else{
                    fprintf(Log,"Skip log rotate to %s (no images were saved in dir)\n", NewLogTo);
                    strncpy(ThisLogTo, NewLogTo, PATH_MAX);
                }
            }
        }
    }
    
    if (Log == NULL){
        Log = fopen(LogToFile,"a");
        if (Log == NULL){
            fprintf(stderr, "Failed to open log file %s\n",LogToFile);
            exit(-1);
        }
        if (HaveLogAlready){
            fprintf(Log,"<pre>Rotated log file (previous %s)\n",ThisLogTo);
        }else{
            fprintf(Log,"\n\n----------Restarting imcomp, keep old log---------------\n");
        }
        if (ThisLogTo[0]){
            strncpy(ThisLogTo, NewLogTo, PATH_MAX);
        }
        strcpy(ThisLogTo, NewLogTo);        
    }
    
    static int lastmin = 0;
    time_t now;
    time(&now);
    struct tm * nowTm = localtime(&now);
    if (nowTm->tm_min != lastmin && Log != NULL){
        // Put an indexable tag into the log file every minute.
        fprintf(Log, "<z id=\"%02d\"/>\n", nowTm->tm_min);
        lastmin = nowTm->tm_min;
    }
    
    fflush(Log); // Assuming logging to ramdisk, or we wear out flash.
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1010, %rsp           # imm = 0x1010
leaq 0x9775(%rip), %rax      # 0x12e30
cmpb $0x0, (%rax)
je 0x97e6
movl %edi, %ebx
leaq 0x98fb(%rip), %r12      # 0x12fc8
movq (%r12), %r13
leaq 0x9828(%rip), %rax      # 0x12f00
cmpb $0x0, (%rax)
movq %r13, %rdi
je 0x982d
leaq 0x8c2d(%rip), %rdi      # 0x12318
callq 0x3060
leaq 0x9809(%rip), %rdx      # 0x12f00
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rcx
callq 0x3410
testl %ebx, %ebx
je 0x973b
leaq 0xb39c(%rip), %rbx      # 0x14ab0
movq %rbx, %rdi
callq 0x3130
movabsq $0x746f6f62657220, %rcx # imm = 0x746F6F62657220
movq %rcx, (%rax,%rbx)
leaq 0x4b95(%rip), %rdi      # 0xe2c6
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
leaq 0xb36e(%rip), %rdi      # 0x14ab0
leaq 0x10(%rsp), %rsi
callq 0x3260
testl %eax, %eax
setne %al
movq (%r12), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x982d
cmpl $0x0, 0xb333(%rip)      # 0x14aa0
je 0x97ff
leaq 0x4b63(%rip), %r14      # 0xe2dd
leaq 0xb32f(%rip), %rbx      # 0x14ab0
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3170
movq (%r12), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x32a0
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x8f67
movq (%r12), %rdi
callq 0x3110
andq $0x0, (%r12)
leaq 0x9666(%rip), %r14      # 0x12e30
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9593
movq %r14, %rdi
callq 0x3080
andl $0x0, 0xb2bc(%rip)      # 0x14aa0
jmp 0x9829
movq 0x87e3(%rip), %rax      # 0x11fd0
movq (%rax), %rax
leaq 0x97d1(%rip), %rcx      # 0x12fc8
movq %rax, (%rcx)
jmp 0x990f
leaq 0x4aed(%rip), %rsi      # 0xe2f3
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32a0
leaq 0xb294(%rip), %rdi      # 0x14ab0
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
callq 0x3090
movq (%r12), %rdi
testq %rdi, %rdi
jne 0x98ba
leaq 0x95f3(%rip), %rdi      # 0x12e30
leaq 0x42cd(%rip), %rsi      # 0xdb11
callq 0x3440
movq %rax, (%r12)
testq %rax, %rax
je 0x9920
testq %r13, %r13
je 0x9875
leaq 0x4ae2(%rip), %rsi      # 0xe344
leaq 0xb247(%rip), %rdx      # 0x14ab0
movq %rax, %rdi
xorl %eax, %eax
callq 0x32a0
jmp 0x988a
leaq 0x4aed(%rip), %rdi      # 0xe369
pushq $0x3b
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x34b0
cmpb $0x0, 0xb21f(%rip)      # 0x14ab0
je 0x98a9
leaq 0xb216(%rip), %rdi      # 0x14ab0
leaq 0x10(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3090
leaq 0xb200(%rip), %rdi      # 0x14ab0
leaq 0x10(%rsp), %rsi
callq 0x30a0
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3060
movq %rax, %rbx
movl 0x4(%rax), %edx
cmpl 0xc1d5(%rip), %edx      # 0x15ab0
setne %al
movq (%r12), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x990a
leaq 0x4aaf(%rip), %rsi      # 0xe3a5
xorl %eax, %eax
callq 0x32a0
movl 0x4(%rbx), %eax
movl %eax, 0xc1aa(%rip)      # 0x15ab0
movq (%r12), %rdi
callq 0x3350
addq $0x1010, %rsp           # imm = 0x1010
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x86d1(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x49f7(%rip), %rsi      # 0xe328
leaq 0x94f8(%rip), %rdx      # 0x12e30
xorl %eax, %eax
callq 0x32a0
pushq $-0x1
popq %rdi
callq 0x34a0
nop