

.intel_syntax noprefix

.global omf_write_record

.extern asmerr
.extern WriteError
.extern fwrite


.SECTION .text
	.ALIGN	16

$_001:	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	cmp	ax, 127
	jbe	$_002
	or	ah, 0x80
	mov	byte ptr [rbx+rcx+0x7], ah
	inc	dword ptr [rbx]
	inc	ecx
$_002:	mov	byte ptr [rbx+rcx+0x7], al
	ret

$_003:
	push	rdi
	sub	rsp, 8
	mov	rdi, rax
	movzx	eax, word ptr [rdi]
	call	$_001
	movzx	eax, word ptr [rdi+0x2]
	call	$_001
	cmp	word ptr [rdi], 0
	jnz	$_004
	cmp	word ptr [rdi+0x2], 0
	jnz	$_004
	movzx	eax, word ptr [rdi+0x4]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 2
	mov	word ptr [rbx+rcx+0x7], ax
$_004:	add	rsp, 8
	pop	rdi
	ret

omf_write_record:
	push	rsi
	push	rdi
	push	rbx
	push	rbp
	mov	rbp, rsp
	sub	rsp, 4136
	mov	dword ptr [rbp-0xFF8], 0
	lea	rbx, [rbp-0xFF8]
	mov	rsi, rcx
	movzx	ecx, byte ptr [rsi+0x10]
	sub	ecx, 128
	shr	ecx, 1
	lea	rax, [func_index+rip]
	movzx	eax, byte ptr [rax+rcx]
	jmp	$_018

	mov	ecx, 1901
	call	asmerr@PLT
$_005:	mov	al, byte ptr [rsi+0x10]
	mov	byte ptr [rbp-0xFF4], al
	jmp	LC0006

	mov	al, byte ptr [rsi+0x10]
	or	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF4], al
	jmp	LC0006

	mov	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF9], al
	add	al, -104
	mov	byte ptr [rbp-0xFF4], al
	mov	al, byte ptr [rsi+0x18]
	shl	al, 2
	or	al, byte ptr [rsi+0x16]
	mov	cl, byte ptr [rsi+0x17]
	mov	byte ptr [rbp-0xFFA], cl
	shl	cl, 5
	or	al, cl
	cmp	byte ptr [rbp-0xFF9], 0
	jnz	$_006
	cmp	dword ptr [rsi+0x24], 65536
	jnz	$_006
	or	al, 0x02
$_006:	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	cmp	byte ptr [rbp-0xFFA], 0
	jnz	$_007
	movzx	eax, word ptr [rsi+0x1C]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 2
	mov	word ptr [rbx+rcx+0x7], ax
	movzx	eax, byte ptr [rsi+0x20]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
$_007:	cmp	byte ptr [rbp-0xFF9], 0
	jz	$_008
	mov	eax, dword ptr [rsi+0x24]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 4
	mov	dword ptr [rbx+rcx+0x7], eax
	jmp	$_009

$_008:	movzx	eax, word ptr [rsi+0x24]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 2
	mov	word ptr [rbx+rcx+0x7], ax
$_009:	movzx	eax, word ptr [rsi+0x28]
	call	$_001
	movzx	eax, word ptr [rsi+0x2A]
	call	$_001
	movzx	eax, word ptr [rsi+0x2C]
	call	$_001
	jmp	$_020

	mov	al, byte ptr [rsi+0x10]
	add	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF4], al
	movzx	eax, word ptr [rsi+0x14]
	call	$_001
	cmp	byte ptr [rsi+0x11], 0
	jz	$_010
	mov	eax, dword ptr [rsi+0x18]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 4
	mov	dword ptr [rbx+rcx+0x7], eax
	jmp	$_011

$_010:	movzx	eax, word ptr [rsi+0x18]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 2
	mov	word ptr [rbx+rcx+0x7], ax
$_011:	jmp	LC0006

	mov	byte ptr [rbp-0xFF4], 136
	movzx	eax, byte ptr [rsi+0x14]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	movzx	eax, byte ptr [rsi+0x15]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	jmp	LC0006

	jmp	$_005

	xor	eax, eax
	cmp	byte ptr [rsi+0x11], 0
	jz	$_012
	cmp	byte ptr [rsi+0x15], 0
	jz	$_012
	inc	eax
$_012:	add	eax, 138
	mov	byte ptr [rbp-0xFF4], al
	xor	eax, eax
	cmp	byte ptr [rsi+0x14], 0
	jz	$_013
	mov	eax, 128
$_013:	cmp	byte ptr [rsi+0x15], 0
	jz	$_014
	or	eax, 0x41
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	jmp	LC0006

$_014:	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	jmp	$_020

	mov	al, byte ptr [rsi+0x10]
	add	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF4], al
	lea	rax, [rsi+0x14]
	call	$_003
	jmp	LC0006

	mov	al, 148
	add	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF4], al
	lea	rax, [rsi+0x14]
	call	$_003
	jmp	LC0006

	mov	al, byte ptr [rsi+0x10]
	add	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF4], al
	movzx	eax, byte ptr [rsi+0x14]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	movzx	eax, byte ptr [rsi+0x15]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	movzx	eax, byte ptr [rsi+0x16]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	cmp	byte ptr [rsi+0x11], 0
	jz	$_015
	mov	eax, dword ptr [rsi+0x18]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 4
	mov	dword ptr [rbx+rcx+0x7], eax
	jmp	$_016

$_015:	movzx	eax, word ptr [rsi+0x18]
	mov	ecx, dword ptr [rbx]
	add	dword ptr [rbx], 2
	mov	word ptr [rbx+rcx+0x7], ax
$_016:	movzx	eax, word ptr [rsi+0x1C]
	call	$_001
	mov	al, byte ptr [rsi+0x15]
	and	eax, 0x0F
	test	eax, eax
	jnz	$_017
	lea	rax, [rsi+0x1E]
	call	$_003
$_017:	movzx	eax, word ptr [rsi+0x24]
	call	$_001
	jmp	LC0006

	mov	al, 196
	add	al, byte ptr [rsi+0x11]
	mov	byte ptr [rbp-0xFF4], al
	movzx	eax, byte ptr [rsi+0x14]
	mov	ecx, dword ptr [rbx]
	inc	dword ptr [rbx]
	mov	byte ptr [rbx+rcx+0x7], al
	movzx	eax, word ptr [rsi+0x16]
	call	$_001
	jmp	LC0006

$_018:	cmp	eax, 11
	ja	LC0006
	lea	r11, [LC001D+rip]
	movzx	r10, word ptr [r11+rax*2]
	lea	r11, [LC0006+rip]
	sub	r11, r10
	jmp	r11

LC0006: mov	ecx, dword ptr [rsi]
	mov	eax, 4079
	sub	eax, dword ptr [rbp-0xFF8]
	cmp	ecx, eax
	jg	$_019
	mov	rsi, qword ptr [rsi+0x8]
	lea	rdi, [rbp-0xFF1]
	mov	eax, dword ptr [rbp-0xFF8]
	add	rdi, rax
	add	dword ptr [rbp-0xFF8], ecx
	rep movsb
	jmp	$_020

$_019:	mov	ecx, 1901
	call	asmerr@PLT
$_020:	mov	eax, dword ptr [rbp-0xFF8]
	inc	eax
	mov	word ptr [rbp-0xFF3], ax
	add	al, ah
	add	al, byte ptr [rbp-0xFF4]
	lea	rdx, [rbp-0xFF1]
	mov	ecx, dword ptr [rbp-0xFF8]
	add	rcx, rdx
$_021:	cmp	rdx, rcx
	jnc	$_022
	add	al, byte ptr [rdx]
	inc	rdx
	jmp	$_021

$_022:
	neg	al
	mov	byte ptr [rdx], al
	mov	ebx, dword ptr [rbp-0xFF8]
	add	ebx, 4
	mov	rcx, qword ptr [r14+0x1F0]
	mov	edx, ebx
	mov	esi, 1
	lea	rdi, [rbp-0xFF4]
	call	fwrite@PLT
	cmp	rax, rbx
	jz	$_023
	call	WriteError@PLT
$_023:	leave
	pop	rbx
	pop	rdi
	pop	rsi
	ret


.SECTION .data
	.ALIGN	16

func_index:
	.byte  0x06, 0x00, 0x00, 0x00, 0x05, 0x07, 0x01, 0x00
	.byte  0x08, 0x00, 0x09, 0x01, 0x03, 0x01, 0x02, 0x00
	.byte  0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	.byte  0x01, 0x02, 0x01, 0x08, 0x01, 0x00, 0x01, 0x00
	.byte  0x00, 0x0A, 0x0B, 0x01, 0x02, 0x01

LC001D:
	.byte  0x71, 0x02, 0x67, 0x02, 0x59, 0x02, 0x48, 0x02
	.byte  0x9C, 0x01, 0x60, 0x01, 0x3C, 0x01, 0x37, 0x01
	.byte  0xEC, 0x00, 0xD2, 0x00, 0xB9, 0x00, 0x40, 0x00


.att_syntax prefix
