// File: /workspace/github_c_makefile_star2_O1/Matthias-Wandel[P]imgcomp/src/start_camera_prog.c
// Function: DoMotionRun
void DoMotionRun(int SawMotion)
{
    int NowSec = (int) time(NULL);
    
    static int LastMotion = 0;
    static int LightOn;
    static pid_t child_pid = 0;
    char CmdCopy[200];

    //fprintf(Log, "DoMotionRun %d %d %d\n",SawMotion, SinceLightChange, NowSec-LastMotion);

    if (child_pid > 0){
        // Check if child has exited.
        pid_t r = waitpid(child_pid, NULL, WNOHANG);
        if (r == child_pid || r == -1){
            child_pid = 0;
            fprintf(Log,"Motionrun Child exited %d\n",r);
        }else{
            fprintf(Log,"Child still running  r=%d\n",r);
        }
    }

    // Ignore "motion" events for a few seconds after we hit the light switch.
    SinceLightChange += 1;
    if (SawMotion || NowSec-LastMotion <= 2){
        MotionAccumulate += 5;
        if (MotionAccumulate > 1000) MotionAccumulate = 1000;
    }else{
        MotionAccumulate -= 1;
        if (MotionAccumulate < 0) MotionAccumulate = 0;
    }
    //printf("Ma = %4d\n",MotionAccumulate);    
        
    if (SinceLightChange > 3 && SawMotion){
		//if (!LightOn && NowSec - LastMotion > 2) fprintf(Log, "delayed light on\n");
        if (!LightOn && NowSec - LastMotion <= 2){
			// Want two motion events close together to avoid false triggers
			// which can happen on lighting changes or camera artifacts.
            if (lighton_run[0]){
                if (child_pid <= 0){
                    fprintf(Log, "Turn light ON\n");
                    strncpy(CmdCopy, lighton_run, 200);
                    child_pid = do_launch_program(CmdCopy);
                    SinceLightChange = 0;
                    LightOn = 1;
                }else{
                    fprintf(Log,"Turn lights ON (wait child exit first)\n");
                }
            }else{
                LightOn = 1;
            }
        }
        LastMotion = NowSec;
    }else if (LightOn){
        // Compute how long the light should be left on.
        int mm = MotionAccumulate; if (mm > 500) mm = 500;
        mm -= 30; if (mm < 0) mm = 0;
        
        int timeout = lightoff_min + (lightoff_max-lightoff_min)*mm/500;
        //printf("Ma=%4d  timeout=%d (%d-%d)\n",MotionAccumulate, timeout, lightoff_min,lightoff_max);
        
        if ((NowSec-LastMotion) > timeout){
            if (lightoff_run[0]){
                if (child_pid <= 0){
                    fprintf(Log, "Turn light OFF (%d sec timeout)\n",timeout);
                    strncpy(CmdCopy, lightoff_run, 200);
                    child_pid = do_launch_program(CmdCopy);
                    SinceLightChange = 0;
                    LightOn = 0;
                }else{
                    fprintf(Log,"Turn light OFF (wait for child exit first)\n");
                }
            }else{
                LightOn = 0;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0xd0, %rsp
movl %edi, %ebp
xorl %edi, %edi
callq 0x3300
movq %rax, %rbx
movl 0xb2b2(%rip), %edi      # 0x14674
testl %edi, %edi
jle 0x940c
xorl %esi, %esi
movl $0x1, %edx
callq 0x3400
cmpl $-0x1, %eax
je 0x93e8
cmpl 0xb297(%rip), %eax      # 0x14674
je 0x93e8
leaq 0x4bbc(%rip), %rsi      # 0xdfa2
jmp 0x93f9
movl $0x0, 0xb282(%rip)      # 0x14674
leaq 0x4b8e(%rip), %rsi      # 0xdf87
leaq 0x9bb8(%rip), %rcx      # 0x12fb8
movq (%rcx), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x3290
movl 0xb266(%rip), %eax      # 0x14678
leal 0x1(%rax), %ecx
movl %ecx, 0xb25d(%rip)      # 0x14678
testl %ebp, %ebp
jne 0x9430
movl %ebx, %ecx
subl 0xb245(%rip), %ecx      # 0x1466c
cmpl $0x2, %ecx
jg 0x9561
movl 0xb246(%rip), %edx      # 0x1467c
movl $0x3e3, %ecx            # imm = 0x3E3
cmpl %ecx, %edx
cmovll %edx, %ecx
addl $0x5, %ecx
testl %ebp, %ebp
sete %dl
movl %ecx, 0xb22e(%rip)      # 0x1467c
cmpl $0x3, %eax
setl %sil
movb 0xb215(%rip), %al       # 0x14670
orb %dl, %sil
jne 0x94bb
testb $0x1, %al
jne 0x95c5
movl %ebx, %eax
subl 0xb1fc(%rip), %eax      # 0x1466c
cmpl $0x2, %eax
jg 0x95c5
leaq 0x9c20(%rip), %rax      # 0x130a0
cmpb $0x0, (%rax)
je 0x95be
cmpl $0x0, 0xb1e4(%rip)      # 0x14674
leaq 0x9b21(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
jle 0x9579
leaq 0x4b25(%rip), %rdi      # 0xdfcc
movl $0x27, %esi
movl $0x1, %edx
callq 0x3490
jmp 0x95c5
testb $0x1, %al
je 0x95cb
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %ecx
cmovll %ecx, %eax
cmpl $0x1f, %eax
movl $0x1e, %ecx
cmovgel %eax, %ecx
addl $-0x1e, %ecx
leaq 0x8dca(%rip), %rax      # 0x122ac
movl (%rax), %eax
leaq 0x8dc5(%rip), %rdx      # 0x122b0
movl (%rdx), %edx
subl %eax, %edx
imull %ecx, %edx
movslq %edx, %rcx
imulq $0x10624dd3, %rcx, %rdx # imm = 0x10624DD3
movq %rdx, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rdx
addl %ecx, %edx
addl %eax, %edx
subl 0xb15b(%rip), %ebx      # 0x1466c
cmpl %edx, %ebx
jle 0x95cb
leaq 0x9c50(%rip), %rax      # 0x13170
cmpb $0x0, (%rax)
je 0x9617
cmpl $0x0, 0xb144(%rip)      # 0x14674
leaq 0x9a81(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
jle 0x95d7
leaq 0x4ace(%rip), %rdi      # 0xe015
movl $0x2b, %esi
movl $0x1, %edx
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
jmp 0x3490
movl 0xb115(%rip), %edx      # 0x1467c
cmpl $0x2, %edx
movl $0x1, %ecx
cmovgel %edx, %ecx
decl %ecx
jmp 0x9443
leaq 0x4a3d(%rip), %rdi      # 0xdfbd
movl $0xe, %esi
movl $0x1, %edx
callq 0x3490
leaq 0x9b0a(%rip), %rsi      # 0x130a0
movq %rsp, %r14
movl $0xc8, %edx
movq %r14, %rdi
callq 0x3090
movq %r14, %rdi
callq 0x9314
movl %eax, 0xb0c0(%rip)      # 0x14674
movl $0x0, 0xb0ba(%rip)      # 0x14678
movb $0x1, 0xb0ab(%rip)      # 0x14670
movl %ebx, 0xb0a1(%rip)      # 0x1466c
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x4a16(%rip), %rsi      # 0xdff4
movq %rcx, %rdi
xorl %eax, %eax
callq 0x3290
leaq 0x9b81(%rip), %rsi      # 0x13170
movq %rsp, %rbx
movl $0xc8, %edx
movq %rbx, %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x9314
movl %eax, 0xb067(%rip)      # 0x14674
movl $0x0, 0xb061(%rip)      # 0x14678
movb $0x0, 0xb052(%rip)      # 0x14670
jmp 0x95cb