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

; 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.RNFlags = type { i64 }
%class.R2Polygon = type <{ %class.R2Solid, ptr, i32, [4 x i8], %class.R2Box, i32, [4 x i8] }>
%class.R2Solid = type { %class.R2Shape }
%class.R2Shape = type { ptr }
%class.R2Box = type { %class.R2Solid, %class.R2Point, %class.R2Point }
%class.R2Point = type { [2 x double] }

@R2_EDGES_DRAW_FLAG = external global %class.RNFlags, align 8
@R2_SURFACES_DRAW_FLAG = external global %class.RNFlags, align 8

; 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 void @_Z11R2LoadPointRK7R2Point(ptr noundef nonnull align 8 dereferenceable(16)) #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZNK7RNFlagsixES_(ptr noundef nonnull align 8 dereferenceable(8), i64) #1 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z14R2BeginPolygonv() #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z12R2EndPolygonv() #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11R2BeginLoopv() #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z9R2EndLoopv() #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK9R2Polygon4DrawE7RNFlags(ptr noundef nonnull align 8 dereferenceable(68) %0, i64 %1) unnamed_addr #3 align 2 {
  %3 = alloca %class.RNFlags, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %class.RNFlags, align 8
  %6 = alloca i32, align 4
  %7 = alloca %class.RNFlags, align 8
  %8 = alloca i32, align 4
  %9 = getelementptr inbounds %class.RNFlags, ptr %3, i32 0, i32 0
  store i64 %1, ptr %9, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 8 @R2_SURFACES_DRAW_FLAG, i64 8, i1 false), !tbaa.struct !9
  %11 = getelementptr inbounds %class.RNFlags, ptr %5, i32 0, i32 0
  %12 = load i64, ptr %11, align 8
  %13 = call noundef i32 @_ZNK7RNFlagsixES_(ptr noundef nonnull align 8 dereferenceable(8) %3, i64 %12)
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %32

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

16:                                               ; preds = %28, %15
  %17 = load i32, ptr %6, align 4, !tbaa !12
  %18 = getelementptr inbounds %class.R2Polygon, ptr %10, i32 0, i32 2
  %19 = load i32, ptr %18, align 8, !tbaa !14
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %31

22:                                               ; preds = %16
  %23 = getelementptr inbounds %class.R2Polygon, ptr %10, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !20
  %25 = load i32, ptr %6, align 4, !tbaa !12
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds %class.R2Point, ptr %24, i64 %26
  call void @_Z11R2LoadPointRK7R2Point(ptr noundef nonnull align 8 dereferenceable(16) %27)
  br label %28

28:                                               ; preds = %22
  %29 = load i32, ptr %6, align 4, !tbaa !12
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %6, align 4, !tbaa !12
  br label %16, !llvm.loop !21

31:                                               ; preds = %21
  call void @_Z12R2EndPolygonv()
  br label %32

32:                                               ; preds = %31, %2
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 @R2_EDGES_DRAW_FLAG, i64 8, i1 false), !tbaa.struct !9
  %33 = getelementptr inbounds %class.RNFlags, ptr %7, i32 0, i32 0
  %34 = load i64, ptr %33, align 8
  %35 = call noundef i32 @_ZNK7RNFlagsixES_(ptr noundef nonnull align 8 dereferenceable(8) %3, i64 %34)
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %54

37:                                               ; preds = %32
  call void @_Z11R2BeginLoopv()
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %38

38:                                               ; preds = %50, %37
  %39 = load i32, ptr %8, align 4, !tbaa !12
  %40 = getelementptr inbounds %class.R2Polygon, ptr %10, i32 0, i32 2
  %41 = load i32, ptr %40, align 8, !tbaa !14
  %42 = icmp slt i32 %39, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %53

44:                                               ; preds = %38
  %45 = getelementptr inbounds %class.R2Polygon, ptr %10, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !20
  %47 = load i32, ptr %8, align 4, !tbaa !12
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds %class.R2Point, ptr %46, i64 %48
  call void @_Z11R2LoadPointRK7R2Point(ptr noundef nonnull align 8 dereferenceable(16) %49)
  br label %50

50:                                               ; preds = %44
  %51 = load i32, ptr %8, align 4, !tbaa !12
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %8, align 4, !tbaa !12
  br label %38, !llvm.loop !23

53:                                               ; preds = %43
  call void @_Z9R2EndLoopv()
  br label %54

54:                                               ; preds = %53, %32
  ret void
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{i64 0, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 16}
!15 = !{!"_ZTS9R2Polygon", !16, i64 0, !6, i64 8, !13, i64 16, !18, i64 24, !13, i64 64}
!16 = !{!"_ZTS7R2Solid", !17, i64 0}
!17 = !{!"_ZTS7R2Shape"}
!18 = !{!"_ZTS5R2Box", !16, i64 0, !19, i64 8, !19, i64 24}
!19 = !{!"_ZTS7R2Point", !7, i64 0}
!20 = !{!15, !6, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


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

%class.RNFlags = type { i64 }
%class.R2Point = type { [2 x double] }

@R2_EDGES_DRAW_FLAG = external local_unnamed_addr global %class.RNFlags, align 8
@R2_SURFACES_DRAW_FLAG = external local_unnamed_addr global %class.RNFlags, align 8

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11R2LoadPointRK7R2Point(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZNK7RNFlagsixES_(ptr noundef nonnull align 8 dereferenceable(8), i64) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z14R2BeginPolygonv() local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z12R2EndPolygonv() local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11R2BeginLoopv() local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z9R2EndLoopv() local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK9R2Polygon4DrawE7RNFlags(ptr nocapture noundef nonnull readonly align 8 dereferenceable(68) %0, i64 %1) unnamed_addr #1 align 2 {
  %3 = alloca %class.RNFlags, align 8
  store i64 %1, ptr %3, align 8
  %.sroa.06.0.copyload = load i64, ptr @R2_SURFACES_DRAW_FLAG, align 8, !tbaa !5
  %4 = call noundef i32 @_ZNK7RNFlagsixES_(ptr noundef nonnull align 8 dereferenceable(8) %3, i64 %.sroa.06.0.copyload)
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %16, label %5

5:                                                ; preds = %2
  call void @_Z14R2BeginPolygonv()
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  br label %10

._crit_edge:                                      ; preds = %10, %5
  call void @_Z12R2EndPolygonv()
  br label %16

10:                                               ; preds = %.lr.ph, %10
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %10 ]
  %11 = load ptr, ptr %9, align 8, !tbaa !17
  %12 = getelementptr inbounds %class.R2Point, ptr %11, i64 %indvars.iv
  call void @_Z11R2LoadPointRK7R2Point(ptr noundef nonnull align 8 dereferenceable(16) %12)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = load i32, ptr %6, align 8, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = icmp slt i64 %indvars.iv.next, %14
  br i1 %15, label %10, label %._crit_edge, !llvm.loop !18

16:                                               ; preds = %._crit_edge, %2
  %.sroa.0.0.copyload = load i64, ptr @R2_EDGES_DRAW_FLAG, align 8, !tbaa !5
  %17 = call noundef i32 @_ZNK7RNFlagsixES_(ptr noundef nonnull align 8 dereferenceable(8) %3, i64 %.sroa.0.0.copyload)
  %.not8 = icmp eq i32 %17, 0
  br i1 %.not8, label %29, label %18

18:                                               ; preds = %16
  call void @_Z11R2BeginLoopv()
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %.lr.ph12, label %._crit_edge13

.lr.ph12:                                         ; preds = %18
  %22 = getelementptr inbounds i8, ptr %0, i64 8
  br label %23

._crit_edge13:                                    ; preds = %23, %18
  call void @_Z9R2EndLoopv()
  br label %29

23:                                               ; preds = %.lr.ph12, %23
  %indvars.iv15 = phi i64 [ 0, %.lr.ph12 ], [ %indvars.iv.next16, %23 ]
  %24 = load ptr, ptr %22, align 8, !tbaa !17
  %25 = getelementptr inbounds %class.R2Point, ptr %24, i64 %indvars.iv15
  call void @_Z11R2LoadPointRK7R2Point(ptr noundef nonnull align 8 dereferenceable(16) %25)
  %indvars.iv.next16 = add nuw nsw i64 %indvars.iv15, 1
  %26 = load i32, ptr %19, align 8, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = icmp slt i64 %indvars.iv.next16, %27
  br i1 %28, label %23, label %._crit_edge13, !llvm.loop !20

29:                                               ; preds = %._crit_edge13, %16
  ret void
}

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

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !14, i64 16}
!10 = !{!"_ZTS9R2Polygon", !11, i64 0, !13, i64 8, !14, i64 16, !15, i64 24, !14, i64 64}
!11 = !{!"_ZTS7R2Solid", !12, i64 0}
!12 = !{!"_ZTS7R2Shape"}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!"_ZTS5R2Box", !11, i64 0, !16, i64 8, !16, i64 24}
!16 = !{!"_ZTS7R2Point", !7, i64 0}
!17 = !{!10, !13, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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