// File: /workspace/github_c_makefile_star2_O3/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 0x14(%rsp), %rsi
callq 0xe8a1c
cmpl $0x0, 0x8(%r14)
je 0x58307
leaq 0xc968f(%rip), %rsi     # 0x121853
movq %rbx, %rdi
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x6, %r15
jl 0x58307
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeebc8
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0x2(%r13), %rsi
callq 0xeebc8
movq %rax, %rbp
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
movq %rsi, 0x8(%rsp)
callq 0xeebc8
cmpq $0x800, %r12            # imm = 0x800
jne 0x58258
cmpq $0x100, %rbp            # imm = 0x100
je 0x58258
cmpq $0x100, %rax            # imm = 0x100
jne 0x58258
leaq 0xc964a(%rip), %rsi     # 0x121887
movq %rbx, %rdi
movl $0x1, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0xc9643(%rip), %rsi     # 0x121896
jmp 0x582fd
cmpq $0x100, %rbp            # imm = 0x100
jne 0x582f6
leaq 0xc961b(%rip), %rsi     # 0x121887
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movl %r12d, 0xf8(%r14)
leaq 0xc962b(%rip), %rsi     # 0x1218b1
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %r12
movq %r12, 0x20(%rsp)
movl 0x8c(%r14), %ebp
xorl %edx, %edx
cmpl $0x11, %ebp
setge %dl
leaq 0xc960e(%rip), %rsi     # 0x1218c1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl %ebp, 0x10(%rsp)
cmpl $0x10, %ebp
jle 0x58322
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0xeed83
leaq 0xc9602(%rip), %rsi     # 0x1218e0
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%r12), %rbp
jmp 0x5832e
leaq 0xc9573(%rip), %rsi     # 0x121870
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x18(%rsp)
movq %r12, %rbp
movl 0xf8(%r14), %eax
movq %rax, 0x8(%rsp)
leaq 0x2(,%rax,2), %r12
leaq 0xc95b8(%rip), %rsi     # 0x121901
movq %rbx, %rdi
movq %rbp, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %rax
leaq 0x2(,%rax,2), %rax
addq %rbp, %rax
addq %r13, %r15
subq %rax, %r15
js 0x58307
movq %rax, 0x8(%rsp)
movl 0xf8(%r14), %esi
movl $0x8, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, 0x100(%r14)
cmpl $-0x1, 0xf8(%r14)
je 0x583e5
xorl %ebp, %ebp
leaq 0x20(%rsp), %r13
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeebeb
subl %r12d, %eax
movl 0xf8(%r14), %ecx
cmpq %rcx, %rbp
jae 0x583c7
movq 0x100(%r14), %rdx
movl %eax, (%rdx,%rbp,8)
testq %rbp, %rbp
je 0x583db
movq 0x100(%r14), %rdx
subl -0x8(%rdx,%rbp,8), %eax
movl %eax, -0x4(%rdx,%rbp,8)
incq %rbp
incl %ecx
cmpq %rcx, %rbp
jb 0x583a2
cmpl $0x2, (%rbx)
jl 0x583f5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x594fb
leaq 0xc9522(%rip), %rsi     # 0x12191e
movq %rbx, %rdi
movq 0x8(%rsp), %r12
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
cmpl $0x11, 0x10(%rsp)
movq 0x18(%rsp), %r8
jl 0x58430
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x59556
jmp 0x58446
movq 0x18(%rbx), %rdi
movq 0xf0(%r14), %rcx
movq %r12, %rsi
movq %r15, %rdx
callq 0xef481
movb $0x1, 0x5d(%r14)
jmp 0x58307