// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/officeart.c
// Function: do_OfficeArtStream_record
static int do_OfficeArtStream_record(deark *c, struct officeartctx *oactx,
	dbuf *inf)
{
	unsigned int rectype;
	unsigned int recinstance;
	unsigned int recver;
	unsigned int n;
	i64 reclen;
	i64 extra_bytes = 0;
	dbuf *outf = NULL;
	const char *ext = "bin";
	int has_metafileHeader = 0;
	int has_zlib_cmpr = 0;
	int is_dib = 0;
	int is_pict = 0;
	int retval = 0;
	int is_blip = 0;
	int saved_indent_level;
	i64 pos1 = oactx->record_pos;
	i64 pos = pos1;

	oactx->record_bytes_consumed = 0;
	oactx->is_container = 0;
	oactx->container_endpos = 0;

	de_dbg_indent_save(c, &saved_indent_level);

	n = (unsigned int)dbuf_getu16le_p(inf, &pos);
	recver = n&0x0f;
	if(recver==0x0f) oactx->is_container = 1;
	recinstance = n>>4;

	rectype = (unsigned int)dbuf_getu16le_p(inf, &pos);
	if((rectype&0xf000)!=0xf000) {
		// Assume this is the end of data, not necessarily an error.
		goto done;
	}

	reclen = dbuf_getu32le_p(inf, &pos);

	de_dbg(c, "record at [%"I64_FMT"], ver=0x%x, inst=0x%03x, type=0x%04x (%s), dlen=%"I64_FMT,
		pos1, recver, recinstance,
		rectype, get_officeart_rectype_name(rectype), reclen);
	de_dbg_indent(c, 1);

	if(pos + reclen > inf->len) goto done;
	if(oactx->is_container) {
		// A container is described as *being* its header record. It does have
		// a recLen, but it should be safe to ignore it if all we care about is
		// reading the records at a low level.
		oactx->record_bytes_consumed = 8;
		oactx->container_endpos = oactx->record_pos + 8 + reclen;
	}
	else {
		oactx->record_bytes_consumed = (pos-pos1) + reclen;
	}
	retval = 1;

	if(rectype>=0xf018 && rectype<=0xf117) is_blip = 1;
	if(!is_blip) goto done;

	if(rectype==0xf01a) {
		ext = "emf";
		if(recinstance==0x3d4) extra_bytes=50;
		else if(recinstance==0x3d5) extra_bytes=66;
		if(extra_bytes) has_metafileHeader=1;
	}
	else if(rectype==0xf01b) {
		ext = "wmf";
		if(recinstance==0x216) extra_bytes=50;
		else if(recinstance==0x217) extra_bytes=66;
		if(extra_bytes) has_metafileHeader=1;
	}
	else if(rectype==0xf01c) {
		ext = "pict";
		if(recinstance==0x542) extra_bytes=50;
		else if(recinstance==0x543) extra_bytes=66;
		if(extra_bytes) has_metafileHeader=1;
		is_pict = 1;
	}
	else if(rectype==0xf01d) {
		ext = "jpg";
		if(recinstance==0x46a || recinstance==0x6e2) extra_bytes = 17;
		else if(recinstance==0x46b || recinstance==0x6e3) extra_bytes = 33;
	}
	else if(rectype==0xf01e) {
		ext = "png";
		if(recinstance==0x6e0) extra_bytes = 17;
		else if(recinstance==0x6e1) extra_bytes = 33;
	}
	else if(rectype==0xf01f) {
		ext = "dib";
		if(recinstance==0x7a8) extra_bytes = 17;
		else if(recinstance==0x7a9) extra_bytes = 33;
		if(extra_bytes) is_dib=1;
	}
	else if(rectype==0xf029) {
		ext = "tif";
		if(recinstance==0x6e4) extra_bytes = 17;
		else if(recinstance==0x6e5) extra_bytes = 33;
	}

	if(extra_bytes==0) {
		de_warn(c, "Unsupported OfficeArtBlip format (recInstance=0x%03x, recType=0x%04x)",
			recinstance, rectype);
		goto done;
	}

	if(has_metafileHeader) {
		// metafileHeader starts at pos+extra_bytes-34
		u8 cmpr = dbuf_getbyte(inf, pos+extra_bytes-2);
		// 0=DEFLATE, 0xfe=NONE
		de_dbg(c, "compression type: %u", (unsigned int)cmpr);
		has_zlib_cmpr = (cmpr==0);
	}

	pos += extra_bytes;

	if(is_dib) {
		de_run_module_by_id_on_slice2(c, "dib", "X", inf, pos, reclen-extra_bytes);
		goto done;
	}

	outf = dbuf_create_output_file(c, ext, NULL, DE_CREATEFLAG_IS_AUX);
	if(is_pict) {
		dbuf_write_zeroes(outf, 512);
	}

	if(has_zlib_cmpr) {
		i64 cmprlen;

		cmprlen = reclen-extra_bytes;
		fmtutil_decompress_deflate(inf, pos, cmprlen, outf, 0, NULL, DE_DEFLATEFLAG_ISZLIB);
		de_dbg(c, "decompressed %"I64_FMT" to %"I64_FMT" bytes", cmprlen, outf->len);
	}
	else {
		dbuf_copy(inf, pos, reclen-extra_bytes, outf);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
	dbuf_close(outf);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0xd7139(%rip), %rax     # 0x1c1207
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x68(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x70(%rax)
movq -0x8(%rbp), %rdi
leaq -0x64(%rbp), %rsi
callq 0x15b580
movq -0x18(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x24(%rbp)
cmpl $0xf, -0x24(%rbp)
jne 0xea16a
movq -0x10(%rbp), %rax
movl $0x1, 0x68(%rax)
movl -0x28(%rbp), %eax
shrl $0x4, %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0xf000, %eax           # imm = 0xF000
je 0xea197
jmp 0xea615
movq -0x18(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x1c(%rbp), %edi
callq 0xea640
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rdx
movl -0x94(%rbp), %ecx
movl -0x90(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movq %rax, %r10
movq -0x30(%rbp), %rax
leaq 0xd3722(%rip), %rsi     # 0x1bd933
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x78(%rbp), %rax
addq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jle 0xea246
jmp 0xea615
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0xea276
movq -0x10(%rbp), %rax
movq $0x8, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
addq $0x8, %rcx
addq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
jmp 0xea28a
movq -0x78(%rbp), %rcx
subq -0x70(%rbp), %rcx
addq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movl $0x1, -0x5c(%rbp)
cmpl $0xf018, -0x1c(%rbp)    # imm = 0xF018
jb 0xea2aa
cmpl $0xf117, -0x1c(%rbp)    # imm = 0xF117
ja 0xea2aa
movl $0x1, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jne 0xea2b5
jmp 0xea615
cmpl $0xf01a, -0x1c(%rbp)    # imm = 0xF01A
jne 0xea302
leaq 0xbcafc(%rip), %rax     # 0x1a6dc1
movq %rax, -0x48(%rbp)
cmpl $0x3d4, -0x20(%rbp)     # imm = 0x3D4
jne 0xea2dc
movq $0x32, -0x38(%rbp)
jmp 0xea2ef
cmpl $0x3d5, -0x20(%rbp)     # imm = 0x3D5
jne 0xea2ed
movq $0x42, -0x38(%rbp)
jmp 0xea2ef
cmpq $0x0, -0x38(%rbp)
je 0xea2fd
movl $0x1, -0x4c(%rbp)
jmp 0xea4c5
cmpl $0xf01b, -0x1c(%rbp)    # imm = 0xF01B
jne 0xea34f
leaq 0xbde41(%rip), %rax     # 0x1a8153
movq %rax, -0x48(%rbp)
cmpl $0x216, -0x20(%rbp)     # imm = 0x216
jne 0xea329
movq $0x32, -0x38(%rbp)
jmp 0xea33c
cmpl $0x217, -0x20(%rbp)     # imm = 0x217
jne 0xea33a
movq $0x42, -0x38(%rbp)
jmp 0xea33c
cmpq $0x0, -0x38(%rbp)
je 0xea34a
movl $0x1, -0x4c(%rbp)
jmp 0xea4c3
cmpl $0xf01c, -0x1c(%rbp)    # imm = 0xF01C
jne 0xea3a3
leaq 0xb79c9(%rip), %rax     # 0x1a1d28
movq %rax, -0x48(%rbp)
cmpl $0x542, -0x20(%rbp)     # imm = 0x542
jne 0xea376
movq $0x32, -0x38(%rbp)
jmp 0xea389
cmpl $0x543, -0x20(%rbp)     # imm = 0x543
jne 0xea387
movq $0x42, -0x38(%rbp)
jmp 0xea389
cmpq $0x0, -0x38(%rbp)
je 0xea397
movl $0x1, -0x4c(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0xea4c1
cmpl $0xf01d, -0x1c(%rbp)    # imm = 0xF01D
jne 0xea3f4
leaq 0xd4f46(%rip), %rax     # 0x1bf2f9
movq %rax, -0x48(%rbp)
cmpl $0x46a, -0x20(%rbp)     # imm = 0x46A
je 0xea3c9
cmpl $0x6e2, -0x20(%rbp)     # imm = 0x6E2
jne 0xea3d3
movq $0x11, -0x38(%rbp)
jmp 0xea3ef
cmpl $0x46b, -0x20(%rbp)     # imm = 0x46B
je 0xea3e5
cmpl $0x6e3, -0x20(%rbp)     # imm = 0x6E3
jne 0xea3ed
movq $0x21, -0x38(%rbp)
jmp 0xea3ef
jmp 0xea4bf
cmpl $0xf01e, -0x1c(%rbp)    # imm = 0xF01E
jne 0xea433
leaq 0xc9ad4(%rip), %rax     # 0x1b3ed8
movq %rax, -0x48(%rbp)
cmpl $0x6e0, -0x20(%rbp)     # imm = 0x6E0
jne 0xea41b
movq $0x11, -0x38(%rbp)
jmp 0xea42e
cmpl $0x6e1, -0x20(%rbp)     # imm = 0x6E1
jne 0xea42c
movq $0x21, -0x38(%rbp)
jmp 0xea42e
jmp 0xea4bd
cmpl $0xf01f, -0x1c(%rbp)    # imm = 0xF01F
jne 0xea47d
leaq 0xc49b0(%rip), %rax     # 0x1aedf3
movq %rax, -0x48(%rbp)
cmpl $0x7a8, -0x20(%rbp)     # imm = 0x7A8
jne 0xea45a
movq $0x11, -0x38(%rbp)
jmp 0xea46d
cmpl $0x7a9, -0x20(%rbp)     # imm = 0x7A9
jne 0xea46b
movq $0x21, -0x38(%rbp)
jmp 0xea46d
cmpq $0x0, -0x38(%rbp)
je 0xea47b
movl $0x1, -0x54(%rbp)
jmp 0xea4bb
cmpl $0xf029, -0x1c(%rbp)    # imm = 0xF029
jne 0xea4b9
leaq 0xeba8a(%rip), %rax     # 0x1d5f17
movq %rax, -0x48(%rbp)
cmpl $0x6e4, -0x20(%rbp)     # imm = 0x6E4
jne 0xea4a4
movq $0x11, -0x38(%rbp)
jmp 0xea4b7
cmpl $0x6e5, -0x20(%rbp)     # imm = 0x6E5
jne 0xea4b5
movq $0x21, -0x38(%rbp)
jmp 0xea4b7
jmp 0xea4b9
jmp 0xea4bb
jmp 0xea4bd
jmp 0xea4bf
jmp 0xea4c1
jmp 0xea4c3
jmp 0xea4c5
cmpq $0x0, -0x38(%rbp)
jne 0xea4e9
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq 0xd3499(%rip), %rsi     # 0x1bd976
movb $0x0, %al
callq 0x15bc10
jmp 0xea615
cmpl $0x0, -0x4c(%rbp)
je 0xea52f
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
addq -0x38(%rbp), %rsi
subq $0x2, %rsi
callq 0x165060
movb %al, -0x79(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x79(%rbp), %edx
leaq 0xb93e1(%rip), %rsi     # 0x1a38f7
movb $0x0, %al
callq 0x15b0d0
movzbl -0x79(%rbp), %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xea56d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x30(%rbp), %r9
subq -0x38(%rbp), %r9
leaq 0xc4897(%rip), %rsi     # 0x1aedf3
leaq 0xe500c(%rip), %rdx     # 0x1cf56f
callq 0x15cab0
jmp 0xea615
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x166c60
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x58(%rbp)
je 0xea59b
movq -0x40(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x1685f0
cmpl $0x0, -0x50(%rbp)
je 0xea5fa
movq -0x30(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq -0x40(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
movl $0x1, (%rsp)
callq 0x17f4f0
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rdx
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0xc2b07(%rip), %rsi     # 0x1ad0f8
movb $0x0, %al
callq 0x15b0d0
jmp 0xea613
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x30(%rbp), %rdx
subq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x166070
jmp 0xea615
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x15b5a0
movq -0x40(%rbp), %rdi
callq 0x1676a0
movl -0x5c(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)