require '_h2ph_pre.ph';

no warnings qw(redefine misc);

unless(defined(&_SYS_RBTREE_H_)) {
    eval 'sub _SYS_RBTREE_H_ () {1;}' unless defined(&_SYS_RBTREE_H_);
    require 'sys/types.ph';
    require 'sys/queue.ph';
    require 'sys/endian.ph';
    if(!defined(&_KERNEL)  && !defined(&_STANDALONE)  && defined(&RBDEBUG)) {
	require 'stdbool.ph';
    }
    eval 'sub RB_DIR_LEFT () {0;}' unless defined(&RB_DIR_LEFT);
    eval 'sub RB_DIR_RIGHT () {1;}' unless defined(&RB_DIR_RIGHT);
    eval 'sub RB_DIR_OTHER () {1;}' unless defined(&RB_DIR_OTHER);
    eval 'sub rb_left () { $rb_nodes[ &RB_DIR_LEFT];}' unless defined(&rb_left);
    eval 'sub rb_right () { $rb_nodes[ &RB_DIR_RIGHT];}' unless defined(&rb_right);
    eval 'sub RB_FLAG_POSITION () {( &uintptr_t)0x2;}' unless defined(&RB_FLAG_POSITION);
    eval 'sub RB_FLAG_RED () {( &uintptr_t)0x1;}' unless defined(&RB_FLAG_RED);
    eval 'sub RB_FLAG_MASK () {( &RB_FLAG_POSITION| &RB_FLAG_RED);}' unless defined(&RB_FLAG_MASK);
    eval 'sub RB_FATHER {
        my($rb) = @_;
	    eval q(((($rb)-> &rb_info & ~ &RB_FLAG_MASK)));
    }' unless defined(&RB_FATHER);
    eval 'sub RB_SET_FATHER {
        my($rb, $father) = @_;
	    eval q((( &void)(($rb)-> &rb_info = ( &uintptr_t)($father)|(($rb)-> &rb_info &  &RB_FLAG_MASK))));
    }' unless defined(&RB_SET_FATHER);
    eval 'sub RB_SENTINEL_P {
        my($rb) = @_;
	    eval q((($rb) ==  &NULL));
    }' unless defined(&RB_SENTINEL_P);
    eval 'sub RB_LEFT_SENTINEL_P {
        my($rb) = @_;
	    eval q( &RB_SENTINEL_P(($rb)-> &rb_left));
    }' unless defined(&RB_LEFT_SENTINEL_P);
    eval 'sub RB_RIGHT_SENTINEL_P {
        my($rb) = @_;
	    eval q( &RB_SENTINEL_P(($rb)-> &rb_right));
    }' unless defined(&RB_RIGHT_SENTINEL_P);
    eval 'sub RB_FATHER_SENTINEL_P {
        my($rb) = @_;
	    eval q( &RB_SENTINEL_P( &RB_FATHER(($rb))));
    }' unless defined(&RB_FATHER_SENTINEL_P);
    eval 'sub RB_CHILDLESS_P {
        my($rb) = @_;
	    eval q(( &RB_SENTINEL_P($rb) || ( &RB_LEFT_SENTINEL_P($rb)  &&  &RB_RIGHT_SENTINEL_P($rb))));
    }' unless defined(&RB_CHILDLESS_P);
    eval 'sub RB_TWOCHILDREN_P {
        my($rb) = @_;
	    eval q((! &RB_SENTINEL_P($rb)  && ! &RB_LEFT_SENTINEL_P($rb)  && ! &RB_RIGHT_SENTINEL_P($rb)));
    }' unless defined(&RB_TWOCHILDREN_P);
    eval 'sub RB_POSITION {
        my($rb) = @_;
	    eval q(((($rb)-> &rb_info &  &RB_FLAG_POSITION) ?  &RB_DIR_RIGHT :  &RB_DIR_LEFT));
    }' unless defined(&RB_POSITION);
    eval 'sub RB_RIGHT_P {
        my($rb) = @_;
	    eval q(( &RB_POSITION($rb) ==  &RB_DIR_RIGHT));
    }' unless defined(&RB_RIGHT_P);
    eval 'sub RB_LEFT_P {
        my($rb) = @_;
	    eval q(( &RB_POSITION($rb) ==  &RB_DIR_LEFT));
    }' unless defined(&RB_LEFT_P);
    eval 'sub RB_RED_P {
        my($rb) = @_;
	    eval q((! &RB_SENTINEL_P($rb)  && (($rb)-> &rb_info &  &RB_FLAG_RED) != 0));
    }' unless defined(&RB_RED_P);
    eval 'sub RB_BLACK_P {
        my($rb) = @_;
	    eval q(( &RB_SENTINEL_P($rb) || (($rb)-> &rb_info &  &RB_FLAG_RED) == 0));
    }' unless defined(&RB_BLACK_P);
    eval 'sub RB_MARK_RED {
        my($rb) = @_;
	    eval q((( &void)(($rb)-> &rb_info |=  &RB_FLAG_RED)));
    }' unless defined(&RB_MARK_RED);
    eval 'sub RB_MARK_BLACK {
        my($rb) = @_;
	    eval q((( &void)(($rb)-> &rb_info &= ~ &RB_FLAG_RED)));
    }' unless defined(&RB_MARK_BLACK);
    eval 'sub RB_INVERT_COLOR {
        my($rb) = @_;
	    eval q((( &void)(($rb)-> &rb_info ^=  &RB_FLAG_RED)));
    }' unless defined(&RB_INVERT_COLOR);
    eval 'sub RB_ROOT_P {
        my($rbt, $rb) = @_;
	    eval q((($rbt)-> &rbt_root == ($rb)));
    }' unless defined(&RB_ROOT_P);
    eval 'sub RB_SET_POSITION {
        my($rb, $position) = @_;
	    eval q((( &void)(($position) ? (($rb)-> &rb_info |=  &RB_FLAG_POSITION) : (($rb)-> &rb_info &= ~ &RB_FLAG_POSITION))));
    }' unless defined(&RB_SET_POSITION);
    eval 'sub RB_ZERO_PROPERTIES {
        my($rb) = @_;
	    eval q((( &void)(($rb)-> &rb_info &= ~ &RB_FLAG_MASK)));
    }' unless defined(&RB_ZERO_PROPERTIES);
    eval 'sub RB_COPY_PROPERTIES {
        my($dst, $src) = @_;
	    eval q((( &void)(($dst)-> &rb_info ^= (($dst)-> &rb_info ^ ($src)-> &rb_info) &  &RB_FLAG_MASK)));
    }' unless defined(&RB_COPY_PROPERTIES);
    eval 'sub RB_SWAP_PROPERTIES {
        my($a, $b) = @_;
	    eval q( &do {  &uintptr_t  &xorinfo = (($a)-> &rb_info ^ ($b)-> &rb_info) &  &RB_FLAG_MASK; ($a)-> &rb_info ^=  &xorinfo; ($b)-> &rb_info ^=  &xorinfo; }  &while ( 0));
    }' unless defined(&RB_SWAP_PROPERTIES);
    if(defined(&RBDEBUG)) {
    }
    eval 'sub RB_TREE_MIN {
        my($T) = @_;
	    eval q( &rb_tree_iterate(($T),  &NULL,  &RB_DIR_LEFT));
    }' unless defined(&RB_TREE_MIN);
    eval 'sub RB_TREE_MAX {
        my($T) = @_;
	    eval q( &rb_tree_iterate(($T),  &NULL,  &RB_DIR_RIGHT));
    }' unless defined(&RB_TREE_MAX);
    eval 'sub RB_TREE_NEXT {
        my($T, $N) = @_;
	    eval q( &rb_tree_iterate(($T), ($N),  &RB_DIR_RIGHT));
    }' unless defined(&RB_TREE_NEXT);
    eval 'sub RB_TREE_PREV {
        my($T, $N) = @_;
	    eval q( &rb_tree_iterate(($T), ($N),  &RB_DIR_LEFT));
    }' unless defined(&RB_TREE_PREV);
    eval 'sub RB_TREE_FOREACH {
        my($N, $T) = @_;
	    eval q( &for (($N) =  &RB_TREE_MIN($T); ($N); ($N) =  &RB_TREE_NEXT(($T), ($N))));
    }' unless defined(&RB_TREE_FOREACH);
    eval 'sub RB_TREE_FOREACH_REVERSE {
        my($N, $T) = @_;
	    eval q( &for (($N) =  &RB_TREE_MAX($T); ($N); ($N) =  &RB_TREE_PREV(($T), ($N))));
    }' unless defined(&RB_TREE_FOREACH_REVERSE);
    eval 'sub RB_TREE_FOREACH_SAFE {
        my($N, $T, $S) = @_;
	    eval q( &for (($N) =  &RB_TREE_MIN($T); ($N)  && (($S) =  &RB_TREE_NEXT(($T), ($N)), 1); ($N) = ($S)));
    }' unless defined(&RB_TREE_FOREACH_SAFE);
    eval 'sub RB_TREE_FOREACH_REVERSE_SAFE {
        my($N, $T, $S) = @_;
	    eval q( &for (($N) =  &RB_TREE_MAX($T); ($N)  && (($S) =  &RB_TREE_PREV(($T), ($N)), 1); ($N) = ($S)));
    }' unless defined(&RB_TREE_FOREACH_REVERSE_SAFE);
    if(defined(&RBDEBUG)) {
	eval 'sub RB_TAILQ_REMOVE {
	    my($a, $b, $c) = @_;
    	    eval q( &TAILQ_REMOVE($a, $b, $c));
	}' unless defined(&RB_TAILQ_REMOVE);
	eval 'sub RB_TAILQ_INIT {
	    my($a) = @_;
    	    eval q( &TAILQ_INIT($a));
	}' unless defined(&RB_TAILQ_INIT);
	eval 'sub RB_TAILQ_INSERT_HEAD {
	    my($a, $b, $c) = @_;
    	    eval q( &TAILQ_INSERT_HEAD($a, $b, $c));
	}' unless defined(&RB_TAILQ_INSERT_HEAD);
	eval 'sub RB_TAILQ_INSERT_BEFORE {
	    my($a, $b, $c) = @_;
    	    eval q( &TAILQ_INSERT_BEFORE($a, $b, $c));
	}' unless defined(&RB_TAILQ_INSERT_BEFORE);
	eval 'sub RB_TAILQ_INSERT_AFTER {
	    my($a, $b, $c, $d) = @_;
    	    eval q( &TAILQ_INSERT_AFTER($a, $b, $c, $d));
	}' unless defined(&RB_TAILQ_INSERT_AFTER);
    } else {
	eval 'sub RB_TAILQ_REMOVE {
	    my($a, $b, $c) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RB_TAILQ_REMOVE);
	eval 'sub RB_TAILQ_INIT {
	    my($a) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RB_TAILQ_INIT);
	eval 'sub RB_TAILQ_INSERT_HEAD {
	    my($a, $b, $c) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RB_TAILQ_INSERT_HEAD);
	eval 'sub RB_TAILQ_INSERT_BEFORE {
	    my($a, $b, $c) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RB_TAILQ_INSERT_BEFORE);
	eval 'sub RB_TAILQ_INSERT_AFTER {
	    my($a, $b, $c, $d) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RB_TAILQ_INSERT_AFTER);
    }
    if(defined(&RBDEBUG)) {
    }
    if(defined(&RBSTATS)) {
    }
    if(defined(&RBSTATS)) {
	eval 'sub RBSTAT_INC {
	    my($v) = @_;
    	    eval q((( &void)(($v)++)));
	}' unless defined(&RBSTAT_INC);
	eval 'sub RBSTAT_DEC {
	    my($v) = @_;
    	    eval q((( &void)(($v)--)));
	}' unless defined(&RBSTAT_DEC);
    } else {
	eval 'sub RBSTAT_INC {
	    my($v) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RBSTAT_INC);
	eval 'sub RBSTAT_DEC {
	    my($v) = @_;
    	    eval q( &do { }  &while ( 0));
	}' unless defined(&RBSTAT_DEC);
    }
    if(defined(&RBDEBUG)) {
    }
    if(defined(&RBSTATS)) {
    }
}
1;
