		INCLUDE	AINCLUDE:IncDirs.i
		INCLUDE	exec/libraries.i
		INCLUDE	exec/tasks.i
		INCLUDE	exec/memory.i
		INCLUDE	lvo.i

		SECTION	"StartCode",CODE
		XREF	@start

* Could not do that in main program, because of C language limitations
*
* This is not required (e.g. You can compile the main program without that
* start header), but may increase stability for files with really deep
* recursion (I have a file with up to 23 link levels).

STACKSIZE	EQU	10240

		MOVEA.L	4.W,A6
		MOVEQ	#37,D0
		CMP.L	LIB_VERSION(A6),D0 * we need OS2.0 to use StackSwap
		BHI.B	start
		MOVE.L	#STACKSIZE,D2
		SUBA.L	A1,A1
		JSR	_LVOFindTask(A6)
		MOVE.L	D0,A0
		MOVE.L	TC_SPUPPER(A0),D0  * calculate current size
		SUB.L	TC_SPLOWER(A0),D0
		CMP.L	D2,D0
		BGE.B	start		   * enough ? then do nothing
		MOVE.L	D2,D0
		MOVEQ	#MEMF_PUBLIC,D1
		JSR	_LVOAllocMem(A6)   * get memory
		MOVE.L	D0,D3
		BEQ.B	start		   * failed ? call without StackSwap
		LEA	-StackSwapStruct_SIZEOF(A7),A7
		MOVE.L	A7,A4
		MOVE.L	D0,stk_Lower(A4)   * initialize StackSwapStruct
		ADD.L	D2,D0
		MOVE.L	D0,stk_Upper(A4)
		MOVE.L	D0,stk_Pointer(A4)
		MOVEA.L	A4,A0
		JSR	_LVOStackSwap(A6)  * swap stack
		JSR	@start(PC)	   * call main function
		MOVE.L	D0,D6
		MOVEA.L	A4,A0
		JSR	_LVOStackSwap(A6)  * swap stack back
		LEA	StackSwapStruct_SIZEOF(A7),A7
		MOVEA.L	D3,A1
		MOVE.L	D2,D0
		JSR	_LVOFreeMem(A6)	   * free stack
		MOVE.L	D6,D0		   * set return code
		RTS
start		JMP	@start(PC)
		END
