// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x173d79(%rip), %rsi    # 0x19fb2c
movb $0x0, %al
callq 0x15b0d0
movq $0x40, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x15c450
movq %rax, -0x38(%rbp)
movq $0x20, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x18(%rcx), %rcx
subq $0x24, %rcx
cmpq %rcx, %rax
jle 0x2be10
movq -0x8(%rbp), %rdi
leaq 0x173d46(%rip), %rsi    # 0x19fb4a
movb $0x0, %al
callq 0x15bfb0
jmp 0x2c15f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2be2e
jmp 0x2bfe1
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x30(%rbp), %rax
jle 0x2beae
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16a9a0
cmpl $0x0, %eax
jne 0x2be75
movq -0x8(%rbp), %rdi
leaq 0x173cf8(%rip), %rsi    # 0x19fb61
movb $0x0, %al
callq 0x15bfb0
jmp 0x2c15f
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
shlq %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %r8
movl $0x10, %ecx
callq 0x15c4d0
movq %rax, -0x38(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x2bf0f
movq -0x8(%rbp), %rdi
leaq 0x173c6e(%rip), %rsi    # 0x19fb71
movb $0x0, %al
callq 0x15bfb0
jmp 0x2c15f
cmpq $0x0, -0x48(%rbp)
jle 0x2bf59
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jge 0x2bf57
movq -0x8(%rbp), %rdi
leaq 0x173c26(%rip), %rsi    # 0x19fb71
movb $0x0, %al
callq 0x15bfb0
jmp 0x2c15f
jmp 0x2bf59
cmpq $0x0, -0x48(%rbp)
jle 0x2bf9f
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
addq $0x24, %rax
movq %rax, -0x18(%rbp)
jmp 0x2bde0
movq -0x8(%rbp), %rdi
leaq 0x173b97(%rip), %rsi    # 0x19fb83
movb $0x0, %al
callq 0x15b0d0
movq $0x20, -0x18(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x40(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x2c15d
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x173b68(%rip), %rsi    # 0x19fb9f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl $0x1f, %edx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, %edx
leaq 0x173b1e(%rip), %rsi    # 0x19fbb5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x173b03(%rip), %rsi    # 0x19fbc1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
movq -0x60(%rbp), %r8
leaq 0x173ab7(%rip), %rcx    # 0x19fbd4
xorl %r9d, %r9d
callq 0x166be0
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
addq $0x24, %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x2c010
jmp 0x2c15f
movq -0x40(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)