// File: harry830622[P]floorplanner/src/floorplanner.cpp
// Function: Floorplanner::Floorplanner(Database const&, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool, bool)
Floorplanner::Floorplanner(const Database& database, double alpha,
                           const string& sa_mode, bool is_verbose,
                           bool is_drawing)
    : database_(database),
      alpha_(alpha),
      sa_mode_(sa_mode),
      is_verbose_(is_verbose),
      is_drawing_(is_drawing),
      min_area_(numeric_limits<double>::max()),
      max_area_(0.0),
      min_wirelength_(numeric_limits<double>::max()),
      max_wirelength_(0.0),
      average_area_(0.0),
      average_wirelength_(0.0),
      average_uphill_cost_(0.0),
      best_floorplan_(Floorplan(database_.num_macros(), is_drawing)),
      drawing_(Json::object()) {
  const int num_macros = database_.num_macros();
  const int num_perturbations = num_macros * num_macros;
  Floorplan floorplan(best_floorplan_);

  double total_area = 0.0;
  double total_wirelength = 0.0;
  for (int i = 0; i < num_perturbations; ++i) {
    floorplan.Perturb(database_);
    floorplan.Pack(database_);
    const double area = floorplan.area();
    const double wirelength = floorplan.wirelength();
    if (area < min_area_) {
      min_area_ = area;
    }
    if (area > max_area_) {
      max_area_ = area;
    }
    if (wirelength < min_wirelength_) {
      min_wirelength_ = wirelength;
    }
    if (wirelength > max_wirelength_) {
      max_wirelength_ = wirelength;
    }
    total_area += area;
    total_wirelength += wirelength;
  }
  average_area_ = total_area / num_perturbations;
  average_wirelength_ = total_wirelength / num_perturbations;

  double total_uphill_cost = 0.0;
  int num_uphills = 0;
  double adaptive_alpha = alpha_ / 4.0;
  double adaptive_beta = (1.0 - alpha_) / 4.0;
  double last_cost =
      ComputeCost(best_floorplan_, adaptive_alpha, adaptive_beta);
  for (int i = 0; i < num_perturbations; ++i) {
    floorplan.Perturb(database_);
    floorplan.Pack(database_);
    double cost = ComputeCost(floorplan, adaptive_alpha, adaptive_beta);
    double delta_cost = cost - last_cost;
    if (delta_cost > 0) {
      ++num_uphills;
      total_uphill_cost += delta_cost;
    }
    last_cost = cost;
  }
  average_uphill_cost_ = total_uphill_cost / num_uphills;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %cl
movb %cl, -0x21(%rbp)
andb $0x1, %al
movb %al, -0x22(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x178(%rbp)
movq -0x10(%rbp), %rax
movq %rax, (%rdi)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, 0x8(%rdi)
addq $0x10, %rdi
movq %rdi, -0x180(%rbp)
movq -0x20(%rbp), %rsi
callq 0x9200
movq -0x178(%rbp), %rax
movb -0x21(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x30(%rax)
movb -0x22(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x31(%rax)
callq 0x2c3e0
movq -0x178(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movq $0x0, 0x40(%rax)
callq 0x2c3e0
movq -0x178(%rbp), %rax
movsd %xmm0, 0x48(%rax)
movq $0x0, 0x50(%rax)
movq $0x0, 0x58(%rax)
movq $0x0, 0x60(%rax)
movq $0x0, 0x68(%rax)
movq %rax, %rcx
addq $0x70, %rcx
movq %rcx, -0x170(%rbp)
movq (%rax), %rdi
callq 0xddf0
movl %eax, -0x164(%rbp)
jmp 0x2814b
movl -0x164(%rbp), %esi
movq -0x170(%rbp), %rdi
movzbl -0x22(%rbp), %edx
andl $0x1, %edx
callq 0x16450
jmp 0x28166
movq -0x178(%rbp), %rax
addq $0x118, %rax            # imm = 0x118
movq %rax, -0x188(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x1bdd0
movq -0x188(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x1bd80
jmp 0x28199
movq -0x178(%rbp), %rax
movq (%rax), %rdi
callq 0xddf0
movl %eax, -0x18c(%rbp)
jmp 0x281b0
movq -0x178(%rbp), %rsi
movl -0x18c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
imull %eax, %eax
movl %eax, -0x50(%rbp)
addq $0x70, %rsi
leaq -0xf8(%rbp), %rdi
callq 0x2c3f0
jmp 0x281db
xorps %xmm0, %xmm0
movsd %xmm0, -0x100(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x108(%rbp)
movl $0x0, -0x10c(%rbp)
movl -0x10c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x283cc
movq -0x178(%rbp), %rax
movq (%rax), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x16830
jmp 0x28222
movq -0x178(%rbp), %rax
movq (%rax), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x17e90
jmp 0x2823a
leaq -0xf8(%rbp), %rdi
callq 0x167c0
movsd %xmm0, -0x198(%rbp)
jmp 0x28250
movsd -0x198(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x167e0
movsd %xmm0, -0x1a0(%rbp)
jmp 0x28276
movq -0x178(%rbp), %rax
movsd -0x1a0(%rbp), %xmm0
movsd %xmm0, -0x120(%rbp)
movsd -0x118(%rbp), %xmm1
movsd 0x38(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x28306
movq -0x178(%rbp), %rax
movsd -0x118(%rbp), %xmm0
movsd %xmm0, 0x38(%rax)
jmp 0x28306
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x285c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x285bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x285af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x2c510
jmp 0x285af
movq -0x178(%rbp), %rax
movsd -0x118(%rbp), %xmm0
ucomisd 0x40(%rax), %xmm0
jbe 0x28330
movq -0x178(%rbp), %rax
movsd -0x118(%rbp), %xmm0
movsd %xmm0, 0x40(%rax)
movq -0x178(%rbp), %rax
movsd -0x120(%rbp), %xmm1
movsd 0x48(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2835e
movq -0x178(%rbp), %rax
movsd -0x120(%rbp), %xmm0
movsd %xmm0, 0x48(%rax)
movq -0x178(%rbp), %rax
movsd -0x120(%rbp), %xmm0
ucomisd 0x50(%rax), %xmm0
jbe 0x28388
movq -0x178(%rbp), %rax
movsd -0x120(%rbp), %xmm0
movsd %xmm0, 0x50(%rax)
movsd -0x118(%rbp), %xmm0
addsd -0x100(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0x120(%rbp), %xmm0
addsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x281fb
movq -0x178(%rbp), %rdi
movsd -0x100(%rbp), %xmm0
cvtsi2sdl -0x50(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rdi)
movsd -0x108(%rbp), %xmm0
cvtsi2sdl -0x50(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rdi)
movq $0x0, -0x128(%rbp)
movl $0x0, -0x12c(%rbp)
movsd 0x8(%rdi), %xmm0
movsd 0x22e1f(%rip), %xmm1    # 0x4b240
mulsd %xmm1, %xmm0
movsd %xmm0, -0x138(%rbp)
movsd 0x8(%rdi), %xmm2
movsd 0x22e0e(%rip), %xmm0    # 0x4b248
subsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
movq %rdi, %rsi
addq $0x70, %rsi
movsd -0x138(%rbp), %xmm0
movsd -0x140(%rbp), %xmm1
callq 0x285e0
movsd %xmm0, -0x1a8(%rbp)
jmp 0x28470
movsd -0x1a8(%rbp), %xmm0
movsd %xmm0, -0x148(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x2857a
movq -0x178(%rbp), %rax
movq (%rax), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x16830
jmp 0x284b1
movq -0x178(%rbp), %rax
movq (%rax), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x17e90
jmp 0x284c9
movq -0x178(%rbp), %rdi
movsd -0x138(%rbp), %xmm0
movsd -0x140(%rbp), %xmm1
leaq -0xf8(%rbp), %rsi
callq 0x285e0
movsd %xmm0, -0x1b0(%rbp)
jmp 0x284f6
movsd -0x1b0(%rbp), %xmm0
movsd %xmm0, -0x158(%rbp)
movsd -0x158(%rbp), %xmm0
subsd -0x148(%rbp), %xmm0
movsd %xmm0, -0x160(%rbp)
movsd -0x160(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x28556
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
movsd -0x160(%rbp), %xmm0
addsd -0x128(%rbp), %xmm0
movsd %xmm0, -0x128(%rbp)
movsd -0x158(%rbp), %xmm0
movsd %xmm0, -0x148(%rbp)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x2848a
movq -0x178(%rbp), %rax
movsd -0x128(%rbp), %xmm0
cvtsi2sdl -0x12c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rax)
leaq -0xf8(%rbp), %rdi
callq 0x2c510
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x188(%rbp), %rdi
callq 0x1c060
movq -0x170(%rbp), %rdi
callq 0x2c510
movq -0x180(%rbp), %rdi
callq 0x9a48
movq -0x30(%rbp), %rdi
callq 0x98f0
nopl (%rax)