// File: /workspace/github_cpp_makefile_star2_O2/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, %rbp
movq %rsi, %r13
movq %rdi, %rbx
addq $0xb8, %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x129fe
leaq 0x118(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x118(%rbx), %r15
movq 0x130(%rbx), %r14
movq %rbx, 0x18(%rsp)
leaq 0x130(%rbx), %rax
movq %rax, 0x8(%rsp)
movq (%rbp), %r12
cmpq %rbp, %r12
je 0x12b8b
leaq 0x10(%r12), %rax
movq 0x8(%rsp), %rdi
movq (%rdi), %rsi
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdx
callq 0x1b5e0
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x174ad
movq %rax, %rbx
movq 0x10(%r12), %rdi
movq 0x118(%rdi), %rsi
addq $0x118, %rdi            # imm = 0x118
movq %rbp, %rdx
movq %r12, %rcx
callq 0x1bbec
movq %rbx, %r12
jmp 0x12b39
movq (%r13), %rbx
movq 0x18(%rsp), %r12
cmpq %r13, %rbx
je 0x12bbb
movq 0x10(%rbx), %rsi
movl 0x18(%rbx), %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x1b4a6
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1b57e
movq %rax, %rbx
jmp 0x12b94
movq 0x10(%rsp), %rbx
cmpq %rbx, %r15
je 0x12be1
cmpl $0x4, 0x24(%r15)
jne 0x12bd1
movq (%r15), %r15
jmp 0x12bc0
movq %r12, %rdi
movq %r15, %rsi
callq 0x1b58c
movq %rax, %r15
jmp 0x12bc0
pushq $0x1
popq %rbx
cmpq 0x8(%rsp), %r14
je 0x12c13
movq %r14, %rdi
movq %rbx, %rsi
callq 0x174cb
movq 0x10(%r14), %rsi
cmpl $0x4, 0x14(%rsi)
movq %rax, %r14
jne 0x12be4
movq %rax, %r15
movq (%rsi), %rdi
callq 0x174ea
movq %r15, %r14
jmp 0x12be4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12c26
jmp 0x12c26
movq %rax, %rdi
callq 0x8200
movq 0x8(%rsp), %rax
movq (%rax), %r12
pushq $0x1
popq %rbx
cmpq %r14, %r12
je 0x12c5e
movq %r12, %rdi
movq %rbx, %rsi
callq 0x174cb
movq %rax, %r13
movq 0x10(%r12), %rsi
movq (%rsi), %rdi
callq 0x174ea
movq %r13, %r12
jmp 0x12c39
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rbx
cmpq %r15, %rax
je 0x12c7d
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1b58c
jmp 0x12c6b
callq 0x8910
jmp 0x12c84
movq %rax, %rbx
callq 0x8a40
movq %rbx, %rdi
callq 0x8a90
movq %rax, %rdi
callq 0xaebb