// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/psd.c
// Function: do_layer_info_section
static int do_layer_info_section(deark *c, lctx *d, zztype *zz, int has_len_field)
{
	int retval = 0;
	i64 layer_info_len;
	i64 layer_count_raw, layer_count;
	int saved_indent_level;
	int merged_result_flag;
	i64 layer_idx;
	zztype datazz;
	zztype czz;
	struct channel_data *cd = NULL;

	de_dbg_indent_save(c, &saved_indent_level);
	if(zz_avail(zz)<4) goto done;

	de_dbg(c, "layer info section at %d", (int)zz->pos);
	de_dbg_indent(c, 1);

	if(has_len_field) {
		layer_info_len = psd_getu32or64zz(c, d, zz);
		de_dbg(c, "length of layer info section: %d", (int)layer_info_len);
		de_sanitize_length(&layer_info_len);
	}
	else {
		layer_info_len = zz_avail(zz);
	}
	zz_init_with_len(&datazz, zz, layer_info_len);
	zz->pos += layer_info_len;
	retval = 1;

	if(datazz.pos>=datazz.endpos) {
		// If the length field is 0, it's legal for this section to end here.
		goto done;
	}

	layer_count_raw = psd_geti16zz(&datazz);
	if(layer_count_raw<0) {
		merged_result_flag = 1;
		layer_count = -layer_count_raw;
	}
	else {
		merged_result_flag = 0;
		layer_count = layer_count_raw;
	}
	de_dbg(c, "layer count: %d", (int)layer_count);
	de_dbg(c, "merged result flag: %d", (int)merged_result_flag);

	// Due to the recursive possibilities of PSD format, it would probably
	// be a bad idea to store this channel information in the 'd' struct.
	// Instead, we'll use a local variable.
	cd = de_malloc(c, sizeof(struct channel_data));
	cd->num_channels = 0;
	cd->total_len = 0;

	for(layer_idx=0; layer_idx<layer_count; layer_idx++) {
		de_dbg(c, "layer record[%d] at %d", (int)layer_idx, (int)datazz.pos);
		de_dbg_indent(c, 1);
		zz_init(&czz, &datazz);
		if(!do_layer_record(c, d, &czz, cd))
			goto done;
		datazz.pos += zz_used(&czz);
		de_dbg_indent(c, -1);
	}

	de_dbg(c, "channel image data records at %d, count=%d, total len=%"I64_FMT"",
		(int)datazz.pos, (int)cd->num_channels, cd->total_len);

done:
	de_dbg_indent_restore(c, saved_indent_level);
	de_free(c, cd);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x44(%rsp), %rsi
callq 0xdebae
movq %r15, %rdi
callq 0x9ff2f
cmpq $0x4, %rax
jge 0xa4439
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
jmp 0xa49c3
movl (%r15), %edx
leaq 0x8299a(%rip), %rsi     # 0x126ddd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %rsi, 0x30(%rsp)
callq 0xdeba7
testl %ebp, %ebp
je 0xa449c
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xa49f1
leaq 0x88(%rsp), %r12
movq %rax, (%r12)
leaq 0x82974(%rip), %rsi     # 0x126df6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
callq 0xe10b1
movq (%r12), %rax
jmp 0xa44ac
movq %r15, %rdi
callq 0x9ff2f
movq %rax, 0x88(%rsp)
xorl %r12d, %r12d
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
movq (%r15), %rdx
movq 0x10(%r15), %rsi
addq %rdx, %rcx
cmpq %rsi, %rcx
cmovgeq %rsi, %rcx
movq %rdx, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rcx, 0xa0(%rsp)
addq %rdx, %rax
movq %rax, (%r15)
cmpq %rcx, %rdx
jge 0xa49c3
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
leaq 0x90(%rsp), %r15
movq %r15, %rsi
callq 0xa4a0d
movq %rax, %r12
movq %rax, %rdx
negq %rdx
cmovsq %rax, %rdx
shrq $0x3f, %r12
leaq 0x828f4(%rip), %rsi     # 0x126e17
movq %rbx, %rdi
movq %rdx, 0x58(%rsp)
xorl %eax, %eax
callq 0xde896
leaq 0x828ee(%rip), %rsi     # 0x126e27
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
xorps %xmm0, %xmm0
movq %rax, 0x38(%rsp)
movups %xmm0, (%rax)
movq (%r15), %r13
movq 0x10(%r15), %rax
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x30(%rsp)
leaq 0x18(%rsp), %r15
xorl %edx, %edx
cmpq 0x58(%rsp), %rdx
je 0xa4975
movq %rbx, %rdi
leaq 0x828b2(%rip), %rsi     # 0x126e3e
movq %rdx, 0x78(%rsp)
movl %r13d, %ecx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %r13, 0x20(%rsp)
movq %r13, 0x80(%rsp)
movq %r13, 0x18(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
leaq 0x8202f(%rip), %rcx     # 0x126601
callq 0xa1409
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %r15, %rsi
callq 0x9feb0
movq %rax, %r15
movq %rbx, %rdi
leaq 0x6d63c(%rip), %rsi     # 0x111c30
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0xa4675
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
leaq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0xa4a0d
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xa49f1
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
leaq 0x82849(%rip), %rsi     # 0x126e8f
movl %ebp, %edx
movl %r13d, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0xde896
leaq 0x48(%rsp), %rdi
callq 0xe10b1
movq 0x38(%rsp), %rcx
incq (%rcx)
movq 0x48(%rsp), %rax
addq %rax, 0x8(%rcx)
incq %rbp
jmp 0xa460c
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x18(%rsp), %r15
movq %r15, %rdx
leaq 0xa8(%rsp), %r12
movq %r12, %rcx
callq 0x9fe82
cmpl $0x3842494d, 0xac(%rsp) # imm = 0x3842494D
jne 0xa4999
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x9fe82
movq %rbx, %rdi
leaq 0x8281e(%rip), %rsi     # 0x126edd
leaq 0xb8(%rsp), %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x9ff51
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x82807(%rip), %rsi     # 0x126eee
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x9ff51
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x827f3(%rip), %rsi     # 0x126efa
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x9ff51
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x7e6bf(%rip), %rsi     # 0x122de6
xorl %eax, %eax
callq 0xde896
incq 0x18(%rsp)
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %r15, %rsi
callq 0x9fe70
movq %rax, %r15
movq 0x18(%rsp), %r13
movq 0x28(%rsp), %rcx
addq %r13, %rax
cmpq %rcx, %rax
jg 0xa49a9
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
addq %r13, %r15
cmpq %rcx, %r15
movq %rcx, 0x70(%rsp)
cmovgeq %rcx, %r15
movq %r15, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movq %r13, (%rsp)
movq %r15, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %rsp, %rsi
callq 0x9fe70
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x827a6(%rip), %rsi     # 0x126f4d
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
movq %rdx, %rcx
movl $0x0, %r12d
subq %rax, %rcx
jle 0xa47dc
addq (%rsp), %rbp
cmpq %rdx, %rbp
jle 0xa47d6
movq %rcx, %r12
jmp 0xa47dc
subq %rax, %rbp
movq %rbp, %r12
addq %r13, %r12
movq %r12, 0x8(%rsp)
movq %r12, (%rsp)
movq %r15, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %rsp, %rsi
callq 0x9fe70
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x8275c(%rip), %rsi     # 0x126f66
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
jle 0xa4836
addq (%rsp), %rbp
cmpq %rcx, %rbp
jg 0xa4838
subq %rax, %rbp
movq %rbp, %rdx
jmp 0xa4838
xorl %edx, %edx
addq %r12, %rdx
movq %rdx, 0x8(%rsp)
movq %rdx, 0x68(%rsp)
movq %rdx, (%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %rsp, %rcx
callq 0x9fec2
movq %r13, 0x60(%rsp)
movq %r13, %rdi
callq 0xeb7e4
movq %rbx, %rdi
leaq 0x82709(%rip), %rsi     # 0x126f8a
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %r13
movq %r13, %rbp
subq %rax, %rbp
jle 0xa48ae
movq (%rsp), %rdi
cmpq %r13, %rdi
jle 0xa48b5
movq %rax, %r12
movq %rbp, %rdi
jmp 0xa48bb
movq %rax, %r12
xorl %edi, %edi
jmp 0xa48bb
movq %rax, %r12
subq %rax, %rdi
callq 0xdfb90
leaq (%rax,%r12), %rcx
cmpq %r13, %rcx
cmovleq %rax, %rbp
cmpq %r12, %r13
movl $0x0, %eax
cmovleq %rax, %rbp
movq 0x60(%rsp), %rdi
callq 0xeaf3c
addq 0x68(%rsp), %rbp
cmpq %rbp, %r15
jle 0xa4937
movl %r15d, %ecx
subl %ebp, %ecx
movq %rbx, %rdi
leaq 0x8262a(%rip), %rsi     # 0x126f24
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %rbp, 0x8(%rsp)
movq %rbp, (%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %rsp, %rdx
xorl %ecx, %ecx
callq 0xa2803
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x20(%rsp), %rax
subq %rax, %r15
cmpq %rax, 0x70(%rsp)
movl $0x0, %eax
cmovleq %rax, %r15
movq 0x80(%rsp), %r13
addq %r15, %r13
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x78(%rsp), %rdx
incq %rdx
leaq 0x18(%rsp), %r15
jmp 0xa4577
movq 0x38(%rsp), %r12
movl (%r12), %ecx
movq 0x8(%r12), %r8
leaq 0x824cb(%rip), %rsi     # 0x126e55
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
jmp 0xa49c3
movl 0x18(%rsp), %edx
addl $-0x4, %edx
leaq 0x82508(%rip), %rsi     # 0x126eaf
jmp 0xa49b4
movl 0x20(%rsp), %edx
leaq 0x82553(%rip), %rsi     # 0x126f07
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x38(%rsp), %r12
movl 0x44(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq %rbx, %rdi
movq %r12, %rsi
callq 0xded32
movq 0x30(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq