// File: Aries-orz[P]Ari-RayTracing/src/Engine.cpp
// Function: rt::engine::raytracing(rt::Scene*, int, rt::Ray, int, int, char const*)
void raytracing(Scene* scene, int samps, Ray camera, int w, int h, const char* outfile) {
    Vec3d cx = Vec3d(w*.5135/h);
    Vec3d cy = (cx^camera.d).norm()*.5135;
    Vec3d r;
    Vec3d *c = new Vec3d[w*h];
    
    #pragma omp parallel for schedule(dynamic, 1) private(r)
    for (int y=0; y<h; ++y) {
        fprintf(stderr,"\rRendering (%d samples per pixel) %5.2f%%",samps*4,100.*y/(h-1));
        for (unsigned short x=0; x<w; x++)
            //For every pixel, make a 2x2 sampling
            for (int sy=0, i=(h-y-1)*w+x; sy<2; sy++)
                for (int sx=0; sx<2; sx++, r=Vec3d()) {
                    for (int s=0; s<samps; s++) {
                        double r1=2*randRT(), dx=r1<1 ? sqrt(r1)-1: 1-sqrt(2-r1);
                        double r2=2*randRT(), dy=r2<1 ? sqrt(r2)-1: 1-sqrt(2-r2);
                        Vec3d d = cx*( ( (sx+.5 + dx)/2 + x)/w - .5) +
                        cy*( ( (sy+.5 + dy)/2 + y)/h - .5) + camera.d;
                        r = r + radianceRT(Ray(camera.o+d*140,d.norm()), 0, scene)*(1./samps);
                    }
                    c[i] = c[i] + Vec3d(clamp(r.x),clamp(r.y),clamp(r.z))*.25;
                }
    }
    
    FILE *f = fopen(outfile, "w");
    fprintf(f, "P3\n%d %d\n%d\n", w, h, 255);
    for (int i=0; i<w*h; i++)
        fprintf(f,"%d %d %d ", toInt(c[i].x), toInt(c[i].y), toInt(c[i].z));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdx, -0x288(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd 0x6d5b(%rip), %xmm1     # 0xb1a0
mulsd %xmm1, %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
leaq -0x40(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm1
callq 0x3540
movq -0x288(%rbp), %rdx
addq $0x18, %rdx
leaq -0x70(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xa050
leaq -0x70(%rbp), %rdi
callq 0xa170
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
movsd 0x6d0f(%rip), %xmm0     # 0xb1a0
callq 0xa000
leaq -0x88(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x3540
movl -0x1c(%rbp), %eax
imull -0x20(%rbp), %eax
cltq
movq %rax, -0x278(%rbp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x280(%rbp)
callq 0x2030
movq %rax, %rcx
movq -0x278(%rbp), %rax
movq %rcx, -0x270(%rbp)
cmpq $0x0, %rax
je 0x455c
movq -0x270(%rbp), %rax
movq -0x278(%rbp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x298(%rbp)
movq %rax, -0x290(%rbp)
movq -0x290(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x3540
jmp 0x453e
movq -0x298(%rbp), %rcx
movq -0x2a0(%rbp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0x290(%rbp)
jne 0x4520
movq -0x270(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x4a9e
movq 0xba56(%rip), %rax      # 0xffe0
movq (%rax), %rdi
movl -0xc(%rbp), %edx
shll $0x2, %edx
cvtsi2sdl -0xa0(%rbp), %xmm1
movsd 0x6c05(%rip), %xmm0     # 0xb1a8
mulsd %xmm1, %xmm0
movl -0x20(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
leaq 0x6c24(%rip), %rsi      # 0xb1e0
movb $0x1, %al
callq 0x2140
movw $0x0, -0xa2(%rbp)
movzwl -0xa2(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4a88
movl $0x0, -0xa8(%rbp)
movl -0x20(%rbp), %eax
subl -0xa0(%rbp), %eax
subl $0x1, %eax
imull -0x1c(%rbp), %eax
movzwl -0xa2(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0xac(%rbp)
cmpl $0x2, -0xa8(%rbp)
jge 0x4a6f
movl $0x0, -0xb0(%rbp)
cmpl $0x2, -0xb0(%rbp)
jge 0x4a59
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x495b
callq 0x4bc0
movaps %xmm0, %xmm1
movsd 0x6b26(%rip), %xmm0     # 0xb178
mulsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd 0x69a2(%rip), %xmm0     # 0xb008
ucomisd -0xc0(%rbp), %xmm0
jbe 0x4693
movsd -0xc0(%rbp), %xmm0
callq 0x20d0
movsd 0x6983(%rip), %xmm1     # 0xb008
subsd %xmm1, %xmm0
movsd %xmm0, -0x2a8(%rbp)
jmp 0x46bf
movsd 0x6add(%rip), %xmm0     # 0xb178
subsd -0xc0(%rbp), %xmm0
callq 0x20d0
movaps %xmm0, %xmm1
movsd 0x6955(%rip), %xmm0     # 0xb008
subsd %xmm1, %xmm0
movsd %xmm0, -0x2a8(%rbp)
movsd -0x2a8(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
callq 0x4bc0
movaps %xmm0, %xmm1
movsd 0x6a99(%rip), %xmm0     # 0xb178
mulsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd 0x6915(%rip), %xmm0     # 0xb008
ucomisd -0xd0(%rbp), %xmm0
jbe 0x4720
movsd -0xd0(%rbp), %xmm0
callq 0x20d0
movsd 0x68f6(%rip), %xmm1     # 0xb008
subsd %xmm1, %xmm0
movsd %xmm0, -0x2b0(%rbp)
jmp 0x474c
movsd 0x6a50(%rip), %xmm0     # 0xb178
subsd -0xd0(%rbp), %xmm0
callq 0x20d0
movaps %xmm0, %xmm1
movsd 0x68c8(%rip), %xmm0     # 0xb008
subsd %xmm1, %xmm0
movsd %xmm0, -0x2b0(%rbp)
movsd -0x2b0(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
cvtsi2sdl -0xb0(%rbp), %xmm0
movsd 0x6a14(%rip), %xmm1     # 0xb180
addsd %xmm1, %xmm0
addsd -0xc8(%rbp), %xmm0
movsd 0x69f8(%rip), %xmm1     # 0xb178
divsd %xmm1, %xmm0
movzwl -0xa2(%rbp), %eax
cvtsi2sd %eax, %xmm1
addsd %xmm1, %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x69dc(%rip), %xmm1     # 0xb180
subsd %xmm1, %xmm0
leaq -0x120(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xa000
cvtsi2sdl -0xa8(%rbp), %xmm0
movsd 0x69b8(%rip), %xmm1     # 0xb180
addsd %xmm1, %xmm0
addsd -0xd8(%rbp), %xmm0
movsd 0x699c(%rip), %xmm1     # 0xb178
divsd %xmm1, %xmm0
cvtsi2sdl -0xa0(%rbp), %xmm1
addsd %xmm1, %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x6983(%rip), %xmm1     # 0xb180
subsd %xmm1, %xmm0
leaq -0x138(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xa000
leaq -0x108(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x9ee0
movq -0x288(%rbp), %rdx
addq $0x18, %rdx
leaq -0xf0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x9ee0
leaq -0x1e0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movsd 0x6951(%rip), %xmm0     # 0xb1b0
callq 0xa000
movq -0x288(%rbp), %rsi
leaq -0x1c8(%rbp), %rdi
leaq -0x1e0(%rbp), %rdx
callq 0x9ee0
leaq -0xf0(%rbp), %rdi
callq 0xa170
movq %rax, %rsi
leaq -0x1f8(%rbp), %rdi
callq 0x9ea0
leaq -0x1b0(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
leaq -0x1f8(%rbp), %rdx
callq 0x8bc0
movq -0x8(%rbp), %rcx
leaq -0x180(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
xorl %edx, %edx
callq 0x3730
cvtsi2sdl -0xc(%rbp), %xmm1
movsd 0x672f(%rip), %xmm0     # 0xb008
divsd %xmm1, %xmm0
leaq -0x168(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0xa000
leaq -0x150(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x168(%rbp), %rdx
callq 0x9ee0
leaq -0x88(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0xa130
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x4633
movq -0x280(%rbp), %rsi
movq -0x270(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
callq 0x21e0
jmp 0x4bac
movq -0x90(%rbp), %rax
movslq -0xac(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x2b8(%rbp)
movsd -0x88(%rbp), %xmm0
callq 0x4be0
movsd %xmm0, -0x2c0(%rbp)
movsd -0x80(%rbp), %xmm0
callq 0x4be0
movsd %xmm0, -0x2c8(%rbp)
movsd -0x78(%rbp), %xmm0
callq 0x4be0
movsd -0x2c8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x2c0(%rbp), %xmm0
leaq -0x240(%rbp), %rdi
callq 0x3540
leaq -0x228(%rbp), %rdi
leaq -0x240(%rbp), %rsi
movsd 0x663b(%rip), %xmm0     # 0xb018
callq 0xa000
movq -0x2b8(%rbp), %rsi
leaq -0x210(%rbp), %rdi
leaq -0x228(%rbp), %rdx
callq 0x9ee0
movq -0x90(%rbp), %rdi
movslq -0xac(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq -0x210(%rbp), %rsi
callq 0xa130
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
leaq -0x258(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x3540
leaq -0x88(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0xa130
jmp 0x461c
jmp 0x4a5b
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x4605
jmp 0x4a71
movw -0xa2(%rbp), %ax
addw $0x1, %ax
movw %ax, -0xa2(%rbp)
jmp 0x45cc
jmp 0x4a8a
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x4574
movq -0x28(%rbp), %rdi
leaq 0x6761(%rip), %rsi      # 0xb20a
callq 0x2120
movq %rax, -0x260(%rbp)
movq -0x260(%rbp), %rdi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
leaq 0x6743(%rip), %rsi      # 0xb20c
movl $0xff, %r8d
movb $0x0, %al
callq 0x2140
movl $0x0, -0x264(%rbp)
movl -0x264(%rbp), %eax
movl -0x1c(%rbp), %ecx
imull -0x20(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4ba3
movq -0x260(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0x90(%rbp), %rax
movslq -0x264(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
callq 0x4c40
movl %eax, -0x2d0(%rbp)
movq -0x90(%rbp), %rax
movslq -0x264(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
callq 0x4c40
movl %eax, -0x2cc(%rbp)
movq -0x90(%rbp), %rax
movslq -0x264(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm0
callq 0x4c40
movq -0x2d8(%rbp), %rdi
movl -0x2d0(%rbp), %edx
movl -0x2cc(%rbp), %ecx
movl %eax, %r8d
leaq 0x6691(%rip), %rsi      # 0xb219
movb $0x0, %al
callq 0x2140
movl -0x264(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x264(%rbp)
jmp 0x4ae0
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x21b0
nopl (%rax,%rax)