; LLVM IR from tsotchke/quantum_rng :: tests (benchmark_suite)

; 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.function_benchmark = type { ptr, double, double, double, double }

@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @tracked_malloc(i64 noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @tracked_free(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local double @get_time_us() #0

; Function Attrs: nounwind uwtable
define dso_local void @benchmark_entanglement(ptr dead_on_unwind noalias writable sret(%struct.function_benchmark) align 8 %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca double, align 8
  %7 = alloca i32, align 4
  %8 = alloca double, align 8
  store ptr %1, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %9 = call ptr @tracked_malloc(i64 noundef 256)
  store ptr %9, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %10 = call ptr @tracked_malloc(i64 noundef 256)
  store ptr %10, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %11 = call double @get_time_us()
  store double %11, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %21, %2
  %13 = load i32, ptr %7, align 4, !tbaa !11
  %14 = icmp slt i32 %13, 1000000
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %24

16:                                               ; preds = %12
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = call i32 @qrng_entangle_states(ptr noundef %17, ptr noundef %18, ptr noundef %19, i64 noundef 256)
  br label %21

21:                                               ; preds = %16
  %22 = load i32, ptr %7, align 4, !tbaa !11
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %7, align 4, !tbaa !11
  br label %12, !llvm.loop !13

24:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %25 = call double @get_time_us()
  %26 = load double, ptr %6, align 8, !tbaa !9
  %27 = fsub double %25, %26
  store double %27, ptr %8, align 8, !tbaa !9
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  call void @tracked_free(ptr noundef %28, i64 noundef 256)
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  call void @tracked_free(ptr noundef %29, i64 noundef 256)
  %30 = getelementptr inbounds %struct.function_benchmark, ptr %0, i32 0, i32 0
  store ptr @.str.4, ptr %30, align 8, !tbaa !15
  %31 = getelementptr inbounds %struct.function_benchmark, ptr %0, i32 0, i32 1
  %32 = load double, ptr %8, align 8, !tbaa !9
  %33 = fdiv double %32, 1.000000e+06
  store double %33, ptr %31, align 8, !tbaa !17
  %34 = getelementptr inbounds %struct.function_benchmark, ptr %0, i32 0, i32 2
  %35 = load double, ptr %8, align 8, !tbaa !9
  %36 = fdiv double %35, 1.000000e+06
  %37 = fdiv double 1.000000e+06, %36
  store double %37, ptr %34, align 8, !tbaa !18
  %38 = getelementptr inbounds %struct.function_benchmark, ptr %0, i32 0, i32 3
  %39 = load double, ptr %8, align 8, !tbaa !9
  %40 = fdiv double %39, 1.000000e+06
  %41 = fdiv double 5.120000e+08, %40
  store double %41, ptr %38, align 8, !tbaa !19
  %42 = getelementptr inbounds %struct.function_benchmark, ptr %0, i32 0, i32 4
  %43 = load double, ptr %8, align 8, !tbaa !9
  %44 = fdiv double %43, 1.000000e+06
  store double %44, ptr %42, align 8, !tbaa !20
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  ret void
}

declare i32 @qrng_entangle_states(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #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 = { 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 = { 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 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!17 = !{!16, !10, i64 8}
!18 = !{!16, !10, i64 16}
!19 = !{!16, !10, i64 24}
!20 = !{!16, !10, i64 32}


; 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.function_benchmark = type { ptr, double, double, double, double }

@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @tracked_malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @tracked_free(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local double @get_time_us() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @benchmark_entanglement(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.function_benchmark) align 8 %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @tracked_malloc(i64 noundef 256)
  %4 = tail call ptr @tracked_malloc(i64 noundef 256)
  %5 = tail call double @get_time_us()
  br label %16

6:                                                ; preds = %16
  %7 = tail call double @get_time_us()
  %8 = fsub double %7, %5
  tail call void @tracked_free(ptr noundef %3, i64 noundef 256)
  tail call void @tracked_free(ptr noundef %4, i64 noundef 256)
  store ptr @.str.4, ptr %0, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = fdiv double %8, 1.000000e+06
  store double %10, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = insertelement <2 x double> poison, double %10, i64 0
  %13 = shufflevector <2 x double> %12, <2 x double> poison, <2 x i32> zeroinitializer
  %14 = fdiv <2 x double> <double 1.000000e+06, double 5.120000e+08>, %13
  store <2 x double> %14, ptr %11, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  store double %10, ptr %15, align 8, !tbaa !13
  ret void

16:                                               ; preds = %2, %16
  %.011 = phi i32 [ 0, %2 ], [ %18, %16 ]
  %17 = tail call i32 @qrng_entangle_states(ptr noundef %1, ptr noundef %3, ptr noundef %4, i64 noundef 256) #2
  %18 = add nuw nsw i32 %.011, 1
  %exitcond.not = icmp eq i32 %18, 1000000
  br i1 %exitcond.not, label %6, label %16, !llvm.loop !14
}

declare i32 @qrng_entangle_states(ptr noundef, ptr noundef, ptr noundef, i64 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 = { "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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"double", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 32}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass