// File: /workspace/github_c_makefile_star2_O2/adelciotto[P]scg/examples/../scg.h
// Function: scg_image_draw_image_rotate
void scg_image_draw_image_rotate(scg_image_t *dest, scg_image_t *src, int x,
                                 int y, float32_t angle) {
    float32_t src_w = src->width;
    float32_t src_h = src->height;
    float32_t origin_x = src_w * 0.5f;
    float32_t origin_y = src_h * 0.5f;
    float32_t sin_theta = sinf(-angle);
    float32_t cos_theta = cosf(-angle);

    float32_t a = src_w * cos_theta;
    float32_t b = src_h * cos_theta;
    float32_t c = src_w * sin_theta;
    float32_t d = src_h * sin_theta;
    float32_t e = -src_w * cos_theta;
    float32_t f = -src_w * sin_theta;
    int x0 = (int)(e + d);
    int y0 = (int)(f - b);
    int x1 = (int)(a + d);
    int y1 = (int)(c - b);
    int x2 = (int)(a - d);
    int y2 = (int)(c + b);
    int x3 = (int)(e - d);
    int y3 = (int)(f + b);

    int minx =
        scg_min_int(0, scg_min_int(scg_min_int(x0, scg_min_int(x1, x2)), x3));
    int miny =
        scg_min_int(0, scg_min_int(scg_min_int(y0, scg_min_int(y1, y2)), y3));
    int maxx = scg_max_int(x0, scg_max_int(x1, scg_max_int(x2, x3)));
    int maxy = scg_max_int(y0, scg_max_int(y1, scg_max_int(y2, y3)));

    // TODO: Try a pixel subsampling approach to improve the visual quality
    // of the rotation.
    // Reference:
    // http://www.leptonica.org/rotation.html#ROTATION-BY-AREA-MAPPING
    for (int i = miny; i < maxy; i++) {
        for (int j = minx; j < maxx; j++) {
            float32_t image_x = (float32_t)j - origin_x;
            float32_t image_y = (float32_t)i - origin_y;
            float32_t xt =
                (image_x * cos_theta - image_y * sin_theta) + origin_x;
            float32_t yt =
                (image_x * sin_theta + image_y * cos_theta) + origin_y;

            if (xt >= 0 && xt < src_w && yt >= 0 && yt < src_h) {
                scg_pixel_t color = scg_image_get_pixel(src, (int)xt, (int)yt);
                scg_image_set_pixel(dest, x + j, y + i, color);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movaps %xmm0, %xmm2
movq %rcx, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movsd (%rsi), %xmm0
cvtdq2ps %xmm0, %xmm1
movaps %xmm1, 0x60(%rsp)
movaps %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm0     # xmm0 = xmm0[1,1],xmm1[1,1]
movaps %xmm0, 0x30(%rsp)
movaps 0x3320(%rip), %xmm0     # 0x6060
mulps %xmm1, %xmm0
movaps %xmm0, 0x50(%rsp)
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm0, 0x70(%rsp)
xorps 0x3318(%rip), %xmm2     # 0x6070
movaps %xmm2, 0x20(%rsp)
movaps %xmm2, %xmm0
callq 0x21d0
movaps %xmm0, 0x10(%rsp)
movaps 0x20(%rsp), %xmm0
callq 0x2180
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
movaps %xmm0, %xmm3
movaps 0x60(%rsp), %xmm5
mulss %xmm5, %xmm3
movaps 0x30(%rsp), %xmm8
mulss %xmm8, %xmm1
movaps 0x10(%rsp), %xmm6
movaps %xmm6, %xmm4
mulss %xmm5, %xmm4
mulss %xmm6, %xmm8
xorps 0x32c7(%rip), %xmm5     # 0x6070
mulss %xmm5, %xmm2
mulss %xmm6, %xmm5
movaps %xmm6, %xmm7
movaps %xmm8, %xmm6
addss %xmm2, %xmm6
cvttss2si %xmm6, %ecx
movaps %xmm5, %xmm6
subss %xmm1, %xmm6
cvttss2si %xmm6, %eax
movaps %xmm8, %xmm6
addss %xmm3, %xmm6
cvttss2si %xmm6, %esi
movaps %xmm4, %xmm6
subss %xmm1, %xmm6
cvttss2si %xmm6, %edx
subss %xmm8, %xmm3
cvttss2si %xmm3, %r8d
addss %xmm1, %xmm4
cvttss2si %xmm4, %edi
subss %xmm8, %xmm2
cvttss2si %xmm2, %r13d
movaps 0x50(%rsp), %xmm2
addss %xmm1, %xmm5
cvttss2si %xmm5, %r10d
cmpl %r8d, %esi
movl %r8d, %r9d
cmovll %esi, %r9d
cmpl %r9d, %ecx
cmovll %ecx, %r9d
cmpl %r13d, %r9d
cmovgel %r13d, %r9d
movl %r9d, %r11d
sarl $0x1f, %r11d
andl %r9d, %r11d
movl %r11d, 0x8(%rsp)
cmpl %edi, %edx
movl %edi, %r9d
cmovll %edx, %r9d
cmpl %r9d, %eax
cmovll %eax, %r9d
cmpl %r10d, %r9d
cmovgel %r10d, %r9d
movl %r9d, %ebx
sarl $0x1f, %ebx
andl %r9d, %ebx
cmpl %r13d, %r8d
cmovgl %r8d, %r13d
cmpl %r13d, %esi
cmovgl %esi, %r13d
cmpl %r13d, %ecx
cmovgl %ecx, %r13d
cmpl %r10d, %edi
cmovgl %edi, %r10d
cmpl %r10d, %edx
cmovgl %edx, %r10d
cmpl %r10d, %eax
cmovgl %eax, %r10d
movl %r10d, 0xc(%rsp)
unpcklps %xmm0, %xmm7            # xmm7 = xmm7[0],xmm0[0],xmm7[1],xmm0[1]
movaps %xmm7, 0x10(%rsp)
shufps $0xe1, %xmm7, %xmm7     # xmm7 = xmm7[1,0,2,3]
xorps %xmm3, %xmm3
movaps %xmm7, 0x20(%rsp)
cmpl 0xc(%rsp), %ebx
je 0x2f64
cvtsi2ss %ebx, %xmm0
subss 0x70(%rsp), %xmm0
movaps %xmm0, %xmm4
xorps 0x31bc(%rip), %xmm4     # 0x6070
unpcklps %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0],xmm4[1],xmm0[1]
mulps 0x10(%rsp), %xmm4
movq 0x40(%rsp), %rax
leal (%rbx,%rax), %ebp
movl 0x8(%rsp), %eax
movl %eax, %r14d
movaps %xmm4, 0x30(%rsp)
cmpl %r14d, %r13d
je 0x2f5d
cvtsi2ss %r14d, %xmm0
subss %xmm2, %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm7, %xmm0
addps %xmm4, %xmm0
addps %xmm2, %xmm0
ucomiss %xmm3, %xmm0
jb 0x2f55
movaps %xmm0, %xmm1
cmpltps 0x60(%rsp), %xmm1
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
movl %eax, %ecx
shrb %cl
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
ucomiss %xmm3, %xmm1
setae %dl
andb %al, %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2f55
cvttss2si %xmm0, %esi
cvttss2si %xmm1, %edx
movq %r15, %rdi
callq 0x2b28
movq 0x48(%rsp), %rcx
leal (%rcx,%r14), %esi
movq %r12, %rdi
movl %ebp, %edx
movl %eax, %ecx
callq 0x2b4d
movaps 0x30(%rsp), %xmm4
xorps %xmm3, %xmm3
movaps 0x20(%rsp), %xmm7
movaps 0x50(%rsp), %xmm2
incl %r14d
jmp 0x2ed0
incl %ebx
jmp 0x2e96
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq