// File: /workspace/github_c_makefile_star2_O3/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 0x8dcb(%rip), %rax      # 0x12e20
cmpb $0x0, (%rax)
je 0xa188
movl %edi, %ebx
leaq 0x8f51(%rip), %r12      # 0x12fb8
movq (%r12), %r13
leaq 0x8e7e(%rip), %rax      # 0x12ef0
cmpb $0x0, (%rax)
movq %r13, %rdi
je 0xa1cf
leaq 0x8283(%rip), %rdi      # 0x12308
callq 0x3060
leaq 0x8e5f(%rip), %rdx      # 0x12ef0
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rcx
callq 0x3400
testl %ebx, %ebx
je 0xa0d5
leaq 0xa9f2(%rip), %rbx      # 0x14aa0
movq %rbx, %rdi
callq 0x3130
movabsq $0x746f6f62657220, %rcx # imm = 0x746F6F62657220
movq %rcx, (%rax,%rbx)
leaq 0x422b(%rip), %rdi      # 0xe2f6
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
leaq 0xa9c4(%rip), %rdi      # 0x14aa0
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 0xa1cf
cmpl $0x0, 0xa989(%rip)      # 0x14a90
je 0xa1a1
leaq 0x41f9(%rip), %r14      # 0xe30d
leaq 0xa985(%rip), %rbx      # 0x14aa0
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
movq %rbx, %rdi
movl $0x1, %esi
callq 0x98e5
movq (%r12), %rdi
callq 0x3110
movq $0x0, (%r12)
leaq 0x8cb7(%rip), %r14      # 0x12e20
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9f2b
movq %r14, %rdi
callq 0x3080
movl $0x0, 0xa90a(%rip)      # 0x14a90
jmp 0xa1cb
movq 0x7e41(%rip), %rax      # 0x11fd0
movq (%rax), %rax
leaq 0x8e1f(%rip), %rcx      # 0x12fb8
movq %rax, (%rcx)
jmp 0xa2b5
leaq 0x417b(%rip), %rsi      # 0xe323
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32a0
leaq 0xa8e2(%rip), %rdi      # 0x14aa0
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
callq 0x3090
movq (%r12), %rdi
testq %rdi, %rdi
jne 0xa260
leaq 0x8c41(%rip), %rdi      # 0x12e20
leaq 0x395b(%rip), %rsi      # 0xdb41
callq 0x3430
movq %rax, (%r12)
testq %rax, %rax
je 0xa2c6
testq %r13, %r13
je 0xa217
leaq 0x4170(%rip), %rsi      # 0xe374
leaq 0xa895(%rip), %rdx      # 0x14aa0
movq %rax, %rdi
xorl %eax, %eax
callq 0x32a0
jmp 0xa230
leaq 0x417b(%rip), %rdi      # 0xe399
movl $0x3b, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x34a0
cmpb $0x0, 0xa869(%rip)      # 0x14aa0
je 0xa24f
leaq 0xa860(%rip), %rdi      # 0x14aa0
leaq 0x10(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3090
leaq 0xa84a(%rip), %rdi      # 0x14aa0
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 0xb81f(%rip), %edx      # 0x15aa0
setne %al
movq (%r12), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa2b0
leaq 0x4139(%rip), %rsi      # 0xe3d5
xorl %eax, %eax
callq 0x32a0
movl 0x4(%rbx), %eax
movl %eax, 0xb7f4(%rip)      # 0x15aa0
movq (%r12), %rdi
callq 0x3350
addq $0x1010, %rsp           # imm = 0x1010
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
callq 0x38bb
nop