; LLVM IR from Wilson-ZheLin/Introduction-to-Digital-Image-Processing :: 1. Average Filter and Median Filter/src/main.ll (Z9SwapImageP5Image)

; 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 [14 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 @_Z14CreateNewImageP5ImagePc(ptr noundef, ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z9SwapImageP5Image(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  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 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = call noundef ptr @_Z14CreateNewImageP5ImagePc(ptr noundef %8, ptr noundef @.str.5)
  store ptr %9, ptr %7, align 8, !tbaa !5
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Image, ptr %10, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %12, ptr %3, align 8, !tbaa !5
  %13 = load ptr, ptr %7, 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 %4, align 8, !tbaa !5
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Image, ptr %16, i32 0, i32 2
  %18 = load i32, ptr %17, align 8, !tbaa !12
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %20, label %28

20:                                               ; preds = %1
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Image, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Image, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = mul nsw i32 %23, %26
  store i32 %27, ptr %6, align 4, !tbaa !15
  br label %43

28:                                               ; preds = %1
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.Image, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 8, !tbaa !12
  %32 = icmp eq i32 %31, 2
  br i1 %32, label %33, label %42

33:                                               ; preds = %28
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Image, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !13
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Image, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !14
  %40 = mul nsw i32 %36, %39
  %41 = mul nsw i32 %40, 3
  store i32 %41, ptr %6, align 4, !tbaa !15
  br label %42

42:                                               ; preds = %33, %28
  br label %43

43:                                               ; preds = %42, %20
  store i32 0, ptr %5, align 4, !tbaa !15
  br label %44

44:                                               ; preds = %56, %43
  %45 = load i32, ptr %5, align 4, !tbaa !15
  %46 = load i32, ptr %6, align 4, !tbaa !15
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %59

48:                                               ; preds = %44
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = load i8, ptr %49, align 1, !tbaa !16
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  store i8 %50, ptr %51, align 1, !tbaa !16
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i32 1
  store ptr %53, ptr %3, align 8, !tbaa !5
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %54, i32 1
  store ptr %55, ptr %4, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %48
  %57 = load i32, ptr %5, align 4, !tbaa !15
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %5, align 4, !tbaa !15
  br label %44, !llvm.loop !17

59:                                               ; preds = %44
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, 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 ptr %60
}

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, !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 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 0}
!14 = !{!10, !11, i64 4}
!15 = !{!11, !11, i64 0}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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.5 = external hidden unnamed_addr constant [14 x i8], align 1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z9SwapImageP5Image(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 = ptrtoint ptr %4 to i64
  %6 = getelementptr inbounds i8, ptr %2, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = ptrtoint ptr %7 to i64
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !11
  switch i32 %10, label %._crit_edge [
    i32 1, label %11
    i32 2, label %16
  ]

11:                                               ; preds = %1
  %12 = load i32, ptr %0, align 8, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %0, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = mul nsw i32 %14, %12
  br label %22

16:                                               ; preds = %1
  %17 = load i32, ptr %0, align 8, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %0, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = mul i32 %17, 3
  %21 = mul i32 %20, %19
  br label %22

22:                                               ; preds = %16, %11
  %.016 = phi i32 [ %15, %11 ], [ %21, %16 ]
  %23 = icmp sgt i32 %.016, 0
  br i1 %23, label %iter.check, label %._crit_edge

iter.check:                                       ; preds = %22
  %24 = zext nneg i32 %.016 to i64
  %min.iters.check = icmp ult i32 %.016, 8
  %25 = sub i64 %8, %5
  %diff.check = icmp ult i64 %25, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %.lr.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %iter.check
  %min.iters.check24 = icmp ult i32 %.016, 32
  br i1 %min.iters.check24, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %24, 2147483616
  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 %4, i64 %index
  %next.gep26 = getelementptr i8, ptr %7, i64 %index
  %26 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <16 x i8>, ptr %next.gep, align 1, !tbaa !14
  %wide.load28 = load <16 x i8>, ptr %26, align 1, !tbaa !14
  %27 = getelementptr i8, ptr %next.gep26, i64 16
  store <16 x i8> %wide.load, ptr %next.gep26, align 1, !tbaa !14
  store <16 x i8> %wide.load28, ptr %27, align 1, !tbaa !14
  %index.next = add nuw i64 %index, 32
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, label %middle.block, label %vector.body, !llvm.loop !15

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

vec.epilog.iter.check:                            ; preds = %middle.block
  %ind.end36 = getelementptr i8, ptr %7, i64 %n.vec
  %ind.end33 = trunc nuw nsw i64 %n.vec to i32
  %ind.end31 = getelementptr i8, ptr %4, i64 %n.vec
  %n.vec.remaining = and i64 %24, 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

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.vec30 = and i64 %24, 2147483640
  %ind.end = getelementptr i8, ptr %4, i64 %n.vec30
  %ind.end32 = trunc nuw nsw i64 %n.vec30 to i32
  %ind.end35 = getelementptr i8, ptr %7, i64 %n.vec30
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index38 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next42, %vec.epilog.vector.body ]
  %next.gep39 = getelementptr i8, ptr %4, i64 %index38
  %next.gep40 = getelementptr i8, ptr %7, i64 %index38
  %wide.load41 = load <8 x i8>, ptr %next.gep39, align 1, !tbaa !14
  store <8 x i8> %wide.load41, ptr %next.gep40, align 1, !tbaa !14
  %index.next42 = add nuw i64 %index38, 8
  %29 = icmp eq i64 %index.next42, %n.vec30
  br i1 %29, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !19

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n43 = icmp eq i64 %n.vec30, %24
  br i1 %cmp.n43, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %.022.ph = phi ptr [ %4, %iter.check ], [ %ind.end31, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.01721.ph = phi i32 [ 0, %iter.check ], [ %ind.end33, %vec.epilog.iter.check ], [ %ind.end32, %vec.epilog.middle.block ]
  %.01820.ph = phi ptr [ %7, %iter.check ], [ %ind.end36, %vec.epilog.iter.check ], [ %ind.end35, %vec.epilog.middle.block ]
  %30 = sub i32 %.016, %.01721.ph
  %xtraiter = and i32 %30, 7
  %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, %.lr.ph.prol
  %.022.prol = phi ptr [ %32, %.lr.ph.prol ], [ %.022.ph, %.lr.ph.preheader ]
  %.01721.prol = phi i32 [ %34, %.lr.ph.prol ], [ %.01721.ph, %.lr.ph.preheader ]
  %.01820.prol = phi ptr [ %33, %.lr.ph.prol ], [ %.01820.ph, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %31 = load i8, ptr %.022.prol, align 1, !tbaa !14
  store i8 %31, ptr %.01820.prol, align 1, !tbaa !14
  %32 = getelementptr inbounds i8, ptr %.022.prol, i64 1
  %33 = getelementptr inbounds i8, ptr %.01820.prol, i64 1
  %34 = add nuw nsw i32 %.01721.prol, 1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !20

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.022.unr = phi ptr [ %.022.ph, %.lr.ph.preheader ], [ %32, %.lr.ph.prol ]
  %.01721.unr = phi i32 [ %.01721.ph, %.lr.ph.preheader ], [ %34, %.lr.ph.prol ]
  %.01820.unr = phi ptr [ %.01820.ph, %.lr.ph.preheader ], [ %33, %.lr.ph.prol ]
  %35 = sub i32 %.01721.ph, %.016
  %36 = icmp ugt i32 %35, -8
  br i1 %36, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.022 = phi ptr [ %59, %.lr.ph ], [ %.022.unr, %.lr.ph.prol.loopexit ]
  %.01721 = phi i32 [ %61, %.lr.ph ], [ %.01721.unr, %.lr.ph.prol.loopexit ]
  %.01820 = phi ptr [ %60, %.lr.ph ], [ %.01820.unr, %.lr.ph.prol.loopexit ]
  %37 = load i8, ptr %.022, align 1, !tbaa !14
  store i8 %37, ptr %.01820, align 1, !tbaa !14
  %38 = getelementptr inbounds i8, ptr %.022, i64 1
  %39 = getelementptr inbounds i8, ptr %.01820, i64 1
  %40 = load i8, ptr %38, align 1, !tbaa !14
  store i8 %40, ptr %39, align 1, !tbaa !14
  %41 = getelementptr inbounds i8, ptr %.022, i64 2
  %42 = getelementptr inbounds i8, ptr %.01820, i64 2
  %43 = load i8, ptr %41, align 1, !tbaa !14
  store i8 %43, ptr %42, align 1, !tbaa !14
  %44 = getelementptr inbounds i8, ptr %.022, i64 3
  %45 = getelementptr inbounds i8, ptr %.01820, i64 3
  %46 = load i8, ptr %44, align 1, !tbaa !14
  store i8 %46, ptr %45, align 1, !tbaa !14
  %47 = getelementptr inbounds i8, ptr %.022, i64 4
  %48 = getelementptr inbounds i8, ptr %.01820, i64 4
  %49 = load i8, ptr %47, align 1, !tbaa !14
  store i8 %49, ptr %48, align 1, !tbaa !14
  %50 = getelementptr inbounds i8, ptr %.022, i64 5
  %51 = getelementptr inbounds i8, ptr %.01820, i64 5
  %52 = load i8, ptr %50, align 1, !tbaa !14
  store i8 %52, ptr %51, align 1, !tbaa !14
  %53 = getelementptr inbounds i8, ptr %.022, i64 6
  %54 = getelementptr inbounds i8, ptr %.01820, i64 6
  %55 = load i8, ptr %53, align 1, !tbaa !14
  store i8 %55, ptr %54, align 1, !tbaa !14
  %56 = getelementptr inbounds i8, ptr %.022, i64 7
  %57 = getelementptr inbounds i8, ptr %.01820, i64 7
  %58 = load i8, ptr %56, align 1, !tbaa !14
  store i8 %58, ptr %57, align 1, !tbaa !14
  %59 = getelementptr inbounds i8, ptr %.022, i64 8
  %60 = getelementptr inbounds i8, ptr %.01820, i64 8
  %61 = add nuw nsw i32 %.01721, 8
  %exitcond.not.7 = icmp eq i32 %61, %.016
  br i1 %exitcond.not.7, label %._crit_edge, label %.lr.ph, !llvm.loop !22

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %vec.epilog.middle.block, %1, %22
  ret ptr %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" }

!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 8}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 4}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16, !17, !18}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !16, !17, !18}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.unroll.disable"}
!22 = distinct !{!22, !16, !17}


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