void copy_identity(const int nr, const int nc, double *__restrict__ z,
                   const int ldz) {
  register int i, j;
  double *zp;
  for (i = 0; i < nr; i++) {
    zp = z + i * ldz;
    for (j = 0; j < nc; j++) {
      (*zp++) = 0.0;
    }
    (*(z + i * ldz + i)) = 1.0;
  }
}


x86 assembly (clang, -O0):
<copy_identity>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a8 <copy_identity+0xa8>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
imul   -0x14(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1174 <copy_identity+0x74>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1144 <copy_identity+0x44>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
imul   -0x14(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x18(%rbp),%rcx
movsd  0xe6b(%rip),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1118 <copy_identity+0x18>
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_identity>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x8(%rsp)
test   %edi,%edi
jle    118f <copy_identity+0x7f>
mov    %esi,%ebp
mov    %esi,%eax
shl    $0x3,%rax
mov    %rax,0x10(%rsp)
movslq 0x8(%rsp),%rax
mov    %edi,%ecx
xor    %r14d,%r14d
cmp    $0x1,%edi
mov    %rax,0x20(%rsp)
mov    %rcx,0x28(%rsp)
jne    119e <copy_identity+0x8e>
xor    %ebx,%ebx
testb  $0x1,0x28(%rsp)
je     118f <copy_identity+0x7f>
mov    0x20(%rsp),%rax
imul   %rbx,%rax
lea    (%rdx,%rax,8),%r14
test   %ebp,%ebp
jle    1181 <copy_identity+0x71>
mov    0x8(%rsp),%rax
imul   %ebx,%eax
cltq
lea    (%rdx,%rax,8),%rdi
xor    %esi,%esi
mov    0x10(%rsp),%rdx
call   1030 <memset@plt>
movabs $0x3ff0000000000000,%rax
mov    %rax,(%r14,%rbx,8)
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
and    $0xfffffffe,%ecx
mov    %rcx,0x30(%rsp)
mov    %rax,%r15
shl    $0x4,%r15
add    $0x10,%r15
add    %eax,%eax
mov    %eax,0x1c(%rsp)
mov    %rdx,%r13
xor    %ebx,%ebx
mov    %rdx,%r12
jmp    11fb <copy_identity+0xeb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rax
movabs $0x3ff0000000000000,%rcx
mov    %rcx,0x8(%r13,%rax,8)
add    $0x2,%rbx
add    %r15,%r13
add    0x1c(%rsp),%r14d
cmp    %rbx,0x30(%rsp)
je     114f <copy_identity+0x3f>
test   %ebp,%ebp
jle    1215 <copy_identity+0x105>
movslq %r14d,%rax
lea    (%rdx,%rax,8),%rdi
xor    %esi,%esi
mov    0x10(%rsp),%rdx
call   1030 <memset@plt>
mov    %r12,%rdx
movabs $0x3ff0000000000000,%rax
mov    %rax,0x0(%r13)
test   %ebp,%ebp
jle    11d0 <copy_identity+0xc0>
mov    0x8(%rsp),%rax
add    %r14d,%eax
cltq
lea    (%rdx,%rax,8),%rdi
xor    %esi,%esi
mov    0x10(%rsp),%rdx
call   1030 <memset@plt>
mov    %r12,%rdx
jmp    11d0 <copy_identity+0xc0>

LLVM IR (-O0):
define void @copy_identity(i32 %0, i32 %1, double* noalias %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca double*
store i32 %0, i32* %5
store i32 %1, i32* %6
store double* %2, double** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %12
12: ; preds = %43, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %46
16: ; preds = %12
%17 = load double*, double** %7
%18 = load i32, i32* %9
%19 = load i32, i32* %8
%20 = mul nsw i32 %18, %19
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double, double* %17, i64 %21
store double* %22, double** %11
store i32 0, i32* %10
br label %23
23: ; preds = %30, %16
%24 = load i32, i32* %10
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load double*, double** %11
%29 = getelementptr inbounds double, double* %28, i32 1
store double* %29, double** %11
store double 0.000000e+00, double* %28
br label %30
30: ; preds = %27
%31 = load i32, i32* %10
%32 = add nsw i32 %31, 1
store i32 %32, i32* %10
br label %23
33: ; preds = %23
%34 = load double*, double** %7
%35 = load i32, i32* %9
%36 = load i32, i32* %8
%37 = mul nsw i32 %35, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds double, double* %34, i64 %38
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %39, i64 %41
store double 1.000000e+00, double* %42
br label %43
43: ; preds = %33
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
br label %12
46: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @copy_identity(i32 %0, i32 %1, double* noalias nocapture writeonly %2, i32 %3) {
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %57
6: ; preds = %4
%7 = icmp sgt i32 %1, 0
%8 = zext i32 %1 to i64
%9 = shl nuw nsw i64 %8, 3
%10 = sext i32 %3 to i64
%11 = zext i32 %0 to i64
%12 = and i64 %11, 1
%13 = icmp eq i32 %0, 1
br i1 %13, label %43, label %14
14: ; preds = %6
%15 = and i64 %11, 4294967294
br label %16
16: ; preds = %38, %14
%17 = phi i64 [ 0, %14 ], [ %40, %38 ]
%18 = phi i64 [ 0, %14 ], [ %41, %38 ]
%19 = mul nsw i64 %17, %10
%20 = getelementptr inbounds double, double* %2, i64 %19
br i1 %7, label %21, label %27
21: ; preds = %16
%22 = trunc i64 %17 to i32
%23 = mul i32 %22, %3
%24 = sext i32 %23 to i64
%25 = getelementptr double, double* %2, i64 %24
%26 = bitcast double* %25 to i8*
br label %27
27: ; preds = %21, %16
%28 = getelementptr inbounds double, double* %20, i64 %17
store double 1.000000e+00, double* %28
%29 = or i64 %17, 1
%30 = mul nsw i64 %29, %10
%31 = getelementptr inbounds double, double* %2, i64 %30
br i1 %7, label %32, label %38
32: ; preds = %27
%33 = trunc i64 %29 to i32
%34 = mul i32 %33, %3
%35 = sext i32 %34 to i64
%36 = getelementptr double, double* %2, i64 %35
%37 = bitcast double* %36 to i8*
br label %38
38: ; preds = %32, %27
%39 = getelementptr inbounds double, double* %31, i64 %29
store double 1.000000e+00, double* %39
%40 = add nuw nsw i64 %17, 2
%41 = add i64 %18, 2
%42 = icmp eq i64 %41, %15
br i1 %42, label %43, label %16
43: ; preds = %38, %6
%44 = phi i64 [ 0, %6 ], [ %40, %38 ]
%45 = icmp eq i64 %12, 0
br i1 %45, label %57, label %46
46: ; preds = %43
%47 = mul nsw i64 %44, %10
%48 = getelementptr inbounds double, double* %2, i64 %47
br i1 %7, label %49, label %55
49: ; preds = %46
%50 = trunc i64 %44 to i32
%51 = mul i32 %50, %3
%52 = sext i32 %51 to i64
%53 = getelementptr double, double* %2, i64 %52
%54 = bitcast double* %53 to i8*
br label %55
55: ; preds = %49, %46
%56 = getelementptr inbounds double, double* %48, i64 %44
store double 1.000000e+00, double* %56
br label %57
57: ; preds = %55, %43, %4
ret void
}