// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/drhalo.c
// Function: do_read_pal_file
static int do_read_pal_file(deark *c, lctx *d, const char *palfn)
{
	dbuf *palfile = NULL;
	i64 pos;
	i64 sig;
	i64 filever;
	i64 datasize;
	i64 k, z;
	i64 num_entries;
	i64 maxidx;
	i64 maxsamp[3];
	UI board_id;
	UI graphics_mode;
	u8 filetype;
	u8 filesubtype;
	i64 osamp[3];
	u8 samp[3];
	int retval = 0;
	char tmps[64];

	de_dbg(c, "palette file");
	de_dbg_indent(c, 1);

	palfile = dbuf_open_input_file(c, palfn);
	if(!palfile) {
		goto done;
	}

	sig = dbuf_getu16le(palfile, 0);
	filever = dbuf_getu16le(palfile, 2);
	de_dbg(c, "file version: %d", (int)filever);
	datasize = dbuf_getu16le(palfile, 4);
	de_dbg(c, "data size: %"I64_FMT, datasize);
	filetype = dbuf_getbyte(palfile, 6);
	de_dbg(c, "file type: 0x%02x", (UI)filever);
	filesubtype = dbuf_getbyte(palfile, 7);
	de_dbg(c, "file subtype: 0x%02x", (UI)filesubtype);

	if(sig!=0x4841 /* "HA" */ || filetype!=0x0a) {
		de_err(c, "Invalid palette file");
		goto done;
	}

	board_id = (UI)dbuf_getu16le(palfile, 8);
	de_dbg(c, "board id: 0x%04x", board_id);
	graphics_mode = (UI)dbuf_getu16le(palfile, 10);
	de_dbg(c, "graphics mode: 0x%04x", graphics_mode);

	if(filesubtype!=0) {
		de_warn(c, "Hardware-specific palettes are not supported");
		retval = 1;
		goto done;
	}

	maxidx = dbuf_getu16le(palfile, 0x0c);
	de_dbg(c, "maxidx: %u", (UI)maxidx);

	for(k=0; k<3; k++) {
		maxsamp[k] = dbuf_getu16le(palfile, 0x0e + 2*k);
		de_dbg(c, "maxsamp[%d]: %u", (int)k, (UI)maxsamp[k]);
		if(maxsamp[k]<1) maxsamp[k]=1;
	}

	pos = 0x14;

	pos += 20; // Skip palette name  TODO: Display this

	num_entries = maxidx+1;
	if(num_entries>256) num_entries=256;

	for(k=0; k<num_entries; k++) {
		// If we imagine the palette file being split into 512-byte chunks, a
		// (6-byte) palette entry is not allowed to cross a chunk boundary.
		// If an entry would do so, it instead starts at the beginning of the
		// next chunk.
		if((pos%512) > 506) {
			pos = de_pad_to_n(pos, 512);
		}

		for(z=0; z<3; z++) {
			osamp[z] = dbuf_getu16le_p(palfile, &pos);
			// I think portable palette samples are always in the range 0-255,
			// regardless of the maxsamp fields.
			if(maxsamp[z]>255) {
				samp[z] = de_scale_n_to_255(maxsamp[z], osamp[z]);
			}
			else {
				samp[z] = (u8)osamp[z];
			}
		}
		d->pal[k] = DE_MAKE_RGB(samp[0], samp[1], samp[2]);

		de_snprintf(tmps, sizeof(tmps), "(%5d,%5d,%5d) "DE_CHAR_RIGHTARROW" ",
			(int)osamp[0], (int)osamp[1], (int)osamp[2]);
		de_dbg_pal_entry2(c, k, d->pal[k], tmps, NULL, NULL);
	}

	d->have_palfile = 1;
	retval = 1;
done:
	dbuf_close(palfile);
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0xd984d(%rip), %rsi     # 0x11a71c
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf1114
movq %rax, %r14
testq %rax, %rax
je 0x411b4
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0xeebc8
movq %rax, %r15
movl $0x2, %esi
movq %r14, %rdi
callq 0xeebc8
movq %rax, %rbp
leaq 0xf3c4b(%rip), %rsi     # 0x134b6f
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x4, %esi
movq %r14, %rdi
callq 0xeebc8
leaq 0xf0e06(%rip), %rsi     # 0x131d4a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movl $0x6, %esi
movq %r14, %rdi
callq 0xeead3
movl %eax, %r13d
leaq 0xd42bd(%rip), %rsi     # 0x115225
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x7, %esi
movq %r14, %rdi
callq 0xeead3
movl %eax, %ebp
movzbl %al, %edx
leaq 0xd9772(%rip), %rsi     # 0x11a6ff
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpq $0x4841, %r15           # imm = 0x4841
jne 0x41001
cmpb $0xa, %r13b
jne 0x41001
movl $0x8, %esi
movq %r14, %rdi
callq 0xeebc8
leaq 0xd976f(%rip), %rsi     # 0x11a729
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl $0xa, %esi
movq %r14, %rdi
callq 0xeebc8
leaq 0xd9760(%rip), %rsi     # 0x11a73a
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
testb %bpl, %bpl
je 0x41017
leaq 0xd975e(%rip), %rsi     # 0x11a750
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0x411ae
leaq 0xd970c(%rip), %rsi     # 0x11a714
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x411b4
movl $0xc, %esi
movq %r14, %rdi
callq 0xeebc8
leaq 0xd9752(%rip), %rsi     # 0x11a77d
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl $0xe, %r13d
leaq 0xd973f(%rip), %rbp     # 0x11a788
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rsi
callq 0xeebc8
movq %rax, %r15
movq %rax, 0x8(%rsp,%r13,4)
movq %rbx, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8617
testq %r15, %r15
jg 0x41080
movq $0x1, 0x8(%rsp,%r13,4)
addq $0x2, %r13
incl %r12d
cmpq $0x14, %r13
jne 0x4104c
movq $0x28, 0x10(%rsp)
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
js 0x411a5
movl $0xff, %eax
cmpq %rax, %rcx
cmovbq %rcx, %rax
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %r12
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
leaq 0x1ff(%rdi), %rax
testq %rdi, %rdi
cmovnsq %rdi, %rax
andq $-0x200, %rax           # imm = 0xFE00
movq %rdi, %rcx
subq %rax, %rcx
cmpq $0x1fb, %rcx            # imm = 0x1FB
jl 0x410f4
movl $0x200, %esi            # imm = 0x200
callq 0xe9cb4
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movq %r14, %rdi
movq %r12, %rsi
callq 0xeebeb
movq %rax, 0x20(%rsp,%rbp,8)
movq 0x40(%rsp,%rbp,8), %rdi
cmpq $0x100, %rdi            # imm = 0x100
jl 0x4111c
movq %rax, %rsi
callq 0xec7dc
movb %al, 0x5(%rsp,%rbp)
incq %rbp
cmpq $0x3, %rbp
jne 0x410f6
movzbl 0x5(%rsp), %eax
shll $0x10, %eax
movzbl 0x6(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x7(%rsp), %eax
orl %ecx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movq 0x18(%rsp), %rbp
movl %eax, 0x18(%rbp,%r13,4)
movl 0x20(%rsp), %ecx
movl 0x28(%rsp), %r8d
movl 0x30(%rsp), %r9d
movl $0x40, %esi
leaq 0x60(%rsp), %r15
movq %r15, %rdi
leaq 0xd9625(%rip), %rdx     # 0x11a798
xorl %eax, %eax
callq 0xe8193
movl 0x18(%rbp,%r13,4), %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
leaq 0x1(%r13), %rax
cmpq 0x8(%rsp), %r13
movq %rax, %r13
jne 0x410bd
movq 0x18(%rsp), %rax
movb $0x1, 0x10(%rax)
movl $0x1, %r12d
movq %r14, %rdi
callq 0xf052b
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq