// File: /workspace/github_c_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
leaq 0x4c(%rsp), %rsi
callq 0xe8a1c
movl $0x20, %esi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, %r12
leaq 0xc9c2c(%rip), %rsi     # 0x1214a5
movq %r14, %rdi
movq %r13, %rdx
movq %rbx, 0x30(%rsp)
movq %rbx, %rcx
movq %r14, %rbx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpb $0x0, 0x58(%r15)
je 0x578b5
movq 0x90(%r15), %rcx
cmpq $0x7ff, %rcx            # imm = 0x7FF
jg 0x5790b
leaq 0xc9bff(%rip), %rsi     # 0x1214bb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
testq %r12, %r12
je 0x578ed
movq 0x10(%r12), %rdi
callq 0xf052b
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
movl 0x4c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%r15)
je 0x5792a
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x10(%r12)
movq 0x90(%r15), %rcx
movq 0x30(%rsp), %rax
addq %rcx, %rax
addq $-0xc, %rax
cqto
idivq %rcx
movq %rdx, %rsi
movq %rdx, (%r12)
movl $0x10, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, 0x8(%r12)
movl $0xffffffff, 0x18(%r12) # imm = 0xFFFFFFFF
cmpq $0x0, (%r12)
movq %rbx, 0x20(%rsp)
movq %r12, 0x8(%rsp)
jle 0x57bec
addq %r13, 0x30(%rsp)
xorl %ebp, %ebp
xorl %edx, %edx
movq %r15, 0x40(%rsp)
movq 0x90(%r15), %rax
movq 0x30(%rsp), %r14
subq %r13, %r14
cmpq %r14, %rax
cmovlq %rax, %r14
cmpq $0xc, %r14
jl 0x57bec
movq %rbp, 0x18(%rsp)
leaq 0xc(%r13), %r8
leaq -0xc(%r14), %r9
movq %rbx, %rdi
leaq 0xc9b1a(%rip), %rsi     # 0x1214d1
movq %rdx, 0x28(%rsp)
movq %r13, %rcx
movq %r8, 0x50(%rsp)
movq %r9, 0x10(%rsp)
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeed61
movq %rax, %rbp
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xeed61
movq %rbx, %rcx
movq %r15, %rbx
movq %rax, %r15
movq 0x18(%rcx), %rdi
movq %r13, 0x38(%rsp)
leaq 0x8(%r13), %rsi
callq 0xeed61
movq %rax, %r12
movl 0x8c(%rbx), %edi
movl %ebp, %esi
movq 0x8(%rsp), %r13
leaq 0x90(%rsp), %rdx
callq 0x58d30
movl 0x8c(%rbx), %edi
movl %r15d, %esi
leaq 0xd0(%rsp), %rbp
movq %rbp, %rdx
callq 0x58d30
movl 0x8c(%rbx), %edi
movl %r12d, %esi
movq 0x20(%rsp), %rbx
leaq 0xb0(%rsp), %r12
movq %r12, %rdx
callq 0x58d30
movq %rbx, %rdi
leaq 0xc9a8a(%rip), %rsi     # 0x1214fc
leaq 0x90(%rsp), %rdx
movq %rbp, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0xe8617
movq %r13, %r12
cmpl $-0x1, 0x18(%r13)
jne 0x57a9c
cmpl $-0x1, %r15d
je 0x57a9c
movl %r15d, 0x18(%r12)
movq 0x40(%rsp), %rax
cmpl $0x0, 0x8(%rax)
movq 0x38(%rsp), %r13
movq 0x18(%rsp), %rbp
je 0x57bc1
movq 0x10(%r12), %r15
testq %r15, %r15
je 0x57bc1
movq 0x18(%r15), %rax
movq 0x8(%r12), %rcx
movq %rax, (%rcx,%rbp)
movq 0x40(%rsp), %rax
cmpl $0x0, 0x98(%rax)
je 0x57b80
movq %rbx, %rdi
leaq 0x70(%rsp), %rbp
movq %rbp, %rsi
leaq 0x58(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
callq 0xfa1dc
movq 0x18(%rbx), %rax
movq %rax, 0x70(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %r15, 0x58(%rsp)
movb $0x1, 0x60(%rsp)
movq $0x3ff4, 0x68(%rsp)     # imm = 0x3FF4
movq %rbx, %r12
movq 0x18(%r15), %rbx
movq %r12, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rbp
leaq 0x58(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
xorl %r8d, %r8d
callq 0xfaf12
movq 0x18(%r15), %rcx
subq %rbx, %rcx
movq %r12, %rbx
movq 0x8(%rsp), %r12
movq %rbx, %rdi
leaq 0xc99dd(%rip), %rsi     # 0x12154f
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xe8617
jmp 0x57b96
movq 0x18(%rbx), %rdi
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r15, %rcx
callq 0xef481
movq 0x8(%r12), %rax
movq 0x10(%r12), %rcx
movq 0x18(%rcx), %rdx
movq %rdx, %rcx
subq (%rax,%rbp), %rcx
movq %rcx, 0x8(%rax,%rbp)
movq %rbx, %rdi
leaq 0xc996b(%rip), %rsi     # 0x121525
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq %r14, %r13
movq 0x28(%rsp), %rdx
incq %rdx
addq $0x10, %rbp
cmpq (%r12), %rdx
movq 0x40(%rsp), %r15
jl 0x57980
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x57c65
cmpq $0x0, 0x18(%rdi)
jle 0x578d0
movq %rdi, 0x18(%rsp)
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
leaq 0xc9953(%rip), %rsi     # 0x12156d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
movl 0x18(%r12), %r14d
leal 0x1(%r14), %eax
cmpl $0xc, %eax
ja 0x57c6c
leaq 0xc9922(%rip), %rsi     # 0x121578
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0x57c70
xorl %edi, %edi
jmp 0x578d0
addl $-0xc, %r14d
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rcx
cmpq %rcx, %r14
jle 0x57c94
leaq 0xc9908(%rip), %rsi     # 0x12158d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x5817d
cmpq %rcx, %r14
je 0x58139
leaq 0x15(%r14), %rax
cmpq %rcx, %rax
jg 0x58145
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
leaq 0xc9977(%rip), %rsi     # 0x121630
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r14, 0x70(%rsp)
movq 0x10(%r12), %r13
movl $0x60, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %rbp
movq %r13, %rdi
leaq 0x70(%rsp), %rsi
callq 0xeee2f
movq %rax, (%rbp)
movq %rbx, %rdi
leaq 0xc993e(%rip), %rsi     # 0x121643
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%rbp), %rax
cmpq $0x15, %rax
jl 0x5810d
addq %r14, %rax
cmpq 0x18(%r13), %rax
jg 0x5810d
movq %r14, 0x28(%rsp)
movq %r13, %rdi
leaq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0xeee2f
movq %rax, 0x8(%rbp)
movq 0x20(%rsp), %r14
movq %r14, %rdi
leaq 0xc9918(%rip), %rsi     # 0x12166a
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r13, %rdi
movq %r12, %rsi
callq 0xeed83
movl %eax, 0x10(%rbp)
movl 0x8c(%r15), %edi
movl %eax, %esi
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdx
callq 0x58d30
movq %r14, %rdi
leaq 0xc9905(%rip), %rsi     # 0x121692
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r13, %rdi
movq %r12, %rsi
callq 0xeed83
movl %eax, 0x14(%rbp)
movl 0x8c(%r15), %edi
movl %eax, %esi
movq %rbx, %rdx
callq 0x58d30
movq %r14, %rdi
leaq 0xc98e0(%rip), %rsi     # 0x1216a0
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe8617
movl 0x14(%rbp), %ebx
movq %r13, %rdi
movq %r12, %rsi
callq 0xeee2f
movq %rax, 0x18(%rbp)
movq %r14, %rdi
leaq 0xc98c8(%rip), %rsi     # 0x1216ae
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0xeeb4b
movb %al, 0x20(%rbp)
movzbl %al, %edx
movq %r14, %rdi
leaq 0xc9914(%rip), %rsi     # 0x121724
xorl %eax, %eax
callq 0xe8617
movq 0x38(%rsp), %rdx
movq %rdx, 0x28(%rbp)
movq 0x18(%rbp), %rcx
addq $-0x15, %rcx
movq %rcx, 0x30(%rbp)
movq %r14, %rdi
leaq 0xc9886(%rip), %rsi     # 0x1216bc
xorl %eax, %eax
callq 0xe8617
movq (%rbp), %rcx
movq 0x28(%rbp), %rsi
movq 0x30(%rbp), %rax
addq %rsi, %rax
movq %rax, 0x38(%rbp)
subq 0x18(%rbp), %rcx
cmpl $0x0, 0x9c(%r15)
jne 0x57e6c
cmpl $0x0, 0xa0(%r15)
je 0x58034
movq 0x8(%rbp), %rax
cmpq %rcx, %rax
movq 0x28(%rsp), %r13
jle 0x57e88
movb $0x1, 0x58(%rbp)
movq %rcx, 0x40(%rbp)
movq %rax, 0x48(%rbp)
jmp 0x57e93
movq %rax, 0x40(%rbp)
movq %rax, 0x48(%rbp)
movq %rax, %rcx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r12
cmpq %r13, %rsi
jl 0x57f34
movq 0x38(%rbp), %rdx
cmpq %r13, %rdx
jl 0x57f34
movq 0x30(%rbp), %rdi
testq %rdi, %rdi
js 0x57f34
testq %rcx, %rcx
js 0x57f34
addq %rsi, %rdi
movq (%rbp), %rsi
addq %r13, %rsi
cmpq %rsi, %rdi
jg 0x57f34
leaq (%rdx,%rcx), %rdi
cmpq %rsi, %rdi
jg 0x57f34
movq 0x20(%rsp), %rdi
leaq 0xc9802(%rip), %rsi     # 0x1216e7
movq %rax, %r8
xorl %eax, %eax
callq 0xe8617
movq 0xd8(%r15), %rdi
xorl %esi, %esi
callq 0xf0f27
cmpb $0x0, 0x58(%rbp)
je 0x58088
cmpl $0x0, 0x9c(%r15)
je 0x5805e
movq 0x38(%rbp), %rdx
movq 0x40(%rbp), %rcx
movq 0xd8(%r15), %r8
movq %r15, %rdi
movq %r12, %rsi
callq 0x58d91
jmp 0x5809f
movq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0xc9797(%rip), %rsi     # 0x1216da
xorl %eax, %eax
callq 0xe8617
movq %r12, %rdi
movq %rbp, %rsi
callq 0xe8be2
movq %r12, %rbp
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $-0x1, %ebx
je 0x5815b
cmpl $0x10, 0x8c(%r15)
jg 0x57fc6
cmpl $0x14, %ebx
jbe 0x58130
movq 0x90(%r15), %rsi
leaq -0xc(%rsi), %rcx
movq %r13, %rax
cqto
idivq %rcx
movq %r13, %rax
subq %rdx, %rax
leaq (%rsi,%rax), %rdx
addq $-0xc, %rdx
addq %r13, %rbx
movq %rbx, %rax
subq %rdx, %rax
jle 0x58017
cqto
idivq %rcx
shlq $0x2, %rax
leaq (%rax,%rax,2), %rax
negq %rax
addq %rax, %rbx
addq $-0xc, %rbx
jmp 0x58017
cmpq $0x0, 0x90(%r15)
je 0x58130
movl %ebx, %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cmpq $0xc, %rax
jb 0x58130
shrl $0xe, %ebx
movq 0x8(%rsp), %rcx
cmpq (%rcx), %rbx
jge 0x58130
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
shll $0x4, %ebx
movq (%rcx,%rbx), %rcx
leaq (%rax,%rcx), %rbx
addq $-0xc, %rbx
cmpq %r13, %rbx
jle 0x58164
movq 0x18(%r14), %rcx
movq %rbx, %r14
cmpq %rcx, %rbx
movq 0x8(%rsp), %r12
movq %rbp, %rbx
jle 0x57c94
jmp 0x57c7e
movq 0x8(%rbp), %rdi
movq %rcx, %rsi
callq 0xe9c3b
movq %rax, 0x40(%rbp)
movq %rax, 0x48(%rbp)
movq 0x28(%rbp), %rsi
movq %rax, %rcx
movq 0x18(%rsp), %r14
movq 0x28(%rsp), %r13
jmp 0x57e98
cmpl $0x0, 0xa0(%r15)
je 0x58088
movq 0x38(%rbp), %rdx
movq 0x40(%rbp), %rcx
movq 0xd8(%r15), %r8
movq 0x48(%rbp), %r9
movq %r15, %rdi
movq %r12, %rsi
callq 0x58e76
jmp 0x5809f
movq 0x38(%rbp), %rsi
movq 0x40(%rbp), %rdx
movq 0xd8(%r15), %rcx
movq %r12, %rdi
callq 0xef481
movzbl 0x20(%rbp), %edx
cmpl $0x1f, %edx
movq 0x20(%rsp), %r12
jg 0x580ce
cmpl $0x1, %edx
je 0x580d8
cmpl $0x2, %edx
jne 0x580f7
movq 0xd8(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x59294
jmp 0x57f4a
cmpl $0x23, %edx
je 0x580d8
cmpl $0x20, %edx
jne 0x580f7
movq 0xd8(%r15), %r8
movq %r12, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq %rbp, %rcx
callq 0x58ff8
jmp 0x57f4a
movq %r12, %rdi
leaq 0xc9617(%rip), %rsi     # 0x121718
xorl %eax, %eax
callq 0xe8faa
jmp 0x57f4a
leaq 0xc953e(%rip), %rsi     # 0x121652
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe8be2
jmp 0x58178
leaq 0xc96bd(%rip), %rsi     # 0x1217f4
jmp 0x5816b
leaq 0xc947c(%rip), %rsi     # 0x1215bc
jmp 0x57c85
leaq 0xc949e(%rip), %rsi     # 0x1215ea
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8faa
jmp 0x5817d
leaq 0xc965e(%rip), %rsi     # 0x1217c0
jmp 0x5816b
leaq 0xc96ba(%rip), %rsi     # 0x121825
movq %rbp, %rbx
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %r12
movl 0x58(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
jmp 0x578c6