; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R2Shapes/R2Grid.ll (ZN6R2Grid8AddNoiseEd)

; 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.R2Grid = type { %class.R2Affine, %class.R2Affine, double, ptr, [2 x i32], i32, i32 }
%class.R2Affine = type <{ %class.R2Transformation, %class.R3Matrix, i32, [4 x i8] }>
%class.R2Transformation = type { ptr }
%class.R3Matrix = type { [3 x [3 x double]] }

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: nounwind
declare double @exp(double noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6R2Grid8AddNoiseEd(ptr noundef nonnull align 8 dereferenceable(208) %0, double noundef %1) #3 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca double, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store double %1, ptr %4, align 8, !tbaa !9
  %10 = load ptr, ptr %3, align 8
  %11 = load double, ptr %4, align 8, !tbaa !9
  %12 = fcmp oeq double %11, 0.000000e+00
  br i1 %12, label %13, label %14

13:                                               ; preds = %2
  br label %64

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !11
  br label %15

15:                                               ; preds = %61, %14
  %16 = load i32, ptr %5, align 4, !tbaa !11
  %17 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 6
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = icmp slt i32 %16, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %64

21:                                               ; preds = %15
  %22 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 3
  %23 = load ptr, ptr %22, align 8, !tbaa !18
  %24 = load i32, ptr %5, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds double, ptr %23, i64 %25
  %27 = load double, ptr %26, align 8, !tbaa !9
  %28 = fcmp oeq double %27, -3.210000e+02
  br i1 %28, label %29, label %30

29:                                               ; preds = %21
  br label %61

30:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %31 = call noundef double @_Z14RNRandomScalarv()
  store double %31, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %32 = call noundef double @_Z14RNRandomScalarv()
  %33 = fcmp olt double %32, 5.000000e-01
  %34 = zext i1 %33 to i64
  %35 = select i1 %33, double -1.000000e+00, double 1.000000e+00
  store double %35, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %36 = load double, ptr %4, align 8, !tbaa !9
  %37 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 3
  %38 = load ptr, ptr %37, align 8, !tbaa !18
  %39 = load i32, ptr %5, align 4, !tbaa !11
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds double, ptr %38, i64 %40
  %42 = load double, ptr %41, align 8, !tbaa !9
  %43 = fmul double %36, %42
  store double %43, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %44 = load double, ptr %8, align 8, !tbaa !9
  %45 = load double, ptr %6, align 8, !tbaa !9
  %46 = fmul double -5.000000e-01, %45
  %47 = load double, ptr %6, align 8, !tbaa !9
  %48 = fmul double %46, %47
  %49 = call double @exp(double noundef %48) #5
  %50 = fsub double 1.000000e+00, %49
  %51 = fmul double %44, %50
  store double %51, ptr %9, align 8, !tbaa !9
  %52 = load double, ptr %7, align 8, !tbaa !9
  %53 = load double, ptr %9, align 8, !tbaa !9
  %54 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 3
  %55 = load ptr, ptr %54, align 8, !tbaa !18
  %56 = load i32, ptr %5, align 4, !tbaa !11
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds double, ptr %55, i64 %57
  %59 = load double, ptr %58, align 8, !tbaa !9
  %60 = call double @llvm.fmuladd.f64(double %52, double %53, double %59)
  store double %60, ptr %58, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %61

61:                                               ; preds = %30, %29
  %62 = load i32, ptr %5, align 4, !tbaa !11
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %5, align 4, !tbaa !11
  br label %15, !llvm.loop !19

64:                                               ; preds = %20, %13
  ret void
}

declare noundef double @_Z14RNRandomScalarv() #4

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

!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 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 204}
!14 = !{!"_ZTS6R2Grid", !15, i64 0, !15, i64 88, !10, i64 176, !6, i64 184, !7, i64 192, !12, i64 200, !12, i64 204}
!15 = !{!"_ZTS8R2Affine", !16, i64 0, !17, i64 8, !12, i64 80}
!16 = !{!"_ZTS16R2Transformation"}
!17 = !{!"_ZTS8R3Matrix", !7, i64 0}
!18 = !{!14, !6, i64 184}
!19 = distinct !{!19, !20}
!20 = !{!"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"

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @exp(double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6R2Grid8AddNoiseEd(ptr nocapture noundef nonnull readonly align 8 dereferenceable(208) %0, double noundef %1) local_unnamed_addr #2 align 2 {
  %3 = fcmp oeq double %1, 0.000000e+00
  br i1 %3, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 204
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %7 = getelementptr inbounds i8, ptr %0, i64 184
  %.pre = load ptr, ptr %7, align 8, !tbaa !15
  br label %8

8:                                                ; preds = %.lr.ph, %32
  %9 = phi i32 [ %5, %.lr.ph ], [ %33, %32 ]
  %10 = phi ptr [ %.pre, %.lr.ph ], [ %34, %32 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %32 ]
  %11 = getelementptr inbounds double, ptr %10, i64 %indvars.iv
  %12 = load double, ptr %11, align 8, !tbaa !16
  %13 = fcmp oeq double %12, -3.210000e+02
  br i1 %13, label %32, label %14

14:                                               ; preds = %8
  %15 = tail call noundef double @_Z14RNRandomScalarv()
  %16 = tail call noundef double @_Z14RNRandomScalarv()
  %17 = fcmp olt double %16, 5.000000e-01
  %18 = select i1 %17, double -1.000000e+00, double 1.000000e+00
  %19 = load ptr, ptr %7, align 8, !tbaa !15
  %20 = getelementptr inbounds double, ptr %19, i64 %indvars.iv
  %21 = load double, ptr %20, align 8, !tbaa !16
  %22 = fmul double %21, %1
  %23 = fmul double %15, -5.000000e-01
  %24 = fmul double %15, %23
  %25 = tail call double @exp(double noundef %24) #4
  %26 = fsub double 1.000000e+00, %25
  %27 = fmul double %22, %26
  %28 = load ptr, ptr %7, align 8, !tbaa !15
  %29 = getelementptr inbounds double, ptr %28, i64 %indvars.iv
  %30 = load double, ptr %29, align 8, !tbaa !16
  %31 = tail call double @llvm.fmuladd.f64(double %18, double %27, double %30)
  store double %31, ptr %29, align 8, !tbaa !16
  %.pre15 = load i32, ptr %4, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %8, %14
  %33 = phi i32 [ %9, %8 ], [ %.pre15, %14 ]
  %34 = phi ptr [ %10, %8 ], [ %28, %14 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %35 = sext i32 %33 to i64
  %36 = icmp slt i64 %indvars.iv.next, %35
  br i1 %36, label %8, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %32, %.preheader, %2
  ret void
}

declare noundef double @_Z14RNRandomScalarv() local_unnamed_addr #3

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

!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, !12, i64 204}
!6 = !{!"_ZTS6R2Grid", !7, i64 0, !7, i64 88, !13, i64 176, !14, i64 184, !10, i64 192, !12, i64 200, !12, i64 204}
!7 = !{!"_ZTS8R2Affine", !8, i64 0, !9, i64 8, !12, i64 80}
!8 = !{!"_ZTS16R2Transformation"}
!9 = !{!"_ZTS8R3Matrix", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"double", !10, i64 0}
!14 = !{!"any pointer", !10, i64 0}
!15 = !{!6, !14, i64 184}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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