// File: /workspace/github_c_makefile_star2_O3/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 0x6915d(%rip), %rdi     # 0x74bea
leaq 0x682b0(%rip), %rsi     # 0x73d44
callq 0x1fe59
testq %rax, %rax
jne 0xbaba
leaq 0x6914d(%rip), %rdi     # 0x74bf2
leaq 0x68298(%rip), %rsi     # 0x73d44
callq 0x1fe59
testq %rax, %rax
je 0xbd4d
movq %rax, %rbx
leaq 0x1362fc(%rip), %rdi    # 0x141dc0
movl $0x38400, %edx          # imm = 0x38400
xorl %esi, %esi
callq 0x62f0
leaq 0x6913b(%rip), %rdi     # 0x74c12
xorl %eax, %eax
callq 0x25221
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x6660
movl $0x0, 0x14(%rsp)
movl $0x0, %r14d
testq %rax, %rax
je 0xbd1c
xorl %r14d, %r14d
movl $0x0, 0x14(%rsp)
movq %rbx, 0x8(%rsp)
movl %r14d, %ebp
movq %r13, %rdi
callq 0x6420
testl %eax, %eax
jle 0xbb56
cmpl $0x1, %eax
je 0xbb41
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
addl %eax, %ecx
cmpb $0xd, 0x20(%rsp,%rcx)
jne 0xbb41
movb $0x0, 0x20(%rsp,%rcx)
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
addl %ecx, %eax
incl %eax
cmpb $0xa, 0x20(%rsp,%rax)
jne 0xbb56
movb $0x0, 0x20(%rsp,%rax)
leal 0x1(%rbp), %r14d
leaq 0x2a(%rsp), %rax
movq %r13, %r15
movzbl (%r15), %ecx
cmpl $0x20, %ecx
jne 0xbb73
incq %r15
incq %rax
jmp 0xbb62
testl %ecx, %ecx
je 0xbbdb
cmpl $0x2f, %ecx
je 0xbb9f
cmpl $0x23, %ecx
jne 0xbbc5
movq %rax, %rbx
movzbl 0x1(%r15), %ecx
incq %r15
incq %rax
cmpl $0x20, %ecx
je 0xbb81
testl %ecx, %ecx
jne 0xbba8
movq 0x8(%rsp), %rbx
jmp 0xbbdb
cmpb $0x2f, 0x1(%r15)
jne 0xbbc5
jmp 0xbbdb
movl $0x7, %edx
movq %r15, %rdi
leaq 0x6906c(%rip), %rsi     # 0x74c23
callq 0x6250
testl %eax, %eax
je 0xbbf9
movq 0x8(%rsp), %rbx
subl %r13d, %r15d
leaq 0x6912f(%rip), %rdi     # 0x74cfe
movl %r15d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x25221
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x6660
testq %rax, %rax
jne 0xbb1a
jmp 0xbd1c
addq $0x7, %r15
movq %r15, %r12
movzbl (%r12), %eax
cmpl $0x20, %eax
jne 0xbc12
incq %r12
incq %rbx
jmp 0xbc00
movq %r12, %rdi
testl %eax, %eax
jne 0xbc2e
leaq 0x6900b(%rip), %rdi     # 0x74c2b
movl %ebp, %esi
xorl %eax, %eax
callq 0x25221
jmp 0xbb98
testb %al, %al
je 0xbc5e
movzbl %al, %eax
cmpl $0x20, %eax
je 0xbc45
movb 0x1(%rdi), %al
incq %rdi
incq %rbx
jmp 0xbc2e
movb $0x0, (%rdi)
movq %rbx, %rax
movzbl 0x1(%rdi), %ecx
incq %rdi
incq %rbx
cmpl $0x20, %ecx
je 0xbc48
testl %ecx, %ecx
jne 0xbc76
leaq 0x68feb(%rip), %rdi     # 0x74c50
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x25221
jmp 0xbb98
cmpb $0x0, (%rax)
leaq 0x1(%rax), %rax
jne 0xbc76
leaq 0x18(%rsp), %rsi
movl $0xa, %edx
callq 0x6940
movq 0x18(%rsp), %r15
cmpb $0x0, (%r15)
movq 0x8(%rsp), %rbx
jne 0xbbc5
cmpl $0x2400, %eax           # imm = 0x2400
jb 0xbcc3
leaq 0x68fd0(%rip), %rdi     # 0x74c80
movl %eax, %esi
movq %r12, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x25221
jmp 0xbbdb
movq %rax, %rbx
movq %r12, %rdi
callq 0x6420
cmpq $0x19, %rax
jb 0xbce7
leaq 0x68fde(%rip), %rdi     # 0x74cb9
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x25221
andl $0x3fff, %ebx           # imm = 0x3FFF
leaq (%rbx,%rbx,4), %rax
leaq (%rax,%rax,4), %rbx
leaq 0x1360c4(%rip), %r15    # 0x141dc0
leaq (%r15,%rbx), %rdi
movl $0x18, %edx
movq %r12, %rsi
callq 0x6190
movb $0x0, 0x18(%r15,%rbx)
incl 0x14(%rsp)
jmp 0xbb98
leaq 0x69016(%rip), %rdi     # 0x74d39
movl %r14d, %esi
movl 0x14(%rsp), %edx
xorl %eax, %eax
callq 0x25221
movq %rbx, %rdi
callq 0x6480
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68ea6(%rip), %rdi     # 0x74bfa
xorl %eax, %eax
callq 0x25221
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xbd3b