// File: Erkaman[P]spiky-anim/main.cpp
// Function: main
int main(int argc, char** argv) {
    // create output dir.
    system("mkdir -p temp/");

    float START_RAD = 3.0f;
    float SPAWN_RAD = 40.0f;

    // colors of spikys.
    const int NC = 3;
    string colors[NC] = {
        "#990022",
        "#888822",
        "#9C8066",
    };

    int ic = 0;
    string curColor = colors[ic];

    vector<Star> stars;

    stars.push_back(Star(START_RAD, curColor));

    for(int i = 0; i < 300; i++) { // 1000

        FILE* fp = fopen(("temp/out" + zeroPad(i, 5) + ".svg").c_str(), "w");

        fprintf(fp, "<svg width=\"%f\" height=\"%f\" version=\"1.1\" baseProfile=\"full\" xmlns=\"http://www.w3.org/2000/svg\">\n", (float)WIDTH, (float)HEIGHT);

        // clear to black background.
        fprintf(fp,"<rect ");
        fprintf(fp,"width=\"%d\" ", WIDTH);
        fprintf(fp,"height=\"%d\" ", HEIGHT);
        fprintf(fp,"x=\"0\" ");
        fprintf(fp,"y=\"0\" ");
        fprintf(fp,"fill=\"#000000\"/>\n");

        for(int j = 0; j < stars.size(); j++) {
            Star& star = stars[j];

            makeSpiky(fp, star.rad, star.color);
            star.rad+=2.0f; // spiky velocity.
        }

        // once last spiky is big enough, spawn another one.
        const float& lastRad = stars[stars.size()-1].rad;
        if(lastRad > SPAWN_RAD) {
            ic = (ic+1)%NC;
            curColor = colors[ic];

            stars.push_back(Star(START_RAD, curColor));
        }

        fprintf(fp, "</svg>");
        fclose(fp);
    }
    printf("created svgs\n");

    system("cd temp/ && for i in *.svg; do rsvg-convert $i -o `echo $i | sed -e 's/svg$/png/'`; done");
    printf("converted svgs to png\n");

    // frames from (225) to (225+58) makes a good loop.
    system("cd temp/ && ffmpeg -start_number 225 -y -framerate 30 -i  out%05d.png -vframes 58 -c:v libx264 -vf \"scale=512:512\" -r 30 ../out.mp4 ");
    printf("encoded to mp4\n");


    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x33e5(%rip), %rdi      # 0x6095
callq 0x21d0
movl $0x40400000, -0x14(%rbp) # imm = 0x40400000
movl $0x42200000, -0x18(%rbp) # imm = 0x42200000
movl $0x3, -0x1c(%rbp)
movb $0x1, -0x9f(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x228(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x2330
movq -0x228(%rbp), %rdi
movq -0x220(%rbp), %rdx
leaq 0x3399(%rip), %rsi      # 0x60a4
callq 0x3500
jmp 0x2d12
leaq -0x60(%rbp), %rax
movq %rax, -0x238(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x9d(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x2330
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rdx
leaq 0x3360(%rip), %rsi      # 0x60ac
callq 0x3500
jmp 0x2d53
leaq -0x40(%rbp), %rax
movq %rax, -0x248(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x9e(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x2330
movq -0x248(%rbp), %rdi
movq -0x240(%rbp), %rdx
leaq 0x3327(%rip), %rsi      # 0x60b4
callq 0x3500
jmp 0x2d94
movb $0x0, -0x9f(%rbp)
leaq -0x9e(%rbp), %rdi
callq 0x2230
leaq -0x9d(%rbp), %rdi
callq 0x2230
leaq -0x89(%rbp), %rdi
callq 0x2230
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
shlq $0x5, %rax
leaq -0x80(%rbp,%rax), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x20f0
jmp 0x2de7
leaq -0xe0(%rbp), %rdi
callq 0x3b10
movss -0x14(%rbp), %xmm0
movss %xmm0, -0x24c(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x20f0
jmp 0x2e15
movss -0x24c(%rbp), %xmm0
leaq -0x108(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x3b60
jmp 0x2e32
leaq -0xe0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3b30
jmp 0x2e47
leaq -0x108(%rbp), %rdi
callq 0x3bd0
leaq -0x128(%rbp), %rdi
callq 0x2140
movl $0x0, -0x12c(%rbp)
cmpl $0x12c, -0x12c(%rbp)    # imm = 0x12C
jge 0x33c5
movl -0x12c(%rbp), %esi
leaq -0x198(%rbp), %rdi
movl $0x5, %edx
callq 0x2490
jmp 0x2e92
leaq 0x3223(%rip), %rsi      # 0x60bc
leaq -0x178(%rbp), %rdi
leaq -0x198(%rbp), %rdx
callq 0x3a10
jmp 0x2eae
leaq 0x3210(%rip), %rdx      # 0x60c5
leaq -0x158(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x39c0
jmp 0x2eca
leaq -0x158(%rbp), %rdi
callq 0x20e0
movq %rax, %rdi
leaq 0x31ea(%rip), %rsi      # 0x60ca
callq 0x2270
movq %rax, -0x258(%rbp)
jmp 0x2eee
leaq -0x158(%rbp), %rdi
callq 0x2140
leaq -0x178(%rbp), %rdi
callq 0x2140
leaq -0x198(%rbp), %rdi
callq 0x2140
movq -0x258(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
leaq 0x319e(%rip), %rsi      # 0x60cc
movsd 0x30f2(%rip), %xmm1     # 0x6028
movaps %xmm1, %xmm0
movb $0x2, %al
callq 0x22c0
movq -0x138(%rbp), %rdi
leaq 0x31e0(%rip), %rsi      # 0x612e
movb $0x0, %al
callq 0x22c0
movq -0x138(%rbp), %rdi
leaq 0x31d2(%rip), %rsi      # 0x6135
movl $0x200, %edx            # imm = 0x200
movb $0x0, %al
callq 0x22c0
movq -0x138(%rbp), %rdi
leaq 0x31c4(%rip), %rsi      # 0x6141
movl $0x200, %edx            # imm = 0x200
movb $0x0, %al
callq 0x22c0
movq -0x138(%rbp), %rdi
leaq 0x31b7(%rip), %rsi      # 0x614e
movb $0x0, %al
callq 0x22c0
movq -0x138(%rbp), %rdi
leaq 0x31a9(%rip), %rsi      # 0x6155
movb $0x0, %al
callq 0x22c0
movq -0x138(%rbp), %rdi
leaq 0x319b(%rip), %rsi      # 0x615c
movb $0x0, %al
callq 0x22c0
movl $0x0, -0x19c(%rbp)
movslq -0x19c(%rbp), %rax
movq %rax, -0x260(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x3bf0
movq %rax, %rcx
movq -0x260(%rbp), %rax
cmpq %rcx, %rax
jae 0x324c
movslq -0x19c(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x3c20
movq %rax, -0x1a8(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x270(%rbp)
movq -0x1a8(%rbp), %rsi
movss (%rsi), %xmm0
movss %xmm0, -0x264(%rbp)
addq $0x8, %rsi
leaq -0x1c8(%rbp), %rdi
callq 0x20f0
jmp 0x304c
movss -0x264(%rbp), %xmm0
movq -0x270(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x25a0
jmp 0x3069
leaq -0x1c8(%rbp), %rdi
callq 0x2140
movq -0x1a8(%rbp), %rax
movss 0x2f8c(%rip), %xmm0     # 0x6010
addss (%rax), %xmm0
movss %xmm0, (%rax)
movl -0x19c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x19c(%rbp)
jmp 0x2fd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x30f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x30e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x9e(%rbp), %rdi
callq 0x2230
leaq -0x9d(%rbp), %rdi
callq 0x2230
leaq -0x89(%rbp), %rdi
callq 0x2230
testb $0x1, -0x9f(%rbp)
jne 0x3109
jmp 0x3150
movq -0x88(%rbp), %rax
leaq -0x80(%rbp), %rcx
cmpq %rax, %rcx
movq %rax, -0x278(%rbp)
je 0x314e
movq -0x278(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x280(%rbp)
callq 0x2140
movq -0x280(%rbp), %rax
leaq -0x80(%rbp), %rcx
cmpq %rcx, %rax
movq %rax, -0x278(%rbp)
jne 0x3120
jmp 0x3150
jmp 0x34e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x349f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x3448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x31b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x3bd0
leaq -0x128(%rbp), %rdi
callq 0x2140
jmp 0x3448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x3218
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x320c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x2140
leaq -0x178(%rbp), %rdi
callq 0x2140
leaq -0x198(%rbp), %rdi
callq 0x2140
jmp 0x3448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x2140
jmp 0x3448
leaq -0xe0(%rbp), %rdi
callq 0x3bf0
movq %rax, %rsi
subq $0x1, %rsi
leaq -0xe0(%rbp), %rdi
callq 0x3c20
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
movss (%rax), %xmm0
ucomiss -0x18(%rbp), %xmm0
jbe 0x338c
movl -0xa4(%rbp), %edx
movl %edx, %ecx
incl %ecx
movl %edx, %eax
movslq %ecx, %rcx
imulq $0x55555556, %rcx, %rdx # imm = 0x55555556
movq %rdx, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rdx
addl %ecx, %edx
movl %edx, %ecx
leal (%rcx,%rcx,2), %edx
negl %edx
movl %edx, %ecx
leal 0x1(%rax,%rcx), %eax
movl %eax, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
shlq $0x5, %rax
leaq -0x80(%rbp,%rax), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x2040
jmp 0x32db
movss -0x14(%rbp), %xmm0
movss %xmm0, -0x284(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x20f0
jmp 0x32fd
movss -0x284(%rbp), %xmm0
leaq -0x1f8(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x3b60
jmp 0x331a
leaq -0xe0(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x3b30
jmp 0x332f
leaq -0x1f8(%rbp), %rdi
callq 0x3bd0
leaq -0x218(%rbp), %rdi
callq 0x2140
jmp 0x338c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x337b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x3bd0
leaq -0x218(%rbp), %rdi
callq 0x2140
jmp 0x3448
movq -0x138(%rbp), %rdi
leaq 0x2dd4(%rip), %rsi      # 0x616e
xorl %eax, %eax
callq 0x22c0
movq -0x138(%rbp), %rdi
callq 0x21f0
jmp 0x33af
jmp 0x33b1
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x2e69
leaq 0x2da9(%rip), %rdi      # 0x6175
xorl %eax, %eax
callq 0x2050
jmp 0x33d5
leaq 0x2da7(%rip), %rdi      # 0x6183
callq 0x21d0
jmp 0x33e3
leaq 0x2df2(%rip), %rdi      # 0x61dc
xorl %eax, %eax
callq 0x2050
jmp 0x33f3
leaq 0x2df9(%rip), %rdi      # 0x61f3
callq 0x21d0
jmp 0x3401
leaq 0x2e70(%rip), %rdi      # 0x6278
xorl %eax, %eax
callq 0x2050
jmp 0x3411
movl $0x0, -0x4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x3c40
leaq -0xc8(%rbp), %rdi
callq 0x2140
leaq -0x80(%rbp), %rax
movq %rax, -0x298(%rbp)
addq $0x60, %rax
movq %rax, -0x290(%rbp)
jmp 0x3462
leaq -0xe0(%rbp), %rdi
callq 0x3c40
leaq -0xc8(%rbp), %rdi
callq 0x2140
jmp 0x349f
movq -0x290(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x2140
movq -0x298(%rbp), %rcx
movq -0x2a0(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x290(%rbp)
jne 0x3462
movl -0x4(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
leaq -0x80(%rbp), %rax
movq %rax, -0x2b0(%rbp)
addq $0x60, %rax
movq %rax, -0x2a8(%rbp)
movq -0x2a8(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x2140
movq -0x2b0(%rbp), %rcx
movq -0x2b8(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x2a8(%rbp)
jne 0x34b5
jmp 0x34e8
movq -0x98(%rbp), %rdi
callq 0x2320
nopw %cs:(%rax,%rax)
nop