// File: amukherj[P]concurrencyts/future.cc
// Function: when_any()
void when_any() {
  std::vector<std::experimental::future<std::string>> futures;

  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(10));
    return "Hello";
  }) );
  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(9));
    return "World";
  }) );
  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(8));
    return "I've";
  }) );
  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(7));
    return "been";
  }) );
  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(6));
    return "waiting";
  }) );
  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return "for";
  }) );
  futures.push_back( std::experimental::async([]()->std::string {
    std::this_thread::sleep_for(std::chrono::seconds(6));
    return "you";
  }) );

  auto rf = std::experimental::when_any(futures.begin(), futures.end());
  auto result = std::move(rf.get());
  std::cout << result.futures[result.index].get() << '\n';
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
leaq -0x18(%rbp), %rdi
callq 0xaf30
leaq -0x28(%rbp), %rdi
leaq -0x29(%rbp), %rsi
callq 0x47f0
jmp 0x44c3
leaq -0x18(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0xaf50
jmp 0x44d2
leaq -0x28(%rbp), %rdi
callq 0xaf10
leaq -0x50(%rbp), %rdi
leaq -0x51(%rbp), %rsi
callq 0x48d0
jmp 0x44ea
leaq -0x18(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0xaf50
jmp 0x44f9
leaq -0x50(%rbp), %rdi
callq 0xaf10
leaq -0x68(%rbp), %rdi
leaq -0x69(%rbp), %rsi
callq 0x49b0
jmp 0x4511
leaq -0x18(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xaf50
jmp 0x4520
leaq -0x68(%rbp), %rdi
callq 0xaf10
leaq -0x80(%rbp), %rdi
leaq -0x81(%rbp), %rsi
callq 0x4a90
jmp 0x453b
leaq -0x18(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xaf50
jmp 0x454a
leaq -0x80(%rbp), %rdi
callq 0xaf10
leaq -0x98(%rbp), %rdi
leaq -0x99(%rbp), %rsi
callq 0x4b70
jmp 0x4568
leaq -0x18(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xaf50
jmp 0x457a
leaq -0x98(%rbp), %rdi
callq 0xaf10
leaq -0xb0(%rbp), %rdi
leaq -0xb1(%rbp), %rsi
callq 0x4c50
jmp 0x459b
leaq -0x18(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xaf50
jmp 0x45ad
leaq -0xb0(%rbp), %rdi
callq 0xaf10
leaq -0xc8(%rbp), %rdi
leaq -0xc9(%rbp), %rsi
callq 0x4d30
jmp 0x45ce
leaq -0x18(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0xaf50
jmp 0x45e0
leaq -0xc8(%rbp), %rdi
callq 0xaf10
leaq -0x18(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0xb0d0
movq -0x118(%rbp), %rdi
movq %rax, -0xe8(%rbp)
callq 0xb100
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0xb2c0
jmp 0x4632
leaq -0xe0(%rbp), %rdi
callq 0xb750
movq %rax, -0x120(%rbp)
jmp 0x4647
movq -0x120(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0xb780
leaq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0xb7c0
movq %rax, %rdi
callq 0xb1f0
movq %rax, -0x128(%rbp)
jmp 0x467e
movq -0x128(%rbp), %rsi
movq 0x1e944(%rip), %rdi     # 0x22fd0
callq 0x3190
movq %rax, -0x130(%rbp)
jmp 0x469a
movq -0x130(%rbp), %rdi
movl $0xa, %esi
callq 0x3210
jmp 0x46ad
leaq -0x110(%rbp), %rdi
callq 0xb7e0
leaq -0xe0(%rbp), %rdi
callq 0xb800
leaq -0x18(%rbp), %rdi
callq 0xb240
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0xaf10
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x47c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0xb7e0
leaq -0xe0(%rbp), %rdi
callq 0xb800
leaq -0x18(%rbp), %rdi
callq 0xb240
movq -0x38(%rbp), %rdi
callq 0x32d0
nopw %cs:(%rax,%rax)