// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc2.c
// Function: do_pixfolio_item
static void do_pixfolio_item(deark *c, struct pixfolio_ctx *d,
	struct pixfolio_item_ctx *md)
{
	i64 pos;
	i64 dibpos;
	i64 dib_len1, dib_len2, dib_len;
	i64 descr_len;
	i64 xtra_len;
	i64 amt_to_copy1, amt_to_copy2;
	dbuf *outf = NULL;
	de_finfo *fi = NULL;
	int ret;
	int errflag = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "item at %"I64_FMT, md->pos);
	de_dbg_indent(c, 1);

	fi = de_finfo_create(c);
	md->basefn_srd = dbuf_read_string(c->infile, md->pos, 12, 12, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "name: \"%s\"", ucstring_getpsz_d(md->basefn_srd->str));
	pixfolio_check_blank_name(c, d, md);

	ucstring_empty(d->tmpstr);
	dbuf_read_to_ucstring(c->infile, md->pos+25, 64, d->tmpstr, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "path: \"%s\"", ucstring_getpsz_d(d->tmpstr));

	read_pixfolio_timestamp(c, d, md);

	pos = md->pos + 363;
	// TODO: The "dib_len" fields need more research. They are usually equal.
	// Sometimes, one of them is 0, so we use the other one.
	dib_len1 = de_getu32le_p(&pos); // Is this 32 or 16 bit?
	de_dbg(c, "dib len1: %"I64_FMT, dib_len1);
	dib_len2 = de_getu16le_p(&pos);
	de_dbg(c, "dib len2: %"I64_FMT, dib_len2);
	if(dib_len1 && dib_len2 && (dib_len1 != dib_len2)) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}
	dib_len = dib_len2 ? dib_len2 : dib_len1;

	descr_len = de_getu16le_p(&pos);
	de_dbg(c, "descr len: %"I64_FMT, descr_len);
	xtra_len = de_getu16le_p(&pos); // TODO: What is this segment
	de_dbg(c, "xtra len: %"I64_FMT, xtra_len);

	md->total_len = 373 + descr_len + 1 + dib_len + xtra_len;
	pos += descr_len + 1;
	dibpos = pos;
	de_dbg(c, "dib pos: %"I64_FMT, dibpos);
	if(dibpos+dib_len > c->infile->len) {
		errflag = 1;
		goto done;
	}

	if(dib_len==0) {
		de_dbg(c, "[no image]");
		goto done;
	}

	if(md->blank_basefn) {
		de_dbg(c, "[deleted]");
		d->deleted_count++;
		if(!d->opt_undelete) goto done;
	}

	// Expecting a 40-byte BITMAPINFOHEADER here. If it's not, assume
	// we've gone off the rails.
	if((UI)de_getu32le(dibpos) != 40) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	ret = fmtutil_get_bmpinfo(c, c->infile, &md->bi, dibpos,
		c->infile->len - dibpos, 0);
	if(!ret) {
		errflag = 1;
		goto done;
	}

	amt_to_copy1 = dib_len;
	amt_to_copy2 = md->bi.size_of_headers_and_pal + md->bi.sizeImage_field;
	amt_to_copy1 = de_min_int(amt_to_copy1, amt_to_copy2);

	if(md->blank_basefn) {
		de_finfo_set_name_from_sz(c, fi, "deleted", 0, DE_ENCODING_LATIN1);
	}
	else if(c->filenames_from_file) {
		de_finfo_set_name_from_ucstring(c, fi, md->basefn_srd->str, 0);
	}

	outf = dbuf_create_output_file(c, "bmp", fi, 0);
	fmtutil_generate_bmpfileheader(c, outf, &md->bi, 14+amt_to_copy1);
	dbuf_copy(c->infile, dibpos, amt_to_copy1, outf);

done:
	if(md->total_len<=0) {
		d->fatalerrflag = 1;
	}
	if(errflag) {
		de_err(c, "Failed to decode item at %"I64_FMT, md->pos);
	}
	dbuf_close(outf);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq 0x11f34d(%rip), %rsi    # 0x1bc77a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0xc, %ecx
movl $0x1, %r8d
movq %rcx, %rdx
callq 0x1664c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172650
movq -0x90(%rbp), %rdi
movq %rax, %rdx
leaq 0x110d20(%rip), %rsi    # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x9d8d0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
addq $0x19, %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x40, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x172650
movq -0x88(%rbp), %rdi
movq %rax, %rdx
leaq 0x13292c(%rip), %rsi    # 0x1cfe5c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x9d950
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x16b, %rax            # imm = 0x16B
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x113979(%rip), %rsi    # 0x1b0ef6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x11395c(%rip), %rsi    # 0x1b0f04
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0x30(%rbp)
je 0x9d5dc
cmpq $0x0, -0x38(%rbp)
je 0x9d5dc
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x9d5dc
movq -0x10(%rbp), %rax
movb $0x1, 0x5(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x9d865
cmpq $0x0, -0x38(%rbp)
je 0x9d5f0
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x9d5fb
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x1138e8(%rip), %rsi    # 0x1b0f12
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x1138cc(%rip), %rsi    # 0x1b0f21
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rcx
addq $0x175, %rcx            # imm = 0x175
addq $0x1, %rcx
addq -0x40(%rbp), %rcx
addq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x11388d(%rip), %rsi    # 0x1b0f2f
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x9d6cb
movl $0x1, -0x78(%rbp)
jmp 0x9d865
cmpq $0x0, -0x40(%rbp)
jne 0x9d6e9
movq -0x8(%rbp), %rdi
leaq 0x11385f(%rip), %rsi    # 0x1b0f3c
movb $0x0, %al
callq 0x15b0d0
jmp 0x9d865
movq -0x18(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x9d723
movq -0x8(%rbp), %rdi
leaq 0x110b8e(%rip), %rsi    # 0x1ae28c
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x4(%rax)
jne 0x9d721
jmp 0x9d865
jmp 0x9d723
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x165540
cmpl $0x28, %eax
je 0x9d74e
movq -0x10(%rbp), %rax
movb $0x1, 0x5(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x9d865
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rdx
addq $0x20, %rdx
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %r8
subq -0x28(%rbp), %r8
xorl %r9d, %r9d
callq 0x172d70
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jne 0x9d793
movl $0x1, -0x78(%rbp)
jmp 0x9d865
movq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0x60(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x15ccf0
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x9d7eb
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
leaq 0x104cb0(%rip), %rdx    # 0x1a248c
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0x15d4c0
jmp 0x9d815
movq -0x8(%rbp), %rax
cmpb $0x0, 0xd0(%rax)
je 0x9d813
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
jmp 0x9d815
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq 0x10f4e3(%rip), %rsi    # 0x1acd07
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x20, %rdx
movq -0x58(%rbp), %rcx
addq $0xe, %rcx
callq 0x1734e0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x166070
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jg 0x9d878
movq -0x10(%rbp), %rax
movb $0x1, 0x5(%rax)
cmpl $0x0, -0x78(%rbp)
je 0x9d897
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq 0x1136b7(%rip), %rsi    # 0x1b0f47
movb $0x0, %al
callq 0x15bfb0
movq -0x68(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0x7c(%rbp), %esi
callq 0x15b5a0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)