void kernel_sgemm_nt_4x4_lib4(int kmax, float *A0, float *B, float *C0,
                              float *D0, int alg) {
  if (kmax <= 0)
    return;
  int k_iter = kmax / 4;
  int k_left = kmax % 4;
  __asm__ volatile(
      "                                \n\t"
      "                                \n\t"
      "movq          %2, %%rax         \n\t"
      "movq          %3, %%rbx         \n\t"
      "                                \n\t"
      "                                \n\t"
      "movaps        0(%%rax), %%xmm0  \n\t"
      "movaps       16(%%rax), %%xmm1  \n\t"
      "movaps        0(%%rbx), %%xmm2  \n\t"
      "                                \n\t"
      "                                \n\t"
      "xorpd     %%xmm3,  %%xmm3       \n\t"
      "movaps    %%xmm3,  %%xmm4       \n\t"
      "movaps    %%xmm3,  %%xmm5       \n\t"
      "movaps    %%xmm3,  %%xmm6       \n\t"
      "movaps    %%xmm3,  %%xmm7       \n\t"
      "movaps    %%xmm3,  %%xmm8       \n\t"
      "movaps    %%xmm3,  %%xmm9       \n\t"
      "movaps    %%xmm3, %%xmm10       \n\t"
      "movaps    %%xmm3, %%xmm11       \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      "movl      %0, %%esi             \n\t"
      "testl  %%esi, %%esi             \n\t"
      "je     .SCONSIDKLEFT_4X4        \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      ".SLOOPKITER_4X4:                \n\t"
      "                                \n\t"
      "addps   %%xmm6, %%xmm10         \n\t"
      "pshufd   $0x39, %%xmm2, %%xmm7  \n\t"
      "mulps   %%xmm0, %%xmm2          \n\t"
      "                                \n\t"
      "addps   %%xmm4, %%xmm11         \n\t"
      "pshufd   $0x39, %%xmm7, %%xmm6  \n\t"
      "mulps   %%xmm0, %%xmm7          \n\t"
      "                                \n\t"
      "addps   %%xmm2, %%xmm8          \n\t"
      "movaps       16(%%rbx), %%xmm2  \n\t"
      "pshufd   $0x39, %%xmm6, %%xmm4  \n\t"
      "mulps   %%xmm0, %%xmm6          \n\t"
      "                                \n\t"
      "addps   %%xmm7, %%xmm9          \n\t"
      "mulps   %%xmm0, %%xmm4          \n\t"
      "movaps       32(%%rax), %%xmm0  \n\t"
      "                                \n\t"
      "                                \n\t"
      "addps   %%xmm6, %%xmm10         \n\t"
      "pshufd   $0x39, %%xmm2, %%xmm7  \n\t"
      "mulps   %%xmm1, %%xmm2          \n\t"
      "                                \n\t"
      "addps   %%xmm4, %%xmm11         \n\t"
      "pshufd   $0x39, %%xmm7, %%xmm6  \n\t"
      "mulps   %%xmm1, %%xmm7          \n\t"
      "                                \n\t"
      "addps   %%xmm2, %%xmm8          \n\t"
      "movaps       32(%%rbx), %%xmm2  \n\t"
      "pshufd   $0x39, %%xmm6, %%xmm4  \n\t"
      "mulps   %%xmm1, %%xmm6          \n\t"
      "                                \n\t"
      "addps   %%xmm7, %%xmm9          \n\t"
      "mulps   %%xmm1, %%xmm4          \n\t"
      "movaps       48(%%rax), %%xmm1  \n\t"
      "                                \n\t"
      "                                \n\t"
      "addps   %%xmm6, %%xmm10         \n\t"
      "pshufd   $0x39, %%xmm2, %%xmm7  \n\t"
      "mulps   %%xmm0, %%xmm2          \n\t"
      "                                \n\t"
      "addps   %%xmm4, %%xmm11         \n\t"
      "pshufd   $0x39, %%xmm7, %%xmm6  \n\t"
      "mulps   %%xmm0, %%xmm7          \n\t"
      "                                \n\t"
      "addps   %%xmm2, %%xmm8          \n\t"
      "movaps       48(%%rbx), %%xmm2  \n\t"
      "pshufd   $0x39, %%xmm6, %%xmm4  \n\t"
      "mulps   %%xmm0, %%xmm6          \n\t"
      "                                \n\t"
      "addps   %%xmm7, %%xmm9          \n\t"
      "mulps   %%xmm0, %%xmm4          \n\t"
      "movaps       64(%%rax), %%xmm0  \n\t"
      "                                \n\t"
      "                                \n\t"
      "addps   %%xmm6, %%xmm10         \n\t"
      "pshufd   $0x39, %%xmm2, %%xmm7  \n\t"
      "mulps   %%xmm1, %%xmm2          \n\t"
      "addq       $64, %%rax           \n\t"
      "                                \n\t"
      "addps   %%xmm4, %%xmm11         \n\t"
      "pshufd   $0x39, %%xmm7, %%xmm6  \n\t"
      "mulps   %%xmm1, %%xmm7          \n\t"
      "addq       $64, %%rbx           \n\t"
      "                                \n\t"
      "addps   %%xmm2, %%xmm8          \n\t"
      "movaps         (%%rbx), %%xmm2  \n\t"
      "pshufd   $0x39, %%xmm6, %%xmm4  \n\t"
      "mulps   %%xmm1, %%xmm6          \n\t"
      "                                \n\t"
      "addps   %%xmm7, %%xmm9          \n\t"
      "decl    %%esi                   \n\t"
      "mulps   %%xmm1, %%xmm4          \n\t"
      "movaps       16(%%rax), %%xmm1  \n\t"
      "                                \n\t"
      "                                \n\t"
      "jne    .SLOOPKITER_4X4          \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      ".SCONSIDKLEFT_4X4:              \n\t"
      "                                \n\t"
      "movl      %1, %%esi             \n\t"
      "testl  %%esi, %%esi             \n\t"
      "je     .SPOSTACCUM_4X4          \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      ".SLOOPKLEFT_4X4:                \n\t"
      "                                \n\t"
      "addps   %%xmm6, %%xmm10         \n\t"
      "pshufd   $0x39, %%xmm2, %%xmm7  \n\t"
      "mulps   %%xmm0, %%xmm2          \n\t"
      "                                \n\t"
      "addps   %%xmm4, %%xmm11         \n\t"
      "pshufd   $0x39, %%xmm7, %%xmm6  \n\t"
      "mulps   %%xmm0, %%xmm7          \n\t"
      "                                \n\t"
      "addps   %%xmm2, %%xmm8          \n\t"
      "movaps       16(%%rbx), %%xmm2  \n\t"
      "pshufd   $0x39, %%xmm6, %%xmm4  \n\t"
      "mulps   %%xmm0, %%xmm6          \n\t"
      "                                \n\t"
      "addps   %%xmm7, %%xmm9          \n\t"
      "mulps   %%xmm0, %%xmm4          \n\t"
      "movaps       16(%%rax), %%xmm0  \n\t"
      "                                \n\t"
      "addq          $16, %%rax        \n\t"
      "addq          $16, %%rbx        \n\t"
      "                                \n\t"
      "                                \n\t"
      "decl    %%esi                   \n\t"
      "jne    .SLOOPKLEFT_4X4          \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      ".SPOSTACCUM_4X4:                \n\t"
      "                                \n\t"
      "addps   %%xmm6, %%xmm10         \n\t"
      "addps   %%xmm4, %%xmm11         \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      "movaps  %%xmm9, %%xmm4          \n\t"
      "shufps   $0xd8, %%xmm8,  %%xmm9 \n\t"
      "shufps   $0xd8, %%xmm11, %%xmm8 \n\t"
      "shufps   $0xd8, %%xmm10, %%xmm11\n\t"
      "shufps   $0xd8, %%xmm4,  %%xmm10\n\t"
      "                                \n\t"
      "movaps  %%xmm8, %%xmm4          \n\t"
      "shufps   $0xd8, %%xmm10, %%xmm8 \n\t"
      "shufps   $0xd8, %%xmm4, %%xmm10 \n\t"
      "movaps  %%xmm9, %%xmm5          \n\t"
      "shufps   $0xd8, %%xmm11, %%xmm9 \n\t"
      "shufps   $0xd8, %%xmm5, %%xmm11 \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      "movq   %4, %%rax                \n\t"
      "movq   %6, %%r8                 \n\t"
      "                                \n\t"
      "                                \n\t"
      "movl   %5, %%ecx                \n\t"
      "testl  %%ecx, %%ecx             \n\t"
      "je     .S0_4X4                  \n\t"
      "                                \n\t"
      "cmpl	$1, %%ecx                \n\t"
      "                                \n\t"
      "movaps  (%%rax),   %%xmm0       \n\t"
      "movaps  16(%%rax), %%xmm1       \n\t"
      "movaps  32(%%rax), %%xmm2       \n\t"
      "movaps  48(%%rax), %%xmm3       \n\t"
      "                                \n\t"
      "je     .S1_4X4                  \n\t"
      "                                \n\t"
      "                                \n\t"
      "subps  %%xmm8,  %%xmm0           \n\t"
      "subps  %%xmm9,  %%xmm1           \n\t"
      "subps  %%xmm10, %%xmm2           \n\t"
      "subps  %%xmm11, %%xmm3           \n\t"
      "                                \n\t"
      "movaps  %%xmm0, (%%r8)         \n\t"
      "movaps  %%xmm1, 16(%%r8)       \n\t"
      "movaps  %%xmm2, 32(%%r8)       \n\t"
      "movaps  %%xmm3, 48(%%r8)       \n\t"
      "                                \n\t"
      "jmp    .SDONE_4X4               \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      ".S1_4X4:                        \n\t"
      "                                \n\t"
      "addps  %%xmm0, %%xmm8           \n\t"
      "addps  %%xmm1, %%xmm9           \n\t"
      "addps  %%xmm2, %%xmm10          \n\t"
      "addps  %%xmm3, %%xmm11          \n\t"
      "                                \n\t"
      ".S0_4X4:                        \n\t"
      "                                \n\t"
      "movaps	%%xmm8,  (%%r8)          \n\t"
      "movaps	%%xmm9,  16(%%r8)        \n\t"
      "movaps	%%xmm10, 32(%%r8)        \n\t"
      "movaps	%%xmm11, 48(%%r8)        \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      "                                \n\t"
      ".SDONE_4X4:                     \n\t"
      "                                \n\t"
      :
      : "m"(k_iter), "m"(k_left), "m"(A0), "m"(B), "m"(C0), "m"(alg), "m"(D0)
      : "rax", "rbx", "rcx", "r8", "r9", "rsi", "xmm0", "xmm1", "xmm2", "xmm3",
        "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11",
        "xmm12", "xmm13", "xmm14", "xmm15", "memory");
}


