
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

void usage() {
    printf("Usage: malloc <size>\n");
    exit(1);
}

int main(int argc, char *argv[]) {
    // parse the command line
    if(argc != 2)
        usage();

    size_t size = (size_t)strtoull(argv[1], NULL, 10);
    if(size == -1 || size == 0)
        usage();

    // main loop
    while(1) {
        // allocate
        char* p = malloc(size);
        if(p == NULL) {
            printf("malloc returned NULL!\n");
            break;
        }

        // write to the buffer in 4k increments
        size_t i = 0;
        while(1) {
            // hit the beginning of the 4k segment
            if(i + 1 >= size)
                break;
            p[i + 1] = '\x2a';

            // hit the end of the 4k segment
            if(i + 4095 >= size)
                break;
            p[i + 4095] = '\xd6';

            i += 4096;
        }
    }

    return 0;
}
