// File: /workspace/github_c_makefile_star2_O1/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 0xb449(%rip), %edi      # 0x14530
testl %edi, %edi
je 0x91c3
movl $0x9, %esi
callq 0x32d0
movq %rsp, %r14
movl $0x7b, (%r14)
xorl %edi, %edi
callq 0x3300
movq %rax, %rbx
movq %r14, %rdi
callq 0x34d0
leaq 0x9e9c(%rip), %r15      # 0x12fb8
movq (%r15), %rdi
movl (%r14), %edx
leaq 0x4d5e(%rip), %rsi      # 0xde87
movl %eax, %ecx
xorl %eax, %eax
callq 0x3290
xorl %edi, %edi
callq 0x3300
movq %rax, %r8
movq (%r15), %rdi
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
imulq $0xe10, %rdx, %rax      # imm = 0xE10
movq %r8, %rcx
subq %rax, %rcx
movslq %ecx, %r9
imulq $-0x77777777, %r9, %rax # imm = 0x88888889
shrq $0x20, %rax
addl %eax, %r9d
movl %r9d, %eax
shrl $0x1f, %eax
sarl $0x5, %r9d
addl %eax, %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 0x4cf5(%rip), %rsi      # 0xdeac
movl %r9d, %edx
xorl %eax, %eax
callq 0x3290
jmp 0x9205
xorl %ecx, %ecx
leaq 0x9e04(%rip), %rax      # 0x12fd0
cmpb $0x20, (%rax,%rcx)
je 0x91dd
incq %rcx
cmpq $0x50, %rcx
jne 0x91cc
jmp 0x9205
leaq 0x4cde(%rip), %rdx      # 0xdec2
leaq 0x9de5(%rip), %r8       # 0x12fd0
movq %rsp, %rbx
movl $0x63, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3190
movq %rbx, %rdi
callq 0x3150
leaq 0x9dac(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x4cbc(%rip), %rdi      # 0xded2
movl $0x19, %esi
movl $0x1, %edx
callq 0x3490
leaq 0x9da4(%rip), %r15      # 0x12fd0
leaq 0x4cb9(%rip), %rsi      # 0xdeec
movq %r15, %rdi
callq 0x34f0
movq %rax, %r14
leaq 0xb2fb(%rip), %rbx      # 0x14540
movl $0xc8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3090
movabsq $0x6974736970736172, %rax # imm = 0x6974736970736172
xorq 0xb2da(%rip), %rax      # 0x14540
movzwl 0xb2db(%rip), %ecx      # 0x14548
xorq $0x6c6c, %rcx           # imm = 0x6C6C
orq %rax, %rcx
jne 0x92ec
leaq 0x9710(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x92e5
xorl %eax, %eax
callq 0xa554
leaq 0xb2ad(%rip), %rdi      # 0x14540
movq %rax, %rsi
callq 0x3460
testq %r14, %r14
jne 0x930f
movq %rbx, %rdi
callq 0x3130
movslq %eax, %rdi
addq %rbx, %rdi
movsbl 0x900f(%rip), %ecx      # 0x122c4
leal 0x1(%rcx), %eax
movb %al, 0x9006(%rip)       # 0x122c4
leaq 0x4c64(%rip), %rsi      # 0xdf29
leaq 0x96e4(%rip), %rdx      # 0x129b0
xorl %eax, %eax
callq 0x3470
cmpb $0x7a, 0x8fea(%rip)     # 0x122c4
jl 0x92f1
movb $0x61, 0x8fe1(%rip)     # 0x122c4
jmp 0x92f1
testq %r14, %r14
jne 0x92f1
jmp 0x92a0
callq 0x37cb
leaq 0xb248(%rip), %rdi      # 0x14540
callq 0x9314
movl %eax, 0xb22d(%rip)      # 0x14530
xorl %eax, %eax
addq $0x70, %rsp
popq %rbx
popq %r14
popq %r15
retq
callq 0x37ed