x86 assembly (clang, -O0):
<kernel_sgemm_nt_4x4_lib4>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     112b <kernel_sgemm_nt_4x4_lib4+0x2b>
jmp    135f <.SDONE_4X4>
mov    -0xc(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rbx
movaps (%rax),%xmm0
movaps 0x10(%rax),%xmm1
movaps (%rbx),%xmm2
xorpd  %xmm3,%xmm3
movaps %xmm3,%xmm4
movaps %xmm3,%xmm5
movaps %xmm3,%xmm6
movaps %xmm3,%xmm7
movaps %xmm3,%xmm8
movaps %xmm3,%xmm9
movaps %xmm3,%xmm10
movaps %xmm3,%xmm11
mov    -0x38(%rbp),%esi
test   %esi,%esi
je     125f <.SCONSIDKLEFT_4X4>

x86 assembly (clang, -O2):
<kernel_sgemm_nt_4x4_lib4>:
push   %rbx
mov    %rsi,-0x8(%rsp)
mov    %rdx,-0x10(%rsp)
mov    %rcx,-0x18(%rsp)
mov    %r8,-0x20(%rsp)
mov    %r9d,-0x24(%rsp)
test   %edi,%edi
jle    133e <.SDONE_4X4>
mov    %edi,%eax
shr    $0x2,%eax
mov    %eax,-0x28(%rsp)
and    $0x3,%edi
mov    %edi,-0x2c(%rsp)
mov    -0x8(%rsp),%rax
mov    -0x10(%rsp),%rbx
movaps (%rax),%xmm0
movaps 0x10(%rax),%xmm1
movaps (%rbx),%xmm2
xorpd  %xmm3,%xmm3
movaps %xmm3,%xmm4
movaps %xmm3,%xmm5
movaps %xmm3,%xmm6
movaps %xmm3,%xmm7
movaps %xmm3,%xmm8
movaps %xmm3,%xmm9
movaps %xmm3,%xmm10
movaps %xmm3,%xmm11
mov    -0x28(%rsp),%esi
test   %esi,%esi
je     124d <.SCONSIDKLEFT_4X4>

LLVM IR (-O0):
define void @kernel_sgemm_nt_4x4_lib4(i32 %0, float* %1, float* %2, float* %3, float* %4, i32 %5) {
%7 = alloca i32
%8 = alloca float*
%9 = alloca float*
%10 = alloca float*
%11 = alloca float*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store float* %1, float** %8
store float* %2, float** %9
store float* %3, float** %10
store float* %4, float** %11
store i32 %5, i32* %12
%15 = load i32, i32* %7
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %6
br label %23
18: ; preds = %6
%19 = load i32, i32* %7
%20 = sdiv i32 %19, 4
store i32 %20, i32* %13
%21 = load i32, i32* %7
%22 = srem i32 %21, 4
store i32 %22, i32* %14
br label %23
23: ; preds = %18, %17
ret void
}

LLVM IR (-O2):
define void @kernel_sgemm_nt_4x4_lib4(i32 %0, float* %1, float* %2, float* %3, float* %4, i32 %5) {
%7 = alloca float*
%8 = alloca float*
%9 = alloca float*
%10 = alloca float*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store float* %1, float** %7
store float* %2, float** %8
store float* %3, float** %9
store float* %4, float** %10
store i32 %5, i32* %11
%14 = icmp slt i32 %0, 1
br i1 %14, label %20, label %15
15: ; preds = %6
%16 = bitcast i32* %12 to i8*
%17 = lshr i32 %0, 2
store i32 %17, i32* %12
%18 = bitcast i32* %13 to i8*
%19 = and i32 %0, 3
store i32 %19, i32* %13
br label %20
20: ; preds = %6, %15
ret void
}