; LLVM IR from attractivechaos/klib :: test (ksort_test)

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

12:                                               ; preds = %66, %2
  %13 = load i64, ptr %7, align 8, !tbaa !5
  %14 = icmp ugt i64 %13, 2
  br i1 %14, label %15, label %27

15:                                               ; preds = %12
  %16 = load i64, ptr %7, align 8, !tbaa !5
  %17 = uitofp i64 %16 to double
  %18 = fdiv double %17, 0x3FF3F5114C5FC35D
  %19 = fptoui double %18 to i64
  store i64 %19, ptr %7, align 8, !tbaa !5
  %20 = load i64, ptr %7, align 8, !tbaa !5
  %21 = icmp eq i64 %20, 9
  br i1 %21, label %25, label %22

22:                                               ; preds = %15
  %23 = load i64, ptr %7, align 8, !tbaa !5
  %24 = icmp eq i64 %23, 10
  br i1 %24, label %25, label %26

25:                                               ; preds = %22, %15
  store i64 11, ptr %7, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %25, %22
  br label %27

27:                                               ; preds = %26, %12
  store i32 0, ptr %6, align 4, !tbaa !13
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %28, ptr %9, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %56, %27
  %30 = load ptr, ptr %9, align 8, !tbaa !9
  %31 = load ptr, ptr %4, align 8, !tbaa !9
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds i32, ptr %31, i64 %32
  %34 = load i64, ptr %7, align 8, !tbaa !5
  %35 = sub i64 0, %34
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  %37 = icmp ult ptr %30, %36
  br i1 %37, label %38, label %59

38:                                               ; preds = %29
  %39 = load ptr, ptr %9, align 8, !tbaa !9
  %40 = load i64, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds i32, ptr %39, i64 %40
  store ptr %41, ptr %10, align 8, !tbaa !9
  %42 = load ptr, ptr %10, align 8, !tbaa !9
  %43 = load i32, ptr %42, align 4, !tbaa !13
  %44 = load ptr, ptr %9, align 8, !tbaa !9
  %45 = load i32, ptr %44, align 4, !tbaa !13
  %46 = icmp slt i32 %43, %45
  br i1 %46, label %47, label %55

47:                                               ; preds = %38
  %48 = load ptr, ptr %9, align 8, !tbaa !9
  %49 = load i32, ptr %48, align 4, !tbaa !13
  store i32 %49, ptr %8, align 4, !tbaa !13
  %50 = load ptr, ptr %10, align 8, !tbaa !9
  %51 = load i32, ptr %50, align 4, !tbaa !13
  %52 = load ptr, ptr %9, align 8, !tbaa !9
  store i32 %51, ptr %52, align 4, !tbaa !13
  %53 = load i32, ptr %8, align 4, !tbaa !13
  %54 = load ptr, ptr %10, align 8, !tbaa !9
  store i32 %53, ptr %54, align 4, !tbaa !13
  store i32 1, ptr %6, align 4, !tbaa !13
  br label %55

55:                                               ; preds = %47, %38
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr %9, align 8, !tbaa !9
  %58 = getelementptr inbounds i32, ptr %57, i32 1
  store ptr %58, ptr %9, align 8, !tbaa !9
  br label %29, !llvm.loop !15

59:                                               ; preds = %29
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %6, align 4, !tbaa !13
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %66, label %63

63:                                               ; preds = %60
  %64 = load i64, ptr %7, align 8, !tbaa !5
  %65 = icmp ugt i64 %64, 2
  br label %66

66:                                               ; preds = %63, %60
  %67 = phi i1 [ true, %60 ], [ %65, %63 ]
  br i1 %67, label %12, label %68, !llvm.loop !17

68:                                               ; preds = %66
  %69 = load i64, ptr %7, align 8, !tbaa !5
  %70 = icmp ne i64 %69, 1
  br i1 %70, label %71, label %76

71:                                               ; preds = %68
  %72 = load ptr, ptr %4, align 8, !tbaa !9
  %73 = load ptr, ptr %4, align 8, !tbaa !9
  %74 = load i64, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds i32, ptr %73, i64 %74
  call void @__ks_insertsort_int(ptr noundef %72, ptr noundef %75)
  br label %76

76:                                               ; preds = %71, %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @__ks_insertsort_int(ptr noundef, ptr noundef) #2

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 = { inlinehint 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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: nounwind uwtable
define dso_local void @ks_combsort_int(i64 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i32, ptr %1, i64 %0
  br label %4

4:                                                ; preds = %._crit_edge, %2
  %.027 = phi i64 [ %0, %2 ], [ %.128, %._crit_edge ]
  %5 = icmp ugt i64 %.027, 2
  br i1 %5, label %6, label %11

6:                                                ; preds = %4
  %7 = uitofp i64 %.027 to double
  %8 = fdiv double %7, 0x3FF3F5114C5FC35D
  %9 = fptoui double %8 to i64
  %10 = add i64 %9, -9
  %or.cond = icmp ult i64 %10, 2
  %spec.store.select = select i1 %or.cond, i64 11, i64 %9
  br label %11

11:                                               ; preds = %6, %4
  %.128 = phi i64 [ %spec.store.select, %6 ], [ %.027, %4 ]
  %12 = sub i64 0, %.128
  %13 = getelementptr inbounds i32, ptr %3, i64 %12
  %14 = icmp ugt ptr %13, %1
  br i1 %14, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %11, %20
  %.032 = phi i32 [ %.1, %20 ], [ 0, %11 ]
  %.02631 = phi ptr [ %21, %20 ], [ %1, %11 ]
  %15 = getelementptr inbounds i32, ptr %.02631, i64 %.128
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = load i32, ptr %.02631, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %.lr.ph
  store i32 %16, ptr %.02631, align 4, !tbaa !5
  store i32 %17, ptr %15, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %.lr.ph, %19
  %.1 = phi i32 [ 1, %19 ], [ %.032, %.lr.ph ]
  %21 = getelementptr inbounds i8, ptr %.02631, i64 4
  %22 = icmp ult ptr %21, %13
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %20, %11
  %.0.lcssa = phi i32 [ 0, %11 ], [ %.1, %20 ]
  %23 = icmp ne i32 %.0.lcssa, 0
  %24 = icmp ugt i64 %.128, 2
  %25 = or i1 %24, %23
  br i1 %25, label %4, label %26, !llvm.loop !11

26:                                               ; preds = %._crit_edge
  %.not = icmp eq i64 %.128, 1
  br i1 %.not, label %28, label %27

27:                                               ; preds = %26
  tail call void @__ks_insertsort_int(ptr noundef %1, ptr noundef %3)
  br label %28

28:                                               ; preds = %27, %26
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @__ks_insertsort_int(ptr noundef, ptr noundef) local_unnamed_addr #1

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

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass