; LLVM IR from ermig1979/Simd :: src/Simd/SimdBaseImageSavePng.ll (ZN4Simd4Base11EncodeLine0EPKhmmmPa)

; 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"

; 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 nounwind uwtable
define dso_local noundef i32 @_ZN4Simd4Base11EncodeLine0EPKhmmmPa(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %34, %5
  %14 = load i64, ptr %12, align 8, !tbaa !9
  %15 = load i64, ptr %9, align 8, !tbaa !9
  %16 = icmp ult i64 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %37

18:                                               ; preds = %13
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %12, align 8, !tbaa !9
  %21 = getelementptr inbounds i8, ptr %19, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !13
  %23 = load ptr, ptr %10, align 8, !tbaa !5
  %24 = load i64, ptr %12, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  store i8 %22, ptr %25, align 1, !tbaa !13
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = load i64, ptr %12, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %26, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !13
  %30 = sext i8 %29 to i32
  %31 = call i32 @llvm.abs.i32(i32 %30, i1 true)
  %32 = load i32, ptr %11, align 4, !tbaa !11
  %33 = add i32 %32, %31
  store i32 %33, ptr %11, align 4, !tbaa !11
  br label %34

34:                                               ; preds = %18
  %35 = load i64, ptr %12, align 8, !tbaa !9
  %36 = add i64 %35, 1
  store i64 %36, ptr %12, align 8, !tbaa !9
  br label %13, !llvm.loop !14

37:                                               ; preds = %17
  %38 = load i32, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  ret i32 %38
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef i32 @_ZN4Simd4Base11EncodeLine0EPKhmmmPa(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %5
  %6 = ptrtoint ptr %4 to i64
  %7 = ptrtoint ptr %0 to i64
  %min.iters.check = icmp ult i64 %3, 8
  %8 = sub i64 %6, %7
  %diff.check = icmp ult i64 %8, 8
  %or.cond = or i1 %min.iters.check, %diff.check
  br i1 %or.cond, label %.lr.ph.preheader15, label %vector.ph

.lr.ph.preheader15:                               ; preds = %middle.block, %.lr.ph.preheader
  %.012.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.01011.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %29, %middle.block ]
  %xtraiter = and i64 %3, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader15
  %9 = getelementptr inbounds i8, ptr %0, i64 %.012.ph
  %10 = load i8, ptr %9, align 1, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %4, i64 %.012.ph
  store i8 %10, ptr %11, align 1, !tbaa !5
  %12 = tail call i8 @llvm.abs.i8(i8 %10, i1 false)
  %13 = zext i8 %12 to i32
  %14 = add i32 %.01011.ph, %13
  %15 = or disjoint i64 %.012.ph, 1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader15
  %.lcssa.unr = phi i32 [ poison, %.lr.ph.preheader15 ], [ %14, %.lr.ph.prol ]
  %.012.unr = phi i64 [ %.012.ph, %.lr.ph.preheader15 ], [ %15, %.lr.ph.prol ]
  %.01011.unr = phi i32 [ %.01011.ph, %.lr.ph.preheader15 ], [ %14, %.lr.ph.prol ]
  %16 = add i64 %3, -1
  %17 = icmp eq i64 %.012.ph, %16
  br i1 %17, label %._crit_edge, label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %3, -8
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %26, %vector.body ]
  %vec.phi13 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %27, %vector.body ]
  %18 = getelementptr inbounds i8, ptr %0, i64 %index
  %19 = getelementptr inbounds i8, ptr %18, i64 4
  %wide.load = load <4 x i8>, ptr %18, align 1, !tbaa !5
  %wide.load14 = load <4 x i8>, ptr %19, align 1, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %4, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 4
  store <4 x i8> %wide.load, ptr %20, align 1, !tbaa !5
  store <4 x i8> %wide.load14, ptr %21, align 1, !tbaa !5
  %22 = tail call <4 x i8> @llvm.abs.v4i8(<4 x i8> %wide.load, i1 false)
  %23 = tail call <4 x i8> @llvm.abs.v4i8(<4 x i8> %wide.load14, i1 false)
  %24 = zext <4 x i8> %22 to <4 x i32>
  %25 = zext <4 x i8> %23 to <4 x i32>
  %26 = add <4 x i32> %vec.phi, %24
  %27 = add <4 x i32> %vec.phi13, %25
  %index.next = add nuw i64 %index, 8
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %27, %26
  %29 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %3
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader15

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %5
  %.010.lcssa = phi i32 [ 0, %5 ], [ %29, %middle.block ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %42, %.lr.ph ]
  ret i32 %.010.lcssa

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.012 = phi i64 [ %43, %.lr.ph ], [ %.012.unr, %.lr.ph.prol.loopexit ]
  %.01011 = phi i32 [ %42, %.lr.ph ], [ %.01011.unr, %.lr.ph.prol.loopexit ]
  %30 = getelementptr inbounds i8, ptr %0, i64 %.012
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %4, i64 %.012
  store i8 %31, ptr %32, align 1, !tbaa !5
  %33 = tail call i8 @llvm.abs.i8(i8 %31, i1 false)
  %34 = zext i8 %33 to i32
  %35 = add i32 %.01011, %34
  %36 = add nuw i64 %.012, 1
  %37 = getelementptr inbounds i8, ptr %0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %4, i64 %36
  store i8 %38, ptr %39, align 1, !tbaa !5
  %40 = tail call i8 @llvm.abs.i8(i8 %38, i1 false)
  %41 = zext i8 %40 to i32
  %42 = add i32 %35, %41
  %43 = add nuw i64 %.012, 2
  %exitcond.not.1 = icmp eq i64 %43, %3
  br i1 %exitcond.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !12
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.abs.i8(i8, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i8> @llvm.abs.v4i8(<4 x i8>, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !10}


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