; LLVM IR from Wilson-ZheLin/Introduction-to-Digital-Image-Processing :: 2. Image Reduction, Enlargement and Negative/src (main)

; 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 [19 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 noundef ptr @_Z27ImageReduce_AlternativeLineP5Imagef(ptr noundef %0, float noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca float, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %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 %3, align 8, !tbaa !5
  store float %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !11
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call noundef ptr @_Z14CreateNewImageP5ImagePcif(ptr noundef %13, ptr noundef @.str.13, i32 noundef 1, float noundef 5.000000e-01)
  store ptr %14, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Image, ptr %15, i32 0, i32 3
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  store ptr %17, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Image, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  store ptr %20, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !11
  br label %21

21:                                               ; preds = %70, %2
  %22 = load i32, ptr %10, align 4, !tbaa !11
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Image, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !15
  %26 = sdiv i32 %25, 2
  %27 = sub nsw i32 %26, 1
  %28 = icmp slt i32 %22, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %21
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %73

30:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !11
  br label %31

31:                                               ; preds = %64, %30
  %32 = load i32, ptr %12, align 4, !tbaa !11
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Image, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !16
  %36 = sdiv i32 %35, 2
  %37 = sub nsw i32 %36, 1
  %38 = icmp slt i32 %32, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %31
  store i32 5, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %67

40:                                               ; preds = %31
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Image, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !16
  %45 = load i32, ptr %8, align 4, !tbaa !11
  %46 = mul nsw i32 %44, %45
  %47 = load i32, ptr %9, align 4, !tbaa !11
  %48 = add nsw i32 %46, %47
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %41, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !17
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.Image, ptr %53, i32 0, i32 0
  %55 = load i32, ptr %54, align 8, !tbaa !16
  %56 = load i32, ptr %10, align 4, !tbaa !11
  %57 = mul nsw i32 %55, %56
  %58 = load i32, ptr %12, align 4, !tbaa !11
  %59 = add nsw i32 %57, %58
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i8, ptr %52, i64 %60
  store i8 %51, ptr %61, align 1, !tbaa !17
  %62 = load i32, ptr %9, align 4, !tbaa !11
  %63 = add nsw i32 %62, 2
  store i32 %63, ptr %9, align 4, !tbaa !11
  br label %64

64:                                               ; preds = %40
  %65 = load i32, ptr %12, align 4, !tbaa !11
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %12, align 4, !tbaa !11
  br label %31, !llvm.loop !18

67:                                               ; preds = %39
  %68 = load i32, ptr %8, align 4, !tbaa !11
  %69 = add nsw i32 %68, 2
  store i32 %69, ptr %8, align 4, !tbaa !11
  store i32 0, ptr %9, align 4, !tbaa !11
  br label %70

70:                                               ; preds = %67
  %71 = load i32, ptr %10, align 4, !tbaa !11
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %10, align 4, !tbaa !11
  br label %21, !llvm.loop !20

73:                                               ; preds = %29
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  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 8, ptr %5) #2
  ret ptr %74
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z14CreateNewImageP5ImagePcif(ptr noundef, ptr noundef, i32 noundef, float 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, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"_ZTS5Image", !12, i64 0, !12, i64 4, !12, i64 8, !6, i64 16, !7, i64 24, !12, i64 1524}
!15 = !{!14, !12, i64 4}
!16 = !{!14, !12, i64 0}
!17 = !{!7, !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.13 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z27ImageReduce_AlternativeLineP5Imagef(ptr noundef %0, float noundef %1) local_unnamed_addr #0 {
  %3 = tail call noundef ptr @_Z14CreateNewImageP5ImagePcif(ptr noundef %0, ptr noundef nonnull @.str.13, i32 noundef 1, float noundef 5.000000e-01)
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %3, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = icmp sgt i32 %9, 3
  br i1 %10, label %.preheader.preheader, label %._crit_edge26

.preheader.preheader:                             ; preds = %2
  %.pre = load i32, ptr %0, align 8, !tbaa !12
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %11 = phi i32 [ %14, %._crit_edge ], [ %9, %.preheader.preheader ]
  %12 = phi i32 [ %15, %._crit_edge ], [ %.pre, %.preheader.preheader ]
  %.01925 = phi i32 [ %17, %._crit_edge ], [ 0, %.preheader.preheader ]
  %.02124 = phi i32 [ %16, %._crit_edge ], [ 0, %.preheader.preheader ]
  %13 = icmp sgt i32 %12, 3
  br i1 %13, label %.lr.ph, label %._crit_edge

._crit_edge26:                                    ; preds = %._crit_edge, %2
  ret ptr %3

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

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %14 = phi i32 [ %.pre27, %._crit_edge.loopexit ], [ %11, %.preheader ]
  %15 = phi i32 [ %34, %._crit_edge.loopexit ], [ %12, %.preheader ]
  %16 = add nuw nsw i32 %.02124, 2
  %17 = add nuw nsw i32 %.01925, 1
  %18 = sdiv i32 %14, 2
  %19 = add nsw i32 %18, -1
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %.preheader, label %._crit_edge26, !llvm.loop !13

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %21 = phi i32 [ %34, %.lr.ph ], [ %12, %.preheader ]
  %.023 = phi i32 [ %33, %.lr.ph ], [ 0, %.preheader ]
  %.122 = phi i32 [ %32, %.lr.ph ], [ 0, %.preheader ]
  %22 = mul nsw i32 %21, %.02124
  %23 = add nsw i32 %22, %.122
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %5, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !15
  %27 = load i32, ptr %3, align 8, !tbaa !12
  %28 = mul nsw i32 %27, %.01925
  %29 = add nsw i32 %28, %.023
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %7, i64 %30
  store i8 %26, ptr %31, align 1, !tbaa !15
  %32 = add nuw nsw i32 %.122, 2
  %33 = add nuw nsw i32 %.023, 1
  %34 = load i32, ptr %0, align 8, !tbaa !12
  %35 = sdiv i32 %34, 2
  %36 = add nsw i32 %35, -1
  %37 = icmp slt i32 %33, %36
  br i1 %37, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z14CreateNewImageP5ImagePcif(ptr noundef, ptr noundef, i32 noundef, float 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, !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}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!8, !8, i64 0}
!16 = distinct !{!16, !14}


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