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

; 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.13 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [10 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
define dso_local void @_Z18connectedComponentP5Image(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Image, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Image, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = mul nsw i32 %15, %18
  store i32 %19, ptr %3, align 4, !tbaa !13
  %20 = load i32, ptr %3, align 4, !tbaa !13
  %21 = zext i32 %20 to i64
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %4, align 8
  %23 = alloca i32, i64 %21, align 16
  store i64 %21, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %24 = call noalias ptr @fopen(ptr noundef @.str.13, ptr noundef @.str.14)
  store ptr %24, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 1, ptr %7, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !13
  br label %25

25:                                               ; preds = %34, %1
  %26 = load i32, ptr %8, align 4, !tbaa !13
  %27 = load i32, ptr %3, 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 %8) #5
  br label %37

30:                                               ; preds = %25
  %31 = load i32, ptr %8, align 4, !tbaa !13
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i32, ptr %23, i64 %32
  store i32 0, ptr %33, align 4, !tbaa !13
  br label %34

34:                                               ; preds = %30
  %35 = load i32, ptr %8, align 4, !tbaa !13
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %8, align 4, !tbaa !13
  br label %25, !llvm.loop !14

37:                                               ; preds = %29
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.15) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !13
  br label %40

40:                                               ; preds = %73, %37
  %41 = load i32, ptr %9, align 4, !tbaa !13
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Image, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !12
  %45 = icmp slt i32 %41, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %40
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %76

47:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !13
  br label %48

48:                                               ; preds = %69, %47
  %49 = load i32, ptr %11, align 4, !tbaa !13
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.Image, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 8, !tbaa !9
  %53 = icmp slt i32 %49, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %48
  store i32 8, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %72

55:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !13
  %58 = load i32, ptr %9, align 4, !tbaa !13
  %59 = call noundef i32 @_Z3DFSP5ImagePiii(ptr noundef %56, ptr noundef %23, i32 noundef %57, i32 noundef %58)
  store i32 %59, ptr %12, align 4, !tbaa !13
  %60 = load i32, ptr %12, align 4, !tbaa !13
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %68

62:                                               ; preds = %55
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load i32, ptr %7, align 4, !tbaa !13
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %7, align 4, !tbaa !13
  %66 = load i32, ptr %12, align 4, !tbaa !13
  %67 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %63, ptr noundef @.str.16, i32 noundef %64, i32 noundef %66) #5
  br label %68

68:                                               ; preds = %62, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %11, align 4, !tbaa !13
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %11, align 4, !tbaa !13
  br label %48, !llvm.loop !16

72:                                               ; preds = %54
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %9, align 4, !tbaa !13
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %9, align 4, !tbaa !13
  br label %40, !llvm.loop !17

76:                                               ; preds = %46
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = call i32 @fclose(ptr noundef %77)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %79 = load ptr, ptr %4, align 8
  call void @llvm.stackrestore.p0(ptr %79)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #4

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

declare i32 @fclose(ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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 = { nocallback nofree nosync nounwind willreturn }
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 "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 #5 = { 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 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


; 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.13 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z18connectedComponentP5Image(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = load i32, ptr %3, align 4, !tbaa !11
  %5 = mul i32 %4, %2
  %6 = zext i32 %5 to i64
  %7 = alloca i32, i64 %6, align 16
  %8 = tail call noalias ptr @fopen(ptr noundef nonnull @.str.13, ptr noundef nonnull @.str.14)
  %9 = icmp sgt i32 %5, 0
  br i1 %9, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %10 = shl nuw nsw i64 %6, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %7, i8 0, i64 %10, i1 false), !tbaa !12
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %1
  %11 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %8, ptr noundef nonnull @.str.15) #3
  %12 = load i32, ptr %3, align 4, !tbaa !11
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %.preheader.lr.ph, label %._crit_edge33

.preheader.lr.ph:                                 ; preds = %._crit_edge
  %14 = load i32, ptr %0, align 8, !tbaa !5
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.preheader, label %._crit_edge33

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge30
  %16 = phi i32 [ %20, %._crit_edge30 ], [ %12, %.preheader.lr.ph ]
  %17 = phi i32 [ %21, %._crit_edge30 ], [ %14, %.preheader.lr.ph ]
  %.032 = phi i32 [ %.1.lcssa, %._crit_edge30 ], [ 1, %.preheader.lr.ph ]
  %.02331 = phi i32 [ %22, %._crit_edge30 ], [ 0, %.preheader.lr.ph ]
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %.lr.ph29, label %._crit_edge30

._crit_edge33:                                    ; preds = %._crit_edge30, %.preheader.lr.ph, %._crit_edge
  %19 = call i32 @fclose(ptr noundef %8)
  ret void

._crit_edge30.loopexit:                           ; preds = %28
  %.pre = load i32, ptr %3, align 4, !tbaa !11
  br label %._crit_edge30

._crit_edge30:                                    ; preds = %._crit_edge30.loopexit, %.preheader
  %20 = phi i32 [ %16, %.preheader ], [ %.pre, %._crit_edge30.loopexit ]
  %21 = phi i32 [ %17, %.preheader ], [ %30, %._crit_edge30.loopexit ]
  %.1.lcssa = phi i32 [ %.032, %.preheader ], [ %.2, %._crit_edge30.loopexit ]
  %22 = add nuw nsw i32 %.02331, 1
  %23 = icmp slt i32 %22, %20
  br i1 %23, label %.preheader, label %._crit_edge33, !llvm.loop !13

.lr.ph29:                                         ; preds = %.preheader, %28
  %.128 = phi i32 [ %.2, %28 ], [ %.032, %.preheader ]
  %.02227 = phi i32 [ %29, %28 ], [ 0, %.preheader ]
  %24 = call noundef i32 @_Z3DFSP5ImagePiii(ptr noundef nonnull %0, ptr noundef nonnull %7, i32 noundef %.02227, i32 noundef %.02331)
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %28, label %25

25:                                               ; preds = %.lr.ph29
  %26 = add nsw i32 %.128, 1
  %27 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %8, ptr noundef nonnull @.str.16, i32 noundef %.128, i32 noundef %24) #3
  br label %28

28:                                               ; preds = %25, %.lr.ph29
  %.2 = phi i32 [ %26, %25 ], [ %.128, %.lr.ph29 ]
  %29 = add nuw nsw i32 %.02227, 1
  %30 = load i32, ptr %0, align 8, !tbaa !5
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %.lr.ph29, label %._crit_edge30.loopexit, !llvm.loop !16
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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" }
attributes #1 = { nofree nounwind "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: write) }
attributes #3 = { 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, !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 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14, !15}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.unswitch.partial.disable"}
!16 = distinct !{!16, !14}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass