Index: boot/generic/include/memstr.h
===================================================================
--- boot/generic/include/memstr.h	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ boot/generic/include/memstr.h	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -35,6 +35,4 @@
 #include <typedefs.h>
 
-extern void *memcpy(void *, const void *, size_t);
-extern void *memset(void *, int, size_t);
 extern void *memmove(void *, const void *, size_t);
 
Index: boot/generic/src/memstr.c
===================================================================
--- boot/generic/src/memstr.c	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ boot/generic/src/memstr.c	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -29,48 +29,4 @@
 #include <memstr.h>
 #include <typedefs.h>
-
-/** Move memory block without overlapping.
- *
- * Copy cnt bytes from src address to dst address. The source
- * and destination memory areas cannot overlap.
- *
- * @param dst Destination address to copy to.
- * @param src Source address to copy from.
- * @param cnt Number of bytes to copy.
- *
- * @return Destination address.
- *
- */
-void *memcpy(void *dst, const void *src, size_t cnt)
-{
-	uint8_t *dp = (uint8_t *) dst;
-	const uint8_t *sp = (uint8_t *) src;
-	
-	while (cnt-- != 0)
-		*dp++ = *sp++;
-	
-	return dst;
-}
-
-/** Fill block of memory.
- *
- * Fill cnt bytes at dst address with the value val.
- *
- * @param dst Destination address to fill.
- * @param val Value to fill.
- * @param cnt Number of bytes to fill.
- *
- * @return Destination address.
- *
- */
-void *memset(void *dst, int val, size_t cnt)
-{
-	uint8_t *dp = (uint8_t *) dst;
-	
-	while (cnt-- != 0)
-		*dp++ = val;
-	
-	return dst;
-}
 
 /** Move memory block with possible overlapping.
Index: kernel/Makefile
===================================================================
--- kernel/Makefile	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ kernel/Makefile	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -228,5 +228,4 @@
 	generic/src/lib/func.c \
 	generic/src/lib/memstr.c \
-	generic/src/lib/memfnc.c \
 	generic/src/lib/sort.c \
 	generic/src/lib/str.c \
Index: kernel/arch/ia32/src/asm.S
===================================================================
--- kernel/arch/ia32/src/asm.S	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ kernel/arch/ia32/src/asm.S	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -38,4 +38,6 @@
 .global paging_on
 .global enable_l_apic_in_msr
+.global memset
+.global memcpy
 .global memcpy_from_uspace
 .global memcpy_from_uspace_failover_address
@@ -44,7 +46,55 @@
 .global early_putchar
 
+#define MEMSET_DST   4
+#define MEMSET_VAL   8
+#define MEMSET_SIZE  12
+
 #define MEMCPY_DST   4
 #define MEMCPY_SRC   8
 #define MEMCPY_SIZE  12
+
+/* Fill memory with byte pattern
+ *
+ * This is a conventional memset().
+ *
+ * @param MEMSET_DST(%esp)  Destination address.
+ * @param MEMSET_VAL(%esp)  Value to fill.
+ * @param MEMSET_SIZE(%esp) Size.
+ *
+ * @return MEMSET_DST(%esp).
+ *
+ */
+memset:
+	movl %edi, %edx  /* save %edi */
+	
+	movl MEMSET_DST(%esp), %edi
+	movl MEMSET_VAL(%esp), %ecx
+	
+	/* Create byte pattern */
+	movb %cl, %ch
+	movw %cx, %ax
+	shll $16, %eax
+	orw %cx, %ax
+	
+	movl MEMSET_SIZE(%esp), %ecx
+	shrl $2, %ecx  /* size / 4 */
+	
+	/* Write whole words */
+	rep stosl
+	
+	movl MEMSET_SIZE(%esp), %ecx
+	andl $3, %ecx  /* size % 4 */
+	jz 0f
+	
+	/* Copy the rest byte by byte */
+	rep stosb
+	
+	0:
+	
+		movl %edx, %edi
+		
+		/* MEMSET_DST(%esp), success */
+		movl MEMSET_DST(%esp), %eax
+		ret
 
 /** Copy memory to/from userspace.
@@ -63,4 +113,5 @@
  *
  */
+memcpy:
 memcpy_from_uspace:
 memcpy_to_uspace:
Index: rnel/generic/include/lib/memfnc.h
===================================================================
--- kernel/generic/include/lib/memfnc.h	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ 	(revision )
@@ -1,46 +1,0 @@
-/*
- * Copyright (c) 2011 Martin Decky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup generic
- * @{
- */
-/** @file
- */
-
-#ifndef KERN_LIB_MEMFNC_H_
-#define KERN_LIB_MEMFNC_H_
-
-#include <typedefs.h>
-
-extern void *memset(void *, int, size_t);
-extern void *memcpy(void *, const void *, size_t);
-
-#endif
-
-/** @}
- */
Index: rnel/generic/src/lib/memfnc.c
===================================================================
--- kernel/generic/src/lib/memfnc.c	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ 	(revision )
@@ -1,90 +1,0 @@
-	/*
- * Copyright (c) 2011 Martin Decky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup generic
- * @{
- */
-
-/**
- * @file
- * @brief Memory string functions.
- *
- * This file provides architecture independent functions to manipulate blocks
- * of memory. These functions are optimized as much as generic functions of
- * this type can be.
- */
-
-#include <lib/memfnc.h>
-#include <typedefs.h>
-
-/** Fill block of memory.
- *
- * Fill cnt bytes at dst address with the value val.
- *
- * @param dst Destination address to fill.
- * @param val Value to fill.
- * @param cnt Number of bytes to fill.
- *
- * @return Destination address.
- *
- */
-void *memset(void *dst, int val, size_t cnt)
-{
-	uint8_t *dp = (uint8_t *) dst;
-	
-	while (cnt-- != 0)
-		*dp++ = val;
-	
-	return dst;
-}
-
-/** Move memory block without overlapping.
- *
- * Copy cnt bytes from src address to dst address. The source
- * and destination memory areas cannot overlap.
- *
- * @param dst Destination address to copy to.
- * @param src Source address to copy from.
- * @param cnt Number of bytes to copy.
- *
- * @return Destination address.
- *
- */
-void *memcpy(void *dst, const void *src, size_t cnt)
-{
-	uint8_t *dp = (uint8_t *) dst;
-	const uint8_t *sp = (uint8_t *) src;
-	
-	while (cnt-- != 0)
-		*dp++ = *sp++;
-	
-	return dst;
-}
-
-/** @}
- */
Index: uspace/lib/c/arch/ia32/Makefile.inc
===================================================================
--- uspace/lib/c/arch/ia32/Makefile.inc	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ uspace/lib/c/arch/ia32/Makefile.inc	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -28,4 +28,5 @@
 
 ARCH_SOURCES = \
+	arch/$(UARCH)/src/asm.S \
 	arch/$(UARCH)/src/entry.S \
 	arch/$(UARCH)/src/entryjmp.s \
Index: uspace/lib/c/arch/ia32/src/asm.S
===================================================================
--- uspace/lib/c/arch/ia32/src/asm.S	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
+++ uspace/lib/c/arch/ia32/src/asm.S	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2013 Martin Decky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+.text
+.global memset
+.global memcpy
+
+#define MEMSET_DST   4
+#define MEMSET_VAL   8
+#define MEMSET_SIZE  12
+
+#define MEMCPY_DST   4
+#define MEMCPY_SRC   8
+#define MEMCPY_SIZE  12
+
+/* Fill memory with byte pattern
+ *
+ * This is a conventional memset().
+ *
+ * @param MEMSET_DST(%esp)  Destination address.
+ * @param MEMSET_VAL(%esp)  Value to fill.
+ * @param MEMSET_SIZE(%esp) Size.
+ *
+ * @return MEMSET_DST(%esp).
+ *
+ */
+memset:
+	movl %edi, %edx  /* save %edi */
+	
+	movl MEMSET_DST(%esp), %edi
+	movl MEMSET_VAL(%esp), %ecx
+	
+	/* Create byte pattern */
+	movb %cl, %ch
+	movw %cx, %ax
+	shll $16, %eax
+	orw %cx, %ax
+	
+	movl MEMSET_SIZE(%esp), %ecx
+	shrl $2, %ecx  /* size / 4 */
+	
+	/* Write whole words */
+	rep stosl
+	
+	movl MEMSET_SIZE(%esp), %ecx
+	andl $3, %ecx  /* size % 4 */
+	jz 0f
+	
+	/* Copy the rest byte by byte */
+	rep stosb
+	
+	0:
+	
+		movl %edx, %edi
+		
+		/* MEMSET_DST(%esp), success */
+		movl MEMSET_DST(%esp), %eax
+		ret
+
+/** Copy memory to/from userspace.
+ *
+ * This is a conventional memcpy().
+ *
+ * @param MEMCPY_DST(%esp)  Destination address.
+ * @param MEMCPY_SRC(%esp)  Source address.
+ * @param MEMCPY_SIZE(%esp) Size.
+ *
+ * @return MEMCPY_DST(%esp).
+ *
+ */
+memcpy:
+	movl %edi, %edx  /* save %edi */
+	movl %esi, %eax  /* save %esi */
+	
+	movl MEMCPY_SIZE(%esp), %ecx
+	shrl $2, %ecx  /* size / 4 */
+	
+	movl MEMCPY_DST(%esp), %edi
+	movl MEMCPY_SRC(%esp), %esi
+	
+	/* Copy whole words */
+	rep movsl
+	
+	movl MEMCPY_SIZE(%esp), %ecx
+	andl $3, %ecx  /* size % 4 */
+	jz 0f
+	
+	/* Copy the rest byte by byte */
+	rep movsb
+	
+	0:
+	
+		movl %edx, %edi
+		movl %eax, %esi
+		
+		/* MEMCPY_DST(%esp), success */
+		movl MEMCPY_DST(%esp), %eax
+		ret
Index: uspace/lib/c/generic/mem.c
===================================================================
--- uspace/lib/c/generic/mem.c	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ uspace/lib/c/generic/mem.c	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -37,151 +37,4 @@
 #include <stdlib.h>
 #include <sys/types.h>
-
-/** Fill memory block with a constant value. */
-void *memset(void *dest, int b, size_t n)
-{
-	char *pb;
-	unsigned long *pw;
-	size_t word_size;
-	size_t n_words;
-
-	unsigned long pattern;
-	size_t i;
-	size_t fill;
-
-	/* Fill initial segment. */
-	word_size = sizeof(unsigned long);
-	fill = word_size - ((uintptr_t) dest & (word_size - 1));
-	if (fill > n) fill = n;
-
-	pb = dest;
-
-	i = fill;
-	while (i-- != 0)
-		*pb++ = b;
-
-	/* Compute remaining size. */
-	n -= fill;
-	if (n == 0) return dest;
-
-	n_words = n / word_size;
-	n = n % word_size;
-	pw = (unsigned long *) pb;
-
-	/* Create word-sized pattern for aligned segment. */
-	pattern = 0;
-	i = word_size;
-	while (i-- != 0)
-		pattern = (pattern << 8) | (uint8_t) b;
-
-	/* Fill aligned segment. */
-	i = n_words;
-	while (i-- != 0)
-		*pw++ = pattern;
-
-	pb = (char *) pw;
-
-	/* Fill final segment. */
-	i = n;
-	while (i-- != 0)
-		*pb++ = b;
-
-	return dest;
-}
-
-struct along {
-	unsigned long n;
-} __attribute__ ((packed));
-
-static void *unaligned_memcpy(void *dst, const void *src, size_t n)
-{
-	size_t i, j;
-	struct along *adst = dst;
-	const struct along *asrc = src;
-
-	for (i = 0; i < n / sizeof(unsigned long); i++)
-		adst[i].n = asrc[i].n;
-		
-	for (j = 0; j < n % sizeof(unsigned long); j++)
-		((unsigned char *) (((unsigned long *) dst) + i))[j] =
-		    ((unsigned char *) (((unsigned long *) src) + i))[j];
-		
-	return (char *) dst;
-}
-
-/** Copy memory block. */
-void *memcpy(void *dst, const void *src, size_t n)
-{
-	size_t i;
-	size_t mod, fill;
-	size_t word_size;
-	size_t n_words;
-
-	const unsigned long *srcw;
-	unsigned long *dstw;
-	const uint8_t *srcb;
-	uint8_t *dstb;
-
-	word_size = sizeof(unsigned long);
-
-	/*
-	 * Are source and destination addresses congruent modulo word_size?
-	 * If not, use unaligned_memcpy().
-	 */
-
-	if (((uintptr_t) dst & (word_size - 1)) !=
-	    ((uintptr_t) src & (word_size - 1)))
- 		return unaligned_memcpy(dst, src, n);
-
-	/*
-	 * mod is the address modulo word size. fill is the length of the
-	 * initial buffer segment before the first word boundary.
-	 * If the buffer is very short, use unaligned_memcpy(), too.
-	 */
-
-	mod = (uintptr_t) dst & (word_size - 1);
-	fill = word_size - mod;
-	if (fill > n) fill = n;
-
-	/* Copy the initial segment. */
-
-	srcb = src;
-	dstb = dst;
-
-	i = fill;
-	while (i-- != 0)
-		*dstb++ = *srcb++;
-
-	/* Compute remaining length. */
-
-	n -= fill;
-	if (n == 0) return dst;
-
-	/* Pointers to aligned segment. */
-
-	dstw = (unsigned long *) dstb;
-	srcw = (const unsigned long *) srcb;
-
-	n_words = n / word_size;	/* Number of whole words to copy. */
-	n -= n_words * word_size;	/* Remaining bytes at the end. */
-
-	/* "Fast" copy. */
-	i = n_words;
-	while (i-- != 0)
-		*dstw++ = *srcw++;
-
-	/*
-	 * Copy the rest.
-	 */
-
-	srcb = (const uint8_t *) srcw;
-	dstb = (uint8_t *) dstw;
-
-	i = n;
-	while (i-- != 0)
-		*dstb++ = *srcb++;
-
-	return dst;
-}
 
 /** Move memory block with possible overlapping. */
Index: uspace/lib/c/include/mem.h
===================================================================
--- uspace/lib/c/include/mem.h	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ uspace/lib/c/include/mem.h	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -38,10 +38,10 @@
 #include <sys/types.h>
 
+#define memset(dst, val, cnt)  __builtin_memset((dst), (val), (cnt))
+#define memcpy(dst, src, cnt)  __builtin_memcpy((dst), (src), (cnt))
+
 #define bzero(ptr, len)  memset((ptr), 0, (len))
 
-extern void *memset(void *, int, size_t);
-extern void *memcpy(void *, const void *, size_t);
 extern void *memmove(void *, const void *, size_t);
-
 extern int bcmp(const void *, const void *, size_t);
 
Index: uspace/lib/posix/include/posix/string.h
===================================================================
--- uspace/lib/posix/include/posix/string.h	(revision 2d1195c0dd1b314c846280e600e9899f43e9c478)
+++ uspace/lib/posix/include/posix/string.h	(revision 8a5a902af34592bce127314cf2fc188c642817f3)
@@ -60,14 +60,19 @@
  * forward declarations ought to be enough.
  */
+
 /* From str.h. */
-extern char * strtok_r(char *, const char *, char **);
-extern char * strtok(char *, const char *);
+
+extern char *strtok_r(char *, const char *, char **);
+extern char *strtok(char *, const char *);
 
 /* From mem.h */
+
+#define memset(dst, val, cnt)  __builtin_memset((dst), (val), (cnt))
+#define memcpy(dst, src, cnt)  __builtin_memcpy((dst), (src), (cnt))
+
 #define bzero(ptr, len)  memset((ptr), 0, (len))
-extern void *memset(void *, int, size_t);
-extern void *memcpy(void *, const void *, size_t);
+
 extern void *memmove(void *, const void *, size_t);
-
+extern int bcmp(const void *, const void *, size_t);
 
 /* Copying and Concatenation */
