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

; 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 @_ZN7R2Image8SubtractERKS_(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 = %40, %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 %48

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
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load i8, ptr %28, align 1, !tbaa !15
  %30 = zext i8 %29 to i32
  %31 = sub nsw i32 %27, %30
  %32 = add nsw i32 %31, 128
  store i32 %32, ptr %8, align 4, !tbaa !13
  %33 = load i32, ptr %8, align 4, !tbaa !13
  %34 = icmp slt i32 %33, 0
  br i1 %34, label %35, label %36

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

36:                                               ; preds = %35, %24
  %37 = load i32, ptr %8, align 4, !tbaa !13
  %38 = icmp sgt i32 %37, 255
  br i1 %38, label %39, label %40

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

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

48:                                               ; 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 @_ZN7R2Image8SubtractERKS_(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
  %.not14 = icmp eq i32 %8, 0
  br i1 %.not14, 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
  %scevgep17 = getelementptr i8, ptr %10, i64 %16
  %bound0 = icmp ult ptr %12, %scevgep17
  %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.check18 = icmp ult i32 %8, 16
  br i1 %min.iters.check18, 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.gep19 = getelementptr i8, ptr %12, i64 %index
  %wide.load = load <16 x i8>, ptr %next.gep19, align 1, !tbaa !13, !alias.scope !14, !noalias !17
  %17 = zext <16 x i8> %wide.load to <16 x i32>
  %wide.load20 = load <16 x i8>, ptr %next.gep, align 1, !tbaa !13, !alias.scope !17
  %18 = zext <16 x i8> %wide.load20 to <16 x i32>
  %19 = sub nsw <16 x i32> %17, %18
  %20 = tail call <16 x i32> @llvm.smax.v16i32(<16 x i32> %19, <16 x i32> <i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128>)
  %21 = tail call <16 x i32> @llvm.smin.v16i32(<16 x i32> %20, <16 x i32> <i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127>)
  %22 = trunc nsw <16 x i32> %21 to <16 x i8>
  %23 = xor <16 x i8> %22, <i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128>
  store <16 x i8> %23, ptr %next.gep19, align 1, !tbaa !13, !alias.scope !14, !noalias !17
  %index.next = add nuw i64 %index, 16
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, 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.end29 = getelementptr i8, ptr %12, i64 %n.vec
  %ind.end26 = getelementptr i8, ptr %10, i64 %n.vec
  %.cast23 = trunc nuw i64 %n.vec to i32
  %ind.end24 = sub i32 %8, %.cast23
  %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.vec22 = and i64 %13, 4294967288
  %.cast = trunc nuw i64 %n.vec22 to i32
  %ind.end = sub i32 %8, %.cast
  %ind.end25 = getelementptr i8, ptr %10, i64 %n.vec22
  %ind.end28 = getelementptr i8, ptr %12, i64 %n.vec22
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index31 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next36, %vec.epilog.vector.body ]
  %next.gep32 = getelementptr i8, ptr %10, i64 %index31
  %next.gep33 = getelementptr i8, ptr %12, i64 %index31
  %wide.load34 = load <8 x i8>, ptr %next.gep33, align 1, !tbaa !13, !alias.scope !23, !noalias !26
  %25 = zext <8 x i8> %wide.load34 to <8 x i32>
  %wide.load35 = load <8 x i8>, ptr %next.gep32, align 1, !tbaa !13, !alias.scope !26
  %26 = zext <8 x i8> %wide.load35 to <8 x i32>
  %27 = sub nsw <8 x i32> %25, %26
  %28 = tail call <8 x i32> @llvm.smax.v8i32(<8 x i32> %27, <8 x i32> <i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128, i32 -128>)
  %29 = tail call <8 x i32> @llvm.smin.v8i32(<8 x i32> %28, <8 x i32> <i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127, i32 127>)
  %30 = trunc nsw <8 x i32> %29 to <8 x i8>
  %31 = xor <8 x i8> %30, <i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128>
  store <8 x i8> %31, ptr %next.gep33, align 1, !tbaa !13, !alias.scope !23, !noalias !26
  %index.next36 = add nuw i64 %index31, 8
  %32 = icmp eq i64 %index.next36, %n.vec22
  br i1 %32, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !28

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n37 = icmp eq i64 %n.vec22, %13
  br i1 %cmp.n37, 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.end24, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.01116.ph = phi ptr [ %10, %iter.check ], [ %10, %vector.memcheck ], [ %ind.end26, %vec.epilog.iter.check ], [ %ind.end25, %vec.epilog.middle.block ]
  %.01215.ph = phi ptr [ %12, %iter.check ], [ %12, %vector.memcheck ], [ %ind.end29, %vec.epilog.iter.check ], [ %ind.end28, %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
  %33 = load i8, ptr %.01215.ph, align 1, !tbaa !13
  %34 = zext i8 %33 to i32
  %35 = load i8, ptr %.01116.ph, align 1, !tbaa !13
  %36 = zext i8 %35 to i32
  %37 = sub nsw i32 %34, %36
  %38 = tail call i32 @llvm.smax.i32(i32 %37, i32 -128)
  %39 = tail call i32 @llvm.smin.i32(i32 %38, i32 127)
  %40 = trunc nsw i32 %39 to i8
  %41 = xor i8 %40, -128
  store i8 %41, ptr %.01215.ph, align 1, !tbaa !13
  %42 = getelementptr inbounds i8, ptr %.01215.ph, i64 1
  %43 = getelementptr inbounds i8, ptr %.01116.ph, i64 1
  %44 = 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 ], [ %44, %.lr.ph.prol ]
  %.01116.unr = phi ptr [ %.01116.ph, %.lr.ph.preheader ], [ %43, %.lr.ph.prol ]
  %.01215.unr = phi ptr [ %.01215.ph, %.lr.ph.preheader ], [ %42, %.lr.ph.prol ]
  %45 = icmp eq i32 %.ph, 1
  br i1 %45, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %46 = phi i32 [ %69, %.lr.ph ], [ %.unr, %.lr.ph.prol.loopexit ]
  %.01116 = phi ptr [ %68, %.lr.ph ], [ %.01116.unr, %.lr.ph.prol.loopexit ]
  %.01215 = phi ptr [ %67, %.lr.ph ], [ %.01215.unr, %.lr.ph.prol.loopexit ]
  %47 = load i8, ptr %.01215, align 1, !tbaa !13
  %48 = zext i8 %47 to i32
  %49 = load i8, ptr %.01116, align 1, !tbaa !13
  %50 = zext i8 %49 to i32
  %51 = sub nsw i32 %48, %50
  %52 = tail call i32 @llvm.smax.i32(i32 %51, i32 -128)
  %53 = tail call i32 @llvm.smin.i32(i32 %52, i32 127)
  %54 = trunc nsw i32 %53 to i8
  %55 = xor i8 %54, -128
  store i8 %55, ptr %.01215, align 1, !tbaa !13
  %56 = getelementptr inbounds i8, ptr %.01215, i64 1
  %57 = getelementptr inbounds i8, ptr %.01116, i64 1
  %58 = load i8, ptr %56, align 1, !tbaa !13
  %59 = zext i8 %58 to i32
  %60 = load i8, ptr %57, align 1, !tbaa !13
  %61 = zext i8 %60 to i32
  %62 = sub nsw i32 %59, %61
  %63 = tail call i32 @llvm.smax.i32(i32 %62, i32 -128)
  %64 = tail call i32 @llvm.smin.i32(i32 %63, i32 127)
  %65 = trunc nsw i32 %64 to i8
  %66 = xor i8 %65, -128
  store i8 %66, ptr %56, align 1, !tbaa !13
  %67 = getelementptr inbounds i8, ptr %.01215, i64 2
  %68 = getelementptr inbounds i8, ptr %.01116, i64 2
  %69 = add nsw i32 %46, -2
  %.not.1 = icmp eq i32 %69, 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.smax.i32(i32, i32) #1

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <8 x i32> @llvm.smin.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,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass