// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/ole1.c
// Function: do_ole_object_presentation
static int do_ole_object_presentation(deark *c, lctx *d,
	i64 pos1, i64 len, unsigned int formatID, i64 *bytes_consumed)
{
	i64 pos = pos1;
	i64 endpos = pos1 + len;
	i64 dlen;
	i64 stringlen;
	struct de_stringreaderdata *classname_srd = NULL;
	struct de_stringreaderdata *clipfmtname_srd = NULL;
	const char *name;
	int retval = 0;

	name = (formatID==3)?"static":"presentation";
	stringlen = de_getu32le_p(&pos);
	classname_srd = dbuf_read_string(c->infile, pos, stringlen, 260, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "%s ClassName: \"%s\"", name, ucstring_getpsz(classname_srd->str));
	pos += stringlen;

	// TODO: Better handle the fields between ClassName and PresentationData
	// (and maybe after PresentationData?).

	if(!de_strcmp(classname_srd->sz, "DIB")) {
		pos += 8;
		dlen = de_getu32le_p(&pos);
		if(pos + dlen > endpos) goto done;
		de_dbg_indent(c, 1);
		de_run_module_by_id_on_slice(c, "dib", NULL, c->infile, pos, dlen);
		de_dbg_indent(c, -1);
		pos += dlen;
	}
	else if(!de_strcmp(classname_srd->sz, "METAFILEPICT")) {
		pos += 8; // ??
		dlen = de_getu32le_p(&pos);
		de_dbg(c, "metafile size: %d", (int)dlen); // Includes "mfp", apparently
		pos += 8; // "mfp" struct
		dbuf_create_file_from_slice(c->infile, pos, dlen-8, "wmf", NULL, 0);
		pos += dlen-8;
	}
	else if(!de_strcmp(classname_srd->sz, "BITMAP")) {
		do_static_bitmap(c, d, pos, endpos-pos);
		goto done; // FIXME, calculate length
	}
	else {
		u32 clipfmt;
		i64 clp_data_size;
		u8 buf[16];

		// This is a GenericPresentationObject, a.k.a. clipboard format,
		// either a StandardClipboardFormatPresentationObject
		// or a RegisteredClipboardFormatPresentationObject.
		clipfmt = (u32)de_getu32le_p(&pos);
		de_dbg(c, "clipboard fmt: %u", (unsigned int)clipfmt);

		if(clipfmt==0) {
			stringlen = de_getu32le_p(&pos);
			clipfmtname_srd = dbuf_read_string(c->infile, pos, stringlen, 260, DE_CONVFLAG_STOP_AT_NUL,
				d->input_encoding);
			de_dbg(c, "clipboard fmt name: \"%s\"", ucstring_getpsz(clipfmtname_srd->str));
			pos += stringlen;
		}

		clp_data_size = de_getu32le_p(&pos);
		de_dbg(c, "clipboard data size: %"I64_FMT, clp_data_size);

		de_read(buf, pos, de_min_int((i64)sizeof(buf), clp_data_size));

		if(clipfmtname_srd) {
			if(!de_strcmp(classname_srd->sz, "PBrush") &&
				buf[0]=='B' && buf[1]=='M')
			{
				dbuf_create_file_from_slice(c->infile, pos, clp_data_size, "bmp", NULL, 0);
			}
			else {
				de_warn(c, "OLE clipboard type (\"%s\"/\"%s\") is not supported",
					ucstring_getpsz(classname_srd->str),
					ucstring_getpsz(clipfmtname_srd->str));
			}
		}
		else {
			de_warn(c, "OLE clipboard type %u is not supported", (unsigned int)clipfmt);
		}

		pos += clp_data_size;
	}

	*bytes_consumed = pos-pos1;
	retval = 1;

done:
	de_destroy_stringreaderdata(c, classname_srd);
	de_destroy_stringreaderdata(c, clipfmtname_srd);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movq %rcx, %r12
movq %rsi, %rbx
movq %rdi, %rbp
movq %rsp, %r14
movq %rdx, (%r14)
movq %rdx, 0x28(%rsp)
addq %rdx, %r12
cmpl $0x3, %r8d
leaq 0x9412d(%rip), %rax     # 0x131b10
leaq 0x9412d(%rip), %r15     # 0x131b17
cmoveq %rax, %r15
movq 0x18(%rdi), %rdi
movq %r14, %rsi
callq 0xeed83
movq %rax, %r13
movq 0x18(%rbp), %rdi
movq (%r14), %rsi
movq %rbx, 0x20(%rsp)
movl (%rbx), %r9d
movl $0x104, %ecx            # imm = 0x104
movq %rax, %rdx
movl $0x1, %r8d
callq 0xef87c
movq %rax, %rbx
movq 0x18(%rax), %rdi
callq 0xf705c
leaq 0x942be(%rip), %rsi     # 0x131cf0
movq %rbp, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe8617
addq (%r14), %r13
movq %r13, (%r14)
movq %rbx, 0x8(%rsp)
movq 0x8(%rbx), %r14
leaq 0xa3085(%rip), %rsi     # 0x140add
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x9dbc6
leaq 0xaccbf(%rip), %rsi     # 0x14a72e
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
je 0x9dbfd
leaq 0xacc9e(%rip), %rsi     # 0x14a724
movq %r14, %rdi
callq 0x181a0
testl %eax, %eax
movq %rbp, %rbx
je 0x9dcaa
movq 0x18(%rbx), %rdi
movq %rsp, %rsi
callq 0xeed83
movq %rax, %r12
leaq 0x94266(%rip), %rsi     # 0x131d15
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
testl %r12d, %r12d
jne 0x9db1b
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xeed83
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x20(%rsp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movq %r15, %rdx
movl $0x1, %r8d
callq 0xef87c
movq %rax, %r13
movq 0x18(%rax), %rdi
callq 0xf705c
leaq 0x9421c(%rip), %rsi     # 0x131d27
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq %r15, (%r14)
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xeed83
movq %rax, %rbp
leaq 0x9420c(%rip), %rsi     # 0x131d40
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %r15
movq (%r14), %r14
movl $0x10, %edi
movq %rbp, %rsi
callq 0xe9c3b
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0xee8d4
testq %r13, %r13
je 0x9dd48
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
leaq 0x941d8(%rip), %rsi     # 0x131d59
callq 0x181a0
testl %eax, %eax
jne 0x9dd63
cmpb $0x42, 0x10(%rsp)
jne 0x9dd63
cmpb $0x4d, 0x11(%rsp)
jne 0x9dd63
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
leaq 0x835af(%rip), %rcx     # 0x121162
movq %rbp, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xefd48
jmp 0x9dd94
addq $0x8, %r13
movq %rsp, %r14
movq %r13, (%r14)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeed83
movq %rax, %r15
movq (%r14), %rax
addq %r15, %rax
cmpq %r12, %rax
movq %rbp, %rbx
jle 0x9dc5f
xorl %r13d, %r13d
movq 0x8(%rsp), %r14
xorl %r15d, %r15d
jmp 0x9ddb2
addq $0x8, %r13
movq %rsp, %r14
movq %r13, (%r14)
movq %rbp, %rbx
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeed83
movq %rax, %r15
leaq 0x940e3(%rip), %rsi     # 0x131d03
xorl %r13d, %r13d
movq %rbp, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq (%r14), %rsi
addq $0x8, %rsi
movq %rsi, (%r14)
movq 0x18(%rbp), %rdi
addq $-0x8, %r15
leaq 0x7e92e(%rip), %rcx     # 0x11c577
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xefd48
addq (%r14), %r15
movq %r15, (%r14)
jmp 0x9dca0
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rcx
movq (%rsp), %r8
leaq 0x85794(%rip), %rsi     # 0x12340f
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r9
callq 0xe9aa3
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq (%rsp), %r15
movq %r15, (%rsp)
movq 0x8(%rsp), %r14
jmp 0x9dd9f
addq $0x8, %r13
leaq 0x10(%rsp), %r14
movq %r13, (%r14)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeed83
movq %rax, %r15
leaq 0x940eb(%rip), %rsi     # 0x131db7
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq (%r14), %rdx
leaq 0x7f6ce(%rip), %rsi     # 0x11d3b4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rbp
movq (%r14), %r14
subq %r14, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0xe9c3b
leaq 0x856b4(%rip), %rsi     # 0x1233cd
leaq 0x761a7(%rip), %rdx     # 0x113ec7
movq %rbx, %rdi
movq %rbp, %rcx
movq %r14, %r8
movq %rax, %r9
callq 0xe9b13
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
xorl %r15d, %r15d
movq 0x8(%rsp), %r14
jmp 0x9ddb2
leaq 0x94041(%rip), %rsi     # 0x131d90
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8faa
movq 0x8(%rsp), %r14
jmp 0x9dd94
movq 0x18(%r14), %rdi
callq 0xf705c
movq %rax, %r14
movq 0x18(%r13), %rdi
callq 0xf705c
leaq 0x93fe1(%rip), %rsi     # 0x131d60
movq %rbx, %rdi
movq %r14, %rdx
movq 0x8(%rsp), %r14
movq %rax, %rcx
xorl %eax, %eax
callq 0xe8faa
movq (%rsp), %r15
addq %rbp, %r15
movq %r15, (%rsp)
subq 0x28(%rsp), %r15
movq 0x30(%rsp), %rax
movq %r15, (%rax)
movl $0x1, %r15d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xefaca
movq %rbx, %rdi
movq %r13, %rsi
callq 0xefaca
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq