<commit_before>/*
** Copyright 2001, <NAME>. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <libc/string.h>
#include <libc/ctype.h>

#if !_ASM_MEMCPY

typedef int word;

#define lsize sizeof(word)
#define lmask (lsize - 1)

void *memmove(void *dest, const void *src, size_t count)
{
	char *d = (char *)dest;
	const char *s = (const char *)src;
	int len;

	if(count == 0 || dest == src)
		return dest;

	if((long)d < (long)s) {
		if(((long)d | (long)s) & lmask) {
			// src and/or dest do not align on word boundary
			if(((long)d ^ (long)s) & lmask || count < lsize)
				len = count; // copy the rest of the buffer with the byte mover
			else
				len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary

			count -= len;
			for(; len >= 0; len--)
				*d++ = *s++;
		}
		for(len = count / lsize; len > 0; len--) {
			*(word *)d = *(word *)s;
			d += lsize;
			s += lsize;
		}
		for(len = count & lmask; len > 0; len--)
			*d++ = *s++;
	} else {
		d += count;
		s += count;
		if(((long)d | (long)s) & lmask) {
			// src and/or dest do not align on word boundary
			if(((long)d ^ (long)s) & lmask || count <= lsize)
				len = count;
			else
				len = ((long)d & lmask);

			count -= len;
			for(; len >= 0; len--)
				*--d = *--s;
		}
		for(len = count / lsize; len > 0; len--) {
			d -= lsize;
			s -= lsize;
			*(word *)d = *(word *)s;
		}
		for(len = count & lmask; len > 0; len--)
			*--d = *--s;
	}

	return dest;
}

#if 0
void *memmove(void *dest, const void *src, size_t count)
{
	char *tmp, *s;

	if(dest <= src) {
		tmp = (char *)dest;
		s = (char *)src;
		while(count-- > 0)
			*tmp++ = *s++;
		}
	else {
		tmp = (char *)dest + count;<commit_msg>fix memmove.c a la freston's fix of memcpy.c<commit_after>/*
** Copyright 2001, <NAME>. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <libc/string.h>
#include <libc/ctype.h>

#if !_ASM_MEMCPY

typedef int word;

#define lsize sizeof(word)
#define lmask (lsize - 1)

void *memmove(void *dest, const void *src, size_t count)
{
	char *d = (char *)dest;
	const char *s = (const char *)src;
	int len;

	if(count == 0 || dest == src)
		return dest;

	if((long)d < (long)s) {
		if(((long)d | (long)s) & lmask) {
			// src and/or dest do not align on word boundary
			if((((long)d ^ (long)s) & lmask) || (count < lsize))
				len = count; // copy the rest of the buffer with the byte mover
			else
				len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary

			count -= len;
			for(; len > 0; len--)
				*d++ = *s++;
		}
		for(len = count / lsize; len > 0; len--) {
			*(word *)d = *(word *)s;
			d += lsize;
			s += lsize;
		}
		for(len = count & lmask; len > 0; len--)
			*d++ = *s++;
	} else {
		d += count;
		s += count;
		if(((long)d | (long)s) & lmask) {
			// src and/or dest do not align on word boundary
			if((((long)d ^ (long)s) & lmask) || (count <= lsize))
				len = count;
			else
				len = ((long)d & lmask);

			count -= len;
			for(; len > 0; len--)
				*--d = *--s;
		}
		for(len = count / lsize; len > 0; len--) {
			d -= lsize;
			s -= lsize;
			*(word *)d = *(word *)s;
		}
		for(len = count & lmask; len > 0; len--)
			*--d = *--s;
	}

	return dest;
}

#if 0
void *memmove(void *dest, const void *src, size_t count)
{
	char *tmp, *s;

	if(dest <= src) {
		tmp = (char *)dest;
		s = (char *)src;
		while(count-- > 0)
			*tmp++ = *s++;
		}
	else {
		tmp = (char *)dest + count;
{'fraction_non_alphanumeric': 0.20011584129742252, 'fraction_numerical': 0.00810889081957718, 'mean_word_length': 2.4609218436873745, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5479526', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1399, 'n_words': 448}