// File: sjain12[P]CliqueCounting/GraphIO.cpp
// Function: loadGraph_Escape_p(char const*, Escape::Graph&, int, float)
static ErrorCode loadGraph_Escape_p(const char *path, Graph& graph, int undirected,float p)
{
  FILE* f = fopen(path, "r");
  if (!f)
  {
    fprintf(stderr, "could not open file %s\n", path);
    return ecInvalidInput;
  }

  graph.nVertices = 0;
  EdgeIdx iEdge = 0;
  char line[1024];
  while (fgets(line, sizeof(line), f))
  {
    //Ignore comment lines.
    if (line[0] == '#')
      continue;

    if (isBlankLine(line))
      continue;
      
    int64_t i1, i2;
    sscanf(line, "%ld%ld", &i1, &i2);
    // std::default_random_engine generator;
    std::random_device rd{}; // use to seed the rng 
    std::mt19937 rng{rd()}; // rng

    std::bernoulli_distribution distribution(p);
    bool toss = distribution(rng);
    // printf("toss = %d\n", toss);
    
    // printf("Inside if\n");
    if (graph.nVertices == 0)
    {
      graph.nVertices = i1;
      if (undirected)
          graph.nEdges = 2*i2;
      else
          graph.nEdges = i2;
      graph.srcs = new VertexIdx[graph.nEdges];
      graph.dsts = new VertexIdx[graph.nEdges];
    }
    else
    {
      if (toss)
      {
        graph.srcs[iEdge] = i1;
        graph.dsts[iEdge] = i2;
        ++iEdge;
        if (undirected)
        {
            graph.srcs[iEdge] = i2;
            graph.dsts[iEdge] = i1;
            ++iEdge;
        }
      }
    }
  }
  fclose(f);

  // if (iEdge < graph.nEdges)
  // {
  //   fprintf(stderr, "expected %ld edges, only got %ld\n", graph.nEdges, iEdge);
  //   return ecIOError;
  // }
  graph.nEdges = iEdge;
  printf("In loadGraph_Escape_p. numbre of edges = %ld \n", graph.nEdges);
  return ecNone;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b90, %rsp           # imm = 0x2B90
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movss %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x13cd(%rip), %rsi      # 0x19153
callq 0x33c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x17dbe
movq 0xc23b(%rip), %rax      # 0x23fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1879(%rip), %rsi      # 0x19624
movb $0x0, %al
callq 0x3450
movl $0x1, -0x4(%rbp)
jmp 0x18061
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x30(%rbp)
leaq -0x430(%rbp), %rdi
movq -0x28(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x34c0
cmpq $0x0, %rax
je 0x1802f
movsbl -0x430(%rbp), %eax
cmpl $0x23, %eax
jne 0x17dfe
jmp 0x17dd1
leaq -0x430(%rbp), %rdi
callq 0x18080
testb $0x1, %al
jne 0x17e10
jmp 0x17e12
jmp 0x17dd1
leaq 0x1823(%rip), %rsi      # 0x1963c
xorl %eax, %eax
leaq -0x430(%rbp), %rdi
leaq -0x438(%rbp), %rdx
leaq -0x440(%rbp), %rcx
callq 0x32f0
leaq -0x17c8(%rbp), %rdi
movq %rdi, -0x2b78(%rbp)
callq 0x180d0
movq -0x2b78(%rbp), %rdi
callq 0x18170
movl %eax, -0x2b70(%rbp)
jmp 0x17e5c
movl -0x2b70(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x2b50(%rbp), %rdi
callq 0x18190
jmp 0x17e74
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq -0x2b68(%rbp), %rdi
callq 0x181c0
jmp 0x17e8b
leaq -0x2b68(%rbp), %rdi
leaq -0x2b50(%rbp), %rsi
callq 0x181f0
movb %al, -0x2b79(%rbp)
jmp 0x17ea6
movb -0x2b79(%rbp), %al
andb $0x1, %al
movb %al, -0x2b69(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x17f97
movq -0x438(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x1c(%rbp)
je 0x17f0d
movq -0x440(%rbp), %rcx
shlq %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x17f1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b58(%rbp)
movl %eax, -0x2b5c(%rbp)
leaq -0x17c8(%rbp), %rdi
callq 0x18220
jmp 0x1806d
movq -0x440(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x3030
movq %rax, -0x2b88(%rbp)
jmp 0x17f48
movq -0x2b88(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x3030
movq %rax, -0x2b90(%rbp)
jmp 0x17f83
movq -0x2b90(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x1801e
testb $0x1, -0x2b69(%rbp)
je 0x1801c
movq -0x438(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x440(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x1801a
movq -0x440(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x438(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x1801c
jmp 0x1801e
leaq -0x17c8(%rbp), %rdi
callq 0x18220
jmp 0x17dd1
movq -0x28(%rbp), %rdi
callq 0x3310
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x1612(%rip), %rdi      # 0x19665
movb $0x0, %al
callq 0x3050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2b90, %rsp           # imm = 0x2B90
popq %rbp
retq
movq -0x2b58(%rbp), %rdi
callq 0x3580
nopl (%rax)