// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0xb8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x14e2f0(%rip), %rsi    # 0x1a6354
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x168de0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x58096
jmp 0x58460
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movl $0x2, %esi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x1684ec(%rip), %rsi    # 0x1c05b6
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
movl $0x4, %esi
callq 0x165280
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x165649(%rip), %rsi    # 0x1bd73b
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
movl $0x6, %esi
callq 0x165060
movb %al, -0x89(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x148eab(%rip), %rsi    # 0x1a0fc9
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
movl $0x7, %esi
callq 0x165060
movb %al, -0x8a(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x8a(%rbp), %edx
leaq 0x14e1ec(%rip), %rsi    # 0x1a6337
movb $0x0, %al
callq 0x15b0d0
cmpq $0x4841, -0x30(%rbp)    # imm = 0x4841
jne 0x58168
movzbl -0x89(%rbp), %eax
cmpl $0xa, %eax
je 0x5817f
movq -0x8(%rbp), %rdi
leaq 0x14e1d9(%rip), %rsi    # 0x1a634c
movb $0x0, %al
callq 0x15bfb0
jmp 0x58460
movq -0x20(%rbp), %rdi
movl $0x8, %esi
callq 0x165280
movl %eax, -0x84(%rbp)
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %edx
leaq 0x14e1bd(%rip), %rsi    # 0x1a6361
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
movl $0xa, %esi
callq 0x165280
movl %eax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movl -0x88(%rbp), %edx
leaq 0x14e1a2(%rip), %rsi    # 0x1a6372
movb $0x0, %al
callq 0x15b0d0
movzbl -0x8a(%rbp), %eax
cmpl $0x0, %eax
je 0x58204
movq -0x8(%rbp), %rdi
leaq 0x14e19a(%rip), %rsi    # 0x1a6388
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0xb8(%rbp)
jmp 0x58460
movq -0x20(%rbp), %rdi
movl $0xc, %esi
callq 0x165280
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %edx
leaq 0x14e18e(%rip), %rsi    # 0x1a63b5
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x48(%rbp)
cmpq $0x3, -0x48(%rbp)
jge 0x582a9
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rsi
shlq %rsi
addq $0xe, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, -0x80(%rbp,%rax,8)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
movq -0x48(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
movl %eax, %ecx
leaq 0x14e147(%rip), %rsi    # 0x1a63c0
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
cmpq $0x1, -0x80(%rbp,%rax,8)
jge 0x58299
movq -0x48(%rbp), %rax
movq $0x1, -0x80(%rbp,%rax,8)
jmp 0x5829b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x58236
movq $0x14, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x28(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
cmpq $0x100, -0x58(%rbp)     # imm = 0x100
jle 0x582db
movq $0x100, -0x58(%rbp)     # imm = 0x100
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x5844e
movq -0x28(%rbp), %rax
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
cmpq $0x1fa, %rdx            # imm = 0x1FA
jle 0x5831a
movq -0x28(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x15ce90
movq %rax, -0x28(%rbp)
movq $0x0, -0x50(%rbp)
cmpq $0x3, -0x50(%rbp)
jge 0x583ad
movq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, -0xb0(%rbp,%rax,8)
movq -0x50(%rbp), %rax
cmpq $0xff, -0x80(%rbp,%rax,8)
jle 0x58381
movq -0x50(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rdi
movq -0x50(%rbp), %rax
movq -0xb0(%rbp,%rax,8), %rsi
callq 0x1617b0
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, -0xb3(%rbp,%rax)
jmp 0x5839a
movq -0x50(%rbp), %rax
movq -0xb0(%rbp,%rax,8), %rax
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, -0xb3(%rbp,%rax)
jmp 0x5839c
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x58322
movzbl -0xb3(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0xb2(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0xb1(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movl %edx, 0x18(%rax,%rcx,4)
leaq -0x100(%rbp), %rdi
movq -0xb0(%rbp), %rax
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movl %eax, %r8d
movq -0xa0(%rbp), %rax
movl %eax, %r9d
movl $0x40, %esi
leaq 0x14dfc2(%rip), %rdx    # 0x1a63d0
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movl 0x18(%rax,%rcx,4), %edx
leaq -0x100(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x582e3
movq -0x10(%rbp), %rax
movb $0x1, 0x10(%rax)
movl $0x1, -0xb8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0xb8(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)