// File: /workspace/github_cpp_makefile_star2_O1/WeihuaZhu[P]hpc_prog2/jacobi.cpp
// Function: jacobi(int, double*, double*, double*, int, double)
void jacobi(const int n, double* A, double* b, double* x, int max_iter, double l2_termination)
{
    for (int i = 0; i < n; i++) x[i] = 0;

    double* D = (double *) malloc(sizeof(double)*(n*n));
    double* R = (double *) malloc(sizeof(double)*(n*n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) {
                D[i*n+j] = A[i*n+j];
                R[i*n+j] = 0;
            } else {
                D[i*n+j] = 0;
                R[i*n+j] = A[i*n+j];
            }
        }
    }

    for (int iter = 0; iter < max_iter; iter++) {
        //calculate 2 norm
        double* sol = (double *) malloc(sizeof(double)*n);
        matrix_vector_mult(n, A, x, sol);
        for (int i = 0; i < n; i++) {
            sol[i] -= b[i];
        }
        double sol2;
        matrix_vector_mult(1, n, sol, sol, &sol2);
        double _2norm = sqrt(sol2);
        free(sol);

        if (_2norm > l2_termination) {
            double* b_Rx = (double *) malloc(sizeof(double)*n);
            matrix_vector_mult(n, R, x, b_Rx);
            for(int i = 0; i < n; i++){
                x[i] = (b[i] - b_Rx[i])/D[i*n+i];
            }
            free(b_Rx);
        } else {
            break;
        }
    }

    free(D);
    free(R);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x40(%rsp)
movl %r8d, 0x4(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x18(%rsp)
movl %edi, %r13d
testl %edi, %edi
movq %rdi, 0x8(%rsp)
jle 0x26a4b
leaq (,%r13,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x6230
movq 0x8(%rsp), %rdi
movl %edi, %ebx
imull %ebx, %ebx
shlq $0x3, %rbx
movq %rdi, %rbp
movq %rbx, %rdi
callq 0x6680
movq %rbx, %rdi
movq %rax, %rbx
callq 0x6680
movq %rax, %r12
testl %ebp, %ebp
jle 0x26adb
leaq (,%r13,8), %rax
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movq %rbx, %rsi
xorl %edi, %edi
movq %rdi, %r8
imulq %r13, %r8
xorl %r9d, %r9d
cmpq %r9, %rdi
jne 0x26aaa
leaq (%rcx,%r9), %r10
movsd (%rdx,%r9,8), %xmm0
movsd %xmm0, (%rsi,%r9,8)
xorpd %xmm0, %xmm0
jmp 0x26abc
leaq (%r9,%r8), %r10
movq $0x0, (%rsi,%r9,8)
movsd (%rdx,%r9,8), %xmm0
movsd %xmm0, (%r12,%r10,8)
incq %r9
cmpq %r9, %r13
jne 0x26a8f
incq %rdi
addq %r13, %rcx
addq %rax, %rsi
addq %rax, %rdx
cmpq %r13, %rdi
jne 0x26a85
cmpl $0x0, 0x4(%rsp)
jle 0x26c76
movq 0x8(%rsp), %rax
movslq %eax, %rcx
shlq $0x3, %rcx
movq %rcx, 0x30(%rsp)
incl %eax
movl %eax, 0x14(%rsp)
leaq (,%r13,8), %rbp
movq $0x0, 0x20(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x30(%rsp), %rdi
callq 0x6680
movq %rax, %rbx
movq 0x8(%rsp), %rsi
testl %esi, %esi
jle 0x26b65
movq 0x18(%rsp), %rax
xorl %ecx, %ecx
movq $0x0, (%rbx,%rcx,8)
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd (%rax,%rdx,8), %xmm1
mulsd (%r14,%rdx,8), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %r13
jne 0x26b3e
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
addq %rbp, %rax
cmpq %r13, %rcx
jne 0x26b30
testl %esi, %esi
jle 0x26b83
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm0
subsd (%r15,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x26b6b
xorpd %xmm0, %xmm0
testl %esi, %esi
jle 0x26ba2
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x26b8d
ucomisd 0x368e(%rip), %xmm0     # 0x2a238
jb 0x26bb2
sqrtsd %xmm0, %xmm0
jmp 0x26bb7
callq 0x6340
movsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
callq 0x65a0
movsd 0x28(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
movq 0x38(%rsp), %rbx
jbe 0x26c76
movq 0x30(%rsp), %rdi
callq 0x6680
movq 0x8(%rsp), %rdi
testl %edi, %edi
jle 0x26c29
movq %r12, %rcx
xorl %edx, %edx
movq $0x0, (%rax,%rdx,8)
xorpd %xmm0, %xmm0
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm1
mulsd (%r14,%rsi,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %r13
jne 0x26c02
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
addq %rbp, %rcx
cmpq %r13, %rdx
jne 0x26bf4
testl %edi, %edi
movl 0x14(%rsp), %esi
jle 0x26c58
xorl %ecx, %ecx
xorl %edx, %edx
movsd (%r15,%rdx,8), %xmm0
subsd (%rax,%rdx,8), %xmm0
movslq %ecx, %rcx
divsd (%rbx,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
addl %esi, %ecx
cmpq %rdx, %r13
jne 0x26c35
movq %rax, %rdi
callq 0x65a0
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
cmpl 0x4(%rsp), %eax
jne 0x26b13
movq %rbx, %rdi
callq 0x65a0
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x65a0