// File: /workspace/github_cpp_makefile_star2_O3/integeruser[P]GA3C-cpp/src/ga3c.cc
// Function: extract_accumulated_experience(std::deque<std::tuple<std::vector<float, std::allocator<float>>, long, float, std::vector<float, std::allocator<float>>, bool>, std::allocator<std::tuple<std::vector<float, std::allocator<float>>, long, float, std::vector<float, std::allocator<float>>, bool>>>&, float)
training_experience_t extract_accumulated_experience(memory_t& memory, float next_state_value)
{
    assert(not memory.empty());
    const auto n = std::min<uint32_t>(N_STEP_RETURN, memory.size());

    // pick experience at time step t
    uint32_t t = 0;
    const auto curr_state = std::get<0>(memory[t]);
    const auto action = std::get<1>(memory[t]);

    // and calculate its n-step return (until time step t+n)
    float n_step_return = 0.0f;
    for (; t < n; ++t) {
        const auto reward = std::get<2>(memory[t]);
        n_step_return += std::pow(GAMMA, t)*reward;
    }
    const auto done = std::get<4>(memory[n-1]);
    if (not done) { n_step_return += std::pow(GAMMA, n)*next_state_value; }

    memory.pop_front();
    return training_experience_t{curr_state, action, n_step_return};
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq 0x10(%rsi), %rsi
movq 0x30(%r14), %rax
cmpq %rsi, %rax
je 0x2961
movq %rdi, %r15
movq 0x48(%r14), %rcx
movq 0x28(%r14), %rdi
movq %rcx, %rdx
subq %rdi, %rdx
shrq $0x3, %rdx
cmpq $0x1, %rcx
adcl $-0x1, %edx
movq 0x20(%r14), %rcx
leal (,%rdx,8), %r8d
subq 0x38(%r14), %rax
subl %edx, %r8d
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
subq %rsi, %rcx
shrq $0x3, %rcx
imull $0x38e38e39, %ecx, %ebp # imm = 0x38E38E39
addl %eax, %ebp
addl %r8d, %ebp
cmpl $0x8, %ebp
movl $0x8, %r13d
cmovbl %ebp, %r13d
movq %rsi, %r8
subq 0x18(%r14), %r8
movq %r8, %rcx
sarq $0x3, %rcx
movabsq $-0x71c71c71c71c71c7, %rbx # imm = 0x8E38E38E38E38E39
imulq %rbx, %rcx
cmpq $-0x47, %r8
jl 0x278e
cmpq $0x7, %rcx
jb 0x27bf
movabsq $0x2492492492492493, %rdx # imm = 0x2492492492492493
movq %rcx, %rax
mulq %rdx
subq %rdx, %rcx
shrq %rcx
addq %rdx, %rcx
shrq $0x2, %rcx
jmp 0x27b1
notq %rcx
movabsq $0x2492492492492493, %rdx # imm = 0x2492492492492493
movq %rcx, %rax
mulq %rdx
subq %rdx, %rcx
shrq %rcx
addq %rdx, %rcx
shrq $0x2, %rcx
notq %rcx
imulq $-0x1f8, %rcx, %rsi     # imm = 0xFE08
addq (%rdi,%rcx,8), %rsi
addq %r8, %rsi
movss %xmm0, 0xc(%rsp)
addq $0x30, %rsi
leaq 0x20(%rsp), %rdi
callq 0x3692
movq 0x10(%r14), %rax
movq 0x28(%r14), %rdi
movq %rax, %rsi
subq 0x18(%r14), %rsi
movq %rsi, %rcx
sarq $0x3, %rcx
imulq %rbx, %rcx
cmpq $-0x47, %rsi
jl 0x2818
cmpq $0x7, %rcx
jb 0x2849
movabsq $0x2492492492492493, %rdx # imm = 0x2492492492492493
movq %rcx, %rax
mulq %rdx
subq %rdx, %rcx
shrq %rcx
addq %rdx, %rcx
shrq $0x2, %rcx
jmp 0x283b
notq %rcx
movabsq $0x2492492492492493, %rdx # imm = 0x2492492492492493
movq %rcx, %rax
mulq %rdx
subq %rdx, %rcx
shrq %rcx
addq %rdx, %rcx
shrq $0x2, %rcx
notq %rcx
imulq $-0x1f8, %rcx, %rax     # imm = 0xFE08
addq (%rdi,%rcx,8), %rax
addq %rsi, %rax
movq %r15, 0x10(%rsp)
leaq 0x10(%r14), %r15
movq 0x28(%rax), %rbx
testl %ebp, %ebp
je 0x28bb
movl %r13d, %ebp
xorps %xmm0, %xmm0
xorl %r12d, %r12d
movss %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4a9c
movl %r12d, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
xorps %xmm0, %xmm0
cvtss2sd 0x20(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x3773(%rip), %xmm0     # 0x6008
callq 0x22a0
xorps %xmm1, %xmm1
cvtss2sd 0x8(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
incq %r12
cmpq %rbp, %r12
jb 0x2863
jmp 0x28be
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
leal -0x1(%r13), %esi
movq %r15, %rdi
callq 0x4a9c
cmpb $0x0, (%rax)
jne 0x290e
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
movsd 0x3723(%rip), %xmm0     # 0x6008
callq 0x22a0
xorps %xmm1, %xmm1
cvtss2sd 0xc(%rsp), %xmm1
cvtss2sd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x10(%rsp), %r15
movq %r14, %rdi
callq 0x3712
movss 0x8(%rsp), %xmm0
movss %xmm0, (%r15)
movq %rbx, 0x8(%r15)
leaq 0x10(%r15), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3692
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x294f
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x36f8(%rip), %rdi      # 0x6060
leaq 0x3704(%rip), %rsi      # 0x6073
leaq 0x3709(%rip), %rcx      # 0x607f
movl $0x3d, %edx
callq 0x20d0
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x299a
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22b0