// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
leaq -0x14(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xf9b0
movl $0x0, -0x18(%rbp)
cmpl $0x12c, -0x18(%rbp)     # imm = 0x12C
jge 0x5af40
cvtsi2sdl -0x4(%rbp), %xmm0
movsd %xmm0, -0xf0(%rbp)
callq 0x7480
movaps %xmm0, %xmm1
movsd -0xf0(%rbp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss 0x243d0(%rip), %xmm1    # 0x7f204
movss %xmm1, -0xe4(%rbp)
divss %xmm1, %xmm0
movss %xmm0, -0x1c(%rbp)
cvtsi2sdl -0x8(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
callq 0x7480
movss -0xe4(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xe0(%rbp), %xmm0
addsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x20(%rbp)
movss -0x1c(%rbp), %xmm0
movss -0x20(%rbp), %xmm1
leaq 0x55ce8(%rip), %rsi     # 0xb0b74
leaq -0x3c(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x73ec0
leaq 0x55c9d(%rip), %rsi     # 0xb0b40
leaq -0x58(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x9f20
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
xorl %edx, %edx
callq 0x5aa80
movss %xmm1, -0xc4(%rbp)
movaps %xmm0, -0xc0(%rbp)
jmp 0x5aed9
movss -0xc4(%rbp), %xmm0
movaps -0xc0(%rbp), %xmm1
movlpd %xmm1, -0x78(%rbp)
movss %xmm0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x14(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x554a0
jmp 0x5af0f
leaq -0x58(%rbp), %rdi
callq 0x8e00
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5adfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8e00
jmp 0x5b15f
leaq -0x14(%rbp), %rdi
movss 0x23620(%rip), %xmm0    # 0x7e56c
callq 0x74270
leaq -0x14(%rbp), %rdi
xorl %esi, %esi
callq 0x1b400
movss (%rax), %xmm0
callq 0x11960
movss %xmm0, -0xf4(%rbp)
leaq -0x14(%rbp), %rdi
movl $0x1, %esi
callq 0x1b400
movss (%rax), %xmm0
callq 0x11960
movss %xmm0, -0xf8(%rbp)
leaq -0x14(%rbp), %rdi
movl $0x2, %esi
callq 0x1b400
movss (%rax), %xmm0
callq 0x11960
movss -0xf8(%rbp), %xmm1
movaps %xmm0, %xmm2
movss -0xf4(%rbp), %xmm0
leaq -0x84(%rbp), %rdi
callq 0xf9b0
movq -0x84(%rbp), %rax
movq %rax, -0x14(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0xc(%rbp)
leaq -0x14(%rbp), %rdi
xorl %esi, %esi
callq 0x1b400
movss 0x2421a(%rip), %xmm0    # 0x7f200
mulss (%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x88(%rbp)
leaq -0x14(%rbp), %rdi
movl $0x1, %esi
callq 0x1b400
movss 0x241f6(%rip), %xmm0    # 0x7f200
mulss (%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x8c(%rbp)
leaq -0x14(%rbp), %rdi
movl $0x2, %esi
callq 0x1b400
movss 0x241d2(%rip), %xmm0    # 0x7f200
mulss (%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl $0xff, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x94(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0x101b0
movl (%rax), %eax
movl %eax, -0x88(%rbp)
movl $0x0, -0x9c(%rbp)
movl $0xff, -0xa0(%rbp)
leaq -0x8c(%rbp), %rdi
leaq -0x9c(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x101b0
movl (%rax), %eax
movl %eax, -0x8c(%rbp)
movl $0x0, -0xa4(%rbp)
movl $0xff, -0xa8(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
callq 0x101b0
movl (%rax), %eax
movl %eax, -0x90(%rbp)
movl -0x88(%rbp), %eax
movb %al, %dl
imull $0x3, -0x8(%rbp), %eax
imull $0x320, %eax, %eax      # imm = 0x320
imull $0x3, -0x4(%rbp), %ecx
addl %ecx, %eax
addl $0x0, %eax
movslq %eax, %rcx
leaq 0x55add(%rip), %rax     # 0xb0be0
movb %dl, (%rax,%rcx)
movl -0x8c(%rbp), %eax
movb %al, %dl
imull $0x3, -0x8(%rbp), %eax
imull $0x320, %eax, %eax      # imm = 0x320
imull $0x3, -0x4(%rbp), %ecx
addl %ecx, %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x55ab5(%rip), %rax     # 0xb0be0
movb %dl, (%rax,%rcx)
movl -0x90(%rbp), %eax
movb %al, %dl
imull $0x3, -0x8(%rbp), %eax
imull $0x320, %eax, %eax      # imm = 0x320
imull $0x3, -0x4(%rbp), %ecx
addl %ecx, %eax
addl $0x2, %eax
movslq %eax, %rcx
leaq 0x55a8d(%rip), %rax     # 0xb0be0
movb %dl, (%rax,%rcx)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x7860
nopl (%rax,%rax)