; LLVM IR from CLASS-SZ/class_sz :: class-sz/tools (r8lib)

; 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"

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

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @r8mat_diagonal_new(i32 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = mul nsw i32 %8, %9
  %11 = sext i32 %10 to i64
  %12 = mul i64 %11, 8
  %13 = call noalias ptr @malloc(i64 noundef %12) #4
  store ptr %13, ptr %5, align 8, !tbaa !9
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %55, %2
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %58

18:                                               ; preds = %14
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %51, %18
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %23, label %54

23:                                               ; preds = %19
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = icmp eq i32 %24, %25
  br i1 %26, label %27, label %41

27:                                               ; preds = %23
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds double, ptr %28, i64 %30
  %32 = load double, ptr %31, align 8, !tbaa !11
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = mul nsw i32 %35, %36
  %38 = add nsw i32 %34, %37
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds double, ptr %33, i64 %39
  store double %32, ptr %40, align 8, !tbaa !11
  br label %50

41:                                               ; preds = %23
  %42 = load ptr, ptr %5, align 8, !tbaa !9
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = mul nsw i32 %44, %45
  %47 = add nsw i32 %43, %46
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds double, ptr %42, i64 %48
  store double 0.000000e+00, ptr %49, align 8, !tbaa !11
  br label %50

50:                                               ; preds = %41, %27
  br label %51

51:                                               ; preds = %50
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %6, align 4, !tbaa !5
  br label %19, !llvm.loop !13

54:                                               ; preds = %19
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !5
  br label %14, !llvm.loop !15

58:                                               ; preds = %14
  %59 = load ptr, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %59
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 #2 = { 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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"

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind memory(write, argmem: read, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @r8mat_diagonal_new(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 {
  %3 = mul nsw i32 %0, %0
  %4 = zext nneg i32 %3 to i64
  %5 = shl nuw nsw i64 %4, 3
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #2
  %7 = icmp sgt i32 %0, 0
  br i1 %7, label %.preheader.us.preheader, label %._crit_edge24

.preheader.us.preheader:                          ; preds = %2
  %8 = zext nneg i32 %0 to i64
  %xtraiter = and i64 %8, 1
  %9 = icmp eq i32 %0, 1
  %unroll_iter = and i64 %8, 2147483646
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv26 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next27, %._crit_edge.us ]
  %10 = mul nuw nsw i64 %indvars.iv26, %8
  %invariant.gep = getelementptr inbounds double, ptr %6, i64 %10
  %11 = getelementptr inbounds double, ptr %1, i64 %indvars.iv26
  %12 = getelementptr inbounds double, ptr %6, i64 %indvars.iv26
  %13 = getelementptr inbounds double, ptr %12, i64 %10
  br i1 %9, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %23
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %23 ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.1, %23 ], [ 0, %.preheader.us ]
  %14 = icmp eq i64 %indvars.iv, %indvars.iv26
  br i1 %14, label %16, label %15

15:                                               ; preds = %.preheader.us.new
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv
  store double 0.000000e+00, ptr %gep, align 8, !tbaa !5
  br label %18

16:                                               ; preds = %.preheader.us.new
  %17 = load double, ptr %11, align 8, !tbaa !5
  store double %17, ptr %13, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %16, %15
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %19 = icmp eq i64 %indvars.iv.next, %indvars.iv26
  br i1 %19, label %21, label %20

20:                                               ; preds = %18
  %gep.1 = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv.next
  store double 0.000000e+00, ptr %gep.1, align 8, !tbaa !5
  br label %23

21:                                               ; preds = %18
  %22 = load double, ptr %11, align 8, !tbaa !5
  store double %22, ptr %13, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %21, %20
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !9

._crit_edge.us.unr-lcssa:                         ; preds = %23, %.preheader.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next.1, %23 ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa
  %24 = icmp eq i64 %indvars.iv.unr, %indvars.iv26
  br i1 %24, label %26, label %25

25:                                               ; preds = %.epil.preheader
  %gep.epil = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv.unr
  store double 0.000000e+00, ptr %gep.epil, align 8, !tbaa !5
  br label %._crit_edge.us

26:                                               ; preds = %.epil.preheader
  %27 = load double, ptr %11, align 8, !tbaa !5
  store double %27, ptr %13, align 8, !tbaa !5
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %25, %26, %._crit_edge.us.unr-lcssa
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next27, %8
  br i1 %exitcond30.not, label %._crit_edge24, label %.preheader.us, !llvm.loop !11

._crit_edge24:                                    ; preds = %._crit_edge.us, %2
  ret ptr %6
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree nounwind memory(write, argmem: read, inaccessiblemem: readwrite) 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 #2 = { nounwind allocsize(0) }

!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: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass