// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/hlp.c
// Function: do_file_TOPIC
static void do_file_TOPIC(deark *c, lctx *d, i64 pos1, i64 len)
{
	i64 pos = pos1;
	int saved_indent_level;
	struct topic_ctx *tctx = NULL;
	i64 blknum;

	de_dbg_indent_save(c, &saved_indent_level);
	tctx = de_malloc(c, sizeof(struct topic_ctx));
	de_dbg(c, "TOPIC at %"I64_FMT", len=%"I64_FMT, pos1, len);
	de_dbg_indent(c, 1);

	if(!d->found_system_file || d->topic_block_size<2048) {
		de_err(c, "SYSTEM file not found");
		goto done;
	}

	if(d->extract_text) {
		tctx->unc_topicdata = dbuf_create_membuf(c, 0, 0);
	}
	tctx->num_topic_blocks = (len + (d->topic_block_size - TOPICBLOCKHDRSIZE)) % d->topic_block_size;
	tctx->topic_block_info = de_mallocarray(c, tctx->num_topic_blocks, sizeof(struct topic_block_info_item));

	tctx->pos_of_first_topiclink = INVALIDPOS;

	// A series of blocks, each with a 12-byte header
	for(blknum=0; blknum<tctx->num_topic_blocks; blknum++) {
		u32 lastlink, firstlink, lastheader;
		i64 blklen;
		i64 blk_dpos;
		i64 blk_dlen;
		char tbuf1[24], tbuf2[24], tbuf3[24];

		blklen = d->topic_block_size;
		if(blklen > (pos1+len)-pos) {
			blklen = (pos1+len)-pos;
		}
		if(blklen<TOPICBLOCKHDRSIZE) break;
		blk_dpos = pos+TOPICBLOCKHDRSIZE;
		blk_dlen = blklen-TOPICBLOCKHDRSIZE;

		de_dbg(c, "TOPIC block #%d at %"I64_FMT", dpos=%"I64_FMT", dlen=%"I64_FMT,
			(int)blknum, pos, blk_dpos, blk_dlen);
		de_dbg_indent(c, 1);
		lastlink = (u32)de_getu32le(pos);
		firstlink = (u32)de_getu32le(pos+4);
		lastheader = (u32)de_getu32le(pos+8);
		format_topiclink(c, d, lastlink, tbuf1, sizeof(tbuf1));
		format_topiclink(c, d, firstlink, tbuf2, sizeof(tbuf2));
		format_topiclink(c, d, lastheader, tbuf3, sizeof(tbuf3));
		de_dbg(c, "LastLink=%s, FirstLink=%s, LastHeader=%s",
			tbuf1, tbuf2, tbuf3);
		if(tctx->pos_of_first_topiclink==INVALIDPOS && firstlink!=INVALIDPOS) {
			tctx->pos_of_first_topiclink = firstlink;
		}

		if(d->extract_text && tctx->unc_topicdata) {
			// Record the position for later reference.
			tctx->topic_block_info[blknum].pos = tctx->unc_topicdata->len;

			if(d->is_lz77_compressed) {
				decompress_topic_block(c, d, tctx, blknum, blk_dpos, blk_dlen, tctx->unc_topicdata);
			}
			else {
				dbuf_copy(c->infile, blk_dpos, blk_dlen, tctx->unc_topicdata);
			}

			tctx->topic_block_info[blknum].len = tctx->unc_topicdata->len - tctx->topic_block_info[blknum].pos;

			de_dbg2(c, "[current decompressed size: %"I64_FMT"]", tctx->unc_topicdata->len);
		}

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

	if(tctx->unc_topicdata && tctx->unc_topicdata->len>0) {
		do_topicdata(c, d, tctx);
	}

done:
	if(tctx) {
		dbuf_close(tctx->unc_topicdata);
		de_free(c, tctx->topic_block_info);
		de_free(c, tctx);
	}
	de_dbg_indent_restore(c, saved_indent_level);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r15
leaq 0x3c(%rsp), %rsi
callq 0xe958c
movl $0x20, %esi
movq %r15, %rdi
callq 0xe969b
movq %rax, %r12
leaq 0xc9964(%rip), %rsi     # 0x121399
movq %r15, %rdi
movq %r13, %rbp
movq %r13, %rdx
movq %rbx, 0x10(%rsp)
movq %rbx, %rcx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
cmpb $0x0, 0x58(%r14)
movq %r12, 0x18(%rsp)
je 0x57a73
cmpq $0x7ff, 0x90(%r14) # imm = 0x7FF
jg 0x57acc
leaq 0xc9935(%rip), %rsi     # 0x1213af
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
je 0x57aae
movq 0x10(%rbx), %rdi
callq 0xf0b13
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0xe9752
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe9752
movl 0x3c(%rsp), %esi
movq %r15, %rdi
callq 0xe9595
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%r14)
je 0x57ae4
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x10(%r12)
movq 0x90(%r14), %rcx
movq 0x10(%rsp), %rax
addq %rcx, %rax
addq $-0xc, %rax
cqto
idivq %rcx
movq %rdx, %rsi
movq %rdx, (%r12)
movl $0x10, %edx
movq %r15, %rdi
callq 0xea259
movq %rax, 0x8(%r12)
movl $0xffffffff, 0x18(%r12) # imm = 0xFFFFFFFF
cmpq $0x0, (%r12)
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
jle 0x57d9b
movq %rbp, %rcx
addq %rbp, 0x10(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x90(%r14), %rax
movq 0x10(%rsp), %r13
subq %rcx, %r13
cmpq %r13, %rax
cmovlq %rax, %r13
cmpq $0xc, %r13
jl 0x57d84
movq %rbx, 0x20(%rsp)
leaq 0xc(%rcx), %r8
leaq -0xc(%r13), %r9
movq %r15, %rdi
leaq 0xc9850(%rip), %rsi     # 0x1213c5
movq %r14, %r12
movq %rcx, %r14
movl %ebp, %edx
movq %r8, 0x40(%rsp)
movq %r9, 0x30(%rsp)
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r15), %rdi
movq %r14, %rsi
callq 0xef37e
movq %rax, %r15
movq (%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x4(%r14), %rsi
callq 0xef37e
movq %rax, %rbx
movq (%rsp), %rax
movq 0x18(%rax), %rdi
movq %r14, 0x28(%rsp)
leaq 0x8(%r14), %rsi
callq 0xef37e
movq %rax, %r14
movq %r12, %rdi
movl %r15d, %esi
movq (%rsp), %r15
leaq 0x80(%rsp), %rdx
callq 0x58f07
movq %r12, %rdi
movl %ebx, %esi
leaq 0xc0(%rsp), %rdx
callq 0x58f07
movq %r12, %rdi
movl %r14d, %esi
movq 0x18(%rsp), %r12
leaq 0xa0(%rsp), %rdx
callq 0x58f07
movq %r15, %r14
movq %r15, %rdi
leaq 0xc97cb(%rip), %rsi     # 0x1213f0
leaq 0x80(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xa0(%rsp), %r8
xorl %eax, %eax
callq 0xe9226
cmpl $-0x1, 0x18(%r12)
jne 0x57c56
cmpl $-0x1, %ebx
je 0x57c56
movl %ebx, 0x18(%r12)
movq 0x8(%rsp), %rax
cmpl $0x0, 0x8(%rax)
movq %r14, %r15
movq 0x20(%rsp), %rbx
je 0x57d6a
movq 0x10(%r12), %r14
testq %r14, %r14
je 0x57d6a
movq 0x18(%r14), %rax
movq 0x8(%r12), %rcx
movq %rax, (%rcx,%rbx)
movq 0x8(%rsp), %rax
cmpl $0x0, 0x98(%rax)
je 0x57d29
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
leaq 0x68(%rsp), %rdx
leaq 0xe0(%rsp), %rcx
callq 0xfa6e0
movq 0x18(%r15), %rax
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %r14, 0x68(%rsp)
movb $0x1, 0x70(%rsp)
movq $0x3ff4, 0x78(%rsp)     # imm = 0x3FF4
movq 0x18(%r14), %r12
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
leaq 0x68(%rsp), %rdx
leaq 0xe0(%rsp), %rcx
xorl %r8d, %r8d
callq 0xfb446
movq 0x18(%r14), %rcx
subq %r12, %rcx
movq 0x18(%rsp), %r12
movq %r15, %rdi
leaq 0xc9728(%rip), %rsi     # 0x121443
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0xe9226
jmp 0x57d3f
movq 0x18(%r15), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r14, %rcx
callq 0xefabc
movq 0x8(%r12), %rax
movq 0x10(%r12), %rcx
movq 0x18(%rcx), %rdx
movq %rdx, %rcx
subq (%rax,%rbx), %rcx
movq %rcx, 0x8(%rax,%rbx)
movq %r15, %rdi
leaq 0xc96b6(%rip), %rsi     # 0x121419
xorl %eax, %eax
callq 0xe93cf
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x28(%rsp), %rcx
addq %r13, %rcx
movq 0x8(%rsp), %r14
cmpq $0xb, %r13
jle 0x57d9b
incq %rbp
addq $0x10, %rbx
cmpq (%r12), %rbp
jl 0x57b3e
movq 0x10(%r12), %r14
testq %r14, %r14
je 0x57a84
cmpq $0x0, 0x18(%r14)
jle 0x57a84
leaq 0x68(%rsp), %rsi
movq %r15, %rdi
callq 0xe958c
leaq 0xc9699(%rip), %rsi     # 0x121461
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x80(%rsp), %rsi
movq %r15, %rdi
callq 0xe958c
movq 0x18(%rsp), %rax
movl 0x18(%rax), %r12d
leal 0x1(%r12), %eax
cmpl $0xc, %eax
ja 0x57e1b
leaq 0xc9663(%rip), %rsi     # 0x12146c
xorl %r12d, %r12d
movq %r15, %r13
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x57e22
movq %r15, %r13
addl $-0xc, %r12d
leaq 0xc9658(%rip), %r15     # 0x121481
leaq 0xc9680(%rip), %rbp     # 0x1214b0
movq %r14, 0x10(%rsp)
movq 0x18(%r14), %rcx
cmpq %rcx, %r12
jle 0x57e48
xorl %ebx, %ebx
movq %r13, %rdi
movq %r15, %rsi
jmp 0x57e52
jne 0x57e62
xorl %ebx, %ebx
movq %r13, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0xe9226
testb %bl, %bl
jne 0x57e35
jmp 0x58383
leaq 0x15(%r12), %r14
cmpq %rcx, %r14
jle 0x57e89
xorl %ebx, %ebx
movq %r13, %rdi
leaq 0xc9666(%rip), %rsi     # 0x1214de
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9b27
movq 0x10(%rsp), %r14
jmp 0x57e59
movq %r13, %rdi
leaq 0xc9691(%rip), %rsi     # 0x121524
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r13, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r12, 0x48(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rbx
movl $0x60, %esi
movq %r13, %rdi
callq 0xe969b
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x48(%rsp), %rsi
callq 0xef475
movq %rax, (%rbp)
movq %r13, %rdi
leaq 0xc9654(%rip), %rsi     # 0x121537
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%rbp), %rax
cmpq $0x15, %rax
jl 0x57f00
addq %r12, %rax
cmpq 0x18(%rbx), %rax
jle 0x57f20
xorl %r15d, %r15d
movq %r13, %rdi
leaq 0xc9639(%rip), %rsi     # 0x121546
xorl %eax, %eax
callq 0xe9226
movb $0x1, %bl
movq 0x10(%rsp), %r14
jmp 0x58108
movq %rbx, %rdi
leaq 0x48(%rsp), %r15
movq %r15, %rsi
callq 0xef475
movq %rax, 0x8(%rbp)
movq %r13, %rdi
leaq 0xc9620(%rip), %rsi     # 0x12155e
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r15, %rsi
callq 0xef3a0
movl %eax, 0x10(%rbp)
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl %eax, %esi
movq %rbx, %r15
leaq 0xe0(%rsp), %rdx
callq 0x58f07
movq (%rsp), %rbx
movq %rbx, %rdi
leaq 0xc9608(%rip), %rsi     # 0x121586
leaq 0xe0(%rsp), %rdx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0xef3a0
movl %eax, 0x14(%rbp)
movq %r13, %rdi
movl %eax, %esi
leaq 0xe0(%rsp), %rdx
callq 0x58f07
movq %rbx, %rdi
leaq 0xc95db(%rip), %rsi     # 0x121594
leaq 0xe0(%rsp), %rdx
xorl %eax, %eax
callq 0xe9226
movl 0x14(%rbp), %eax
movl %eax, 0x28(%rsp)
movq %r15, %rdi
leaq 0x48(%rsp), %r13
movq %r13, %rsi
callq 0xef475
movq %rax, 0x18(%rbp)
movq %rbx, %rdi
leaq 0xc95b5(%rip), %rsi     # 0x1215a2
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r15, 0x20(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0xef14d
movb %al, 0x20(%rbp)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0xc9601(%rip), %rsi     # 0x121618
xorl %eax, %eax
callq 0xe9226
movq %r14, 0x28(%rbp)
movq 0x18(%rbp), %rcx
addq $-0x15, %rcx
movq %rcx, 0x30(%rbp)
movq %rbx, %rdi
leaq 0xc9578(%rip), %rsi     # 0x1215b0
movq %r14, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%rbp), %rax
movq 0x30(%rbp), %rcx
addq 0x28(%rbp), %rcx
movq %rcx, 0x38(%rbp)
subq 0x18(%rbp), %rax
movq 0x8(%rsp), %rcx
cmpl $0x0, 0x9c(%rcx)
jne 0x58072
movq 0x8(%rsp), %rcx
cmpl $0x0, 0xa0(%rcx)
je 0x58086
movq 0x8(%rbp), %rcx
cmpq %rax, %rcx
jle 0x58081
movb $0x1, 0x58(%rbp)
jmp 0x58095
movq %rcx, %rax
jmp 0x58095
movq 0x8(%rbp), %rdi
movq %rax, %rsi
callq 0xea792
movq %rax, %rcx
movq 0x10(%rsp), %r14
movl 0x28(%rsp), %r15d
movq %rax, 0x40(%rbp)
movq %rcx, 0x48(%rbp)
movq 0x28(%rbp), %rax
cmpq %r12, %rax
jl 0x580f1
movq 0x38(%rbp), %rdx
cmpq %r12, %rdx
jl 0x580f1
movq 0x30(%rbp), %rsi
testq %rsi, %rsi
js 0x580f1
movq 0x40(%rbp), %rcx
testq %rcx, %rcx
js 0x580f1
addq %rax, %rsi
movq (%rbp), %rax
addq %r12, %rax
cmpq %rax, %rsi
setg %sil
leaq (%rcx,%rdx), %rdi
cmpq %rax, %rdi
setg %al
orb %sil, %al
cmpb $0x1, %al
jne 0x58256
xorl %ebx, %ebx
movq (%rsp), %r13
movq %r13, %rdi
leaq 0xc94cd(%rip), %rsi     # 0x1215ce
xorl %eax, %eax
callq 0xe9226
movq %r13, %rdi
movq %rbp, %rsi
callq 0xe9752
testb %bl, %bl
je 0x5812c
xorl %ebx, %ebx
leaq 0xc9361(%rip), %r15     # 0x121481
leaq 0xc9389(%rip), %rbp     # 0x1214b0
jmp 0x57e59
movq %r13, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $-0x1, %r15d
je 0x581b3
movq 0x8(%rsp), %rax
cmpl $0x10, 0x8c(%rax)
jg 0x581d2
cmpl $0x14, %r15d
leaq 0xc9354(%rip), %rbp     # 0x1214b0
jbe 0x58226
movl %r15d, %r15d
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rsi
leaq -0xc(%rsi), %rcx
movq %r12, %rax
cqto
idivq %rcx
movq %r12, %rax
subq %rdx, %rax
leaq (%rsi,%rax), %rdx
addq $-0xc, %rdx
addq %r12, %r15
movq %r15, %rax
subq %rdx, %rax
jle 0x58222
cqto
idivq %rcx
shlq $0x2, %rax
leaq (%rax,%rax,2), %rax
negq %rax
addq %rax, %r15
addq $-0xc, %r15
jmp 0x58222
movq %r13, %rdi
leaq 0xc94f7(%rip), %rsi     # 0x1216b4
xorl %eax, %eax
callq 0xe9226
movb $0x1, %bl
xorl %r15d, %r15d
leaq 0xc92e0(%rip), %rbp     # 0x1214b0
jmp 0x5823d
cmpq $0x0, 0x90(%rax)
leaq 0xc92cf(%rip), %rbp     # 0x1214b0
je 0x58226
movl %r15d, %eax
movl %eax, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
cmpq $0xc, %rcx
jb 0x58226
shrl $0xe, %eax
movq 0x18(%rsp), %rdx
cmpq (%rdx), %rax
jge 0x58226
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rdx
shll $0x4, %eax
movq (%rdx,%rax), %rax
leaq (%rcx,%rax), %r15
addq $-0xc, %r15
cmpq %r12, %r15
jle 0x582ac
xorl %ebx, %ebx
jmp 0x5823d
movq (%rsp), %rdi
leaq 0xc94b7(%rip), %rsi     # 0x1216e8
xorl %eax, %eax
callq 0xe9226
movb $0x1, %bl
xorl %r15d, %r15d
testb %bl, %bl
cmoveq %r15, %r12
xorb $0x1, %bl
movq (%rsp), %r13
leaq 0xc9230(%rip), %r15     # 0x121481
jmp 0x57e59
movq 0x48(%rbp), %r8
movq (%rsp), %rdi
leaq 0xc9376(%rip), %rsi     # 0x1215db
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rsp), %rbx
movq 0xd8(%rbx), %rdi
xorl %esi, %esi
callq 0xf152a
cmpb $0x0, 0x58(%rbp)
je 0x582f0
cmpl $0x0, 0x9c(%rbx)
je 0x582c5
movq 0x38(%rbp), %rdx
movq 0x40(%rbp), %rcx
movq 0xd8(%rbx), %r8
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x58f6c
jmp 0x58309
movq (%rsp), %rdi
leaq 0xc9462(%rip), %rsi     # 0x121719
xorl %eax, %eax
callq 0xe9226
movb $0x1, %bl
jmp 0x5823d
cmpl $0x0, 0xa0(%rbx)
je 0x582f0
movq 0x38(%rbp), %rdx
movq 0x40(%rbp), %rcx
movq 0xd8(%rbx), %r8
movq 0x48(%rbp), %r9
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x59072
jmp 0x58309
movq 0x38(%rbp), %rsi
movq 0x40(%rbp), %rdx
movq 0xd8(%rbx), %rcx
movq 0x20(%rsp), %rdi
callq 0xefabc
movzbl 0x20(%rbp), %edx
cmpl $0x1f, %edx
jg 0x58336
cmpl $0x1, %edx
je 0x58340
cmpl $0x2, %edx
jne 0x58367
movq 0x8(%rsp), %rsi
movq 0xd8(%rsi), %rdx
movq (%rsp), %r13
movq %r13, %rdi
callq 0x5948c
jmp 0x58360
cmpl $0x23, %edx
je 0x58340
cmpl $0x20, %edx
jne 0x58367
movq 0x8(%rsp), %rsi
movq 0xd8(%rsi), %r8
movq (%rsp), %r13
movq %r13, %rdi
movq 0x18(%rsp), %rdx
movq %rbp, %rcx
callq 0x591f4
xorl %ebx, %ebx
jmp 0x58108
xorl %ebx, %ebx
movq (%rsp), %r13
movq %r13, %rdi
leaq 0xc9295(%rip), %rsi     # 0x12160c
xorl %eax, %eax
callq 0xe9b27
jmp 0x58108
movl 0x68(%rsp), %esi
movq %r13, %r15
movq %r13, %rdi
callq 0xe9595
jmp 0x57a84