// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tga.c
// Function: should_use_alpha_channel
static int should_use_alpha_channel(deark *c, lctx *d, struct tgaimginfo *imginfo,
	int has_alpha_0, int has_alpha_partial, int has_alpha_255)
{
	const char *prefix = "";

	if(imginfo->is_thumb) {
		prefix = "[thumbnail] ";
	}

	if(d->trans_setting==TRANS_REMOVE) return 0;

	if(d->has_ext_area_attribs_type) {
		// attributes_type==0 technically also means there is no transparency,
		// but this cannot be trusted.
		if(d->attributes_type!=0 && d->attributes_type!=3 &&
			d->attributes_type!=4)
		{
			return 0;
		}
	}

	if(d->pixel_depth!=32 || d->color_type!=TGA_CLRTYPE_TRUECOLOR) {
		return 0;
	}

	if(d->num_attribute_bits!=0 && d->num_attribute_bits!=8) {
		if(!imginfo->is_thumb) { // Hack to suppress duplicate warning
			de_warn(c, "%d-bit attribute channel not supported. Transparency disabled.",
				(int)d->num_attribute_bits);
		}
		return 0;
	}

	if(d->trans_setting==TRANS_RESPECT || d->trans_setting==TRANS_ASSOC) {
		return 1;
	}

	if(d->has_ext_area_attribs_type) {
		if(d->attributes_type==3 && d->num_attribute_bits==8) {
			// Alpha channel seems to be labeled correctly.
			// Trust it.
			return 1;
		}
		else if(d->attributes_type==4 && d->num_attribute_bits==8) {
			// Premultiplied alpha
			return 1;
		}
	}

	if(has_alpha_partial || (has_alpha_0 && has_alpha_255)) {
		if(d->num_attribute_bits==0) {
			de_warn(c, "%sDetected likely alpha channel. Enabling transparency, even though "
				"the image is labeled as non-transparent.", prefix);
		}
		return 1;
	}
	else if(has_alpha_0) { // All 0x00
		if(d->num_attribute_bits!=0) {
			de_warn(c, "%sNon-visible image detected. Disabling transparency.", prefix);
		}
		else {
			de_dbg(c, "potential alpha channel ignored: all 0 bits");
		}
		return 0;
	}
	else { // All 0xff
		de_dbg(c, "potential alpha channel is moot: all 1 bits");
		return 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
leaq 0xa36ba(%rip), %rax     # 0x1d0bd0
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x12d52f
leaq 0x9a73f(%rip), %rax     # 0x1c7c6a
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x65, %eax
jne 0x12d548
movl $0x0, -0x4(%rbp)
jmp 0x12d732
movq -0x18(%rbp), %rax
cmpl $0x0, 0x9c(%rax)
je 0x12d593
movq -0x18(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x0, %eax
je 0x12d591
movq -0x18(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x3, %eax
je 0x12d591
movq -0x18(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x4, %eax
je 0x12d591
movl $0x0, -0x4(%rbp)
jmp 0x12d732
jmp 0x12d593
movq -0x18(%rbp), %rax
cmpq $0x20, 0x70(%rax)
jne 0x12d5ab
movq -0x18(%rbp), %rax
cmpl $0x2, 0xa4(%rax)
je 0x12d5b7
movl $0x0, -0x4(%rbp)
jmp 0x12d732
movq -0x18(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x12d608
movq -0x18(%rbp), %rax
cmpq $0x8, 0x80(%rax)
je 0x12d608
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x12d5fc
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rax
movl %eax, %edx
leaq 0x9a682(%rip), %rsi     # 0x1c7c77
movb $0x0, %al
callq 0x15bc10
movl $0x0, -0x4(%rbp)
jmp 0x12d732
movq -0x18(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x66, %eax
je 0x12d622
movq -0x18(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x67, %eax
jne 0x12d62e
movl $0x1, -0x4(%rbp)
jmp 0x12d732
movq -0x18(%rbp), %rax
cmpl $0x0, 0x9c(%rax)
je 0x12d693
movq -0x18(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x3, %eax
jne 0x12d665
movq -0x18(%rbp), %rax
cmpq $0x8, 0x80(%rax)
jne 0x12d665
movl $0x1, -0x4(%rbp)
jmp 0x12d732
movq -0x18(%rbp), %rax
movzbl 0x88(%rax), %eax
cmpl $0x4, %eax
jne 0x12d68f
movq -0x18(%rbp), %rax
cmpq $0x8, 0x80(%rax)
jne 0x12d68f
movl $0x1, -0x4(%rbp)
jmp 0x12d732
jmp 0x12d691
jmp 0x12d693
cmpl $0x0, -0x28(%rbp)
jne 0x12d6a5
cmpl $0x0, -0x24(%rbp)
je 0x12d6d2
cmpl $0x0, -0x2c(%rbp)
je 0x12d6d2
movq -0x18(%rbp), %rax
cmpq $0x0, 0x80(%rax)
jne 0x12d6c9
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x9a5f4(%rip), %rsi     # 0x1c7cb6
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0x4(%rbp)
jmp 0x12d732
cmpl $0x0, -0x24(%rbp)
je 0x12d719
movq -0x18(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x12d6fe
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x9a62e(%rip), %rsi     # 0x1c7d23
movb $0x0, %al
callq 0x15bc10
jmp 0x12d710
movq -0x10(%rbp), %rdi
leaq 0x9a650(%rip), %rsi     # 0x1c7d59
movb $0x0, %al
callq 0x15b0d0
movl $0x0, -0x4(%rbp)
jmp 0x12d732
movq -0x10(%rbp), %rdi
leaq 0x9a661(%rip), %rsi     # 0x1c7d85
movb $0x0, %al
callq 0x15b0d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)