// File: integeruser[P]GA3C-cpp/src/ga3c.cc
// Function: train(std::shared_ptr<Model>, std::vector<std::shared_ptr<Environment>, std::allocator<std::shared_ptr<Environment>>> const&)
void train(std::shared_ptr<Model> model, const std::vector<std::shared_ptr<Environment>>& environments)
{
    assert(BATCH_SIZE >= 0);

    assert(N_STEP_RETURN >= 1);
    assert(GAMMA >= 0.0f and GAMMA <= 1.0f);

    assert(EPSILON_START >= 0.0f and EPSILON_START <= 1.0f);
    assert(EPSILON_END   >= 0.0f and EPSILON_END   <= 1.0f and EPSILON_END <= EPSILON_START);

    assert(NUM_AGENTS >= 0);
    assert(MAX_NUM_TRAINING_EXPERIENCES >= 0);

    shared_model = model;
    assert(environments.size() == NUM_AGENTS);

    // start the trainer and the agents
    auto trainer_thread = std::thread(trainer);
    auto agents_threads = std::vector<std::thread>(NUM_AGENTS);
    for (uint32_t id = 0; id < NUM_AGENTS; ++id) {
        agents_threads[id] = std::thread(agent, id, environments[id]);
    }

    // and wait for them to finish
    for (uint32_t id = 0; id < NUM_AGENTS; ++id) {
        agents_threads[id].join();
    }
    trainer_thread.join();
    assert(num_training_experiences == MAX_NUM_TRAINING_EXPERIENCES);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x1cf3d(%rip), %rax     # 0x211e8
cmpl $0x0, (%rax)
jb 0x42b2
jmp 0x42d1
leaq 0xfe11(%rip), %rdi      # 0x140ca
leaq 0xfda3(%rip), %rsi      # 0x14063
movl $0xb3, %edx
leaq 0xfe0e(%rip), %rcx      # 0x140da
callq 0x30f0
leaq 0x1cf14(%rip), %rax     # 0x211ec
cmpl $0x0, (%rax)
jb 0x42df
jmp 0x42fe
leaq 0xfe4a(%rip), %rdi      # 0x14130
leaq 0xfd76(%rip), %rsi      # 0x14063
movl $0xb4, %edx
leaq 0xfde1(%rip), %rcx      # 0x140da
callq 0x30f0
movq -0x60(%rbp), %rsi
leaq 0x1cef7(%rip), %rdi     # 0x21200
callq 0x58d0
movq -0x10(%rbp), %rdi
callq 0x5900
leaq 0x1ceca(%rip), %rcx     # 0x211e8
movl (%rcx), %ecx
cmpq %rcx, %rax
jne 0x4327
jmp 0x4346
leaq 0xfe24(%rip), %rdi      # 0x14152
leaq 0xfd2e(%rip), %rsi      # 0x14063
movl $0xb7, %edx
leaq 0xfd99(%rip), %rcx      # 0x140da
callq 0x30f0
leaq -0x20d(%rip), %rsi      # 0x4140
leaq -0x18(%rbp), %rdi
callq 0x5920
leaq 0x1ce8b(%rip), %rax     # 0x211e8
movl (%rax), %eax
movq %rax, -0x70(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x59e0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x5a00
jmp 0x4383
leaq -0x31(%rbp), %rdi
callq 0x5a70
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
leaq 0x1ce4b(%rip), %rcx     # 0x211e8
cmpl (%rcx), %eax
jae 0x442e
movq -0x10(%rbp), %rdi
movl -0x48(%rbp), %eax
movl %eax, %esi
callq 0x5a90
movq %rax, %rcx
leaq -0xacd(%rip), %rsi      # 0x38f0
leaq -0x50(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x5ab0
jmp 0x43cc
movl -0x48(%rbp), %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x5b80
movq %rax, %rdi
leaq -0x50(%rbp), %rsi
callq 0x5ba0
leaq -0x50(%rbp), %rdi
callq 0x5be0
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4393
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x5a70
jmp 0x44bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x5c10
jmp 0x44bd
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
leaq 0x1cda9(%rip), %rcx     # 0x211e8
cmpl (%rcx), %eax
jae 0x4468
movl -0x54(%rbp), %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x5b80
movq %rax, %rdi
callq 0x3330
jmp 0x445b
jmp 0x445d
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4435
leaq -0x18(%rbp), %rdi
callq 0x3330
jmp 0x4473
movl 0x1ce3f(%rip), %eax     # 0x212b8
leaq 0x1cd6c(%rip), %rcx     # 0x211ec
cmpl (%rcx), %eax
jne 0x4486
jmp 0x44a5
leaq 0xfce7(%rip), %rdi      # 0x14174
leaq 0xfbcf(%rip), %rsi      # 0x14063
movl $0xc5, %edx
leaq 0xfc3a(%rip), %rcx      # 0x140da
callq 0x30f0
leaq -0x30(%rbp), %rdi
callq 0x5c10
leaq -0x18(%rbp), %rdi
callq 0x5be0
addq $0x70, %rsp
popq %rbp
retq
leaq -0x18(%rbp), %rdi
callq 0x5be0
movq -0x40(%rbp), %rdi
callq 0x3350
nop