; LLVM IR from Wilson-ZheLin/Introduction-to-Digital-Image-Processing :: 9. Thresholding, Roberts, Prewitt, Sobel, and Edge Detection/src/main.ll (Z7RobertsP5Image)

; 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.5 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [12 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 @_Z7RobertsP5Image(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca float, align 4
  %6 = alloca float, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = call noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef %11, ptr noundef @.str.5)
  store ptr %12, ptr %7, align 8, !tbaa !5
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Image, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Image, ptr %16, i32 0, i32 3
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  store ptr %18, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !12
  br label %19

19:                                               ; preds = %115, %1
  %20 = load i32, ptr %8, align 4, !tbaa !12
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Image, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !13
  %24 = sub nsw i32 %23, 1
  %25 = icmp slt i32 %20, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %19
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %118

27:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !12
  br label %28

28:                                               ; preds = %111, %27
  %29 = load i32, ptr %10, align 4, !tbaa !12
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Image, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !14
  %33 = sub nsw i32 %32, 1
  %34 = icmp slt i32 %29, %33
  br i1 %34, label %36, label %35

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

36:                                               ; preds = %28
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.Image, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !14
  %41 = load i32, ptr %8, align 4, !tbaa !12
  %42 = mul nsw i32 %40, %41
  %43 = load i32, ptr %10, align 4, !tbaa !12
  %44 = add nsw i32 %42, %43
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %37, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !15
  %48 = uitofp i8 %47 to float
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %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 !14
  %53 = load i32, ptr %8, align 4, !tbaa !12
  %54 = add nsw i32 %53, 1
  %55 = mul nsw i32 %52, %54
  %56 = load i32, ptr %10, align 4, !tbaa !12
  %57 = add nsw i32 %55, %56
  %58 = add nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds i8, ptr %49, i64 %59
  %61 = load i8, ptr %60, align 1, !tbaa !15
  %62 = uitofp i8 %61 to float
  %63 = fsub float %48, %62
  %64 = call noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef %63, i32 noundef 2)
  %65 = fptrunc double %64 to float
  store float %65, ptr %5, align 4, !tbaa !16
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Image, ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 8, !tbaa !14
  %70 = load i32, ptr %8, align 4, !tbaa !12
  %71 = mul nsw i32 %69, %70
  %72 = load i32, ptr %10, align 4, !tbaa !12
  %73 = add nsw i32 %71, %72
  %74 = add nsw i32 %73, 1
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds i8, ptr %66, i64 %75
  %77 = load i8, ptr %76, align 1, !tbaa !15
  %78 = uitofp i8 %77 to float
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = load ptr, ptr %2, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Image, ptr %80, i32 0, i32 0
  %82 = load i32, ptr %81, align 8, !tbaa !14
  %83 = load i32, ptr %8, align 4, !tbaa !12
  %84 = add nsw i32 %83, 1
  %85 = mul nsw i32 %82, %84
  %86 = load i32, ptr %10, align 4, !tbaa !12
  %87 = add nsw i32 %85, %86
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds i8, ptr %79, i64 %88
  %90 = load i8, ptr %89, align 1, !tbaa !15
  %91 = uitofp i8 %90 to float
  %92 = fsub float %78, %91
  %93 = call noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef %92, i32 noundef 2)
  %94 = fptrunc double %93 to float
  store float %94, ptr %6, align 4, !tbaa !16
  %95 = load float, ptr %5, align 4, !tbaa !16
  %96 = load float, ptr %6, align 4, !tbaa !16
  %97 = fadd float %95, %96
  %98 = call noundef float @_ZSt4sqrtf(float noundef %97)
  %99 = fptosi float %98 to i32
  %100 = trunc i32 %99 to i8
  %101 = load ptr, ptr %4, align 8, !tbaa !5
  %102 = load ptr, ptr %2, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.Image, ptr %102, i32 0, i32 0
  %104 = load i32, ptr %103, align 8, !tbaa !14
  %105 = load i32, ptr %8, align 4, !tbaa !12
  %106 = mul nsw i32 %104, %105
  %107 = load i32, ptr %10, align 4, !tbaa !12
  %108 = add nsw i32 %106, %107
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds i8, ptr %101, i64 %109
  store i8 %100, ptr %110, align 1, !tbaa !15
  br label %111

111:                                              ; preds = %36
  %112 = load i32, ptr %10, align 4, !tbaa !12
  %113 = add nsw i32 %112, 1
  store i32 %113, ptr %10, align 4, !tbaa !12
  br label %28, !llvm.loop !18

114:                                              ; preds = %35
  br label %115

115:                                              ; preds = %114
  %116 = load i32, ptr %8, align 4, !tbaa !12
  %117 = add nsw i32 %116, 1
  store i32 %117, ptr %8, align 4, !tbaa !12
  br label %19, !llvm.loop !20

