// File: pollo[P]graph-partitioning/preprocessing.cpp
// Function: main
int main(int argc, char** argv)
{
  Graph graph;

  if (argc != 2)
  {
    printf("Invalid number of parameters\n");
    printf("Usage: %s <dataset name>\n",
           argv[0]);
    exit(1);
  }

  char output_name[1000];
  if (strncmp(argv[1],"slashdot",8) == 0)
  {
    snap_loader(&graph, SLASHDOT_FILE);
    strcpy(output_name, SLASHDOT_FILE);
  }
  else if (strncmp(argv[1],"amazon",6) == 0)
  {
    snap_loader(&graph, AMAZON_FILE);
    strcpy(output_name, AMAZON_FILE);
  }
  else if (strncmp(argv[1],"4elt",4) == 0)
  {
    elt_loader(&graph);
    strcpy(output_name, ELT_FILE);
  }
  else if (strncmp(argv[1],"twitter",7) == 0)
  {
    twitter_loader(&graph);
    strcpy(output_name, TWITTER_FILE);
  }
  else
  {
    printf("Invalid dataset name %s\n",argv[1]);
    exit(1);
  }
  strcat(output_name, ".preprocessed");

  vector<int> component;
  int largest_component = find_largest_component(component, graph);

  int n,m;
  count_nodes_edges(largest_component, component, graph, &n, &m);;

  printf("Nodes %d, edges %d\n",n,m);

  //assign new id
  vector<int> remap(component.size(), -1);
  int node_id = 0;
  for (size_t i=0; i<component.size(); i++)
  {
    if (component[i] == largest_component)
    {
      remap[i] = node_id;
      node_id ++;
    }
  }

  FILE *fp = fopen(output_name, "w");
  fprintf(fp, "%d %d\n",n,m*2);
  for (size_t i=0; i<component.size(); i++)
  {
    if (component[i] == largest_component)
    {
      for (size_t j=0; j<graph.get_neighbors(i).size(); j++)
      {
        int neighbor = graph.get_neighbors(i)[j];
        fprintf(fp, "%d %d\n",remap[i],remap[neighbor]);
      }
    }
  }

  fclose(fp);
  return 0;
}

