; 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
declare double @drand48() #1

; Function Attrs: nounwind uwtable
define dso_local void @ks_sample_int(i64 noundef %0, i64 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca double, align 8
  %11 = alloca double, align 8
  %12 = alloca i32, align 4
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %13 = load i64, ptr %4, align 8, !tbaa !5
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr %9, align 4, !tbaa !11
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = trunc i64 %15 to i32
  store i32 %16, ptr %7, align 4, !tbaa !11
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %17

17:                                               ; preds = %75, %3
  %18 = load i32, ptr %7, align 4, !tbaa !11
  %19 = icmp sge i32 %18, 0
  br i1 %19, label %20, label %78

20:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store double 1.000000e+00, ptr %10, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %21 = call double @drand48() #3
  store double %21, ptr %11, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  br label %22

22:                                               ; preds = %26, %20
  %23 = load double, ptr %11, align 8, !tbaa !13
  %24 = load double, ptr %10, align 8, !tbaa !13
  %25 = fcmp olt double %23, %24
  br i1 %25, label %26, label %37

26:                                               ; preds = %22
  %27 = load double, ptr %10, align 8, !tbaa !13
  %28 = load i32, ptr %7, align 4, !tbaa !11
  %29 = sitofp i32 %28 to double
  %30 = fmul double %27, %29
  %31 = load i32, ptr %9, align 4, !tbaa !11
  %32 = add nsw i32 %31, -1
  store i32 %32, ptr %9, align 4, !tbaa !11
  %33 = sitofp i32 %31 to double
  %34 = fdiv double %30, %33
  %35 = load double, ptr %10, align 8, !tbaa !13
  %36 = fsub double %35, %34
  store double %36, ptr %10, align 8, !tbaa !13
  br label %22, !llvm.loop !15

37:                                               ; preds = %22
  %38 = load i32, ptr %8, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = load i64, ptr %4, align 8, !tbaa !5
  %41 = load i32, ptr %9, align 4, !tbaa !11
  %42 = sext i32 %41 to i64
  %43 = sub i64 %40, %42
  %44 = sub i64 %43, 1
  %45 = icmp ne i64 %39, %44
  br i1 %45, label %46, label %72

46:                                               ; preds = %37
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !11
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %47, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !11
  store i32 %51, ptr %12, align 4, !tbaa !11
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = load i64, ptr %4, align 8, !tbaa !5
  %54 = load i32, ptr %9, align 4, !tbaa !11
  %55 = sext i32 %54 to i64
  %56 = sub i64 %53, %55
  %57 = sub i64 %56, 1
  %58 = getelementptr inbounds i32, ptr %52, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !11
  %60 = load ptr, ptr %6, align 8, !tbaa !9
  %61 = load i32, ptr %8, align 4, !tbaa !11
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i32, ptr %60, i64 %62
  store i32 %59, ptr %63, align 4, !tbaa !11
  %64 = load i32, ptr %12, align 4, !tbaa !11
  %65 = load ptr, ptr %6, align 8, !tbaa !9
  %66 = load i64, ptr %4, align 8, !tbaa !5
  %67 = load i32, ptr %9, align 4, !tbaa !11
  %68 = sext i32 %67 to i64
  %69 = sub i64 %66, %68
  %70 = sub i64 %69, 1
  %71 = getelementptr inbounds i32, ptr %65, i64 %70
  store i32 %64, ptr %71, align 4, !tbaa !11
  br label %72

72:                                               ; preds = %46, %37
  %73 = load i32, ptr %8, align 4, !tbaa !11
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %75

75:                                               ; preds = %72
  %76 = load i32, ptr %7, align 4, !tbaa !11
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr %7, align 4, !tbaa !11
  br label %17, !llvm.loop !17

78:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !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
declare double @drand48() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @ks_sample_int(i64 noundef %0, i64 noundef %1, ptr nocapture noundef %2) local_unnamed_addr #1 {
  %4 = trunc i64 %1 to i32
  %5 = icmp sgt i32 %4, -1
  br i1 %5, label %.lr.ph35.preheader, label %._crit_edge36

.lr.ph35.preheader:                               ; preds = %3
  %6 = trunc i64 %0 to i32
  %7 = add nuw nsw i64 %1, 1
  %wide.trip.count = and i64 %7, 4294967295
  br label %.lr.ph35

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %26
  %indvars.iv = phi i64 [ 0, %.lr.ph35.preheader ], [ %indvars.iv.next, %26 ]
  %.033 = phi i32 [ %4, %.lr.ph35.preheader ], [ %27, %26 ]
  %.02631 = phi i32 [ %6, %.lr.ph35.preheader ], [ %.1.lcssa, %26 ]
  %8 = tail call double @drand48() #2
  %9 = fcmp olt double %8, 1.000000e+00
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph35
  %10 = uitofp nneg i32 %.033 to double
  br label %11

11:                                               ; preds = %.lr.ph, %11
  %.02530 = phi double [ 1.000000e+00, %.lr.ph ], [ %16, %11 ]
  %.129 = phi i32 [ %.02631, %.lr.ph ], [ %13, %11 ]
  %12 = fmul double %.02530, %10
  %13 = add nsw i32 %.129, -1
  %14 = sitofp i32 %.129 to double
  %15 = fdiv double %12, %14
  %16 = fsub double %.02530, %15
  %17 = fcmp olt double %8, %16
  br i1 %17, label %11, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %11, %.lr.ph35
  %.1.lcssa = phi i32 [ %.02631, %.lr.ph35 ], [ %13, %11 ]
  %18 = xor i32 %.1.lcssa, -1
  %19 = sext i32 %18 to i64
  %20 = add i64 %19, %0
  %.not = icmp eq i64 %20, %indvars.iv
  br i1 %.not, label %26, label %21

21:                                               ; preds = %._crit_edge
  %22 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %23 = load i32, ptr %22, align 4, !tbaa !7
  %24 = getelementptr inbounds i32, ptr %2, i64 %20
  %25 = load i32, ptr %24, align 4, !tbaa !7
  store i32 %25, ptr %22, align 4, !tbaa !7
  store i32 %23, ptr %24, align 4, !tbaa !7
  br label %26

26:                                               ; preds = %21, %._crit_edge
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %27 = add nsw i32 %.033, -1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge36, label %.lr.ph35, !llvm.loop !11

._crit_edge36:                                    ; preds = %26, %3
  ret void
}

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


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