#include <stdlib.h>
#ifndef __FreeBSD__
#include <malloc.h>
#endif
#include <my_assert.h>

#ifdef __FreeBSD__
#define memalign        my_memalign
void *memalign (size_t alignment, size_t size)
{
    void *r;
    if (!posix_memalign (&r, alignment, size))
        return r;
    return NULL;
}
#endif

void entry (void)
{
    char *p;

    p = (char *) memalign (80, 1024);
    assert (!p);
    assert (errno == EINVAL);

    p = (char *) memalign (8, 1);
    printf ("memalign(13) => %p\n", (void *) p);
    assert (((ULONG64) p & 0x7) == 0);
    free (p);

    p = (char *) memalign (0xfff + 1, 11111);
    printf ("memalign(13) => %p\n", (void *) p);
    assert (((ULONG64) p & 0xfff) == 0);
    free (p);

    p = (char *) memalign (0xfff + 1, 13);
    printf ("memalign(13) => %p\n", (void *) p);
    assert (((ULONG64) p & 0xfff) == 0);
    free (p);

    p = (char *) malloc (1);
    printf ("   malloc(1) => %p\n", (void *) p);

    p = (char *) memalign (0xfff + 1, 77);
    printf ("memalign(77) => %p\n", (void *) p);
    assert (((ULONG64) p & 0xfff) == 0);
    free (p);

    p = (char *) malloc (55);
    printf ("  malloc(55) => %p\n", (void *) p);
    free (p);

    p = (char *) memalign (0xfff + 1, 61);
    printf ("memalign(61) => %p\n", (void *) p);
    assert (((ULONG64) p & 0xfff) == 0);
}

