
.intel_syntax noprefix

.global tstrupr
.global tstrstart
.global tmemcpy
.global tmemset
.global tmemcmp
.global tstrlen
.global tmemicmp
.global tstrchr
.global tstrrchr
.global tstrcpy
.global tstrcat
.global tstrncpy
.global tstrcmp
.global tstricmp
.global tstrstr
.global tfwrite
.global tfseek
.global _ltype

.extern fseek
.extern fwrite


.SECTION .text
	.ALIGN	16

tstrupr:
	mov	rdx, rcx
$_001:	movzx	eax, byte ptr [rcx]
	test	byte ptr [r15+rax], 0x02
	jz	$_002
	and	byte ptr [rcx], 0xDF
$_002:	inc	rcx
	test	eax, eax
	jnz	$_001
	mov	rax, rdx
	ret

tstrstart:
	jmp	$_004

$_003:	add	rcx, 1
$_004:	movzx	eax, byte ptr [rcx]
	test	byte ptr [r15+rax], 0x08
	jnz	$_003
	xchg	rcx, rax
	ret

tmemcpy:
	mov	r10, rdi
	mov	r11, rsi
	mov	rax, rcx
	mov	rdi, rcx
	mov	rsi, rdx
	mov	ecx, r8d
	rep movsb
	mov	rsi, r11
	mov	rdi, r10
	ret

tmemset:
	mov	r10, rdi
	mov	r9, rcx
	mov	rdi, rcx
	mov	eax, edx
	mov	ecx, r8d
	rep stosb
	mov	rax, r9
	mov	rdi, r10
	ret

tmemcmp:
	mov	r10, rdi
	mov	r11, rsi
	mov	rdi, rcx
	mov	rsi, rdx
	mov	ecx, r8d
	xor	eax, eax
	repe cmpsb
	mov	rsi, r11
	mov	rdi, r10
	jz	$_005
	sbb	eax, eax
	sbb	eax, -1
$_005:	ret

tstrlen:
	mov	rdx, rdi
	mov	rdi, rcx
	mov	rcx, -1
	xor	eax, eax
	repne scasb
	mov	rax, rcx
	mov	rdi, rdx
	not	rax
	dec	rax
	ret

tmemicmp:
	mov	eax, r8d
$_006:	test	eax, eax
	jz	$_007
	dec	eax
	mov	r8b, byte ptr [rcx+rax]
	cmp	r8b, byte ptr [rdx+rax]
	jz	$_006
	xor	r8b, 0x20
	cmp	r8b, byte ptr [rdx+rax]
	jz	$_006
	sbb	eax, eax
	sbb	eax, -1
$_007:	ret

tstrchr:
	xor	eax, eax
$_008:	cmp	al, byte ptr [rcx]
	jz	$_010
	cmp	dl, byte ptr [rcx]
	jz	$_009
	inc	rcx
	jmp	$_008

$_009:	mov	rax, rcx
$_010:	ret

tstrrchr:
	xor	eax, eax
$_011:	cmp	byte ptr [rcx], 0
	jz	$_012
	cmp	dl, byte ptr [rcx]
	cmove	rax, rcx
	inc	rcx
	jmp	$_011

$_012:
	ret

tstrcpy:
	mov	r8, rcx
$_013:	mov	al, byte ptr [rdx]
	mov	byte ptr [rcx], al
	inc	rdx
	inc	rcx
	test	al, al
	jnz	$_013
	mov	rax, r8
	ret

tstrcat:
	mov	r10, rbx
	mov	r11, rcx
	mov	rbx, rcx
$_014:	mov	eax, dword ptr [rbx]
	add	rbx, 4
	lea	ecx, [rax-0x1010101]
	not	eax
	and	ecx, eax
	and	ecx, 0x80808080
	jz	$_014
	bsf	ecx, ecx
	shr	ecx, 3
	lea	rbx, [rbx+rcx-0x4]
	mov	rcx, rdx
	jmp	$_016

$_015:	mov	dword ptr [rbx], eax
	add	rbx, 4
$_016:	mov	eax, dword ptr [rcx]
	add	rcx, 4
	lea	edx, [rax-0x1010101]
	not	eax
	and	edx, eax
	not	eax
	and	edx, 0x80808080
	jz	$_015
	bt	edx, 7
	mov	byte ptr [rbx], al
	jc	$_017
	bt	edx, 15
	mov	byte ptr [rbx+0x1], ah
	jc	$_017
	shr	eax, 16
	bt	edx, 23
	mov	byte ptr [rbx+0x2], al
	jc	$_017
	mov	byte ptr [rbx+0x3], ah
$_017:	mov	rax, r11
	mov	rbx, r10
	ret

tstrncpy:
	mov	r11, rdi
	mov	rdi, rcx
	mov	r10, rcx
	mov	ecx, r8d
$_018:	test	ecx, ecx
	jz	$_019
	dec	ecx
	mov	al, byte ptr [rdx]
	mov	byte ptr [rdi], al
	add	rdx, 1
	add	rdi, 1
	test	al, al
	jnz	$_018
	rep stosb
$_019:	mov	rax, r10
	mov	rdi, r11
	ret

tstrcmp:
	dec	rcx
	dec	rdx
	mov	eax, 1
$_020:	test	eax, eax
	jz	$_021
	inc	rcx
	inc	rdx
	mov	al, byte ptr [rcx]
	cmp	al, byte ptr [rdx]
	jz	$_020
	sbb	eax, eax
	sbb	eax, -1
$_021:	ret

tstricmp:
	dec	rcx
	dec	rdx
	mov	eax, 1
$_022:	test	eax, eax
	jz	$_023
	inc	rcx
	inc	rdx
	mov	al, byte ptr [rcx]
	cmp	al, byte ptr [rdx]
	jz	$_022
	xor	al, 0x20
	cmp	al, byte ptr [rdx]
	jz	$_022
	xor	al, 0x20
	cmp	al, byte ptr [rdx]
	sbb	eax, eax
	sbb	eax, -1
$_023:	ret

tstrstr:
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 40
	mov	rdi, rcx
	mov	rbx, rdx
	mov	rcx, rbx
	call	tstrlen
	mov	esi, eax
	test	eax, eax
	jz	$_027
	mov	rcx, rdi
	call	tstrlen
	mov	ecx, eax
	test	eax, eax
	jz	$_027
	xor	eax, eax
	dec	rsi
$_024:	mov	al, byte ptr [rbx]
	repne scasb
	mov	al, 0
	jnz	$_027
	test	esi, esi
	jz	$_026
	cmp	ecx, esi
	jc	$_027
	mov	edx, esi
$_025:	mov	al, byte ptr [rbx+rdx]
	cmp	al, byte ptr [rdi+rdx-0x1]
	jnz	$_024
	dec	edx
	jnz	$_025
$_026:	lea	rax, [rdi-0x1]
$_027:	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret

tfwrite:
	push	rsi
	push	rdi
	push	rbp
	mov	rbp, rsp
	mov	esi, edx
	mov	rdi, rcx
	mov	rcx, r9
	mov	edx, r8d
	call	fwrite@PLT
	leave
	pop	rdi
	pop	rsi
	ret

tfseek:
	push	rsi
	push	rdi
	push	rbp
	mov	rbp, rsp
	mov	rsi, rdx
	mov	edx, r8d
	mov	rdi, rcx
	call	fseek@PLT
	leave
	pop	rdi
	pop	rsi
	ret


.SECTION .data
	.ALIGN	16

_ltype:
	.byte  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
	.byte  0x20, 0x28, 0x28, 0x28, 0x28, 0x28, 0x20, 0x20
	.byte  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
	.byte  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
	.byte  0x08, 0x10, 0x10, 0x10, 0x50, 0x10, 0x10, 0x10
	.byte  0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10
	.byte  0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84
	.byte  0x84, 0x84, 0x10, 0x10, 0x10, 0x10, 0x10, 0x50
	.byte  0x50, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0x41
	.byte  0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
	.byte  0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
	.byte  0x41, 0x41, 0x41, 0x10, 0x10, 0x10, 0x10, 0x50
	.byte  0x10, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0x42
	.byte  0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42
	.byte  0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42
	.byte  0x42, 0x42, 0x42, 0x10, 0x10, 0x10, 0x10, 0x20
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00


.att_syntax prefix
