// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bintext.c
// Function: decode_grabber_com
static void decode_grabber_com(deark *c, lctx *d, struct grabber_ctx *gctx)
{
	i64 foundpos = 0;
	i64 pos_of_data_ptr;
	i64 pos_of_mode;
	int ret;
	u8 *mem = NULL;

#define GRABBER_SEARCH1_START 112
#define GRABBER_BUF_LEN1 1024
	mem = de_malloc(c, GRABBER_BUF_LEN1);
	de_read(mem, GRABBER_SEARCH1_START, GRABBER_BUF_LEN1);
	// Search for the byte pattern preceding the data pointer.
	// Known positions range from 121 (v2.10) to 869 (v3.34).
	ret = de_memsearch_match(mem, GRABBER_BUF_LEN1,
		(const u8*)"\xb8\x00?\x8e\xc0\xbe", 6,
		'?', &foundpos);
	if(!ret) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	pos_of_data_ptr = foundpos+GRABBER_SEARCH1_START+6;
	de_dbg(c, "pos of data ptr: %"I64_FMT, pos_of_data_ptr);

	gctx->data_pos = de_getu16le(pos_of_data_ptr);
	gctx->data_pos -= 256;
	de_dbg(c, "data pos: %"I64_FMT, gctx->data_pos);

	if(gctx->gi.fmt_class<300) {
		pos_of_mode = gctx->data_pos - 7;
	}
	else {
		pos_of_mode = gctx->data_pos - 17;
	}

	gctx->screen_mode = de_getbyte(pos_of_mode);
	de_dbg(c, "mode: 0x%02x", (UI)gctx->screen_mode);
	if(gctx->screen_mode != 3) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	gctx->data_len = de_getu16le(pos_of_mode+2);
	de_dbg(c, "data len: %"I64_FMT, gctx->data_len);
	d->csctx.width_in_chars = 80;
done:
	de_free(c, mem);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movl $0x70, %edx
movl $0x400, %ecx            # imm = 0x400
callq 0x164d10
movq -0x40(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
leaq 0x1611de(%rip), %rdx    # 0x1a2dff
movl $0x6, %ecx
movl $0x3f, %r8d
leaq -0x20(%rbp), %r9
callq 0x15f740
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x41c52
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x41d85
movq -0x20(%rbp), %rax
addq $0x70, %rax
addq $0x6, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x161728(%rip), %rsi    # 0x1a3399
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
subq $0x100, %rcx            # imm = 0x100
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x16fbb8(%rip), %rsi    # 0x1b1872
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpl $0x12c, 0x24(%rax)      # imm = 0x12C
jae 0x41ce0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x7, %rax
movq %rax, -0x30(%rbp)
jmp 0x41cf0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x11, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl (%rax), %edx
leaq 0x1959da(%rip), %rsi    # 0x1d76f5
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3, %eax
je 0x41d3f
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x41d85
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x17c89d(%rip), %rsi    # 0x1be60f
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq $0x50, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)