#ifndef __STRING_H
#define __STRING_H 1

#ifndef __SIZE_T
#define __SIZE_T 1
typedef unsigned long size_t;
#endif

#undef NULL
#define NULL ((void *)0)

void *memcpy(void *,const void *,size_t);
void *memmove(void *,const void *,size_t);
char *strcpy(char *,const char *);
char *strncpy(char *,const char *,size_t);
char *strcat(char *,const char *);
char *strncat(char *,const char *,size_t);
int memcmp(const void *,const void *,size_t);
int strcmp(const char *,const char *);
int strncmp(const char *,const char *,size_t);
void *memchr(const void *,int,size_t);
char *strchr(const char *,int);
size_t strcspn(const char *,const char *);
char *strpbrk(const char *,const char *);
char *strrchr(const char *,int);
size_t strspn(const char *,const char *);
char *strstr(const char *,const char *);
void *memset(void *,int,size_t);
size_t strlen(const char *);
char *strtok(char *,const char *);
char *strerror(int);

#ifdef __INLINE_ALL
#define __INLINE_MEMCHR
#define __INLINE_MEMCMP
#define __INLINE_STRCAT
#define __INLINE_STRCHR
#define __INLINE_STRCSPN
#define __INLINE_STRERROR
#define __INLINE_STRNCAT
#define __INLINE_STRPBRK
#define __INLINE_STRRCHR
#define __INLINE_STRSPN
#define __INLINE_STRSTR
#define __INLINE_STRTOL
#endif

#ifdef __INLINE_MEMCHR
#pragma only-inline on
#include "vbccm68k:libsrc/string/memchr.c"
#pragma only-inline off
#endif
#ifdef __INLINE_MEMCMP
#pragma only-inline on
#include "vbccm68k:libsrc/string/memcmp.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRCAT
#pragma only-inline on
#include "vbccm68k:libsrc/string/strcat.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRCHR
#pragma only-inline on
#include "vbccm68k:libsrc/string/strchr.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRCSPN
#pragma only-inline on
#include "vbccm68k:libsrc/string/strcspn.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRERROR
#pragma only-inline on
#include "vbccm68k:libsrc/string/strerror.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRNCAT
#pragma only-inline on
#include "vbccm68k:libsrc/string/strncat.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRPBRK
#pragma only-inline on
#include "vbccm68k:libsrc/string/strpbrk.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRRCHR
#pragma only-inline on
#include "vbccm68k:libsrc/string/strrchr.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRSPN
#pragma only-inline on
#include "vbccm68k:libsrc/string/strspn.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRSTR
#pragma only-inline on
#include "vbccm68k:libsrc/string/strstr.c"
#pragma only-inline off
#endif
#ifdef __INLINE_STRTOK
#pragma only-inline on
#include "vbccm68k:libsrc/string/strtok.c"
#pragma only-inline off
#endif

void *memcpy(__reg("a0") void *, __reg("a1") const void *,
             __reg("d2") size_t) =
        "\tcmp.l\t#16,d2\n"
        "\tblo.b\t*+60\n"
        "\tmove.l\ta0,d0\n"
        "\tmove.l\ta1,d1\n"
        "\tand.b\t#1,d0\n"
        "\tand.b\t#1,d1\n"
        "\tcmp.b\td0,d1\n"
        "\tbne.b\t*+26\n"
        "\tmove.l\ta0,d0\n"
        "\ttst.b\td1\n"
        "\tbeq.b\t*+4\n"
        "\tmove.b\t(a1)+,(a0)+\n"
        "\tsubq.l\t#1,d2\n"
        ";=barrier\n"
        "\tmoveq\t#3,d1\n"
        "\tand.l\td2,d1\n"
        "\tsub.l\td1,d2\n"
        ";=barrier\n"
        "\tmove.l\t(a1)+,(a0)+\n"
        "\tsubq.l\t#4,d2\n"
        "\tbne.b\t*-6\n"
        "\tmove.w\td1,d2\n"
        "\tbra.b\t*+20\n"
        ";=barrier\n"
        "\tcmp.l\t#$10000,d2\n"
        "\tblo.b\t*+10\n"
        "\tmove.l\ta0,d0\n"
        ";=barrier\n"
        "\tmove.b\t(a1)+,(a0)+\n"
        "\tsubq.l\t#1,d2\n"
        "\tbne.b\t*-6\n"
        "\tbra.b\t*+12\n"
        ";=barrier\n"
        "\tmove.l\ta0,d0\n"
        ";=barrier\n"
        "\tsubq.w\t#1,d2\n"
        "\tblo.b\t*+6\n"
        ";=barrier\n"
        "\tmove.b\t(a1)+,(a0)+\n"
        "\tdbf\td2,*-4\n"
        ";=barrier";

