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

; 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.14 = external hidden unnamed_addr constant [18 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 @_Z8NegativeP5Image(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
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = call noundef ptr @_Z14CreateNewImageP5ImagePcif(ptr noundef %8, ptr noundef @.str.14, i32 noundef 0, float noundef 1.000000e+00)
  store ptr %9, ptr %5, 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 %5, 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !15
  br label %44

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

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

49:                                               ; preds = %44
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = load i8, ptr %50, align 1, !tbaa !16
  %52 = zext i8 %51 to i32
  %53 = sub nsw i32 255, %52
  %54 = trunc i32 %53 to i8
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  store i8 %54, ptr %55, align 1, !tbaa !16
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds i8, ptr %56, i32 1
  store ptr %57, ptr %3, align 8, !tbaa !5
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %4, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %49
  %61 = load i32, ptr %7, align 4, !tbaa !15
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %7, align 4, !tbaa !15
  br label %44, !llvm.loop !17

63:                                               ; preds = %48
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  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 ptr %64
}

; 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, !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.14 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z8NegativeP5Image(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef ptr @_Z14CreateNewImageP5ImagePcif(ptr noundef %0, ptr noundef nonnull @.str.14, i32 noundef 0, float noundef 1.000000e+00)
  %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 = xor <16 x i8> %wide.load, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  %28 = xor <16 x i8> %wide.load28, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  %29 = getelementptr i8, ptr %next.gep26, i64 16
  store <16 x i8> %27, ptr %next.gep26, align 1, !tbaa !14
  store <16 x i8> %28, ptr %29, align 1, !tbaa !14
  %index.next = add nuw i64 %index, 32
  %30 = icmp eq i64 %index.next, %n.vec
  br i1 %30, 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 = getelementptr i8, ptr %4, i64 %n.vec
  %ind.end31 = trunc nuw nsw i64 %n.vec to i32
  %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

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %.022.ph = phi i32 [ 0, %iter.check ], [ %ind.end31, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.01721.ph = phi ptr [ %4, %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 ]
  %31 = sub i32 %.016, %.022.ph
  %xtraiter = and i32 %31, 3
  %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 i32 [ %36, %.lr.ph.prol ], [ %.022.ph, %.lr.ph.preheader ]
  %.01721.prol = phi ptr [ %34, %.lr.ph.prol ], [ %.01721.ph, %.lr.ph.preheader ]
  %.01820.prol = phi ptr [ %35, %.lr.ph.prol ], [ %.01820.ph, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %32 = load i8, ptr %.01721.prol, align 1, !tbaa !14
  %33 = xor i8 %32, -1
  store i8 %33, ptr %.01820.prol, align 1, !tbaa !14
  %34 = getelementptr inbounds i8, ptr %.01721.prol, i64 1
  %35 = getelementptr inbounds i8, ptr %.01820.prol, i64 1
  %36 = add nuw nsw i32 %.022.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 !19

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.022.unr = phi i32 [ %.022.ph, %.lr.ph.preheader ], [ %36, %.lr.ph.prol ]
  %.01721.unr = phi ptr [ %.01721.ph, %.lr.ph.preheader ], [ %34, %.lr.ph.prol ]
  %.01820.unr = phi ptr [ %.01820.ph, %.lr.ph.preheader ], [ %35, %.lr.ph.prol ]
  %37 = sub i32 %.022.ph, %.016
  %38 = icmp ugt i32 %37, -4
  br i1 %38, 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.vec30 = and i64 %24, 2147483640
  %ind.end = trunc nuw nsw i64 %n.vec30 to i32
  %ind.end32 = getelementptr i8, ptr %4, i64 %n.vec30
  %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
  %39 = xor <8 x i8> %wide.load41, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  store <8 x i8> %39, ptr %next.gep40, align 1, !tbaa !14
  %index.next42 = add nuw i64 %index38, 8
  %40 = icmp eq i64 %index.next42, %n.vec30
  br i1 %40, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !21

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

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %vec.epilog.middle.block, %1, %22
  ret ptr %2

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

; 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 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, !20}
!20 = !{!"llvm.loop.unroll.disable"}
!21 = distinct !{!21, !16, !17, !18}
!22 = distinct !{!22, !16, !17}


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