// File: /workspace/github_c_makefile_star2_O3/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 0xefd72(%rip), %rsi     # 0x113bf7
xorl %eax, %eax
callq 0xe8617
movl $0x40, %eax
movq %rax, (%rsp)
movl $0x40, %esi
movl $0x10, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, %rbp
movq 0x18(%rbx), %rdi
cmpq $0x44, 0x18(%rdi)
jge 0x23ec1
leaq 0xefd59(%rip), %rsi     # 0x113c15
jmp 0x24093
movl $0x20, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r12, %rsi
callq 0xeead3
testb %al, %al
je 0x23f80
leaq 0x1(%r14), %r13
cmpq (%rsp), %r14
jl 0x23f1c
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf1e9d
testl %eax, %eax
je 0x2408c
movq %rbp, %rsi
movq (%rsp), %rdx
leaq (%rdx,%rdx), %rbp
movl $0x10, %ecx
movq %rbx, %rdi
movq %rbp, %r8
callq 0xe9758
movq %rbp, (%rsp)
movq %rax, %rbp
movq 0x18(%rbx), %rdi
addq $0x20, %r12
movq %r12, %rsi
callq 0xeed61
movq %rax, (%rbp,%r15)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rcx
movq %rcx, %rdx
subq %rax, %rdx
jl 0x23f74
testq %r14, %r14
je 0x23f52
subq -0x10(%rbp,%r15), %rax
jl 0x23f74
movq %rax, -0x8(%rbp,%r15)
movq %rdx, 0x8(%rbp,%r15)
addq $-0x24, %rcx
addq $0x10, %r15
addq $0x4, %r12
movq %r13, %r14
cmpq %rcx, %r12
jle 0x23ecd
jmp 0x23eb5
leaq 0xefcc1(%rip), %rsi     # 0x113c3c
jmp 0x24093
leaq 0xefcc7(%rip), %rsi     # 0x113c4e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r15
testq %r14, %r14
je 0x240a0
movq %rbp, (%rsp)
addq $0x8, %rbp
movl $0x20, %r12d
movq %rbx, %rdi
leaq 0xefcad(%rip), %rsi     # 0x113c6a
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r15, %rdi
callq 0xf6423
movq 0x18(%rbx), %rdi
movl $0x1f, %edx
movq %r12, %rsi
movq %r15, %rcx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xefbd7
movl -0x8(%rbp), %edx
movq %rbx, %rdi
leaq 0xefc77(%rip), %rsi     # 0x113c80
xorl %eax, %eax
callq 0xe8617
movl (%rbp), %edx
movq %rbx, %rdi
leaq 0xefc6f(%rip), %rsi     # 0x113c8c
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
callq 0xea504
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xea53f
movq 0x18(%rbx), %rdi
movq -0x8(%rbp), %rsi
movq (%rbp), %rdx
leaq 0xefc4d(%rip), %rcx     # 0x113c9f
movq %r13, %r8
xorl %r9d, %r9d
callq 0xefd48
movq %rbx, %rdi
movq %r13, %rsi
callq 0xea50e
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x24, %r12
addq $0x10, %rbp
decq %r14
jne 0x23fb3
movq (%rsp), %rbp
jmp 0x240a0
leaq 0xefb99(%rip), %rsi     # 0x113c2c
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %r15, %rdi
callq 0xf6753
movq %rbx, %rdi
movq %rbp, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2