// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/cfb.c
// Function: do_Corel_UIformat
static void do_Corel_UIformat(deark *c, lctx *d, struct dir_entry_info *dei,
	dbuf *f, i64 pos1, i64 len, int is_thumb)
{
	i64 pos = pos1;
	i64 hdr_len;
	i64 ri_pos;
	i64 ri_len;
	i64 w, h;
	i64 pal_offs, img_offs;
	i64 rowspan;
	i64 pixels_size;
	i64 npalent;
	int bpp;
	int ok = 0;
	int saved_indent_level;
	de_bitmap *img = NULL;
	u32 pal[256];

	de_dbg_indent_save(c, &saved_indent_level);

	if(dbuf_memcmp(f, pos, "UI\x00\x00", 4)) goto done;

	de_dbg(c, "CorelUI at [%"I64_FMT"], len=%"I64_FMT, pos1, len);
	de_dbg_indent(c, 1);
	pos += 2; // "UI"
	pos += 2; // ?
	pos += 4; // The size of the "RI" segment? Redundant?
	pos += 4; // ?

	hdr_len = dbuf_getu32le_p(f, &pos);
	// Apparently the size of the "UI" segment
	if(hdr_len != 32) goto done;
	// TODO: More fields here

	ri_pos = pos1+hdr_len;
	pos = ri_pos;
	if(dbuf_memcmp(f, pos, "RI", 2)) goto done;
	pos += 2;
	ri_len = dbuf_getu32le_p(f, &pos);
	if(ri_pos + ri_len > f->len) goto done;

	pos += 16;
	w = dbuf_getu32le_p(f, &pos);
	h = dbuf_getu32le_p(f, &pos);
	de_dbg_dimensions(c, w, h);

	pos += 4; // ? (observed 1)

	bpp = (int)dbuf_getu32le_p(f, &pos);
	de_dbg(c, "bits/pixel?: %d", bpp);
	rowspan = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "bytes/row?: %d", (int)rowspan);
	pixels_size = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "pixels size: %"I64_FMT, pixels_size);
	pos += 4; // ?
	pos += 8; // ? (density?)

	pal_offs = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "pal offs: %"I64_FMT, pal_offs);

	img_offs = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "img offs: %"I64_FMT, img_offs);

	pos += 12; // ?

	if(bpp!=8 && bpp!=24) goto done;

	// == palette ==
	de_make_grayscale_palette(pal, 256, 0);
	if(pal_offs!=0) {
		// This formula doesn't make sense to me, but seems to work.
		pos = ri_pos+14+pal_offs;
		de_dbg(c, "palette at [%"I64_FMT"]", pos);
		de_dbg_indent(c, 1);
		pos += 2; // ? (observed 4, 5)

		npalent = dbuf_getu16le_p(f, &pos);
		de_dbg(c, "num pal entries: %d", (int)npalent);
		if(npalent>256) goto done;

		de_read_palette_rgb(f, pos, npalent, 3, pal, 256, DE_GETRGBFLAG_BGR);
		de_dbg_indent(c, -1);
	}

	// == image ==
	if(!de_good_image_dimensions(c, w, h)) goto done;

	img = de_bitmap_create(c, w, h, (bpp<24 && pal_offs==0)?1:3);
	pos = ri_pos+14+img_offs;
	de_dbg(c, "bitmap at [%"I64_FMT"]", pos);
	if(bpp<24) {
		de_convert_image_paletted(f, pos, 8, rowspan, pal, img, 0);
	}
	else {
		de_convert_image_rgb(f, pos, rowspan, 3, img, DE_GETRGBFLAG_BGR);
	}

	de_bitmap_write_to_file(img, is_thumb?"thumb":NULL, DE_CREATEFLAG_FLIP_IMAGE);

	ok = 1;

done:
	if(!ok) {
		de_dbg(c, "[unsupported image type]");
	}
	de_bitmap_destroy(img);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4d0, %rsp            # imm = 0x4D0
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x90(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x94(%rbp), %rsi
callq 0x15b580
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq 0x1501c4(%rip), %rdx    # 0x1a54a6
movl $0x4, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x552f6
jmp 0x55744
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq 0x1509d6(%rip), %rsi    # 0x1a5cdf
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x40(%rbp)
cmpq $0x20, -0x40(%rbp)
je 0x5536b
jmp 0x55744
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq 0x1612a5(%rip), %rdx    # 0x1b6633
movl $0x2, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x553a2
jmp 0x55744
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
addq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jle 0x553d6
jmp 0x55744
movq -0x38(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x15bba0
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movl %eax, -0x8c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x8c(%rbp), %edx
leaq 0x1508b4(%rip), %rsi    # 0x1a5cf9
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl %eax, %edx
leaq 0x15089b(%rip), %rsi    # 0x1a5d09
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq 0x150883(%rip), %rsi    # 0x1a5d18
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0x150855(%rip), %rsi    # 0x1a5d29
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq 0x15083c(%rip), %rsi    # 0x1a5d37
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
cmpl $0x8, -0x8c(%rbp)
je 0x55525
cmpl $0x18, -0x8c(%rbp)
je 0x55525
jmp 0x55744
leaq -0x4a0(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0x16d580
cmpq $0x0, -0x68(%rbp)
je 0x555ff
movq -0x48(%rbp), %rax
addq $0xe, %rax
addq -0x68(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x1507e3(%rip), %rsi    # 0x1a5d45
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %edx
leaq 0x1507ab(%rip), %rsi    # 0x1a5d56
movb $0x0, %al
callq 0x15b0d0
cmpq $0x100, -0x88(%rbp) # imm = 0x100
jle 0x555c4
jmp 0x55744
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x4a0(%rbp), %r8
movl $0x3, %ecx
movl $0x100, %r9d            # imm = 0x100
movl $0x1, (%rsp)
callq 0x16c0c0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x5561a
jmp 0x55744
movq -0x8(%rbp), %rax
movq %rax, -0x4c0(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x4b8(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x4b0(%rbp)
xorl %eax, %eax
cmpl $0x18, -0x8c(%rbp)
movb %al, -0x4a1(%rbp)
jge 0x5565a
cmpq $0x0, -0x68(%rbp)
sete %al
movb %al, -0x4a1(%rbp)
movq -0x4b0(%rbp), %rdx
movq -0x4b8(%rbp), %rsi
movq -0x4c0(%rbp), %rdi
movb -0x4a1(%rbp), %r8b
movl $0x3, %ecx
movl $0x1, %eax
testb $0x1, %r8b
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rax
addq $0xe, %rax
addq -0x70(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x1506b8(%rip), %rsi    # 0x1a5d6a
movb $0x0, %al
callq 0x15b0d0
cmpl $0x18, -0x8c(%rbp)
jge 0x556f1
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x78(%rbp), %rcx
leaq -0x4a0(%rbp), %r8
movq -0xa0(%rbp), %r9
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
jmp 0x55714
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0xa0(%rbp), %r8
movl $0x3, %ecx
movl $0x1, %r9d
callq 0x16c8b0
movq -0xa0(%rbp), %rdi
movl 0x10(%rbp), %ecx
xorl %eax, %eax
movl %eax, %esi
leaq 0x161412(%rip), %rax    # 0x1b6b3b
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movl $0x4, %edx
callq 0x16afd0
movl $0x1, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
jne 0x5575f
movq -0x8(%rbp), %rdi
leaq 0x150622(%rip), %rsi    # 0x1a5d7a
movb $0x0, %al
callq 0x15b0d0
movq -0xa0(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x15b5a0
addq $0x4d0, %rsp            # imm = 0x4D0
popq %rbp
retq
nopw %cs:(%rax,%rax)