// File: /workspace/github_cpp_makefile_star2_O2/pollo[P]graph-partitioning/partitioners.cpp
// Function: fennel(Graph const&, int, Partition*)
void fennel(const Graph& graph,
            int partitions_number,
            Partition* partition)
{
  const double gamma = 3/2.0;
  const double alfa = graph.get_edges_number() * pow(partitions_number, gamma-1.0) /
    pow(graph.get_nodes_number(), gamma);

  //printf("gamma=%f alfa=%f\n",gamma,alfa);

  const bool with_threshold = true;
  const double v = 1.1;

  /*
    if (with_threshold)
    printf("With threshold set to %f\n",v);
  else
  printf("Without threshold\n");*/

  vector<int> nodes;
  vector<double> partition_score(partitions_number);

  //get random sorted nodes
  graph.get_nodes_randomly(&nodes);

  //initialize partitions
  partition->set_size(nodes.size(), partitions_number);

  int count = 0;

  for (vector<int>::iterator node = nodes.begin();
       node != nodes.end();
       ++node) {

    //initialize partitions score
    for (int i=0; i<partitions_number; i++)
      partition_score[i] = 0;

    int nn = 0;
    //add to partitions score number of neighbors in partition
    for (vector<int>::const_iterator neighbor =
           graph.get_neighbors(*node).begin();
         neighbor != graph.get_neighbors(*node).end();
         ++neighbor)
    {
      int neighbor_partition = partition->get_node_partition(*neighbor);
      if (neighbor_partition >= 0) {
        partition_score[neighbor_partition]++;
        //nn++;
      }
    }

    count ++;
    if (nn>0)
      printf("iteration %d neigh %d\n",count,nn);

    //subtract to partitions score cost function
    for (int i=0; i<partitions_number; i++)
    {
      int partition_size = partition->get_partition_size(i);
      double c = alfa * (pow(partition_size+1, gamma) -
                          pow(partition_size, gamma));
      //double c = alfa * gamma * pow(partition_size,gamma-1);
    if (nn>0)
    {
      printf("partition %d size %d\n",i,partition_size);
      printf("partition %d score %f\n",i,partition_score[i]);
    }
    partition_score[i] -= c;
    if (nn>0)
      printf("partition %d score %f\n",i,partition_score[i]);
    }
    //printf("\n\n");

    //filter out partition above treshold
    vector<pair<double,int> > filtered_partition_score;
    for (int i=0; i<partitions_number; i++)
    {
      //printf("Partition %d, score %f\n",i,partition_score[i]);
      if (partition->get_partition_size(i) <=
          v * graph.get_nodes_number() / ((double)partitions_number) ||
          !with_threshold)
      {
        filtered_partition_score.push_back(pair<double, int>(partition_score[i],
                                                             i));
      }
    }

    //select best partition
    int selected_partition = filtered_partition_score[0].second;
    double score = filtered_partition_score[0].first;
    for (size_t i=1; i<filtered_partition_score.size(); i++)
    {
      if (filtered_partition_score[i].first > score )
      {
        selected_partition = filtered_partition_score[i].second;
        score = filtered_partition_score[i].first;
      }
    }

    if (nn>0)
    printf("Selected partition %d\n",selected_partition);

    //assign node to partition
    partition->set_node_partition(*node, selected_partition);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %esi, %ebx
movq %rdi, %r14
cvtsi2sdl (%rdi), %xmm0
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %esi, %xmm0
movq %rdx, %r15
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
jb 0x4599
sqrtsd %xmm0, %xmm0
jmp 0x459e
callq 0x2150
movapd %xmm0, 0x50(%rsp)
cvtsi2sdl 0x8(%r14), %xmm0
movsd 0xdae(%rip), %xmm1      # 0x5360
callq 0x2270
movsd %xmm0, 0x78(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movslq %ebx, %rsi
leaq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x4840
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x2a1c
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
shrq $0x2, %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0x3f38
movapd 0x50(%rsp), %xmm0
andpd 0xd7e(%rip), %xmm0      # 0x5390
mulsd 0x8(%rsp), %xmm0
divsd 0x78(%rsp), %xmm0
movapd %xmm0, 0x50(%rsp)
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
movq 0x10(%rsp), %r12
cmpq 0x18(%rsp), %r12
je 0x47da
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x4651
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x4642
movl (%r12), %esi
movq %r14, %rdi
callq 0x2a00
movq (%rax), %r13
movl (%r12), %esi
movq %r14, %rdi
callq 0x2a00
cmpq 0x8(%rax), %r13
je 0x46a1
movl (%r13), %esi
movq %r15, %rdi
callq 0x3f8e
testl %eax, %eax
js 0x469b
movl %eax, %eax
movq 0x60(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd 0xcc2(%rip), %xmm0      # 0x5358
movsd %xmm0, (%rcx,%rax,8)
addq $0x4, %r13
jmp 0x4660
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x470a
movq %r15, %rdi
movl %ebp, %esi
callq 0x3f9a
movl %eax, %r13d
leal 0x1(%r13), %eax
cvtsi2sd %eax, %xmm0
movsd 0xc9b(%rip), %xmm1      # 0x5360
callq 0x2270
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %r13d, %xmm0
movsd 0xc83(%rip), %xmm1      # 0x5360
callq 0x2270
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x50(%rsp), %xmm1
movq 0x60(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
jmp 0x46a3
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x477d
movq %r15, %rdi
movl %ebp, %esi
callq 0x3f9a
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x8(%r14), %xmm1
mulsd 0xc2b(%rip), %xmm1      # 0x5368
divsd 0x80(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x4778
movq 0x60(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x88(%rsp)
movl %ebp, 0x90(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x4a72
incq %rbp
jmp 0x471c
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movl 0x8(%rax), %edx
movsd (%rax), %xmm0
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x18, %rax
pushq $0x1
popq %rsi
cmpq %rcx, %rsi
jae 0x47bb
movsd -0x8(%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x47b2
movl (%rax), %edx
movapd %xmm1, %xmm0
incq %rsi
addq $0x10, %rax
jmp 0x479c
movl (%r12), %esi
movq %r15, %rdi
callq 0x3f78
leaq 0x30(%rsp), %rdi
callq 0x4a3e
addq $0x4, %r12
jmp 0x4630
leaq 0x60(%rsp), %rdi
callq 0x4910
leaq 0x10(%rsp), %rdi
callq 0x27d4
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x482d
jmp 0x4820
jmp 0x480d
jmp 0x4820
jmp 0x480d
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4a3e
jmp 0x4823
jmp 0x4820
jmp 0x4820
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4910
leaq 0x10(%rsp), %rdi
callq 0x27d4
movq %rbx, %rdi
callq 0x2280
nop