; LLVM IR from zchrissirhcz/pixel :: perfs/flip (flip)

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

16:                                               ; preds = %52, %4
  %17 = load i64, ptr %10, align 8, !tbaa !9
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %48, %21
  %23 = load i64, ptr %12, align 8, !tbaa !9
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %27, label %26

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

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %28 = load i64, ptr %10, align 8, !tbaa !9
  %29 = load i64, ptr %9, align 8, !tbaa !9
  %30 = mul i64 %28, %29
  %31 = load i64, ptr %12, align 8, !tbaa !9
  %32 = add i64 %30, %31
  store i64 %32, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = sub i64 %33, 1
  %35 = load i64, ptr %10, align 8, !tbaa !9
  %36 = sub i64 %34, %35
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = mul i64 %36, %37
  %39 = load i64, ptr %12, align 8, !tbaa !9
  %40 = add i64 %38, %39
  store i64 %40, ptr %14, align 8, !tbaa !9
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i64, ptr %13, align 8, !tbaa !9
  %43 = getelementptr inbounds i8, ptr %41, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !11
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = load i64, ptr %14, align 8, !tbaa !9
  %47 = getelementptr inbounds i8, ptr %45, i64 %46
  store i8 %44, ptr %47, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %48

48:                                               ; preds = %27
  %49 = load i64, ptr %12, align 8, !tbaa !9
  %50 = add i64 %49, 1
  store i64 %50, ptr %12, align 8, !tbaa !9
  br label %22, !llvm.loop !12

51:                                               ; preds = %26
  br label %52

52:                                               ; preds = %51
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = add i64 %53, 1
  store i64 %54, ptr %10, align 8, !tbaa !9
  br label %16, !llvm.loop !14

55:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @flip_vert_gray_naive(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %1, 0
  %.not23 = icmp eq i64 %2, 0
  %or.cond = or i1 %.not, %.not23
  br i1 %or.cond, label %._crit_edge22, label %iter.check.preheader

iter.check.preheader:                             ; preds = %4
  %5 = add i64 %2, -1
  %xtraiter = and i64 %2, 3
  %6 = icmp ult i64 %5, 3
  %unroll_iter = and i64 %2, -4
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %iter.check

iter.check:                                       ; preds = %iter.check.preheader, %._crit_edge.us
  %.021.us = phi i64 [ %33, %._crit_edge.us ], [ 0, %iter.check.preheader ]
  %7 = mul i64 %.021.us, %2
  %8 = xor i64 %.021.us, -1
  %9 = add i64 %8, %1
  %10 = mul i64 %9, %2
  %11 = getelementptr i8, ptr %0, i64 %7
  %12 = getelementptr i8, ptr %3, i64 %10
  br i1 %6, label %._crit_edge.us.unr-lcssa, label %iter.check.new

iter.check.new:                                   ; preds = %iter.check, %iter.check.new
  %.01720.us = phi i64 [ %28, %iter.check.new ], [ 0, %iter.check ]
  %niter = phi i64 [ %niter.next.3, %iter.check.new ], [ 0, %iter.check ]
  %13 = getelementptr i8, ptr %11, i64 %.01720.us
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %15 = getelementptr i8, ptr %12, i64 %.01720.us
  store i8 %14, ptr %15, align 1, !tbaa !5
  %16 = or disjoint i64 %.01720.us, 1
  %17 = getelementptr i8, ptr %11, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %19 = getelementptr i8, ptr %12, i64 %16
  store i8 %18, ptr %19, align 1, !tbaa !5
  %20 = or disjoint i64 %.01720.us, 2
  %21 = getelementptr i8, ptr %11, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %23 = getelementptr i8, ptr %12, i64 %20
  store i8 %22, ptr %23, align 1, !tbaa !5
  %24 = or disjoint i64 %.01720.us, 3
  %25 = getelementptr i8, ptr %11, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !5
  %27 = getelementptr i8, ptr %12, i64 %24
  store i8 %26, ptr %27, align 1, !tbaa !5
  %28 = add nuw i64 %.01720.us, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.unr-lcssa, label %iter.check.new, !llvm.loop !8

._crit_edge.us.unr-lcssa:                         ; preds = %iter.check.new, %iter.check
  %.01720.us.unr = phi i64 [ 0, %iter.check ], [ %28, %iter.check.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %.01720.us.epil = phi i64 [ %32, %.epil.preheader ], [ %.01720.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %29 = getelementptr i8, ptr %11, i64 %.01720.us.epil
  %30 = load i8, ptr %29, align 1, !tbaa !5
  %31 = getelementptr i8, ptr %12, i64 %.01720.us.epil
  store i8 %30, ptr %31, align 1, !tbaa !5
  %32 = add nuw i64 %.01720.us.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us, label %.epil.preheader, !llvm.loop !11

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %33 = add nuw i64 %.021.us, 1
  %exitcond26.not = icmp eq i64 %33, %1
  br i1 %exitcond26.not, label %._crit_edge22, label %iter.check, !llvm.loop !13

._crit_edge22:                                    ; preds = %._crit_edge.us, %4
  ret void
}

attributes #0 = { 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" }

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


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