// File: Czxck001[P]algorithms/src/mom.cc
// Function: int protoSelect<int>(std::vector<int, std::allocator<int>>::iterator, std::vector<int, std::allocator<int>>::iterator, int)
T protoSelect(VIter<T> l, VIter<T> r, int k) {
  assert(l + k < r);

  auto G = (r - l) / GROUPSIZE;
  if (l == r - 1 && k == 0 || G == 0) {
    return *(l + k);
  }

  auto M = std::vector<T>(G);
  for (auto g = 0; g < G; g++) {
    M[g] = medianOfGroup<T>(l + g * GROUPSIZE,
                            l + (g + 1) * GROUPSIZE);
  }  // O(n)

  auto P = protoSelect<T>(M.begin(), M.end(), G / 2);  // T(n / 5)

  auto s = partition<T>(l, r, P);  // O(n)

  if (l + k == s) {
    return *s;
  } else if (l + k < s) {
    return protoSelect<T>(l, s, k);
  } else {  // l + k > s
    return protoSelect<T>(s + 1, r, l + k - s - 1);
  }  // T(7n / 10)

  // T(n) = T(n / 5) + T(7n / 10) + O(n) = O(n)
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movslq -0x1c(%rbp), %rsi
leaq -0x10(%rbp), %rdi
callq 0x2320
movq %rax, -0x28(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x22e0
testb $0x1, %al
jne 0x169a
jmp 0x169c
jmp 0x16bb
leaq 0x19c2(%rip), %rdi      # 0x3065
leaq 0x19c5(%rip), %rsi      # 0x306f
movl $0x36, %edx
leaq 0x19c4(%rip), %rcx      # 0x307a
callq 0x1070
leaq -0x18(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x1d80
movl $0x5, %ecx
cqto
idivq %rcx
movq %rax, -0x30(%rbp)
leaq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x23a0
movq %rax, -0x38(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2360
testb $0x1, %al
jne 0x16fb
jmp 0x1709
movb $0x1, %al
cmpl $0x0, -0x1c(%rbp)
movb %al, -0xf1(%rbp)
je 0x1717
cmpq $0x0, -0x30(%rbp)
sete %al
movb %al, -0xf1(%rbp)
movb -0xf1(%rbp), %al
testb $0x1, %al
jne 0x1723
jmp 0x1747
movslq -0x1c(%rbp), %rsi
leaq -0x10(%rbp), %rdi
callq 0x2320
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x23e0
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x1a3c
movq -0x30(%rbp), %rax
movq %rax, -0x108(%rbp)
leaq -0x59(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x1390
movq -0x108(%rbp), %rsi
movq -0x100(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x2400
jmp 0x177b
leaq -0x59(%rbp), %rdi
callq 0x1430
movl $0x0, -0x70(%rbp)
movslq -0x70(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x183d
movl -0x70(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
movslq %eax, %rsi
leaq -0x10(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x2320
movq -0x118(%rbp), %rdi
movq %rax, -0x78(%rbp)
movl -0x70(%rbp), %ecx
movl %ecx, %eax
leal 0x5(%rax,%rax,4), %eax
movslq %eax, %rsi
callq 0x2320
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x2470
movl %eax, -0x10c(%rbp)
jmp 0x17e9
movslq -0x70(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2570
movl -0x10c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x178b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x1430
jmp 0x1a48
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x1580
jmp 0x1a48
leaq -0x58(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x1520
movq -0x128(%rbp), %rdi
movq %rax, -0x90(%rbp)
callq 0x1550
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rcx, %rax
shrq %rax
movl %eax, %edx
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x1660
movl %eax, -0x11c(%rbp)
jmp 0x1895
movl -0x11c(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x84(%rbp), %edx
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x2590
movq %rax, -0x130(%rbp)
jmp 0x18d9
movq -0x130(%rbp), %rax
movq %rax, -0xa0(%rbp)
movslq -0x1c(%rbp), %rsi
leaq -0x10(%rbp), %rdi
callq 0x2320
movq %rax, -0xb8(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2360
testb $0x1, %al
jne 0x1914
jmp 0x1934
leaq -0xa0(%rbp), %rdi
callq 0x23e0
movl (%rax), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xbc(%rbp)
jmp 0x1a33
movslq -0x1c(%rbp), %rsi
leaq -0x10(%rbp), %rdi
callq 0x2320
movq %rax, -0xc8(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x22e0
testb $0x1, %al
jne 0x1961
jmp 0x19b0
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x1c(%rbp), %edx
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x1660
movl %eax, -0x134(%rbp)
jmp 0x1998
movl -0x134(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xbc(%rbp)
jmp 0x1a33
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x140(%rbp)
movl $0x1, %esi
callq 0x2320
movq %rax, -0xe0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xe8(%rbp)
movslq -0x1c(%rbp), %rsi
leaq -0x10(%rbp), %rdi
callq 0x2320
movq -0x140(%rbp), %rsi
movq %rax, -0xf0(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x1d80
movl %eax, %edx
decl %edx
movq -0xe0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x1660
movl %eax, -0x138(%rbp)
jmp 0x1a20
movl -0x138(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xbc(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x1580
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x1100
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)