// File: /workspace/github_c_makefile_star2_O1/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 0x8dc6(%rip), %rax      # 0x12e20
cmpb $0x0, (%rax)
je 0xa18a
leaq 0x8f4e(%rip), %r12      # 0x12fb8
movq (%r12), %r13
leaq 0x8e7b(%rip), %rax      # 0x12ef0
cmpb $0x0, (%rax)
je 0xa1cd
movl %edi, %ebx
leaq 0x8281(%rip), %rdi      # 0x12308
callq 0x3060
leaq 0x8e5d(%rip), %rdx      # 0x12ef0
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rcx
callq 0x33f0
testl %ebx, %ebx
je 0xa0d7
leaq 0xa9f0(%rip), %rbx      # 0x14aa0
movq %rbx, %rdi
callq 0x3130
movabsq $0x746f6f62657220, %rcx # imm = 0x746F6F62657220
movq %rcx, (%rax,%rbx)
leaq 0x41b9(%rip), %rdi      # 0xe286
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
leaq 0xa9c2(%rip), %rdi      # 0x14aa0
leaq 0x10(%rsp), %rsi
callq 0x3250
testl %eax, %eax
setne %al
movq (%r12), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa1cd
cmpl $0x0, 0xa987(%rip)      # 0x14a90
je 0xa1a3
leaq 0x4187(%rip), %r14      # 0xe29d
leaq 0xa983(%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 0x3290
movq %rbx, %rdi
movl $0x1, %esi
callq 0x98f1
movq (%r12), %rdi
callq 0x3110
movq $0x0, (%r12)
leaq 0x8cb5(%rip), %r14      # 0x12e20
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9f30
movq %r14, %rdi
callq 0x3080
movl $0x0, 0xa908(%rip)      # 0x14a90
jmp 0xa1cd
movq 0x7e3f(%rip), %rax      # 0x11fd0
movq (%rax), %rax
leaq 0x8e1d(%rip), %rcx      # 0x12fb8
movq %rax, (%rcx)
jmp 0xa2b5
leaq 0x4109(%rip), %rsi      # 0xe2b3
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3290
leaq 0xa8e0(%rip), %rdi      # 0x14aa0
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
callq 0x3090
cmpq $0x0, (%r12)
jne 0xa260
leaq 0x8c41(%rip), %rdi      # 0x12e20
leaq 0x38eb(%rip), %rsi      # 0xdad1
callq 0x3420
movq %rax, (%r12)
testq %rax, %rax
je 0xa2c6
testq %r13, %r13
je 0xa217
leaq 0x4100(%rip), %rsi      # 0xe304
leaq 0xa895(%rip), %rdx      # 0x14aa0
movq %rax, %rdi
xorl %eax, %eax
callq 0x3290
jmp 0xa230
leaq 0x410b(%rip), %rdi      # 0xe329
movl $0x3b, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x3490
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 0x3300
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 0xa2ac
leaq 0x40c9(%rip), %rsi      # 0xe365
xorl %eax, %eax
callq 0x3290
movl 0x4(%rbx), %eax
movl %eax, 0xb7f4(%rip)      # 0x15aa0
movq (%r12), %rdi
callq 0x3340
addq $0x1010, %rsp           # imm = 0x1010
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
callq 0x38ab
nop