// File: /workspace/github_c_makefile_star2_O3/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 0xeebc8
movq %rax, %r15
leaq 0x6f021(%rip), %rsi     # 0x14004e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0x37(%r15), %rcx
movq 0x18(%rbx), %rax
movq %rcx, 0x20(%rsp)
cmpq 0x18(%rax), %rcx
jle 0xd106c
leaq 0x6f010(%rip), %rsi     # 0x140064
movq %rbx, %rdi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9295
testq %r15, %r15
jle 0xd1207
addq $0x2f, %r15
movl $0x37, %r13d
movl $0x0, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq %r15, 0x18(%rsp)
cmpq %r15, %r13
jg 0xd1216
movq 0x18(%rbx), %rdi
leaq 0x2(%r13), %rsi
callq 0xeebc8
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xeead3
movl %eax, %ebp
movq 0x18(%rbx), %rdi
leaq 0x5(%r13), %rsi
callq 0xeead3
movzbl %al, %r15d
movq %rbx, %rdi
leaq 0x6f040(%rip), %rsi     # 0x140114
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
cmpl $0x7, 0x10(%r14)
jne 0xd1150
cmpl $0x8, %r15d
jne 0xd10f3
movl $0x8, %r15d
jmp 0xd1150
movq 0x18(%rbx), %rdi
leaq 0xe(%r13), %rsi
callq 0xeebc8
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq (%r15,%r13), %rsi
addq $0x6, %rsi
callq 0xeebc8
cmpq %r12, %r14
je 0xd1145
cmpq %r12, %rax
jne 0xd1145
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
jne 0xd113b
movq %rbx, %rdi
leaq 0x6effd(%rip), %rsi     # 0x140131
xorl %eax, %eax
callq 0xe8faa
movl $0x1, 0xc(%rsp)
jmp 0xd1150
movl $0x8, %r15d
movq 0x10(%rsp), %r14
addq %r13, %r15
addq $0x6, %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebc8
cmpq %r12, %rax
je 0xd1179
movq %rbx, %rdi
leaq 0x6f023(%rip), %rsi     # 0x140195
xorl %eax, %eax
callq 0xe8faa
leaq 0x2(%r15), %r13
movzbl %bpl, %ebp
movq %rbx, %rdi
leaq 0x6eefe(%rip), %rsi     # 0x140089
movl %ebp, %edx
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xe8617
cmpb $0x11, %bpl
jne 0xd11f4
movq %rbx, %rdi
leaq 0x6eefd(%rip), %rsi     # 0x1400a7
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeebc8
movq %rbx, %rdi
leaq 0x6eeeb(%rip), %rsi     # 0x1400b5
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq $0x80, (%r14)
movq $0x3f, 0x8(%r14)
addq $0x4, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xd13b4
addq %r12, %r13
cmpq 0x20(%rsp), %r13
movq 0x18(%rsp), %r15
jl 0xd1091
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6eea9(%rip), %rsi     # 0x1400c6
movq %rbx, %rdi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8faa