// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1f8, %rsp            # imm = 0x1F8
movq %r9, 0x70(%rsp)
movl %r8d, %r14d
movl %ecx, %ebp
movq %rsi, 0x18(%rsp)
cvtsi2sd %ecx, %xmm0
movq %rdi, 0xb8(%rsp)
movsd %xmm0, 0xc0(%rsp)
mulsd 0x3640(%rip), %xmm0     # 0x70a8
cvtsi2sd %r8d, %xmm1
movsd %xmm1, 0xb0(%rsp)
divsd %xmm1, %xmm0
leaq 0x168(%rsp), %rsi
movsd %xmm0, (%rsi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsi)
movq %rdx, 0xc8(%rsp)
addq $0x18, %rdx
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, 0xa8(%rsp)
callq 0x5f14
movq %rbx, %rdi
callq 0x5f8a
leaq 0x150(%rsp), %rdi
movq %rax, %rsi
movsd 0x35da(%rip), %xmm0     # 0x70a8
callq 0x5eec
movl %r14d, %eax
movl %ebp, 0x8(%rsp)
imull %ebp, %eax
movl %eax, 0x28(%rsp)
movslq %eax, %r15
movl $0x18, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
callq 0x2030
movq %rax, %rbx
testl %r15d, %r15d
je 0x3b4d
leaq (%r15,%r15,2), %rax
leaq -0x18(,%rax,8), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
movq %r15, 0x68(%rsp)
movq %rbx, 0x40(%rsp)
testl %r14d, %r14d
jle 0x40a4
movq 0x18(%rsp), %rcx
leal (,%rcx,4), %eax
movl %eax, 0x2c(%rsp)
leal -0x1(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x78(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x34d0(%rip), %xmm1     # 0x7060
divsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%rsp)
movl $0x0, 0xc(%rsp)
xorpd %xmm0, %xmm0
movq %r14, 0x38(%rsp)
movq 0x642b(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
movsd %xmm0, 0x48(%rsp)
mulsd 0x34ea(%rip), %xmm0     # 0x70b0
divsd 0x78(%rsp), %xmm0
leaq 0x353d(%rip), %rsi      # 0x7110
movl 0x2c(%rsp), %edx
movb $0x1, %al
callq 0x2150
movsd 0x347a(%rip), %xmm0     # 0x7060
cmpl $0x0, 0x8(%rsp)
jle 0x407e
movl 0xc(%rsp), %eax
notl %eax
addl 0x38(%rsp), %eax
imull 0x8(%rsp), %eax
movl %eax, 0x30(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
addl 0x30(%rsp), %eax
movl %ecx, 0x34(%rsp)
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0xd0(%rsp)
cltq
leaq (%rax,%rax,2), %rax
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x90(%rsp)
xorpd %xmm1, %xmm1
xorl %eax, %eax
movq %rax, 0x80(%rsp)
movsd %xmm1, 0x88(%rsp)
addsd 0x3437(%rip), %xmm1     # 0x7090
movsd %xmm1, 0xd8(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rax, 0x98(%rsp)
cmpl $0x0, 0x18(%rsp)
jle 0x3f40
movsd 0x20(%rsp), %xmm0
addsd 0x3401(%rip), %xmm0     # 0x7090
movsd %xmm0, 0xe0(%rsp)
movq 0x18(%rsp), %rax
movl %eax, %r14d
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x33a4(%rip), %xmm0     # 0x7058
addsd %xmm0, %xmm0
movsd 0x33a0(%rip), %xmm2     # 0x7060
ucomisd %xmm0, %xmm2
jbe 0x3cd6
ucomisd 0x33d2(%rip), %xmm0     # 0x70a0
jb 0x3cf5
sqrtsd %xmm0, %xmm0
jmp 0x3cfa
movsd 0x338a(%rip), %xmm1     # 0x7068
subsd %xmm0, %xmm1
ucomisd 0x33b6(%rip), %xmm1     # 0x70a0
jb 0x3d0a
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x3d1b
callq 0x20e0
addsd 0x334e(%rip), %xmm0     # 0x7050
movsd %xmm0, 0x10(%rsp)
jmp 0x3d29
movapd %xmm1, %xmm0
callq 0x20e0
movsd 0x3345(%rip), %xmm2     # 0x7060
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x331b(%rip), %xmm0     # 0x7058
addsd %xmm0, %xmm0
movsd 0x3317(%rip), %xmm2     # 0x7060
ucomisd %xmm0, %xmm2
jbe 0x3d62
ucomisd 0x3349(%rip), %xmm0     # 0x70a0
jb 0x3d81
xorps %xmm2, %xmm2
sqrtsd %xmm0, %xmm2
jmp 0x3d8a
movsd 0x32fe(%rip), %xmm1     # 0x7068
subsd %xmm0, %xmm1
ucomisd 0x332a(%rip), %xmm1     # 0x70a0
jb 0x3d9a
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x3dab
callq 0x20e0
movapd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
addsd 0x32b8(%rip), %xmm2     # 0x7050
jmp 0x3db5
movapd %xmm1, %xmm0
callq 0x20e0
movsd 0x32b5(%rip), %xmm2     # 0x7060
subsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
addsd 0xd8(%rsp), %xmm2
movsd %xmm2, 0x10(%rsp)
addsd 0xe0(%rsp), %xmm1
movsd 0x32bb(%rip), %xmm0     # 0x7090
mulsd %xmm0, %xmm1
addsd 0xd0(%rsp), %xmm1
divsd 0xc0(%rsp), %xmm1
movsd 0x32c5(%rip), %xmm0     # 0x70b8
addsd %xmm0, %xmm1
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0x168(%rsp), %rsi
movapd %xmm1, %xmm0
callq 0x5eec
movsd 0x10(%rsp), %xmm0
mulsd 0x326f(%rip), %xmm0     # 0x7090
addsd 0x48(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
addsd 0x3280(%rip), %xmm0     # 0x70b8
leaq 0x1e0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x150(%rsp), %rsi
callq 0x5eec
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5e62
leaq 0x138(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq 0xa8(%rsp), %rdx
callq 0x5e62
leaq 0x198(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movsd 0x3229(%rip), %xmm0     # 0x70c0
callq 0x5eec
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
movq 0xc8(%rsp), %rsi
movq %r15, %rdx
callq 0x5e62
movq %r13, %rdi
callq 0x5f8a
leaq 0x180(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x5e44
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x5b70
leaq 0x1c8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq 0xb8(%rsp), %rcx
callq 0x3024
movq %rbp, %rdi
movq %r15, %rsi
movsd 0xa0(%rsp), %xmm0
callq 0x5eec
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x5e62
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5f6a
decl %r14d
jne 0x3ca0
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x310c(%rip), %xmm3     # 0x7060
movapd %xmm3, %xmm2
minsd %xmm0, %xmm2
xorpd %xmm4, %xmm4
cmpltsd %xmm4, %xmm0
andnpd %xmm2, %xmm0
movapd %xmm3, %xmm2
minsd %xmm1, %xmm2
cmpltsd %xmm4, %xmm1
andnpd %xmm2, %xmm1
movsd 0x60(%rsp), %xmm2
minsd %xmm2, %xmm3
cmpltsd %xmm4, %xmm2
andnpd %xmm3, %xmm2
movlpd %xmm0, 0xe8(%rsp)
movlpd %xmm1, 0xf0(%rsp)
movlpd %xmm2, 0xf8(%rsp)
leaq 0x138(%rsp), %r14
movq %r14, %rdi
leaq 0xe8(%rsp), %rsi
movsd 0x30d5(%rip), %xmm0     # 0x7098
callq 0x5eec
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
movq 0x90(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x5e62
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5f6a
movsd 0x20(%rsp), %xmm0
addsd 0x3061(%rip), %xmm0     # 0x7060
movsd %xmm0, 0x20(%rsp)
movq 0x98(%rsp), %r15
leal 0x1(%r15), %r14d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x100(%rsp)
movq $0x0, 0x110(%rsp)
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x5f6a
testl %r15d, %r15d
movl %r14d, %eax
je 0x3c6e
movsd 0x3015(%rip), %xmm0     # 0x7060
movsd 0x88(%rsp), %xmm1
addsd %xmm0, %xmm1
movq 0x80(%rsp), %rcx
leal 0x1(%rcx), %eax
testl %ecx, %ecx
je 0x3c40
movl 0x34(%rsp), %ecx
incl %ecx
movzwl %cx, %eax
cmpl 0x8(%rsp), %eax
jl 0x3c08
movsd 0x48(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl 0xc(%rsp), %eax
incl %eax
movq 0x38(%rsp), %r14
movl %eax, 0xc(%rsp)
cmpl %r14d, %eax
jne 0x3bae
leaq 0x308f(%rip), %rsi      # 0x713a
movq 0x70(%rsp), %rdi
callq 0x2130
movq %rax, %rbx
leaq 0x307d(%rip), %rsi      # 0x713c
movq %rax, %rdi
movl 0x8(%rsp), %edx
movl %r14d, %ecx
movl $0xff, %r8d
xorl %eax, %eax
callq 0x2150
cmpl $0x0, 0x28(%rsp)
movq 0x40(%rsp), %r14
jle 0x41e5
movq 0x68(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
xorl %r13d, %r13d
movsd (%r14,%r13), %xmm0
movsd 0x2f5c(%rip), %xmm1     # 0x7060
minsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm0
andnpd %xmm1, %xmm0
movsd 0x2fab(%rip), %xmm1     # 0x70c8
callq 0x21b0
movsd 0x2fa6(%rip), %xmm1     # 0x70d0
mulsd %xmm1, %xmm0
movsd 0x2f5a(%rip), %xmm1     # 0x7090
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
movsd 0x8(%r14,%r13), %xmm0
movsd 0x2f13(%rip), %xmm1     # 0x7060
minsd %xmm0, %xmm1
cmpltsd 0x2f46(%rip), %xmm0     # 0x70a0
andnpd %xmm1, %xmm0
movsd 0x2f62(%rip), %xmm1     # 0x70c8
callq 0x21b0
mulsd 0x2f5d(%rip), %xmm0     # 0x70d0
addsd 0x2f15(%rip), %xmm0     # 0x7090
cvttsd2si %xmm0, %r15d
movsd 0x10(%r14,%r13), %xmm0
movsd 0x2ed1(%rip), %xmm1     # 0x7060
minsd %xmm0, %xmm1
cmpltsd 0x2f04(%rip), %xmm0     # 0x70a0
andnpd %xmm1, %xmm0
movsd 0x2f20(%rip), %xmm1     # 0x70c8
callq 0x21b0
mulsd 0x2f1b(%rip), %xmm0     # 0x70d0
addsd 0x2ed3(%rip), %xmm0     # 0x7090
cvttsd2si %xmm0, %r8d
movq %rbx, %rdi
leaq 0x2f7d(%rip), %rsi      # 0x7149
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2150
addq $0x18, %r13
cmpq %r13, %r12
jne 0x40f6
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop