// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movq %rcx, %r14
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movl %edi, %r15d
movq %rdi, 0x8(%rsp)
testl %edi, %edi
jle 0x26448
leaq (,%r15,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x6230
movq 0x8(%rsp), %r13
movl %r13d, %ebx
imull %ebx, %ebx
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x6680
movq %rax, %rbp
movq %rbx, %rdi
callq 0x6680
movq %rax, %rbx
testl %r13d, %r13d
jle 0x264db
leaq (,%r15,8), %rax
xorl %ecx, %ecx
movq %rbp, %rdx
movq %r12, %rsi
movq %rbx, %rdi
movq %rcx, %r8
imulq %r15, %r8
addq %rcx, %r8
xorl %r9d, %r9d
cmpq %r9, %rcx
jne 0x264ae
movsd (%r12,%r8,8), %xmm0
movsd %xmm0, (%rbp,%r8,8)
movq $0x0, (%rbx,%r8,8)
jmp 0x264c2
movq $0x0, (%rdx,%r9,8)
movsd (%rsi,%r9,8), %xmm0
movsd %xmm0, (%rdi,%r9,8)
incq %r9
cmpq %r9, %r15
jne 0x26492
incq %rcx
addq %rax, %rdi
addq %rax, %rsi
addq %rax, %rdx
cmpq %r15, %rcx
jne 0x26485
movq %r12, 0x30(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x26686
movq 0x8(%rsp), %rax
movslq %eax, %r13
shlq $0x3, %r13
incl %eax
movl %eax, 0x14(%rsp)
leaq (,%r15,8), %r12
movq $0x0, 0x18(%rsp)
movq %r13, 0x38(%rsp)
movq %r13, %rdi
callq 0x6680
movq %rax, %r13
xorpd %xmm0, %xmm0
cmpl $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rsi
jle 0x265b4
movq 0x30(%rsp), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd (%rax,%rdx,8), %xmm1
mulsd (%r14,%rdx,8), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %r15
jne 0x2653f
movsd %xmm0, (%r13,%rcx,8)
incq %rcx
addq %r12, %rax
cmpq %r15, %rcx
jne 0x26539
xorpd %xmm0, %xmm0
cmpl $0x0, 0x8(%rsp)
jle 0x265b4
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm1
subsd (%rsi,%rax,8), %xmm1
movsd %xmm1, (%r13,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x26575
cmpl $0x0, 0x8(%rsp)
jle 0x265b4
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r15
jne 0x2659d
ucomisd 0x3c7c(%rip), %xmm0     # 0x2a238
jb 0x265c4
sqrtsd %xmm0, %xmm0
jmp 0x265c9
callq 0x6340
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
callq 0x65a0
movsd 0x20(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
movq 0x38(%rsp), %r13
jbe 0x26686
movq %r13, %rdi
callq 0x6680
cmpl $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movl 0x14(%rsp), %r8d
jle 0x26668
movq %rbx, %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm1
mulsd (%r14,%rsi,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %r15
jne 0x26612
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
addq %r12, %rcx
cmpq %r15, %rdx
jne 0x2660c
cmpl $0x0, 0x8(%rsp)
jle 0x26668
xorl %ecx, %ecx
xorl %edx, %edx
movsd (%rdi,%rdx,8), %xmm0
subsd (%rax,%rdx,8), %xmm0
movslq %ecx, %rcx
divsd (%rbp,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
addl %r8d, %ecx
cmpq %rdx, %r15
jne 0x26644
movq %rax, %rdi
callq 0x65a0
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
cmpl 0x10(%rsp), %eax
jne 0x26513
movq %rbp, %rdi
callq 0x65a0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x65a0