// File: /workspace/github_cpp_makefile_star2_O2/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, 0x30(%rsp)
movl %r8d, 0x4(%rsp)
movq %rdx, %r15
movq %rsi, %r13
xorl %eax, %eax
testl %edi, %edi
movl $0x0, %r12d
movq %rdi, 0x8(%rsp)
cmovgl %edi, %r12d
cmpq %rax, %r12
je 0x1c7ba
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x1c7ab
movq %rcx, 0x10(%rsp)
movq 0x8(%rsp), %r14
movl %r14d, %ebx
imull %ebx, %ebx
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x66b0
movq %rax, %rbp
movq %rbx, %rdi
callq 0x66b0
movq %rax, %rbx
movslq %r14d, %rax
movl %r14d, %ecx
leaq (,%rax,8), %rdi
xorl %edx, %edx
movq %rbx, %rsi
movq %r13, %r11
movq %rbp, %r8
cmpq %r12, %rdx
je 0x1c852
movq %rdx, %r9
imulq %rax, %r9
addq %rdx, %r9
xorl %r10d, %r10d
cmpq %r10, %rcx
je 0x1c844
cmpq %r10, %rdx
jne 0x1c82e
movsd (%r13,%r9,8), %xmm0
movsd %xmm0, (%rbp,%r9,8)
andq $0x0, (%rbx,%r9,8)
jmp 0x1c83f
andq $0x0, (%r8,%r10,8)
movsd (%r11,%r10,8), %xmm0
movsd %xmm0, (%rsi,%r10,8)
incq %r10
jmp 0x1c80f
incq %rdx
addq %rdi, %r8
addq %rdi, %r11
addq %rdi, %rsi
jmp 0x1c7fd
xorl %ecx, %ecx
movl 0x4(%rsp), %r14d
testl %r14d, %r14d
cmovlel %ecx, %r14d
movl %r14d, 0x4(%rsp)
movq 0x8(%rsp), %rax
leal 0x1(%rax), %r14d
movq %r13, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
cmpl 0x4(%rsp), %ecx
je 0x1c983
movq %rcx, 0x38(%rsp)
callq 0x66b0
movq %r13, %rsi
movq %rax, %r13
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq %rax, %rcx
callq 0x1c6d4
xorl %eax, %eax
cmpq %rax, %r12
je 0x1c8c4
movsd (%r13,%rax,8), %xmm0
subsd (%r15,%rax,8), %xmm0
movsd %xmm0, (%r13,%rax,8)
incq %rax
jmp 0x1c8a6
pushq $0x1
popq %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq %r13, %rcx
leaq 0x40(%rsp), %r8
callq 0x1c725
movsd 0x40(%rsp), %xmm0
ucomisd 0x391e(%rip), %xmm0     # 0x20208
jb 0x1c8f2
sqrtsd %xmm0, %xmm0
jmp 0x1c8f7
callq 0x6350
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
callq 0x65c0
movsd 0x18(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x1c983
movq 0x20(%rsp), %rdi
callq 0x66b0
movq %rax, %r13
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq %rax, %rcx
callq 0x1c6d4
movq 0x10(%rsp), %rdx
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x1c965
movsd (%r15,%rcx,8), %xmm0
subsd (%r13,%rcx,8), %xmm0
cltq
divsd (%rbp,%rax,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
addl %r14d, %eax
jmp 0x1c93e
movq %r13, %rdi
callq 0x65c0
movq 0x38(%rsp), %rcx
incl %ecx
movq 0x28(%rsp), %r13
movq 0x20(%rsp), %rdi
jmp 0x1c878
movq %rbp, %rdi
callq 0x65c0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x65c0
nopl (%rax)