// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl %edi, -0x4(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3300
movl %eax, -0x8(%rbp)
cmpl $0x0, 0xb1e3(%rip)      # 0x166f4
jle 0xb58e
movl 0xb1db(%rip), %edi      # 0x166f4
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x3400
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl 0xb1bb(%rip), %eax      # 0x166f4
je 0xb544
cmpl $-0x1, -0xd4(%rbp)
jne 0xb56e
movl $0x0, 0xb1a6(%rip)      # 0x166f4
leaq 0x9ac3(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl -0xd4(%rbp), %edx
leaq 0x5ab3(%rip), %rsi      # 0x11018
movb $0x0, %al
callq 0x3290
jmp 0xb58c
leaq 0x9aa3(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl -0xd4(%rbp), %edx
leaq 0x5aae(%rip), %rsi      # 0x11033
movb $0x0, %al
callq 0x3290
jmp 0xb58e
movl 0xb164(%rip), %eax      # 0x166f8
addl $0x1, %eax
movl %eax, 0xb15b(%rip)      # 0x166f8
cmpl $0x0, -0x4(%rbp)
jne 0xb5b1
movl -0x8(%rbp), %eax
subl 0xb140(%rip), %eax      # 0x166ec
cmpl $0x2, %eax
jg 0xb5d8
movl 0xb145(%rip), %eax      # 0x166fc
addl $0x5, %eax
movl %eax, 0xb13c(%rip)      # 0x166fc
cmpl $0x3e8, 0xb132(%rip)    # imm = 0x3E8
jle 0xb5d6
movl $0x3e8, 0xb126(%rip)    # imm = 0x3E8
jmp 0xb5fc
movl 0xb11e(%rip), %eax      # 0x166fc
subl $0x1, %eax
movl %eax, 0xb115(%rip)      # 0x166fc
cmpl $0x0, 0xb10e(%rip)      # 0x166fc
jge 0xb5fa
movl $0x0, 0xb102(%rip)      # 0x166fc
jmp 0xb5fc
cmpl $0x3, 0xb0f5(%rip)      # 0x166f8
jle 0xb6d3
cmpl $0x0, -0x4(%rbp)
je 0xb6d3
cmpl $0x0, 0xb0d6(%rip)      # 0x166f0
jne 0xb6c5
movl -0x8(%rbp), %eax
subl 0xb0c3(%rip), %eax      # 0x166ec
cmpl $0x2, %eax
jg 0xb6c5
leaq 0x9ac7(%rip), %rax      # 0x15100
cmpb $0x0, (%rax)
je 0xb6b9
cmpl $0x0, 0xb0af(%rip)      # 0x166f4
jg 0xb69f
leaq 0x99ca(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x59f6(%rip), %rsi      # 0x1104e
movb $0x0, %al
callq 0x3290
leaq -0xd0(%rbp), %rdi
leaq 0x9a93(%rip), %rsi      # 0x15100
movl $0xc8, %edx
callq 0x3090
leaq -0xd0(%rbp), %rdi
callq 0xb350
movl %eax, 0xb06b(%rip)      # 0x166f4
movl $0x0, 0xb065(%rip)      # 0x166f8
movl $0x1, 0xb053(%rip)      # 0x166f0
jmp 0xb6b7
leaq 0x9972(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x59ad(%rip), %rsi      # 0x1105d
movb $0x0, %al
callq 0x3290
jmp 0xb6c3
movl $0x1, 0xb02d(%rip)      # 0x166f0
jmp 0xb6c5
movl -0x8(%rbp), %eax
movl %eax, 0xb01e(%rip)      # 0x166ec
jmp 0xb81a
cmpl $0x0, 0xb016(%rip)      # 0x166f0
je 0xb818
movl 0xb016(%rip), %eax      # 0x166fc
movl %eax, -0xd8(%rbp)
cmpl $0x1f4, -0xd8(%rbp)     # imm = 0x1F4
jle 0xb702
movl $0x1f4, -0xd8(%rbp)     # imm = 0x1F4
movl -0xd8(%rbp), %eax
subl $0x1e, %eax
movl %eax, -0xd8(%rbp)
cmpl $0x0, -0xd8(%rbp)
jge 0xb724
movl $0x0, -0xd8(%rbp)
leaq 0x8b81(%rip), %rax      # 0x142ac
movl (%rax), %eax
movl %eax, -0xe0(%rbp)
leaq 0x8b76(%rip), %rax      # 0x142b0
movl (%rax), %eax
leaq 0x8b69(%rip), %rcx      # 0x142ac
subl (%rcx), %eax
imull -0xd8(%rbp), %eax
movl $0x1f4, %ecx            # imm = 0x1F4
cltd
idivl %ecx
movl %eax, %ecx
movl -0xe0(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xdc(%rbp)
movl -0x8(%rbp), %eax
subl 0xaf7f(%rip), %eax      # 0x166ec
cmpl -0xdc(%rbp), %eax
jle 0xb816
leaq 0x9a50(%rip), %rax      # 0x151d0
cmpb $0x0, (%rax)
je 0xb80a
cmpl $0x0, 0xaf64(%rip)      # 0x166f4
jg 0xb7f0
leaq 0x987f(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl -0xdc(%rbp), %edx
leaq 0x58dc(%rip), %rsi      # 0x11085
movb $0x0, %al
callq 0x3290
leaq -0xd0(%rbp), %rdi
leaq 0x9a12(%rip), %rsi      # 0x151d0
movl $0xc8, %edx
callq 0x3090
leaq -0xd0(%rbp), %rdi
callq 0xb350
movl %eax, 0xaf1a(%rip)      # 0x166f4
movl $0x0, 0xaf14(%rip)      # 0x166f8
movl $0x0, 0xaf02(%rip)      # 0x166f0
jmp 0xb808
leaq 0x9821(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x58a5(%rip), %rsi      # 0x110a6
movb $0x0, %al
callq 0x3290
jmp 0xb814
movl $0x0, 0xaedc(%rip)      # 0x166f0
jmp 0xb816
jmp 0xb818
jmp 0xb81a
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)