// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq %rsi, %r12
movq %rdi, %r14
callq 0x6c5a
movq %rax, %r13
leaq 0xa0(%rsp), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x6a7f
testq %rax, %rax
je 0x7dee
movq %rax, %rbp
movq (%rax), %rbx
testq %rbx, %rbx
jne 0x7df0
movl $0x60, %edi
callq 0xb1ac
movq %rax, %rbx
movq %r12, %rdi
callq 0xaec0
movq %rax, (%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rbx)
movupd %xmm0, 0x18(%rbx)
movupd %xmm0, 0x28(%rbx)
movq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rdx
callq 0x4cd9
testl %eax, %eax
jne 0x7dee
movl 0x70(%rsp), %eax
cmpl $0x7, %eax
je 0x7ddc
cmpl $0x2, %eax
jne 0x7dba
leaq 0x78(%rsp), %rdi
leaq 0x4f9e(%rip), %rsi      # 0xcd32
callq 0xa88b
testq %rax, %rax
je 0x7dc3
leaq 0x38(%rbx), %rdi
movq (%rax), %rdx
movq %r14, %rsi
callq 0x8548
testl %eax, %eax
je 0x7e05
leaq 0x4f81(%rip), %rdx      # 0xcd39
jmp 0x7dca
leaq 0x4f4e(%rip), %rdx      # 0xcd0f
jmp 0x7dca
leaq 0x4f98(%rip), %rdx      # 0xcd62
movq %r13, %rdi
movl $0x4, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0x6e97
leaq 0x70(%rsp), %rdi
callq 0x5222
movq %rbx, %rdi
callq 0x8650
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x50(%rsp)
leaq 0x44da(%rip), %rsi      # 0xc2eb
leaq 0x70(%rsp), %rdi
movl $0x1004, %edx           # imm = 0x1004
callq 0x4c98
testq %rax, %rax
je 0x7e36
movq %rax, %r14
movq (%rbx), %rdi
callq 0x2570
movq (%r14), %rax
movq %rax, (%rbx)
leaq 0x4ecb(%rip), %rsi      # 0xcd08
leaq 0x70(%rsp), %rdi
movl $0x1004, %edx           # imm = 0x1004
callq 0x4c98
testq %rax, %rax
je 0x7e5e
movq %rbx, %rcx
addq $0x8, %rcx
movq (%rax), %rax
movq %rax, (%rcx)
leaq 0x4f21(%rip), %rsi      # 0xcd86
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
callq 0x4c98
testq %rax, %rax
movq %rbx, 0x8(%rsp)
je 0x801f
movq %rax, %r15
movq (%rax), %r14
movq %r14, %rdi
callq 0xb1ac
movq %rax, 0x10(%rsp)
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdi
callq 0xb1ac
movq %rax, 0x20(%rbx)
movq %r14, 0x30(%rsp)
testq %r14, %r14
je 0x8031
movq %r15, 0x20(%rsp)
movq %rbp, 0x48(%rsp)
xorl %r14d, %r14d
leaq 0x8e25(%rip), %r12      # 0x10cf0
movq 0x20(%rbx), %r13
leaq (%r14,%r14,2), %r15
shlq $0x4, %r15
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rbp
movq %r14, %rax
shlq $0x5, %rax
movq 0x10(%rsp), %rcx
movb $0x0, (%rcx,%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%r13,%r15)
movupd %xmm0, 0x10(%r13,%r15)
movupd %xmm0, (%r13,%r15)
cmpl $0x2, (%rbp,%rax)
jne 0x8005
addq %rax, %rbp
movq %rbp, %rdi
leaq 0x4ecf(%rip), %rsi      # 0xcdf1
movl $0x6, %edx
callq 0x4c98
testq %rax, %rax
je 0x7f44
cmpb $0x1, (%rax)
jne 0x7f44
movq 0x10(%rsp), %rax
movb $-0x1, (%rax,%r14)
movb $0x1, %bl
jmp 0x7f46
xorl %ebx, %ebx
addq %r15, %r13
movq %rbp, %rdi
leaq 0x4d45(%rip), %rsi      # 0xcc98
movl $0x4, %edx
callq 0x4c98
testq %rax, %rax
je 0x7f8c
movq (%rax), %rsi
movq 0x50(%rsp), %rdi
callq 0x66c4
testq %rax, %rax
je 0x7f8c
movq %rax, 0x28(%r13)
movq %rax, 0x20(%r13)
movq %rax, 0x18(%r13)
movq %rax, 0x10(%r13)
movq %rax, 0x8(%r13)
movq %rax, (%r13)
movl $0x10, %r15d
movq -0x10(%r15,%r12), %rsi
movq %rbp, %rdi
movl $0x4, %edx
callq 0x4c98
testq %rax, %rax
je 0x7fc4
movq (%rax), %rsi
movq 0x50(%rsp), %rdi
callq 0x66c4
testq %rax, %rax
je 0x7fc4
movl (%r15,%r12), %ecx
movq %rax, (%r13,%rcx,8)
testb %bl, %bl
jne 0x7ff8
movq -0x8(%r15,%r12), %rsi
movq %rbp, %rdi
movl $0x6, %edx
callq 0x4c98
testq %rax, %rax
je 0x7ff8
cmpb $0x1, (%rax)
jne 0x7ff8
movb (%r15,%r12), %cl
movl $0x1, %eax
shll %cl, %eax
movq 0x10(%rsp), %rcx
orb %al, (%rcx,%r14)
addq $0x18, %r15
cmpq $0xa0, %r15
jne 0x7f92
incq %r14
cmpq 0x30(%rsp), %r14
movq 0x8(%rsp), %rbx
jne 0x7ecb
movq 0x48(%rsp), %rbp
jmp 0x8031
movq $0x0, 0x10(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x4d55(%rip), %rsi      # 0xcd8d
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
callq 0x4c98
movq %rax, %r12
testq %rax, %rax
je 0x8082
movq (%r12), %r15
testq %r15, %r15
je 0x808a
movq 0x8(%r12), %rax
addq $0x8, %rax
xorl %r14d, %r14d
movq %r15, %rcx
cmpl $0x3, -0x8(%rax)
jne 0x8077
movq (%rax), %rdx
cmpq %r14, %rdx
cmovaq %rdx, %r14
addq $0x20, %rax
decq %rcx
jne 0x8067
jmp 0x808d
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x808d
xorl %r14d, %r14d
testq %r12, %r12
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
testq %r15, %r15
sete %al
orb %cl, %al
jne 0x83be
movq %rbp, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2995
movq %rax, %rdi
callq 0xb302
movq %rax, 0x10(%rbx)
movq %r15, %rdi
movq %r14, 0x98(%rsp)
movq %r14, %rsi
callq 0xb292
movq %rax, 0x18(%rbx)
leaq -0x1(%r15), %r14
movsd 0x3f45(%rip), %xmm1     # 0xc038
movq %r15, 0x20(%rsp)
movq %r15, 0x68(%rsp)
movq %r12, 0x90(%rsp)
movq 0x20(%rsp), %rcx
decq %rcx
movq 0x8(%r12), %rax
movq %rcx, 0x20(%rsp)
shlq $0x5, %rcx
cmpl $0x3, (%rax,%rcx)
jne 0x81f3
movq 0x18(%rsp), %rcx
shlq $0x5, %rcx
cmpq $0x0, 0x8(%rax,%rcx)
je 0x81f3
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movl $0x8, %ebp
xorl %r13d, %r13d
movq 0x8(%r15), %rax
cmpl $0x5, -0x8(%rax,%rbp)
jne 0x81e3
movsd (%rax,%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %r12
sarq $0x3f, %rcx
andq %rcx, %r12
orq %rax, %r12
cmpq %r12, 0x30(%rsp)
jbe 0x81e3
leaq (%r12,%r12,2), %rbx
shlq $0x4, %rbx
movq 0x8(%rsp), %rax
addq 0x20(%rax), %rbx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
callq 0x39d4
movq %rbx, (%rax)
movq 0x10(%rsp), %rax
movb (%rax,%r12), %bl
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x18(%rax), %r12
callq 0x39cb
movsd 0x3e66(%rip), %xmm1     # 0xc038
imulq %r14, %rax
addq %r12, %rax
movb %bl, (%r13,%rax)
movq 0x8(%rsp), %rbx
incq %r13
addq $0x20, %rbp
cmpq (%r15), %r13
jb 0x814a
movq 0x18(%rsp), %rcx
incq %rcx
decq %r14
movq 0x68(%rsp), %r15
movq %rcx, 0x18(%rsp)
cmpq %r15, %rcx
movq 0x90(%rsp), %r12
jne 0x8105
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r15, 0x18(%rsp)
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %r14
callq 0x39cb
imulq %r15, %rax
addq %r14, %rax
movq %r12, 0x20(%rsp)
movb (%r12,%rax), %r13b
xorl %r12d, %r12d
leaq 0x4c05(%rip), %rdx      # 0xce50
movl (%r12,%rdx), %r14d
movl $0x1, %r15d
movl %r14d, %ecx
shll %cl, %r15d
movzbl %r13b, %ebp
btl %r14d, %ebp
jb 0x82f9
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
movl %r14d, %edi
leaq 0x60(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0xb12a
movq 0x10(%rbx), %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x39d4
testq %rax, %rax
je 0x82e9
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rbx
callq 0x39cb
imulq %rbx, %rax
movq 0x60(%rsp), %rcx
addq 0x30(%rsp), %rax
movzbl (%rcx,%rax), %ebx
movl %r14d, %edi
callq 0xaeed
xorl %ecx, %ecx
btl %eax, %ebx
jae 0x82e2
movl %r15d, %ecx
movq 0x8(%rsp), %rbx
jmp 0x82ec
movl %r15d, %ecx
leaq 0x4b5d(%rip), %rdx      # 0xce50
orb %r13b, %cl
movl %ecx, %r13d
testl %ebp, %r15d
jne 0x8310
leaq 0x4(%r12), %rax
cmpq $0xc, %r12
movq %rax, %r12
jne 0x824b
movq 0x10(%rbx), %rdi
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rbx
callq 0x39cb
movq 0x18(%rsp), %r15
imulq %r15, %rax
addq %rbx, %rax
movq 0x8(%rsp), %rbx
movq 0x20(%rsp), %r12
movb %r13b, (%r12,%rax)
incq %r12
movq 0x98(%rsp), %rcx
cmpq %rcx, %r12
jne 0x8224
incq %r15
movq 0x68(%rsp), %rax
cmpq %rax, %r15
jne 0x821c
movq %rcx, %xmm0
movq 0x3d02(%rip), %xmm1     # 0xc070
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x3d06(%rip), %xmm2     # 0xc080
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addsd 0x4966(%rip), %xmm3     # 0xccf8
movapd %xmm3, 0x20(%rsp)
movq %rax, %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, 0x30(%rsp)
movq 0x48(%rsp), %rbp
jmp 0x8409
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2995
movq %rax, %rdi
callq 0xb302
movq %rax, 0x10(%rbx)
movl $0x1, %edi
movl $0x1, %esi
callq 0xb292
movq %rax, 0x18(%rbx)
movsd 0x3c4a(%rip), %xmm0     # 0xc040
movaps %xmm0, 0x30(%rsp)
movsd 0x48ed(%rip), %xmm0     # 0xccf0
movapd %xmm0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2570
movsd 0x38(%rbx), %xmm0
movsd 0x40(%rbx), %xmm1
movapd 0x20(%rsp), %xmm3
minsd %xmm0, %xmm3
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm0
andnpd %xmm3, %xmm0
movlpd %xmm0, 0x38(%rbx)
movapd 0x30(%rsp), %xmm4
movapd %xmm4, %xmm0
minsd %xmm1, %xmm0
movsd 0x48b1(%rip), %xmm2     # 0xcd00
cmpltsd %xmm2, %xmm1
movapd %xmm1, %xmm3
andnpd %xmm0, %xmm3
andpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movapd %xmm4, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rbx)
movq $0x0, 0x28(%rbx)
leaq 0x4162(%rip), %rsi      # 0xc5e2
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
callq 0x4c98
testq %rax, %rax
je 0x8512
movq %rax, %r12
movq (%rax), %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdi
callq 0xb1ac
movq 0x8(%rsp), %rcx
movq %rax, 0x30(%rcx)
cmpq $0x0, (%r12)
je 0x8512
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
movq 0x28(%rcx), %rax
leaq (%rax,%rax,4), %r14
shlq $0x3, %r14
addq 0x30(%rcx), %r14
movq 0x8(%r12), %rdx
addq %rbx, %rdx
movq %r14, %rdi
movq 0x50(%rsp), %rsi
callq 0x8548
testl %eax, %eax
jne 0x8505
movapd 0x30(%rsp), %xmm0
subsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r14)
movq 0x8(%rsp), %rax
incq 0x28(%rax)
incq %r15
addq $0x20, %rbx
cmpq (%r12), %r15
jb 0x84bc
movq 0x8(%rsp), %rbx
cmpq $0x0, 0x10(%rbx)
jne 0x8535
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2995
movq %rax, %rdi
callq 0xb302
movq %rax, 0x10(%rbx)
leaq 0x70(%rsp), %rdi
callq 0x5222
movq %rbx, (%rbp)
jmp 0x7df0