// File: /workspace/github_cpp_makefile_star2_O3/sjain12[P]CliqueCounting/Graph.cpp
// Function: Escape::makeCSR(Escape::Graph, bool)
CGraph Escape::makeCSR(Graph g, bool inPlace)
{
  //create a temporary graph for sorting unless the user requests in-place
  //operation.
  printf("In makeCSR. number of edges = %ld \n", g.nEdges);
  auto tmpG = inPlace ? g : g.copy();

  auto begin = JSIterator<VertexIdx, VertexIdx>{tmpG.srcs, tmpG.dsts};
  auto end   = begin + tmpG.nEdges;
  std::sort(begin, end);

  //We steal the dsts array from tmpG.
  CGraph ret = {g.nVertices, g.nEdges, new EdgeIdx[g.nVertices + 1], tmpG.dsts};

  //Now we have everything sorted by src, compress:
  VertexIdx cv = 0;
  for (EdgeIdx i = 0; i < tmpG.nEdges; ++i)
  {
    auto src = tmpG.srcs[i];
    while (cv <= src)
      ret.offsets[cv++] = i;
  }
  while (cv <= g.nVertices)
    ret.offsets[cv++] = g.nEdges;

  delete[] tmpG.srcs; //we retain tmpG.dsts in the output

  ret.sortById();

  return ret; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x70(%rsp), %r13
movq 0x8(%r13), %r14
leaq 0x3a93(%rip), %rdi      # 0xc5b1
movq %r14, %rsi
xorl %eax, %eax
callq 0x3050
testl %ebp, %ebp
movq %rbx, 0x8(%rsp)
je 0x8b4b
movdqu (%r13), %xmm0
movdqu 0x10(%r13), %xmm1
movdqa %xmm1, 0x20(%rsp)
movdqa %xmm0, 0x10(%rsp)
jmp 0x8b58
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x851c
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbx
testq %r12, %r12
je 0x8baa
leaq (%r15,%r12,8), %rbp
leaq (%rbx,%r12,8), %r13
bsrq %r12, %r8
xorl $0x3f, %r8d
addl %r8d, %r8d
xorq $0x7e, %r8
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x9c8d
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r13, %rcx
leaq 0x70(%rsp), %r13
callq 0x9e36
movq (%r13), %rcx
movq 0x8(%rsp), %r13
movq %rcx, (%r13)
movq %r14, 0x8(%r13)
leaq 0x1(%rcx), %rax
shrq $0x3d, %rax
movq %rcx, (%rsp)
leaq 0x8(,%rcx,8), %rcx
xorl %ebp, %ebp
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x3030
movq %rax, 0x10(%r13)
movq %rbx, 0x18(%r13)
testq %r12, %r12
jle 0x8cb6
leaq 0x8(%rax), %rcx
xorl %ebp, %ebp
movdqa 0x342d(%rip), %xmm0     # 0xc030
movdqa 0x3955(%rip), %xmm1     # 0xc560
pcmpeqd %xmm2, %xmm2
xorl %edx, %edx
movq (%r15,%rdx,8), %rsi
cmpq %rsi, %rbp
jg 0x8caa
incq %rsi
movq %rsi, %r8
subq %rbp, %r8
leaq 0x1(%r8), %rdi
andq $-0x2, %rdi
decq %r8
movq %r8, %xmm3
pshufd $0x44, %xmm3, %xmm3     # xmm3 = xmm3[0,1,0,1]
leaq (%rcx,%rbp,8), %r8
pxor %xmm1, %xmm3
xorl %r9d, %r9d
movq %r9, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm0, %xmm4
pxor %xmm1, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm3, %xmm5
pcmpeqd %xmm3, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %r10d
notl %r10d
testb $0x1, %r10b
je 0x8c8a
movq %rdx, -0x8(%r8,%r9,8)
pxor %xmm2, %xmm4
pextrw $0x4, %xmm4, %r10d
testb $0x1, %r10b
je 0x8c9e
movq %rdx, (%r8,%r9,8)
addq $0x2, %r9
cmpq %r9, %rdi
jne 0x8c47
movq %rsi, %rbp
incq %rdx
cmpq %r12, %rdx
jne 0x8c11
movq (%rsp), %rdx
cmpq %rdx, %rbp
jg 0x8d57
subq %rbp, %rdx
leaq 0x2(%rdx), %rcx
andq $-0x2, %rcx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%rax,%rbp,8), %rax
addq $0x8, %rax
xorl %edx, %edx
movdqa 0x3346(%rip), %xmm1     # 0xc030
movdqa 0x386e(%rip), %xmm2     # 0xc560
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movq %rdx, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm1, %xmm4
pxor %xmm2, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm0, %xmm5
pcmpeqd %xmm0, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %esi
notl %esi
testb $0x1, %sil
je 0x8d3b
movq %r14, -0x8(%rax,%rdx,8)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %esi
testb $0x1, %sil
je 0x8d4e
movq %r14, (%rax,%rdx,8)
addq $0x2, %rdx
cmpq %rdx, %rcx
jne 0x8cfa
testq %r15, %r15
je 0x8d64
movq %r15, %rdi
callq 0x3370
movq %r13, %rdi
callq 0x8912
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq