; LLVM IR from aacoppa/flaming-flamingos :: src/matrix (matrix)

; 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 { i32, i32, ptr }

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

13:                                               ; preds = %45, %2
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %48

17:                                               ; preds = %13
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = mul i64 8, %19
  %21 = call noalias ptr @malloc(i64 noundef %20) #3
  %22 = getelementptr inbounds %struct.matrix, ptr %3, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds ptr, ptr %23, i64 %25
  store ptr %21, ptr %26, align 8, !tbaa !12
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %41, %17
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %44

31:                                               ; preds = %27
  %32 = getelementptr inbounds %struct.matrix, ptr %3, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds ptr, ptr %33, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds double, ptr %37, i64 %39
  store double 1.000000e+00, ptr %40, align 8, !tbaa !13
  br label %41

41:                                               ; preds = %31
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %7, align 4, !tbaa !5
  br label %27, !llvm.loop !15

44:                                               ; preds = %27
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %6, align 4, !tbaa !5
  br label %13, !llvm.loop !17

48:                                               ; preds = %13
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = getelementptr inbounds %struct.matrix, ptr %3, i32 0, i32 0
  store i32 %49, ptr %50, align 8, !tbaa !18
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = getelementptr inbounds %struct.matrix, ptr %3, i32 0, i32 1
  store i32 %51, ptr %52, align 4, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %53 = load { i64, ptr }, ptr %3, align 8
  ret { i64, ptr } %53
}

; 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

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

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 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 #3 = { nounwind allocsize(0) }
attributes #4 = { 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, !11, i64 8}
!10 = !{!"", !6, i64 0, !6, i64 4, !11, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = !{!10, !6, i64 0}
!19 = !{!10, !6, i64 4}


; 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: nofree nounwind memory(write, argmem: none, inaccessiblemem: readwrite) uwtable
define dso_local { i64, ptr } @init_matrix(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = shl nsw i64 %3, 3
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #2
  %6 = icmp sgt i32 %0, 0
  br i1 %6, label %.lr.ph19, label %.._crit_edge20_crit_edge

.._crit_edge20_crit_edge:                         ; preds = %2
  %.pre = zext i32 %0 to i64
  br label %._crit_edge20

.lr.ph19:                                         ; preds = %2
  %7 = sext i32 %1 to i64
  %8 = shl nsw i64 %7, 3
  %9 = icmp sgt i32 %1, 0
  %wide.trip.count31 = zext nneg i32 %0 to i64
  br i1 %9, label %.lr.ph.us.preheader, label %.lr.ph19.split

.lr.ph.us.preheader:                              ; preds = %.lr.ph19
  %wide.trip.count26 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 4
  %n.vec = and i64 %wide.trip.count26, 2147483644
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count26
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %indvars.iv28 = phi i64 [ 0, %.lr.ph.us.preheader ], [ %indvars.iv.next29, %._crit_edge.us ]
  %10 = tail call noalias ptr @malloc(i64 noundef %8) #2
  %11 = getelementptr inbounds ptr, ptr %5, i64 %indvars.iv28
  store ptr %10, ptr %11, align 8, !tbaa !5
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.lr.ph.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.lr.ph.us ]
  %12 = getelementptr inbounds double, ptr %10, i64 %index
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  store <2 x double> <double 1.000000e+00, double 1.000000e+00>, ptr %12, align 8, !tbaa !9
  store <2 x double> <double 1.000000e+00, double 1.000000e+00>, ptr %13, align 8, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph.us
  %indvars.iv23.ph = phi i64 [ 0, %.lr.ph.us ], [ %n.vec, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv23 = phi i64 [ %indvars.iv.next24, %scalar.ph ], [ %indvars.iv23.ph, %scalar.ph.preheader ]
  %15 = getelementptr inbounds double, ptr %10, i64 %indvars.iv23
  store double 1.000000e+00, ptr %15, align 8, !tbaa !9
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %exitcond27.not = icmp eq i64 %indvars.iv.next24, %wide.trip.count26
  br i1 %exitcond27.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !15

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %exitcond32.not = icmp eq i64 %indvars.iv.next29, %wide.trip.count31
  br i1 %exitcond32.not, label %._crit_edge20, label %.lr.ph.us, !llvm.loop !16

.lr.ph19.split:                                   ; preds = %.lr.ph19, %.lr.ph19.split
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph19.split ], [ 0, %.lr.ph19 ]
  %16 = tail call noalias ptr @malloc(i64 noundef %8) #2
  %17 = getelementptr inbounds ptr, ptr %5, i64 %indvars.iv
  store ptr %16, ptr %17, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count31
  br i1 %exitcond.not, label %._crit_edge20, label %.lr.ph19.split, !llvm.loop !16

._crit_edge20:                                    ; preds = %.lr.ph19.split, %._crit_edge.us, %.._crit_edge20_crit_edge
  %.sroa.0.0.insert.ext.pre-phi = phi i64 [ %.pre, %.._crit_edge20_crit_edge ], [ %wide.trip.count31, %._crit_edge.us ], [ %wide.trip.count31, %.lr.ph19.split ]
  %.sroa.2.0.insert.ext = zext i32 %1 to i64
  %.sroa.2.0.insert.shift = shl nuw i64 %.sroa.2.0.insert.ext, 32
  %.sroa.0.0.insert.insert = or disjoint i64 %.sroa.2.0.insert.shift, %.sroa.0.0.insert.ext.pre-phi
  %.fca.0.insert = insertvalue { i64, ptr } poison, i64 %.sroa.0.0.insert.insert, 0
  %.fca.1.insert = insertvalue { i64, ptr } %.fca.0.insert, ptr %5, 1
  ret { i64, ptr } %.fca.1.insert
}

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

attributes #0 = { nofree nounwind memory(write, argmem: none, 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 #1 = { 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 #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 = !{!"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 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}
!16 = distinct !{!16, !12}


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