// File: /workspace/github_c_makefile_star2_O2/TurkeyMcMac[P]ts3d/src/load-texture.c
// Function: load_texture
d3d_texture *load_texture(struct loader *ldr, const char *name)
{
	FILE *file;
	d3d_texture *txtr = NULL, **txtrp;
	txtrp = loader_texture(ldr, name, &file);
	if (!txtrp) return NULL;
	txtr = *txtrp;
	if (txtr) return txtr;
	size_t width;
	size_t height;
	struct string *lines = read_lines(file, &height);
	if (!lines) {
		logger_printf(loader_logger(ldr), LOGGER_ERROR,
			"Error while reading lines: %s\n", strerror(errno));
		return NULL;
	}
	width = 0;
	for (size_t y = 0; y < height; ++y) {
		if (lines[y].len > width) width = lines[y].len;
	}
	struct color_map *colors = loader_color_map(ldr);
	if (width > 0 && height > 0) {
		txtr = assert_alloc(d3d_new_texture(width, height,
			TRANSPARENT_PIXEL));
		for (size_t y = 0; y < height; ++y) {
			struct string *line = &lines[y];
			size_t x;
			for (x = 0; x < line->len; ++x) {
				d3d_pixel pix = pixel_from_char(line->text[x]);
				int pair = color_map_add_pair(colors, pix);
				if (pair <= 0 && pix != TRANSPARENT_PIXEL)
					logger_printf(loader_logger(ldr),
						LOGGER_WARNING,
						"Pixel not registered: %c\n",
						line->text[x]);
				*d3d_texture_get(txtr, x, y) = pix;
			}
		}
	} else {
		txtr = new_empty_texture();
	}
	for (size_t i = 0; i < height; ++i) {
		free(lines[i].text);
	}
	free(lines);
	*txtrp = txtr;
	return txtr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x30(%rsp), %rdx
movq %rdi, 0x10(%rsp)
callq 0x6557
testq %rax, %rax
je 0x6220
movq %rax, %r15
movq (%rax), %r13
testq %r13, %r13
jne 0x6389
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x9894
testq %rax, %rax
je 0x6305
movq %rax, %r12
movq 0x8(%rsp), %rax
xorl %ebx, %ebx
movq %r12, %rcx
subq $0x1, %rax
jb 0x6228
movq (%rcx), %rdx
cmpq %rbx, %rdx
cmovaq %rdx, %rbx
addq $0x10, %rcx
jmp 0x620a
xorl %r13d, %r13d
jmp 0x6389
movq 0x10(%rsp), %rdi
callq 0x6700
movq %rax, 0x28(%rsp)
testq %rbx, %rbx
setne %al
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
setne %cl
testb %cl, %al
je 0x633a
movq %r15, 0x18(%rsp)
movq %rbx, %rdi
movl $0xff, %edx
callq 0x2901
movq %rax, %rdi
callq 0xa922
movq %rax, %r13
xorl %r15d, %r15d
movq %r12, 0x20(%rsp)
cmpq 0x8(%rsp), %r15
jae 0x6357
movq %r15, %r14
shlq $0x4, %r14
addq %r12, %r14
xorl %ebp, %ebp
cmpq (%r14), %rbp
jae 0x62f8
movq 0x8(%r14), %rax
movb (%rax,%rbp), %r12b
addb $-0x21, %r12b
movzbl %r12b, %ebx
movq 0x28(%rsp), %rdi
movl %ebx, %esi
callq 0xa14f
testl %eax, %eax
setg %al
cmpb $-0x1, %bl
sete %cl
orb %al, %cl
jne 0x62e2
movq 0x10(%rsp), %rdi
callq 0x6705
movq 0x8(%r14), %rcx
movsbl (%rcx,%rbp), %ecx
movq %rax, %rdi
pushq $0x2
popq %rsi
leaq 0x5324(%rip), %rdx      # 0xb5ff
xorl %eax, %eax
callq 0x691c
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x37cd
movb %r12b, (%rax)
incq %rbp
jmp 0x628c
incq %r15
movq 0x20(%rsp), %r12
jmp 0x6275
movq 0x10(%rsp), %rdi
callq 0x6705
movq %rax, %rbx
callq 0x2330
movl (%rax), %edi
callq 0x2260
leaq 0x52bb(%rip), %rdx      # 0xb5e0
xorl %r13d, %r13d
pushq $0x4
popq %rsi
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x691c
jmp 0x6389
pushq $0x1
popq %rdi
movq %rdi, %rsi
movl $0xff, %edx
callq 0x2901
movq %rax, %rdi
callq 0xa922
movq %rax, %r13
jmp 0x635c
movq 0x18(%rsp), %r15
movq %r12, %rbx
addq $0x8, %rbx
xorl %r14d, %r14d
cmpq 0x8(%rsp), %r14
jae 0x637e
movq (%rbx), %rdi
callq 0x2580
incq %r14
addq $0x10, %rbx
jmp 0x6366
movq %r12, %rdi
callq 0x2580
movq %r13, (%r15)
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop