// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x131571(%rip), %rsi    # 0x1ad04e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpb $0x0, 0x58(%rax)
je 0x7bb0d
movq -0x10(%rbp), %rax
cmpq $0x800, 0x90(%rax) # imm = 0x800
jge 0x7bb24
movq -0x8(%rbp), %rdi
leaq 0x13154c(%rip), %rsi    # 0x1ad064
movb $0x0, %al
callq 0x15bfb0
jmp 0x7be59
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x7bb48
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x90(%rcx), %rcx
subq $0xc, %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
cqto
idivq 0x90(%rcx)
movq -0x38(%rbp), %rax
movq %rdx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
movl $0x10, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x7be2c
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
subq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jle 0x7bbea
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0xc, -0x58(%rbp)
jge 0x7bbf6
jmp 0x7be2c
movq -0x28(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
subq $0xc, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x68(%rbp), %r9
leaq 0x13144f(%rip), %rsi    # 0x1ad07a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x165540
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
callq 0x165540
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x44(%rbp), %edx
leaq -0x80(%rbp), %rcx
movl $0x18, %r8d
callq 0x7d5b0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x48(%rbp), %edx
leaq -0xa0(%rbp), %rcx
movl $0x18, %r8d
callq 0x7d5b0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x4c(%rbp), %edx
leaq -0xc0(%rbp), %rcx
movl $0x18, %r8d
callq 0x7d5b0
movq -0x8(%rbp), %rdi
leaq -0x80(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
leaq -0xc0(%rbp), %r8
leaq 0x1313b0(%rip), %rsi    # 0x1ad0a5
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
cmpl $-0x1, 0x18(%rax)
jne 0x7bd16
cmpl $-0x1, -0x48(%rbp)
je 0x7bd16
movl -0x48(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x7be01
movq -0x38(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x7be01
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x98(%rax)
je 0x7bd8d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x68(%rbp), %r9
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, (%rsp)
callq 0x7d640
jmp 0x7bdaa
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rcx
callq 0x166070
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rdx
leaq 0x1312d4(%rip), %rsi    # 0x1ad0ce
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x58(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x7bba5
movq -0x38(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x7be57
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x18(%rax)
jle 0x7be57
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x7d710
jmp 0x7be59
cmpq $0x0, -0x38(%rbp)
je 0x7be8b
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x15b5a0
addq $0xd0, %rsp
popq %rbp
retq