// File: /workspace/github_c_makefile_star2_O1/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 $0xe8, %rsp
movapd %xmm0, 0x20(%rsp)
xorpd %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movapd %xmm0, (%rdi)
movq 0x2bcb(%rip), %r14      # 0x3fd8
movq (%r14), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x1040
cmpq $0x10, %rax
jne 0x1a86
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x30(%rsp)
jne 0x1a8b
movl 0x38(%rsp), %ebp
movl 0x3c(%rsp), %r12d
movl %ebp, %r13d
bswapl %r13d
movl %r12d, %r15d
bswapl %r15d
movq %r15, 0x70(%rsp)
imull %r13d, %r15d
movl $0x8, %esi
movq %r15, %rdi
callq 0x1090
testq %rax, %rax
je 0x1a64
movq %rax, %rbx
movq (%r14), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %r15, %rdx
callq 0x1040
cmpq %r15, %rax
jne 0x1a90
testl %r12d, %r12d
movq 0x70(%rsp), %r15
je 0x14da
movl %r13d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl %ecx, %esi
movq %rax, %rdi
testl %ebp, %ebp
je 0x14d0
movl %esi, %r8d
rolw $0x8, (%rbx,%r8,8)
rolw $0x8, 0x2(%rbx,%r8,8)
rolw $0x8, 0x4(%rbx,%r8,8)
rolw $0x8, 0x6(%rbx,%r8,8)
incl %esi
decq %rdi
jne 0x14ab
incl %edx
addl %r13d, %ecx
cmpl %r15d, %edx
jne 0x14a2
movapd 0x20(%rsp), %xmm0
divsd 0xb28(%rip), %xmm0      # 0x2010
mulsd 0xb28(%rip), %xmm0      # 0x2018
movapd %xmm0, 0x20(%rsp)
callq 0x10c0
movsd %xmm0, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
callq 0x1080
movapd %xmm0, 0x50(%rsp)
movl %r13d, %eax
shrl %eax
cvtsi2sd %eax, %xmm2
movsd %xmm2, 0x68(%rsp)
movapd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movl %r15d, %eax
shrl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x10(%rsp), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm1, %xmm3
xorpd 0xacf(%rip), %xmm3      # 0x2020
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
movapd %xmm2, %xmm4
addsd %xmm1, %xmm4
maxsd %xmm4, %xmm0
subsd %xmm2, %xmm3
subsd %xmm2, %xmm1
maxsd %xmm1, %xmm3
maxsd %xmm3, %xmm0
callq 0x1030
addsd %xmm0, %xmm0
cvttsd2si %xmm0, %ebp
movapd 0x50(%rsp), %xmm3
mulsd 0x60(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
mulsd 0x68(%rsp), %xmm2
movapd 0xa7e(%rip), %xmm4      # 0x2020
xorpd %xmm3, %xmm4
movapd %xmm2, %xmm0
subsd %xmm3, %xmm0
movapd %xmm2, %xmm1
addsd %xmm3, %xmm1
maxsd %xmm1, %xmm0
subsd %xmm2, %xmm4
subsd %xmm2, %xmm3
maxsd %xmm3, %xmm4
maxsd %xmm4, %xmm0
callq 0x1030
movsd %xmm0, 0x10(%rsp)
movq 0x29f4(%rip), %r14      # 0x3fd0
movq (%r14), %rsi
leaq 0xa97(%rip), %rdi       # 0x207d
callq 0x1070
movl %ebp, 0x8(%rsp)
movl %ebp, %eax
bswapl %eax
leaq 0x30(%rsp), %rdi
movl %eax, (%rdi)
movq (%r14), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10e0
cmpq $0x1, %rax
jne 0x1a7c
movsd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm0
cvttsd2si %xmm0, %ebp
movl %ebp, %eax
bswapl %eax
leaq 0x30(%rsp), %rdi
movl %eax, (%rdi)
movq (%r14), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10e0
cmpq $0x1, %rax
jne 0x1a7c
movaps 0x20(%rsp), %xmm0
xorps 0x9c9(%rip), %xmm0      # 0x2020
movaps %xmm0, 0x20(%rsp)
callq 0x10c0
movaps %xmm0, 0xc0(%rsp)
movapd 0x20(%rsp), %xmm0
callq 0x1080
movsd %xmm0, 0x78(%rsp)
testl %ebp, %ebp
jle 0x1a50
movl 0x8(%rsp), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %ebp, %ecx
shrl %ecx
movl %ecx, 0x44(%rsp)
movapd 0x981(%rip), %xmm0      # 0x2020
movapd 0xc0(%rsp), %xmm1
xorpd %xmm1, %xmm0
movapd %xmm0, 0xd0(%rsp)
leal -0x1(%r13), %ecx
movl %ecx, 0x1c(%rsp)
leal -0x1(%r15), %ecx
movl %ecx, 0x18(%rsp)
movl $0x1, %ecx
subl %eax, %ecx
movl %ecx, 0x40(%rsp)
movl $0x0, 0xc(%rsp)
movq %r13, 0x80(%rsp)
movl %ebp, 0x48(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x1a3a
movl 0xc(%rsp), %eax
subl 0x44(%rsp), %eax
incl %eax
cvtsi2sd %eax, %xmm2
movapd 0xd0(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x88(%rsp)
mulsd 0x78(%rsp), %xmm2
movsd %xmm2, 0x90(%rsp)
movl 0x40(%rsp), %eax
movl 0x8(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl %eax, 0x4c(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd %xmm0, %xmm1
mulsd 0x78(%rsp), %xmm0
addsd 0x88(%rsp), %xmm0
addsd 0x68(%rsp), %xmm0
addsd 0x90(%rsp), %xmm1
addsd 0x60(%rsp), %xmm1
movapd %xmm1, 0x50(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x1060
movapd %xmm0, 0x20(%rsp)
movapd 0x50(%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, %r13d
setg %dil
jg 0x17fb
movl 0x1c(%rsp), %edx
testl %ecx, %ecx
setns %r8b
jns 0x1805
xorl %edx, %edx
imull %r13d, %esi
movq %rdx, 0xa8(%rsp)
leal (%rdx,%rsi), %ecx
movslq %ecx, %rdx
movzwl 0x6(%rbx,%rdx,8), %ecx
shlq $0x30, %rcx
movb %dil, 0x7(%rsp)
movb %r8b, 0x6(%rsp)
testb %dil, %r8b
cmoveq %rbp, %rcx
cmpl %eax, %r15d
cvttsd2si %xmm1, %edi
cmovleq %rbp, %rcx
cmpl %edi, %r13d
movq %r15, %r14
movl 0x1c(%rsp), %r15d
cmovgl %edi, %r15d
setg %r8b
testl %edi, %edi
cmovsl %ebp, %r15d
setns %r12b
addl %r15d, %esi
movslq %esi, %rdi
movzwl 0x6(%rbx,%rdi,8), %esi
shlq $0x30, %rsi
movb %r8b, 0x5(%rsp)
testb %r8b, %r12b
cmoveq %rbp, %rsi
cmpl %eax, %r14d
cmovleq %rbp, %rsi
testl %eax, %eax
cmovsq %rbp, %rsi
cmovsq %rbp, %rcx
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %r13b
xorl %eax, %eax
movapd %xmm0, 0x50(%rsp)
ucomisd %xmm0, %xmm1
seta %al
subl %eax, %r13d
movl (%rbx,%rdi,8), %eax
movzwl 0x4(%rbx,%rdi,8), %edi
shlq $0x20, %rdi
orq %rax, %rdi
orq %rsi, %rdi
movl (%rbx,%rdx,8), %eax
movzwl 0x4(%rbx,%rdx,8), %esi
shlq $0x20, %rsi
orq %rax, %rsi
orq %rcx, %rsi
andpd 0x75e(%rip), %xmm3      # 0x2030
movapd %xmm3, 0x20(%rsp)
movapd %xmm3, %xmm0
callq 0x130e
movq %rax, 0x98(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd 0xa0(%rsp), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movl %eax, %edx
cmpl %eax, %r14d
jg 0x190d
movl 0x18(%rsp), %edx
testl %eax, %eax
movq 0x26ba(%rip), %r14      # 0x3fd0
jns 0x191a
xorl %edx, %edx
movq 0x80(%rsp), %r13
imull %r13d, %edx
addl %edx, %r15d
movslq %r15d, %rsi
movzwl 0x6(%rbx,%rsi,8), %ecx
shlq $0x30, %rcx
movq 0x70(%rsp), %r15
cmpl %eax, %r15d
cmovleq %rbp, %rcx
testb %r12b, 0x5(%rsp)
cmoveq %rbp, %rcx
addl 0xa8(%rsp), %edx
movslq %edx, %r8
movzwl 0x6(%rbx,%r8,8), %edx
shlq $0x30, %rdx
movb 0x6(%rsp), %dil
testb %dil, 0x7(%rsp)
cmoveq %rbp, %rdx
cmpl %eax, %r15d
cmovleq %rbp, %rdx
testl %eax, %eax
movl (%rbx,%rsi,8), %eax
movzwl 0x4(%rbx,%rsi,8), %edi
cmovsq %rbp, %rcx
movl (%rbx,%r8,8), %r9d
movzwl 0x4(%rbx,%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
movapd 0x50(%rsp), %xmm0
andpd 0x675(%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, 0x30(%rsp)
rolw $0x8, %cx
movw %cx, 0x32(%rsp)
rolw $0x8, %dx
movw %dx, 0x34(%rsp)
rolw $0x8, %si
movw %si, 0x36(%rsp)
movq (%r14), %rcx
movl $0x2, %esi
movl $0x4, %edx
leaq 0x30(%rsp), %rdi
callq 0x10e0
cmpq $0x4, %rax
movl 0x10(%rsp), %ecx
jne 0x1a77
movl 0x4c(%rsp), %eax
incl %eax
decl %ecx
movapd 0xc0(%rsp), %xmm1
jne 0x172a
movl 0xc(%rsp), %eax
incl %eax
movl 0x48(%rsp), %ebp
movl %eax, 0xc(%rsp)
cmpl %ebp, %eax
jne 0x16e4
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5d5(%rip), %rdi       # 0x2040
callq 0x10b0
movl $0x1, %eax
jmp 0x1a52
callq 0x116f
movl $0x1, %edi
callq 0x10d0
callq 0x1100
callq 0x1125
callq 0x114a