if (p->trb_tag[1] == TRB_THREAD)
  {
    if (p->trb_tag[0] == TRB_CHILD)
      {
        struct trb_node *t = p->trb_link[0];
        while (t->trb_tag[1] == TRB_CHILD)
          t = t->trb_link[1];
        t->trb_link[1] = p->trb_link[1];
        pa[k - 1]->trb_link[da[k - 1]] = p->trb_link[0];
      }
    else
      {
        pa[k - 1]->trb_link[da[k - 1]] = p->trb_link[da[k - 1]];
        if (pa[k - 1] != (struct trb_node *) &tree->trb_root)
          pa[k - 1]->trb_tag[da[k - 1]] = TRB_THREAD;
      }
  }
else
  {
    enum trb_color t;
    struct trb_node *r = p->trb_link[1];

    if (r->trb_tag[0] == TRB_THREAD)
      {
        r->trb_link[0] = p->trb_link[0];
        r->trb_tag[0] = p->trb_tag[0];
        if (r->trb_tag[0] == TRB_CHILD)
          {
            struct trb_node *t = r->trb_link[0];
            while (t->trb_tag[1] == TRB_CHILD)
              t = t->trb_link[1];
            t->trb_link[1] = r;
          }
        pa[k - 1]->trb_link[da[k - 1]] = r;
        t = r->trb_color;
        r->trb_color = p->trb_color;
        p->trb_color = t;
        da[k] = 1;
        pa[k++] = r;
      }
    else
      {
        struct trb_node *s;
        int j = k++;

        for (;;)
          {
            da[k] = 0;
            pa[k++] = r;
            s = r->trb_link[0];
            if (s->trb_tag[0] == TRB_THREAD)
              break;

            r = s;
          }

        da[j] = 1;
        pa[j] = s;
        if (s->trb_tag[1] == TRB_CHILD)
          r->trb_link[0] = s->trb_link[1];
        else
          {
            r->trb_link[0] = s;
            r->trb_tag[0] = TRB_THREAD;
          }

        s->trb_link[0] = p->trb_link[0];
        if (p->trb_tag[0] == TRB_CHILD)
          {
            struct trb_node *t = p->trb_link[0];
            while (t->trb_tag[1] == TRB_CHILD)
              t = t->trb_link[1];
            t->trb_link[1] = s;

            s->trb_tag[0] = TRB_CHILD;
          }

        s->trb_link[1] = p->trb_link[1];
        s->trb_tag[1] = TRB_CHILD;

        t = s->trb_color;
        s->trb_color = p->trb_color;
        p->trb_color = t;

        pa[j - 1]->trb_link[da[j - 1]] = s;

      }
  }

