// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/start_camera_prog.c
// Function: relaunch_camera_prog
int relaunch_camera_prog(void)
{
    // Kill raspistill or libcamera if it's already running.
    if (camera_prog_pid){
        kill(camera_prog_pid,SIGKILL);
        // If we launched the camera program (raspistill or libcamera), 
		// need to call wait() so that we dont' accumulate an army of child zombie processes
        int exit_code = 123;
        int a;
        time_t then, now = time(NULL);
        a = wait(&exit_code);
        fprintf(Log,"Child exit code %d, wait returned %d",exit_code, a);
        then = time(NULL);
        fprintf(Log," At %02d:%02d (%d s)\n",(int)(then%3600)/60, (int)(then%60), (int)(then-now));
    } else {
        // kill libcamera or raspistill if it was already launched when we started.
		// In that case, we don't have a PID for it.
		char KillCmd[100];
		for (int a=0;a<80;a++){
			// Search for first space to get capture command name.
			if (camera_prog_cmd[a] == ' '){ 
				snprintf(KillCmd, 99, "killall -9 %.*s",a,camera_prog_cmd);
				(void)system(KillCmd);
				break;
			}
		}
    }

    fprintf(Log,"Launching camera program\n");

    int DashOOption = (strstr(camera_prog_cmd, " -o ") != NULL);

    static char cmd_appended[300];
    strncpy(cmd_appended, camera_prog_cmd, 200);

    if (strncmp(cmd_appended, "raspistill", 10) == 0) { // check if the aquire cmd is actually a raspistill cmd
        if (ExposureManagementOn) { // Exposure managemnt by imgcomp
            strcat(cmd_appended, GetRaspistillExpParms());
            if (DashOOption){
                fprintf(stderr, "Must not specify -o option with -exm option\n");
                exit(-1);
            }
        }

        if (!DashOOption){
            // No output specified with raspistill command  Add the option,
            // with a different prefix each time so numbers don't overlap.
            int l = strlen(cmd_appended);
            sprintf(cmd_appended+l," -o %s/out%c%%05d.jpg",DoDirName, OutNameSeq++);
            if (OutNameSeq >= 'z') OutNameSeq = 'a';
            //fprintf(Log,"Run program: %s\n",cmd_appended);
        }
    }
    else {
        fprintf(stderr, "aquire_cmd was not raspistill, not setting output or exposure settings\n");
    }

    camera_prog_pid = do_launch_program(cmd_appended);
    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x70, %rsp
movl 0xbd71(%rip), %edi      # 0x14540
testl %edi, %edi
je 0x8863
pushq $0x9
popq %rsi
callq 0x32e0
movq %rsp, %r14
movl $0x7b, (%r14)
xorl %edi, %edi
callq 0x3310
movq %rax, %rbx
movq %r14, %rdi
callq 0x34f0
leaq 0xa7c6(%rip), %r15      # 0x12fc8
movq (%r15), %rdi
movl (%r14), %edx
leaq 0x56b8(%rip), %rsi      # 0xdec7
movl %eax, %ecx
xorl %eax, %eax
callq 0x32a0
xorl %edi, %edi
callq 0x3310
movq %rax, %r8
movq (%r15), %rdi
movl $0xe10, %ecx            # imm = 0xE10
cqto
idivq %rcx
movq %rdx, %rax
movw $0x3c, %cx
cwtd
idivw %cx
movswl %ax, %r9d
pushq $0x3c
popq %rcx
movq %r8, %rax
cqto
idivq %rcx
movq %rdx, %rcx
subl %ebx, %r8d
leaq 0x5695(%rip), %rsi      # 0xdeec
movl %r9d, %edx
xorl %eax, %eax
callq 0x32a0
jmp 0x88a3
xorl %ecx, %ecx
leaq 0xa774(%rip), %rax      # 0x12fe0
cmpq $0x50, %rcx
je 0x88a3
cmpb $0x20, (%rax,%rcx)
je 0x887d
incq %rcx
jmp 0x886c
leaq 0x567e(%rip), %rdx      # 0xdf02
leaq 0xa755(%rip), %r8       # 0x12fe0
movq %rsp, %rbx
pushq $0x63
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3190
movq %rbx, %rdi
callq 0x3150
leaq 0xa71e(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x565e(%rip), %rdi      # 0xdf12
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
leaq 0xa71a(%rip), %r15      # 0x12fe0
leaq 0x565f(%rip), %rsi      # 0xdf2c
movq %r15, %rdi
callq 0x3510
movq %rax, %r14
leaq 0xbc71(%rip), %rbx      # 0x14550
movl $0xc8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3090
leaq 0x563b(%rip), %rsi      # 0xdf31
pushq $0xa
popq %rdx
movq %rbx, %rdi
callq 0x33a0
testl %eax, %eax
jne 0x897c
leaq 0xa094(%rip), %rax      # 0x129a0
cmpl $0x0, (%rax)
je 0x8975
xorl %eax, %eax
callq 0x9bf8
leaq 0xbc31(%rip), %rdi      # 0x14550
movq %rax, %rsi
callq 0x3480
testq %r14, %r14
jne 0x89b6
movq %rbx, %rdi
callq 0x3130
movslq %eax, %rdi
addq %rbx, %rdi
movsbl 0x998f(%rip), %ecx      # 0x122d4
leal 0x1(%rcx), %eax
movb %al, 0x9986(%rip)       # 0x122d4
leaq 0x5614(%rip), %rsi      # 0xdf69
leaq 0xa064(%rip), %rdx      # 0x129c0
xorl %eax, %eax
callq 0x3490
cmpb $0x7a, 0x996a(%rip)     # 0x122d4
jl 0x8998
movb $0x61, 0x9961(%rip)     # 0x122d4
jmp 0x8998
testq %r14, %r14
jne 0x8998
jmp 0x8930
movq 0x9675(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x55f2(%rip), %rdi      # 0xdf7f
pushq $0x47
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
leaq 0xbbb1(%rip), %rdi      # 0x14550
callq 0x89da
movl %eax, 0xbb96(%rip)      # 0x14540
xorl %eax, %eax
addq $0x70, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x963b(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x5575(%rip), %rdi      # 0xdf3c
pushq $0x2c
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
pushq $-0x1
popq %rdi
callq 0x34a0