// File: /workspace/github_c_makefile_star2_O0/FredTheDino[P]Fog/src/engine/logic/block_physics.cpp
// Function: Physics::check_overlap(Physics::Body*, Physics::Body*)
Overlap check_overlap(Body *body_a, Body *body_b) {
    // NOTE: If I find that dragging along a surface is jagged,
    // we could try having weighted directions and add a little bit
    // of weight to the ones along the closing velocity. It might
    // help. I don't know.

    Overlap overlap = {body_a, body_b, -1.0f};
    if ((body_a->layer & body_b->layer) == 0) return overlap;

    Shape shape_a = find_shape(body_a->shape);
    Shape shape_b = find_shape(body_b->shape);

    Vec2 center = (body_a->position + body_b->position) * 0.5;

    Vec2 relative_position = (body_b->position) - (body_a->position);

    Vec2 scale = inverse(body_a->scale);
    List<Vec2> normals = shape_a.normals;
    for (u32 n = 0; n < 2; n++) {
        for (u32 i = 0; i < normals.length; i++) {
            Vec2 normal, axis_a, axis_b;
            normal = normals[i];
            normal = normalize(hadamard(normal, scale));

            if (n == 0) {
                f32 rotation = body_a->rotation - body_b->rotation;
                axis_a = normal;
                axis_b = rotate(normal, rotation);
                normal = rotate(normal, body_a->rotation);
                if (debug_view_is_on())
                    Renderer::push_line(MAX_LAYER, body_a->position, body_a->position + normal,
                                        V4(0.6, 0.0, 0.3, 1.0));
            } else {
                f32 rotation = body_b->rotation - body_a->rotation;
                axis_a = rotate(normal, rotation);
                axis_b = normal;
                normal = rotate(normal, body_b->rotation);
                if (debug_view_is_on())
                    Renderer::push_line(MAX_LAYER, body_b->position, body_b->position + normal,
                                        V4(0.0, 0.6, 0.3, 1.0));
            }

#if 0
            Vec2 axis_a, axis_b;
            axis_a = normal;
            axis_b = normal;
#endif

            Limit limit_a, limit_b;
            limit_a = project_shape(shape_a, axis_a, body_a->scale, body_a->offset);
            limit_b = project_shape(shape_b, axis_b, body_b->scale, body_b->offset);

            f32 projected_distance = dot(relative_position, normal);


            f32 depth;
            if (projected_distance > 0)
                depth = limit_a.upper - limit_b.lower - projected_distance;
            else
                depth = limit_b.upper - limit_a.lower + projected_distance;

            if (debug_view_is_on()) {
                Vec2 a1, a2;
                a1 = body_a->position + normal * limit_a.lower;
                a2 = body_a->position + normal * limit_a.upper;
                Renderer::push_line(MAX_LAYER, a1, a2, V4(1, 0, 1, 1));

                Vec2 b1, b2;
                b1 = body_b->position + normal * limit_b.lower;
                b2 = body_b->position + normal * limit_b.upper;
                Renderer::push_line(MAX_LAYER, b1, b2, V4(0, 1, 1, 1));

                //debug_line(body_a->position, body_a->position + axis_a);
                //debug_line(body_b->position, body_b->position + axis_b);
                Vec4 color = depth > 0 ? V4(1, 1, 0, 1) : V4(1, 0, 0, 1);
                Renderer::push_line(MAX_LAYER, center, center + normal * depth, color);
            }

            if (depth < 0)
                return overlap;

            if (depth < overlap.depth || overlap.depth == -1.0f) {
                overlap.depth = depth;
                overlap.normal = normal;
            }
        }

        normals = shape_b.normals;
        scale = inverse(body_b->scale);
    }

    if (debug_view_is_on())
        Renderer::push_line(MAX_LAYER, center, center + overlap.normal * overlap.depth,
                            V4(0, 1, 0, 1));

    if (dot(overlap.normal, relative_position) < 0) {
        // Make sure the normal faces body a
        overlap.normal = -overlap.normal;
    }

    overlap.is_valid = true;
    return overlap;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x460, %rsp            # imm = 0x460
movq %rdi, -0x3f0(%rbp)
movq %rdi, %rax
movq %rax, -0x3e8(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, (%rdi)
movq -0x10(%rbp), %rax
movq %rax, 0x8(%rdi)
movss 0x11d1d(%rip), %xmm0    # 0x45018
movss %xmm0, 0x10(%rdi)
xorps %xmm0, %xmm0
movss %xmm0, 0x14(%rdi)
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rdi)
movb $0x0, 0x1c(%rdi)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
andl 0x4(%rcx), %eax
cmpl $0x0, %eax
jne 0x3332c
jmp 0x33e99
movq -0x8(%rbp), %rax
movl (%rax), %esi
leaq -0x50(%rbp), %rdi
callq 0x326a0
movq -0x10(%rbp), %rax
movl (%rax), %esi
leaq -0x90(%rbp), %rdi
callq 0x326a0
movq -0x8(%rbp), %rdi
addq $0x14, %rdi
movq -0x10(%rbp), %rax
movq 0x14(%rax), %rax
movq %rax, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
callq 0x40dc0
movlpd %xmm0, -0xa0(%rbp)
leaq -0xa0(%rbp), %rdi
movss 0x11c88(%rip), %xmm0    # 0x45010
callq 0x5810
movlpd %xmm0, -0x98(%rbp)
movq -0x10(%rbp), %rdi
addq $0x14, %rdi
movq -0x8(%rbp), %rax
movq 0x14(%rax), %rax
movq %rax, -0xb8(%rbp)
movsd -0xb8(%rbp), %xmm0
callq 0x5850
movlpd %xmm0, -0xb0(%rbp)
movq -0x8(%rbp), %rax
movq 0x1c(%rax), %rax
movq %rax, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
callq 0xb490
movlpd %xmm0, -0xc0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl $0x0, -0xe4(%rbp)
cmpl $0x2, -0xe4(%rbp)
jae 0x33d75
movl $0x0, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
cmpl -0xdc(%rbp), %eax
jae 0x33d1b
movl -0xe8(%rbp), %esi
leaq -0xe0(%rbp), %rdi
callq 0x43650
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x120(%rbp)
movsd -0x118(%rbp), %xmm0
movsd -0x120(%rbp), %xmm1
callq 0xb450
movlpd %xmm0, -0x110(%rbp)
movsd -0x110(%rbp), %xmm0
callq 0xb410
movlpd %xmm0, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpl $0x0, -0xe4(%rbp)
jne 0x3360b
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm0
movq -0x10(%rbp), %rax
movss 0x10(%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x124(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x138(%rbp)
movss -0x124(%rbp), %xmm1
movsd -0x138(%rbp), %xmm0
callq 0xb500
movlpd %xmm0, -0x12c(%rbp)
movq -0x12c(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm1
movsd -0x148(%rbp), %xmm0
callq 0xb500
movlpd %xmm0, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0xf0(%rbp)
callq 0xf620
cmpb $0x0, %al
je 0x33606
movq -0x8(%rbp), %rax
movq 0x14(%rax), %rax
movq %rax, -0x150(%rbp)
movq -0x8(%rbp), %rdi
addq $0x14, %rdi
movq -0xf0(%rbp), %rax
movq %rax, -0x160(%rbp)
movsd -0x160(%rbp), %xmm0
callq 0x40dc0
movlpd %xmm0, -0x158(%rbp)
movss 0x11c9c(%rip), %xmm0    # 0x45248
xorps %xmm1, %xmm1
movss 0x11c69(%rip), %xmm2    # 0x45220
movss 0x11a49(%rip), %xmm3    # 0x45008
callq 0xbc90
movlpd %xmm0, -0x170(%rbp)
movlpd %xmm1, -0x168(%rbp)
movsd -0x150(%rbp), %xmm0
movsd -0x158(%rbp), %xmm1
movsd -0x170(%rbp), %xmm2
movsd -0x168(%rbp), %xmm3
movl $0xe, %edi
movss 0x11c3b(%rip), %xmm4    # 0x4523c
callq 0x2d860
jmp 0x33755
movq -0x10(%rbp), %rax
movss 0x10(%rax), %xmm0
movq -0x8(%rbp), %rax
movss 0x10(%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x174(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x188(%rbp)
movss -0x174(%rbp), %xmm1
movsd -0x188(%rbp), %xmm0
callq 0xb500
movlpd %xmm0, -0x17c(%rbp)
movq -0x17c(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x10(%rbp), %rax
movss 0x10(%rax), %xmm1
movsd -0x198(%rbp), %xmm0
callq 0xb500
movlpd %xmm0, -0x190(%rbp)
movq -0x190(%rbp), %rax
movq %rax, -0xf0(%rbp)
callq 0xf620
cmpb $0x0, %al
je 0x33753
movq -0x10(%rbp), %rax
movq 0x14(%rax), %rax
movq %rax, -0x1a0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x14, %rdi
movq -0xf0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movsd -0x1b0(%rbp), %xmm0
callq 0x40dc0
movlpd %xmm0, -0x1a8(%rbp)
xorps %xmm0, %xmm0
movss 0x11b4c(%rip), %xmm1    # 0x45248
movss 0x11b1c(%rip), %xmm2    # 0x45220
movss 0x118fc(%rip), %xmm3    # 0x45008
callq 0xbc90
movlpd %xmm0, -0x1c0(%rbp)
movlpd %xmm1, -0x1b8(%rbp)
movsd -0x1a0(%rbp), %xmm0
movsd -0x1a8(%rbp), %xmm1
movsd -0x1c0(%rbp), %xmm2
movsd -0x1b8(%rbp), %xmm3
movl $0xe, %edi
movss 0x11aee(%rip), %xmm4    # 0x4523c
callq 0x2d860
jmp 0x33755
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups -0x30(%rbp), %xmm2
movups -0x20(%rbp), %xmm3
movaps %xmm3, -0x1f0(%rbp)
movaps %xmm2, -0x200(%rbp)
movaps %xmm1, -0x210(%rbp)
movaps %xmm0, -0x220(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x228(%rbp)
movq -0x8(%rbp), %rax
movq 0x1c(%rax), %rax
movq %rax, -0x230(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x238(%rbp)
movsd -0x228(%rbp), %xmm0
movsd -0x230(%rbp), %xmm1
movsd -0x238(%rbp), %xmm2
movaps -0x220(%rbp), %xmm3
movaps -0x210(%rbp), %xmm4
movaps -0x200(%rbp), %xmm5
movaps -0x1f0(%rbp), %xmm6
movq %rsp, %rax
movups %xmm6, 0x30(%rax)
movups %xmm5, 0x20(%rax)
movups %xmm4, 0x10(%rax)
movups %xmm3, (%rax)
callq 0x327f0
movlpd %xmm0, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movups -0x90(%rbp), %xmm0
movups -0x80(%rbp), %xmm1
movups -0x70(%rbp), %xmm2
movups -0x60(%rbp), %xmm3
movaps %xmm3, -0x250(%rbp)
movaps %xmm2, -0x260(%rbp)
movaps %xmm1, -0x270(%rbp)
movaps %xmm0, -0x280(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x10(%rbp), %rax
movq 0x1c(%rax), %rax
movq %rax, -0x290(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x298(%rbp)
movsd -0x288(%rbp), %xmm0
movsd -0x290(%rbp), %xmm1
movsd -0x298(%rbp), %xmm2
movaps -0x280(%rbp), %xmm3
movaps -0x270(%rbp), %xmm4
movaps -0x260(%rbp), %xmm5
movaps -0x250(%rbp), %xmm6
movq %rsp, %rax
movups %xmm6, 0x30(%rax)
movups %xmm5, 0x20(%rax)
movups %xmm4, 0x10(%rax)
movups %xmm3, (%rax)
callq 0x327f0
movlpd %xmm0, -0x240(%rbp)
movq -0x240(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movsd -0x2a8(%rbp), %xmm0
movsd -0x2b0(%rbp), %xmm1
callq 0xb340
movss %xmm0, -0x29c(%rbp)
movss -0x29c(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x33935
movss -0x1c4(%rbp), %xmm0
subss -0x1d0(%rbp), %xmm0
subss -0x29c(%rbp), %xmm0
movss %xmm0, -0x2b4(%rbp)
jmp 0x33955
movss -0x1cc(%rbp), %xmm0
subss -0x1c8(%rbp), %xmm0
addss -0x29c(%rbp), %xmm0
movss %xmm0, -0x2b4(%rbp)
callq 0xf620
cmpb $0x0, %al
je 0x33ca1
movq -0x8(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x420(%rbp)
movss -0x1c8(%rbp), %xmm0
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x408(%rbp)
callq 0x5810
movq -0x420(%rbp), %rdi
movlpd %xmm0, -0x2d8(%rbp)
movsd -0x2d8(%rbp), %xmm0
callq 0x40dc0
movq -0x408(%rbp), %rdi
movlpd %xmm0, -0x2d0(%rbp)
movq -0x2d0(%rbp), %rax
movq %rax, -0x2c0(%rbp)
movq -0x8(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x418(%rbp)
movss -0x1c4(%rbp), %xmm0
callq 0x5810
movq -0x418(%rbp), %rdi
movlpd %xmm0, -0x2e8(%rbp)
movsd -0x2e8(%rbp), %xmm0
callq 0x40dc0
movlpd %xmm0, -0x2e0(%rbp)
movq -0x2e0(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movq -0x2c0(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq -0x2c8(%rbp), %rax
movq %rax, -0x2f8(%rbp)
xorps %xmm1, %xmm1
movss %xmm1, -0x3f4(%rbp)
movss 0x115c6(%rip), %xmm3    # 0x45008
movss %xmm3, -0x3f8(%rbp)
movaps %xmm3, %xmm0
movaps %xmm3, %xmm2
callq 0xbc90
movlpd %xmm0, -0x308(%rbp)
movlpd %xmm1, -0x300(%rbp)
movsd -0x2f0(%rbp), %xmm0
movsd -0x2f8(%rbp), %xmm1
movsd -0x308(%rbp), %xmm2
movsd -0x300(%rbp), %xmm3
movl $0xe, %edi
movss 0x117aa(%rip), %xmm4    # 0x4523c
callq 0x2d860
movq -0x408(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x410(%rbp)
movss -0x1d0(%rbp), %xmm0
callq 0x5810
movq -0x410(%rbp), %rdi
movlpd %xmm0, -0x328(%rbp)
movsd -0x328(%rbp), %xmm0
callq 0x40dc0
movq -0x408(%rbp), %rdi
movlpd %xmm0, -0x320(%rbp)
movq -0x320(%rbp), %rax
movq %rax, -0x310(%rbp)
movq -0x10(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x400(%rbp)
movss -0x1cc(%rbp), %xmm0
callq 0x5810
movq -0x400(%rbp), %rdi
movlpd %xmm0, -0x338(%rbp)
movsd -0x338(%rbp), %xmm0
callq 0x40dc0
movss -0x3f8(%rbp), %xmm3
movaps %xmm0, %xmm1
movss -0x3f4(%rbp), %xmm0
movlpd %xmm1, -0x330(%rbp)
movq -0x330(%rbp), %rax
movq %rax, -0x318(%rbp)
movq -0x310(%rbp), %rax
movq %rax, -0x340(%rbp)
movq -0x318(%rbp), %rax
movq %rax, -0x348(%rbp)
movaps %xmm3, %xmm1
movaps %xmm3, %xmm2
callq 0xbc90
movlpd %xmm0, -0x358(%rbp)
movlpd %xmm1, -0x350(%rbp)
movsd -0x340(%rbp), %xmm0
movsd -0x348(%rbp), %xmm1
movsd -0x358(%rbp), %xmm2
movsd -0x350(%rbp), %xmm3
movl $0xe, %edi
movss 0x11684(%rip), %xmm4    # 0x4523c
callq 0x2d860
movss -0x2b4(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x33bf5
xorps %xmm2, %xmm2
movss 0x11430(%rip), %xmm3    # 0x45008
movaps %xmm3, %xmm0
movaps %xmm3, %xmm1
callq 0xbc90
movlpd %xmm0, -0x368(%rbp)
movlpd %xmm1, -0x360(%rbp)
jmp 0x33c1b
xorps %xmm2, %xmm2
movss 0x11408(%rip), %xmm3    # 0x45008
movaps %xmm3, %xmm0
movaps %xmm2, %xmm1
callq 0xbc90
movlpd %xmm0, -0x368(%rbp)
movlpd %xmm1, -0x360(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x370(%rbp)
movss -0x2b4(%rbp), %xmm0
leaq -0xf0(%rbp), %rdi
callq 0x5810
movlpd %xmm0, -0x380(%rbp)
movsd -0x380(%rbp), %xmm0
leaq -0x98(%rbp), %rdi
callq 0x40dc0
movlpd %xmm0, -0x378(%rbp)
movups -0x368(%rbp), %xmm0
movaps %xmm0, -0x390(%rbp)
movsd -0x370(%rbp), %xmm0
movsd -0x378(%rbp), %xmm1
movsd -0x390(%rbp), %xmm2
movsd -0x388(%rbp), %xmm3
movl $0xe, %edi
movss 0x115a0(%rip), %xmm4    # 0x4523c
callq 0x2d860
xorps %xmm0, %xmm0
ucomiss -0x2b4(%rbp), %xmm0
jbe 0x33cb2
jmp 0x33e99
movq -0x3f0(%rbp), %rax
movss -0x2b4(%rbp), %xmm1
movss 0x10(%rax), %xmm0
ucomiss %xmm1, %xmm0
ja 0x33ce6
movq -0x3f0(%rbp), %rax
movss 0x10(%rax), %xmm0
movss 0x11339(%rip), %xmm1    # 0x45018
ucomiss %xmm1, %xmm0
jne 0x33d05
jp 0x33d05
movq -0x3f0(%rbp), %rax
movss -0x2b4(%rbp), %xmm0
movss %xmm0, 0x10(%rax)
movq -0xf0(%rbp), %rcx
movq %rcx, 0x14(%rax)
jmp 0x33d07
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x33427
movb -0x70(%rbp), %al
movb %al, -0xd0(%rbp)
movups -0x80(%rbp), %xmm0
movaps %xmm0, -0xe0(%rbp)
movq -0x10(%rbp), %rax
movq 0x1c(%rax), %rax
movq %rax, -0x3a0(%rbp)
movsd -0x3a0(%rbp), %xmm0
callq 0xb490
movlpd %xmm0, -0x398(%rbp)
movq -0x398(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x33410
callq 0xf620
cmpb $0x0, %al
je 0x33e24
movq -0x3f0(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, -0x3a8(%rbp)
movq %rax, %rdi
addq $0x14, %rdi
movss 0x10(%rax), %xmm0
callq 0x5810
movlpd %xmm0, -0x3b8(%rbp)
movsd -0x3b8(%rbp), %xmm0
leaq -0x98(%rbp), %rdi
callq 0x40dc0
movlpd %xmm0, -0x3b0(%rbp)
xorps %xmm2, %xmm2
movss 0x11231(%rip), %xmm3    # 0x45008
movaps %xmm2, %xmm0
movaps %xmm3, %xmm1
callq 0xbc90
movlpd %xmm0, -0x3c8(%rbp)
movlpd %xmm1, -0x3c0(%rbp)
movsd -0x3a8(%rbp), %xmm0
movsd -0x3b0(%rbp), %xmm1
movsd -0x3c8(%rbp), %xmm2
movsd -0x3c0(%rbp), %xmm3
movl $0xe, %edi
movss 0x1141d(%rip), %xmm4    # 0x4523c
callq 0x2d860
movq -0x3f0(%rbp), %rax
movq 0x14(%rax), %rax
movq %rax, -0x3d0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x3d8(%rbp)
movsd -0x3d0(%rbp), %xmm0
movsd -0x3d8(%rbp), %xmm1
callq 0xb340
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x33e8e
movq -0x3f0(%rbp), %rdi
addq $0x14, %rdi
callq 0x40d80
movq -0x3f0(%rbp), %rax
movlpd %xmm0, -0x3e0(%rbp)
movq -0x3e0(%rbp), %rcx
movq %rcx, 0x14(%rax)
movq -0x3f0(%rbp), %rax
movb $0x1, 0x1c(%rax)
movq -0x3e8(%rbp), %rax
addq $0x460, %rsp            # imm = 0x460
popq %rbp
retq
nopl (%rax)