// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x24(%rbp), %edx
leaq 0xd5030(%rip), %rax     # 0x1bd508
leaq 0xd5022(%rip), %rcx     # 0x1bd501
cmpl $0x3, %edx
cmoveq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movl $0x1, %r8d
callq 0x1664c0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rdx
movq %rax, %rcx
leaq 0xd517c(%rip), %rsi     # 0x1bd6e1
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe41a6(%rip), %rsi     # 0x1cc72d
callq 0x191a0
cmpl $0x0, %eax
jne 0xe861a
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
addq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0xe85c9
jmp 0xe8903
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %r8
movq -0x48(%rbp), %r9
leaq 0xc6801(%rip), %rsi     # 0x1aedf3
xorl %eax, %eax
movl %eax, %edx
callq 0x15c9f0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe88ed
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xedbb5(%rip), %rsi     # 0x1d61de
callq 0x191a0
cmpl $0x0, %eax
jne 0xe86b9
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0xd508b(%rip), %rsi     # 0x1bd6f4
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
subq $0x8, %rdx
leaq 0xbfabc(%rip), %rcx     # 0x1a8153
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x166be0
movq -0x48(%rbp), %rax
subq $0x8, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe88eb
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xedb0c(%rip), %rsi     # 0x1d61d4
callq 0x191a0
cmpl $0x0, %eax
jne 0xe86f0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
subq -0x38(%rbp), %rcx
callq 0xe8e10
jmp 0xe8903
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movl %eax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movl -0x70(%rbp), %edx
leaq 0xd4ff4(%rip), %rsi     # 0x1bd706
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x70(%rbp)
jne 0xe879b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x104, %ecx            # imm = 0x104
movl $0x1, %r8d
callq 0x1664c0
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xa8(%rbp), %rdi
movq %rax, %rdx
leaq 0xd4f90(%rip), %rsi     # 0x1bd718
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rdx
leaq 0xd4f72(%rip), %rsi     # 0x1bd731
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xc0(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rsi
movl $0x10, %edi
callq 0x15ccf0
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq %rax, %rcx
callq 0x164d10
cmpq $0x0, -0x60(%rbp)
je 0xe88c8
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd4f17(%rip), %rsi     # 0x1bd74a
callq 0x191a0
cmpl $0x0, %eax
jne 0xe887b
movzbl -0x90(%rbp), %eax
cmpl $0x42, %eax
jne 0xe887b
movzbl -0x8f(%rbp), %eax
cmpl $0x4d, %eax
jne 0xe887b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x78(%rbp), %rdx
leaq 0xc449b(%rip), %rcx     # 0x1acd07
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x166be0
jmp 0xe88c6
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq %rax, -0xc8(%rbp)
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rdx
movq %rax, %rcx
leaq 0xd4e92(%rip), %rsi     # 0x1bd751
movb $0x0, %al
callq 0x15bc10
jmp 0xe88dd
movq -0x8(%rbp), %rdi
movl -0x70(%rbp), %edx
leaq 0xd4eab(%rip), %rsi     # 0x1bd781
movb $0x0, %al
callq 0x15bc10
movq -0x78(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe88eb
jmp 0xe88ed
movq -0x38(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x6c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x166830
movl -0x6c(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)