// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/psd.c
// Function: de_run_psd
static void de_run_psd(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 x;
	zztype *zz = NULL;
	zztype czz;
	int whattodo = 0;

	d = de_malloc(c, sizeof(lctx));
	zz = de_malloc(c, sizeof(zztype));
	zz_init_absolute(zz, 0, c->infile->len);

	if(de_havemodcode(c, mparams, 'R')) {
		whattodo = 'R';
	}
	else if(de_havemodcode(c, mparams, 'T')) {
		whattodo = 'T';
	}
	else if(de_havemodcode(c, mparams, 'B')) {
		whattodo = 'B';
	}
	else if(!dbuf_memcmp(c->infile, 0, "8BIM", 4)) {
		// Assume this is a raw resources file (maybe extracted from an
		// .8bimtiff file)
		whattodo = 'R';
	}

	if(whattodo=='R') { // Image resources
		de_declare_fmt(c, "Photoshop resources");
		d->version = 1;
		init_version_specific_info(c, d);
		do_image_resource_blocks(c, d, zz);
		if(mparams) {
			// .out_params.flags: 0x02: has_iptc
			mparams->out_params.flags = 0;
			if(d->has_iptc) mparams->out_params.flags |= 0x02;
		}
		goto done;
	}
	else if(whattodo=='T') { // Tagged blocks
		d->version = 1;
		init_version_specific_info(c, d);
		do_external_tagged_blocks(c, d, zz);
		goto done;
	}
	else if(whattodo=='B') { // Tagged blocks, PSB-format
		d->version = 2;
		init_version_specific_info(c, d);
		do_external_tagged_blocks(c, d, zz);
		goto done;
	}

	d->main_iinfo = de_malloc(c, sizeof(struct image_info));

	if(!do_psd_header(c, d, zz->pos)) goto done;
	zz->pos += 26;

	de_dbg(c, "color mode data section at %d", (int)zz->pos);
	de_dbg_indent(c, 1);
	x = psd_getu32zz(zz);
	zz_init_with_len(&czz, zz, x);
	do_psd_color_mode_data(c, d, &czz);
	zz->pos += x;
	de_dbg_indent(c, -1);

	de_dbg(c, "image resources section at %d", (int)zz->pos);
	de_dbg_indent(c, 1);
	x = psd_getu32zz(zz); // Length of Image Resources
	// The PSD spec is ambiguous, but in practice the "length" field's value
	// does not include the size of the "length" field itself.
	de_dbg(c, "image resources data at %d, len=%d", (int)zz->pos, (int)x);

	if(x>0) {
		if(de_get_ext_option_bool(c, "extract8bim", 0)) {
			fmtutil_handle_photoshop_rsrc(c, c->infile, zz->pos, x, 0x1);
		}
		else {
			de_dbg_indent(c, 1);
			zz_init_with_len(&czz, zz, x);
			do_image_resource_blocks(c, d, &czz);
			de_dbg_indent(c, -1);
		}
	}
	zz->pos += x;
	de_dbg_indent(c, -1);

	zz_init(&czz, zz);
	if(!do_layer_and_mask_info_section(c, d, &czz)) goto done;
	zz->pos += zz_used(&czz);

	zz_init(&czz, zz);
	do_image_data(c, d, &czz);

done:
	de_free(c, zz);
	if(d) {
		de_free(c, d->main_iinfo);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movl $0x58, %esi
callq 0xe8b2b
movq %rax, %r14
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r12, %rsi
movl $0x52, %edx
callq 0xea4d9
testl %eax, %eax
je 0xa3d11
leaq 0x87266(%rip), %rsi     # 0x12af16
movq %rbx, %rdi
callq 0xeb125
movl $0x1, (%r14)
movq $0x2, 0x18(%r14)
movq $0x4, 0x20(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x11, %edx
callq 0xeb220
movl %eax, 0x8(%r14)
movb $0x1, 0x14(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xa5b77
testq %r12, %r12
je 0xa3d8f
xorl %eax, %eax
cmpb $0x0, 0x30(%r14)
setne %al
addl %eax, %eax
movl %eax, 0x40(%r12)
jmp 0xa3d8f
movq %rbx, %rdi
movq %r12, %rsi
movl $0x54, %edx
callq 0xea4d9
testl %eax, %eax
je 0xa3d3e
movl $0x1, (%r14)
movq $0x2, 0x18(%r14)
movq $0x4, 0x20(%r14)
jmp 0xa3d69
movq %rbx, %rdi
movq %r12, %rsi
movl $0x42, %edx
callq 0xea4d9
testl %eax, %eax
je 0xa3dc0
movl $0x2, (%r14)
movq $0x4, 0x18(%r14)
movq $0x8, 0x20(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x11, %edx
callq 0xeb220
movl %eax, 0x8(%r14)
movb $0x1, 0x14(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xa5f72
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
movq 0x50(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
leaq 0xa66da(%rip), %rdx     # 0x14a4a5
movl $0x4, %ecx
xorl %esi, %esi
callq 0xefc85
testl %eax, %eax
je 0xa3ca9
movl $0x448, %esi            # imm = 0x448
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x50(%r14)
movq (%r13), %r12
leaq 0x958f3(%rip), %rsi     # 0x1396ee
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq 0x4(%r12), %rsi
movl 0x4(%r14), %edx
callq 0xeef43
movl %eax, (%r14)
leaq 0x90994(%rip), %rsi     # 0x1347c5
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
xorl %eax, %eax
cmpl $0x2, (%r14)
sete %al
leaq 0x2(%rax,%rax), %rcx
leaq 0x4(,%rax,4), %rax
movq %rcx, 0x18(%r14)
movq %rax, 0x20(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x11, %edx
callq 0xeb220
movl %eax, 0x8(%r14)
movb $0x1, 0x14(%r14)
movl (%r14), %edx
cmpl $0x1, %edx
je 0xa3e8d
cmpl $0x2, %edx
jne 0xa41b7
leaq 0x9092a(%rip), %rsi     # 0x1347b5
jmp 0xa3e94
leaq 0x8ef92(%rip), %rsi     # 0x132e26
movq %rbx, %rdi
callq 0xeb125
movq 0x18(%rbx), %rdi
leaq 0xc(%r12), %rsi
movl 0x4(%r14), %edx
callq 0xeef43
movq 0x50(%r14), %rcx
movq %rax, 0x18(%rcx)
leaq 0x7ae43(%rip), %rsi     # 0x11ed00
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0xe(%r12), %rsi
movl 0x4(%r14), %edx
callq 0xeeff3
movq 0x50(%r14), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rbx), %rdi
leaq 0x12(%r12), %rsi
movl 0x4(%r14), %edx
callq 0xeeff3
movq 0x50(%r14), %rcx
movq %rax, (%rcx)
movq 0x8(%rcx), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0xe8f88
movq 0x18(%rbx), %rdi
leaq 0x16(%r12), %rsi
movl 0x4(%r14), %edx
callq 0xeef43
movq 0x50(%r14), %rcx
movq %rax, 0x20(%rcx)
leaq 0x90a57(%rip), %rsi     # 0x134983
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
addq $0x18, %r12
movl 0x4(%r14), %edx
movq %r12, %rsi
callq 0xeef43
movq 0x50(%r14), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x9, %rax
ja 0xa3f67
leaq 0xe1eef(%rip), %rcx     # 0x185e50
movq (%rcx,%rax,8), %rcx
jmp 0xa3f6e
leaq 0x881e3(%rip), %rcx     # 0x12c151
leaq 0x90428(%rip), %rsi     # 0x13439d
xorl %r15d, %r15d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq (%r13), %rdx
addq $0x1a, %rdx
movq %rdx, (%r13)
leaq 0x8ef2f(%rip), %rsi     # 0x132ed3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq (%r13), %rsi
callq 0xeeff3
movq %rax, %r12
movq (%r13), %rax
movq 0x10(%r13), %rbp
leaq 0x4(%rax), %rdx
movq %rdx, (%r13)
testq %r12, %r12
movq %r12, %rcx
cmovleq %r15, %rcx
addq %rcx, %rax
addq $0x4, %rax
cmpq %rbp, %rax
cmovlq %rax, %rbp
movq 0x50(%r14), %rax
movq %rax, (%rsp)
subq %rdx, %rbp
cmovleq %r15, %rbp
movq %rdx, %r15
leaq 0x907c5(%rip), %rsi     # 0x1347d5
movq %rbx, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe8617
movabsq $0x5555555555555556, %rcx # imm = 0x5555555555555556
movq %rbp, %rax
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
addq %rdx, %rax
movq (%rsp), %rcx
movq %rax, 0x40(%rcx)
cmpq $0x3, %rbp
movq %r13, 0x10(%rsp)
jl 0xa4117
movq %r14, 0x18(%rsp)
movq %r12, 0x20(%rsp)
cmpq $0x303, %rbp            # imm = 0x303
movq (%rsp), %r14
jb 0xa406c
movq $0x100, 0x40(%r14)      # imm = 0x100
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x0, 0x40(%r14)
jle 0xa40fb
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
leaq (%r15,%rbp), %r12
movq %r12, %rsi
callq 0xeead3
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq 0x40(%r14), %rsi
addq %r12, %rsi
callq 0xeead3
movq %r15, %r14
movl %eax, %r15d
movq 0x18(%rbx), %rdi
movq (%rsp), %rax
movq 0x40(%rax), %rax
leaq (%r12,%rax,2), %rsi
callq 0xeead3
movzbl %r13b, %ecx
shll $0x10, %ecx
movzbl %r15b, %esi
movq %r14, %r15
movq (%rsp), %r14
shll $0x8, %esi
orl %ecx, %esi
movzbl %al, %edx
orl %esi, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, 0x48(%r14,%rbp,4)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe91fc
incq %rbp
cmpq 0x40(%r14), %rbp
jl 0xa4082
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r14
addq %r12, (%r13)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl (%r13), %edx
leaq 0x8edbe(%rip), %rsi     # 0x132ef1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq (%r13), %rsi
callq 0xeeff3
movq %rax, %r12
movq (%r13), %rdx
addq $0x4, %rdx
movq %rdx, (%r13)
leaq 0x8ed9e(%rip), %rsi     # 0x132f0f
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
testq %r12, %r12
jle 0xa4224
leaq 0x8eda4(%rip), %rsi     # 0x132f32
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe9bd0
testl %eax, %eax
je 0xa41da
movq 0x18(%rbx), %rsi
movq (%r13), %rdx
movq %rbx, %rdi
movq %r12, %rcx
movl $0x1, %r8d
callq 0xf7eb8
jmp 0xa4224
leaq 0x905fb(%rip), %rsi     # 0x1347b9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
jmp 0xa3d8f
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq (%r13), %rax
movq 0x10(%r13), %rcx
movq %r12, %rsi
addq %rax, %rsi
cmpq %rcx, %rsi
cmovgeq %rcx, %rsi
leaq 0x28(%rsp), %rdx
movq %rax, 0x8(%rdx)
movq %rax, (%rdx)
movq %rsi, 0x10(%rdx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa5b77
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq %r12, (%r13)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq (%r13), %r15
movq 0x10(%r13), %rax
leaq 0x28(%rsp), %r12
movq %r15, 0x8(%r12)
movq %r15, (%r12)
movq %rax, 0x10(%r12)
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
leaq 0x9058a(%rip), %rsi     # 0x1347ee
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xab121
leaq 0x58(%rsp), %r15
movq %rax, (%r15)
leaq 0x90573(%rip), %rsi     # 0x13480e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
callq 0xebad2
movq (%r12), %rax
movq 0x10(%r12), %rbp
movq (%r15), %rcx
leaq (%rcx,%rax), %r13
cmpq %rbp, %r13
jle 0xa42eb
leaq 0x90570(%rip), %rsi     # 0x13483b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq 0x10(%rsp), %r13
jmp 0xa3d8f
xorl %r12d, %r12d
testq %rcx, %rcx
cmovgq %rcx, %r12
addq %rax, %r12
cmpq %rbp, %r12
cmovgeq %rbp, %r12
leaq 0x40(%rsp), %rdx
movq %rax, 0x8(%rdx)
movq %rax, (%rdx)
movq %r12, 0x10(%rdx)
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %ecx
callq 0xaaad5
testl %eax, %eax
je 0xa442b
movq 0x50(%rsp), %r15
cmpq %r15, %r12
jle 0xa4347
leaq 0x9051e(%rip), %rsi     # 0x134856
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0xa442b
movq 0x40(%rsp), %r12
cmpq %r15, %r12
jge 0xa442b
leaq 0x90517(%rip), %rsi     # 0x134873
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %r12, %rsi
callq 0xeeff3
movq %rax, (%rsp)
leaq 0x904ff(%rip), %rsi     # 0x134890
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq (%rsp), %rax
addq %rax, %r12
addq $0x4, %r12
cmpq %r12, %r15
jge 0xa43c7
leaq 0x904fc(%rip), %rsi     # 0x1348be
jmp 0xa4338
jle 0xa442b
leaq 0x90517(%rip), %rsi     # 0x1348e7
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl %r15d, %edx
subl %r12d, %edx
leaq 0x90504(%rip), %rsi     # 0x1348fb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
leaq 0x60(%rsp), %rdx
movq %r12, 0x8(%rdx)
movq %r12, (%rdx)
movq %r15, 0x10(%rdx)
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0xa8d66
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq 0x30(%rsp), %rax
subq %rax, %r13
xorl %r12d, %r12d
cmpq %rax, %rbp
cmovgq %r13, %r12
movq 0x10(%rsp), %r13
addq (%r13), %r12
movq %r12, (%r13)
movq 0x10(%r13), %rcx
subq %r12, %rcx
jle 0xa3d8f
cmpq $0x2, %rcx
jl 0xa3d8f
leaq 0x904b4(%rip), %rsi     # 0x134928
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %r12, %rsi
callq 0xeef43
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
callq 0xab73d
cmpl $0x2, 0x8c(%rbx)
je 0xa4652
addq $0x2, %r12
movq 0x50(%r14), %rax
movq 0x48(%r14), %rcx
movq %rcx, 0x38(%rax)
movups 0x38(%r14), %xmm0
movups %xmm0, 0x28(%rax)
cmpq $0x1, %r13
je 0xa44fc
testq %r13, %r13
jne 0xa45b4
movq 0x50(%r14), %rdx
movq 0x18(%rbx), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %r8
callq 0xab76b
jmp 0xa4652
movq 0x50(%r14), %r13
movl 0x8(%r13), %ecx
imull 0x18(%r13), %ecx
imull 0x18(%r14), %ecx
leaq 0x904d0(%rip), %rsi     # 0x1349e5
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r13), %rax
imulq 0x18(%r13), %rax
testq %rax, %rax
jle 0xa4576
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq $0x4, 0x18(%r14)
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %edx
movq %r12, %rsi
jne 0xa4555
callq 0xeeff3
movl $0x4, %ecx
jmp 0xa455f
callq 0xeef43
movl $0x2, %ecx
addq %rax, %rbp
addq %rcx, %r12
incq %r15
movq 0x8(%r13), %rax
imulq 0x18(%r13), %rax
cmpq %rax, %r15
jl 0xa4537
leaq 0x81cae(%rip), %rsi     # 0x12622b
movq %rbx, %rdi
movq %r12, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
leaq (%r12,%rbp), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xa45cd
leaq 0x6e772(%rip), %rsi     # 0x112d14
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xa464a
leaq 0x90397(%rip), %rsi     # 0x134952
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9295
jmp 0xa4652
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r15
movq %rax, %rdi
callq 0xf0855
movq 0x18(%rbx), %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xfa81c
movq %r15, %rdi
callq 0xf08ba
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r15), %rcx
leaq 0x6e67c(%rip), %rsi     # 0x112c98
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xab76b
movq %r15, %rdi
callq 0xf052b
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
jmp 0xa42e1