// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/build.c
// Function: loadnames
int loadnames(void)
{
	char buffer[1024], *p, *name, *number, *endptr;
	int num, syms=0, line=0, a;
	BFILE *fp;

	fp = fopenfrompath("NAMES.H","r");
	if (!fp) {
		if ((fp = fopenfrompath("names.h","r")) == NULL) {
			buildprintf("Failed to open NAMES.H\n");
			return -1;
		}
	}

	//clearbufbyte(names, sizeof(names), 0);
	memset(names,0,sizeof(names));

	buildprintf("Loading NAMES.H\n");

	while (Bfgets(buffer, 1024, fp)) {
		a = (int)strlen(buffer);
		if (a >= 1) {
			if (a > 1)
				if (buffer[a-2] == '\r') buffer[a-2] = 0;
			if (buffer[a-1] == '\n') buffer[a-1] = 0;
		}

		p = buffer;
		line++;
		while (*p == 32) p++;
		if (*p == 0) continue;	// blank line

		if (*p == '#') {
			p++;
			while (*p == 32) p++;
			if (*p == 0) continue;	// null directive

			if (!Bstrncmp(p, "define ", 7)) {
				// #define_...
				p += 7;
				while (*p == 32) p++;
				if (*p == 0) {
					buildprintf("Error: Malformed #define at line %d\n", line-1);
					continue;
				}

				name = p;
				while (*p != 32 && *p != 0) p++;
				if (*p == 32) {
					*(p++) = 0;
					while (*p == 32) p++;
					if (*p == 0) {	// #define_NAME with no number
						buildprintf("Error: No number given for name \"%s\" (line %d)\n", name, line-1);
						continue;
					}

					number = p;
					while (*p != 0) p++;
					if (*p != 0) *p = 0;

					// add to list
					num = (int)strtol(number, &endptr, 10);
					if (*endptr != 0) {
						p = endptr;
						goto badline;
					}
					if (num < 0 || num >= MAXTILES) {
						buildprintf("Error: Constant %d for name \"%s\" out of range (line %d)\n", num, name, line-1);
						continue;
					}

					if (Bstrlen(name) > 24)
						buildprintf("Warning: Name \"%s\" longer than 24 characters (line %d). Truncating.\n", name, line-1);

					Bstrncpy(names[num], name, 24);
					names[num][24] = 0;

					syms++;

					continue;

				} else {	// #define_NAME with no number
					buildprintf("Error: No number given for name \"%s\" (line %d)\n", name, line-1);
					continue;
				}
			} else goto badline;
		} else if (*p == '/') {
			if (*(p+1) == '/') continue;	// comment
		}
badline:
		buildprintf("Error: Invalid statement found at character %d on line %d\n", (int)(p-buffer), line-1);
	}
	buildprintf("Read %d lines, loaded %d names.\n", line, syms);

	Bfclose(fp);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
leaq 0x5ee3b(%rip), %rdi     # 0x69b2a
leaq 0x5e00c(%rip), %rsi     # 0x68d02
callq 0x1d75b
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0xad26
leaq 0x5ee26(%rip), %rdi     # 0x69b32
leaq 0x5dfef(%rip), %rsi     # 0x68d02
callq 0x1d75b
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0xaf83
leaq 0x130093(%rip), %rdi    # 0x13adc0
movl $0x38400, %edx          # imm = 0x38400
xorl %esi, %esi
callq 0x62f0
leaq 0x5ee12(%rip), %rdi     # 0x69b52
xorl %eax, %eax
callq 0x22427
leaq 0x20(%rsp), %rbp
movl $0x0, 0xc(%rsp)
xorl %r12d, %r12d
movl %r12d, %r14d
movq %rbp, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x10(%rsp), %rdx
callq 0x6660
testq %rax, %rax
je 0xaf4e
movq %rbp, %rdi
callq 0x6420
testl %eax, %eax
jle 0xadae
cmpl $0x1, %eax
je 0xad99
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
addl %eax, %ecx
cmpb $0xd, 0x20(%rsp,%rcx)
jne 0xad99
movb $0x0, 0x20(%rsp,%rcx)
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
addl %ecx, %eax
incl %eax
cmpb $0xa, 0x20(%rsp,%rax)
jne 0xadae
movb $0x0, 0x20(%rsp,%rax)
leal 0x1(%r14), %r12d
leaq 0x2a(%rsp), %rax
movq %rbp, %r15
movzbl (%r15), %ecx
cmpl $0x20, %ecx
jne 0xadcb
incq %r15
incq %rax
jmp 0xadba
testl %ecx, %ecx
je 0xad57
cmpl $0x2f, %ecx
je 0xae23
cmpl $0x23, %ecx
jne 0xae2e
movq %rax, %rbx
movzbl 0x1(%r15), %ecx
incq %r15
incq %rax
cmpl $0x20, %ecx
je 0xadd9
testl %ecx, %ecx
je 0xad57
movq %r15, %rdi
leaq 0x5ed65(%rip), %rsi     # 0x69b63
pushq $0x7
popq %rdx
callq 0x6250
testl %eax, %eax
jne 0xae2e
addq $0x7, %r15
movq %r15, %r13
movzbl (%r13), %eax
cmpl $0x20, %eax
jne 0xae4a
incq %r13
incq %rbx
jmp 0xae11
cmpb $0x2f, 0x1(%r15)
je 0xad57
subl %ebp, %r15d
leaq 0x5ee06(%rip), %rdi     # 0x69c3e
movl %r15d, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x22427
jmp 0xad57
movq %r13, %rdi
testl %eax, %eax
jne 0xae67
leaq 0x5ed13(%rip), %rdi     # 0x69b6b
movl %r14d, %esi
xorl %eax, %eax
callq 0x22427
jmp 0xad57
testb %al, %al
je 0xae97
movzbl %al, %eax
cmpl $0x20, %eax
je 0xae7e
movb 0x1(%rdi), %al
incq %rdi
incq %rbx
jmp 0xae67
movb $0x0, (%rdi)
movq %rbx, %rax
movzbl 0x1(%rdi), %ecx
incq %rdi
incq %rbx
cmpl $0x20, %ecx
je 0xae81
testl %ecx, %ecx
jne 0xaeb0
leaq 0x5ecf2(%rip), %rdi     # 0x69b90
movq %r13, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x22427
jmp 0xad57
testb %cl, %cl
je 0xaebb
movb (%rax), %cl
incq %rax
jmp 0xaeb0
leaq 0x18(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x6940
movq 0x18(%rsp), %r15
cmpb $0x0, (%r15)
jne 0xae2e
movq %rax, %rbx
cmpl $0x2400, %ebx           # imm = 0x2400
jb 0xaefd
leaq 0x5ecd7(%rip), %rdi     # 0x69bc0
movl %ebx, %esi
movq %r13, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22427
jmp 0xad57
movq %r13, %rdi
callq 0x6420
cmpq $0x19, %rax
jb 0xaf1f
leaq 0x5ece7(%rip), %rdi     # 0x69bf9
movq %r13, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x22427
andl $0x3fff, %ebx           # imm = 0x3FFF
imulq $0x19, %rbx, %rbx
leaq 0x12fe90(%rip), %r14    # 0x13adc0
leaq (%r14,%rbx), %rdi
movq %r13, %rsi
pushq $0x18
popq %rdx
callq 0x6190
movb $0x0, 0x18(%rbx,%r14)
incl 0xc(%rsp)
jmp 0xad57
leaq 0x5ed24(%rip), %rdi     # 0x69c79
xorl %ebp, %ebp
movl %r14d, %esi
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x22427
movq 0x10(%rsp), %rdi
callq 0x6480
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5ebb0(%rip), %rdi     # 0x69b3a
xorl %eax, %eax
callq 0x22427
pushq $-0x1
popq %rbp
jmp 0xaf6f