// File: /workspace/github_c_makefile_star2_O0/archer1357[P]Offline-Raytracer/utils.c
// Function: renderSceneGBuffer
void renderSceneGBuffer(int updateFrom,int updateTo,
                        int width, int height,
                        float aspect,float yFov,
                        const float *minBound,const float *maxBound,
                        const float *origin,
                        const float *rotMat,
                        struct Ray *rayStack,
                        struct KdTraverseNode *traverseStack,
                        struct KdNode *rootNode,
                        struct Triangle *triangles,int trianglesNum,
                        struct Sphere *spheres,int spheresNum,
                        sobol_state_t *qrnd,
                        struct GBufferPixel *canvas) {

  // float mat[16]={viewRot[0],viewRot[1],viewRot[2],viewPos[0],
  //                    viewRot[3],viewRot[4],viewRot[5],viewPos[1],
  //                    viewRot[6],viewRot[7],viewRot[8],viewPos[2],
  //                    0.0f,      0.0f,      0.0f,      1.0f};

  float viewMat[16]={rotMat[0],rotMat[3],rotMat[6],-origin[0],
                     rotMat[1],rotMat[4],rotMat[7],-origin[1],
                     rotMat[2],rotMat[5],rotMat[8],-origin[2],
                     0.0f,      0.0f,      0.0f,       1.0f};

  float viewRotMat[9]={rotMat[0],rotMat[3],rotMat[6],
                       rotMat[1],rotMat[4],rotMat[7],
                       rotMat[2],rotMat[5],rotMat[8]};
  float znear=20.0f;
  float zfar=100.0f;

  float d;
  int i;

  //
  d=1.0f/tanf(yFov/2.0f);

  //
  int raycastsNum;


  for(i=updateFrom;i<updateTo;i++) {

    int x,y;
    float u,v,px,py;


    raycastsNum=1;

    rayStack[raycastsNum-1].bounces=0;
    rayStack[raycastsNum-1].origin[0]=origin[0];
    rayStack[raycastsNum-1].origin[1]=origin[1];
    rayStack[raycastsNum-1].origin[2]=origin[2];
    rayStack[raycastsNum-1].ratio=1.0f;

    x=i%width;
    y=i/width;
    u=x/(float)width;
    v=1.0f-y/(float)height; //flip
    px=2.0f*u-1.0f;
    py=2.0f*v-1.0f;

    float bla[3]={px*aspect,py,-d};//rhs


    mat3_mulVector(rayStack[raycastsNum-1].vec,rotMat,bla);
    vec3_normal(rayStack[raycastsNum-1].vec,rayStack[raycastsNum-1].vec);

    //



    //
    while(raycastsNum > 0) {
    float nor[3],colPt[3];


    struct Material *material;

    //
    raycastsNum--;
    struct Ray ray=rayStack[raycastsNum];

    if(!intersectSceneObject(&ray,traverseStack,minBound,maxBound,rootNode,
                             triangles,trianglesNum,spheres,spheresNum,
                             colPt,nor,&material)) {
      canvas[i].col[0]=0.0f;
      canvas[i].col[1]=0.0f;
      canvas[i].col[2]=0.0f;
      canvas[i].nor[0]=0.5f;
      canvas[i].nor[1]=0.5f;
      canvas[i].nor[2]=0.5f;
      canvas[i].depth=1.0f;
      break;
    }



    //
     canvas[i].col[0]=material->col[0];
     canvas[i].col[1]=material->col[1];
     canvas[i].col[2]=material->col[2];


     mat3_mulVector(canvas[i].nor,viewRotMat,nor);


     float viewPos[4];
     float pos[4]={colPt[0],colPt[1],colPt[2],1.0f};
     mat4_mulVector(viewPos,viewMat,pos);



     float p10=(zfar+znear)/(znear-zfar);
     float p11=2.0f*zfar*znear/(znear-zfar);

     // float clipZ=p10*viewPos[2]+p11;

     float mvZ=viewPos[2];
     float clipZ=(mvZ*(zfar+znear)+(2.0f*zfar*znear))/(znear-zfar);
     float clipW=-mvZ;
     float ndcZ = clipZ/clipW;

     // if(ndcZ > 1.0f || ndcZ < -1.0f) {
     //   printf("depth %g\n",ndcZ);
     // }

     // float depth=ndcZ*0.5f+0.5f;
     float nearRange=0.0f;
     float farRange=1.0f;
     float depth = (((farRange-nearRange) * ndcZ) + nearRange + farRange) / 2.0f;
     if(depth>1.0f || depth<0.0f) {
       depth=1.0f;

     }
     // if(depth > 1.0f || depth < 0.0f) {
     //   printf("depth %g\n",depth);
     // }
     canvas[i].depth=depth;

     // float linearDepth=(2.0f*znear)/(zfar+znear-depth*(zfar-znear));
     // canvas[i].depth=linearDepth;
     // if(depth<0.98f) {
     //   printf("less\n");
     // }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movl 0x50(%rbp), %eax
movq 0x48(%rbp), %rax
movl 0x40(%rbp), %eax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x1c(%rbp)
movl %esi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
movss %xmm0, -0x2c(%rbp)
movss %xmm1, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq 0x18(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x80(%rbp)
movq 0x18(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0x7c(%rbp)
movq 0x18(%rbp), %rax
movss 0x18(%rax), %xmm0
movss %xmm0, -0x78(%rbp)
movq 0x10(%rbp), %rax
movss (%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x74(%rbp)
movq 0x18(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0x70(%rbp)
movq 0x18(%rbp), %rax
movss 0x10(%rax), %xmm0
movss %xmm0, -0x6c(%rbp)
movq 0x18(%rbp), %rax
movss 0x1c(%rax), %xmm0
movss %xmm0, -0x68(%rbp)
movq 0x10(%rbp), %rax
movss 0x4(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x64(%rbp)
movq 0x18(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0x60(%rbp)
movq 0x18(%rbp), %rax
movss 0x14(%rax), %xmm0
movss %xmm0, -0x5c(%rbp)
movq 0x18(%rbp), %rax
movss 0x20(%rax), %xmm0
movss %xmm0, -0x58(%rbp)
movq 0x10(%rbp), %rax
movss 0x8(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x54(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x4c(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x48(%rbp)
movss 0xbf60f(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x44(%rbp)
movq 0x18(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0xb0(%rbp)
movq 0x18(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0xac(%rbp)
movq 0x18(%rbp), %rax
movss 0x18(%rax), %xmm0
movss %xmm0, -0xa8(%rbp)
movq 0x18(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0xa4(%rbp)
movq 0x18(%rbp), %rax
movss 0x10(%rax), %xmm0
movss %xmm0, -0xa0(%rbp)
movq 0x18(%rbp), %rax
movss 0x1c(%rax), %xmm0
movss %xmm0, -0x9c(%rbp)
movq 0x18(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0x98(%rbp)
movq 0x18(%rbp), %rax
movss 0x14(%rax), %xmm0
movss %xmm0, -0x94(%rbp)
movq 0x18(%rbp), %rax
movss 0x20(%rax), %xmm0
movss %xmm0, -0x90(%rbp)
movss 0xc2006(%rip), %xmm0    # 0x4e5f20
movss %xmm0, -0xb4(%rbp)
movss 0xc1ff2(%rip), %xmm0    # 0x4e5f1c
movss %xmm0, -0xb8(%rbp)
movss -0x30(%rbp), %xmm0
movss 0xc09cd(%rip), %xmm1    # 0x4e490c
divss %xmm1, %xmm0
callq 0x426640
movaps %xmm0, %xmm1
movss 0xbf531(%rip), %xmm0    # 0x4e3484
divss %xmm1, %xmm0
movss %xmm0, -0xbc(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x4245a8
movl $0x1, -0xc4(%rbp)
movq 0x20(%rbp), %rax
movl -0xc4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl $0x0, 0x18(%rax)
movq 0x10(%rbp), %rax
movss (%rax), %xmm0
movq 0x20(%rbp), %rax
movl -0xc4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0x10(%rbp), %rax
movss 0x4(%rax), %xmm0
movq 0x20(%rbp), %rax
movl -0xc4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0x10(%rbp), %rax
movss 0x8(%rax), %xmm0
movq 0x20(%rbp), %rax
movl -0xc4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movq 0x20(%rbp), %rax
movl -0xc4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss 0xbf459(%rip), %xmm0    # 0x4e3484
movss %xmm0, 0x1c(%rax)
movl -0xc0(%rbp), %eax
cltd
idivl -0x24(%rbp)
movl %edx, -0xc8(%rbp)
movl -0xc0(%rbp), %eax
cltd
idivl -0x24(%rbp)
movl %eax, -0xcc(%rbp)
cvtsi2ssl -0xc8(%rbp), %xmm0
cvtsi2ssl -0x24(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0xd0(%rbp)
cvtsi2ssl -0xcc(%rbp), %xmm1
cvtsi2ssl -0x28(%rbp), %xmm0
divss %xmm0, %xmm1
movss 0xbf402(%rip), %xmm0    # 0x4e3484
subss %xmm1, %xmm0
movss %xmm0, -0xd4(%rbp)
movss -0xd0(%rbp), %xmm0
addss %xmm0, %xmm0
movss 0xc0b62(%rip), %xmm1    # 0x4e4c04
addss %xmm1, %xmm0
movss %xmm0, -0xd8(%rbp)
movss -0xd4(%rbp), %xmm0
addss %xmm0, %xmm0
movss 0xc0b42(%rip), %xmm1    # 0x4e4c04
addss %xmm1, %xmm0
movss %xmm0, -0xdc(%rbp)
movss -0xd8(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
movss %xmm0, -0xe8(%rbp)
movss -0xdc(%rbp), %xmm0
movss %xmm0, -0xe4(%rbp)
movss -0xbc(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0xe0(%rbp)
movq 0x20(%rbp), %rdi
movl -0xc4(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq %rax, %rdi
addq $0xc, %rdi
movq 0x18(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x40da30
movq 0x20(%rbp), %rdi
movl -0xc4(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq %rax, %rdi
addq $0xc, %rdi
movq 0x20(%rbp), %rsi
movl -0xc4(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq %rax, %rsi
addq $0xc, %rsi
callq 0x40ca00
cmpl $0x0, -0xc4(%rbp)
jle 0x424592
movl -0xc4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc4(%rbp)
movq 0x20(%rbp), %rax
movslq -0xc4(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x128(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x120(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x118(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x110(%rbp)
movq 0x28(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq 0x30(%rbp), %r8
movq 0x38(%rbp), %r9
movl 0x40(%rbp), %r15d
movq 0x48(%rbp), %r14
movl 0x50(%rbp), %ebx
leaq -0x100(%rbp), %r11
leaq -0xf4(%rbp), %r10
leaq -0x128(%rbp), %rdi
leaq -0x108(%rbp), %rax
movl %r15d, (%rsp)
movq %r14, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x422d20
testb $0x1, %al
jne 0x4242fe
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rax)
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0xbf1cd(%rip), %xmm0    # 0x4e3464
movss %xmm0, 0xc(%rax)
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0xbf1ae(%rip), %xmm0    # 0x4e3464
movss %xmm0, 0x10(%rax)
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0xbf18f(%rip), %xmm0    # 0x4e3464
movss %xmm0, 0x14(%rax)
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0xbf190(%rip), %xmm0    # 0x4e3484
movss %xmm0, 0x18(%rax)
jmp 0x424592
movq -0x108(%rbp), %rax
movss (%rax), %xmm0
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq -0x108(%rbp), %rax
movss 0x4(%rax), %xmm0
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq -0x108(%rbp), %rax
movss 0x8(%rax), %xmm0
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movq 0x60(%rbp), %rdi
movslq -0xc0(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0xc, %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xf4(%rbp), %rdx
callq 0x40da30
movss -0x100(%rbp), %xmm0
movss %xmm0, -0x150(%rbp)
movss -0xfc(%rbp), %xmm0
movss %xmm0, -0x14c(%rbp)
movss -0xf8(%rbp), %xmm0
movss %xmm0, -0x148(%rbp)
movss 0xbf0be(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x144(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x150(%rbp), %rdx
callq 0x40cdb0
movss -0xb8(%rbp), %xmm0
addss -0xb4(%rbp), %xmm0
movss -0xb4(%rbp), %xmm1
subss -0xb8(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x154(%rbp)
movss 0xc04f3(%rip), %xmm0    # 0x4e490c
mulss -0xb8(%rbp), %xmm0
mulss -0xb4(%rbp), %xmm0
movss -0xb4(%rbp), %xmm1
subss -0xb8(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x158(%rbp)
movss -0x138(%rbp), %xmm0
movss %xmm0, -0x15c(%rbp)
movss -0x15c(%rbp), %xmm0
movss -0xb8(%rbp), %xmm2
addss -0xb4(%rbp), %xmm2
movss 0xc0497(%rip), %xmm1    # 0x4e490c
mulss -0xb8(%rbp), %xmm1
mulss -0xb4(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss -0xb4(%rbp), %xmm1
subss -0xb8(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x160(%rbp)
movss -0x15c(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x164(%rbp)
movss -0x160(%rbp), %xmm0
divss -0x164(%rbp), %xmm0
movss %xmm0, -0x168(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x16c(%rbp)
movss 0xbef93(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x170(%rbp)
movss -0x170(%rbp), %xmm0
subss -0x16c(%rbp), %xmm0
movss -0x168(%rbp), %xmm2
movss -0x16c(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
addss -0x170(%rbp), %xmm0
movss 0xc03db(%rip), %xmm1    # 0x4e490c
divss %xmm1, %xmm0
movss %xmm0, -0x174(%rbp)
movss -0x174(%rbp), %xmm0
movss 0xbef37(%rip), %xmm1    # 0x4e3484
ucomiss %xmm1, %xmm0
ja 0x42455e
xorps %xmm0, %xmm0
ucomiss -0x174(%rbp), %xmm0
jbe 0x42456e
movss 0xbef1e(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x174(%rbp)
movss -0x174(%rbp), %xmm0
movq 0x60(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x18(%rax)
jmp 0x424173
jmp 0x424594
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x423f68
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)