// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, %rbx
movl %esi, %r12d
movq %rdi, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movslq %esi, %rsi
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4f82
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x2b10
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x4158
movq 0x10(%rsp), %rbp
cmpq 0x18(%rsp), %rbp
je 0x49a5
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x60(%rsp)
movl %r12d, %r14d
leaq (,%r14,8), %rax
movq %rax, 0x50(%rsp)
movl %r12d, 0x2c(%rsp)
movq %r13, 0x58(%rsp)
testl %r12d, %r12d
jle 0x4838
movq 0x38(%rsp), %rdi
xorl %esi, %esi
movq 0x50(%rsp), %rdx
callq 0x2100
movl (%rbp), %esi
movq %r13, %rdi
callq 0x2af0
movq (%rax), %r15
movl (%rbp), %esi
movq %r13, %rdi
callq 0x2af0
cmpq 0x8(%rax), %r15
je 0x4885
movl (%r15), %esi
movq %rbx, %rdi
callq 0x41b4
testl %eax, %eax
js 0x487f
movl %eax, %eax
movq 0x38(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd 0x1b0e(%rip), %xmm0     # 0x6388
movsd %xmm0, (%rcx,%rax,8)
addq $0x4, %r15
jmp 0x4846
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
testl %r12d, %r12d
jle 0x4982
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r15
movq %rbx, %rdi
movl %r13d, %esi
callq 0x41c0
subq %r12, %r15
sarq $0x2, %r15
movq %r15, %xmm0
punpckldq 0x1ade(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1ae6(%rip), %xmm0     # 0x63b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd 0x60(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
movsd 0x1a99(%rip), %xmm1     # 0x6388
subsd %xmm0, %xmm1
movq 0x38(%rsp), %rax
mulsd (%rax,%r13,8), %xmm1
movsd %xmm1, (%rax,%r13,8)
incq %r13
cmpq %r13, %r14
jne 0x4899
movl 0x2c(%rsp), %r12d
testl %r12d, %r12d
jle 0x4982
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
movq 0x38(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x494b
movl %r13d, 0x8(%rsp)
movsd %xmm0, 0x30(%rsp)
jmp 0x497a
jne 0x497a
jp 0x497a
cmpl $-0x1, 0x8(%rsp)
je 0x4975
movq %rbx, %rdi
movl %r13d, %esi
callq 0x41c0
movl %eax, %r15d
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x41c0
cmpl %eax, %r15d
jg 0x497a
movl %r13d, 0x8(%rsp)
incq %r13
cmpq %r13, %r14
jne 0x492b
movl (%rbp), %esi
movq %rbx, %rdi
movl 0x8(%rsp), %edx
callq 0x419e
addq $0x4, %rbp
cmpq 0x18(%rsp), %rbp
movq 0x58(%rsp), %r13
jne 0x4822
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x49bc
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x49d3
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x4a0d
jmp 0x49f3
jmp 0x49f3
jmp 0x49f3
jmp 0x49f3
jmp 0x49f3
jmp 0x49f3
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x4a0d
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4a24
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2280