extern long gemdos();
extern long detect_nf();
extern long nf_id();
extern nf_call();

#define Super(a)        (long)gemdos(0x20, a)
#define Cconws(a)       gemdos(0x09, a)

asm {
        gemdos:
                move.l  (A7)+,D3      /* Backup return address and remove it from the stack */
                trap    #1            /* Call GEMDOS */
                move.l  D3,-(A7)      /* Restore return address */
                rts

        nf_version:
                dc.b  "NF_VERSION\0\0"

        /* NatFeats test. Needs to be called from Supervisor mode, otherwise exception handler change bombs */
        detect_nf:
                clr.l   D0              /* assume no NatFeats available */
                move.l  A7, A1
                move.l  0x10, A0        /* illegal vector */
                lea     fail_nf(PC),A0
                move.l  A0, 0x10
                pea     nf_version
                suba.l  #4, A7
                dc.w    0x7300          /* Jump to NATFEAT_ID */
                tst.l   D0
                beq.s   fail_nf
                moveq   #1, D0          /* NatFeats detected */
        fail_nf:
                move.l  A1, A7
                move.l  A0, 0x10        /* illegal vector */

                rts

        /* map native feature to its ID */
        nf_id:
                dc.w   0x7300
                rts

        /* call native feature by its ID */
        nf_call:
                dc.w   0x7301
                rts
}

int main() {
	long id, sup;


        sup = Super(0);
	if (!detect_nf()) {
		Cconws("Native Features initialization failed!\r\n");
	} else {
                if (id = nf_id("NF_SHUTDOWN")) {
                        nf_call(id | 0);
                } else {
                        Cconws("NF_SHUTDOWN unavailable!\r\n");
                }
        }
	Super(sup);
        return 0;
}
