// File: /workspace/github_c_makefile_star2_O2/TurkeyMcMac[P]ts3d/src/map.c
// Function: load_map
struct map *load_map(struct loader *ldr, const char *name)
{
	struct logger *log = loader_logger(ldr);
	FILE *file;
	struct map **mapp = loader_map(ldr, name, &file);
	if (!mapp) return NULL;
	struct map *map = *mapp;
	if (map) return map;
	map = xmalloc(sizeof(*map));
	struct json_node jtree;
	map->name = str_dup(name);
	map->prereq = NULL;
	map->board = NULL;
	map->walls = NULL;
	map->blocks = NULL;
	map->ents = NULL;
	map->n_ents = 0;
	if (parse_json_tree(name, file, log, &jtree)) goto parse_error;
	if (jtree.kind != JN_MAP) {
		if (jtree.kind != JN_ERROR)
			logger_printf(log, LOGGER_ERROR,
				"Map \"%s\" is not a JSON dictionary\n", name);
		goto format_error;
	}
	void **player_nodep;
	if ((player_nodep = table_get(&jtree.d.map, "player"))) {
		if (parse_ent_start(&map->player, ldr, *player_nodep)) {
			logger_printf(log, LOGGER_ERROR,
				"Map \"%s\" has invalid \"player\" attribute\n",
				name);
			goto format_error;
		}
	} else {
		logger_printf(log, LOGGER_ERROR,
			"Map \"%s\" has no \"player\" attribute\n", name);
		goto format_error;
	}
	union json_node_data *got;
	if ((got = json_map_get(&jtree, "name", TAKE_NODE | JN_STRING))) {
		free(map->name);
		map->name = got->str;
	}
	if ((got = json_map_get(&jtree, "prereq", TAKE_NODE | JN_STRING)))
		map->prereq = got->str;
	uint8_t *walls = NULL;
	size_t n_blocks = 0;
	if ((got = json_map_get(&jtree, "blocks", JN_LIST))) {
		n_blocks = got->list.n_vals;
		walls = xmalloc(n_blocks);
		map->blocks = xmalloc(n_blocks * sizeof(*map->blocks));
		for (size_t i = 0; i < n_blocks; ++i) {
			parse_block(&map->blocks[i], &walls[i],
				&got->list.vals[i], ldr);
		}
	}
	struct json_node_data_list *layout = NULL;
	if ((got = json_map_get(&jtree, "layout", JN_LIST)))
		layout = &got->list;
	size_t width = 0, height = 0;
	if (layout) {
		height = layout->n_vals;
		for (size_t y = 0; y < height; ++y) {
			if (layout->vals[y].kind != JN_LIST) continue;
			struct json_node_data_list *row =
				&layout->vals[y].d.list;
			if (row->n_vals > width) width = row->n_vals;
		}
	}
	if (layout && width > 0 && height > 0) {
		map->board = assert_alloc(d3d_new_board(width, height, NULL));
		map->walls = xcalloc(height, width);
		// See DIRECTION NOTE regarding y:
		for (size_t r = 0, y = height - 1; r < height; ++r, --y) {
			if (layout->vals[y].kind != JN_LIST) continue;
			struct json_node_data_list *row =
				&layout->vals[r].d.list;
			for (size_t x = 0; x < row->n_vals; ++x) {
				if (row->vals[x].kind != JN_NUMBER) continue;
				size_t idx = row->vals[x].d.num;
				if (idx >= n_blocks) continue;
				*d3d_board_get(map->board, x, y) =
					&map->blocks[idx];
				*get_wall(map, x, y) = walls[idx];
			}
		}
		for (size_t y = 0; y < height; ++y) {
			for (size_t x = 0; x < width; ++x) {
				*get_wall(map, x, y) =
					normalize_wall(map, x, y);
			}
		}
	} else {
		width = height = 1;
		map->board = assert_alloc(d3d_new_board(width, height, NULL));
		map->walls = xcalloc(1, 1);
	}
	free(walls);
	map->player.pos.x = CLAMP(map->player.pos.x, 0, width - 0.01);
	// See DIRECTION NOTE:
	map->player.pos.y = height - CLAMP(map->player.pos.y, 0.01, height);
	map->n_ents = 0;
	if ((got = json_map_get(&jtree, "ents", JN_LIST))) {
		map->ents = xmalloc(got->list.n_vals * sizeof(*map->ents));
		for (size_t i = 0; i < got->list.n_vals; ++i) {
			struct map_ent_start *start = &map->ents[map->n_ents];
			if (!parse_ent_start(start, ldr, &got->list.vals[i])) {
				// See DIRECTION NOTE:
				start->pos.y = height - start->pos.y;
				++map->n_ents;
			}
		}
	}
	if (!map->board) map->board = assert_alloc(d3d_new_board(0, 0, NULL));
	free_json_tree(&jtree);
	*mapp = map;
	return map;

format_error:
	free_json_tree(&jtree);
	map_free(map);
parse_error:
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %r12
callq 0x6705
movq %rax, %r15
leaq 0x90(%rsp), %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x653d
testq %rax, %rax
je 0x77d5
movq (%rax), %rbx
testq %rbx, %rbx
jne 0x77d7
movq %r12, 0x38(%rsp)
movq %rax, 0x88(%rsp)
pushq $0x60
popq %rdi
callq 0xa7d0
movq %rax, %r12
movq %r14, %rdi
callq 0xa554
movq %rax, (%r12)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r12)
movupd %xmm0, 0x18(%r12)
movupd %xmm0, 0x28(%r12)
movq 0x90(%rsp), %rsi
leaq 0x60(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rdx
callq 0x4a25
testl %eax, %eax
jne 0x77d5
movl 0x60(%rsp), %eax
cmpl $0x7, %eax
je 0x77c3
cmpl $0x2, %eax
jne 0x77a3
leaq 0x68(%rsp), %rdi
leaq 0x45f8(%rip), %rsi      # 0xbd72
callq 0x9f6a
testq %rax, %rax
je 0x77ac
leaq 0x38(%r12), %rdi
movq (%rax), %rdx
movq 0x38(%rsp), %rsi
callq 0x7e86
testl %eax, %eax
je 0x77ec
leaq 0x45d8(%rip), %rdx      # 0xbd79
jmp 0x77b3
leaq 0x45a5(%rip), %rdx      # 0xbd4f
jmp 0x77b3
leaq 0x45ef(%rip), %rdx      # 0xbda2
pushq $0x4
popq %rsi
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x691c
leaq 0x60(%rsp), %rdi
callq 0x4eaf
movq %r12, %rdi
callq 0x7fa6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b38(%rip), %rsi      # 0xb32b
leaq 0x60(%rsp), %rdi
movl $0x1004, %edx           # imm = 0x1004
callq 0x49e4
testq %rax, %rax
je 0x781a
movq %rax, %r14
movq (%r12), %rdi
callq 0x2580
movq (%r14), %rax
movq %rax, (%r12)
leaq 0x4527(%rip), %rsi      # 0xbd48
leaq 0x60(%rsp), %rdi
movl $0x1004, %edx           # imm = 0x1004
callq 0x49e4
testq %rax, %rax
je 0x7842
movq %r12, %rcx
addq $0x8, %rcx
movq (%rax), %rax
movq %rax, (%rcx)
leaq 0x457d(%rip), %rsi      # 0xbdc6
leaq 0x60(%rsp), %rdi
pushq $0x3
popq %rdx
callq 0x49e4
testq %rax, %rax
movq %r12, (%rsp)
je 0x79dd
movq %rax, %r13
movq (%rax), %rbx
movq %rbx, %rdi
callq 0xa7d0
movq %rax, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
imulq $0x30, %rbx, %rdi
callq 0xa7d0
movq %rax, 0x20(%r12)
xorl %r14d, %r14d
leaq 0x945d(%rip), %r15      # 0x10cf0
movq %r13, 0x20(%rsp)
cmpq 0x10(%rsp), %r14
je 0x79ef
movq 0x20(%r12), %rbp
imulq $0x30, %r14, %r12
movq 0x8(%r13), %r13
movq %r14, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rcx
movb $0x0, (%rcx,%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rbp,%r12)
movupd %xmm0, 0x10(%rbp,%r12)
movupd %xmm0, (%rbp,%r12)
cmpl $0x2, (%r13,%rax)
jne 0x79cc
addq %rax, %r13
movq %r13, %rdi
leaq 0x453e(%rip), %rsi      # 0xbe31
pushq $0x6
popq %rdx
callq 0x49e4
testq %rax, %rax
je 0x7913
cmpb $0x1, (%rax)
jne 0x7913
movq 0x8(%rsp), %rax
movb $-0x1, (%rax,%r14)
movb $0x1, %bl
jmp 0x7915
xorl %ebx, %ebx
addq %r12, %rbp
movq %r13, %rdi
leaq 0x43b6(%rip), %rsi      # 0xbcd8
pushq $0x4
popq %rdx
callq 0x49e4
testq %rax, %rax
je 0x7959
movq (%rax), %rsi
movq 0x38(%rsp), %rdi
callq 0x61b4
testq %rax, %rax
je 0x7959
movq %rax, 0x28(%rbp)
movq %rax, 0x20(%rbp)
movq %rax, 0x18(%rbp)
movq %rax, 0x10(%rbp)
movq %rax, 0x8(%rbp)
movq %rax, (%rbp)
pushq $0x10
popq %r12
cmpq $0xa0, %r12
je 0x79cc
movq -0x10(%r12,%r15), %rsi
movq %r13, %rdi
pushq $0x4
popq %rdx
callq 0x49e4
testq %rax, %rax
je 0x7996
movq (%rax), %rsi
movq 0x38(%rsp), %rdi
callq 0x61b4
testq %rax, %rax
je 0x7996
movl (%r12,%r15), %ecx
movq %rax, (%rbp,%rcx,8)
testb %bl, %bl
jne 0x79c6
movq -0x8(%r12,%r15), %rsi
movq %r13, %rdi
pushq $0x6
popq %rdx
callq 0x49e4
testq %rax, %rax
je 0x79c6
cmpb $0x1, (%rax)
jne 0x79c6
movb (%r12,%r15), %cl
pushq $0x1
popq %rax
shll %cl, %eax
movq 0x8(%rsp), %rcx
orb %al, (%rcx,%r14)
addq $0x18, %r12
jmp 0x795d
incq %r14
movq (%rsp), %r12
movq 0x20(%rsp), %r13
jmp 0x7898
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x43d7(%rip), %rsi      # 0xbdcd
leaq 0x60(%rsp), %rdi
pushq $0x3
popq %rdx
callq 0x49e4
movq %rax, %r15
testq %rax, %rax
je 0x7a39
movq (%r15), %rbx
pushq $0x8
popq %rax
xorl %r14d, %r14d
movq %rbx, %rcx
subq $0x1, %rcx
jb 0x7a3e
movq 0x8(%r15), %rdx
cmpl $0x3, -0x8(%rdx,%rax)
jne 0x7a33
movq (%rdx,%rax), %rdx
cmpq %r14, %rdx
cmovaq %rdx, %r14
addq $0x20, %rax
jmp 0x7a17
xorl %r14d, %r14d
xorl %ebx, %ebx
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
testq %rbx, %rbx
sete %al
orb %cl, %al
jne 0x7b89
xorl %r13d, %r13d
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x296b
movq %rax, %rdi
callq 0xa922
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movq %r14, 0x50(%rsp)
movq %r14, %rsi
callq 0xa8b2
movq %rax, 0x18(%r12)
movsd 0x3654(%rip), %xmm1     # 0xb0e8
movq %rbx, %r14
movq %rbx, 0x58(%rsp)
movq %r15, 0x80(%rsp)
cmpq %rbx, %r13
je 0x7bd7
movq %r13, 0x20(%rsp)
decq %r14
movq 0x8(%r15), %rax
movq %r14, %rcx
shlq $0x5, %rcx
cmpl $0x3, (%rax,%rcx)
jne 0x7b6f
movq 0x20(%rsp), %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %r13
addq $0x8, %r13
pushq $0x8
popq %rbp
xorl %r15d, %r15d
cmpq (%r13), %r15
jae 0x7b6f
movq 0x8(%r13), %rax
cmpl $0x5, -0x8(%rax,%rbp)
jne 0x7b63
movsd (%rax,%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
cmpq %rbx, 0x10(%rsp)
jbe 0x7b63
imulq $0x30, %rbx, %r12
movq (%rsp), %rax
addq 0x20(%rax), %r12
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x37fb
movq %r12, (%rax)
movq (%rsp), %r12
movq 0x8(%rsp), %rax
movb (%rax,%rbx), %bl
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x7f7b
movsd 0x3587(%rip), %xmm1     # 0xb0e8
movb %bl, (%rax)
incq %r15
addq $0x20, %rbp
jmp 0x7ae1
movq 0x20(%rsp), %r13
incq %r13
movq 0x58(%rsp), %rbx
movq 0x80(%rsp), %r15
jmp 0x7aa4
pushq $0x1
popq %r14
movq %r14, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x296b
movq %rax, %rdi
callq 0xa922
movq %rax, 0x10(%r12)
movq %r14, %rdi
movq %r14, %rsi
callq 0xa8b2
movq %rax, 0x18(%r12)
movsd 0x3531(%rip), %xmm0     # 0xb0f0
movaps %xmm0, 0x10(%rsp)
movsd 0x4164(%rip), %xmm0     # 0xbd30
movapd %xmm0, 0x20(%rsp)
jmp 0x7d55
xorl %r15d, %r15d
movq 0x50(%rsp), %rax
cmpq %rbx, %r15
je 0x7cff
xorl %r14d, %r14d
movq %r15, 0x20(%rsp)
cmpq %rax, %r14
je 0x7cf7
movq 0x10(%r12), %rdi
movq 0x18(%r12), %rbx
callq 0x37f2
imulq %r15, %rax
addq %rbx, %rax
movq %r14, 0x10(%rsp)
movb (%r14,%rax), %r13b
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0x7ccf
leaq 0x4264(%rip), %rax      # 0xbe90
movl (%r14,%rax), %ebp
pushq $0x1
popq %rbx
movl %ebp, %ecx
shll %cl, %ebx
movzbl %r13b, %eax
btl %ebp, %eax
jb 0x7ccf
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %r15, 0x40(%rsp)
movl %ebp, %edi
leaq 0x48(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xa761
movq 0x10(%r12), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x37fb
testq %rax, %rax
je 0x7cbe
movq 0x10(%r12), %rdi
movq 0x18(%r12), %r12
movq 0x40(%rsp), %r15
callq 0x37f2
imulq %r15, %rax
movq 0x48(%rsp), %rcx
addq %r12, %rax
movzbl (%rcx,%rax), %r15d
movl %ebp, %edi
callq 0xa581
xorl %ecx, %ecx
btl %eax, %r15d
jae 0x7cb3
movl %ebx, %ecx
movq (%rsp), %r12
movq 0x20(%rsp), %r15
jmp 0x7cc0
movl %ebx, %ecx
orb %r13b, %cl
addq $0x4, %r14
movl %ecx, %r13d
jmp 0x7c1b
movq %r12, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x7f7b
movb %r13b, (%rax)
incq %r14
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %rax
jmp 0x7bf0
incq %r15
jmp 0x7bdf
movq %rax, %xmm0
movq 0x3324(%rip), %xmm1     # 0xb030
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x3328(%rip), %xmm2     # 0xb040
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addsd 0x4008(%rip), %xmm3     # 0xbd38
movapd %xmm3, 0x20(%rsp)
movq %rbx, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2580
movsd 0x38(%r12), %xmm0
movsd 0x40(%r12), %xmm1
movapd 0x20(%rsp), %xmm3
minsd %xmm0, %xmm3
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm0
andnpd %xmm3, %xmm0
movlpd %xmm0, 0x38(%r12)
movapd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm0
minsd %xmm1, %xmm0
movsd 0x3f9f(%rip), %xmm2     # 0xbd40
cmpltsd %xmm2, %xmm1
movapd %xmm1, %xmm3
andnpd %xmm0, %xmm3
andpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movapd %xmm4, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%r12)
andq $0x0, 0x28(%r12)
leaq 0x3850(%rip), %rsi      # 0xb622
leaq 0x60(%rsp), %rdi
pushq $0x3
popq %rdx
callq 0x49e4
testq %rax, %rax
je 0x7e4a
movq %rax, %r14
imulq $0x28, (%rax), %rdi
callq 0xa7d0
movq (%rsp), %rcx
movq %rax, 0x30(%rcx)
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq (%r14), %r12
jae 0x7e4a
movq (%rsp), %rax
imulq $0x28, 0x28(%rax), %r15
addq 0x30(%rax), %r15
movq 0x8(%r14), %rdx
addq %rbx, %rdx
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x7e86
testl %eax, %eax
jne 0x7e41
movapd 0x10(%rsp), %xmm0
subsd 0x8(%r15), %xmm0
movsd %xmm0, 0x8(%r15)
movq (%rsp), %rax
incq 0x28(%rax)
incq %r12
addq $0x20, %rbx
jmp 0x7dfd
movq (%rsp), %rbx
cmpq $0x0, 0x10(%rbx)
jne 0x7e6c
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x296b
movq %rax, %rdi
callq 0xa922
movq %rax, 0x10(%rbx)
leaq 0x60(%rsp), %rdi
callq 0x4eaf
movq 0x88(%rsp), %rax
movq %rbx, (%rax)
jmp 0x77d7