// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/hlp.c
// Function: do_file_Phrases
static void do_file_Phrases(deark *c, lctx *d, i64 pos1, i64 len)
{
	i64 pos;
	i64 s0, s1, s2;
	i64 phrase_data_pos;
	i64 phrase_data_cmpr_len;
	i64 phrase_data_uncmpr_len = 0;
	i64 phrase_offset_table_pos;
	i64 phrase_offset_table_len;
	UI k;
	int is_MVB_format = 0;
	int is_compressed = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	if(!d->extract_text) goto done;

	de_dbg(c, "Phrases data at %"I64_FMT", len=%"I64_FMT, pos1, len);
	de_dbg_indent(c, 1);
	if(len<6) goto done;

	s0 = de_getu16le(pos1);
	s1 = de_getu16le(pos1+2);
	s2 = de_getu16le(pos1+4);

	if(s0==0x0800 && s1!=0x0100 && s2==0x0100) {
		is_MVB_format = 1;
	}
	else if(s1==0x0100) {
		;
	}
	else {
		de_err(c, "Unknown Phrases format");
		goto done;
	}
	de_dbg(c, "MVB format: %d", is_MVB_format);
	if(is_MVB_format) {
		de_err(c, "Unsupported Phrases format");
		goto done;
	}

	d->num_phrases = (UI)s0;
	de_dbg(c, "num phrases: %u", d->num_phrases);
	pos = pos1+4;

	if(d->ver_minor>16) {
		is_compressed = 1;
	}
	de_dbg(c, "Phrases are lzw-compressed: %d", is_compressed);

	if(is_compressed) {
		phrase_data_uncmpr_len = de_getu32le_p(&pos);
		de_dbg(c, "decompressed len (reported): %"I64_FMT, phrase_data_uncmpr_len);
	}

	// Phrase offsets are measured from the start of the offset table.
	phrase_offset_table_pos = pos;
	phrase_offset_table_len = ((i64)d->num_phrases+1)*2;
	de_dbg(c, "offset table at %"I64_FMT", len=%"I64_FMT, phrase_offset_table_pos,
		phrase_offset_table_len);

	phrase_data_pos = phrase_offset_table_pos + phrase_offset_table_len;
	phrase_data_cmpr_len = pos1+len - phrase_data_pos; // (before any decompression)
	if(phrase_data_cmpr_len<0) goto done;
	if(!is_compressed) {
		phrase_data_uncmpr_len = phrase_data_cmpr_len;
	}

	d->phrase_info = de_mallocarray(c, (i64)d->num_phrases, sizeof(struct phrase_item));
	for(k=0; k<d->num_phrases+1; k++) {
		u32 offs;

		offs = (u32)de_getu16le_p(&pos);
		offs -= (u32)phrase_offset_table_len;

		if(k<d->num_phrases) {
			d->phrase_info[k].pos = offs;
		}
		if(k>=1) {
			d->phrase_info[k-1].len = offs - d->phrase_info[k-1].pos;
		}
	}
	if(c->debug_level>=2) {
		dump_phrase_offset_table(c, d);
	}

	de_dbg(c, "phrase data at %"I64_FMT", len=%"I64_FMT, phrase_data_pos, phrase_data_cmpr_len);

	if(is_compressed) {
		decompress_Phrases(c, d, phrase_data_pos, phrase_data_cmpr_len, phrase_data_uncmpr_len);
	}
	else {
		dbuf_copy(c->infile, phrase_data_pos, phrase_data_cmpr_len, d->phrases_data);
	}

	d->valid_Phrases_file = 1;

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x24(%rsp), %rsi
callq 0xe958c
cmpl $0x0, 0x8(%r14)
je 0x58507
leaq 0xc937a(%rip), %rsi     # 0x121747
movq %rbx, %rdi
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq $0x6, %r15
jl 0x58507
movq %r15, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ca
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0x2(%r13), %rsi
callq 0xef1ca
movq %rax, %r15
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rbp
movq %rbp, %rsi
callq 0xef1ca
cmpq $0x800, %r12            # imm = 0x800
jne 0x58464
cmpq $0x100, %r15            # imm = 0x100
je 0x58464
cmpq $0x100, %rax            # imm = 0x100
jne 0x58464
leaq 0xc9332(%rip), %rsi     # 0x12177b
movq %rbx, %rdi
movl $0x1, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0xc932b(%rip), %rsi     # 0x12178a
jmp 0x584fd
cmpq $0x100, %r15            # imm = 0x100
jne 0x584f6
leaq 0xc9303(%rip), %rsi     # 0x12177b
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movl %r12d, 0xf8(%r14)
leaq 0xc9313(%rip), %rsi     # 0x1217a5
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbp, 0x18(%rsp)
movl 0x8c(%r14), %ebp
xorl %edx, %edx
cmpl $0x11, %ebp
setge %dl
leaq 0xc92fb(%rip), %rsi     # 0x1217b5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl %ebp, 0x4(%rsp)
cmpl $0x10, %ebp
jle 0x58522
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef3a0
leaq 0xc92f2(%rip), %rsi     # 0x1217d4
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
jmp 0x5852b
leaq 0xc9267(%rip), %rsi     # 0x121764
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movl 0x24(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %r15
movl 0xf8(%r14), %ebp
leaq 0x2(,%rbp,2), %r12
leaq 0xc92af(%rip), %rsi     # 0x1217f5
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xe9226
leaq (%r15,%rbp,2), %rbp
addq $0x2, %rbp
movq 0x10(%rsp), %rax
addq %r13, %rax
subq %rbp, %rax
js 0x58507
cmpl $0x11, 0x4(%rsp)
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
cmovlq %rax, %rcx
movq %rcx, 0x8(%rsp)
movl 0xf8(%r14), %esi
movl $0x8, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, 0x100(%r14)
cmpl $-0x1, 0xf8(%r14)
je 0x585fd
xorl %r15d, %r15d
leaq 0x18(%rsp), %r13
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ed
subl %r12d, %eax
movl 0xf8(%r14), %ecx
cmpq %rcx, %r15
jae 0x585d6
movq 0x100(%r14), %rcx
movl %eax, (%rcx,%r15,8)
testq %r15, %r15
je 0x585ec
movq 0x100(%r14), %rcx
subl -0x8(%rcx,%r15,8), %eax
movl %eax, -0x4(%rcx,%r15,8)
incq %r15
movl 0xf8(%r14), %eax
incl %eax
cmpq %rax, %r15
jb 0x585b0
cmpl $0x2, (%rbx)
jl 0x5860d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x596fb
leaq 0xc91fe(%rip), %rsi     # 0x121812
movq %rbx, %rdi
movq %rbp, %rdx
movq 0x10(%rsp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9226
cmpl $0x11, 0x4(%rsp)
jl 0x58648
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq 0x8(%rsp), %r8
callq 0x59756
jmp 0x5865e
movq 0x18(%rbx), %rdi
movq 0xf0(%r14), %rcx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xefabc
movb $0x1, 0x5d(%r14)
jmp 0x58507