; LLVM IR from YanzhenHuang/c-nn :: linalg (mat_elemSum)

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

%struct.Matrix = type { i64, i64, ptr }

@stderr = external global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [55 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local double @mat_elemSum(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.Matrix, ptr %5, i32 0, i32 0
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %8 = icmp sle i64 %7, 0
  br i1 %8, label %14, label %9

9:                                                ; preds = %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Matrix, ptr %10, i32 0, i32 1
  %12 = load i64, ptr %11, align 8, !tbaa !12
  %13 = icmp sle i64 %12, 0
  br i1 %13, label %14, label %17

14:                                               ; preds = %9, %1
  %15 = load ptr, ptr @stderr, align 8, !tbaa !5
  %16 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef @.str.6) #4
  call void @exit(i32 noundef 1) #5
  unreachable

17:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store double 0.000000e+00, ptr %3, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 0, ptr %4, align 8, !tbaa !15
  br label %18

18:                                               ; preds = %38, %17
  %19 = load i64, ptr %4, align 8, !tbaa !15
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Matrix, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Matrix, ptr %23, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !12
  %26 = mul nsw i64 %22, %25
  %27 = icmp slt i64 %19, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %41

29:                                               ; preds = %18
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Matrix, ptr %30, i32 0, i32 2
  %32 = load ptr, ptr %31, align 8, !tbaa !16
  %33 = load i64, ptr %4, align 8, !tbaa !15
  %34 = getelementptr inbounds double, ptr %32, i64 %33
  %35 = load double, ptr %34, align 8, !tbaa !13
  %36 = load double, ptr %3, align 8, !tbaa !13
  %37 = fadd double %36, %35
  store double %37, ptr %3, align 8, !tbaa !13
  br label %38

38:                                               ; preds = %29
  %39 = load i64, ptr %4, align 8, !tbaa !15
  %40 = add nsw i64 %39, 1
  store i64 %40, ptr %4, align 8, !tbaa !15
  br label %18, !llvm.loop !17

41:                                               ; preds = %28
  %42 = load double, ptr %3, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret double %42
}

attributes #0 = { nounwind "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 = { noreturn nounwind "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 8, !6, i64 16}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [55 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local double @mat_elemSum(ptr nocapture noundef readonly %0) local_unnamed_addr #2 {
  %2 = load i64, ptr %0, align 8, !tbaa !5
  %3 = icmp slt i64 %2, 1
  br i1 %3, label %12, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = icmp slt i64 %6, 1
  br i1 %7, label %12, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %8 = mul i64 %6, %2
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %smax = tail call i64 @llvm.smax.i64(i64 %8, i64 1)
  %xtraiter = and i64 %smax, 7
  %11 = icmp slt i64 %8, 8
  br i1 %11, label %._crit_edge.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %smax, 9223372036854775800
  br label %19

12:                                               ; preds = %4, %1
  %13 = load ptr, ptr @stderr, align 8, !tbaa !13
  %14 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef nonnull @.str.6) #4
  tail call void @exit(i32 noundef 1) #5
  unreachable

._crit_edge.unr-lcssa:                            ; preds = %19, %.lr.ph
  %.lcssa.ph = phi double [ poison, %.lr.ph ], [ %50, %19 ]
  %.011.unr = phi i64 [ 0, %.lr.ph ], [ %51, %19 ]
  %.0910.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %50, %19 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.unr-lcssa, %.epil.preheader
  %.011.epil = phi i64 [ %18, %.epil.preheader ], [ %.011.unr, %._crit_edge.unr-lcssa ]
  %.0910.epil = phi double [ %17, %.epil.preheader ], [ %.0910.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.unr-lcssa ]
  %15 = getelementptr inbounds double, ptr %10, i64 %.011.epil
  %16 = load double, ptr %15, align 8, !tbaa !14
  %17 = fadd double %.0910.epil, %16
  %18 = add nuw nsw i64 %.011.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !16

._crit_edge:                                      ; preds = %.epil.preheader, %._crit_edge.unr-lcssa
  %.lcssa = phi double [ %.lcssa.ph, %._crit_edge.unr-lcssa ], [ %17, %.epil.preheader ]
  ret double %.lcssa

19:                                               ; preds = %19, %.lr.ph.new
  %.011 = phi i64 [ 0, %.lr.ph.new ], [ %51, %19 ]
  %.0910 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %50, %19 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.7, %19 ]
  %20 = getelementptr inbounds double, ptr %10, i64 %.011
  %21 = load double, ptr %20, align 8, !tbaa !14
  %22 = fadd double %.0910, %21
  %23 = or disjoint i64 %.011, 1
  %24 = getelementptr inbounds double, ptr %10, i64 %23
  %25 = load double, ptr %24, align 8, !tbaa !14
  %26 = fadd double %22, %25
  %27 = or disjoint i64 %.011, 2
  %28 = getelementptr inbounds double, ptr %10, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !14
  %30 = fadd double %26, %29
  %31 = or disjoint i64 %.011, 3
  %32 = getelementptr inbounds double, ptr %10, i64 %31
  %33 = load double, ptr %32, align 8, !tbaa !14
  %34 = fadd double %30, %33
  %35 = or disjoint i64 %.011, 4
  %36 = getelementptr inbounds double, ptr %10, i64 %35
  %37 = load double, ptr %36, align 8, !tbaa !14
  %38 = fadd double %34, %37
  %39 = or disjoint i64 %.011, 5
  %40 = getelementptr inbounds double, ptr %10, i64 %39
  %41 = load double, ptr %40, align 8, !tbaa !14
  %42 = fadd double %38, %41
  %43 = or disjoint i64 %.011, 6
  %44 = getelementptr inbounds double, ptr %10, i64 %43
  %45 = load double, ptr %44, align 8, !tbaa !14
  %46 = fadd double %42, %45
  %47 = or disjoint i64 %.011, 7
  %48 = getelementptr inbounds double, ptr %10, i64 %47
  %49 = load double, ptr %48, align 8, !tbaa !14
  %50 = fadd double %46, %49
  %51 = add nuw nsw i64 %.011, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.unr-lcssa, label %19, !llvm.loop !18
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #3

attributes #0 = { nofree nounwind "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 = { noreturn nounwind "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { cold nounwind }
attributes #5 = { noreturn 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!10, !10, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass