; LLVM IR from Wilson-ZheLin/Introduction-to-Digital-Image-Processing :: 8. Erosion, Dilation, Opening and Closing Operations, Boundary Extraction and Connect_27eee6b3 (Z17ExtractBoundariesP5Image)

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

%struct.Image = type { i32, i32, i32, ptr, [15 x [100 x i8]], i32 }

@.str.9 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [15 x i8], align 1

; 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 uwtable
declare dso_local noundef ptr @_Z7ErosionP5Image(ptr noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z12SavePNMImageP5ImagePc(ptr noundef, ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z17ExtractBoundariesP5Image(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = call noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef %8, ptr noundef @.str.9)
  store ptr %9, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Image, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Image, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 4, !tbaa !12
  %16 = mul nsw i32 %12, %15
  store i32 %16, ptr %6, align 4, !tbaa !13
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = call noundef ptr @_Z7ErosionP5Image(ptr noundef %17)
  store ptr %18, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Image, ptr %19, i32 0, i32 3
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  store ptr %21, ptr %3, align 8, !tbaa !5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Image, ptr %22, i32 0, i32 3
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  store ptr %24, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !13
  br label %25

25:                                               ; preds = %49, %1
  %26 = load i32, ptr %7, align 4, !tbaa !13
  %27 = load i32, ptr %6, align 4, !tbaa !13
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %52

30:                                               ; preds = %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !13
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %31, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !15
  %36 = zext i8 %35 to i32
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !13
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i8, ptr %37, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !15
  %42 = zext i8 %41 to i32
  %43 = sub nsw i32 %36, %42
  %44 = trunc i32 %43 to i8
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !13
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %45, i64 %47
  store i8 %44, ptr %48, align 1, !tbaa !15
  br label %49

49:                                               ; preds = %30
  %50 = load i32, ptr %7, align 4, !tbaa !13
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !13
  br label %25, !llvm.loop !16

52:                                               ; preds = %29
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_Z12SavePNMImageP5ImagePc(ptr noundef %53, ptr noundef @.str.12)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 0}
!10 = !{!"_ZTS5Image", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !7, i64 24, !11, i64 1524}
!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"

@.str.9 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z7ErosionP5Image(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z12SavePNMImageP5ImagePc(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z17ExtractBoundariesP5Image(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef %0, ptr noundef nonnull @.str.9)
  %3 = load i32, ptr %0, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !11
  %6 = mul i32 %5, %3
  %7 = tail call noundef ptr @_Z7ErosionP5Image(ptr noundef nonnull %0)
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %7, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = icmp sgt i32 %6, 0
  br i1 %12, label %iter.check, label %._crit_edge

iter.check:                                       ; preds = %1
  %wide.trip.count = zext nneg i32 %6 to i64
  %min.iters.check = icmp ult i32 %6, 8
  br i1 %min.iters.check, label %.lr.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %iter.check
  %scevgep = getelementptr i8, ptr %11, i64 %wide.trip.count
  %scevgep17 = getelementptr i8, ptr %9, i64 %wide.trip.count
  %bound0 = icmp ult ptr %11, %scevgep17
  %bound1 = icmp ult ptr %9, %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 %6, 32
  br i1 %min.iters.check18, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %wide.trip.count, 2147483616
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %13 = getelementptr inbounds i8, ptr %9, i64 %index
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %wide.load = load <16 x i8>, ptr %13, align 1, !tbaa !13, !alias.scope !14
  %wide.load19 = load <16 x i8>, ptr %14, align 1, !tbaa !13, !alias.scope !14
  %15 = getelementptr inbounds i8, ptr %11, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %wide.load20 = load <16 x i8>, ptr %15, align 1, !tbaa !13, !alias.scope !17, !noalias !14
  %wide.load21 = load <16 x i8>, ptr %16, align 1, !tbaa !13, !alias.scope !17, !noalias !14
  %17 = sub <16 x i8> %wide.load, %wide.load20
  %18 = sub <16 x i8> %wide.load19, %wide.load21
  store <16 x i8> %17, ptr %15, align 1, !tbaa !13, !alias.scope !17, !noalias !14
  store <16 x i8> %18, ptr %16, align 1, !tbaa !13, !alias.scope !17, !noalias !14
  %index.next = add nuw i64 %index, 32
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %middle.block, label %vector.body, !llvm.loop !19

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

vec.epilog.iter.check:                            ; preds = %middle.block
  %n.vec.remaining = and i64 %wide.trip.count, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph.preheader, label %vec.epilog.ph

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %vector.memcheck, %iter.check, %vec.epilog.iter.check
  %indvars.iv.ph = phi i64 [ 0, %iter.check ], [ 0, %vector.memcheck ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec23, %vec.epilog.middle.block ]
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph.prol ], [ %indvars.iv.ph, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %20 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.prol
  %21 = load i8, ptr %20, align 1, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv.prol
  %23 = load i8, ptr %22, align 1, !tbaa !13
  %24 = sub i8 %21, %23
  store i8 %24, ptr %22, align 1, !tbaa !13
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !23

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %25 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %26 = icmp ugt i64 %25, -4
  br i1 %26, label %._crit_edge, label %.lr.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.vec23 = and i64 %wide.trip.count, 2147483640
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index24 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next27, %vec.epilog.vector.body ]
  %27 = getelementptr inbounds i8, ptr %9, i64 %index24
  %wide.load25 = load <8 x i8>, ptr %27, align 1, !tbaa !13, !alias.scope !25
  %28 = getelementptr inbounds i8, ptr %11, i64 %index24
  %wide.load26 = load <8 x i8>, ptr %28, align 1, !tbaa !13, !alias.scope !28, !noalias !25
  %29 = sub <8 x i8> %wide.load25, %wide.load26
  store <8 x i8> %29, ptr %28, align 1, !tbaa !13, !alias.scope !28, !noalias !25
  %index.next27 = add nuw i64 %index24, 8
  %30 = icmp eq i64 %index.next27, %n.vec23
  br i1 %30, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !30

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n28 = icmp eq i64 %n.vec23, %wide.trip.count
  br i1 %cmp.n28, label %._crit_edge, label %.lr.ph.preheader

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %vec.epilog.middle.block, %1
  tail call void @_Z12SavePNMImageP5ImagePc(ptr noundef %7, ptr noundef nonnull @.str.12)
  ret void

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %31 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv
  %34 = load i8, ptr %33, align 1, !tbaa !13
  %35 = sub i8 %32, %34
  store i8 %35, ptr %33, align 1, !tbaa !13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.next
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv.next
  %39 = load i8, ptr %38, align 1, !tbaa !13
  %40 = sub i8 %37, %39
  store i8 %40, ptr %38, align 1, !tbaa !13
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %41 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.next.1
  %42 = load i8, ptr %41, align 1, !tbaa !13
  %43 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv.next.1
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = sub i8 %42, %44
  store i8 %45, ptr %43, align 1, !tbaa !13
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %46 = getelementptr inbounds i8, ptr %9, i64 %indvars.iv.next.2
  %47 = load i8, ptr %46, align 1, !tbaa !13
  %48 = getelementptr inbounds i8, ptr %11, i64 %indvars.iv.next.2
  %49 = load i8, ptr %48, align 1, !tbaa !13
  %50 = sub i8 %47, %49
  store i8 %50, ptr %48, align 1, !tbaa !13
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !31
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #0 = { 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, !7, i64 0}
!6 = !{!"_ZTS5Image", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !8, i64 24, !7, i64 1524}
!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 = distinct !{!23, !24}
!24 = !{!"llvm.loop.unroll.disable"}
!25 = !{!26}
!26 = distinct !{!26, !27}
!27 = distinct !{!27, !"LVerDomain"}
!28 = !{!29}
!29 = distinct !{!29, !27}
!30 = distinct !{!30, !20, !21, !22}
!31 = distinct !{!31, !20, !21}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass