// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %r9, 0x20(%rsp)
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movl 0xc0(%rsp), %ebx
movl 0xc8(%rsp), %ebp
movl 0xd0(%rsp), %r13d
movl 0xd8(%rsp), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
movl %eax, %r12d
movq %rcx, %r9
movq %rcx, 0x58(%rsp)
addl %r12d, %ecx
movl %ecx, (%rsp)
leal (%r12,%r13), %r14d
movq %rdi, 0x18(%rsp)
movq %rdi, %rsi
movq %r15, %rdx
movq %r8, 0x30(%rsp)
pushq %r12
pushq %r14
pushq %rbp
pushq %r8
callq 0x26a1
addq $0x20, %rsp
movq %rbx, %r8
movq %rbx, 0x50(%rsp)
leal (%r12,%rbx), %eax
movq %rax, 0x38(%rsp)
leal (%r12,%rbp), %ebx
movq 0x10(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movl %ecx, %r9d
pushq %r12
pushq %r13
pushq %rbx
pushq %rax
callq 0x25eb
addq $0x20, %rsp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movl %ebp, %ecx
movl %r14d, %r8d
movq %rbx, %r9
movq %rbx, 0x40(%rsp)
pushq %r12
pushq %r14
movq %r14, 0x18(%rsp)
pushq %rbx
pushq %r13
callq 0x2d93
addq $0x20, %rsp
movq 0x30(%rsp), %rax
leal (%r12,%rax), %r8d
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x58(%rsp), %rcx
movq %r8, 0x28(%rsp)
movl (%rsp), %r9d
pushq %r12
pushq %r14
movq %rbp, %r14
pushq %rbp
pushq %r8
callq 0x26a1
addq $0x20, %rsp
movq 0x20(%rsp), %rax
leal (%r12,%rax), %ecx
movl %ecx, 0x4(%rsp)
movq 0x10(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x50(%rsp), %rbp
movl %ebp, %r8d
movl %ecx, %r9d
pushq %r12
movq %r13, %rbx
pushq %r13
movq 0x50(%rsp), %r13
pushq %r13
pushq 0x50(%rsp)
callq 0x25eb
addq $0x20, %rsp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq 0x8(%rsp), %r14
movl %r14d, %r8d
movl %r13d, %r9d
pushq %r12
movq %rbx, %r13
pushq %rbx
pushq %rcx
movq %rcx, %rbx
movq %rcx, 0x88(%rsp)
pushq %r13
callq 0x2d93
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x58(%rsp), %rcx
movq 0x30(%rsp), %r8
movl (%rsp), %r9d
pushq %r12
pushq %r14
pushq %rbx
pushq 0x40(%rsp)
callq 0x25eb
addq $0x20, %rsp
movq 0x10(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movl %ebp, %r8d
movl 0x4(%rsp), %r9d
pushq %r12
pushq %r13
movq 0x50(%rsp), %r14
pushq %r14
pushq 0x50(%rsp)
callq 0x25eb
addq $0x20, %rsp
pushq $0x20
popq %rdi
callq 0x2190
movq %rax, %rbx
addq $0x8, %rax
movq %rax, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movaps %xmm0, 0x10(%rbx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x23cc
movq %r15, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq 0x70(%rsp), %rbp
movl %ebp, %ecx
movq 0x8(%rsp), %r8
movl %r14d, %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq %r13
callq 0x2d93
addq $0x20, %rsp
movq %rbx, %rdi
movq %rbx, 0x68(%rsp)
movq %r15, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %ebp, %r9d
pushq %r12
movq %r13, 0x50(%rsp)
pushq %r13
pushq %rbp
pushq %r13
callq 0x25eb
addq $0x20, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r14, %r13
movl %r13d, %r9d
pushq %r12
movq 0x10(%rsp), %rax
pushq %rax
pushq %r14
pushq %rax
callq 0x25eb
addq $0x20, %rsp
pushq $0x20
popq %rdi
callq 0x2190
movq %rax, %rbx
leaq 0x8(%rax), %rcx
movq %rcx, 0x78(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movq %rax, %rdi
movl %r12d, %esi
callq 0x23cc
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
movq %rbx, 0x60(%rsp)
movl (%rsp), %ecx
movq 0x30(%rsp), %r8
movl %ecx, %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq 0x40(%rsp)
callq 0x25eb
addq $0x20, %rsp
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x20(%rsp), %rbp
movl %ebp, %r9d
pushq %r12
movq 0x50(%rsp), %r14
pushq %r14
pushq %r13
pushq 0x68(%rsp)
callq 0x2d93
addq $0x20, %rsp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movl %r13d, %ecx
movq 0x8(%rsp), %r8
movl %r13d, %r9d
pushq %r12
pushq %r8
pushq %r13
pushq %r14
callq 0x26a1
addq $0x20, %rsp
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x68(%rsp), %rbx
movq %rbx, %rdx
movl 0x4(%rsp), %ecx
movq 0x50(%rsp), %r8
movl %ebp, %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq %r8
callq 0x26a1
addq $0x20, %rsp
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x60(%rsp), %r14
movq %r14, %rdx
movl (%rsp), %ecx
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq $0x0
callq 0x2d93
addq $0x20, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq 0x48(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r8
movq %r8, %r13
pushq %rcx
pushq $0x0
callq 0x25eb
addq $0x20, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x70(%rsp), %r14
movl %r14d, %ecx
movq %r13, %r8
xorl %r9d, %r9d
pushq %r12
pushq %r13
pushq %r14
pushq $0x0
callq 0x25eb
addq $0x20, %rsp
movq 0x10(%rsp), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
movq 0x20(%rsp), %rcx
movq 0x38(%rsp), %r8
movl 0x4(%rsp), %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq %r8
callq 0x26a1
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x58(%rsp), %rbp
movl %ebp, %ecx
movq 0x30(%rsp), %rbx
movl %ebx, %r8d
xorl %r9d, %r9d
pushq %r12
movq 0x10(%rsp), %r13
pushq %r13
pushq %r14
pushq $0x0
callq 0x2d93
addq $0x20, %rsp
movq %r15, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq 0x40(%rsp), %rcx
movq %r13, %r8
movl %r14d, %r9d
pushq %r12
pushq %r13
pushq %rcx
pushq %r13
callq 0x25eb
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq 0x60(%rsp), %r13
movq %r13, %rdx
movl %ebp, %ecx
movl %ebx, %r8d
movl %ebp, %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq 0x40(%rsp)
callq 0x25eb
addq $0x20, %rsp
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq 0x68(%rsp), %rbx
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl 0x4(%rsp), %r9d
pushq %r12
pushq $0x0
pushq $0x0
pushq 0x50(%rsp)
callq 0x2d93
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl %r14d, %ecx
movq 0x8(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r8
pushq %r14
pushq $0x0
callq 0x25eb
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl %r14d, %ecx
movq 0x48(%rsp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r8
pushq %r14
pushq $0x0
callq 0x26a1
addq $0x20, %rsp
movq 0x80(%rsp), %rdi
callq 0x71bc
movq %rbx, %rdi
callq 0x2170
movq 0x78(%rsp), %rdi
callq 0x71bc
movq %r13, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2170