// File: /workspace/github_cpp_makefile_star2_O2/alan0201tw[P]RayTracer/src/main.cpp
// Function: thread_entry(int, int)
void thread_entry(int i, int j)
{
    vec3 accumulated_col(0, 0, 0);

    for(int s = 0; s < msaa_sample_count; s++)
    {
        float u = float(i + drand48()) / float(image_width);
        float v = float(j + drand48()) / float(image_height);

        ray r = cam.get_ray(u, v);
        accumulated_col += get_color(r, world, 0);
    }
    accumulated_col /= (float)msaa_sample_count;

    // accumulated_col[0] = std::clamp(accumulated_col[0], 0.0f, 1.0f);
    // accumulated_col[1] = std::clamp(accumulated_col[1], 0.0f, 1.0f);
    // accumulated_col[2] = std::clamp(accumulated_col[2], 0.0f, 1.0f);

    accumulated_col = vec3(std::sqrt(accumulated_col[0]), std::sqrt(accumulated_col[1]), std::sqrt(accumulated_col[2]));
    int r255 = int(255.99f * accumulated_col[0]);
    int g255 = int(255.99f * accumulated_col[1]);
    int b255 = int(255.99f * accumulated_col[2]);

    r255 = std::clamp(r255, 0, 255);
    g255 = std::clamp(g255, 0, 255);
    b255 = std::clamp(b255, 0, 255);

    //std::cout << r01 << " " << g01 << " " << b01 << "\n";
    //  
    //  r[j][i]
    //  
    //  r01 g01 b01, r02 g02 b02, r03 g03 b03 .... , r0<width> b0<width> g0<width>,
    //  r11 g11 b11 ( = rgb[ width + 1 ] )
    //
    image[j * 3 * image_width + i * 3 + 0] = r255;
    image[j * 3 * image_width + i * 3 + 1] = g255;
    image[j * 3 * image_width + i * 3 + 2] = b255;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edi, 0x10(%rsp)
cvtsi2sd %edi, %xmm0
movsd %xmm0, 0x20(%rsp)
movl %esi, 0xc(%rsp)
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x18(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movl $0x12c, %ebp            # imm = 0x12C
leaq 0x30(%rsp), %r14
leaq 0x24b7c(%rip), %r15     # 0x4856c
leaq 0x3c(%rsp), %r12
leaq 0x24b34(%rip), %r13     # 0x48530
leaq 0x28(%rsp), %rbx
movss %xmm0, (%rsp)
movss %xmm0, 0x4(%rsp)
subl $0x1, %ebp
jb 0x23ab5
callq 0x63e0
addsd 0x20(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm1
movss 0xd9e4(%rip), %xmm0     # 0x31410
divss %xmm0, %xmm1
movss %xmm1, 0x14(%rsp)
callq 0x63e0
addsd 0x18(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm1
divss 0xd9c3(%rip), %xmm1     # 0x31410
movq %r12, %rdi
movq %r15, %rsi
movss 0x14(%rsp), %xmm0
callq 0x2d9a0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7666
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2385a
movss 0x8(%rsp), %xmm2
addss %xmm0, %xmm2
movss %xmm2, 0x8(%rsp)
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movss (%rsp), %xmm2
addss %xmm0, %xmm2
movss %xmm2, (%rsp)
movss 0x4(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movq %r14, %rdi
callq 0x7426
jmp 0x23a0c
movss 0xd957(%rip), %xmm3     # 0x31414
movss 0x8(%rsp), %xmm1
mulss %xmm3, %xmm1
movss (%rsp), %xmm0
mulss %xmm3, %xmm0
xorps %xmm4, %xmm4
ucomiss %xmm4, %xmm1
jb 0x23ade
sqrtss %xmm1, %xmm2
jmp 0x23afe
movss %xmm0, (%rsp)
movaps %xmm1, %xmm0
callq 0x6750
xorps %xmm4, %xmm4
movss 0xd91e(%rip), %xmm3     # 0x31414
movaps %xmm0, %xmm2
movss (%rsp), %xmm0
movl 0x10(%rsp), %ebx
movl 0xc(%rsp), %ebp
movss 0x4(%rsp), %xmm1
mulss %xmm3, %xmm1
ucomiss %xmm4, %xmm0
movss %xmm2, (%rsp)
jb 0x23b20
sqrtss %xmm0, %xmm3
jmp 0x23b34
movss %xmm1, 0x4(%rsp)
callq 0x6750
movss 0x4(%rsp), %xmm1
movaps %xmm0, %xmm3
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jb 0x23b42
sqrtss %xmm1, %xmm0
jmp 0x23b56
movaps %xmm1, %xmm0
movss %xmm3, 0x4(%rsp)
callq 0x6750
movss 0x4(%rsp), %xmm3
movss (%rsp), %xmm2
movss 0xd8b5(%rip), %xmm1     # 0x31418
mulss %xmm1, %xmm2
cvttss2si %xmm2, %eax
mulss %xmm1, %xmm3
cvttss2si %xmm3, %ecx
mulss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movl $0xff, %edx
cmpl %edx, %eax
cmovgel %edx, %eax
xorl %edi, %edi
testl %eax, %eax
cmovlel %edi, %eax
cmpl %edx, %ecx
cmovgel %edx, %ecx
testl %ecx, %ecx
cmovlel %edi, %ecx
cmpl %edx, %esi
cmovll %esi, %edx
testl %edx, %edx
cmovlel %edi, %edx
imull $0x960, %ebp, %esi      # imm = 0x960
movslq %ebx, %rdi
leaq (%rdi,%rdi,2), %rdi
movslq %esi, %rsi
addq %rdi, %rsi
leaq 0x24a16(%rip), %rdi     # 0x485d0
movb %al, (%rdi,%rsi)
movb %cl, 0x1(%rdi,%rsi)
movb %dl, 0x2(%rdi,%rsi)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r14, %rdi
callq 0x7426
movq %rbx, %rdi
callq 0x6710