# cc -nostdlib v3.s
_start:	.globl _start
	bufsiz = 0x2000
	sub	$bufsiz,%rsp
	and	$-64,%rsp
	sub	$16,%rsp
	push	%rsp
	pop	%rdi
	cld
	push	$15
	pop	%rcx
	mov	$':',%al
	rep	stosb
	mov	$'1',%al
	stosb
	mov	$1,%cl
	push	%rdi
	pop	%rsi

	o10 = 0
	lf = 5
	lb = 5
	lfb = 9
.macro	setup	n,x,y
	ln = \n+1
	o11 = o10+lb
	o12 = o11+ln
	o13 = o12+lf
	o14 = o13+ln
	o15 = o14+ln
	o16 = o15+lfb
	o17 = o16+ln
	o18 = o17+ln
	o19 = o18+lf
	o20 = o19+ln
	o21 = o20+lb
	o22 = o21+lf
	o23 = o22+ln
	o24 = o23+ln
	o25 = o24+lf
	o26 = o25+lb
	o27 = o26+ln
	o28 = o27+lf
	o29 = o28+ln
	o30 = o29+ln
	o31 = o30+lfb
	o32 = o31+ln
	o33 = o32+ln
	o34 = o33+lf
	o35 = o34+ln
	o36 = o35+lb
	o37 = o36+lf
	o38 = o37+ln
	o39 = o38+ln
	o40 = o39+lf
	len30 = o40
	nrep = bufsiz/len30
	nrep2 = ((\y-\x)/30)/nrep
	nrep3 = ((\y-\x)/30)%nrep
	mov	$\n,%ecx
	mov	$nrep*30,%ebp
	call	main3
.endm
.macro	fast	s
	mov	$nrep2-1,%r15d
	movabs	$0x100000000000000,%rbp
	# skip -10(%rsi)
	mov	-9(%rsi),%r14
2:	mov	$nrep,%r13d
	mov	%rsi,%r12
1:	call	inc3
	\s
	add	$len30,%r12
	sub	$1,%r13d
	jne	1b
	call	flush
	sub	$1,%r15d
	jne	2b
.if	nrep3>0
	mov	$nrep3,%r13d
	mov	%rsi,%r12
1:	call	inc3
	\s
	add	$len30,%r12
	sub	$1,%r13d
	jne	1b
	mov	%r12,%rdi
	call	flush
.endif
.endm
.macro	constno	i,a,b,c,d,e,f,g,h,m
	\i	$\a|\b<<8|\c<<16|\d<<24|\e<<32|\f<<40|\g<<48|\h<<56,\m
.endm
.macro	s4
	shr	$32,%r8
	shr	$32,%r9
	shr	$32,%r10
	mov	$'\n',%r8b
	mov	$'\n',%r9b
	mov	$'\n',%r10b
	mov	%r8d,o11-1(%r12)
	mov	%r8d,o13-1(%r12)
	mov	%r8d,o14-1(%r12)
	mov	%r8d,o16-1(%r12)
	mov	%r8d,o17-1(%r12)
	mov	%r8d,o19-1(%r12)
	mov	%r9d,o22-1(%r12)
	mov	%r9d,o23-1(%r12)
	mov	%r9d,o26-1(%r12)
	mov	%r9d,o28-1(%r12)
	mov	%r9d,o29-1(%r12)
	mov	%r10d,o31-1(%r12)
	mov	%r10d,o32-1(%r12)
	mov	%r10d,o34-1(%r12)
	mov	%r10d,o37-1(%r12)
	mov	%r10d,o38-1(%r12)
.endm
.macro	s5
	shr	$32,%r8
	shr	$32,%r9
	shr	$32,%r10
	mov	%r8d,o11(%r12)
	mov	%r8d,o13(%r12)
	mov	%r8d,o14(%r12)
	mov	%r8d,o16(%r12)
	mov	%r8d,o17(%r12)
	mov	%r8d,o19(%r12)
	mov	%r9d,o22(%r12)
	mov	%r9d,o23(%r12)
	mov	%r9d,o26(%r12)
	mov	%r9d,o28(%r12)
	mov	%r9d,o29(%r12)
	mov	%r10d,o31(%r12)
	mov	%r10d,o32(%r12)
	mov	%r10d,o34(%r12)
	mov	%r10d,o37(%r12)
	mov	%r10d,o38(%r12)
.endm
.macro	s6
	mov	%r8,%rax
	shr	$24,%rax
	shr	$56,%r8
	mov	%eax,o11(%r12)
	mov	%r8b,o11+4(%r12)
	mov	%eax,o13(%r12)
	mov	%r8b,o13+4(%r12)
	mov	%eax,o14(%r12)
	mov	%r8b,o14+4(%r12)
	mov	%eax,o16(%r12)
	mov	%r8b,o16+4(%r12)
	mov	%eax,o17(%r12)
	mov	%r8b,o17+4(%r12)
	mov	%eax,o19(%r12)
	mov	%r8b,o19+4(%r12)
	mov	%r9,%rax
	shr	$24,%rax
	shr	$56,%r9
	mov	%eax,o22(%r12)
	mov	%r9b,o22+4(%r12)
	mov	%eax,o23(%r12)
	mov	%r9b,o23+4(%r12)
	mov	%eax,o26(%r12)
	mov	%r9b,o26+4(%r12)
	mov	%eax,o28(%r12)
	mov	%r9b,o28+4(%r12)
	mov	%eax,o29(%r12)
	mov	%r9b,o29+4(%r12)
	mov	%r10,%rax
	shr	$24,%rax
	shr	$56,%r10
	mov	%eax,o31(%r12)
	mov	%r10b,o31+4(%r12)
	mov	%eax,o32(%r12)
	mov	%r10b,o32+4(%r12)
	mov	%eax,o34(%r12)
	mov	%r10b,o34+4(%r12)
	mov	%eax,o37(%r12)
	mov	%r10b,o37+4(%r12)
	mov	%eax,o38(%r12)
	mov	%r10b,o38+4(%r12)
.endm
.macro	s7
	mov	%r8,%rax
	shr	$16,%rax
	shr	$48,%r8
	mov	%eax,o11(%r12)
	mov	%r8w,o11+4(%r12)
	mov	%eax,o13(%r12)
	mov	%r8w,o13+4(%r12)
	mov	%eax,o14(%r12)
	mov	%r8w,o14+4(%r12)
	mov	%eax,o16(%r12)
	mov	%r8w,o16+4(%r12)
	mov	%eax,o17(%r12)
	mov	%r8w,o17+4(%r12)
	mov	%eax,o19(%r12)
	mov	%r8w,o19+4(%r12)
	mov	%r9,%rax
	shr	$16,%rax
	shr	$48,%r9
	mov	%eax,o22(%r12)
	mov	%r9w,o22+4(%r12)
	mov	%eax,o23(%r12)
	mov	%r9w,o23+4(%r12)
	mov	%eax,o26(%r12)
	mov	%r9w,o26+4(%r12)
	mov	%eax,o28(%r12)
	mov	%r9w,o28+4(%r12)
	mov	%eax,o29(%r12)
	mov	%r9w,o29+4(%r12)
	mov	%r10,%rax
	shr	$16,%rax
	shr	$48,%r10
	mov	%eax,o31(%r12)
	mov	%r10w,o31+4(%r12)
	mov	%eax,o32(%r12)
	mov	%r10w,o32+4(%r12)
	mov	%eax,o34(%r12)
	mov	%r10w,o34+4(%r12)
	mov	%eax,o37(%r12)
	mov	%r10w,o37+4(%r12)
	mov	%eax,o38(%r12)
	mov	%r10w,o38+4(%r12)
.endm
.macro	s8
	mov	$'\n',%r8b
	mov	$'\n',%r9b
	mov	$'\n',%r10b
	mov	%r8,o11-1(%r12)
	mov	%r8,o13-1(%r12)
	mov	%r8,o14-1(%r12)
	mov	%r8,o16-1(%r12)
	mov	%r8,o17-1(%r12)
	mov	%r8,o19-1(%r12)
	mov	%r9,o22-1(%r12)
	mov	%r9,o23-1(%r12)
	mov	%r9,o26-1(%r12)
	mov	%r9,o28-1(%r12)
	mov	%r9,o29-1(%r12)
	mov	%r10,o31-1(%r12)
	mov	%r10,o32-1(%r12)
	mov	%r10,o34-1(%r12)
	mov	%r10,o37-1(%r12)
	mov	%r10,o38-1(%r12)
.endm
.macro	s9
	mov	%r8,o11(%r12)
	mov	%r8,o13(%r12)
	mov	%r8,o14(%r12)
	mov	%r8,o16(%r12)
	mov	%r8,o17(%r12)
	mov	%r8,o19(%r12)
	mov	%r9,o22(%r12)
	mov	%r9,o23(%r12)
	mov	%r9,o26(%r12)
	mov	%r9,o28(%r12)
	mov	%r9,o29(%r12)
	mov	%r10,o31(%r12)
	mov	%r10,o32(%r12)
	mov	%r10,o34(%r12)
	mov	%r10,o37(%r12)
	mov	%r10,o38(%r12)
.endm
.macro	s10
	mov	%r8,o11+1(%r12)
	mov	%r8,o13+1(%r12)
	mov	%r8,o14+1(%r12)
	mov	%r8,o16+1(%r12)
	mov	%r8,o17+1(%r12)
	mov	%r8,o19+1(%r12)
	mov	%r9,o22+1(%r12)
	mov	%r9,o23+1(%r12)
	mov	%r9,o26+1(%r12)
	mov	%r9,o28+1(%r12)
	mov	%r9,o29+1(%r12)
	mov	%r10,o31+1(%r12)
	mov	%r10,o32+1(%r12)
	mov	%r10,o34+1(%r12)
	mov	%r10,o37+1(%r12)
	mov	%r10,o38+1(%r12)
.endm

	mov	$999,%ebp
	call	main2
	constno	movl,':','1','0','0',0,0,0,0,-5(%rsi)
	setup	4,1000,10000
	fast	s4
	constno	movl,'1','0','0','0',0,0,0,0,-5(%rsi)
	setup	5,10000,100000
	fast	s5
	constno	movabs,':',':',':','1','0','0','0','0',%rax
	mov	%rax,-9(%rsi)
	setup	6,100000,1000000
	fast	s6
	constno	movabs,':',':','1','0','0','0','0','0',%rax
	mov	%rax,-9(%rsi)
	setup	7,1000000,10000000
	fast	s7
	constno	movabs,':','1','0','0','0','0','0','0',%rax
	mov	%rax,-9(%rsi)
	setup	8,10000000,100000000
	fast	s8
	constno	movabs,'1','0','0','0','0','0','0','0',%rax
	mov	%rax,-9(%rsi)
	setup	9,100000000,1000000000
	fast	s9
	movb	$'1',-10(%rsi)
	constno	movabs,'0','0','0','0','0','0','0','0',%rax
	mov	%rax,-9(%rsi)
	setup	10,1000000000,1999999990
	fast	s10
	constno	movabs,'9','9','9','9','9','9','9','9',%rax
	mov	%rax,-9(%rsi)
	mov	$10,%ecx
	mov	$30,%ebp
	call	main3
	setup	10,2000000020,2999999980
	fast	s10
	constno	movabs,'9','9','9','9','9','9','9','8',%rax
	mov	%rax,-9(%rsi)
	mov	$10,%ecx
	mov	$30,%ebp
	call	main3
	setup	10,3000000010,4000000000
	fast	s10
	movb	$'4',-10(%rsi)
	constno	movabs,'0','0','0','0','0','0','0','0',%rax
	mov	%rax,-9(%rsi)
	setup	10,4000000000,4294967290
	fast	s10
	constno	movabs,'2','9','4','9','6','7','2','9',%rax
	mov	%rax,-9(%rsi)
	mov	$10,%ecx
	mov	$5,%ebp
	call	main3
	xor	%edi,%edi
	push	$60
	pop	%rax
	syscall
main2:	mov	$0x5f562d0f,%ebx	# 56 5f = push %rsi; pop %rdi
	main3 = .-2
main1:	dec	%bl
	js	main2
	push	%rcx
	jpo	nextm
	mov	$'F'+'i'*0x100+'z'*0x10000+'z'*0x1000000,%eax
	shr	%bh
	jnbe	skipf
stor:	stosl
	jb	skipn
skipf:	sub	$'F'+'i'*0x100-('B'+'u'*0x100),%ax
	jmp	stor
nextm:	sub	%rcx,%rsi
	rep	movsb
skipn:	mov	$0xa,%al
	stosb
	pop	%rcx
	push	%rsi
next1:	dec	%rsi
	incb	(%rsi)
	cmpb	$':',(%rsi)
	jb	next2
	andb	$'1',(%rsi)
	jpe	next1
	inc	%ecx
next2:	pop	%rsi
	dec	%ebp
	jne	main1
flush:	mov	%edi,%edx
	sub	%esi,%edx
	push	%rcx
	push	%rdi
	push	$1
	pop	%rax
	mov	%eax,%edi
	syscall
	pop	%rdi
	pop	%rcx
	ret
inc3:	mov	%r14,%r8
	bts	$56,%r14
	jc	1f
	mov	%r14,%r9
	call	inc1
	mov	%r14,%r10
	add	%rbp,%r14
	ret
1:	call	inc1
	mov	%r14,%r9
	add	%rbp,%r14
	mov	%r14,%r10
	call	inc1
	ret
inc1:	movabs	$0x3939393939393939,%rax
	xor	%r14,%rax
	je	1f
	bsr	%rax,%rcx
	and	$-8,%ecx
	movabs	$0xf6f6f6f6f6f6f701,%rax
	shl	%cl,%rax
	add	%rax,%r14
	ret
1:	mov	$0x3030303030303030,%r14
	ret
