// File: /workspace/github_c_makefile_star2_O3/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 $0x108, %rsp            # imm = 0x108
movdqa %xmm0, 0x20(%rsp)
pxor %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movdqa %xmm0, (%rdi)
movq 0x2bcb(%rip), %r14      # 0x3fd8
movq (%r14), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x1040
cmpq $0x10, %rax
jne 0x1ad9
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x40(%rsp)
jne 0x1ade
movq 0x48(%rsp), %xmm1
pxor %xmm0, %xmm0
movdqa %xmm1, 0x30(%rsp)
punpcklbw %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3],xmm1[4],xmm0[4],xmm1[5],xmm0[5],xmm1[6],xmm0[6],xmm1[7],xmm0[7]
pshuflw $0x1b, %xmm1, %xmm1     # xmm1 = xmm1[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm1, %xmm1     # xmm1 = xmm1[0,1,2,3,7,6,5,4]
packuswb %xmm0, %xmm1
movd %xmm1, %ebx
movdqa %xmm1, 0x50(%rsp)
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
movd %xmm0, %r15d
movq %r15, 0x78(%rsp)
imull %ebx, %r15d
movl $0x8, %esi
movq %r15, %rdi
callq 0x1090
testq %rax, %rax
je 0x1ab7
movq %rax, %r13
movq (%r14), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %r15, %rdx
callq 0x1040
cmpq %r15, %rax
jne 0x1ae3
movdqa 0x30(%rsp), %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
movd %xmm0, %eax
testl %eax, %eax
movq 0x78(%rsp), %r15
je 0x1510
movd %xmm1, %eax
movl %ebx, %ecx
xorl %edx, %edx
xorl %esi, %esi
movl %edx, %edi
movq %rcx, %r8
testl %eax, %eax
je 0x1507
movl %edi, %r9d
movq (%r13,%r9,8), %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%r13,%r9,8)
incl %edi
decq %r8
jne 0x14dd
incl %esi
addl %ebx, %edx
cmpl %r15d, %esi
jne 0x14d4
movq %rbx, 0x70(%rsp)
movapd 0x20(%rsp), %xmm0
divsd 0xaed(%rip), %xmm0      # 0x2010
mulsd 0xaed(%rip), %xmm0      # 0x2018
movapd %xmm0, 0x20(%rsp)
callq 0x10c0
movapd %xmm0, 0x30(%rsp)
movapd 0x20(%rsp), %xmm0
callq 0x1080
movdqa 0x50(%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 0x30(%rsp), %xmm4
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
movapd %xmm2, 0xd0(%rsp)
mulpd %xmm2, %xmm4
movapd 0xa9a(%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, 0x30(%rsp)
movapd %xmm3, 0x50(%rsp)
jne 0x15d2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
jmp 0x15da
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
callq 0x1030
addsd %xmm0, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rsp)
testb $0x1, %bl
movapd 0x50(%rsp), %xmm0
jne 0x15fc
movapd 0x30(%rsp), %xmm0
callq 0x1030
movsd %xmm0, 0x50(%rsp)
movq 0x29c2(%rip), %r14      # 0x3fd0
movq (%r14), %rsi
leaq 0xa65(%rip), %rdi       # 0x207d
callq 0x1070
movl 0x10(%rsp), %eax
bswapl %eax
leaq 0x40(%rsp), %rdi
movl %eax, (%rdi)
movq (%r14), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10e0
cmpq $0x1, %rax
movq 0x70(%rsp), %rbx
jne 0x1acf
movsd 0x50(%rsp), %xmm0
addsd %xmm0, %xmm0
cvttsd2si %xmm0, %ebp
movl %ebp, %eax
bswapl %eax
leaq 0x40(%rsp), %rdi
movl %eax, (%rdi)
movq (%r14), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10e0
cmpq $0x1, %rax
jne 0x1acf
movaps 0x20(%rsp), %xmm0
xorps 0x994(%rip), %xmm0      # 0x2020
movaps %xmm0, 0x20(%rsp)
callq 0x10c0
movaps %xmm0, 0xc0(%rsp)
movdqa 0x20(%rsp), %xmm0
callq 0x1080
movq %xmm0, 0x80(%rsp)
testl %ebp, %ebp
jle 0x1aa3
movl 0x10(%rsp), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %ebp, %ecx
shrl %ecx
movl %ecx, 0x64(%rsp)
movapd 0x949(%rip), %xmm0      # 0x2020
movapd 0xc0(%rsp), %xmm1
xorpd %xmm1, %xmm0
movapd %xmm0, 0xe0(%rsp)
leal -0x1(%rbx), %ecx
movl %ecx, 0x1c(%rsp)
leal -0x1(%r15), %ecx
movl %ecx, 0x18(%rsp)
movdqa 0xd0(%rsp), %xmm0
punpckhqdq %xmm0, %xmm0    # xmm0 = xmm0[1,1]
movdqa %xmm0, 0xf0(%rsp)
movl $0x1, %ecx
subl %eax, %ecx
movl %ecx, 0x60(%rsp)
movl $0x0, 0x14(%rsp)
movl %ebp, 0x68(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x1a8d
movl 0x14(%rsp), %eax
subl 0x64(%rsp), %eax
incl %eax
cvtsi2sd %eax, %xmm2
movapd 0xe0(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x88(%rsp)
mulsd 0x80(%rsp), %xmm2
movsd %xmm2, 0x90(%rsp)
movl 0x60(%rsp), %eax
movl 0x10(%rsp), %ecx
movl %ecx, 0x50(%rsp)
movl %eax, 0x6c(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd %xmm0, %xmm1
mulsd 0x80(%rsp), %xmm0
addsd 0x88(%rsp), %xmm0
addsd 0xd0(%rsp), %xmm0
addsd 0x90(%rsp), %xmm1
addsd 0xf0(%rsp), %xmm1
movapd %xmm1, 0x30(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x1060
movapd %xmm0, 0x20(%rsp)
movapd 0x30(%rsp), %xmm0
leaq 0xb0(%rsp), %rdi
callq 0x1060
movapd 0x20(%rsp), %xmm3
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm3
seta %cl
xorl %eax, %eax
ucomisd %xmm3, %xmm1
seta %al
subl %eax, %ecx
movsd 0xb0(%rsp), %xmm1
movsd %xmm1, 0xa0(%rsp)
cvttsd2si %xmm1, %eax
cmpl %eax, %r15d
movl 0x18(%rsp), %edx
movl %edx, %esi
cmovgl %eax, %esi
xorl %ebp, %ebp
testl %eax, %eax
cmovsl %ebp, %esi
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
movsd 0xb8(%rsp), %xmm1
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %ecx
movl %ecx, %edx
cmpl %ecx, %ebx
setg %dil
jg 0x184b
movl 0x1c(%rsp), %edx
testl %ecx, %ecx
setns %r8b
jns 0x1855
xorl %edx, %edx
imull %ebx, %esi
movq %rdx, 0xa8(%rsp)
leal (%rdx,%rsi), %ecx
movslq %ecx, %rdx
movzwl 0x6(%r13,%rdx,8), %ecx
shlq $0x30, %rcx
movb %dil, 0xf(%rsp)
movb %r8b, 0xe(%rsp)
testb %dil, %r8b
cmoveq %rbp, %rcx
cmpl %eax, %r15d
cvttsd2si %xmm1, %edi
cmovleq %rbp, %rcx
cmpl %edi, %ebx
movq %r15, %r14
movl 0x1c(%rsp), %r15d
cmovgl %edi, %r15d
setg %r9b
testl %edi, %edi
cmovsl %ebp, %r15d
setns %r12b
addl %r15d, %esi
movslq %esi, %rdi
movzwl 0x6(%r13,%rdi,8), %esi
shlq $0x30, %rsi
movb %r9b, 0xd(%rsp)
testb %r9b, %r12b
cmoveq %rbp, %rsi
cmpl %eax, %r14d
cmovleq %rbp, %rsi
testl %eax, %eax
cmovsq %rbp, %rsi
cmovsq %rbp, %rcx
xorl %ebx, %ebx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %bl
xorl %eax, %eax
movapd %xmm0, 0x30(%rsp)
ucomisd %xmm0, %xmm1
seta %al
subl %eax, %ebx
movl (%r13,%rdi,8), %eax
movzwl 0x4(%r13,%rdi,8), %edi
shlq $0x20, %rdi
orq %rax, %rdi
orq %rsi, %rdi
movl (%r13,%rdx,8), %eax
movzwl 0x4(%r13,%rdx,8), %esi
shlq $0x20, %rsi
orq %rax, %rsi
orq %rcx, %rsi
andpd 0x70b(%rip), %xmm3      # 0x2030
movapd %xmm3, 0x20(%rsp)
movapd %xmm3, %xmm0
callq 0x130e
movq %rax, 0x98(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd 0xa0(%rsp), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movl %eax, %edx
cmpl %eax, %r14d
jg 0x195f
movl 0x18(%rsp), %edx
testl %eax, %eax
movq 0x2668(%rip), %r14      # 0x3fd0
jns 0x196c
xorl %edx, %edx
movq 0x70(%rsp), %rbx
imull %ebx, %edx
addl %edx, %r15d
movslq %r15d, %rsi
movzwl 0x6(%r13,%rsi,8), %ecx
shlq $0x30, %rcx
movq 0x78(%rsp), %r15
cmpl %eax, %r15d
cmovleq %rbp, %rcx
testb %r12b, 0xd(%rsp)
cmoveq %rbp, %rcx
addl 0xa8(%rsp), %edx
movslq %edx, %r8
movzwl 0x6(%r13,%r8,8), %edx
shlq $0x30, %rdx
movb 0xe(%rsp), %dil
testb %dil, 0xf(%rsp)
cmoveq %rbp, %rdx
cmpl %eax, %r15d
cmovleq %rbp, %rdx
testl %eax, %eax
movl (%r13,%rsi,8), %eax
movzwl 0x4(%r13,%rsi,8), %edi
cmovsq %rbp, %rcx
movl (%r13,%r8,8), %r9d
movzwl 0x4(%r13,%r8,8), %esi
cmovsq %rbp, %rdx
shlq $0x20, %rdi
orq %rax, %rdi
orq %rcx, %rdi
shlq $0x20, %rsi
orq %r9, %rsi
orq %rdx, %rsi
movaps 0x20(%rsp), %xmm0
callq 0x130e
movdqa 0x30(%rsp), %xmm0
pand 0x622(%rip), %xmm0      # 0x2030
movq 0x98(%rsp), %rdi
movq %rax, %rsi
callq 0x130e
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 (%r14), %rcx
movl $0x2, %esi
movl $0x4, %edx
leaq 0x40(%rsp), %rdi
callq 0x10e0
cmpq $0x4, %rax
movl 0x50(%rsp), %ecx
jne 0x1aca
movl 0x6c(%rsp), %eax
incl %eax
decl %ecx
movapd 0xc0(%rsp), %xmm1
jne 0x1772
movl 0x14(%rsp), %eax
incl %eax
movl 0x68(%rsp), %ebp
movl %eax, 0x14(%rsp)
cmpl %ebp, %eax
jne 0x1729
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x582(%rip), %rdi       # 0x2040
callq 0x10b0
movl $0x1, %eax
jmp 0x1aa5
callq 0x116f
movl $0x1, %edi
callq 0x10d0
callq 0x1100
callq 0x1125
callq 0x114a