// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/png.c
// Function: handler_text
static void handler_text(deark *c, lctx *d, struct handler_params *hp)
{
	i64 pos;
	i64 endpos;
	i64 field_bytes_consumed;
	int is_compressed = 0;
	de_encoding encoding;
	int ret;
	struct text_chunk_ctx tcc;

	de_zeromem(&tcc, sizeof(struct text_chunk_ctx));

	endpos = hp->dpos+hp->dlen;
	pos = hp->dpos;

	// Keyword
	ret = do_text_field(c, d, &tcc, FIELD_KEYWORD, pos, endpos-pos,
		1, 0, DE_ENCODING_LATIN1, &field_bytes_consumed);
	if(!ret) goto done;
	pos += field_bytes_consumed;
	pos += 1;

	// Compression flag
	if(hp->chunk4cc.id==CODE_iTXt) {
		is_compressed = (int)de_getbyte(pos++);
		de_dbg(c, "compression flag: %d", (int)is_compressed);
	}
	else if(hp->chunk4cc.id==CODE_zTXt) {
		is_compressed = 1;
	}

	// Compression method
	if(hp->chunk4cc.id==CODE_zTXt || hp->chunk4cc.id==CODE_iTXt) {
		u8 cmpr_method;
		cmpr_method = de_getbyte(pos++);
		if(is_compressed && cmpr_method!=0) {
			de_warn(c, "Unsupported text compression type: %d", (int)cmpr_method);
			goto done;
		}
	}

	if(hp->chunk4cc.id==CODE_iTXt) {
		// Language tag
		ret = do_text_field(c, d, &tcc, FIELD_LANG, pos, endpos-pos,
			1, 0, DE_ENCODING_ASCII, &field_bytes_consumed);
		if(!ret) goto done;
		pos += field_bytes_consumed;
		pos += 1;

		// Translated keyword
		ret = do_text_field(c, d, &tcc, FIELD_XKEYWORD, pos, endpos-pos,
			1, 0, DE_ENCODING_UTF8, &field_bytes_consumed);
		if(!ret) goto done;
		pos += field_bytes_consumed;
		pos += 1;
	}

	if(hp->chunk4cc.id==CODE_iTXt)
		encoding = DE_ENCODING_UTF8;
	else
		encoding = DE_ENCODING_LATIN1;

	do_text_field(c, d, &tcc, FIELD_MAIN, pos, endpos-pos,
		0, is_compressed, encoding, &field_bytes_consumed);

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r12
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq (%rdx), %r15
movq 0x8(%rdx), %r14
movq 0x18(%rdi), %rcx
subq $0x8, %rsp
leaq 0x10(%rsp), %rax
movl $0x1, %edx
movq %r15, %r8
movq %r14, %r9
pushq %rax
pushq $0x5
pushq $0x1
callq 0xb66ce
addq $0x20, %rsp
movl %eax, %ebp
xorl %r13d, %r13d
xorl %edi, %edi
callq 0xf052b
testl %ebp, %ebp
je 0xb602d
addq %r15, %r14
movq 0x8(%rsp), %rbp
leaq (%r15,%rbp), %rsi
incq %rsi
movl 0x1c(%r12), %eax
cmpl $0x7a545874, %eax       # imm = 0x7A545874
je 0xb5e3e
cmpl $0x69545874, %eax       # imm = 0x69545874
jne 0xb5e49
addq %r15, %rbp
movq 0x18(%rbx), %rdi
addq $0x2, %rbp
callq 0xeead3
movzbl %al, %edx
leaq 0x81c6b(%rip), %rsi     # 0x137a95
movq %rbx, %rdi
movq %rdx, %r13
xorl %eax, %eax
callq 0xe8617
movl 0x1c(%r12), %eax
jmp 0xb5e4c
movl $0x1, %r13d
movq %rsi, %rbp
jmp 0xb5e64
movq %rsi, %rbp
cmpl $0x7a545874, %eax       # imm = 0x7A545874
je 0xb5e64
movl $0x5, %r15d
cmpl $0x69545874, %eax       # imm = 0x69545874
jne 0xb5f79
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeead3
testl %r13d, %r13d
je 0xb5e9b
testb %al, %al
je 0xb5e9b
movzbl %al, %edx
leaq 0x81c27(%rip), %rsi     # 0x137aaa
movq %rbx, %rdi
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8faa
leaq 0x1(%rbp), %r8
movl $0x5, %r15d
cmpl $0x69545874, 0x1c(%r12) # imm = 0x69545874
jne 0xb5f76
movq %r13, 0x10(%rsp)
movq %r14, %r9
subq %r8, %r9
movq 0x18(%rbx), %rcx
subq $0x8, %rsp
movl $0x1, %eax
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
movl $0x2, %edx
leaq 0x10(%rsp), %r10
pushq %r10
pushq %rax
pushq %rax
callq 0xb66ce
addq $0x20, %rsp
movl %eax, %r15d
xorl %edi, %edi
callq 0xf052b
testl %r15d, %r15d
je 0xb602d
movq 0x8(%rsp), %rax
leaq (%rax,%rbp), %r15
addq $0x2, %r15
movq %r14, %r9
subq %r15, %r9
movq 0x18(%rbx), %rcx
subq $0x8, %rsp
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
movl $0x3, %edx
movq %r15, %r8
leaq 0x10(%rsp), %rax
pushq %rax
pushq $0x2
pushq $0x1
callq 0xb66ce
addq $0x20, %rsp
movl %eax, %ebp
xorl %edi, %edi
callq 0xf052b
testl %ebp, %ebp
je 0xb602d
movq 0x8(%rsp), %rax
leaq (%r15,%rax), %rbp
incq %rbp
xorl %eax, %eax
cmpl $0x69545874, 0x1c(%r12) # imm = 0x69545874
setne %al
leal (%rax,%rax,2), %r15d
addl $0x2, %r15d
movq 0x10(%rsp), %r13
jmp 0xb5f79
movq %r8, %rbp
subq %rbp, %r14
testl %r13d, %r13d
je 0xb5ff4
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq 0x18(%rbx), %rdi
xorl %eax, %eax
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x7(%rcx)
sete %al
subq $0x8, %rsp
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
callq 0x100594
addq $0x10, %rsp
testl %eax, %eax
je 0xb6025
movq 0x18(%r12), %r9
subq $0x8, %rsp
leaq 0x48(%rsp), %rax
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
movl $0x4, %edx
movq %r12, %rcx
xorl %r8d, %r8d
pushq %rax
pushq %r15
pushq $0x0
callq 0xb66ce
addq $0x20, %rsp
jmp 0xb6025
movq 0x18(%rbx), %rcx
subq $0x8, %rsp
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
movl $0x4, %edx
movq %rbp, %r8
movq %r14, %r9
leaq 0x10(%rsp), %rax
pushq %rax
pushq %r15
pushq $0x0
callq 0xb66ce
addq $0x20, %rsp
xorl %r12d, %r12d
movq %r12, %rdi
callq 0xf052b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq