// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movl 0x8(%rdi), %r12d
testl %r12d, %r12d
js 0x7c33
movq %rdi, %rbx
movq %rsp, -0xb8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movl %r12d, %eax
movq %rsp, %r13
movq %rax, -0x68(%rbp)
leaq (%rax,%rax,2), %rax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r13
movq %r13, %rsp
testl %r12d, %r12d
je 0x797c
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
movq %rbx, -0x40(%rbp)
movl %r12d, -0x2c(%rbp)
jle 0x7a09
movq %rbx, %r15
xorl %ebx, %ebx
movq %r13, %r12
xorl %r14d, %r14d
movq (%r15), %rdi
addq %rbx, %rdi
movq %r12, %rsi
leaq -0xb0(%rbp), %rdx
callq 0x4514
incq %r14
movslq 0x8(%r15), %rsi
addq $0x18, %r12
addq $0x30, %rbx
cmpq %rsi, %r14
jl 0x7996
movq 0x8(%r13), %r15
subq (%r13), %r15
shrq $0x2, %r15
cmpl $0x2, %esi
jl 0x7a17
leaq (%rsi,%rsi,2), %rax
leaq -0x18(,%rax,8), %rax
xorl %ecx, %ecx
movl -0x2c(%rbp), %r12d
movq -0x40(%rbp), %rbx
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 0x79e6
jmp 0x7a1f
movq 0x8(%r13), %r15
subq (%r13), %r15
shrq $0x2, %r15
jmp 0x7a1f
movl -0x2c(%rbp), %r12d
movq -0x40(%rbp), %rbx
movq %r13, -0x38(%rbp)
leaq -0x90(%rbp), %rdi
movl %r15d, %edx
callq 0x2a5c
cmpl $0x0, 0x8(%rbx)
jle 0x7bd4
movl %r15d, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
xorl %r14d, %r14d
movq %r15, -0xc0(%rbp)
testl %r15d, %r15d
jle 0x7a83
movq -0x90(%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 0x7a6a
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
leaq (%r14,%r14,2), %r12
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %r15
movq 0x8(%rax,%r12,8), %rbx
cmpq %rbx, %r15
je 0x7b0e
movl (%r15), %eax
movq -0xb0(%rbp), %rcx
movq -0xa8(%rbp), %rdx
movl $0x0, -0x80(%rbp)
subq %rcx, %rdx
je 0x7b08
sarq $0x2, %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
xorl %edi, %edi
cmpl %eax, (%rcx,%rdi,4)
je 0x7ae5
incq %rdi
movl %edi, -0x80(%rbp)
cmpq %rdi, %rdx
jne 0x7ad3
jmp 0x7b08
movq -0x58(%rbp), %rsi
cmpq -0x50(%rbp), %rsi
je 0x7afb
movl %edi, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x58(%rbp)
jmp 0x7b08
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0x4696
addq $0x4, %r15
jmp 0x7aa3
shlq $0x4, %r12
movq -0x40(%rbp), %rax
addq (%rax), %r12
leaq 0x27b2(%rip), %rdi      # 0xa2d2
callq 0x2050
movaps %xmm0, %xmm1
leaq -0x80(%rbp), %rdi
movq %r12, %rsi
movl $0x7, %edx
movsd 0x26fc(%rip), %xmm0     # 0xa238
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x4a5e
movq -0x80(%rbp), %rdi
cmpq %rdi, -0x78(%rbp)
movl -0x2c(%rbp), %r12d
je 0x7ba1
movq %r14, %rbx
shlq $0x4, %rbx
xorl %r15d, %r15d
movsd (%rdi,%r15,8), %xmm0
movq -0x90(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq (%rcx,%r15,4), %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq (%rax,%rbx), %rdi
callq 0x6104
incq %r15
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpq %r15, %rax
ja 0x7b64
testq %rdi, %rdi
movq -0x40(%rbp), %rbx
je 0x7baf
callq 0x2170
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
movq -0xc0(%rbp), %r15
je 0x7bc4
callq 0x2170
incq %r14
movslq 0x8(%rbx), %rax
cmpq %rax, %r14
jl 0x7a51
testl %r12d, %r12d
movq -0x38(%rbp), %r14
je 0x7bfe
movq -0x68(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq -0x18(%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x7bf8
callq 0x2170
addq $-0x18, %rbx
jne 0x7be9
movq -0xb8(%rbp), %rsp
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x7c16
callq 0x2170
movq -0x90(%rbp), %rax
movq -0x88(%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 0x26a9(%rip), %rsi      # 0xa2f0
movq %rax, %rdi
callq 0x2290
movq 0x738a(%rip), %rsi      # 0xefe0
movq 0x7373(%rip), %rdx      # 0xefd0
movq %rbx, %rdi
callq 0x2210
movq %rax, %r14
movq %rbx, %rdi
callq 0x2130
jmp 0x7ced
movq %rax, %r14
movq -0x38(%rbp), %r13
jmp 0x7cb6
jmp 0x7c86
movq %rax, %r14
movl -0x2c(%rbp), %r12d
jmp 0x7cb6
movq %rax, %r14
movq -0x38(%rbp), %r13
movl -0x2c(%rbp), %r12d
jmp 0x7ca8
movq %rax, %r14
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x7ca4
callq 0x2170
movq -0x38(%rbp), %r13
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x7cb6
callq 0x2170
testl %r12d, %r12d
je 0x7cdc
movq -0x68(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq -0x18(%r13,%rbx), %rdi
testq %rdi, %rdi
je 0x7cd6
callq 0x2170
addq $-0x18, %rbx
jne 0x7cc7
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x7ced
callq 0x2170
movq %r14, %rdi
callq 0x2250
nopl (%rax)