// File: /workspace/github_cpp_makefile_star2_O1/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 0x7ade
movl $0x90, %esi
addq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x797c
cmpq %r15, %r12
je 0x6609
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 0x6649
movq 0x89c9(%rip), %rcx      # 0xefc8
cmpb $0x0, (%rcx)
je 0x6645
incl 0x8(%rax)
jmp 0x6649
movq 0x8(%rsp), %rbx
leaq 0x28(%rsp), %rdi
movq $-0x1, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x79d3
movq 0x90(%rbx), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7dac
leaq 0x30(%rsp), %rdi
callq 0x6536
jmp 0x6789
lock
incl 0x8(%rax)
leaq 0x20(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %r13, 0x18(%rsp)
callq 0x7a38
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x666c
callq 0x6854
leaq 0x20(%rsp), %rdi
callq 0x3270
movq 0x8(%rsp), %rax
leaq 0x48(%rsp), %rdi
movq %rax, (%rdi)
movb $0x0, 0x8(%rdi)
callq 0x6984
movb $0x1, 0x50(%rsp)
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x66b7
movq 0x8921(%rip), %rax      # 0xefc8
cmpb $0x0, (%rax)
je 0x66b2
incl 0x8(%r13)
jmp 0x66b7
lock
incl 0x8(%r13)
cmpb $0x0, 0x89(%r14)
jne 0x66df
leaq 0x58(%r14), %rbp
leaq 0x48(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3060
cmpb $0x1, 0x89(%r14)
jne 0x66ca
testq %r13, %r13
je 0x66ec
movq %r13, %rdi
callq 0x6854
cmpq %r15, %r12
je 0x6769
xorl %r13d, %r13d
movq (%r12,%r13), %rdi
movq 0x8(%rsp), %rsi
cmpb $0x1, 0x79(%rdi)
je 0x671f
addq $0x28, %rsi
callq 0x7ffc
leaq (%r12,%r13), %rax
addq $0x10, %rax
addq $0x10, %r13
cmpq %r15, %rax
jne 0x66f4
jmp 0x6769
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 0x79d3
movq 0x90(%rbx), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7dac
leaq 0x30(%rsp), %rdi
callq 0x6536
leaq 0x48(%rsp), %rdi
callq 0x696e
cmpq $0x0, 0x20(%rsp)
jne 0x684f
leaq 0x60(%rsp), %r14
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6798
callq 0x6854
movq %r14, %rdi
callq 0x6536
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 0x6536
jmp 0x6809
jmp 0x6806
movq %r13, 0x18(%rsp)
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x6536
jmp 0x681b
movq %r13, 0x18(%rsp)
movq %rax, %r15
jmp 0x681b
jmp 0x67e3
movq %rax, %r15
jmp 0x6813
movq %rax, %r15
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x681b
callq 0x6854
jmp 0x681b
movq %rax, %r15
jmp 0x682e
movq %rax, %r15
jmp 0x683d
movq %rax, %r15
leaq 0x48(%rsp), %rdi
callq 0x696e
cmpq $0x0, 0x20(%rsp)
jne 0x684f
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x682e
callq 0x6854
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x683d
callq 0x6854
leaq 0x60(%rsp), %rdi
callq 0x6536
movq %r15, %rdi
callq 0x3280
callq 0x3050