// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
leaq 0x60(%rsp), %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %rsp, %rdi
callq 0x6da1
movl $0x90, %esi
addq (%rsp), %rsi
movq %r14, %rdi
callq 0x6dbc
cmpq %r15, %r12
je 0x57dd
leaq 0x28(%rsp), %rdi
movq %r12, -0x10(%rdi)
movq %r15, -0x8(%rdi)
movq %rsp, %rsi
callq 0x73b6
leaq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x6e5c
leaq 0x30(%rsp), %rdi
callq 0x594e
leaq 0x10(%rsp), %rdi
callq 0x3290
movq (%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x5ab8
movq %rsp, %rsi
movq (%rsi), %r13
addq $0x58, %r13
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
callq 0x73b6
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x6edc
leaq 0x50(%rsp), %rdi
callq 0x594e
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
cmpq %r15, %rdi
je 0x5860
movq (%rdi), %rax
movq (%rsp), %rsi
cmpb $0x1, 0x79(%rax)
je 0x5814
addq $0x28, %rsi
callq 0x6f0a
addq $0x10, %r13
jmp 0x57b4
movq (%rsp), %r13
leaq 0x18(%rsp), %rdi
pushq $-0x1
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x6dfc
addq $0x90, %r13
leaq 0x18(%rsp), %rsi
movq %r13, %rdi
callq 0x6df4
leaq 0x20(%rsp), %rdi
callq 0x56d0
jmp 0x5874
movb $0x1, 0x88(%rsi)
addq $0x28, %rsi
movq %rsi, %rdi
callq 0x30e0
movq (%rsp), %rbp
sarq $0x4, %r13
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x6dfc
addq $0x90, %rbp
leaq 0x18(%rsp), %rsi
movq %rbp, %rdi
callq 0x6df4
leaq 0x20(%rsp), %rdi
callq 0x56d0
leaq 0x38(%rsp), %rdi
callq 0x5b06
leaq 0x10(%rsp), %rdi
callq 0x55a8
leaq 0x8(%rsp), %rdi
callq 0x594e
movq %rbx, %rdi
callq 0x56d0
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
leaq 0x20(%rsp), %rdi
callq 0x56d0
jmp 0x58ef
jmp 0x58ec
movq %rax, %r15
leaq 0x20(%rsp), %rdi
callq 0x56d0
jmp 0x5903
movq %rax, %r15
jmp 0x5903
movq %rax, %r15
leaq 0x50(%rsp), %rdi
callq 0x594e
jmp 0x58ef
jmp 0x58ce
movq %rax, %r15
jmp 0x58f9
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x594e
jmp 0x5903
movq %rax, %r15
jmp 0x590f
movq %rax, %r15
jmp 0x5919
movq %rax, %r15
leaq 0x38(%rsp), %rdi
callq 0x5b06
leaq 0x10(%rsp), %rdi
callq 0x55a8
addq $0x8, %r14
movq %r14, %rdi
callq 0x594e
leaq 0x8(%rsp), %rdi
callq 0x594e
movq %rbx, %rdi
callq 0x56d0
movq %r15, %rdi
callq 0x32a0
nop