// File: /workspace/github_c_makefile_star2_O3/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
movq %rcx, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movsd (%rsi), %xmm1
cvtdq2ps %xmm1, %xmm1
movaps %xmm1, 0x50(%rsp)
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movaps %xmm1, 0x30(%rsp)
xorps 0x323c(%rip), %xmm0     # 0x6080
movaps %xmm0, 0x20(%rsp)
callq 0x21d0
movaps %xmm0, 0x10(%rsp)
movaps 0x20(%rsp), %xmm0
callq 0x2180
movaps 0x50(%rsp), %xmm7
movaps %xmm0, %xmm2
mulss %xmm7, %xmm2
movaps %xmm0, %xmm1
movaps 0x30(%rsp), %xmm9
mulss %xmm9, %xmm1
movaps 0x10(%rsp), %xmm6
movaps %xmm6, %xmm3
mulss %xmm7, %xmm3
mulss %xmm6, %xmm9
movaps %xmm7, %xmm4
xorps 0x31ee(%rip), %xmm4     # 0x6080
movaps %xmm0, %xmm5
mulss %xmm4, %xmm5
movaps %xmm6, %xmm8
mulss %xmm6, %xmm4
movaps %xmm9, %xmm6
addss %xmm5, %xmm6
cvttss2si %xmm6, %ecx
movaps %xmm4, %xmm6
subss %xmm1, %xmm6
cvttss2si %xmm6, %eax
movaps %xmm9, %xmm6
addss %xmm2, %xmm6
cvttss2si %xmm6, %edi
movaps %xmm3, %xmm6
subss %xmm1, %xmm6
cvttss2si %xmm6, %esi
subss %xmm9, %xmm2
cvttss2si %xmm2, %r9d
addss %xmm1, %xmm3
cvttss2si %xmm3, %r8d
subss %xmm9, %xmm5
cvttss2si %xmm5, %r13d
addss %xmm1, %xmm4
cvttss2si %xmm4, %r11d
cmpl %r9d, %edi
movl %r9d, %edx
cmovll %edi, %edx
cmpl %edx, %ecx
cmovll %ecx, %edx
cmpl %r13d, %edx
cmovgel %r13d, %edx
cmpl %r8d, %esi
movl %r8d, %r10d
cmovll %esi, %r10d
cmpl %r10d, %eax
cmovll %eax, %r10d
cmpl %r11d, %r10d
cmovgel %r11d, %r10d
movl %r10d, %ebx
sarl $0x1f, %ebx
andl %r10d, %ebx
cmpl %r13d, %r9d
cmovgl %r9d, %r13d
cmpl %r13d, %edi
cmovgl %edi, %r13d
cmpl %r13d, %ecx
cmovgl %ecx, %r13d
cmpl %r11d, %r8d
cmovgl %r8d, %r11d
cmpl %r11d, %esi
cmovgl %esi, %r11d
cmpl %r11d, %eax
cmovgl %eax, %r11d
movl %r11d, 0xc(%rsp)
cmpl %r11d, %ebx
jge 0x309b
movaps 0x3136(%rip), %xmm2     # 0x60a0
mulps %xmm7, %xmm2
movl %edx, %eax
sarl $0x1f, %eax
andl %edx, %eax
movl %eax, 0x8(%rsp)
unpcklps %xmm0, %xmm8            # xmm8 = xmm8[0],xmm0[0],xmm8[1],xmm0[1]
movaps %xmm2, %xmm0
shufps $0x55, %xmm2, %xmm0     # xmm0 = xmm0[1,1],xmm2[1,1]
movaps %xmm0, 0x60(%rsp)
movaps %xmm8, 0x10(%rsp)
shufps $0xe1, %xmm8, %xmm8     # xmm8 = xmm8[1,0,2,3]
xorps %xmm3, %xmm3
movaps %xmm2, 0x20(%rsp)
movaps %xmm8, 0x70(%rsp)
cmpl %r13d, 0x8(%rsp)
jge 0x308f
xorps %xmm0, %xmm0
cvtsi2ss %ebx, %xmm0
subss 0x60(%rsp), %xmm0
movaps %xmm0, %xmm4
xorps 0x30bd(%rip), %xmm4     # 0x6080
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)
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
subss %xmm2, %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm8, %xmm0
addps %xmm4, %xmm0
addps %xmm2, %xmm0
ucomiss %xmm3, %xmm0
jb 0x3083
movaps %xmm0, %xmm1
cmpltps %xmm7, %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 0x3083
cvttss2si %xmm0, %eax
movl $0xffff00ff, %ecx       # imm = 0xFFFF00FF
testl %eax, %eax
js 0x3058
cvttss2si %xmm1, %esi
testl %esi, %esi
js 0x3058
movl (%r15), %edx
cmpl %eax, %edx
jle 0x3058
cmpl %esi, 0x4(%r15)
jle 0x3058
imull %esi, %edx
addl %eax, %edx
movq 0x10(%r15), %rax
movl (%rax,%rdx,4), %ecx
movq 0x48(%rsp), %rax
leal (%rax,%r14), %esi
movq %r12, %rdi
movl %ebp, %edx
callq 0x2b68
movaps 0x50(%rsp), %xmm7
movaps 0x20(%rsp), %xmm2
movaps 0x70(%rsp), %xmm8
xorps %xmm3, %xmm3
movaps 0x30(%rsp), %xmm4
incl %r14d
cmpl %r14d, %r13d
jne 0x2fdf
incl %ebx
cmpl 0xc(%rsp), %ebx
jne 0x2fa1
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq