// File: integeruser[P]GA3C-cpp/src/ga3c.cc
// Function: agent(unsigned int, std::shared_ptr<Environment>)
void agent(uint32_t id, std::shared_ptr<Environment> env)
{
    for (uint32_t episode = 1; true; ++episode) {
        const float t = (float)num_training_experiences / (MAX_NUM_TRAINING_EXPERIENCES-1);
        const float epsilon = (1-t)*EPSILON_START + t*EPSILON_END;

        // reset the environment starting a new episode
        observation_t curr_state, next_state;
        curr_state = env->reset();

        memory_t memory;
        float reward = 0.0f;
        float episode_reward = 0.0f;

        bool done = false;
        while (not done) {
            // stop training after reaching a fixed number of training experiences
            if (num_training_experiences >= MAX_NUM_TRAINING_EXPERIENCES) { return; }

            // predict policy and value
            std::vector<float> policy;
            float value;
            std::tie(policy, value) = shared_model->predict_policy_and_value(curr_state);

            // select and perform an action
            const auto action = select_action(env, epsilon, policy);
            std::tie(next_state, reward, done) = env->step(action);
            episode_reward += reward;

            experience_t experience = experience_t{curr_state, action, reward, next_state, done};
            memory.push_back(experience);

            // add experiences to the training queue when:
            // - enough of them are accumulated in memory and the n-step return can be computed
            // - the episode is over and the memory is not empty
            while ((memory.size() >= N_STEP_RETURN) or (done and not memory.empty())) {
                const auto training_experience = extract_accumulated_experience(memory, value);
                training_experiences_queue.push(training_experience);
            }

            curr_state = next_state;
        }

        if (episode % 10 == 0) {
            std::cout << "[" << std::setfill('0') << std::setw(2) << id << "] "
                      << "Ep. " << std::setfill('0') << std::setw(5) << episode << " "
                      << "reward: " << episode_reward << std::endl;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rsi, -0x200(%rbp)
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0x391b
jmp 0x3f76
movl 0x1d997(%rip), %eax     # 0x212b8
cvtsi2ss %rax, %xmm0
leaq 0x1d8bf(%rip), %rax     # 0x211ec
movl (%rax), %eax
decl %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x18(%rbp)
movss -0x18(%rbp), %xmm1
movss 0x106b8(%rip), %xmm0    # 0x14004
subss %xmm1, %xmm0
movss 0x106b4(%rip), %xmm2    # 0x1400c
mulss %xmm2, %xmm1
addss %xmm1, %xmm0
movss %xmm0, -0x1c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4c20
leaq -0x50(%rbp), %rdi
callq 0x4c20
movq -0x200(%rbp), %rdi
callq 0x4790
movq %rax, %rsi
movq (%rsi), %rax
movq (%rax), %rax
leaq -0x68(%rbp), %rdi
callq *%rax
jmp 0x3994
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movq %rsi, -0x208(%rbp)
callq 0x4c40
movq -0x208(%rbp), %rdi
callq 0x4bc0
leaq -0xc8(%rbp), %rdi
callq 0x4c70
jmp 0x39c2
xorps %xmm0, %xmm0
movss %xmm0, -0xcc(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xd0(%rbp)
movb $0x0, -0xd1(%rbp)
movb -0xd1(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x39f0
jmp 0x3d17
movl 0x1d8c2(%rip), %eax     # 0x212b8
leaq 0x1d7ef(%rip), %rcx     # 0x211ec
cmpl (%rcx), %eax
jb 0x3a21
movl $0x1, -0xd8(%rbp)
jmp 0x3f1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x3f62
leaq -0xf0(%rbp), %rdi
callq 0x4c20
leaq 0x1d7cc(%rip), %rdi     # 0x21200
callq 0x4c90
movq %rax, %rsi
movq (%rsi), %rax
movq 0x8(%rax), %rax
leaq -0x118(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq *%rax
jmp 0x3a52
leaq -0x128(%rbp), %rdi
movq %rdi, -0x230(%rbp)
leaq -0xf0(%rbp), %rsi
movq %rsi, -0x218(%rbp)
leaq -0xf4(%rbp), %rdx
callq 0x4cb0
movq -0x230(%rbp), %rdi
leaq -0x118(%rbp), %rsi
movq %rsi, -0x228(%rbp)
callq 0x4cf0
movq -0x228(%rbp), %rdi
callq 0x4d60
movq -0x200(%rbp), %rsi
leaq -0x140(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x4d80
movq -0x220(%rbp), %rdi
movq -0x218(%rbp), %rsi
movss -0x1c(%rbp), %xmm0
callq 0x35e0
movq %rax, -0x210(%rbp)
jmp 0x3adb
leaq -0x140(%rbp), %rdi
callq 0x4db0
movq -0x210(%rbp), %rax
movq -0x200(%rbp), %rdi
movq %rax, -0x130(%rbp)
callq 0x4790
movq %rax, %rsi
movq (%rsi), %rax
movq 0x8(%rax), %rax
leaq -0x160(%rbp), %rdi
leaq -0x130(%rbp), %rdx
callq *%rax
jmp 0x3b1d
leaq -0x178(%rbp), %rdi
movq %rdi, -0x258(%rbp)
leaq -0x50(%rbp), %rsi
movq %rsi, -0x240(%rbp)
leaq -0xcc(%rbp), %rdx
movq %rdx, -0x248(%rbp)
leaq -0xd1(%rbp), %rcx
movq %rcx, -0x238(%rbp)
callq 0x4dd0
movq -0x258(%rbp), %rdi
leaq -0x160(%rbp), %rsi
movq %rsi, -0x250(%rbp)
callq 0x4e10
movq -0x250(%rbp), %rdi
callq 0x4e50
movq -0x248(%rbp), %rcx
movq -0x240(%rbp), %r8
movq -0x238(%rbp), %r9
movss -0xcc(%rbp), %xmm1
movss -0xd0(%rbp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xd0(%rbp)
leaq -0x1c0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x130(%rbp), %rdx
callq 0x4e70
jmp 0x3bc7
leaq -0xc8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x4ec0
jmp 0x3bdc
jmp 0x3bde
leaq -0xc8(%rbp), %rdi
callq 0x4910
movq %rax, %rcx
movb $0x1, %al
cmpq $0x8, %rcx
movb %al, -0x259(%rbp)
jae 0x3c2c
xorl %eax, %eax
testb $0x1, -0xd1(%rbp)
movb %al, -0x25a(%rbp)
je 0x3c20
leaq -0xc8(%rbp), %rdi
callq 0x48a0
xorb $-0x1, %al
movb %al, -0x25a(%rbp)
movb -0x25a(%rbp), %al
movb %al, -0x259(%rbp)
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x3c3b
jmp 0x3cce
movss -0xf4(%rbp), %xmm0
leaq -0x1e8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x36f0
jmp 0x3c58
leaq 0x1d5b1(%rip), %rdi     # 0x21210
leaq -0x1e8(%rbp), %rsi
callq 0x4f20
jmp 0x3c6d
leaq -0x1e8(%rbp), %rdi
callq 0x4f90
jmp 0x3bde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x3d06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x4db0
jmp 0x3d06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x3cfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x4f90
jmp 0x3cfa
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4fb0
jmp 0x3cdd
leaq -0x1c0(%rbp), %rdi
callq 0x5200
leaq -0xf0(%rbp), %rdi
callq 0x4bc0
jmp 0x39df
leaq -0x1c0(%rbp), %rdi
callq 0x5200
leaq -0xf0(%rbp), %rdi
callq 0x4bc0
jmp 0x3f56
movl -0x14(%rbp), %eax
movl $0xa, %ecx
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
jne 0x3f10
movq 0x1d2a5(%rip), %rdi     # 0x20fd8
leaq 0x1037d(%rip), %rsi     # 0x140b7
callq 0x31d0
movq %rax, -0x268(%rbp)
jmp 0x3d48
movl $0x30, %edi
callq 0x5220
movb %al, -0x269(%rbp)
jmp 0x3d5a
movq -0x268(%rbp), %rdi
movb -0x269(%rbp), %al
movb %al, -0x1e9(%rbp)
movzbl -0x1e9(%rbp), %esi
callq 0x32f0
movq %rax, -0x278(%rbp)
jmp 0x3d82
movl $0x2, %edi
callq 0x5240
movl %eax, -0x27c(%rbp)
jmp 0x3d94
movq -0x278(%rbp), %rdi
movl -0x27c(%rbp), %eax
movl %eax, -0x1f0(%rbp)
movl -0x1f0(%rbp), %esi
callq 0x3250
movq %rax, -0x288(%rbp)
jmp 0x3dbb
movq -0x288(%rbp), %rdi
movl -0x4(%rbp), %esi
callq 0x3280
movq %rax, -0x290(%rbp)
jmp 0x3dd3
movq -0x290(%rbp), %rdi
leaq 0x102d8(%rip), %rsi     # 0x140b9
callq 0x31d0
movq %rax, -0x298(%rbp)
jmp 0x3def
movq -0x298(%rbp), %rdi
leaq 0x102bf(%rip), %rsi     # 0x140bc
callq 0x31d0
movq %rax, -0x2a0(%rbp)
jmp 0x3e0b
movl $0x30, %edi
callq 0x5220
movb %al, -0x2a1(%rbp)
jmp 0x3e1d
movq -0x2a0(%rbp), %rdi
movb -0x2a1(%rbp), %al
movb %al, -0x1f1(%rbp)
movzbl -0x1f1(%rbp), %esi
callq 0x32f0
movq %rax, -0x2b0(%rbp)
jmp 0x3e45
movl $0x5, %edi
callq 0x5240
movl %eax, -0x2b4(%rbp)
jmp 0x3e57
movq -0x2b0(%rbp), %rdi
movl -0x2b4(%rbp), %eax
movl %eax, -0x1f8(%rbp)
movl -0x1f8(%rbp), %esi
callq 0x3250
movq %rax, -0x2c0(%rbp)
jmp 0x3e7e
movq -0x2c0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3280
movq %rax, -0x2c8(%rbp)
jmp 0x3e96
movq -0x2c8(%rbp), %rdi
leaq 0x1021b(%rip), %rsi     # 0x140bf
callq 0x31d0
movq %rax, -0x2d0(%rbp)
jmp 0x3eb2
movq -0x2d0(%rbp), %rdi
leaq 0x10201(%rip), %rsi     # 0x140c1
callq 0x31d0
movq %rax, -0x2d8(%rbp)
jmp 0x3ece
movq -0x2d8(%rbp), %rdi
movss -0xd0(%rbp), %xmm0
callq 0x3140
movq %rax, -0x2e0(%rbp)
jmp 0x3eeb
movq -0x2e0(%rbp), %rdi
movq 0x1d0bf(%rip), %rsi     # 0x20fb8
callq 0x3200
jmp 0x3f00
jmp 0x3f10
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x3f56
movl $0x0, -0xd8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5260
leaq -0x50(%rbp), %rdi
callq 0x4bc0
leaq -0x38(%rbp), %rdi
callq 0x4bc0
movl -0xd8(%rbp), %eax
testl %eax, %eax
je 0x3f46
jmp 0x3f44
jmp 0x3f76
jmp 0x3f48
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3910
leaq -0xc8(%rbp), %rdi
callq 0x5260
leaq -0x50(%rbp), %rdi
callq 0x4bc0
leaq -0x38(%rbp), %rdi
callq 0x4bc0
jmp 0x3f7f
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x3350
nopl (%rax,%rax)