// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tga.c
// Function: do_read_extension_area
static void do_read_extension_area(deark *c, lctx *d, i64 pos1)
{
	i64 ext_area_size;
	i64 endpos;
	i64 k;
	int has_date;
	de_ucstring *s = NULL;
	i64 pos = pos1;
	i64 val[6];

	de_dbg(c, "extension area at %"I64_FMT, pos1);
	if(pos1 > c->infile->len - 2) {
		de_warn(c, "Bad extension area offset: %"I64_FMT, pos1);
		return;
	}

	de_dbg_indent(c, 1);

	s = ucstring_create(c);

	ext_area_size = de_getu16le_p(&pos);
	de_dbg(c, "extension area size: %"I64_FMT, ext_area_size);
	endpos = pos1+ext_area_size;

	if(pos+41>endpos) goto done;
	ucstring_empty(s);
	dbuf_read_to_ucstring(c->infile, pos, 41, s, DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	ucstring_strip_trailing_spaces(s);
	de_dbg(c, "author: \"%s\"", ucstring_getpsz_d(s));
	pos += 41;

	for(k=0; k<4; k++) {
		if(pos+81>endpos) goto done;
		ucstring_empty(s);
		dbuf_read_to_ucstring(c->infile, pos, 81, s, DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
		ucstring_strip_trailing_spaces(s);
		de_dbg(c, "comment line %d: \"%s\"", (int)k, ucstring_getpsz_d(s));
		pos += 81;
	}

	// date/time: pos=367, size=12
	if(pos+12>endpos) goto done;
	has_date = 0;
	for(k=0; k<6; k++) {
		val[k] = de_getu16le_p(&pos);
		if(val[k]!=0) has_date = 1;
	}
	if(has_date) {
		char timestamp_buf[64];

		de_make_timestamp(&d->mod_time, val[2], val[0], val[1], val[3], val[4], val[5]);
		d->mod_time.tzcode = DE_TZCODE_LOCAL;
		de_timestamp_to_string(&d->mod_time, timestamp_buf, sizeof(timestamp_buf), 0);
		de_dbg(c, "timestamp: %s", timestamp_buf);
	}

	// Job name: pos=379, size=41 (not implemented)
	pos += 41;
	// Job time: pos=420, size=6 (not implemented)
	pos += 6;

	if(pos+41>endpos) goto done;
	ucstring_empty(s);
	dbuf_read_to_ucstring(c->infile, pos, 41, s, DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	ucstring_strip_trailing_spaces(s);
	de_dbg(c, "software id: \"%s\"", ucstring_getpsz_d(s));
	pos += 41;

	if(pos+3>endpos) goto done;
	val[0] = de_getu16le_p(&pos);
	val[1] = (i64)de_getbyte_p(&pos);
	if(val[0]!=0 || val[1]!=32) {
		de_dbg(c, "software version: %u,%u,%u",
			(UI)(val[0]/100), (UI)(val[0]%100),
			(UI)val[1]);
	}

	if(pos+4>endpos) goto done;
	val[0] = de_getu32le_p(&pos);
	if(val[0]!=0) {
		de_dbg(c, "background color: 0x%08x", (UI)val[0]);
	}

	// TODO: Retain the aspect ratio. (Need sample files. Nobody seems to use this field.)
	if(pos+4>endpos) goto done;
	d->aspect_ratio_num = de_getu16le_p(&pos);
	d->aspect_ratio_den = de_getu16le_p(&pos);
	if(d->aspect_ratio_den!=0) {
		de_dbg(c, "aspect ratio: %d/%d", (int)d->aspect_ratio_num, (int)d->aspect_ratio_den);
	}

	// Gamma: pos=478, size=4 (not implemented)
	pos += 4;
	// Color correction table offset: pos=482, size=4 (not implemented)
	pos += 4;

	if(pos+4>endpos) goto done;
	d->thumbnail_offset = de_getu32le_p(&pos);
	de_dbg(c, "thumbnail image offset: %"I64_FMT, d->thumbnail_offset);

	if(pos+4>endpos) goto done;
	val[0] = de_getu32le_p(&pos);
	de_dbg(c, "scan line table offset: %"I64_FMT, val[0]);

	if(pos+1>endpos) goto done;
	d->attributes_type = de_getbyte_p(&pos);
	d->has_ext_area_attribs_type = 1;
	de_dbg(c, "attributes type: %u", (UI)d->attributes_type);
	if(d->attributes_type==4) {
		d->alpha_is_premultiplied = 1;
	}
	if(d->attributes_type==0 && d->num_attribute_bits!=0) {
		de_warn(c, "Incompatible \"number of attribute bits\" (%d) and \"attributes type\" "
			"(%u) fields. Transparency may not be handled correctly.",
			(int)d->num_attribute_bits, (UI)d->attributes_type);
	}

done:
	de_dbg_indent(c, -1);
	ucstring_destroy(s);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x9ae5f(%rip), %rsi     # 0x1c79b5
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x18(%rcx), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
jle 0x12cb91
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x9ae46(%rip), %rsi     # 0x1c79cb
movb $0x0, %al
callq 0x15bc10
jmp 0x12d17d
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x9ae1a(%rip), %rsi     # 0x1c79ea
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x48(%rbp), %rax
addq $0x29, %rax
cmpq -0x28(%rbp), %rax
jle 0x12cbf6
jmp 0x12d166
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %r9d
movl $0x29, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x40(%rbp), %rdi
callq 0x171360
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172650
movq -0xc8(%rbp), %rdi
movq %rax, %rdx
leaq 0x81cfc(%rip), %rsi     # 0x1ae951
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x29, %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
cmpq $0x4, -0x30(%rbp)
jge 0x12cd21
movq -0x48(%rbp), %rax
addq $0x51, %rax
cmpq -0x28(%rbp), %rax
jle 0x12cc8e
jmp 0x12d166
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %r9d
movl $0x51, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x40(%rbp), %rdi
callq 0x171360
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x30(%rbp), %rax
movl %eax, -0xcc(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172650
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %edx
movq %rax, %rcx
leaq 0x9ad06(%rip), %rsi     # 0x1c7a03
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x51, %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x12cc70
movq -0x48(%rbp), %rax
addq $0xc, %rax
cmpq -0x28(%rbp), %rax
jle 0x12cd34
jmp 0x12d166
movl $0x0, -0x34(%rbp)
movq $0x0, -0x30(%rbp)
cmpq $0x6, -0x30(%rbp)
jge 0x12cd8a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, -0x80(%rbp,%rax,8)
movq -0x30(%rbp), %rax
cmpq $0x0, -0x80(%rbp,%rax,8)
je 0x12cd7a
movl $0x1, -0x34(%rbp)
jmp 0x12cd7c
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x12cd43
cmpl $0x0, -0x34(%rbp)
je 0x12cdfe
movq -0x10(%rbp), %rdi
addq $0x4e8, %rdi            # imm = 0x4E8
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x68(%rbp), %r8
movq -0x60(%rbp), %r9
movq -0x58(%rbp), %rax
movq %rax, (%rsp)
callq 0x15d830
movq -0x10(%rbp), %rax
movb $0x2, 0x4e9(%rax)
movq -0x10(%rbp), %rdi
addq $0x4e8, %rdi            # imm = 0x4E8
leaq -0xc0(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
callq 0x15e130
movq -0x8(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq 0x77e50(%rip), %rsi     # 0x1a4c47
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x29, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x29, %rax
cmpq -0x28(%rbp), %rax
jle 0x12ce29
jmp 0x12d166
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %r9d
movl $0x29, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x40(%rbp), %rdi
callq 0x171360
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172650
movq -0xe0(%rbp), %rdi
movq %rax, %rdx
leaq 0x9ab91(%rip), %rsi     # 0x1c7a19
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x29, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x3, %rax
cmpq -0x28(%rbp), %rax
jle 0x12ceae
jmp 0x12d166
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x12cee9
cmpq $0x20, -0x78(%rbp)
je 0x12cf2f
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movl $0x64, %ecx
cqto
idivq %rcx
movl %eax, -0xe4(%rbp)
movq -0x80(%rbp), %rax
movl $0x64, %ecx
cqto
idivq %rcx
movq %rdx, %rax
movl -0xe4(%rbp), %edx
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %eax, %r8d
leaq 0x9ab03(%rip), %rsi     # 0x1c7a2b
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x4, %rax
cmpq -0x28(%rbp), %rax
jle 0x12cf42
jmp 0x12d166
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x165580
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x12cf76
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movl %eax, %edx
leaq 0x9aad7(%rip), %rsi     # 0x1c7a46
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x4, %rax
cmpq -0x28(%rbp), %rax
jle 0x12cf89
jmp 0x12d166
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd0(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd8(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xd8(%rax)
je 0x12d001
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movl %eax, %ecx
leaq 0x9aa65(%rip), %rsi     # 0x1c7a5f
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x4, %rax
cmpq -0x28(%rbp), %rax
jle 0x12d02c
jmp 0x12d166
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe0(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rdx
leaq 0x9aa12(%rip), %rsi     # 0x1c7a73
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x4, %rax
cmpq -0x28(%rbp), %rax
jle 0x12d07b
jmp 0x12d166
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x165580
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq 0x9a9f0(%rip), %rsi     # 0x1c7a8f
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x1, %rax
cmpq -0x28(%rbp), %rax
jle 0x12d0b9
jmp 0x12d166
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x88(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x9c(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x88(%rax), %edx
leaq 0x9a9b1(%rip), %rsi     # 0x1c7aab
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x4, %eax
jne 0x12d11c
movq -0x10(%rbp), %rax
movb $0x1, 0x89(%rax)
movq -0x10(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x0, %eax
jne 0x12d164
movq -0x10(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x12d164
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movzbl 0x88(%rax), %ecx
leaq 0x9a962(%rip), %rsi     # 0x1c7abf
movb $0x0, %al
callq 0x15bc10
jmp 0x12d166
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x40(%rbp), %rdi
callq 0x171750
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)