; LLVM IR from buresjan/optim_lbm :: nomad/ext/sgtelib/src/Matrix.ll (ZN7SGTELIB6Matrix10set_randomEddb)

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

%"class.SGTELIB::Matrix" = type { ptr, %"class.std::__cxx11::basic_string", i32, i32, ptr }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

; 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: mustprogress uwtable
define dso_local void @_ZN7SGTELIB6Matrix10set_randomEddb(ptr noundef nonnull align 8 dereferenceable(56) %0, double noundef %1, double noundef %2, i1 noundef zeroext %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store double %1, ptr %6, align 8, !tbaa !9
  store double %2, ptr %7, align 8, !tbaa !9
  %11 = zext i1 %3 to i8
  store i8 %11, ptr %8, align 1, !tbaa !11
  %12 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %9, align 4, !tbaa !13
  br label %13

13:                                               ; preds = %69, %4
  %14 = load i32, ptr %9, align 4, !tbaa !13
  %15 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %12, i32 0, i32 2
  %16 = load i32, ptr %15, align 8, !tbaa !15
  %17 = icmp slt i32 %14, %16
  br i1 %17, label %18, label %72

18:                                               ; preds = %13
  store i32 0, ptr %10, align 4, !tbaa !13
  br label %19

19:                                               ; preds = %65, %18
  %20 = load i32, ptr %10, align 4, !tbaa !13
  %21 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %12, i32 0, i32 3
  %22 = load i32, ptr %21, align 4, !tbaa !20
  %23 = icmp slt i32 %20, %22
  br i1 %23, label %24, label %68

24:                                               ; preds = %19
  %25 = load double, ptr %6, align 8, !tbaa !9
  %26 = load double, ptr %7, align 8, !tbaa !9
  %27 = load double, ptr %6, align 8, !tbaa !9
  %28 = fsub double %26, %27
  %29 = call noundef double @_ZN7SGTELIB12uniform_randEv()
  %30 = call double @llvm.fmuladd.f64(double %28, double %29, double %25)
  %31 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %12, i32 0, i32 4
  %32 = load ptr, ptr %31, align 8, !tbaa !21
  %33 = load i32, ptr %9, align 4, !tbaa !13
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds ptr, ptr %32, i64 %34
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = load i32, ptr %10, align 4, !tbaa !13
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds double, ptr %36, i64 %38
  store double %30, ptr %39, align 8, !tbaa !9
  %40 = load i8, ptr %8, align 1, !tbaa !11, !range !22, !noundef !23
  %41 = trunc i8 %40 to i1
  br i1 %41, label %42, label %64

42:                                               ; preds = %24
  %43 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %12, i32 0, i32 4
  %44 = load ptr, ptr %43, align 8, !tbaa !21
  %45 = load i32, ptr %9, align 4, !tbaa !13
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds ptr, ptr %44, i64 %46
  %48 = load ptr, ptr %47, align 8, !tbaa !5
  %49 = load i32, ptr %10, align 4, !tbaa !13
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds double, ptr %48, i64 %50
  %52 = load double, ptr %51, align 8, !tbaa !9
  %53 = call noundef i32 @_ZN7SGTELIB5roundEd(double noundef %52)
  %54 = sitofp i32 %53 to double
  %55 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %12, i32 0, i32 4
  %56 = load ptr, ptr %55, align 8, !tbaa !21
  %57 = load i32, ptr %9, align 4, !tbaa !13
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds ptr, ptr %56, i64 %58
  %60 = load ptr, ptr %59, align 8, !tbaa !5
  %61 = load i32, ptr %10, align 4, !tbaa !13
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds double, ptr %60, i64 %62
  store double %54, ptr %63, align 8, !tbaa !9
  br label %64

64:                                               ; preds = %42, %24
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %10, align 4, !tbaa !13
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %10, align 4, !tbaa !13
  br label %19, !llvm.loop !24

68:                                               ; preds = %19
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %9, align 4, !tbaa !13
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %9, align 4, !tbaa !13
  br label %13, !llvm.loop !26

72:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret void
}

declare noundef double @_ZN7SGTELIB12uniform_randEv() #2

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

