// File: /workspace/github_c_makefile_star2_O3/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 0x3310
movq %rax, %rbx
movl 0xb2be(%rip), %edi      # 0x14674
testl %edi, %edi
jle 0x9400
xorl %esi, %esi
movl $0x1, %edx
callq 0x3410
cmpl $-0x1, %eax
je 0x93dc
cmpl 0xb2a3(%rip), %eax      # 0x14674
je 0x93dc
leaq 0x4c38(%rip), %rsi      # 0xe012
jmp 0x93ed
movl $0x0, 0xb28e(%rip)      # 0x14674
leaq 0x4c0a(%rip), %rsi      # 0xdff7
leaq 0x9bc4(%rip), %rcx      # 0x12fb8
movq (%rcx), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
movl 0xb272(%rip), %ecx      # 0x14678
leal 0x1(%rcx), %eax
movl %eax, 0xb269(%rip)      # 0x14678
testl %ebp, %ebp
jne 0x9424
movl %ebx, %eax
subl 0xb251(%rip), %eax      # 0x1466c
cmpl $0x3, %eax
jge 0x94ac
testl %ebp, %ebp
sete %dl
movl 0xb24d(%rip), %esi      # 0x1467c
movl $0x3e3, %eax            # imm = 0x3E3
cmpl %eax, %esi
cmovll %esi, %eax
addl $0x5, %eax
movl %eax, 0xb23a(%rip)      # 0x1467c
cmpl $0x3, %ecx
setl %cl
orb %dl, %cl
jne 0x94c5
cmpb $0x0, 0xb21d(%rip)      # 0x14670
jne 0x95fd
movl %ebx, %eax
subl 0xb20b(%rip), %eax      # 0x1466c
cmpl $0x2, %eax
jg 0x95fd
leaq 0x9c2f(%rip), %rax      # 0x130a0
cmpb $0x0, (%rax)
je 0x95f6
cmpl $0x0, 0xb1f3(%rip)      # 0x14674
leaq 0x9b30(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
jle 0x95b1
leaq 0x4ba4(%rip), %rdi      # 0xe03c
movl $0x27, %esi
movl $0x1, %edx
callq 0x34a0
jmp 0x95fd
movl 0xb1ca(%rip), %ecx      # 0x1467c
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
decl %eax
movl %eax, 0xb1b7(%rip)      # 0x1467c
cmpb $0x1, 0xb1a4(%rip)      # 0x14670
jne 0x9603
movl $0x1f4, %ecx            # imm = 0x1F4
cmpl %ecx, %eax
cmovll %eax, %ecx
cmpl $0x1f, %ecx
movl $0x1e, %eax
cmovgel %ecx, %eax
addl $-0x1e, %eax
leaq 0x8dc3(%rip), %rcx      # 0x122b4
movl (%rcx), %ecx
leaq 0x8dbe(%rip), %rdx      # 0x122b8
movl (%rdx), %edx
subl %ecx, %edx
imull %eax, %edx
movslq %edx, %rax
imulq $0x10624dd3, %rax, %rdx # imm = 0x10624DD3
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x25, %rdx
addl %eax, %edx
addl %ecx, %edx
subl 0xb14c(%rip), %ebx      # 0x1466c
cmpl %edx, %ebx
jle 0x9603
leaq 0x9c41(%rip), %rax      # 0x13170
cmpb $0x0, (%rax)
je 0x95a8
cmpl $0x0, 0xb139(%rip)      # 0x14674
leaq 0x9a76(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
jle 0x9568
leaq 0x4b37(%rip), %rdi      # 0xe085
movl $0x2b, %esi
movl $0x1, %edx
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
jmp 0x34a0
leaq 0x4af5(%rip), %rsi      # 0xe064
movq %rcx, %rdi
xorl %eax, %eax
callq 0x32a0
leaq 0x9bf0(%rip), %rsi      # 0x13170
movq %rsp, %rbx
movl $0xc8, %edx
movq %rbx, %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x9307
movl %eax, 0xb0d6(%rip)      # 0x14674
movl $0x0, 0xb0d0(%rip)      # 0x14678
movb $0x0, 0xb0c1(%rip)      # 0x14670
jmp 0x9603
leaq 0x4a75(%rip), %rdi      # 0xe02d
movl $0xe, %esi
movl $0x1, %edx
callq 0x34a0
leaq 0x9ad2(%rip), %rsi      # 0x130a0
movq %rsp, %r14
movl $0xc8, %edx
movq %r14, %rdi
callq 0x3090
movq %r14, %rdi
callq 0x9307
movl %eax, 0xb088(%rip)      # 0x14674
movl $0x0, 0xb082(%rip)      # 0x14678
movb $0x1, 0xb073(%rip)      # 0x14670
movl %ebx, 0xb069(%rip)      # 0x1466c
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq