// File: /workspace/github_cpp_makefile_star2_O1/aronszanto[P]strassen/main.cpp
// Function: strassenMult(Matrix*, Matrix*, Matrix*, int, int, int, int, int, int, int)
void strassenMult(Matrix* A, Matrix* B, Matrix* C, int topA, int leftA, int topB, int leftB, int topC, int leftC, int dimension) {
    
    // C12 = A21 - A11
    subtract(A, A, C, topA + dimension/2, leftA, topA, leftA, topC, leftC + dimension/2, dimension/2);
    // C21 = B11 + B12
    add(B,B,C,topB,leftB,topB,leftB + dimension/2,topC + dimension/2,leftC,dimension/2);
    // C22 = C12 * C21

    multiply(C,C,C,topC,leftC + dimension/2,topC + dimension/2,leftC,topC + dimension/2,leftC + dimension/2,dimension/2);

    //C12 = A12 - A22
    subtract(A,A,C,topA,leftA + dimension/2,topA + dimension/2,leftA + dimension/2,topC,leftC + dimension/2,dimension/2);
    //C21 = B21 + B22
    add(B, B, C, topB + dimension/2,leftB,topB + dimension/2,leftB + dimension/2,topC + dimension/2,leftC,dimension/2);
    //C11 = C12 * C21

    multiply(C,C,C,topC,leftC + dimension/2,topC + dimension/2,leftC,topC,leftC,dimension/2);

    //C12 = A11 + A22
    add(A, A, C, topA, leftA, topA + dimension/2, leftA + dimension/2, topC, leftC + dimension/2, dimension/2);
    //C21 = B11 + B22
    add(B,B,C,topB,leftB,topB + dimension/2,leftB + dimension/2,topC + dimension/2,leftC,dimension/2);
    
    Matrix* T1 = new Matrix();
    initMatrix(T1, dimension/2); // TODO deal with non-power of 2 case
    
    
    //T1 = C12*C21
    multiply(C,C,T1,topC,leftC + dimension/2,topC + dimension/2,leftC,0,0,dimension/2);
    //C11 = T1 + C11
    add(T1,C,C,0,0,topC,leftC,topC,leftC,dimension/2);
    //C22 = T1 + C22
    add(T1,C,C,0,0,topC + dimension/2,leftC + dimension/2,topC + dimension/2,leftC + dimension/2,dimension/2);
    
    Matrix* T2 = new Matrix();
    initMatrix(T2, dimension/2); // TODO deal with non-power of 2 case
    //T2 = A21 + A22
    add(A,A,T2,topA + dimension/2,leftA,topA + dimension/2,leftA + dimension/2,0,0,dimension/2);
    //C21 = T2 * B11

    multiply(T2,B,C,0,0,topB,leftB,topC + dimension/2,leftC,dimension/2);

    //C22 = C22 - C21
    subtract(C,C,C,topC + dimension/2,leftC + dimension/2,topC + dimension/2,leftC,topC + dimension/2,leftC + dimension/2,dimension/2);
    //T1 = B21 - B11
    subtract(B,B,T1,topB + dimension/2,leftB,topB,leftB,0,0,dimension/2);
    //T2 = A22 * T1

    multiply(A,T1,T2,topA + dimension/2,leftA + dimension/2,0,0,0,0,dimension/2);

    //C21 = C21 + T2
    add(C,T2,C,topC + dimension/2,leftC,0,0,topC + dimension/2,leftC,dimension/2);
    //C11 = C11 + T2
    add(C,T2,C,topC,leftC,0,0,topC,leftC,dimension/2);
    //T1 = B12 - B22
    subtract(B,B,T1,topB,leftB + dimension/2,topB + dimension/2,leftB + dimension/2,0,0,dimension/2);
    //C12 = A11 * T1

    multiply(A,T1,C,topA,leftA,0,0,topC,leftC + dimension/2,dimension/2);

    //C22 = C22 + C12
    add(C,C,C,topC + dimension/2,leftC + dimension/2,topC,leftC + dimension/2,topC + dimension/2,leftC + dimension/2,dimension/2);
    //T2 = A11 + A12
    add(A,A,T2,topA,leftA,topA,leftA + dimension/2,0,0,dimension/2);
    //T1 = T2 * B22

    multiply(T2,B,T1,0,0,topB + dimension/2,leftB + dimension/2,0,0,dimension/2);

    //C12 = C12 + T1
    add(C,T1,C,topC,leftC + dimension/2,0,0,topC,leftC + dimension/2,dimension/2);
    //C11 = C11 - T1
    subtract(C,T1,C,topC,leftC,0,0,topC,leftC,dimension/2);
    
    delete(T1);
    delete(T2);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r9d, %ebp
movq %rdx, %r15
movq %rsi, 0x18(%rsp)
movl 0xd0(%rsp), %ebx
movl 0xd8(%rsp), %r14d
movl 0xe0(%rsp), %edx
movl 0xe8(%rsp), %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
movq %rcx, %r9
movq %rcx, 0x60(%rsp)
addl %r12d, %ecx
movl %ecx, 0x8(%rsp)
leal (%r12,%rdx), %eax
movq %rax, 0x10(%rsp)
movq %rdx, %r13
movq %rdi, 0x28(%rsp)
movq %rdi, %rsi
movq %r15, %rdx
movq %r8, 0x40(%rsp)
pushq %r12
pushq %rax
pushq %r14
pushq %r8
callq 0x277a
addq $0x20, %rsp
movq %rbx, %r8
movq %rbx, 0x58(%rsp)
leal (%r12,%rbx), %eax
movq %rax, 0x38(%rsp)
leal (%r12,%r14), %ebx
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq %rbp, 0x48(%rsp)
movl %ebp, %ecx
movl %ebp, %r9d
pushq %r12
pushq %r13
movq %r13, %rbp
pushq %rbx
pushq %rax
callq 0x26d2
addq $0x20, %rsp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movl %r14d, %ecx
movq %r14, %r13
movq %r14, 0x78(%rsp)
movq 0x10(%rsp), %r14
movl %r14d, %r8d
movq %rbx, %r9
movq %rbx, 0x20(%rsp)
pushq %r12
pushq %r14
pushq %rbx
pushq %rbp
callq 0x2eb7
addq $0x20, %rsp
movq 0x40(%rsp), %rax
leal (%r12,%rax), %r8d
movq 0x28(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x60(%rsp), %rcx
movq %r8, 0x30(%rsp)
movl 0x8(%rsp), %r9d
pushq %r12
pushq %r14
pushq %r13
pushq %r8
callq 0x277a
addq $0x20, %rsp
movq 0x48(%rsp), %r14
leal (%r12,%r14), %ecx
movl %ecx, 0xc(%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x58(%rsp), %r8
movl %ecx, %r9d
pushq %r12
movq %rbp, %rbx
pushq %rbp
movq 0x30(%rsp), %r13
pushq %r13
pushq 0x50(%rsp)
callq 0x26d2
addq $0x20, %rsp
movq %r15, %rbp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq 0x78(%rsp), %rcx
movq 0x10(%rsp), %r15
movl %r15d, %r8d
movl %r13d, %r9d
pushq %r12
movq %rbx, %r13
pushq %rbx
pushq %rcx
movq %rcx, %rbx
pushq %r13
callq 0x2eb7
addq $0x20, %rsp
movq 0x28(%rsp), %rdi
movq %rdi, %rsi
movq %rbp, %rdx
movq 0x60(%rsp), %rcx
movq 0x40(%rsp), %r8
movl 0x8(%rsp), %r9d
pushq %r12
pushq %r15
pushq %rbx
movq %rbx, %r15
pushq 0x48(%rsp)
callq 0x26d2
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %rbp, %rdx
movl %r14d, %ecx
movq 0x58(%rsp), %r8
movl 0xc(%rsp), %r9d
pushq %r12
pushq %r13
movq 0x30(%rsp), %r14
pushq %r14
pushq 0x50(%rsp)
callq 0x26d2
addq $0x20, %rsp
movl $0x20, %edi
callq 0x21b0
movq %rax, %rbx
addq $0x8, %rax
movq %rax, 0x90(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movaps %xmm0, 0x10(%rbx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x245c
xorl %eax, %eax
movq %rbp, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r15d, %ecx
movq 0x10(%rsp), %r8
movl %r14d, %r9d
pushq %r12
pushq %rax
pushq %rax
pushq %r13
callq 0x2eb7
addq $0x20, %rsp
movq %rbx, %rdi
movq %rbx, 0x70(%rsp)
movq %rbp, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r15d, %r9d
pushq %r12
movq %r13, 0x58(%rsp)
pushq %r13
pushq %r15
pushq %r13
callq 0x26d2
addq $0x20, %rsp
movq %rbx, %rdi
movq %rbp, %rbx
movq %rbp, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r14d, %r9d
pushq %r12
movq 0x18(%rsp), %rax
pushq %rax
pushq %r14
pushq %rax
callq 0x26d2
addq $0x20, %rsp
movl $0x20, %edi
callq 0x21b0
movq %rax, %r13
leaq 0x8(%rax), %rcx
movq %rcx, 0x88(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movq %rax, %rdi
movl %r12d, %esi
callq 0x245c
movq 0x28(%rsp), %rdi
movq %rdi, %rsi
movq %r13, %rdx
movq %r13, 0x68(%rsp)
movl 0x8(%rsp), %ecx
movq 0x40(%rsp), %r8
movl %ecx, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
pushq %rax
pushq 0x48(%rsp)
callq 0x26d2
addq $0x20, %rsp
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x48(%rsp), %rbp
movl %ebp, %r9d
pushq %r12
movq 0x58(%rsp), %r14
pushq %r14
movq 0x30(%rsp), %r15
pushq %r15
movq 0x70(%rsp), %r13
pushq %r13
callq 0x2eb7
addq $0x20, %rsp
movq %rbx, 0x80(%rsp)
movq %rbx, %rdi
movq %rbx, %rsi
movq %rbx, %rdx
movl %r15d, %ecx
movq 0x10(%rsp), %r8
movl %r15d, %r9d
pushq %r12
pushq %r8
pushq %r15
pushq %r14
callq 0x277a
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq 0x70(%rsp), %rbx
movq %rbx, %rdx
movl 0xc(%rsp), %ecx
movl %r13d, %r8d
movl %ebp, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
pushq %rax
pushq %r13
callq 0x277a
addq $0x20, %rsp
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x68(%rsp), %r14
movq %r14, %rdx
movl 0x8(%rsp), %ecx
movq 0x30(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
pushq %rax
pushq %rax
callq 0x2eb7
addq $0x20, %rsp
movq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq 0x50(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r8
movq %r8, %r13
pushq %rcx
xorl %eax, %eax
pushq %rax
callq 0x26d2
addq $0x20, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x78(%rsp), %r14
movl %r14d, %ecx
movq %r13, %r8
xorl %r9d, %r9d
pushq %r12
pushq %r13
pushq %r14
xorl %eax, %eax
pushq %rax
callq 0x26d2
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
movq 0x48(%rsp), %rcx
movq 0x38(%rsp), %r8
movl 0xc(%rsp), %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
pushq %rax
pushq %r8
callq 0x277a
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x60(%rsp), %rbx
movl %ebx, %ecx
movq 0x40(%rsp), %r13
movl %r13d, %r8d
xorl %r9d, %r9d
pushq %r12
movq 0x18(%rsp), %rbp
pushq %rbp
pushq %r14
xorl %eax, %eax
pushq %rax
callq 0x2eb7
addq $0x20, %rsp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq %rbp, %r8
movl %r14d, %r9d
pushq %r12
pushq %rbp
pushq %rcx
pushq %rbp
callq 0x26d2
addq $0x20, %rsp
movq 0x28(%rsp), %rdi
movq %rdi, %rsi
movq 0x68(%rsp), %rbp
movq %rbp, %rdx
movl %ebx, %ecx
movl %r13d, %r8d
movl %ebx, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
pushq %rax
pushq 0x48(%rsp)
callq 0x26d2
addq $0x20, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq 0x70(%rsp), %rbx
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl 0xc(%rsp), %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
pushq %rax
xorl %r13d, %r13d
pushq 0x50(%rsp)
callq 0x2eb7
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl %r14d, %ecx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r8
pushq %r14
pushq %r13
callq 0x26d2
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl %r14d, %ecx
movq 0x50(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r8
pushq %r14
pushq %r13
callq 0x277a
addq $0x20, %rsp
movq 0x90(%rsp), %rdi
callq 0x7bfc
movq %rbx, %rdi
callq 0x2190
movq 0x88(%rsp), %rdi
callq 0x7bfc
movq %rbp, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2190