; LLVM IR from intel/cryptography-primitives :: sources/ippcp (pcpprngen_range)

; 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: nounwind uwtable
define dso_local i32 @cpPRNGenRange(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, ptr noundef %5, ptr noundef %6) #0 {
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  %17 = alloca i64, align 8
  %18 = alloca i32, align 4
  %19 = alloca i32, align 4
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  store ptr %0, ptr %9, align 8, !tbaa !5
  store ptr %1, ptr %10, align 8, !tbaa !5
  store i32 %2, ptr %11, align 4, !tbaa !9
  store ptr %3, ptr %12, align 8, !tbaa !5
  store i32 %4, ptr %13, align 4, !tbaa !9
  store ptr %5, ptr %14, align 8, !tbaa !5
  store ptr %6, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %22 = load i32, ptr %13, align 4, !tbaa !9
  %23 = mul nsw i32 %22, 64
  %24 = load ptr, ptr %12, align 8, !tbaa !5
  %25 = load i32, ptr %13, align 4, !tbaa !9
  %26 = sub nsw i32 %25, 1
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i64, ptr %24, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !11
  %30 = call i32 @cpNLZ_BNU(i64 noundef %29)
  %31 = sub nsw i32 %23, %30
  store i32 %31, ptr %16, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  %32 = load i32, ptr %16, align 4, !tbaa !9
  %33 = and i32 %32, 63
  %34 = sub nsw i32 64, %33
  %35 = and i32 %34, 63
  %36 = zext i32 %35 to i64
  %37 = lshr i64 -1, %36
  store i64 %37, ptr %17, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #4
  store i32 0, ptr %18, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %80, %7
  %39 = load i32, ptr %18, align 4, !tbaa !9
  %40 = icmp slt i32 %39, 1000
  br i1 %40, label %41, label %83

41:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #4
  %42 = load ptr, ptr %14, align 8, !tbaa !5
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = load i32, ptr %16, align 4, !tbaa !9
  %45 = load ptr, ptr %15, align 8, !tbaa !5
  %46 = call i32 %42(ptr noundef %43, i32 noundef %44, ptr noundef %45)
  store i32 %46, ptr %20, align 4, !tbaa !9
  %47 = load i32, ptr %20, align 4, !tbaa !9
  %48 = icmp ne i32 0, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %41
  store i32 -1, ptr %8, align 4
  store i32 1, ptr %21, align 4
  br label %77

50:                                               ; preds = %41
  %51 = load i64, ptr %17, align 8, !tbaa !11
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load i32, ptr %13, align 4, !tbaa !9
  %54 = sub nsw i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i64, ptr %52, i64 %55
  %57 = load i64, ptr %56, align 8, !tbaa !11
  %58 = and i64 %57, %51
  store i64 %58, ptr %56, align 8, !tbaa !11
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = load i32, ptr %13, align 4, !tbaa !9
  %61 = call i32 @cpFix_BNU(ptr noundef %59, i32 noundef %60)
  store i32 %61, ptr %19, align 4, !tbaa !9
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = load i32, ptr %19, align 4, !tbaa !9
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  %65 = load i32, ptr %11, align 4, !tbaa !9
  %66 = call i32 @cpCmp_BNU(ptr noundef %62, i32 noundef %63, ptr noundef %64, i32 noundef %65)
  %67 = icmp slt i32 0, %66
  br i1 %67, label %68, label %76

68:                                               ; preds = %50
  %69 = load ptr, ptr %12, align 8, !tbaa !5
  %70 = load i32, ptr %13, align 4, !tbaa !9
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = load i32, ptr %19, align 4, !tbaa !9
  %73 = call i32 @cpCmp_BNU(ptr noundef %69, i32 noundef %70, ptr noundef %71, i32 noundef %72)
  %74 = icmp slt i32 0, %73
  br i1 %74, label %75, label %76

75:                                               ; preds = %68
  store i32 1, ptr %8, align 4
  store i32 1, ptr %21, align 4
  br label %77

76:                                               ; preds = %68, %50
  store i32 0, ptr %21, align 4
  br label %77

77:                                               ; preds = %76, %75, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #4
  %78 = load i32, ptr %21, align 4
  switch i32 %78, label %84 [
    i32 0, label %79
  ]

79:                                               ; preds = %77
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %18, align 4, !tbaa !9
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %18, align 4, !tbaa !9
  br label %38, !llvm.loop !13

83:                                               ; preds = %38
  store i32 0, ptr %8, align 4
  store i32 1, ptr %21, align 4
  br label %84

84:                                               ; preds = %83, %77
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  %85 = load i32, ptr %8, align 4
  ret i32 %85
}

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

declare i32 @cpNLZ_BNU(i64 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cpFix_BNU(ptr noundef, i32 noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cpCmp_BNU(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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: nounwind uwtable
define dso_local noundef i32 @cpPRNGenRange(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, ptr nocapture noundef readonly %5, ptr noundef %6) local_unnamed_addr #0 {
  %8 = shl nsw i32 %4, 6
  %9 = add nsw i32 %4, -1
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i64, ptr %3, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = tail call i32 @cpNLZ_BNU(i64 noundef %12) #3
  %14 = sub nsw i32 %8, %13
  %15 = sub i32 0, %14
  %16 = and i32 %15, 63
  %17 = zext nneg i32 %16 to i64
  %18 = lshr i64 -1, %17
  %19 = getelementptr inbounds i64, ptr %0, i64 %10
  br label %20

20:                                               ; preds = %7, %31
  %.02629 = phi i32 [ 0, %7 ], [ %32, %31 ]
  %21 = tail call i32 %5(ptr noundef %0, i32 noundef %14, ptr noundef %6) #3
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %22, label %.thread

22:                                               ; preds = %20
  %23 = load i64, ptr %19, align 8, !tbaa !5
  %24 = and i64 %23, %18
  store i64 %24, ptr %19, align 8, !tbaa !5
  %25 = tail call i32 @cpFix_BNU(ptr noundef %0, i32 noundef %4)
  %26 = tail call i32 @cpCmp_BNU(ptr noundef %0, i32 noundef %25, ptr noundef %1, i32 noundef %2)
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %22
  %29 = tail call i32 @cpCmp_BNU(ptr noundef %3, i32 noundef %4, ptr noundef nonnull %0, i32 noundef %25)
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %.thread, label %31

31:                                               ; preds = %28, %22
  %32 = add nuw nsw i32 %.02629, 1
  %exitcond.not = icmp eq i32 %32, 1000
  br i1 %exitcond.not, label %.thread, label %20, !llvm.loop !9

.thread:                                          ; preds = %28, %20, %31
  %.2 = phi i32 [ 0, %31 ], [ 1, %28 ], [ -1, %20 ]
  ret i32 %.2
}

declare i32 @cpNLZ_BNU(i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cpFix_BNU(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cpCmp_BNU(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass