// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x35, %esi
callq 0x165280
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x9561c(%rip), %rsi     # 0x1cbcba
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x37, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x1366da
movq -0x8(%rbp), %rdi
leaq 0x95602(%rip), %rsi     # 0x1cbcd0
movb $0x0, %al
callq 0x15bfb0
jmp 0x1368b1
movq $0x37, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x1368af
movq -0x28(%rbp), %rax
subq -0x18(%rbp), %rax
cmpq $0x8, %rax
jge 0x136715
movq -0x8(%rbp), %rdi
leaq 0x95629(%rip), %rsi     # 0x1cbd32
movb $0x0, %al
callq 0x15bc10
jmp 0x1368af
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x165060
movb %al, -0x41(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x5, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x9560d(%rip), %rsi     # 0x1cbd80
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x7, 0x10(%rax)
jne 0x13680c
movq $0x8, -0x40(%rbp)
cmpq $0x8, -0x38(%rbp)
je 0x13680a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0xe, %rsi
callq 0x165280
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x6, %rsi
addq -0x38(%rbp), %rsi
callq 0x165280
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x136808
movq -0x58(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x136808
cmpl $0x0, -0x5c(%rbp)
jne 0x136800
movq -0x8(%rbp), %rdi
leaq 0x955ab(%rip), %rsi     # 0x1cbd9d
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0x5c(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x13680a
jmp 0x136814
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x6, %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165280
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x136855
movq -0x8(%rbp), %rdi
leaq 0x955b3(%rip), %rsi     # 0x1cbe01
movb $0x0, %al
callq 0x15bc10
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x41(%rbp), %edx
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %eax, %r8d
leaq 0x95478(%rip), %rsi     # 0x1cbcf5
movb $0x0, %al
callq 0x15b0d0
movzbl -0x41(%rbp), %eax
cmpl $0x11, %eax
jne 0x13689e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x136f50
movq -0x30(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x1366e2
jmp 0x1368b1
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)