// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/dinit-monitor.cc
// Function: issue_command(char const*, char const*, char const*, std::vector<stringview, std::allocator<stringview>>&, bool)
static void issue_command(const char* name, const char* value, const char* event, std::vector<stringview> &command_parts, bool is_env) {
    std::vector<std::string> final_cmd_parts;
    std::vector<const char *> final_cmd_parts_cstr;

    for (stringview cmd_part : command_parts) {
        std::string cmd_part_str;
        cmd_part_str.reserve(cmd_part.len);

        for (size_t i = 0; i < cmd_part.len; ++i) {
            if (cmd_part.str[i] == '%') {
                ++i;
                if (i >= cmd_part.len) {
                    cmd_part_str.append(1, '%');
                    break;
                }
                if (cmd_part.str[i] == 'n') {
                    cmd_part_str.append(name);
                }
                else if (cmd_part.str[i] == 'v') {
                    if (value) cmd_part_str.append(value);
                }
                else if (cmd_part.str[i] == 's') {
                    cmd_part_str.append(event);
                }
                else {
                    // invalid specifier, just output as is
                    cmd_part_str.append(1, '%');
                    cmd_part_str.append(1, cmd_part_str[i]);
                }
            }
            else if (cmd_part.str[i] == '"') {
                // consume.
            }
            else {
                cmd_part_str.append(1, cmd_part.str[i]);
            }
        }

        final_cmd_parts.emplace_back(std::move(cmd_part_str));
    }

    for (const std::string &cmd_part : final_cmd_parts) {
        final_cmd_parts_cstr.push_back(cmd_part.c_str());
    }
    final_cmd_parts_cstr.push_back(nullptr);

    pid_t child_pid = fork();
    if (child_pid == 0) {
        // we are the child
        char **argv = const_cast<char **>(final_cmd_parts_cstr.data());
        execvp(argv[0], argv);
        perror("dinit-monitor: exec");
    }
    else if (child_pid == -1) {
        perror("dinit-monitor: fork");
    }
    else {
        int wstatus;
        if (wait(&wstatus) != -1) {
            if (wstatus != 0) {
                if (WIFEXITED(wstatus)) {
                    std::cerr << "dinit-monitor: notification command terminated with exit status "
                            << WEXITSTATUS(wstatus) << "\n";
                }
                if (WIFSIGNALED(wstatus)) {
                    std::cerr << "dinit-monitor: notification command terminated due to signal "
                            << WTERMSIG(wstatus) << "\n";
                }
            }
        }

        // Don't bother clearing any pending SIGCHLD. POSIX says that:
        // - either SIGCHLD doesn't queue, in which case we're only leaving one pending signal
        // - or, it does queue, but wait() removes it from the queue.
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x70(%rsp)
movq %rsi, %rbx
movq %rdi, 0x68(%rsp)
leaq 0x40(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
andq $0x0, 0x30(%rsp)
movaps %xmm0, (%rax)
movaps %xmm0, 0x20(%rsp)
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, 0x58(%rsp)
movq %rsp, %r13
pushq $0x1
popq %rbp
cmpq 0x58(%rsp), %rax
je 0x38ae
movq (%rax), %r15
movq %rax, 0x60(%rsp)
movq 0x8(%rax), %r12
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
andq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2380
xorl %r14d, %r14d
cmpq %r12, %r14
jae 0x388b
movzbl (%r15,%r14), %eax
cmpl $0x22, %eax
je 0x3842
cmpl $0x25, %eax
jne 0x3834
incq %r14
cmpq %r12, %r14
jae 0x387d
movzbl (%r15,%r14), %eax
cmpl $0x76, %eax
je 0x3847
movq 0x70(%rsp), %rsi
cmpl $0x73, %eax
je 0x382a
movq 0x68(%rsp), %rsi
cmpl $0x6e, %eax
jne 0x3859
movq %r13, %rdi
callq 0x23f0
jmp 0x3842
movsbl %al, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2240
incq %r14
jmp 0x37ec
testq %rbx, %rbx
je 0x3842
movq %rbx, %rsi
movq %r13, %rdi
callq 0x23f0
jmp 0x3842
movq %r13, %rdi
movq %rbp, %rsi
pushq $0x25
popq %rdx
callq 0x2240
movq (%rsp), %rax
movsbl (%rax,%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2240
jmp 0x3842
movq %r13, %rdi
movq %rbp, %rsi
pushq $0x25
popq %rdx
callq 0x2240
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x4316
movq %r13, %rdi
callq 0x2100
movq 0x60(%rsp), %rax
addq $0x10, %rax
jmp 0x37b3
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r12
leaq 0x20(%rsp), %rbx
movq %rsp, %r14
cmpq %r12, %r15
je 0x38dd
movq (%r15), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4516
addq $0x20, %r15
jmp 0x38c0
movq %rsp, %rsi
andq $0x0, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x4516
callq 0x2070
cmpl $-0x1, %eax
je 0x39b5
testl %eax, %eax
je 0x399f
movq %rsp, %rdi
callq 0x22a0
cmpl $-0x1, %eax
setne %cl
movl (%rsp), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x39c1
testb $0x7f, %al
jne 0x395b
movq 0x66c0(%rip), %rdi      # 0x9ff0
leaq 0x303a(%rip), %rsi      # 0x6971
callq 0x2210
movzbl 0x1(%rsp), %esi
movq %rax, %rdi
callq 0x23c0
leaq 0x2f5a(%rip), %rsi      # 0x68aa
movq %rax, %rdi
callq 0x2210
movl (%rsp), %eax
andl $0x7f, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x39c1
movq 0x667c(%rip), %rdi      # 0x9ff0
leaq 0x3037(%rip), %rsi      # 0x69b2
callq 0x2210
movl (%rsp), %esi
andl $0x7f, %esi
movq %rax, %rdi
callq 0x23c0
leaq 0x2f15(%rip), %rsi      # 0x68aa
movq %rax, %rdi
callq 0x2210
jmp 0x39c1
movq 0x20(%rsp), %rsi
movq (%rsi), %rdi
callq 0x2180
leaq 0x2f96(%rip), %rdi      # 0x6949
jmp 0x39bc
leaq 0x2fa1(%rip), %rdi      # 0x695d
callq 0x21c0
leaq 0x20(%rsp), %rdi
callq 0x4ae6
leaq 0x40(%rsp), %rdi
callq 0x4352
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x39ea
jmp 0x39ea
jmp 0x39f1
movq %rax, %rbx
jmp 0x39fc
jmp 0x39f1
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2100
leaq 0x20(%rsp), %rdi
callq 0x4ae6
leaq 0x40(%rsp), %rdi
callq 0x4352
movq %rbx, %rdi
callq 0x23d0