// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: loadpalette
static int loadpalette(void)
{
	int fil = -1;
	off_t flen;

	if ((fil = kopen4load("palette.dat",0)) < 0) goto badpalette;
	flen = kfilelength(fil);

	if (kread(fil,palette,768) != 768) {
		buildputs("loadpalette: truncated palette\n");
		goto badpalette;
	}

	if ((flen - 65536 - 2 - 768) % 256 == 0) {
		// Map format 7+ palette.
		if (kread(fil,&numpalookups,2) != 2) {
			buildputs("loadpalette: read error\n");
			goto badpalette;
		}
		numpalookups = B_LITTLE16(numpalookups);
	} else if ((flen - 32640 - 768) % 256 == 0) {
		// Old format palette.
		numpalookups = (flen - 32640 - 768) >> 8;
		buildprintf("loadpalette: old format palette (%d shades)\n",
			numpalookups);
		goto badpalette;
	} else {
		buildprintf("loadpalette: damaged palette\n");
		goto badpalette;
	}

	if ((palookup[0] = kmalloc(numpalookups<<8)) == NULL) {
		engineerrstr = "Failed to allocate palette memory";
		kclose(fil);
		return 1;
	}
	if ((transluc = kmalloc(65536L)) == NULL) {
		engineerrstr = "Failed to allocate translucency memory";
		kclose(fil);
		return 1;
	}

	globalpalwritten = palookup[0]; globalpal = 0;
	setpalookupaddress(globalpalwritten);

	fixtransluscence(transluc);

	kread(fil,palookup[globalpal],numpalookups<<8);
	kread(fil,transluc,65536);
	kclose(fil);

	initfastcolorlookup(30L,59L,11L);

	return 0;

badpalette:
	engineerrstr = "Failed to load PALETTE.DAT!";
	if (fil >= 0) kclose(fil);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl $0xffffffff, -0x8(%rbp) # imm = 0xFFFFFFFF
leaq 0x7b1c3(%rip), %rdi     # 0xb3429
xorl %esi, %esi
callq 0x2fee0
movl %eax, -0x8(%rbp)
cmpl $0x0, %eax
jge 0x3827a
jmp 0x38481
movl -0x8(%rbp), %edi
callq 0x30750
cltq
movq %rax, -0x10(%rbp)
movl -0x8(%rbp), %edi
leaq 0x280e7e(%rip), %rsi    # 0x2b9110
movl $0x300, %edx            # imm = 0x300
callq 0x302b0
cmpl $0x300, %eax            # imm = 0x300
je 0x382b4
leaq 0x7b18b(%rip), %rdi     # 0xb3435
callq 0x4a720
jmp 0x38481
movq -0x10(%rbp), %rax
subq $0x10000, %rax          # imm = 0x10000
subq $0x2, %rax
subq $0x300, %rax            # imm = 0x300
movl $0x100, %ecx            # imm = 0x100
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x38312
movl -0x8(%rbp), %edi
leaq 0x4172a6(%rip), %rsi    # 0x44f588
movl $0x2, %edx
callq 0x302b0
cmpl $0x2, %eax
je 0x38302
leaq 0x7b15d(%rip), %rdi     # 0xb3455
callq 0x4a720
jmp 0x38481
movw 0x41727f(%rip), %ax     # 0x44f588
movw %ax, 0x417278(%rip)     # 0x44f588
jmp 0x3837a
movq -0x10(%rbp), %rax
subq $0x7f80, %rax           # imm = 0x7F80
subq $0x300, %rax            # imm = 0x300
movl $0x100, %ecx            # imm = 0x100
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x38367
movq -0x10(%rbp), %rax
subq $0x7f80, %rax           # imm = 0x7F80
subq $0x300, %rax            # imm = 0x300
sarq $0x8, %rax
movw %ax, 0x41723b(%rip)     # 0x44f588
movswl 0x417234(%rip), %esi    # 0x44f588
leaq 0x7b113(%rip), %rdi     # 0xb346e
movb $0x0, %al
callq 0x37c90
jmp 0x38481
leaq 0x7b12d(%rip), %rdi     # 0xb349b
movb $0x0, %al
callq 0x37c90
jmp 0x38481
movswl 0x417207(%rip), %eax    # 0x44f588
shll $0x8, %eax
movslq %eax, %rdi
callq 0x35ef0
movq %rax, 0x39d2ad(%rip)    # 0x3d5640
cmpq $0x0, %rax
jne 0x383bb
leaq 0x7b119(%rip), %rax     # 0xb34b9
movq %rax, 0x1bd1d9(%rip)    # 0x1f5580
movl -0x8(%rbp), %edi
callq 0x30960
movl $0x1, -0x4(%rbp)
jmp 0x384a4
movl $0x10000, %edi          # imm = 0x10000
callq 0x35ef0
movq %rax, 0x1bd1c4(%rip)    # 0x1f5590
cmpq $0x0, %rax
jne 0x383f4
leaq 0x7b102(%rip), %rax     # 0xb34db
movq %rax, 0x1bd1a0(%rip)    # 0x1f5580
movl -0x8(%rbp), %edi
callq 0x30960
movl $0x1, -0x4(%rbp)
jmp 0x384a4
movq 0x39d245(%rip), %rax    # 0x3d5640
movq %rax, 0x412966(%rip)    # 0x44ad68
movl $0x0, 0x412958(%rip)    # 0x44ad64
movq 0x412955(%rip), %rdi    # 0x44ad68
callq 0xa75e0
movq 0x1bd171(%rip), %rdi    # 0x1f5590
callq 0xa7570
movl -0x8(%rbp), %edi
movslq 0x412936(%rip), %rcx    # 0x44ad64
leaq 0x39d20b(%rip), %rax    # 0x3d5640
movq (%rax,%rcx,8), %rsi
movswl 0x417148(%rip), %edx    # 0x44f588
shll $0x8, %edx
callq 0x302b0
movl -0x8(%rbp), %edi
movq 0x1bd13e(%rip), %rsi    # 0x1f5590
movl $0x10000, %edx          # imm = 0x10000
callq 0x302b0
movl -0x8(%rbp), %edi
callq 0x30960
movl $0x1e, %edi
movl $0x3b, %esi
movl $0xb, %edx
callq 0x5ab50
movl $0x0, -0x4(%rbp)
jmp 0x384a4
leaq 0x7b07a(%rip), %rax     # 0xb3502
movq %rax, 0x1bd0f1(%rip)    # 0x1f5580
cmpl $0x0, -0x8(%rbp)
jl 0x3849d
movl -0x8(%rbp), %edi
callq 0x30960
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax)