// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x8(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3300
movq %rax, -0x18(%rbp)
movl 0xaeb2(%rip), %eax      # 0x16700
addl $0x3e8, %eax            # imm = 0x3E8
movl %eax, 0xaea7(%rip)      # 0x16700
movl 0xaea5(%rip), %eax      # 0x16704
addl $0x3e8, %eax            # imm = 0x3E8
movl %eax, 0xae9a(%rip)      # 0x16704
cmpl $0x0, -0x8(%rbp)
jle 0xb88b
movl $0x0, 0xae86(%rip)      # 0x16700
movl -0x8(%rbp), %eax
addl 0xae85(%rip), %eax      # 0x16708
movl %eax, 0xae7f(%rip)      # 0x16708
jmp 0xb8ee
cmpl $0xbb8, 0xae6b(%rip)    # imm = 0xBB8
jl 0xb8ec
leaq 0x977a(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl 0xae59(%rip), %eax      # 0x16700
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl $0xe10, %ecx            # imm = 0xE10
cqto
idivq %rcx
movq %rdx, %rax
movl $0x3c, %ecx
cqto
idivq %rcx
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl $0x3c, %esi
cqto
idivq %rsi
movq %rdx, %rax
movl -0x20(%rbp), %edx
movl %eax, %r8d
leaq 0x57ed(%rip), %rsi      # 0x110d2
movb $0x0, %al
callq 0x3290
jmp 0xb8ee
cmpl $0x0, 0xacbb(%rip)      # 0x165b0
jne 0xb914
leaq 0x971a(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x57e8(%rip), %rsi      # 0x110f0
movb $0x0, %al
callq 0x3290
jmp 0xba56
imull $0x3e8, 0x89fa(%rip), %eax # imm = 0x3E8
movl %eax, -0xc(%rbp)
movl 0xadd9(%rip), %eax      # 0x16700
cmpl -0xc(%rbp), %eax
jle 0xba4d
movl 0xadce(%rip), %eax      # 0x16704
cmpl -0xc(%rbp), %eax
jle 0xba4b
cmpl $0x0, 0x89d6(%rip)      # 0x1431c
je 0xba21
movl 0xadae(%rip), %eax      # 0x16700
imull $0x3e8, 0x89c0(%rip), %ecx # imm = 0x3E8
cmpl %ecx, %eax
jle 0xba21
cmpl $0x5, 0xad9d(%rip)      # 0x16708
jl 0xb9e8
leaq 0x96a4(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl 0xad83(%rip), %eax      # 0x16700
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, %edx
leaq 0x5784(%rip), %rsi      # 0x11112
movb $0x0, %al
callq 0x3290
movl $0x1, %edi
callq 0xc6d0
movl $0x0, 0xad57(%rip)      # 0x16700
leaq 0x57a7(%rip), %rdi      # 0x11157
movb $0x0, %al
callq 0x3170
leaq 0x5954(%rip), %rdi      # 0x11312
callq 0x3150
movl %eax, -0x1c(%rbp)
leaq 0x964b(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0x5789(%rip), %rsi      # 0x11163
movb $0x0, %al
callq 0x3290
xorl %edi, %edi
callq 0x3480
leaq 0x9629(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl 0xad08(%rip), %eax      # 0x16700
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, %edx
leaq 0x57a7(%rip), %rsi      # 0x111b0
movb $0x0, %al
callq 0x3290
movl $0x1, %edi
callq 0xc6d0
xorl %edi, %edi
callq 0x3480
leaq 0x95f0(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl 0xaccf(%rip), %eax      # 0x16700
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, %edx
leaq 0x579c(%rip), %rsi      # 0x111de
movb $0x0, %al
callq 0x3290
jmp 0xba56
jmp 0xba4d
movl $0x0, -0x4(%rbp)
jmp 0xba8a
callq 0xb0a0
movl $0x0, 0xac9f(%rip)      # 0x16704
movl $0x0, 0xac9d(%rip)      # 0x1670c
movl $0x0, 0xac97(%rip)      # 0x16710
movl $0x0, 0xac75(%rip)      # 0x166f8
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)