// File: /workspace/github_c_makefile_star2_O1/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 0xefcb2(%rip), %rsi     # 0x113b47
xorl %eax, %eax
callq 0xe9226
movl $0x40, %r15d
movl $0x40, %esi
movl $0x10, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, %r14
movl $0x20, %r12d
leaq 0xefca1(%rip), %rbp     # 0x113b65
xorl %r13d, %r13d
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rax
addq $-0x24, %rax
cmpq %rax, %r12
jle 0x23eef
movq %rbx, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0xe9df7
movl $0x4, %eax
jmp 0x23fc8
movq %r12, %rsi
callq 0xef0d5
testb %al, %al
je 0x23f36
leaq 0x1(%r13), %rsi
cmpq %r15, %r13
movq %rsi, (%rsp)
jl 0x23f61
movq %rbx, %rdi
callq 0xf2509
movl %eax, %ebp
testl %eax, %eax
je 0x23f40
movq %r15, %rdx
addq %r15, %r15
movl $0x10, %ecx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %r8
callq 0xea2ba
movq %rax, %r14
xorl %eax, %eax
jmp 0x23f56
movl $0x3, %eax
jmp 0x23fc8
movq %rbx, %rdi
leaq 0xefc32(%rip), %rsi     # 0x113b7c
xorl %eax, %eax
callq 0xe9df7
movl $0x4, %eax
testl %ebp, %ebp
leaq 0xefc06(%rip), %rbp     # 0x113b65
je 0x23fc4
movq 0x18(%rbx), %rdi
leaq 0x20(%r12), %rsi
callq 0xef37e
movq %r13, %rcx
shlq $0x4, %rcx
movq %rax, (%r14,%rcx)
movq 0x18(%rbx), %rdx
movq 0x18(%rdx), %rdx
subq %rax, %rdx
jl 0x23f93
testq %r13, %r13
jle 0x23fb7
subq -0x10(%rcx,%r14), %rax
jge 0x23fab
movq %rbx, %rdi
leaq 0xefbef(%rip), %rsi     # 0x113b8c
xorl %eax, %eax
callq 0xe9df7
movl $0x4, %eax
jmp 0x23fc4
leaq (%rcx,%r14), %rsi
addq $-0x10, %rsi
movq %rax, 0x8(%rsi)
addq %r14, %rcx
movq %rdx, 0x8(%rcx)
addq $0x24, %r12
xorl %eax, %eax
movq (%rsp), %r13
testl %eax, %eax
je 0x23ec7
cmpl $0x4, %eax
jne 0x23fdd
xorl %r15d, %r15d
jmp 0x240e7
leaq 0xefbba(%rip), %rsi     # 0x113b9e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0xf6870
movq %rax, %r15
testq %r13, %r13
jle 0x240e7
movq %r14, (%rsp)
addq $0x8, %r14
movl $0x20, %r12d
movq %rbx, %rdi
leaq 0xefba0(%rip), %rsi     # 0x113bba
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r15, %rdi
callq 0xf6883
movq 0x18(%rbx), %rdi
movl $0x1f, %edx
movq %r12, %rsi
movq %r15, %rcx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xf01b0
movl -0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xefb69(%rip), %rsi     # 0x113bd0
xorl %eax, %eax
callq 0xe9226
movl (%r14), %edx
movq %rbx, %rdi
leaq 0xefb61(%rip), %rsi     # 0x113bdc
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %rbp
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xea9e4
movq 0x18(%rbx), %rdi
movq -0x8(%r14), %rsi
movq (%r14), %rdx
leaq 0xefb40(%rip), %rcx     # 0x113bef
movq %rbp, %r8
xorl %r9d, %r9d
callq 0xf0321
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xea9b3
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq $0x24, %r12
addq $0x10, %r14
decq %r13
jne 0x24010
movq (%rsp), %r14
movq %r15, %rdi
callq 0xf6bb0
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752