// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/autocad.c
// Function: de_run_autocad_slb
static void de_run_autocad_slb(deark *c, de_module_params *mparams)
{
	i64 pos;
	i64 nslides = 0;
	i64 k;
	struct slideinfo {
		i64 pos;
		i64 len;
	};
	i64 si_numalloc;
	struct slideinfo *si = NULL;
	de_ucstring *slidename = NULL;

	de_dbg(c, "[pass 1: recording addresses]");
	si_numalloc = 64;
	si = de_mallocarray(c, si_numalloc, sizeof(struct slideinfo));
	pos = 32;
	while(1) {
		i64 k;

		if(pos > (c->infile->len-36)) {
			de_err(c, "Unterminated directory");
			goto done;
		}
		if(de_getbyte(pos)==0) {
			break;
		}

		k = nslides; // Index of the new slide
		nslides++;

		if(nslides > si_numalloc) {
			i64 old_numalloc, new_numalloc;

			if(!de_good_image_count(c, nslides)) {
				de_err(c, "Too many slides");
				goto done;
			}
			old_numalloc = si_numalloc;
			new_numalloc = old_numalloc*2;
			si = de_reallocarray(c, si, old_numalloc, sizeof(struct slideinfo),
				new_numalloc);
			si_numalloc = new_numalloc;
		}

		si[k].pos = de_getu32le(pos+32);

		if(si[k].pos > c->infile->len) {
				de_err(c, "Invalid directory");
				goto done;
		}
		if(k>0) {
			if(si[k].pos < si[k-1].pos) {
				de_err(c, "Invalid directory");
				goto done;
			}
		}

		if(k>0) {
			// Set the previous slide's length.
			si[k-1].len = si[k].pos - si[k-1].pos;
		}
		// Start by assuming this slide ends at the end of the file. If this
		// turns out not to be the last slide, this value will be changed later.
		si[k].len = c->infile->len - si[k].pos;

		pos += 36;
	}

	de_dbg(c, "[pass 2: extracting slides]");
	pos = 32;
	slidename = ucstring_create(c);
	for(k=0; k<nslides; k++) {
		de_finfo *fi = NULL;

		de_dbg(c, "slide dir entry at %d", (int)pos);
		de_dbg_indent(c, 1);

		ucstring_empty(slidename);
		dbuf_read_to_ucstring(c->infile, pos, 31, slidename,
			DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);

		de_dbg(c, "address: %u", (unsigned int)si[k].pos);
		de_dbg(c, "calculated len: %u", (unsigned int)si[k].len);

		fi = de_finfo_create(c);
		de_finfo_set_name_from_ucstring(c, fi, slidename, 0);
		dbuf_create_file_from_slice(c->infile, si[k].pos, si[k].len, "sld", fi, 0);
		de_finfo_destroy(c, fi);

		de_dbg_indent(c, -1);
		pos += 36;
	}

done:
	ucstring_destroy(slidename);
	de_free(c, si);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0xe34be(%rip), %rsi     # 0x106b97
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xde896
pushq $0x40
popq %rsi
pushq $0x10
popq %rdx
movq %rbx, %rdi
movq %rsi, (%rsp)
callq 0xdf66f
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rcx
pushq $0x20
popq %r13
xorl %ebp, %ebp
addq $-0x24, %rcx
cmpq %rcx, %r13
jg 0x237b6
movq %r13, %rsi
callq 0xe3eb4
testb %al, %al
je 0x237c2
leaq 0x1(%rbp), %r14
cmpq (%rsp), %rbp
jl 0x23760
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe6ccb
testl %eax, %eax
je 0x238c2
movq %r12, %rsi
movq (%rsp), %rdx
leaq (%rdx,%rdx), %r12
movq %rbx, %rdi
pushq $0x10
popq %rcx
movq %r12, %r8
callq 0xdf6b6
movq %r12, (%rsp)
movq %rax, %r12
movq 0x18(%rbx), %rdi
addq $0x20, %r13
movq %r13, %rsi
callq 0xe409f
movq %rax, (%r12,%r15)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rcx
movq %rcx, %rdx
subq %rax, %rdx
jl 0x237aa
testq %rbp, %rbp
je 0x23795
subq -0x10(%r12,%r15), %rax
jl 0x237aa
movq %rax, -0x8(%r12,%r15)
movq %rdx, 0x8(%r12,%r15)
addq $0x10, %r15
addq $0x4, %r13
movq %r14, %rbp
jmp 0x23706
leaq 0xe342b(%rip), %rsi     # 0x106bdc
jmp 0x238c9
leaq 0xe33f8(%rip), %rsi     # 0x106bb5
jmp 0x238c9
leaq 0xe3425(%rip), %rsi     # 0x106bee
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movq %r12, (%rsp)
leaq 0x8(%r12), %r14
pushq $0x20
popq %r12
subq $0x1, %rbp
jb 0x238bc
movq %rbx, %rdi
leaq 0xe340b(%rip), %rsi     # 0x106c0a
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %r13
movl %r13d, %esi
callq 0xdeba7
movq %r15, %rdi
callq 0xeac85
movq 0x18(%rbx), %rdi
movq %r12, %rsi
pushq $0x1f
popq %rdx
movq %r15, %rcx
movl %r13d, %r8d
movl %r13d, %r9d
callq 0xe4c9a
movl -0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xe33da(%rip), %rsi     # 0x106c20
xorl %eax, %eax
callq 0xde896
movl (%r14), %edx
movq %rbx, %rdi
leaq 0xe33d2(%rip), %rsi     # 0x106c2c
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xdfd55
movq 0x18(%rbx), %rdi
movq -0x8(%r14), %rsi
movq (%r14), %rdx
leaq 0xe33b1(%rip), %rcx     # 0x106c3f
movq %r13, %r8
xorl %r9d, %r9d
callq 0xe4e0b
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdfd24
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
addq $0x24, %r12
addq $0x10, %r14
jmp 0x237eb
movq (%rsp), %r12
jmp 0x238d6
leaq 0xe3303(%rip), %rsi     # 0x106bcc
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %r15, %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq %r12, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32