// File: /workspace/github_cpp_makefile_star2_O1/Crawler995[P]Cpp-Ray-Tracing/libs/render_engine.cpp
// Function: RenderEngine::render(PerspectiveCamera&, GeometryUnion&, _IO_FILE*, unsigned char*)
void RenderEngine::render(
    PerspectiveCamera &camera, 
    GeometryUnion &geo_union,
    FILE *f, 
    unsigned char *output_img_data
) {
    int index = 0;
    int two_percent_num = image_width * image_height / 50;
    unsigned char *p = output_img_data;
    clock_t start, end;
    start = clock();

    std::string progress_char;
    std::cout << "Start rendering..." << std::endl;

    for(int i = image_height - 1; i >= 0; i--) {
        for(int j = 0; j < image_width; j++) {
            Color color(0, 0, 0);

            for(int k = 0; k < sample_times; k++) {
                double u = 1.0 * (j + rand_num_0_to_1()) / image_width;
                double v = 1.0 * (i + rand_num_0_to_1()) / image_height;

                Ray3 ray = camera.generate_ray(u, v);
                color = color.add(ray_tracing_recursive(ray, geo_union, 0));
            }

            color = color.multiply(1.0 / sample_times);

            *p++ = (unsigned char)color.get_r();
            *p++ = (unsigned char)color.get_g();
            *p++ = (unsigned char)color.get_b();

            index++;
            if(index % two_percent_num == 0) {
                update_progress(progress_char);
            }
        }
    }

    end = clock();
    double run_time = (double)(end - start) / CLOCKS_PER_SEC;

    svpng(f, image_width, image_height, output_img_data, 0);
    
    std::cout << std::endl << "Render done! Spend " << run_time << "s." << std::endl;

    fclose(f);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r8, -0x50(%rbp)
movq %rcx, -0x60(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, -0x80(%rbp)
movq %rdi, %r12
movl 0x4(%rdi), %r15d
imull (%rdi), %r15d
callq 0x2080
movq %rax, -0x58(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x3c5d(%rip), %rbx      # 0x8fd8
leaq 0xea9(%rip), %rsi       # 0x622b
movl $0x12, %edx
movq %rbx, %rdi
callq 0x2110
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq 0x3c2b(%rip), %rdi      # 0x8fd8
callq 0x2030
movq %rax, %rdi
callq 0x2090
movl 0x4(%r12), %r13d
testl %r13d, %r13d
jle 0x5595
movslq %r15d, %rax
imulq $0x51eb851f, %rax, %rcx # imm = 0x51EB851F
movq %rcx, %rax
shrq $0x3f, %rax
sarq $0x24, %rcx
addl %eax, %ecx
movq %rcx, -0x70(%rbp)
movl $0x0, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movl %r13d, %ecx
movq %rcx, -0x68(%rbp)
leal -0x1(%rcx), %r13d
cmpl $0x0, (%r12)
jle 0x558b
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x88(%rbp)
movl $0x0, -0x2c(%rbp)
movq %rdi, %r14
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x35f4
movl 0x8(%r12), %eax
testl %eax, %eax
jle 0x5504
xorps %xmm0, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
xorl %r15d, %r15d
callq 0x3737
movsd %xmm0, -0x98(%rbp)
movl (%r12), %ebx
callq 0x3737
movapd %xmm0, %xmm1
movsd -0x98(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
divsd %xmm2, %xmm0
addsd -0x88(%rbp), %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl 0x4(%r12), %xmm2
divsd %xmm2, %xmm1
leaq -0xe8(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x30be
movupd -0xe8(%rbp), %xmm0
movupd -0xd8(%rbp), %xmm1
movups -0xc8(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r12, %rdi
movq -0x78(%rbp), %rsi
xorl %edx, %edx
callq 0x50ce
movq %r14, %rdi
movq %rax, %rsi
callq 0x3622
movq %rax, -0x48(%rbp)
movl %edx, -0x40(%rbp)
incl %r15d
movl 0x8(%r12), %eax
cmpl %eax, %r15d
jl 0x5451
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd 0xb85(%rip), %xmm0      # 0x6098
divsd %xmm1, %xmm0
movq %r14, %r15
movq %r14, %rdi
callq 0x363e
movq %rax, -0x48(%rbp)
movl %edx, -0x40(%rbp)
movq %r15, %rdi
callq 0x36ca
movq -0x38(%rbp), %rcx
movb %al, (%rcx)
movq %r14, %rdi
callq 0x36dc
movq -0x38(%rbp), %rcx
movb %al, 0x1(%rcx)
movq %r14, %rdi
callq 0x36d2
movq -0x38(%rbp), %rcx
movb %al, 0x2(%rcx)
movl -0x30(%rbp), %eax
incl %eax
movl %eax, -0x30(%rbp)
cltd
idivl -0x70(%rbp)
testl %edx, %edx
jne 0x5571
leaq -0xb8(%rbp), %rdi
callq 0x3704
addq $0x3, -0x38(%rbp)
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpl (%r12), %eax
movq %r14, %rdi
jl 0x5423
cmpl $0x2, -0x68(%rbp)
jge 0x53f6
callq 0x2080
movq %rax, %rbx
movl (%r12), %esi
movl 0x4(%r12), %edx
movq -0x60(%rbp), %r15
movq %r15, %rdi
movq -0x50(%rbp), %rcx
xorl %r8d, %r8d
callq 0x48d4
movq 0x3a18(%rip), %rdi      # 0x8fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq 0x39fd(%rip), %rdi      # 0x8fd8
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq %rax, %r14
leaq 0xc4c(%rip), %rsi       # 0x623e
movl $0x13, %edx
movq %rax, %rdi
callq 0x2110
subq -0x58(%rbp), %rbx
cvtsi2sd %rbx, %xmm0
divsd 0xba8(%rip), %xmm0      # 0x61b8
movq %r14, %rdi
callq 0x2150
movq %rax, %rbx
leaq 0xc30(%rip), %rsi       # 0x6252
movl $0x2, %edx
movq %rax, %rdi
callq 0x2110
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq %r15, %rdi
callq 0x20e0
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x5680
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2100
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x56a0
jmp 0x56a0
jmp 0x56a0
jmp 0x56a0
jmp 0x56a0
jmp 0x56a0
jmp 0x56a0
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x56c5
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2100
movq %rbx, %rdi
callq 0x2190
nop