// File: /workspace/github_c_makefile_star2_O3/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 0x6c2a
movq %rax, %r13
leaq 0xa0(%rsp), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x6a4f
testq %rax, %rax
je 0x7d9d
movq %rax, %rbp
movq (%rax), %rbx
testq %rbx, %rbx
jne 0x7d9f
movl $0x60, %edi
callq 0xb080
movq %rax, %r15
movq %r12, %rdi
callq 0xada8
movq %rax, (%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15)
movupd %xmm0, 0x18(%r15)
movupd %xmm0, 0x28(%r15)
movq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rdx
callq 0x4cfd
testl %eax, %eax
jne 0x7d9d
movl 0x70(%rsp), %eax
cmpl $0x7, %eax
je 0x7d8b
cmpl $0x2, %eax
jne 0x7d69
leaq 0x78(%rsp), %rdi
leaq 0x505f(%rip), %rsi      # 0xcda2
callq 0xa772
testq %rax, %rax
je 0x7d72
leaq 0x38(%r15), %rdi
movq (%rax), %rdx
movq %r14, %rsi
callq 0x84ad
testl %eax, %eax
je 0x7db4
leaq 0x5042(%rip), %rdx      # 0xcda9
jmp 0x7d79
leaq 0x500f(%rip), %rdx      # 0xcd7f
jmp 0x7d79
leaq 0x5059(%rip), %rdx      # 0xcdd2
movq %r13, %rdi
movl $0x4, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0x6e50
leaq 0x70(%rsp), %rdi
callq 0x5215
movq %r15, %rdi
callq 0x85b5
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x48(%rsp)
leaq 0x459b(%rip), %rsi      # 0xc35b
leaq 0x70(%rsp), %rdi
movl $0x1004, %edx           # imm = 0x1004
callq 0x4cbc
testq %rax, %rax
je 0x7de5
movq %rax, %r14
movq (%r15), %rdi
callq 0x2570
movq (%r14), %rax
movq %rax, (%r15)
leaq 0x4f8c(%rip), %rsi      # 0xcd78
leaq 0x70(%rsp), %rdi
movl $0x1004, %edx           # imm = 0x1004
callq 0x4cbc
testq %rax, %rax
je 0x7e0d
movq %r15, %rcx
addq $0x8, %rcx
movq (%rax), %rax
movq %rax, (%rcx)
leaq 0x4fe2(%rip), %rsi      # 0xcdf6
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
callq 0x4cbc
testq %rax, %rax
movq %r15, (%rsp)
je 0x7fcc
movq %rax, %r14
movq (%rax), %rbx
movq %rbx, %rdi
callq 0xb080
movq %rax, 0x8(%rsp)
movq %rbx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdi
callq 0xb080
movq %rax, 0x20(%r15)
movq %rbx, 0x20(%rsp)
testq %rbx, %rbx
je 0x7fd5
movq %r14, 0x10(%rsp)
movq %rbp, 0x38(%rsp)
xorl %r14d, %r14d
leaq 0x8e77(%rip), %r12      # 0x10cf0
movq 0x20(%r15), %r13
leaq (%r14,%r14,2), %r15
shlq $0x4, %r15
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rbp
movq %r14, %rax
shlq $0x5, %rax
movq 0x8(%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 0x7fb3
addq %rax, %rbp
movq %rbp, %rdi
leaq 0x4f91(%rip), %rsi      # 0xce61
movl $0x6, %edx
callq 0x4cbc
testq %rax, %rax
je 0x7ef2
cmpb $0x1, (%rax)
jne 0x7ef2
movq 0x8(%rsp), %rax
movb $-0x1, (%rax,%r14)
movb $0x1, %bl
jmp 0x7ef4
xorl %ebx, %ebx
addq %r15, %r13
movq %rbp, %rdi
leaq 0x4e07(%rip), %rsi      # 0xcd08
movl $0x4, %edx
callq 0x4cbc
testq %rax, %rax
je 0x7f3a
movq (%rax), %rsi
movq 0x48(%rsp), %rdi
callq 0x6690
testq %rax, %rax
je 0x7f3a
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 0x4cbc
testq %rax, %rax
je 0x7f72
movq (%rax), %rsi
movq 0x48(%rsp), %rdi
callq 0x6690
testq %rax, %rax
je 0x7f72
movl (%r15,%r12), %ecx
movq %rax, (%r13,%rcx,8)
testb %bl, %bl
jne 0x7fa6
movq -0x8(%r15,%r12), %rsi
movq %rbp, %rdi
movl $0x6, %edx
callq 0x4cbc
testq %rax, %rax
je 0x7fa6
cmpb $0x1, (%rax)
jne 0x7fa6
movb (%r15,%r12), %cl
movl $0x1, %eax
shll %cl, %eax
movq 0x8(%rsp), %rcx
orb %al, (%rcx,%r14)
addq $0x18, %r15
cmpq $0xa0, %r15
jne 0x7f40
incq %r14
cmpq 0x20(%rsp), %r14
movq (%rsp), %r15
jne 0x7e79
movq 0x38(%rsp), %rbp
jmp 0x7fde
movq $0x0, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x4e18(%rip), %rsi      # 0xcdfd
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
callq 0x4cbc
testq %rax, %rax
je 0x8323
movq %rax, %r12
movq (%rax), %rbx
testq %rbx, %rbx
je 0x8323
movq 0x8(%r12), %rax
addq $0x8, %rax
xorl %r14d, %r14d
movq %rbx, %rcx
cmpl $0x3, -0x8(%rax)
jne 0x802b
movq (%rax), %rdx
cmpq %r14, %rdx
cmovaq %rdx, %r14
addq $0x20, %rax
decq %rcx
jne 0x801b
testq %r14, %r14
je 0x8323
movq %rbp, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2979
movq %rax, %rdi
callq 0xb1d6
movq %rax, 0x10(%r15)
movq %rbx, %rdi
movq %r14, 0x98(%rsp)
movq %r14, %rsi
callq 0xb166
movq %rax, 0x18(%r15)
leaq -0x1(%rbx), %rax
movq %rax, 0x68(%rsp)
movsd 0x4074(%rip), %xmm1     # 0xc100
movq %rbx, 0x10(%rsp)
movq %rbx, 0x60(%rsp)
movq %r12, 0x90(%rsp)
movq 0x10(%rsp), %rax
decq %rax
movq 0x8(%r12), %rcx
movq %rax, 0x10(%rsp)
shlq $0x5, %rax
cmpl $0x3, (%rcx,%rax)
jne 0x817a
movq 0x40(%rsp), %rdx
shlq $0x5, %rdx
movq 0x8(%rcx,%rdx), %rax
testq %rax, %rax
je 0x817a
leaq (%rcx,%rdx), %r15
addq $0x8, %r15
movl $0x8, %ebp
xorl %r13d, %r13d
movq 0x8(%r15), %rcx
cmpl $0x5, -0x8(%rcx,%rbp)
jne 0x816a
movsd (%rcx,%rbp), %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %r12
sarq $0x3f, %rdx
andq %rdx, %r12
orq %rcx, %r12
cmpq %r12, 0x20(%rsp)
jbe 0x816a
leaq (%r12,%r12,2), %r14
shlq $0x4, %r14
movq (%rsp), %rbx
addq 0x20(%rbx), %r14
movq 0x10(%rbx), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
callq 0x3a57
movq %r14, (%rax)
movq 0x8(%rsp), %rax
movb (%rax,%r12), %r14b
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %r12
callq 0x3a4e
movsd 0x3fa7(%rip), %xmm1     # 0xc100
imulq 0x68(%rsp), %rax
addq %r12, %rax
movb %r14b, (%r13,%rax)
movq (%r15), %rax
incq %r13
addq $0x20, %rbp
cmpq %rax, %r13
jb 0x80e5
movq 0x40(%rsp), %rcx
incq %rcx
decq 0x68(%rsp)
movq 0x60(%rsp), %rbx
movq %rcx, 0x40(%rsp)
cmpq %rbx, %rcx
movq 0x90(%rsp), %r12
jne 0x809e
xorl %ebx, %ebx
movq (%rsp), %r15
xorl %r12d, %r12d
movq %rbx, 0x10(%rsp)
movq 0x10(%r15), %rdi
movq 0x18(%r15), %r14
callq 0x3a4e
imulq %rbx, %rax
addq %r14, %rax
movq %r12, 0x20(%rsp)
movb (%r12,%rax), %r13b
xorl %r12d, %r12d
leaq 0x4ce9(%rip), %rax      # 0xcec0
movl (%r12,%rax), %r14d
movl $0x1, %ebp
movl %r14d, %ecx
shll %cl, %ebp
movzbl %r13b, %eax
btl %r14d, %eax
jb 0x827d
movq 0x20(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %rbx, 0x50(%rsp)
movl %r14d, %edi
leaq 0x58(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0xb005
movq 0x10(%r15), %rdi
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x3a57
testq %rax, %rax
je 0x826a
movq 0x10(%r15), %rdi
movq 0x18(%r15), %r15
movq 0x50(%rsp), %rbx
callq 0x3a4e
imulq %rbx, %rax
movq 0x58(%rsp), %rcx
addq %r15, %rax
movzbl (%rcx,%rax), %ebx
movl %r14d, %edi
callq 0xadd5
xorl %ecx, %ecx
btl %eax, %ebx
jae 0x825f
movl %ebp, %ecx
movq (%rsp), %r15
movq 0x10(%rsp), %rbx
jmp 0x826c
movl %ebp, %ecx
orb %cl, %r13b
addq $0x4, %r12
cmpq $0x10, %r12
jne 0x81d0
movq 0x10(%r15), %rdi
movq 0x18(%r15), %rbx
callq 0x3a4e
imulq 0x10(%rsp), %rax
addq %rbx, %rax
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r12
movb %r13b, (%r12,%rax)
incq %r12
movq 0x98(%rsp), %rcx
cmpq %rcx, %r12
jne 0x81b0
incq %rbx
movq 0x60(%rsp), %rax
cmpq %rax, %rbx
jne 0x81a8
movq %rcx, %xmm0
movq 0x3d8d(%rip), %xmm1     # 0xc060
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x3d91(%rip), %xmm2     # 0xc070
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addsd 0x4a69(%rip), %xmm3     # 0xcd60
movapd %xmm3, 0x10(%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, 0x20(%rsp)
movq 0x38(%rsp), %rbp
jmp 0x836e
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2979
movq %rax, %rdi
callq 0xb1d6
movq %rax, 0x10(%r15)
movl $0x1, %edi
movl $0x1, %esi
callq 0xb166
movq %rax, 0x18(%r15)
movsd 0x3dad(%rip), %xmm0     # 0xc108
movaps %xmm0, 0x20(%rsp)
movsd 0x4a00(%rip), %xmm0     # 0xcd68
movapd %xmm0, 0x10(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2570
movsd 0x38(%r15), %xmm0
movsd 0x40(%r15), %xmm1
movapd 0x10(%rsp), %xmm3
minsd %xmm0, %xmm3
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm0
andnpd %xmm3, %xmm0
movlpd %xmm0, 0x38(%r15)
movapd 0x20(%rsp), %xmm4
movapd %xmm4, %xmm0
minsd %xmm1, %xmm0
movsd 0x49b9(%rip), %xmm2     # 0xcd70
cmpltsd %xmm2, %xmm1
movapd %xmm1, %xmm3
andnpd %xmm0, %xmm3
andpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movapd %xmm4, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%r15)
movq $0x0, 0x28(%r15)
leaq 0x4269(%rip), %rsi      # 0xc652
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
callq 0x4cbc
testq %rax, %rax
je 0x8478
movq %rax, %r12
movq (%rax), %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdi
callq 0xb080
movq (%rsp), %rcx
movq %rax, 0x30(%rcx)
cmpq $0x0, (%r12)
je 0x8478
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%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 0x48(%rsp), %rsi
callq 0x84ad
testl %eax, %eax
jne 0x846b
movapd 0x20(%rsp), %xmm0
subsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r14)
movq (%rsp), %rax
incq 0x28(%rax)
incq %r15
addq $0x20, %rbx
cmpq (%r12), %r15
jb 0x8424
movq (%rsp), %rbx
cmpq $0x0, 0x10(%rbx)
jne 0x849a
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2979
movq %rax, %rdi
callq 0xb1d6
movq %rax, 0x10(%rbx)
leaq 0x70(%rsp), %rdi
callq 0x5215
movq %rbx, (%rbp)
jmp 0x7d9f