
.intel_syntax noprefix

.global Mangle
.global SetMangler

.extern tstrupr
.extern tsprintf
.extern Options
.extern tstricmp
.extern tstrcpy


.SECTION .text
	.ALIGN	16

$_001:	mov	rdi, rdx
	mov	rsi, qword ptr [rcx+0x8]
	mov	eax, dword ptr [rcx+0x14]
	lea	ecx, [rax+0x1]
	rep movsb
	ret

$_002:
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	mov	rdi, rdx
	mov	rsi, qword ptr [rcx+0x8]
	mov	eax, dword ptr [rcx+0x14]
	lea	ecx, [rax+0x1]
	rep movsb
	mov	rsi, rax
	mov	rcx, rdx
	call	tstrupr@PLT
	mov	rax, rsi
	leave
	ret

$_003:
	lea	rdi, [rdx+0x1]
	mov	rsi, qword ptr [rcx+0x8]
	mov	eax, dword ptr [rcx+0x14]
	inc	eax
	mov	ecx, eax
	rep movsb
	mov	byte ptr [rdx], 95
	ret

$_004:
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	cmp	byte ptr [Options+0xAE+rip], 0
	jnz	$_006
	test	byte ptr [rcx+0x10], 0x02
	jz	$_006
	mov	rax, rdx
	mov	rdx, qword ptr [rcx+0x78]
	test	rdx, rdx
	jz	$_005
	mov	edx, dword ptr [rdx+0xC]
$_005:	mov	r9d, edx
	mov	r8, qword ptr [rcx+0x8]
	lea	rdx, [DS0000+rip]
	mov	rcx, rax
	call	tsprintf@PLT
	jmp	$_007

$_006:	call	$_003
$_007:	leave
	ret

$_008:
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	test	byte ptr [rcx+0x10], 0x02
	jz	$_010
	mov	rax, rdx
	mov	rdx, qword ptr [rcx+0x78]
	test	rdx, rdx
	jz	$_009
	mov	edx, dword ptr [rdx+0xC]
$_009:	mov	r9d, edx
	mov	r8, qword ptr [rcx+0x8]
	lea	rdx, [DS0001+rip]
	mov	rcx, rax
	call	tsprintf@PLT
	jmp	$_011

$_010:	call	$_003
$_011:	leave
	ret

$_012:
	mov	eax, 0
	test	byte ptr [rcx+0x10], 0x02
	jz	$_013
	or	eax, 0x02
	jmp	$_017

$_013:	jmp	$_016

$_014:	or	eax, 0x02
	jmp	$_017

$_015:	or	eax, 0x01
	jmp	$_017

$_016:	cmp	byte ptr [rcx+0x1E], 129
	jz	$_014
	cmp	byte ptr [rcx+0x1E], 130
	jz	$_014
	cmp	byte ptr [rcx+0x1E], 192
	jz	$_014
	jmp	$_015

$_017:
	mov	rdi, rdx
	test	eax, 0x1
	jz	$_018
	mov	byte ptr [rdi], 95
	inc	rdi
$_018:	mov	rsi, qword ptr [rcx+0x8]
	mov	ecx, dword ptr [rcx+0x14]
	inc	ecx
	rep movsb
	dec	rdi
	test	eax, 0x2
	jz	$_019
	mov	word ptr [rdi], 95
	inc	rdi
$_019:	sub	rdi, rdx
	mov	rax, rdi
	ret

$_020:
	mov	rdi, rdx
	mov	rsi, qword ptr [rcx+0x8]
	mov	eax, dword ptr [rcx+0x14]
	lea	ecx, [rax+0x1]
	rep movsb
	ret

$_021:
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	mov	rdi, rdx
	mov	rsi, rcx
	mov	eax, 1
	test	byte ptr [rcx+0x10], 0x02
	jnz	$_022
	xor	eax, eax
	jmp	$_023

$_022:	lea	rdx, [DS0002+rip]
	mov	rcx, qword ptr [rcx+0x8]
	call	tstricmp@PLT
	mov	rcx, rsi
$_023:	test	eax, eax
	jnz	$_024
	mov	rdx, qword ptr [rcx+0x8]
	mov	rcx, rdi
	call	tstrcpy@PLT
	mov	eax, dword ptr [rsi+0x14]
	jmp	$_026

$_024:	mov	rdx, qword ptr [rsi+0x78]
	test	rdx, rdx
	jz	$_025
	mov	edx, dword ptr [rdx+0xC]
$_025:	mov	r9d, edx
	mov	r8, qword ptr [rcx+0x8]
	lea	rdx, [DS0003+rip]
	mov	rcx, rdi
	call	tsprintf@PLT
$_026:	leave
	ret

Mangle:
	push	rsi
	push	rdi
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	mov	al, byte ptr [rcx+0x1F]
	and	eax, 0x0F
	jmp	$_037

$_027:	test	byte ptr [Options+0x5+rip], 0x04
	jnz	$_028
	call	$_003
	jmp	$_039

$_028:	jmp	$_038

$_029:	cmp	byte ptr [Options+0xAE+rip], 1
	jz	$_030
	call	$_004
	jmp	$_039

$_030:	jmp	$_038

$_031:	call	$_002
	jmp	$_039

$_032:	call	$_012
	jmp	$_039

$_033:	cmp	byte ptr [r14+0x4], 2
	jnz	$_034
	call	$_020
	jmp	$_039

	jmp	$_035

$_034:	cmp	byte ptr [r14+0x7], 1
	jnz	$_035
	jmp	$_032

$_035:	call	$_008
	jmp	$_039

$_036:	call	$_021
	jmp	$_039

	jmp	$_038

$_037:	cmp	eax, 1
	jz	$_027
	cmp	eax, 3
	jz	$_029
	cmp	eax, 4
	jz	$_031
	cmp	eax, 5
	jz	$_031
	cmp	eax, 6
	jz	$_031
	cmp	eax, 9
	jz	$_032
	cmp	eax, 7
	jz	$_033
	cmp	eax, 8
	jz	$_036
$_038:	call	$_001
$_039:	leave
	pop	rdi
	pop	rsi
	ret

SetMangler:
	test	edx, edx
	jz	$_040
	mov	byte ptr [rcx+0x1F], dl
$_040:	ret


.SECTION .data
	.ALIGN	16

DS0000:
	.byte  0x5F, 0x25, 0x73, 0x40, 0x25, 0x64, 0x00

DS0001:
	.byte  0x40, 0x25, 0x73, 0x40, 0x25, 0x75, 0x00

DS0002:
	.byte  0x6D, 0x61, 0x69, 0x6E, 0x00

DS0003:
	.byte  0x25, 0x73, 0x40, 0x40, 0x25, 0x75, 0x00


.att_syntax prefix
