; LLVM IR from bondhugula/pluto :: examples/matmul (matmul)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@A = external dso_local global [2048 x [2048 x double]], align 16
@B = external dso_local global [2048 x [2048 x double]], align 16
@C = external dso_local global [2048 x [2048 x double]], align 16

; Function Attrs: nounwind uwtable
define dso_local void @init_array() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %41, %0
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = icmp slt i32 %4, 2048
  br i1 %5, label %6, label %44

6:                                                ; preds = %3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %37, %6
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 2048
  br i1 %9, label %10, label %40

10:                                               ; preds = %7
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = add nsw i32 %11, %12
  %14 = sitofp i32 %13 to double
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [2048 x [2048 x double]], ptr @A, i64 0, i64 %16
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [2048 x double], ptr %17, i64 0, i64 %19
  store double %14, ptr %20, align 8, !tbaa !9
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = mul nsw i32 %21, %22
  %24 = sitofp i32 %23 to double
  %25 = load i32, ptr %1, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [2048 x [2048 x double]], ptr @B, i64 0, i64 %26
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [2048 x double], ptr %27, i64 0, i64 %29
  store double %24, ptr %30, align 8, !tbaa !9
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2048 x [2048 x double]], ptr @C, i64 0, i64 %32
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2048 x double], ptr %33, i64 0, i64 %35
  store double 0.000000e+00, ptr %36, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %10
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !11

40:                                               ; preds = %7
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %1, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %1, align 4, !tbaa !5
  br label %3, !llvm.loop !13

44:                                               ; preds = %3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@A = external dso_local local_unnamed_addr global [2048 x [2048 x double]], align 16
@B = external dso_local local_unnamed_addr global [2048 x [2048 x double]], align 16
@C = external dso_local local_unnamed_addr global [2048 x [2048 x double]], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(write, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @init_array() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(33554432) @C, i8 0, i64 33554432, i1 false), !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %0, %18
  %indvar = phi i64 [ 0, %0 ], [ %indvar.next, %18 ]
  br label %1

1:                                                ; preds = %1, %.preheader
  %indvars.iv = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.1, %1 ]
  %2 = add nuw nsw i64 %indvars.iv, %indvar
  %3 = trunc nuw nsw i64 %2 to i32
  %4 = uitofp nneg i32 %3 to double
  %5 = getelementptr inbounds [2048 x [2048 x double]], ptr @A, i64 0, i64 %indvar, i64 %indvars.iv
  store double %4, ptr %5, align 16, !tbaa !5
  %6 = mul nuw nsw i64 %indvars.iv, %indvar
  %7 = trunc nuw nsw i64 %6 to i32
  %8 = uitofp nneg i32 %7 to double
  %9 = getelementptr inbounds [2048 x [2048 x double]], ptr @B, i64 0, i64 %indvar, i64 %indvars.iv
  store double %8, ptr %9, align 16, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %10 = add nuw nsw i64 %indvars.iv.next, %indvar
  %11 = trunc nuw nsw i64 %10 to i32
  %12 = uitofp nneg i32 %11 to double
  %13 = getelementptr inbounds [2048 x [2048 x double]], ptr @A, i64 0, i64 %indvar, i64 %indvars.iv.next
  store double %12, ptr %13, align 8, !tbaa !5
  %14 = mul nuw nsw i64 %indvars.iv.next, %indvar
  %15 = trunc nuw nsw i64 %14 to i32
  %16 = uitofp nneg i32 %15 to double
  %17 = getelementptr inbounds [2048 x [2048 x double]], ptr @B, i64 0, i64 %indvar, i64 %indvars.iv.next
  store double %16, ptr %17, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, 2048
  br i1 %exitcond.not.1, label %18, label %1, !llvm.loop !9

18:                                               ; preds = %1
  %indvar.next = add nuw nsw i64 %indvar, 1
  %exitcond19.not = icmp eq i64 %indvar.next, 2048
  br i1 %exitcond19.not, label %19, label %.preheader, !llvm.loop !11

19:                                               ; preds = %18
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(write, argmem: none, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass