	.section	.text
@ r0 = destination
@ r1 = source, until the end of loop
@ r2 = length, until shortly before the loop
	.global	strcpyz
	.type	strcpyz, %function
strcpyz:
	sub	sp, sp, #8
	str	r0, [sp]		@ Save destination
	bl	strlen
	cmp	r0, #255
	bls	strcpyz.prepare_loop	@ length <= 255
	mov	r0, #255		@ length = 255
strcpyz.prepare_loop:
	str	r0, [sp, #4]		@ index = length
	ldr	r2, [sp]		@ Restore destination
	add	r2, r2, #1		@ destination = destination + 1
	add	r3, r2, r0		@ r3 = destination + length
	mov	r0, #0			@ r0 overwritten
	strb	r0, [r3]		@ r3, dereferenced = 0
	b	strcpyz.loop
strcpyz.decrement:
	ldr	r0, [sp, #4]
	sub	r3, r0, #1		@ index = index - 1
	str	r3, [sp, #4]
	add	r0, r1, r3		@ r2 = source + index
	add	r3, r2, r3		@ r3 = destination + index
	ldrb	r0, [r0]		@ r0 = r0, dereferenced
	strb	r0, [r3]		@ r3, dereferenced = r0
strcpyz.loop:
	ldr	r0, [sp, #4]
	cmp	r0, #0
	bne	strcpyz.decrement	@ index <> 0
	sub	r2, r2, #1		@ destination = destination - 1
	ldr	r1, [sp]		@ r1 overwritten
	strb	r1, [r2]		@ destination, dereferenced = length
	add	sp, sp, #8
	mov	pc, lr			@ Return
