// File: /workspace/github_cpp_makefile_star2_O1/Eshaancoding[P]AutoDiff/src/Vector/grad.cpp
// Function: Vector::grad()
Matrix Vector::grad () {
    if (this->width <= -1) throw invalid_argument("Vector not initialized ");
    vector<int> input_map;
    vector<int> IDs[this->width];
    for (int i = 0; i < this->width; i++) {
        // find unique ID;
        this->array[i].find_unique_id(&IDs[i], &input_map); 
    }
    // get maximum size of variables
    int max_size = IDs[0].size();
    for (int i = 1; i < this->width; i++) {
        if (IDs[i].size() > max_size) {
            max_size = IDs[i].size();
        }
    }
    // initalize grad
    Matrix grad = Matrix(this->width, max_size);
    
    for (int i = 0; i < this->width; i++) {
        // reset grad
        for (int x = 0; x < max_size; x++) {
            grad.array[i].array[x].operation = op::argument;
            grad.array[i].array[x].value = 0;
        }
        vector<int> indexs; 
        for (auto x : IDs[i]) {
            for (int f = 0; f < input_map.size(); f++) {
                if (input_map[f] == x) {    
                    indexs.push_back(f);
                    break;
                }
            }
        }  
        // get grad
        vector<double> grad_i = this->array[i].grad();
        // assign grad
        for (int x = 0; x < grad_i.size(); x++) {
            grad.array[i].array[indexs[x]] = grad_i[x];
        }
    }
    return grad;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl 0x8(%rdi), %r12d
testl %r12d, %r12d
js 0x7c78
movq %rdi, %rbx
movq %rsp, -0xa8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
movl %r12d, %eax
movq %rsp, %r13
movq %rax, -0x58(%rbp)
leaq (%rax,%rax,2), %rax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r13
movq %r13, %rsp
testl %r12d, %r12d
je 0x79dc
leaq -0x18(,%rax,8), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x20f0
movl 0x8(%rbx), %esi
testl %esi, %esi
movl %r12d, -0x2c(%rbp)
jle 0x7a22
movq %rbx, %r15
xorl %ebx, %ebx
movq %r13, %r12
xorl %r14d, %r14d
movq (%r15), %rdi
addq %rbx, %rdi
movq %r12, %rsi
leaq -0xa0(%rbp), %rdx
callq 0x454c
incq %r14
movslq 0x8(%r15), %rsi
addq $0x18, %r12
addq $0x30, %rbx
cmpq %rsi, %r14
jl 0x79f2
movl -0x2c(%rbp), %r12d
movq %r15, %rbx
movq 0x8(%r13), %r15
subq (%r13), %r15
shrq $0x2, %r15
cmpl $0x2, %esi
jl 0x7a64
movl %esi, %eax
leaq (%rax,%rax,2), %rax
leaq -0x18(,%rax,8), %rax
xorl %ecx, %ecx
movq 0x20(%r13,%rcx), %rdx
subq 0x18(%r13,%rcx), %rdx
sarq $0x2, %rdx
movslq %r15d, %r15
cmpq %r15, %rdx
cmoval %edx, %r15d
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x7a43
movq %r13, -0x38(%rbp)
leaq -0x88(%rbp), %rdi
movl %r15d, %edx
callq 0x2a5c
cmpl $0x0, 0x8(%rbx)
jle 0x7c1c
movl %r15d, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
xorl %r14d, %r14d
movq %rbx, -0x60(%rbp)
movq %r15, -0xb0(%rbp)
testl %r15d, %r15d
jle 0x7acc
movq -0x88(%rbp), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
xorl %ecx, %ecx
movl $0x1, 0x8(%rax,%rcx)
movq $0x0, (%rax,%rcx)
addq $0x30, %rcx
cmpq %rcx, %r13
jne 0x7ab3
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq (%r14,%r14,2), %r12
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %rbx
movq 0x8(%rax,%r12,8), %r15
cmpq %r15, %rbx
je 0x7b56
movl (%rbx), %eax
movq -0xa0(%rbp), %rcx
movq -0x98(%rbp), %rdx
movl $0x0, -0x78(%rbp)
subq %rcx, %rdx
je 0x7b50
sarq $0x2, %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
xorl %edi, %edi
cmpl %eax, (%rcx,%rdi,4)
je 0x7b2d
incq %rdi
movl %edi, -0x78(%rbp)
cmpq %rdi, %rdx
jne 0x7b1b
jmp 0x7b50
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x7b43
movl %edi, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x48(%rbp)
jmp 0x7b50
leaq -0x50(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x46fe
addq $0x4, %rbx
jmp 0x7aec
shlq $0x4, %r12
movq -0x60(%rbp), %rax
addq (%rax), %r12
leaq 0x276a(%rip), %rdi      # 0xa2d2
callq 0x2050
movaps %xmm0, %xmm1
leaq -0x78(%rbp), %rdi
movq %r12, %rsi
movl $0x7, %edx
movsd 0x26b4(%rip), %xmm0     # 0xa238
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x4ac6
movq -0x78(%rbp), %rdi
cmpq %rdi, -0x70(%rbp)
movl -0x2c(%rbp), %r12d
je 0x7be9
movq %r14, %rbx
shlq $0x4, %rbx
xorl %r15d, %r15d
movsd (%rdi,%r15,8), %xmm0
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx,%r15,4), %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq (%rax,%rbx), %rdi
callq 0x6164
incq %r15
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpq %r15, %rax
ja 0x7bac
testq %rdi, %rdi
movq -0x60(%rbp), %rbx
je 0x7bf7
callq 0x2170
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
movq -0xb0(%rbp), %r15
je 0x7c0c
callq 0x2170
incq %r14
movslq 0x8(%rbx), %rax
cmpq %rax, %r14
jl 0x7a9a
testl %r12d, %r12d
movq -0x38(%rbp), %r14
je 0x7c46
movq -0x58(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq -0x18(%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x7c40
callq 0x2170
addq $-0x18, %rbx
jne 0x7c31
movq -0xa8(%rbp), %rsp
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x7c5e
callq 0x2170
movq -0x88(%rbp), %rax
movq -0x80(%rbp), %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20c0
movq %rax, %rbx
leaq 0x2664(%rip), %rsi      # 0xa2f0
movq %rax, %rdi
callq 0x2290
movq 0x7345(%rip), %rsi      # 0xefe0
movq 0x732e(%rip), %rdx      # 0xefd0
movq %rbx, %rdi
callq 0x2210
movq %rax, %r14
movq %rbx, %rdi
callq 0x2130
jmp 0x7d32
movq %rax, %r14
movq -0x38(%rbp), %r13
jmp 0x7cfb
jmp 0x7ccb
movq %rax, %r14
movl -0x2c(%rbp), %r12d
jmp 0x7cfb
movq %rax, %r14
movq -0x38(%rbp), %r13
movl -0x2c(%rbp), %r12d
jmp 0x7ced
movq %rax, %r14
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x7ce9
callq 0x2170
movq -0x38(%rbp), %r13
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x7cfb
callq 0x2170
testl %r12d, %r12d
je 0x7d21
movq -0x58(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq -0x18(%r13,%rbx), %rdi
testq %rdi, %rdi
je 0x7d1b
callq 0x2170
addq $-0x18, %rbx
jne 0x7d0c
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x7d32
callq 0x2170
movq %r14, %rdi
callq 0x2250
nop