// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movl %esi, %ebp
movl %edi, %ebx
cvtsi2sd %edi, %xmm0
movsd %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x58(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movl $0x12c, %r12d           # imm = 0x12C
leaq 0x70(%rsp), %r14
leaq 0x30(%rsp), %r15
movq 0x26d5f(%rip), %r13     # 0x4efa0
callq 0x63a0
addsd 0x60(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss 0x151fd(%rip), %xmm0    # 0x3d458
divss %xmm0, %xmm1
movaps %xmm1, 0x40(%rsp)
callq 0x63a0
addsd 0x58(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
divss 0x151dd(%rip), %xmm0    # 0x3d458
movaps %xmm0, 0x10(%rsp)
movss 0x272ec(%rip), %xmm0    # 0x4f574
movss %xmm0, 0xc(%rsp)
callq 0x149b5
movss 0xc(%rsp), %xmm1
movaps %xmm1, %xmm2
mulss %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss %xmm1, %xmm0
movss 0x272b4(%rip), %xmm1    # 0x4f564
mulss %xmm2, %xmm1
movss 0x272b4(%rip), %xmm3    # 0x4f570
mulss %xmm0, %xmm3
cvtss2sd 0x272b0(%rip), %xmm4    # 0x4f578
movsd %xmm4, 0x68(%rsp)
addss %xmm1, %xmm3
movss %xmm3, 0xc(%rsp)
movsd 0x2727c(%rip), %xmm1    # 0x4f55c
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
mulps %xmm1, %xmm2
movsd 0x27279(%rip), %xmm1    # 0x4f568
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm1, %xmm0
addps %xmm2, %xmm0
movaps %xmm0, 0x90(%rsp)
callq 0x63a0
movss 0x2726e(%rip), %xmm1    # 0x4f57c
subss 0x27262(%rip), %xmm1    # 0x4f578
cvtss2sd %xmm1, %xmm1
mulsd %xmm0, %xmm1
addsd 0x68(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movsd 0x27211(%rip), %xmm3    # 0x4f544
movaps 0x90(%rsp), %xmm11
movaps %xmm11, %xmm1
addps %xmm3, %xmm1
movss 0x27201(%rip), %xmm4    # 0x4f54c
movss 0xc(%rsp), %xmm10
movaps %xmm10, %xmm2
addss %xmm4, %xmm2
movsd 0x271ca(%rip), %xmm5    # 0x4f52c
movaps 0x40(%rsp), %xmm7
movaps %xmm7, %xmm6
shufps $0x0, %xmm7, %xmm6      # xmm6 = xmm6[0,0],xmm7[0,0]
mulps %xmm5, %xmm6
mulss 0x271bb(%rip), %xmm7    # 0x4f534
movsd 0x2719f(%rip), %xmm5    # 0x4f520
addps %xmm6, %xmm5
addss 0x2719c(%rip), %xmm7    # 0x4f528
movaps %xmm7, %xmm9
movsd 0x271a0(%rip), %xmm6    # 0x4f538
movaps 0x10(%rsp), %xmm8
movaps %xmm8, %xmm7
unpcklps %xmm8, %xmm7            # xmm7 = xmm7[0],xmm8[0],xmm7[1],xmm8[1]
mulps %xmm6, %xmm7
mulss 0x2718e(%rip), %xmm8    # 0x4f540
addps %xmm5, %xmm7
addss %xmm9, %xmm8
subps %xmm3, %xmm7
subss %xmm4, %xmm8
subps %xmm11, %xmm7
subss %xmm10, %xmm8
movlps %xmm1, 0x70(%rsp)
movss %xmm2, 0x78(%rsp)
movlps %xmm7, 0x7c(%rsp)
movss %xmm8, 0x84(%rsp)
movss %xmm0, 0x88(%rsp)
movq 0x270eb(%rip), %rax     # 0x4f4e0
movq %rax, 0x30(%rsp)
movq 0x270e7(%rip), %rax     # 0x4f4e8
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x2841b
cmpb $0x0, (%r13)
je 0x28417
incl 0x8(%rax)
jmp 0x2841b
lock
incl 0x8(%rax)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x28061
movaps %xmm0, %xmm2
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x28450
movss %xmm1, 0x10(%rsp)
movaps %xmm2, 0x40(%rsp)
callq 0x7774
movaps 0x40(%rsp), %xmm2
movss 0x10(%rsp), %xmm1
movaps 0x20(%rsp), %xmm0
addps %xmm2, %xmm0
movaps %xmm0, 0x20(%rsp)
movss 0x8(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
decl %r12d
jne 0x28241
movss 0x14fde(%rip), %xmm2    # 0x3d45c
movaps 0x20(%rsp), %xmm1
movaps %xmm1, %xmm0
mulss %xmm2, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
mulss %xmm2, %xmm1
movaps %xmm1, 0x20(%rsp)
xorps %xmm3, %xmm3
ucomiss %xmm3, %xmm0
jb 0x284a8
xorps %xmm1, %xmm1
sqrtss %xmm0, %xmm1
jmp 0x284bb
callq 0x6710
xorps %xmm3, %xmm3
movss 0x14fa4(%rip), %xmm2    # 0x3d45c
movaps %xmm0, %xmm1
movss 0x8(%rsp), %xmm0
mulss %xmm2, %xmm0
movss %xmm0, 0x8(%rsp)
movaps 0x20(%rsp), %xmm0
ucomiss %xmm3, %xmm0
movss %xmm1, 0x10(%rsp)
jb 0x284e4
xorps %xmm3, %xmm3
sqrtss %xmm0, %xmm3
jmp 0x284ec
callq 0x6710
movaps %xmm0, %xmm3
xorps %xmm1, %xmm1
movss 0x8(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x28500
sqrtss %xmm0, %xmm0
jmp 0x28511
movss %xmm3, 0x8(%rsp)
callq 0x6710
movss 0x8(%rsp), %xmm3
movss 0x10(%rsp), %xmm2
movss 0x14f41(%rip), %xmm1    # 0x3d460
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 0x2700a(%rip), %rdi     # 0x4f580
movb %al, (%rdi,%rsi)
movb %cl, 0x1(%rdi,%rsi)
movb %dl, 0x2(%rdi,%rsi)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x285a5
callq 0x7774
movq %rbx, %rdi
callq 0x66e0