// File: /workspace/github_cpp_makefile_star2_O3/amukherj[P]concurrencyts/./future.h
// Function: concurrencyts::future<concurrencyts::when_any_result<std::vector<std::iterator_traits<__gnu_cxx::__normal_iterator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*, std::vector<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>>::value_type, std::allocator<std::iterator_traits<__gnu_cxx::__normal_iterator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*, std::vector<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>>::value_type>>>> concurrencyts::when_any<__gnu_cxx::__normal_iterator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*, std::vector<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>>(__gnu_cxx::__normal_iterator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*, std::vector<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>, __gnu_cxx::__normal_iterator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*, std::vector<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<concurrencyts::future<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>)
auto when_any(InputIt begin, InputIt end)
  -> future<when_any_result<
      std::vector<typename std::iterator_traits<InputIt>::value_type>
  >> {

  using ret_type = when_any_result<
    std::vector<typename std::iterator_traits<InputIt>::value_type>>;
  ret_type result_value;

  struct _sync_context {
    std::mutex mtx;
    std::condition_variable found, start;
    bool done, startCheck;
    promise<ret_type> prom;

    _sync_context() : done(false), startCheck(false) {}
  };
  auto context = std::make_shared<_sync_context>();
  auto result = context->prom.get_future();

  if (begin == end) {
    context->prom.set(make_when_any_result(-1, begin, end));
    return result;
  }

  std::thread thr([begin, end, context]() {
    std::unique_lock<std::mutex> ul{context->mtx};
    context->startCheck = true;
    context->start.notify_one();
    while (!context->done) {
      context->found.wait(ul);

      // main thread found one already
      if (context->done) {
        return;
      }

      for (auto it = begin; it != end; ++it) {
        if (it->is_ready()) {
          context->prom.set(make_when_any_result(std::distance(begin, it),
            begin, end));
          break;
        }
      }
    }
  });
  thr.detach();

  std::unique_lock<std::mutex> ul{context->mtx};
  context->start.wait(ul, [context]()->bool {return context->startCheck;});
  for (auto it = begin; it != end; ++it) {
    if (it->is_ready()) {
      context->done = true;
      context->found.notify_one();
      context->prom.set(make_when_any_result(std::distance(begin, it),
        begin, end));
      break;
    } else {
      it->await(context->found);
    }
  }

  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
leaq 0x60(%rsp), %r14
xorl %eax, %eax
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
leaq 0x10(%rsp), %rdi
movq %rax, -0x8(%rdi)
leaq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x78d4
movl $0x90, %esi
addq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x7790
cmpq %r15, %r12
je 0x6489
movq %r12, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x64c9
movq 0x7b49(%rip), %rcx      # 0xdfc8
cmpb $0x0, (%rcx)
je 0x64c5
incl 0x8(%rax)
jmp 0x64c9
movq 0x8(%rsp), %rbx
leaq 0x28(%rsp), %rdi
movq $-0x1, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x77e8
movq 0x90(%rbx), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7b88
leaq 0x30(%rsp), %rdi
callq 0x63b0
jmp 0x6604
lock
incl 0x8(%rax)
leaq 0x20(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %r13, 0x18(%rsp)
callq 0x784c
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x64ec
callq 0x66d0
leaq 0x20(%rsp), %rdi
callq 0x3270
movq 0x8(%rsp), %rax
leaq 0x48(%rsp), %rdi
movq %rax, (%rdi)
movb $0x0, 0x8(%rdi)
callq 0x6800
movb $0x1, 0x50(%rsp)
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x6537
movq 0x7aa1(%rip), %rax      # 0xdfc8
cmpb $0x0, (%rax)
je 0x6532
incl 0x8(%r13)
jmp 0x6537
lock
incl 0x8(%r13)
cmpb $0x0, 0x89(%r14)
jne 0x655f
leaq 0x58(%r14), %rbp
leaq 0x48(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3060
cmpb $0x1, 0x89(%r14)
jne 0x654a
testq %r13, %r13
je 0x656c
movq %r13, %rdi
callq 0x66d0
xorl %r13d, %r13d
movq (%r12,%r13), %rdi
movq 0x8(%rsp), %rsi
cmpb $0x1, 0x79(%rdi)
je 0x659a
addq $0x28, %rsi
callq 0x7de8
leaq (%r12,%r13), %rax
addq $0x10, %rax
addq $0x10, %r13
cmpq %r15, %rax
jne 0x656f
jmp 0x65e4
movb $0x1, 0x88(%rsi)
addq $0x28, %rsi
movq %rsi, %rdi
callq 0x30e0
movq 0x8(%rsp), %rbx
sarq $0x4, %r13
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x77e8
movq 0x90(%rbx), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7b88
leaq 0x30(%rsp), %rdi
callq 0x63b0
leaq 0x48(%rsp), %rdi
callq 0x67ea
cmpq $0x0, 0x20(%rsp)
jne 0x66ca
leaq 0x60(%rsp), %r14
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6613
callq 0x66d0
movq %r14, %rdi
callq 0x63b0
movq %r13, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x63b0
jmp 0x6684
jmp 0x6681
movq %r13, 0x18(%rsp)
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x63b0
jmp 0x6696
movq %r13, 0x18(%rsp)
movq %rax, %r15
jmp 0x6696
jmp 0x665e
movq %rax, %r15
jmp 0x668e
movq %rax, %r15
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x6696
callq 0x66d0
jmp 0x6696
movq %rax, %r15
jmp 0x66a9
movq %rax, %r15
jmp 0x66b8
movq %rax, %r15
leaq 0x48(%rsp), %rdi
callq 0x67ea
cmpq $0x0, 0x20(%rsp)
jne 0x66ca
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x66a9
callq 0x66d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x66b8
callq 0x66d0
leaq 0x60(%rsp), %rdi
callq 0x63b0
movq %r15, %rdi
callq 0x3280
callq 0x3050
nop