// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
callq 0x6a99
testq %rax, %rax
je 0x682c
movq %rax, %r15
movq (%rax), %r13
testq %r13, %r13
jne 0x68c1
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xa0d4
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x6834
movq 0x8(%rsp), %rax
xorl %ebx, %ebx
testq %rax, %rax
je 0x673a
movq 0x10(%rsp), %rcx
movq (%rcx), %rdx
cmpq %rbx, %rdx
cmovaq %rdx, %rbx
addq $0x10, %rcx
decq %rax
jne 0x6727
movq 0x18(%rsp), %rdi
callq 0x6c55
movq %rax, 0x28(%rsp)
testq %rbx, %rbx
setne %al
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
setne %cl
testb %cl, %al
je 0x686b
movq %rbx, %rdi
movl $0xff, %edx
callq 0x2917
movq %rax, %rdi
callq 0xb302
movq %rax, %r13
cmpq $0x0, 0x8(%rsp)
je 0x688a
movq %r15, 0x20(%rsp)
xorl %r15d, %r15d
movq %r15, %rbx
shlq $0x4, %rbx
movq 0x10(%rsp), %rax
cmpq $0x0, (%rax,%rbx)
je 0x6817
addq 0x10(%rsp), %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 0xaa82
testl %eax, %eax
setg %al
cmpb $-0x1, %bpl
sete %cl
orb %al, %cl
jne 0x67fe
movq 0x18(%rsp), %rdi
callq 0x6c5a
movq 0x8(%rbx), %rcx
movsbl (%rcx,%r14), %ecx
movq %rax, %rdi
movl $0x2, %esi
leaq 0x5dc8(%rip), %rdx      # 0xc5bf
xorl %eax, %eax
callq 0x6e97
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x39a6
movb %r12b, (%rax)
incq %r14
cmpq (%rbx), %r14
jb 0x67a9
incq %r15
cmpq 0x8(%rsp), %r15
jb 0x678e
movq 0x20(%rsp), %r15
jmp 0x688a
xorl %r13d, %r13d
jmp 0x68c1
movq 0x18(%rsp), %rdi
callq 0x6c5a
movq %rax, %rbx
callq 0x2320
movl (%rax), %edi
callq 0x2260
leaq 0x5d4c(%rip), %rdx      # 0xc5a0
xorl %r13d, %r13d
movq %rbx, %rdi
movl $0x4, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x6e97
jmp 0x68c1
movl $0x1, %edi
movl $0x1, %esi
movl $0xff, %edx
callq 0x2917
movq %rax, %rdi
callq 0xb302
movq %rax, %r13
cmpq $0x0, 0x8(%rsp)
je 0x68b4
movq 0x10(%rsp), %rbx
addq $0x8, %rbx
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x2570
incq %r14
addq $0x10, %rbx
cmpq 0x8(%rsp), %r14
jb 0x689e
movq 0x10(%rsp), %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
nop