// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/psd.c
// Function: do_one_linked_layer
static int do_one_linked_layer(deark *c, lctx *d, zztype *zz, const struct de_fourcc *blk4cc)
{
	int retval = 0;
	i64 dlen, dlen2=0;
	i64 ver;
	u8 file_open_descr_flag;
	struct de_fourcc type4cc;
	struct de_fourcc tmp4cc;
	de_ucstring *s = NULL;
	zztype datazz;

	dlen = psd_geti64zz(zz);
	de_dbg(c, "length: %"I64_FMT"", dlen);
	de_sanitize_length(&dlen);
	if(dlen<8 || zz->pos+dlen>zz->endpos) {
		de_warn(c, "Bad linked layer size %"I64_FMT" at %"I64_FMT"", dlen, zz->startpos);
		goto done;
	}

	zz_init_with_len(&datazz, zz, dlen);

	// Seems to be padded to a multiple of 4 bytes. (The spec says nothing
	// about this.)
	zz->pos += de_pad_to_4(dlen);
	retval = 1;

	psd_read_fourcc_zz(c, d, &datazz, &type4cc);
	de_dbg(c, "type: '%s'", type4cc.id_dbgstr);

	ver = psd_getu32zz(&datazz);
	de_dbg(c, "version: %d", (int)ver);

	s = ucstring_create(c);
	read_pascal_string_to_ucstring(c, d, s, &datazz);
	de_dbg(c, "unique id: \"%s\"", ucstring_getpsz(s));

	ucstring_empty(s);
	read_unicode_string(c, d, s, &datazz);
	de_dbg(c, "original file name: \"%s\"", ucstring_getpsz(s));

	psd_read_fourcc_zz(c, d, &datazz, &tmp4cc);
	de_dbg(c, "file type: '%s'", tmp4cc.id_dbgstr);

	psd_read_fourcc_zz(c, d, &datazz, &tmp4cc);
	de_dbg(c, "file creator: '%s'", tmp4cc.id_dbgstr);

	dlen2 = psd_geti64zz(&datazz);
	de_dbg(c, "length2: %"I64_FMT"", dlen2);
	de_sanitize_length(&dlen2);
	if(dlen2<0) goto done;

	file_open_descr_flag = psd_getbytezz(&datazz);
	de_dbg(c, "has file open descriptor: %d", (int)file_open_descr_flag);

	if(file_open_descr_flag) {
		if(!read_descriptor(c, d, &datazz, 1, " (of open parameters)")) {
			goto done;
		}
	}

	if(type4cc.id!=CODE_liFD) {
		// TODO: liFA and liFE need special handling.
		de_dbg(c, "[this linked layer type is not supported]");
		goto done;
	}

	de_dbg(c, "raw file bytes at %"I64_FMT", len=%"I64_FMT"", datazz.pos, dlen2);
	extract_linked_layer_blob(c, d, datazz.pos, dlen2);

	// TODO: There may be more fields after this, depending on the version.

done:
	ucstring_destroy(s);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0xf8890
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xa8877(%rip), %rsi     # 0x1a2a97
movb $0x0, %al
callq 0x15b0d0
leaq -0x30(%rbp), %rdi
callq 0x15f830
cmpq $0x8, -0x30(%rbp)
jl 0xfa24c
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jle 0xfa26f
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0xc5a70(%rip), %rsi     # 0x1bfcd3
movb $0x0, %al
callq 0x15bc10
jmp 0xfa521
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0xf3380
movq -0x30(%rbp), %rdi
callq 0x15ce30
movq %rax, %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl $0x1, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
leaq -0x74(%rbp), %rcx
callq 0xf3d80
movq -0x8(%rbp), %rdi
leaq -0x74(%rbp), %rdx
addq $0x10, %rdx
leaq 0xb3714(%rip), %rsi     # 0x1ad9df
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0xc8(%rbp), %rsi
callq 0xf3330
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0xc5f0e(%rip), %rsi     # 0x1c0210
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xb0(%rbp), %rdx
leaq -0xc8(%rbp), %rcx
callq 0xf3e30
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x172500
movq -0xd8(%rbp), %rdi
movq %rax, %rdx
leaq 0xc5998(%rip), %rsi     # 0x1bfcf4
movb $0x0, %al
callq 0x15b0d0
movq -0xb0(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xb0(%rbp), %rdx
leaq -0xc8(%rbp), %rcx
callq 0xf54d0
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x172500
movq -0xd0(%rbp), %rdi
movq %rax, %rdx
leaq 0xc5952(%rip), %rsi     # 0x1bfd04
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
leaq -0xa4(%rbp), %rcx
callq 0xf3d80
movq -0x8(%rbp), %rdi
leaq -0xa4(%rbp), %rdx
addq $0x10, %rdx
leaq 0xc5933(%rip), %rsi     # 0x1bfd1d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
leaq -0xa4(%rbp), %rcx
callq 0xf3d80
movq -0x8(%rbp), %rdi
leaq -0xa4(%rbp), %rdx
addq $0x10, %rdx
leaq 0xc590b(%rip), %rsi     # 0x1bfd2d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0xc8(%rbp), %rsi
callq 0xf8890
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xc58e9(%rip), %rsi     # 0x1bfd40
movb $0x0, %al
callq 0x15b0d0
leaq -0x38(%rbp), %rdi
callq 0x15f830
cmpq $0x0, -0x38(%rbp)
jge 0xfa473
jmp 0xfa521
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xc8(%rbp), %rsi
callq 0xf4120
movb %al, -0x41(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x41(%rbp), %edx
leaq 0xc58b4(%rip), %rsi     # 0x1bfd4d
movb $0x0, %al
callq 0x15b0d0
cmpb $0x0, -0x41(%rbp)
je 0xfa4cf
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
movl $0x1, %ecx
leaq 0xc58a9(%rip), %r8      # 0x1bfd6a
callq 0xf5b50
cmpl $0x0, %eax
jne 0xfa4cd
jmp 0xfa521
jmp 0xfa4cf
cmpl $0x6c694644, -0x70(%rbp) # imm = 0x6C694644
je 0xfa4ec
movq -0x8(%rbp), %rdi
leaq 0xc589d(%rip), %rsi     # 0x1bfd80
movb $0x0, %al
callq 0x15b0d0
jmp 0xfa521
movq -0x8(%rbp), %rdi
movq -0xc8(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq 0xc58a8(%rip), %rsi     # 0x1bfdaa
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0xfa540
movq -0xb0(%rbp), %rdi
callq 0x171750
movl -0x24(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)