// File: /workspace/github_c_makefile_star2_O1/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 0x3300
movq %rax, %r8
movl 0xb04d(%rip), %eax      # 0x14680
movl $0x3e8, %edx            # imm = 0x3E8
leal (%rax,%rdx), %ecx
movl %ecx, 0xb03f(%rip)      # 0x14680
addl %edx, 0xb03d(%rip)      # 0x14684
testl %ebx, %ebx
jle 0x9660
movl $0x0, 0xb02b(%rip)      # 0x14680
addl %ebx, 0xb02d(%rip)      # 0x14688
jmp 0x96f0
cmpl $0x7d0, %eax            # imm = 0x7D0
jl 0x96f0
leaq 0x9946(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movabsq $0x48d159e26af37c05, %rdx # imm = 0x48D159E26AF37C05
movq %r8, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
imulq $0xe10, %rdx, %rax      # imm = 0xE10
movq %r8, %r9
subq %rax, %r9
movabsq $-0x7777777777777777, %rsi # imm = 0x8888888888888889
movq %r9, %rax
imulq %rsi
addq %rdx, %r9
movq %r9, %rax
shrq $0x3f, %rax
shrq $0x5, %r9
addl %eax, %r9d
movq %r8, %rax
imulq %rsi
addq %r8, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
subl %eax, %r8d
leaq 0x495d(%rip), %rsi      # 0xe041
movl %ecx, %edx
movl %r9d, %ecx
xorl %eax, %eax
callq 0x3290
cmpl $0x0, 0xae39(%rip)      # 0x14530
je 0x9760
imull $0x3e8, 0x8bb9(%rip), %edx # imm = 0x3E8
movl 0xaf77(%rip), %ecx      # 0x14680
xorl %eax, %eax
cmpl %edx, %ecx
jle 0x9798
cmpl %edx, 0xaf6b(%rip)      # 0x14684
jle 0x9798
movl 0x8b9f(%rip), %eax      # 0x122c0
testl %eax, %eax
je 0x972f
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cmpl %eax, %ecx
jg 0x979a
leaq 0x9882(%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 0x49f1(%rip), %rsi      # 0xe148
xorl %eax, %eax
callq 0x3290
jmp 0x9780
leaq 0x9851(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x48ee(%rip), %rdi      # 0xe05f
movl $0x21, %esi
movl $0x1, %edx
callq 0x3490
callq 0x90d8
xorl %eax, %eax
movl %eax, 0xaef7(%rip)      # 0x14684
movl %eax, 0xaee5(%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, 0xaed0(%rip)      # 0x14688
leaq 0x97f9(%rip), %rbx      # 0x12fb8
movq (%rbx), %rdi
jl 0x9813
leaq 0x48b6(%rip), %rsi      # 0xe081
xorl %eax, %eax
callq 0x3290
movl $0x1, %edi
callq 0xa043
movl $0x0, 0xae9a(%rip)      # 0x14680
leaq 0x49ec(%rip), %rdi      # 0xe1d9
callq 0x30c0
leaq 0x48cd(%rip), %rdi      # 0xe0c6
callq 0x3150
movq (%rbx), %rdi
leaq 0x48c5(%rip), %rsi      # 0xe0cd
movl %eax, %edx
xorl %eax, %eax
callq 0x3290
jmp 0x982b
leaq 0x4900(%rip), %rsi      # 0xe11a
xorl %eax, %eax
callq 0x3290
movl $0x1, %edi
callq 0xa043
xorl %edi, %edi
callq 0x3480
nop