// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
movq %rdi, %rbx
movq %rsi, 0x20(%rsp)
leaq 0xb8(%rsi), %rdi
leaq 0x8(%rbx), %rdx
movq %rdi, 0x18(%rsp)
movq %rbx, %rsi
callq 0x13e37
leaq 0x118(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x118(%rbx), %r14
movq 0x130(%rbx), %r12
leaq 0x130(%rbx), %rax
movq %rax, 0x8(%rsp)
movq (%r13), %rbp
cmpq %r13, %rbp
je 0x14efd
movq 0x8(%rsp), %rax
movq (%rax), %r15
movl $0x18, %edi
callq 0x8600
leaq 0x10(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r15, %rsi
callq 0x8060
incq 0x140(%rbx)
movq (%rbp), %r15
movq 0x10(%rbp), %rdi
movq 0x118(%rdi), %rsi
addq $0x118, %rdi            # imm = 0x118
movq %r13, %rdx
movq %rbp, %rcx
callq 0x1fc16
movq %r15, %rbp
cmpq %r13, %r15
jne 0x14ea8
movq 0x18(%rsp), %rax
movq (%rax), %r13
cmpq %rax, %r13
movq 0x20(%rsp), %rbp
je 0x14f4c
movq 0x10(%r13), %rsi
movl 0x18(%r13), %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x1f58a
movq (%r13), %r15
decq 0xc8(%rbp)
movq %r13, %rdi
callq 0x8a40
movl $0x20, %esi
movq %r13, %rdi
callq 0x8620
movq %r15, %r13
cmpq 0x18(%rsp), %r15
jne 0x14f0f
movq 0x10(%rsp), %r15
cmpq %r15, %r14
je 0x14fa4
cmpl $0x4, 0x24(%r14)
jne 0x14f62
movq (%r14), %r14
jmp 0x14f51
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1f674
movq %rax, %r14
jmp 0x14f51
movq 0x10(%r12), %rax
movq (%r12), %r12
cmpl $0x4, 0x14(%rax)
jne 0x14fa4
movq (%rax), %rdi
leaq 0x118(%rdi), %rcx
movq %rcx, %rsi
movq (%rsi), %rsi
cmpq %rcx, %rsi
je 0x14fa4
leaq 0x10(%rsi), %rdx
cmpq %rax, %rdx
jne 0x14f8e
callq 0x1f674
cmpq 0x8(%rsp), %r12
jne 0x14f72
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x14fbc
movq %rax, %rdi
callq 0x8210
movq 0x8(%rsp), %rax
movq (%rax), %r15
cmpq %r12, %r15
je 0x14ffd
movq 0x10(%r15), %rax
movq (%r15), %r15
movq (%rax), %rdi
leaq 0x118(%rdi), %rcx
movq %rcx, %rsi
movq (%rsi), %rsi
cmpq %rcx, %rsi
je 0x14fcc
leaq 0x10(%rsi), %rdx
cmpq %rax, %rdx
jne 0x14fe5
callq 0x1f674
jmp 0x14fcc
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpq %r14, %rax
je 0x15017
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1f674
jmp 0x15005
callq 0x8920
movq %rax, %rbx
callq 0x8a50
movq %rbx, %rdi
callq 0x8ab0
movq %rax, %rdi
callq 0xb74e