// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
movslq %edx, %rax
leaq (%rdi,%rax,4), %rdi
cmpq %rsi, %rdi
jae 0x1608
movl %edx, %r8d
movq %rsi, %rbx
subq %r14, %rbx
sarq $0x2, %rbx
movabsq $0x6666666666666667, %rcx # imm = 0x6666666666666667
movq %rbx, %rax
imulq %rcx
leaq -0x4(%rsi), %rax
cmpq %r14, %rax
sete %al
testl %r8d, %r8d
sete %cl
andb %al, %cl
leaq 0x4(%rbx), %rax
cmpq $0x9, %rax
setb %al
orb %cl, %al
cmpb $0x1, %al
jne 0x149a
movl (%rdi), %ebx
jmp 0x15f7
movq %rdx, %r15
movl %r8d, -0x30(%rbp)
movq %rdi, -0x50(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdx, %rax
shrq $0x3f, %rax
sarq %r15
addq %rax, %r15
leaq -0x48(%rbp), %rdi
leaq -0x29(%rbp), %rdx
movq %r15, %rsi
callq 0x16e8
movq %rbx, -0x60(%rbp)
cmpq $0x5, %rbx
jl 0x1570
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
xorl %edx, %edx
movq %r14, %rcx
movq %r14, -0x78(%rbp)
movq %r15, -0x70(%rbp)
leaq (%rdx,%rdx,4), %rsi
leaq (%r14,%rsi,4), %rdi
leaq 0x1(%rdx), %rsi
movq %rcx, %r8
xorl %r10d, %r10d
leaq (%rdi,%r10), %r13
leaq 0x4(%r10), %r9
movl (%rdi,%r10), %r12d
movl $0x4, %ebx
movl %r12d, %r14d
movq %r13, %r11
leaq (%r8,%rbx), %rax
movl (%r8,%rbx), %r15d
cmpl %r15d, %r14d
cmovgel %r15d, %r14d
cmovgq %rax, %r11
leaq (%r10,%rbx), %rax
addq $0x4, %rax
addq $0x4, %rbx
cmpq $0x14, %rax
jne 0x1512
movl (%r11), %r10d
movl %r12d, (%r11)
movl %r10d, (%r13)
addq $0x4, %r8
movq %r9, %r10
cmpq $0xc, %r9
jne 0x14fb
movl 0x8(%rdi), %eax
movq -0x68(%rbp), %rdi
movl %eax, (%rdi,%rdx,4)
addq $0x14, %rcx
movq %rsi, %rdx
movq -0x70(%rbp), %r15
cmpq %r15, %rsi
movq -0x78(%rbp), %r14
jne 0x14e9
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rax
movabsq $0x6666666666666667, %rcx # imm = 0x6666666666666667
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x2, %rdx
addl %eax, %edx
callq 0x142f
movq %r14, %rdi
movq -0x58(%rbp), %rbx
movq %rbx, %rsi
movl %eax, %edx
callq 0x177e
movq -0x50(%rbp), %rdx
subq %rax, %rdx
je 0x15c7
jae 0x15cb
movq %r14, %rdi
movq %rax, %rsi
movl -0x30(%rbp), %edx
callq 0x142f
jmp 0x15e0
movl (%rax), %ebx
jmp 0x15e2
addq $0x4, %rax
shrq $0x2, %rdx
decl %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x142f
movl %eax, %ebx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x15f7
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x10d0
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa56(%rip), %rdi       # 0x2065
leaq 0xa59(%rip), %rsi       # 0x206f
leaq 0xa5d(%rip), %rcx       # 0x207a
movl $0x36, %edx
callq 0x1070
jmp 0x162b
jmp 0x162b
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x1643
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq %rbx, %rdi
callq 0x1110
nop