// File: /workspace/github_cpp_makefile_star2_O1/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 0x14005
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 0x150b1
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 0x1fa24
movq %r15, %rbp
cmpq %r13, %r15
jne 0x1505c
movq 0x18(%rsp), %rax
movq (%rax), %r13
cmpq %rax, %r13
movq 0x20(%rsp), %rbp
je 0x15100
movq 0x10(%r13), %rsi
movl 0x18(%r13), %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x1f5c4
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 0x150c3
movq 0x10(%rsp), %r15
cmpq %r15, %r14
je 0x15158
cmpl $0x4, 0x24(%r14)
jne 0x15116
movq (%r14), %r14
jmp 0x15105
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1f6ae
movq %rax, %r14
jmp 0x15105
movq 0x10(%r12), %rax
movq (%r12), %r12
cmpl $0x4, 0x14(%rax)
jne 0x15158
movq (%rax), %rdi
leaq 0x118(%rdi), %rcx
movq %rcx, %rsi
movq (%rsi), %rsi
cmpq %rcx, %rsi
je 0x15158
leaq 0x10(%rsi), %rdx
cmpq %rax, %rdx
jne 0x15142
callq 0x1f6ae
cmpq 0x8(%rsp), %r12
jne 0x15126
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15170
movq %rax, %rdi
callq 0x8210
movq 0x8(%rsp), %rax
movq (%rax), %r15
cmpq %r12, %r15
je 0x151b1
movq 0x10(%r15), %rax
movq (%r15), %r15
movq (%rax), %rdi
leaq 0x118(%rdi), %rcx
movq %rcx, %rsi
movq (%rsi), %rsi
cmpq %rcx, %rsi
je 0x15180
leaq 0x10(%rsi), %rdx
cmpq %rax, %rdx
jne 0x15199
callq 0x1f6ae
jmp 0x15180
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpq %r14, %rax
je 0x151cb
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1f6ae
jmp 0x151b9
callq 0x8920
movq %rax, %rbx
callq 0x8a50
movq %rbx, %rdi
callq 0x8ab0
movq %rax, %rdi
callq 0xb8be