// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %r9, 0x78(%rsp)
movl %r8d, %r14d
movl %ecx, %ebp
movl %esi, %r12d
cvtsi2sd %ecx, %xmm0
movq %rdi, 0xc0(%rsp)
movsd %xmm0, 0xc8(%rsp)
mulsd 0x28e0(%rip), %xmm0     # 0x6198
cvtsi2sd %r8d, %xmm1
movsd %xmm1, 0xb8(%rsp)
divsd %xmm1, %xmm0
leaq 0x178(%rsp), %rsi
movsd %xmm0, (%rsi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsi)
movq %rdx, 0xd0(%rsp)
addq $0x18, %rdx
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, 0xb0(%rsp)
callq 0x5c34
movq %rbx, %rdi
callq 0x5c8e
leaq 0x160(%rsp), %rdi
movq %rax, %rsi
movsd 0x287a(%rip), %xmm0     # 0x6198
callq 0x5c12
movq %r14, 0x20(%rsp)
movl %ebp, 0x4(%rsp)
imull %ebp, %r14d
movq %r14, 0x70(%rsp)
movslq %r14d, %rbx
pushq $0x18
popq %r14
movq %rbx, %rax
mulq %r14
pushq $-0x1
popq %rdi
cmovnoq %rax, %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
callq 0x2030
movq %rax, %r15
testl %ebx, %ebx
je 0x3987
imulq $0x18, %rbx, %rcx
addq $-0x18, %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r14
subq %rdx, %rcx
addq $0x18, %rcx
movq %r15, %rdi
xorl %esi, %esi
movq %rcx, %rdx
callq 0x20c0
movq %r15, 0x48(%rsp)
leal (,%r12,4), %eax
movl %eax, 0x3c(%rsp)
movq 0x20(%rsp), %rcx
leal -0x1(%rcx), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x80(%rsp)
cvtsi2sd %r12d, %xmm0
movsd 0x2736(%rip), %xmm1     # 0x60f0
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
movq %r12, 0x90(%rsp)
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
divsd %xmm0, %xmm1
movsd %xmm1, 0xa8(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl %edx, 0x38(%rsp)
cmpl %edx, %eax
je 0x3ea7
movq 0x55e1(%rip), %rcx      # 0x8fe0
movq (%rcx), %rdi
movsd 0x18(%rsp), %xmm0
mulsd 0x2790(%rip), %xmm0     # 0x61a0
divsd 0x80(%rsp), %xmm0
leaq 0x27e0(%rip), %rsi      # 0x6200
movl 0x3c(%rsp), %edx
movl %eax, 0x14(%rsp)
movb $0x1, %al
callq 0x2150
movl 0x14(%rsp), %eax
movl %eax, %ecx
notl %ecx
addl 0x20(%rsp), %ecx
imull 0x4(%rsp), %ecx
movq %rcx, 0x88(%rsp)
xorl %ecx, %ecx
movsd 0x269e(%rip), %xmm0     # 0x60f0
movzwl %cx, %ecx
cmpl 0x4(%rsp), %ecx
jge 0x3e8c
movq 0x88(%rsp), %rax
addl %ecx, %eax
cvtsi2sd %ecx, %xmm1
movsd %xmm1, 0xd8(%rsp)
cltq
imulq $0x18, %rax, %rax
addq 0x48(%rsp), %rax
movq %rax, 0x98(%rsp)
xorl %eax, %eax
xorpd %xmm1, %xmm1
movsd %xmm1, 0x28(%rsp)
movq %rcx, 0xa0(%rsp)
cmpl $0x2, %eax
je 0x3e81
movsd 0x28(%rsp), %xmm1
addsd 0x26c4(%rip), %xmm1     # 0x6178
movsd %xmm1, 0xe0(%rsp)
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
movsd %xmm1, 0x30(%rsp)
movl %eax, 0x40(%rsp)
cmpl $0x2, %ebp
je 0x3e6a
movl %ebp, 0x44(%rsp)
movsd 0x30(%rsp), %xmm0
addsd 0x2690(%rip), %xmm0     # 0x6178
movsd %xmm0, 0xe8(%rsp)
movq 0x90(%rsp), %rax
movl %eax, %r12d
subl $0x1, %r12d
jb 0x3d7b
callq 0x3ffb
addsd %xmm0, %xmm0
movsd 0x25d9(%rip), %xmm2     # 0x60f0
ucomisd %xmm0, %xmm2
jbe 0x3b2d
ucomisd 0x2663(%rip), %xmm0     # 0x6188
jb 0x3b49
sqrtsd %xmm0, %xmm0
jmp 0x3b4e
movsd 0x261b(%rip), %xmm1     # 0x6150
subsd %xmm0, %xmm1
ucomisd 0x2647(%rip), %xmm1     # 0x6188
jb 0x3b5e
sqrtsd %xmm1, %xmm0
jmp 0x3b6f
callq 0x20e0
addsd 0x25aa(%rip), %xmm0     # 0x6100
movsd %xmm0, 0x8(%rsp)
jmp 0x3b7d
movapd %xmm1, %xmm0
callq 0x20e0
movsd 0x2581(%rip), %xmm2     # 0x60f0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
callq 0x3ffb
addsd %xmm0, %xmm0
movsd 0x2562(%rip), %xmm2     # 0x60f0
ucomisd %xmm0, %xmm2
jbe 0x3ba4
ucomisd 0x25ec(%rip), %xmm0     # 0x6188
jb 0x3bc0
sqrtsd %xmm0, %xmm2
jmp 0x3bc9
movsd 0x25a4(%rip), %xmm1     # 0x6150
subsd %xmm0, %xmm1
ucomisd 0x25d0(%rip), %xmm1     # 0x6188
jb 0x3bd9
sqrtsd %xmm1, %xmm0
jmp 0x3bea
callq 0x20e0
movapd %xmm0, %xmm2
movsd 0x8(%rsp), %xmm1
addsd 0x2529(%rip), %xmm2     # 0x6100
jmp 0x3bf4
movapd %xmm1, %xmm0
callq 0x20e0
movsd 0x2506(%rip), %xmm2     # 0x60f0
subsd %xmm0, %xmm2
movsd 0x8(%rsp), %xmm1
addsd 0xe0(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
addsd 0xe8(%rsp), %xmm1
movsd 0x2564(%rip), %xmm0     # 0x6178
mulsd %xmm0, %xmm1
addsd 0xd8(%rsp), %xmm1
divsd 0xc8(%rsp), %xmm1
movsd 0x2576(%rip), %xmm0     # 0x61a8
addsd %xmm0, %xmm1
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x178(%rsp), %rsi
movapd %xmm1, %xmm0
callq 0x5c12
movsd 0x8(%rsp), %xmm0
mulsd 0x2518(%rip), %xmm0     # 0x6178
addsd 0x18(%rsp), %xmm0
divsd 0xb8(%rsp), %xmm0
addsd 0x2531(%rip), %xmm0     # 0x61a8
leaq 0x1f0(%rsp), %r13
movq %r13, %rdi
leaq 0x160(%rsp), %rsi
callq 0x5c12
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x5ba6
leaq 0x148(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq 0xb0(%rsp), %rdx
callq 0x5ba6
leaq 0x1a8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd 0x24da(%rip), %xmm0     # 0x61b0
callq 0x5c12
leaq 0x1c0(%rsp), %rbp
movq %rbp, %rdi
movq 0xd0(%rsp), %rsi
movq %r14, %rdx
callq 0x5ba6
movq %rbx, %rdi
callq 0x5c8e
leaq 0x190(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5b94
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x587c
leaq 0x1d8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
movq 0xc0(%rsp), %rcx
callq 0x2eb8
movq %r13, %rdi
movq %rbx, %rsi
movsd 0xa8(%rsp), %xmm0
callq 0x5c12
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x50(%rsp), %r14
movq %r14, %rsi
movq %r13, %rdx
callq 0x5ba6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5c7a
jmp 0x3afc
movsd 0x60(%rsp), %xmm0
movsd 0x2367(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
cmpltsd 0x23f2(%rip), %xmm0     # 0x6188
andnpd %xmm1, %xmm0
movapd 0x50(%rsp), %xmm1
movapd 0x2288(%rip), %xmm2     # 0x6030
minpd %xmm1, %xmm2
xorpd %xmm3, %xmm3
cmpnltpd %xmm3, %xmm1
andpd %xmm2, %xmm1
movapd %xmm1, 0xf0(%rsp)
movlpd %xmm0, 0x100(%rsp)
leaq 0x148(%rsp), %r14
movq %r14, %rdi
leaq 0xf0(%rsp), %rsi
movsd 0x239a(%rip), %xmm0     # 0x6180
callq 0x5c12
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq 0x98(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x5ba6
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5c7a
movsd 0x30(%rsp), %xmm0
addsd 0x22ce(%rip), %xmm0     # 0x60f0
movsd %xmm0, 0x30(%rsp)
movl 0x44(%rsp), %ebp
incl %ebp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x110(%rsp)
andq $0x0, 0x120(%rsp)
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x5c7a
movsd 0x2297(%rip), %xmm0     # 0x60f0
movq 0xa0(%rsp), %rcx
movl 0x40(%rsp), %eax
jmp 0x3acd
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
incl %eax
jmp 0x3a9d
incl %ecx
movl 0x14(%rsp), %eax
jmp 0x3a52
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
incl %eax
movl 0x38(%rsp), %edx
jmp 0x39f0
leaq 0x237c(%rip), %rsi      # 0x622a
movq 0x78(%rsp), %rdi
callq 0x2130
movq %rax, %rbx
leaq 0x236a(%rip), %rsi      # 0x622c
xorl %r14d, %r14d
movq %rax, %rdi
movl 0x4(%rsp), %edx
movq 0x20(%rsp), %rcx
movl $0xff, %r8d
xorl %eax, %eax
callq 0x2150
movq 0x70(%rsp), %rax
testl %eax, %eax
cmovlel %r14d, %eax
imulq $0x18, %rax, %r12
xorl %r13d, %r13d
movq 0x48(%rsp), %r14
cmpq %r13, %r12
movsd 0x21f0(%rip), %xmm1     # 0x60f0
je 0x3fe9
movsd (%r14,%r13), %xmm0
minsd %xmm0, %xmm1
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm0
andnpd %xmm1, %xmm0
movsd 0x2293(%rip), %xmm1     # 0x61b8
callq 0x21b0
movsd 0x228e(%rip), %xmm1     # 0x61c0
mulsd %xmm1, %xmm0
movsd 0x223a(%rip), %xmm1     # 0x6178
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
movsd 0x8(%r14,%r13), %xmm0
movsd 0x219b(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
cmpltsd 0x2226(%rip), %xmm0     # 0x6188
andnpd %xmm1, %xmm0
movsd 0x224a(%rip), %xmm1     # 0x61b8
callq 0x21b0
mulsd 0x2245(%rip), %xmm0     # 0x61c0
addsd 0x21f5(%rip), %xmm0     # 0x6178
cvttsd2si %xmm0, %r15d
movsd 0x10(%r14,%r13), %xmm0
movsd 0x2159(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
cmpltsd 0x21e4(%rip), %xmm0     # 0x6188
andnpd %xmm1, %xmm0
movsd 0x2208(%rip), %xmm1     # 0x61b8
callq 0x21b0
mulsd 0x2203(%rip), %xmm0     # 0x61c0
addsd 0x21b3(%rip), %xmm0     # 0x6178
cvttsd2si %xmm0, %r8d
movq %rbx, %rdi
leaq 0x2265(%rip), %rsi      # 0x6239
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2150
addq $0x18, %r13
jmp 0x3ef5
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq