// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tga.c
// Function: de_run_tga
static void de_run_tga(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;
	dbuf *unc_pixels = NULL;
	int saved_indent_level;
	i64 rowspan_tmp;
	const char *opt_trans_str;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_malloc(c, sizeof(lctx));
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_ASCII);
	d->trans_setting = TRANS_AUTO;

	opt_trans_str = de_get_ext_option(c, "tga:trans");
	if(opt_trans_str) {
		if(!de_strcmp(opt_trans_str, "auto")) {
			d->trans_setting = TRANS_AUTO;
		}
		else if(!de_strcmp(opt_trans_str, "0")) {
			d->trans_setting = TRANS_REMOVE;
		}
		else if(!de_strcmp(opt_trans_str, "1")) {
			d->trans_setting = TRANS_RESPECT;
		}
		else if(!de_strcmp(opt_trans_str, "2")) {
			d->trans_setting = TRANS_ASSOC;
		}
	}

	detect_file_format(c, d);

	if(d->file_format==FMT_VST) {
		de_declare_fmt(c, "TrueVista");
	}
	else {
		de_declare_fmt(c, "TGA");
	}

	d->thumbnail_image.is_thumb = 1;

	pos = 0;

	if(d->file_format==FMT_VST) {
		if(!do_read_vst_headers(c, d)) goto done;
	}
	else {
		if(!do_read_tga_headers(c, d)) goto done;
	}

	if(d->trans_setting==TRANS_ASSOC) {
		d->alpha_is_premultiplied = 1;
	}

	pos += 18;

	de_dbg(c, "image/color data at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	read_image_id(c, d, pos);
	pos += d->id_field_len;

	if(d->color_map_type!=0) {
		d->bytes_per_pal_entry = (d->cmap_depth+7)/8;
		d->pal_size_in_bytes = d->cmap_length * d->bytes_per_pal_entry;
		de_dbg(c, "color map at %"I64_FMT" (%d colors, %d bytes)", pos,
			(int)d->cmap_length, (int)d->pal_size_in_bytes);

		de_dbg_indent(c, 1);
		if(!do_read_palette(c, d, pos)) goto done;
		de_dbg_indent(c, -1);

		pos += d->pal_size_in_bytes;
	}

	de_dbg(c, "bitmap at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	d->bytes_per_pixel = ((d->pixel_depth+7)/8);
	if(d->pixel_depth==1) {
		rowspan_tmp = (d->main_image.width+7)/8;
	}
	else {
		rowspan_tmp = d->main_image.width * d->bytes_per_pixel;
	}
	d->main_image.img_size_in_bytes = d->main_image.height * rowspan_tmp;

	if(d->color_type!=TGA_CLRTYPE_PALETTE && d->color_type!=TGA_CLRTYPE_TRUECOLOR &&
		d->color_type!=TGA_CLRTYPE_GRAYSCALE)
	{
		de_err(c, "Unsupported color type (%d: %s)", (int)d->color_type, d->clrtype_name);
		goto done;
	}

	if( (d->color_type==TGA_CLRTYPE_PALETTE && d->pixel_depth==8) ||
		(d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==15) ||
		(d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==16) ||
		(d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==24) ||
		(d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==32) ||
		(d->color_type==TGA_CLRTYPE_GRAYSCALE && d->pixel_depth==1) ||
		(d->color_type==TGA_CLRTYPE_GRAYSCALE && d->pixel_depth==8) )
	{
		;
	}
	else {
		de_err(c, "Unsupported TGA image type (%s, depth=%d)", d->clrtype_name,
			(int)d->pixel_depth);
		goto done;
	}

	if(d->cmpr_type==TGA_CMPR_RLE) {
		if(d->pixel_depth<8) {
			de_err(c, "RLE compression not supported when depth (%d) is less than 8",
				(int)d->pixel_depth);
			goto done;
		}
		unc_pixels = dbuf_create_membuf(c, d->main_image.img_size_in_bytes, 1);
		dbuf_enable_wbuffer(unc_pixels);
		if(!do_decode_rle(c, d, pos, unc_pixels)) goto done;
	}
	else if(d->cmpr_type==TGA_CMPR_NONE) {
		unc_pixels = dbuf_open_input_subfile(c->infile, pos, d->main_image.img_size_in_bytes);
	}
	else {
		de_err(c, "Unsupported image or compression type");
		goto done;
	}

	do_decode_image(c, d, &d->main_image, unc_pixels, NULL, 0);

	de_dbg_indent(c, -2);

	if(d->thumbnail_offset!=0) {
		do_decode_thumbnail(c, d);
	}

done:
	dbuf_close(unc_pixels);
	de_dbg_indent_restore(c, saved_indent_level);
	de_free(c, d);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x4f8, %esi            # imm = 0x4F8
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movb $0x64, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq 0x9cb06(%rip), %rsi     # 0x1c7743
callq 0x15cb30
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x12acd3
movq -0x40(%rbp), %rdi
leaq 0x71917(%rip), %rsi     # 0x19c573
callq 0x191a0
cmpl $0x0, %eax
jne 0x12ac70
movq -0x18(%rbp), %rax
movb $0x64, 0x10(%rax)
jmp 0x12acd1
movq -0x40(%rbp), %rdi
leaq 0x99a85(%rip), %rsi     # 0x1c4700
callq 0x191a0
cmpl $0x0, %eax
jne 0x12ac8f
movq -0x18(%rbp), %rax
movb $0x65, 0x10(%rax)
jmp 0x12accf
movq -0x40(%rbp), %rdi
leaq 0x941e2(%rip), %rsi     # 0x1bee7c
callq 0x191a0
cmpl $0x0, %eax
jne 0x12acae
movq -0x18(%rbp), %rax
movb $0x66, 0x10(%rax)
jmp 0x12accd
movq -0x40(%rbp), %rdi
leaq 0xa23b9(%rip), %rsi     # 0x1cd072
callq 0x191a0
cmpl $0x0, %eax
jne 0x12accb
movq -0x18(%rbp), %rax
movb $0x67, 0x10(%rax)
jmp 0x12accd
jmp 0x12accf
jmp 0x12acd1
jmp 0x12acd3
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x12b400
movq -0x18(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x12acfc
movq -0x8(%rbp), %rdi
leaq 0x9ca58(%rip), %rsi     # 0x1c774d
callq 0x15e380
jmp 0x12ad0c
movq -0x8(%rbp), %rdi
leaq 0x8b467(%rip), %rsi     # 0x1b616e
callq 0x15e380
movq -0x18(%rbp), %rax
movl $0x1, 0x50(%rax)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x12ad42
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x12b4f0
cmpl $0x0, %eax
jne 0x12ad40
jmp 0x12b163
jmp 0x12ad5b
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x12b6e0
cmpl $0x0, %eax
jne 0x12ad59
jmp 0x12b163
jmp 0x12ad5b
movq -0x18(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x67, %eax
jne 0x12ad73
movq -0x18(%rbp), %rax
movb $0x1, 0x89(%rax)
movq -0x20(%rbp), %rax
addq $0x12, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x9c9c9(%rip), %rsi     # 0x1c7757
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x12bb60
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x11(%rax), %eax
cmpl $0x0, %eax
je 0x12ae8f
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x18(%rbp), %rax
imulq 0xc0(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xc8(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0xc8(%rax), %rax
movl %eax, %r8d
leaq 0x9c931(%rip), %rsi     # 0x1c776f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x12bc00
cmpl $0x0, %eax
jne 0x12ae6e
jmp 0x12b163
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0xc8(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x97069(%rip), %rsi     # 0x1c1f07
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x18(%rbp), %rax
cmpq $0x1, 0x70(%rax)
jne 0x12aefe
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x38(%rbp)
jmp 0x12af16
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
imulq 0xb8(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
imulq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, 0xa4(%rax)
je 0x12af7e
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa4(%rax)
je 0x12af7e
movq -0x18(%rbp), %rax
cmpl $0x3, 0xa4(%rax)
je 0x12af7e
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0xa4(%rax), %edx
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rcx
leaq 0x9c824(%rip), %rsi     # 0x1c7796
movb $0x0, %al
callq 0x15bfb0
jmp 0x12b163
movq -0x18(%rbp), %rax
cmpl $0x1, 0xa4(%rax)
jne 0x12af9a
movq -0x18(%rbp), %rax
cmpq $0x8, 0x70(%rax)
je 0x12b02a
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa4(%rax)
jne 0x12afb2
movq -0x18(%rbp), %rax
cmpq $0xf, 0x70(%rax)
je 0x12b02a
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa4(%rax)
jne 0x12afca
movq -0x18(%rbp), %rax
cmpq $0x10, 0x70(%rax)
je 0x12b02a
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa4(%rax)
jne 0x12afe2
movq -0x18(%rbp), %rax
cmpq $0x18, 0x70(%rax)
je 0x12b02a
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa4(%rax)
jne 0x12affa
movq -0x18(%rbp), %rax
cmpq $0x20, 0x70(%rax)
je 0x12b02a
movq -0x18(%rbp), %rax
cmpl $0x3, 0xa4(%rax)
jne 0x12b012
movq -0x18(%rbp), %rax
cmpq $0x1, 0x70(%rax)
je 0x12b02a
movq -0x18(%rbp), %rax
cmpl $0x3, 0xa4(%rax)
jne 0x12b02c
movq -0x18(%rbp), %rax
cmpq $0x8, 0x70(%rax)
jne 0x12b02c
jmp 0x12b058
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movl %eax, %ecx
leaq 0x9c76a(%rip), %rsi     # 0x1c77b6
movb $0x0, %al
callq 0x15bfb0
jmp 0x12b163
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa0(%rax)
jne 0x12b0d5
movq -0x18(%rbp), %rax
cmpq $0x8, 0x70(%rax)
jge 0x12b091
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movl %eax, %edx
leaq 0x9c75b(%rip), %rsi     # 0x1c77e0
movb $0x0, %al
callq 0x15bfb0
jmp 0x12b163
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x12bd60
cmpl $0x0, %eax
jne 0x12b0d3
jmp 0x12b163
jmp 0x12b117
movq -0x18(%rbp), %rax
cmpl $0x1, 0xa0(%rax)
jne 0x12b101
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x1691b0
movq %rax, -0x28(%rbp)
jmp 0x12b115
movq -0x8(%rbp), %rdi
leaq 0x9c711(%rip), %rsi     # 0x1c781d
movb $0x0, %al
callq 0x15bfb0
jmp 0x12b163
jmp 0x12b117
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x18, %rdx
movq -0x28(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x12be80
movq -0x8(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x15b560
movq -0x18(%rbp), %rax
cmpq $0x0, 0xe0(%rax)
je 0x12b161
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x12c490
jmp 0x12b163
movq -0x28(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x15b5a0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)