// File: /workspace/github_cpp_makefile_star2_O3/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 0x3c8d(%rip), %rbx      # 0x8fd8
leaq 0xed6(%rip), %rsi       # 0x6228
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 0x3c5b(%rip), %rdi      # 0x8fd8
callq 0x2030
movq %rax, %rdi
callq 0x2090
movl 0x4(%r12), %r14d
testl %r14d, %r14d
jle 0x5562
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 (%r12), %eax
movl $0x0, -0x30(%rbp)
leaq -0x48(%rbp), %r13
movq -0x50(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movl %r14d, %edx
movq %rdx, -0x68(%rbp)
leal -0x1(%rdx), %r14d
testl %eax, %eax
jle 0x5558
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, -0x88(%rbp)
movl $0x0, -0x2c(%rbp)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x35c8
movl 0x8(%r12), %eax
testl %eax, %eax
jle 0x54d5
xorps %xmm0, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
xorl %r15d, %r15d
callq 0x370b
movsd %xmm0, -0x98(%rbp)
movl (%r12), %ebx
callq 0x370b
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 0x3092
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 0x509e
movq %r13, %rdi
movq %rax, %rsi
callq 0x35f6
movq %rax, -0x48(%rbp)
movl %edx, -0x40(%rbp)
incl %r15d
movl 0x8(%r12), %eax
cmpl %eax, %r15d
jl 0x5422
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd 0xbb4(%rip), %xmm0      # 0x6098
divsd %xmm1, %xmm0
movq %r13, %rdi
callq 0x3612
movq %rax, -0x48(%rbp)
movl %edx, -0x40(%rbp)
movq %r13, %rdi
callq 0x369e
movq -0x38(%rbp), %rcx
movb %al, (%rcx)
movq %r13, %rdi
callq 0x36b0
movq -0x38(%rbp), %rcx
movb %al, 0x1(%rcx)
movq %r13, %rdi
callq 0x36a6
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 0x553f
leaq -0xb8(%rbp), %rdi
callq 0x36d8
addq $0x3, -0x38(%rbp)
movl -0x2c(%rbp), %ecx
incl %ecx
movl (%r12), %eax
movl %ecx, -0x2c(%rbp)
cmpl %eax, %ecx
jl 0x53f4
cmpl $0x2, -0x68(%rbp)
jge 0x53ca
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 0x48a8
movq 0x3a4b(%rip), %rdi      # 0x8fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq 0x3a30(%rip), %rdi      # 0x8fd8
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq %rax, %r14
leaq 0xc7c(%rip), %rsi       # 0x623b
movl $0x13, %edx
movq %rax, %rdi
callq 0x2110
subq -0x58(%rbp), %rbx
cvtsi2sd %rbx, %xmm0
divsd 0xbdb(%rip), %xmm0      # 0x61b8
movq %r14, %rdi
callq 0x2150
movq %rax, %rbx
leaq 0xc60(%rip), %rsi       # 0x624f
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 0x564d
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 0x566d
jmp 0x566d
jmp 0x566d
jmp 0x566d
jmp 0x566d
jmp 0x566d
jmp 0x566d
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x5692
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2100
movq %rbx, %rdi
callq 0x2190