// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1e8, %rsp            # imm = 0x1E8
movq %r9, 0x80(%rsp)
movl %r8d, %r14d
movl %ecx, %ebp
movq %rsi, 0x40(%rsp)
cvtsi2sd %ecx, %xmm0
movq %rdi, 0xb0(%rsp)
movsd %xmm0, 0xb8(%rsp)
mulsd 0x2813(%rip), %xmm0     # 0x6198
cvtsi2sd %r8d, %xmm1
movsd %xmm1, 0xa8(%rsp)
divsd %xmm1, %xmm0
leaq 0x158(%rsp), %rsi
movsd %xmm0, (%rsi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsi)
movq %rdx, 0xc0(%rsp)
addq $0x18, %rdx
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, 0xa0(%rsp)
callq 0x5d34
movq %rbx, %rdi
callq 0x5d8e
leaq 0x140(%rsp), %rdi
movq %rax, %rsi
movsd 0x27ad(%rip), %xmm0     # 0x6198
callq 0x5d12
movl %r14d, %eax
movl %ebp, 0x8(%rsp)
imull %ebp, %eax
movl %eax, 0x18(%rsp)
movslq %eax, %r15
movl $0x18, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
callq 0x2030
movq %rax, %rbx
testl %r15d, %r15d
je 0x3a6a
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, 0x78(%rsp)
movq %rbx, 0x38(%rsp)
testl %r14d, %r14d
jle 0x3f5c
movq 0x40(%rsp), %rcx
leal (,%rcx,4), %eax
movl %eax, 0x1c(%rsp)
leal -0x1(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x2640(%rip), %xmm1     # 0x60f0
divsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
movl $0x0, 0xc(%rsp)
xorpd %xmm0, %xmm0
movq %r14, 0x30(%rsp)
movq 0x550b(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movsd %xmm0, 0x48(%rsp)
mulsd 0x26ba(%rip), %xmm0     # 0x61a0
divsd 0x88(%rsp), %xmm0
leaq 0x270a(%rip), %rsi      # 0x6200
movl 0x1c(%rsp), %edx
movb $0x1, %al
callq 0x2150
cmpl $0x0, 0x8(%rsp)
jle 0x3f2e
movl 0xc(%rsp), %eax
notl %eax
addl 0x30(%rsp), %eax
imull 0x8(%rsp), %eax
movl %eax, 0x20(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
addl 0x20(%rsp), %eax
movl %ecx, 0x24(%rsp)
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0xc8(%rsp)
cltq
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x90(%rsp)
movb $0x1, %al
movsd 0x2621(%rip), %xmm0     # 0x6180
movsd %xmm0, 0x50(%rsp)
movl %eax, 0x28(%rsp)
movb $0x1, %al
movsd 0x260d(%rip), %xmm0     # 0x6180
movsd %xmm0, 0x58(%rsp)
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rax
movl %eax, %r14d
testl %eax, %eax
jle 0x3e27
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x25af(%rip), %xmm0     # 0x6150
addsd %xmm0, %xmm0
movsd 0x2543(%rip), %xmm2     # 0x60f0
ucomisd %xmm0, %xmm2
jbe 0x3bc3
ucomisd 0x25d5(%rip), %xmm0     # 0x6190
jb 0x3be2
sqrtsd %xmm0, %xmm0
jmp 0x3be7
movsd 0x258d(%rip), %xmm1     # 0x6158
subsd %xmm0, %xmm1
ucomisd 0x25b9(%rip), %xmm1     # 0x6190
jb 0x3bf7
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x3c08
callq 0x20e0
addsd 0x2511(%rip), %xmm0     # 0x6100
movsd %xmm0, 0x10(%rsp)
jmp 0x3c16
movapd %xmm1, %xmm0
callq 0x20e0
movsd 0x24e8(%rip), %xmm2     # 0x60f0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x2526(%rip), %xmm0     # 0x6150
addsd %xmm0, %xmm0
movsd 0x24ba(%rip), %xmm2     # 0x60f0
ucomisd %xmm0, %xmm2
jbe 0x3c4f
ucomisd 0x254c(%rip), %xmm0     # 0x6190
jb 0x3c6e
xorps %xmm2, %xmm2
sqrtsd %xmm0, %xmm2
jmp 0x3c77
movsd 0x2501(%rip), %xmm1     # 0x6158
subsd %xmm0, %xmm1
ucomisd 0x252d(%rip), %xmm1     # 0x6190
jb 0x3c87
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x3c98
callq 0x20e0
movapd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
addsd 0x247b(%rip), %xmm2     # 0x6100
jmp 0x3ca2
movapd %xmm1, %xmm0
callq 0x20e0
movsd 0x2458(%rip), %xmm2     # 0x60f0
subsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
addsd 0x50(%rsp), %xmm2
movsd %xmm2, 0x10(%rsp)
addsd 0x58(%rsp), %xmm1
movsd 0x24c4(%rip), %xmm0     # 0x6180
mulsd %xmm0, %xmm1
addsd 0xc8(%rsp), %xmm1
divsd 0xb8(%rsp), %xmm1
movsd 0x24ce(%rip), %xmm0     # 0x61a8
addsd %xmm0, %xmm1
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
leaq 0x158(%rsp), %rsi
movapd %xmm1, %xmm0
callq 0x5d12
movsd 0x10(%rsp), %xmm0
mulsd 0x2478(%rip), %xmm0     # 0x6180
addsd 0x48(%rsp), %xmm0
divsd 0xa8(%rsp), %xmm0
addsd 0x2489(%rip), %xmm0     # 0x61a8
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x140(%rsp), %rsi
callq 0x5d12
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5ca6
leaq 0x128(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq 0xa0(%rsp), %rdx
callq 0x5ca6
leaq 0x188(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movsd 0x2432(%rip), %xmm0     # 0x61b0
callq 0x5d12
leaq 0x1a0(%rsp), %r12
movq %r12, %rdi
movq 0xc0(%rsp), %rsi
movq %r15, %rdx
callq 0x5ca6
movq %r13, %rdi
callq 0x5d8e
leaq 0x170(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x5c94
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x59b4
leaq 0x1b8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq 0xb0(%rsp), %rcx
callq 0x2f38
movq %rbp, %rdi
movq %r15, %rsi
movsd 0x98(%rsp), %xmm0
callq 0x5d12
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
leaq 0x60(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x5ca6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5d7a
decl %r14d
jne 0x3b8d
movsd 0x70(%rsp), %xmm0
movsd 0x22bb(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
cmpltsd 0x234e(%rip), %xmm0     # 0x6190
andnpd %xmm1, %xmm0
movapd 0x60(%rsp), %xmm1
movapd 0x21dc(%rip), %xmm2     # 0x6030
minpd %xmm1, %xmm2
xorpd %xmm3, %xmm3
cmpnltpd %xmm3, %xmm1
andpd %xmm2, %xmm1
movapd %xmm1, 0xd0(%rsp)
movlpd %xmm0, 0xe0(%rsp)
leaq 0x128(%rsp), %r14
movq %r14, %rdi
leaq 0xd0(%rsp), %rsi
movsd 0x22f6(%rip), %xmm0     # 0x6188
callq 0x5d12
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movq 0x90(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x5ca6
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5d7a
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x5d7a
movsd 0x22cc(%rip), %xmm0     # 0x61b8
movsd %xmm0, 0x58(%rsp)
testb $0x1, 0x2c(%rsp)
movl $0x0, %eax
jne 0x3b79
movsd 0x22ae(%rip), %xmm0     # 0x61b8
movsd %xmm0, 0x50(%rsp)
testb $0x1, 0x28(%rsp)
jne 0x3b65
movl 0x24(%rsp), %ecx
incl %ecx
movzwl %cx, %eax
cmpl 0x8(%rsp), %eax
jl 0x3b23
movsd 0x21ba(%rip), %xmm0     # 0x60f0
movsd 0x48(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl 0xc(%rsp), %eax
incl %eax
movq 0x30(%rsp), %r14
movl %eax, 0xc(%rsp)
cmpl %r14d, %eax
jne 0x3ace
leaq 0x22c7(%rip), %rsi      # 0x622a
movq 0x80(%rsp), %rdi
callq 0x2130
movq %rax, %rbx
leaq 0x22b2(%rip), %rsi      # 0x622c
movq %rax, %rdi
movl 0x8(%rsp), %edx
movl %r14d, %ecx
movl $0xff, %r8d
xorl %eax, %eax
callq 0x2150
cmpl $0x0, 0x18(%rsp)
movq 0x38(%rsp), %r14
jle 0x40a0
movq 0x78(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
xorl %r13d, %r13d
movsd (%r14,%r13), %xmm0
movsd 0x2131(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm0
andnpd %xmm1, %xmm0
movsd 0x21e8(%rip), %xmm1     # 0x61c0
callq 0x21b0
movsd 0x21e3(%rip), %xmm1     # 0x61c8
mulsd %xmm1, %xmm0
movsd 0x218f(%rip), %xmm1     # 0x6180
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
movsd 0x8(%r14,%r13), %xmm0
movsd 0x20e8(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
cmpltsd 0x217b(%rip), %xmm0     # 0x6190
andnpd %xmm1, %xmm0
movsd 0x219f(%rip), %xmm1     # 0x61c0
callq 0x21b0
mulsd 0x219a(%rip), %xmm0     # 0x61c8
addsd 0x214a(%rip), %xmm0     # 0x6180
cvttsd2si %xmm0, %r15d
movsd 0x10(%r14,%r13), %xmm0
movsd 0x20a6(%rip), %xmm1     # 0x60f0
minsd %xmm0, %xmm1
cmpltsd 0x2139(%rip), %xmm0     # 0x6190
andnpd %xmm1, %xmm0
movsd 0x215d(%rip), %xmm1     # 0x61c0
callq 0x21b0
mulsd 0x2158(%rip), %xmm0     # 0x61c8
addsd 0x2108(%rip), %xmm0     # 0x6180
cvttsd2si %xmm0, %r8d
movq %rbx, %rdi
leaq 0x21b2(%rip), %rsi      # 0x6239
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2150
addq $0x18, %r13
cmpq %r13, %r12
jne 0x3fb1
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop