// File: /workspace/github_cpp_makefile_star2_O3/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 %rdi, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %r8, 0x60(%rsp)
cmpq %r8, %rcx
je 0x41cb
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x68(%rsp)
movq %rsp, %rbp
movq (%r14), %r12
movq 0x8(%r14), %rbx
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2350
testq %rbx, %rbx
je 0x4162
xorl %r13d, %r13d
movzbl (%r12,%r13), %eax
cmpl $0x22, %eax
je 0x40f1
cmpl $0x25, %eax
jne 0x40d9
incq %r13
cmpq %rbx, %r13
jae 0x4148
movzbl (%r12,%r13), %eax
cmpl $0x76, %eax
je 0x40fb
movq %r15, %rsi
cmpl $0x73, %eax
je 0x40cf
movq 0x70(%rsp), %rsi
cmpl $0x6e, %eax
jne 0x410f
movq %rbp, %rdi
callq 0x23e0
jmp 0x40f1
movq 0x8(%rsp), %rsi
movsbl %al, %r8d
movl $0x1, %ecx
movq %rbp, %rdi
xorl %edx, %edx
callq 0x23d0
incq %r13
cmpq %rbx, %r13
jb 0x4098
jmp 0x4162
movq 0x68(%rsp), %rsi
testq %rsi, %rsi
je 0x40f1
movq %rbp, %rdi
callq 0x23e0
jmp 0x40f1
movq 0x8(%rsp), %rsi
movl $0x1, %ecx
movq %rbp, %rdi
xorl %edx, %edx
movl $0x25, %r8d
callq 0x23d0
movq (%rsp), %rax
movq 0x8(%rsp), %rsi
movsbl (%rax,%r13), %r8d
movl $0x1, %ecx
movq %rbp, %rdi
xorl %edx, %edx
callq 0x23d0
jmp 0x40f1
movq 0x8(%rsp), %rsi
movl $0x1, %ecx
movq %rbp, %rdi
xorl %edx, %edx
movl $0x25, %r8d
callq 0x23d0
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x4cfc
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x418a
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2200
addq $0x10, %r14
cmpq 0x60(%rsp), %r14
jne 0x4063
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r12
cmpq %r12, %r15
je 0x41cb
leaq 0x20(%rsp), %rbx
movq %rsp, %r14
movq (%r15), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4fca
addq $0x20, %r15
cmpq %r12, %r15
jne 0x41b0
movq %rsp, %rsi
movq $0x0, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x4fca
callq 0x2070
cmpl $-0x1, %eax
je 0x42e6
testl %eax, %eax
je 0x42da
movq %rsp, %rdi
callq 0x2280
cmpl $-0x1, %eax
setne %cl
movl (%rsp), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x42aa
testb $0x7f, %al
jne 0x425a
movq 0x4dcf(%rip), %rdi      # 0x8ff0
leaq 0x2749(%rip), %rsi      # 0x6971
movl $0x40, %edx
callq 0x2230
movzbl 0x1(%rsp), %esi
movq 0x4db2(%rip), %rdi      # 0x8ff0
callq 0x23a0
leaq 0x2660(%rip), %rsi      # 0x68aa
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movl (%rsp), %eax
andl $0x7f, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x42aa
movq 0x4d7d(%rip), %rdi      # 0x8ff0
leaq 0x2738(%rip), %rsi      # 0x69b2
movl $0x3d, %edx
callq 0x2230
movl (%rsp), %esi
andl $0x7f, %esi
movq 0x4d5f(%rip), %rdi      # 0x8ff0
callq 0x23a0
leaq 0x260d(%rip), %rsi      # 0x68aa
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x42c1
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x40(%rsp), %rdi
callq 0x4d6a
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
callq 0x2400
jmp 0x42aa
leaq 0x2670(%rip), %rdi      # 0x695d
callq 0x21a0
jmp 0x42aa
jmp 0x42fa
jmp 0x42fa
jmp 0x4301
movq %rax, %rbx
jmp 0x431f
jmp 0x4301
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x431f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4336
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x40(%rsp), %rdi
callq 0x4d6a
movq %rbx, %rdi
callq 0x23b0