// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, 0x68(%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 (%rcx), %r13
movq 0x8(%rcx), %rax
movq %rax, 0x60(%rsp)
cmpq %rax, %r13
je 0x4322
movq %rdx, %rbx
movq %rsp, %r12
movq (%r13), %r14
movq 0x8(%r13), %rbp
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2350
testq %rbp, %rbp
je 0x42eb
xorl %r15d, %r15d
movzbl (%r14,%r15), %eax
cmpl $0x22, %eax
je 0x427a
cmpl $0x25, %eax
jne 0x4262
incq %r15
cmpq %rbp, %r15
jae 0x42d1
movzbl (%r14,%r15), %eax
cmpl $0x76, %eax
je 0x4284
movq %rbx, %rsi
cmpl $0x73, %eax
je 0x4258
movq 0x70(%rsp), %rsi
cmpl $0x6e, %eax
jne 0x4298
movq %r12, %rdi
callq 0x23e0
jmp 0x427a
movq 0x8(%rsp), %rsi
movsbl %al, %r8d
movl $0x1, %ecx
movq %r12, %rdi
xorl %edx, %edx
callq 0x23d0
incq %r15
cmpq %rbp, %r15
jb 0x4221
jmp 0x42eb
movq 0x68(%rsp), %rsi
testq %rsi, %rsi
je 0x427a
movq %r12, %rdi
callq 0x23e0
jmp 0x427a
movq 0x8(%rsp), %rsi
movl $0x1, %ecx
movq %r12, %rdi
xorl %edx, %edx
movl $0x25, %r8d
callq 0x23d0
movq (%rsp), %rax
movq 0x8(%rsp), %rsi
movsbl (%rax,%r15), %r8d
movl $0x1, %ecx
movq %r12, %rdi
xorl %edx, %edx
callq 0x23d0
jmp 0x427a
movq 0x8(%rsp), %rsi
movl $0x1, %ecx
movq %r12, %rdi
xorl %edx, %edx
movl $0x25, %r8d
callq 0x23d0
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x4f20
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4313
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2200
addq $0x10, %r13
cmpq 0x60(%rsp), %r13
jne 0x41eb
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r12
cmpq %r12, %r15
je 0x4354
leaq 0x20(%rsp), %rbx
movq %rsp, %r14
movq (%r15), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x51e4
addq $0x20, %r15
cmpq %r12, %r15
jne 0x4339
movq %rsp, %rsi
movq $0x0, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x51e4
callq 0x2070
cmpl $-0x1, %eax
je 0x446f
testl %eax, %eax
je 0x4463
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 0x4433
testb $0x7f, %al
jne 0x43e0
movq 0x5c46(%rip), %rdi      # 0x9ff0
leaq 0x25d5(%rip), %rsi      # 0x6986
movl $0x40, %edx
callq 0x2230
movzbl 0x1(%rsp), %esi
movq 0x5c29(%rip), %rdi      # 0x9ff0
callq 0x23a0
leaq 0x24ec(%rip), %rsi      # 0x68bf
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 0x4433
movq 0x5bf4(%rip), %rdi      # 0x9ff0
leaq 0x25c4(%rip), %rsi      # 0x69c7
movl $0x3d, %edx
callq 0x2230
movl (%rsp), %esi
andl $0x7f, %esi
movq 0x5bd6(%rip), %rdi      # 0x9ff0
callq 0x23a0
leaq 0x2499(%rip), %rsi      # 0x68bf
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x444a
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x40(%rsp), %rdi
callq 0x4f86
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
callq 0x2400
jmp 0x4433
leaq 0x24fc(%rip), %rdi      # 0x6972
callq 0x21a0
jmp 0x4433
jmp 0x4483
jmp 0x4483
jmp 0x448a
movq %rax, %rbx
jmp 0x44a8
jmp 0x448a
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x44a8
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x44bf
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x40(%rsp), %rdi
callq 0x4f86
movq %rbx, %rdi
callq 0x23b0