
.intel_syntax noprefix

.global OrgDirective
.global AlignCurrOffset
.global AlignDirective

.extern StoreLine
.extern StoreState
.extern LstWrite
.extern AlignInStruct
.extern SetStructCurrentOffset
.extern CurrStruct
.extern EvalOperand
.extern GetCurrSegAlign
.extern SetCurrOffset
.extern GetCurrOffset
.extern FillDataBytes
.extern OutputByte
.extern asmerr
.extern Options
.extern Parse_Pass


.SECTION .text
	.ALIGN	16

OrgDirective:
	mov	qword ptr [rsp+0x8], rcx
	mov	qword ptr [rsp+0x10], rdx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 160
	inc	dword ptr [rbp+0x10]
	xor	ecx, ecx
	test	byte ptr [Options+0x6+rip], 0x10
	jz	$_001
	mov	ecx, 2
$_001:	mov	byte ptr [rsp+0x20], cl
	lea	r9, [rbp-0x68]
	mov	r8d, dword ptr [r14+0x64]
	mov	rdx, qword ptr [rbp+0x18]
	lea	rcx, [rbp+0x10]
	call	EvalOperand@PLT
	cmp	eax, -1
	je	$_010
	imul	ecx, dword ptr [rbp+0x10], 32
	add	rcx, qword ptr [rbp+0x18]
	cmp	byte ptr [rcx], 0
	jz	$_002
	mov	rdx, qword ptr [rcx+0x10]
	mov	ecx, 2008
	call	asmerr@PLT
	jmp	$_010

$_002:	cmp	qword ptr [CurrStruct+rip], 0
	jz	$_004
	cmp	dword ptr [rbp-0x2C], 0
	jnz	$_003
	mov	ecx, dword ptr [rbp-0x68]
	call	SetStructCurrentOffset@PLT
	jmp	$_010

$_003:	jmp	$_009

$_004:	cmp	qword ptr [r14+0x38], 0
	jnz	$_005
	mov	ecx, 2034
	call	asmerr@PLT
	jmp	$_010

$_005:	cmp	dword ptr [StoreState+rip], 0
	jnz	$_006
	cmp	dword ptr [Parse_Pass+rip], 0
	jnz	$_006
	mov	rcx, qword ptr [r14+0x1D8]
	call	StoreLine@PLT
$_006:	mov	rcx, qword ptr [r14+0x38]
	mov	rcx, qword ptr [rcx+0x78]
	cmp	dword ptr [Parse_Pass+rip], 0
	jnz	$_007
	cmp	qword ptr [rcx+0x28], 0
	jz	$_007
	mov	rdx, qword ptr [rcx+0x28]
	or	byte ptr [rdx+0x1B], 0x02
$_007:	cmp	dword ptr [rbp-0x2C], 0
	jnz	$_008
	xor	r9d, r9d
	xor	r8d, r8d
	mov	edx, dword ptr [rbp-0x68]
	mov	rcx, qword ptr [r14+0x38]
	call	SetCurrOffset@PLT
	jmp	$_010

	jmp	$_009

$_008:	cmp	dword ptr [rbp-0x2C], 1
	jnz	$_009
	test	byte ptr [rbp-0x28], 0x01
	jnz	$_009
	mov	rcx, qword ptr [rbp-0x18]
	mov	edx, dword ptr [rcx+0x30]
	add	edx, dword ptr [rbp-0x68]
	xor	r9d, r9d
	xor	r8d, r8d
	mov	rcx, qword ptr [r14+0x38]
	call	SetCurrOffset@PLT
	jmp	$_010

$_009:	mov	ecx, 2132
	call	asmerr@PLT
$_010:	leave
	ret

$_011:
	mov	qword ptr [rsp+0x8], rcx
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 40
	mov	rdi, qword ptr [r14+0x38]
	mov	rsi, qword ptr [rdi+0x78]
	test	byte ptr [rsi+0x1C], 0x20
	jnz	$_012
	mov	r9d, 1
	mov	r8d, 1
	mov	edx, dword ptr [rbp+0x28]
	mov	rcx, rdi
	call	SetCurrOffset@PLT
	jmp	$_020

$_012:	cmp	dword ptr [rsi+0x48], 1
	jz	$_013
	mov	edx, dword ptr [rbp+0x28]
	xor	ecx, ecx
	call	FillDataBytes@PLT
	jmp	$_020

$_013:	movzx	ebx, byte ptr [r14+0x4]
	lea	rcx, [NopLists+rip]
	mov	rsi, qword ptr [rcx+rbx*8]
	movzx	ebx, byte ptr [rsi]
	jmp	$_017

$_014:	mov	edi, 1
$_015:	cmp	edi, ebx
	ja	$_016
	mov	ecx, dword ptr [rsi+rdi]
	call	OutputByte@PLT
	inc	edi
	jmp	$_015

$_016:	sub	dword ptr [rbp+0x28], ebx
$_017:	cmp	dword ptr [rbp+0x28], ebx
	ja	$_014
	cmp	dword ptr [rbp+0x28], 0
	jz	$_020
	inc	rsi
	mov	edi, ebx
$_018:	cmp	edi, dword ptr [rbp+0x28]
	jle	$_019
	add	rsi, rdi
	dec	edi
	jmp	$_018

$_019:	cmp	edi, 0
	jle	$_020
	mov	ecx, dword ptr [rsi]
	call	OutputByte@PLT
	dec	edi
	inc	rsi
	jmp	$_019

$_020:
	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret

AlignCurrOffset:
	mov	qword ptr [rsp+0x8], rcx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 32
	call	GetCurrOffset@PLT
	mov	ecx, dword ptr [rbp+0x10]
	mov	edx, 1
	shl	edx, cl
	mov	ecx, edx
	cdq
	div	ecx
	test	edx, edx
	jz	$_021
	sub	ecx, edx
	call	$_011
$_021:	leave
	ret

AlignDirective:
	mov	qword ptr [rsp+0x8], rcx
	mov	qword ptr [rsp+0x10], rdx
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 168
	mov	qword ptr [rbp-0x8], 0
	mov	rbx, rdx
	imul	ecx, ecx, 32
	cmp	dword ptr [rbx+rcx+0x4], 611
	jne	$_031
	inc	dword ptr [rbp+0x18]
	mov	byte ptr [rsp+0x20], 2
	lea	r9, [rbp-0x70]
	mov	r8d, dword ptr [r14+0x64]
	mov	rdx, qword ptr [rbp+0x20]
	lea	rcx, [rbp+0x18]
	call	EvalOperand@PLT
	cmp	eax, -1
	jnz	$_022
	jmp	$_041

$_022:	cmp	dword ptr [rbp-0x34], 0
	jnz	$_026
	mov	ecx, 1
$_023:	cmp	ecx, dword ptr [rbp-0x70]
	jge	$_024
	shl	ecx, 1
	jmp	$_023

$_024:	cmp	ecx, dword ptr [rbp-0x70]
	jz	$_025
	mov	edx, dword ptr [rbp-0x70]
	mov	ecx, 2063
	call	asmerr@PLT
	jmp	$_041

$_025:	jmp	$_030

$_026:	cmp	dword ptr [rbp-0x34], -2
	jnz	$_029
	cmp	qword ptr [CurrStruct+rip], 0
	jz	$_027
	mov	rcx, qword ptr [CurrStruct+rip]
	mov	rcx, qword ptr [rcx+0x78]
	movzx	eax, byte ptr [rcx+0x10]
	mov	dword ptr [rbp-0x70], eax
	jmp	$_028

$_027:	call	GetCurrSegAlign@PLT
	mov	dword ptr [rbp-0x70], eax
$_028:	jmp	$_030

$_029:	mov	ecx, 2026
	call	asmerr@PLT
	jmp	$_041

$_030:	jmp	$_032

$_031:	cmp	dword ptr [rbx+rcx+0x4], 612
	jnz	$_032
	mov	dword ptr [rbp-0x70], 2
	inc	dword ptr [rbp+0x18]
$_032:	imul	ecx, dword ptr [rbp+0x18], 32
	add	rbx, rcx
	cmp	byte ptr [rbx], 0
	jz	$_033
	mov	rdx, qword ptr [rbx+0x10]
	mov	ecx, 2008
	call	asmerr@PLT
	jmp	$_041

$_033:	cmp	qword ptr [CurrStruct+rip], 0
	jz	$_034
	mov	ecx, dword ptr [rbp-0x70]
	call	AlignInStruct@PLT
	jmp	$_041

$_034:	cmp	dword ptr [StoreState+rip], 0
	jnz	$_035
	cmp	dword ptr [Parse_Pass+rip], 0
	jnz	$_035
	mov	rcx, qword ptr [r14+0x1D8]
	call	StoreLine@PLT
$_035:	call	GetCurrSegAlign@PLT
	cmp	eax, 0
	jg	$_036
	mov	ecx, 2034
	call	asmerr@PLT
	jmp	$_041

$_036:	cmp	dword ptr [rbp-0x70], eax
	jle	$_037
	cmp	dword ptr [Parse_Pass+rip], 0
	jnz	$_037
	mov	edx, dword ptr [rbp-0x70]
	mov	ecx, 2189
	call	asmerr@PLT
$_037:	call	GetCurrOffset@PLT
	mov	dword ptr [rbp-0x74], eax
	cdq
	div	dword ptr [rbp-0x70]
	mov	rax, qword ptr [r14+0x38]
	cmp	dword ptr [Parse_Pass+rip], 0
	jnz	$_038
	test	rax, rax
	jz	$_038
	mov	rax, qword ptr [rax+0x78]
	mov	rax, qword ptr [rax+0x28]
	test	rax, rax
	jz	$_038
	or	byte ptr [rax+0x1B], 0x02
$_038:	test	edx, edx
	jz	$_039
	sub	dword ptr [rbp-0x70], edx
	mov	ecx, dword ptr [rbp-0x70]
	call	$_011
$_039:	cmp	qword ptr [r14+0x1F8], 0
	jz	$_040
	xor	r8d, r8d
	mov	edx, dword ptr [rbp-0x74]
	xor	ecx, ecx
	call	LstWrite@PLT
$_040:	xor	eax, eax
$_041:	leave
	pop	rbx
	ret


.SECTION .data
	.ALIGN	16

NopList16:
	.byte  0x03, 0x2E, 0x8B, 0xC0, 0x8B, 0xC0, 0x90

NopList32:
	.byte  0x07, 0x8D, 0xA4, 0x24, 0x00, 0x00, 0x00, 0x00
	.byte  0x8D, 0x80, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x8D
	.byte  0x44, 0x20, 0x00, 0x8D, 0x44, 0x20, 0x00, 0x8D
	.byte  0x40, 0x00, 0x8B, 0xFF, 0x90

NopList64:
	.byte  0x07, 0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00
	.byte  0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00, 0x0F, 0x1F
	.byte  0x44, 0x00, 0x00, 0x0F, 0x1F, 0x40, 0x00, 0x0F
	.byte  0x1F, 0x00, 0x66, 0x90, 0x90

NopLists:
	.quad  NopList16
	.quad  NopList32
	.quad  NopList64


.att_syntax prefix
