; LLVM IR from DeanoC/gfx_imagecompress :: src/amd_hdr_encode.ll (ZN10HDR_Encode14sortProjectionEPfPii)

; 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.HDR_Encode::a" = type { float, i32 }

; 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 void @_ZN10HDR_Encode14sortProjectionEPfPii(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca [193 x %"struct.HDR_Encode::a"], align 16
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 1544, ptr %8) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %27, %3
  %10 = load i32, ptr %7, align 4, !tbaa !9
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %30

13:                                               ; preds = %9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds float, ptr %14, i64 %16
  %18 = load float, ptr %17, align 4, !tbaa !11
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %8, i64 0, i64 %21
  %23 = getelementptr inbounds %"struct.HDR_Encode::a", ptr %22, i32 0, i32 1
  store i32 %19, ptr %23, align 4, !tbaa !13
  %24 = sext i32 %19 to i64
  %25 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %8, i64 0, i64 %24
  %26 = getelementptr inbounds %"struct.HDR_Encode::a", ptr %25, i32 0, i32 0
  store float %18, ptr %26, align 8, !tbaa !15
  br label %27

27:                                               ; preds = %13
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %7, align 4, !tbaa !9
  br label %9, !llvm.loop !16

30:                                               ; preds = %9
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  call void @qsort(ptr noundef %8, i64 noundef %32, i64 noundef 8, ptr noundef @_ZN10HDR_Encode9a_compareEPKvS1_)
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %47, %30
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = icmp slt i32 %34, %35
  br i1 %36, label %37, label %50

37:                                               ; preds = %33
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %8, i64 0, i64 %39
  %41 = getelementptr inbounds %"struct.HDR_Encode::a", ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !13
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %43, i64 %45
  store i32 %42, ptr %46, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %37
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %7, align 4, !tbaa !9
  br label %33, !llvm.loop !18

50:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 1544, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret void
}

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10HDR_Encode9a_compareEPKvS1_(ptr noundef, ptr noundef) #3

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 = { "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 #3 = { inlinehint 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 #4 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!14, !10, i64 4}
!14 = !{!"_ZTSN10HDR_Encode1aE", !12, i64 0, !10, i64 4}
!15 = !{!14, !12, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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

%"struct.HDR_Encode::a" = type { float, i32 }

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nofree uwtable
define dso_local void @_ZN10HDR_Encode14sortProjectionEPfPii(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = alloca [193 x %"struct.HDR_Encode::a"], align 16
  call void @llvm.lifetime.start.p0(i64 1544, ptr nonnull %4) #4
  %5 = icmp sgt i32 %2, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %3
  %6 = sext i32 %2 to i64
  call void @qsort(ptr noundef nonnull %4, i64 noundef %6, i64 noundef 8, ptr noundef nonnull @_ZN10HDR_Encode9a_compareEPKvS1_)
  br label %._crit_edge18

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 4
  br i1 %min.iters.check, label %.lr.ph.preheader41, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483644
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <2 x i32> [ <i32 0, i32 1>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <2 x i32> %vec.ind, <i32 2, i32 2>
  %7 = or disjoint i64 %index, 2
  %8 = getelementptr inbounds float, ptr %0, i64 %index
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %wide.load = load <2 x float>, ptr %8, align 4, !tbaa !5
  %wide.load26 = load <2 x float>, ptr %9, align 4, !tbaa !5
  %10 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %4, i64 0, i64 %index
  %11 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %4, i64 0, i64 %7
  %12 = bitcast <2 x i32> %vec.ind to <2 x float>
  %interleaved.vec = shufflevector <2 x float> %wide.load, <2 x float> %12, <4 x i32> <i32 0, i32 2, i32 1, i32 3>
  store <4 x float> %interleaved.vec, ptr %10, align 16, !tbaa !9
  %13 = bitcast <2 x i32> %step.add to <2 x float>
  %interleaved.vec27 = shufflevector <2 x float> %wide.load26, <2 x float> %13, <4 x i32> <i32 0, i32 2, i32 1, i32 3>
  store <4 x float> %interleaved.vec27, ptr %11, align 16, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %vec.ind.next = add <2 x i32> %vec.ind, <i32 4, i32 4>
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !10

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader41

.lr.ph.preheader41:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader41, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader41 ]
  %15 = getelementptr inbounds float, ptr %0, i64 %indvars.iv
  %16 = load float, ptr %15, align 4, !tbaa !5
  %17 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %4, i64 0, i64 %indvars.iv
  %18 = getelementptr inbounds i8, ptr %17, i64 4
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %19, ptr %18, align 4, !tbaa !14
  store float %16, ptr %17, align 8, !tbaa !17
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %.lr.ph, %middle.block
  %20 = zext nneg i32 %2 to i64
  call void @qsort(ptr noundef nonnull %4, i64 noundef %20, i64 noundef 8, ptr noundef nonnull @_ZN10HDR_Encode9a_compareEPKvS1_)
  br i1 %5, label %.lr.ph17.preheader, label %._crit_edge18

.lr.ph17.preheader:                               ; preds = %._crit_edge
  %wide.trip.count23 = zext nneg i32 %2 to i64
  %min.iters.check30 = icmp ult i32 %2, 9
  br i1 %min.iters.check30, label %.lr.ph17.preheader40, label %vector.ph31

vector.ph31:                                      ; preds = %.lr.ph17.preheader
  %n.mod.vf32 = and i64 %wide.trip.count, 7
  %21 = icmp eq i64 %n.mod.vf32, 0
  %22 = select i1 %21, i64 8, i64 %n.mod.vf32
  %n.vec33 = sub nsw i64 %wide.trip.count, %22
  br label %vector.body35

vector.body35:                                    ; preds = %vector.body35, %vector.ph31
  %index36 = phi i64 [ 0, %vector.ph31 ], [ %index.next39, %vector.body35 ]
  %23 = or disjoint i64 %index36, 4
  %24 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %4, i64 0, i64 %index36, i32 1
  %25 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %4, i64 0, i64 %23, i32 1
  %wide.vec = load <8 x i32>, ptr %24, align 4, !tbaa !14
  %wide.vec37 = load <8 x i32>, ptr %25, align 4, !tbaa !14
  %strided.vec = shufflevector <8 x i32> %wide.vec, <8 x i32> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %strided.vec38 = shufflevector <8 x i32> %wide.vec37, <8 x i32> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %26 = getelementptr inbounds i32, ptr %1, i64 %index36
  %27 = getelementptr inbounds i8, ptr %26, i64 16
  store <4 x i32> %strided.vec, ptr %26, align 4, !tbaa !19
  store <4 x i32> %strided.vec38, ptr %27, align 4, !tbaa !19
  %index.next39 = add nuw i64 %index36, 8
  %28 = icmp eq i64 %index.next39, %n.vec33
  br i1 %28, label %.lr.ph17.preheader40, label %vector.body35, !llvm.loop !20

.lr.ph17.preheader40:                             ; preds = %vector.body35, %.lr.ph17.preheader
  %indvars.iv20.ph = phi i64 [ 0, %.lr.ph17.preheader ], [ %n.vec33, %vector.body35 ]
  br label %.lr.ph17

.lr.ph17:                                         ; preds = %.lr.ph17.preheader40, %.lr.ph17
  %indvars.iv20 = phi i64 [ %indvars.iv.next21, %.lr.ph17 ], [ %indvars.iv20.ph, %.lr.ph17.preheader40 ]
  %29 = getelementptr inbounds [193 x %"struct.HDR_Encode::a"], ptr %4, i64 0, i64 %indvars.iv20, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !14
  %31 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv20
  store i32 %30, ptr %31, align 4, !tbaa !19
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, 1
  %exitcond24.not = icmp eq i64 %indvars.iv.next21, %wide.trip.count23
  br i1 %exitcond24.not, label %._crit_edge18, label %.lr.ph17, !llvm.loop !21

._crit_edge18:                                    ; preds = %.lr.ph17, %._crit_edge.thread, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 1544, ptr nonnull %4) #4
  ret void
}

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10HDR_Encode9a_compareEPKvS1_(ptr noundef, ptr noundef) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree 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 = { nofree "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 #3 = { inlinehint 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 #4 = { 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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11, !12, !13}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = !{!15, !16, i64 4}
!15 = !{!"_ZTSN10HDR_Encode1aE", !6, i64 0, !16, i64 4}
!16 = !{!"int", !7, i64 0}
!17 = !{!15, !6, i64 0}
!18 = distinct !{!18, !11, !13, !12}
!19 = !{!16, !16, i64 0}
!20 = distinct !{!20, !11, !12, !13}
!21 = distinct !{!21, !11, !13, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass