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

; 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: inlinehint mustprogress uwtable
declare dso_local noundef double @_ZNK6R2Grid9GridValueEii(ptr noundef nonnull align 8 dereferenceable(208), i32 noundef, i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK6R2Grid8DrawMeshEv(ptr noundef nonnull align 8 dereferenceable(208) %0) #2 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  %7 = getelementptr inbounds %class.R2Grid, ptr %6, i32 0, i32 0
  call void @_ZNK8R2Affine4PushEv(ptr noundef nonnull align 8 dereferenceable(84) %7)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %44, %1
  %9 = load i32, ptr %3, align 4, !tbaa !9
  %10 = getelementptr inbounds %class.R2Grid, ptr %6, i32 0, i32 4
  %11 = getelementptr inbounds [2 x i32], ptr %10, i64 0, i64 1
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = icmp slt i32 %9, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %8
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %47

15:                                               ; preds = %8
  call void @glBegin(i32 noundef 5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %40, %15
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = getelementptr inbounds %class.R2Grid, ptr %6, i32 0, i32 4
  %19 = getelementptr inbounds [2 x i32], ptr %18, i64 0, i64 0
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %16
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %43

23:                                               ; preds = %16
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = sitofp i32 %24 to double
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = sitofp i32 %26 to double
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = call noundef double @_ZNK6R2Grid9GridValueEii(ptr noundef nonnull align 8 dereferenceable(208) %6, i32 noundef %28, i32 noundef %29)
  call void @_Z11R3LoadPointddd(double noundef %25, double noundef %27, double noundef %30)
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = sitofp i32 %31 to double
  %33 = load i32, ptr %3, align 4, !tbaa !9
  %34 = sub nsw i32 %33, 1
  %35 = sitofp i32 %34 to double
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = load i32, ptr %3, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  %39 = call noundef double @_ZNK6R2Grid9GridValueEii(ptr noundef nonnull align 8 dereferenceable(208) %6, i32 noundef %36, i32 noundef %38)
  call void @_Z11R3LoadPointddd(double noundef %32, double noundef %35, double noundef %39)
  br label %40

40:                                               ; preds = %23
  %41 = load i32, ptr %5, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %5, align 4, !tbaa !9
  br label %16, !llvm.loop !11

43:                                               ; preds = %22
  call void @glEnd()
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %3, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %3, align 4, !tbaa !9
  br label %8, !llvm.loop !13

47:                                               ; preds = %14
  %48 = getelementptr inbounds %class.R2Grid, ptr %6, i32 0, i32 0
  call void @_ZNK8R2Affine3PopEv(ptr noundef nonnull align 8 dereferenceable(84) %48)
  ret void
}

declare void @_ZNK8R2Affine4PushEv(ptr noundef nonnull align 8 dereferenceable(84)) unnamed_addr #3

declare void @glBegin(i32 noundef) #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11R3LoadPointddd(double noundef, double noundef, double noundef) #1

declare void @glEnd() #3

declare void @_ZNK8R2Affine3PopEv(ptr noundef nonnull align 8 dereferenceable(84)) unnamed_addr #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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, !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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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: inlinehint mustprogress uwtable
declare dso_local noundef double @_ZNK6R2Grid9GridValueEii(ptr noundef nonnull align 8 dereferenceable(208), i32 noundef, i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK6R2Grid8DrawMeshEv(ptr noundef nonnull align 8 dereferenceable(208) %0) local_unnamed_addr #1 align 2 {
  tail call void @_ZNK8R2Affine4PushEv(ptr noundef nonnull align 8 dereferenceable(84) %0)
  %2 = getelementptr inbounds i8, ptr %0, i64 192
  %3 = getelementptr inbounds i8, ptr %0, i64 196
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 1
  br i1 %5, label %.lr.ph16, label %._crit_edge17

._crit_edge17:                                    ; preds = %._crit_edge, %1
  tail call void @_ZNK8R2Affine3PopEv(ptr noundef nonnull align 8 dereferenceable(84) %0)
  ret void

.lr.ph16:                                         ; preds = %1, %._crit_edge
  %.01214 = phi i32 [ %11, %._crit_edge ], [ 1, %1 ]
  tail call void @glBegin(i32 noundef 5)
  %6 = load i32, ptr %2, align 8, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph16
  %8 = uitofp nneg i32 %.01214 to double
  %9 = add nsw i32 %.01214, -1
  %10 = sitofp i32 %9 to double
  br label %14

._crit_edge:                                      ; preds = %14, %.lr.ph16
  tail call void @glEnd()
  %11 = add nuw nsw i32 %.01214, 1
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %.lr.ph16, label %._crit_edge17, !llvm.loop !9

14:                                               ; preds = %.lr.ph, %14
  %.013 = phi i32 [ 0, %.lr.ph ], [ %18, %14 ]
  %15 = uitofp nneg i32 %.013 to double
  %16 = tail call noundef double @_ZNK6R2Grid9GridValueEii(ptr noundef nonnull align 8 dereferenceable(208) %0, i32 noundef %.013, i32 noundef %.01214)
  tail call void @_Z11R3LoadPointddd(double noundef %15, double noundef %8, double noundef %16)
  %17 = tail call noundef double @_ZNK6R2Grid9GridValueEii(ptr noundef nonnull align 8 dereferenceable(208) %0, i32 noundef %.013, i32 noundef %9)
  tail call void @_Z11R3LoadPointddd(double noundef %15, double noundef %10, double noundef %17)
  %18 = add nuw nsw i32 %.013, 1
  %19 = load i32, ptr %2, align 8, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %14, label %._crit_edge, !llvm.loop !11
}

declare void @_ZNK8R2Affine4PushEv(ptr noundef nonnull align 8 dereferenceable(84)) unnamed_addr #2

declare void @glBegin(i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11R3LoadPointddd(double noundef, double noundef, double noundef) local_unnamed_addr #0

declare void @glEnd() local_unnamed_addr #2

declare void @_ZNK8R2Affine3PopEv(ptr noundef nonnull align 8 dereferenceable(84)) unnamed_addr #2

attributes #0 = { inlinehint 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 = { 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" }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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