// File: /workspace/github_cpp_makefile_star2_O2/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), %ecx
testl %ecx, %ecx
js 0x7630
movq %rdi, -0x38(%rbp)
movq %rsp, -0xb8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
movl %ecx, %eax
movq %rsp, %rbx
movq %rax, -0xa8(%rbp)
imulq $0x18, %rax, %r14
leaq 0xf(%r14), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl %ecx, -0x2c(%rbp)
testl %ecx, %ecx
je 0x740e
leaq -0x18(%r14), %rcx
pushq $0x18
popq %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
subq %rdx, %rcx
addq $0x18, %rcx
movq %rbx, %rdi
xorl %esi, %esi
movq %rcx, %rdx
callq 0x20f0
movq %r14, -0xb0(%rbp)
xorl %r14d, %r14d
leaq -0x70(%rbp), %r15
movq %rbx, %r12
xorl %r13d, %r13d
movq -0x38(%rbp), %rdx
movslq 0x8(%rdx), %rsi
cmpq %rsi, %r13
jge 0x744d
movq (%rdx), %rdi
addq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3fa4
incq %r13
addq $0x18, %r12
addq $0x30, %r14
jmp 0x7422
movq 0x8(%rbx), %r15
subq (%rbx), %r15
shrq $0x2, %r15
leaq 0x20(%rbx), %rax
pushq $0x1
popq %rcx
movq %rdx, %r12
cmpq %rsi, %rcx
jge 0x7488
movq (%rax), %rdx
subq -0x8(%rax), %rdx
sarq $0x2, %rdx
movslq %r15d, %rdi
cmpq %rdi, %rdx
cmovbel %r15d, %edx
incq %rcx
addq $0x18, %rax
movl %edx, %r15d
jmp 0x7462
leaq -0x50(%rbp), %rdi
movl %r15d, %edx
callq 0x285c
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
imulq $0x30, %r15, %r14
xorl %esi, %esi
movslq 0x8(%r12), %rax
cmpq %rax, %rsi
jge 0x75e9
movq %rsi, %r13
shlq $0x4, %r13
movq -0x50(%rbp), %rax
addq %r13, %rax
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x74dc
movq (%rax), %rdx
movl $0x1, 0x8(%rdx,%rcx)
andq $0x0, (%rdx,%rcx)
addq $0x30, %rcx
jmp 0x74c1
andq $0x0, -0x90(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq %rsi, -0x40(%rbp)
imulq $0x18, %rsi, %rax
movq (%rbx,%rax), %r15
movq 0x8(%rbx,%rax), %r12
cmpq %r12, %r15
je 0x7548
movl (%r15), %eax
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
xorl %esi, %esi
movl %esi, -0x88(%rbp)
cmpq %rsi, %rdx
je 0x7542
leaq 0x1(%rsi), %rdi
cmpl %eax, (%rcx,%rsi,4)
movq %rdi, %rsi
jne 0x7518
leaq -0xa0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x4048
addq $0x4, %r15
jmp 0x74ff
imulq $0x30, -0x40(%rbp), %r15
movq -0x38(%rbp), %r12
addq (%r12), %r15
leaq 0x2d56(%rip), %rdi      # 0xa2b2
callq 0x2040
movaps %xmm0, %xmm1
leaq -0x88(%rbp), %rdi
movq %r15, %rsi
pushq $0x7
popq %rdx
movsd 0x2c97(%rip), %xmm0     # 0xa210
pushq $0x1
popq %rcx
movl %ecx, %r8d
callq 0x4424
xorl %r15d, %r15d
movq -0x88(%rbp), %rax
movq -0x80(%rbp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x75c5
movsd (%rax,%r15,8), %xmm0
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movslq (%rcx,%r15,4), %rcx
imulq $0x30, %rcx, %rdi
addq (%rax,%r13), %rdi
callq 0x5b68
incq %r15
jmp 0x7587
leaq -0x88(%rbp), %rdi
callq 0x4a4a
leaq -0xa0(%rbp), %rdi
callq 0x406e
movq -0x40(%rbp), %rsi
incq %rsi
jmp 0x74a3
cmpl $0x0, -0x2c(%rbp)
movq -0xb0(%rbp), %r14
je 0x7609
addq $-0x18, %rbx
leaq (%rbx,%r14), %rdi
callq 0x406e
addq $-0x18, %r14
jne 0x75fa
movq -0xb8(%rbp), %rsp
leaq -0x70(%rbp), %rdi
callq 0x406e
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20c0
movq %rax, %rbx
leaq 0x2c8e(%rip), %rsi      # 0xa2d0
movq %rax, %rdi
callq 0x2260
movq 0x798f(%rip), %rsi      # 0xefe0
movq 0x7978(%rip), %rdx      # 0xefd0
movq %rbx, %rdi
callq 0x21f0
movq %rax, %r14
movq %rbx, %rdi
callq 0x2110
jmp 0x76c0
jmp 0x7671
jmp 0x7676
movq %rax, %r14
jmp 0x7696
movq %rax, %r14
jmp 0x768a
movq %rax, %r14
leaq -0x88(%rbp), %rdi
callq 0x4a4a
leaq -0xa0(%rbp), %rdi
callq 0x406e
cmpl $0x0, -0x2c(%rbp)
je 0x76b7
addq $-0x18, %rbx
imulq $0x18, -0xa8(%rbp), %r15
leaq (%rbx,%r15), %rdi
callq 0x406e
addq $-0x18, %r15
jne 0x76a8
leaq -0x70(%rbp), %rdi
callq 0x406e
movq %r14, %rdi
callq 0x2230