// File: /workspace/github_cpp_makefile_star2_O2/pollo[P]graph-partitioning/partitioners.cpp
// Function: linear_deterministic_greedy(Graph const&, int, Partition*)
void linear_deterministic_greedy(const Graph& graph,
                                 int partitions_number,
                                 Partition* partition)
{
  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);

  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;

    //compute partitions score
    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]++;
      }
    }

    //weight partitions score
    for (int i=0; i<partitions_number; i++)
    {
      double C = ((double)nodes.size())/partitions_number;
      partition_score[i] *= 1 - partition->get_partition_size(i)/C;
    }

    //select best partition
    int selected_partition = -1;
    double score = 0;
    for (int i=0; i<partitions_number; i++)
    {
      if (partition_score[i] > score)
      {
        selected_partition = i;
        score = partition_score[i];
      }
      else if (partition_score[i] == score)
      {
        if (selected_partition == -1 ||
            partition->get_partition_size(i) <=
            partition->get_partition_size(selected_partition))
        {
          selected_partition = i;
        }
      }
    }

    //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 $0x58, %rsp
movq %rdx, %rbx
movl %esi, %r14d
movq %rdi, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movslq %esi, %rsi
leaq 0x30(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4840
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x2a1c
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x3f38
movq 0x10(%rsp), %r15
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x50(%rsp)
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
movq %r13, 0x48(%rsp)
cmpq 0x18(%rsp), %r15
je 0x4389
movq 0x30(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x424a
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x423b
movl (%r15), %esi
movq %r13, %rdi
callq 0x2a00
movq (%rax), %r12
movl (%r15), %esi
movq %r13, %rdi
callq 0x2a00
cmpq 0x8(%rax), %r12
je 0x4298
movl (%r12), %esi
movq %rbx, %rdi
callq 0x3f8e
testl %eax, %eax
js 0x4292
movl %eax, %eax
movq 0x30(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd 0x10cb(%rip), %xmm0     # 0x5358
movsd %xmm0, (%rcx,%rax,8)
addq $0x4, %r12
jmp 0x4258
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x430d
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r13
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3f9a
subq %rbp, %r13
sarq $0x2, %r13
movq %r13, %xmm0
punpckldq 0x10a7(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x10af(%rip), %xmm0     # 0x5380
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd 0x50(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
movsd 0x1065(%rip), %xmm1     # 0x5358
subsd %xmm0, %xmm1
movq 0x30(%rsp), %rax
mulsd (%rax,%r12,8), %xmm1
movsd %xmm1, (%rax,%r12,8)
incq %r12
jmp 0x429b
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r12d, %r12d
pushq $-0x1
popq %rbp
cmpq %r12, %r14
je 0x436e
movq 0x30(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd 0x28(%rsp), %xmm0
jbe 0x4340
movl %r12d, %ebp
movsd %xmm0, 0x28(%rsp)
jmp 0x4369
jne 0x4369
jp 0x4369
cmpl $-0x1, %ebp
je 0x4366
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3f9a
movl %eax, %r13d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3f9a
cmpl %eax, %r13d
jg 0x4369
movl %r12d, %ebp
incq %r12
jmp 0x431d
movl (%r15), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x3f78
addq $0x4, %r15
movq 0x48(%rsp), %r13
jmp 0x4229
leaq 0x30(%rsp), %rdi
callq 0x4910
leaq 0x10(%rsp), %rdi
callq 0x27d4
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x43ca
jmp 0x43bd
jmp 0x43bd
jmp 0x43bd
jmp 0x43bd
jmp 0x43bd
jmp 0x43bd
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4910
leaq 0x10(%rsp), %rdi
callq 0x27d4
movq %rbx, %rdi
callq 0x2280