// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/gws.c
// Function: de_run_imggal_alch
static void de_run_imggal_alch(deark *c, de_module_params *mparams)
{
	struct imggal_ctx *d = NULL;
	const char *s;
	i64 i;

	d = de_malloc(c, sizeof(struct imggal_ctx));
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

	// 8: gallery comment

	d->item_count = de_getu16le(73);
	de_dbg(c, "number of items: %d", (int)d->item_count);

	d->imgtype = de_getbyte(75);
	de_dbg(c, "image type: %u", (UI)d->imgtype);
	de_dbg_indent(c, 1);

	s = (d->imgtype<=1 || d->imgtype==4) ? "portrait" : "landscape";
	de_dbg(c, "orientation: %s", s);

	if(d->imgtype==1 || d->imgtype==3) {
		s = "grayscale";
		d->bpp = 4;
	}
	else if(d->imgtype==4 || d->imgtype==5) {
		s = "color";
		d->is_color = 1;
		d->bpp = 4;
	}
	else {
		s = "bilevel";
		d->bpp = 1;
	}
	de_dbg(c, "color mode: %s", s);

	de_dbg_indent(c, -1);

	d->item_size = de_getu16le(87);
	de_dbg(c, "item size: %"I64_FMT, d->item_size);

	// 89: gallery creation time
	// 93: gallery last-modified time

	if(d->imgtype>5) {
		d->need_errmsg = 1;
		goto done;
	}

	if(d->item_size<649 || (IMGGAL_HDRSIZE+d->item_count*d->item_size > c->infile->len)) {
		d->need_errmsg = 1;
		goto done;
	}

	for(i=0; i<d->item_count; i++) {
		if(!do_imggal_member(c, d, i, IMGGAL_HDRSIZE+i*d->item_size)) {
			goto done;
		}
	}

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Invalid or unsupported GAL file");
		}
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
pushq $0x20
popq %rsi
callq 0xdecaf
movq %rax, %r14
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, (%r14)
movq 0x18(%rbx), %rdi
pushq $0x49
popq %rsi
callq 0xe3fa3
movq %rax, 0x8(%r14)
leaq 0xbec85(%rip), %rsi     # 0x11f29c
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x4b
popq %rsi
callq 0xe3eb4
movb %al, 0x4(%r14)
movzbl %al, %edx
leaq 0xccebb(%rip), %rsi     # 0x12d4f8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movb 0x4(%r14), %al
cmpb $0x4, %al
leaq 0xb684d(%rip), %rcx     # 0x116eac
leaq 0xb684f(%rip), %rdx     # 0x116eb5
cmoveq %rcx, %rdx
cmpb $0x2, %al
cmovbq %rcx, %rdx
leaq 0xb6848(%rip), %rsi     # 0x116ebf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl 0x4(%r14), %ecx
pushq $0x4
popq %rax
leal -0x4(%rcx), %edx
cmpl $0x2, %edx
jb 0x606ae
leaq 0xa8958(%rip), %rdx     # 0x108ff0
cmpl $0x1, %ecx
je 0x606ba
cmpl $0x3, %ecx
je 0x606ba
pushq $0x1
popq %rax
leaq 0xb6823(%rip), %rdx     # 0x116ecf
jmp 0x606ba
movb $0x1, 0x6(%r14)
leaq 0xa887c(%rip), %rdx     # 0x108f36
movq %rax, 0x18(%r14)
leaq 0xb6812(%rip), %rsi     # 0x116ed7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
pushq $0x57
popq %rsi
callq 0xe3fa3
movq %rax, 0x10(%r14)
leaq 0xb67f5(%rip), %rsi     # 0x116ee6
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpb $0x5, 0x4(%r14)
ja 0x60724
movq 0x10(%r14), %rax
cmpq $0x289, %rax            # imm = 0x289
jl 0x60724
imulq 0x8(%r14), %rax
addq $0x61, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x60754
movb $0x1, 0x5(%r14)
leaq 0xb67c5(%rip), %rsi     # 0x116ef5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
movq %r14, 0x10(%rsp)
cmpq 0x8(%r14), %rax
jge 0x60afd
movq 0x10(%r14), %r12
movq %rax, 0x30(%rsp)
imulq %rax, %r12
addq $0x61, %r12
movq %r12, (%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0xdebae
movl $0x458, %esi            # imm = 0x458
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
movq %rbx, %rdi
leaq 0xc3157(%rip), %rsi     # 0x1238f8
movq %r12, 0x28(%rsp)
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xe410e
leaq 0x38(%r15), %r13
movq %rax, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0xe002e
movq %rbx, %rdi
movq %r13, 0x20(%rsp)
movq %r13, %rsi
leaq 0xc19a9(%rip), %rdx     # 0x122198
callq 0x611c2
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe410e
leaq 0x48(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0xe002e
movq %rbx, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
leaq 0xc05c1(%rip), %rdx     # 0x120de4
callq 0x611c2
addq $0x1c, (%rsp)
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x28(%r15)
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movl (%r14), %r9d
pushq $0x50
popq %rdx
movq %rax, %rcx
movl %ebp, %r8d
callq 0xe4c9a
movq 0x28(%r15), %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0xb66b0(%rip), %rsi     # 0x116f15
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0x51, (%rsp)
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r12
movq %rax, 0x30(%r15)
movq 0x28(%r15), %rbp
movq %rax, %rdi
callq 0xeac85
movq 0x10(%rbp), %r14
testq %r14, %r14
movl $0x0, %eax
cmovleq %rax, %r14
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x608d0
movq 0x8(%rbp), %rax
movl (%rax,%r13,4), %esi
cmpl $0x5c, %esi
je 0x608b9
cmpl $0x2f, %esi
jne 0x608c3
movq %r12, %rdi
callq 0xeac85
jmp 0x608cb
movq %r12, %rdi
callq 0xeada5
incq %r13
jmp 0x608a2
addq $0x210, (%rsp)          # imm = 0x210
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xe3fc4
incq %rax
movq %rax, (%r15)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3fc4
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%r15)
movq (%r15), %rsi
movq %rbx, %rdi
callq 0xdf038
movq (%r15), %rsi
movq 0x10(%r15), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x60977
movq (%r15), %rsi
movq 0x10(%r15), %rcx
leaq 0x7(%rsi), %rax
cqto
pushq $0x8
popq %rdi
idivq %rdi
movq %rax, 0x18(%r15)
movq 0x10(%rsp), %r14
movq 0x18(%r14), %rdx
imulq %rax, %rdx
movq %rdx, 0x20(%r15)
shlq $0x3, %rax
movq %rax, 0x8(%r15)
imulq %rcx, %rdx
addq (%rsp), %rdx
movq 0x28(%rsp), %rdi
addq 0x10(%r14), %rdi
cmpq %rdi, %rdx
jle 0x60989
movb $0x1, 0x5(%r14)
movb $0x1, %r13b
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x60a7e
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x10(%rsp), %r14
jmp 0x60a7e
xorl %edx, %edx
cmpb $0x0, 0x6(%r14)
setne %dl
leal 0x1(,%rdx,2), %r8d
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe746f
movq %rax, %rbp
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
cmpb $0x0, 0x6(%r14)
je 0x609f4
movaps 0xb665e(%rip), %xmm0    # 0x117020
movups %xmm0, 0x88(%r15)
movaps 0xb663f(%rip), %xmm0    # 0x117010
movups %xmm0, 0x78(%r15)
movaps 0xb6623(%rip), %xmm0    # 0x117000
movups %xmm0, 0x68(%r15)
movaps 0xb6607(%rip), %xmm0    # 0x116ff0
movups %xmm0, 0x58(%r15)
movq 0x18(%r14), %rdx
jmp 0x60a13
movq 0x18(%r14), %rdx
cmpq $0x1, %rdx
jne 0x60acd
movabsq $-0xc0c0c01000000, %rax # imm = 0xFFF3F3F3FF000000
movq %rax, 0x58(%r15)
pushq $0x1
popq %rdx
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq 0x18(%r15), %r8
movq %r8, %rcx
imulq %rdx, %rcx
movq %r15, %r9
addq $0x58, %r9
pushq $0x0
pushq %rbp
callq 0xe7de1
popq %rax
popq %rcx
cmpb $0x0, 0xd0(%rbx)
je 0x60a51
movq 0x30(%r15), %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xdfd55
movq 0x18(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x18(%r12)
movq 0x20(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x28(%r12)
movq %rbp, %rdi
movq %r12, %rsi
pushq $0x2
popq %rdx
callq 0xe70ab
xorl %r13d, %r13d
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
movq %rbp, %rdi
callq 0xe7084
movq 0x28(%r15), %rdi
callq 0xeaf3c
movq 0x30(%r15), %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq 0x30(%rsp), %rax
incq %rax
testb %r13b, %r13b
je 0x6075b
jmp 0x60afd
leaq 0x58(%r15), %rax
xorl %ecx, %ecx
cmpq $0x410, %rcx            # imm = 0x410
je 0x60a13
movl %ecx, %esi
shrl $0x2, %esi
imull $0x10101, %esi, %esi    # imm = 0x10101
addl $0xff000000, %esi       # imm = 0xFF000000
movl %esi, (%rax)
addq $0x4, %rax
addq $0x41, %rcx
jmp 0x60ad3
cmpb $0x0, 0x5(%r14)
jne 0x60729
jmp 0x6073a