// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/jpeg.c
// Function: finalize_fpxr_stream
static void finalize_fpxr_stream(deark *c, lctx *d, struct fpxr_entity_struct *fe)
{
	de_finfo *fi = NULL;
	dbuf *outf = NULL;
	de_ucstring *name2 = NULL;

	if(!fe || !fe->stream) goto done;
	if(fe->done_flag || fe->is_storage) goto done;

	if(fe->stream->len != fe->stream_size) {
		de_warn(c, "Expected FPXR stream #%u to have %"I64_FMT" bytes, found %"I64_FMT,
			(unsigned int)fe->index, fe->stream_size, fe->stream->len);
	}

	// Process some known streams
	if(fe->name_srd) {
		if(fe->name_srd->sz_utf8 && !de_strcmp(fe->name_srd->sz_utf8, "/FUJIFILM/Preview")) {
			do_fpxr_fujifilm_preview(c, d, fe);
		}

		// The FlashPix spec says "Names in an IStorage that begin with the
		// value '\0x05' are reserved exclusively for the storage of property
		// sets."
		//
		// It probably means the last *component* of the name begins with 0x05.
		// 0x05 shouldn't appear anywhere else, I think, so I'll just search
		// the whole string for it.
		if(ucstring_contains_char(fe->name_srd->str, 0x05)) {
			do_fpxr_olepropset_stream(c, d, fe);
		}
	}

	if(c->extract_level<2) goto done;

	fi = de_finfo_create(c);

	name2 = ucstring_create(c);
	if(fe->name_srd) {
		ucstring_append_ucstring(name2, fe->name_srd->str);
	}
	if(name2->len>0) {
		ucstring_append_char(name2, '.');
	}
	ucstring_append_sz(name2, "fpxr.bin", DE_ENCODING_LATIN1);
	de_finfo_set_name_from_ucstring(c, fi, name2, 0);

	outf = dbuf_create_output_file(c, NULL, fi, DE_CREATEFLAG_IS_AUX);
	dbuf_copy(fe->stream, 0, fe->stream->len, outf);

done:
	if(fe && fe->stream) {
		dbuf_close(fe->stream);
		fe->stream = NULL;
	}
	if(fe) {
		fe->done_flag = 1;
	}
	ucstring_destroy(name2);
	dbuf_close(outf);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
testq %rsi, %rsi
je 0x840e3
movq %rsi, %r14
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x840f1
cmpl $0x0, 0x24(%r14)
jne 0x8414f
cmpl $0x0, 0x20(%r14)
jne 0x8414f
movq 0x18(%rax), %r8
movq 0x18(%r14), %rcx
cmpq %rcx, %r8
je 0x84085
movl (%r14), %edx
leaq 0xa72c7(%rip), %rsi     # 0x12b342
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x8(%r14), %r15
testq %r15, %r15
je 0x84146
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0x840b7
leaq 0xa72d6(%rip), %rsi     # 0x12b378
callq 0x181a0
testl %eax, %eax
jne 0x840b7
movq %r14, %rdi
callq 0x84229
movq 0x8(%r14), %r15
movq 0x18(%r15), %rcx
testq %rcx, %rcx
je 0x84146
movq 0x10(%rcx), %rax
testq %rax, %rax
jle 0x84146
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $0x5, (%rcx,%rdx,4)
je 0x840fc
incq %rdx
cmpq %rdx, %rax
jne 0x840d3
jmp 0x84146
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
jmp 0x84176
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x8416e
movl (%r14), %edx
leaq 0xa729d(%rip), %rsi     # 0x12b3a3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %r9
leaq 0x95e23(%rip), %rsi     # 0x119f4f
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xe9aa3
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x2, 0x90(%rbx)
jge 0x8419a
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x8416e
callq 0xf052b
movq $0x0, 0x10(%r14)
movl $0x1, 0x24(%r14)
movq %r15, %rdi
callq 0xf6753
movq %r13, %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r12, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xea50e
movq %rbx, %rdi
callq 0xea504
movq %rax, %r12
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r15
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x841c5
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0xf6509
cmpq $0x0, 0x10(%r15)
jle 0x841d9
movq %r15, %rdi
movl $0x2e, %esi
callq 0xf6542
leaq 0xa71aa(%rip), %rsi     # 0x12b38a
movq %r15, %rdi
movl $0x5, %edx
callq 0xf65e3
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xea53f
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movl $0x1, %ecx
callq 0xefda1
movq %rax, %r13
movq 0x10(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rax, %rcx
callq 0xef481
jmp 0x84158