// File: /workspace/github_c_makefile_star2_O0/TurkeyMcMac[P]ts3d/src/d3d.c
// Function: draw_sprites
static void draw_sprites(
	d3d_camera *cam,
	d3d_vec_s cam_pos,
	d3d_scalar cam_facing,
	size_t n_sprites,
	const d3d_sprite_s sprites[])
{
	size_t i;
	if (n_sprites == cam->last_n_sprites && sprites == cam->last_sprites) {
		// This assumes the sprites didn't move much, and are mostly
		// sorted. Therefore, insertion sort is used.
		for (i = 0; i < n_sprites; ++i) {
			size_t move_to;
			struct d3d_sprite_order ord = cam->order[i];
			size_t s = ord.index;
			ord.dist = hypot(sprites[s].pos.x - cam_pos.x,
				sprites[s].pos.y - cam_pos.y);
			move_to = i;
			for (long j = (long)i - 1; j >= 0; --j) {
				if (cam->order[j].dist <= ord.dist) break;
				move_to = j;
			}
			memmove(cam->order + move_to + 1, cam->order + move_to,
				(i - move_to) * sizeof(*cam->order));
			cam->order[move_to] = ord;
	       }
	} else {
		if (n_sprites > cam->order_buf_cap) {
			struct d3d_sprite_order *new_order;
			size_t size = n_sprites * sizeof(*cam->order);
			if (size / sizeof(*cam->order) == n_sprites
			 && (new_order = d3d_realloc(cam->order, size))) {
				cam->order = new_order;
				cam->order_buf_cap = n_sprites;
			} else {
				// XXX Silently truncate the list of sprites
				// drawn. This may be a bad decision, but
				// failure is unlikely and this shouldn't break
				// any client code.
				n_sprites = cam->order_buf_cap;
			}
		}
		for (i = 0; i < n_sprites; ++i) {
			struct d3d_sprite_order *ord = &cam->order[i];
			ord->dist = hypot(sprites[i].pos.x - cam_pos.x,
				sprites[i].pos.y - cam_pos.y);
			ord->index = i;
		}
		qsort(cam->order, n_sprites, sizeof(*cam->order),
			compar_sprite_order);
		cam->last_sprites = sprites;
		cam->last_n_sprites = n_sprites;
	}
	i = n_sprites;
	while (i--) {
		struct d3d_sprite_order *ord = &cam->order[i];
		draw_sprite_dist(cam, cam_pos, cam_facing, &sprites[ord->index],
			ord->dist);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movsd %xmm0, -0x30(%rbp)
movsd %xmm1, -0x28(%rbp)
movq %rdi, -0x38(%rbp)
movsd %xmm2, -0x40(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdx, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x68(%rcx), %rax
jne 0x39db
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x60(%rcx), %rax
jne 0x39db
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x39d6
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x70(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rax
imulq $0x30, -0x78(%rbp), %rcx
addq %rcx, %rax
movsd (%rax), %xmm1
subsd -0x30(%rbp), %xmm1
movq -0x50(%rbp), %rax
imulq $0x30, -0x78(%rbp), %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
subsd -0x28(%rbp), %xmm0
movsd %xmm1, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd -0x8(%rbp), %xmm0
movsd -0x10(%rbp), %xmm1
callq 0x21a0
movsd %xmm0, -0x70(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x3968
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm1
movsd -0x70(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3952
jmp 0x3968
movq -0x80(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x3927
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
addq $0x10, %rdi
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rsi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq -0x58(%rbp), %rdx
subq -0x60(%rbp), %rdx
shlq $0x4, %rdx
callq 0x22b0
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x70(%rbp), %rcx
movq %rcx, (%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x3892
jmp 0x3b29
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jbe 0x3a55
movq -0x48(%rbp), %rax
shlq $0x4, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
shrq $0x4, %rax
cmpq -0x48(%rbp), %rax
jne 0x3a47
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x90(%rbp), %rsi
callq 0x2770
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x3a47
movq -0x88(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x3a53
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x3a55
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x3af4
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x50(%rbp), %rax
imulq $0x30, -0x58(%rbp), %rcx
addq %rcx, %rax
movsd (%rax), %xmm1
subsd -0x30(%rbp), %xmm1
movq -0x50(%rbp), %rax
imulq $0x30, -0x58(%rbp), %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
subsd -0x28(%rbp), %xmm0
movsd %xmm1, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
callq 0x21a0
movq -0x98(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x58(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x3a5d
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x48(%rbp), %rsi
movl $0x10, %edx
leaq 0x9e4(%rip), %rcx       # 0x44f0
callq 0x2330
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x58(%rbp)
cmpq $0x0, %rax
je 0x3b98
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rdi
movsd -0x40(%rbp), %xmm2
movq -0x50(%rbp), %rsi
movq -0xa0(%rbp), %rax
imulq $0x30, 0x8(%rax), %rax
addq %rax, %rsi
movq -0xa0(%rbp), %rax
movsd (%rax), %xmm3
movsd -0x30(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
callq 0x4550
jmp 0x3b31
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)