// File: /workspace/github_cpp_makefile_star2_O2/cegme[P]vose/VoseTree.cc
// Function: VoseTree::add(marble)
double VoseTree::add(marble m) {
  
  // Update the new weight
  total_weight += m.weight;
  double contribution = (double) m.weight / total_weight;

  // Add the marble to another branch
  if(!branches.empty()) {
    double new_total = branches.back() + m.weight;
    branches.push_back(new_total);
  }
  else {
    branches.push_back(m.weight);
  }
    
  // Add this marble to the free marble list
  vosetreenode vtn;
  vtn.isBag = false;
  vtn.ball = m;
  nodes.push_back(vtn);

  // Shrink data structure if necessary
  if (branches.size() - last_bag > max_marbles) {
    // Compress the marbles to make a new bag
    Vose v;
    for(size_t i = last_bag; i != nodes.size(); ++i) {
      v.add(nodes[i].ball);
    }
    v.init();
    vosetreenode vtn2 (v);

    nodes[last_bag] = vtn2;
    nodes.resize(last_bag+1);
    
    branches.resize(last_bag+1);
    branches[last_bag] = total_weight;

    ++last_bag;
    if (last_bag > max_bags) {
      // Merge all the bags into one bag
      Vose z;
      z = merge_bags();
      nodes.resize(1);
      nodes[0] = z;

      // Update the branch and bag locations
      last_bag = 1;
      total_weight = z.weight();

      branches.resize(1);
      branches[0] = total_weight;
    } 
  } 
  
  // Update a random number generator to new range
  std::default_random_engine generator(VoseUtil::RANDOM_SEED);
  std::uniform_real_distribution<double>
    real_distribution(0, total_weight);
  random_branch = std::bind(real_distribution, generator);

  return contribution;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %r13
movq 0x58(%rdi), %rax
addq %r13, %rax
movq %rax, -0x30(%rbp)
movq %rax, 0x58(%rdi)
addq $0x20, %rdi
movq 0x28(%rbx), %rax
cmpq %rax, 0x20(%rbx)
movq %rdi, -0x48(%rbp)
je 0x2568
movq -0x8(%rax), %rax
addq %r13, %rax
cvtsi2sd %rax, %xmm0
cvttsd2si %xmm0, %rax
leaq -0x260(%rbp), %rsi
movq %rax, (%rsi)
callq 0x4596
jmp 0x2570
movq %r15, %rsi
callq 0x32b2
leaq -0x240(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %r12d, %r12d
movb %r12b, (%rax)
leaq -0x228(%rbp), %r14
movl $0x90, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2110
movb %r12b, -0x38(%r14)
leaq -0x258(%rbp), %rdi
movq %r15, %rsi
callq 0x32da
leaq 0x38(%rbx), %r14
leaq -0x260(%rbp), %rsi
movq %r14, %rdi
callq 0x330a
movq 0x28(%rbx), %rax
movq 0x50(%rbx), %r15
subq 0x20(%rbx), %rax
sarq $0x3, %rax
subq %r15, %rax
cmpq 0x60(%rbx), %rax
jbe 0x27d6
movq %r14, -0x38(%rbp)
movq %r13, -0x40(%rbp)
leaq -0x198(%rbp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x2110
imulq $0xc8, %r15, %r12
addq $0x8, %r12
movl $0xc8, %r13d
leaq -0x78(%rbp), %r14
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rax
subq %rsi, %rax
cqto
idivq %r13
cmpq %rax, %r15
je 0x2653
addq %r12, %rsi
movq %r14, %rdi
callq 0x3284
leaq -0x198(%rbp), %rdi
movq %r14, %rsi
callq 0x5d40
leaq -0x70(%rbp), %rdi
callq 0x20d0
incq %r15
addq $0xc8, %r12
jmp 0x260f
leaq -0x198(%rbp), %rdi
callq 0x5d72
leaq -0x380(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x3348
movq -0x40(%rbp), %r13
movq -0x48(%rbp), %r15
movq -0x38(%rbp), %r12
leaq -0x448(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x33f6
leaq -0x380(%rbp), %rdi
callq 0x3436
imulq $0xc8, 0x50(%rbx), %rdi
addq 0x38(%rbx), %rdi
leaq -0x448(%rbp), %rsi
callq 0x3472
movq 0x50(%rbx), %rsi
incq %rsi
movq %r12, %rdi
callq 0x34a8
movq 0x50(%rbx), %rsi
incq %rsi
movq %r15, %rdi
callq 0x34ea
movq 0x58(%rbx), %rax
movq 0x20(%rbx), %rcx
movq 0x50(%rbx), %rdx
movq %rax, (%rcx,%rdx,8)
movq 0x50(%rbx), %rax
incq %rax
movq %rax, 0x50(%rbx)
cmpq 0x68(%rbx), %rax
jbe 0x27be
leaq -0x108(%rbp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x2110
leaq -0x510(%rbp), %rdi
movq %rbx, %rsi
callq 0x28b8
leaq -0x108(%rbp), %rdi
leaq -0x510(%rbp), %r14
movq %r14, %rsi
callq 0x3522
movq %r14, %rdi
callq 0x3436
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x34a8
leaq -0x2f0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3348
leaq -0x510(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x33f6
movq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0x357c
movq %r14, %rdi
callq 0x35b2
leaq -0x2f0(%rbp), %rdi
callq 0x3436
movq $0x1, 0x50(%rbx)
movq -0xb0(%rbp), %rax
movq %rax, 0x58(%rbx)
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x34ea
movq 0x20(%rbx), %rax
movq 0x58(%rbx), %rcx
movq %rcx, (%rax)
leaq -0x108(%rbp), %rdi
callq 0x3436
leaq -0x448(%rbp), %rdi
callq 0x35b2
leaq -0x198(%rbp), %rdi
callq 0x3436
cvtsi2sdq 0x58(%rbx), %xmm0
leaq -0x448(%rbp), %rsi
andq $0x0, (%rsi)
movsd %xmm0, 0x8(%rsi)
movq $0x2a, 0x10(%rsi)
movq %rbx, %rdi
callq 0x35d6
cvtsi2sd %r13, %xmm1
cvtsi2sdq -0x30(%rbp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x35b2
movsd -0x30(%rbp), %xmm0
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
callq 0x3436
jmp 0x284b
jmp 0x2848
jmp 0x2848
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x3436
jmp 0x2873
movq %rax, %rbx
leaq -0x380(%rbp), %rdi
callq 0x3436
jmp 0x2897
jmp 0x2886
jmp 0x2886
jmp 0x2881
movq %rax, %rbx
leaq -0x448(%rbp), %rdi
callq 0x35b2
jmp 0x2897
movq %rax, %rbx
jmp 0x28a3
movq %rax, %rbx
jmp 0x2897
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x20d0
leaq -0x198(%rbp), %rdi
callq 0x3436
leaq -0x260(%rbp), %rdi
callq 0x35b2
movq %rbx, %rdi
callq 0x22d0
nop