void *__memcpy_desc(__reg("a0") void *, __reg("a1") const void *,
                    __reg("d2") size_t) =
        "\tcmp.l\t#16,d2\n"
        "\tblo.b\t*+56\n"
        "\tmove.l\ta0,d0\n"
        "\tmove.l\ta1,d1\n"
        "\tand.b\t#1,d0\n"
        "\tand.b\t#1,d1\n"
        "\tcmp.b\td0,d1\n"
        "\tbne.b\t*+24\n"
        "\ttst.b\td1\n"
        "\tbeq.b\t*+4\n"
        "\tmove.b\t-(a1),-(a0)\n"
        "\tsubq.l\t#1,d2\n"
        ";=barrier\n"
        "\tmoveq\t#3,d1\n"
        "\tand.l\td2,d1\n"
        "\tsub.l\td1,d2\n"
        ";=barrier\n"
        "\tmove.l\t-(a1),-(a0)\n"
        "\tsubq.l\t#4,d2\n"
        "\tbne.b\t*-6\n"
        "\tmove.w\td1,d2\n"
        "\tbra.b\t*+16\n"
        ";=barrier\n"
        "\tcmp.l\t#$10000,d2\n"
        "\tblo.b\t*+8\n"
        ";=barrier\n"
        "\tmove.b\t-(a1),-(a0)\n"
        "\tsubq.l\t#1,d2\n"
        "\tbne.b\t*-6\n"
        "\tbra.b\t*+10\n"
        ";=barrier\n"
        "\tsubq.w\t#1,d2\n"
        "\tblo.b\t*+6\n"
        ";=barrier\n"
        "\tmove.b\t-(a1),-(a0)\n"
        "\tdbf\td2,*-4\n"
        ";=barrier\n"
        "\tmove.l\ta0,d0";

#define memmove(d,s,n) ((d)<=(s) ? memcpy(d,s,n) : \
                                   __memcpy_desc((char *)(d)+(n),(char *)(s)+(n),n))

void *memset(__reg("a0") void *, __reg("d0") int, __reg("d2") size_t) =
        "\tmove.l\ta0,a1\n"
        "\tcmp.l\t#16,d2\n"
        "\tblo.b\t*+38\n"
        "\tmove.l\ta0,d1\n"
        "\tand.b\t#1,d1\n"
        "\tbeq.b\t*+4\n"
        "\tmove.b\td0,(a0)+\n"
        "\tsubq.l\t#1,d2\n"
        ";=barrier\n"
        "\tmove.b\td0,d1\n"
        "\tlsl.w\t#8,d0\n"
        "\tmove.b\td1,d0\n"
        "\tmove.w\td0,d1\n"
        "\tswap\td0\n"
        "\tmove.w\td1,d0\n"
        "\tmoveq\t#3,d1\n"
        "\tand.l\td2,d1\n"
        "\tsub.l\td1,d2\n"
        ";=barrier\n"
        "\tmove.l\td0,(a0)+\n"
        "\tsubq.l\t#4,d2\n"
        "\tbne.b\t*-6\n"
        "\tmove.w\td1,d2\n"
        ";=barrier\n"
        "\tsubq.w\t#1,d2\n"
        "\tblo.b\t*+6\n"
        ";=barrier\n"
        "\tmove.b\td0,(a0)+\n"
        "\tdbf\td2,*-4\n"
        ";=barrier\n"
        "\tmove.l\ta1,d0";

size_t strlen(__reg("a0") const char *) =
        "\tmove.l\ta0,d0\n"
        ";=barrier\n"
        "\ttst.b\t(a0)+\n"
        "\tbne.b\t*-4\n"
        "\tsub.l\ta0,d0\n"
        "\tnot.l\td0";

char *strcpy(__reg("a0") char *, __reg("a1") const char *) =
        "\tmove.l\ta0,d0\n"
        ";=barrier\n"
        "\tmove.b\t(a1)+,(a0)+\n"
        "\tbne.b\t*-4";

char *strncpy(__reg("a0") char *, __reg("a1") const char *,
              __reg("d1") size_t) =
        "\tmove.l\ta0,d0\n"
        "\tbra.b\t*+6\n"
        ";=barrier\n"
        "\tmove.b\t(a1),(a0)+\n"
        "\tbeq.b\t*+2\n"
        "\taddq.l\t#1,a1\n"
        ";=barrier\n"
        "\tsubq.l\t#1,d1\n"
        "\tbpl.b\t*-10";

int strcmp(__reg("a0") const char *, __reg("a1") const char *) =
        "\tmoveq\t#0,d0\n"
        "\tmoveq\t#0,d1\n"
        ";=barrier\n"
        "\tmove.b\t(a0)+,d0\n"
        "\tmove.b\t(a1)+,d1\n"
        "\tbeq.b\t*+6\n"
        "\tsub.l\td1,d0\n"
        "\tbeq.b\t*-10\n"
        "\tmoveq\t#0,d1\n"
        ";=barrier\n"
        "\tsub.l\td1,d0";

int strncmp(__reg("a0") const char *, __reg("a1") const char *,
            __reg("d2") size_t) =
        "\tmoveq\t#0,d0\n"
        "\tmoveq\t#0,d1\n"
        ";=barrier\n"
        "\tsubq.l\t#1,d2\n"
        "\tbmi.b\t*+14\n"
        "\tmove.b\t(a0)+,d0\n"
        "\tmove.b\t(a1)+,d1\n"
        "\tbeq.b\t*+6\n"
        "\tsub.l\td1,d0\n"
        "\tbeq.b\t*-14\n"
        "\tmoveq\t#0,d1\n"
        ";=barrier\n"
        "\tsub.l\td1,d0";


#endif
