// File: 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
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpl $-0x1, 0x8(%rax)
jg 0xb44c
movl $0x10, %edi
callq 0x20c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
leaq 0x3e2b(%rip), %rsi      # 0xf239
callq 0x22c0
jmp 0xb415
movq -0xd0(%rbp), %rdi
movq 0x9bbd(%rip), %rsi      # 0x14fe0
movq 0x9ba6(%rip), %rdx      # 0x14fd0
callq 0x2240
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x2150
jmp 0xb98a
leaq -0x40(%rbp), %rdi
callq 0x52f0
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0xe0(%rbp)
movq %rsp, %rcx
movq %rcx, -0x48(%rbp)
leaq (%rax,%rax,2), %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd8(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0xb4ee
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xf0(%rbp)
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x52f0
movq -0xf8(%rbp), %rax
movq -0xf0(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0xe8(%rbp)
jne 0xb4bd
movl $0x0, -0x54(%rbp)
movq -0xc8(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0xb554
movq -0xd8(%rbp), %rax
movq -0xc8(%rbp), %rcx
movq (%rcx), %rdi
movslq -0x54(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, %rdx
shlq $0x4, %rdx
addq %rdx, %rdi
leaq (%rax,%rcx,8), %rsi
leaq -0x40(%rbp), %rdx
callq 0x5150
jmp 0xb536
jmp 0xb538
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xb4f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xb92c
movq -0xd8(%rbp), %rdi
callq 0x5bb0
movl %eax, -0x58(%rbp)
movl $0x1, -0x5c(%rbp)
movq -0xc8(%rbp), %rcx
movl -0x5c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0xb5c0
movq -0xd8(%rbp), %rdi
movslq -0x5c(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x5bb0
movslq -0x58(%rbp), %rcx
cmpq %rcx, %rax
jbe 0xb5b3
movq -0xd8(%rbp), %rdi
movslq -0x5c(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x5bb0
movl %eax, -0x58(%rbp)
jmp 0xb5b5
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xb56a
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x58(%rbp), %edx
leaq -0x10(%rbp), %rdi
callq 0x2a50
jmp 0xb5d8
movl $0x0, -0x60(%rbp)
movq -0xc8(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0xb8ba
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0xb654
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x64(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x64(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xb5f9
leaq -0x80(%rbp), %rdi
callq 0x52f0
movq -0xd8(%rbp), %rax
movslq -0x60(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x5390
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x53c0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x53f0
testb $0x1, %al
jne 0xb6b8
jmp 0xb761
leaq -0x90(%rbp), %rdi
callq 0x5430
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movslq -0xa0(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x5bb0
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0xb74e
movslq -0xa0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x6c50
movl (%rax), %eax
cmpl -0x9c(%rbp), %eax
jne 0xb73b
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x5470
jmp 0xb728
jmp 0xb74e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xb8af
jmp 0xb73d
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0xb6d6
jmp 0xb750
leaq -0x90(%rbp), %rdi
callq 0x5450
jmp 0xb69c
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movslq -0x60(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x108(%rbp)
leaq 0x3aae(%rip), %rdi      # 0xf236
callq 0x2050
movq -0x108(%rbp), %rsi
movaps %xmm0, %xmm1
leaq -0xb8(%rbp), %rdi
movl $0x7, %edx
movsd 0x3a45(%rip), %xmm0     # 0xf1f0
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x62c0
jmp 0xb7bb
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x74d0
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0xb88c
movslq -0xbc(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x6c70
movsd (%rax), %xmm0
movsd %xmm0, -0x118(%rbp)
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
movq %rax, -0x120(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x6c50
movq -0x120(%rbp), %rdi
movsd -0x118(%rbp), %xmm0
movslq (%rax), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x9250
jmp 0xb85c
jmp 0xb85e
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xb7c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x6bd0
jmp 0xb8af
leaq -0xb8(%rbp), %rdi
callq 0x6bd0
leaq -0x80(%rbp), %rdi
callq 0x5310
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xb5df
leaq -0x80(%rbp), %rdi
callq 0x5310
jmp 0xb92c
movq -0xd8(%rbp), %rcx
movq -0xe0(%rbp), %rax
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x128(%rbp)
je 0xb90f
movq -0x128(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x130(%rbp)
callq 0x5310
movq -0xd8(%rbp), %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x128(%rbp)
jne 0xb8de
movq -0x48(%rbp), %rax
movq %rax, %rsp
leaq -0x40(%rbp), %rdi
callq 0x5310
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
movq %rbp, %rsp
popq %rbp
retq
movq -0xd8(%rbp), %rcx
movq -0xe0(%rbp), %rax
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x138(%rbp)
je 0xb981
movq -0x138(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x140(%rbp)
callq 0x5310
movq -0xd8(%rbp), %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x138(%rbp)
jne 0xb950
leaq -0x40(%rbp), %rdi
callq 0x5310
movq -0x20(%rbp), %rdi
callq 0x2280
nopw %cs:(%rax,%rax)
nopl (%rax)