// File: /workspace/github_c_makefile_star2_O2/erik[P]gamut/rotate.c
// Function: rotate
int rotate(double angle)
{
    uint32_t inw, inh;
    pixel_t* image = NULL;

    read_header(&inw, &inh);

    if (!(image = calloc(inw * inh, sizeof(pixel_t)))) {
        perror("couldn't allocate memory");
        return 1;
    }

    read_image(image, inw, inh);

    const double rad = angle / 180.0 * M_PI;

    // calculate output dimensions
    int outw, outh;
    {
        const double S = sin(rad);
        const double C = cos(rad);
        double a = inw / 2 * C;
        double b = inh / 2 * S;
        outw = round(MAX(MAX(-a + b, a + b), MAX(-a - b, a - b))) * 2.0;
        double c = inh / 2 * C;
        double d = inw / 2 * S;
        outh = round(MAX(MAX(-c + d, c + d), MAX(-c - d, c - d))) * 2.0;
    }

    write_header(outw, outh);

    const double S = sin(-rad);
    const double C = cos(-rad);
    for (int outy = 0; outy < outh; outy++) {
        for (int outx = 0; outx < outw; outx++) {
            double cox = outx - outw / 2 + 1;
            double coy = outy - outh / 2 + 1;
            // rotate back to find input image coordinates
            double inx = cox * C - coy * S + inw / 2;
            double iny = coy * C + cox * S + inh / 2;
            // separate base coordinates and interpolation factors
            double basex, basey;
            double tx = modf(inx, &basex);
            double ty = modf(iny, &basey);
            int signx = SIGN(tx), signy = SIGN(ty);
            // bilinear interpolation
            pixel_t pixel = interpolate(
                interpolate(
                    sampleat(image, basex, basey, inw, inh),
                    sampleat(image, basex + signx, basey, inw, inh),
                    fabs(tx)),
                interpolate(
                    sampleat(image, basex, basey + signy, inw, inh),
                    sampleat(image, basex + signx, basey + signy, inw, inh),
                    fabs(tx)),
                fabs(ty));
            write_pixel(&pixel);
        }
   }
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movdqa %xmm0, 0x10(%rsp)
pxor %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movdqa %xmm0, (%rdi)
movq 0x2c9d(%rip), %rbx      # 0x3fd8
movq (%rbx), %rcx
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
callq 0x1040
cmpq $0x10, %rax
jne 0x18f8
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x40(%rsp)
jne 0x190d
movq 0x48(%rsp), %xmm0
pxor %xmm1, %xmm1
punpcklbw %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
pshuflw $0x1b, %xmm0, %xmm0     # xmm0 = xmm0[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm0, %xmm0     # xmm0 = xmm0[0,1,2,3,7,6,5,4]
packuswb %xmm1, %xmm0
movd %xmm0, %eax
movdqa %xmm0, 0x30(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
movl %ecx, 0x2c(%rsp)
movl %ecx, %r15d
movl %eax, 0xc(%rsp)
imull %eax, %r15d
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x1090
testq %rax, %rax
je 0x18c3
movq (%rbx), %rcx
pushq $0x8
popq %rsi
movq %rax, 0x80(%rsp)
movq %rax, %rdi
movq %r15, %rdx
callq 0x1040
cmpq %r15, %rax
jne 0x1922
movl 0xc(%rsp), %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x80(%rsp), %r9
cmpl 0x2c(%rsp), %edx
je 0x142a
movq %rax, %rsi
movl %ecx, %edi
subq $0x1, %rsi
jb 0x1422
movl %edi, %r8d
movq (%r9,%r8,8), %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%r9,%r8,8)
incl %edi
jmp 0x13f7
incl %edx
addl 0xc(%rsp), %ecx
jmp 0x13ec
movapd 0x10(%rsp), %xmm0
divsd 0xbd8(%rip), %xmm0      # 0x2010
mulsd 0xbd8(%rip), %xmm0      # 0x2018
movapd %xmm0, 0x10(%rsp)
callq 0x10c0
movapd %xmm0, 0x70(%rsp)
movapd 0x10(%rsp), %xmm0
callq 0x1080
movdqa 0x30(%rsp), %xmm1
psrld $0x1, %xmm1
cvtdq2pd %xmm1, %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm2, %xmm1
shufpd $0x1, %xmm2, %xmm1      # xmm1 = xmm1[1],xmm2[0]
mulpd %xmm0, %xmm1
movapd 0x70(%rsp), %xmm4
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
movapd %xmm2, 0xf0(%rsp)
mulpd %xmm2, %xmm4
movapd 0xb85(%rip), %xmm2      # 0x2020
xorpd %xmm1, %xmm2
movapd %xmm4, %xmm3
subpd %xmm1, %xmm3
movapd %xmm4, %xmm0
addpd %xmm1, %xmm0
maxpd %xmm0, %xmm3
subpd %xmm4, %xmm2
subpd %xmm4, %xmm1
maxpd %xmm1, %xmm2
movapd %xmm2, %xmm0
cmpltpd %xmm3, %xmm0
movmskpd %xmm0, %ebx
testb $0x2, %bl
movapd %xmm2, 0x70(%rsp)
movapd %xmm3, 0x30(%rsp)
jne 0x14e7
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
jmp 0x14ef
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
callq 0x1030
addsd %xmm0, %xmm0
cvttsd2si %xmm0, %ebp
testb $0x1, %bl
movapd 0x30(%rsp), %xmm0
jne 0x150d
movapd 0x70(%rsp), %xmm0
callq 0x1030
movsd %xmm0, 0x30(%rsp)
movq 0x2ab1(%rip), %rbx      # 0x3fd0
movq (%rbx), %rsi
leaq 0xb54(%rip), %rdi       # 0x207d
callq 0x1070
movl %ebp, %eax
bswapl %eax
leaq 0x40(%rsp), %rdi
movl %eax, (%rdi)
movq (%rbx), %rcx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
cmpq $0x1, %rax
jne 0x18f0
movsd 0x30(%rsp), %xmm0
addsd %xmm0, %xmm0
cvttsd2si %xmm0, %r14d
movl %r14d, %eax
bswapl %eax
leaq 0x40(%rsp), %rdi
movl %eax, (%rdi)
movq (%rbx), %rcx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
cmpq $0x1, %rax
jne 0x18f0
movapd 0xa94(%rip), %xmm1      # 0x2020
movapd %xmm1, 0x90(%rsp)
movapd 0x10(%rsp), %xmm0
xorpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
callq 0x10c0
movapd %xmm0, 0xe0(%rsp)
movaps 0x10(%rsp), %xmm0
callq 0x1080
pushq $-0x2
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %esi
movl %r14d, %eax
cltd
idivl %ecx
incl %eax
movq %rax, 0xa8(%rsp)
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
movl %ebp, 0x64(%rsp)
movsd %xmm0, 0x88(%rsp)
testl %r14d, %r14d
cmovlel %eax, %r14d
movaps 0x90(%rsp), %xmm0
xorps 0xe0(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
movapd 0xf0(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0x100(%rsp)
incl %esi
movl %esi, 0x60(%rsp)
xorl %eax, %eax
movl %r14d, 0x5c(%rsp)
movq 0x80(%rsp), %r15
movl 0x2c(%rsp), %r12d
cmpl %r14d, %eax
je 0x18af
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movq %rcx, 0xb0(%rsp)
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
movapd 0x90(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
mulsd 0x88(%rsp), %xmm1
movsd %xmm1, 0xc0(%rsp)
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
subl $0x1, %ecx
jb 0x189b
movl %ecx, 0x6c(%rsp)
cvtsi2sd %eax, %xmm0
movapd 0xe0(%rsp), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x88(%rsp), %xmm0
addsd 0xb8(%rsp), %xmm0
addsd 0xf0(%rsp), %xmm0
addsd 0xc0(%rsp), %xmm1
addsd 0x100(%rsp), %xmm1
movapd %xmm1, 0x30(%rsp)
leaq 0xd8(%rsp), %rdi
movl %eax, 0x68(%rsp)
callq 0x1060
movapd %xmm0, 0x10(%rsp)
movapd 0x30(%rsp), %xmm0
leaq 0xd0(%rsp), %rdi
callq 0x1060
xorl %ebx, %ebx
xorpd %xmm1, %xmm1
movapd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm0, 0x30(%rsp)
seta %bl
xorl %eax, %eax
ucomisd %xmm2, %xmm1
seta %al
subl %eax, %ebx
xorl %ebp, %ebp
ucomisd %xmm1, %xmm0
seta %bpl
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
movsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
cvttsd2si %xmm0, %esi
movl %esi, 0x70(%rsp)
subl %eax, %ebp
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
cvttsd2si %xmm0, %r13d
movq %r15, %rdi
movl %r13d, %edx
movl 0xc(%rsp), %r14d
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x11ec
cvtsi2sd %ebx, %xmm0
movq %rax, %rbx
addsd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %esi
movl %esi, 0x50(%rsp)
movq %r15, %rdi
movl %r13d, %edx
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x11ec
movdqa 0x87e(%rip), %xmm1      # 0x2030
movdqa 0x10(%rsp), %xmm0
pand %xmm1, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x124e
cvtsi2sd %ebp, %xmm0
movq %rax, %r13
addsd 0xc8(%rsp), %xmm0
cvttsd2si %xmm0, %ebx
movq %r15, %rdi
movl 0x70(%rsp), %esi
movl %ebx, %edx
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x11ec
movq %rax, %rbp
movq %r15, %rdi
movl 0x50(%rsp), %esi
movl %ebx, %edx
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x11ec
movq %rbp, %rdi
movq %rax, %rsi
movaps 0x10(%rsp), %xmm0
callq 0x124e
movdqa 0x30(%rsp), %xmm0
pand 0x806(%rip), %xmm0      # 0x2030
movq %r13, %rdi
movq %rax, %rsi
callq 0x124e
movl %eax, %ecx
shrl $0x10, %ecx
movq %rax, %rdx
shrq $0x20, %rdx
movq %rax, %rsi
rolw $0x8, %ax
shrq $0x30, %rsi
movw %ax, 0x40(%rsp)
rolw $0x8, %cx
movw %cx, 0x42(%rsp)
rolw $0x8, %dx
movw %dx, 0x44(%rsp)
rolw $0x8, %si
movw %si, 0x46(%rsp)
movq 0x275d(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
leaq 0x40(%rsp), %rdi
pushq $0x2
popq %rsi
pushq $0x4
popq %rdx
callq 0x10e0
cmpq $0x4, %rax
jne 0x18d4
movl 0x68(%rsp), %eax
incl %eax
movl 0x6c(%rsp), %ecx
jmp 0x168c
movq 0xb0(%rsp), %rax
incl %eax
movl 0x5c(%rsp), %r14d
jmp 0x163a
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x776(%rip), %rdi       # 0x2040
callq 0x10b0
pushq $0x1
popq %rax
jmp 0x18b1
movq 0x271d(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x7c6(%rip), %rdi       # 0x20ab
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
pushq $0x1
popq %rdi
callq 0x10d0
movq 0x26f9(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x761(%rip), %rdi       # 0x206a
pushq $0x12
jmp 0x18e7
movq 0x26e4(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x768(%rip), %rdi       # 0x2086
pushq $0x14
jmp 0x18e7
movq 0x26cf(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x768(%rip), %rdi       # 0x209b
pushq $0xf
jmp 0x18e7