void Identity_Matrix(double *A, int n) {
  int i, j;
  for (i = 0; i < n - 1; i++) {
    *A++ = 1.0;
    for (j = 0; j < n; j++)
      *A++ = 0.0;
  }
  *A = 1.0;
}


x86 assembly (clang, -O0):
<Identity_Matrix>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1188 <Identity_Matrix+0x88>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x8(%rbp)
movsd  0xec6(%rip),%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1175 <Identity_Matrix+0x75>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x8(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1145 <Identity_Matrix+0x45>
jmp    117a <Identity_Matrix+0x7a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1112 <Identity_Matrix+0x12>
mov    -0x8(%rbp),%rax
movsd  0xe6c(%rip),%xmm0
movsd  %xmm0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<Identity_Matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
movabs $0x3ff0000000000000,%r12
cmp    $0x2,%esi
jl     1154 <Identity_Matrix+0x44>
mov    %esi,%r14d
shl    $0x3,%r14
lea    -0x1(%rsi),%eax
add    $0xfffffffe,%esi
mov    %eax,%ebp
and    $0x7,%ebp
cmp    $0x7,%esi
jae    115c <Identity_Matrix+0x4c>
test   %ebp,%ebp
jne    12a4 <Identity_Matrix+0x194>
jmp    12c9 <Identity_Matrix+0x1b9>
mov    %r13,%rbx
jmp    12c9 <Identity_Matrix+0x1b9>
mov    %ebp,(%rsp)
mov    %eax,%edx
and    $0xfffffff8,%edx
mov    %rax,%rcx
shl    $0x6,%rcx
sub    $0xffffffffffffff80,%rcx
mov    %rcx,0x8(%rsp)
mov    %rax,0x10(%rsp)
lea    0x10(,%rax,8),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %edx,0x4(%rsp)
mov    %r13,%rbx
movabs $0x3ff0000000000000,%rax
mov    %rax,0x0(%r13)
mov    %rax,%r13
lea    0x8(%rbx),%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%rbx,%r15,1),%rbp
mov    %r13,(%rbx,%r15,1)
lea    (%rbx,%r15,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%r15,%rbp,1),%r12
mov    %r13,(%r15,%rbp,1)
lea    (%r15,%rbp,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%r12,%r15,1),%rbp
mov    %r13,(%r15,%r12,1)
lea    (%r15,%r12,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%r15,%rbp,1),%r12
mov    %r13,(%r15,%rbp,1)
lea    (%r15,%rbp,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%r12,%r15,1),%rbp
mov    %r13,(%r15,%r12,1)
lea    (%r15,%r12,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%r15,%rbp,1),%r12
mov    %r13,(%r15,%rbp,1)
lea    (%r15,%rbp,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
lea    (%r12,%r15,1),%r13
movabs $0x3ff0000000000000,%rax
mov    %rax,(%r15,%r12,1)
lea    (%r15,%r12,1),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    0x4(%rsp),%edx
add    %r15,%r13
add    $0xfffffff8,%edx
jne    1190 <Identity_Matrix+0x80>
add    0x8(%rsp),%rbx
mov    %rbx,%r13
movabs $0x3ff0000000000000,%r12
mov    (%rsp),%ebp
mov    0x10(%rsp),%rax
test   %ebp,%ebp
je     12c9 <Identity_Matrix+0x1b9>
lea    0x10(,%rax,8),%r15
mov    %r13,%rbx
nop
mov    %r12,(%rbx)
lea    0x8(%rbx),%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
add    %r15,%rbx
add    $0xffffffff,%ebp
jne    12b0 <Identity_Matrix+0x1a0>
mov    %r12,(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @Identity_Matrix(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store double* %0, double** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %26, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %29
12: ; preds = %7
%13 = load double*, double** %3
%14 = getelementptr inbounds double, double* %13, i32 1
store double* %14, double** %3
store double 1.000000e+00, double* %13
store i32 0, i32* %6
br label %15
15: ; preds = %22, %12
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load double*, double** %3
%21 = getelementptr inbounds double, double* %20, i32 1
store double* %21, double** %3
store double 0.000000e+00, double* %20
br label %22
22: ; preds = %19
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %15
25: ; preds = %15
br label %26
26: ; preds = %25
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %7
29: ; preds = %7
%30 = load double*, double** %3
store double 1.000000e+00, double* %30
ret void
}

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