// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, %r14
cvtsi2sdl (%rdi), %xmm0
movsd %xmm0, 0x50(%rsp)
movl %esi, 0xc(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movq %rdx, %r15
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
jb 0x4c29
sqrtsd %xmm0, %xmm0
jmp 0x4c2e
callq 0x2140
movapd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%r14), %xmm0
movsd 0x174b(%rip), %xmm1     # 0x6390
callq 0x2270
movsd %xmm0, 0x48(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movslq 0xc(%rsp), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x4f82
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x2b1a
movq 0x38(%rsp), %rsi
subq 0x30(%rsp), %rsi
shrq $0x2, %rsi
movq %r15, %rdi
movl 0xc(%rsp), %edx
callq 0x4150
movq 0x30(%rsp), %rbp
cmpq 0x38(%rsp), %rbp
je 0x4edb
movapd 0x70(%rsp), %xmm0
andpd 0x1704(%rip), %xmm0     # 0x63c0
mulsd 0x50(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movapd %xmm0, 0x70(%rsp)
movl 0xc(%rsp), %ebx
leaq (,%rbx,8), %rax
movq %rax, 0x48(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x4cf7
movq 0x58(%rsp), %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
callq 0x2100
movl (%rbp), %esi
movq %r14, %rdi
callq 0x2afa
movq (%rax), %r12
movl (%rbp), %esi
movq %r14, %rdi
callq 0x2afa
cmpq 0x8(%rax), %r12
je 0x4d45
movl (%r12), %esi
movq %r15, %rdi
callq 0x41ac
testl %eax, %eax
js 0x4d3f
movl %eax, %eax
movq 0x58(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd 0x164e(%rip), %xmm0     # 0x6388
movsd %xmm0, (%rcx,%rax,8)
addq $0x4, %r12
jmp 0x4d05
cmpl $0x0, 0xc(%rsp)
jle 0x4dbd
xorl %r12d, %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0x41b8
movl %eax, %r13d
leal 0x1(%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x1620(%rip), %xmm1     # 0x6390
callq 0x2270
movsd %xmm0, 0x50(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd 0x1605(%rip), %xmm1     # 0x6390
callq 0x2270
movsd 0x50(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x70(%rsp), %xmm1
movq 0x58(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%r12,8)
incq %r12
cmpq %r12, %rbx
jne 0x4d4f
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x4e6b
xorl %r12d, %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0x41b8
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x8(%r14), %xmm1
mulsd 0x1597(%rip), %xmm1     # 0x6398
divsd 0x80(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x4e5f
movq 0x58(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
movsd %xmm0, 0x88(%rsp)
movl %r12d, 0x90(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4e4d
movupd 0x88(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x4e5f
leaq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x50b2
incq %r12
cmpq %r12, %rbx
jne 0x4dde
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movl 0x8(%rax), %edx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq $0x2, %rcx
jb 0x4eaa
movsd (%rax), %xmm0
decq %rcx
addq $0x18, %rax
movsd -0x8(%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4ea1
movl (%rax), %edx
movapd %xmm1, %xmm0
addq $0x10, %rax
decq %rcx
jne 0x4e90
movl (%rbp), %esi
movq %r15, %rdi
callq 0x4196
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4ecc
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x4, %rbp
cmpq 0x38(%rsp), %rbp
jne 0x4cdf
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4ef2
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4f09
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x4f62
jmp 0x4f48
jmp 0x4f28
jmp 0x4f48
jmp 0x4f28
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4f4b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
jmp 0x4f4b
jmp 0x4f48
jmp 0x4f48
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4f62
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4f79
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2280
nop