declare noundef i32 @_ZN7SGTELIB5roundEd(double noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { "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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !14, i64 40}
!16 = !{!"_ZTSN7SGTELIB6MatrixE", !17, i64 8, !14, i64 40, !14, i64 44, !6, i64 48}
!17 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !18, i64 0, !19, i64 8, !7, i64 16}
!18 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !6, i64 0}
!19 = !{!"long", !7, i64 0}
!20 = !{!16, !14, i64 44}
!21 = !{!16, !6, i64 48}
!22 = !{i8 0, i8 2}
!23 = !{}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = distinct !{!26, !25}


; 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 uwtable
define dso_local void @_ZN7SGTELIB6Matrix10set_randomEddb(ptr nocapture noundef nonnull readonly align 8 dereferenceable(56) %0, double noundef %1, double noundef %2, i1 noundef zeroext %3) local_unnamed_addr #0 align 2 {
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.preheader.lr.ph, label %._crit_edge17

.preheader.lr.ph:                                 ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 44
  %9 = fsub double %2, %1
  %10 = getelementptr inbounds i8, ptr %0, i64 48
  %11 = load i32, ptr %8, align 4, !tbaa !14
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.preheader, label %._crit_edge17

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %13 = phi i32 [ %39, %._crit_edge ], [ %11, %.preheader.lr.ph ]
  %indvars.iv24 = phi i64 [ %indvars.iv.next25, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  br i1 %3, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %.lr.ph.split.us ], [ 0, %.lr.ph ]
  %15 = tail call noundef double @_ZN7SGTELIB12uniform_randEv()
  %16 = tail call double @llvm.fmuladd.f64(double %9, double %15, double %1)
  %17 = load ptr, ptr %10, align 8, !tbaa !15
  %18 = getelementptr inbounds ptr, ptr %17, i64 %indvars.iv24
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = getelementptr inbounds double, ptr %19, i64 %indvars.iv21
  store double %16, ptr %20, align 8, !tbaa !17
  %21 = tail call noundef i32 @_ZN7SGTELIB5roundEd(double noundef %16)
  %22 = sitofp i32 %21 to double
  %23 = load ptr, ptr %10, align 8, !tbaa !15
  %24 = getelementptr inbounds ptr, ptr %23, i64 %indvars.iv24
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = getelementptr inbounds double, ptr %25, i64 %indvars.iv21
  store double %22, ptr %26, align 8, !tbaa !17
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
  %27 = load i32, ptr %8, align 4, !tbaa !14
  %28 = sext i32 %27 to i64
  %29 = icmp slt i64 %indvars.iv.next22, %28
  br i1 %29, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !19

.lr.ph.split:                                     ; preds = %.lr.ph, %.lr.ph.split
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph.split ], [ 0, %.lr.ph ]
  %30 = tail call noundef double @_ZN7SGTELIB12uniform_randEv()
  %31 = tail call double @llvm.fmuladd.f64(double %9, double %30, double %1)
  %32 = load ptr, ptr %10, align 8, !tbaa !15
  %33 = getelementptr inbounds ptr, ptr %32, i64 %indvars.iv24
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = getelementptr inbounds double, ptr %34, i64 %indvars.iv
  store double %31, ptr %35, align 8, !tbaa !17
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = load i32, ptr %8, align 4, !tbaa !14
  %37 = sext i32 %36 to i64
  %38 = icmp slt i64 %indvars.iv.next, %37
  br i1 %38, label %.lr.ph.split, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %.lr.ph.split, %.lr.ph.split.us, %.preheader
  %39 = phi i32 [ %13, %.preheader ], [ %27, %.lr.ph.split.us ], [ %36, %.lr.ph.split ]
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
  %40 = load i32, ptr %5, align 8, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = icmp slt i64 %indvars.iv.next25, %41
  br i1 %42, label %.preheader, label %._crit_edge17, !llvm.loop !21

._crit_edge17:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %4
  ret void
}

declare noundef double @_ZN7SGTELIB12uniform_randEv() local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #2

declare noundef i32 @_ZN7SGTELIB5roundEd(double noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { "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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !13, i64 40}
!6 = !{!"_ZTSN7SGTELIB6MatrixE", !7, i64 8, !13, i64 40, !13, i64 44, !9, i64 48}
!7 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !8, i64 0, !12, i64 8, !10, i64 16}
!8 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !9, i64 0}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"long", !10, i64 0}
!13 = !{!"int", !10, i64 0}
!14 = !{!6, !13, i64 44}
!15 = !{!6, !9, i64 48}
!16 = !{!9, !9, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"double", !10, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20, !22}
!22 = !{!"llvm.loop.unswitch.partial.disable"}


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