// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x98(%rbp)
movb %sil, %cl
movq %rdi, -0x90(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
andb $0x1, %cl
movb %cl, -0x1(%rbp)
movq 0x8(%rax), %rsi
leaq 0xb481(%rip), %rdi      # 0x195e9
movb $0x0, %al
callq 0x3050
testb $0x1, -0x1(%rbp)
je 0xe19d
movq -0x88(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x18(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x10(%rbp)
jmp 0xe1ad
movq -0x88(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xd6c0
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xe9b0
movq %rax, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0xe9f0
movq -0x88(%rbp), %rax
movq -0x98(%rbp), %rcx
movq (%rax), %rdx
movq %rdx, (%rcx)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rcx)
movq (%rax), %rax
addq $0x1, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0xe2c9
movq -0x18(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0xe2b9
movq -0x98(%rbp), %rax
movq -0x78(%rbp), %rdx
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x70(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0xe28b
jmp 0xe2bb
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0xe271
jmp 0xe2cb
movq -0x88(%rbp), %rcx
movq -0x70(%rbp), %rax
cmpq (%rcx), %rax
jg 0xe306
movq -0x98(%rbp), %rax
movq -0x88(%rbp), %rcx
movq 0x8(%rcx), %rdx
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x70(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0xe2cb
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0xe323
movq -0xa0(%rbp), %rdi
callq 0x3400
movq -0x98(%rbp), %rdi
callq 0xde10
movq -0x90(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nop