

.intel_syntax noprefix

.global DeleteLineQueue
.global tvsprintf
.global tvfprintf
.global tfprintf
.global tsprintf
.global tprintf
.global AddLineQueue
.global AddLineQueueX
.global RunLineQueue
.global InsertLineQueue

.extern PreprocessLine
.extern ParseLine
.extern PopInputStatus
.extern PushInputStatus
.extern ResWordNames
.extern ResWordTable
.extern MemFree
.extern MemAlloc
.extern tstrcpy
.extern fwrite
.extern write


.SECTION .text
	.ALIGN	16

DeleteLineQueue:
	push	rsi
	push	rdi
	sub	rsp, 40
	mov	rsi, qword ptr [r14+0x2B0]
$_001:	test	rsi, rsi
	jz	$_002
	mov	rdi, qword ptr [rsi]
	mov	rcx, rsi
	call	MemFree@PLT
	mov	rsi, rdi
	jmp	$_001

$_002:
	mov	qword ptr [r14+0x2B0], 0
	add	rsp, 40
	pop	rdi
	pop	rsi
	ret

tvsprintf:
	mov	qword ptr [rsp+0x8], rcx
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 120
	mov	rdi, rcx
	mov	rsi, rdx
	mov	rbx, r8
$_003:	cmp	byte ptr [rsi], 0
	je	$_030
	lodsb
	cmp	al, 37
	jne	$_028
	mov	byte ptr [rbp-0xE], 32
	mov	dword ptr [rbp-0x4], 0
	mov	byte ptr [rbp-0xD], 0
	mov	byte ptr [rbp-0xF], 0
	mov	dword ptr [rbp-0x8], 10
	lodsb
	jmp	$_026

	mov	rdx, qword ptr [rbx]
	test	rdx, rdx
	jz	$_004
	xchg	rdi, rdx
	mov	ecx, 4294967295
	xor	eax, eax
	repne scasb
	mov	eax, ecx
	mov	rdi, rdx
	not	eax
	dec	eax
	mov	rdx, qword ptr [rbx]
	add	rbx, 8
	jmp	$_012

$_004:	mov	ecx, dword ptr [rbx]
	add	rbx, 8
	lea	rdx, [ResWordTable+rip]
	cmp	ecx, 343
	movzx	eax, byte ptr [rdx+rcx*8+0x2]
	lea	rdx, [ResWordNames+rip]
	mov	rdx, qword ptr [rdx+rcx*8]
	jnz	$_012
	mov	eax, 1
	jmp	$_012

	mov	rax, qword ptr [rbx]
	add	rbx, 8
	cmp	byte ptr [rbp-0xF], 0
	jnz	$_005
	movsxd	rax, eax
$_005:	test	rax, rax
	jns	$_006
	mov	byte ptr [rdi], 45
	inc	rdi
	neg	rax
$_006:	mov	ecx, 63
	cmp	rax, 9
	ja	$_007
	add	al, 48
	mov	byte ptr [rbp+rcx-0x50], al
	jmp	$_011

$_007:	mov	r8d, dword ptr [rbp-0x8]
$_008:	test	rax, rax
	jz	$_010
	xor	edx, edx
	div	r8
	add	dl, 48
	cmp	dl, 57
	jbe	$_009
	add	dl, byte ptr [rbp-0x10]
$_009:	mov	byte ptr [rbp+rcx-0x50], dl
	dec	ecx
	jmp	$_008

$_010:	inc	ecx
$_011:	mov	eax, 64
	sub	eax, ecx
	lea	rdx, [rbp+rcx-0x50]
$_012:	mov	dword ptr [rbp-0xC], eax
	cmp	byte ptr [rbp-0xD], 0
	jnz	$_013
	cmp	eax, dword ptr [rbp-0x4]
	jnc	$_013
	mov	ecx, dword ptr [rbp-0x4]
	sub	ecx, eax
	mov	al, byte ptr [rbp-0xE]
	rep stosb
$_013:	mov	ecx, dword ptr [rbp-0xC]
	xchg	rdx, rsi
	mov	eax, ecx
	rep movsb
	mov	rsi, rdx
	cmp	byte ptr [rbp-0xD], 0
	jz	$_014
	cmp	eax, dword ptr [rbp-0x4]
	jnc	$_014
	mov	ecx, dword ptr [rbp-0x4]
	sub	ecx, eax
	mov	al, byte ptr [rbp-0xE]
	rep stosb
$_014:	mov	al, byte ptr [rsi-0x1]
	cmp	dword ptr [rbp-0x8], 10
	jz	$_016
	cmp	al, 100
	jz	$_015
	cmp	al, 117
	jnz	$_016
$_015:	mov	al, 116
	stosb
$_016:	jmp	$_027

	mov	rax, qword ptr [rbx]
	add	rbx, 8
	cmp	byte ptr [rbp-0xF], 0
	jnz	$_017
	mov	eax, eax
$_017:	jmp	$_006

	mov	byte ptr [rbp-0x10], 39
$_018:	cmp	al, 88
	jnz	$_019
	mov	byte ptr [rbp-0x10], 7
$_019:	mov	dword ptr [rbp-0x8], 16
	mov	rax, qword ptr [rbx]
	add	rbx, 8
	cmp	byte ptr [rbp-0xF], 0
	jnz	$_020
	mov	eax, eax
$_020:	jmp	$_006

	mov	al, 88
	mov	dword ptr [rbp-0x4], 16
	mov	byte ptr [rbp-0xE], 48
	inc	byte ptr [rbp-0xF]
	jmp	$_018

	mov	eax, dword ptr [rbx]
	add	rbx, 8
	cmp	eax, 0
	jge	$_021
	mov	byte ptr [rbp-0xD], 45
	neg	eax
$_021:	mov	dword ptr [rbp-0x4], eax
	lodsb
	jmp	$_026

	mov	byte ptr [rbp-0xD], al
	lodsb
	jmp	$_026

$_022:	mov	byte ptr [rbp-0xE], al
	lodsb
	jmp	$_026

	cmp	byte ptr [rbp-0xE], 32
	jz	$_022
$_023:	movsx	eax, al
	imul	edx, dword ptr [rbp-0x4], 10
	add	eax, edx
	add	eax, -48
	mov	dword ptr [rbp-0x4], eax
	lodsb
	cmp	al, 48
	jc	$_024
	cmp	al, 57
	jbe	$_023
$_024:	jmp	$_026

	mov	byte ptr [rbp-0xF], al
	lodsb
	cmp	al, 108
	jnz	$_025
	lodsb
$_025:	jmp	$_026

	mov	eax, dword ptr [rbx]
	add	rbx, 8
	test	al, al
	jz	$_027
LC0037: stosb
	jmp	$_027

$_026:	cmp	al, 42
	jl	LC0037
	cmp	al, 120
	jg	LC0037
	movsx	r10, al
	lea	r11, [LC0038+rip]
	movzx	r10, byte ptr [r11+r10-0xA]
	movzx	r10, word ptr [r11+r10*2]
	lea	r11, [LC0037+rip]
	sub	r11, r10
	jmp	r11

$_027:	jmp	$_029

$_028:	stosb
$_029:	jmp	$_003

$_030:
	mov	byte ptr [rdi], 0
	mov	rax, rdi
	sub	rax, qword ptr [rbp+0x28]
	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret

tvfprintf:
	mov	qword ptr [rsp+0x8], rcx
	mov	qword ptr [rsp+0x10], rdx
	mov	qword ptr [rsp+0x18], r8
	push	rsi
	push	rdi
	push	rbp
	mov	rbp, rsp
	sub	rsp, 4128
	mov	r8, qword ptr [rbp+0x30]
	mov	rdx, qword ptr [rbp+0x28]
	lea	rcx, [rbp-0x1000]
	call	tvsprintf
	mov	rcx, qword ptr [rbp+0x20]
	mov	edx, eax
	mov	esi, 1
	lea	rdi, [rbp-0x1000]
	call	fwrite@PLT
	leave
	pop	rdi
	pop	rsi
	ret

tfprintf:
	mov	qword ptr [rsp+0x18], r8
	mov	qword ptr [rsp+0x20], r9
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	lea	r8, [rbp+0x20]
	call	tvfprintf
	leave
	ret

tsprintf:
	mov	qword ptr [rsp+0x18], r8
	mov	qword ptr [rsp+0x20], r9
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	lea	r8, [rbp+0x20]
	call	tvsprintf
	leave
	ret

tprintf:
	mov	qword ptr [rsp+0x8], rcx
	mov	qword ptr [rsp+0x10], rdx
	mov	qword ptr [rsp+0x18], r8
	mov	qword ptr [rsp+0x20], r9
	push	rsi
	push	rdi
	push	rbp
	mov	rbp, rsp
	sub	rsp, 4128
	lea	r8, [rbp+0x28]
	mov	rdx, qword ptr [rbp+0x20]
	lea	rcx, [rbp-0x1000]
	call	tvsprintf
	mov	edx, eax
	lea	rsi, [rbp-0x1000]
	mov	edi, 1
	call	write@PLT
	leave
	pop	rdi
	pop	rsi
	ret

AddLineQueue:
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 40
	mov	rsi, rcx
	mov	rdi, rsi
	xor	eax, eax
	mov	ecx, 4294967295
	repne scasb
	not	ecx
	mov	ebx, ecx
	dec	ebx
	jz	$_034
$_031:	mov	rdi, rsi
	mov	ecx, ebx
	mov	eax, 10
	repne scasb
	sete	al
	sub	rdi, rax
	mov	rcx, rdi
	sub	rcx, rsi
	sub	ebx, ecx
	test	ecx, ecx
	jz	$_033
	add	ecx, 16
	call	MemAlloc@PLT
	xor	ecx, ecx
	mov	qword ptr [rax], rcx
	cmp	rcx, qword ptr [r14+0x2B0]
	jz	$_035
	mov	rcx, qword ptr [r14+0x2B8]
	mov	qword ptr [rcx], rax
$_032:	mov	qword ptr [r14+0x2B8], rax
	mov	rcx, rdi
	sub	rcx, rsi
	lea	rdi, [rax+0x8]
	rep movsb
	mov	byte ptr [rdi], cl
$_033:	inc	rsi
	dec	ebx
	jg	$_031
$_034:	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret

$_035:
	mov	qword ptr [r14+0x2B0], rax
	jmp	$_032

AddLineQueueX:
	mov	qword ptr [rsp+0x8], rcx
	mov	qword ptr [rsp+0x10], rdx
	mov	qword ptr [rsp+0x18], r8
	mov	qword ptr [rsp+0x20], r9
	push	rbp
	mov	rbp, rsp
	sub	rsp, 48
	mov	byte ptr [rbp-0x9], 0
	mov	ecx, dword ptr [r14+0x234]
	cmp	ecx, 2048
	jbe	$_036
	mov	byte ptr [rbp-0x9], 1
	call	MemAlloc@PLT
	jmp	$_037

$_036:	mov	eax, ecx
	add	eax, 15
	and	eax, 0xFFFFFFF0
	sub	rsp, rax
	lea	rax, [rsp+0x20]
$_037:	mov	qword ptr [rbp-0x8], rax
	lea	r8, [rbp+0x18]
	mov	rdx, qword ptr [rbp+0x10]
	mov	rcx, qword ptr [rbp-0x8]
	call	tvsprintf
	mov	rcx, qword ptr [rbp-0x8]
	call	AddLineQueue
	cmp	byte ptr [rbp-0x9], 0
	jz	$_038
	mov	rcx, qword ptr [rbp-0x8]
	call	MemFree@PLT
$_038:	leave
	ret

RunLineQueue:
	push	rsi
	push	rdi
	push	rbp
	mov	rbp, rsp
	sub	rsp, 80
	lea	rcx, [rbp-0x20]
	call	PushInputStatus@PLT
	mov	qword ptr [rbp-0x28], rax
	inc	dword ptr [r14+0x60]
	mov	rsi, qword ptr [r14+0x2B0]
	mov	qword ptr [r14+0x2B0], 0
$_039:	test	rsi, rsi
	jz	$_041
	lea	rdx, [rsi+0x8]
	mov	rcx, qword ptr [r14+0x1D8]
	call	tstrcpy@PLT
	mov	rcx, qword ptr [rbp-0x28]
	call	PreprocessLine@PLT
	test	rax, rax
	jz	$_040
	mov	rcx, qword ptr [rbp-0x28]
	call	ParseLine@PLT
$_040:	mov	rcx, rsi
	mov	rsi, qword ptr [rsi]
	call	MemFree@PLT
	jmp	$_039

$_041:
	dec	dword ptr [r14+0x60]
	lea	rcx, [rbp-0x20]
	call	PopInputStatus@PLT
	leave
	pop	rdi
	pop	rsi
	ret

InsertLineQueue:
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 72
	mov	ebx, dword ptr [r14+0x60]
	lea	rcx, [rbp-0x20]
	call	PushInputStatus@PLT
	mov	qword ptr [rbp-0x28], rax
	mov	dword ptr [r14+0x60], 0
	mov	rsi, qword ptr [r14+0x2B0]
	mov	qword ptr [r14+0x2B0], 0
$_042:	test	rsi, rsi
	jz	$_044
	lea	rdx, [rsi+0x8]
	mov	rcx, qword ptr [r14+0x1D8]
	call	tstrcpy@PLT
	mov	rcx, qword ptr [rbp-0x28]
	call	PreprocessLine@PLT
	test	rax, rax
	jz	$_043
	mov	rcx, qword ptr [rbp-0x28]
	call	ParseLine@PLT
$_043:	mov	rcx, rsi
	mov	rsi, qword ptr [rsi]
	call	MemFree@PLT
	jmp	$_042

$_044:
	mov	dword ptr [r14+0x60], ebx
	lea	rcx, [rbp-0x20]
	call	PopInputStatus@PLT
	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret


.SECTION .data
	.ALIGN	16

LC0038:
	.byte  0xAD, 0x01, 0x84, 0x01, 0x58, 0x01, 0x3A, 0x01
	.byte  0xA5, 0x00, 0x91, 0x00, 0x8D, 0x00, 0x6A, 0x00
	.byte  0x58, 0x00, 0x41, 0x00, 0x3B, 0x00, 0x35, 0x00
	.byte  0x2F, 0x00, 0x15, 0x00, 0x0A, 0x00, 0x00, 0x00
	.byte  0x08, 0x0F, 0x0F, 0x09, 0x0A, 0x0F, 0x0B, 0x0C
	.byte  0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C
	.byte  0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F
	.byte  0x0F, 0x0F, 0x03, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F
	.byte  0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F
	.byte  0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x06, 0x0F
	.byte  0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F
	.byte  0x0F, 0x0E, 0x02, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F
	.byte  0x0F, 0x0F, 0x0D, 0x0F, 0x0F, 0x0F, 0x07, 0x0F
	.byte  0x01, 0x00, 0x0F, 0x04, 0x0F, 0x0F, 0x05, 0x00


.att_syntax prefix
