// File: /workspace/github_c_makefile_star2_O3/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 0x6a69
testq %rax, %rax
je 0x67fc
movq %rax, %r15
movq (%rax), %r13
testq %r13, %r13
jne 0x688f
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x9ff0
testq %rax, %rax
je 0x6804
movq %rax, %r12
movq 0x8(%rsp), %rax
xorl %ebx, %ebx
testq %rax, %rax
je 0x6702
movq %r12, %rcx
movq (%rcx), %rdx
cmpq %rbx, %rdx
cmovaq %rdx, %rbx
addq $0x10, %rcx
decq %rax
jne 0x66ef
movq 0x10(%rsp), %rdi
callq 0x6c25
movq %rax, 0x28(%rsp)
testq %rbx, %rbx
setne %al
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
setne %cl
testb %cl, %al
je 0x683b
movq %rbx, %rdi
movl $0xff, %edx
callq 0x2900
movq %rax, %rdi
callq 0xb1d6
movq %rax, %r13
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x6884
movq %r15, 0x18(%rsp)
xorl %r15d, %r15d
movq %r12, 0x20(%rsp)
movq %r15, %rbx
shlq $0x4, %rbx
cmpq $0x0, (%r12,%rbx)
je 0x67e9
addq %r12, %rbx
xorl %r14d, %r14d
movq 0x8(%rbx), %rax
movb (%rax,%r14), %r12b
addb $-0x21, %r12b
movzbl %r12b, %ebp
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0xa96a
testl %eax, %eax
setg %al
cmpb $-0x1, %bpl
sete %cl
orb %al, %cl
jne 0x67c6
movq 0x10(%rsp), %rdi
callq 0x6c2a
movq 0x8(%rbx), %rcx
movsbl (%rcx,%r14), %ecx
movq %rax, %rdi
movl $0x2, %esi
leaq 0x5e70(%rip), %rdx      # 0xc62f
xorl %eax, %eax
callq 0x6e50
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3a29
movb %r12b, (%rax)
incq %r14
cmpq (%rbx), %r14
jb 0x6771
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %r12
incq %r15
cmpq %rax, %r15
jb 0x675d
movq 0x18(%rsp), %r15
jmp 0x685f
xorl %r13d, %r13d
jmp 0x688f
movq 0x10(%rsp), %rdi
callq 0x6c2a
movq %rax, %rbx
callq 0x2320
movl (%rax), %edi
callq 0x2260
leaq 0x5dec(%rip), %rdx      # 0xc610
xorl %r13d, %r13d
movq %rbx, %rdi
movl $0x4, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x6e50
jmp 0x688f
movl $0x1, %edi
movl $0x1, %esi
movl $0xff, %edx
callq 0x2900
movq %rax, %rdi
callq 0xb1d6
movq %rax, %r13
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x6884
movq %r12, %rbx
addq $0x8, %rbx
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x2570
incq %r14
addq $0x10, %rbx
cmpq 0x8(%rsp), %r14
jb 0x686e
movq %r12, %rdi
callq 0x2570
movq %r13, (%r15)
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)