// File: /workspace/github_c_makefile_star2_O3/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 0xb455(%rip), %edi      # 0x14530
testl %edi, %edi
je 0x91b6
movl $0x9, %esi
callq 0x32e0
movq %rsp, %r14
movl $0x7b, (%r14)
xorl %edi, %edi
callq 0x3310
movq %rax, %rbx
movq %r14, %rdi
callq 0x34e0
leaq 0x9ea8(%rip), %r15      # 0x12fb8
movq (%r15), %rdi
movl (%r14), %edx
leaq 0x4dda(%rip), %rsi      # 0xdef7
movl %eax, %ecx
xorl %eax, %eax
callq 0x32a0
xorl %edi, %edi
callq 0x3310
movq %rax, %r8
movq (%r15), %rdi
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0xa, %rdx
addl %eax, %edx
imull $0xe10, %edx, %eax      # imm = 0xE10
movl %r8d, %ecx
subl %eax, %ecx
movswl %cx, %eax
imull $0xffff8889, %eax, %eax # imm = 0xFFFF8889
shrl $0x10, %eax
addl %ecx, %eax
movzwl %ax, %eax
movswl %ax, %ecx
shrl $0xf, %eax
sarl $0x5, %ecx
addl %eax, %ecx
movswl %cx, %r9d
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
movq %r8, %rax
imulq %rcx
addq %r8, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
movl %r8d, %ecx
subl %eax, %ecx
subl %ebx, %r8d
leaq 0x4d72(%rip), %rsi      # 0xdf1c
movl %r9d, %edx
xorl %eax, %eax
callq 0x32a0
jmp 0x91f8
xorl %ecx, %ecx
leaq 0x9e11(%rip), %rax      # 0x12fd0
cmpb $0x20, (%rax,%rcx)
je 0x91d0
incq %rcx
cmpq $0x50, %rcx
jne 0x91bf
jmp 0x91f8
leaq 0x4d5b(%rip), %rdx      # 0xdf32
leaq 0x9df2(%rip), %r8       # 0x12fd0
movq %rsp, %rbx
movl $0x63, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3190
movq %rbx, %rdi
callq 0x3150
leaq 0x9db9(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x4d39(%rip), %rdi      # 0xdf42
movl $0x19, %esi
movl $0x1, %edx
callq 0x34a0
leaq 0x9db1(%rip), %r15      # 0x12fd0
leaq 0x4d36(%rip), %rsi      # 0xdf5c
movq %r15, %rdi
callq 0x3500
movq %rax, %r14
leaq 0xb308(%rip), %rbx      # 0x14540
movl $0xc8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3090
movabsq $0x6974736970736172, %rax # imm = 0x6974736970736172
xorq 0xb2e7(%rip), %rax      # 0x14540
movzwl 0xb2e8(%rip), %ecx      # 0x14548
xorq $0x6c6c, %rcx           # imm = 0x6C6C
orq %rax, %rcx
jne 0x92df
leaq 0x971d(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x92d8
xorl %eax, %eax
callq 0xa53c
leaq 0xb2ba(%rip), %rdi      # 0x14540
movq %rax, %rsi
callq 0x3470
testq %r14, %r14
jne 0x9302
movq %rbx, %rdi
callq 0x3130
movslq %eax, %rdi
addq %rbx, %rdi
movsbl 0x9024(%rip), %ecx      # 0x122cc
leal 0x1(%rcx), %eax
movb %al, 0x901b(%rip)       # 0x122cc
leaq 0x4ce1(%rip), %rsi      # 0xdf99
leaq 0x96f1(%rip), %rdx      # 0x129b0
xorl %eax, %eax
callq 0x3480
cmpb $0x7a, 0x8fff(%rip)     # 0x122cc
jl 0x92e4
movb $0x61, 0x8ff6(%rip)     # 0x122cc
jmp 0x92e4
testq %r14, %r14
jne 0x92e4
jmp 0x9293
callq 0x37db
leaq 0xb255(%rip), %rdi      # 0x14540
callq 0x9307
movl %eax, 0xb23a(%rip)      # 0x14530
xorl %eax, %eax
addq $0x70, %rsp
popq %rbx
popq %r14
popq %r15
retq
callq 0x37fd