// Assembly:
subq $0x518, %rsp            # imm = 0x518
movl $0x0, 0x514(%rsp)
movl %edi, 0x510(%rsp)
movq %rsi, 0x508(%rsp)
leaq 0x4e0(%rsp), %rdi
callq 0x2d60
cmpl $0x2, 0x510(%rsp)
je 0x26c6
leaq 0x7991(%rip), %rdi      # 0xa010
xorl %eax, %eax
callq 0x2030
jmp 0x2688
movq 0x508(%rsp), %rax
movq (%rax), %rsi
leaq 0x7994(%rip), %rdi      # 0xa02e
xorl %eax, %eax
callq 0x2030
jmp 0x26a3
movl $0x1, %edi
callq 0x21b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4d8(%rsp)
movl %eax, 0x4d4(%rsp)
jmp 0x2c16
movq 0x508(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x796f(%rip), %rsi      # 0xa048
movl $0x8, %edx
callq 0x20b0
cmpl $0x0, %eax
jne 0x2717
leaq 0x7962(%rip), %rsi      # 0xa051
leaq 0x4e0(%rsp), %rdi
callq 0x8f20
jmp 0x26fe
leaq 0xe0(%rsp), %rdi
leaq 0x7944(%rip), %rsi      # 0xa051
callq 0x2140
jmp 0x2822
movq 0x508(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x7944(%rip), %rsi      # 0xa06e
movl $0x6, %edx
callq 0x20b0
cmpl $0x0, %eax
jne 0x2768
leaq 0x7935(%rip), %rsi      # 0xa075
leaq 0x4e0(%rsp), %rdi
callq 0x8f20
jmp 0x274f
leaq 0xe0(%rsp), %rdi
leaq 0x7917(%rip), %rsi      # 0xa075
callq 0x2140
jmp 0x2820
movq 0x508(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x7911(%rip), %rsi      # 0xa08c
movl $0x4, %edx
callq 0x20b0
cmpl $0x0, %eax
jne 0x27af
leaq 0x4e0(%rsp), %rdi
callq 0x9170
jmp 0x2799
leaq 0xe0(%rsp), %rdi
leaq 0x78e9(%rip), %rsi      # 0xa091
callq 0x2140
jmp 0x281e
movq 0x508(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x78e6(%rip), %rsi      # 0xa0a8
movl $0x7, %edx
callq 0x20b0
cmpl $0x0, %eax
jne 0x27f6
leaq 0x4e0(%rsp), %rdi
callq 0x93a0
jmp 0x27e0
leaq 0xe0(%rsp), %rdi
leaq 0x78c1(%rip), %rsi      # 0xa0b0
callq 0x2140
jmp 0x281c
movq 0x508(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x78be(%rip), %rdi      # 0xa0c7
xorl %eax, %eax
callq 0x2030
jmp 0x2812
movl $0x1, %edi
callq 0x21b0
jmp 0x281e
jmp 0x2820
jmp 0x2822
leaq 0x78b7(%rip), %rsi      # 0xa0e0
leaq 0xe0(%rsp), %rdi
callq 0x2250
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x2d90
movq 0x60(%rsp), %rdi
leaq 0x4e0(%rsp), %rsi
callq 0x2460
movl %eax, 0x68(%rsp)
jmp 0x2860
movl 0x68(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %edi
leaq 0xc8(%rsp), %rsi
leaq 0x4e0(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xbc(%rsp), %r8
callq 0x2550
jmp 0x2899
movl 0xc0(%rsp), %esi
movl 0xbc(%rsp), %edx
leaq 0x7840(%rip), %rdi      # 0xa0ee
xorl %eax, %eax
callq 0x2030
jmp 0x28b7
leaq 0xc8(%rsp), %rdi
callq 0x2c50
movq %rax, 0x50(%rsp)
movl $0xffffffff, 0x9c(%rsp) # imm = 0xFFFFFFFF
leaq 0x9b(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x2da0
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rcx
leaq 0xa0(%rsp), %rdi
leaq 0x9c(%rsp), %rdx
callq 0x2db0
jmp 0x2907
leaq 0x9b(%rsp), %rdi
callq 0x2e30
movl $0x0, 0x94(%rsp)
movq $0x0, 0x88(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x2c50
movq %rax, %rcx
movq 0x48(%rsp), %rax
cmpq %rcx, %rax
jae 0x2a09
movq 0x88(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x2c30
movl (%rax), %eax
cmpl 0xc4(%rsp), %eax
jne 0x29ee
movl 0x94(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x88(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x2c30
movl 0x44(%rsp), %ecx
movl %ecx, (%rax)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x29ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4d8(%rsp)
movl %eax, 0x4d4(%rsp)
jmp 0x2c09
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4d8(%rsp)
movl %eax, 0x4d4(%rsp)
leaq 0x9b(%rsp), %rdi
callq 0x2e30
jmp 0x2c09
jmp 0x29f0
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
jmp 0x292b
leaq 0x76f2(%rip), %rsi      # 0xa102
leaq 0xe0(%rsp), %rdi
callq 0x21a0
movq %rax, 0x38(%rsp)
jmp 0x2a24
movq 0x38(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
movl 0xc0(%rsp), %edx
movl 0xbc(%rsp), %ecx
shll %ecx
leaq 0x76b4(%rip), %rsi      # 0xa104
movb $0x0, %al
callq 0x21e0
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x2c50
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0x2bb9
movq 0x78(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x2c30
movl (%rax), %eax
cmpl 0xc4(%rsp), %eax
jne 0x2ba4
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x78(%rsp), %esi
leaq 0x4e0(%rsp), %rdi
callq 0x4d00
movq %rax, 0x28(%rsp)
jmp 0x2ad4
movq 0x28(%rsp), %rdi
callq 0x2c50
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x2ba2
movl 0x78(%rsp), %esi
leaq 0x4e0(%rsp), %rdi
callq 0x4d00
movq %rax, 0x18(%rsp)
jmp 0x2b07
movq 0x18(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x2c70
movl (%rax), %eax
movl %eax, 0x6c(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x78(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x2c30
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movslq 0x6c(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x2c30
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %edx
movl (%rax), %ecx
leaq 0x759f(%rip), %rsi      # 0xa104
movb $0x0, %al
callq 0x21e0
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x2ab2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x4d8(%rsp)
movl %eax, 0x4d4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x2e40
jmp 0x2c09
jmp 0x2ba4
jmp 0x2ba6
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x2a60
movq 0x80(%rsp), %rdi
callq 0x2170
jmp 0x2bc8
movl $0x0, 0x514(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x2e40
leaq 0xc8(%rsp), %rdi
callq 0x2e40
leaq 0x4e0(%rsp), %rdi
callq 0x2ea0
movl 0x514(%rsp), %eax
addq $0x518, %rsp            # imm = 0x518
retq
leaq 0xc8(%rsp), %rdi
callq 0x2e40
leaq 0x4e0(%rsp), %rdi
callq 0x2ea0
movq 0x4d8(%rsp), %rdi
callq 0x2240