

.intel_syntax noprefix

.global OmfFixGenFixModend
.global OmfFixGenFix

.extern szNull
.extern omf_GetGrpIdx
.extern GetSegIdx
.extern SymTables
.extern asmerr


.SECTION .text
	.ALIGN	16

$_001:	cmp	dx, 127
	jbe	$_002
	or	dh, 0x80
	mov	byte ptr [rax], dh
	inc	rax
$_002:	mov	byte ptr [rax], dl
	inc	rax
	ret

$_003:
	mov	word ptr [rax], dx
	add	rax, 2
	ret

$_004:
	mov	dword ptr [rax], edx
	add	rax, 4
	ret

$_005:
	mov	qword ptr [rsp+0x18], r8
	push	rbp
	mov	rbp, rsp
	jmp	$_007

$_006:	movzx	edx, word ptr [rbp+0x20]
	mov	rax, rcx
	call	$_001
	jmp	$_009

	jmp	$_008

$_007:	cmp	dl, 0
	jz	$_006
	cmp	dl, 1
	jz	$_006
	cmp	dl, 2
	jz	$_006
$_008:	mov	rax, rcx
$_009:	leave
	ret

$_010:
	mov	qword ptr [rsp+0x18], r8
	push	rbp
	mov	rbp, rsp
	movzx	edx, word ptr [rbp+0x20]
	mov	rax, rcx
	call	$_001
	leave
	ret

$_011:
	mov	qword ptr [rsp+0x10], rdx
	mov	qword ptr [rsp+0x18], r8
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 40
	mov	rsi, rcx
	mov	bl, byte ptr [rsi+0x5]
	cmp	dword ptr [rsi+0x8], 0
	jnz	$_012
	cmp	byte ptr [rsi+0x4], 0
	jz	$_012
	or	bl, 0x04
$_012:	mov	rdi, qword ptr [rbp+0x30]
	mov	al, byte ptr [rsi]
	shl	al, 4
	or	al, bl
	stosb
	movzx	r8d, word ptr [rsi+0x2]
	movzx	edx, byte ptr [rsi]
	mov	rcx, rdi
	call	$_005
	mov	rdi, rax
	movzx	r8d, word ptr [rsi+0x6]
	movzx	edx, bl
	mov	rcx, rdi
	call	$_010
	mov	rdi, rax
	test	bl, 0x04
	jnz	$_014
	mov	edx, dword ptr [rsi+0x8]
	cmp	dword ptr [rbp+0x38], 1
	jnz	$_013
	mov	rax, rdi
	call	$_004
	mov	rdi, rax
	jmp	$_014

$_013:	movzx	edx, dx
	mov	rax, rdi
	call	$_003
	mov	rdi, rax
$_014:	mov	rax, rdi
	sub	rax, qword ptr [rbp+0x30]
	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret

OmfFixGenFixModend:
	mov	qword ptr [rsp+0x10], rdx
	mov	qword ptr [rsp+0x18], r8
	mov	qword ptr [rsp+0x20], r9
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 48
	mov	rbx, rcx
	mov	rdi, qword ptr [rbx+0x30]
	mov	byte ptr [rbp-0xC], 0
	mov	eax, dword ptr [rdi+0x30]
	add	eax, dword ptr [rbp+0x30]
	mov	dword ptr [rbp-0x8], eax
	mov	ax, word ptr [rbx+0x22]
	mov	word ptr [rbp-0xE], ax
	cmp	byte ptr [rdi+0x1D], 2
	jnz	$_016
	mov	byte ptr [rbp-0xB], 2
	mov	ax, word ptr [rdi+0x70]
	mov	word ptr [rbp-0xA], ax
	cmp	byte ptr [rbx+0x20], 1
	jnz	$_015
	cmp	word ptr [rbx+0x22], 0
	jnz	$_015
	mov	rcx, rdi
	call	omf_GetGrpIdx@PLT
	mov	word ptr [rbp-0xE], ax
$_015:	jmp	$_017

$_016:	mov	byte ptr [rbp-0xB], 0
	mov	rcx, qword ptr [rdi+0x40]
	call	GetSegIdx@PLT
	mov	word ptr [rbp-0xA], ax
$_017:	cmp	byte ptr [rbx+0x20], 6
	jz	$_018
	cmp	byte ptr [rbx+0x20], 0
	jz	$_018
	mov	al, byte ptr [rbx+0x20]
	mov	byte ptr [rbp-0x10], al
	jmp	$_019

$_018:	mov	byte ptr [rbp-0x10], 5
$_019:	mov	r8d, dword ptr [rbp+0x38]
	mov	rdx, qword ptr [rbp+0x28]
	lea	rcx, [rbp-0x10]
	call	$_011
	leave
	pop	rbx
	pop	rdi
	ret

$_020:
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 40
	mov	rsi, rdx
	mov	rbx, rcx
	mov	rdi, qword ptr [rbx+0x30]
	test	rdi, rdi
	jne	$_027
	cmp	byte ptr [rbx+0x20], 6
	jnz	$_021
	xor	eax, eax
	jmp	$_043

$_021:	mov	rcx, qword ptr [r14+0x40]
	test	rcx, rcx
	jz	$_022
	mov	rcx, qword ptr [rcx+0x78]
	cmp	byte ptr [rbx+0x20], 1
	jnz	$_022
	mov	eax, dword ptr [rcx+0x8]
	cmp	word ptr [rbx+0x22], ax
	jnz	$_022
	xor	eax, eax
	jmp	$_043

$_022:	mov	al, byte ptr [rbx+0x20]
	mov	byte ptr [rsi+0x5], al
	mov	ax, word ptr [rbx+0x22]
	mov	word ptr [rsi+0x6], ax
	mov	byte ptr [rsi], 5
	cmp	byte ptr [rbx+0x20], 0
	jnz	$_026
	mov	rcx, qword ptr [SymTables+0x20+rip]
$_023:	test	rcx, rcx
	jz	$_026
	mov	rdx, qword ptr [rcx+0x78]
	movzx	eax, word ptr [rbx+0x22]
	cmp	dword ptr [rdx+0x44], eax
	jnz	$_025
	cmp	qword ptr [rdx], 0
	jz	$_024
	mov	byte ptr [rsi], 1
	mov	rcx, qword ptr [rdx]
	mov	eax, dword ptr [rcx+0x8]
	mov	word ptr [rsi+0x2], ax
$_024:	jmp	$_026

$_025:	mov	rcx, qword ptr [rcx+0x80]
	jmp	$_023

$_026:	jmp	$_041

$_027:	cmp	byte ptr [rdi+0x1D], 0
	jnz	$_028
	mov	rdx, qword ptr [rdi+0x8]
	mov	ecx, 2006
	call	asmerr@PLT
	xor	eax, eax
	jmp	$_043

	jmp	$_041

$_028:	cmp	byte ptr [rdi+0x1D], 4
	jnz	$_031
	mov	byte ptr [rsi+0x5], 5
	mov	rcx, qword ptr [rdi+0x78]
	mov	eax, dword ptr [rcx+0x8]
	mov	word ptr [rsi+0x6], ax
	cmp	byte ptr [rbx+0x20], 6
	jz	$_029
	mov	al, byte ptr [rbx+0x20]
	mov	byte ptr [rsi], al
	mov	ax, word ptr [rbx+0x22]
	mov	word ptr [rsi+0x2], ax
	jmp	$_030

$_029:	mov	byte ptr [rsi], 1
	mov	ax, word ptr [rsi+0x6]
	mov	word ptr [rsi+0x2], ax
$_030:	jmp	$_041

$_031:	cmp	byte ptr [rdi+0x1D], 3
	jnz	$_034
	mov	byte ptr [rsi+0x5], 4
	mov	rcx, rdi
	call	GetSegIdx@PLT
	mov	word ptr [rsi+0x6], ax
	cmp	byte ptr [rbx+0x20], 6
	jz	$_032
	mov	al, byte ptr [rbx+0x20]
	mov	byte ptr [rsi], al
	mov	ax, word ptr [rbx+0x22]
	mov	word ptr [rsi+0x2], ax
	jmp	$_033

$_032:	mov	byte ptr [rsi], 0
	mov	ax, word ptr [rsi+0x6]
	mov	word ptr [rsi+0x2], ax
$_033:	jmp	$_041

$_034:	mov	ax, word ptr [rbx+0x22]
	mov	word ptr [rsi+0x2], ax
	cmp	byte ptr [rdi+0x1D], 2
	jnz	$_036
	mov	byte ptr [rsi+0x5], 6
	mov	ax, word ptr [rdi+0x70]
	mov	word ptr [rsi+0x6], ax
	cmp	byte ptr [rbx+0x20], 1
	jnz	$_035
	cmp	word ptr [rbx+0x22], 0
	jnz	$_035
	mov	rcx, rdi
	call	omf_GetGrpIdx@PLT
	mov	word ptr [rsi+0x2], ax
$_035:	jmp	$_039

$_036:	cmp	qword ptr [rdi+0x40], 0
	jnz	$_037
	mov	rdx, qword ptr [rdi+0x8]
	mov	ecx, 3005
	call	asmerr@PLT
	xor	eax, eax
	jmp	$_043

	jmp	$_039

$_037:	mov	rcx, qword ptr [rdi+0x40]
	mov	rcx, qword ptr [rcx+0x78]
	cmp	byte ptr [rcx+0x5D], 0
	jz	$_038
	mov	byte ptr [rsi+0x5], 6
	mov	eax, dword ptr [rcx+0x44]
	mov	word ptr [rsi+0x6], ax
	mov	byte ptr [rsi], 5
	mov	eax, 1
	jmp	$_043

$_038:	mov	byte ptr [rsi+0x5], 4
	mov	rcx, qword ptr [rdi+0x40]
	call	GetSegIdx@PLT
	mov	word ptr [rsi+0x6], ax
$_039:	cmp	byte ptr [rbx+0x20], 6
	jz	$_040
	mov	al, byte ptr [rbx+0x20]
	mov	byte ptr [rsi], al
	jmp	$_041

$_040:	mov	byte ptr [rsi], 5
$_041:	mov	al, byte ptr [rsi+0x5]
	sub	al, 4
	cmp	byte ptr [rsi], al
	jnz	$_042
	mov	byte ptr [rsi], 5
$_042:	mov	eax, 1
$_043:	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret

OmfFixGenFix:
	mov	qword ptr [rsp+0x20], r9
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 56
	mov	rbx, rcx
	mov	rdi, r8
	mov	esi, edx
	mov	byte ptr [rbp-0xC], 1
	mov	dword ptr [rbp-0x8], 0
	movzx	eax, byte ptr [rbx+0x18]
	xor	ecx, ecx
	jmp	$_047

	mov	ch, 1
	mov	cl, 0
	jmp	$_048

	mov	ch, 1
	mov	cl, 4
	test	byte ptr [rbx+0x1B], 0x01
	jz	$_044
	mov	cl, 20
$_044:	jmp	$_048

	mov	ch, 1
	mov	cl, 36
	test	byte ptr [rbx+0x1B], 0x01
	jz	$_045
	mov	cl, 52
$_045:	jmp	$_048

	mov	cl, 16
	jmp	$_048

	mov	cl, 8
	jmp	$_048

	mov	cl, 12
	jmp	$_048

	mov	cl, 44
	jmp	$_048

LC003D: mov	rcx, qword ptr [r14+0x1B8]
	lea	rax, [szNull+rip]
	cmp	qword ptr [rbx+0x30], 0
	jz	$_046
	mov	rax, qword ptr [rbx+0x30]
	mov	rax, qword ptr [rax+0x8]
$_046:	mov	r8, rax
	lea	rdx, [rcx+0xA]
	mov	ecx, 3001
	call	asmerr@PLT
	xor	eax, eax
	jmp	$_051

	jmp	$_048

$_047:	cmp	eax, 1
	jl	LC003D
	cmp	eax, 11
	jg	LC003D
	lea	r11, [LC0040+rip]
	movzx	r10, byte ptr [r11+rax+0xA]
	movzx	r10, byte ptr [r11+r10]
	lea	r11, [LC003D+rip]
	sub	r11, r10
	jmp	r11

$_048:	mov	al, 192
	test	ch, ch
	jz	$_049
	mov	al, 128
$_049:	or	al, cl
	mov	byte ptr [rbp-0x11], al
	lea	rdx, [rbp-0x10]
	mov	rcx, rbx
	call	$_020
	test	eax, eax
	jnz	$_050
	jmp	$_051

$_050:	mov	ecx, dword ptr [rbx+0x14]
	sub	ecx, esi
	mov	al, byte ptr [rbp-0x11]
	or	al, ch
	mov	ah, cl
	stosw
	mov	r8d, dword ptr [rbp+0x40]
	mov	rdx, rdi
	lea	rcx, [rbp-0x10]
	call	$_011
	lea	rax, [rax+0x2]
$_051:	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret


.SECTION .data
	.ALIGN	16

LC0040:
	.byte  0x35, 0x33, 0x2C, 0x2A, 0x1E, 0x1C, 0x10, 0x0C
	.byte  0x08, 0x04, 0x00, 0x00, 0x02, 0x04, 0x01, 0x03
	.byte  0x05, 0x0A, 0x07, 0x08, 0x09, 0x06


.att_syntax prefix
