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

; 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.R2Image = type { i32, i32, i32, i32, ptr }

; 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 nounwind uwtable
define dso_local void @_ZN7R2Image3AddERKS_(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(24) %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %10 = getelementptr inbounds %class.R2Image, ptr %9, i32 0, i32 3
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = getelementptr inbounds %class.R2Image, ptr %9, i32 0, i32 1
  %13 = load i32, ptr %12, align 4, !tbaa !12
  %14 = mul nsw i32 %11, %13
  store i32 %14, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = getelementptr inbounds %class.R2Image, ptr %9, i32 0, i32 4
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  store ptr %16, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %class.R2Image, ptr %17, i32 0, i32 4
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  store ptr %19, ptr %7, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %37, %2
  %21 = load i32, ptr %5, align 4, !tbaa !13
  %22 = add nsw i32 %21, -1
  store i32 %22, ptr %5, align 4, !tbaa !13
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %42

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i8, ptr %25, align 1, !tbaa !15
  %27 = zext i8 %26 to i32
  store i32 %27, ptr %8, align 4, !tbaa !13
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %7, align 8, !tbaa !5
  %30 = load i8, ptr %28, align 1, !tbaa !15
  %31 = zext i8 %30 to i32
  %32 = load i32, ptr %8, align 4, !tbaa !13
  %33 = add nsw i32 %32, %31
  store i32 %33, ptr %8, align 4, !tbaa !13
  %34 = load i32, ptr %8, align 4, !tbaa !13
  %35 = icmp sgt i32 %34, 255
  br i1 %35, label %36, label %37

36:                                               ; preds = %24
  store i32 255, ptr %8, align 4, !tbaa !13
  br label %37

37:                                               ; preds = %36, %24
  %38 = load i32, ptr %8, align 4, !tbaa !13
  %39 = trunc i32 %38 to i8
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i32 1
  store ptr %41, ptr %6, align 8, !tbaa !5
  store i8 %39, ptr %40, align 1, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %20, !llvm.loop !16

42:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #2 = { 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, !11, i64 12}
!10 = !{!"_ZTS7R2Image", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 16}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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 nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN7R2Image3AddERKS_(ptr nocapture noundef nonnull readonly align 8 dereferenceable(24) %0, ptr nocapture noundef nonnull readonly align 8 dereferenceable(24) %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 12
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %7 = mul nsw i32 %6, %4
  %8 = add nsw i32 %7, -1
  %.not11 = icmp eq i32 %8, 0
  br i1 %.not11, label %._crit_edge, label %iter.check

iter.check:                                       ; preds = %2
  %9 = getelementptr inbounds i8, ptr %1, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = zext i32 %8 to i64
  %min.iters.check = icmp ult i32 %8, 8
  br i1 %min.iters.check, label %.lr.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %iter.check
  %14 = add i32 %7, -2
  %15 = zext i32 %14 to i64
  %16 = add nuw nsw i64 %15, 1
  %scevgep = getelementptr i8, ptr %12, i64 %16
  %scevgep14 = getelementptr i8, ptr %10, i64 %16
  %bound0 = icmp ult ptr %12, %scevgep14
  %bound1 = icmp ult ptr %10, %scevgep
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %vector.memcheck
  %min.iters.check15 = icmp ult i32 %8, 16
  br i1 %min.iters.check15, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %13, 4294967280
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %next.gep = getelementptr i8, ptr %10, i64 %index
  %next.gep16 = getelementptr i8, ptr %12, i64 %index
  %wide.load = load <16 x i8>, ptr %next.gep16, align 1, !tbaa !13, !alias.scope !14, !noalias !17
  %17 = zext <16 x i8> %wide.load to <16 x i32>
  %wide.load17 = load <16 x i8>, ptr %next.gep, align 1, !tbaa !13, !alias.scope !17
  %18 = zext <16 x i8> %wide.load17 to <16 x i32>
  %19 = add nuw nsw <16 x i32> %18, %17
  %20 = tail call <16 x i32> @llvm.umin.v16i32(<16 x i32> %19, <16 x i32> <i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255>)
  %21 = trunc nuw <16 x i32> %20 to <16 x i8>
  store <16 x i8> %21, ptr %next.gep16, align 1, !tbaa !13, !alias.scope !14, !noalias !17
  %index.next = add nuw i64 %index, 16
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !19

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %13
  br i1 %cmp.n, label %._crit_edge, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %ind.end26 = getelementptr i8, ptr %12, i64 %n.vec
  %ind.end23 = getelementptr i8, ptr %10, i64 %n.vec
  %.cast20 = trunc nuw i64 %n.vec to i32
  %ind.end21 = sub i32 %8, %.cast20
  %n.vec.remaining = and i64 %13, 8
  %min.epilog.iters.check.not.not = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check.not.not, label %.lr.ph.preheader, label %vec.epilog.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec19 = and i64 %13, 4294967288
  %.cast = trunc nuw i64 %n.vec19 to i32
  %ind.end = sub i32 %8, %.cast
  %ind.end22 = getelementptr i8, ptr %10, i64 %n.vec19
  %ind.end25 = getelementptr i8, ptr %12, i64 %n.vec19
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index28 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next33, %vec.epilog.vector.body ]
  %next.gep29 = getelementptr i8, ptr %10, i64 %index28
  %next.gep30 = getelementptr i8, ptr %12, i64 %index28
  %wide.load31 = load <8 x i8>, ptr %next.gep30, align 1, !tbaa !13, !alias.scope !23, !noalias !26
  %23 = zext <8 x i8> %wide.load31 to <8 x i32>
  %wide.load32 = load <8 x i8>, ptr %next.gep29, align 1, !tbaa !13, !alias.scope !26
  %24 = zext <8 x i8> %wide.load32 to <8 x i32>
  %25 = add nuw nsw <8 x i32> %24, %23
  %26 = tail call <8 x i32> @llvm.umin.v8i32(<8 x i32> %25, <8 x i32> <i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255, i32 255>)
  %27 = trunc nuw <8 x i32> %26 to <8 x i8>
  store <8 x i8> %27, ptr %next.gep30, align 1, !tbaa !13, !alias.scope !23, !noalias !26
  %index.next33 = add nuw i64 %index28, 8
  %28 = icmp eq i64 %index.next33, %n.vec19
  br i1 %28, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !28

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n34 = icmp eq i64 %n.vec19, %13
  br i1 %cmp.n34, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %vector.memcheck, %iter.check, %vec.epilog.iter.check
  %.ph = phi i32 [ %8, %iter.check ], [ %8, %vector.memcheck ], [ %ind.end21, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.0813.ph = phi ptr [ %10, %iter.check ], [ %10, %vector.memcheck ], [ %ind.end23, %vec.epilog.iter.check ], [ %ind.end22, %vec.epilog.middle.block ]
  %.0912.ph = phi ptr [ %12, %iter.check ], [ %12, %vector.memcheck ], [ %ind.end26, %vec.epilog.iter.check ], [ %ind.end25, %vec.epilog.middle.block ]
  %xtraiter = and i32 %.ph, 1
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader
  %29 = load i8, ptr %.0912.ph, align 1, !tbaa !13
  %30 = zext i8 %29 to i32
  %31 = getelementptr inbounds i8, ptr %.0813.ph, i64 1
  %32 = load i8, ptr %.0813.ph, align 1, !tbaa !13
  %33 = zext i8 %32 to i32
  %34 = add nuw nsw i32 %33, %30
  %spec.store.select.prol = tail call i32 @llvm.umin.i32(i32 %34, i32 255)
  %35 = trunc nuw i32 %spec.store.select.prol to i8
  %36 = getelementptr inbounds i8, ptr %.0912.ph, i64 1
  store i8 %35, ptr %.0912.ph, align 1, !tbaa !13
  %37 = add nsw i32 %.ph, -1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.unr = phi i32 [ %.ph, %.lr.ph.preheader ], [ %37, %.lr.ph.prol ]
  %.0813.unr = phi ptr [ %.0813.ph, %.lr.ph.preheader ], [ %31, %.lr.ph.prol ]
  %.0912.unr = phi ptr [ %.0912.ph, %.lr.ph.preheader ], [ %36, %.lr.ph.prol ]
  %38 = icmp eq i32 %.ph, 1
  br i1 %38, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %39 = phi i32 [ %56, %.lr.ph ], [ %.unr, %.lr.ph.prol.loopexit ]
  %.0813 = phi ptr [ %50, %.lr.ph ], [ %.0813.unr, %.lr.ph.prol.loopexit ]
  %.0912 = phi ptr [ %55, %.lr.ph ], [ %.0912.unr, %.lr.ph.prol.loopexit ]
  %40 = load i8, ptr %.0912, align 1, !tbaa !13
  %41 = zext i8 %40 to i32
  %42 = getelementptr inbounds i8, ptr %.0813, i64 1
  %43 = load i8, ptr %.0813, align 1, !tbaa !13
  %44 = zext i8 %43 to i32
  %45 = add nuw nsw i32 %44, %41
  %spec.store.select = tail call i32 @llvm.umin.i32(i32 %45, i32 255)
  %46 = trunc nuw i32 %spec.store.select to i8
  %47 = getelementptr inbounds i8, ptr %.0912, i64 1
  store i8 %46, ptr %.0912, align 1, !tbaa !13
  %48 = load i8, ptr %47, align 1, !tbaa !13
  %49 = zext i8 %48 to i32
  %50 = getelementptr inbounds i8, ptr %.0813, i64 2
  %51 = load i8, ptr %42, align 1, !tbaa !13
  %52 = zext i8 %51 to i32
  %53 = add nuw nsw i32 %52, %49
  %spec.store.select.1 = tail call i32 @llvm.umin.i32(i32 %53, i32 255)
  %54 = trunc nuw i32 %spec.store.select.1 to i8
  %55 = getelementptr inbounds i8, ptr %.0912, i64 2
  store i8 %54, ptr %47, align 1, !tbaa !13
  %56 = add nsw i32 %39, -2
  %.not.1 = icmp eq i32 %56, 0
  br i1 %.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !29

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %vec.epilog.middle.block, %2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <16 x i32> @llvm.umin.v16i32(<16 x i32>, <16 x i32>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <8 x i32> @llvm.umin.v8i32(<8 x i32>, <8 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 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, !7, i64 12}
!6 = !{!"_ZTS7R2Image", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 16}
!13 = !{!8, !8, i64 0}
!14 = !{!15}
!15 = distinct !{!15, !16}
!16 = distinct !{!16, !"LVerDomain"}
!17 = !{!18}
!18 = distinct !{!18, !16}
!19 = distinct !{!19, !20, !21, !22}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!"llvm.loop.isvectorized", i32 1}
!22 = !{!"llvm.loop.unroll.runtime.disable"}
!23 = !{!24}
!24 = distinct !{!24, !25}
!25 = distinct !{!25, !"LVerDomain"}
!26 = !{!27}
!27 = distinct !{!27, !25}
!28 = distinct !{!28, !20, !21, !22}
!29 = distinct !{!29, !20, !21}


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