// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/start_camera_prog.c
// Function: manage_camera_prog
int manage_camera_prog(int NewImages)
{
    int timeout;
    time_t now = time(NULL);

    MsSinceImage += 1000;
    MsSinceLaunch += 1000;

    if (NewImages > 0){
        MsSinceImage = 0;
        NumTotalImages += NewImages;
    }else{
        if (MsSinceImage >= 3000){
            fprintf(Log,"No new images, %d (at %d:%d)\n",MsSinceImage, (int)(now%3600/60), (int)(now%60));
        }
    }

    if (camera_prog_pid == 0){
        // Camera program has not been launched.
        fprintf(Log,"Initial launch of camera program\n");
        goto force_restart;
    }

    timeout = relaunch_timeout * 1000;
    if (MsSinceImage > timeout){
        // Not getting any images for 5 seconds or vide ofiles for 10.
        // Probably something went wrong with raspistill or raspivid.
        if (MsSinceLaunch > timeout){
            if (give_up_timeout && MsSinceImage > give_up_timeout * 1000){
                if (NumTotalImages >= 5){
					// sometimes camera system just hangs.  This was rare with raspistill, 
					// but with libcamera-still it happens more often (about every two months)
                    fprintf(Log,"Relaunch camera program didn't fix.  Reboot!.  (%d sec since image)\n",MsSinceImage/1000);
                    // force rotation of log.
                    LogFileMaintain(1);
                    MsSinceImage = 0; // dummy for now.
                    printf("Reboot now\n");   // Requires setuid bit of reboot to be set as reboot
                    int r = system("reboot"); // normally requires root prviledges.
                                              // do "sudo chmod +s /usr/sbin/reboot" so normal process can run it.
                    fprintf(Log,"reboot returned %d (should not return -- please set the SUID bit of reboot)\n",r);
                    exit(0);
                }else{
                    // Less than 5 images.  Probably left over from last run.
                    fprintf(Log,"Camera program never worked! Give up. %d sec\n",MsSinceImage/1000);
                    LogFileMaintain(1);
                    // A reboot wouldn't fix this!
                    exit(0);
                }
            }else{
                fprintf(Log,"No images for %d sec.  Relaunch camera program\n",MsSinceImage/1000);
                goto force_restart;
            }
        }
    }
    return 0;

force_restart:
    relaunch_camera_prog();
    MsSinceLaunch = 0;
    InitialBrSum = InitialNumBr = 0;
    SinceLightChange = 0;
    return 1;
}

// Assembly:
pushq %rbx
movl %edi, %ebx
xorl %edi, %edi
callq 0x3310
movq %rax, %rsi
movl 0xb979(%rip), %eax      # 0x14690
movl $0x3e8, %ecx            # imm = 0x3E8
leal (%rax,%rcx), %r8d
movl %r8d, 0xb969(%rip)      # 0x14690
addl %ecx, 0xb967(%rip)      # 0x14694
testl %ebx, %ebx
jle 0x8d40
andl $0x0, 0xb958(%rip)      # 0x14690
addl %ebx, 0xb95a(%rip)      # 0x14698
jmp 0x8d90
cmpl $0x7d0, %eax            # imm = 0x7D0
jl 0x8d90
leaq 0xa27a(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
movl $0xe10, %ecx            # imm = 0xE10
movq %rsi, %rax
cqto
idivq %rcx
movq %rdx, %rax
movw $0x3c, %cx
cwtd
idivw %cx
movswl %ax, %ecx
pushq $0x3c
popq %r9
movq %rsi, %rax
cqto
idivq %r9
movq %rdx, %rax
leaq 0x52fe(%rip), %rsi      # 0xe081
movl %r8d, %edx
movl %eax, %r8d
xorl %eax, %eax
callq 0x32a0
cmpl $0x0, 0xb7a9(%rip)      # 0x14540
je 0x8def
imull $0x3e8, 0x9529(%rip), %edx # imm = 0x3E8
movl 0xb8e7(%rip), %eax      # 0x14690
xorl %ecx, %ecx
cmpl %edx, %eax
jle 0x8e21
cmpl %edx, 0xb8df(%rip)      # 0x14694
jle 0x8e21
movl 0x9513(%rip), %ecx      # 0x122d0
testl %ecx, %ecx
je 0x8dcb
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
cmpl %ecx, %eax
jg 0x8e25
leaq 0xa1f6(%rip), %rcx      # 0x12fc8
movq (%rcx), %rdi
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
leaq 0x53a4(%rip), %rsi      # 0xe188
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
jmp 0x8e0b
leaq 0xa1d2(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x529f(%rip), %rdi      # 0xe09f
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
callq 0x87c0
andl $0x0, 0xb87d(%rip)      # 0x14694
andl $0x0, 0xb86a(%rip)      # 0x14688
pushq $0x1
popq %rcx
movl %ecx, %eax
popq %rbx
retq
movl 0xb86d(%rip), %ecx      # 0x14698
movl $0x3e8, %esi            # imm = 0x3E8
cltd
idivl %esi
leaq 0xa18e(%rip), %rbx      # 0x12fc8
movq (%rbx), %rdi
cmpl $0x5, %ecx
jl 0x8e8e
leaq 0x5278(%rip), %rsi      # 0xe0c1
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
pushq $0x1
popq %rdi
callq 0x96a4
andl $0x0, 0xb82f(%rip)      # 0x14690
leaq 0x53b1(%rip), %rdi      # 0xe219
callq 0x30c0
leaq 0x5292(%rip), %rdi      # 0xe106
callq 0x3150
movq (%rbx), %rdi
leaq 0x528a(%rip), %rsi      # 0xe10d
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
jmp 0x8ea6
leaq 0x52c5(%rip), %rsi      # 0xe15a
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
pushq $0x1
popq %rdi
callq 0x96a4
xorl %edi, %edi
callq 0x34a0
nopl (%rax)