#include <atmel_start.h>

int __attribute__((weak)) ffs(int v)
{
        int i, bit = 1;
        for (i = 0; i < sizeof(int) * 8; i++) {
                if (v & bit) {
                        return i + 1;
                }
                bit <<= 1;
        }
        return 0;
}

int __attribute__((weak)) abs(int v)
{
        if (v < 0)
                return -v;
        else
                return v;
}

#ifdef USE_CLANG
void __attribute__((weak)) *memset(void *m, int c, size_t n)
{
        char *s = (char *) m;
        while (n--)
                *s++ = (char) c;
        return m;
}

void __attribute__((weak)) *memcpy(void *dst0, const void *src0, size_t len0)
{
        char *dst = (char *)dst0;
        char *src = (char *)src0;

        void *save = dst0;

        while (len0--)
        {
                *dst++ = *src++;
        }

        return save;
}
#endif

void __attribute__((weak)) __libc_init_array(void) {};

int myerrno;

int __attribute__((used)) __attribute__((weak)) *__errno(void)
{
        return &myerrno;
}

__attribute__((weak)) uintptr_t __stack_chk_guard = 0;

void __attribute__((used)) __attribute__((weak)) __attribute__((__constructor__)) __stack_chk_init(void)
{
        if (__stack_chk_guard != 0)
                return;

        /* If getentropy is not available, use the "terminator canary". */
        ((unsigned char *)&__stack_chk_guard)[0] = 0;
        ((unsigned char *)&__stack_chk_guard)[1] = 0;
        ((unsigned char *)&__stack_chk_guard)[2] = '\n';
        ((unsigned char *)&__stack_chk_guard)[3] = 255;
}

int _read(int file, char *ptr, int len) {return 0;};
int _write(int file, char *ptr, int len) {return 0;};
