// File: davmac314[P]dinit/src/includes/service.h
// Function: service_set::unload_service(service_record*)
void unload_service(service_record *svc)
    {
        // Check if there are any "after" dependents, or any "before" dependencies. We need a placeholder
        // if so. Note that the placeholder is created before making any destructive changes, so if we fail
        // to create it no rollback is needed.
        placeholder_service *placeholder = nullptr;
        auto make_placeholder = [&]() {
            if (placeholder == nullptr) {
                std::unique_ptr<placeholder_service> ph { new placeholder_service(this, svc->get_name()) };
                add_service(ph.get());
                placeholder = ph.release();
            }
        };

        auto *consumer = svc->get_log_consumer();
        if (consumer != nullptr) {
            make_placeholder();
            placeholder->set_output_pipe(svc->transfer_output_pipe());
        }

        auto &svc_depts = svc->get_dependents();
        for (auto i = svc_depts.begin(); i != svc_depts.end(); ) {
            auto *dept = *i;
            if (dept->dep_type == dependency_type::AFTER) {
                make_placeholder();
                dept->set_to(placeholder);
                auto &ph_depts = placeholder->get_dependents();
                ph_depts.splice(ph_depts.end(), svc_depts, i++);
                continue;
            }
            ++i;
        }

        auto &svc_deps = svc->get_dependencies();
        for (auto i = svc_deps.begin(); i != svc_deps.end(); ) {
            auto &dep = *i;
            if (dep.dep_type == dependency_type::BEFORE) {
                make_placeholder();
                dep.set_from(placeholder);
                auto &ph_deps = placeholder->get_dependencies();
                ph_deps.splice(ph_deps.end(), svc_deps, i++);
                continue;
            }
            ++i;
        }

        // Proceed with unload.
        svc->prepare_for_unload();
        remove_service(svc);
        delete svc;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq $0x0, -0x18(%rbp)
leaq -0x18(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2f020
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x5ec30
leaq -0x30(%rbp), %rdi
callq 0x60970
movq -0x18(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movq -0xe0(%rbp), %rdi
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
callq 0x60a60
movq -0x10(%rbp), %rdi
callq 0x30220
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x304d0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x30530
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x30500
testb $0x1, %al
jne 0x5ec6d
jmp 0x5ed2e
leaq -0x50(%rbp), %rdi
callq 0x30590
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpl $0x5, 0x14(%rax)
jne 0x5ed20
leaq -0x30(%rbp), %rdi
callq 0x60970
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x30400
movq -0x18(%rbp), %rdi
callq 0x30220
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x68(%rbp), %rdi
callq 0x30530
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x30680
movq -0x48(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x4e1c0
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x30680
movq -0xf0(%rbp), %rdi
movq -0xe8(%rbp), %rdx
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rcx
callq 0x60a90
jmp 0x5ec4a
leaq -0x50(%rbp), %rdi
callq 0x306a0
jmp 0x5ec4a
movq -0x10(%rbp), %rdi
callq 0x30440
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x30240
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
callq 0x30270
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x302a0
testb $0x1, %al
jne 0x5ed80
jmp 0x5ee6e
leaq -0x98(%rbp), %rdi
callq 0x302d0
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpl $0x4, 0x14(%rax)
jne 0x5ee5d
leaq -0x30(%rbp), %rdi
callq 0x60970
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x306c0
movq -0x18(%rbp), %rdi
callq 0x30440
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x30270
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x304b0
movq -0x90(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
callq 0x306e0
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x304b0
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rdx
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rcx
callq 0x30460
jmp 0x5ed51
leaq -0x98(%rbp), %rdi
callq 0x30330
jmp 0x5ed51
movq -0x10(%rbp), %rdi
callq 0x4b8b0
movq -0xd8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2f4a0
movq -0x10(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, %rax
je 0x5eea5
movq -0x108(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop