while (k >= 3 && pa[k - 1]->trb_color == TRB_RED)
  {
    if (da[k - 2] == 0)
      {
        struct trb_node *y = pa[k - 2]->trb_link[1];
        if (pa[k - 2]->trb_tag[1] == TRB_CHILD && y->trb_color == TRB_RED)
          {
            pa[k - 1]->trb_color = y->trb_color = TRB_BLACK;
            pa[k - 2]->trb_color = TRB_RED;
            k -= 2;
          }
        else
          {
            struct trb_node *x;

            if (da[k - 1] == 0)
              y = pa[k - 1];
            else
              {
                x = pa[k - 1];
                y = x->trb_link[1];
                x->trb_link[1] = y->trb_link[0];
                y->trb_link[0] = x;
                pa[k - 2]->trb_link[0] = y;

                if (y->trb_tag[0] == TRB_THREAD)
                  {
                    y->trb_tag[0] = TRB_CHILD;
                    x->trb_tag[1] = TRB_THREAD;
                    x->trb_link[1] = y;
                  }
              }

            x = pa[k - 2];
            x->trb_color = TRB_RED;
            y->trb_color = TRB_BLACK;

            x->trb_link[0] = y->trb_link[1];
            y->trb_link[1] = x;
            pa[k - 3]->trb_link[da[k - 3]] = y;

            if (y->trb_tag[1] == TRB_THREAD)
              {
                y->trb_tag[1] = TRB_CHILD;
                x->trb_tag[0] = TRB_THREAD;
                x->trb_link[0] = y;
              }
            break;
          }
      }
    else
      {
        struct trb_node *y = pa[k - 2]->trb_link[0];
        if (pa[k - 2]->trb_tag[0] == TRB_CHILD && y->trb_color == TRB_RED)
          {
            pa[k - 1]->trb_color = y->trb_color = TRB_BLACK;
            pa[k - 2]->trb_color = TRB_RED;
            k -= 2;
          }
        else
          {
            struct trb_node *x;

            if (da[k - 1] == 1)
              y = pa[k - 1];
            else
              {
                x = pa[k - 1];
                y = x->trb_link[0];
                x->trb_link[0] = y->trb_link[1];
                y->trb_link[1] = x;
                pa[k - 2]->trb_link[1] = y;

                if (y->trb_tag[1] == TRB_THREAD)
                  {
                    y->trb_tag[1] = TRB_CHILD;
                    x->trb_tag[0] = TRB_THREAD;
                    x->trb_link[0] = y;
                  }
              }

            x = pa[k - 2];
            x->trb_color = TRB_RED;
            y->trb_color = TRB_BLACK;

            x->trb_link[1] = y->trb_link[0];
            y->trb_link[0] = x;
            pa[k - 3]->trb_link[da[k - 3]] = y;

            if (y->trb_tag[0] == TRB_THREAD)
              {
                y->trb_tag[0] = TRB_CHILD;
                x->trb_tag[1] = TRB_THREAD;
                x->trb_link[1] = y;
              }
            break;
          }
      }
  }
tree->trb_root->trb_color = TRB_BLACK;
