// File: davmac314[P]dinit/src/load-service.cc
// Function: update_depenencies(service_record*, dinit_load::service_settings_wrapper<prelim_dep>&, std::__cxx11::list<service_dep, std::allocator<service_dep>>&)
static void update_depenencies(service_record *service,
        dinit_load::service_settings_wrapper<prelim_dep> &settings,
        std::list<service_dep> &before_deps)
{
    check_cycle(settings.depends, service);

    std::list<service_dep> &deps = service->get_dependencies();
    auto first_preexisting = deps.begin();

    auto &depts = service->get_dependents();
    auto first_pre_dept = depts.begin();

    try {
        // Insert all new dependents (from "before" relationships) before the first pre-existing dependent
        for (auto new_dept_i = before_deps.begin(); new_dept_i != before_deps.end(); ) {
            auto &new_dept = *new_dept_i;
            depts.insert(depts.begin(), &new_dept);
            // splice the dependency into the dependent:
            auto next_dept_i = std::next(new_dept_i);
            auto &from_deps = new_dept.get_from()->get_dependencies();
            from_deps.splice(from_deps.begin(), before_deps, new_dept_i);
            new_dept_i = next_dept_i;
        }

        // Insert all the new dependencies before the first pre-existing dependency
        for (auto i = settings.depends.begin(); i != settings.depends.end(); ) {
            auto &new_dep = *i;
            service->add_dep(new_dep.to, new_dep.dep_type, first_preexisting);
            i = settings.depends.erase(i);
        }
    }
    catch (...) {
        // remove "before" dependencies from dependents
        for (auto i = depts.begin(); i != first_pre_dept; ) {
            auto next_i = std::next(i);
            (*i)->get_from()->rm_dep(**i);
            i = next_i;
        }

        // remove the inserted dependencies
        for (auto i = deps.begin(); i != first_preexisting; ) {
            i = service->rm_dep(i);
        }

        // re-throw the exception
        throw;
    }

    // Now remove all pre-existing dependencies, except for "before" dependencies (which come from the
    // linked service and so must be retained)
    for( ; first_preexisting != deps.end(); ) {
        if (first_preexisting->dep_type != dependency_type::BEFORE) {
            first_preexisting = service->rm_dep(first_preexisting);
        }
        else {
            ++first_preexisting;
        }
    }

    // Also remove pre-existing "before" dependents (because they come from this service)
    for( ; first_pre_dept != depts.end(); ) {
        auto next_pre_dept = std::next(first_pre_dept);
        if ((*first_pre_dept)->dep_type == dependency_type::BEFORE) {
            (*first_pre_dept)->get_from()->rm_dep(**first_pre_dept);
        }
        first_pre_dept = next_pre_dept;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
addq $0xb8, %rdi
movq -0x8(%rbp), %rsi
callq 0x238d0
movq -0x8(%rbp), %rdi
callq 0x30440
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x30240
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x30220
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x304d0
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0x30240
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0x30270
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x302a0
testb $0x1, %al
jne 0x23abf
jmp 0x23cb5
leaq -0x40(%rbp), %rdi
callq 0x302d0
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x304d0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x30680
movq -0x150(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x432e0
movq %rax, -0x148(%rbp)
jmp 0x23b12
movq -0x148(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movl $0x1, %esi
callq 0x303b0
movq %rax, -0x158(%rbp)
jmp 0x23b42
movq -0x158(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x50(%rbp), %rdi
callq 0x30420
movq %rax, %rdi
callq 0x30440
movq %rax, -0x160(%rbp)
jmp 0x23b6a
movq -0x160(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x98(%rbp), %rdi
callq 0x30240
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x304b0
movq -0x18(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x304b0
movq -0x170(%rbp), %rdi
movq -0x168(%rbp), %rdx
movq -0xa0(%rbp), %rsi
movq -0xb0(%rbp), %rcx
callq 0x30460
movq -0x88(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x23a9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rdi
callq 0xa240
movq -0x30(%rbp), %rdi
callq 0x304d0
movq %rax, -0xe8(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x30500
testb $0x1, %al
jne 0x23c36
jmp 0x23da2
movq -0xe8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdi
movl $0x1, %esi
callq 0x30560
movq %rax, -0x178(%rbp)
jmp 0x23c5e
movq -0x178(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x30590
movq (%rax), %rdi
callq 0x30420
movq %rax, -0x180(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x30590
movq -0x180(%rbp), %rdi
movq (%rax), %rsi
callq 0x305b0
movq -0xf0(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x23c1d
movq -0x10(%rbp), %rdi
addq $0xb8, %rdi
callq 0x3c750
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
addq $0xb8, %rdi
callq 0x3c780
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3c7b0
testb $0x1, %al
jne 0x23cff
jmp 0x23d8d
leaq -0xb8(%rbp), %rdi
callq 0x3c7e0
movq %rax, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc8(%rbp), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq -0x28(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rcx
callq 0x43320
jmp 0x23d3c
movq -0x10(%rbp), %rax
addq $0xb8, %rax
movq %rax, -0x188(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x434e0
movq -0x188(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x43490
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x23ccc
jmp 0x23e0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0xac30
jmp 0x23e07
movq -0x20(%rbp), %rdi
callq 0x30240
movq %rax, -0x100(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x302a0
testb $0x1, %al
jne 0x23dc8
jmp 0x23dfd
movq -0x8(%rbp), %rdi
movq -0x100(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rsi
callq 0x43500
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x23db2
callq 0xaaf0
jmp 0x23f34
jmp 0x23f23
jmp 0x23e0e
movq -0x20(%rbp), %rdi
callq 0x30270
movq %rax, -0x118(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x302a0
testb $0x1, %al
jne 0x23e34
jmp 0x23e7d
leaq -0x28(%rbp), %rdi
callq 0x303e0
cmpl $0x4, 0x14(%rax)
je 0x23e72
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rsi
callq 0x43500
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x23e7b
leaq -0x28(%rbp), %rdi
callq 0x30330
jmp 0x23e0e
jmp 0x23e7f
movq -0x30(%rbp), %rdi
callq 0x30530
movq %rax, -0x130(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x30500
testb $0x1, %al
jne 0x23ea5
jmp 0x23f1a
movq -0x38(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
movl $0x1, %esi
callq 0x30560
movq %rax, -0x138(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x30590
movq (%rax), %rax
cmpl $0x4, 0x14(%rax)
jne 0x23f0a
leaq -0x38(%rbp), %rdi
callq 0x30590
movq (%rax), %rdi
callq 0x30420
movq %rax, -0x190(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x30590
movq -0x190(%rbp), %rdi
movq (%rax), %rsi
callq 0x305b0
movq -0x138(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x23e7f
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0xacc0
movq %rax, %rdi
callq 0xfa30
nopw %cs:(%rax,%rax)