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

; 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.9 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @mat_create(i64 noundef, i64 noundef, ptr noundef) #0

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

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

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

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

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

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

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

17:                                               ; preds = %12, %2
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef @.str.9) #5
  call void @exit(i32 noundef 1) #6
  unreachable

20:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Matrix, ptr %21, i32 0, i32 0
  %23 = load i64, ptr %22, align 8, !tbaa !11
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Matrix, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !14
  %27 = mul nsw i64 %23, %26
  %28 = mul i64 %27, 8
  %29 = call noalias ptr @malloc(i64 noundef %28) #7
  store ptr %29, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !15
  br label %30

30:                                               ; preds = %53, %20
  %31 = load i64, ptr %6, align 8, !tbaa !15
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Matrix, ptr %32, i32 0, i32 0
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Matrix, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = mul nsw i64 %34, %37
  %39 = icmp slt i64 %31, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %56

41:                                               ; preds = %30
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Matrix, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !16
  %45 = load i64, ptr %6, align 8, !tbaa !15
  %46 = getelementptr inbounds double, ptr %44, i64 %45
  %47 = load double, ptr %46, align 8, !tbaa !9
  %48 = load double, ptr %4, align 8, !tbaa !9
  %49 = fmul double %47, %48
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load i64, ptr %6, align 8, !tbaa !15
  %52 = getelementptr inbounds double, ptr %50, i64 %51
  store double %49, ptr %52, align 8, !tbaa !9
  br label %53

53:                                               ; preds = %41
  %54 = load i64, ptr %6, align 8, !tbaa !15
  %55 = add nsw i64 %54, 1
  store i64 %55, ptr %6, align 8, !tbaa !15
  br label %30, !llvm.loop !17

56:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.Matrix, ptr %57, i32 0, i32 0
  %59 = load i64, ptr %58, align 8, !tbaa !11
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Matrix, ptr %60, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !14
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = call ptr @mat_create(i64 noundef %59, i64 noundef %62, ptr noundef %63)
  store ptr %64, ptr %7, align 8, !tbaa !5
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret ptr %65
}

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 = { 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 = { 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn nounwind }
attributes #7 = { 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 = !{!"any pointer", !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 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"long long", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !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.9 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @mat_create(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

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

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

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

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

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

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

.lr.ph:                                           ; preds = %5
  %12 = mul nuw nsw i64 %7, %3
  %13 = shl i64 %12, 3
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #6
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %min.iters.check = icmp ult i64 %12, 4
  %17 = ptrtoint ptr %16 to i64
  %18 = ptrtoint ptr %14 to i64
  %19 = sub i64 %18, %17
  %diff.check = icmp ult i64 %19, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %.018.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %xtraiter = and i64 %12, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %.018.prol = phi i64 [ %24, %scalar.ph.prol ], [ %.018.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %20 = getelementptr inbounds double, ptr %16, i64 %.018.prol
  %21 = load double, ptr %20, align 8, !tbaa !14
  %22 = fmul double %21, %1
  %23 = getelementptr inbounds double, ptr %14, i64 %.018.prol
  store double %22, ptr %23, align 8, !tbaa !14
  %24 = add nuw nsw i64 %.018.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !16

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %.018.unr = phi i64 [ %.018.ph, %scalar.ph.preheader ], [ %24, %scalar.ph.prol ]
  %25 = sub nsw i64 %.018.ph, %12
  %26 = icmp ugt i64 %25, -4
  br i1 %26, label %._crit_edge, label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %12, 9223372036854775804
  %broadcast.splatinsert = insertelement <2 x double> poison, double %1, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %27 = getelementptr inbounds double, ptr %16, i64 %index
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  %wide.load = load <2 x double>, ptr %27, align 8, !tbaa !14
  %wide.load19 = load <2 x double>, ptr %28, align 8, !tbaa !14
  %29 = fmul <2 x double> %wide.load, %broadcast.splat
  %30 = fmul <2 x double> %wide.load19, %broadcast.splat
  %31 = getelementptr inbounds double, ptr %14, i64 %index
  %32 = getelementptr inbounds i8, ptr %31, i64 16
  store <2 x double> %29, ptr %31, align 8, !tbaa !14
  store <2 x double> %30, ptr %32, align 8, !tbaa !14
  %index.next = add nuw i64 %index, 4
  %33 = icmp eq i64 %index.next, %n.vec
  br i1 %33, label %middle.block, label %vector.body, !llvm.loop !18

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %12, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %34 = tail call ptr @mat_create(i64 noundef %3, i64 noundef %7, ptr noundef nonnull %14)
  ret ptr %34

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %.018 = phi i64 [ %54, %scalar.ph ], [ %.018.unr, %scalar.ph.prol.loopexit ]
  %35 = getelementptr inbounds double, ptr %16, i64 %.018
  %36 = load double, ptr %35, align 8, !tbaa !14
  %37 = fmul double %36, %1
  %38 = getelementptr inbounds double, ptr %14, i64 %.018
  store double %37, ptr %38, align 8, !tbaa !14
  %39 = add nuw nsw i64 %.018, 1
  %40 = getelementptr inbounds double, ptr %16, i64 %39
  %41 = load double, ptr %40, align 8, !tbaa !14
  %42 = fmul double %41, %1
  %43 = getelementptr inbounds double, ptr %14, i64 %39
  store double %42, ptr %43, align 8, !tbaa !14
  %44 = add nuw nsw i64 %.018, 2
  %45 = getelementptr inbounds double, ptr %16, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !14
  %47 = fmul double %46, %1
  %48 = getelementptr inbounds double, ptr %14, i64 %44
  store double %47, ptr %48, align 8, !tbaa !14
  %49 = add nuw nsw i64 %.018, 3
  %50 = getelementptr inbounds double, ptr %16, i64 %49
  %51 = load double, ptr %50, align 8, !tbaa !14
  %52 = fmul double %51, %1
  %53 = getelementptr inbounds double, ptr %14, i64 %49
  store double %52, ptr %53, align 8, !tbaa !14
  %54 = add nuw nsw i64 %.018, 4
  %exitcond.not.3 = icmp eq i64 %54, %12
  br i1 %exitcond.not.3, label %._crit_edge, label %scalar.ph, !llvm.loop !22
}

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 = { 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 #2 = { 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 #3 = { 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 #4 = { cold nounwind }
attributes #5 = { noreturn nounwind }
attributes #6 = { 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, !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 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 16}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}
!18 = distinct !{!18, !19, !20, !21}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!"llvm.loop.isvectorized", i32 1}
!21 = !{!"llvm.loop.unroll.runtime.disable"}
!22 = distinct !{!22, !19, !20}


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