// File: /workspace/github_c_makefile_star2_O1/TurkeyMcMac[P]ts3d/src/d3d.c
// Function: d3d_new_camera
d3d_camera *d3d_new_camera(
	d3d_scalar fovx,
	d3d_scalar fovy,
	size_t width,
	size_t height,
	d3d_pixel empty_pixel)
{
	size_t size;
	size_t pixels_size, txtr_offset, tans_offset, dists_offset;
	d3d_texture *empty_txtr;
	d3d_camera *cam;
	size = offsetof(d3d_camera, pixels);
	pixels_size = width * height * sizeof(d3d_pixel);
	if (width != 0 && pixels_size / sizeof(d3d_pixel) / width != height)
		return NULL;
	CHECKED_ADD(size, pixels_size);
	// This type probably has the alignment of a one-pixel texture:
	typedef struct { size_t width, height; d3d_pixel pixels[1]; } one_pix;
	ALIGN_SIZE(size, one_pix);
	txtr_offset = size;
	CHECKED_ADD(size, texture_size(1, 1));
	ALIGN_SIZE(size, d3d_scalar);
	tans_offset = size;
	if (height * sizeof(d3d_scalar) / sizeof(d3d_scalar) != height)
		return NULL;
	CHECKED_ADD(size, height * sizeof(d3d_scalar));
	dists_offset = size;
	if (width * sizeof(d3d_scalar) / sizeof(d3d_scalar) != width)
		return NULL;
	CHECKED_ADD(size, width * sizeof(d3d_scalar));
	cam = d3d_malloc(size);
	if (!cam) return NULL;
	// The members 'tans' and 'dists' are actually pointers to parts of the
	// same allocation. 'empty_txtr' is glued on before them, but is not a
	// member.
	empty_txtr = (void *)((char *)cam + txtr_offset);
	cam->tans = (void *)((char *)cam + tans_offset);
	cam->dists = (void *)((char *)cam + dists_offset);
	// Just do basic protection against non-positive FOVs as they might
	// cause issues. I could do something better than silently clamping, but
	// what do you expect a non-positive FOV to do anyway?
	cam->fov.x = fovx > (d3d_scalar)0.0 ? fovx : 0.001;
	cam->fov.y = fovy > (d3d_scalar)0.0 ? fovy : 0.001;
	cam->width = width;
	cam->height = height;
	empty_txtr->width = 1;
	empty_txtr->height = 1;
	empty_txtr->pixels[0] = empty_pixel;
	cam->blank_block.faces[D3D_DPOSX] =
	cam->blank_block.faces[D3D_DPOSY] =
	cam->blank_block.faces[D3D_DNEGX] =
	cam->blank_block.faces[D3D_DNEGY] =
	cam->blank_block.faces[D3D_DUP] =
	cam->blank_block.faces[D3D_DDOWN] = empty_txtr;
	cam->order = NULL;
	cam->order_buf_cap = 0;
	cam->last_sprites = NULL;
	cam->last_n_sprites = 0;
	empty_camera_pixels(cam);
	for (size_t y = 0; y < height; ++y) {
		d3d_scalar angle =
			cam->fov.y * ((d3d_scalar)0.5 - (d3d_scalar)y / height);
		cam->tans[y] = tan(angle);
	}
	return cam;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r8d
movq %rsi, %rbx
movq %rdi, %r12
movq %rdi, %rax
mulq %rsi
movq %rax, %r15
seto %al
testq %rdi, %rdi
je 0x270c
cmpq $-0x80, %r15
setae %cl
orb %al, %cl
je 0x2715
jmp 0x273c
cmpq $-0x81, %r15
ja 0x273c
leaq 0x80(%r15), %rax
addq $0x87, %r15
andq $-0x8, %r15
cmpq %rax, %r15
cmovbeq %rax, %r15
setb %al
cmpq $-0x11, %r15
setae %cl
orb %al, %cl
je 0x2751
xorl %r14d, %r14d
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11(%r15), %rax
movq %r15, %rcx
andq $-0x8, %rcx
addq $0x18, %rcx
cmpq %rax, %rcx
movq %rax, %r13
cmovaq %rcx, %r13
xorl %r14d, %r14d
movq %rbx, %rdx
shrq $0x3d, %rdx
jne 0x273f
cmpq %rax, %rcx
jb 0x273f
leaq (,%rbx,8), %rax
addq %r13, %rax
cmpq %r13, %rax
movq %r13, %rbp
cmovaq %rax, %rbp
movabsq $0x2000000000000000, %rcx # imm = 0x2000000000000000
xorl %r14d, %r14d
cmpq %rcx, %r12
jae 0x273f
cmpq %r13, %rax
jb 0x273f
leaq (,%r12,8), %rdi
addq %rbp, %rdi
jb 0x273c
movapd %xmm0, 0x10(%rsp)
movapd %xmm1, 0x20(%rsp)
movl %r8d, 0x8(%rsp)
callq 0x2150
testq %rax, %rax
je 0x273c
movq %rax, %r14
addq %r15, %rax
addq %r14, %r13
movq %r13, 0x70(%r14)
addq %r14, %rbp
movq %rbp, 0x78(%r14)
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movapd 0x10(%rsp), %xmm3
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm3
movsd 0x981a(%rip), %xmm2     # 0xc020
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movlpd %xmm1, (%r14)
movapd 0x20(%rsp), %xmm1
cmpltsd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movlpd %xmm0, 0x8(%r14)
movq %r12, 0x10(%r14)
movq %rbx, 0x18(%r14)
movl $0x1, %ecx
movq %rcx, (%r14,%r15)
movq %rcx, 0x8(%r14,%r15)
movl 0x8(%rsp), %ecx
movb %cl, 0x10(%r14,%r15)
movq %rax, 0x48(%r14)
movq %rax, 0x40(%r14)
movq %rax, 0x38(%r14)
movq %rax, 0x30(%r14)
movq %rax, 0x28(%r14)
movq %rax, 0x20(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x50(%r14)
movupd %xmm0, 0x60(%r14)
movq 0x18(%r14), %rax
imulq 0x10(%r14), %rax
testq %rax, %rax
je 0x28a5
movb 0x10(%r14,%r15), %al
xorl %ecx, %ecx
movb %al, 0x80(%r14,%rcx)
incq %rcx
movq 0x18(%r14), %rdx
imulq 0x10(%r14), %rdx
cmpq %rdx, %rcx
jb 0x288c
testq %rbx, %rbx
je 0x273f
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r15d, %r15d
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
divsd 0x8(%rsp), %xmm1
movsd 0x9753(%rip), %xmm0     # 0xc028
subsd %xmm1, %xmm0
mulsd 0x8(%r14), %xmm0
callq 0x20f0
movq 0x70(%r14), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq %r15, %rbx
jne 0x28bf
jmp 0x273f