		/*    sv.c
		 *
		 *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
		 *    2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
		 *
		 *    You may distribute under the terms of either the GNU General Public
		 *    License or the Artistic License, as specified in the README file.
		 *
		 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
		 *
		 *
		 * This file contains the code that creates, manipulates and destroys
		 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
		 * structure of an SV, so their creation and destruction is handled
		 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
		 * level functions (eg. substr, split, join) for each of the types are
		 * in the pp*.c files.
		 */
		
		#include "EXTERN.h"
		#define PERL_IN_SV_C
		#include "perl.h"
		#include "regcomp.h"
		
		#define FCALL *f
		
		#ifdef __Lynx__
		/* Missing proto on LynxOS */
		  char *gconvert(double, int, int,  char *);
		#endif
		
		#ifdef PERL_UTF8_CACHE_ASSERT
		/* The cache element 0 is the Unicode offset;
		 * the cache element 1 is the byte offset of the element 0;
		 * the cache element 2 is the Unicode length of the substring;
		 * the cache element 3 is the byte length of the substring;
		 * The checking of the substring side would be good
		 * but substr() has enough code paths to make my head spin;
		 * if adding more checks watch out for the following tests:
		 *   t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
		 *   lib/utf8.t lib/Unicode/Collate/t/index.t
		 * --jhi
		 */
		#define ASSERT_UTF8_CACHE(cache) \
			STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
		#else
		#define ASSERT_UTF8_CACHE(cache) NOOP
		#endif
		
		#ifdef PERL_OLD_COPY_ON_WRITE
		#define SV_COW_NEXT_SV(sv)	INT2PTR(SV *,SvUVX(sv))
		#define SV_COW_NEXT_SV_SET(current,next)	SvUV_set(current, PTR2UV(next))
		/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
		   on-write.  */
		#endif
		
		/* ============================================================================
		
		=head1 Allocation and deallocation of SVs.
		
		An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
		av, hv...) contains type and reference count information, as well as a
		pointer to the body (struct xrv, xpv, xpviv...), which contains fields
		specific to each type.
		
		Normally, this allocation is done using arenas, which by default are
		approximately 4K chunks of memory parcelled up into N heads or bodies.  The
		first slot in each arena is reserved, and is used to hold a link to the next
		arena.  In the case of heads, the unused first slot also contains some flags
		and a note of the number of slots.  Snaked through each arena chain is a
		linked list of free items; when this becomes empty, an extra arena is
		allocated and divided up into N items which are threaded into the free list.
		
		The following global variables are associated with arenas:
		
		    PL_sv_arenaroot	pointer to list of SV arenas
		    PL_sv_root		pointer to list of free SV structures
		
		    PL_foo_arenaroot	pointer to list of foo arenas,
		    PL_foo_root		pointer to list of free foo bodies
					    ... for foo in xiv, xnv, xrv, xpv etc.
		
		Note that some of the larger and more rarely used body types (eg xpvio)
		are not allocated using arenas, but are instead just malloc()/free()ed as
		required. Also, if PURIFY is defined, arenas are abandoned altogether,
		with all items individually malloc()ed. In addition, a few SV heads are
		not allocated from an arena, but are instead directly created as static
		or auto variables, eg PL_sv_undef.  The size of arenas can be changed from
		the default by setting PERL_ARENA_SIZE appropriately at compile time.
		
		The SV arena serves the secondary purpose of allowing still-live SVs
		to be located and destroyed during final cleanup.
		
		At the lowest level, the macros new_SV() and del_SV() grab and free
		an SV head.  (If debugging with -DD, del_SV() calls the function S_del_sv()
		to return the SV to the free list with error checking.) new_SV() calls
		more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
		SVs in the free list have their SvTYPE field set to all ones.
		
		Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
		that allocate and return individual body types. Normally these are mapped
		to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
		instead mapped directly to malloc()/free() if PURIFY is defined. The
		new/del functions remove from, or add to, the appropriate PL_foo_root
		list, and call more_xiv() etc to add a new arena if the list is empty.
		
		At the time of very final cleanup, sv_free_arenas() is called from
		perl_destruct() to physically free all the arenas allocated since the
		start of the interpreter.  Note that this also clears PL_he_arenaroot,
		which is otherwise dealt with in hv.c.
		
		Manipulation of any of the PL_*root pointers is protected by enclosing
		LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
		if threads are enabled.
		
		The function visit() scans the SV arenas list, and calls a specified
		function for each SV it finds which is still live - ie which has an SvTYPE
		other than all 1's, and a non-zero SvREFCNT. visit() is used by the
		following functions (specified as [function that calls visit()] / [function
		called by visit() for each SV]):
		
		    sv_report_used() / do_report_used()
		    			dump all remaining SVs (debugging aid)
		
		    sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
					Attempt to free all objects pointed to by RVs,
					and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
					try to do the same for all objects indirectly
					referenced by typeglobs too.  Called once from
					perl_destruct(), prior to calling sv_clean_all()
					below.
		
		    sv_clean_all() / do_clean_all()
					SvREFCNT_dec(sv) each remaining SV, possibly
					triggering an sv_free(). It also sets the
					SVf_BREAK flag on the SV to indicate that the
					refcnt has been artificially lowered, and thus
					stopping sv_free() from giving spurious warnings
					about SVs which unexpectedly have a refcnt
					of zero.  called repeatedly from perl_destruct()
					until there are no SVs left.
		
		=head2 Summary
		
		Private API to rest of sv.c
		
		    new_SV(),  del_SV(),
		
		    new_XIV(), del_XIV(),
		    new_XNV(), del_XNV(),
		    etc
		
		Public API:
		
		    sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
		
		
		=cut
		
		============================================================================ */
		
		
		
		/*
		 * "A time to plant, and a time to uproot what was planted..."
		 */
		
		/*
		 * nice_chunk and nice_chunk size need to be set
		 * and queried under the protection of sv_mutex
		 */
		void
		Perl_offer_nice_chunk(pTHX_ void *chunk, U32 chunk_size)
       27360    {
       27360        void *new_chunk;
       27360        U32 new_chunk_size;
		    LOCK_SV_MUTEX;
       27360        new_chunk = (void *)(chunk);
       27360        new_chunk_size = (chunk_size);
       27360        if (new_chunk_size > PL_nice_chunk_size) {
       20939    	Safefree(PL_nice_chunk);
       20939    	PL_nice_chunk = (char *) new_chunk;
       20939    	PL_nice_chunk_size = new_chunk_size;
		    } else {
        6421    	Safefree(chunk);
		    }
		    UNLOCK_SV_MUTEX;
		}
		
		#ifdef DEBUG_LEAKING_SCALARS
		#  ifdef NETWARE
		#    define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
		#  else
		#    define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
		#  endif
		#else
		#  define FREE_SV_DEBUG_FILE(sv)
		#endif
		
		#define plant_SV(p) \
		    STMT_START {					\
			FREE_SV_DEBUG_FILE(p);				\
			SvANY(p) = (void *)PL_sv_root;			\
			SvFLAGS(p) = SVTYPEMASK;			\
			PL_sv_root = (p);				\
			--PL_sv_count;					\
		    } STMT_END
		
		/* sv_mutex must be held while calling uproot_SV() */
		#define uproot_SV(p) \
		    STMT_START {					\
			(p) = PL_sv_root;				\
			PL_sv_root = (SV*)SvANY(p);			\
			++PL_sv_count;					\
		    } STMT_END
		
		
		/* make some more SVs by adding another arena */
		
		/* sv_mutex must be held while calling more_sv() */
		STATIC SV*
		S_more_sv(pTHX)
      110123    {
      110123        SV* sv;
		
      110123        if (PL_nice_chunk) {
       18375    	sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
       18375    	PL_nice_chunk = Nullch;
       18375            PL_nice_chunk_size = 0;
		    }
		    else {
       91748    	char *chunk;                /* must use New here to match call to */
       91748    	New(704,chunk,PERL_ARENA_SIZE,char);   /* Safefree() in sv_free_arenas()     */
       91748    	sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
		    }
      110123        uproot_SV(sv);
      110123        return sv;
		}
		
		/* new_SV(): return a new, empty SV head */
		
		#ifdef DEBUG_LEAKING_SCALARS
		/* provide a real function for a debugger to play with */
		STATIC SV*
		S_new_SV(pTHX)
		{
		    SV* sv;
		
		    LOCK_SV_MUTEX;
		    if (PL_sv_root)
			uproot_SV(sv);
		    else
			sv = S_more_sv(aTHX);
		    UNLOCK_SV_MUTEX;
		    SvANY(sv) = 0;
		    SvREFCNT(sv) = 1;
		    SvFLAGS(sv) = 0;
		    sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
		    sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
		        (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
		    sv->sv_debug_inpad = 0;
		    sv->sv_debug_cloned = 0;
		#  ifdef NETWARE
		    sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
		#  else
		    sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
		#  endif
		    
		    return sv;
		}
		#  define new_SV(p) (p)=S_new_SV(aTHX)
		
		#else
		#  define new_SV(p) \
		    STMT_START {					\
			LOCK_SV_MUTEX;					\
			if (PL_sv_root)					\
			    uproot_SV(p);				\
			else						\
			    (p) = S_more_sv(aTHX);			\
			UNLOCK_SV_MUTEX;				\
			SvANY(p) = 0;					\
			SvREFCNT(p) = 1;				\
			SvFLAGS(p) = 0;					\
		    } STMT_END
		#endif
		
		
		/* del_SV(): return an empty SV head to the free list */
		
		#ifdef DEBUGGING
		
		#define del_SV(p) \
		    STMT_START {					\
			LOCK_SV_MUTEX;					\
			if (DEBUG_D_TEST)				\
			    del_sv(p);					\
			else						\
			    plant_SV(p);				\
			UNLOCK_SV_MUTEX;				\
		    } STMT_END
		
		STATIC void
		S_del_sv(pTHX_ SV *p)
      ######    {
      ######        if (DEBUG_D_TEST) {
      ######    	SV* sva;
      ######    	bool ok = 0;
      ######    	for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
      ######    	    const SV * const sv = sva + 1;
      ######    	    const SV * const svend = &sva[SvREFCNT(sva)];
      ######    	    if (p >= sv && p < svend) {
      ######    		ok = 1;
      ######    		break;
			    }
			}
      ######    	if (!ok) {
      ######    	    if (ckWARN_d(WARN_INTERNAL))	
      ######    	        Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
					    "Attempt to free non-arena SV: 0x%"UVxf
		                            pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
      ######    	    return;
			}
		    }
      ######        plant_SV(p);
		}
		
		#else /* ! DEBUGGING */
		
		#define del_SV(p)   plant_SV(p)
		
		#endif /* DEBUGGING */
		
		
		/*
		=head1 SV Manipulation Functions
		
		=for apidoc sv_add_arena
		
		Given a chunk of memory, link it to the head of the list of arenas,
		and split it into a list of free SVs.
		
		=cut
		*/
		
		void
		Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
      110123    {
      110123        SV* sva = (SV*)ptr;
      110123        register SV* sv;
      110123        register SV* svend;
		
		    /* The first SV in an arena isn't an SV. */
      110123        SvANY(sva) = (void *) PL_sv_arenaroot;		/* ptr to next arena */
      110123        SvREFCNT(sva) = size / sizeof(SV);		/* number of SV slots */
      110123        SvFLAGS(sva) = flags;			/* FAKE if not to be freed */
		
      110123        PL_sv_arenaroot = sva;
      110123        PL_sv_root = sva + 1;
		
      110123        svend = &sva[SvREFCNT(sva) - 1];
      110123        sv = sva + 1;
    24562683        while (sv < svend) {
    24452560    	SvANY(sv) = (void *)(SV*)(sv + 1);
		#ifdef DEBUGGING
    24452560    	SvREFCNT(sv) = 0;
		#endif
			/* Must always set typemask because it's awlays checked in on cleanup
			   when the arenas are walked looking for objects.  */
    24452560    	SvFLAGS(sv) = SVTYPEMASK;
    24452560    	sv++;
		    }
      110123        SvANY(sv) = 0;
		#ifdef DEBUGGING
      110123        SvREFCNT(sv) = 0;
		#endif
      110123        SvFLAGS(sv) = SVTYPEMASK;
		}
		
		/* visit(): call the named function for each non-free SV in the arenas
		 * whose flags field matches the flags/mask args. */
		
		STATIC I32
		S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
       10605    {
       10605        SV* sva;
       10605        I32 visited = 0;
		
      340578        for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
      329973    	register const SV * const svend = &sva[SvREFCNT(sva)];
      329973    	register SV* sv;
    73465595    	for (sv = sva + 1; sv < svend; ++sv) {
    73135622    	    if (SvTYPE(sv) != SVTYPEMASK
				    && (sv->sv_flags & mask) == flags
				    && SvREFCNT(sv))
			    {
     8819028    		(FCALL)(aTHX_ sv);
     8819028    		++visited;
			    }
			}
		    }
       10605        return visited;
		}
		
		#ifdef DEBUGGING
		
		/* called by sv_report_used() for each live SV */
		
		static void
		do_report_used(pTHX_ SV *sv)
      ######    {
      ######        if (SvTYPE(sv) != SVTYPEMASK) {
      ######    	PerlIO_printf(Perl_debug_log, "****\n");
      ######    	sv_dump(sv);
		    }
		}
		#endif
		
		/*
		=for apidoc sv_report_used
		
		Dump the contents of all SVs not yet freed. (Debugging aid).
		
		=cut
		*/
		
		void
		Perl_sv_report_used(pTHX)
      ######    {
		#ifdef DEBUGGING
      ######        visit(do_report_used, 0, 0);
		#endif
		}
		
		/* called by sv_clean_objs() for each live SV */
		
		static void
		do_clean_objs(pTHX_ SV *ref)
     1670847    {
     1670847        SV* target;
		
     1670847        if (SvROK(ref) && SvOBJECT(target = SvRV(ref))) {
       46751    	DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
       46751    	if (SvWEAKREF(ref)) {
      ######    	    sv_del_backref(target, ref);
      ######    	    SvWEAKREF_off(ref);
      ######    	    SvRV_set(ref, NULL);
			} else {
       46751    	    SvROK_off(ref);
       46751    	    SvRV_set(ref, NULL);
       46751    	    SvREFCNT_dec(target);
			}
		    }
		
		    /* XXX Might want to check arrays, etc. */
		}
		
		/* called by sv_clean_objs() for each live SV */
		
		#ifndef DISABLE_DESTRUCTOR_KLUDGE
		static void
		do_clean_named_objs(pTHX_ SV *sv)
     1153703    {
     1153703        if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
     1031864    	if ((
		#ifdef PERL_DONT_CREATE_GVSV
			     GvSV(sv) &&
		#endif
			     SvOBJECT(GvSV(sv))) ||
			     (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
			     (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
			     (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
			     (GvCV(sv) && SvOBJECT(GvCV(sv))) )
			{
       24281    	    DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
       24281    	    SvFLAGS(sv) |= SVf_BREAK;
       24281    	    SvREFCNT_dec(sv);
			}
		    }
		}
		#endif
		
		/*
		=for apidoc sv_clean_objs
		
		Attempt to destroy all objects not yet freed
		
		=cut
		*/
		
		void
		Perl_sv_clean_objs(pTHX)
        2213    {
        2213        PL_in_clean_objs = TRUE;
        2213        visit(do_clean_objs, SVf_ROK, SVf_ROK);
		#ifndef DISABLE_DESTRUCTOR_KLUDGE
		    /* some barnacles may yet remain, clinging to typeglobs */
        2213        visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
		#endif
        2213        PL_in_clean_objs = FALSE;
		}
		
		/* called by sv_clean_all() for each live SV */
		
		static void
		do_clean_all(pTHX_ SV *sv)
     5994478    {
     5994478        DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
     5994478        SvFLAGS(sv) |= SVf_BREAK;
     5994478        if (PL_comppad == (AV*)sv) {
           2    	PL_comppad = Nullav;
           2    	PL_curpad = Null(SV**);
		    }
     5994478        SvREFCNT_dec(sv);
		}
		
		/*
		=for apidoc sv_clean_all
		
		Decrement the refcnt of each remaining SV, possibly triggering a
		cleanup. This function may have to be called multiple times to free
		SVs which are in complex self-referential hierarchies.
		
		=cut
		*/
		
		I32
		Perl_sv_clean_all(pTHX)
        6179    {
        6179        I32 cleaned;
        6179        PL_in_clean_all = TRUE;
        6179        cleaned = visit(do_clean_all, 0,0);
        6179        PL_in_clean_all = FALSE;
        6179        return cleaned;
		}
		
		static void 
       54588    S_free_arena(pTHX_ void **root) {
      168271        while (root) {
      113683    	void ** const next = *(void **)root;
      113683    	Safefree(root);
      113683    	root = next;
		    }
		}
		    
		/*
		=for apidoc sv_free_arenas
		
		Deallocate the memory used by all arenas. Note that all the individual SV
		heads and bodies within the arenas must already have been freed.
		
		=cut
		*/
		
		#define free_arena(name)					\
		    STMT_START {						\
			S_free_arena(aTHX_ (void**) PL_ ## name ## _arenaroot);	\
			PL_ ## name ## _arenaroot = 0;				\
			PL_ ## name ## _root = 0;				\
		    } STMT_END
		
		void
		Perl_sv_free_arenas(pTHX)
        4549    {
        4549        SV* sva;
        4549        SV* svanext;
		
		    /* Free arenas here, but be careful about fake ones.  (We assume
		       contiguity of the fake ones with the corresponding real ones.) */
		
      115323        for (sva = PL_sv_arenaroot; sva; sva = svanext) {
      110774    	svanext = (SV*) SvANY(sva);
      110774    	while (svanext && SvFAKE(svanext))
      ######    	    svanext = (SV*) SvANY(svanext);
		
      110774    	if (!SvFAKE(sva))
      110774    	    Safefree(sva);
		    }
		    
        4549        free_arena(xnv);
        4549        free_arena(xpv);
        4549        free_arena(xpviv);
        4549        free_arena(xpvnv);
        4549        free_arena(xpvcv);
        4549        free_arena(xpvav);
        4549        free_arena(xpvhv);
        4549        free_arena(xpvmg);
        4549        free_arena(xpvgv);
        4549        free_arena(xpvlv);
        4549        free_arena(xpvbm);
        4549        free_arena(he);
		#if defined(USE_ITHREADS)
		    free_arena(pte);
		#endif
		
        4549        Safefree(PL_nice_chunk);
        4549        PL_nice_chunk = Nullch;
        4549        PL_nice_chunk_size = 0;
        4549        PL_sv_arenaroot = 0;
        4549        PL_sv_root = 0;
		}
		
		/* ---------------------------------------------------------------------
		 *
		 * support functions for report_uninit()
		 */
		
		/* the maxiumum size of array or hash where we will scan looking
		 * for the undefined element that triggered the warning */
		
		#define FUV_MAX_SEARCH_SIZE 1000
		
		/* Look for an entry in the hash whose value has the same SV as val;
		 * If so, return a mortal copy of the key. */
		
		STATIC SV*
		S_find_hash_subscript(pTHX_ HV *hv, SV* val)
         332    {
		    dVAR;
         332        register HE **array;
         332        I32 i;
		
         332        if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
					(HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
         218    	return Nullsv;
		
         114        array = HvARRAY(hv);
		
         522        for (i=HvMAX(hv); i>0; i--) {
         520    	register HE *entry;
         684    	for (entry = array[i]; entry; entry = HeNEXT(entry)) {
         276    	    if (HeVAL(entry) != val)
         164    		continue;
         112    	    if (    HeVAL(entry) == &PL_sv_undef ||
				    HeVAL(entry) == &PL_sv_placeholder)
         112    		continue;
         112    	    if (!HeKEY(entry))
      ######    		return Nullsv;
         112    	    if (HeKLEN(entry) == HEf_SVKEY)
      ######    		return sv_mortalcopy(HeKEY_sv(entry));
         112    	    return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
			}
		    }
           2        return Nullsv;
		}
		
		/* Look for an entry in the array whose value has the same SV as val;
		 * If so, return the index, otherwise return -1. */
		
		STATIC I32
		S_find_array_subscript(pTHX_ AV *av, SV* val)
          28    {
          28        SV** svp;
          28        I32 i;
          28        if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
					(AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
          15    	return -1;
		
          13        svp = AvARRAY(av);
        1228        for (i=AvFILLp(av); i>=0; i--) {
        1228    	if (svp[i] == val && svp[i] != &PL_sv_undef)
          13    	    return i;
		    }
      ######        return -1;
		}
		
		/* S_varname(): return the name of a variable, optionally with a subscript.
		 * If gv is non-zero, use the name of that global, along with gvtype (one
		 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
		 * targ.  Depending on the value of the subscript_type flag, return:
		 */
		
		#define FUV_SUBSCRIPT_NONE	1	/* "@foo"          */
		#define FUV_SUBSCRIPT_ARRAY	2	/* "$foo[aindex]"  */
		#define FUV_SUBSCRIPT_HASH	3	/* "$foo{keyname}" */
		#define FUV_SUBSCRIPT_WITHIN	4	/* "within @foo"   */
		
		STATIC SV*
		S_varname(pTHX_ GV *gv, const char gvtype, PADOFFSET targ,
			SV* keyname, I32 aindex, int subscript_type)
        2749    {
		
        2749        SV * const name = sv_newmortal();
        2749        if (gv) {
		
			/* simulate gv_fullname4(), but add literal '^' for $^FOO names
			 * XXX get rid of all this if gv_fullnameX() ever supports this
			 * directly */
		
         263    	const char *p;
         263    	HV * const hv = GvSTASH(gv);
         263    	if (!hv)
      ######    	    p = "???";
         263    	else if (!(p=HvNAME_get(hv)))
      ######    	    p = "__ANON__";
         263    	if (strEQ(p, "main"))
         263    	    sv_setpvn(name, &gvtype, 1);
			else
      ######    	    Perl_sv_setpvf(aTHX_ name, "%c%s::", gvtype, p);
		
         263    	if (GvNAMELEN(gv)>= 1 &&
			    ((unsigned int)*GvNAME(gv)) <= 26)
			{ /* handle $^FOO */
           2    	    Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
           2    	    sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
			}
			else
         261    	    sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
		    }
		    else {
        2486    	U32 unused;
        2486    	CV * const cv = find_runcv(&unused);
        2486    	SV *sv;
        2486    	AV *av;
		
        2486    	if (!cv || !CvPADLIST(cv))
      ######    	    return Nullsv;
        2486    	av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
        2486    	sv = *av_fetch(av, targ, FALSE);
			/* SvLEN in a pad name is not to be trusted */
        2486    	sv_setpv(name, SvPV_nolen_const(sv));
		    }
		
        2749        if (subscript_type == FUV_SUBSCRIPT_HASH) {
         119    	SV * const sv = NEWSV(0,0);
         119    	*SvPVX(name) = '$';
         119    	Perl_sv_catpvf(aTHX_ name, "{%s}",
			    pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
         119    	SvREFCNT_dec(sv);
		    }
        2630        else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
          53    	*SvPVX(name) = '$';
          53    	Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
		    }
        2577        else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
         172    	sv_insert(name, 0, 0,  "within ", 7);
		
        2749        return name;
		}
		
		
		/*
		=for apidoc find_uninit_var
		
		Find the name of the undefined variable (if any) that caused the operator o
		to issue a "Use of uninitialized value" warning.
		If match is true, only return a name if it's value matches uninit_sv.
		So roughly speaking, if a unary operator (such as OP_COS) generates a
		warning, then following the direct child of the op may yield an
		OP_PADSV or OP_GV that gives the name of the undefined variable. On the
		other hand, with OP_ADD there are two branches to follow, so we only print
		the variable name if we get an exact match.
		
		The name is returned as a mortal SV.
		
		Assumes that PL_op is the op that originally triggered the error, and that
		PL_comppad/PL_curpad points to the currently executing pad.
		
		=cut
		*/
		
		STATIC SV *
		S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
        9372    {
		    dVAR;
        9372        SV *sv;
        9372        AV *av;
        9372        GV *gv;
        9372        OP *o, *o2, *kid;
		
        9372        if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
					    uninit_sv == &PL_sv_placeholder)))
          92    	return Nullsv;
		
        9280        switch (obase->op_type) {
		
		    case OP_RV2AV:
		    case OP_RV2HV:
		    case OP_PADAV:
		    case OP_PADHV:
		      {
         339    	const bool pad  = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
         339    	const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
         339    	I32 index = 0;
         339    	SV *keysv = Nullsv;
         339    	int subscript_type = FUV_SUBSCRIPT_WITHIN;
		
         339    	if (pad) { /* @lex, %lex */
         326    	    sv = PAD_SVl(obase->op_targ);
         326    	    gv = Nullgv;
			}
			else {
          13    	    if (cUNOPx(obase)->op_first->op_type == OP_GV) {
			    /* @global, %global */
           7    		gv = cGVOPx_gv(cUNOPx(obase)->op_first);
           7    		if (!gv)
      ######    		    break;
           7    		sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
			    }
			    else /* @{expr}, %{expr} */
           6    		return find_uninit_var(cUNOPx(obase)->op_first,
								    uninit_sv, match);
			}
		
			/* attempt to find a match within the aggregate */
         333    	if (hash) {
         320    	    keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
         320    	    if (keysv)
         107    		subscript_type = FUV_SUBSCRIPT_HASH;
			}
			else {
          13    	    index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
          13    	    if (index >= 0)
           8    		subscript_type = FUV_SUBSCRIPT_ARRAY;
			}
		
         333    	if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
          55    	    break;
		
         278    	return varname(gv, hash ? '%' : '@', obase->op_targ,
						    keysv, index, subscript_type);
		      }
		
		    case OP_PADSV:
        2419    	if (match && PAD_SVl(obase->op_targ) != uninit_sv)
         232    	    break;
        2187    	return varname(Nullgv, '$', obase->op_targ,
						    Nullsv, 0, FUV_SUBSCRIPT_NONE);
		
		    case OP_GVSV:
         404    	gv = cGVOPx_gv(obase);
         404    	if (!gv || (match && GvSV(gv) != uninit_sv))
         217    	    break;
         217    	return varname(gv, '$', 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
		
		    case OP_AELEMFAST:
          28    	if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
          14    	    if (match) {
           8    		SV **svp;
           8    		av = (AV*)PAD_SV(obase->op_targ);
           8    		if (!av || SvRMAGICAL(av))
           4    		    break;
           4    		svp = av_fetch(av, (I32)obase->op_private, FALSE);
           4    		if (!svp || *svp != uninit_sv)
           8    		    break;
			    }
           8    	    return varname(Nullgv, '$', obase->op_targ,
				    Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
			}
			else {
          14    	    gv = cGVOPx_gv(obase);
          14    	    if (!gv)
      ######    		break;
          14    	    if (match) {
           5    		SV **svp;
           5    		av = GvAV(gv);
           5    		if (!av || SvRMAGICAL(av))
           5    		    break;
           5    		svp = av_fetch(av, (I32)obase->op_private, FALSE);
           5    		if (!svp || *svp != uninit_sv)
          11    		    break;
			    }
          11    	    return varname(gv, '$', 0,
				    Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
			}
           4    	break;
		
		    case OP_EXISTS:
           4    	o = cUNOPx(obase)->op_first;
           4    	if (!o || o->op_type != OP_NULL ||
				! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
           4    	    break;
           4    	return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
		
		    case OP_AELEM:
		    case OP_HELEM:
          80    	if (PL_op == obase)
			    /* $a[uninit_expr] or $h{uninit_expr} */
          11    	    return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
		
          69    	gv = Nullgv;
          69    	o = cBINOPx(obase)->op_first;
          69    	kid = cBINOPx(obase)->op_last;
		
			/* get the av or hv, and optionally the gv */
          69    	sv = Nullsv;
          69    	if  (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
          39    	    sv = PAD_SV(o->op_targ);
			}
          30    	else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
				&& cUNOPo->op_first->op_type == OP_GV)
			{
          30    	    gv = cGVOPx_gv(cUNOPo->op_first);
          30    	    if (!gv)
      ######    		break;
          30    	    sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
			}
          69    	if (!sv)
      ######    	    break;
		
          69    	if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
			    /* index is constant */
          42    	    if (match) {
          26    		if (SvMAGICAL(sv))
           8    		    break;
          18    		if (obase->op_type == OP_HELEM) {
           6    		    HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
           6    		    if (!he || HeVAL(he) != uninit_sv)
          12    			break;
				}
				else {
          12    		    SV ** const svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
          12    		    if (!svp || *svp != uninit_sv)
          28    			break;
				}
			    }
          28    	    if (obase->op_type == OP_HELEM)
           7    		return varname(gv, '%', o->op_targ,
					    cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
			    else
          21    		return varname(gv, '@', o->op_targ, Nullsv,
					    SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
			    ;
			}
			else  {
			    /* index is an expression;
			     * attempt to find a match within the aggregate */
          27    	    if (obase->op_type == OP_HELEM) {
          12    		SV * const keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
          12    		if (keysv)
           5    		    return varname(gv, '%', o->op_targ,
								keysv, 0, FUV_SUBSCRIPT_HASH);
			    }
			    else {
          15    		const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
          15    		if (index >= 0)
           5    		    return varname(gv, '@', o->op_targ,
							Nullsv, index, FUV_SUBSCRIPT_ARRAY);
			    }
          17    	    if (match)
           8    		break;
           9    	    return varname(gv,
				(o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
				? '@' : '%',
				o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
			}
		
           1    	break;
		
		    case OP_AASSIGN:
			/* only examine RHS */
           1    	return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
		
		    case OP_OPEN:
           7    	o = cUNOPx(obase)->op_first;
           7    	if (o->op_type == OP_PUSHMARK)
           7    	    o = o->op_sibling;
		
           7    	if (!o->op_sibling) {
			    /* one-arg version of open is highly magical */
		
           4    	    if (o->op_type == OP_GV) { /* open FOO; */
           1    		gv = cGVOPx_gv(o);
           1    		if (match && GvSV(gv) != uninit_sv)
      ######    		    break;
           1    		return varname(gv, '$', 0,
					    Nullsv, 0, FUV_SUBSCRIPT_NONE);
			    }
			    /* other possibilities not handled are:
			     * open $x; or open my $x;	should return '${*$x}'
			     * open expr;		should return '$'.expr ideally
			     */
          44    	     break;
			}
          44    	goto do_op;
		
		    /* ops where $_ may be an implicit arg */
		    case OP_TRANS:
		    case OP_SUBST:
		    case OP_MATCH:
          44    	if ( !(obase->op_flags & OPf_STACKED)) {
          28    	    if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
						 ? PAD_SVl(obase->op_targ)
						 : DEFSV))
			    {
          24    		sv = sv_newmortal();
          24    		sv_setpvn(sv, "$_", 2);
          24    		return sv;
			    }
			}
          30    	goto do_op;
		
		    case OP_PRTF:
		    case OP_PRINT:
			/* skip filehandle as it can't produce 'undef' warning  */
          30    	o = cUNOPx(obase)->op_first;
          30    	if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
          16    	    o = o->op_sibling->op_sibling;
          16    	goto do_op2;
		
		
		    case OP_RV2SV:
		    case OP_CUSTOM:
		    case OP_ENTERSUB:
          15    	match = 1; /* XS or custom code could trigger random warnings */
          15    	goto do_op;
		
		    case OP_SCHOMP:
		    case OP_CHOMP:
          10    	if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
           3    	    return sv_2mortal(newSVpvn("${$/}", 5));
			/* FALL THROUGH */
		
		    default:
		    do_op:
        5944    	if (!(obase->op_flags & OPf_KIDS))
        2029    	    break;
        3915    	o = cUNOPx(obase)->op_first;
			
		    do_op2:
        3945    	if (!o)
      ######    	    break;
		
			/* if all except one arg are constant, or have no side-effects,
			 * or are optimized away, then it's unambiguous */
        3945    	o2 = Nullop;
        8661    	for (kid=o; kid; kid = kid->op_sibling) {
        7031    	    if (kid &&
				(    (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
				  || (kid->op_type == OP_NULL  && ! (kid->op_flags & OPf_KIDS))
				  || (kid->op_type == OP_PUSHMARK)
				)
			    )
        6254    		continue;
        6254    	    if (o2) { /* more than one found */
        2315    		o2 = Nullop;
        2315    		break;
			    }
        3939    	    o2 = kid;
			}
        3945    	if (o2)
        1624    	    return find_uninit_var(o2, uninit_sv, match);
		
			/* scan all args */
        5013    	while (o) {
        4849    	    sv = find_uninit_var(o, uninit_sv, 1);
        4849    	    if (sv)
        2157    		return sv;
        2692    	    o = o->op_sibling;
			}
        2701    	break;
		    }
        2701        return Nullsv;
		}
		
		
		/*
		=for apidoc report_uninit
		
		Print appropriate "Use of uninitialized variable" warning
		
		=cut
		*/
		
		void
		Perl_report_uninit(pTHX_ SV* uninit_sv)
        2877    {
        2877        if (PL_op) {
        2877    	SV* varname = Nullsv;
        2877    	if (uninit_sv) {
        2877    	    varname = find_uninit_var(PL_op, uninit_sv,0);
        2877    	    if (varname)
        2776    		sv_insert(varname, 0, 0, " ", 1);
			}
        2877    	Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
				varname ? SvPV_nolen_const(varname) : "",
				" in ", OP_DESC(PL_op));
		    }
		    else
      ######    	Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
				    "", "", "");
		}
		
		STATIC void *
		S_more_bodies (pTHX_ void **arena_root, void **root, size_t size)
       97801    {
       97801        char *start;
       97801        const char *end;
       97801        const size_t count = PERL_ARENA_SIZE/size;
       97801        New(0, start, count*size, char);
       97801        *((void **) start) = *arena_root;
       97801        *arena_root = (void *)start;
		
       97801        end = start + (count-1) * size;
		
		    /* The initial slot is used to link the arenas together, so it isn't to be
		       linked into the list of ready-to-use bodies.  */
		
       97801        start += size;
		
       97801        *root = (void *)start;
		
    20690665        while (start < end) {
    20592864    	char * const next = start + size;
    20592864    	*(void**) start = (void *)next;
    20592864    	start = next;
		    }
       97801        *(void **)start = 0;
		
       97801        return *root;
		}
		
		/* grab a new thing from the free list, allocating more if necessary */
		
		STATIC void *
		S_new_body(pTHX_ void **arena_root, void **root, size_t size)
    57898079    {
    57898079        void *xpv;
		    LOCK_SV_MUTEX;
    57898079        xpv = *root ? *root : S_more_bodies(aTHX_ arena_root, root, size);
    57898079        *root = *(void**)xpv;
		    UNLOCK_SV_MUTEX;
    57898079        return xpv;
		}
		
		/* return a thing to the free list */
		
		#define del_body(thing, root)			\
		    STMT_START {				\
			void **thing_copy = (void **)thing;	\
			LOCK_SV_MUTEX;				\
			*thing_copy = *root;			\
			*root = (void*)thing_copy;		\
			UNLOCK_SV_MUTEX;			\
		    } STMT_END
		
		/* Conventionally we simply malloc() a big block of memory, then divide it
		   up into lots of the thing that we're allocating.
		
		   This macro will expand to call to S_new_body. So for XPVBM (with ithreads),
		   it would become
		
		   S_new_body(my_perl, (void**)&(my_perl->Ixpvbm_arenaroot),
			      (void**)&(my_perl->Ixpvbm_root), sizeof(XPVBM), 0)
		*/
		
		#define new_body(TYPE,lctype)						\
		    S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot,		\
				 (void**)&PL_ ## lctype ## _root,			\
				 sizeof(TYPE))
		
		#define del_body_type(p,TYPE,lctype)			\
		    del_body((void*)p, (void**)&PL_ ## lctype ## _root)
		
		/* But for some types, we cheat. The type starts with some members that are
		   never accessed. So we allocate the substructure, starting at the first used
		   member, then adjust the pointer back in memory by the size of the bit not
		   allocated, so it's as if we allocated the full structure.
		   (But things will all go boom if you write to the part that is "not there",
		   because you'll be overwriting the last members of the preceding structure
		   in memory.)
		
		   We calculate the correction using the STRUCT_OFFSET macro. For example, if
		   xpv_allocated is the same structure as XPV then the two OFFSETs sum to zero,
		   and the pointer is unchanged. If the allocated structure is smaller (no
		   initial NV actually allocated) then the net effect is to subtract the size
		   of the NV from the pointer, to return a new pointer as if an initial NV were
		   actually allocated.
		
		   This is the same trick as was used for NV and IV bodies. Ironically it
		   doesn't need to be used for NV bodies any more, because NV is now at the
		   start of the structure. IV bodies don't need it either, because they are
		   no longer allocated.  */
		
		#define new_body_allocated(TYPE,lctype,member)				\
		    (void*)((char*)S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
					      (void**)&PL_ ## lctype ## _root,		\
					      sizeof(lctype ## _allocated)) -		\
					      STRUCT_OFFSET(TYPE, member)		\
			    + STRUCT_OFFSET(lctype ## _allocated, member))
		
		
		#define del_body_allocated(p,TYPE,lctype,member)			\
		    del_body((void*)((char*)p + STRUCT_OFFSET(TYPE, member)		\
				     - STRUCT_OFFSET(lctype ## _allocated, member)),	\
			     (void**)&PL_ ## lctype ## _root)
		
		#define my_safemalloc(s)	(void*)safemalloc(s)
		#define my_safefree(p)	safefree((char*)p)
		
		#ifdef PURIFY
		
		#define new_XNV()	my_safemalloc(sizeof(XPVNV))
		#define del_XNV(p)	my_safefree(p)
		
		#define new_XPV()	my_safemalloc(sizeof(XPV))
		#define del_XPV(p)	my_safefree(p)
		
		#define new_XPVIV()	my_safemalloc(sizeof(XPVIV))
		#define del_XPVIV(p)	my_safefree(p)
		
		#define new_XPVNV()	my_safemalloc(sizeof(XPVNV))
		#define del_XPVNV(p)	my_safefree(p)
		
		#define new_XPVCV()	my_safemalloc(sizeof(XPVCV))
		#define del_XPVCV(p)	my_safefree(p)
		
		#define new_XPVAV()	my_safemalloc(sizeof(XPVAV))
		#define del_XPVAV(p)	my_safefree(p)
		
		#define new_XPVHV()	my_safemalloc(sizeof(XPVHV))
		#define del_XPVHV(p)	my_safefree(p)
		
		#define new_XPVMG()	my_safemalloc(sizeof(XPVMG))
		#define del_XPVMG(p)	my_safefree(p)
		
		#define new_XPVGV()	my_safemalloc(sizeof(XPVGV))
		#define del_XPVGV(p)	my_safefree(p)
		
		#define new_XPVLV()	my_safemalloc(sizeof(XPVLV))
		#define del_XPVLV(p)	my_safefree(p)
		
		#define new_XPVBM()	my_safemalloc(sizeof(XPVBM))
		#define del_XPVBM(p)	my_safefree(p)
		
		#else /* !PURIFY */
		
		#define new_XNV()	new_body(NV, xnv)
		#define del_XNV(p)	del_body_type(p, NV, xnv)
		
		#define new_XPV()	new_body_allocated(XPV, xpv, xpv_cur)
		#define del_XPV(p)	del_body_allocated(p, XPV, xpv, xpv_cur)
		
		#define new_XPVIV()	new_body_allocated(XPVIV, xpviv, xpv_cur)
		#define del_XPVIV(p)	del_body_allocated(p, XPVIV, xpviv, xpv_cur)
		
		#define new_XPVNV()	new_body(XPVNV, xpvnv)
		#define del_XPVNV(p)	del_body_type(p, XPVNV, xpvnv)
		
		#define new_XPVCV()	new_body(XPVCV, xpvcv)
		#define del_XPVCV(p)	del_body_type(p, XPVCV, xpvcv)
		
		#define new_XPVAV()	new_body_allocated(XPVAV, xpvav, xav_fill)
		#define del_XPVAV(p)	del_body_allocated(p, XPVAV, xpvav, xav_fill)
		
		#define new_XPVHV()	new_body_allocated(XPVHV, xpvhv, xhv_fill)
		#define del_XPVHV(p)	del_body_allocated(p, XPVHV, xpvhv, xhv_fill)
		
		#define new_XPVMG()	new_body(XPVMG, xpvmg)
		#define del_XPVMG(p)	del_body_type(p, XPVMG, xpvmg)
		
		#define new_XPVGV()	new_body(XPVGV, xpvgv)
		#define del_XPVGV(p)	del_body_type(p, XPVGV, xpvgv)
		
		#define new_XPVLV()	new_body(XPVLV, xpvlv)
		#define del_XPVLV(p)	del_body_type(p, XPVLV, xpvlv)
		
		#define new_XPVBM()	new_body(XPVBM, xpvbm)
		#define del_XPVBM(p)	del_body_type(p, XPVBM, xpvbm)
		
		#endif /* PURIFY */
		
		#define new_XPVFM()	my_safemalloc(sizeof(XPVFM))
		#define del_XPVFM(p)	my_safefree(p)
		
		#define new_XPVIO()	my_safemalloc(sizeof(XPVIO))
		#define del_XPVIO(p)	my_safefree(p)
		
		/*
		=for apidoc sv_upgrade
		
		Upgrade an SV to a more complex form.  Generally adds a new body type to the
		SV, then copies across as much information as possible from the old body.
		You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
		
		=cut
		*/
		
		void
		Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
    86785603    {
    86785603        void**	old_body_arena;
    86785603        size_t	old_body_offset;
    86785603        size_t	old_body_length;	/* Well, the length to copy.  */
    86785603        void*	old_body;
		#ifndef NV_ZERO_IS_ALLBITS_ZERO
		    /* If NV 0.0 is store as all bits 0 then Zero() already creates a correct
		       0.0 for us.  */
		    bool	zero_nv = TRUE;
		#endif
    86785603        void*	new_body;
    86785603        size_t	new_body_length;
    86785603        size_t	new_body_offset;
    86785603        void**	new_body_arena;
    86785603        void**	new_body_arenaroot;
    86785603        const U32	old_type = SvTYPE(sv);
		
    86785603        if (mt != SVt_PV && SvIsCOW(sv)) {
           3    	sv_force_normal_flags(sv, 0);
		    }
		
    86785603        if (SvTYPE(sv) == mt)
        4459    	return;
		
    86781144        if (SvTYPE(sv) > mt)
      ######    	Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
				(int)SvTYPE(sv), (int)mt);
		
		
    86781144        old_body = SvANY(sv);
    86781144        old_body_arena = 0;
    86781144        old_body_offset = 0;
    86781144        old_body_length = 0;
    86781144        new_body_offset = 0;
    86781144        new_body_length = ~0;
		
		    /* Copying structures onto other structures that have been neatly zeroed
		       has a subtle gotcha. Consider XPVMG
		
		       +------+------+------+------+------+-------+-------+
		       |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH |
		       +------+------+------+------+------+-------+-------+
		       0      4      8     12     16     20      24      28
		
		       where NVs are aligned to 8 bytes, so that sizeof that structure is
		       actually 32 bytes long, with 4 bytes of padding at the end:
		
		       +------+------+------+------+------+-------+-------+------+
		       |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH | ???  |
		       +------+------+------+------+------+-------+-------+------+
		       0      4      8     12     16     20      24      28     32
		
		       so what happens if you allocate memory for this structure:
		
		       +------+------+------+------+------+-------+-------+------+------+...
		       |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH |  GP  | NAME |
		       +------+------+------+------+------+-------+-------+------+------+...
		       0      4      8     12     16     20      24      28     32     36
		
		       zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
		       expect, because you copy the area marked ??? onto GP. Now, ??? may have
		       started out as zero once, but it's quite possible that it isn't. So now,
		       rather than a nicely zeroed GP, you have it pointing somewhere random.
		       Bugs ensue.
		
		       (In fact, GP ends up pointing at a previous GP structure, because the
		       principle cause of the padding in XPVMG getting garbage is a copy of
		       sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob)
		
		       So we are careful and work out the size of used parts of all the
		       structures.  */
		
    86781144        switch (SvTYPE(sv)) {
		    case SVt_NULL:
     1820351    	break;
		    case SVt_IV:
     1820351    	if (mt == SVt_NV)
         581    	    mt = SVt_PVNV;
     1819770    	else if (mt < SVt_PVIV)
       21998    	    mt = SVt_PVIV;
     1820351    	old_body_offset = STRUCT_OFFSET(XPVIV, xiv_iv);
     1820351    	old_body_length = sizeof(IV);
     1820351    	break;
		    case SVt_NV:
       77987    	old_body_arena = (void **) &PL_xnv_root;
       77987    	old_body_length = sizeof(NV);
		#ifndef NV_ZERO_IS_ALLBITS_ZERO
			zero_nv = FALSE;
		#endif
       77987    	if (mt < SVt_PVNV)
          12    	    mt = SVt_PVNV;
          12    	break;
		    case SVt_RV:
     5797837    	break;
		    case SVt_PV:
     5797837    	old_body_arena = (void **) &PL_xpv_root;
     5797837    	old_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
			    - STRUCT_OFFSET(xpv_allocated, xpv_cur);
     5797837    	old_body_length = STRUCT_OFFSET(XPV, xpv_len)
			    + sizeof (((XPV*)SvANY(sv))->xpv_len)
			    - old_body_offset;
     5797837    	if (mt <= SVt_IV)
      ######    	    mt = SVt_PVIV;
     5797837    	else if (mt == SVt_NV)
      ######    	    mt = SVt_PVNV;
      ######    	break;
		    case SVt_PVIV:
       50410    	old_body_arena = (void **) &PL_xpviv_root;
       50410    	old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
			    - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
       50410    	old_body_length =  STRUCT_OFFSET(XPVIV, xiv_u)
			    + sizeof (((XPVIV*)SvANY(sv))->xiv_u)
			    - old_body_offset;
       50410    	break;
		    case SVt_PVNV:
        1764    	old_body_arena = (void **) &PL_xpvnv_root;
        1764    	old_body_length = STRUCT_OFFSET(XPVNV, xiv_u)
			    + sizeof (((XPVNV*)SvANY(sv))->xiv_u);
		#ifndef NV_ZERO_IS_ALLBITS_ZERO
			zero_nv = FALSE;
		#endif
        1764    	break;
		    case SVt_PVMG:
			/* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
			   there's no way that it can be safely upgraded, because perl.c
			   expects to Safefree(SvANY(PL_mess_sv))  */
       37321    	assert(sv != PL_mess_sv);
			/* This flag bit is used to mean other things in other scalar types.
			   Given that it only has meaning inside the pad, it shouldn't be set
			   on anything that can get upgraded.  */
       37321    	assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
       37321    	old_body_arena = (void **) &PL_xpvmg_root;
       37321    	old_body_length = STRUCT_OFFSET(XPVMG, xmg_stash)
			    + sizeof (((XPVMG*)SvANY(sv))->xmg_stash);
		#ifndef NV_ZERO_IS_ALLBITS_ZERO
			zero_nv = FALSE;
		#endif
       37321    	break;
		    default:
      ######    	Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
		    }
		
    86781144        SvFLAGS(sv) &= ~SVTYPEMASK;
    86781144        SvFLAGS(sv) |= mt;
		
    86781144        switch (mt) {
		    case SVt_NULL:
      ######    	Perl_croak(aTHX_ "Can't upgrade to undef");
		    case SVt_IV:
    22115837    	assert(old_type == SVt_NULL);
    22115837    	SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
    22115837    	SvIV_set(sv, 0);
    22115837    	return;
		    case SVt_NV:
     1669529    	assert(old_type == SVt_NULL);
     1669529    	SvANY(sv) = new_XNV();
     1669529    	SvNV_set(sv, 0);
     1669529    	return;
		    case SVt_RV:
     6721187    	assert(old_type == SVt_NULL);
     6721187    	SvANY(sv) = &sv->sv_u.svu_rv;
     6721187    	SvRV_set(sv, 0);
     6721187    	return;
		    case SVt_PVHV:
     1306224    	SvANY(sv) = new_XPVHV();
     1306224    	HvFILL(sv)	= 0;
     1306224    	HvMAX(sv)	= 0;
     1306224    	HvTOTALKEYS(sv)	= 0;
		
     1306224    	goto hv_av_common;
		
		    case SVt_PVAV:
     4065780    	SvANY(sv) = new_XPVAV();
     4065780    	AvMAX(sv)	= -1;
     4065780    	AvFILLp(sv)	= -1;
     4065780    	AvALLOC(sv)	= 0;
     4065780    	AvREAL_only(sv);
		
		    hv_av_common:
			/* SVt_NULL isn't the only thing upgraded to AV or HV.
			   The target created by newSVrv also is, and it can have magic.
			   However, it never has SvPVX set.
			*/
     5372004    	if (old_type >= SVt_RV) {
       21276    	    assert(SvPVX_const(sv) == 0);
			}
		
			/* Could put this in the else clause below, as PVMG must have SvPVX
			   0 already (the assertion above)  */
     5372004    	SvPV_set(sv, (char*)0);
		
     5372004    	if (old_type >= SVt_PVMG) {
       21276    	    SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_magic);
       21276    	    SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
			} else {
     5350728    	    SvMAGIC_set(sv, 0);
     5350728    	    SvSTASH_set(sv, 0);
			}
     5350728    	break;
		
		    case SVt_PVIO:
       39393    	new_body = new_XPVIO();
       39393    	new_body_length = sizeof(XPVIO);
       39393    	goto zero;
		    case SVt_PVFM:
        9129    	new_body = new_XPVFM();
        9129    	new_body_length = sizeof(XPVFM);
        9129    	goto zero;
		
		    case SVt_PVBM:
      232693    	new_body_length = sizeof(XPVBM);
      232693    	new_body_arena = (void **) &PL_xpvbm_root;
      232693    	new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
      232693    	goto new_body;
		    case SVt_PVGV:
     1462808    	new_body_length = sizeof(XPVGV);
     1462808    	new_body_arena = (void **) &PL_xpvgv_root;
     1462808    	new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
     1462808    	goto new_body;
		    case SVt_PVCV:
      781202    	new_body_length = sizeof(XPVCV);
      781202    	new_body_arena = (void **) &PL_xpvcv_root;
      781202    	new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
      781202    	goto new_body;
		    case SVt_PVLV:
      480976    	new_body_length = sizeof(XPVLV);
      480976    	new_body_arena = (void **) &PL_xpvlv_root;
      480976    	new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
      480976    	goto new_body;
		    case SVt_PVMG:
     6035096    	new_body_length = sizeof(XPVMG);
     6035096    	new_body_arena = (void **) &PL_xpvmg_root;
     6035096    	new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
     6035096    	goto new_body;
		    case SVt_PVNV:
     3173409    	new_body_length = sizeof(XPVNV);
     3173409    	new_body_arena = (void **) &PL_xpvnv_root;
     3173409    	new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
     3173409    	goto new_body;
		    case SVt_PVIV:
     4924769    	new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
			    - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
     4924769    	new_body_length = sizeof(XPVIV) - new_body_offset;
     4924769    	new_body_arena = (void **) &PL_xpviv_root;
     4924769    	new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
			/* XXX Is this still needed?  Was it ever needed?   Surely as there is
			   no route from NV to PVIV, NOK can never be true  */
     4924769    	if (SvNIOK(sv))
     1410032    	    (void)SvIOK_on(sv);
     4924769    	SvNOK_off(sv);
     4924769    	goto new_body_no_NV; 
		    case SVt_PV:
    33763112    	new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
			    - STRUCT_OFFSET(xpv_allocated, xpv_cur);
    33763112    	new_body_length = sizeof(XPV) - new_body_offset;
    33763112    	new_body_arena = (void **) &PL_xpv_root;
    33763112    	new_body_arenaroot = (void **) &PL_xpv_arenaroot;
		    new_body_no_NV:
			/* PV and PVIV don't have an NV slot.  */
		#ifndef NV_ZERO_IS_ALLBITS_ZERO
			zero_nv = FALSE;
		#endif
		
		    new_body:
    50854065    	assert(new_body_length);
		#ifndef PURIFY
			/* This points to the start of the allocated area.  */
    50854065    	new_body = S_new_body(aTHX_ new_body_arenaroot, new_body_arena,
					      new_body_length);
		#else
			/* We always allocated the full length item with PURIFY */
			new_body_length += new_body_offset;
			new_body_offset = 0;
			new_body = my_safemalloc(new_body_length);
		
		#endif
		    zero:
    50902587    	Zero(new_body, new_body_length, char);
    50902587    	new_body = ((char *)new_body) - new_body_offset;
    50902587    	SvANY(sv) = new_body;
		
    50902587    	if (old_body_length) {
			    Copy((char *)old_body + old_body_offset,
				 (char *)new_body + old_body_offset,
     7764394    		 old_body_length, char);
			}
		
		#ifndef NV_ZERO_IS_ALLBITS_ZERO
			if (zero_nv)
			    SvNV_set(sv, 0);
		#endif
		
    50902587    	if (mt == SVt_PVIO)
       39393    	    IoPAGE_LEN(sv)	= 60;
    50902587    	if (old_type < SVt_RV)
    44989656    	    SvPV_set(sv, 0);
    44989656    	break;
		    default:
      ######    	Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt);
		    }
		
		
    56274591        if (old_body_arena) {
		#ifdef PURIFY
			my_safefree(old_body);
		#else
			del_body((void*)((char*)old_body + old_body_offset),
     5965319    		 old_body_arena);
		#endif
		    }
		}
		
		/*
		=for apidoc sv_backoff
		
		Remove any string offset. You should normally use the C<SvOOK_off> macro
		wrapper instead.
		
		=cut
		*/
		
		int
		Perl_sv_backoff(pTHX_ register SV *sv)
      249730    {
      249730        assert(SvOOK(sv));
      249730        assert(SvTYPE(sv) != SVt_PVHV);
      249730        assert(SvTYPE(sv) != SVt_PVAV);
      249730        if (SvIVX(sv)) {
      249527    	const char * const s = SvPVX_const(sv);
      249527    	SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
      249527    	SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
      249527    	SvIV_set(sv, 0);
      249527    	Move(s, SvPVX(sv), SvCUR(sv)+1, char);
		    }
      249730        SvFLAGS(sv) &= ~SVf_OOK;
      249730        return 0;
		}
		
		/*
		=for apidoc sv_grow
		
		Expands the character buffer in the SV.  If necessary, uses C<sv_unref> and
		upgrades the SV to C<SVt_PV>.  Returns a pointer to the character buffer.
		Use the C<SvGROW> wrapper instead.
		
		=cut
		*/
		
		char *
		Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
    42813860    {
    42813860        register char *s;
		
		#ifdef HAS_64K_LIMIT
		    if (newlen >= 0x10000) {
			PerlIO_printf(Perl_debug_log,
				      "Allocation too large: %"UVxf"\n", (UV)newlen);
			my_exit(1);
		    }
		#endif /* HAS_64K_LIMIT */
    42813860        if (SvROK(sv))
      ######    	sv_unref(sv);
    42813860        if (SvTYPE(sv) < SVt_PV) {
        4504    	sv_upgrade(sv, SVt_PV);
        4504    	s = SvPVX_mutable(sv);
		    }
    42809356        else if (SvOOK(sv)) {	/* pv is offset? */
        9748    	sv_backoff(sv);
        9748    	s = SvPVX_mutable(sv);
        9748    	if (newlen > SvLEN(sv))
         596    	    newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
		#ifdef HAS_64K_LIMIT
			if (newlen >= 0x10000)
			    newlen = 0xFFFF;
		#endif
		    }
		    else
    42799608    	s = SvPVX_mutable(sv);
		
    42813860        if (newlen > SvLEN(sv)) {		/* need more room? */
    42804264    	newlen = PERL_STRLEN_ROUNDUP(newlen);
    42804264    	if (SvLEN(sv) && s) {
		#ifdef MYMALLOC
			    const STRLEN l = malloced_size((void*)SvPVX_const(sv));
			    if (newlen <= l) {
				SvLEN_set(sv, l);
				return s;
			    } else
		#endif
     6684289    	    s = saferealloc(s, newlen);
			}
			else {
    36119975    	    s = safemalloc(newlen);
    36119975    	    if (SvPVX_const(sv) && SvCUR(sv)) {
          55    	        Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
			    }
			}
    42804264    	SvPV_set(sv, s);
    42804264            SvLEN_set(sv, newlen);
		    }
    42813860        return s;
		}
		
		/*
		=for apidoc sv_setiv
		
		Copies an integer into the given SV, upgrading first if necessary.
		Does not handle 'set' magic.  See also C<sv_setiv_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setiv(pTHX_ register SV *sv, IV i)
    77038730    {
    77038730        SV_CHECK_THINKFIRST_COW_DROP(sv);
    77038730        switch (SvTYPE(sv)) {
		    case SVt_NULL:
     5302918    	sv_upgrade(sv, SVt_IV);
     5302918    	break;
		    case SVt_NV:
          94    	sv_upgrade(sv, SVt_PVNV);
          94    	break;
		    case SVt_RV:
		    case SVt_PV:
          49    	sv_upgrade(sv, SVt_PVIV);
          49    	break;
		
		    case SVt_PVGV:
		    case SVt_PVAV:
		    case SVt_PVHV:
		    case SVt_PVCV:
		    case SVt_PVFM:
		    case SVt_PVIO:
      ######    	Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
				   OP_DESC(PL_op));
		    }
    77038730        (void)SvIOK_only(sv);			/* validate number */
    77038730        SvIV_set(sv, i);
    77038730        SvTAINT(sv);
		}
		
		/*
		=for apidoc sv_setiv_mg
		
		Like C<sv_setiv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
          13    {
          13        sv_setiv(sv,i);
          13        SvSETMAGIC(sv);
		}
		
		/*
		=for apidoc sv_setuv
		
		Copies an unsigned integer into the given SV, upgrading first if necessary.
		Does not handle 'set' magic.  See also C<sv_setuv_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setuv(pTHX_ register SV *sv, UV u)
    32637341    {
		    /* With these two if statements:
		       u=1.49  s=0.52  cu=72.49  cs=10.64  scripts=270  tests=20865
		
		       without
		       u=1.35  s=0.47  cu=73.45  cs=11.43  scripts=270  tests=20865
		
		       If you wish to remove them, please benchmark to see what the effect is
		    */
    32637341        if (u <= (UV)IV_MAX) {
    32546899           sv_setiv(sv, (IV)u);
    32546899           return;
		    }
       90442        sv_setiv(sv, 0);
       90442        SvIsUV_on(sv);
       90442        SvUV_set(sv, u);
		}
		
		/*
		=for apidoc sv_setuv_mg
		
		Like C<sv_setuv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
          14    {
		    /* With these two if statements:
		       u=1.49  s=0.52  cu=72.49  cs=10.64  scripts=270  tests=20865
		
		       without
		       u=1.35  s=0.47  cu=73.45  cs=11.43  scripts=270  tests=20865
		
		       If you wish to remove them, please benchmark to see what the effect is
		    */
          14        if (u <= (UV)IV_MAX) {
          14           sv_setiv(sv, (IV)u);
		    } else {
      ######           sv_setiv(sv, 0);
      ######           SvIsUV_on(sv);
      ######           sv_setuv(sv,u);
		    }
          14        SvSETMAGIC(sv);
		}
		
		/*
		=for apidoc sv_setnv
		
		Copies a double into the given SV, upgrading first if necessary.
		Does not handle 'set' magic.  See also C<sv_setnv_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setnv(pTHX_ register SV *sv, NV num)
     5173748    {
     5173748        SV_CHECK_THINKFIRST_COW_DROP(sv);
     5173748        switch (SvTYPE(sv)) {
		    case SVt_NULL:
		    case SVt_IV:
      876086    	sv_upgrade(sv, SVt_NV);
      876086    	break;
		    case SVt_RV:
		    case SVt_PV:
		    case SVt_PVIV:
          14    	sv_upgrade(sv, SVt_PVNV);
          14    	break;
		
		    case SVt_PVGV:
		    case SVt_PVAV:
		    case SVt_PVHV:
		    case SVt_PVCV:
		    case SVt_PVFM:
		    case SVt_PVIO:
      ######    	Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
				   OP_NAME(PL_op));
		    }
     5173748        SvNV_set(sv, num);
     5173748        (void)SvNOK_only(sv);			/* validate number */
     5173748        SvTAINT(sv);
		}
		
		/*
		=for apidoc sv_setnv_mg
		
		Like C<sv_setnv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
          13    {
          13        sv_setnv(sv,num);
          13        SvSETMAGIC(sv);
		}
		
		/* Print an "isn't numeric" warning, using a cleaned-up,
		 * printable version of the offending string
		 */
		
		STATIC void
		S_not_a_number(pTHX_ SV *sv)
          16    {
          16         SV *dsv;
          16         char tmpbuf[64];
          16         const char *pv;
		
          16         if (DO_UTF8(sv)) {
           2              dsv = sv_2mortal(newSVpvn("", 0));
           2              pv = sv_uni_display(dsv, sv, 10, 0);
		     } else {
          14    	  char *d = tmpbuf;
          14    	  char *limit = tmpbuf + sizeof(tmpbuf) - 8;
			  /* each *s can expand to 4 chars + "...\0",
			     i.e. need room for 8 chars */
			
          14    	  const char *s, *end;
          78    	  for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
			       s++) {
          64    	       int ch = *s & 0xFF;
          64    	       if (ch & 128 && !isPRINT_LC(ch)) {
      ######    		    *d++ = 'M';
      ######    		    *d++ = '-';
      ######    		    ch &= 127;
			       }
          64    	       if (ch == '\n') {
      ######    		    *d++ = '\\';
      ######    		    *d++ = 'n';
			       }
          64    	       else if (ch == '\r') {
      ######    		    *d++ = '\\';
      ######    		    *d++ = 'r';
			       }
          64    	       else if (ch == '\f') {
      ######    		    *d++ = '\\';
      ######    		    *d++ = 'f';
			       }
          64    	       else if (ch == '\\') {
      ######    		    *d++ = '\\';
      ######    		    *d++ = '\\';
			       }
          64    	       else if (ch == '\0') {
           1    		    *d++ = '\\';
           1    		    *d++ = '0';
			       }
          63    	       else if (isPRINT_LC(ch))
          63    		    *d++ = ch;
			       else {
      ######    		    *d++ = '^';
      ######    		    *d++ = toCTRL(ch);
			       }
			  }
          14    	  if (s < end) {
      ######    	       *d++ = '.';
      ######    	       *d++ = '.';
      ######    	       *d++ = '.';
			  }
          14    	  *d = '\0';
          14    	  pv = tmpbuf;
		    }
		
          16        if (PL_op)
          16    	Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
				    "Argument \"%s\" isn't numeric in %s", pv,
				    OP_DESC(PL_op));
		    else
      ######    	Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
				    "Argument \"%s\" isn't numeric", pv);
		}
		
		/*
		=for apidoc looks_like_number
		
		Test if the content of an SV looks like a number (or is a number).
		C<Inf> and C<Infinity> are treated as numbers (so will not issue a
		non-numeric warning), even if your atof() doesn't grok them.
		
		=cut
		*/
		
		I32
		Perl_looks_like_number(pTHX_ SV *sv)
          86    {
          86        register const char *sbegin;
          86        STRLEN len;
		
          86        if (SvPOK(sv)) {
          84    	sbegin = SvPVX_const(sv);
          84    	len = SvCUR(sv);
		    }
           2        else if (SvPOKp(sv))
           1    	sbegin = SvPV_const(sv, len);
		    else
           1    	return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
          85        return grok_number(sbegin, len, NULL);
		}
		
		/* Actually, ISO C leaves conversion of UV to IV undefined, but
		   until proven guilty, assume that things are not that bad... */
		
		/*
		   NV_PRESERVES_UV:
		
		   As 64 bit platforms often have an NV that doesn't preserve all bits of
		   an IV (an assumption perl has been based on to date) it becomes necessary
		   to remove the assumption that the NV always carries enough precision to
		   recreate the IV whenever needed, and that the NV is the canonical form.
		   Instead, IV/UV and NV need to be given equal rights. So as to not lose
		   precision as a side effect of conversion (which would lead to insanity
		   and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
		   1) to distinguish between IV/UV/NV slots that have cached a valid
		      conversion where precision was lost and IV/UV/NV slots that have a
		      valid conversion which has lost no precision
		   2) to ensure that if a numeric conversion to one form is requested that
		      would lose precision, the precise conversion (or differently
		      imprecise conversion) is also performed and cached, to prevent
		      requests for different numeric formats on the same SV causing
		      lossy conversion chains. (lossless conversion chains are perfectly
		      acceptable (still))
		
		
		   flags are used:
		   SvIOKp is true if the IV slot contains a valid value
		   SvIOK  is true only if the IV value is accurate (UV if SvIOK_UV true)
		   SvNOKp is true if the NV slot contains a valid value
		   SvNOK  is true only if the NV value is accurate
		
		   so
		   while converting from PV to NV, check to see if converting that NV to an
		   IV(or UV) would lose accuracy over a direct conversion from PV to
		   IV(or UV). If it would, cache both conversions, return NV, but mark
		   SV as IOK NOKp (ie not NOK).
		
		   While converting from PV to IV, check to see if converting that IV to an
		   NV would lose accuracy over a direct conversion from PV to NV. If it
		   would, cache both conversions, flag similarly.
		
		   Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
		   correctly because if IV & NV were set NV *always* overruled.
		   Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
		   changes - now IV and NV together means that the two are interchangeable:
		   SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
		
		   The benefit of this is that operations such as pp_add know that if
		   SvIOK is true for both left and right operands, then integer addition
		   can be used instead of floating point (for cases where the result won't
		   overflow). Before, floating point was always used, which could lead to
		   loss of precision compared with integer addition.
		
		   * making IV and NV equal status should make maths accurate on 64 bit
		     platforms
		   * may speed up maths somewhat if pp_add and friends start to use
		     integers when possible instead of fp. (Hopefully the overhead in
		     looking for SvIOK and checking for overflow will not outweigh the
		     fp to integer speedup)
		   * will slow down integer operations (callers of SvIV) on "inaccurate"
		     values, as the change from SvIOK to SvIOKp will cause a call into
		     sv_2iv each time rather than a macro access direct to the IV slot
		   * should speed up number->string conversion on integers as IV is
		     favoured when IV and NV are equally accurate
		
		   ####################################################################
		   You had better be using SvIOK_notUV if you want an IV for arithmetic:
		   SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
		   On the other hand, SvUOK is true iff UV.
		   ####################################################################
		
		   Your mileage will vary depending your CPU's relative fp to integer
		   performance ratio.
		*/
		
		#ifndef NV_PRESERVES_UV
		#  define IS_NUMBER_UNDERFLOW_IV 1
		#  define IS_NUMBER_UNDERFLOW_UV 2
		#  define IS_NUMBER_IV_AND_UV    2
		#  define IS_NUMBER_OVERFLOW_IV  4
		#  define IS_NUMBER_OVERFLOW_UV  5
		
		/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
		
		/* For sv_2nv these three cases are "SvNOK and don't bother casting"  */
		STATIC int
		S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
		{
		    DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
		    if (SvNVX(sv) < (NV)IV_MIN) {
			(void)SvIOKp_on(sv);
			(void)SvNOK_on(sv);
			SvIV_set(sv, IV_MIN);
			return IS_NUMBER_UNDERFLOW_IV;
		    }
		    if (SvNVX(sv) > (NV)UV_MAX) {
			(void)SvIOKp_on(sv);
			(void)SvNOK_on(sv);
			SvIsUV_on(sv);
			SvUV_set(sv, UV_MAX);
			return IS_NUMBER_OVERFLOW_UV;
		    }
		    (void)SvIOKp_on(sv);
		    (void)SvNOK_on(sv);
		    /* Can't use strtol etc to convert this string.  (See truth table in
		       sv_2iv  */
		    if (SvNVX(sv) <= (UV)IV_MAX) {
		        SvIV_set(sv, I_V(SvNVX(sv)));
		        if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
		            SvIOK_on(sv); /* Integer is precise. NOK, IOK */
		        } else {
		            /* Integer is imprecise. NOK, IOKp */
		        }
		        return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
		    }
		    SvIsUV_on(sv);
		    SvUV_set(sv, U_V(SvNVX(sv)));
		    if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
		        if (SvUVX(sv) == UV_MAX) {
		            /* As we know that NVs don't preserve UVs, UV_MAX cannot
		               possibly be preserved by NV. Hence, it must be overflow.
		               NOK, IOKp */
		            return IS_NUMBER_OVERFLOW_UV;
		        }
		        SvIOK_on(sv); /* Integer is precise. NOK, UOK */
		    } else {
		        /* Integer is imprecise. NOK, IOKp */
		    }
		    return IS_NUMBER_OVERFLOW_IV;
		}
		#endif /* !NV_PRESERVES_UV*/
		
		/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
		 * this function provided for binary compatibility only
		 */
		
		IV
		Perl_sv_2iv(pTHX_ register SV *sv)
      ######    {
      ######        return sv_2iv_flags(sv, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_2iv_flags
		
		Return the integer value of an SV, doing any necessary string
		conversion.  If flags includes SV_GMAGIC, does an mg_get() first.
		Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
		
		=cut
		*/
		
		IV
		Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
     4688460    {
     4688460        if (!sv)
      ######    	return 0;
     4688460        if (SvGMAGICAL(sv)) {
       11412    	if (flags & SV_GMAGIC)
       11399    	    mg_get(sv);
       11412    	if (SvIOKp(sv))
       11197    	    return SvIVX(sv);
         215    	if (SvNOKp(sv)) {
          14    	    return I_V(SvNVX(sv));
			}
         201    	if (SvPOKp(sv) && SvLEN(sv))
         188    	    return asIV(sv);
          13    	if (!SvROK(sv)) {
          13    	    if (!(SvFLAGS(sv) & SVs_PADTMP)) {
          13    		if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
          10    		    report_uninit(sv);
			    }
          13    	    return 0;
			}
		    }
     4677048        if (SvTHINKFIRST(sv)) {
       21737    	if (SvROK(sv)) {
         349    	  SV* tmpstr;
         349              if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
		                (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
         323    	      return SvIV(tmpstr);
          26    	  return PTR2IV(SvRV(sv));
			}
       21388    	if (SvIsCOW(sv)) {
       17783    	    sv_force_normal_flags(sv, 0);
			}
       21388    	if (SvREADONLY(sv) && !SvOK(sv)) {
         117    	    if (ckWARN(WARN_UNINITIALIZED))
           5    		report_uninit(sv);
         117    	    return 0;
			}
		    }
     4676582        if (SvIOKp(sv)) {
       46431    	if (SvIsUV(sv)) {
         490    	    return (IV)(SvUVX(sv));
			}
			else {
       45941    	    return SvIVX(sv);
			}
		    }
     4630151        if (SvNOKp(sv)) {
			/* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
			 * without also getting a cached IV/UV from it at the same time
			 * (ie PV->NV conversion should detect loss of accuracy and cache
			 * IV or UV at same time to avoid this.  NWC */
		
     3763876    	if (SvTYPE(sv) == SVt_NV)
       46410    	    sv_upgrade(sv, SVt_PVNV);
		
     3763876    	(void)SvIOKp_on(sv);	/* Must do this first, to clear any SvOOK */
			/* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
			   certainly cast into the IV range at IV_MAX, whereas the correct
			   answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
			   cases go to UV */
     3763876    	if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
     2836720    	    SvIV_set(sv, I_V(SvNVX(sv)));
     2836720    	    if (SvNVX(sv) == (NV) SvIVX(sv)
		#ifndef NV_PRESERVES_UV
				&& (((UV)1 << NV_PRESERVES_UV_BITS) >
				    (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
				/* Don't flag it as "accurately an integer" if the number
				   came from a (by definition imprecise) NV operation, and
				   we're outside the range of NV integer precision */
		#endif
				) {
     1834156    		SvIOK_on(sv);  /* Can this go wrong with rounding? NWC */
				DEBUG_c(PerlIO_printf(Perl_debug_log,
						      "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
						      PTR2UV(sv),
						      SvNVX(sv),
     1834156    				      SvIVX(sv)));
		
			    } else {
				/* IV not precise.  No need to convert from PV, as NV
				   conversion would already have cached IV if it detected
				   that PV->IV would be better than PV->NV->IV
				   flags already correct - don't set public IOK.  */
				DEBUG_c(PerlIO_printf(Perl_debug_log,
						      "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
						      PTR2UV(sv),
						      SvNVX(sv),
     1002564    				      SvIVX(sv)));
			    }
			    /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
			       but the cast (NV)IV_MIN rounds to a the value less (more
			       negative) than IV_MIN which happens to be equal to SvNVX ??
			       Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
			       NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
			       (NV)UVX == NVX are both true, but the values differ. :-(
			       Hopefully for 2s complement IV_MIN is something like
			       0x8000000000000000 which will be exact. NWC */
			}
			else {
      927156    	    SvUV_set(sv, U_V(SvNVX(sv)));
      927156    	    if (
				(SvNVX(sv) == (NV) SvUVX(sv))
		#ifndef  NV_PRESERVES_UV
				/* Make sure it's not 0xFFFFFFFFFFFFFFFF */
				/*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
				&& (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
				/* Don't flag it as "accurately an integer" if the number
				   came from a (by definition imprecise) NV operation, and
				   we're outside the range of NV integer precision */
		#endif
				)
         267    		SvIOK_on(sv);
      927156    	    SvIsUV_on(sv);
			  ret_iv_max:
			    DEBUG_c(PerlIO_printf(Perl_debug_log,
						  "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
						  PTR2UV(sv),
						  SvUVX(sv),
      929064    				  SvUVX(sv)));
      929064    	    return (IV)SvUVX(sv);
			}
		    }
      866275        else if (SvPOKp(sv) && SvLEN(sv)) {
      843969    	UV value;
      843969    	const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
			/* We want to avoid a possible problem when we cache an IV which
			   may be later translated to an NV, and the resulting NV is not
			   the same as the direct translation of the initial string
			   (eg 123.456 can shortcut to the IV 123 with atol(), but we must
			   be careful to ensure that the value with the .456 is around if the
			   NV value is requested in the future).
			
			   This means that if we cache such an IV, we need to cache the
			   NV as well.  Moreover, we trade speed for space, and do not
			   cache the NV if we are sure it's not needed.
			 */
		
			/* SVt_PVNV is one higher than SVt_PVIV, hence this order  */
      843969    	if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			     == IS_NUMBER_IN_UV) {
			    /* It's definitely an integer, only upgrade to PVIV */
      833578    	    if (SvTYPE(sv) < SVt_PVIV)
      404791    		sv_upgrade(sv, SVt_PVIV);
      833578    	    (void)SvIOK_on(sv);
       10391    	} else if (SvTYPE(sv) < SVt_PVNV)
        5076    	    sv_upgrade(sv, SVt_PVNV);
		
			/* If NV preserves UV then we only use the UV value if we know that
			   we aren't going to call atof() below. If NVs don't preserve UVs
			   then the value returned may have more precision than atof() will
			   return, even though value isn't perfectly accurate.  */
      843969    	if ((numtype & (IS_NUMBER_IN_UV
		#ifdef NV_PRESERVES_UV
					| IS_NUMBER_NOT_INT
		#endif
			    )) == IS_NUMBER_IN_UV) {
			    /* This won't turn off the public IOK flag if it was set above  */
      833578    	    (void)SvIOKp_on(sv);
		
      833578    	    if (!(numtype & IS_NUMBER_NEG)) {
				/* positive */;
      820370    		if (value <= (UV)IV_MAX) {
      816217    		    SvIV_set(sv, (IV)value);
				} else {
        4153    		    SvUV_set(sv, value);
        4153    		    SvIsUV_on(sv);
				}
			    } else {
				/* 2s complement assumption  */
       13208    		if (value <= (UV)IV_MIN) {
       11274    		    SvIV_set(sv, -(IV)value);
				} else {
				    /* Too negative for an IV.  This is a double upgrade, but
				       I'm assuming it will be rare.  */
        1934    		    if (SvTYPE(sv) < SVt_PVNV)
      ######    			sv_upgrade(sv, SVt_PVNV);
        1934    		    SvNOK_on(sv);
        1934    		    SvIOK_off(sv);
        1934    		    SvIOKp_on(sv);
        1934    		    SvNV_set(sv, -(NV)value);
        1934    		    SvIV_set(sv, IV_MIN);
				}
			    }
			}
			/* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
		           will be in the previous block to set the IV slot, and the next
		           block to set the NV slot.  So no else here.  */
			
      843969    	if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			    != IS_NUMBER_IN_UV) {
			    /* It wasn't an (integer that doesn't overflow the UV). */
       10391    	    SvNV_set(sv, Atof(SvPVX_const(sv)));
		
       10391    	    if (! numtype && ckWARN(WARN_NUMERIC))
          14    		not_a_number(sv);
		
		#if defined(USE_LONG_DOUBLE)
			    DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
						  PTR2UV(sv), SvNVX(sv)));
		#else
			    DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
       10389    				  PTR2UV(sv), SvNVX(sv)));
		#endif
		
		
		#ifdef NV_PRESERVES_UV
       10389    	    (void)SvIOKp_on(sv);
       10389    	    (void)SvNOK_on(sv);
       10389    	    if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
        8481    		SvIV_set(sv, I_V(SvNVX(sv)));
        8481    		if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
        5767    		    SvIOK_on(sv);
				} else {
				    /* Integer is imprecise. NOK, IOKp */
				}
				/* UV will not work better than IV */
			    } else {
        1908    		if (SvNVX(sv) > (NV)UV_MAX) {
        1908    		    SvIsUV_on(sv);
				    /* Integer is inaccurate. NOK, IOKp, is UV */
        1908    		    SvUV_set(sv, UV_MAX);
        1908    		    SvIsUV_on(sv);
				} else {
      ######    		    SvUV_set(sv, U_V(SvNVX(sv)));
				    /* 0xFFFFFFFFFFFFFFFF not an issue in here */
      ######    		    if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
      ######    			SvIOK_on(sv);
      ######    			SvIsUV_on(sv);
				    } else {
					/* Integer is imprecise. NOK, IOKp, is UV */
      ######    			SvIsUV_on(sv);
				    }
				}
      ######    		goto ret_iv_max;
			    }
		#else /* NV_PRESERVES_UV */
		            if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
		                == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
		                /* The IV slot will have been set from value returned by
		                   grok_number above.  The NV slot has just been set using
		                   Atof.  */
			        SvNOK_on(sv);
		                assert (SvIOKp(sv));
		            } else {
		                if (((UV)1 << NV_PRESERVES_UV_BITS) >
		                    U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
		                    /* Small enough to preserve all bits. */
		                    (void)SvIOKp_on(sv);
		                    SvNOK_on(sv);
		                    SvIV_set(sv, I_V(SvNVX(sv)));
		                    if ((NV)(SvIVX(sv)) == SvNVX(sv))
		                        SvIOK_on(sv);
		                    /* Assumption: first non-preserved integer is < IV_MAX,
		                       this NV is in the preserved range, therefore: */
		                    if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
		                          < (UV)IV_MAX)) {
		                        Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
		                    }
		                } else {
		                    /* IN_UV NOT_INT
		                         0      0	already failed to read UV.
		                         0      1       already failed to read UV.
		                         1      0       you won't get here in this case. IV/UV
		                         	        slot set, public IOK, Atof() unneeded.
		                         1      1       already read UV.
		                       so there's no point in sv_2iuv_non_preserve() attempting
		                       to use atol, strtol, strtoul etc.  */
		                    if (sv_2iuv_non_preserve (sv, numtype)
		                        >= IS_NUMBER_OVERFLOW_IV)
		                    goto ret_iv_max;
		                }
		            }
		#endif /* NV_PRESERVES_UV */
			}
		    } else  {
       22306    	if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
         111    	    report_uninit(sv);
       22306    	if (SvTYPE(sv) < SVt_IV)
			    /* Typically the caller expects that sv_any is not NULL now.  */
          58    	    sv_upgrade(sv, SVt_IV);
       22306    	return 0;
		    }
		    DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
     3678779    	PTR2UV(sv),SvIVX(sv)));
     3678779        return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
		}
		
		/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
		 * this function provided for binary compatibility only
		 */
		
		UV
		Perl_sv_2uv(pTHX_ register SV *sv)
      ######    {
      ######        return sv_2uv_flags(sv, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_2uv_flags
		
		Return the unsigned integer value of an SV, doing any necessary string
		conversion.  If flags includes SV_GMAGIC, does an mg_get() first.
		Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
		
		=cut
		*/
		
		UV
		Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
       97628    {
       97628        if (!sv)
      ######    	return 0;
       97628        if (SvGMAGICAL(sv)) {
       27718    	if (flags & SV_GMAGIC)
          46    	    mg_get(sv);
       27718    	if (SvIOKp(sv))
       27703    	    return SvUVX(sv);
          15    	if (SvNOKp(sv))
           8    	    return U_V(SvNVX(sv));
           7    	if (SvPOKp(sv) && SvLEN(sv))
      ######    	    return asUV(sv);
           7    	if (!SvROK(sv)) {
           7    	    if (!(SvFLAGS(sv) & SVs_PADTMP)) {
           7    		if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
           6    		    report_uninit(sv);
			    }
           7    	    return 0;
			}
		    }
       69910        if (SvTHINKFIRST(sv)) {
          36    	if (SvROK(sv)) {
      ######    	  SV* tmpstr;
      ######              if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
		                (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
      ######    	      return SvUV(tmpstr);
      ######    	  return PTR2UV(SvRV(sv));
			}
          36    	if (SvIsCOW(sv)) {
          18    	    sv_force_normal_flags(sv, 0);
			}
          36    	if (SvREADONLY(sv) && !SvOK(sv)) {
           4    	    if (ckWARN(WARN_UNINITIALIZED))
           2    		report_uninit(sv);
           4    	    return 0;
			}
		    }
       69906        if (SvIOKp(sv)) {
         449    	if (SvIsUV(sv)) {
         118    	    return SvUVX(sv);
			}
			else {
         331    	    return (UV)SvIVX(sv);
			}
		    }
       69457        if (SvNOKp(sv)) {
			/* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
			 * without also getting a cached IV/UV from it at the same time
			 * (ie PV->NV conversion should detect loss of accuracy and cache
			 * IV or UV at same time to avoid this. */
			/* IV-over-UV optimisation - choose to cache IV if possible */
		
       54080    	if (SvTYPE(sv) == SVt_NV)
          21    	    sv_upgrade(sv, SVt_PVNV);
		
       54080    	(void)SvIOKp_on(sv);	/* Must do this first, to clear any SvOOK */
       54080    	if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
       43639    	    SvIV_set(sv, I_V(SvNVX(sv)));
       43639    	    if (SvNVX(sv) == (NV) SvIVX(sv)
		#ifndef NV_PRESERVES_UV
				&& (((UV)1 << NV_PRESERVES_UV_BITS) >
				    (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
				/* Don't flag it as "accurately an integer" if the number
				   came from a (by definition imprecise) NV operation, and
				   we're outside the range of NV integer precision */
		#endif
				) {
       27863    		SvIOK_on(sv);  /* Can this go wrong with rounding? NWC */
				DEBUG_c(PerlIO_printf(Perl_debug_log,
						      "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
						      PTR2UV(sv),
						      SvNVX(sv),
       27863    				      SvIVX(sv)));
		
			    } else {
				/* IV not precise.  No need to convert from PV, as NV
				   conversion would already have cached IV if it detected
				   that PV->IV would be better than PV->NV->IV
				   flags already correct - don't set public IOK.  */
				DEBUG_c(PerlIO_printf(Perl_debug_log,
						      "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
						      PTR2UV(sv),
						      SvNVX(sv),
       15776    				      SvIVX(sv)));
			    }
			    /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
			       but the cast (NV)IV_MIN rounds to a the value less (more
			       negative) than IV_MIN which happens to be equal to SvNVX ??
			       Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
			       NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
			       (NV)UVX == NVX are both true, but the values differ. :-(
			       Hopefully for 2s complement IV_MIN is something like
			       0x8000000000000000 which will be exact. NWC */
			}
			else {
       10441    	    SvUV_set(sv, U_V(SvNVX(sv)));
       10441    	    if (
				(SvNVX(sv) == (NV) SvUVX(sv))
		#ifndef  NV_PRESERVES_UV
				/* Make sure it's not 0xFFFFFFFFFFFFFFFF */
				/*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
				&& (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
				/* Don't flag it as "accurately an integer" if the number
				   came from a (by definition imprecise) NV operation, and
				   we're outside the range of NV integer precision */
		#endif
				)
       10116    		SvIOK_on(sv);
       10441    	    SvIsUV_on(sv);
			    DEBUG_c(PerlIO_printf(Perl_debug_log,
						  "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
						  PTR2UV(sv),
						  SvUVX(sv),
       10441    				  SvUVX(sv)));
			}
		    }
       15377        else if (SvPOKp(sv) && SvLEN(sv)) {
       15355    	UV value;
       15355    	const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
		
			/* We want to avoid a possible problem when we cache a UV which
			   may be later translated to an NV, and the resulting NV is not
			   the translation of the initial data.
			
			   This means that if we cache such a UV, we need to cache the
			   NV as well.  Moreover, we trade speed for space, and do not
			   cache the NV if not needed.
			 */
		
			/* SVt_PVNV is one higher than SVt_PVIV, hence this order  */
       15355    	if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			     == IS_NUMBER_IN_UV) {
			    /* It's definitely an integer, only upgrade to PVIV */
       13438    	    if (SvTYPE(sv) < SVt_PVIV)
         622    		sv_upgrade(sv, SVt_PVIV);
       13438    	    (void)SvIOK_on(sv);
        1917    	} else if (SvTYPE(sv) < SVt_PVNV)
           7    	    sv_upgrade(sv, SVt_PVNV);
		
			/* If NV preserves UV then we only use the UV value if we know that
			   we aren't going to call atof() below. If NVs don't preserve UVs
			   then the value returned may have more precision than atof() will
			   return, even though it isn't accurate.  */
       15355    	if ((numtype & (IS_NUMBER_IN_UV
		#ifdef NV_PRESERVES_UV
					| IS_NUMBER_NOT_INT
		#endif
			    )) == IS_NUMBER_IN_UV) {
			    /* This won't turn off the public IOK flag if it was set above  */
       13438    	    (void)SvIOKp_on(sv);
		
       13438    	    if (!(numtype & IS_NUMBER_NEG)) {
				/* positive */;
        7675    		if (value <= (UV)IV_MAX) {
        5562    		    SvIV_set(sv, (IV)value);
				} else {
				    /* it didn't overflow, and it was positive. */
        2113    		    SvUV_set(sv, value);
        2113    		    SvIsUV_on(sv);
				}
			    } else {
				/* 2s complement assumption  */
        5763    		if (value <= (UV)IV_MIN) {
        4833    		    SvIV_set(sv, -(IV)value);
				} else {
				    /* Too negative for an IV.  This is a double upgrade, but
				       I'm assuming it will be rare.  */
         930    		    if (SvTYPE(sv) < SVt_PVNV)
      ######    			sv_upgrade(sv, SVt_PVNV);
         930    		    SvNOK_on(sv);
         930    		    SvIOK_off(sv);
         930    		    SvIOKp_on(sv);
         930    		    SvNV_set(sv, -(NV)value);
         930    		    SvIV_set(sv, IV_MIN);
				}
			    }
			}
			
       15355    	if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			    != IS_NUMBER_IN_UV) {
			    /* It wasn't an integer, or it overflowed the UV. */
        1917    	    SvNV_set(sv, Atof(SvPVX_const(sv)));
		
        1917                if (! numtype && ckWARN(WARN_NUMERIC))
           1    		    not_a_number(sv);
		
		#if defined(USE_LONG_DOUBLE)
		            DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
		                                  PTR2UV(sv), SvNVX(sv)));
		#else
		            DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
        1917                                      PTR2UV(sv), SvNVX(sv)));
		#endif
		
		#ifdef NV_PRESERVES_UV
        1917                (void)SvIOKp_on(sv);
        1917                (void)SvNOK_on(sv);
        1917                if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
         946                    SvIV_set(sv, I_V(SvNVX(sv)));
         946                    if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
          22                        SvIOK_on(sv);
		                } else {
		                    /* Integer is imprecise. NOK, IOKp */
		                }
		                /* UV will not work better than IV */
		            } else {
         971                    if (SvNVX(sv) > (NV)UV_MAX) {
         971                        SvIsUV_on(sv);
		                    /* Integer is inaccurate. NOK, IOKp, is UV */
         971                        SvUV_set(sv, UV_MAX);
         971                        SvIsUV_on(sv);
		                } else {
      ######                        SvUV_set(sv, U_V(SvNVX(sv)));
		                    /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
		                       NV preservse UV so can do correct comparison.  */
      ######                        if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
      ######                            SvIOK_on(sv);
      ######                            SvIsUV_on(sv);
		                    } else {
		                        /* Integer is imprecise. NOK, IOKp, is UV */
      ######                            SvIsUV_on(sv);
		                    }
		                }
		            }
		#else /* NV_PRESERVES_UV */
		            if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
		                == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
		                /* The UV slot will have been set from value returned by
		                   grok_number above.  The NV slot has just been set using
		                   Atof.  */
			        SvNOK_on(sv);
		                assert (SvIOKp(sv));
		            } else {
		                if (((UV)1 << NV_PRESERVES_UV_BITS) >
		                    U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
		                    /* Small enough to preserve all bits. */
		                    (void)SvIOKp_on(sv);
		                    SvNOK_on(sv);
		                    SvIV_set(sv, I_V(SvNVX(sv)));
		                    if ((NV)(SvIVX(sv)) == SvNVX(sv))
		                        SvIOK_on(sv);
		                    /* Assumption: first non-preserved integer is < IV_MAX,
		                       this NV is in the preserved range, therefore: */
		                    if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
		                          < (UV)IV_MAX)) {
		                        Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
		                    }
		                } else
		                    sv_2iuv_non_preserve (sv, numtype);
		            }
		#endif /* NV_PRESERVES_UV */
			}
		    }
		    else  {
          22    	if (!(SvFLAGS(sv) & SVs_PADTMP)) {
          22    	    if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
           8    		report_uninit(sv);
			}
          22    	if (SvTYPE(sv) < SVt_IV)
			    /* Typically the caller expects that sv_any is not NULL now.  */
           6    	    sv_upgrade(sv, SVt_IV);
          22    	return 0;
		    }
		
		    DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
       69435    			  PTR2UV(sv),SvUVX(sv)));
       69435        return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
		}
		
		/*
		=for apidoc sv_2nv
		
		Return the num value of an SV, doing any necessary string or integer
		conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
		macros.
		
		=cut
		*/
		
		NV
		Perl_sv_2nv(pTHX_ register SV *sv)
     3990162    {
     3990162        if (!sv)
      ######    	return 0.0;
     3990162        if (SvGMAGICAL(sv)) {
      465245    	mg_get(sv);
      465245    	if (SvNOKp(sv))
        4903    	    return SvNVX(sv);
      460342    	if (SvPOKp(sv) && SvLEN(sv)) {
         275    	    if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
				!grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
           1    		not_a_number(sv);
         275    	    return Atof(SvPVX_const(sv));
			}
      460067    	if (SvIOKp(sv)) {
      459913    	    if (SvIsUV(sv))
      ######    		return (NV)SvUVX(sv);
			    else
      459913    		return (NV)SvIVX(sv);
			}	
         154            if (!SvROK(sv)) {
         154    	    if (!(SvFLAGS(sv) & SVs_PADTMP)) {
         154    		if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
           9    		    report_uninit(sv);
			    }
         154                return (NV)0;
		        }
		    }
     3524917        if (SvTHINKFIRST(sv)) {
     1969278    	if (SvROK(sv)) {
     1957766    	  SV* tmpstr;
     1957766              if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
		                (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
        1187    	      return SvNV(tmpstr);
     1956579    	  return PTR2NV(SvRV(sv));
			}
       11512    	if (SvIsCOW(sv)) {
        3140    	    sv_force_normal_flags(sv, 0);
			}
       11512    	if (SvREADONLY(sv) && !SvOK(sv)) {
         585    	    if (ckWARN(WARN_UNINITIALIZED))
          27    		report_uninit(sv);
         585    	    return 0.0;
			}
		    }
     1566566        if (SvTYPE(sv) < SVt_NV) {
      346096    	if (SvTYPE(sv) == SVt_IV)
      345991    	    sv_upgrade(sv, SVt_PVNV);
			else
         105    	    sv_upgrade(sv, SVt_NV);
		#ifdef USE_LONG_DOUBLE
			DEBUG_c({
			    STORE_NUMERIC_LOCAL_SET_STANDARD();
			    PerlIO_printf(Perl_debug_log,
					  "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
					  PTR2UV(sv), SvNVX(sv));
			    RESTORE_NUMERIC_LOCAL();
			});
		#else
			DEBUG_c({
			    STORE_NUMERIC_LOCAL_SET_STANDARD();
			    PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
					  PTR2UV(sv), SvNVX(sv));
			    RESTORE_NUMERIC_LOCAL();
      346096    	});
		#endif
		    }
     1220470        else if (SvTYPE(sv) < SVt_PVNV)
       52765    	sv_upgrade(sv, SVt_PVNV);
     1566566        if (SvNOKp(sv)) {
           3            return SvNVX(sv);
		    }
     1566563        if (SvIOKp(sv)) {
     1550431    	SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
		#ifdef NV_PRESERVES_UV
     1550431    	SvNOK_on(sv);
		#else
			/* Only set the public NV OK flag if this NV preserves the IV  */
			/* Check it's not 0xFFFFFFFFFFFFFFFF */
			if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
				       : (SvIVX(sv) == I_V(SvNVX(sv))))
			    SvNOK_on(sv);
			else
			    SvNOKp_on(sv);
		#endif
		    }
       16132        else if (SvPOKp(sv) && SvLEN(sv)) {
       14868    	UV value;
       14868    	const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
       14868    	if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
      ######    	    not_a_number(sv);
		#ifdef NV_PRESERVES_UV
       14868    	if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			    == IS_NUMBER_IN_UV) {
			    /* It's definitely an integer */
        9501    	    SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
			} else
        5367    	    SvNV_set(sv, Atof(SvPVX_const(sv)));
       14868    	SvNOK_on(sv);
		#else
			SvNV_set(sv, Atof(SvPVX_const(sv)));
			/* Only set the public NV OK flag if this NV preserves the value in
			   the PV at least as well as an IV/UV would.
			   Not sure how to do this 100% reliably. */
			/* if that shift count is out of range then Configure's test is
			   wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
			   UV_BITS */
			if (((UV)1 << NV_PRESERVES_UV_BITS) >
			    U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
			    SvNOK_on(sv); /* Definitely small enough to preserve all bits */
			} else if (!(numtype & IS_NUMBER_IN_UV)) {
		            /* Can't use strtol etc to convert this string, so don't try.
		               sv_2iv and sv_2uv will use the NV to convert, not the PV.  */
		            SvNOK_on(sv);
		        } else {
		            /* value has been set.  It may not be precise.  */
			    if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
				/* 2s complement assumption for (UV)IV_MIN  */
		                SvNOK_on(sv); /* Integer is too negative.  */
		            } else {
		                SvNOKp_on(sv);
		                SvIOKp_on(sv);
		
		                if (numtype & IS_NUMBER_NEG) {
		                    SvIV_set(sv, -(IV)value);
		                } else if (value <= (UV)IV_MAX) {
				    SvIV_set(sv, (IV)value);
				} else {
				    SvUV_set(sv, value);
				    SvIsUV_on(sv);
				}
		
		                if (numtype & IS_NUMBER_NOT_INT) {
		                    /* I believe that even if the original PV had decimals,
		                       they are lost beyond the limit of the FP precision.
		                       However, neither is canonical, so both only get p
		                       flags.  NWC, 2000/11/25 */
		                    /* Both already have p flags, so do nothing */
		                } else {
				    const NV nv = SvNVX(sv);
		                    if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
		                        if (SvIVX(sv) == I_V(nv)) {
		                            SvNOK_on(sv);
		                            SvIOK_on(sv);
		                        } else {
		                            SvIOK_on(sv);
		                            /* It had no "." so it must be integer.  */
		                        }
		                    } else {
		                        /* between IV_MAX and NV(UV_MAX).
		                           Could be slightly > UV_MAX */
		
		                        if (numtype & IS_NUMBER_NOT_INT) {
		                            /* UV and NV both imprecise.  */
		                        } else {
					    const UV nv_as_uv = U_V(nv);
		
		                            if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
		                                SvNOK_on(sv);
		                                SvIOK_on(sv);
		                            } else {
		                                SvIOK_on(sv);
		                            }
		                        }
		                    }
		                }
		            }
		        }
		#endif /* NV_PRESERVES_UV */
		    }
		    else  {
        1264    	if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
         122    	    report_uninit(sv);
        1264    	if (SvTYPE(sv) < SVt_NV)
			    /* Typically the caller expects that sv_any is not NULL now.  */
			    /* XXX Ilya implies that this is a bug in callers that assume this
			       and ideally should be fixed.  */
      ######    	    sv_upgrade(sv, SVt_NV);
        1264    	return 0.0;
		    }
		#if defined(USE_LONG_DOUBLE)
		    DEBUG_c({
			STORE_NUMERIC_LOCAL_SET_STANDARD();
			PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
				      PTR2UV(sv), SvNVX(sv));
			RESTORE_NUMERIC_LOCAL();
		    });
		#else
		    DEBUG_c({
			STORE_NUMERIC_LOCAL_SET_STANDARD();
			PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
				      PTR2UV(sv), SvNVX(sv));
			RESTORE_NUMERIC_LOCAL();
     1565299        });
		#endif
     1565299        return SvNVX(sv);
		}
		
		/* asIV(): extract an integer from the string value of an SV.
		 * Caller must validate PVX  */
		
		STATIC IV
		S_asIV(pTHX_ SV *sv)
         188    {
         188        UV value;
         188        const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
		
         188        if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			== IS_NUMBER_IN_UV) {
			/* It's definitely an integer */
         187    	if (numtype & IS_NUMBER_NEG) {
      ######    	    if (value < (UV)IV_MIN)
      ######    		return -(IV)value;
			} else {
         187    	    if (value < (UV)IV_MAX)
         187    		return (IV)value;
			}
		    }
           1        if (!numtype) {
           1    	if (ckWARN(WARN_NUMERIC))
      ######    	    not_a_number(sv);
		    }
           1        return I_V(Atof(SvPVX_const(sv)));
		}
		
		/* asUV(): extract an unsigned integer from the string value of an SV
		 * Caller must validate PVX  */
		
		STATIC UV
		S_asUV(pTHX_ SV *sv)
      ######    {
      ######        UV value;
      ######        const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
		
      ######        if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
			== IS_NUMBER_IN_UV) {
			/* It's definitely an integer */
      ######    	if (!(numtype & IS_NUMBER_NEG))
      ######    	    return value;
		    }
      ######        if (!numtype) {
      ######    	if (ckWARN(WARN_NUMERIC))
      ######    	    not_a_number(sv);
		    }
      ######        return U_V(Atof(SvPVX_const(sv)));
		}
		
		/*
		=for apidoc sv_2pv_nolen
		
		Like C<sv_2pv()>, but doesn't return the length too. You should usually
		use the macro wrapper C<SvPV_nolen(sv)> instead.
		=cut
		*/
		
		char *
		Perl_sv_2pv_nolen(pTHX_ register SV *sv)
      ######    {
      ######        return sv_2pv(sv, 0);
		}
		
		/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
		 * UV as a string towards the end of buf, and return pointers to start and
		 * end of it.
		 *
		 * We assume that buf is at least TYPE_CHARS(UV) long.
		 */
		
		static char *
		uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
     2919319    {
     2919319        char *ptr = buf + TYPE_CHARS(UV);
     2919319        char *ebuf = ptr;
     2919319        int sign;
		
     2919319        if (is_uv)
        5629    	sign = 0;
     2913690        else if (iv >= 0) {
     2905142    	uv = iv;
     2905142    	sign = 0;
		    } else {
        8548    	uv = -iv;
        8548    	sign = 1;
		    }
     9350132        do {
     9350132    	*--ptr = '0' + (char)(uv % 10);
     9350132        } while (uv /= 10);
     2919319        if (sign)
        8548    	*--ptr = '-';
     2919319        *peob = ebuf;
     2919319        return ptr;
		}
		
		/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
		 * this function provided for binary compatibility only
		 */
		
		char *
		Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
      ######    {
      ######        return sv_2pv_flags(sv, lp, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_2pv_flags
		
		Returns a pointer to the string value of an SV, and sets *lp to its length.
		If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
		if necessary.
		Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
		usually end up here too.
		
		=cut
		*/
		
		char *
		Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
     8795333    {
     8795333        register char *s;
     8795333        int olderrno;
     8795333        SV *tsv, *origsv;
     8795333        char tbuf[64];	/* Must fit sprintf/Gconvert of longest IV/NV */
     8795333        char *tmpbuf = tbuf;
		
     8795333        if (!sv) {
      ######    	if (lp)
      ######    	    *lp = 0;
      ######    	return (char *)"";
		    }
     8795333        if (SvGMAGICAL(sv)) {
     5635326    	if (flags & SV_GMAGIC)
     5618308    	    mg_get(sv);
     5635320    	if (SvPOKp(sv)) {
     5628991    	    if (lp)
     5622798    		*lp = SvCUR(sv);
     5628991    	    if (flags & SV_MUTABLE_RETURN)
        1782    		return SvPVX_mutable(sv);
     5627209    	    if (flags & SV_CONST_RETURN)
     5608766    		return (char *)SvPVX_const(sv);
       18443    	    return SvPVX(sv);
			}
        6329    	if (SvIOKp(sv)) {
        5949    	    if (SvIsUV(sv))
      ######    		(void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
			    else
        5949    		(void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
        5949    	    tsv = Nullsv;
        5949    	    goto tokensave;
			}
         380    	if (SvNOKp(sv)) {
          12    	    Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
          12    	    tsv = Nullsv;
          12    	    goto tokensave;
			}
         368            if (!SvROK(sv)) {
         354    	    if (!(SvFLAGS(sv) & SVs_PADTMP)) {
         354    		if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
          53    		    report_uninit(sv);
			    }
         354    	    if (lp)
         306    		*lp = 0;
         354                return (char *)"";
		        }
		    }
     3160021        if (SvTHINKFIRST(sv)) {
      139156    	if (SvROK(sv)) {
      119012    	    SV* tmpstr;
      119012                register const char *typestr;
      119012                if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
		                (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
				/* Unwrap this:  */
				/* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
		
       10854                    char *pv;
       10854    		if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
       10837    		    if (flags & SV_CONST_RETURN) {
       10718    			pv = (char *) SvPVX_const(tmpstr);
				    } else {
         119    			pv = (flags & SV_MUTABLE_RETURN)
					    ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
				    }
       10837    		    if (lp)
       10837    			*lp = SvCUR(tmpstr);
				} else {
          17    		    pv = sv_2pv_flags(tmpstr, lp, flags);
				}
       10854                    if (SvUTF8(tmpstr))
           3                        SvUTF8_on(sv);
		                else
       10851                        SvUTF8_off(sv);
       10854                    return pv;
		            }
      108158    	    origsv = sv;
      108158    	    sv = (SV*)SvRV(sv);
      108158    	    if (!sv)
      ######    		typestr = "NULLREF";
			    else {
      108158    		MAGIC *mg;
				
      108158    		switch (SvTYPE(sv)) {
				case SVt_PVMG:
       72748    		    if ( ((SvFLAGS(sv) &
					   (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
					  == (SVs_OBJECT|SVs_SMG))
					 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
       70163                            const regexp *re = (regexp *)mg->mg_obj;
		
       70163    			if (!mg->mg_ptr) {
       11899                                const char *fptr = "msix";
       11899    			    char reflags[6];
       11899    			    char ch;
       11899    			    int left = 0;
       11899    			    int right = 4;
       11899                                char need_newline = 0;
       11899     			    U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
		
       59495     			    while((ch = *fptr++)) {
       47596     				if(reganch & 1) {
        1433     				    reflags[left++] = ch;
		 				}
		 				else {
       46163     				    reflags[right--] = ch;
		 				}
       47596     				reganch >>= 1;
		 			    }
       11899     			    if(left != 4) {
       11898     				reflags[left] = '-';
       11898     				left = 5;
		 			    }
		
       11899    			    mg->mg_len = re->prelen + 4 + left;
		                            /*
		                             * If /x was used, we have to worry about a regex
		                             * ending with a comment later being embedded
		                             * within another regex. If so, we don't want this
		                             * regex's "commentization" to leak out to the
		                             * right part of the enclosing regex, we must cap
		                             * it with a newline.
		                             *
		                             * So, if /x was used, we scan backwards from the
		                             * end of the regex. If we find a '#' before we
		                             * find a newline, we need to add a newline
		                             * ourself. If we find a '\n' first (or if we
		                             * don't find '#' or '\n'), we don't need to add
		                             * anything.  -jfriedl
		                             */
       11899                                if (PMf_EXTENDED & re->reganch)
		                            {
         256                                    const char *endptr = re->precomp + re->prelen;
       18120                                    while (endptr >= re->precomp)
		                                {
       17904                                        const char c = *(endptr--);
       17904                                        if (c == '\n')
          38                                            break; /* don't need another */
       17866                                        if (c == '#') {
		                                        /* we end while in a comment, so we
		                                           need a newline */
           2                                            mg->mg_len++; /* save space for it */
           2                                            need_newline = 1; /* note to add it */
							break;
		                                    }
		                                }
		                            }
		
       11899    			    New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
       11899    			    Copy("(?", mg->mg_ptr, 2, char);
       11899    			    Copy(reflags, mg->mg_ptr+2, left, char);
       11899    			    Copy(":", mg->mg_ptr+left+2, 1, char);
       11899    			    Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
       11899                                if (need_newline)
           2                                    mg->mg_ptr[mg->mg_len - 2] = '\n';
       11899    			    mg->mg_ptr[mg->mg_len - 1] = ')';
       11899    			    mg->mg_ptr[mg->mg_len] = 0;
					}
       70163    			PL_reginterp_cnt += re->program[0].next_off;
		
       70163    			if (re->reganch & ROPT_UTF8)
           4    			    SvUTF8_on(origsv);
					else
       70159    			    SvUTF8_off(origsv);
       70163    			if (lp)
       70163    			    *lp = mg->mg_len;
       70163    			return mg->mg_ptr;
				    }
							/* Fall through */
				case SVt_NULL:
				case SVt_IV:
				case SVt_NV:
				case SVt_RV:
				case SVt_PV:
				case SVt_PVIV:
				case SVt_PVNV:
        8799    		case SVt_PVBM:	typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
          23    		case SVt_PVLV:	typestr = SvROK(sv) ? "REF"
						/* tied lvalues should appear to be
						 * scalars for backwards compatitbility */
						: (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
          23    				    ? "SCALAR" : "LVALUE";	break;
       24283    		case SVt_PVAV:	typestr = "ARRAY";	break;
        2760    		case SVt_PVHV:	typestr = "HASH";	break;
        1959    		case SVt_PVCV:	typestr = "CODE";	break;
         168    		case SVt_PVGV:	typestr = "GLOB";	break;
      ######    		case SVt_PVFM:	typestr = "FORMAT";	break;
           3    		case SVt_PVIO:	typestr = "IO";		break;
      ######    		default:	typestr = "UNKNOWN";	break;
				}
       37995    		tsv = NEWSV(0,0);
       37995    		if (SvOBJECT(sv)) {
        2661    		    const char *name = HvNAME_get(SvSTASH(sv));
        2661    		    Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
						   name ? name : "__ANON__" , typestr, PTR2UV(sv));
				}
				else
       35334    		    Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
       35334    		goto tokensaveref;
			    }
      ######    	    if (lp)
      ######    		*lp = strlen(typestr);
      ######    	    return (char *)typestr;
			}
       20144    	if (SvREADONLY(sv) && !SvOK(sv)) {
         105    	    if (ckWARN(WARN_UNINITIALIZED))
          16    		report_uninit(sv);
         105    	    if (lp)
          99    		*lp = 0;
         105    	    return (char *)"";
			}
		    }
     3040904        if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
			/* I'm assuming that if both IV and NV are equally valid then
			   converting the IV is going to be more efficient */
     2919319    	const U32 isIOK = SvIOK(sv);
     2919319    	const U32 isUIOK = SvIsUV(sv);
     2919319    	char buf[TYPE_CHARS(UV)];
     2919319    	char *ebuf, *ptr;
		
     2919319    	if (SvTYPE(sv) < SVt_PVIV)
     1387710    	    sv_upgrade(sv, SVt_PVIV);
     2919319    	if (isUIOK)
        5629    	    ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
			else
     2913690    	    ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
			/* inlined from sv_setpvn */
     2919319    	SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
     2919319    	Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
     2919319    	SvCUR_set(sv, ebuf - ptr);
     2919319    	s = SvEND(sv);
     2919319    	*s = '\0';
     2919319    	if (isIOK)
     2916427    	    SvIOK_on(sv);
			else
        2892    	    SvIOKp_on(sv);
     2919319    	if (isUIOK)
        5629    	    SvIsUV_on(sv);
		    }
      121585        else if (SvNOKp(sv)) {
       52933    	if (SvTYPE(sv) < SVt_PVNV)
       27633    	    sv_upgrade(sv, SVt_PVNV);
			/* The +20 is pure guesswork.  Configure test needed. --jhi */
       52933    	s = SvGROW_mutable(sv, NV_DIG + 20);
       52933    	olderrno = errno;	/* some Xenix systems wipe out errno here */
		#ifdef apollo
			if (SvNVX(sv) == 0.0)
			    (void)strcpy(s,"0");
			else
		#endif /*apollo*/
			{
       52933    	    Gconvert(SvNVX(sv), NV_DIG, 0, s);
			}
       52933    	errno = olderrno;
		#ifdef FIXNEGATIVEZERO
		        if (*s == '-' && s[1] == '0' && !s[2])
			    strcpy(s,"0");
		#endif
      431770    	while (*s) s++;
		#ifdef hcx
			if (s[-1] == '.')
			    *--s = '\0';
		#endif
		    }
		    else {
       68652    	if (ckWARN(WARN_UNINITIALIZED)
			    && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
        2468    	    report_uninit(sv);
       68634    	if (lp)
       68579    	*lp = 0;
       68634    	if (SvTYPE(sv) < SVt_PV)
			    /* Typically the caller expects that sv_any is not NULL now.  */
         454    	    sv_upgrade(sv, SVt_PV);
       68634    	return (char *)"";
		    }
		    {
     2972252    	STRLEN len = s - SvPVX_const(sv);
     2972252    	if (lp) 
     2972148    	    *lp = len;
     2972252    	SvCUR_set(sv, len);
		    }
     2972252        SvPOK_on(sv);
		    DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
     2972252    			  PTR2UV(sv),SvPVX_const(sv)));
     2972252        if (flags & SV_CONST_RETURN)
     2947356    	return (char *)SvPVX_const(sv);
       24896        if (flags & SV_MUTABLE_RETURN)
         761    	return SvPVX_mutable(sv);
       24135        return SvPVX(sv);
		
		  tokensave:
        5961        if (SvROK(sv)) {	/* XXX Skip this when sv_pvn_force calls */
			/* Sneaky stuff here */
		
		      tokensaveref:
       37995    	if (!tsv)
      ######    	    tsv = newSVpv(tmpbuf, 0);
       37995    	sv_2mortal(tsv);
       37995    	if (lp)
       37975    	    *lp = SvCUR(tsv);
       37995    	return SvPVX(tsv);
		    }
		    else {
		        dVAR;
        5961    	STRLEN len;
        5961            const char *t;
		
        5961    	if (tsv) {
      ######    	    sv_2mortal(tsv);
      ######    	    t = SvPVX_const(tsv);
      ######    	    len = SvCUR(tsv);
			}
			else {
        5961    	    t = tmpbuf;
        5961    	    len = strlen(tmpbuf);
			}
		#ifdef FIXNEGATIVEZERO
			if (len == 2 && t[0] == '-' && t[1] == '0') {
			    t = "0";
			    len = 1;
			}
		#endif
        5961    	SvUPGRADE(sv, SVt_PV);
        5961    	if (lp)
        5941    	    *lp = len;
        5961    	s = SvGROW_mutable(sv, len + 1);
        5961    	SvCUR_set(sv, len);
        5961    	SvPOKp_on(sv);
        5961    	return memcpy(s, t, len + 1);
		    }
		}
		
		/*
		=for apidoc sv_copypv
		
		Copies a stringified representation of the source SV into the
		destination SV.  Automatically performs any necessary mg_get and
		coercion of numeric values into strings.  Guaranteed to preserve
		UTF-8 flag even from overloaded objects.  Similar in nature to
		sv_2pv[_flags] but operates directly on an SV instead of just the
		string.  Mostly uses sv_2pv_flags to do its work, except when that
		would lose the UTF-8'ness of the PV.
		
		=cut
		*/
		
		void
		Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
      485504    {
      485504        STRLEN len;
      485504        const char * const s = SvPV_const(ssv,len);
      485504        sv_setpvn(dsv,s,len);
      485504        if (SvUTF8(ssv))
        3456    	SvUTF8_on(dsv);
		    else
      482048    	SvUTF8_off(dsv);
		}
		
		/*
		=for apidoc sv_2pvbyte_nolen
		
		Return a pointer to the byte-encoded representation of the SV.
		May cause the SV to be downgraded from UTF-8 as a side-effect.
		
		Usually accessed via the C<SvPVbyte_nolen> macro.
		
		=cut
		*/
		
		char *
		Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
      ######    {
      ######        return sv_2pvbyte(sv, 0);
		}
		
		/*
		=for apidoc sv_2pvbyte
		
		Return a pointer to the byte-encoded representation of the SV, and set *lp
		to its length.  May cause the SV to be downgraded from UTF-8 as a
		side-effect.
		
		Usually accessed via the C<SvPVbyte> macro.
		
		=cut
		*/
		
		char *
		Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
        2931    {
        2931        sv_utf8_downgrade(sv,0);
        2929        return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
		}
		
		/*
		=for apidoc sv_2pvutf8_nolen
		
		Return a pointer to the UTF-8-encoded representation of the SV.
		May cause the SV to be upgraded to UTF-8 as a side-effect.
		
		Usually accessed via the C<SvPVutf8_nolen> macro.
		
		=cut
		*/
		
		char *
		Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
      ######    {
      ######        return sv_2pvutf8(sv, 0);
		}
		
		/*
		=for apidoc sv_2pvutf8
		
		Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
		to its length.  May cause the SV to be upgraded to UTF-8 as a side-effect.
		
		Usually accessed via the C<SvPVutf8> macro.
		
		=cut
		*/
		
		char *
		Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
          52    {
          52        sv_utf8_upgrade(sv);
          52        return SvPV(sv,*lp);
		}
		
		/*
		=for apidoc sv_2bool
		
		This function is only called on magical items, and is only used by
		sv_true() or its macro equivalent.
		
		=cut
		*/
		
		bool
		Perl_sv_2bool(pTHX_ register SV *sv)
     5995807    {
     5995807        if (SvGMAGICAL(sv))
      207584    	mg_get(sv);
		
     5995807        if (!SvOK(sv))
     3121906    	return 0;
     2873901        if (SvROK(sv)) {
     2689449    	SV* tmpsv;
     2689449            if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
		                (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
        2073    	    return (bool)SvTRUE(tmpsv);
     2687374          return SvRV(sv) != 0;
		    }
      184452        if (SvPOKp(sv)) {
       93556    	register XPV* const Xpvtmp = (XPV*)SvANY(sv);
       93556    	if (Xpvtmp &&
				(*sv->sv_u.svu_pv > '0' ||
				Xpvtmp->xpv_cur > 1 ||
				(Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
       32564    	    return 1;
			else
       60992    	    return 0;
		    }
		    else {
       90896    	if (SvIOKp(sv))
       90896    	    return SvIVX(sv) != 0;
			else {
      ######    	    if (SvNOKp(sv))
      ######    		return SvNVX(sv) != 0.0;
			    else
      ######    		return FALSE;
			}
		    }
		}
		
		/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
		 * this function provided for binary compatibility only
		 */
		
		
		STRLEN
		Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
      ######    {
      ######        return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_utf8_upgrade
		
		Converts the PV of an SV to its UTF-8-encoded form.
		Forces the SV to string form if it is not already.
		Always sets the SvUTF8 flag to avoid future validity checks even
		if all the bytes have hibit clear.
		
		This is not as a general purpose byte encoding to Unicode interface:
		use the Encode extension for that.
		
		=for apidoc sv_utf8_upgrade_flags
		
		Converts the PV of an SV to its UTF-8-encoded form.
		Forces the SV to string form if it is not already.
		Always sets the SvUTF8 flag to avoid future validity checks even
		if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
		will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
		C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
		
		This is not as a general purpose byte encoding to Unicode interface:
		use the Encode extension for that.
		
		=cut
		*/
		
		STRLEN
		Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
      470138    {
      470138        if (sv == &PL_sv_undef)
      ######    	return 0;
      470138        if (!SvPOK(sv)) {
          28    	STRLEN len = 0;
          28    	if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
      ######    	    (void) sv_2pv_flags(sv,&len, flags);
      ######    	    if (SvUTF8(sv))
      ######    		return len;
			} else {
          28    	    (void) SvPV_force(sv,len);
			}
		    }
		
      470138        if (SvUTF8(sv)) {
      285456    	return SvCUR(sv);
		    }
		
      184682        if (SvIsCOW(sv)) {
           4            sv_force_normal_flags(sv, 0);
		    }
		
      184682        if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
         406            sv_recode_to_utf8(sv, PL_encoding);
		    else { /* Assume Latin-1/EBCDIC */
			/* This function could be much more efficient if we
			 * had a FLAG in SVs to signal if there are any hibit
			 * chars in the PV.  Given that there isn't such a flag
			 * make the loop as fast as possible. */
      184276    	const U8 *s = (U8 *) SvPVX_const(sv);
      184276    	const U8 *e = (U8 *) SvEND(sv);
      184276    	const U8 *t = s;
      184276    	int hibit = 0;
			
     2201436    	while (t < e) {
     2028152    	    const U8 ch = *t++;
     2028152    	    if ((hibit = !NATIVE_IS_INVARIANT(ch)))
      184276    		break;
			}
      184276    	if (hibit) {
       10992    	    STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
       10992    	    U8 * const recoded = bytes_to_utf8((U8*)s, &len);
		
       10992    	    SvPV_free(sv); /* No longer using what was there before. */
		
       10992    	    SvPV_set(sv, (char*)recoded);
       10992    	    SvCUR_set(sv, len - 1);
       10992    	    SvLEN_set(sv, len); /* No longer know the real size. */
			}
			/* Mark as UTF-8 even if no hibit - saves scanning loop */
      184276    	SvUTF8_on(sv);
		    }
      184680        return SvCUR(sv);
		}
		
		/*
		=for apidoc sv_utf8_downgrade
		
		Attempts to convert the PV of an SV from characters to bytes.
		If the PV contains a character beyond byte, this conversion will fail;
		in this case, either returns false or, if C<fail_ok> is not
		true, croaks.
		
		This is not as a general purpose Unicode to byte encoding interface:
		use the Encode extension for that.
		
		=cut
		*/
		
		bool
		Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
        6828    {
        6828        if (SvPOKp(sv) && SvUTF8(sv)) {
        3031            if (SvCUR(sv)) {
        3031    	    U8 *s;
        3031    	    STRLEN len;
		
        3031                if (SvIsCOW(sv)) {
      ######                    sv_force_normal_flags(sv, 0);
		            }
        3031    	    s = (U8 *) SvPV(sv, len);
        3031    	    if (!utf8_to_bytes(s, &len)) {
          40    	        if (fail_ok)
          31    		    return FALSE;
				else {
           9    		    if (PL_op)
           9    		        Perl_croak(aTHX_ "Wide character in %s",
						   OP_DESC(PL_op));
				    else
      ######    		        Perl_croak(aTHX_ "Wide character");
				}
			    }
        2991    	    SvCUR_set(sv, len);
			}
		    }
        6788        SvUTF8_off(sv);
        6788        return TRUE;
		}
		
		/*
		=for apidoc sv_utf8_encode
		
		Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
		flag off so that it looks like octets again.
		
		=cut
		*/
		
		void
		Perl_sv_utf8_encode(pTHX_ register SV *sv)
      268352    {
      268352        (void) sv_utf8_upgrade(sv);
      268352        if (SvIsCOW(sv)) {
           1            sv_force_normal_flags(sv, 0);
		    }
      268352        if (SvREADONLY(sv)) {
           1    	Perl_croak(aTHX_ PL_no_modify);
		    }
      268351        SvUTF8_off(sv);
		}
		
		/*
		=for apidoc sv_utf8_decode
		
		If the PV of the SV is an octet sequence in UTF-8
		and contains a multiple-byte character, the C<SvUTF8> flag is turned on
		so that it looks like a character. If the PV contains only single-byte
		characters, the C<SvUTF8> flag stays being off.
		Scans PV for validity and returns false if the PV is invalid UTF-8.
		
		=cut
		*/
		
		bool
		Perl_sv_utf8_decode(pTHX_ register SV *sv)
          17    {
          17        if (SvPOKp(sv)) {
          17            const U8 *c;
          17            const U8 *e;
		
			/* The octets may have got themselves encoded - get them back as
			 * bytes
			 */
          17    	if (!sv_utf8_downgrade(sv, TRUE))
      ######    	    return FALSE;
		
		        /* it is actually just a matter of turning the utf8 flag on, but
		         * we want to make sure everything inside is valid utf8 first.
		         */
          17            c = (const U8 *) SvPVX_const(sv);
          17    	if (!is_utf8_string(c, SvCUR(sv)+1))
      ######    	    return FALSE;
          17            e = (const U8 *) SvEND(sv);
          22            while (c < e) {
          20    	    U8 ch = *c++;
          20                if (!UTF8_IS_INVARIANT(ch)) {
          15    		SvUTF8_on(sv);
				break;
			    }
		        }
		    }
          17        return TRUE;
		}
		
		/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
		 * this function provided for binary compatibility only
		 */
		
		void
		Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
      ######    {
      ######        sv_setsv_flags(dstr, sstr, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_setsv
		
		Copies the contents of the source SV C<ssv> into the destination SV
		C<dsv>.  The source SV may be destroyed if it is mortal, so don't use this
		function if the source SV needs to be reused. Does not handle 'set' magic.
		Loosely speaking, it performs a copy-by-value, obliterating any previous
		content of the destination.
		
		You probably want to use one of the assortment of wrappers, such as
		C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
		C<SvSetMagicSV_nosteal>.
		
		=for apidoc sv_setsv_flags
		
		Copies the contents of the source SV C<ssv> into the destination SV
		C<dsv>.  The source SV may be destroyed if it is mortal, so don't use this
		function if the source SV needs to be reused. Does not handle 'set' magic.
		Loosely speaking, it performs a copy-by-value, obliterating any previous
		content of the destination.
		If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
		C<ssv> if appropriate, else not. If the C<flags> parameter has the
		C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
		and C<sv_setsv_nomg> are implemented in terms of this function.
		
		You probably want to use one of the assortment of wrappers, such as
		C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
		C<SvSetMagicSV_nosteal>.
		
		This is the primary function for copying scalars, and most other
		copy-ish functions and macros use this underneath.
		
		=cut
		*/
		
		void
		Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
    97031387    {
    97031387        register U32 sflags;
    97031387        register int dtype;
    97031387        register int stype;
		
    97031387        if (sstr == dstr)
         522    	return;
    97030865        SV_CHECK_THINKFIRST_COW_DROP(dstr);
    97030842        if (!sstr)
          99    	sstr = &PL_sv_undef;
    97030842        stype = SvTYPE(sstr);
    97030842        dtype = SvTYPE(dstr);
		
    97030842        SvAMAGIC_off(dstr);
    97030842        if ( SvVOK(dstr) )
		    {
			/* need to nuke the magic */
          37    	mg_free(dstr);
          37    	SvRMAGICAL_off(dstr);
		    }
		
		    /* There's a lot of redundancy below but we're going for speed here */
		
    97030842        switch (stype) {
		    case SVt_NULL:
		      undef_sstr:
     3985914    	if (dtype != SVt_PVGV) {
     3985910    	    (void)SvOK_off(dstr);
          41    	    return;
			}
    40748825    	break;
		    case SVt_IV:
    40748825    	if (SvIOK(sstr)) {
    40733757    	    switch (dtype) {
			    case SVt_NULL:
    16511045    		sv_upgrade(dstr, SVt_IV);
    16511045    		break;
			    case SVt_NV:
         581    		sv_upgrade(dstr, SVt_PVNV);
         581    		break;
			    case SVt_RV:
			    case SVt_PV:
       15279    		sv_upgrade(dstr, SVt_PVIV);
				break;
			    }
    40733757    	    (void)SvIOK_only(dstr);
    40733757    	    SvIV_set(dstr,  SvIVX(sstr));
    40733757    	    if (SvIsUV(sstr))
       16707    		SvIsUV_on(dstr);
    40733757    	    if (SvTAINTED(sstr))
      ######    		SvTAINT(dstr);
      ######    	    return;
			}
     2242829    	goto undef_sstr;
		
		    case SVt_NV:
     2242829    	if (SvNOK(sstr)) {
     2242810    	    switch (dtype) {
			    case SVt_NULL:
			    case SVt_IV:
      793919    		sv_upgrade(dstr, SVt_NV);
      793919    		break;
			    case SVt_RV:
			    case SVt_PV:
			    case SVt_PVIV:
        2429    		sv_upgrade(dstr, SVt_PVNV);
				break;
			    }
     2242810    	    SvNV_set(dstr, SvNVX(sstr));
     2242810    	    (void)SvNOK_only(dstr);
     2242810    	    if (SvTAINTED(sstr))
      ######    		SvTAINT(dstr);
      ######    	    return;
			}
    14540065    	goto undef_sstr;
		
		    case SVt_RV:
    14540065    	if (dtype < SVt_RV)
     2579426    	    sv_upgrade(dstr, SVt_RV);
    11960639    	else if (dtype == SVt_PVGV &&
				 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
         829    	    sstr = SvRV(sstr);
         829    	    if (sstr == dstr) {
          18    		if (GvIMPORTED(dstr) != GVf_IMPORTED
				    && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
				{
          18    		    GvIMPORTED_on(dstr);
				}
          18    		GvMULTI_on(dstr);
          18    		return;
			    }
    24708013    	    goto glob_assign;
			}
    24708013    	break;
		    case SVt_PVFM:
		#ifdef PERL_OLD_COPY_ON_WRITE
			if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
			    if (dtype < SVt_PVIV)
				sv_upgrade(dstr, SVt_PVIV);
			    break;
			}
			/* Fall through */
		#endif
		    case SVt_PV:
    24708013    	if (dtype < SVt_PV)
    11017976    	    sv_upgrade(dstr, SVt_PV);
    11017976    	break;
		    case SVt_PVIV:
     1893390    	if (dtype < SVt_PVIV)
      801523    	    sv_upgrade(dstr, SVt_PVIV);
      801523    	break;
		    case SVt_PVNV:
     4443533    	if (dtype < SVt_PVNV)
     1722234    	    sv_upgrade(dstr, SVt_PVNV);
     1722234    	break;
		    case SVt_PVAV:
		    case SVt_PVHV:
		    case SVt_PVCV:
		    case SVt_PVIO:
			{
      ######    	const char * const type = sv_reftype(sstr,0);
      ######    	if (PL_op)
      ######    	    Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
			else
      ######    	    Perl_croak(aTHX_ "Bizarre copy of %s", type);
			}
       10311    	break;
		
		    case SVt_PVGV:
       10311    	if (dtype <= SVt_PVGV) {
		  glob_assign:
       11121    	    if (dtype != SVt_PVGV) {
        6972    		const char * const name = GvNAME(sstr);
        6972    		const STRLEN len = GvNAMELEN(sstr);
				/* don't upgrade SVt_PVLV: it can hold a glob */
        6972    		if (dtype != SVt_PVLV)
        6972    		    sv_upgrade(dstr, SVt_PVGV);
        6972    		sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
        6972    		GvSTASH(dstr) = GvSTASH(sstr);
        6972    		if (GvSTASH(dstr))
        6972    		    Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr);
        6972    		GvNAME(dstr) = savepvn(name, len);
        6972    		GvNAMELEN(dstr) = len;
        6972    		SvFAKE_on(dstr);	/* can coerce to non-glob */
			    }
			    /* ahem, death to those who redefine active sort subs */
        4149    	    else if (PL_curstackinfo->si_type == PERLSI_SORT
				     && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
           1    		Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
				      GvNAME(dstr));
		
		#ifdef GV_UNIQUE_CHECK
		                if (GvUNIQUE((GV*)dstr)) {
		                    Perl_croak(aTHX_ PL_no_modify);
		                }
		#endif
		
       11120    	    (void)SvOK_off(dstr);
       11120    	    GvINTRO_off(dstr);		/* one-shot flag */
       11120    	    gp_free((GV*)dstr);
       11120    	    GvGP(dstr) = gp_ref(GvGP(sstr));
       11120    	    if (SvTAINTED(sstr))
      ######    		SvTAINT(dstr);
       11120    	    if (GvIMPORTED(dstr) != GVf_IMPORTED
				&& CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
			    {
        4871    		GvIMPORTED_on(dstr);
			    }
       11120    	    GvMULTI_on(dstr);
       11120    	    return;
			}
			/* FALL THROUGH */
		
		    default:
     4473050    	if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
     2413651    	    mg_get(sstr);
     2413649    	    if ((int)SvTYPE(sstr) != stype) {
      ######    		stype = SvTYPE(sstr);
      ######    		if (stype == SVt_PVGV && dtype <= SVt_PVGV)
      ######    		    goto glob_assign;
			    }
			}
     4473048    	if (stype == SVt_PVLV)
       73035    	    SvUPGRADE(dstr, SVt_PVNV);
			else
     4400013    	    SvUPGRADE(dstr, (U32)stype);
		    }
		
    50057224        sflags = SvFLAGS(sstr);
		
    50057224        if (sflags & SVf_ROK) {
    15028492    	if (dtype >= SVt_PV) {
      742525    	    if (dtype == SVt_PVGV) {
      125283    		SV *sref = SvREFCNT_inc(SvRV(sstr));
      125283    		SV *dref = 0;
      125283    		const int intro = GvINTRO(dstr);
		
		#ifdef GV_UNIQUE_CHECK
		                if (GvUNIQUE((GV*)dstr)) {
		                    Perl_croak(aTHX_ PL_no_modify);
		                }
		#endif
		
      125283    		if (intro) {
       15141    		    GvINTRO_off(dstr);	/* one-shot flag */
       15141    		    GvLINE(dstr) = CopLINE(PL_curcop);
       15141    		    GvEGV(dstr) = (GV*)dstr;
				}
      125283    		GvMULTI_on(dstr);
      125283    		switch (SvTYPE(sref)) {
				case SVt_PVAV:
       17003    		    if (intro)
        8372    			SAVEGENERICSV(GvAV(dstr));
				    else
        8631    			dref = (SV*)GvAV(dstr);
       17003    		    GvAV(dstr) = (AV*)sref;
       17003    		    if (!GvIMPORTED_AV(dstr)
					&& CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
				    {
        8622    			GvIMPORTED_AV_on(dstr);
				    }
        8622    		    break;
				case SVt_PVHV:
       11649    		    if (intro)
        5885    			SAVEGENERICSV(GvHV(dstr));
				    else
        5764    			dref = (SV*)GvHV(dstr);
       11649    		    GvHV(dstr) = (HV*)sref;
       11649    		    if (!GvIMPORTED_HV(dstr)
					&& CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
				    {
        5637    			GvIMPORTED_HV_on(dstr);
				    }
        5637    		    break;
				case SVt_PVCV:
       78372    		    if (intro) {
          50    			if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
      ######    			    SvREFCNT_dec(GvCV(dstr));
      ######    			    GvCV(dstr) = Nullcv;
      ######    			    GvCVGEN(dstr) = 0; /* Switch off cacheness. */
      ######    			    PL_sub_generation++;
					}
          50    			SAVEGENERICSV(GvCV(dstr));
				    }
				    else
       78322    			dref = (SV*)GvCV(dstr);
       78372    		    if (GvCV(dstr) != (CV*)sref) {
       71621    			CV* cv = GvCV(dstr);
       71621    			if (cv) {
        2631    			    if (!GvCVGEN((GV*)dstr) &&
						(CvROOT(cv) || CvXSUB(cv)))
					    {
						/* ahem, death to those who redefine
						 * active sort subs */
          78    				if (PL_curstackinfo->si_type == PERLSI_SORT &&
						      PL_sortcop == CvSTART(cv))
           1    				    Perl_croak(aTHX_
						    "Can't redefine active sort subroutine %s",
							  GvENAME((GV*)dstr));
		 				/* Redefining a sub - warning is mandatory if
		 				   it was a const and its value changed. */
          77     				if (ckWARN(WARN_REDEFINE)
		 				    || (CvCONST(cv)
		 					&& (!CvCONST((CV*)sref)
		 					    || sv_cmp(cv_const_sv(cv),
		 						      cv_const_sv((CV*)sref)))))
		 				{
           2     				    Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
		 					CvCONST(cv)
		 					? "Constant subroutine %s::%s redefined"
		 					: "Subroutine %s::%s redefined",
							HvNAME_get(GvSTASH((GV*)dstr)),
		 					GvENAME((GV*)dstr));
		 				}
					    }
        2630    			    if (!intro)
        2624    				cv_ckproto(cv, (GV*)dstr,
							   SvPOK(sref)
							   ? SvPVX_const(sref) : Nullch);
					}
       71620    			GvCV(dstr) = (CV*)sref;
       71620    			GvCVGEN(dstr) = 0; /* Switch off cacheness. */
       71620    			GvASSUMECV_on(dstr);
       71620    			PL_sub_generation++;
				    }
       78371    		    if (!GvIMPORTED_CV(dstr)
					&& CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
				    {
       59564    			GvIMPORTED_CV_on(dstr);
				    }
       59564    		    break;
				case SVt_PVIO:
           2    		    if (intro)
      ######    			SAVEGENERICSV(GvIOp(dstr));
				    else
           2    			dref = (SV*)GvIOp(dstr);
           2    		    GvIOp(dstr) = (IO*)sref;
           2    		    break;
				case SVt_PVFM:
      ######    		    if (intro)
      ######    			SAVEGENERICSV(GvFORM(dstr));
				    else
      ######    			dref = (SV*)GvFORM(dstr);
      ######    		    GvFORM(dstr) = (CV*)sref;
      ######    		    break;
				default:
       18257    		    if (intro)
         834    			SAVEGENERICSV(GvSV(dstr));
				    else
       17423    			dref = (SV*)GvSV(dstr);
       18257    		    GvSV(dstr) = sref;
       18257    		    if (!GvIMPORTED_SV(dstr)
					&& CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
				    {
       17718    			GvIMPORTED_SV_on(dstr);
				    }
      125282    		    break;
				}
      125282    		if (dref)
       38377    		    SvREFCNT_dec(dref);
      125282    		if (SvTAINTED(sstr))
      ######    		    SvTAINT(dstr);
      ######    		return;
			    }
      617242    	    if (SvPVX_const(dstr)) {
       72301    		SvPV_free(dstr);
       72301    		SvLEN_set(dstr, 0);
       72301                    SvCUR_set(dstr, 0);
			    }
			}
    14903209    	(void)SvOK_off(dstr);
    14903209    	SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
    14903209    	SvROK_on(dstr);
    14903209    	if (sflags & SVp_NOK) {
      ######    	    SvNOKp_on(dstr);
			    /* Only set the public OK flag if the source has public OK.  */
      ######    	    if (sflags & SVf_NOK)
      ######    		SvFLAGS(dstr) |= SVf_NOK;
      ######    	    SvNV_set(dstr, SvNVX(sstr));
			}
    14903209    	if (sflags & SVp_IOK) {
      ######    	    (void)SvIOKp_on(dstr);
      ######    	    if (sflags & SVf_IOK)
      ######    		SvFLAGS(dstr) |= SVf_IOK;
      ######    	    if (sflags & SVf_IVisUV)
      ######    		SvIsUV_on(dstr);
      ######    	    SvIV_set(dstr, SvIVX(sstr));
			}
    14903209    	if (SvAMAGIC(sstr)) {
      596984    	    SvAMAGIC_on(dstr);
			}
		    }
    35028732        else if (sflags & SVp_POK) {
    31445152            bool isSwipe = 0;
		
			/*
			 * Check to see if we can just swipe the string.  If so, it's a
			 * possible small lose on short strings, but a big win on long ones.
			 * It might even be a win on short strings if SvPVX_const(dstr)
			 * has to be allocated and SvPVX_const(sstr) has to be freed.
			 */
		
			/* Whichever path we take through the next code, we want this true,
			   and doing it now facilitates the COW check.  */
    31445152    	(void)SvPOK_only(dstr);
		
    31445152    	if (
			    /* We're not already COW  */
		            ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
		#ifndef PERL_OLD_COPY_ON_WRITE
			     /* or we are, but dstr isn't a suitable target.  */
			     || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
		#endif
			     )
		            &&
		            !(isSwipe =
		                 (sflags & SVs_TEMP) &&   /* slated for free anyway? */
		                 !(sflags & SVf_OOK) &&   /* and not involved in OOK hack? */
			         (!(flags & SV_NOSTEAL)) &&
							/* and we're allowed to steal temps */
		                 SvREFCNT(sstr) == 1 &&   /* and no other references to it? */
		                 SvLEN(sstr) 	&&	  /* and really is a string */
			    			/* and won't be needed again, potentially */
			      !(PL_op && PL_op->op_type == OP_AASSIGN))
		#ifdef PERL_OLD_COPY_ON_WRITE
		            && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
				 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
		                 && SvTYPE(sstr) >= SVt_PVIV)
		#endif
		            ) {
		            /* Failed the swipe test, and it's not a shared hash key either.
		               Have to copy the string.  */
    29523255    	    STRLEN len = SvCUR(sstr);
    29523255                SvGROW(dstr, len + 1);	/* inlined from sv_setpvn */
    29523255                Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
    29523255                SvCUR_set(dstr, len);
    29523255                *SvEND(dstr) = '\0';
		        } else {
		            /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
		               be true in here.  */
		            /* Either it's a shared hash key, or it's suitable for
		               copy-on-write or we can swipe the string.  */
     1921897                if (DEBUG_C_TEST) {
      ######                    PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
      ######                    sv_dump(sstr);
      ######                    sv_dump(dstr);
		            }
		#ifdef PERL_OLD_COPY_ON_WRITE
		            if (!isSwipe) {
		                /* I believe I should acquire a global SV mutex if
		                   it's a COW sv (not a shared hash key) to stop
		                   it going un copy-on-write.
		                   If the source SV has gone un copy on write between up there
		                   and down here, then (assert() that) it is of the correct
		                   form to make it copy on write again */
		                if ((sflags & (SVf_FAKE | SVf_READONLY))
		                    != (SVf_FAKE | SVf_READONLY)) {
		                    SvREADONLY_on(sstr);
		                    SvFAKE_on(sstr);
		                    /* Make the source SV into a loop of 1.
		                       (about to become 2) */
		                    SV_COW_NEXT_SV_SET(sstr, sstr);
		                }
		            }
		#endif
		            /* Initial code is common.  */
     1921897    	    if (SvPVX_const(dstr)) {	/* we know that dtype >= SVt_PV */
      858017    		SvPV_free(dstr);
			    }
		
     1921897                if (!isSwipe) {
		                /* making another shared SV.  */
      375677                    STRLEN cur = SvCUR(sstr);
      375677                    STRLEN len = SvLEN(sstr);
		#ifdef PERL_OLD_COPY_ON_WRITE
		                if (len) {
				    assert (SvTYPE(dstr) >= SVt_PVIV);
		                    /* SvIsCOW_normal */
		                    /* splice us in between source and next-after-source.  */
		                    SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
		                    SV_COW_NEXT_SV_SET(sstr, dstr);
		                    SvPV_set(dstr, SvPVX_mutable(sstr));
		                } else
		#endif
				{
		                    /* SvIsCOW_shared_hash */
		                    DEBUG_C(PerlIO_printf(Perl_debug_log,
      375677                                              "Copy on write: Sharing hash\n"));
		
      375677    		    assert (SvTYPE(dstr) >= SVt_PV);
		                    SvPV_set(dstr,
      375677    			     HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
				}
      375677                    SvLEN_set(dstr, len);
      375677                    SvCUR_set(dstr, cur);
      375677                    SvREADONLY_on(dstr);
      375677                    SvFAKE_on(dstr);
		                /* Relesase a global SV mutex.  */
		            }
		            else
		                {	/* Passes the swipe test.  */
     1546220                    SvPV_set(dstr, SvPVX_mutable(sstr));
     1546220                    SvLEN_set(dstr, SvLEN(sstr));
     1546220                    SvCUR_set(dstr, SvCUR(sstr));
		
     1546220                    SvTEMP_off(dstr);
     1546220                    (void)SvOK_off(sstr);	/* NOTE: nukes most SvFLAGS on sstr */
     1546220                    SvPV_set(sstr, Nullch);
     1546220                    SvLEN_set(sstr, 0);
     1546220                    SvCUR_set(sstr, 0);
     1546220                    SvTEMP_off(sstr);
		            }
		        }
    31445152    	if (sflags & SVf_UTF8)
      903526    	    SvUTF8_on(dstr);
    31445152    	if (sflags & SVp_NOK) {
     2037390    	    SvNOKp_on(dstr);
     2037390    	    if (sflags & SVf_NOK)
     2037080    		SvFLAGS(dstr) |= SVf_NOK;
     2037390    	    SvNV_set(dstr, SvNVX(sstr));
			}
    31445152    	if (sflags & SVp_IOK) {
     2173320    	    (void)SvIOKp_on(dstr);
     2173320    	    if (sflags & SVf_IOK)
     2170794    		SvFLAGS(dstr) |= SVf_IOK;
     2173320    	    if (sflags & SVf_IVisUV)
         227    		SvIsUV_on(dstr);
     2173320    	    SvIV_set(dstr, SvIVX(sstr));
			}
    31445152    	if (SvVOK(sstr)) {
         150    	    MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
         150    	    sv_magic(dstr, NULL, PERL_MAGIC_vstring,
					smg->mg_ptr, smg->mg_len);
         150    	    SvRMAGICAL_on(dstr);
			}
		    }
     3583580        else if (sflags & SVp_IOK) {
     1653484    	if (sflags & SVf_IOK)
     1482316    	    (void)SvIOK_only(dstr);
			else {
      171168    	    (void)SvOK_off(dstr);
      171168    	    (void)SvIOKp_on(dstr);
			}
			/* XXXX Do we want to set IsUV for IV(ROK)?  Be extra safe... */
     1653484    	if (sflags & SVf_IVisUV)
         988    	    SvIsUV_on(dstr);
     1653484    	SvIV_set(dstr, SvIVX(sstr));
     1653484    	if (sflags & SVp_NOK) {
      103702    	    if (sflags & SVf_NOK)
      103702    		(void)SvNOK_on(dstr);
			    else
      ######    		(void)SvNOKp_on(dstr);
      103702    	    SvNV_set(dstr, SvNVX(sstr));
			}
		    }
     1930096        else if (sflags & SVp_NOK) {
     1515104    	if (sflags & SVf_NOK)
     1515098    	    (void)SvNOK_only(dstr);
			else {
           6    	    (void)SvOK_off(dstr);
           6    	    SvNOKp_on(dstr);
			}
     1515104    	SvNV_set(dstr, SvNVX(sstr));
		    }
		    else {
      414992    	if (dtype == SVt_PVGV) {
           5    	    if (ckWARN(WARN_MISC))
           2    		Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
			}
			else
      414987    	    (void)SvOK_off(dstr);
		    }
    49931941        if (SvTAINTED(sstr))
     1767174    	SvTAINT(dstr);
		}
		
		/*
		=for apidoc sv_setsv_mg
		
		Like C<sv_setsv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
           1    {
           1        sv_setsv(dstr,sstr);
           1        SvSETMAGIC(dstr);
		}
		
		#ifdef PERL_OLD_COPY_ON_WRITE
		SV *
		Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
		{
		    STRLEN cur = SvCUR(sstr);
		    STRLEN len = SvLEN(sstr);
		    register char *new_pv;
		
		    if (DEBUG_C_TEST) {
			PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
				      sstr, dstr);
			sv_dump(sstr);
			if (dstr)
				    sv_dump(dstr);
		    }
		
		    if (dstr) {
			if (SvTHINKFIRST(dstr))
			    sv_force_normal_flags(dstr, SV_COW_DROP_PV);
			else if (SvPVX_const(dstr))
			    Safefree(SvPVX_const(dstr));
		    }
		    else
			new_SV(dstr);
		    SvUPGRADE(dstr, SVt_PVIV);
		
		    assert (SvPOK(sstr));
		    assert (SvPOKp(sstr));
		    assert (!SvIOK(sstr));
		    assert (!SvIOKp(sstr));
		    assert (!SvNOK(sstr));
		    assert (!SvNOKp(sstr));
		
		    if (SvIsCOW(sstr)) {
		
			if (SvLEN(sstr) == 0) {
			    /* source is a COW shared hash key.  */
			    DEBUG_C(PerlIO_printf(Perl_debug_log,
						  "Fast copy on write: Sharing hash\n"));
			    new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
			    goto common_exit;
			}
			SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
		    } else {
			assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
			SvUPGRADE(sstr, SVt_PVIV);
			SvREADONLY_on(sstr);
			SvFAKE_on(sstr);
			DEBUG_C(PerlIO_printf(Perl_debug_log,
					      "Fast copy on write: Converting sstr to COW\n"));
			SV_COW_NEXT_SV_SET(dstr, sstr);
		    }
		    SV_COW_NEXT_SV_SET(sstr, dstr);
		    new_pv = SvPVX_mutable(sstr);
		
		  common_exit:
		    SvPV_set(dstr, new_pv);
		    SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
		    if (SvUTF8(sstr))
			SvUTF8_on(dstr);
		    SvLEN_set(dstr, len);
		    SvCUR_set(dstr, cur);
		    if (DEBUG_C_TEST) {
			sv_dump(dstr);
		    }
		    return dstr;
		}
		#endif
		
		/*
		=for apidoc sv_setpvn
		
		Copies a string into an SV.  The C<len> parameter indicates the number of
		bytes to be copied.  If the C<ptr> argument is NULL the SV will become
		undefined.  Does not handle 'set' magic.  See C<sv_setpvn_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
    29388753    {
    29388753        register char *dptr;
		
    29388753        SV_CHECK_THINKFIRST_COW_DROP(sv);
    29388753        if (!ptr) {
          53    	(void)SvOK_off(sv);
      ######    	return;
		    }
		    else {
		        /* len is STRLEN which is unsigned, need to copy to signed */
    29388700    	const IV iv = len;
    29388700    	if (iv < 0)
      ######    	    Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
		    }
    29388700        SvUPGRADE(sv, SVt_PV);
		
    29388700        dptr = SvGROW(sv, len + 1);
    29388700        Move(ptr,dptr,len,char);
    29388700        dptr[len] = '\0';
    29388700        SvCUR_set(sv, len);
    29388700        (void)SvPOK_only_UTF8(sv);		/* validate pointer */
    29388700        SvTAINT(sv);
		}
		
		/*
		=for apidoc sv_setpvn_mg
		
		Like C<sv_setpvn>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
          13    {
          13        sv_setpvn(sv,ptr,len);
          13        SvSETMAGIC(sv);
		}
		
		/*
		=for apidoc sv_setpv
		
		Copies a string into an SV.  The string must be null-terminated.  Does not
		handle 'set' magic.  See C<sv_setpv_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
     1592607    {
     1592607        register STRLEN len;
		
     1592607        SV_CHECK_THINKFIRST_COW_DROP(sv);
     1592607        if (!ptr) {
        3268    	(void)SvOK_off(sv);
      ######    	return;
		    }
     1589339        len = strlen(ptr);
     1589339        SvUPGRADE(sv, SVt_PV);
		
     1589339        SvGROW(sv, len + 1);
     1589339        Move(ptr,SvPVX(sv),len+1,char);
     1589339        SvCUR_set(sv, len);
     1589339        (void)SvPOK_only_UTF8(sv);		/* validate pointer */
     1589339        SvTAINT(sv);
		}
		
		/*
		=for apidoc sv_setpv_mg
		
		Like C<sv_setpv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
           1    {
           1        sv_setpv(sv,ptr);
           1        SvSETMAGIC(sv);
		}
		
		/*
		=for apidoc sv_usepvn
		
		Tells an SV to use C<ptr> to find its string value.  Normally the string is
		stored inside the SV but sv_usepvn allows the SV to use an outside string.
		The C<ptr> should point to memory that was allocated by C<malloc>.  The
		string length, C<len>, must be supplied.  This function will realloc the
		memory pointed to by C<ptr>, so that pointer should not be freed or used by
		the programmer after giving it to sv_usepvn.  Does not handle 'set' magic.
		See C<sv_usepvn_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
          15    {
          15        STRLEN allocate;
          15        SV_CHECK_THINKFIRST_COW_DROP(sv);
          15        SvUPGRADE(sv, SVt_PV);
          15        if (!ptr) {
      ######    	(void)SvOK_off(sv);
      ######    	return;
		    }
          15        if (SvPVX_const(sv))
          13    	SvPV_free(sv);
		
          15        allocate = PERL_STRLEN_ROUNDUP(len + 1);
          15        ptr = saferealloc (ptr, allocate);
          15        SvPV_set(sv, ptr);
          15        SvCUR_set(sv, len);
          15        SvLEN_set(sv, allocate);
          15        *SvEND(sv) = '\0';
          15        (void)SvPOK_only_UTF8(sv);		/* validate pointer */
          15        SvTAINT(sv);
		}
		
		/*
		=for apidoc sv_usepvn_mg
		
		Like C<sv_usepvn>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
           1    {
           1        sv_usepvn(sv,ptr,len);
           1        SvSETMAGIC(sv);
		}
		
		#ifdef PERL_OLD_COPY_ON_WRITE
		/* Need to do this *after* making the SV normal, as we need the buffer
		   pointer to remain valid until after we've copied it.  If we let go too early,
		   another thread could invalidate it by unsharing last of the same hash key
		   (which it can do by means other than releasing copy-on-write Svs)
		   or by changing the other copy-on-write SVs in the loop.  */
		STATIC void
		S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, STRLEN len, SV *after)
		{
		    if (len) { /* this SV was SvIsCOW_normal(sv) */
		         /* we need to find the SV pointing to us.  */
		        SV *current = SV_COW_NEXT_SV(after);
		
		        if (current == sv) {
		            /* The SV we point to points back to us (there were only two of us
		               in the loop.)
		               Hence other SV is no longer copy on write either.  */
		            SvFAKE_off(after);
		            SvREADONLY_off(after);
		        } else {
		            /* We need to follow the pointers around the loop.  */
		            SV *next;
		            while ((next = SV_COW_NEXT_SV(current)) != sv) {
		                assert (next);
		                current = next;
		                 /* don't loop forever if the structure is bust, and we have
		                    a pointer into a closed loop.  */
		                assert (current != after);
		                assert (SvPVX_const(current) == pvx);
		            }
		            /* Make the SV before us point to the SV after us.  */
		            SV_COW_NEXT_SV_SET(current, after);
		        }
		    } else {
		        unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
		    }
		}
		
		int
		Perl_sv_release_IVX(pTHX_ register SV *sv)
		{
		    if (SvIsCOW(sv))
		        sv_force_normal_flags(sv, 0);
		    SvOOK_off(sv);
		    return 0;
		}
		#endif
		/*
		=for apidoc sv_force_normal_flags
		
		Undo various types of fakery on an SV: if the PV is a shared string, make
		a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
		an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
		we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
		then a copy-on-write scalar drops its PV buffer (if any) and becomes
		SvPOK_off rather than making a copy. (Used where this scalar is about to be
		set to some other value.) In addition, the C<flags> parameter gets passed to
		C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
		with flags set to 0.
		
		=cut
		*/
		
		void
		Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
    12788445    {
		#ifdef PERL_OLD_COPY_ON_WRITE
		    if (SvREADONLY(sv)) {
		        /* At this point I believe I should acquire a global SV mutex.  */
			if (SvFAKE(sv)) {
			    const char *pvx = SvPVX_const(sv);
			    const STRLEN len = SvLEN(sv);
			    const STRLEN cur = SvCUR(sv);
			    SV * const next = SV_COW_NEXT_SV(sv);   /* next COW sv in the loop. */
		            if (DEBUG_C_TEST) {
		                PerlIO_printf(Perl_debug_log,
		                              "Copy on write: Force normal %ld\n",
		                              (long) flags);
		                sv_dump(sv);
		            }
		            SvFAKE_off(sv);
		            SvREADONLY_off(sv);
		            /* This SV doesn't own the buffer, so need to New() a new one:  */
		            SvPV_set(sv, (char*)0);
		            SvLEN_set(sv, 0);
		            if (flags & SV_COW_DROP_PV) {
		                /* OK, so we don't need to copy our buffer.  */
		                SvPOK_off(sv);
		            } else {
		                SvGROW(sv, cur + 1);
		                Move(pvx,SvPVX(sv),cur,char);
		                SvCUR_set(sv, cur);
		                *SvEND(sv) = '\0';
		            }
		            sv_release_COW(sv, pvx, len, next);
		            if (DEBUG_C_TEST) {
		                sv_dump(sv);
		            }
			}
			else if (IN_PERL_RUNTIME)
			    Perl_croak(aTHX_ PL_no_modify);
		        /* At this point I believe that I can drop the global SV mutex.  */
		    }
		#else
    12788445        if (SvREADONLY(sv)) {
      513786    	if (SvFAKE(sv)) {
      509822    	    const char *pvx = SvPVX_const(sv);
      509822    	    const STRLEN len = SvCUR(sv);
      509822    	    SvFAKE_off(sv);
      509822    	    SvREADONLY_off(sv);
      509822    	    SvPV_set(sv, Nullch);
      509822    	    SvLEN_set(sv, 0);
      509822    	    SvGROW(sv, len + 1);
      509822    	    Move(pvx,SvPVX_const(sv),len,char);
      509822    	    *SvEND(sv) = '\0';
      509822    	    unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
			}
        3964    	else if (IN_PERL_RUNTIME)
          25    	    Perl_croak(aTHX_ PL_no_modify);
		    }
		#endif
    12788420        if (SvROK(sv))
    12272178    	sv_unref_flags(sv, flags);
      516242        else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
        2481    	sv_unglob(sv);
		}
		
		/*
		=for apidoc sv_force_normal
		
		Undo various types of fakery on an SV: if the PV is a shared string, make
		a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
		an xpvmg. See also C<sv_force_normal_flags>.
		
		=cut
		*/
		
		void
		Perl_sv_force_normal(pTHX_ register SV *sv)
      ######    {
      ######        sv_force_normal_flags(sv, 0);
		}
		
		/*
		=for apidoc sv_chop
		
		Efficient removal of characters from the beginning of the string buffer.
		SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
		the string buffer.  The C<ptr> becomes the first character of the adjusted
		string. Uses the "OOK hack".
		Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
		refer to the same chunk of data.
		
		=cut
		*/
		
		void
		Perl_sv_chop(pTHX_ register SV *sv, register const char *ptr)
      343973    {
      343973        register STRLEN delta;
      343973        if (!ptr || !SvPOKp(sv))
      ######    	return;
      343973        delta = ptr - SvPVX_const(sv);
      343973        SV_CHECK_THINKFIRST(sv);
      343973        if (SvTYPE(sv) < SVt_PVIV)
       14629    	sv_upgrade(sv,SVt_PVIV);
		
      343973        if (!SvOOK(sv)) {
      265087    	if (!SvLEN(sv)) { /* make copy of shared string */
      ######    	    const char *pvx = SvPVX_const(sv);
      ######    	    const STRLEN len = SvCUR(sv);
      ######    	    SvGROW(sv, len + 1);
      ######    	    Move(pvx,SvPVX_const(sv),len,char);
      ######    	    *SvEND(sv) = '\0';
			}
      265087    	SvIV_set(sv, 0);
			/* Same SvOOK_on but SvOOK_on does a SvIOK_off
			   and we do that anyway inside the SvNIOK_off
			*/
      265087    	SvFLAGS(sv) |= SVf_OOK;
		    }
      343973        SvNIOK_off(sv);
      343973        SvLEN_set(sv, SvLEN(sv) - delta);
      343973        SvCUR_set(sv, SvCUR(sv) - delta);
      343973        SvPV_set(sv, SvPVX(sv) + delta);
      343973        SvIV_set(sv, SvIVX(sv) + delta);
		}
		
		/* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
		 * this function provided for binary compatibility only
		 */
		
		void
		Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
      ######    {
      ######        sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_catpvn
		
		Concatenates the string onto the end of the string which is in the SV.  The
		C<len> indicates number of bytes to copy.  If the SV has the UTF-8
		status set, then the bytes appended should be valid UTF-8.
		Handles 'get' magic, but not 'set' magic.  See C<sv_catpvn_mg>.
		
		=for apidoc sv_catpvn_flags
		
		Concatenates the string onto the end of the string which is in the SV.  The
		C<len> indicates number of bytes to copy.  If the SV has the UTF-8
		status set, then the bytes appended should be valid UTF-8.
		If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
		appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
		in terms of this function.
		
		=cut
		*/
		
		void
		Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
    17315063    {
    17315063        STRLEN dlen;
    17315063        const char *dstr = SvPV_force_flags(dsv, dlen, flags);
		
    17315063        SvGROW(dsv, dlen + slen + 1);
    17315063        if (sstr == dstr)
          14    	sstr = SvPVX_const(dsv);
    17315063        Move(sstr, SvPVX(dsv) + dlen, slen, char);
    17315063        SvCUR_set(dsv, SvCUR(dsv) + slen);
    17315063        *SvEND(dsv) = '\0';
    17315063        (void)SvPOK_only_UTF8(dsv);		/* validate pointer */
    17315063        SvTAINT(dsv);
		}
		
		/*
		=for apidoc sv_catpvn_mg
		
		Like C<sv_catpvn>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
           1    {
           1        sv_catpvn(sv,ptr,len);
           1        SvSETMAGIC(sv);
		}
		
		/* sv_catsv() is now a macro using Perl_sv_catsv_flags();
		 * this function provided for binary compatibility only
		 */
		
		void
		Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
      ######    {
      ######        sv_catsv_flags(dstr, sstr, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_catsv
		
		Concatenates the string from SV C<ssv> onto the end of the string in
		SV C<dsv>.  Modifies C<dsv> but not C<ssv>.  Handles 'get' magic, but
		not 'set' magic.  See C<sv_catsv_mg>.
		
		=for apidoc sv_catsv_flags
		
		Concatenates the string from SV C<ssv> onto the end of the string in
		SV C<dsv>.  Modifies C<dsv> but not C<ssv>.  If C<flags> has C<SV_GMAGIC>
		bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
		and C<sv_catsv_nomg> are implemented in terms of this function.
		
		=cut */
		
		void
		Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
     8540321    {
     8540321        const char *spv;
     8540321        STRLEN slen;
     8540321        if (!ssv)
      ######    	return;
     8540321        if ((spv = SvPV_const(ssv, slen))) {
			/*  sutf8 and dutf8 were type bool, but under USE_ITHREADS,
			    gcc version 2.95.2 20000220 (Debian GNU/Linux) for
			    Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
			    get dutf8 = 0x20000000, (i.e.  SVf_UTF8) even though
			    dsv->sv_flags doesn't have that bit set.
				Andy Dougherty  12 Oct 2001
			*/
     8540321    	const I32 sutf8 = DO_UTF8(ssv);
     8540321    	I32 dutf8;
		
     8540321    	if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
       11413    	    mg_get(dsv);
     8540321    	dutf8 = DO_UTF8(dsv);
		
     8540321    	if (dutf8 != sutf8) {
       36895    	    if (dutf8) {
				/* Not modifying source SV, so taking a temporary copy. */
       36194    		SV* csv = sv_2mortal(newSVpvn(spv, slen));
		
       36194    		sv_utf8_upgrade(csv);
       36194    		spv = SvPV_const(csv, slen);
			    }
			    else
         701    		sv_utf8_upgrade_nomg(dsv);
			}
     8540321    	sv_catpvn_nomg(dsv, spv, slen);
		    }
		}
		
		/*
		=for apidoc sv_catsv_mg
		
		Like C<sv_catsv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
           1    {
           1        sv_catsv(dsv,ssv);
           1        SvSETMAGIC(dsv);
		}
		
		/*
		=for apidoc sv_catpv
		
		Concatenates the string onto the end of the string which is in the SV.
		If the SV has the UTF-8 status set, then the bytes appended should be
		valid UTF-8.  Handles 'get' magic, but not 'set' magic.  See C<sv_catpv_mg>.
		
		=cut */
		
		void
		Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
      725523    {
      725523        register STRLEN len;
      725523        STRLEN tlen;
      725523        char *junk;
		
      725523        if (!ptr)
      ######    	return;
      725523        junk = SvPV_force(sv, tlen);
      725523        len = strlen(ptr);
      725523        SvGROW(sv, tlen + len + 1);
      725523        if (ptr == junk)
      ######    	ptr = SvPVX_const(sv);
      725523        Move(ptr,SvPVX(sv)+tlen,len+1,char);
      725523        SvCUR_set(sv, SvCUR(sv) + len);
      725523        (void)SvPOK_only_UTF8(sv);		/* validate pointer */
      725523        SvTAINT(sv);
		}
		
		/*
		=for apidoc sv_catpv_mg
		
		Like C<sv_catpv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
           1    {
           1        sv_catpv(sv,ptr);
           1        SvSETMAGIC(sv);
		}
		
		/*
		=for apidoc newSV
		
		Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
		with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
		macro.
		
		=cut
		*/
		
		SV *
		Perl_newSV(pTHX_ STRLEN len)
    50484332    {
    50484332        register SV *sv;
		
    50484332        new_SV(sv);
    50484332        if (len) {
     3796506    	sv_upgrade(sv, SVt_PV);
     3796506    	SvGROW(sv, len + 1);
		    }
    50484332        return sv;
		}
		/*
		=for apidoc sv_magicext
		
		Adds magic to an SV, upgrading it if necessary. Applies the
		supplied vtable and returns a pointer to the magic added.
		
		Note that C<sv_magicext> will allow things that C<sv_magic> will not.
		In particular, you can add magic to SvREADONLY SVs, and add more than
		one instance of the same 'how'.
		
		If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
		stored, if C<namlen> is zero then C<name> is stored as-is and - as another
		special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
		to contain an C<SV*> and is stored as-is with its REFCNT incremented.
		
		(This is now used as a subroutine by C<sv_magic>.)
		
		=cut
		*/
		MAGIC *	
		Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable,
				 const char* name, I32 namlen)
    10952522    {
    10952522        MAGIC* mg;
		
    10952522        if (SvTYPE(sv) < SVt_PVMG) {
     3461722    	SvUPGRADE(sv, SVt_PVMG);
		    }
    10952522        Newz(702,mg, 1, MAGIC);
    10952522        mg->mg_moremagic = SvMAGIC(sv);
    10952522        SvMAGIC_set(sv, mg);
		
		    /* Sometimes a magic contains a reference loop, where the sv and
		       object refer to each other.  To prevent a reference loop that
		       would prevent such objects being freed, we look for such loops
		       and if we find one we avoid incrementing the object refcount.
		
		       Note we cannot do this to avoid self-tie loops as intervening RV must
		       have its REFCNT incremented to keep it in existence.
		
		    */
    10952522        if (!obj || obj == sv ||
			how == PERL_MAGIC_arylen ||
			how == PERL_MAGIC_qr ||
			how == PERL_MAGIC_symtab ||
			(SvTYPE(obj) == SVt_PVGV &&
			    (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
			    GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
			    GvFORM(obj) == (CV*)sv)))
		    {
    10686995    	mg->mg_obj = obj;
		    }
		    else {
      265527    	mg->mg_obj = SvREFCNT_inc(obj);
      265527    	mg->mg_flags |= MGf_REFCOUNTED;
		    }
		
		    /* Normal self-ties simply pass a null object, and instead of
		       using mg_obj directly, use the SvTIED_obj macro to produce a
		       new RV as needed.  For glob "self-ties", we are tieing the PVIO
		       with an RV obj pointing to the glob containing the PVIO.  In
		       this case, to avoid a reference loop, we need to weaken the
		       reference.
		    */
		
    10952522        if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
		        obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
		    {
           2          sv_rvweaken(obj);
		    }
		
    10952522        mg->mg_type = how;
    10952522        mg->mg_len = namlen;
    10952522        if (name) {
      786398    	if (namlen > 0)
      645069    	    mg->mg_ptr = savepvn(name, namlen);
      141329    	else if (namlen == HEf_SVKEY)
      141329    	    mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
			else
      ######    	    mg->mg_ptr = (char *) name;
		    }
    10952522        mg->mg_virtual = vtable;
		
    10952522        mg_magical(sv);
    10952522        if (SvGMAGICAL(sv))
     5377796    	SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
    10952522        return mg;
		}
		
		/*
		=for apidoc sv_magic
		
		Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
		then adds a new magic item of type C<how> to the head of the magic list.
		
		See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
		handling of the C<name> and C<namlen> arguments.
		
		You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
		to add more than one instance of the same 'how'.
		
		=cut
		*/
		
		void
		Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
     9333746    {
     9333746        const MGVTBL *vtable = 0;
     9333746        MAGIC* mg;
		
		#ifdef PERL_OLD_COPY_ON_WRITE
		    if (SvIsCOW(sv))
		        sv_force_normal_flags(sv, 0);
		#endif
     9333746        if (SvREADONLY(sv)) {
       13192    	if (
			    /* its okay to attach magic to shared strings; the subsequent
			     * upgrade to PVMG will unshare the string */
			    !(SvFAKE(sv) && SvTYPE(sv) < SVt_PVMG)
		
			    && IN_PERL_RUNTIME
			    && how != PERL_MAGIC_regex_global
			    && how != PERL_MAGIC_bm
			    && how != PERL_MAGIC_fm
			    && how != PERL_MAGIC_sv
			    && how != PERL_MAGIC_backref
			   )
			{
      ######    	    Perl_croak(aTHX_ PL_no_modify);
			}
		    }
     9333746        if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
     3660633    	if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
			    /* sv_magic() refuses to add a magic of the same 'how' as an
			       existing one
			     */
     2730683    	    if (how == PERL_MAGIC_taint)
     2730651    		mg->mg_len |= 1;
     2730651    	    return;
			}
		    }
		
     6603063        switch (how) {
		    case PERL_MAGIC_sv:
       41349    	vtable = &PL_vtbl_sv;
       41349    	break;
		    case PERL_MAGIC_overload:
         608            vtable = &PL_vtbl_amagic;
         608            break;
		    case PERL_MAGIC_overload_elem:
         228            vtable = &PL_vtbl_amagicelem;
         228            break;
		    case PERL_MAGIC_overload_table:
       42809            vtable = &PL_vtbl_ovrld;
       42809            break;
		    case PERL_MAGIC_bm:
      232490    	vtable = &PL_vtbl_bm;
      232490    	break;
		    case PERL_MAGIC_regdata:
        9000    	vtable = &PL_vtbl_regdata;
        9000    	break;
		    case PERL_MAGIC_regdatum:
        1890    	vtable = &PL_vtbl_regdatum;
        1890    	break;
		    case PERL_MAGIC_env:
        4500    	vtable = &PL_vtbl_env;
        4500    	break;
		    case PERL_MAGIC_fm:
         120    	vtable = &PL_vtbl_fm;
         120    	break;
		    case PERL_MAGIC_envelem:
      215965    	vtable = &PL_vtbl_envelem;
      215965    	break;
		    case PERL_MAGIC_regex_global:
      662189    	vtable = &PL_vtbl_mglob;
      662189    	break;
		    case PERL_MAGIC_isa:
       24556    	vtable = &PL_vtbl_isa;
       24556    	break;
		    case PERL_MAGIC_isaelem:
       14782    	vtable = &PL_vtbl_isaelem;
       14782    	break;
		    case PERL_MAGIC_nkeys:
          16    	vtable = &PL_vtbl_nkeys;
          16    	break;
		    case PERL_MAGIC_dbfile:
          87    	vtable = 0;
          87    	break;
		    case PERL_MAGIC_dbline:
      ######    	vtable = &PL_vtbl_dbline;
      ######    	break;
		#ifdef USE_LOCALE_COLLATE
		    case PERL_MAGIC_collxfrm:
       54254            vtable = &PL_vtbl_collxfrm;
       54254            break;
		#endif /* USE_LOCALE_COLLATE */
		    case PERL_MAGIC_tied:
        4876    	vtable = &PL_vtbl_pack;
        4876    	break;
		    case PERL_MAGIC_tiedelem:
		    case PERL_MAGIC_tiedscalar:
      120235    	vtable = &PL_vtbl_packelem;
      120235    	break;
		    case PERL_MAGIC_qr:
       31490    	vtable = &PL_vtbl_regexp;
       31490    	break;
		    case PERL_MAGIC_sig:
        1978    	vtable = &PL_vtbl_sig;
        1978    	break;
		    case PERL_MAGIC_sigelem:
      137483    	vtable = &PL_vtbl_sigelem;
      137483    	break;
		    case PERL_MAGIC_taint:
     3485443    	vtable = &PL_vtbl_taint;
     3485443    	break;
		    case PERL_MAGIC_uvar:
      ######    	vtable = &PL_vtbl_uvar;
      ######    	break;
		    case PERL_MAGIC_vec:
        9073    	vtable = &PL_vtbl_vec;
        9073    	break;
		    case PERL_MAGIC_arylen_p:
		    case PERL_MAGIC_rhash:
		    case PERL_MAGIC_symtab:
		    case PERL_MAGIC_vstring:
        1839    	vtable = 0;
        1839    	break;
		    case PERL_MAGIC_utf8:
       30956    	vtable = &PL_vtbl_utf8;
       30956    	break;
		    case PERL_MAGIC_substr:
         502    	vtable = &PL_vtbl_substr;
         502    	break;
		    case PERL_MAGIC_defelem:
        2276    	vtable = &PL_vtbl_defelem;
        2276    	break;
		    case PERL_MAGIC_glob:
     1332621    	vtable = &PL_vtbl_glob;
     1332621    	break;
		    case PERL_MAGIC_arylen:
       16154    	vtable = &PL_vtbl_arylen;
       16154    	break;
		    case PERL_MAGIC_pos:
         557    	vtable = &PL_vtbl_pos;
         557    	break;
		    case PERL_MAGIC_backref:
      122737    	vtable = &PL_vtbl_backref;
      122737    	break;
		    case PERL_MAGIC_ext:
			/* Reserved for use by extensions not perl internals.	        */
			/* Useful for attaching extension internal data to perl vars.	*/
			/* Note that multiple extensions may clash if magical scalars	*/
			/* etc holding private data from one are passed to another.	*/
      ######    	break;
		    default:
      ######    	Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
		    }
		
		    /* Rest of work is done else where */
     6603063        mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen);
		
     6603063        switch (how) {
		    case PERL_MAGIC_taint:
     3485443    	mg->mg_len = 1;
     3485443    	break;
		    case PERL_MAGIC_ext:
		    case PERL_MAGIC_dbfile:
          87    	SvRMAGICAL_on(sv);
			break;
		    }
		}
		
		/*
		=for apidoc sv_unmagic
		
		Removes all magic of type C<type> from an SV.
		
		=cut
		*/
		
		int
		Perl_sv_unmagic(pTHX_ SV *sv, int type)
     4383792    {
     4383792        MAGIC* mg;
     4383792        MAGIC** mgp;
     4383792        if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
     4313613    	return 0;
       70179        mgp = &SvMAGIC(sv);
      213908        for (mg = *mgp; mg; mg = *mgp) {
      143729    	if (mg->mg_type == type) {
       38972                const MGVTBL* const vtbl = mg->mg_virtual;
       38972    	    *mgp = mg->mg_moremagic;
       38972    	    if (vtbl && vtbl->svt_free)
       33224    		CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
       38972    	    if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
       33269    		if (mg->mg_len > 0)
       33192    		    Safefree(mg->mg_ptr);
          77    		else if (mg->mg_len == HEf_SVKEY)
          77    		    SvREFCNT_dec((SV*)mg->mg_ptr);
      ######    		else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
      ######    		    Safefree(mg->mg_ptr);
		            }
       38972    	    if (mg->mg_flags & MGf_REFCOUNTED)
        3292    		SvREFCNT_dec(mg->mg_obj);
       38972    	    Safefree(mg);
			}
			else
      104757    	    mgp = &mg->mg_moremagic;
		    }
       70179        if (!SvMAGIC(sv)) {
        5761    	SvMAGICAL_off(sv);
        5761           SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
		    }
		
       70179        return 0;
		}
		
		/*
		=for apidoc sv_rvweaken
		
		Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
		referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
		push a back-reference to this RV onto the array of backreferences
		associated with that magic.
		
		=cut
		*/
		
		SV *
		Perl_sv_rvweaken(pTHX_ SV *sv)
          30    {
          30        SV *tsv;
          30        if (!SvOK(sv))  /* let undefs pass */
      ######    	return sv;
          30        if (!SvROK(sv))
      ######    	Perl_croak(aTHX_ "Can't weaken a nonreference");
          30        else if (SvWEAKREF(sv)) {
      ######    	if (ckWARN(WARN_MISC))
      ######    	    Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
      ######    	return sv;
		    }
          30        tsv = SvRV(sv);
          30        Perl_sv_add_backref(aTHX_ tsv, sv);
          30        SvWEAKREF_on(sv);
          30        SvREFCNT_dec(tsv);
          30        return sv;
		}
		
		/* Give tsv backref magic if it hasn't already got it, then push a
		 * back-reference to sv onto the array associated with the backref magic.
		 */
		
		void
		Perl_sv_add_backref(pTHX_ SV *tsv, SV *sv)
     1432448    {
     1432448        AV *av;
     1432448        MAGIC *mg;
     1432448        if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
     1309711    	av = (AV*)mg->mg_obj;
		    else {
      122737    	av = newAV();
      122737    	sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
			/* av now has a refcnt of 2, which avoids it getting freed
			 * before us during global cleanup. The extra ref is removed
			 * by magic_killbackrefs() when tsv is being freed */
		    }
     1432448        if (AvFILLp(av) >= AvMAX(av)) {
      218791            av_extend(av, AvFILLp(av)+1);
		    }
     1432448        AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
		}
		
		/* delete a back-reference to ourselves from the backref magic associated
		 * with the SV we point to.
		 */
		
		STATIC void
		S_sv_del_backref(pTHX_ SV *tsv, SV *sv)
      958023    {
      958023        AV *av;
      958023        SV **svp;
      958023        I32 i;
      958023        MAGIC *mg = NULL;
      958023        if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref))) {
      ######    	if (PL_in_clean_all)
      ######    	    return;
		    }
      958023        if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
      ######    	Perl_croak(aTHX_ "panic: del_backref");
      958023        av = (AV *)mg->mg_obj;
      958023        svp = AvARRAY(av);
		    /* We shouldn't be in here more than once, but for paranoia reasons lets
		       not assume this.  */
    49609750        for (i = AvFILLp(av); i >= 0; i--) {
    48651727    	if (svp[i] == sv) {
      958022    	    const SSize_t fill = AvFILLp(av);
      958022    	    if (i != fill) {
				/* We weren't the last entry.
				   An unordered list has this property that you can take the
				   last element off the end to fill the hole, and it's still
				   an unordered list :-)
				*/
      635940    		svp[i] = svp[fill];
			    }
      958022    	    svp[fill] = Nullsv;
      958022    	    AvFILLp(av) = fill - 1;
			}
		    }
		}
		
		/*
		=for apidoc sv_insert
		
		Inserts a string at the specified offset/length within the SV. Similar to
		the Perl substr() function.
		
		=cut
		*/
		
		void
		Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen)
       31092    {
       31092        register char *big;
       31092        register char *mid;
       31092        register char *midend;
       31092        register char *bigend;
       31092        register I32 i;
       31092        STRLEN curlen;
		
		
       31092        if (!bigstr)
      ######    	Perl_croak(aTHX_ "Can't modify non-existent substring");
       31092        SvPV_force(bigstr, curlen);
       31092        (void)SvPOK_only_UTF8(bigstr);
       31092        if (offset + len > curlen) {
      ######    	SvGROW(bigstr, offset+len+1);
      ######    	Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
      ######    	SvCUR_set(bigstr, offset+len);
		    }
		
       31092        SvTAINT(bigstr);
       31092        i = littlelen - len;
       31092        if (i > 0) {			/* string might grow */
        8425    	big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
        8425    	mid = big + offset + len;
        8425    	midend = bigend = big + SvCUR(bigstr);
        8425    	bigend += i;
        8425    	*bigend = '\0';
    16845025    	while (midend > mid)		/* shove everything down */
    16836600    	    *--bigend = *--midend;
        8425    	Move(little,big+offset,littlelen,char);
        8425    	SvCUR_set(bigstr, SvCUR(bigstr) + i);
        8425    	SvSETMAGIC(bigstr);
         235    	return;
		    }
       22667        else if (i == 0) {
       12095    	Move(little,SvPVX(bigstr)+offset,len,char);
       12095    	SvSETMAGIC(bigstr);
         166    	return;
		    }
		
       10572        big = SvPVX(bigstr);
       10572        mid = big + offset;
       10572        midend = mid + len;
       10572        bigend = big + SvCUR(bigstr);
		
       10572        if (midend > bigend)
      ######    	Perl_croak(aTHX_ "panic: sv_insert");
		
       10572        if (mid - big > bigend - midend) {	/* faster to shorten from end */
        3573    	if (littlelen) {
         731    	    Move(little, mid, littlelen,char);
         731    	    mid += littlelen;
			}
        3573    	i = bigend - midend;
        3573    	if (i > 0) {
         926    	    Move(midend, mid, i,char);
         926    	    mid += i;
			}
        3573    	*mid = '\0';
        3573    	SvCUR_set(bigstr, mid - big);
		    }
        6999        else if ((i = mid - big)) {	/* faster from front */
        1439    	midend -= littlelen;
        1439    	mid = midend;
        1439    	sv_chop(bigstr,midend-i);
        1439    	big += i;
    10633130    	while (i--)
    10631691    	    *--midend = *--big;
        1439    	if (littlelen)
        1057    	    Move(little, mid, littlelen,char);
		    }
        5560        else if (littlelen) {
         452    	midend -= littlelen;
         452    	sv_chop(bigstr,midend);
         452    	Move(little,midend,littlelen,char);
		    }
		    else {
        5108    	sv_chop(bigstr,midend);
		    }
       10572        SvSETMAGIC(bigstr);
		}
		
		/*
		=for apidoc sv_replace
		
		Make the first argument a copy of the second, then delete the original.
		The target SV physically takes over ownership of the body of the source SV
		and inherits its flags; however, the target keeps any magic it owns,
		and any magic in the source is discarded.
		Note that this is a rather specialist SV copying operation; most of the
		time you'll want to use C<sv_setsv> or one of its many macro front-ends.
		
		=cut
		*/
		
		void
		Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
      524850    {
      524850        const U32 refcnt = SvREFCNT(sv);
      524850        SV_CHECK_THINKFIRST_COW_DROP(sv);
      524850        if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
      ######    	Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
      524850        if (SvMAGICAL(sv)) {
      ######    	if (SvMAGICAL(nsv))
      ######    	    mg_free(nsv);
			else
      ######    	    sv_upgrade(nsv, SVt_PVMG);
      ######    	SvMAGIC_set(nsv, SvMAGIC(sv));
      ######    	SvFLAGS(nsv) |= SvMAGICAL(sv);
      ######    	SvMAGICAL_off(sv);
      ######    	SvMAGIC_set(sv, NULL);
		    }
      524850        SvREFCNT(sv) = 0;
      524850        sv_clear(sv);
      524850        assert(!SvREFCNT(sv));
		#ifdef DEBUG_LEAKING_SCALARS
		    sv->sv_flags  = nsv->sv_flags;
		    sv->sv_any    = nsv->sv_any;
		    sv->sv_refcnt = nsv->sv_refcnt;
		    sv->sv_u      = nsv->sv_u;
		#else
      524850        StructCopy(nsv,sv,SV);
		#endif
		    /* Currently could join these into one piece of pointer arithmetic, but
		       it would be unclear.  */
      524850        if(SvTYPE(sv) == SVt_IV)
      ######    	SvANY(sv)
			    = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
      524850        else if (SvTYPE(sv) == SVt_RV) {
      ######    	SvANY(sv) = &sv->sv_u.svu_rv;
		    }
			
		
		#ifdef PERL_OLD_COPY_ON_WRITE
		    if (SvIsCOW_normal(nsv)) {
			/* We need to follow the pointers around the loop to make the
			   previous SV point to sv, rather than nsv.  */
			SV *next;
			SV *current = nsv;
			while ((next = SV_COW_NEXT_SV(current)) != nsv) {
			    assert(next);
			    current = next;
			    assert(SvPVX_const(current) == SvPVX_const(nsv));
			}
			/* Make the SV before us point to the SV after us.  */
			if (DEBUG_C_TEST) {
			    PerlIO_printf(Perl_debug_log, "previous is\n");
			    sv_dump(current);
			    PerlIO_printf(Perl_debug_log,
		                          "move it from 0x%"UVxf" to 0x%"UVxf"\n",
					  (UV) SV_COW_NEXT_SV(current), (UV) sv);
			}
			SV_COW_NEXT_SV_SET(current, sv);
		    }
		#endif
      524850        SvREFCNT(sv) = refcnt;
      524850        SvFLAGS(nsv) |= SVTYPEMASK;		/* Mark as freed */
      524850        SvREFCNT(nsv) = 0;
      524850        del_SV(nsv);
		}
		
		/*
		=for apidoc sv_clear
		
		Clear an SV: call any destructors, free up any memory used by the body,
		and free the body itself. The SV's head is I<not> freed, although
		its type is set to all 1's so that it won't inadvertently be assumed
		to be live during global destruction etc.
		This function should only be called when REFCNT is zero. Most of the time
		you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
		instead.
		
		=cut
		*/
		
		void
		Perl_sv_clear(pTHX_ register SV *sv)
   104230501    {
		    dVAR;
   104230501        void** old_body_arena;
   104230501        size_t old_body_offset;
   104230501        const U32 type = SvTYPE(sv);
		
   104230501        assert(sv);
   104230501        assert(SvREFCNT(sv) == 0);
		
   104230501        if (type <= SVt_IV)
    45509552    	return;
		
    58720949        old_body_arena = 0;
    58720949        old_body_offset = 0;
		
    58720949        if (SvOBJECT(sv)) {
     1476974    	if (PL_defstash) {		/* Still have a symbol table? */
     1456873    	    dSP;
     1456874    	    HV* stash;
     1456874    	    do {	
     1456874    		CV* destructor;
     1456874    		stash = SvSTASH(sv);
     1456874    		destructor = StashHANDLER(stash,DESTROY);
     1456874    		if (destructor) {
       74311    		    SV* const tmpref = newRV(sv);
       74311    	            SvREADONLY_on(tmpref);   /* DESTROY() could be naughty */
       74311    		    ENTER;
       74311    		    PUSHSTACKi(PERLSI_DESTROY);
       74311    		    EXTEND(SP, 2);
       74311    		    PUSHMARK(SP);
       74311    		    PUSHs(tmpref);
       74311    		    PUTBACK;
       74311    		    call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
				
				
       74311    		    POPSTACK;
       74311    		    SPAGAIN;
       74311    		    LEAVE;
       74311    		    if(SvREFCNT(tmpref) < 2) {
				        /* tmpref is not kept alive! */
       74311    		        SvREFCNT(sv)--;
       74311    			SvRV_set(tmpref, NULL);
       74311    			SvROK_off(tmpref);
				    }
       74311    		    SvREFCNT_dec(tmpref);
				}
     1456874    	    } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
		
		
     1456873    	    if (SvREFCNT(sv)) {
      ######    		if (PL_in_clean_objs)
      ######    		    Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
					  HvNAME_get(stash));
				/* DESTROY gave object new lease on life */
     1476974    		return;
			    }
			}
		
     1476974    	if (SvOBJECT(sv)) {
     1476974    	    SvREFCNT_dec(SvSTASH(sv));	/* possibly of changed persuasion */
     1476974    	    SvOBJECT_off(sv);	/* Curse the object. */
     1476974    	    if (type != SVt_PVIO)
     1439418    		--PL_sv_objcount;	/* XXX Might want something more general */
			}
		    }
    58720949        if (type >= SVt_PVMG) {
    14418808        	if (SvMAGIC(sv))
     5354484    	    mg_free(sv);
    14418808    	if (type == SVt_PVMG && SvFLAGS(sv) & SVpad_TYPED)
          30    	    SvREFCNT_dec(SvSTASH(sv));
		    }
    58720949        switch (type) {
		    case SVt_PVIO:
       39650    	if (IoIFP(sv) &&
			    IoIFP(sv) != PerlIO_stdin() &&
			    IoIFP(sv) != PerlIO_stdout() &&
			    IoIFP(sv) != PerlIO_stderr())
			{
        2501    	    io_close((IO*)sv, FALSE);
			}
       39650    	if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
        2013    	    PerlDir_close(IoDIRP(sv));
       39650    	IoDIRP(sv) = (DIR*)NULL;
       39650    	Safefree(IoTOP_NAME(sv));
       39650    	Safefree(IoFMT_NAME(sv));
       39650    	Safefree(IoBOTTOM_NAME(sv));
			/* PVIOs aren't from arenas  */
       39650    	goto freescalar;
		    case SVt_PVBM:
      233396    	old_body_arena = (void **) &PL_xpvbm_root;
      233396    	goto freescalar;
		    case SVt_PVCV:
      787486    	old_body_arena = (void **) &PL_xpvcv_root;
		    case SVt_PVFM:
			/* PVFMs aren't from arenas  */
      796728    	cv_undef((CV*)sv);
      796728    	goto freescalar;
		    case SVt_PVHV:
     1308195    	hv_undef((HV*)sv);
     1308195    	old_body_arena = (void **) &PL_xpvhv_root;
     1308195    	old_body_offset = STRUCT_OFFSET(XPVHV, xhv_fill);
     1308195    	break;
		    case SVt_PVAV:
     4079720    	av_undef((AV*)sv);
     4079720    	old_body_arena = (void **) &PL_xpvav_root;
     4079720    	old_body_offset = STRUCT_OFFSET(XPVAV, xav_fill);
     4079720    	break;
		    case SVt_PVLV:
      481044    	if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
       11533    	    SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
       11533    	    HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
       11533    	    PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
			}
      469511    	else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV**  */
      465738    	    SvREFCNT_dec(LvTARG(sv));
      481044    	old_body_arena = (void **) &PL_xpvlv_root;
      481044    	goto freescalar;
		    case SVt_PVGV:
     1475801    	gp_free((GV*)sv);
     1475801    	Safefree(GvNAME(sv));
			/* If we're in a stash, we don't own a reference to it. However it does
			   have a back reference to us, which needs to be cleared.  */
     1475801    	if (GvSTASH(sv))
      955531    	    sv_del_backref((SV*)GvSTASH(sv), sv);
     1475801    	old_body_arena = (void **) &PL_xpvgv_root;
     1475801    	goto freescalar;
		    case SVt_PVMG:
     6004274    	old_body_arena = (void **) &PL_xpvmg_root;
     6004274    	goto freescalar;
		    case SVt_PVNV:
     3176343    	old_body_arena = (void **) &PL_xpvnv_root;
     3176343    	goto freescalar;
		    case SVt_PVIV:
     4874474    	old_body_arena = (void **) &PL_xpviv_root;
     4874474    	old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur);
		      freescalar:
			/* Don't bother with SvOOK_off(sv); as we're only going to free it.  */
    17081710    	if (SvOOK(sv)) {
       14374    	    SvPV_set(sv, SvPVX_mutable(sv) - SvIVX(sv));
			    /* Don't even bother with turning off the OOK flag.  */
			}
       14374    	goto pvrv_common;
		    case SVt_PV:
    27984716    	old_body_arena = (void **) &PL_xpv_root;
    27984716    	old_body_offset = STRUCT_OFFSET(XPV, xpv_cur);
		    case SVt_RV:
		    pvrv_common:
    51741461    	if (SvROK(sv)) {
     6660935    	    SV *target = SvRV(sv);
     6660935    	    if (SvWEAKREF(sv))
           9    	        sv_del_backref(target, sv);
			    else
     6660926    	        SvREFCNT_dec(target);
			}
		#ifdef PERL_OLD_COPY_ON_WRITE
			else if (SvPVX_const(sv)) {
		            if (SvIsCOW(sv)) {
		                /* I believe I need to grab the global SV mutex here and
		                   then recheck the COW status.  */
		                if (DEBUG_C_TEST) {
		                    PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
		                    sv_dump(sv);
		                }
		                sv_release_COW(sv, SvPVX_const(sv), SvLEN(sv),
					       SV_COW_NEXT_SV(sv));
		                /* And drop it here.  */
		                SvFAKE_off(sv);
		            } else if (SvLEN(sv)) {
		                Safefree(SvPVX_const(sv));
		            }
			}
		#else
    45080526    	else if (SvPVX_const(sv) && SvLEN(sv))
    34966491    	    Safefree(SvPVX_mutable(sv));
    10114035    	else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
     2967110    	    unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
     2967110    	    SvFAKE_off(sv);
			}
		#endif
     2967110    	break;
		    case SVt_NV:
     1591573    	old_body_arena = (void **) &PL_xnv_root;
			break;
		    }
		
    58720949        SvFLAGS(sv) &= SVf_BREAK;
    58720949        SvFLAGS(sv) |= SVTYPEMASK;
		
		#ifndef PURIFY
    58720949        if (old_body_arena) {
    51997022    	del_body(((char *)SvANY(sv) + old_body_offset), old_body_arena);
		    }
		    else
		#endif
     6723927    	if (type > SVt_RV) {
       48892    	    my_safefree(SvANY(sv));
			}
		}
		
		/*
		=for apidoc sv_newref
		
		Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
		instead.
		
		=cut
		*/
		
		SV *
		Perl_sv_newref(pTHX_ SV *sv)
      ######    {
      ######        if (sv)
      ######    	(SvREFCNT(sv))++;
      ######        return sv;
		}
		
		/*
		=for apidoc sv_free
		
		Decrement an SV's reference count, and if it drops to zero, call
		C<sv_clear> to invoke destructors and free up any memory used by
		the body; finally, deallocate the SV's head itself.
		Normally called via a wrapper macro C<SvREFCNT_dec>.
		
		=cut
		*/
		
		void
		Perl_sv_free(pTHX_ SV *sv)
     6025440    {
		    dVAR;
     6025440        if (!sv)
      ######    	return;
     6025440        if (SvREFCNT(sv) == 0) {
     5789788    	if (SvFLAGS(sv) & SVf_BREAK)
			    /* this SV's refcnt has been artificially decremented to
			     * trigger cleanup */
       18751    	    return;
     5771037    	if (PL_in_clean_all) /* All is fair */
     5771037    	    return;
      ######    	if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
			    /* make sure SvREFCNT(sv)==0 happens very seldom */
      ######    	    SvREFCNT(sv) = (~(U32)0)/2;
      ######    	    return;
			}
      ######    	if (ckWARN_d(WARN_INTERNAL)) {
      ######    	    Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
		                        "Attempt to free unreferenced scalar: SV 0x%"UVxf
		                        pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
		#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
			    Perl_dump_sv_child(aTHX_ sv);
		#endif
			}
      ######    	return;
		    }
      235652        if (--(SvREFCNT(sv)) > 0)
          52    	return;
      235600        Perl_sv_free2(aTHX_ sv);
		}
		
		void
		Perl_sv_free2(pTHX_ SV *sv)
   103682896    {
		    dVAR;
		#ifdef DEBUGGING
   103682896        if (SvTEMP(sv)) {
      ######    	if (ckWARN_d(WARN_DEBUGGING))
      ######    	    Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
					"Attempt to free temp prematurely: SV 0x%"UVxf
		                        pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
      ######    	return;
		    }
		#endif
   103682896        if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
			/* make sure SvREFCNT(sv)==0 happens very seldom */
      ######    	SvREFCNT(sv) = (~(U32)0)/2;
      ######    	return;
		    }
   103682896        sv_clear(sv);
   103682896        if (! SvREFCNT(sv))
   103682896    	del_SV(sv);
		}
		
		/*
		=for apidoc sv_len
		
		Returns the length of the string in the SV. Handles magic and type
		coercion.  See also C<SvCUR>, which gives raw access to the xpv_cur slot.
		
		=cut
		*/
		
		STRLEN
		Perl_sv_len(pTHX_ register SV *sv)
     4714223    {
     4714223        STRLEN len;
		
     4714223        if (!sv)
      ######    	return 0;
		
     4714223        if (SvGMAGICAL(sv))
        9838    	len = mg_length(sv);
		    else
     4704385            (void)SvPV_const(sv, len);
     4714223        return len;
		}
		
		/*
		=for apidoc sv_len_utf8
		
		Returns the number of characters in the string in an SV, counting wide
		UTF-8 bytes as a single character. Handles magic and type coercion.
		
		=cut
		*/
		
		/*
		 * The length is cached in PERL_UTF8_magic, in the mg_len field.  Also the
		 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
		 * (Note that the mg_len is not the length of the mg_ptr field.)
		 *
		 */
		
		STRLEN
		Perl_sv_len_utf8(pTHX_ register SV *sv)
      117341    {
      117341        if (!sv)
      ######    	return 0;
		
      117341        if (SvGMAGICAL(sv))
           8    	return mg_length(sv);
		    else
		    {
      117333    	STRLEN len, ulen;
      117333    	const U8 *s = (U8*)SvPV_const(sv, len);
      117333    	MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
		
      117333    	if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
       41793    	    ulen = mg->mg_len;
		#ifdef PERL_UTF8_CACHE_ASSERT
			    assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
		#endif
			}
			else {
       75540    	    ulen = Perl_utf8_length(aTHX_ s, s + len);
       75540    	    if (!mg && !SvREADONLY(sv)) {
       30887    		sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
       30887    		mg = mg_find(sv, PERL_MAGIC_utf8);
       30887    		assert(mg);
			    }
       75540    	    if (mg)
       75512    		mg->mg_len = ulen;
			}
      117333    	return ulen;
		    }
		}
		
		/* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
		 * a PERL_UTF8_magic.  The mg_ptr is used to store the mapping
		 * between UTF-8 and byte offsets.  There are two (substr offset and substr
		 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
		 * and byte offset) cache positions.
		 *
		 * The mg_len field is used by sv_len_utf8(), see its comments.
		 * Note that the mg_len is not the length of the mg_ptr field.
		 *
		 */
		STATIC bool
		S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i,
				   I32 offsetp, const U8 *s, const U8 *start)
        5394    {
        5394        bool found = FALSE;
		
        5394        if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
        5186    	if (!*mgp)
           8    	    *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0);
        5186    	assert(*mgp);
		
        5186    	if ((*mgp)->mg_ptr)
        2866    	    *cachep = (STRLEN *) (*mgp)->mg_ptr;
			else {
        2320    	    Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
        2320    	    (*mgp)->mg_ptr = (char *) *cachep;
			}
        5186    	assert(*cachep);
		
        5186    	(*cachep)[i]   = offsetp;
        5186    	(*cachep)[i+1] = s - start;
        5186    	found = TRUE;
		    }
		
        5394        return found;
		}
		
		/*
		 * S_utf8_mg_pos() is used to query and update mg_ptr field of
		 * a PERL_UTF8_magic.  The mg_ptr is used to store the mapping
		 * between UTF-8 and byte offsets.  See also the comments of
		 * S_utf8_mg_pos_init().
		 *
		 */
		STATIC bool
		S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, const U8 **sp, const U8 *start, const U8 *send)
       18179    {
       18179        bool found = FALSE;
		
       18179        if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
       17898    	if (!*mgp)
        8985    	    *mgp = mg_find(sv, PERL_MAGIC_utf8);
       17898    	if (*mgp && (*mgp)->mg_ptr) {
       14102    	    *cachep = (STRLEN *) (*mgp)->mg_ptr;
       14102    	    ASSERT_UTF8_CACHE(*cachep);
       14102    	    if ((*cachep)[i] == (STRLEN)uoff)	/* An exact match. */
        9013                     found = TRUE;
			    else {			/* We will skip to the right spot. */
        5089    		 STRLEN forw  = 0;
        5089    		 STRLEN backw = 0;
        5089    		 const U8* p = NULL;
		
				 /* The assumption is that going backward is half
				  * the speed of going forward (that's where the
				  * 2 * backw in the below comes from).  (The real
				  * figure of course depends on the UTF-8 data.) */
		
        5089    		 if ((*cachep)[i] > (STRLEN)uoff) {
         535    		      forw  = uoff;
         535    		      backw = (*cachep)[i] - (STRLEN)uoff;
		
         535    		      if (forw < 2 * backw)
         531    			   p = start;
				      else
           4    			   p = start + (*cachep)[i+1];
				 }
				 /* Try this only for the substr offset (i == 0),
				  * not for the substr length (i == 2). */
        4554    		 else if (i == 0) { /* (*cachep)[i] < uoff */
        1688    		      const STRLEN ulen = sv_len_utf8(sv);
		
        1688    		      if ((STRLEN)uoff < ulen) {
        1688    			   forw  = (STRLEN)uoff - (*cachep)[i];
        1688    			   backw = ulen - (STRLEN)uoff;
		
        1688    			   if (forw < 2 * backw)
         294    				p = start + (*cachep)[i+1];
					   else
        1394    				p = send;
				      }
		
				      /* If the string is not long enough for uoff,
				       * we could extend it, but not at this low a level. */
				 }
		
        5089    		 if (p) {
        2223    		      if (forw < 2 * backw) {
        1148    			   while (forw--)
         323    				p += UTF8SKIP(p);
				      }
				      else {
        2805    			   while (backw--) {
        1407    				p--;
        4120    				while (UTF8_IS_CONTINUATION(*p))
        2713    				     p--;
					   }
				      }
		
				      /* Update the cache. */
        2223    		      (*cachep)[i]   = (STRLEN)uoff;
        2223    		      (*cachep)[i+1] = p - start;
		
				      /* Drop the stale "length" cache */
        2223    		      if (i == 0) {
        2217    			  (*cachep)[2] = 0;
        2217    			  (*cachep)[3] = 0;
				      }
		
        2223    		      found = TRUE;
				 }
			    }
       14102    	    if (found) {	/* Setup the return values. */
       11236    		 *offsetp = (*cachep)[i+1];
       11236    		 *sp = start + *offsetp;
       11236    		 if (*sp >= send) {
        3127    		      *sp = send;
        3127    		      *offsetp = send - start;
				 }
        8109    		 else if (*sp < start) {
      ######    		      *sp = start;
      ######    		      *offsetp = 0;
				 }
			    }
			}
		#ifdef PERL_UTF8_CACHE_ASSERT
			if (found) {
			     U8 *s = start;
			     I32 n = uoff;
		
			     while (n-- && s < send)
				  s += UTF8SKIP(s);
		
			     if (i == 0) {
				  assert(*offsetp == s - start);
				  assert((*cachep)[0] == (STRLEN)uoff);
				  assert((*cachep)[1] == *offsetp);
			     }
			     ASSERT_UTF8_CACHE(*cachep);
			}
		#endif
		    }
		
       18179        return found;
		}
		
		/*
		=for apidoc sv_pos_u2b
		
		Converts the value pointed to by offsetp from a count of UTF-8 chars from
		the start of the string, to a count of the equivalent number of bytes; if
		lenp is non-zero, it does the same to lenp, but this time starting from
		the offset, rather than from the start of the string. Handles magic and
		type coercion.
		
		=cut
		*/
		
		/*
		 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
		 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
		 * byte offsets.  See also the comments of S_utf8_mg_pos().
		 *
		 */
		
		void
		Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
        9120    {
        9120        const U8 *start;
        9120        STRLEN len;
		
        9120        if (!sv)
      ######    	return;
		
        9120        start = (U8*)SvPV_const(sv, len);
        9120        if (len) {
        9120    	STRLEN boffset = 0;
        9120    	STRLEN *cache = 0;
        9120    	const U8 *s = start;
        9120    	I32 uoffset = *offsetp;
        9120    	const U8 * const send = s + len;
        9120    	MAGIC *mg = 0;
        9120    	bool found = FALSE;
		
        9120             if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
        6608                 found = TRUE;
        9120    	 if (!found && uoffset > 0) {
       11638    	      while (s < send && uoffset--)
       10618    		   s += UTF8SKIP(s);
        1020    	      if (s >= send)
         298    		   s = send;
        1020                  if (utf8_mg_pos_init(sv, &mg, &cache, 0, *offsetp, s, start))
         902                      boffset = cache[1];
        1020    	      *offsetp = s - start;
			 }
        9120    	 if (lenp) {
        9059    	      found = FALSE;
        9059    	      start = s;
        9059                  if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
        4628                      *lenp -= boffset;
        4628                      found = TRUE;
		              }
        9059    	      if (!found && *lenp > 0) {
        4374    		   I32 ulen = *lenp;
        4374    		   if (ulen > 0)
     1207432    			while (s < send && ulen--)
     1203058    			     s += UTF8SKIP(s);
        4374    		   if (s >= send)
        2504    			s = send;
        4374                       utf8_mg_pos_init(sv, &mg, &cache, 2, *lenp, s, start);
			      }
        9059    	      *lenp = s - start;
			 }
        9059    	 ASSERT_UTF8_CACHE(cache);
		    }
		    else {
      ######    	 *offsetp = 0;
      ######    	 if (lenp)
      ######    	      *lenp = 0;
		    }
		
        9120        return;
		}
		
		/*
		=for apidoc sv_pos_b2u
		
		Converts the value pointed to by offsetp from a count of bytes from the
		start of the string, to a count of the equivalent number of UTF-8 chars.
		Handles magic and type coercion.
		
		=cut
		*/
		
		/*
		 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
		 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
		 * byte offsets.  See also the comments of S_utf8_mg_pos().
		 *
		 */
		
		void
		Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
        9511    {
        9511        const U8* s;
        9511        STRLEN len;
		
        9511        if (!sv)
      ######    	return;
		
        9511        s = (const U8*)SvPV_const(sv, len);
        9511        if ((I32)len < *offsetp)
      ######    	Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
		    else {
        9511    	const U8* send = s + *offsetp;
        9511    	MAGIC* mg = NULL;
        9511    	STRLEN *cache = NULL;
		
        9511    	len = 0;
		
        9511    	if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
        9501    	    mg = mg_find(sv, PERL_MAGIC_utf8);
        9501    	    if (mg && mg->mg_ptr) {
        9395    		cache = (STRLEN *) mg->mg_ptr;
        9395    		if (cache[1] == (STRLEN)*offsetp) {
		                    /* An exact match. */
           1                        *offsetp = cache[0];
		
           1    		    return;
				}
        9394    		else if (cache[1] < (STRLEN)*offsetp) {
				    /* We already know part of the way. */
        9379    		    len = cache[0];
        9379    		    s  += cache[1];
				    /* Let the below loop do the rest. */
				}
				else { /* cache[1] > *offsetp */
				    /* We already know all of the way, now we may
				     * be able to walk back.  The same assumption
				     * is made as in S_utf8_mg_pos(), namely that
				     * walking backward is twice slower than
				     * walking forward. */
          15    		    const STRLEN forw  = *offsetp;
          15    		    STRLEN backw = cache[1] - *offsetp;
		
          15    		    if (!(forw < 2 * backw)) {
          11    			const U8 *p = s + cache[1];
          11    			STRLEN ubackw = 0;
					
          11    			cache[1] -= backw;
		
          54    			while (backw--) {
          43    			    p--;
          45    			    while (UTF8_IS_CONTINUATION(*p)) {
           2    				p--;
           2    				backw--;
					    }
          43    			    ubackw++;
					}
		
          11    			cache[0] -= ubackw;
          11    			*offsetp = cache[0];
		
					/* Drop the stale "length" cache */
          11    			cache[2] = 0;
          11    			cache[3] = 0;
		
          11    			return;
				    }
				}
			    }
        9499    	    ASSERT_UTF8_CACHE(cache);
			}
		
      378462    	while (s < send) {
      368963    	    STRLEN n = 1;
		
			    /* Call utf8n_to_uvchr() to validate the sequence
			     * (unless a simple non-UTF character) */
      368963    	    if (!UTF8_IS_INVARIANT(*s))
       61728    		utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
      368963    	    if (n > 0) {
      368963    		s += n;
      368963    		len++;
			    }
			    else
        9499    		break;
			}
		
        9499    	if (!SvREADONLY(sv)) {
        9495    	    if (!mg) {
          69    		sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
          69    		mg = mg_find(sv, PERL_MAGIC_utf8);
			    }
        9495    	    assert(mg);
		
        9495    	    if (!mg->mg_ptr) {
         112    		Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
         112    		mg->mg_ptr = (char *) cache;
			    }
        9495    	    assert(cache);
		
        9495    	    cache[0] = len;
        9495    	    cache[1] = *offsetp;
			    /* Drop the stale "length" cache */
        9495    	    cache[2] = 0;
        9495    	    cache[3] = 0;
			}
		
        9499    	*offsetp = len;
		    }
        9511        return;
		}
		
		/*
		=for apidoc sv_eq
		
		Returns a boolean indicating whether the strings in the two SVs are
		identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
		coerce its args to strings if necessary.
		
		=cut
		*/
		
		I32
		Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
     3802034    {
     3802034        const char *pv1;
     3802034        STRLEN cur1;
     3802034        const char *pv2;
     3802034        STRLEN cur2;
     3802034        I32  eq     = 0;
     3802034        char *tpv   = Nullch;
     3802034        SV* svrecode = Nullsv;
		
     3802034        if (!sv1) {
      ######    	pv1 = "";
      ######    	cur1 = 0;
		    }
		    else
     3802034    	pv1 = SvPV_const(sv1, cur1);
		
     3802034        if (!sv2){
      ######    	pv2 = "";
      ######    	cur2 = 0;
		    }
		    else
     3802034    	pv2 = SvPV_const(sv2, cur2);
		
     3802034        if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
		        /* Differing utf8ness.
			 * Do not UTF8size the comparands as a side-effect. */
        3355    	 if (PL_encoding) {
         244    	      if (SvUTF8(sv1)) {
         169    		   svrecode = newSVpvn(pv2, cur2);
         169    		   sv_recode_to_utf8(svrecode, PL_encoding);
         169    		   pv2 = SvPV_const(svrecode, cur2);
			      }
			      else {
          75    		   svrecode = newSVpvn(pv1, cur1);
          75    		   sv_recode_to_utf8(svrecode, PL_encoding);
          75    		   pv1 = SvPV_const(svrecode, cur1);
			      }
			      /* Now both are in UTF-8. */
         244    	      if (cur1 != cur2) {
          75    		   SvREFCNT_dec(svrecode);
          75    		   return FALSE;
			      }
			 }
			 else {
        3111    	      bool is_utf8 = TRUE;
		
        3111    	      if (SvUTF8(sv1)) {
				   /* sv1 is the UTF-8 one,
				    * if is equal it must be downgrade-able */
        2867    		   char * const pv = (char*)bytes_from_utf8((const U8*)pv1,
        2867    						     &cur1, &is_utf8);
        2867    		   if (pv != pv1)
         718    			pv1 = tpv = pv;
			      }
			      else {
				   /* sv2 is the UTF-8 one,
				    * if is equal it must be downgrade-able */
         244    		   char * const pv = (char *)bytes_from_utf8((const U8*)pv2,
         244    						      &cur2, &is_utf8);
         244    		   if (pv != pv2)
         198    			pv2 = tpv = pv;
			      }
        3111    	      if (is_utf8) {
				   /* Downgrade not possible - cannot be eq */
        2195    		   assert (tpv == 0);
        2195    		   return FALSE;
			      }
			 }
		    }
		
     3799764        if (cur1 == cur2)
     2324732    	eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
			
     3799764        if (svrecode)
         169    	 SvREFCNT_dec(svrecode);
		
     3799764        if (tpv)
         916    	Safefree(tpv);
		
     3799764        return eq;
		}
		
		/*
		=for apidoc sv_cmp
		
		Compares the strings in two SVs.  Returns -1, 0, or 1 indicating whether the
		string in C<sv1> is less than, equal to, or greater than the string in
		C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
		coerce its args to strings if necessary.  See also C<sv_cmp_locale>.
		
		=cut
		*/
		
		I32
		Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
     4626113    {
     4626113        STRLEN cur1, cur2;
     4626113        const char *pv1, *pv2;
     4626113        char *tpv = Nullch;
     4626113        I32  cmp;
     4626113        SV *svrecode = Nullsv;
		
     4626113        if (!sv1) {
      ######    	pv1 = "";
      ######    	cur1 = 0;
		    }
		    else
     4626113    	pv1 = SvPV_const(sv1, cur1);
		
     4626113        if (!sv2) {
      ######    	pv2 = "";
      ######    	cur2 = 0;
		    }
		    else
     4626113    	pv2 = SvPV_const(sv2, cur2);
		
     4626113        if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
		        /* Differing utf8ness.
			 * Do not UTF8size the comparands as a side-effect. */
          84    	if (SvUTF8(sv1)) {
          43    	    if (PL_encoding) {
          12    		 svrecode = newSVpvn(pv2, cur2);
          12    		 sv_recode_to_utf8(svrecode, PL_encoding);
          12    		 pv2 = SvPV_const(svrecode, cur2);
			    }
			    else {
          31    		 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
			    }
			}
			else {
          41    	    if (PL_encoding) {
           7    		 svrecode = newSVpvn(pv1, cur1);
           7    		 sv_recode_to_utf8(svrecode, PL_encoding);
           7    		 pv1 = SvPV_const(svrecode, cur1);
			    }
			    else {
          34    		 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
			    }
			}
		    }
		
     4626113        if (!cur1) {
         528    	cmp = cur2 ? -1 : 0;
     4625585        } else if (!cur2) {
         105    	cmp = 1;
		    } else {
     4625480            const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
		
     4625480    	if (retval) {
     4469894    	    cmp = retval < 0 ? -1 : 1;
      155586    	} else if (cur1 == cur2) {
      153414    	    cmp = 0;
		        } else {
        2172    	    cmp = cur1 < cur2 ? -1 : 1;
			}
		    }
		
     4626113        if (svrecode)
          19    	 SvREFCNT_dec(svrecode);
		
     4626113        if (tpv)
          65    	Safefree(tpv);
		
     4626113        return cmp;
		}
		
		/*
		=for apidoc sv_cmp_locale
		
		Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
		'use bytes' aware, handles get magic, and will coerce its args to strings
		if necessary.  See also C<sv_cmp_locale>.  See also C<sv_cmp>.
		
		=cut
		*/
		
		I32
		Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
      476391    {
		#ifdef USE_LOCALE_COLLATE
		
      476391        char *pv1, *pv2;
      476391        STRLEN len1, len2;
      476391        I32 retval;
		
      476391        if (PL_collation_standard)
        1560    	goto raw_compare;
		
      474831        len1 = 0;
      474831        pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
      474831        len2 = 0;
      474831        pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
		
      474831        if (!pv1 || !len1) {
      ######    	if (pv2 && len2)
      ######    	    return -1;
			else
      474831    	    goto raw_compare;
		    }
		    else {
      474831    	if (!pv2 || !len2)
      ######    	    return 1;
		    }
		
      474831        retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
		
      474831        if (retval)
      436002    	return retval < 0 ? -1 : 1;
		
		    /*
		     * When the result of collation is equality, that doesn't mean
		     * that there are no differences -- some locales exclude some
		     * characters from consideration.  So to avoid false equalities,
		     * we use the raw string as a tiebreaker.
		     */
		
		  raw_compare:
		    /* FALL THROUGH */
		
		#endif /* USE_LOCALE_COLLATE */
		
       40389        return sv_cmp(sv1, sv2);
		}
		
		
		#ifdef USE_LOCALE_COLLATE
		
		/*
		=for apidoc sv_collxfrm
		
		Add Collate Transform magic to an SV if it doesn't already have it.
		
		Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
		scalar data of the variable, but transformed to such a format that a normal
		memory comparison can be used to compare the data according to the locale
		settings.
		
		=cut
		*/
		
		char *
		Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
      949662    {
      949662        MAGIC *mg;
		
      949662        mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
      949662        if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
      569651    	const char *s;
      569651    	char *xf;
      569651    	STRLEN len, xlen;
		
      569651    	if (mg)
        4512    	    Safefree(mg->mg_ptr);
      569651    	s = SvPV_const(sv, len);
      569651    	if ((xf = mem_collxfrm(s, len, &xlen))) {
      569651    	    if (SvREADONLY(sv)) {
      510885    		SAVEFREEPV(xf);
      510885    		*nxp = xlen;
      510885    		return xf + sizeof(PL_collation_ix);
			    }
       58766    	    if (! mg) {
       54254    		sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
       54254    		mg = mg_find(sv, PERL_MAGIC_collxfrm);
       54254    		assert(mg);
			    }
       58766    	    mg->mg_ptr = xf;
       58766    	    mg->mg_len = xlen;
			}
			else {
      ######    	    if (mg) {
      ######    		mg->mg_ptr = NULL;
      ######    		mg->mg_len = -1;
			    }
			}
		    }
      438777        if (mg && mg->mg_ptr) {
      438777    	*nxp = mg->mg_len;
      438777    	return mg->mg_ptr + sizeof(PL_collation_ix);
		    }
		    else {
      ######    	*nxp = 0;
      ######    	return NULL;
		    }
		}
		
		#endif /* USE_LOCALE_COLLATE */
		
		/*
		=for apidoc sv_gets
		
		Get a line from the filehandle and store it into the SV, optionally
		appending to the currently-stored string.
		
		=cut
		*/
		
		char *
		Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
    11131675    {
    11131675        const char *rsptr;
    11131675        STRLEN rslen;
    11131675        register STDCHAR rslast;
    11131675        register STDCHAR *bp;
    11131675        register I32 cnt;
    11131675        I32 i = 0;
    11131675        I32 rspara = 0;
    11131675        I32 recsize;
		
    11131675        if (SvTHINKFIRST(sv))
           5    	sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
		    /* XXX. If you make this PVIV, then copy on write can copy scalars read
		       from <>.
		       However, perlbench says it's slower, because the existing swipe code
		       is faster than copy on write.
		       Swings and roundabouts.  */
    11131675        SvUPGRADE(sv, SVt_PV);
		
    11131675        SvSCREAM_off(sv);
		
    11131675        if (append) {
      208028    	if (PerlIO_isutf8(fp)) {
          10    	    if (!SvUTF8(sv)) {
           5    		sv_utf8_upgrade_nomg(sv);
           5    		sv_pos_u2b(sv,&append,0);
			    }
      208018    	} else if (SvUTF8(sv)) {
           5    	    SV * const tsv = NEWSV(0,0);
           5    	    sv_gets(tsv, fp, 0);
           5    	    sv_utf8_upgrade_nomg(tsv);
           5    	    SvCUR_set(sv,append);
           5    	    sv_catsv(sv,tsv);
           5    	    sv_free(tsv);
           5    	    goto return_string_or_null;
			}
		    }
		
    11131670        SvPOK_only(sv);
    11131670        if (PerlIO_isutf8(fp))
       10070    	SvUTF8_on(sv);
		
    11131670        if (IN_PERL_COMPILETIME) {
			/* we always read code in line mode */
    10225113    	rsptr = "\n";
    10225113    	rslen = 1;
		    }
      906557        else if (RsSNARF(PL_rs)) {
		    	/* If it is a regular disk file use size from stat() as estimate
			   of amount we are going to read - may result in malloc-ing
			   more memory than we realy need if layers bellow reduce
			   size we read (e.g. CRLF or a gzip layer)
			 */
        7406    	Stat_t st;
        7406    	if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode))  {
        3667    	    const Off_t offset = PerlIO_tell(fp);
        3667    	    if (offset != (Off_t) -1 && st.st_size + append > offset) {
        3574    	     	(void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
			    }
			}
        7406    	rsptr = NULL;
        7406    	rslen = 0;
		    }
      899151        else if (RsRECORD(PL_rs)) {
           4          I32 bytesread;
           4          char *buffer;
		
		      /* Grab the size of the record we're getting */
           4          recsize = SvIV(SvRV(PL_rs));
           4          buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
		      /* Go yank in */
		#ifdef VMS
		      /* VMS wants read instead of fread, because fread doesn't respect */
		      /* RMS record boundaries. This is not necessarily a good thing to be */
		      /* doing, but we've got no other real choice - except avoid stdio
		         as implementation - perhaps write a :vms layer ?
		       */
		      bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
		#else
           4          bytesread = PerlIO_read(fp, buffer, recsize);
		#endif
           4          if (bytesread < 0)
      ######    	  bytesread = 0;
           4          SvCUR_set(sv, bytesread += append);
           4          buffer[bytesread] = '\0';
           4          goto return_string_or_null;
		    }
      899147        else if (RsPARA(PL_rs)) {
        3717    	rsptr = "\n\n";
        3717    	rslen = 2;
        3717    	rspara = 1;
		    }
		    else {
			/* Get $/ i.e. PL_rs into same encoding as stream wants */
      895430    	if (PerlIO_isutf8(fp)) {
       10064    	    rsptr = SvPVutf8(PL_rs, rslen);
			}
			else {
      885366    	    if (SvUTF8(PL_rs)) {
           7    		if (!sv_utf8_downgrade(PL_rs, TRUE)) {
      ######    		    Perl_croak(aTHX_ "Wide character in $/");
				}
			    }
      885366    	    rsptr = SvPV_const(PL_rs, rslen);
			}
		    }
		
    11131666        rslast = rslen ? rsptr[rslen - 1] : '\0';
		
    11131666        if (rspara) {		/* have to do this both before and after */
        3719    	do {			/* to make sure file boundaries work right */
        3719    	    if (PerlIO_eof(fp))
           9    		return 0;
        3710    	    i = PerlIO_getc(fp);
        3710    	    if (i != '\n') {
        3708    		if (i == -1)
      ######    		    return 0;
        3708    		PerlIO_ungetc(fp,i);
        3708    		break;
			    }
           2    	} while (i != EOF);
		    }
		
		    /* See if we know enough about I/O mechanism to cheat it ! */
		
		    /* This used to be #ifdef test - it is made run-time test for ease
		       of abstracting out stdio interface. One call should be cheap
		       enough here - and may even be a macro allowing compile
		       time optimization.
		     */
		
    11131657        if (PerlIO_fast_gets(fp)) {
		
		    /*
		     * We're going to steal some values from the stdio struct
		     * and put EVERYTHING in the innermost loop into registers.
		     */
    11131655        register STDCHAR *ptr;
    11131655        STRLEN bpx;
    11131655        I32 shortbuffered;
		
		#if defined(VMS) && defined(PERLIO_IS_STDIO)
		    /* An ungetc()d char is handled separately from the regular
		     * buffer, so we getc() it back out and stuff it in the buffer.
		     */
		    i = PerlIO_getc(fp);
		    if (i == EOF) return 0;
		    *(--((*fp)->_ptr)) = (unsigned char) i;
		    (*fp)->_cnt++;
		#endif
		
		    /* Here is some breathtakingly efficient cheating */
		
    11131655        cnt = PerlIO_get_cnt(fp);			/* get count into register */
		    /* make sure we have the room */
    11131655        if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
		    	/* Not room for all of it
			   if we are looking for a separator and room for some
			 */
       93736    	if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
			    /* just process what we have room for */
       93321    	    shortbuffered = cnt - SvLEN(sv) + append + 1;
       93321    	    cnt -= shortbuffered;
			}
			else {
         415    	    shortbuffered = 0;
			    /* remember that cnt can be negative */
         415    	    SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
			}
		    }
		    else
    11037919    	shortbuffered = 0;
    11131655        bp = (STDCHAR*)SvPVX_const(sv) + append;  /* move these two too to registers */
    11131655        ptr = (STDCHAR*)PerlIO_get_ptr(fp);
		    DEBUG_P(PerlIO_printf(Perl_debug_log,
    11131655    	"Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
		    DEBUG_P(PerlIO_printf(Perl_debug_log,
			"Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
			       PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
    11131655    	       PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
    22478759        for (;;) {
		      screamer:
    11363140    	if (cnt > 0) {
    11227505    	    if (rslen) {
   303507072    		while (cnt > 0) {		     /* this     |  eat */
   303423488    		    cnt--;
   303423488    		    if ((*bp++ = *ptr++) == rslast)  /* really   |  dust */
    11110593    			goto thats_all_folks;	     /* screams  |  sed :-) */
				}
			    }
			    else {
       33328    	        Copy(ptr, bp, cnt, char);	     /* this     |  eat */
       33328    		bp += cnt;			     /* screams  |  dust */
       33328    		ptr += cnt;			     /* louder   |  sed :-) */
       33328    		cnt = 0;
			    }
			}
			
      252547    	if (shortbuffered) {		/* oh well, must extend */
       10954    	    cnt = shortbuffered;
       10954    	    shortbuffered = 0;
       10954    	    bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
       10954    	    SvCUR_set(sv, bpx);
       10954    	    SvGROW(sv, SvLEN(sv) + append + cnt + 2);
       10954    	    bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
       10954    	    continue;
			}
		
			DEBUG_P(PerlIO_printf(Perl_debug_log,
					      "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
      241593    			      PTR2UV(ptr),(long)cnt));
      241593    	PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
		#if 0
			DEBUG_P(PerlIO_printf(Perl_debug_log,
			    "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
			    PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
			    PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
		#endif
			/* This used to call 'filbuf' in stdio form, but as that behaves like
			   getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
			   another abstraction.  */
      241593    	i   = PerlIO_getc(fp);		/* get more characters */
		#if 0
			DEBUG_P(PerlIO_printf(Perl_debug_log,
			    "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
			    PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
			    PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
		#endif
      241592    	cnt = PerlIO_get_cnt(fp);
      241592    	ptr = (STDCHAR*)PerlIO_get_ptr(fp);	/* reregisterize cnt and ptr */
			DEBUG_P(PerlIO_printf(Perl_debug_log,
      241592    	    "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
		
      241592    	if (i == EOF)			/* all done for ever? */
       34052    	    goto thats_really_all_folks;
		
      207540    	bpx = bp - (STDCHAR*)SvPVX_const(sv);	/* box up before relocation */
      207540    	SvCUR_set(sv, bpx);
      207540    	SvGROW(sv, bpx + cnt + 2);
      207540    	bp = (STDCHAR*)SvPVX_const(sv) + bpx;	/* unbox after relocation */
		
      207540    	*bp++ = (STDCHAR)i;		/* store character from PerlIO_getc */
		
      207540    	if (rslen && (STDCHAR)i == rslast)  /* all done for now? */
    11115619    	    goto thats_all_folks;
		    }
		
		thats_all_folks:
    11115619        if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
			  memNE((char*)bp - rslen, rsptr, rslen))
    11131654    	goto screamer;				/* go back to the fray */
		thats_really_all_folks:
    11131654        if (shortbuffered)
       81845    	cnt += shortbuffered;
			DEBUG_P(PerlIO_printf(Perl_debug_log,
    11131654    	    "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
    11131654        PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt);	/* put these back or we're in trouble */
		    DEBUG_P(PerlIO_printf(Perl_debug_log,
			"Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
			PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
    11131654    	PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
    11131654        *bp = '\0';
    11131654        SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv));	/* set length */
		    DEBUG_P(PerlIO_printf(Perl_debug_log,
			"Screamer: done, len=%ld, string=|%.*s|\n",
    11131654    	(long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
		    }
		   else
		    {
		       /*The big, slow, and stupid way. */
		#ifdef USE_HEAP_INSTEAD_OF_STACK	/* Even slower way. */
			STDCHAR *buf = 0;
			New(0, buf, 8192, STDCHAR);
			assert(buf);
		#else
           2    	STDCHAR buf[8192];
		#endif
		
		screamer2:
           2    	if (rslen) {
           2                const register STDCHAR *bpe = buf + sizeof(buf);
           2    	    bp = buf;
           8    	    while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
				; /* keep reading */
           2    	    cnt = bp - buf;
			}
			else {
      ######    	    cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
			    /* Accomodate broken VAXC compiler, which applies U8 cast to
			     * both args of ?: operator, causing EOF to change into 255
			     */
      ######    	    if (cnt > 0)
      ######    		 i = (U8)buf[cnt - 1];
			    else
      ######    		 i = EOF;
			}
		
           2    	if (cnt < 0)
      ######    	    cnt = 0;  /* we do need to re-set the sv even when cnt <= 0 */
           2    	if (append)
      ######    	     sv_catpvn(sv, (char *) buf, cnt);
			else
           2    	     sv_setpvn(sv, (char *) buf, cnt);
		
           2    	if (i != EOF &&			/* joy */
			    (!rslen ||
			     SvCUR(sv) < rslen ||
			     memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
			{
      ######    	    append = -1;
			    /*
			     * If we're reading from a TTY and we get a short read,
			     * indicating that the user hit his EOF character, we need
			     * to notice it now, because if we try to read from the TTY
			     * again, the EOF condition will disappear.
			     *
			     * The comparison of cnt to sizeof(buf) is an optimization
			     * that prevents unnecessary calls to feof().
			     *
			     * - jik 9/25/96
			     */
      ######    	    if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
      ######    		goto screamer2;
			}
		
		#ifdef USE_HEAP_INSTEAD_OF_STACK
			Safefree(buf);
		#endif
		    }
		
    11131656        if (rspara) {		/* have to do this both before and after */
        3708            while (i != EOF) {	/* to make sure file boundaries work right */
        3700    	    i = PerlIO_getc(fp);
        3700    	    if (i != '\n') {
        3700    		PerlIO_ungetc(fp,i);
				break;
			    }
			}
		    }
		
		return_string_or_null:
    11131665        return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
		}
		
		/*
		=for apidoc sv_inc
		
		Auto-increment of the value in the SV, doing string to numeric conversion
		if necessary. Handles 'get' magic.
		
		=cut
		*/
		
		void
		Perl_sv_inc(pTHX_ register SV *sv)
     2020711    {
     2020711        register char *d;
     2020711        int flags;
		
     2020711        if (!sv)
      ######    	return;
     2020711        if (SvGMAGICAL(sv))
          43    	mg_get(sv);
     2020711        if (SvTHINKFIRST(sv)) {
         115    	if (SvIsCOW(sv))
           4    	    sv_force_normal_flags(sv, 0);
         115    	if (SvREADONLY(sv)) {
           2    	    if (IN_PERL_RUNTIME)
           2    		Perl_croak(aTHX_ PL_no_modify);
			}
         113    	if (SvROK(sv)) {
         109    	    IV i;
         109    	    if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
         106    		return;
      ######    	    i = PTR2IV(SvRV(sv));
      ######    	    sv_unref(sv);
      ######    	    sv_setiv(sv, i);
			}
		    }
     2020600        flags = SvFLAGS(sv);
     2020600        if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
			/* It's (privately or publicly) a float, but not tested as an
			   integer, so test it to see. */
     1317524    	(void) SvIV(sv);
     1317524    	flags = SvFLAGS(sv);
		    }
     2020600        if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
			/* It's publicly an integer, or privately an integer-not-float */
		#ifdef PERL_PRESERVE_IVUV
		      oops_its_int:
		#endif
     1687372    	if (SvIsUV(sv)) {
        1138    	    if (SvUVX(sv) == UV_MAX)
         385    		sv_setnv(sv, UV_MAX_P1);
			    else
         753    		(void)SvIOK_only_UV(sv);
        1138    		SvUV_set(sv, SvUVX(sv) + 1);
			} else {
     1686234    	    if (SvIVX(sv) == IV_MAX)
         294    		sv_setuv(sv, (UV)IV_MAX + 1);
			    else {
     1685940    		(void)SvIOK_only(sv);
     1685940    		SvIV_set(sv, SvIVX(sv) + 1);
			    }	
			}
     1685940    	return;
		    }
      333688        if (flags & SVp_NOK) {
         439    	(void)SvNOK_only(sv);
         439            SvNV_set(sv, SvNVX(sv) + 1.0);
         439    	return;
		    }
		
      333249        if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
      300832    	if ((flags & SVTYPEMASK) < SVt_PVIV)
      300567    	    sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
      300832    	(void)SvIOK_only(sv);
      300832    	SvIV_set(sv, 1);
      300832    	return;
		    }
       32417        d = SvPVX(sv);
      114189        while (isALPHA(*d)) d++;
       39782        while (isDIGIT(*d)) d++;
       32417        if (*d) {
		#ifdef PERL_PRESERVE_IVUV
			/* Got to punt this as an integer if needs be, but we don't issue
			   warnings. Probably ought to make the sv_iv_please() that does
			   the conversion if possible, and silently.  */
         628    	const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
         628    	if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
			    /* Need to try really hard to see if it's an integer.
			       9.22337203685478e+18 is an integer.
			       but "9.22337203685478e+18" + 0 is UV=9223372036854779904
			       so $a="9.22337203685478e+18"; $a+0; $a++
			       needs to be the same as $a="9.22337203685478e+18"; $a++
			       or we go insane. */
			
         628    	    (void) sv_2iv(sv);
         628    	    if (SvIOK(sv))
         460    		goto oops_its_int;
		
			    /* sv_2iv *should* have made this an NV */
         168    	    if (flags & SVp_NOK) {
      ######    		(void)SvNOK_only(sv);
      ######                    SvNV_set(sv, SvNVX(sv) + 1.0);
      ######    		return;
			    }
			    /* I don't think we can get here. Maybe I should assert this
			       And if we do get here I suspect that sv_setnv will croak. NWC
			       Fall through. */
		#if defined(USE_LONG_DOUBLE)
			    DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
						  SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
		#else
			    DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
         168    				  SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
		#endif
			}
		#endif /* PERL_PRESERVE_IVUV */
         168    	sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
         168    	return;
		    }
       31789        d--;
       32952        while (d >= SvPVX_const(sv)) {
       32939    	if (isDIGIT(*d)) {
        1564    	    if (++*d <= '9')
        1416    		return;
         148    	    *(d--) = '0';
			}
			else {
		#ifdef EBCDIC
			    /* MKS: The original code here died if letters weren't consecutive.
			     * at least it didn't have to worry about non-C locales.  The
			     * new code assumes that ('z'-'a')==('Z'-'A'), letters are
			     * arranged in order (although not consecutively) and that only
			     * [A-Za-z] are accepted by isALPHA in the C locale.
			     */
			    if (*d != 'z' && *d != 'Z') {
				do { ++*d; } while (!isALPHA(*d));
				return;
			    }
			    *(d--) -= 'z' - 'a';
		#else
       31375    	    ++*d;
       31375    	    if (isALPHA(*d))
        1015    		return;
        1015    	    *(d--) -= 'z' - 'a' + 1;
		#endif
			}
		    }
		    /* oh,oh, the number grew */
          13        SvGROW(sv, SvCUR(sv) + 2);
          13        SvCUR_set(sv, SvCUR(sv) + 1);
          63        for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
          50    	*d = d[-1];
          13        if (isDIGIT(d[1]))
           6    	*d = '1';
		    else
           7    	*d = d[1];
		}
		
		/*
		=for apidoc sv_dec
		
		Auto-decrement of the value in the SV, doing string to numeric conversion
		if necessary. Handles 'get' magic.
		
		=cut
		*/
		
		void
		Perl_sv_dec(pTHX_ register SV *sv)
       19898    {
       19898        int flags;
		
       19898        if (!sv)
      ######    	return;
       19898        if (SvGMAGICAL(sv))
        1350    	mg_get(sv);
       19898        if (SvTHINKFIRST(sv)) {
         138    	if (SvIsCOW(sv))
           4    	    sv_force_normal_flags(sv, 0);
         138    	if (SvREADONLY(sv)) {
      ######    	    if (IN_PERL_RUNTIME)
      ######    		Perl_croak(aTHX_ PL_no_modify);
			}
         138    	if (SvROK(sv)) {
         134    	    IV i;
         134    	    if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
         132    		return;
      ######    	    i = PTR2IV(SvRV(sv));
      ######    	    sv_unref(sv);
      ######    	    sv_setiv(sv, i);
			}
		    }
		    /* Unlike sv_inc we don't have to worry about string-never-numbers
		       and keeping them magic. But we mustn't warn on punting */
       19764        flags = SvFLAGS(sv);
       19764        if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
			/* It's publicly an integer, or privately an integer-not-float */
		#ifdef PERL_PRESERVE_IVUV
		      oops_its_int:
		#endif
       17511    	if (SvIsUV(sv)) {
        1344    	    if (SvUVX(sv) == 0) {
      ######    		(void)SvIOK_only(sv);
      ######    		SvIV_set(sv, -1);
			    }
			    else {
        1344    		(void)SvIOK_only_UV(sv);
        1344    		SvUV_set(sv, SvUVX(sv) + 1);
			    }	
			} else {
       16167    	    if (SvIVX(sv) == IV_MIN)
         582    		sv_setnv(sv, (NV)IV_MIN - 1.0);
			    else {
       15585    		(void)SvIOK_only(sv);
       15585    		SvIV_set(sv, SvIVX(sv) - 1);
			    }	
			}
       15585    	return;
		    }
        3383        if (flags & SVp_NOK) {
        1984            SvNV_set(sv, SvNVX(sv) - 1.0);
        1984    	(void)SvNOK_only(sv);
        1984    	return;
		    }
        1399        if (!(flags & SVp_POK)) {
          15    	if ((flags & SVTYPEMASK) < SVt_PVIV)
           9    	    sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
          15    	SvIV_set(sv, -1);
          15    	(void)SvIOK_only(sv);
          15    	return;
		    }
		#ifdef PERL_PRESERVE_IVUV
		    {
        1384    	const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
        1384    	if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
			    /* Need to try really hard to see if it's an integer.
			       9.22337203685478e+18 is an integer.
			       but "9.22337203685478e+18" + 0 is UV=9223372036854779904
			       so $a="9.22337203685478e+18"; $a+0; $a--
			       needs to be the same as $a="9.22337203685478e+18"; $a--
			       or we go insane. */
			
        1382    	    (void) sv_2iv(sv);
        1382    	    if (SvIOK(sv))
        1130    		goto oops_its_int;
		
			    /* sv_2iv *should* have made this an NV */
         252    	    if (flags & SVp_NOK) {
      ######    		(void)SvNOK_only(sv);
      ######                    SvNV_set(sv, SvNVX(sv) - 1.0);
      ######    		return;
			    }
			    /* I don't think we can get here. Maybe I should assert this
			       And if we do get here I suspect that sv_setnv will croak. NWC
			       Fall through. */
		#if defined(USE_LONG_DOUBLE)
			    DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
						  SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
		#else
			    DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
         252    				  SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
		#endif
			}
		    }
		#endif /* PERL_PRESERVE_IVUV */
         254        sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0);	/* punt */
		}
		
		/*
		=for apidoc sv_mortalcopy
		
		Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
		The new SV is marked as mortal. It will be destroyed "soon", either by an
		explicit call to FREETMPS, or by an implicit call at places such as
		statement boundaries.  See also C<sv_newmortal> and C<sv_2mortal>.
		
		=cut
		*/
		
		/* Make a string that will exist for the duration of the expression
		 * evaluation.  Actually, it may have to last longer than that, but
		 * hopefully we won't free it until it has been assigned to a
		 * permanent location. */
		
		SV *
		Perl_sv_mortalcopy(pTHX_ SV *oldstr)
    20392928    {
    20392928        register SV *sv;
		
    20392928        new_SV(sv);
    20392928        sv_setsv(sv,oldstr);
    20392926        EXTEND_MORTAL(1);
    20392926        PL_tmps_stack[++PL_tmps_ix] = sv;
    20392926        SvTEMP_on(sv);
    20392926        return sv;
		}
		
		/*
		=for apidoc sv_newmortal
		
		Creates a new null SV which is mortal.  The reference count of the SV is
		set to 1. It will be destroyed "soon", either by an explicit call to
		FREETMPS, or by an implicit call at places such as statement boundaries.
		See also C<sv_mortalcopy> and C<sv_2mortal>.
		
		=cut
		*/
		
		SV *
		Perl_sv_newmortal(pTHX)
     6977216    {
     6977216        register SV *sv;
		
     6977216        new_SV(sv);
     6977216        SvFLAGS(sv) = SVs_TEMP;
     6977216        EXTEND_MORTAL(1);
     6977216        PL_tmps_stack[++PL_tmps_ix] = sv;
     6977216        return sv;
		}
		
		/*
		=for apidoc sv_2mortal
		
		Marks an existing SV as mortal.  The SV will be destroyed "soon", either
		by an explicit call to FREETMPS, or by an implicit call at places such as
		statement boundaries.  SvTEMP() is turned on which means that the SV's
		string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
		and C<sv_mortalcopy>.
		
		=cut
		*/
		
		SV *
		Perl_sv_2mortal(pTHX_ register SV *sv)
    23912873    {
		    dVAR;
    23912873        if (!sv)
          45    	return sv;
    23912828        if (SvREADONLY(sv) && SvIMMORTAL(sv))
        2582    	return sv;
    23910246        EXTEND_MORTAL(1);
    23910246        PL_tmps_stack[++PL_tmps_ix] = sv;
    23910246        SvTEMP_on(sv);
    23910246        return sv;
		}
		
		/*
		=for apidoc newSVpv
		
		Creates a new SV and copies a string into it.  The reference count for the
		SV is set to 1.  If C<len> is zero, Perl will compute the length using
		strlen().  For efficiency, consider using C<newSVpvn> instead.
		
		=cut
		*/
		
		SV *
		Perl_newSVpv(pTHX_ const char *s, STRLEN len)
     2543216    {
     2543216        register SV *sv;
		
     2543216        new_SV(sv);
     2543216        sv_setpvn(sv,s,len ? len : strlen(s));
     2543216        return sv;
		}
		
		/*
		=for apidoc newSVpvn
		
		Creates a new SV and copies a string into it.  The reference count for the
		SV is set to 1.  Note that if C<len> is zero, Perl will create a zero length
		string.  You are responsible for ensuring that the source string is at least
		C<len> bytes long.  If the C<s> argument is NULL the new SV will be undefined.
		
		=cut
		*/
		
		SV *
		Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
    10892839    {
    10892839        register SV *sv;
		
    10892839        new_SV(sv);
    10892839        sv_setpvn(sv,s,len);
    10892839        return sv;
		}
		
		
		/*
		=for apidoc newSVhek
		
		Creates a new SV from the hash key structure.  It will generate scalars that
		point to the shared string table where possible. Returns a new (undefined)
		SV if the hek is NULL.
		
		=cut
		*/
		
		SV *
		Perl_newSVhek(pTHX_ const HEK *hek)
     2511840    {
     2511840        if (!hek) {
      ######    	SV *sv;
		
      ######    	new_SV(sv);
      ######    	return sv;
		    }
		
     2511840        if (HEK_LEN(hek) == HEf_SVKEY) {
      105841    	return newSVsv(*(SV**)HEK_KEY(hek));
		    } else {
     2405999    	const int flags = HEK_FLAGS(hek);
     2405999    	if (flags & HVhek_WASUTF8) {
			    /* Trouble :-)
			       Andreas would like keys he put in as utf8 to come back as utf8
			    */
         451    	    STRLEN utf8_len = HEK_LEN(hek);
         451    	    U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
         451    	    SV *sv = newSVpvn ((char*)as_utf8, utf8_len);
		
         451    	    SvUTF8_on (sv);
         451    	    Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
         451    	    return sv;
     2405548    	} else if (flags & HVhek_REHASH) {
			    /* We don't have a pointer to the hv, so we have to replicate the
			       flag into every HEK. This hv is using custom a hasing
			       algorithm. Hence we can't return a shared string scalar, as
			       that would contain the (wrong) hash value, and might get passed
			       into an hv routine with a regular hash  */
		
      ######    	    SV *sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
      ######    	    if (HEK_UTF8(hek))
      ######    		SvUTF8_on (sv);
      ######    	    return sv;
			}
			/* This will be overwhelminly the most common case.  */
     2405548    	return newSVpvn_share(HEK_KEY(hek),
					      (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
					      HEK_HASH(hek));
		    }
		}
		
		/*
		=for apidoc newSVpvn_share
		
		Creates a new SV with its SvPVX_const pointing to a shared string in the string
		table. If the string does not already exist in the table, it is created
		first.  Turns on READONLY and FAKE.  The string's hash is stored in the UV
		slot of the SV; if the C<hash> parameter is non-zero, that value is used;
		otherwise the hash is computed.  The idea here is that as the string table
		is used for shared hash keys these strings will have SvPVX_const == HeKEY and
		hash lookup will avoid string compare.
		
		=cut
		*/
		
		SV *
		Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
     3099326    {
     3099326        register SV *sv;
     3099326        bool is_utf8 = FALSE;
     3099326        if (len < 0) {
         399    	STRLEN tmplen = -len;
         399            is_utf8 = TRUE;
			/* See the note in hv.c:hv_fetch() --jhi */
         399    	src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
         399    	len = tmplen;
		    }
     3099326        if (!hash)
      694167    	PERL_HASH(hash, src, len);
     3099326        new_SV(sv);
     3099326        sv_upgrade(sv, SVt_PV);
     3099326        SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
     3099326        SvCUR_set(sv, len);
     3099326        SvLEN_set(sv, 0);
     3099326        SvREADONLY_on(sv);
     3099326        SvFAKE_on(sv);
     3099326        SvPOK_on(sv);
     3099326        if (is_utf8)
         392            SvUTF8_on(sv);
     3099326        return sv;
		}
		
		
		#if defined(PERL_IMPLICIT_CONTEXT)
		
		/* pTHX_ magic can't cope with varargs, so this is a no-context
		 * version of the main function, (which may itself be aliased to us).
		 * Don't access this version directly.
		 */
		
		SV *
		Perl_newSVpvf_nocontext(const char* pat, ...)
		{
		    dTHX;
		    register SV *sv;
		    va_list args;
		    va_start(args, pat);
		    sv = vnewSVpvf(pat, &args);
		    va_end(args);
		    return sv;
		}
		#endif
		
		/*
		=for apidoc newSVpvf
		
		Creates a new SV and initializes it with the string formatted like
		C<sprintf>.
		
		=cut
		*/
		
		SV *
		Perl_newSVpvf(pTHX_ const char* pat, ...)
       48420    {
       48420        register SV *sv;
       48420        va_list args;
       48420        va_start(args, pat);
       48420        sv = vnewSVpvf(pat, &args);
       48420        va_end(args);
       48420        return sv;
		}
		
		/* backend for newSVpvf() and newSVpvf_nocontext() */
		
		SV *
		Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
       48421    {
       48421        register SV *sv;
       48421        new_SV(sv);
       48421        sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
       48421        return sv;
		}
		
		/*
		=for apidoc newSVnv
		
		Creates a new SV and copies a floating point value into it.
		The reference count for the SV is set to 1.
		
		=cut
		*/
		
		SV *
		Perl_newSVnv(pTHX_ NV n)
      848746    {
      848746        register SV *sv;
		
      848746        new_SV(sv);
      848746        sv_setnv(sv,n);
      848746        return sv;
		}
		
		/*
		=for apidoc newSViv
		
		Creates a new SV and copies an integer into it.  The reference count for the
		SV is set to 1.
		
		=cut
		*/
		
		SV *
		Perl_newSViv(pTHX_ IV i)
     1943101    {
     1943101        register SV *sv;
		
     1943101        new_SV(sv);
     1943101        sv_setiv(sv,i);
     1943101        return sv;
		}
		
		/*
		=for apidoc newSVuv
		
		Creates a new SV and copies an unsigned integer into it.
		The reference count for the SV is set to 1.
		
		=cut
		*/
		
		SV *
		Perl_newSVuv(pTHX_ UV u)
     1375150    {
     1375150        register SV *sv;
		
     1375150        new_SV(sv);
     1375150        sv_setuv(sv,u);
     1375150        return sv;
		}
		
		/*
		=for apidoc newRV_noinc
		
		Creates an RV wrapper for an SV.  The reference count for the original
		SV is B<not> incremented.
		
		=cut
		*/
		
		SV *
		Perl_newRV_noinc(pTHX_ SV *tmpRef)
      228100    {
      228100        register SV *sv;
		
      228100        new_SV(sv);
      228100        sv_upgrade(sv, SVt_RV);
      228100        SvTEMP_off(tmpRef);
      228100        SvRV_set(sv, tmpRef);
      228100        SvROK_on(sv);
      228100        return sv;
		}
		
		/* newRV_inc is the official function name to use now.
		 * newRV_inc is in fact #defined to newRV in sv.h
		 */
		
		SV *
		Perl_newRV(pTHX_ SV *tmpRef)
      140769    {
      140769        return newRV_noinc(SvREFCNT_inc(tmpRef));
		}
		
		/*
		=for apidoc newSVsv
		
		Creates a new SV which is an exact duplicate of the original SV.
		(Uses C<sv_setsv>).
		
		=cut
		*/
		
		SV *
		Perl_newSVsv(pTHX_ register SV *old)
     4261806    {
     4261806        register SV *sv;
		
     4261806        if (!old)
      ######    	return Nullsv;
     4261806        if (SvTYPE(old) == SVTYPEMASK) {
      ######            if (ckWARN_d(WARN_INTERNAL))
      ######    	    Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
      ######    	return Nullsv;
		    }
     4261806        new_SV(sv);
		    /* SV_GMAGIC is the default for sv_setv()
		       SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
		       with SvTEMP_off and SvTEMP_on round a call to sv_setsv.  */
     4261806        sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
     4261806        return sv;
		}
		
		/*
		=for apidoc sv_reset
		
		Underlying implementation for the C<reset> Perl function.
		Note that the perl-level function is vaguely deprecated.
		
		=cut
		*/
		
		void
		Perl_sv_reset(pTHX_ register const char *s, HV *stash)
          13    {
		    dVAR;
          13        char todo[PERL_UCHAR_MAX+1];
		
          13        if (!stash)
      ######    	return;
		
          13        if (!*s) {		/* reset ?? searches */
          10    	MAGIC *mg = mg_find((SV *)stash, PERL_MAGIC_symtab);
          10    	if (mg) {
           8    	    PMOP *pm = (PMOP *) mg->mg_obj;
        1500    	    while (pm) {
        1492    		pm->op_pmdynflags &= ~PMdf_USED;
        1492    		pm = pm->op_pmnext;
			    }
			}
           3    	return;
		    }
		
		    /* reset variables */
		
           3        if (!HvARRAY(stash))
      ######    	return;
		
           3        Zero(todo, 256, char);
           6        while (*s) {
           3    	I32 max;
           3    	I32 i = (unsigned char)*s;
           3    	if (s[1] == '-') {
      ######    	    s += 2;
			}
           3    	max = (unsigned char)*s++;
           9    	for ( ; i <= max; i++) {
           3    	    todo[i] = 1;
			}
         515    	for (i = 0; i <= (I32) HvMAX(stash); i++) {
         512    	    HE *entry;
         933    	    for (entry = HvARRAY(stash)[i];
				 entry;
				 entry = HeNEXT(entry))
			    {
         421    		register GV *gv;
         421    		register SV *sv;
		
         421    		if (!todo[(U8)*HeKEY(entry)])
         411    		    continue;
          10    		gv = (GV*)HeVAL(entry);
          10    		sv = GvSV(gv);
          10    		if (sv) {
          10    		    if (SvTHINKFIRST(sv)) {
      ######    			if (!SvREADONLY(sv) && SvROK(sv))
      ######    			    sv_unref(sv);
					/* XXX Is this continue a bug? Why should THINKFIRST
					   exempt us from resetting arrays and hashes?  */
      ######    			continue;
				    }
          10    		    SvOK_off(sv);
          10    		    if (SvTYPE(sv) >= SVt_PV) {
      ######    			SvCUR_set(sv, 0);
      ######    			if (SvPVX_const(sv) != Nullch)
      ######    			    *SvPVX(sv) = '\0';
      ######    			SvTAINT(sv);
				    }
				}
          10    		if (GvAV(gv)) {
           7    		    av_clear(GvAV(gv));
				}
          10    		if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
           3    		    hv_clear(GvHV(gv));
		#ifndef PERL_MICRO
		#ifdef USE_ENVIRON_ARRAY
           3    		    if (gv == PL_envgv
		#  ifdef USE_ITHREADS
					&& PL_curinterp == aTHX
		#  endif
				    )
				    {
      ######    			environ[0] = Nullch;
				    }
		#endif
		#endif /* !PERL_MICRO */
				}
			    }
			}
		    }
		}
		
		/*
		=for apidoc sv_2io
		
		Using various gambits, try to get an IO from an SV: the IO slot if its a
		GV; or the recursive result if we're an RV; or the IO slot of the symbol
		named after the PV if we're a string.
		
		=cut
		*/
		
		IO*
		Perl_sv_2io(pTHX_ SV *sv)
         348    {
         348        IO* io;
         348        GV* gv;
		
         348        switch (SvTYPE(sv)) {
		    case SVt_PVIO:
      ######    	io = (IO*)sv;
      ######    	break;
		    case SVt_PVGV:
         313    	gv = (GV*)sv;
         313    	io = GvIO(gv);
         313    	if (!io)
           1    	    Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
          35    	break;
		    default:
          35    	if (!SvOK(sv))
      ######    	    Perl_croak(aTHX_ PL_no_usym, "filehandle");
          35    	if (SvROK(sv))
          23    	    return sv_2io(SvRV(sv));
          12    	gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
          12    	if (gv)
          10    	    io = GvIO(gv);
			else
           2    	    io = 0;
          12    	if (!io)
           2    	    Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
         322    	break;
		    }
         322        return io;
		}
		
		/*
		=for apidoc sv_2cv
		
		Using various gambits, try to get a CV from an SV; in addition, try if
		possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
		
		=cut
		*/
		
		CV *
		Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
      157990    {
		    dVAR;
      157990        GV *gv = Nullgv;
      157990        CV *cv = Nullcv;
		
      157990        if (!sv)
      ######    	return *gvp = Nullgv, Nullcv;
      157990        switch (SvTYPE(sv)) {
		    case SVt_PVCV:
          15    	*st = CvSTASH(sv);
          15    	*gvp = Nullgv;
          15    	return (CV*)sv;
		    case SVt_PVHV:
		    case SVt_PVAV:
      ######    	*gvp = Nullgv;
      ######    	return Nullcv;
		    case SVt_PVGV:
       30138    	gv = (GV*)sv;
       30138    	*gvp = gv;
       30138    	*st = GvESTASH(gv);
       30138    	goto fix_gv;
		
		    default:
      127837    	if (SvGMAGICAL(sv))
        4087    	    mg_get(sv);
      127837    	if (SvROK(sv)) {
        9159    	    SV **sp = &sv;		/* Used in tryAMAGICunDEREF macro. */
        9159    	    tryAMAGICunDEREF(to_cv);
		
        9159    	    sv = SvRV(sv);
        9159    	    if (SvTYPE(sv) == SVt_PVCV) {
        9155    		cv = (CV*)sv;
        9155    		*gvp = Nullgv;
        9155    		*st = CvSTASH(cv);
        9155    		return cv;
			    }
           4    	    else if(isGV(sv))
           4    		gv = (GV*)sv;
			    else
      ######    		Perl_croak(aTHX_ "Not a subroutine reference");
			}
      118678    	else if (isGV(sv))
      ######    	    gv = (GV*)sv;
			else
      118678    	    gv = gv_fetchsv(sv, lref, SVt_PVCV);
      118682    	*gvp = gv;
      118682    	if (!gv)
       46985    	    return Nullcv;
       71697    	*st = GvESTASH(gv);
		    fix_gv:
      101835    	if (lref && !GvCVu(gv)) {
       11139    	    SV *tmpsv;
       11139    	    ENTER;
       11139    	    tmpsv = NEWSV(704,0);
       11139    	    gv_efullname3(tmpsv, gv, Nullch);
			    /* XXX this is probably not what they think they're getting.
			     * It has the same effect as "sub name;", i.e. just a forward
			     * declaration! */
       11139    	    newSUB(start_subparse(FALSE, 0),
				   newSVOP(OP_CONST, 0, tmpsv),
				   Nullop,
				   Nullop);
       11139    	    LEAVE;
       11139    	    if (!GvCVu(gv))
           2    		Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
					   sv);
			}
      101833    	return GvCVu(gv);
		    }
		}
		
		/*
		=for apidoc sv_true
		
		Returns true if the SV has a true value by Perl's rules.
		Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
		instead use an in-line version.
		
		=cut
		*/
		
		I32
		Perl_sv_true(pTHX_ register SV *sv)
      ######    {
      ######        if (!sv)
      ######    	return 0;
      ######        if (SvPOK(sv)) {
      ######    	const register XPV* tXpv;
      ######    	if ((tXpv = (XPV*)SvANY(sv)) &&
				(tXpv->xpv_cur > 1 ||
				(tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
      ######    	    return 1;
			else
      ######    	    return 0;
		    }
		    else {
      ######    	if (SvIOK(sv))
      ######    	    return SvIVX(sv) != 0;
			else {
      ######    	    if (SvNOK(sv))
      ######    		return SvNVX(sv) != 0.0;
			    else
      ######    		return sv_2bool(sv);
			}
		    }
		}
		
		/*
		=for apidoc sv_iv
		
		A private implementation of the C<SvIVx> macro for compilers which can't
		cope with complex macro expressions. Always use the macro instead.
		
		=cut
		*/
		
		IV
		Perl_sv_iv(pTHX_ register SV *sv)
      ######    {
      ######        if (SvIOK(sv)) {
      ######    	if (SvIsUV(sv))
      ######    	    return (IV)SvUVX(sv);
      ######    	return SvIVX(sv);
		    }
      ######        return sv_2iv(sv);
		}
		
		/*
		=for apidoc sv_uv
		
		A private implementation of the C<SvUVx> macro for compilers which can't
		cope with complex macro expressions. Always use the macro instead.
		
		=cut
		*/
		
		UV
		Perl_sv_uv(pTHX_ register SV *sv)
      ######    {
      ######        if (SvIOK(sv)) {
      ######    	if (SvIsUV(sv))
      ######    	    return SvUVX(sv);
      ######    	return (UV)SvIVX(sv);
		    }
      ######        return sv_2uv(sv);
		}
		
		/*
		=for apidoc sv_nv
		
		A private implementation of the C<SvNVx> macro for compilers which can't
		cope with complex macro expressions. Always use the macro instead.
		
		=cut
		*/
		
		NV
		Perl_sv_nv(pTHX_ register SV *sv)
      ######    {
      ######        if (SvNOK(sv))
      ######    	return SvNVX(sv);
      ######        return sv_2nv(sv);
		}
		
		/* sv_pv() is now a macro using SvPV_nolen();
		 * this function provided for binary compatibility only
		 */
		
		char *
		Perl_sv_pv(pTHX_ SV *sv)
      ######    {
      ######        if (SvPOK(sv))
      ######    	return SvPVX(sv);
		
      ######        return sv_2pv(sv, 0);
		}
		
		/*
		=for apidoc sv_pv
		
		Use the C<SvPV_nolen> macro instead
		
		=for apidoc sv_pvn
		
		A private implementation of the C<SvPV> macro for compilers which can't
		cope with complex macro expressions. Always use the macro instead.
		
		=cut
		*/
		
		char *
		Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
      ######    {
      ######        if (SvPOK(sv)) {
      ######    	*lp = SvCUR(sv);
      ######    	return SvPVX(sv);
		    }
      ######        return sv_2pv(sv, lp);
		}
		
		
		char *
		Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
      ######    {
      ######        if (SvPOK(sv)) {
      ######    	*lp = SvCUR(sv);
      ######    	return SvPVX(sv);
		    }
      ######        return sv_2pv_flags(sv, lp, 0);
		}
		
		/* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
		 * this function provided for binary compatibility only
		 */
		
		char *
		Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
      ######    {
      ######        return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
		}
		
		/*
		=for apidoc sv_pvn_force
		
		Get a sensible string out of the SV somehow.
		A private implementation of the C<SvPV_force> macro for compilers which
		can't cope with complex macro expressions. Always use the macro instead.
		
		=for apidoc sv_pvn_force_flags
		
		Get a sensible string out of the SV somehow.
		If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
		appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
		implemented in terms of this function.
		You normally want to use the various wrapper macros instead: see
		C<SvPV_force> and C<SvPV_force_nomg>
		
		=cut
		*/
		
		char *
		Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
      389784    {
		
      389784        if (SvTHINKFIRST(sv) && !SvROK(sv))
      325353            sv_force_normal_flags(sv, 0);
		
      389784        if (SvPOK(sv)) {
      326135    	if (lp)
      326135    	    *lp = SvCUR(sv);
		    }
		    else {
       63649    	char *s;
       63649    	STRLEN len;
		 
       63649    	if (SvREADONLY(sv) && !(flags & SV_MUTABLE_RETURN)) {
      ######    	    if (PL_op)
      ######    		Perl_croak(aTHX_ "Can't coerce readonly %s to string in %s",
					   sv_reftype(sv,0), OP_NAME(PL_op));
			    else
      ######    		Perl_croak(aTHX_ "Can't coerce readonly %s to string",
					   sv_reftype(sv,0));
			}
       63649    	if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
      ######    	    Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
				OP_NAME(PL_op));
			}
			else
       63649    	    s = sv_2pv_flags(sv, &len, flags);
       63649    	if (lp)
       63131    	    *lp = len;
		
       63649    	if (s != SvPVX_const(sv)) {	/* Almost, but not quite, sv_setpvn() */
       43760    	    if (SvROK(sv))
       18257    		sv_unref(sv);
       43760    	    SvUPGRADE(sv, SVt_PV);		/* Never FALSE */
       43760    	    SvGROW(sv, len + 1);
       43760    	    Move(s,SvPVX_const(sv),len,char);
       43760    	    SvCUR_set(sv, len);
       43760    	    *SvEND(sv) = '\0';
			}
       63649    	if (!SvPOK(sv)) {
       59086    	    SvPOK_on(sv);		/* validate pointer */
       59086    	    SvTAINT(sv);
			    DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
       59086    				  PTR2UV(sv),SvPVX_const(sv)));
			}
		    }
      389784        return SvPVX_mutable(sv);
		}
		
		/* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
		 * this function provided for binary compatibility only
		 */
		
		char *
		Perl_sv_pvbyte(pTHX_ SV *sv)
      ######    {
      ######        sv_utf8_downgrade(sv,0);
      ######        return sv_pv(sv);
		}
		
		/*
		=for apidoc sv_pvbyte
		
		Use C<SvPVbyte_nolen> instead.
		
		=for apidoc sv_pvbyten
		
		A private implementation of the C<SvPVbyte> macro for compilers
		which can't cope with complex macro expressions. Always use the macro
		instead.
		
		=cut
		*/
		
		char *
		Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
      ######    {
      ######        sv_utf8_downgrade(sv,0);
      ######        return sv_pvn(sv,lp);
		}
		
		/*
		=for apidoc sv_pvbyten_force
		
		A private implementation of the C<SvPVbytex_force> macro for compilers
		which can't cope with complex macro expressions. Always use the macro
		instead.
		
		=cut
		*/
		
		char *
		Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
      ######    {
      ######        sv_pvn_force(sv,lp);
      ######        sv_utf8_downgrade(sv,0);
      ######        *lp = SvCUR(sv);
      ######        return SvPVX(sv);
		}
		
		/* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
		 * this function provided for binary compatibility only
		 */
		
		char *
		Perl_sv_pvutf8(pTHX_ SV *sv)
      ######    {
      ######        sv_utf8_upgrade(sv);
      ######        return sv_pv(sv);
		}
		
		/*
		=for apidoc sv_pvutf8
		
		Use the C<SvPVutf8_nolen> macro instead
		
		=for apidoc sv_pvutf8n
		
		A private implementation of the C<SvPVutf8> macro for compilers
		which can't cope with complex macro expressions. Always use the macro
		instead.
		
		=cut
		*/
		
		char *
		Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
      ######    {
      ######        sv_utf8_upgrade(sv);
      ######        return sv_pvn(sv,lp);
		}
		
		/*
		=for apidoc sv_pvutf8n_force
		
		A private implementation of the C<SvPVutf8_force> macro for compilers
		which can't cope with complex macro expressions. Always use the macro
		instead.
		
		=cut
		*/
		
		char *
		Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
         791    {
         791        sv_pvn_force(sv,lp);
         791        sv_utf8_upgrade(sv);
         791        *lp = SvCUR(sv);
         791        return SvPVX(sv);
		}
		
		/*
		=for apidoc sv_reftype
		
		Returns a string describing what the SV is a reference to.
		
		=cut
		*/
		
		char *
		Perl_sv_reftype(pTHX_ const SV *sv, int ob)
      717767    {
		    /* The fact that I don't need to downcast to char * everywhere, only in ?:
		       inside return suggests a const propagation bug in g++.  */
      717767        if (ob && SvOBJECT(sv)) {
      623470    	char * const name = HvNAME_get(SvSTASH(sv));
      623470    	return name ? name : (char *) "__ANON__";
		    }
		    else {
       94297    	switch (SvTYPE(sv)) {
			case SVt_NULL:
			case SVt_IV:
			case SVt_NV:
			case SVt_RV:
			case SVt_PV:
			case SVt_PVIV:
			case SVt_PVNV:
			case SVt_PVMG:
			case SVt_PVBM:
       28233    				if (SvVOK(sv))
           2    				    return "VSTRING";
       28231    				if (SvROK(sv))
        1580    				    return "REF";
						else
       26651    				    return "SCALAR";
		
          27    	case SVt_PVLV:		return (char *)  (SvROK(sv) ? "REF"
						/* tied lvalues should appear to be
						 * scalars for backwards compatitbility */
						: (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
						    ? "SCALAR" : "LVALUE");
       24122    	case SVt_PVAV:		return "ARRAY";
       32958    	case SVt_PVHV:		return "HASH";
        6389    	case SVt_PVCV:		return "CODE";
        2565    	case SVt_PVGV:		return "GLOB";
           1    	case SVt_PVFM:		return "FORMAT";
           2    	case SVt_PVIO:		return "IO";
      ######    	default:		return "UNKNOWN";
			}
		    }
		}
		
		/*
		=for apidoc sv_isobject
		
		Returns a boolean indicating whether the SV is an RV pointing to a blessed
		object.  If the SV is not an RV, or if the object is not blessed, then this
		will return false.
		
		=cut
		*/
		
		int
		Perl_sv_isobject(pTHX_ SV *sv)
       12474    {
       12474        if (!sv)
      ######    	return 0;
       12474        if (SvGMAGICAL(sv))
      ######    	mg_get(sv);
       12474        if (!SvROK(sv))
        5420    	return 0;
        7054        sv = (SV*)SvRV(sv);
        7054        if (!SvOBJECT(sv))
        1580    	return 0;
        5474        return 1;
		}
		
		/*
		=for apidoc sv_isa
		
		Returns a boolean indicating whether the SV is blessed into the specified
		class.  This does not check for subtypes; use C<sv_derived_from> to verify
		an inheritance relationship.
		
		=cut
		*/
		
		int
		Perl_sv_isa(pTHX_ SV *sv, const char *name)
          40    {
          40        const char *hvname;
          40        if (!sv)
      ######    	return 0;
          40        if (SvGMAGICAL(sv))
      ######    	mg_get(sv);
          40        if (!SvROK(sv))
          12    	return 0;
          28        sv = (SV*)SvRV(sv);
          28        if (!SvOBJECT(sv))
      ######    	return 0;
          28        hvname = HvNAME_get(SvSTASH(sv));
          28        if (!hvname)
      ######    	return 0;
		
          28        return strEQ(hvname, name);
		}
		
		/*
		=for apidoc newSVrv
		
		Creates a new SV for the RV, C<rv>, to point to.  If C<rv> is not an RV then
		it will be upgraded to one.  If C<classname> is non-null then the new SV will
		be blessed in the specified package.  The new SV is returned and its
		reference count is 1.
		
		=cut
		*/
		
		SV*
		Perl_newSVrv(pTHX_ SV *rv, const char *classname)
     1004464    {
     1004464        SV *sv;
		
     1004464        new_SV(sv);
		
     1004464        SV_CHECK_THINKFIRST_COW_DROP(rv);
     1004464        SvAMAGIC_off(rv);
		
     1004464        if (SvTYPE(rv) >= SVt_PVMG) {
          10    	const U32 refcnt = SvREFCNT(rv);
          10    	SvREFCNT(rv) = 0;
          10    	sv_clear(rv);
          10    	SvFLAGS(rv) = 0;
          10    	SvREFCNT(rv) = refcnt;
		    }
		
     1004464        if (SvTYPE(rv) < SVt_RV)
      984718    	sv_upgrade(rv, SVt_RV);
       19746        else if (SvTYPE(rv) > SVt_RV) {
       19743    	SvPV_free(rv);
       19743    	SvCUR_set(rv, 0);
       19743    	SvLEN_set(rv, 0);
		    }
		
     1004464        SvOK_off(rv);
     1004464        SvRV_set(rv, sv);
     1004464        SvROK_on(rv);
		
     1004464        if (classname) {
     1004463    	HV* const stash = gv_stashpv(classname, TRUE);
     1004463    	(void)sv_bless(rv, stash);
		    }
     1004464        return sv;
		}
		
		/*
		=for apidoc sv_setref_pv
		
		Copies a pointer into a new SV, optionally blessing the SV.  The C<rv>
		argument will be upgraded to an RV.  That RV will be modified to point to
		the new SV.  If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
		into the SV.  The C<classname> argument indicates the package for the
		blessing.  Set C<classname> to C<Nullch> to avoid the blessing.  The new SV
		will have a reference count of 1, and the RV will be returned.
		
		Do not use with other Perl types such as HV, AV, SV, CV, because those
		objects will become corrupted by the pointer copy process.
		
		Note that C<sv_setref_pvn> copies the string while this copies the pointer.
		
		=cut
		*/
		
		SV*
		Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
         516    {
         516        if (!pv) {
           4    	sv_setsv(rv, &PL_sv_undef);
           4    	SvSETMAGIC(rv);
		    }
		    else
         512    	sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
         516        return rv;
		}
		
		/*
		=for apidoc sv_setref_iv
		
		Copies an integer into a new SV, optionally blessing the SV.  The C<rv>
		argument will be upgraded to an RV.  That RV will be modified to point to
		the new SV.  The C<classname> argument indicates the package for the
		blessing.  Set C<classname> to C<Nullch> to avoid the blessing.  The new SV
		will have a reference count of 1, and the RV will be returned.
		
		=cut
		*/
		
		SV*
		Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
           1    {
           1        sv_setiv(newSVrv(rv,classname), iv);
           1        return rv;
		}
		
		/*
		=for apidoc sv_setref_uv
		
		Copies an unsigned integer into a new SV, optionally blessing the SV.  The C<rv>
		argument will be upgraded to an RV.  That RV will be modified to point to
		the new SV.  The C<classname> argument indicates the package for the
		blessing.  Set C<classname> to C<Nullch> to avoid the blessing.  The new SV
		will have a reference count of 1, and the RV will be returned.
		
		=cut
		*/
		
		SV*
		Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
      ######    {
      ######        sv_setuv(newSVrv(rv,classname), uv);
      ######        return rv;
		}
		
		/*
		=for apidoc sv_setref_nv
		
		Copies a double into a new SV, optionally blessing the SV.  The C<rv>
		argument will be upgraded to an RV.  That RV will be modified to point to
		the new SV.  The C<classname> argument indicates the package for the
		blessing.  Set C<classname> to C<Nullch> to avoid the blessing.  The new SV
		will have a reference count of 1, and the RV will be returned.
		
		=cut
		*/
		
		SV*
		Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
      ######    {
      ######        sv_setnv(newSVrv(rv,classname), nv);
      ######        return rv;
		}
		
		/*
		=for apidoc sv_setref_pvn
		
		Copies a string into a new SV, optionally blessing the SV.  The length of the
		string must be specified with C<n>.  The C<rv> argument will be upgraded to
		an RV.  That RV will be modified to point to the new SV.  The C<classname>
		argument indicates the package for the blessing.  Set C<classname> to
		C<Nullch> to avoid the blessing.  The new SV will have a reference count
		of 1, and the RV will be returned.
		
		Note that C<sv_setref_pv> copies the pointer while this copies the string.
		
		=cut
		*/
		
		SV*
		Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, const char *pv, STRLEN n)
      ######    {
      ######        sv_setpvn(newSVrv(rv,classname), pv, n);
      ######        return rv;
		}
		
		/*
		=for apidoc sv_bless
		
		Blesses an SV into a specified package.  The SV must be an RV.  The package
		must be designated by its stash (see C<gv_stashpv()>).  The reference count
		of the SV is unaffected.
		
		=cut
		*/
		
		SV*
		Perl_sv_bless(pTHX_ SV *sv, HV *stash)
     1445511    {
     1445511        SV *tmpRef;
     1445511        if (!SvROK(sv))
           1            Perl_croak(aTHX_ "Can't bless non-reference value");
     1445510        tmpRef = SvRV(sv);
     1445510        if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
        6304    	if (SvREADONLY(tmpRef))
      ######    	    Perl_croak(aTHX_ PL_no_modify);
        6304    	if (SvOBJECT(tmpRef)) {
        6304    	    if (SvTYPE(tmpRef) != SVt_PVIO)
        6179    		--PL_sv_objcount;
        6304    	    SvREFCNT_dec(SvSTASH(tmpRef));
			}
		    }
     1445510        SvOBJECT_on(tmpRef);
     1445510        if (SvTYPE(tmpRef) != SVt_PVIO)
     1445385    	++PL_sv_objcount;
     1445510        SvUPGRADE(tmpRef, SVt_PVMG);
     1445510        SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
		
     1445510        if (Gv_AMG(stash))
      106117    	SvAMAGIC_on(sv);
		    else
     1339393    	SvAMAGIC_off(sv);
		
     1445510        if(SvSMAGICAL(tmpRef))
         478            if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
      ######                mg_set(tmpRef);
		
		
		
     1445510        return sv;
		}
		
		/* Downgrades a PVGV to a PVMG.
		 */
		
		STATIC void
		S_sv_unglob(pTHX_ SV *sv)
        2481    {
        2481        void *xpvmg;
		
        2481        assert(SvTYPE(sv) == SVt_PVGV);
        2481        SvFAKE_off(sv);
        2481        if (GvGP(sv))
        2481    	gp_free((GV*)sv);
        2481        if (GvSTASH(sv)) {
        2481    	sv_del_backref((SV*)GvSTASH(sv), sv);
        2481    	GvSTASH(sv) = Nullhv;
		    }
        2481        sv_unmagic(sv, PERL_MAGIC_glob);
        2481        Safefree(GvNAME(sv));
        2481        GvMULTI_off(sv);
		
		    /* need to keep SvANY(sv) in the right arena */
        2481        xpvmg = new_XPVMG();
        2481        StructCopy(SvANY(sv), xpvmg, XPVMG);
        2481        del_XPVGV(SvANY(sv));
        2481        SvANY(sv) = xpvmg;
		
        2481        SvFLAGS(sv) &= ~SVTYPEMASK;
        2481        SvFLAGS(sv) |= SVt_PVMG;
		}
		
		/*
		=for apidoc sv_unref_flags
		
		Unsets the RV status of the SV, and decrements the reference count of
		whatever was being referenced by the RV.  This can almost be thought of
		as a reversal of C<newSVrv>.  The C<cflags> argument can contain
		C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
		(otherwise the decrementing is conditional on the reference count being
		different from one or the reference being a readonly SV).
		See C<SvROK_off>.
		
		=cut
		*/
		
		void
		Perl_sv_unref_flags(pTHX_ SV *ref, U32 flags)
    12290435    {
    12290435        SV* target = SvRV(ref);
		
    12290435        if (SvWEAKREF(ref)) {
           2        	sv_del_backref(target, ref);
           2    	SvWEAKREF_off(ref);
           2    	SvRV_set(ref, NULL);
           2    	return;
		    }
    12290433        SvRV_set(ref, NULL);
    12290433        SvROK_off(ref);
		    /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
		       assigned to as BEGIN {$a = \"Foo"} will fail.  */
    12290433        if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
    11329219    	SvREFCNT_dec(target);
		    else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
      961214    	sv_2mortal(target);	/* Schedule for freeing later */
		}
		
		/*
		=for apidoc sv_unref
		
		Unsets the RV status of the SV, and decrements the reference count of
		whatever was being referenced by the RV.  This can almost be thought of
		as a reversal of C<newSVrv>.  This is C<sv_unref_flags> with the C<flag>
		being zero.  See C<SvROK_off>.
		
		=cut
		*/
		
		void
		Perl_sv_unref(pTHX_ SV *sv)
       18257    {
       18257        sv_unref_flags(sv, 0);
		}
		
		/*
		=for apidoc sv_taint
		
		Taint an SV. Use C<SvTAINTED_on> instead.
		=cut
		*/
		
		void
		Perl_sv_taint(pTHX_ SV *sv)
     6216094    {
     6216094        sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
		}
		
		/*
		=for apidoc sv_untaint
		
		Untaint an SV. Use C<SvTAINTED_off> instead.
		=cut
		*/
		
		void
		Perl_sv_untaint(pTHX_ SV *sv)
       14901    {
       14901        if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
       14444    	MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
       14444    	if (mg)
        8082    	    mg->mg_len &= ~1;
		    }
		}
		
		/*
		=for apidoc sv_tainted
		
		Test an SV for taintedness. Use C<SvTAINTED> instead.
		=cut
		*/
		
		bool
		Perl_sv_tainted(pTHX_ SV *sv)
     4353027    {
     4353027        if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
     4353021    	MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
     4353021    	if (mg && (mg->mg_len & 1) )
     2633282    	    return TRUE;
		    }
     1719745        return FALSE;
		}
		
		/*
		=for apidoc sv_setpviv
		
		Copies an integer into the given SV, also updating its string value.
		Does not handle 'set' magic.  See C<sv_setpviv_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
      ######    {
      ######        char buf[TYPE_CHARS(UV)];
      ######        char *ebuf;
      ######        char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
		
      ######        sv_setpvn(sv, ptr, ebuf - ptr);
		}
		
		/*
		=for apidoc sv_setpviv_mg
		
		Like C<sv_setpviv>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
      ######    {
      ######        char buf[TYPE_CHARS(UV)];
      ######        char *ebuf;
      ######        char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
		
      ######        sv_setpvn(sv, ptr, ebuf - ptr);
      ######        SvSETMAGIC(sv);
		}
		
		#if defined(PERL_IMPLICIT_CONTEXT)
		
		/* pTHX_ magic can't cope with varargs, so this is a no-context
		 * version of the main function, (which may itself be aliased to us).
		 * Don't access this version directly.
		 */
		
		void
		Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
		{
		    dTHX;
		    va_list args;
		    va_start(args, pat);
		    sv_vsetpvf(sv, pat, &args);
		    va_end(args);
		}
		
		/* pTHX_ magic can't cope with varargs, so this is a no-context
		 * version of the main function, (which may itself be aliased to us).
		 * Don't access this version directly.
		 */
		
		void
		Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
		{
		    dTHX;
		    va_list args;
		    va_start(args, pat);
		    sv_vsetpvf_mg(sv, pat, &args);
		    va_end(args);
		}
		#endif
		
		/*
		=for apidoc sv_setpvf
		
		Works like C<sv_catpvf> but copies the text into the SV instead of
		appending it.  Does not handle 'set' magic.  See C<sv_setpvf_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
      102175    {
      102175        va_list args;
      102175        va_start(args, pat);
      102175        sv_vsetpvf(sv, pat, &args);
		    va_end(args);
		}
		
		/*
		=for apidoc sv_vsetpvf
		
		Works like C<sv_vcatpvf> but copies the text into the SV instead of
		appending it.  Does not handle 'set' magic.  See C<sv_vsetpvf_mg>.
		
		Usually used via its frontend C<sv_setpvf>.
		
		=cut
		*/
		
		void
		Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
      102176    {
      102176        sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
		}
		
		/*
		=for apidoc sv_setpvf_mg
		
		Like C<sv_setpvf>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
           3    {
           3        va_list args;
           3        va_start(args, pat);
           3        sv_vsetpvf_mg(sv, pat, &args);
		    va_end(args);
		}
		
		/*
		=for apidoc sv_vsetpvf_mg
		
		Like C<sv_vsetpvf>, but also handles 'set' magic.
		
		Usually used via its frontend C<sv_setpvf_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
           3    {
           3        sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
           3        SvSETMAGIC(sv);
		}
		
		#if defined(PERL_IMPLICIT_CONTEXT)
		
		/* pTHX_ magic can't cope with varargs, so this is a no-context
		 * version of the main function, (which may itself be aliased to us).
		 * Don't access this version directly.
		 */
		
		void
		Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
		{
		    dTHX;
		    va_list args;
		    va_start(args, pat);
		    sv_vcatpvf(sv, pat, &args);
		    va_end(args);
		}
		
		/* pTHX_ magic can't cope with varargs, so this is a no-context
		 * version of the main function, (which may itself be aliased to us).
		 * Don't access this version directly.
		 */
		
		void
		Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
		{
		    dTHX;
		    va_list args;
		    va_start(args, pat);
		    sv_vcatpvf_mg(sv, pat, &args);
		    va_end(args);
		}
		#endif
		
		/*
		=for apidoc sv_catpvf
		
		Processes its arguments like C<sprintf> and appends the formatted
		output to an SV.  If the appended data contains "wide" characters
		(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
		and characters >255 formatted with %c), the original SV might get
		upgraded to UTF-8.  Handles 'get' magic, but not 'set' magic.  See
		C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
		valid UTF-8; if the original SV was bytes, the pattern should be too.
		
		=cut */
		
		void
		Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
       32540    {
       32540        va_list args;
       32540        va_start(args, pat);
       32540        sv_vcatpvf(sv, pat, &args);
		    va_end(args);
		}
		
		/*
		=for apidoc sv_vcatpvf
		
		Processes its arguments like C<vsprintf> and appends the formatted output
		to an SV.  Does not handle 'set' magic.  See C<sv_vcatpvf_mg>.
		
		Usually used via its frontend C<sv_catpvf>.
		
		=cut
		*/
		
		void
		Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
       54206    {
       54206        sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
		}
		
		/*
		=for apidoc sv_catpvf_mg
		
		Like C<sv_catpvf>, but also handles 'set' magic.
		
		=cut
		*/
		
		void
		Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
           3    {
           3        va_list args;
           3        va_start(args, pat);
           3        sv_vcatpvf_mg(sv, pat, &args);
		    va_end(args);
		}
		
		/*
		=for apidoc sv_vcatpvf_mg
		
		Like C<sv_vcatpvf>, but also handles 'set' magic.
		
		Usually used via its frontend C<sv_catpvf_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
           3    {
           3        sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
           3        SvSETMAGIC(sv);
		}
		
		/*
		=for apidoc sv_vsetpvfn
		
		Works like C<sv_vcatpvfn> but copies the text into the SV instead of
		appending it.
		
		Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
		
		=cut
		*/
		
		void
		Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
      750103    {
      750103        sv_setpvn(sv, "", 0);
      750103        sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
		}
		
		/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
		
		STATIC I32
		S_expect_number(pTHX_ char** pattern)
     2337654    {
     2337654        I32 var = 0;
     2337654        switch (**pattern) {
		    case '1': case '2': case '3':
		    case '4': case '5': case '6':
		    case '7': case '8': case '9':
      995892    	while (isDIGIT(**pattern))
      503407    	    var = var * 10 + (*(*pattern)++ - '0');
		    }
     2337654        return var;
		}
		#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
		
		static char *
		F0convert(NV nv, char *endbuf, STRLEN *len)
          38    {
          38        const int neg = nv < 0;
          38        UV uv;
		
          38        if (neg)
           7    	nv = -nv;
          38        if (nv < UV_MAX) {
          21    	char *p = endbuf;
          21    	nv += 0.5;
          21    	uv = (UV)nv;
          21    	if (uv & 1 && uv == nv)
      ######    	    uv--;			/* Round to even */
          39    	do {
          39    	    const unsigned dig = uv % 10;
          39    	    *--p = '0' + dig;
          39    	} while (uv /= 10);
          21    	if (neg)
           7    	    *--p = '-';
          21    	*len = endbuf - p;
          21    	return p;
		    }
          17        return Nullch;
		}
		
		
		/*
		=for apidoc sv_vcatpvfn
		
		Processes its arguments like C<vsprintf> and appends the formatted output
		to an SV.  Uses an array of SVs if the C style variable argument list is
		missing (NULL).  When running with taint checks enabled, indicates via
		C<maybe_tainted> if results are untrustworthy (often due to the use of
		locales).
		
		Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
		
		=cut
		*/
		
		/* XXX maybe_tainted is never assigned to, so the doc above is lying. */
		
		void
		Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
      804312    {
      804312        char *p;
      804312        char *q;
      804312        const char *patend;
      804312        STRLEN origlen;
      804312        I32 svix = 0;
      804312        static const char nullstr[] = "(null)";
      804312        SV *argsv = Nullsv;
      804312        bool has_utf8 = DO_UTF8(sv);    /* has the result utf8? */
      804312        const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
      804312        SV *nsv = Nullsv;
		    /* Times 4: a decimal digit takes more than 3 binary digits.
		     * NV_DIG: mantissa takes than many decimal digits.
		     * Plus 32: Playing safe. */
      804312        char ebuf[IV_DIG * 4 + NV_DIG + 32];
		    /* large enough for "%#.#f" --chip */
		    /* what about long double NVs? --jhi */
		
      804312        PERL_UNUSED_ARG(maybe_tainted);
		
		    /* no matter what, this is a string now */
      804312        (void)SvPV_force(sv, origlen);
		
		    /* special-case "", "%s", and "%-p" (SVf) */
      804312        if (patlen == 0)
        1015    	return;
      803297        if (patlen == 2 && pat[0] == '%' && pat[1] == 's') {
        1937    	    if (args) {
        1787    		const char * const s = va_arg(*args, char*);
        1787    		sv_catpv(sv, s ? s : nullstr);
			    }
         150    	    else if (svix < svmax) {
         150    		sv_catsv(sv, *svargs);
         150    		if (DO_UTF8(*svargs))
           2    		    SvUTF8_on(sv);
			    }
           2    	    return;
		    }
      801360        if (patlen == 3 && pat[0] == '%' &&
			pat[1] == '-' && pat[2] == 'p') {
        1618    	    if (args) {
        1618    		argsv = va_arg(*args, SV*);
        1618    		sv_catsv(sv, argsv);
        1618    		if (DO_UTF8(argsv))
           5    		    SvUTF8_on(sv);
           5    		return;
			    }
		    }
		
		#ifndef USE_LONG_DOUBLE
		    /* special-case "%.<number>[gf]" */
      799742        if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.'
			 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
         147    	unsigned digits = 0;
         147    	const char *pp;
		
         147    	pp = pat + 2;
         338    	while (*pp >= '0' && *pp <= '9')
         191    	    digits = 10 * digits + (*pp++ - '0');
         147    	if (pp - pat == (int)patlen - 1) {
         147    	    NV nv;
		
         147    	    if (svix < svmax)
         147    		nv = SvNV(*svargs);
			    else
         147    		return;
         147    	    if (*pp == 'g') {
				/* Add check for digits != 0 because it seems that some
				   gconverts are buggy in this case, and we don't yet have
				   a Configure test for this.  */
          43    		if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
				     /* 0, point, slack */
          43    		    Gconvert(nv, (int)digits, 0, ebuf);
          43    		    sv_catpv(sv, ebuf);
          43    		    if (*ebuf)	/* May return an empty string for digits==0 */
          43    			return;
				}
         104    	    } else if (!digits) {
      ######    		STRLEN l;
		
      ######    		if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
      ######    		    sv_catpvn(sv, p, l);
      ######    		    return;
				}
			    }
			}
		    }
		#endif /* !USE_LONG_DOUBLE */
		
      799699        if (!args && svix < svmax && DO_UTF8(*svargs))
          34    	has_utf8 = TRUE;
		
      799699        patend = (char*)pat + patlen;
     1966180        for (p = (char*)pat; p < patend; p = q) {
     1519801    	bool alt = FALSE;
     1519801    	bool left = FALSE;
     1519801    	bool vectorize = FALSE;
     1519801    	bool vectorarg = FALSE;
     1519801    	bool vec_utf8 = FALSE;
     1519801    	char fill = ' ';
     1519801    	char plus = 0;
     1519801    	char intsize = 0;
     1519801    	STRLEN width = 0;
     1519801    	STRLEN zeros = 0;
     1519801    	bool has_precis = FALSE;
     1519801    	STRLEN precis = 0;
     1519801    	I32 osvix = svix;
     1519801    	bool is_utf8 = FALSE;  /* is this item utf8?   */
		#ifdef HAS_LDBL_SPRINTF_BUG
			/* This is to try to fix a bug with irix/nonstop-ux/powerux and
			   with sfio - Allen <allens@cpan.org> */
			bool fix_ldbl_sprintf_bug = FALSE;
		#endif
		
     1519801    	char esignbuf[4];
     1519801    	U8 utf8buf[UTF8_MAXBYTES+1];
     1519801    	STRLEN esignlen = 0;
		
     1519801    	const char *eptr = Nullch;
     1519801    	STRLEN elen = 0;
     1519801    	SV *vecsv = Nullsv;
     1519801    	const U8 *vecstr = Null(U8*);
     1519801    	STRLEN veclen = 0;
     1519801    	char c = 0;
     1519801    	int i;
     1519801    	unsigned base = 0;
     1519801    	IV iv = 0;
     1519801    	UV uv = 0;
			/* we need a long double target in case HAS_LONG_DOUBLE but
			   not USE_LONG_DOUBLE
			*/
		#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
     1519801    	long double nv;
		#else
			NV nv;
		#endif
     1519801    	STRLEN have;
     1519801    	STRLEN need;
     1519801    	STRLEN gap;
     1519801    	const char *dotstr = ".";
     1519801    	STRLEN dotstrlen = 1;
     1519801    	I32 efix = 0; /* explicit format parameter index */
     1519801    	I32 ewix = 0; /* explicit width index */
     1519801    	I32 epix = 0; /* explicit precision index */
     1519801    	I32 evix = 0; /* explicit vector index */
     1519801    	bool asterisk = FALSE;
		
			/* echo everything up to the next format specification */
     1519801    	for (q = p; q < patend && *q != '%'; ++q) ;
     1519801    	if (q > p) {
      920266    	    if (has_utf8 && !pat_utf8)
           6    		sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
			    else
      920260    		sv_catpvn(sv, p, q - p);
      920266    	    p = q;
			}
     1519801    	if (q++ >= patend)
      353320    	    break;
		
		/*
		    We allow format specification elements in this order:
			\d+\$              explicit format parameter index
			[-+ 0#]+           flags
			v|\*(\d+\$)?v      vector with optional (optionally specified) arg
			0		   flag (as above): repeated to allow "v02" 	
			\d+|\*(\d+\$)?     width using optional (optionally specified) arg
			\.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
			[hlqLV]            size
		    [%bcdefginopsux_DFOUX] format (mandatory)
		*/
     1166481    	if (EXPECT_NUMBER(q, width)) {
        5348    	    if (*q == '$') {
          10    		++q;
          10    		efix = width;
			    } else {
     1161143    		goto gotwidth;
			    }
			}
		
			/* FLAGS */
		
     1789068    	while (*q) {
     1789064    	    switch (*q) {
			    case ' ':
			    case '+':
        1260    		plus = *q++;
        1260    		continue;
		
			    case '-':
       50011    		left = TRUE;
       50011    		q++;
       50011    		continue;
		
			    case '0':
      458600    		fill = *q++;
      458600    		continue;
		
			    case '#':
      118054    		alt = TRUE;
      118054    		q++;
      118054    		continue;
		
			    default:
     1161239    		break;
			    }
     1161239    	    break;
			}
		
		      tryasterisk:
     1161239    	if (*q == '*') {
        4122    	    q++;
        4122    	    if (EXPECT_NUMBER(q, ewix))
           6    		if (*q++ != '$')
           2    		    goto unknown;
        4120    	    asterisk = TRUE;
			}
     1161237    	if (*q == 'v') {
          96    	    q++;
          96    	    if (vectorize)
      ######    		goto unknown;
          96    	    if ((vectorarg = asterisk)) {
          10    		evix = ewix;
          10    		ewix = 0;
          10    		asterisk = FALSE;
			    }
          96    	    vectorize = TRUE;
          96    	    goto tryasterisk;
			}
		
     1161141    	if (!asterisk)
     1157031    	    if( *q == '0' )
           1    		fill = *q++;
     1161141    	    EXPECT_NUMBER(q, width);
		
     1161141    	if (vectorize) {
          95    	    if (vectorarg) {
           9    		if (args)
      ######    		    vecsv = va_arg(*args, SV*);
				else
           9    		    vecsv = (evix ? evix <= svmax : svix < svmax) ?
					svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
           9    		dotstr = SvPV_const(vecsv, dotstrlen);
           9    		if (DO_UTF8(vecsv))
      ######    		    is_utf8 = TRUE;
			    }
          95    	    if (args) {
      ######    		vecsv = va_arg(*args, SV*);
      ######    		vecstr = (U8*)SvPV_const(vecsv,veclen);
      ######    		vec_utf8 = DO_UTF8(vecsv);
			    }
          95    	    else if (efix ? efix <= svmax : svix < svmax) {
          95    		vecsv = svargs[efix ? efix-1 : svix++];
          95    		vecstr = (U8*)SvPV_const(vecsv,veclen);
          95    		vec_utf8 = DO_UTF8(vecsv);
				/* if this is a version object, we need to return the
				 * stringified representation (which the SvPVX_const has
				 * already done for us), but not vectorize the args
				 */
          95    		if ( *q == 'd' && sv_derived_from(vecsv,"version") )
				{
          17    			q++; /* skip past the rest of the %vd format */
          17    			eptr = (const char *) vecstr;
          17    			elen = strlen(eptr);
          17    			vectorize=FALSE;
          17    			goto string;
				}
			    }
			    else {
      ######    		vecstr = (U8*)"";
      ######    		veclen = 0;
			    }
			}
		
     1161124    	if (asterisk) {
        4110    	    if (args)
        4094    		i = va_arg(*args, int);
			    else
          16    		i = (ewix ? ewix <= svmax : svix < svmax) ?
          16    		    SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
        4110    	    left |= (i < 0);
        4110    	    width = (i < 0) ? -i : i;
			}
		      gotwidth:
		
			/* PRECISION */
		
     1166462    	if (*q == '.') {
        6971    	    q++;
        6971    	    if (*q == '*') {
        5910    		q++;
        5910    		if (EXPECT_NUMBER(q, epix) && *q++ != '$')
      ######    		    goto unknown;
				/* XXX: todo, support specified precision parameter */
        5910    		if (epix)
      ######    		    goto unknown;
        5910    		if (args)
        5901    		    i = va_arg(*args, int);
				else
           9    		    i = (ewix ? ewix <= svmax : svix < svmax)
           9    			? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
        5910    		precis = (i < 0) ? 0 : i;
			    }
			    else {
        1061    		precis = 0;
        2124    		while (isDIGIT(*q))
        1063    		    precis = precis * 10 + (*q++ - '0');
			    }
        6971    	    has_precis = TRUE;
			}
		
			/* SIZE */
		
     1166462    	switch (*q) {
		#ifdef WIN32
			case 'I':			/* Ix, I32x, and I64x */
		#  ifdef WIN64
			    if (q[1] == '6' && q[2] == '4') {
				q += 3;
				intsize = 'q';
				break;
			    }
		#  endif
			    if (q[1] == '3' && q[2] == '2') {
				q += 3;
				break;
			    }
		#  ifdef WIN64
			    intsize = 'q';
		#  endif
			    q++;
			    break;
		#endif
		#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
			case 'L':			/* Ld */
			    /* FALL THROUGH */
		#ifdef HAS_QUAD
			case 'q':			/* qd */
		#endif
           1    	    intsize = 'q';
           1    	    q++;
           1    	    break;
		#endif
			case 'l':
		#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
       83490    	    if (*(q + 1) == 'l') {	/* lld, llf */
      ######    		intsize = 'q';
      ######    		q += 2;
      ######    		break;
			     }
		#endif
			    /* FALL THROUGH */
			case 'h':
			    /* FALL THROUGH */
			case 'V':
       83497    	    intsize = *q++;
			    break;
			}
		
			/* CONVERSION */
		
     1166462    	if (*q == '%') {
         163    	    eptr = q++;
         163    	    elen = 1;
         163    	    goto string;
			}
		
     1166299    	if (vectorize)
          78    	    argsv = vecsv;
     1166221    	else if (!args)
      733049    	    argsv = (efix ? efix <= svmax : svix < svmax) ?
				    svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
		
     1166299    	switch (c = *q++) {
		
			    /* STRINGS */
		
			case 'c':
       43224    	    uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
       43224    	    if ((uv > 255 ||
				 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
				&& !IN_BYTES) {
           9    		eptr = (char*)utf8buf;
           9    		elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
           9    		is_utf8 = TRUE;
			    }
			    else {
       43215    		c = (char)uv;
       43215    		eptr = &c;
       43215    		elen = 1;
			    }
       43215    	    goto string;
		
			case 's':
      341807    	    if (args && !vectorize) {
      233148    		eptr = va_arg(*args, char*);
      233148    		if (eptr)
		#ifdef MACOS_TRADITIONAL
				  /* On MacOS, %#s format is used for Pascal strings */
				  if (alt)
				    elen = *eptr++;
				  else
		#endif
      233148    		    elen = strlen(eptr);
				else {
      ######    		    eptr = (char *)nullstr;
      ######    		    elen = sizeof nullstr - 1;
				}
			    }
			    else {
      108659    		eptr = SvPVx_const(argsv, elen);
      108659    		if (DO_UTF8(argsv)) {
          10    		    if (has_precis && precis < elen) {
      ######    			I32 p = precis;
      ######    			sv_pos_u2b(argsv, &p, 0); /* sticks at end */
      ######    			precis = p;
				    }
          10    		    if (width) { /* fudge width (can't fudge elen) */
      ######    			width += elen - sv_len_utf8(argsv);
				    }
          10    		    is_utf8 = TRUE;
				}
			    }
		
			string:
      402917    	    vectorize = FALSE;
      402917    	    if (has_precis && elen > precis)
        5722    		elen = precis;
        5722    	    break;
		
			    /* INTEGERS */
		
			case 'p':
       17710    	    if (left && args) {		/* SVf */
       17706    		left = FALSE;
       17706    		if (width) {
          24    		    precis = width;
          24    		    has_precis = TRUE;
          24    		    width = 0;
				}
       17706    		if (vectorize)
      ######    		    goto unknown;
       17706    		argsv = va_arg(*args, SV*);
       17706    		eptr = SvPVx_const(argsv, elen);
       17706    		if (DO_UTF8(argsv))
           1    		    is_utf8 = TRUE;
           1    		goto string;
			    }
           4    	    if (alt || vectorize)
           2    		goto unknown;
           2    	    uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
           2    	    base = 16;
           2    	    goto integer;
		
			case 'D':
		#ifdef IV_IS_QUAD
			    intsize = 'q';
		#else
           1    	    intsize = 'l';
		#endif
			    /* FALL THROUGH */
			case 'd':
			case 'i':
      207843    	    if (vectorize) {
          52    		STRLEN ulen;
          52    		if (!veclen)
           1    		    continue;
          51    		if (vec_utf8)
          23    		    uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
							UTF8_ALLOW_ANYUV);
				else {
          28    		    uv = *vecstr;
          28    		    ulen = 1;
				}
          51    		vecstr += ulen;
          51    		veclen -= ulen;
          51    		if (plus)
           3    		     esignbuf[esignlen++] = plus;
			    }
      207791    	    else if (args) {
       91720    		switch (intsize) {
      ######    		case 'h':	iv = (short)va_arg(*args, int); break;
       35385    		case 'l':	iv = va_arg(*args, long); break;
      ######    		case 'V':	iv = va_arg(*args, IV); break;
       56335    		default:	iv = va_arg(*args, int); break;
		#ifdef HAS_QUAD
				case 'q':	iv = va_arg(*args, Quad_t); break;
		#endif
				}
			    }
			    else {
      116071    		IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
      116071    		switch (intsize) {
           1    		case 'h':	iv = (short)tiv; break;
        1224    		case 'l':	iv = (long)tiv; break;
				case 'V':
      114846    		default:	iv = tiv; break;
		#ifdef HAS_QUAD
				case 'q':	iv = (Quad_t)tiv; break;
		#endif
				}
			    }
      207842    	    if ( !vectorize )	/* we already set uv above */
			    {
      207791    		if (iv >= 0) {
      200032    		    uv = iv;
      200032    		    if (plus)
         698    			esignbuf[esignlen++] = plus;
				}
				else {
        7759    		    uv = -iv;
        7759    		    esignbuf[esignlen++] = '-';
				}
			    }
      207842    	    base = 10;
      207842    	    goto integer;
		
			case 'U':
		#ifdef IV_IS_QUAD
			    intsize = 'q';
		#else
           1    	    intsize = 'l';
		#endif
			    /* FALL THROUGH */
			case 'u':
       11122    	    base = 10;
       11122    	    goto uns_integer;
		
			case 'b':
        1044    	    base = 2;
        1044    	    goto uns_integer;
		
			case 'O':
		#ifdef IV_IS_QUAD
			    intsize = 'q';
		#else
           1    	    intsize = 'l';
		#endif
			    /* FALL THROUGH */
			case 'o':
       15469    	    base = 8;
       15469    	    goto uns_integer;
		
			case 'X':
			case 'x':
      515611    	    base = 16;
		
			uns_integer:
      543246    	    if (vectorize) {
         172    		STRLEN ulen;
			vector:
         172    		if (!veclen)
      ######    		    continue;
         172    		if (vec_utf8)
          44    		    uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
							UTF8_ALLOW_ANYUV);
				else {
         128    		    uv = *vecstr;
         128    		    ulen = 1;
				}
         172    		vecstr += ulen;
         172    		veclen -= ulen;
			    }
      543227    	    else if (args) {
       47382    		switch (intsize) {
      ######    		case 'h':  uv = (unsigned short)va_arg(*args, unsigned); break;
       45541    		case 'l':  uv = va_arg(*args, unsigned long); break;
      ######    		case 'V':  uv = va_arg(*args, UV); break;
        1841    		default:   uv = va_arg(*args, unsigned); break;
		#ifdef HAS_QUAD
				case 'q':  uv = va_arg(*args, Uquad_t); break;
		#endif
				}
			    }
			    else {
      495845    		UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
      495845    		switch (intsize) {
      ######    		case 'h':	uv = (unsigned short)tuv; break;
        1340    		case 'l':	uv = (unsigned long)tuv; break;
				case 'V':
      494505    		default:	uv = tuv; break;
		#ifdef HAS_QUAD
				case 'q':	uv = (Uquad_t)tuv; break;
		#endif
				}
			    }
		
			integer:
			    {
      751243    		char *ptr = ebuf + sizeof ebuf;
      751243    		switch (base) {
      515645    		    unsigned dig;
				case 16:
      515645    		    if (!uv)
       16514    			alt = FALSE;
      515645    		    p = (char*)((c == 'X')
						? "0123456789ABCDEF" : "0123456789abcdef");
     1841107    		    do {
     1841107    			dig = uv & 15;
     1841107    			*--ptr = p[dig];
     1841107    		    } while (uv >>= 4);
      515645    		    if (alt) {
      102373    			esignbuf[esignlen++] = '0';
      102373    			esignbuf[esignlen++] = c;  /* 'x' or 'X' */
				    }
      102373    		    break;
				case 8:
       42614    		    do {
       42614    			dig = uv & 7;
       42614    			*--ptr = '0' + dig;
       42614    		    } while (uv >>= 3);
       15481    		    if (alt && *ptr != '0')
          18    			*--ptr = '0';
          18    		    break;
				case 2:
        3953    		    do {
        3953    			dig = uv & 1;
        3953    			*--ptr = '0' + dig;
        3953    		    } while (uv >>= 1);
        1059    		    if (alt) {
           1    			esignbuf[esignlen++] = '0';
           1    			esignbuf[esignlen++] = 'b';
				    }
           1    		    break;
				default:		/* it had better be ten or less */
      566732    		    do {
      566732    			dig = uv % base;
      566732    			*--ptr = '0' + dig;
      566732    		    } while (uv /= base);
      751243    		    break;
				}
      751243    		elen = (ebuf + sizeof ebuf) - ptr;
      751243    		eptr = ptr;
      751243    		if (has_precis) {
         737    		    if (precis > elen)
         112    			zeros = precis - elen;
         625    		    else if (precis == 0 && elen == 1 && *eptr == '0')
           2    			elen = 0;
				}
			    }
           2    	    break;
		
			    /* FLOATING POINT */
		
			case 'F':
           1    	    c = 'f';		/* maybe %F isn't supported here */
			    /* FALL THROUGH */
			case 'e': case 'E':
			case 'f':
			case 'g': case 'G':
		
			    /* This is evil, but floating point is even more evil */
		
			    /* for SV-style calling, we can only get NV
			       for C-style calling, we assume %f is double;
			       for simplicity we allow any of %Lf, %llf, %qf for long double
			    */
       12409    	    switch (intsize) {
			    case 'V':
		#if defined(USE_LONG_DOUBLE)
				intsize = 'q';
		#endif
       12408    		break;
		/* [perl #20339] - we should accept and ignore %lf rather than die */
			    case 'l':
				/* FALL THROUGH */
			    default:
		#if defined(USE_LONG_DOUBLE)
				intsize = args ? 0 : 'q';
		#endif
       12408    		break;
			    case 'q':
		#if defined(HAS_LONG_DOUBLE)
       12408    		break;
		#else
				/* FALL THROUGH */
		#endif
			    case 'h':
       12408    		goto unknown;
			    }
		
			    /* now we need (long double) if intsize == 'q', else (double) */
       12408    	    nv = (args && !vectorize) ?
		#if LONG_DOUBLESIZE > DOUBLESIZE
				intsize == 'q' ?
				    va_arg(*args, long double) :
				    va_arg(*args, double)
		#else
				    va_arg(*args, double)
		#endif
       12391    		: SvNVx(argsv);
		
       12408    	    need = 0;
       12408    	    vectorize = FALSE;
       12408    	    if (c != 'e' && c != 'E') {
       12235    		i = PERL_INT_MIN;
				/* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
				   will cast our (long double) to (double) */
       12235    		(void)Perl_frexp(nv, &i);
       12235    		if (i == PERL_INT_MIN)
      ######    		    Perl_die(aTHX_ "panic: frexp");
       12235    		if (i > 0)
       11200    		    need = BIT_DIGITS(i);
			    }
       12408    	    need += has_precis ? precis : 6; /* known default */
		
       12408    	    if (need < width)
         124    		need = width;
		
		#ifdef HAS_LDBL_SPRINTF_BUG
			    /* This is to try to fix a bug with irix/nonstop-ux/powerux and
			       with sfio - Allen <allens@cpan.org> */
		
		#  ifdef DBL_MAX
		#    define MY_DBL_MAX DBL_MAX
		#  else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
		#    if DOUBLESIZE >= 8
		#      define MY_DBL_MAX 1.7976931348623157E+308L
		#    else
		#      define MY_DBL_MAX 3.40282347E+38L
		#    endif
		#  endif
		
		#  ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
		#    define MY_DBL_MAX_BUG 1L
		#  else
		#    define MY_DBL_MAX_BUG MY_DBL_MAX
		#  endif
		
		#  ifdef DBL_MIN
		#    define MY_DBL_MIN DBL_MIN
		#  else  /* XXX guessing! -Allen */
		#    if DOUBLESIZE >= 8
		#      define MY_DBL_MIN 2.2250738585072014E-308L
		#    else
		#      define MY_DBL_MIN 1.17549435E-38L
		#    endif
		#  endif
		
			    if ((intsize == 'q') && (c == 'f') &&
				((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
				(need < DBL_DIG)) {
				/* it's going to be short enough that
				 * long double precision is not needed */
		
				if ((nv <= 0L) && (nv >= -0L))
				    fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
				else {
				    /* would use Perl_fp_class as a double-check but not
				     * functional on IRIX - see perl.h comments */
		
				    if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
					/* It's within the range that a double can represent */
		#if defined(DBL_MAX) && !defined(DBL_MIN)
					if ((nv >= ((long double)1/DBL_MAX)) ||
					    (nv <= (-(long double)1/DBL_MAX)))
		#endif
					fix_ldbl_sprintf_bug = TRUE;
				    }
				}
				if (fix_ldbl_sprintf_bug == TRUE) {
				    double temp;
		
				    intsize = 0;
				    temp = (double)nv;
				    nv = (NV)temp;
				}
			    }
		
		#  undef MY_DBL_MAX
		#  undef MY_DBL_MAX_BUG
		#  undef MY_DBL_MIN
		
		#endif /* HAS_LDBL_SPRINTF_BUG */
		
       12408    	    need += 20; /* fudge factor */
       12408    	    if (PL_efloatsize < need) {
          66    		Safefree(PL_efloatbuf);
          66    		PL_efloatsize = need + 20; /* more fudge */
          66    		New(906, PL_efloatbuf, PL_efloatsize, char);
          66    		PL_efloatbuf[0] = '\0';
			    }
		
       12408    	    if ( !(width || left || plus || alt) && fill != '0'
				 && has_precis && intsize != 'q' ) {	/* Shortcuts */
				/* See earlier comment about buggy Gconvert when digits,
				   aka precis is 0  */
         207    		if ( c == 'g' && precis) {
          43    		    Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
          43    		    if (*PL_efloatbuf)	/* May return an empty string for digits==0 */
          43    			goto float_converted;
         164    		} else if ( c == 'f' && !precis) {
          38    		    if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
          21    			break;
				}
			    }
			    {
       12344    		char *ptr = ebuf + sizeof ebuf;
       12344    		*--ptr = '\0';
       12344    		*--ptr = c;
				/* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
		#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
       12344    		if (intsize == 'q') {
				    /* Copy the one or more characters in a long double
				     * format before the 'base' ([efgEFG]) character to
				     * the format string. */
      ######    		    static char const prifldbl[] = PERL_PRIfldbl;
      ######    		    char const *p = prifldbl + sizeof(prifldbl) - 3;
      ######    		    while (p >= prifldbl) { *--ptr = *p--; }
				}
		#endif
       12344    		if (has_precis) {
         295    		    base = precis;
         297    		    do { *--ptr = '0' + (base % 10); } while (base /= 10);
         295    		    *--ptr = '.';
				}
       12344    		if (width) {
         179    		    base = width;
         194    		    do { *--ptr = '0' + (base % 10); } while (base /= 10);
				}
       12344    		if (fill == '0')
          18    		    *--ptr = fill;
       12344    		if (left)
          10    		    *--ptr = '-';
       12344    		if (plus)
          17    		    *--ptr = plus;
       12344    		if (alt)
          10    		    *--ptr = '#';
       12344    		*--ptr = '%';
		
				/* No taint.  Otherwise we are in the strange situation
				 * where printf() taints but print($float) doesn't.
				 * --jhi */
		#if defined(HAS_LONG_DOUBLE)
       12344    		if (intsize == 'q')
      ######    		    (void)sprintf(PL_efloatbuf, ptr, nv);
				else
       12344    		    (void)sprintf(PL_efloatbuf, ptr, (double)nv);
		#else
				(void)sprintf(PL_efloatbuf, ptr, nv);
		#endif
			    }
			float_converted:
       12387    	    eptr = PL_efloatbuf;
       12387    	    elen = strlen(PL_efloatbuf);
       12387    	    break;
		
			    /* SPECIAL */
		
			case 'n':
           1    	    i = SvCUR(sv) - origlen;
           1    	    if (args && !vectorize) {
      ######    		switch (intsize) {
      ######    		case 'h':	*(va_arg(*args, short*)) = i; break;
      ######    		default:	*(va_arg(*args, int*)) = i; break;
      ######    		case 'l':	*(va_arg(*args, long*)) = i; break;
      ######    		case 'V':	*(va_arg(*args, IV*)) = i; break;
		#ifdef HAS_QUAD
				case 'q':	*(va_arg(*args, Quad_t*)) = i; break;
		#endif
				}
			    }
			    else
           1    		sv_setuv_mg(argsv, (UV)i);
           1    	    vectorize = FALSE;
           1    	    continue;	/* not "break" */
		
			    /* UNKNOWN */
		
			default:
		      unknown:
          64    	    if (!args && ckWARN(WARN_PRINTF) &&
				  (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
          58    		SV *msg = sv_newmortal();
          58    		Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
					  (PL_op->op_type == OP_PRTF) ? "" : "s");
          58    		if (c) {
          54    		    if (isPRINT(c))
          52    			Perl_sv_catpvf(aTHX_ msg,
						       "\"%%%c\"", c & 0xFF);
				    else
           2    			Perl_sv_catpvf(aTHX_ msg,
						       "\"%%\\%03"UVof"\"",
						       (UV)c & 0xFF);
				} else
           4    		    sv_catpv(msg, "end of string");
          58    		Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
			    }
		
			    /* output mangled stuff ... */
          64    	    if (c == '\0')
           6    		--q;
          64    	    eptr = p;
          64    	    elen = q - p;
		
			    /* ... right here, because formatting flags should not apply */
          64    	    SvGROW(sv, SvCUR(sv) + elen + 1);
          64    	    p = SvEND(sv);
          64    	    Copy(eptr, p, elen, char);
          64    	    p += elen;
          64    	    *p = '\0';
          64    	    SvCUR_set(sv, p - SvPVX_const(sv));
          64    	    svix = osvix;
          64    	    continue;	/* not "break" */
			}
		
			/* calculate width before utf8_upgrade changes it */
     1166568    	have = esignlen + zeros + elen;
		
     1166568    	if (is_utf8 != has_utf8) {
          83    	     if (is_utf8) {
           7    		  if (SvCUR(sv))
           5    		       sv_utf8_upgrade(sv);
			     }
			     else {
          76    		  SV * const nsv = sv_2mortal(newSVpvn(eptr, elen));
          76    		  sv_utf8_upgrade(nsv);
          76    		  eptr = SvPVX_const(nsv);
          76    		  elen = SvCUR(nsv);
			     }
          83    	     SvGROW(sv, SvCUR(sv) + elen + 1);
          83    	     p = SvEND(sv);
          83    	     *p = '\0';
			}
		
     1166568    	need = (have > width ? have : width);
     1166568    	gap = need - have;
		
     1166568    	SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
     1166568    	p = SvEND(sv);
     1166568    	if (esignlen && fill == '0') {
      ######    	    int i;
      ######    	    for (i = 0; i < (int)esignlen; i++)
      ######    		*p++ = esignbuf[i];
			}
     1166568    	if (gap && !left) {
      131471    	    memset(p, fill, gap);
      131471    	    p += gap;
			}
     1166568    	if (esignlen && fill != '0') {
      110834    	    int i;
      324042    	    for (i = 0; i < (int)esignlen; i++)
      213208    		*p++ = esignbuf[i];
			}
     1166568    	if (zeros) {
         112    	    int i;
         262    	    for (i = zeros; i; i--)
         150    		*p++ = '0';
			}
     1166568    	if (elen) {
     1143969    	    Copy(eptr, p, elen, char);
     1143969    	    p += elen;
			}
     1166568    	if (gap && left) {
       16228    	    memset(p, ' ', gap);
       16228    	    p += gap;
			}
     1166568    	if (vectorize) {
         223    	    if (veclen) {
         153    		Copy(dotstr, p, dotstrlen, char);
         153    		p += dotstrlen;
			    }
			    else
          70    		vectorize = FALSE;		/* done iterating over vecstr */
			}
     1166568    	if (is_utf8)
          20    	    has_utf8 = TRUE;
     1166568    	if (has_utf8)
          96    	    SvUTF8_on(sv);
     1166568    	*p = '\0';
     1166568    	SvCUR_set(sv, p - SvPVX_const(sv));
     1166568    	if (vectorize) {
         153    	    esignlen = 0;
         153    	    goto vector;
			}
		    }
		}
		
		/* =========================================================================
		
		=head1 Cloning an interpreter
		
		All the macros and functions in this section are for the private use of
		the main function, perl_clone().
		
		The foo_dup() functions make an exact copy of an existing foo thinngy.
		During the course of a cloning, a hash table is used to map old addresses
		to new addresses. The table is created and manipulated with the
		ptr_table_* functions.
		
		=cut
		
		============================================================================*/
		
		
		#if defined(USE_ITHREADS)
		
		#ifndef GpREFCNT_inc
		#  define GpREFCNT_inc(gp)	((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
		#endif
		
		
		#define sv_dup_inc(s,t)	SvREFCNT_inc(sv_dup(s,t))
		#define av_dup(s,t)	(AV*)sv_dup((SV*)s,t)
		#define av_dup_inc(s,t)	(AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
		#define hv_dup(s,t)	(HV*)sv_dup((SV*)s,t)
		#define hv_dup_inc(s,t)	(HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
		#define cv_dup(s,t)	(CV*)sv_dup((SV*)s,t)
		#define cv_dup_inc(s,t)	(CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
		#define io_dup(s,t)	(IO*)sv_dup((SV*)s,t)
		#define io_dup_inc(s,t)	(IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
		#define gv_dup(s,t)	(GV*)sv_dup((SV*)s,t)
		#define gv_dup_inc(s,t)	(GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
		#define SAVEPV(p)	(p ? savepv(p) : Nullch)
		#define SAVEPVN(p,n)	(p ? savepvn(p,n) : Nullch)
		
		
		/* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
		   regcomp.c. AMS 20010712 */
		
		REGEXP *
		Perl_re_dup(pTHX_ const REGEXP *r, CLONE_PARAMS *param)
		{
		    dVAR;
		    REGEXP *ret;
		    int i, len, npar;
		    struct reg_substr_datum *s;
		
		    if (!r)
			return (REGEXP *)NULL;
		
		    if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
			return ret;
		
		    len = r->offsets[0];
		    npar = r->nparens+1;
		
		    Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
		    Copy(r->program, ret->program, len+1, regnode);
		
		    New(0, ret->startp, npar, I32);
		    Copy(r->startp, ret->startp, npar, I32);
		    New(0, ret->endp, npar, I32);
		    Copy(r->startp, ret->startp, npar, I32);
		
		    New(0, ret->substrs, 1, struct reg_substr_data);
		    for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
			s->min_offset = r->substrs->data[i].min_offset;
			s->max_offset = r->substrs->data[i].max_offset;
			s->substr     = sv_dup_inc(r->substrs->data[i].substr, param);
			s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
		    }
		
		    ret->regstclass = NULL;
		    if (r->data) {
			struct reg_data *d;
		        const int count = r->data->count;
			int i;
		
			Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
				char, struct reg_data);
			New(0, d->what, count, U8);
		
			d->count = count;
			for (i = 0; i < count; i++) {
			    d->what[i] = r->data->what[i];
			    switch (d->what[i]) {
			        /* legal options are one of: sfpont
			           see also regcomp.h and pregfree() */
			    case 's':
				d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
				break;
			    case 'p':
				d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
				break;
			    case 'f':
				/* This is cheating. */
				New(0, d->data[i], 1, struct regnode_charclass_class);
				StructCopy(r->data->data[i], d->data[i],
					    struct regnode_charclass_class);
				ret->regstclass = (regnode*)d->data[i];
				break;
			    case 'o':
				/* Compiled op trees are readonly, and can thus be
				   shared without duplication. */
				OP_REFCNT_LOCK;
				d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
				OP_REFCNT_UNLOCK;
				break;
			    case 'n':
				d->data[i] = r->data->data[i];
				break;
			    case 't':
				d->data[i] = r->data->data[i];
				OP_REFCNT_LOCK;
				((reg_trie_data*)d->data[i])->refcount++;
				OP_REFCNT_UNLOCK;
				break;
		            default:
				Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]);
			    }
			}
		
			ret->data = d;
		    }
		    else
			ret->data = NULL;
		
		    New(0, ret->offsets, 2*len+1, U32);
		    Copy(r->offsets, ret->offsets, 2*len+1, U32);
		
		    ret->precomp        = SAVEPVN(r->precomp, r->prelen);
		    ret->refcnt         = r->refcnt;
		    ret->minlen         = r->minlen;
		    ret->prelen         = r->prelen;
		    ret->nparens        = r->nparens;
		    ret->lastparen      = r->lastparen;
		    ret->lastcloseparen = r->lastcloseparen;
		    ret->reganch        = r->reganch;
		
		    ret->sublen         = r->sublen;
		
		    if (RX_MATCH_COPIED(ret))
			ret->subbeg  = SAVEPVN(r->subbeg, r->sublen);
		    else
			ret->subbeg = Nullch;
		#ifdef PERL_OLD_COPY_ON_WRITE
		    ret->saved_copy = Nullsv;
		#endif
		
		    ptr_table_store(PL_ptr_table, r, ret);
		    return ret;
		}
		
		/* duplicate a file handle */
		
		PerlIO *
		Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
		{
		    PerlIO *ret;
		
		    PERL_UNUSED_ARG(type);
		
		    if (!fp)
			return (PerlIO*)NULL;
		
		    /* look for it in the table first */
		    ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
		    if (ret)
			return ret;
		
		    /* create anew and remember what it is */
		    ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
		    ptr_table_store(PL_ptr_table, fp, ret);
		    return ret;
		}
		
		/* duplicate a directory handle */
		
		DIR *
		Perl_dirp_dup(pTHX_ DIR *dp)
		{
		    if (!dp)
			return (DIR*)NULL;
		    /* XXX TODO */
		    return dp;
		}
		
		/* duplicate a typeglob */
		
		GP *
		Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
		{
		    GP *ret;
		    if (!gp)
			return (GP*)NULL;
		    /* look for it in the table first */
		    ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
		    if (ret)
			return ret;
		
		    /* create anew and remember what it is */
		    Newz(0, ret, 1, GP);
		    ptr_table_store(PL_ptr_table, gp, ret);
		
		    /* clone */
		    ret->gp_refcnt	= 0;			/* must be before any other dups! */
		    ret->gp_sv		= sv_dup_inc(gp->gp_sv, param);
		    ret->gp_io		= io_dup_inc(gp->gp_io, param);
		    ret->gp_form	= cv_dup_inc(gp->gp_form, param);
		    ret->gp_av		= av_dup_inc(gp->gp_av, param);
		    ret->gp_hv		= hv_dup_inc(gp->gp_hv, param);
		    ret->gp_egv	= gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
		    ret->gp_cv		= cv_dup_inc(gp->gp_cv, param);
		    ret->gp_cvgen	= gp->gp_cvgen;
		    ret->gp_line	= gp->gp_line;
		    ret->gp_file	= gp->gp_file;		/* points to COP.cop_file */
		    return ret;
		}
		
		/* duplicate a chain of magic */
		
		MAGIC *
		Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
		{
		    MAGIC *mgprev = (MAGIC*)NULL;
		    MAGIC *mgret;
		    if (!mg)
			return (MAGIC*)NULL;
		    /* look for it in the table first */
		    mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
		    if (mgret)
			return mgret;
		
		    for (; mg; mg = mg->mg_moremagic) {
			MAGIC *nmg;
			Newz(0, nmg, 1, MAGIC);
			if (mgprev)
			    mgprev->mg_moremagic = nmg;
			else
			    mgret = nmg;
			nmg->mg_virtual	= mg->mg_virtual;	/* XXX copy dynamic vtable? */
			nmg->mg_private	= mg->mg_private;
			nmg->mg_type	= mg->mg_type;
			nmg->mg_flags	= mg->mg_flags;
			if (mg->mg_type == PERL_MAGIC_qr) {
			    nmg->mg_obj	= (SV*)re_dup((REGEXP*)mg->mg_obj, param);
			}
			else if(mg->mg_type == PERL_MAGIC_backref) {
			    const AV * const av = (AV*) mg->mg_obj;
			    SV **svp;
			    I32 i;
			    (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
			    svp = AvARRAY(av);
			    for (i = AvFILLp(av); i >= 0; i--) {
				if (!svp[i]) continue;
				av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
			    }
			}
			else if (mg->mg_type == PERL_MAGIC_symtab) {
			    nmg->mg_obj	= mg->mg_obj;
			}
			else {
			    nmg->mg_obj	= (mg->mg_flags & MGf_REFCOUNTED)
					      ? sv_dup_inc(mg->mg_obj, param)
					      : sv_dup(mg->mg_obj, param);
			}
			nmg->mg_len	= mg->mg_len;
			nmg->mg_ptr	= mg->mg_ptr;	/* XXX random ptr? */
			if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
			    if (mg->mg_len > 0) {
				nmg->mg_ptr	= SAVEPVN(mg->mg_ptr, mg->mg_len);
				if (mg->mg_type == PERL_MAGIC_overload_table &&
					AMT_AMAGIC((AMT*)mg->mg_ptr))
				{
				    AMT *amtp = (AMT*)mg->mg_ptr;
				    AMT *namtp = (AMT*)nmg->mg_ptr;
				    I32 i;
				    for (i = 1; i < NofAMmeth; i++) {
					namtp->table[i] = cv_dup_inc(amtp->table[i], param);
				    }
				}
			    }
			    else if (mg->mg_len == HEf_SVKEY)
				nmg->mg_ptr	= (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
			}
			if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
			    CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
			}
			mgprev = nmg;
		    }
		    return mgret;
		}
		
		/* create a new pointer-mapping table */
		
		PTR_TBL_t *
		Perl_ptr_table_new(pTHX)
		{
		    PTR_TBL_t *tbl;
		    Newz(0, tbl, 1, PTR_TBL_t);
		    tbl->tbl_max	= 511;
		    tbl->tbl_items	= 0;
		    Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
		    return tbl;
		}
		
		#if (PTRSIZE == 8)
		#  define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
		#else
		#  define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
		#endif
		
		#define new_pte()	new_body(struct ptr_tbl_ent, pte)
		#define del_pte(p)	del_body_type(p, struct ptr_tbl_ent, pte)
		
		/* map an existing pointer using a table */
		
		void *
		Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, const void *sv)
		{
		    PTR_TBL_ENT_t *tblent;
		    const UV hash = PTR_TABLE_HASH(sv);
		    assert(tbl);
		    tblent = tbl->tbl_ary[hash & tbl->tbl_max];
		    for (; tblent; tblent = tblent->next) {
			if (tblent->oldval == sv)
			    return tblent->newval;
		    }
		    return (void*)NULL;
		}
		
		/* add a new entry to a pointer-mapping table */
		
		void
		Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, const void *oldv, void *newv)
		{
		    PTR_TBL_ENT_t *tblent, **otblent;
		    /* XXX this may be pessimal on platforms where pointers aren't good
		     * hash values e.g. if they grow faster in the most significant
		     * bits */
		    const UV hash = PTR_TABLE_HASH(oldv);
		    bool empty = 1;
		
		    assert(tbl);
		    otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
		    for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
			if (tblent->oldval == oldv) {
			    tblent->newval = newv;
			    return;
			}
		    }
		    tblent = new_pte();
		    tblent->oldval = oldv;
		    tblent->newval = newv;
		    tblent->next = *otblent;
		    *otblent = tblent;
		    tbl->tbl_items++;
		    if (!empty && tbl->tbl_items > tbl->tbl_max)
			ptr_table_split(tbl);
		}
		
		/* double the hash bucket size of an existing ptr table */
		
		void
		Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
		{
		    PTR_TBL_ENT_t **ary = tbl->tbl_ary;
		    const UV oldsize = tbl->tbl_max + 1;
		    UV newsize = oldsize * 2;
		    UV i;
		
		    Renew(ary, newsize, PTR_TBL_ENT_t*);
		    Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
		    tbl->tbl_max = --newsize;
		    tbl->tbl_ary = ary;
		    for (i=0; i < oldsize; i++, ary++) {
			PTR_TBL_ENT_t **curentp, **entp, *ent;
			if (!*ary)
			    continue;
			curentp = ary + oldsize;
			for (entp = ary, ent = *ary; ent; ent = *entp) {
			    if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
				*entp = ent->next;
				ent->next = *curentp;
				*curentp = ent;
				continue;
			    }
			    else
				entp = &ent->next;
			}
		    }
		}
		
		/* remove all the entries from a ptr table */
		
		void
		Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
		{
		    register PTR_TBL_ENT_t **array;
		    register PTR_TBL_ENT_t *entry;
		    UV riter = 0;
		    UV max;
		
		    if (!tbl || !tbl->tbl_items) {
		        return;
		    }
		
		    array = tbl->tbl_ary;
		    entry = array[0];
		    max = tbl->tbl_max;
		
		    for (;;) {
		        if (entry) {
		            PTR_TBL_ENT_t *oentry = entry;
		            entry = entry->next;
		            del_pte(oentry);
		        }
		        if (!entry) {
		            if (++riter > max) {
		                break;
		            }
		            entry = array[riter];
		        }
		    }
		
		    tbl->tbl_items = 0;
		}
		
		/* clear and free a ptr table */
		
		void
		Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
		{
		    if (!tbl) {
		        return;
		    }
		    ptr_table_clear(tbl);
		    Safefree(tbl->tbl_ary);
		    Safefree(tbl);
		}
		
		
		void
		Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
		{
		    if (SvROK(sstr)) {
			SvRV_set(dstr, SvWEAKREF(sstr)
				       ? sv_dup(SvRV(sstr), param)
				       : sv_dup_inc(SvRV(sstr), param));
		
		    }
		    else if (SvPVX_const(sstr)) {
			/* Has something there */
			if (SvLEN(sstr)) {
			    /* Normal PV - clone whole allocated space */
			    SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1));
			    if (SvREADONLY(sstr) && SvFAKE(sstr)) {
				/* Not that normal - actually sstr is copy on write.
				   But we are a true, independant SV, so:  */
				SvREADONLY_off(dstr);
				SvFAKE_off(dstr);
			    }
			}
			else {
			    /* Special case - not normally malloced for some reason */
			    if ((SvREADONLY(sstr) && SvFAKE(sstr))) {
				/* A "shared" PV - clone it as "shared" PV */
				SvPV_set(dstr,
					 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)),
							 param)));
			    }
			    else {
				/* Some other special case - random pointer */
				SvPV_set(dstr, SvPVX(sstr));		
			    }
			}
		    }
		    else {
			/* Copy the Null */
			if (SvTYPE(dstr) == SVt_RV)
			    SvRV_set(dstr, NULL);
			else
			    SvPV_set(dstr, 0);
		    }
		}
		
		/* duplicate an SV of any type (including AV, HV etc) */
		
		SV *
		Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
		{
		    dVAR;
		    SV *dstr;
		
		    if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
			return Nullsv;
		    /* look for it in the table first */
		    dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
		    if (dstr)
			return dstr;
		
		    if(param->flags & CLONEf_JOIN_IN) {
		        /** We are joining here so we don't want do clone
			    something that is bad **/
			const char *hvname;
		
		        if(SvTYPE(sstr) == SVt_PVHV &&
			   (hvname = HvNAME_get(sstr))) {
			    /** don't clone stashes if they already exist **/
			    HV* old_stash = gv_stashpv(hvname,0);
			    return (SV*) old_stash;
		        }
		    }
		
		    /* create anew and remember what it is */
		    new_SV(dstr);
		
		#ifdef DEBUG_LEAKING_SCALARS
		    dstr->sv_debug_optype = sstr->sv_debug_optype;
		    dstr->sv_debug_line = sstr->sv_debug_line;
		    dstr->sv_debug_inpad = sstr->sv_debug_inpad;
		    dstr->sv_debug_cloned = 1;
		#  ifdef NETWARE
		    dstr->sv_debug_file = savepv(sstr->sv_debug_file);
		#  else
		    dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
		#  endif
		#endif
		
		    ptr_table_store(PL_ptr_table, sstr, dstr);
		
		    /* clone */
		    SvFLAGS(dstr)	= SvFLAGS(sstr);
		    SvFLAGS(dstr)	&= ~SVf_OOK;		/* don't propagate OOK hack */
		    SvREFCNT(dstr)	= 0;			/* must be before any other dups! */
		
		#ifdef DEBUGGING
		    if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx)
			PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
				      PL_watch_pvx, SvPVX_const(sstr));
		#endif
		
		    /* don't clone objects whose class has asked us not to */
		    if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
			SvFLAGS(dstr) &= ~SVTYPEMASK;
			SvOBJECT_off(dstr);
			return dstr;
		    }
		
		    switch (SvTYPE(sstr)) {
		    case SVt_NULL:
			SvANY(dstr)	= NULL;
			break;
		    case SVt_IV:
			SvANY(dstr)	= (XPVIV*)((char*)&(dstr->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
			SvIV_set(dstr, SvIVX(sstr));
			break;
		    case SVt_NV:
			SvANY(dstr)	= new_XNV();
			SvNV_set(dstr, SvNVX(sstr));
			break;
		    case SVt_RV:
			SvANY(dstr)	= &(dstr->sv_u.svu_rv);
			Perl_rvpv_dup(aTHX_ dstr, sstr, param);
			break;
		    default:
			{
			    /* These are all the types that need complex bodies allocating.  */
			    size_t new_body_length;
			    size_t new_body_offset = 0;
			    void **new_body_arena;
			    void **new_body_arenaroot;
			    void *new_body;
		
			    switch (SvTYPE(sstr)) {
			    default:
				Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]",
					   (IV)SvTYPE(sstr));
				break;
		
			    case SVt_PVIO:
				new_body = new_XPVIO();
				new_body_length = sizeof(XPVIO);
				break;
			    case SVt_PVFM:
				new_body = new_XPVFM();
				new_body_length = sizeof(XPVFM);
				break;
		
			    case SVt_PVHV:
				new_body_arena = (void **) &PL_xpvhv_root;
				new_body_arenaroot = (void **) &PL_xpvhv_arenaroot;
				new_body_offset = STRUCT_OFFSET(XPVHV, xhv_fill)
				    - STRUCT_OFFSET(xpvhv_allocated, xhv_fill);
				new_body_length = STRUCT_OFFSET(XPVHV, xmg_stash)
				    + sizeof (((XPVHV*)SvANY(sstr))->xmg_stash)
				    - new_body_offset;
				goto new_body;
			    case SVt_PVAV:
				new_body_arena = (void **) &PL_xpvav_root;
				new_body_arenaroot = (void **) &PL_xpvav_arenaroot;
				new_body_offset = STRUCT_OFFSET(XPVAV, xav_fill)
				    - STRUCT_OFFSET(xpvav_allocated, xav_fill);
				new_body_length = STRUCT_OFFSET(XPVHV, xmg_stash)
				    + sizeof (((XPVHV*)SvANY(sstr))->xmg_stash)
				    - new_body_offset;
				goto new_body;
			    case SVt_PVBM:
				new_body_length = sizeof(XPVBM);
				new_body_arena = (void **) &PL_xpvbm_root;
				new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
				goto new_body;
			    case SVt_PVGV:
				if (GvUNIQUE((GV*)sstr)) {
				    /* Do sharing here.  */
				}
				new_body_length = sizeof(XPVGV);
				new_body_arena = (void **) &PL_xpvgv_root;
				new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
				goto new_body;
			    case SVt_PVCV:
				new_body_length = sizeof(XPVCV);
				new_body_arena = (void **) &PL_xpvcv_root;
				new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
				goto new_body;
			    case SVt_PVLV:
				new_body_length = sizeof(XPVLV);
				new_body_arena = (void **) &PL_xpvlv_root;
				new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
				goto new_body;
			    case SVt_PVMG:
				new_body_length = sizeof(XPVMG);
				new_body_arena = (void **) &PL_xpvmg_root;
				new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
				goto new_body;
			    case SVt_PVNV:
				new_body_length = sizeof(XPVNV);
				new_body_arena = (void **) &PL_xpvnv_root;
				new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
				goto new_body;
			    case SVt_PVIV:
				new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
				    - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
				new_body_length = sizeof(XPVIV) - new_body_offset;
				new_body_arena = (void **) &PL_xpviv_root;
				new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
				goto new_body; 
			    case SVt_PV:
				new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
				    - STRUCT_OFFSET(xpv_allocated, xpv_cur);
				new_body_length = sizeof(XPV) - new_body_offset;
				new_body_arena = (void **) &PL_xpv_root;
				new_body_arenaroot = (void **) &PL_xpv_arenaroot;
			    new_body:
				assert(new_body_length);
		#ifndef PURIFY
				new_body = (void*)((char*)S_new_body(aTHX_ new_body_arenaroot,
								     new_body_arena,
								     new_body_length)
						   - new_body_offset);
		#else
				/* We always allocated the full length item with PURIFY */
				new_body_length += new_body_offset;
				new_body_offset = 0;
				new_body = my_safemalloc(new_body_length);
		#endif
			    }
			    assert(new_body);
			    SvANY(dstr) = new_body;
		
			    Copy(((char*)SvANY(sstr)) + new_body_offset,
				 ((char*)SvANY(dstr)) + new_body_offset,
				 new_body_length, char);
		
			    if (SvTYPE(sstr) != SVt_PVAV && SvTYPE(sstr) != SVt_PVHV)
				Perl_rvpv_dup(aTHX_ dstr, sstr, param);
		
			    /* The Copy above means that all the source (unduplicated) pointers
			       are now in the destination.  We can check the flags and the
			       pointers in either, but it's possible that there's less cache
			       missing by always going for the destination.
			       FIXME - instrument and check that assumption  */
			    if (SvTYPE(sstr) >= SVt_PVMG) {
				if (SvMAGIC(dstr))
				    SvMAGIC_set(dstr, mg_dup(SvMAGIC(dstr), param));
				if (SvSTASH(dstr))
				    SvSTASH_set(dstr, hv_dup_inc(SvSTASH(dstr), param));
			    }
		
			    switch (SvTYPE(sstr)) {
			    case SVt_PV:
				break;
			    case SVt_PVIV:
				break;
			    case SVt_PVNV:
				break;
			    case SVt_PVMG:
				break;
			    case SVt_PVBM:
				break;
			    case SVt_PVLV:
				/* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
				if (LvTYPE(dstr) == 't') /* for tie: unrefcnted fake (SV**) */
				    LvTARG(dstr) = dstr;
				else if (LvTYPE(dstr) == 'T') /* for tie: fake HE */
				    LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(dstr), 0, param);
				else
				    LvTARG(dstr) = sv_dup_inc(LvTARG(dstr), param);
				break;
			    case SVt_PVGV:
				GvNAME(dstr)	= SAVEPVN(GvNAME(dstr), GvNAMELEN(dstr));
				GvSTASH(dstr)	= hv_dup(GvSTASH(dstr), param);
				/* Don't call sv_add_backref here as it's going to be created
				   as part of the magic cloning of the symbol table.  */
				GvGP(dstr)	= gp_dup(GvGP(dstr), param);
				(void)GpREFCNT_inc(GvGP(dstr));
				break;
			    case SVt_PVIO:
				IoIFP(dstr)	= fp_dup(IoIFP(dstr), IoTYPE(dstr), param);
				if (IoOFP(dstr) == IoIFP(sstr))
				    IoOFP(dstr) = IoIFP(dstr);
				else
				    IoOFP(dstr)	= fp_dup(IoOFP(dstr), IoTYPE(dstr), param);
				/* PL_rsfp_filters entries have fake IoDIRP() */
				if (IoDIRP(dstr) && !(IoFLAGS(dstr) & IOf_FAKE_DIRP))
				    IoDIRP(dstr)	= dirp_dup(IoDIRP(dstr));
				if(IoFLAGS(dstr) & IOf_FAKE_DIRP) {
				    /* I have no idea why fake dirp (rsfps)
				       should be treated differently but otherwise
				       we end up with leaks -- sky*/
				    IoTOP_GV(dstr)      = gv_dup_inc(IoTOP_GV(dstr), param);
				    IoFMT_GV(dstr)      = gv_dup_inc(IoFMT_GV(dstr), param);
				    IoBOTTOM_GV(dstr)   = gv_dup_inc(IoBOTTOM_GV(dstr), param);
				} else {
				    IoTOP_GV(dstr)      = gv_dup(IoTOP_GV(dstr), param);
				    IoFMT_GV(dstr)      = gv_dup(IoFMT_GV(dstr), param);
				    IoBOTTOM_GV(dstr)   = gv_dup(IoBOTTOM_GV(dstr), param);
				}
				IoTOP_NAME(dstr)	= SAVEPV(IoTOP_NAME(dstr));
				IoFMT_NAME(dstr)	= SAVEPV(IoFMT_NAME(dstr));
				IoBOTTOM_NAME(dstr)	= SAVEPV(IoBOTTOM_NAME(dstr));
				break;
			    case SVt_PVAV:
				if (AvARRAY((AV*)sstr)) {
				    SV **dst_ary, **src_ary;
				    SSize_t items = AvFILLp((AV*)sstr) + 1;
		
				    src_ary = AvARRAY((AV*)sstr);
				    Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
				    ptr_table_store(PL_ptr_table, src_ary, dst_ary);
				    SvPV_set(dstr, (char*)dst_ary);
				    AvALLOC((AV*)dstr) = dst_ary;
				    if (AvREAL((AV*)sstr)) {
					while (items-- > 0)
					    *dst_ary++ = sv_dup_inc(*src_ary++, param);
				    }
				    else {
					while (items-- > 0)
					    *dst_ary++ = sv_dup(*src_ary++, param);
				    }
				    items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
				    while (items-- > 0) {
					*dst_ary++ = &PL_sv_undef;
				    }
				}
				else {
				    SvPV_set(dstr, Nullch);
				    AvALLOC((AV*)dstr)	= (SV**)NULL;
				}
				break;
			    case SVt_PVHV:
				{
				    HEK *hvname = 0;
		
				    if (HvARRAY((HV*)sstr)) {
					STRLEN i = 0;
					const bool sharekeys = !!HvSHAREKEYS(sstr);
					XPVHV * const dxhv = (XPVHV*)SvANY(dstr);
					XPVHV * const sxhv = (XPVHV*)SvANY(sstr);
					char *darray;
					New(0, darray,
					    PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1)
					    + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0),
					    char);
					HvARRAY(dstr) = (HE**)darray;
					while (i <= sxhv->xhv_max) {
					    HE *source = HvARRAY(sstr)[i];
					    HvARRAY(dstr)[i] = source
						? he_dup(source, sharekeys, param) : 0;
					    ++i;
					}
					if (SvOOK(sstr)) {
					    struct xpvhv_aux *saux = HvAUX(sstr);
					    struct xpvhv_aux *daux = HvAUX(dstr);
					    /* This flag isn't copied.  */
					    /* SvOOK_on(hv) attacks the IV flags.  */
					    SvFLAGS(dstr) |= SVf_OOK;
		
					    hvname = saux->xhv_name;
					    daux->xhv_name
						= hvname ? hek_dup(hvname, param) : hvname;
		
					    daux->xhv_riter = saux->xhv_riter;
					    daux->xhv_eiter = saux->xhv_eiter
						? he_dup(saux->xhv_eiter,
							 (bool)!!HvSHAREKEYS(sstr), param) : 0;
					}
				    }
				    else {
					SvPV_set(dstr, Nullch);
				    }
				    /* Record stashes for possible cloning in Perl_clone(). */
				    if(hvname)
					av_push(param->stashes, dstr);
				}
				break;
			    case SVt_PVFM:
			    case SVt_PVCV:
				/* NOTE: not refcounted */
				CvSTASH(dstr)	= hv_dup(CvSTASH(dstr), param);
				OP_REFCNT_LOCK;
				CvROOT(dstr)	= OpREFCNT_inc(CvROOT(dstr));
				OP_REFCNT_UNLOCK;
				if (CvCONST(dstr)) {
				    CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(dstr)) ?
					SvREFCNT_inc(CvXSUBANY(dstr).any_ptr) :
					sv_dup_inc((SV *)CvXSUBANY(dstr).any_ptr, param);
				}
				/* don't dup if copying back - CvGV isn't refcounted, so the
				 * duped GV may never be freed. A bit of a hack! DAPM */
				CvGV(dstr)	= (param->flags & CLONEf_JOIN_IN) ?
				    Nullgv : gv_dup(CvGV(dstr), param) ;
				if (!(param->flags & CLONEf_COPY_STACKS)) {
				    CvDEPTH(dstr) = 0;
				}
				PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
				CvOUTSIDE(dstr)	=
				    CvWEAKOUTSIDE(sstr)
				    ? cv_dup(    CvOUTSIDE(dstr), param)
				    : cv_dup_inc(CvOUTSIDE(dstr), param);
				if (!CvXSUB(dstr))
				    CvFILE(dstr) = SAVEPV(CvFILE(dstr));
				break;
			    }
			}
		    }
		
		    if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
			++PL_sv_objcount;
		
		    return dstr;
		 }
		
		/* duplicate a context */
		
		PERL_CONTEXT *
		Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
		{
		    PERL_CONTEXT *ncxs;
		
		    if (!cxs)
			return (PERL_CONTEXT*)NULL;
		
		    /* look for it in the table first */
		    ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
		    if (ncxs)
			return ncxs;
		
		    /* create anew and remember what it is */
		    Newz(56, ncxs, max + 1, PERL_CONTEXT);
		    ptr_table_store(PL_ptr_table, cxs, ncxs);
		
		    while (ix >= 0) {
			PERL_CONTEXT *cx = &cxs[ix];
			PERL_CONTEXT *ncx = &ncxs[ix];
			ncx->cx_type	= cx->cx_type;
			if (CxTYPE(cx) == CXt_SUBST) {
			    Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
			}
			else {
			    ncx->blk_oldsp	= cx->blk_oldsp;
			    ncx->blk_oldcop	= cx->blk_oldcop;
			    ncx->blk_oldmarksp	= cx->blk_oldmarksp;
			    ncx->blk_oldscopesp	= cx->blk_oldscopesp;
			    ncx->blk_oldpm	= cx->blk_oldpm;
			    ncx->blk_gimme	= cx->blk_gimme;
			    switch (CxTYPE(cx)) {
			    case CXt_SUB:
				ncx->blk_sub.cv		= (cx->blk_sub.olddepth == 0
							   ? cv_dup_inc(cx->blk_sub.cv, param)
							   : cv_dup(cx->blk_sub.cv,param));
				ncx->blk_sub.argarray	= (cx->blk_sub.hasargs
							   ? av_dup_inc(cx->blk_sub.argarray, param)
							   : Nullav);
				ncx->blk_sub.savearray	= av_dup_inc(cx->blk_sub.savearray, param);
				ncx->blk_sub.olddepth	= cx->blk_sub.olddepth;
				ncx->blk_sub.hasargs	= cx->blk_sub.hasargs;
				ncx->blk_sub.lval	= cx->blk_sub.lval;
				ncx->blk_sub.retop	= cx->blk_sub.retop;
				break;
			    case CXt_EVAL:
				ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
				ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
				ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
				ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
				ncx->blk_eval.cur_text	= sv_dup(cx->blk_eval.cur_text, param);
				ncx->blk_eval.retop = cx->blk_eval.retop;
				break;
			    case CXt_LOOP:
				ncx->blk_loop.label	= cx->blk_loop.label;
				ncx->blk_loop.resetsp	= cx->blk_loop.resetsp;
				ncx->blk_loop.redo_op	= cx->blk_loop.redo_op;
				ncx->blk_loop.next_op	= cx->blk_loop.next_op;
				ncx->blk_loop.last_op	= cx->blk_loop.last_op;
				ncx->blk_loop.iterdata	= (CxPADLOOP(cx)
							   ? cx->blk_loop.iterdata
							   : gv_dup((GV*)cx->blk_loop.iterdata, param));
				ncx->blk_loop.oldcomppad
				    = (PAD*)ptr_table_fetch(PL_ptr_table,
							    cx->blk_loop.oldcomppad);
				ncx->blk_loop.itersave	= sv_dup_inc(cx->blk_loop.itersave, param);
				ncx->blk_loop.iterlval	= sv_dup_inc(cx->blk_loop.iterlval, param);
				ncx->blk_loop.iterary	= av_dup_inc(cx->blk_loop.iterary, param);
				ncx->blk_loop.iterix	= cx->blk_loop.iterix;
				ncx->blk_loop.itermax	= cx->blk_loop.itermax;
				break;
			    case CXt_FORMAT:
				ncx->blk_sub.cv		= cv_dup(cx->blk_sub.cv, param);
				ncx->blk_sub.gv		= gv_dup(cx->blk_sub.gv, param);
				ncx->blk_sub.dfoutgv	= gv_dup_inc(cx->blk_sub.dfoutgv, param);
				ncx->blk_sub.hasargs	= cx->blk_sub.hasargs;
				ncx->blk_sub.retop	= cx->blk_sub.retop;
				break;
			    case CXt_BLOCK:
			    case CXt_NULL:
				break;
			    }
			}
			--ix;
		    }
		    return ncxs;
		}
		
		/* duplicate a stack info structure */
		
		PERL_SI *
		Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
		{
		    PERL_SI *nsi;
		
		    if (!si)
			return (PERL_SI*)NULL;
		
		    /* look for it in the table first */
		    nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
		    if (nsi)
			return nsi;
		
		    /* create anew and remember what it is */
		    Newz(56, nsi, 1, PERL_SI);
		    ptr_table_store(PL_ptr_table, si, nsi);
		
		    nsi->si_stack	= av_dup_inc(si->si_stack, param);
		    nsi->si_cxix	= si->si_cxix;
		    nsi->si_cxmax	= si->si_cxmax;
		    nsi->si_cxstack	= cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
		    nsi->si_type	= si->si_type;
		    nsi->si_prev	= si_dup(si->si_prev, param);
		    nsi->si_next	= si_dup(si->si_next, param);
		    nsi->si_markoff	= si->si_markoff;
		
		    return nsi;
		}
		
		#define POPINT(ss,ix)	((ss)[--(ix)].any_i32)
		#define TOPINT(ss,ix)	((ss)[ix].any_i32)
		#define POPLONG(ss,ix)	((ss)[--(ix)].any_long)
		#define TOPLONG(ss,ix)	((ss)[ix].any_long)
		#define POPIV(ss,ix)	((ss)[--(ix)].any_iv)
		#define TOPIV(ss,ix)	((ss)[ix].any_iv)
		#define POPBOOL(ss,ix)	((ss)[--(ix)].any_bool)
		#define TOPBOOL(ss,ix)	((ss)[ix].any_bool)
		#define POPPTR(ss,ix)	((ss)[--(ix)].any_ptr)
		#define TOPPTR(ss,ix)	((ss)[ix].any_ptr)
		#define POPDPTR(ss,ix)	((ss)[--(ix)].any_dptr)
		#define TOPDPTR(ss,ix)	((ss)[ix].any_dptr)
		#define POPDXPTR(ss,ix)	((ss)[--(ix)].any_dxptr)
		#define TOPDXPTR(ss,ix)	((ss)[ix].any_dxptr)
		
		/* XXXXX todo */
		#define pv_dup_inc(p)	SAVEPV(p)
		#define pv_dup(p)	SAVEPV(p)
		#define svp_dup_inc(p,pp)	any_dup(p,pp)
		
		/* map any object to the new equivent - either something in the
		 * ptr table, or something in the interpreter structure
		 */
		
		void *
		Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
		{
		    void *ret;
		
		    if (!v)
			return (void*)NULL;
		
		    /* look for it in the table first */
		    ret = ptr_table_fetch(PL_ptr_table, v);
		    if (ret)
			return ret;
		
		    /* see if it is part of the interpreter structure */
		    if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
			ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
		    else {
			ret = v;
		    }
		
		    return ret;
		}
		
		/* duplicate the save stack */
		
		ANY *
		Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
		{
		    ANY * const ss	= proto_perl->Tsavestack;
		    const I32 max	= proto_perl->Tsavestack_max;
		    I32 ix		= proto_perl->Tsavestack_ix;
		    ANY *nss;
		    SV *sv;
		    GV *gv;
		    AV *av;
		    HV *hv;
		    void* ptr;
		    int intval;
		    long longval;
		    GP *gp;
		    IV iv;
		    char *c = NULL;
		    void (*dptr) (void*);
		    void (*dxptr) (pTHX_ void*);
		
		    Newz(54, nss, max, ANY);
		
		    while (ix > 0) {
			I32 i = POPINT(ss,ix);
			TOPINT(nss,ix) = i;
			switch (i) {
			case SAVEt_ITEM:			/* normal string */
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    break;
		        case SAVEt_SV:				/* scalar reference */
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    gv = (GV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = gv_dup_inc(gv, param);
			    break;
			case SAVEt_GENERIC_PVREF:		/* generic char* */
			    c = (char*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = pv_dup(c);
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    break;
			case SAVEt_SHARED_PVREF:		/* char* in shared space */
			    c = (char*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = savesharedpv(c);
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    break;
		        case SAVEt_GENERIC_SVREF:		/* generic sv */
		        case SAVEt_SVREF:			/* scalar reference */
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
			    break;
		        case SAVEt_AV:				/* array reference */
			    av = (AV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = av_dup_inc(av, param);
			    gv = (GV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = gv_dup(gv, param);
			    break;
		        case SAVEt_HV:				/* hash reference */
			    hv = (HV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = hv_dup_inc(hv, param);
			    gv = (GV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = gv_dup(gv, param);
			    break;
			case SAVEt_INT:				/* int reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    intval = (int)POPINT(ss,ix);
			    TOPINT(nss,ix) = intval;
			    break;
			case SAVEt_LONG:			/* long reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    longval = (long)POPLONG(ss,ix);
			    TOPLONG(nss,ix) = longval;
			    break;
			case SAVEt_I32:				/* I32 reference */
			case SAVEt_I16:				/* I16 reference */
			case SAVEt_I8:				/* I8 reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    break;
			case SAVEt_IV:				/* IV reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    iv = POPIV(ss,ix);
			    TOPIV(nss,ix) = iv;
			    break;
			case SAVEt_SPTR:			/* SV* reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup(sv, param);
			    break;
			case SAVEt_VPTR:			/* random* reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    break;
			case SAVEt_PPTR:			/* char* reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    c = (char*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = pv_dup(c);
			    break;
			case SAVEt_HPTR:			/* HV* reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    hv = (HV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = hv_dup(hv, param);
			    break;
			case SAVEt_APTR:			/* AV* reference */
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    av = (AV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = av_dup(av, param);
			    break;
			case SAVEt_NSTAB:
			    gv = (GV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = gv_dup(gv, param);
			    break;
			case SAVEt_GP:				/* scalar reference */
			    gp = (GP*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = gp = gp_dup(gp, param);
			    (void)GpREFCNT_inc(gp);
			    gv = (GV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = gv_dup_inc(gv, param);
		            c = (char*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = pv_dup(c);
			    iv = POPIV(ss,ix);
			    TOPIV(nss,ix) = iv;
			    iv = POPIV(ss,ix);
			    TOPIV(nss,ix) = iv;
		            break;
			case SAVEt_FREESV:
			case SAVEt_MORTALIZESV:
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    break;
			case SAVEt_FREEOP:
			    ptr = POPPTR(ss,ix);
			    if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
				/* these are assumed to be refcounted properly */
				OP *o;
				switch (((OP*)ptr)->op_type) {
				case OP_LEAVESUB:
				case OP_LEAVESUBLV:
				case OP_LEAVEEVAL:
				case OP_LEAVE:
				case OP_SCOPE:
				case OP_LEAVEWRITE:
				    TOPPTR(nss,ix) = ptr;
				    o = (OP*)ptr;
				    OpREFCNT_inc(o);
				    break;
				default:
				    TOPPTR(nss,ix) = Nullop;
				    break;
				}
			    }
			    else
				TOPPTR(nss,ix) = Nullop;
			    break;
			case SAVEt_FREEPV:
			    c = (char*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = pv_dup_inc(c);
			    break;
			case SAVEt_CLEARSV:
			    longval = POPLONG(ss,ix);
			    TOPLONG(nss,ix) = longval;
			    break;
			case SAVEt_DELETE:
			    hv = (HV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = hv_dup_inc(hv, param);
			    c = (char*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = pv_dup_inc(c);
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    break;
			case SAVEt_DESTRUCTOR:
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);	/* XXX quite arbitrary */
			    dptr = POPDPTR(ss,ix);
			    TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
							any_dup(FPTR2DPTR(void *, dptr),
								proto_perl));
			    break;
			case SAVEt_DESTRUCTOR_X:
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);	/* XXX quite arbitrary */
			    dxptr = POPDXPTR(ss,ix);
			    TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
							 any_dup(FPTR2DPTR(void *, dxptr),
								 proto_perl));
			    break;
			case SAVEt_REGCONTEXT:
			case SAVEt_ALLOC:
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    ix -= i;
			    break;
			case SAVEt_STACK_POS:		/* Position on Perl stack */
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    break;
			case SAVEt_AELEM:		/* array element */
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    av = (AV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = av_dup_inc(av, param);
			    break;
			case SAVEt_HELEM:		/* hash element */
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup_inc(sv, param);
			    hv = (HV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = hv_dup_inc(hv, param);
			    break;
			case SAVEt_OP:
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = ptr;
			    break;
			case SAVEt_HINTS:
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    break;
			case SAVEt_COMPPAD:
			    av = (AV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = av_dup(av, param);
			    break;
			case SAVEt_PADSV:
			    longval = (long)POPLONG(ss,ix);
			    TOPLONG(nss,ix) = longval;
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup(sv, param);
			    break;
			case SAVEt_BOOL:
			    ptr = POPPTR(ss,ix);
			    TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
			    longval = (long)POPBOOL(ss,ix);
			    TOPBOOL(nss,ix) = (bool)longval;
			    break;
			case SAVEt_SET_SVFLAGS:
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    i = POPINT(ss,ix);
			    TOPINT(nss,ix) = i;
			    sv = (SV*)POPPTR(ss,ix);
			    TOPPTR(nss,ix) = sv_dup(sv, param);
			    break;
			default:
			    Perl_croak(aTHX_ "panic: ss_dup inconsistency");
			}
		    }
		
		    return nss;
		}
		
		
		/* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
		 * flag to the result. This is done for each stash before cloning starts,
		 * so we know which stashes want their objects cloned */
		
		static void
		do_mark_cloneable_stash(pTHX_ SV *sv)
		{
		    const HEK * const hvname = HvNAME_HEK((HV*)sv);
		    if (hvname) {
			GV* const cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
			SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
			if (cloner && GvCV(cloner)) {
			    dSP;
			    UV status;
		
			    ENTER;
			    SAVETMPS;
			    PUSHMARK(SP);
			    XPUSHs(sv_2mortal(newSVhek(hvname)));
			    PUTBACK;
			    call_sv((SV*)GvCV(cloner), G_SCALAR);
			    SPAGAIN;
			    status = POPu;
			    PUTBACK;
			    FREETMPS;
			    LEAVE;
			    if (status)
				SvFLAGS(sv) &= ~SVphv_CLONEABLE;
			}
		    }
		}
		
		
		
		/*
		=for apidoc perl_clone
		
		Create and return a new interpreter by cloning the current one.
		
		perl_clone takes these flags as parameters:
		
		CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
		without it we only clone the data and zero the stacks,
		with it we copy the stacks and the new perl interpreter is
		ready to run at the exact same point as the previous one.
		The pseudo-fork code uses COPY_STACKS while the
		threads->new doesn't.
		
		CLONEf_KEEP_PTR_TABLE
		perl_clone keeps a ptr_table with the pointer of the old
		variable as a key and the new variable as a value,
		this allows it to check if something has been cloned and not
		clone it again but rather just use the value and increase the
		refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
		the ptr_table using the function
		C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
		reason to keep it around is if you want to dup some of your own
		variable who are outside the graph perl scans, example of this
		code is in threads.xs create
		
		CLONEf_CLONE_HOST
		This is a win32 thing, it is ignored on unix, it tells perls
		win32host code (which is c++) to clone itself, this is needed on
		win32 if you want to run two threads at the same time,
		if you just want to do some stuff in a separate perl interpreter
		and then throw it away and return to the original one,
		you don't need to do anything.
		
		=cut
		*/
		
		/* XXX the above needs expanding by someone who actually understands it ! */
		EXTERN_C PerlInterpreter *
		perl_clone_host(PerlInterpreter* proto_perl, UV flags);
		
		PerlInterpreter *
		perl_clone(PerlInterpreter *proto_perl, UV flags)
		{
		   dVAR;
		#ifdef PERL_IMPLICIT_SYS
		
		   /* perlhost.h so we need to call into it
		   to clone the host, CPerlHost should have a c interface, sky */
		
		   if (flags & CLONEf_CLONE_HOST) {
		       return perl_clone_host(proto_perl,flags);
		   }
		   return perl_clone_using(proto_perl, flags,
					    proto_perl->IMem,
					    proto_perl->IMemShared,
					    proto_perl->IMemParse,
					    proto_perl->IEnv,
					    proto_perl->IStdIO,
					    proto_perl->ILIO,
					    proto_perl->IDir,
					    proto_perl->ISock,
					    proto_perl->IProc);
		}
		
		PerlInterpreter *
		perl_clone_using(PerlInterpreter *proto_perl, UV flags,
				 struct IPerlMem* ipM, struct IPerlMem* ipMS,
				 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
				 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
				 struct IPerlDir* ipD, struct IPerlSock* ipS,
				 struct IPerlProc* ipP)
		{
		    /* XXX many of the string copies here can be optimized if they're
		     * constants; they need to be allocated as common memory and just
		     * their pointers copied. */
		
		    IV i;
		    CLONE_PARAMS clone_params;
		    CLONE_PARAMS* param = &clone_params;
		
		    PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
		    /* for each stash, determine whether its objects should be cloned */
		    S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
		    PERL_SET_THX(my_perl);
		
		#  ifdef DEBUGGING
		    Poison(my_perl, 1, PerlInterpreter);
		    PL_op = Nullop;
		    PL_curcop = (COP *)Nullop;
		    PL_markstack = 0;
		    PL_scopestack = 0;
		    PL_savestack = 0;
		    PL_savestack_ix = 0;
		    PL_savestack_max = -1;
		    PL_sig_pending = 0;
		    Zero(&PL_debug_pad, 1, struct perl_debug_pad);
		#  else	/* !DEBUGGING */
		    Zero(my_perl, 1, PerlInterpreter);
		#  endif	/* DEBUGGING */
		
		    /* host pointers */
		    PL_Mem		= ipM;
		    PL_MemShared	= ipMS;
		    PL_MemParse		= ipMP;
		    PL_Env		= ipE;
		    PL_StdIO		= ipStd;
		    PL_LIO		= ipLIO;
		    PL_Dir		= ipD;
		    PL_Sock		= ipS;
		    PL_Proc		= ipP;
		#else		/* !PERL_IMPLICIT_SYS */
		    IV i;
		    CLONE_PARAMS clone_params;
		    CLONE_PARAMS* param = &clone_params;
		    PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
		    /* for each stash, determine whether its objects should be cloned */
		    S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
		    PERL_SET_THX(my_perl);
		
		#    ifdef DEBUGGING
		    Poison(my_perl, 1, PerlInterpreter);
		    PL_op = Nullop;
		    PL_curcop = (COP *)Nullop;
		    PL_markstack = 0;
		    PL_scopestack = 0;
		    PL_savestack = 0;
		    PL_savestack_ix = 0;
		    PL_savestack_max = -1;
		    PL_sig_pending = 0;
		    Zero(&PL_debug_pad, 1, struct perl_debug_pad);
		#    else	/* !DEBUGGING */
		    Zero(my_perl, 1, PerlInterpreter);
		#    endif	/* DEBUGGING */
		#endif		/* PERL_IMPLICIT_SYS */
		    param->flags = flags;
		    param->proto_perl = proto_perl;
		
		    /* arena roots */
		    PL_xnv_arenaroot	= NULL;
		    PL_xnv_root		= NULL;
		    PL_xpv_arenaroot	= NULL;
		    PL_xpv_root		= NULL;
		    PL_xpviv_arenaroot	= NULL;
		    PL_xpviv_root	= NULL;
		    PL_xpvnv_arenaroot	= NULL;
		    PL_xpvnv_root	= NULL;
		    PL_xpvcv_arenaroot	= NULL;
		    PL_xpvcv_root	= NULL;
		    PL_xpvav_arenaroot	= NULL;
		    PL_xpvav_root	= NULL;
		    PL_xpvhv_arenaroot	= NULL;
		    PL_xpvhv_root	= NULL;
		    PL_xpvmg_arenaroot	= NULL;
		    PL_xpvmg_root	= NULL;
		    PL_xpvgv_arenaroot	= NULL;
		    PL_xpvgv_root	= NULL;
		    PL_xpvlv_arenaroot	= NULL;
		    PL_xpvlv_root	= NULL;
		    PL_xpvbm_arenaroot	= NULL;
		    PL_xpvbm_root	= NULL;
		    PL_he_arenaroot	= NULL;
		    PL_he_root		= NULL;
		#if defined(USE_ITHREADS)
		    PL_pte_arenaroot	= NULL;
		    PL_pte_root		= NULL;
		#endif
		    PL_nice_chunk	= NULL;
		    PL_nice_chunk_size	= 0;
		    PL_sv_count		= 0;
		    PL_sv_objcount	= 0;
		    PL_sv_root		= Nullsv;
		    PL_sv_arenaroot	= Nullsv;
		
		    PL_debug		= proto_perl->Idebug;
		
		    PL_hash_seed	= proto_perl->Ihash_seed;
		    PL_rehash_seed	= proto_perl->Irehash_seed;
		
		#ifdef USE_REENTRANT_API
		    /* XXX: things like -Dm will segfault here in perlio, but doing
		     *  PERL_SET_CONTEXT(proto_perl);
		     * breaks too many other things
		     */
		    Perl_reentrant_init(aTHX);
		#endif
		
		    /* create SV map for pointer relocation */
		    PL_ptr_table = ptr_table_new();
		
		    /* initialize these special pointers as early as possible */
		    SvANY(&PL_sv_undef)		= NULL;
		    SvREFCNT(&PL_sv_undef)	= (~(U32)0)/2;
		    SvFLAGS(&PL_sv_undef)	= SVf_READONLY|SVt_NULL;
		    ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
		
		    SvANY(&PL_sv_no)		= new_XPVNV();
		    SvREFCNT(&PL_sv_no)		= (~(U32)0)/2;
		    SvFLAGS(&PL_sv_no)		= SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
						  |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
		    SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
		    SvCUR_set(&PL_sv_no, 0);
		    SvLEN_set(&PL_sv_no, 1);
		    SvIV_set(&PL_sv_no, 0);
		    SvNV_set(&PL_sv_no, 0);
		    ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
		
		    SvANY(&PL_sv_yes)		= new_XPVNV();
		    SvREFCNT(&PL_sv_yes)	= (~(U32)0)/2;
		    SvFLAGS(&PL_sv_yes)		= SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
						  |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
		    SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
		    SvCUR_set(&PL_sv_yes, 1);
		    SvLEN_set(&PL_sv_yes, 2);
		    SvIV_set(&PL_sv_yes, 1);
		    SvNV_set(&PL_sv_yes, 1);
		    ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
		
		    /* create (a non-shared!) shared string table */
		    PL_strtab		= newHV();
		    HvSHAREKEYS_off(PL_strtab);
		    hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
		    ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
		
		    PL_compiling = proto_perl->Icompiling;
		
		    /* These two PVs will be free'd special way so must set them same way op.c does */
		    PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
		    ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
		
		    PL_compiling.cop_file    = savesharedpv(PL_compiling.cop_file);
		    ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
		
		    ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
		    if (!specialWARN(PL_compiling.cop_warnings))
			PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
		    if (!specialCopIO(PL_compiling.cop_io))
			PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
		    PL_curcop		= (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
		
		    /* pseudo environmental stuff */
		    PL_origargc		= proto_perl->Iorigargc;
		    PL_origargv		= proto_perl->Iorigargv;
		
		    param->stashes      = newAV();  /* Setup array of objects to call clone on */
		
		#ifdef PERLIO_LAYERS
		    /* Clone PerlIO tables as soon as we can handle general xx_dup() */
		    PerlIO_clone(aTHX_ proto_perl, param);
		#endif
		
		    PL_envgv		= gv_dup(proto_perl->Ienvgv, param);
		    PL_incgv		= gv_dup(proto_perl->Iincgv, param);
		    PL_hintgv		= gv_dup(proto_perl->Ihintgv, param);
		    PL_origfilename	= SAVEPV(proto_perl->Iorigfilename);
		    PL_diehook		= sv_dup_inc(proto_perl->Idiehook, param);
		    PL_warnhook		= sv_dup_inc(proto_perl->Iwarnhook, param);
		
		    /* switches */
		    PL_minus_c		= proto_perl->Iminus_c;
		    PL_patchlevel	= sv_dup_inc(proto_perl->Ipatchlevel, param);
		    PL_localpatches	= proto_perl->Ilocalpatches;
		    PL_splitstr		= proto_perl->Isplitstr;
		    PL_preprocess	= proto_perl->Ipreprocess;
		    PL_minus_n		= proto_perl->Iminus_n;
		    PL_minus_p		= proto_perl->Iminus_p;
		    PL_minus_l		= proto_perl->Iminus_l;
		    PL_minus_a		= proto_perl->Iminus_a;
		    PL_minus_F		= proto_perl->Iminus_F;
		    PL_doswitches	= proto_perl->Idoswitches;
		    PL_dowarn		= proto_perl->Idowarn;
		    PL_doextract	= proto_perl->Idoextract;
		    PL_sawampersand	= proto_perl->Isawampersand;
		    PL_unsafe		= proto_perl->Iunsafe;
		    PL_inplace		= SAVEPV(proto_perl->Iinplace);
		    PL_e_script		= sv_dup_inc(proto_perl->Ie_script, param);
		    PL_perldb		= proto_perl->Iperldb;
		    PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
		    PL_exit_flags       = proto_perl->Iexit_flags;
		
		    /* magical thingies */
		    /* XXX time(&PL_basetime) when asked for? */
		    PL_basetime		= proto_perl->Ibasetime;
		    PL_formfeed		= sv_dup(proto_perl->Iformfeed, param);
		
		    PL_maxsysfd		= proto_perl->Imaxsysfd;
		    PL_multiline	= proto_perl->Imultiline;
		    PL_statusvalue	= proto_perl->Istatusvalue;
		#ifdef VMS
		    PL_statusvalue_vms	= proto_perl->Istatusvalue_vms;
		#endif
		    PL_encoding		= sv_dup(proto_perl->Iencoding, param);
		
		    sv_setpvn(PERL_DEBUG_PAD(0), "", 0);	/* For regex debugging. */
		    sv_setpvn(PERL_DEBUG_PAD(1), "", 0);	/* ext/re needs these */
		    sv_setpvn(PERL_DEBUG_PAD(2), "", 0);	/* even without DEBUGGING. */
		
		    /* Clone the regex array */
		    PL_regex_padav = newAV();
		    {
			const I32 len = av_len((AV*)proto_perl->Iregex_padav);
			SV** const regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
			IV i;
			av_push(PL_regex_padav,
				sv_dup_inc(regexen[0],param));
			for(i = 1; i <= len; i++) {
		            if(SvREPADTMP(regexen[i])) {
			      av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
		            } else {
			        av_push(PL_regex_padav,
		                    SvREFCNT_inc(
		                        newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
		                             SvIVX(regexen[i])), param)))
		                       ));
			    }
			}
		    }
		    PL_regex_pad = AvARRAY(PL_regex_padav);
		
		    /* shortcuts to various I/O objects */
		    PL_stdingv		= gv_dup(proto_perl->Istdingv, param);
		    PL_stderrgv		= gv_dup(proto_perl->Istderrgv, param);
		    PL_defgv		= gv_dup(proto_perl->Idefgv, param);
		    PL_argvgv		= gv_dup(proto_perl->Iargvgv, param);
		    PL_argvoutgv	= gv_dup(proto_perl->Iargvoutgv, param);
		    PL_argvout_stack	= av_dup_inc(proto_perl->Iargvout_stack, param);
		
		    /* shortcuts to regexp stuff */
		    PL_replgv		= gv_dup(proto_perl->Ireplgv, param);
		
		    /* shortcuts to misc objects */
		    PL_errgv		= gv_dup(proto_perl->Ierrgv, param);
		
		    /* shortcuts to debugging objects */
		    PL_DBgv		= gv_dup(proto_perl->IDBgv, param);
		    PL_DBline		= gv_dup(proto_perl->IDBline, param);
		    PL_DBsub		= gv_dup(proto_perl->IDBsub, param);
		    PL_DBsingle		= sv_dup(proto_perl->IDBsingle, param);
		    PL_DBtrace		= sv_dup(proto_perl->IDBtrace, param);
		    PL_DBsignal		= sv_dup(proto_perl->IDBsignal, param);
		    PL_DBassertion      = sv_dup(proto_perl->IDBassertion, param);
		    PL_lineary		= av_dup(proto_perl->Ilineary, param);
		    PL_dbargs		= av_dup(proto_perl->Idbargs, param);
		
		    /* symbol tables */
		    PL_defstash		= hv_dup_inc(proto_perl->Tdefstash, param);
		    PL_curstash		= hv_dup(proto_perl->Tcurstash, param);
		    PL_debstash		= hv_dup(proto_perl->Idebstash, param);
		    PL_globalstash	= hv_dup(proto_perl->Iglobalstash, param);
		    PL_curstname	= sv_dup_inc(proto_perl->Icurstname, param);
		
		    PL_beginav		= av_dup_inc(proto_perl->Ibeginav, param);
		    PL_beginav_save	= av_dup_inc(proto_perl->Ibeginav_save, param);
		    PL_checkav_save	= av_dup_inc(proto_perl->Icheckav_save, param);
		    PL_endav		= av_dup_inc(proto_perl->Iendav, param);
		    PL_checkav		= av_dup_inc(proto_perl->Icheckav, param);
		    PL_initav		= av_dup_inc(proto_perl->Iinitav, param);
		
		    PL_sub_generation	= proto_perl->Isub_generation;
		
		    /* funky return mechanisms */
		    PL_forkprocess	= proto_perl->Iforkprocess;
		
		    /* subprocess state */
		    PL_fdpid		= av_dup_inc(proto_perl->Ifdpid, param);
		
		    /* internal state */
		    PL_tainting		= proto_perl->Itainting;
		    PL_taint_warn       = proto_perl->Itaint_warn;
		    PL_maxo		= proto_perl->Imaxo;
		    if (proto_perl->Iop_mask)
			PL_op_mask	= SAVEPVN(proto_perl->Iop_mask, PL_maxo);
		    else
			PL_op_mask 	= Nullch;
		    /* PL_asserting        = proto_perl->Iasserting; */
		
		    /* current interpreter roots */
		    PL_main_cv		= cv_dup_inc(proto_perl->Imain_cv, param);
		    PL_main_root	= OpREFCNT_inc(proto_perl->Imain_root);
		    PL_main_start	= proto_perl->Imain_start;
		    PL_eval_root	= proto_perl->Ieval_root;
		    PL_eval_start	= proto_perl->Ieval_start;
		
		    /* runtime control stuff */
		    PL_curcopdb		= (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
		    PL_copline		= proto_perl->Icopline;
		
		    PL_filemode		= proto_perl->Ifilemode;
		    PL_lastfd		= proto_perl->Ilastfd;
		    PL_oldname		= proto_perl->Ioldname;		/* XXX not quite right */
		    PL_Argv		= NULL;
		    PL_Cmd		= Nullch;
		    PL_gensym		= proto_perl->Igensym;
		    PL_preambled	= proto_perl->Ipreambled;
		    PL_preambleav	= av_dup_inc(proto_perl->Ipreambleav, param);
		    PL_laststatval	= proto_perl->Ilaststatval;
		    PL_laststype	= proto_perl->Ilaststype;
		    PL_mess_sv		= Nullsv;
		
		    PL_ors_sv		= sv_dup_inc(proto_perl->Iors_sv, param);
		
		    /* interpreter atexit processing */
		    PL_exitlistlen	= proto_perl->Iexitlistlen;
		    if (PL_exitlistlen) {
			New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
			Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
		    }
		    else
			PL_exitlist	= (PerlExitListEntry*)NULL;
		    PL_modglobal	= hv_dup_inc(proto_perl->Imodglobal, param);
		    PL_custom_op_names  = hv_dup_inc(proto_perl->Icustom_op_names,param);
		    PL_custom_op_descs  = hv_dup_inc(proto_perl->Icustom_op_descs,param);
		
		    PL_profiledata	= NULL;
		    PL_rsfp		= fp_dup(proto_perl->Irsfp, '<', param);
		    /* PL_rsfp_filters entries have fake IoDIRP() */
		    PL_rsfp_filters	= av_dup_inc(proto_perl->Irsfp_filters, param);
		
		    PL_compcv			= cv_dup(proto_perl->Icompcv, param);
		
		    PAD_CLONE_VARS(proto_perl, param);
		
		#ifdef HAVE_INTERP_INTERN
		    sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
		#endif
		
		    /* more statics moved here */
		    PL_generation	= proto_perl->Igeneration;
		    PL_DBcv		= cv_dup(proto_perl->IDBcv, param);
		
		    PL_in_clean_objs	= proto_perl->Iin_clean_objs;
		    PL_in_clean_all	= proto_perl->Iin_clean_all;
		
		    PL_uid		= proto_perl->Iuid;
		    PL_euid		= proto_perl->Ieuid;
		    PL_gid		= proto_perl->Igid;
		    PL_egid		= proto_perl->Iegid;
		    PL_nomemok		= proto_perl->Inomemok;
		    PL_an		= proto_perl->Ian;
		    PL_evalseq		= proto_perl->Ievalseq;
		    PL_origenviron	= proto_perl->Iorigenviron;	/* XXX not quite right */
		    PL_origalen		= proto_perl->Iorigalen;
		    PL_pidstatus	= newHV();			/* XXX flag for cloning? */
		    PL_osname		= SAVEPV(proto_perl->Iosname);
		    PL_sighandlerp	= proto_perl->Isighandlerp;
		
		    PL_runops		= proto_perl->Irunops;
		
		    Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
		
		#ifdef CSH
		    PL_cshlen		= proto_perl->Icshlen;
		    PL_cshname		= proto_perl->Icshname; /* XXX never deallocated */
		#endif
		
		    PL_lex_state	= proto_perl->Ilex_state;
		    PL_lex_defer	= proto_perl->Ilex_defer;
		    PL_lex_expect	= proto_perl->Ilex_expect;
		    PL_lex_formbrack	= proto_perl->Ilex_formbrack;
		    PL_lex_dojoin	= proto_perl->Ilex_dojoin;
		    PL_lex_starts	= proto_perl->Ilex_starts;
		    PL_lex_stuff	= sv_dup_inc(proto_perl->Ilex_stuff, param);
		    PL_lex_repl		= sv_dup_inc(proto_perl->Ilex_repl, param);
		    PL_lex_op		= proto_perl->Ilex_op;
		    PL_lex_inpat	= proto_perl->Ilex_inpat;
		    PL_lex_inwhat	= proto_perl->Ilex_inwhat;
		    PL_lex_brackets	= proto_perl->Ilex_brackets;
		    i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
		    PL_lex_brackstack	= SAVEPVN(proto_perl->Ilex_brackstack,i);
		    PL_lex_casemods	= proto_perl->Ilex_casemods;
		    i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
		    PL_lex_casestack	= SAVEPVN(proto_perl->Ilex_casestack,i);
		
		    Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
		    Copy(proto_perl->Inexttype, PL_nexttype, 5,	I32);
		    PL_nexttoke		= proto_perl->Inexttoke;
		
		    /* XXX This is probably masking the deeper issue of why
		     * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
		     * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
		     * (A little debugging with a watchpoint on it may help.)
		     */
		    if (SvANY(proto_perl->Ilinestr)) {
			PL_linestr		= sv_dup_inc(proto_perl->Ilinestr, param);
			i = proto_perl->Ibufptr - SvPVX_const(proto_perl->Ilinestr);
			PL_bufptr		= SvPVX(PL_linestr) + (i < 0 ? 0 : i);
			i = proto_perl->Ioldbufptr - SvPVX_const(proto_perl->Ilinestr);
			PL_oldbufptr	= SvPVX(PL_linestr) + (i < 0 ? 0 : i);
			i = proto_perl->Ioldoldbufptr - SvPVX_const(proto_perl->Ilinestr);
			PL_oldoldbufptr	= SvPVX(PL_linestr) + (i < 0 ? 0 : i);
			i = proto_perl->Ilinestart - SvPVX_const(proto_perl->Ilinestr);
			PL_linestart	= SvPVX(PL_linestr) + (i < 0 ? 0 : i);
		    }
		    else {
		        PL_linestr = NEWSV(65,79);
		        sv_upgrade(PL_linestr,SVt_PVIV);
		        sv_setpvn(PL_linestr,"",0);
			PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
		    }
		    PL_bufend		= SvPVX(PL_linestr) + SvCUR(PL_linestr);
		    PL_pending_ident	= proto_perl->Ipending_ident;
		    PL_sublex_info	= proto_perl->Isublex_info;	/* XXX not quite right */
		
		    PL_expect		= proto_perl->Iexpect;
		
		    PL_multi_start	= proto_perl->Imulti_start;
		    PL_multi_end	= proto_perl->Imulti_end;
		    PL_multi_open	= proto_perl->Imulti_open;
		    PL_multi_close	= proto_perl->Imulti_close;
		
		    PL_error_count	= proto_perl->Ierror_count;
		    PL_subline		= proto_perl->Isubline;
		    PL_subname		= sv_dup_inc(proto_perl->Isubname, param);
		
		    /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
		    if (SvANY(proto_perl->Ilinestr)) {
			i = proto_perl->Ilast_uni - SvPVX_const(proto_perl->Ilinestr);
			PL_last_uni		= SvPVX(PL_linestr) + (i < 0 ? 0 : i);
			i = proto_perl->Ilast_lop - SvPVX_const(proto_perl->Ilinestr);
			PL_last_lop		= SvPVX(PL_linestr) + (i < 0 ? 0 : i);
			PL_last_lop_op	= proto_perl->Ilast_lop_op;
		    }
		    else {
			PL_last_uni	= SvPVX(PL_linestr);
			PL_last_lop	= SvPVX(PL_linestr);
			PL_last_lop_op	= 0;
		    }
		    PL_in_my		= proto_perl->Iin_my;
		    PL_in_my_stash	= hv_dup(proto_perl->Iin_my_stash, param);
		#ifdef FCRYPT
		    PL_cryptseen	= proto_perl->Icryptseen;
		#endif
		
		    PL_hints		= proto_perl->Ihints;
		
		    PL_amagic_generation	= proto_perl->Iamagic_generation;
		
		#ifdef USE_LOCALE_COLLATE
		    PL_collation_ix	= proto_perl->Icollation_ix;
		    PL_collation_name	= SAVEPV(proto_perl->Icollation_name);
		    PL_collation_standard	= proto_perl->Icollation_standard;
		    PL_collxfrm_base	= proto_perl->Icollxfrm_base;
		    PL_collxfrm_mult	= proto_perl->Icollxfrm_mult;
		#endif /* USE_LOCALE_COLLATE */
		
		#ifdef USE_LOCALE_NUMERIC
		    PL_numeric_name	= SAVEPV(proto_perl->Inumeric_name);
		    PL_numeric_standard	= proto_perl->Inumeric_standard;
		    PL_numeric_local	= proto_perl->Inumeric_local;
		    PL_numeric_radix_sv	= sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
		#endif /* !USE_LOCALE_NUMERIC */
		
		    /* utf8 character classes */
		    PL_utf8_alnum	= sv_dup_inc(proto_perl->Iutf8_alnum, param);
		    PL_utf8_alnumc	= sv_dup_inc(proto_perl->Iutf8_alnumc, param);
		    PL_utf8_ascii	= sv_dup_inc(proto_perl->Iutf8_ascii, param);
		    PL_utf8_alpha	= sv_dup_inc(proto_perl->Iutf8_alpha, param);
		    PL_utf8_space	= sv_dup_inc(proto_perl->Iutf8_space, param);
		    PL_utf8_cntrl	= sv_dup_inc(proto_perl->Iutf8_cntrl, param);
		    PL_utf8_graph	= sv_dup_inc(proto_perl->Iutf8_graph, param);
		    PL_utf8_digit	= sv_dup_inc(proto_perl->Iutf8_digit, param);
		    PL_utf8_upper	= sv_dup_inc(proto_perl->Iutf8_upper, param);
		    PL_utf8_lower	= sv_dup_inc(proto_perl->Iutf8_lower, param);
		    PL_utf8_print	= sv_dup_inc(proto_perl->Iutf8_print, param);
		    PL_utf8_punct	= sv_dup_inc(proto_perl->Iutf8_punct, param);
		    PL_utf8_xdigit	= sv_dup_inc(proto_perl->Iutf8_xdigit, param);
		    PL_utf8_mark	= sv_dup_inc(proto_perl->Iutf8_mark, param);
		    PL_utf8_toupper	= sv_dup_inc(proto_perl->Iutf8_toupper, param);
		    PL_utf8_totitle	= sv_dup_inc(proto_perl->Iutf8_totitle, param);
		    PL_utf8_tolower	= sv_dup_inc(proto_perl->Iutf8_tolower, param);
		    PL_utf8_tofold	= sv_dup_inc(proto_perl->Iutf8_tofold, param);
		    PL_utf8_idstart	= sv_dup_inc(proto_perl->Iutf8_idstart, param);
		    PL_utf8_idcont	= sv_dup_inc(proto_perl->Iutf8_idcont, param);
		
		    /* Did the locale setup indicate UTF-8? */
		    PL_utf8locale	= proto_perl->Iutf8locale;
		    /* Unicode features (see perlrun/-C) */
		    PL_unicode		= proto_perl->Iunicode;
		
		    /* Pre-5.8 signals control */
		    PL_signals		= proto_perl->Isignals;
		
		    /* times() ticks per second */
		    PL_clocktick	= proto_perl->Iclocktick;
		
		    /* Recursion stopper for PerlIO_find_layer */
		    PL_in_load_module	= proto_perl->Iin_load_module;
		
		    /* sort() routine */
		    PL_sort_RealCmp	= proto_perl->Isort_RealCmp;
		
		    /* Not really needed/useful since the reenrant_retint is "volatile",
		     * but do it for consistency's sake. */
		    PL_reentrant_retint	= proto_perl->Ireentrant_retint;
		
		    /* Hooks to shared SVs and locks. */
		    PL_sharehook	= proto_perl->Isharehook;
		    PL_lockhook		= proto_perl->Ilockhook;
		    PL_unlockhook	= proto_perl->Iunlockhook;
		    PL_threadhook	= proto_perl->Ithreadhook;
		
		    PL_runops_std	= proto_perl->Irunops_std;
		    PL_runops_dbg	= proto_perl->Irunops_dbg;
		
		#ifdef THREADS_HAVE_PIDS
		    PL_ppid		= proto_perl->Ippid;
		#endif
		
		    /* swatch cache */
		    PL_last_swash_hv	= Nullhv;	/* reinits on demand */
		    PL_last_swash_klen	= 0;
		    PL_last_swash_key[0]= '\0';
		    PL_last_swash_tmps	= (U8*)NULL;
		    PL_last_swash_slen	= 0;
		
		    PL_glob_index	= proto_perl->Iglob_index;
		    PL_srand_called	= proto_perl->Isrand_called;
		    PL_uudmap['M']	= 0;		/* reinits on demand */
		    PL_bitcount		= Nullch;	/* reinits on demand */
		
		    if (proto_perl->Ipsig_pend) {
			Newz(0, PL_psig_pend, SIG_SIZE, int);
		    }
		    else {
			PL_psig_pend	= (int*)NULL;
		    }
		
		    if (proto_perl->Ipsig_ptr) {
			Newz(0, PL_psig_ptr,  SIG_SIZE, SV*);
			Newz(0, PL_psig_name, SIG_SIZE, SV*);
			for (i = 1; i < SIG_SIZE; i++) {
			    PL_psig_ptr[i]  = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
			    PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
			}
		    }
		    else {
			PL_psig_ptr	= (SV**)NULL;
			PL_psig_name	= (SV**)NULL;
		    }
		
		    /* thrdvar.h stuff */
		
		    if (flags & CLONEf_COPY_STACKS) {
			/* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
			PL_tmps_ix		= proto_perl->Ttmps_ix;
			PL_tmps_max		= proto_perl->Ttmps_max;
			PL_tmps_floor		= proto_perl->Ttmps_floor;
			Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
			i = 0;
			while (i <= PL_tmps_ix) {
			    PL_tmps_stack[i]	= sv_dup_inc(proto_perl->Ttmps_stack[i], param);
			    ++i;
			}
		
			/* next PUSHMARK() sets *(PL_markstack_ptr+1) */
			i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
			Newz(54, PL_markstack, i, I32);
			PL_markstack_max	= PL_markstack + (proto_perl->Tmarkstack_max
								  - proto_perl->Tmarkstack);
			PL_markstack_ptr	= PL_markstack + (proto_perl->Tmarkstack_ptr
								  - proto_perl->Tmarkstack);
			Copy(proto_perl->Tmarkstack, PL_markstack,
			     PL_markstack_ptr - PL_markstack + 1, I32);
		
			/* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
			 * NOTE: unlike the others! */
			PL_scopestack_ix	= proto_perl->Tscopestack_ix;
			PL_scopestack_max	= proto_perl->Tscopestack_max;
			Newz(54, PL_scopestack, PL_scopestack_max, I32);
			Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
		
			/* NOTE: si_dup() looks at PL_markstack */
			PL_curstackinfo		= si_dup(proto_perl->Tcurstackinfo, param);
		
			/* PL_curstack		= PL_curstackinfo->si_stack; */
			PL_curstack		= av_dup(proto_perl->Tcurstack, param);
			PL_mainstack		= av_dup(proto_perl->Tmainstack, param);
		
			/* next PUSHs() etc. set *(PL_stack_sp+1) */
			PL_stack_base		= AvARRAY(PL_curstack);
			PL_stack_sp		= PL_stack_base + (proto_perl->Tstack_sp
								   - proto_perl->Tstack_base);
			PL_stack_max		= PL_stack_base + AvMAX(PL_curstack);
		
			/* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
			 * NOTE: unlike the others! */
			PL_savestack_ix		= proto_perl->Tsavestack_ix;
			PL_savestack_max	= proto_perl->Tsavestack_max;
			/*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
			PL_savestack		= ss_dup(proto_perl, param);
		    }
		    else {
			init_stacks();
			ENTER;			/* perl_destruct() wants to LEAVE; */
		    }
		
		    PL_start_env	= proto_perl->Tstart_env;	/* XXXXXX */
		    PL_top_env		= &PL_start_env;
		
		    PL_op		= proto_perl->Top;
		
		    PL_Sv		= Nullsv;
		    PL_Xpv		= (XPV*)NULL;
		    PL_na		= proto_perl->Tna;
		
		    PL_statbuf		= proto_perl->Tstatbuf;
		    PL_statcache	= proto_perl->Tstatcache;
		    PL_statgv		= gv_dup(proto_perl->Tstatgv, param);
		    PL_statname		= sv_dup_inc(proto_perl->Tstatname, param);
		#ifdef HAS_TIMES
		    PL_timesbuf		= proto_perl->Ttimesbuf;
		#endif
		
		    PL_tainted		= proto_perl->Ttainted;
		    PL_curpm		= proto_perl->Tcurpm;	/* XXX No PMOP ref count */
		    PL_rs		= sv_dup_inc(proto_perl->Trs, param);
		    PL_last_in_gv	= gv_dup(proto_perl->Tlast_in_gv, param);
		    PL_ofs_sv		= sv_dup_inc(proto_perl->Tofs_sv, param);
		    PL_defoutgv		= gv_dup_inc(proto_perl->Tdefoutgv, param);
		    PL_chopset		= proto_perl->Tchopset;	/* XXX never deallocated */
		    PL_toptarget	= sv_dup_inc(proto_perl->Ttoptarget, param);
		    PL_bodytarget	= sv_dup_inc(proto_perl->Tbodytarget, param);
		    PL_formtarget	= sv_dup(proto_perl->Tformtarget, param);
		
		    PL_restartop	= proto_perl->Trestartop;
		    PL_in_eval		= proto_perl->Tin_eval;
		    PL_delaymagic	= proto_perl->Tdelaymagic;
		    PL_dirty		= proto_perl->Tdirty;
		    PL_localizing	= proto_perl->Tlocalizing;
		
		    PL_errors		= sv_dup_inc(proto_perl->Terrors, param);
		    PL_hv_fetch_ent_mh	= Nullhe;
		    PL_modcount		= proto_perl->Tmodcount;
		    PL_lastgotoprobe	= Nullop;
		    PL_dumpindent	= proto_perl->Tdumpindent;
		
		    PL_sortcop		= (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
		    PL_sortstash	= hv_dup(proto_perl->Tsortstash, param);
		    PL_firstgv		= gv_dup(proto_perl->Tfirstgv, param);
		    PL_secondgv		= gv_dup(proto_perl->Tsecondgv, param);
		    PL_sortcxix		= proto_perl->Tsortcxix;
		    PL_efloatbuf	= Nullch;		/* reinits on demand */
		    PL_efloatsize	= 0;			/* reinits on demand */
		
		    /* regex stuff */
		
		    PL_screamfirst	= NULL;
		    PL_screamnext	= NULL;
		    PL_maxscream	= -1;			/* reinits on demand */
		    PL_lastscream	= Nullsv;
		
		    PL_watchaddr	= NULL;
		    PL_watchok		= Nullch;
		
		    PL_regdummy		= proto_perl->Tregdummy;
		    PL_regprecomp	= Nullch;
		    PL_regnpar		= 0;
		    PL_regsize		= 0;
		    PL_colorset		= 0;		/* reinits PL_colors[] */
		    /*PL_colors[6]	= {0,0,0,0,0,0};*/
		    PL_reginput		= Nullch;
		    PL_regbol		= Nullch;
		    PL_regeol		= Nullch;
		    PL_regstartp	= (I32*)NULL;
		    PL_regendp		= (I32*)NULL;
		    PL_reglastparen	= (U32*)NULL;
		    PL_reglastcloseparen	= (U32*)NULL;
		    PL_regtill		= Nullch;
		    PL_reg_start_tmp	= (char**)NULL;
		    PL_reg_start_tmpl	= 0;
		    PL_regdata		= (struct reg_data*)NULL;
		    PL_bostr		= Nullch;
		    PL_reg_flags	= 0;
		    PL_reg_eval_set	= 0;
		    PL_regnarrate	= 0;
		    PL_regprogram	= (regnode*)NULL;
		    PL_regindent	= 0;
		    PL_regcc		= (CURCUR*)NULL;
		    PL_reg_call_cc	= (struct re_cc_state*)NULL;
		    PL_reg_re		= (regexp*)NULL;
		    PL_reg_ganch	= Nullch;
		    PL_reg_sv		= Nullsv;
		    PL_reg_match_utf8	= FALSE;
		    PL_reg_magic	= (MAGIC*)NULL;
		    PL_reg_oldpos	= 0;
		    PL_reg_oldcurpm	= (PMOP*)NULL;
		    PL_reg_curpm	= (PMOP*)NULL;
		    PL_reg_oldsaved	= Nullch;
		    PL_reg_oldsavedlen	= 0;
		#ifdef PERL_OLD_COPY_ON_WRITE
		    PL_nrs		= Nullsv;
		#endif
		    PL_reg_maxiter	= 0;
		    PL_reg_leftiter	= 0;
		    PL_reg_poscache	= Nullch;
		    PL_reg_poscache_size= 0;
		
		    /* RE engine - function pointers */
		    PL_regcompp		= proto_perl->Tregcompp;
		    PL_regexecp		= proto_perl->Tregexecp;
		    PL_regint_start	= proto_perl->Tregint_start;
		    PL_regint_string	= proto_perl->Tregint_string;
		    PL_regfree		= proto_perl->Tregfree;
		
		    PL_reginterp_cnt	= 0;
		    PL_reg_starttry	= 0;
		
		    /* Pluggable optimizer */
		    PL_peepp		= proto_perl->Tpeepp;
		
		    PL_stashcache       = newHV();
		
		    if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
		        ptr_table_free(PL_ptr_table);
		        PL_ptr_table = NULL;
		    }
		
		    /* Call the ->CLONE method, if it exists, for each of the stashes
		       identified by sv_dup() above.
		    */
		    while(av_len(param->stashes) != -1) {
			HV* const stash = (HV*) av_shift(param->stashes);
			GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
			if (cloner && GvCV(cloner)) {
			    dSP;
			    ENTER;
			    SAVETMPS;
			    PUSHMARK(SP);
			    XPUSHs(sv_2mortal(newSVhek(HvNAME_HEK(stash))));
			    PUTBACK;
			    call_sv((SV*)GvCV(cloner), G_DISCARD);
			    FREETMPS;
			    LEAVE;
			}
		    }
		
		    SvREFCNT_dec(param->stashes);
		
		    /* orphaned? eg threads->new inside BEGIN or use */
		    if (PL_compcv && ! SvREFCNT(PL_compcv)) {
			(void)SvREFCNT_inc(PL_compcv);
			SAVEFREESV(PL_compcv);
		    }
		
		    return my_perl;
		}
		
		#endif /* USE_ITHREADS */
		
		/*
		=head1 Unicode Support
		
		=for apidoc sv_recode_to_utf8
		
		The encoding is assumed to be an Encode object, on entry the PV
		of the sv is assumed to be octets in that encoding, and the sv
		will be converted into Unicode (and UTF-8).
		
		If the sv already is UTF-8 (or if it is not POK), or if the encoding
		is not a reference, nothing is done to the sv.  If the encoding is not
		an C<Encode::XS> Encoding object, bad things will happen.
		(See F<lib/encoding.pm> and L<Encode>).
		
		The PV of the sv is returned.
		
		=cut */
		
		char *
		Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
        1251    {
		    dVAR;
        1251        if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
        1234    	SV *uni;
        1234    	STRLEN len;
        1234    	const char *s;
        1234    	dSP;
        1234    	ENTER;
        1234    	SAVETMPS;
        1234    	save_re_context();
        1234    	PUSHMARK(sp);
        1234    	EXTEND(SP, 3);
        1234    	XPUSHs(encoding);
        1234    	XPUSHs(sv);
		/*
		  NI-S 2002/07/09
		  Passing sv_yes is wrong - it needs to be or'ed set of constants
		  for Encode::XS, while UTf-8 decode (currently) assumes a true value means
		  remove converted chars from source.
		
		  Both will default the value - let them.
		
			XPUSHs(&PL_sv_yes);
		*/
        1234    	PUTBACK;
        1234    	call_method("decode", G_SCALAR);
        1232    	SPAGAIN;
        1232    	uni = POPs;
        1232    	PUTBACK;
        1232    	s = SvPV_const(uni, len);
        1232    	if (s != SvPVX_const(sv)) {
        1232    	    SvGROW(sv, len + 1);
        1232    	    Move(s, SvPVX(sv), len + 1, char);
        1232    	    SvCUR_set(sv, len);
			}
        1232    	FREETMPS;
        1232    	LEAVE;
        1232    	SvUTF8_on(sv);
        1232    	return SvPVX(sv);
		    }
          17        return SvPOKp(sv) ? SvPVX(sv) : NULL;
		}
		
		/*
		=for apidoc sv_cat_decode
		
		The encoding is assumed to be an Encode object, the PV of the ssv is
		assumed to be octets in that encoding and decoding the input starts
		from the position which (PV + *offset) pointed to.  The dsv will be
		concatenated the decoded UTF-8 string from ssv.  Decoding will terminate
		when the string tstr appears in decoding output or the input ends on
		the PV of the ssv. The value which the offset points will be modified
		to the last input position on the ssv.
		
		Returns TRUE if the terminator was found, else returns FALSE.
		
		=cut */
		
		bool
		Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
				   SV *ssv, int *offset, char *tstr, int tlen)
         298    {
		    dVAR;
         298        bool ret = FALSE;
         298        if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
         298    	SV *offsv;
         298    	dSP;
         298    	ENTER;
         298    	SAVETMPS;
         298    	save_re_context();
         298    	PUSHMARK(sp);
         298    	EXTEND(SP, 6);
         298    	XPUSHs(encoding);
         298    	XPUSHs(dsv);
         298    	XPUSHs(ssv);
         298    	XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
         298    	XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
         298    	PUTBACK;
         298    	call_method("cat_decode", G_SCALAR);
         298    	SPAGAIN;
         298    	ret = SvTRUE(TOPs);
         298    	*offset = SvIV(offsv);
         298    	PUTBACK;
         298    	FREETMPS;
         298    	LEAVE;
		    }
		    else
      ######            Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
         298        return ret;
		}
		
		/*
		 * Local variables:
		 * c-indentation-style: bsd
		 * c-basic-offset: 4
		 * indent-tabs-mode: t
		 * End:
		 *
		 * ex: set ts=8 sts=4 sw=4 noet:
		 */

