// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/tivariable.c
// Function: do_ti85
static void do_ti85(deark *c, lctx *d)
{
	i64 pos;
	i64 data_section_size;
	i64 data_section_end;
	i64 var_data_size;
	i64 name_len_reported;
	i64 name_field_len;
	u8 type_id;
	i64 x1, x2;
	int warned = 0;

	// 0-7: signature
	// 8-10: 0x1a 0x0a 0x00
	// 11-52: comment

	data_section_size = de_getu16le(53);
	de_dbg(c, "data section size: %d", (int)data_section_size);
	data_section_end = 55+data_section_size;
	if(data_section_end > c->infile->len) {
		de_err(c, "Data section goes beyond end of file");
		goto done;
	}

	// Read the variables
	pos = 55;
	while(pos < data_section_end) {
		if(data_section_end - pos < 8) {
			de_warn(c, "Invalid variable entry size. This file may not have been processed correctly.");
			break;
		}

		var_data_size = de_getu16le(pos+2);
		type_id = de_getbyte(pos+4);
		name_len_reported = (i64)de_getbyte(pos+5);
		de_dbg(c, "reported var name length: %d", (int)name_len_reported);
		if(d->fmt==DE_FMT_TI86) {
			name_field_len = 8; // Initial default

			// The TI86 name field length *should* always be 8, but some files do not
			// do it that way.
			if(name_len_reported!=8) {
				// There are two "variable data length" fields that should contain the
				// same value. Although this is bad design, we can exploit it to help
				// guess the correct length of the variable name field.

				x1 = de_getu16le(pos+14);
				x2 = de_getu16le(pos+6+name_len_reported);
				if(x1!=var_data_size && x2==var_data_size) {
					if(!warned) {
						de_warn(c, "This TI86 file appears to use TI85 variable name format "
							"instead of TI86 format. Trying to continue.");
						warned = 1;
					}
					name_field_len = name_len_reported;
				}
			}
		}
		else {
			// TI85 format
			name_field_len = name_len_reported;
		}

		pos += 6+name_field_len;

		x1 = de_getu16le(pos);
		if(x1!=var_data_size) {
			de_warn(c, "Inconsistent variable-data-length fields. "
				"This file may not be processed correctly.");
		}
		pos += 2;

		de_dbg(c, "var type=0x%02x pos=%d len=%d", (unsigned int)type_id,
			(int)pos, (int)var_data_size);

		if(type_id==0x11) { // guess
			do_ti85_picture_var(c, d, pos);
		}

		pos += var_data_size;
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
pushq $0x35
popq %rsi
callq 0xe3fa3
movq %rax, %r15
leaq 0x6a8a9(%rip), %rsi     # 0x132d1e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0x37(%r15), %rcx
movq 0x18(%rbx), %rax
movq %rcx, 0x20(%rsp)
cmpq 0x18(%rax), %rcx
jle 0xc84b4
leaq 0x6a898(%rip), %rsi     # 0x132d34
movq %rbx, %rdi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdf2a5
addq $0x2f, %r15
pushq $0x37
popq %r13
movl $0x0, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq %r15, 0x18(%rsp)
cmpq 0x20(%rsp), %r13
jge 0xc8644
cmpq %r15, %r13
jg 0xc8653
movq 0x18(%rbx), %rdi
leaq 0x2(%r13), %rsi
callq 0xe3fa3
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xe3eb4
movl %eax, %ebp
movq 0x18(%rbx), %rdi
leaq 0x5(%r13), %rsi
callq 0xe3eb4
movzbl %al, %r15d
movq %rbx, %rdi
leaq 0x6a8c8(%rip), %rsi     # 0x132de4
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
cmpl $0x7, 0x10(%r14)
jne 0xc8593
cmpl $0x8, %r15d
jne 0xc8539
pushq $0x8
popq %r15
jmp 0xc8593
movq 0x18(%rbx), %rdi
leaq 0xe(%r13), %rsi
callq 0xe3fa3
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq (%r15,%r13), %rsi
addq $0x6, %rsi
callq 0xe3fa3
cmpq %r12, %r14
je 0xc858a
cmpq %r12, %rax
jne 0xc858a
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
jne 0xc8581
movq %rbx, %rdi
leaq 0x6a887(%rip), %rsi     # 0x132e01
xorl %eax, %eax
callq 0xdf05a
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0xc8593
pushq $0x8
popq %r15
movq 0x10(%rsp), %r14
addq %r13, %r15
addq $0x6, %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fa3
cmpq %r12, %rax
je 0xc85bc
movq %rbx, %rdi
leaq 0x6a8b0(%rip), %rsi     # 0x132e65
xorl %eax, %eax
callq 0xdf05a
leaq 0x2(%r15), %r13
movzbl %bpl, %ebp
movq %rbx, %rdi
leaq 0x6a78b(%rip), %rsi     # 0x132d59
movl %ebp, %edx
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xde896
cmpb $0x11, %bpl
jne 0xc8637
movq %rbx, %rdi
leaq 0x6a78a(%rip), %rsi     # 0x132d77
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3fa3
movq %rbx, %rdi
leaq 0x6a778(%rip), %rsi     # 0x132d85
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq $0x80, (%r14)
movq $0x3f, 0x8(%r14)
addq $0x4, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xc87dd
addq %r12, %r13
movq 0x18(%rsp), %r15
jmp 0xc84ce
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a73c(%rip), %rsi     # 0x132d96
movq %rbx, %rdi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdf05a