// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/bintext.c
// Function: de_run_grabber
static void de_run_grabber(deark *c, de_module_params *mparams)
{
	struct grabber_ctx *gctx = NULL;
	lctx *d = NULL;
	UI sig;

	gctx = de_malloc(c, sizeof(struct grabber_ctx));
	gctx->charctx = de_create_charctx(c, 0);
	d = de_malloc(c, sizeof(lctx));
	d->csctx.input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

	sig = (UI)de_getu16le(0);
	if(sig==0x5a4d || sig==0x4d5a) {
		d->need_errmsg = 1;
		goto done;
	}

	grabber_id_com(c, (sig&0xff), &gctx->gi);
	if(!gctx->gi.is_grabber) {
		d->need_errmsg = 1;
		goto done;
	}

	de_dbg(c, "format class: %u", gctx->gi.fmt_class);
	decode_grabber_com(c, d, gctx);
	if(d->errflag) goto done;

	gctx->charctx->screen_image_flag = 1;
	d->csctx.height_in_chars = de_pad_to_n(gctx->data_len, d->csctx.width_in_chars*2) /
		(d->csctx.width_in_chars*2);
	de_dbg(c, "screen size: %"I64_FMT DE_CHAR_TIMES "%"I64_FMT, d->csctx.width_in_chars,
		d->csctx.height_in_chars);
	if(gctx->data_pos+gctx->data_len > c->infile->len) {
		d->need_errmsg = 1;
		goto done;
	}

	if(d->csctx.width_in_chars>80 || d->csctx.height_in_chars>25) {
		gctx->charctx->no_density = 1;
	}

	d->csctx.use_default_pal = 1;
	d->csctx.inf = c->infile;
	d->csctx.inf_pos = gctx->data_pos;
	d->csctx.inf_len = gctx->data_len;
	fmtutil_char_simple_run(c, &d->csctx, gctx->charctx);

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Unsupported GRABBER format");
		}
		free_lctx(c, d);
	}
	if(gctx) {
		de_free_charctx(c, gctx->charctx);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movl $0x30, %esi
callq 0xe969b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf43cc
movq %rax, 0x18(%r14)
movl $0x70, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb7d1
movl %eax, 0x48(%r15)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xef1ca
cmpl $0x5a4d, %eax           # imm = 0x5A4D
je 0x31b33
cmpl $0x4d5a, %eax           # imm = 0x4D5A
je 0x31b33
leaq 0x20(%r14), %rdx
movzbl %al, %esi
movq %rbx, %rdi
callq 0x3241e
cmpb $0x0, 0x20(%r14)
je 0x31b33
movl 0x24(%r14), %edx
leaq 0xe5b70(%rip), %rsi     # 0x117511
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
leaq 0x8(%rsp), %r13
movq $0x0, (%r13)
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r12
movq 0x18(%rbx), %rdi
movl $0x70, %edx
movl $0x400, %ecx            # imm = 0x400
movq %rax, %rsi
callq 0xeeee8
leaq 0xe5592(%rip), %rdx     # 0x116f77
movl $0x400, %esi            # imm = 0x400
movl $0x6, %ecx
movq %r12, %rdi
movl $0x3f, %r8d
movq %r13, %r9
callq 0xec044
testl %eax, %eax
je 0x31ac6
movq 0x8(%rsp), %r13
addq $0x76, %r13
leaq 0xe5b3b(%rip), %rsi     # 0x117553
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ca
leaq -0x100(%rax), %rdx
movq %rdx, 0x8(%r14)
leaq 0xf43c2(%rip), %rsi     # 0x125e05
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpl $0x12c, 0x24(%r14)      # imm = 0x12C
movq $-0x7, %rax
movq $-0x11, %r13
cmovbq %rax, %r13
addq 0x8(%r14), %r13
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef0d5
movb %al, (%r14)
movzbl %al, %edx
leaq 0x119cb1(%rip), %rsi    # 0x14b735
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpb $0x3, (%r14)
jne 0x31ac6
movq 0x18(%rbx), %rdi
addq $0x2, %r13
movq %r13, %rsi
callq 0xef1ca
movq %rax, 0x10(%r14)
leaq 0x100d9c(%rip), %rsi    # 0x13284b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq $0x50, 0x50(%r15)
jmp 0x31acc
movw $0x101, (%r15)          # imm = 0x101
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe9752
cmpb $0x0, (%r15)
jne 0x31b38
movq 0x18(%r14), %rax
movb $0x1, 0x2(%rax)
movq 0x10(%r14), %rdi
movq 0x50(%r15), %rsi
addq %rsi, %rsi
callq 0xea80b
movq 0x50(%r15), %rcx
leaq (%rcx,%rcx), %rsi
cqto
idivq %rsi
movq %rax, 0x58(%r15)
leaq 0xe5a15(%rip), %rsi     # 0x117522
movq %rbx, %rdi
movq %rcx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
leaq (%rcx,%rax), %rsi
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rsi
jle 0x31b7f
movb $0x1, 0x1(%r15)
testq %r15, %r15
je 0x31b60
cmpb $0x0, 0x1(%r15)
je 0x31b55
leaq 0xe59ed(%rip), %rsi     # 0x117538
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31fe3
testq %r14, %r14
je 0x31b71
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xf44c8
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x30(%r15), %rsi
cmpq $0x50, 0x50(%r15)
jg 0x31b91
cmpq $0x1a, 0x58(%r15)
jl 0x31b99
movq 0x18(%r14), %rdi
movb $0x1, 0x3(%rdi)
movb $0x1, 0x4c(%r15)
movq %rdx, 0x30(%r15)
movq %rax, 0x38(%r15)
movq %rcx, 0x40(%r15)
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0xfa176
jmp 0x31b38