#include "stdlib.h"
#include "stdio.h"
#include "string.h"

FILE *stdin = (FILE *) 0;
FILE *stdout = (FILE *) 1;
FILE *stderr = (FILE *) 2;

extern void kernelPrint(const char *str, size_t size);
extern void kernelError(const char *str, size_t size);
extern void _move(size_t size, void *dest, const void *src);

void error(const char *line) {
	kernelError(line, strlen(line));
}

void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), void *tmp) {
	for (size_t i = 0; i < nmemb - 1; i++) {
		void* a = base + (size * i);
		for (size_t j = i + 1; j < nmemb; j++) {
			void* b = base + (size * j);
			if (compar(a, b)) {
				memcpy(tmp, a, size);
				memcpy(a, b, size);
				memcpy(b, tmp, size);
			}
		}
	}
}

double strtod(const char *restrict nptr, char **restrict endptr) {
	return 0.0;
}

int fprintf(FILE *restrict stream, const char *restrict format, ...) {
	kernelPrint(format, strlen(format));
	return 0;
}

int sprintf(char *restrict str, const char *restrict format, ...) {
	return 0;
}

void *memset(void *s, int c, size_t n) {
	unsigned char *s_buffer = (unsigned char *) s;
	while (n > 0) {
		n--;
		s_buffer[n] = c;
	}
	return s;
}

size_t strlen(const char *s) {
	size_t len = 0;
	while (s[len++] != '\0');
	return len;
}

void *memcpy(void *dest, const void *src, size_t n) {
	_move(n, dest, src);
	return dest;
}


void *memmove(void *dest, const void *src, size_t n) {
	_move(n, dest, src);
	return dest;
}
