// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rsi, %rbx
cvtsi2ssl (%rsi), %xmm1
movaps %xmm1, 0x40(%rsp)
movq %rcx, 0x30(%rsp)
xorps %xmm1, %xmm1
cvtsi2ssl 0x4(%rsi), %xmm1
movss %xmm1, 0x8(%rsp)
movq %rdx, 0x38(%rsp)
movq %rdi, %r12
xorps 0x32e1(%rip), %xmm0     # 0x6060
movaps %xmm0, 0x20(%rsp)
callq 0x21d0
movss %xmm0, 0xc(%rsp)
movaps 0x20(%rsp), %xmm0
callq 0x2180
movaps 0x40(%rsp), %xmm7
movaps %xmm0, %xmm1
mulss %xmm7, %xmm1
movaps %xmm0, %xmm8
movss 0x8(%rsp), %xmm4
mulss %xmm4, %xmm8
movss 0xc(%rsp), %xmm6
movaps %xmm6, %xmm2
mulss %xmm7, %xmm2
movaps %xmm6, %xmm3
mulss %xmm4, %xmm3
movaps %xmm7, %xmm4
xorps 0x328e(%rip), %xmm4     # 0x6060
movaps %xmm0, %xmm5
mulss %xmm4, %xmm5
mulss %xmm6, %xmm4
movaps %xmm3, %xmm6
addss %xmm5, %xmm6
cvttss2si %xmm6, %ecx
movaps %xmm4, %xmm6
subss %xmm8, %xmm6
cvttss2si %xmm6, %eax
movaps %xmm3, %xmm6
addss %xmm1, %xmm6
cvttss2si %xmm6, %edi
movaps %xmm2, %xmm6
subss %xmm8, %xmm6
cvttss2si %xmm6, %esi
subss %xmm3, %xmm1
cvttss2si %xmm1, %r9d
addss %xmm8, %xmm2
cvttss2si %xmm2, %r8d
subss %xmm3, %xmm5
cvttss2si %xmm5, %r13d
addss %xmm8, %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, %r14d
sarl $0x1f, %r14d
andl %r10d, %r14d
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, 0x14(%rsp)
cmpl %r11d, %r14d
jge 0x2fd4
movss 0x315c(%rip), %xmm1     # 0x6004
movaps %xmm7, %xmm4
mulss %xmm1, %xmm4
mulss 0x8(%rsp), %xmm1
movss %xmm1, 0x20(%rsp)
movl %edx, %eax
sarl $0x1f, %eax
andl %edx, %eax
movl %eax, 0x10(%rsp)
xorps %xmm5, %xmm5
movss %xmm0, 0x18(%rsp)
movaps %xmm4, 0x50(%rsp)
cmpl %r13d, 0x10(%rsp)
jge 0x2fc6
xorps %xmm6, %xmm6
cvtsi2ss %r14d, %xmm6
subss 0x20(%rsp), %xmm6
movaps %xmm0, %xmm1
mulss %xmm6, %xmm1
movss %xmm1, 0x1c(%rsp)
xorps 0x315f(%rip), %xmm6     # 0x6060
mulss 0xc(%rsp), %xmm6
movq 0x30(%rsp), %rax
leal (%r14,%rax), %ebp
movl 0x10(%rsp), %eax
movl %eax, %r15d
movaps %xmm6, 0x60(%rsp)
xorps %xmm3, %xmm3
cvtsi2ss %r15d, %xmm3
subss %xmm4, %xmm3
movaps %xmm3, %xmm1
mulss %xmm0, %xmm1
addss %xmm6, %xmm1
addss %xmm4, %xmm1
ucomiss %xmm5, %xmm1
jb 0x2fba
ucomiss %xmm1, %xmm7
jbe 0x2fba
mulss 0xc(%rsp), %xmm3
addss 0x1c(%rsp), %xmm3
addss 0x20(%rsp), %xmm3
ucomiss %xmm5, %xmm3
jb 0x2fba
movss 0x8(%rsp), %xmm2
ucomiss %xmm3, %xmm2
jbe 0x2fba
cvttss2si %xmm1, %eax
movl $0xffff00ff, %ecx       # imm = 0xFFFF00FF
testl %eax, %eax
js 0x2f8f
cvttss2si %xmm3, %esi
testl %esi, %esi
js 0x2f8f
movl (%rbx), %edx
cmpl %eax, %edx
jle 0x2f8f
cmpl %esi, 0x4(%rbx)
jle 0x2f8f
imull %esi, %edx
addl %eax, %edx
movq 0x10(%rbx), %rax
movl (%rax,%rdx,4), %ecx
movq 0x38(%rsp), %rax
leal (%rax,%r15), %esi
movq %r12, %rdi
movl %ebp, %edx
callq 0x2b68
movaps 0x40(%rsp), %xmm7
movss 0x18(%rsp), %xmm0
movaps 0x50(%rsp), %xmm4
xorps %xmm5, %xmm5
movaps 0x60(%rsp), %xmm6
incl %r15d
cmpl %r15d, %r13d
jne 0x2f1c
incl %r14d
cmpl 0x14(%rsp), %r14d
jne 0x2ed4
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq