// File: /workspace/github_c_makefile_star2_O1/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
movl $0x35, %esi
callq 0xef1ca
movq %rax, %r15
leaq 0x6e002(%rip), %rsi     # 0x13fc46
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x37(%r15), %rcx
movq 0x18(%rbx), %rax
movq %rcx, 0x20(%rsp)
cmpq 0x18(%rax), %rcx
jle 0xd1c83
leaq 0x6dff1(%rip), %rsi     # 0x13fc5c
movq %rbx, %rdi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9df7
testq %r15, %r15
jle 0xd1e1e
addq $0x2f, %r15
movl $0x37, %r13d
movl $0x0, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq %r15, 0x18(%rsp)
cmpq %r15, %r13
jg 0xd1e2d
movq 0x18(%rbx), %rdi
leaq 0x2(%r13), %rsi
callq 0xef1ca
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xef0d5
movl %eax, %ebp
movq 0x18(%rbx), %rdi
leaq 0x5(%r13), %rsi
callq 0xef0d5
movzbl %al, %r15d
movq %rbx, %rdi
leaq 0x6e021(%rip), %rsi     # 0x13fd0c
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
cmpl $0x7, 0x10(%r14)
jne 0xd1d67
cmpl $0x8, %r15d
jne 0xd1d0a
movl $0x8, %r15d
jmp 0xd1d67
movq 0x18(%rbx), %rdi
leaq 0xe(%r13), %rsi
callq 0xef1ca
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq (%r15,%r13), %rsi
addq $0x6, %rsi
callq 0xef1ca
cmpq %r12, %r14
je 0xd1d5c
cmpq %r12, %rax
jne 0xd1d5c
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
jne 0xd1d52
movq %rbx, %rdi
leaq 0x6dfde(%rip), %rsi     # 0x13fd29
xorl %eax, %eax
callq 0xe9b27
movl $0x1, 0xc(%rsp)
jmp 0xd1d67
movl $0x8, %r15d
movq 0x10(%rsp), %r14
addq %r13, %r15
addq $0x6, %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ca
cmpq %r12, %rax
je 0xd1d90
movq %rbx, %rdi
leaq 0x6e004(%rip), %rsi     # 0x13fd8d
xorl %eax, %eax
callq 0xe9b27
leaq 0x2(%r15), %r13
movzbl %bpl, %ebp
movq %rbx, %rdi
leaq 0x6dedf(%rip), %rsi     # 0x13fc81
movl %ebp, %edx
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xe9226
cmpb $0x11, %bpl
jne 0xd1e0b
movq %rbx, %rdi
leaq 0x6dede(%rip), %rsi     # 0x13fc9f
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ca
movq %rbx, %rdi
leaq 0x6decc(%rip), %rsi     # 0x13fcad
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq $0x80, (%r14)
movq $0x3f, 0x8(%r14)
addq $0x4, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xd1fcb
addq %r12, %r13
cmpq 0x20(%rsp), %r13
movq 0x18(%rsp), %r15
jl 0xd1ca8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6de8a(%rip), %rsi     # 0x13fcbe
movq %rbx, %rdi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9b27