// File: /workspace/github_cpp_makefile_star2_O2/ksherlock[P]wdc-utils/omf.cpp
// Function: save_omf(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<omf::segment, std::allocator<omf::segment>>&, unsigned int)
void save_omf(const std::string &path, std::vector<omf::segment> &segments, unsigned flags) {

	// expressload doesn't support links to other files. 
	// fortunately, we don't either.

	std::vector<uint8_t> expr_headers;
	std::vector<unsigned> expr_offsets;


	bool compress = !(flags & OMF_NO_COMPRESS);
	bool super = !(flags & OMF_NO_SUPER);
	bool expressload = !(flags & OMF_NO_EXPRESS);
	bool v1 = flags & OMF_V1;

	if (v1) {
		expressload = false;
		super = false;
	}

	int fd;
	fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
	if (fd < 0) {
		err(EX_CANTCREAT, "Unable to open %s", path.c_str());
	}


	uint32_t offset = 0;
	if (expressload) {
		for (auto &s : segments) {
			s.segnum++;
			for (auto &r : s.intersegs) r.segment++;
		}

		// calculate express load segment size.
		// sizeof includes the trailing 0, so no need to add in byte size.
		offset = sizeof(omf_header) + 10 + sizeof("~ExpressLoad");

		offset += 6; // lconst + end
		offset += 6;  // header.
		for (auto &s : segments) {
			offset += 8 + 2;
			offset += sizeof(omf_express_header) + 10;
			offset += s.segname.length() + 1;
		}

		lseek(fd, offset, SEEK_SET);
	}


	for (auto &s : segments) {
		omf_header h;
		h.length = s.data.size() + s.reserved_space;
		h.kind = s.kind;
		h.banksize = s.data.size() > 0xffff ? 0x0000 : 0x010000;
		h.segnum = s.segnum;
		h.alignment = s.alignment;
		h.reserved_space = s.reserved_space;
		h.org = s.org;

		uint32_t reserved_space = 0;
		if (expressload) {
			std::swap(reserved_space, h.reserved_space);
		}

		// length field INCLUDES reserved space.  Express expand reserved space.


		std::vector<uint8_t> data;

		// push segname and load name onto data.
		// data.insert(data.end(), 10, ' ');
		push(data, s.loadname, 10);
		push(data, s.segname);

		h.dispname = sizeof(omf_header);
		h.dispdata = sizeof(omf_header) + data.size();



		uint32_t lconst_offset = offset + sizeof(omf_header) + data.size() + 5;
		uint32_t lconst_size = s.data.size() + reserved_space;


		//lconst record
		push(data, (uint8_t)omf::LCONST);
		push(data, (uint32_t)lconst_size);

		size_t data_offset = data.size();

		data.insert(data.end(), s.data.begin(), s.data.end());

		if (reserved_space) {
			data.insert(data.end(), reserved_space, 0);
		}

		uint32_t reloc_offset = offset + sizeof(omf_header) + data.size();
		uint32_t reloc_size = 0;

		reloc_size = add_relocs(data, data_offset, s, compress, super);

		// end-of-record
		push(data, (uint8_t)omf::END);

		h.bytecount = data.size() + sizeof(omf_header);

		if (expressload) {

			expr_offsets.emplace_back(expr_headers.size());

			if (lconst_size == 0) lconst_offset = 0;
			if (reloc_size == 0) reloc_offset = 0;


			push(expr_headers, (uint32_t)lconst_offset);
			push(expr_headers, (uint32_t)lconst_size);
			push(expr_headers, (uint32_t)reloc_offset);
			push(expr_headers, (uint32_t)reloc_size);

			push(expr_headers, h.unused1);
			push(expr_headers, h.lablen);
			push(expr_headers, h.numlen);
			push(expr_headers, h.version);
			push(expr_headers, h.banksize);
			push(expr_headers, h.kind);
			push(expr_headers, h.unused2);
			push(expr_headers, h.org);
			push(expr_headers, h.alignment);
			push(expr_headers, h.numsex);
			push(expr_headers, h.unused3);
			push(expr_headers, h.segnum);
			push(expr_headers, h.entry);
			push(expr_headers, (uint16_t)(h.dispname));
			push(expr_headers, h.dispdata);

			expr_headers.insert(expr_headers.end(), 10, ' ');
			push(expr_headers, s.segname);
		}

		if (v1) to_v1(h);
		to_little(h);

		offset += write(fd, &h, sizeof(h));
		offset += write(fd, data.data(), data.size());

		// version 1 needs 512-byte padding for all but final segment.
		if (v1 && &s != &segments.back()) {
			static uint8_t zero[512];
			offset += write(fd, zero, 512 - (offset & 511) );
		}
	}

	if (expressload) {
		omf_header h;
		h.segnum = 1;
		h.banksize = 0x00010000;
		h.kind = 0x8001;
		h.dispname = 0x2c;
		h.dispdata = 0x43;

		unsigned fudge = 10 * segments.size();

		h.length = 6 + expr_headers.size() + fudge;

		std::vector<uint8_t> data;
		data.insert(data.begin(), 10, ' ');
		push(data, std::string("~ExpressLoad"));
		push(data, (uint8_t)0xf2); // lconst.
		push(data, (uint32_t)h.length);

		push(data, (uint32_t)0); // reserved
		push(data, (uint16_t)(segments.size() - 1)); // seg count - 1


		for (auto &offset : expr_offsets) {
			push(data, (uint16_t)(fudge + offset));
			push(data, (uint16_t)0);
			push(data, (uint32_t)0);
			fudge -= 8;
		}

		for (auto &s : segments) {
			push(data, (uint16_t)s.segnum);
		}

		data.insert(data.end(), expr_headers.begin(), expr_headers.end());
		push(data, (uint8_t)0); // end.

		h.bytecount = data.size() + sizeof(omf_header);

		to_little(h);
		lseek(fd, 0, SEEK_SET);
		write(fd, &h, sizeof(h));
		write(fd, data.data(), data.size());

	}

	close(fd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edx, %r14d
movq %rsi, 0x78(%rsp)
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movaps %xmm0, 0xb0(%rsp)
andq $0x0, 0xc0(%rsp)
movq (%rdi), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x21f0
movl %eax, %ebp
testl %eax, %eax
js 0xbbe4
xorl %ebx, %ebx
testb $0x9, %r14b
jne 0xb56e
movq 0x78(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rax, %rdx
cmpq %rcx, %rdx
je 0xb54b
incw (%rdx)
movq 0x68(%rdx), %rsi
movq 0x70(%rdx), %rdi
cmpq %rdi, %rsi
je 0xb542
incw 0xa(%rsi)
addq $0x10, %rsi
jmp 0xb533
addq $0x98, %rdx
jmp 0xb523
pushq $0x4f
popq %rbx
cmpq %rcx, %rax
je 0xb563
movl 0x38(%rax), %edx
addl %edx, %ebx
addl $0x45, %ebx
addq $0x98, %rax
jmp 0xb54e
movl %ebx, %esi
movl %ebp, %edi
xorl %edx, %edx
callq 0x2420
movl %ebp, 0xc(%rsp)
movq 0x78(%rsp), %rax
movq (%rax), %r15
movq 0x8(%rax), %rax
movq %rax, 0xc8(%rsp)
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r12
movl %r14d, 0x2c(%rsp)
cmpq 0xc8(%rsp), %r15
je 0xb9ac
andq $0x0, 0x36(%rsp)
andq $0x0, 0x30(%rsp)
movw $0x204, 0x3e(%rsp)      # imm = 0x204
leaq 0x46(%rsp), %rax
movups %xmm0, (%rax)
andq $0x0, 0xe(%rax)
movq 0x58(%r15), %rcx
subq 0x50(%r15), %rcx
xorl %edx, %edx
cmpq $0x10000, %rcx          # imm = 0x10000
setb %dl
movl 0x8(%r15), %eax
addl %eax, %ecx
shll $0x10, %edx
xorl %r13d, %r13d
movl %ecx, 0x38(%rsp)
movzwl 0x2(%r15), %ecx
movw %cx, 0x44(%rsp)
movl %edx, 0x40(%rsp)
movzwl (%r15), %ecx
movw %cx, 0x52(%rsp)
movl 0x4(%r15), %ecx
movl %ecx, 0x4c(%rsp)
movl %eax, 0x34(%rsp)
movl 0xc(%r15), %ecx
movl %ecx, 0x48(%rsp)
testb $0x9, %r14b
jne 0xb621
andl $0x0, 0x34(%rsp)
movl %eax, %r13d
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
leaq 0x10(%r15), %rsi
movq %r12, %rdi
pushq $0xa
popq %rdx
callq 0xae09
leaq 0x30(%r15), %rsi
movq %r12, %rdi
movq %rsi, 0xd0(%rsp)
callq 0xadc9
movq %rbx, 0xa8(%rsp)
movw $0x2c, 0x58(%rsp)
movl 0x88(%rsp), %ebp
subl 0x80(%rsp), %ebp
leal 0x2c(%rbp), %eax
movl %eax, 0xa4(%rsp)
movw %ax, 0x5a(%rsp)
movl 0x58(%r15), %ebx
subl 0x50(%r15), %ebx
movq %r12, %rdi
movl $0xf2, %esi
callq 0xad34
addl %r13d, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0xad75
movq 0x88(%rsp), %rsi
movq %r12, %rdi
movq %rsi, %r12
subq 0x80(%rsp), %r12
movq 0x50(%r15), %rdx
movq 0x58(%r15), %rcx
callq 0xbfd4
testl %r13d, %r13d
je 0xb6ed
movq 0x88(%rsp), %rsi
movl %r13d, %edx
movb $0x0, 0x60(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rcx
callq 0x550a
movl 0xa8(%rsp), %r14d
addq $0x2c, %r14
xorl %r8d, %r8d
movl 0x2c(%rsp), %eax
testb $0x3, %al
sete %r8b
xorl %ecx, %ecx
testb $0x4, %al
sete %cl
movl 0x88(%rsp), %r13d
addl %r14d, %r13d
subl 0x80(%rsp), %r13d
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xae75
movl %eax, %r12d
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0xad34
movl 0x88(%rsp), %eax
subl 0x80(%rsp), %eax
leal 0x2c(%rax), %ecx
movl %ecx, 0x30(%rsp)
movl 0x2c(%rsp), %ecx
testb $0x9, %cl
je 0xb790
testb $0x1, %cl
je 0xb901
movb $0x1, 0x3f(%rsp)
addl $0x22b, %eax            # imm = 0x22B
shrl $0x9, %eax
movl %eax, 0x30(%rsp)
movb 0x44(%rsp), %al
movb %al, 0x3c(%rsp)
andw $0x0, 0x44(%rsp)
jmp 0xb901
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xc01c
leal (%r14,%rbp), %esi
addl $0x5, %esi
testl %ebx, %ebx
cmovel %ebx, %esi
testl %r12d, %r12d
cmovel %r12d, %r13d
leaq 0x10(%rsp), %rdi
callq 0xad75
leaq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0xad75
leaq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0xad75
leaq 0x10(%rsp), %rdi
movl %r12d, %esi
callq 0xad75
movzbl 0x3c(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad34
movzbl 0x3d(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad34
movzbl 0x3e(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad34
movzbl 0x3f(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad34
movl 0x40(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad75
movzwl 0x44(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad47
movzwl 0x46(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad47
movl 0x48(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad75
movl 0x4c(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad75
movzbl 0x50(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad34
movzbl 0x51(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad34
movzwl 0x52(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad47
movl 0x54(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad75
leaq 0x10(%rsp), %rdi
pushq $0x2c
popq %rsi
callq 0xad47
movzwl 0xa4(%rsp), %esi
leaq 0x10(%rsp), %rdi
callq 0xad47
movq 0x18(%rsp), %rsi
movb $0x20, 0x60(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
pushq $0xa
popq %rdx
leaq 0x60(%rsp), %rcx
callq 0x550a
movq %rbx, %rdi
movq 0xd0(%rsp), %rsi
callq 0xadc9
movl 0xc(%rsp), %edi
leaq 0x30(%rsp), %rsi
pushq $0x2c
popq %rdx
callq 0x20e0
movq %rax, %rbx
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
subq %rsi, %rdx
movl 0xc(%rsp), %edi
callq 0x20e0
movq 0xa8(%rsp), %rcx
addl %ebx, %ecx
movl %eax, %ebx
addl %ecx, %ebx
movl 0x2c(%rsp), %r14d
testb $0x1, %r14b
je 0xb98d
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rax
movq $-0x98, %rcx
addq %rcx, %rax
cmpq %rax, %r15
je 0xb98d
movq %rbx, %r14
movl %ebx, %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %edx            # imm = 0x200
subl %eax, %edx
movl 0xc(%rsp), %edi
leaq 0x9e92(%rip), %rsi      # 0x15810
callq 0x20e0
movq %r14, %rbx
addl %eax, %ebx
movl 0x2c(%rsp), %r14d
leaq 0x80(%rsp), %r12
movq %r12, %rdi
callq 0x6ac0
addq $0x98, %r15
xorps %xmm0, %xmm0
jmp 0xb596
testb $0x9, %r14b
movl 0xc(%rsp), %ebp
jne 0xbbb4
andq $0x0, 0x36(%rsp)
andq $0x0, 0x30(%rsp)
movw $0x204, 0x3e(%rsp)      # imm = 0x204
xorps %xmm0, %xmm0
movups %xmm0, 0x46(%rsp)
andw $0x0, 0x56(%rsp)
movw $0x1, 0x52(%rsp)
movl $0x10000, 0x40(%rsp)    # imm = 0x10000
movw $0x8001, 0x44(%rsp)     # imm = 0x8001
movl $0x43002c, 0x58(%rsp)   # imm = 0x43002C
movq 0x78(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
movl $0x98, %ecx
cqto
idivq %rcx
movq %rax, %rbx
imull $0xa, %ebx, %eax
movl 0x18(%rsp), %ecx
subl 0x10(%rsp), %ecx
leal (%rcx,%rax), %ebp
addl $0x6, %ebp
leaq 0x60(%rsp), %rdi
andq $0x0, 0x10(%rdi)
movl %ebp, 0x38(%rsp)
movaps %xmm0, (%rdi)
leaq 0x80(%rsp), %rcx
movb $0x20, (%rcx)
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x550a
leaq 0x2089(%rip), %rsi      # 0xdad9
leaq 0x80(%rsp), %rdi
leaq 0x5f(%rsp), %rdx
callq 0x5fc6
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xadc9
leaq 0x80(%rsp), %rdi
callq 0x2130
leaq 0x60(%rsp), %rdi
movl $0xf2, %esi
callq 0xad34
leaq 0x60(%rsp), %rdi
movl %ebp, %esi
callq 0xad75
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0xad75
movq 0x78(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
movl $0x98, %ecx
cqto
idivq %rcx
decl %eax
movzwl %ax, %esi
leaq 0x60(%rsp), %rdi
callq 0xad47
movq 0xb0(%rsp), %r14
movq 0xb8(%rsp), %r15
imull $0xa, %ebx, %ebp
leaq 0x60(%rsp), %rbx
cmpq %r15, %r14
je 0xbb19
movzwl (%r14), %eax
addw %bp, %ax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0xad47
movq %rbx, %rdi
xorl %esi, %esi
callq 0xad47
movq %rbx, %rdi
xorl %esi, %esi
callq 0xad75
addq $0x4, %r14
addl $-0x8, %ebp
jmp 0xbae5
movq 0x78(%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
leaq 0x60(%rsp), %rbx
cmpq %r15, %r14
je 0xbb44
movzwl (%r14), %esi
movq %rbx, %rdi
callq 0xad47
addq $0x98, %r14
jmp 0xbb2a
leaq 0x60(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
callq 0xbfd4
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0xad34
movl 0xc(%rsp), %ebp
movl 0x68(%rsp), %eax
subl 0x60(%rsp), %eax
addl $0x2c, %eax
leaq 0x30(%rsp), %rbx
movl %eax, (%rbx)
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2420
pushq $0x2c
popq %rdx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x20e0
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
subq %rsi, %rdx
movl %ebp, %edi
callq 0x20e0
leaq 0x60(%rsp), %rdi
callq 0x6ac0
movl %ebp, %edi
callq 0x2480
leaq 0xb0(%rsp), %rdi
callq 0xc700
leaq 0x10(%rsp), %rdi
callq 0x6ac0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdx
leaq 0x198f(%rip), %rsi      # 0xd57d
pushq $0x49
popq %rdi
xorl %eax, %eax
callq 0x2240
jmp 0xbc23
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2130
jmp 0xbc26
jmp 0xbc23
jmp 0xbc23
jmp 0xbc12
movq %rax, %rbx
jmp 0xbc41
jmp 0xbc23
jmp 0xbc23
jmp 0xbc31
jmp 0xbc31
jmp 0xbc31
jmp 0xbc31
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
jmp 0xbc3c
jmp 0xbc31
jmp 0xbc31
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x6ac0
leaq 0xb0(%rsp), %rdi
callq 0xc700
leaq 0x10(%rsp), %rdi
callq 0x6ac0
movq %rbx, %rdi
callq 0x23c0