def merge(a, b, cmp, next):
    if a is None:
        head = b
    elif b is None:
        head = a
    else:
        if cmp(a, b) <= 0:
            ptr = a
            a = getattr(a, next)
        else:
            ptr = b
            b = getattr(b, next)

        head = ptr
        while a and b:
            if 0 <= cmp(a, b):
                setattr(ptr, next, a)
                ptr = a
                a = getattr(a, next)
            else:
                setattr(ptr, next, b)
                ptr = b
                b = getattr(b, next)

        if a:
            setattr(ptr, next, a)
        else:
            setattr(ptr, next, b)

    return head


#
# Inputs:
#   list:      Pointer to a singly-linked list of structures.
#   next:      Attribute name of "next" field.
#   cmp:       A comparison function.
#
# Return Value:
#   A pointer to the head of a sorted list containing the elements
#   orginally in list.
#
# Side effects:
#   The "next" pointers for elements in list are changed.
#
dataset/ETHPy150Open probcomp/bayeslite/external/lemonade/dist/lemonade/msort.py/merge/swapped operands of `<=`