118:                                              ; preds = %26
  %119 = load ptr, ptr %7, align 8, !tbaa !5
  %120 = call noundef ptr @_Z9ThresholdP5Image(ptr noundef %119)
  store ptr %120, ptr %7, align 8, !tbaa !5
  %121 = load ptr, ptr %7, align 8, !tbaa !5
  call void @_Z12SavePNMImageP5ImagePc(ptr noundef %121, ptr noundef @.str.6)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef, i32 noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef float @_ZSt4sqrtf(float noundef) #2

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z12SavePNMImageP5ImagePc(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 = { inlinehint 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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 16}
!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 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 4}
!14 = !{!10, !11, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"float", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; 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.5 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7RobertsP5Image(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef %0, ptr noundef nonnull @.str.5)
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %2, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = icmp sgt i32 %8, 1
  br i1 %9, label %.preheader.lr.ph, label %._crit_edge37

.preheader.lr.ph:                                 ; preds = %1
  %invariant.gep = getelementptr i8, ptr %4, i64 1
  %10 = load i32, ptr %0, align 8, !tbaa !12
  %11 = icmp sgt i32 %10, 1
  br i1 %11, label %.preheader, label %._crit_edge37

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %12 = phi i32 [ %17, %._crit_edge ], [ %8, %.preheader.lr.ph ]
  %13 = phi i32 [ %18, %._crit_edge ], [ %10, %.preheader.lr.ph ]
  %.03236 = phi i32 [ %15, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %14 = icmp sgt i32 %13, 1
  %15 = add nuw nsw i32 %.03236, 1
  br i1 %14, label %.lr.ph, label %._crit_edge

._crit_edge37:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %1
  %16 = tail call noundef ptr @_Z9ThresholdP5Image(ptr noundef %2)
  tail call void @_Z12SavePNMImageP5ImagePc(ptr noundef %16, ptr noundef nonnull @.str.6)
  ret void

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr %7, align 4, !tbaa !11
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader, %._crit_edge.loopexit
  %17 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %12, %.preheader ]
  %18 = phi i32 [ %61, %._crit_edge.loopexit ], [ %13, %.preheader ]
  %19 = add nsw i32 %17, -1
  %20 = icmp slt i32 %15, %19
  br i1 %20, label %.preheader, label %._crit_edge37, !llvm.loop !13

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %21 = phi i32 [ %61, %.lr.ph ], [ %13, %.preheader ]
  %.035 = phi i32 [ %60, %.lr.ph ], [ 0, %.preheader ]
  %22 = mul nsw i32 %21, %.03236
  %23 = add nsw i32 %22, %.035
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %4, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !16
  %27 = uitofp i8 %26 to float
  %28 = mul nsw i32 %21, %15
  %29 = add nsw i32 %28, %.035
  %30 = sext i32 %29 to i64
  %gep = getelementptr i8, ptr %invariant.gep, i64 %30
  %31 = load i8, ptr %gep, align 1, !tbaa !16
  %32 = uitofp i8 %31 to float
  %33 = fsub float %27, %32
  %34 = tail call noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef %33, i32 noundef 2)
  %35 = fptrunc double %34 to float
  %36 = load i32, ptr %0, align 8, !tbaa !12
  %37 = mul nsw i32 %36, %.03236
  %38 = add nsw i32 %37, %.035
  %39 = sext i32 %38 to i64
  %gep34 = getelementptr i8, ptr %invariant.gep, i64 %39
  %40 = load i8, ptr %gep34, align 1, !tbaa !16
  %41 = uitofp i8 %40 to float
  %42 = mul nsw i32 %36, %15
  %43 = add nsw i32 %42, %.035
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %4, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !16
  %47 = uitofp i8 %46 to float
  %48 = fsub float %41, %47
  %49 = tail call noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef %48, i32 noundef 2)
  %50 = fptrunc double %49 to float
  %51 = fadd float %35, %50
  %52 = tail call noundef float @_ZSt4sqrtf(float noundef %51)
  %53 = fptosi float %52 to i32
  %54 = trunc i32 %53 to i8
  %55 = load i32, ptr %0, align 8, !tbaa !12
  %56 = mul nsw i32 %55, %.03236
  %57 = add nsw i32 %56, %.035
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %6, i64 %58
  store i8 %54, ptr %59, align 1, !tbaa !16
  %60 = add nuw nsw i32 %.035, 1
  %61 = load i32, ptr %0, align 8, !tbaa !12
  %62 = add nsw i32 %61, -1
  %63 = icmp slt i32 %60, %62
  br i1 %63, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !17
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef float @_ZSt4sqrtf(float noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z12SavePNMImageP5ImagePc(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" }
attributes #1 = { inlinehint 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" }

!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, !10, i64 16}
!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, !7, i64 0}
!13 = distinct !{!13, !14, !15}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.unswitch.partial.disable"}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !14}


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