// File: /workspace/github_c_makefile_star2_O3/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl $0x30, %esi
callq 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf3eb8
movq %rax, 0x18(%r14)
movl $0x70, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, 0x48(%r12)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeebc8
cmpl $0x5a4d, %eax           # imm = 0x5A4D
je 0x319cc
cmpl $0x4d5a, %eax           # imm = 0x4D5A
je 0x319cc
movq %r14, %rdx
addq $0x20, %rdx
movzbl %al, %esi
movq %rbx, %rdi
callq 0x321fd
cmpb $0x0, 0x20(%r14)
je 0x319cc
movl 0x24(%r14), %edx
leaq 0xe5d74(%rip), %rsi     # 0x1175b1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rsp, %rbp
movq $0x0, (%rbp)
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl $0x70, %edx
movl $0x400, %ecx            # imm = 0x400
movq %rax, %rsi
callq 0xee8d4
leaq 0xe5794(%rip), %rdx     # 0x117013
movl $0x400, %esi            # imm = 0x400
movl $0x6, %ecx
movq %r13, %rdi
movl $0x3f, %r8d
movq %rbp, %r9
callq 0xeba66
testl %eax, %eax
je 0x3195f
movq (%rsp), %rbp
addq $0x76, %rbp
leaq 0xe5d42(%rip), %rsi     # 0x1175f3
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebc8
leaq -0x100(%rax), %rdx
movq %rdx, 0x8(%r14)
leaq 0xf4759(%rip), %rsi     # 0x126035
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpl $0x12c, 0x24(%r14)      # imm = 0x12C
movq $-0x7, %rax
movq $-0x11, %rbp
cmovbq %rax, %rbp
addq 0x8(%r14), %rbp
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeead3
movb %al, (%r14)
movzbl %al, %edx
leaq 0x11a434(%rip), %rsi    # 0x14bd51
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpb $0x3, (%r14)
jne 0x3195f
movq 0x18(%rbx), %rdi
addq $0x2, %rbp
movq %rbp, %rsi
callq 0xeebc8
movq %rax, 0x10(%r14)
leaq 0x10129b(%rip), %rsi    # 0x132be3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq $0x50, 0x50(%r15)
jmp 0x31965
movw $0x101, (%r15)          # imm = 0x101
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
cmpb $0x0, (%r15)
jne 0x319d1
movq 0x18(%r14), %rax
movb $0x1, 0x2(%rax)
movq 0x10(%r14), %rdi
movq 0x50(%r15), %rsi
addq %rsi, %rsi
callq 0xe9cb4
movq 0x50(%r15), %rcx
leaq (%rcx,%rcx), %rsi
cqto
idivq %rsi
movq %rax, 0x58(%r15)
leaq 0xe5c1c(%rip), %rsi     # 0x1175c2
movq %rbx, %rdi
movq %rcx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
leaq (%rcx,%rax), %rdx
movq 0x18(%rbx), %rsi
cmpq 0x18(%rsi), %rdx
jle 0x31a0f
movb $0x1, 0x1(%r15)
cmpb $0x0, 0x1(%r15)
je 0x319e9
leaq 0xe5bf9(%rip), %rsi     # 0x1175d8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31e6b
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xf3fb8
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x30, %r12
cmpq $0x50, 0x50(%r15)
jg 0x31a21
cmpq $0x19, 0x58(%r15)
jle 0x31a2b
movq 0x18(%r14), %rdx
movb $0x1, 0x3(%rdx)
jmp 0x31a2f
movq 0x18(%r14), %rdx
movb $0x1, 0x4c(%r15)
movq %rsi, 0x30(%r15)
movq %rax, 0x38(%r15)
movq %rcx, 0x40(%r15)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf9c77
jmp 0x319d1