#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fail(char *, int);
int matrix_trace(float *r, float *a, int nn) {
  int n;
  switch (nn) {
  case 1:
    n = 1;
    break;
  case 4:
    n = 2;
    break;
  case 9:
    n = 3;
    break;
  default:
    fail("can not compute trace of object of size %d", nn);
  }
  assert(n * n == nn);
  *r = 0;
  for (int i = 0; i < n; i++)
    *r += a[i * n + i];
  return 1;
}


x86 assembly (clang, -O0):
<matrix_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x1,%eax
je     1169 <matrix_trace+0x49>
jmp    1147 <matrix_trace+0x27>
mov    -0x20(%rbp),%eax
sub    $0x4,%eax
je     1175 <matrix_trace+0x55>
jmp    1158 <matrix_trace+0x38>
mov    -0x20(%rbp),%eax
sub    $0x9,%eax
je     1181 <matrix_trace+0x61>
jmp    118d <matrix_trace+0x6d>
movl   $0x1,-0x18(%rbp)
jmp    119c <matrix_trace+0x7c>
movl   $0x2,-0x18(%rbp)
jmp    119c <matrix_trace+0x7c>
movl   $0x3,-0x18(%rbp)
jmp    119c <matrix_trace+0x7c>
mov    -0x14(%rbp),%esi
lea    0xe69(%rip),%rdi
call   1030 <fail@plt>
mov    -0x18(%rbp),%eax
imul   -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1201 <matrix_trace+0xe1>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x8(%rbp),%rax
addss  (%rax),%xmm0
movss  %xmm0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c5 <matrix_trace+0xa5>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrix_trace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r13
mov    $0x1,%r12b
cmp    $0x1,%edx
je     114d <matrix_trace+0x2d>
cmp    $0x9,%ebp
je     1152 <matrix_trace+0x32>
cmp    $0x4,%ebp
jne    115a <matrix_trace+0x3a>
mov    $0x2,%r15d
jmp    116b <matrix_trace+0x4b>
mov    %ebp,%r15d
jmp    116b <matrix_trace+0x4b>
mov    $0x3,%r15d
jmp    116b <matrix_trace+0x4b>
lea    0xe9f(%rip),%rdi
mov    %ebp,%esi
call   1030 <fail@plt>
xor    %r12d,%r12d
mov    %r15d,%eax
imul   %r15d,%eax
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1040 <assert@plt>
movl   $0x0,0x0(%r13)
test   %r12b,%r12b
je     122c <matrix_trace+0x10c>
mov    %r15d,%r8d
xor    %eax,%eax
cmp    $0x1,%r15d
jne    11a9 <matrix_trace+0x89>
xorps  %xmm0,%xmm0
test   $0x1,%r8b
jne    1216 <matrix_trace+0xf6>
jmp    122c <matrix_trace+0x10c>
lea    (%r15,%r15,1),%edx
mov    %r8d,%r9d
and    $0xfffffffe,%r9d
neg    %r9
xorps  %xmm0,%xmm0
mov    $0x1,%ebp
xor    %edi,%edi
mov    %r15d,%ecx
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %ecx,%rcx
cltq
lea    (%rbx,%rax,1),%rsi
addss  (%r14,%rsi,4),%xmm0
lea    (%rcx,%rbp,1),%rsi
movss  %xmm0,0x0(%r13)
addss  (%r14,%rsi,4),%xmm0
movss  %xmm0,0x0(%r13)
add    $0x2,%rbx
add    %edx,%eax
add    $0x2,%rbp
add    %edx,%ecx
add    $0xfffffffffffffffe,%rdi
cmp    %rdi,%r9
jne    11d0 <matrix_trace+0xb0>
neg    %rdi
mov    %rdi,%rax
test   $0x1,%r8b
je     122c <matrix_trace+0x10c>
imul   %eax,%r15d
movslq %r15d,%rcx
add    %rax,%rcx
addss  (%r14,%rcx,4),%xmm0
movss  %xmm0,0x0(%r13)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"can not compute trace of object of size %d\00"
define i32 @matrix_trace(float* %0, float* %1, i32 %2) {
%4 = alloca float*
%5 = alloca float*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store float* %0, float** %4
store float* %1, float** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
switch i32 %9, label %13 [
i32 1, label %10
i32 4, label %11
i32 9, label %12
]
10: ; preds = %3
store i32 1, i32* %7
br label %16
11: ; preds = %3
store i32 2, i32* %7
br label %16
12: ; preds = %3
store i32 3, i32* %7
br label %16
13: ; preds = %3
%14 = load i32, i32* %6
%15 = call i32 @fail(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13, %12, %11, %10
%17 = load i32, i32* %7
%18 = load i32, i32* %7
%19 = mul nsw i32 %17, %18
%20 = load i32, i32* %6
%21 = icmp eq i32 %19, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load float*, float** %4
store float 0.000000e+00, float* %24
store i32 0, i32* %8
br label %25
25: ; preds = %42, %16
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %45
29: ; preds = %25
%30 = load float*, float** %5
%31 = load i32, i32* %8
%32 = load i32, i32* %7
%33 = mul nsw i32 %31, %32
%34 = load i32, i32* %8
%35 = add nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds float, float* %30, i64 %36
%38 = load float, float* %37
%39 = load float*, float** %4
%40 = load float, float* %39
%41 = fadd float %40, %38
store float %41, float* %39
br label %42
42: ; preds = %29
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %25
45: ; preds = %25
ret i32 1
}

LLVM IR (-O2):
@.str = [43 x i8] c"can not compute trace of object of size %d\00"
define i32 @matrix_trace(float* nocapture %0, float* nocapture %1, i32 %2) {
switch i32 %2, label %6 [
i32 1, label %8
i32 4, label %4
i32 9, label %5
]
4: ; preds = %3
br label %8
5: ; preds = %3
br label %8
6: ; preds = %3
%7 = tail call i32 @fail(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %8
8: ; preds = %3, %6, %5, %4
%9 = phi i1 [ true, %3 ], [ false, %6 ], [ true, %5 ], [ true, %4 ]
%10 = phi i32 [ %2, %3 ], [ undef, %6 ], [ 3, %5 ], [ 2, %4 ]
%11 = mul nuw nsw i32 %10, %10
%12 = icmp eq i32 %11, %2
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
store float 0.000000e+00, float* %0
br i1 %9, label %15, label %33
15: ; preds = %8
%16 = zext i32 %10 to i64
%17 = and i64 %16, 1
%18 = icmp eq i32 %10, 1
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = and i64 %16, 4294967294
br label %34
21: ; preds = %34, %15
%22 = phi float [ 0.000000e+00, %15 ], [ %52, %34 ]
%23 = phi i64 [ 0, %15 ], [ %53, %34 ]
%24 = icmp eq i64 %17, 0
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = trunc i64 %23 to i32
%27 = mul nsw i32 %10, %26
%28 = sext i32 %27 to i64
%29 = add nsw i64 %23, %28
%30 = getelementptr inbounds float, float* %1, i64 %29
%31 = load float, float* %30
%32 = fadd float %31, %22
store float %32, float* %0
br label %33
33: ; preds = %25, %21, %8
ret i32 1
34: ; preds = %34, %19
%35 = phi float [ 0.000000e+00, %19 ], [ %52, %34 ]
%36 = phi i64 [ 0, %19 ], [ %53, %34 ]
%37 = phi i64 [ 0, %19 ], [ %54, %34 ]
%38 = trunc i64 %36 to i32
%39 = mul nsw i32 %10, %38
%40 = sext i32 %39 to i64
%41 = add nsw i64 %36, %40
%42 = getelementptr inbounds float, float* %1, i64 %41
%43 = load float, float* %42
%44 = fadd float %43, %35
store float %44, float* %0
%45 = or i64 %36, 1
%46 = trunc i64 %45 to i32
%47 = mul nsw i32 %10, %46
%48 = sext i32 %47 to i64
%49 = add nsw i64 %45, %48
%50 = getelementptr inbounds float, float* %1, i64 %49
%51 = load float, float* %50
%52 = fadd float %51, %44
store float %52, float* %0
%53 = add nuw nsw i64 %36, 2
%54 = add i64 %37, 2
%55 = icmp eq i64 %54, %20
br i1 %55, label %21, label %34
}