// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x58
popq %rsi
callq 0xdecaf
movq %rax, %rbp
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq %rax, 0x10(%r15)
pushq $0x52
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfcf4
testl %eax, %eax
je 0x9de34
leaq 0x7feb1(%rip), %rsi     # 0x11dca6
movq %rbx, %rdi
callq 0xe07f7
movl $0x1, (%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x9f9fe
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x9fa35
testq %r12, %r12
je 0x9de81
xorl %eax, %eax
cmpb $0x0, 0x30(%rbp)
setne %al
addl %eax, %eax
movl %eax, 0x40(%r12)
jmp 0x9de81
pushq $0x54
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfcf4
testl %eax, %eax
je 0x9de4f
movl $0x1, (%rbp)
jmp 0x9de68
pushq $0x42
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfcf4
testl %eax, %eax
je 0x9deb2
movl $0x2, (%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x9f9fe
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x9fe11
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq 0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0xded32
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xded32
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
leaq 0x9f118(%rip), %rdx     # 0x13cfd5
pushq $0x4
popq %rcx
xorl %esi, %esi
callq 0xe4d48
testl %eax, %eax
je 0x9ddee
movl $0x448, %esi            # imm = 0x448
movq %rbx, %rdi
callq 0xdecaf
movq %rax, 0x50(%rbp)
movq (%r15), %r12
leaq 0x8e504(%rip), %rsi     # 0x12c3ee
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
leaq 0x4(%r12), %rsi
movl 0x4(%rbp), %edx
callq 0xe41b1
movl %eax, (%rbp)
leaq 0x895d0(%rip), %rsi     # 0x1274ed
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x9f9fe
movl (%rbp), %edx
cmpl $0x1, %edx
movq %r15, 0x8(%rsp)
je 0x9df53
cmpl $0x2, %edx
jne 0x9e217
leaq 0x8958c(%rip), %rsi     # 0x1274dd
jmp 0x9df5a
leaq 0x87bf4(%rip), %rsi     # 0x125b4e
movq %rbx, %rdi
callq 0xe07f7
movq 0x18(%rbx), %rdi
leaq 0xc(%r12), %rsi
movl 0x4(%rbp), %edx
callq 0xe41b1
movq 0x50(%rbp), %rcx
movq %rax, 0x18(%rcx)
leaq 0x73cae(%rip), %rsi     # 0x111c30
xorl %r15d, %r15d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0xe(%r12), %rsi
movl 0x4(%rbp), %edx
callq 0xe41cb
movq 0x50(%rbp), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rbx), %rdi
leaq 0x12(%r12), %rsi
movl 0x4(%rbp), %edx
callq 0xe41cb
movq 0x50(%rbp), %rcx
movq %rax, (%rcx)
movq 0x8(%rcx), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0xdf038
movq 0x18(%rbx), %rdi
leaq 0x16(%r12), %rsi
movl 0x4(%rbp), %edx
callq 0xe41b1
movq 0x50(%rbp), %rcx
movq %rax, 0x20(%rcx)
leaq 0x896ba(%rip), %rsi     # 0x1276ab
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
addq $0x18, %r12
movl 0x4(%rbp), %edx
movq %r12, %rsi
callq 0xe41b1
movq %rax, %r12
movq 0x50(%rbp), %rax
movq %r12, 0x10(%rax)
movq %r12, %rdi
callq 0xa4d0b
leaq 0x8909b(%rip), %rsi     # 0x1270c5
movq %rbx, %rdi
movl %r12d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x8(%rsp), %r14
movq (%r14), %rdx
addq $0x1a, %rdx
movq %rdx, (%r14)
leaq 0x87ba0(%rip), %rsi     # 0x125bfb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movl 0x4(%rbp), %edx
movq %r14, %rsi
callq 0x9fe70
testq %rax, %rax
movq %rbp, %rcx
movq %rax, 0x30(%rsp)
movq %rax, %rbp
cmovleq %r15, %rbp
movq (%r14), %rdx
movq 0x10(%r14), %rax
addq %rdx, %rbp
cmpq %rax, %rbp
cmovgeq %rax, %rbp
movq %rcx, (%rsp)
movq 0x50(%rcx), %r14
subq %rdx, %rbp
cmovleq %r15, %rbp
leaq 0x89445(%rip), %rsi     # 0x1274fd
movq %rbx, %rdi
movq %rdx, 0x38(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde896
pushq $0x3
popq %rcx
movq %rbp, %rax
cqto
idivq %rcx
movq %rax, 0x40(%r14)
cmpq $0x3, %rbp
jl 0x9e17d
cmpq $0x303, %rbp            # imm = 0x303
jb 0x9e0f3
movq $0x100, 0x40(%r14)      # imm = 0x100
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
xorl %ebp, %ebp
cmpq 0x40(%r14), %rbp
jge 0x9e172
movq 0x18(%rbx), %rdi
movq 0x38(%rsp), %rax
leaq (%rax,%rbp), %r12
movq %r12, %rsi
callq 0xe3eb4
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq 0x40(%r14), %rsi
addq %r12, %rsi
callq 0xe3eb4
movl %eax, %r15d
movq 0x18(%rbx), %rdi
movq 0x40(%r14), %rax
leaq (%r12,%rax,2), %rsi
callq 0xe3eb4
movzbl %r13b, %ecx
shll $0x10, %ecx
movzbl %r15b, %esi
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 0xdf23b
incq %rbp
jmp 0x9e100
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x8(%rsp), %r14
movq 0x30(%rsp), %rax
addq %rax, (%r14)
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movl (%r14), %edx
leaq 0x87a7a(%rip), %rsi     # 0x125c19
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq (%rsp), %rbp
movl 0x4(%rbp), %edx
movq %r14, %rsi
callq 0x9fe70
movq %rax, %r12
movl (%r14), %edx
leaq 0x87a63(%rip), %rsi     # 0x125c37
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0xde896
testq %r12, %r12
jle 0x9e27d
leaq 0x87a69(%rip), %rsi     # 0x125c5a
movq %rbx, %rdi
xorl %edx, %edx
callq 0xdfae5
testl %eax, %eax
je 0x9e238
movq 0x18(%rbx), %rsi
movq (%r14), %rdx
pushq $0x1
popq %r8
movq %rbx, %rdi
movq %r12, %rcx
callq 0xec5fa
jmp 0x9e27d
leaq 0x892c3(%rip), %rsi     # 0x1274e1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
jmp 0x9de81
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq (%r14), %rax
movq 0x10(%r14), %rcx
movq %r12, %rsi
addq %rax, %rsi
cmpq %rcx, %rsi
cmovgeq %rcx, %rsi
leaq 0x18(%rsp), %rdx
movq %rax, 0x8(%rdx)
movq %rax, (%rdx)
movq %rsi, 0x10(%rdx)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x9fa35
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq %r12, (%r14)
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq (%r14), %r15
movq 0x10(%r14), %rax
leaq 0x18(%rsp), %r12
movq %r15, 0x8(%r12)
movq %r15, (%r12)
movq %rax, 0x10(%r12)
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0xdebae
leaq 0x8925d(%rip), %rsi     # 0x127516
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xa49f1
leaq 0x58(%rsp), %r15
movq %rax, (%r15)
leaq 0x89248(%rip), %rsi     # 0x127536
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r15, %rdi
callq 0xe10b1
movq (%r12), %r14
movq 0x10(%r12), %r13
movq (%r15), %rax
movq %rbp, %rsi
leaq (%rax,%r14), %rbp
cmpq %r13, %rbp
jle 0x9e330
leaq 0x89242(%rip), %rsi     # 0x127563
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x9e475
xorl %r15d, %r15d
testq %rax, %rax
cmovgq %rax, %r15
addq %r14, %r15
cmpq %r13, %r15
cmovgeq %r13, %r15
leaq 0x40(%rsp), %rdx
movq %r14, 0x8(%rdx)
movq %r14, (%rdx)
movq %r15, 0x10(%rdx)
pushq $0x1
popq %rcx
movq %rbx, %rdi
callq 0xa43f4
movq %rbp, %r14
testl %eax, %eax
je 0x9e475
movq 0x50(%rsp), %rax
cmpq %rax, %r15
jle 0x9e38d
leaq 0x89203(%rip), %rsi     # 0x12757e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq %rbp, %r14
jmp 0x9e475
movq 0x40(%rsp), %rdx
movq %rbp, %r14
cmpq %rax, %rdx
jge 0x9e475
leaq 0x891f6(%rip), %rsi     # 0x12759b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq (%rsp), %rax
movl 0x4(%rax), %edx
leaq 0x40(%rsp), %r15
movq %r15, %rsi
callq 0x9fe70
movq %rax, %r12
leaq 0x891dc(%rip), %rsi     # 0x1275b8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq (%r15), %r12
movq 0x10(%r15), %r15
cmpq %r12, %r15
jge 0x9e40c
leaq 0x891df(%rip), %rsi     # 0x1275e6
jmp 0x9e37b
movq %rbp, %r14
jle 0x9e475
leaq 0x891f7(%rip), %rsi     # 0x12760f
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movl %r15d, %edx
subl %r12d, %edx
leaq 0x891e6(%rip), %rsi     # 0x127623
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0x60(%rsp), %rdx
movq %r12, 0x8(%rdx)
movq %r12, (%rdx)
movq %r15, 0x10(%rdx)
movq %rbx, %rdi
movq (%rsp), %rsi
xorl %ecx, %ecx
callq 0xa2803
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
jmp 0x9e385
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
cmpq %r13, %rbp
movq (%rsp), %rbp
movq 0x8(%rsp), %r15
jg 0x9de81
movq 0x20(%rsp), %rax
movq %r13, %rdx
subq %rax, %rdx
jle 0x9e4ad
cmpq %r13, %r14
jg 0x9e4af
subq %rax, %r14
movq %r14, %rdx
jmp 0x9e4af
xorl %edx, %edx
addq (%r15), %rdx
movq %rdx, (%r15)
movq 0x10(%r15), %rcx
movq %rdx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
subq %rdx, %rcx
jle 0x9de81
cmpq $0x2, %rcx
jl 0x9de81
leaq 0x8916e(%rip), %rsi     # 0x127650
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movl 0x4(%rbp), %edx
leaq 0x18(%rsp), %rsi
callq 0x9feb0
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0xa4fc5
cmpl $0x2, 0x8c(%rbx)
je 0x9e228
movq 0x18(%rsp), %r8
movq 0x28(%rsp), %rax
movq 0x50(%rbp), %rcx
movq 0x48(%rbp), %rdx
movq %rdx, 0x38(%rcx)
movups 0x38(%rbp), %xmm0
movups %xmm0, 0x28(%rcx)
cmpq $0x1, %r12
je 0x9e568
testq %r12, %r12
jne 0x9e62b
movq 0x50(%rbp), %rdx
movq 0x18(%rbx), %rcx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa4ff3
jmp 0x9e228
movq %rbp, %rdx
leaq 0x40(%rsp), %rbp
movq %r8, 0x8(%rbp)
movq %r8, (%rbp)
movq %rax, 0x10(%rbp)
movq 0x50(%rdx), %r12
movl 0x8(%r12), %ecx
imull 0x18(%r12), %ecx
imull 0x18(%rdx), %ecx
leaq 0x89177(%rip), %rsi     # 0x12770d
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r8, %rdx
xorl %eax, %eax
callq 0xde896
xorl %r14d, %r14d
movq 0x8(%r12), %rax
imulq 0x18(%r12), %rax
cmpq %rax, %r14
jge 0x9e5e2
movq (%rsp), %rax
cmpq $0x4, 0x18(%rax)
movq 0x18(%rbx), %rdi
movl 0x4(%rax), %edx
movq %rbp, %rsi
jne 0x9e5d5
callq 0x9fe70
jmp 0x9e5da
callq 0x9feb0
addq %rax, %r13
incq %r14
jmp 0x9e5a9
movq 0x40(%rsp), %rbp
leaq 0x7aa3d(%rip), %rsi     # 0x11902b
movq %rbx, %rdi
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xde896
movq %rbp, %rax
addq %r13, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x9e63d
leaq 0x676df(%rip), %rsi     # 0x105cf4
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq (%rsp), %rbp
jmp 0x9e6ba
leaq 0x89048(%rip), %rsi     # 0x12767a
movq %rbx, %rdi
movl %r12d, %edx
jmp 0x9e221
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r14
movq %rax, %rdi
callq 0xe589c
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0xeed6a
movq %r14, %rdi
callq 0xe58e8
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%r14), %rcx
leaq 0x675ee(%rip), %rsi     # 0x105c78
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq (%rsp), %rbp
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0xa4ff3
movq %r14, %rdi
callq 0xe5591
jmp 0x9e228