// File: /workspace/github_c_makefile_star2_O3/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, %r8
movl 0xb05e(%rip), %eax      # 0x14680
movl $0x3e8, %ecx            # imm = 0x3E8
leal (%rax,%rcx), %r9d
movl %r9d, 0xb04e(%rip)      # 0x14680
addl %ecx, 0xb04c(%rip)      # 0x14684
testl %ebx, %ebx
jle 0x9651
movl $0x0, 0xb03a(%rip)      # 0x14680
addl %ebx, 0xb03c(%rip)      # 0x14688
jmp 0x96e4
cmpl $0x7d0, %eax            # imm = 0x7D0
jl 0x96e4
leaq 0x9955(%rip), %rcx      # 0x12fb8
movabsq $0x48d159e26af37c05, %rdx # imm = 0x48D159E26AF37C05
movq %r8, %rax
imulq %rdx
movq (%rcx), %rdi
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0xa, %rdx
addl %eax, %edx
imull $0xe10, %edx, %eax      # imm = 0xE10
movl %r8d, %ecx
subl %eax, %ecx
movswl %cx, %eax
imull $0xffff8889, %eax, %eax # imm = 0xFFFF8889
shrl $0x10, %eax
addl %ecx, %eax
movzwl %ax, %eax
movswl %ax, %ecx
shrl $0xf, %eax
sarl $0x5, %ecx
addl %eax, %ecx
movswl %cx, %ecx
movabsq $-0x7777777777777777, %rdx # imm = 0x8888888888888889
movq %r8, %rax
imulq %rdx
addq %r8, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
subl %eax, %r8d
leaq 0x49d7(%rip), %rsi      # 0xe0b1
movl %r9d, %edx
xorl %eax, %eax
callq 0x32a0
cmpl $0x0, 0xae45(%rip)      # 0x14530
je 0x9754
imull $0x3e8, 0x8bcd(%rip), %edx # imm = 0x3E8
movl 0xaf83(%rip), %ecx      # 0x14680
xorl %eax, %eax
cmpl %edx, %ecx
jle 0x978c
cmpl %edx, 0xaf77(%rip)      # 0x14684
jle 0x978c
movl 0x8bb3(%rip), %eax      # 0x122c8
testl %eax, %eax
je 0x9723
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cmpl %eax, %ecx
jg 0x978e
leaq 0x988e(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movslq %ecx, %rax
imulq $0x10624dd3, %rax, %rdx # imm = 0x10624DD3
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x26, %rdx
addl %eax, %edx
leaq 0x4a6d(%rip), %rsi      # 0xe1b8
xorl %eax, %eax
callq 0x32a0
jmp 0x9774
leaq 0x985d(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x496a(%rip), %rdi      # 0xe0cf
movl $0x21, %esi
movl $0x1, %edx
callq 0x34a0
callq 0x90cc
xorl %eax, %eax
movl %eax, 0xaf03(%rip)      # 0x14684
movl %eax, 0xaef1(%rip)      # 0x14678
movl $0x1, %eax
popq %rbx
retq
movslq %ecx, %rax
imulq $0x10624dd3, %rax, %rdx # imm = 0x10624DD3
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x26, %rdx
addl %eax, %edx
cmpl $0x5, 0xaedc(%rip)      # 0x14688
leaq 0x9805(%rip), %rbx      # 0x12fb8
movq (%rbx), %rdi
jl 0x9807
leaq 0x4932(%rip), %rsi      # 0xe0f1
xorl %eax, %eax
callq 0x32a0
movl $0x1, %edi
callq 0xa03e
movl $0x0, 0xaea6(%rip)      # 0x14680
leaq 0x4a68(%rip), %rdi      # 0xe249
callq 0x30c0
leaq 0x4949(%rip), %rdi      # 0xe136
callq 0x3150
movq (%rbx), %rdi
leaq 0x4941(%rip), %rsi      # 0xe13d
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
jmp 0x981f
leaq 0x497c(%rip), %rsi      # 0xe18a
xorl %eax, %eax
callq 0x32a0
movl $0x1, %edi
callq 0xa03e
xorl %edi, %edi
callq 0x3490
nop