; LLVM IR from tsotchke/quantum_rng :: tests (comprehensive_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"

%struct.test_result_t = type { ptr, i32, ptr }

@.str = external hidden unnamed_addr constant [24 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [25 x i8], align 1

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

declare i32 @qrng_init(ptr noundef, ptr noundef, i64 noundef) #1

declare void @qrng_free(ptr noundef) #1

; 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 @test_double_distribution(ptr dead_on_unwind noalias writable sret(%struct.test_result_t) align 8 %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca double, align 8
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca double, align 8
  %11 = alloca double, align 8
  %12 = alloca double, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %13 = call i32 @qrng_init(ptr noundef %2, ptr noundef @.str, i64 noundef 23)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 1000000, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store double 0.000000e+00, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store double 0.000000e+00, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store double 1.000000e+00, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store double 0.000000e+00, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %54, %1
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 1000000
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i32 2, ptr %9, align 4
  br label %57

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %19 = load ptr, ptr %2, align 8, !tbaa !11
  %20 = call double @qrng_double(ptr noundef %19)
  store double %20, ptr %10, align 8, !tbaa !9
  %21 = load double, ptr %10, align 8, !tbaa !9
  %22 = load double, ptr %4, align 8, !tbaa !9
  %23 = fadd double %22, %21
  store double %23, ptr %4, align 8, !tbaa !9
  %24 = load double, ptr %10, align 8, !tbaa !9
  %25 = load double, ptr %10, align 8, !tbaa !9
  %26 = load double, ptr %5, align 8, !tbaa !9
  %27 = call double @llvm.fmuladd.f64(double %24, double %25, double %26)
  store double %27, ptr %5, align 8, !tbaa !9
  %28 = load double, ptr %10, align 8, !tbaa !9
  %29 = load double, ptr %6, align 8, !tbaa !9
  %30 = fcmp olt double %28, %29
  br i1 %30, label %31, label %33

31:                                               ; preds = %18
  %32 = load double, ptr %10, align 8, !tbaa !9
  store double %32, ptr %6, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %31, %18
  %34 = load double, ptr %10, align 8, !tbaa !9
  %35 = load double, ptr %7, align 8, !tbaa !9
  %36 = fcmp ogt double %34, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %33
  %38 = load double, ptr %10, align 8, !tbaa !9
  store double %38, ptr %7, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %37, %33
  %40 = load double, ptr %10, align 8, !tbaa !9
  %41 = fcmp olt double %40, 0.000000e+00
  br i1 %41, label %45, label %42

42:                                               ; preds = %39
  %43 = load double, ptr %10, align 8, !tbaa !9
  %44 = fcmp oge double %43, 1.000000e+00
  br i1 %44, label %45, label %50

45:                                               ; preds = %42, %39
  %46 = load ptr, ptr %2, align 8, !tbaa !11
  call void @qrng_free(ptr noundef %46)
  %47 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.13, ptr %47, align 8, !tbaa !13
  %48 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 0, ptr %48, align 8, !tbaa !15
  %49 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.14, ptr %49, align 8, !tbaa !16
  store i32 1, ptr %9, align 4
  br label %51

50:                                               ; preds = %42
  store i32 0, ptr %9, align 4
  br label %51

51:                                               ; preds = %50, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %52 = load i32, ptr %9, align 4
  switch i32 %52, label %57 [
    i32 0, label %53
  ]

53:                                               ; preds = %51
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %8, align 4, !tbaa !5
  br label %14, !llvm.loop !17

57:                                               ; preds = %51, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %58 = load i32, ptr %9, align 4
  switch i32 %58, label %87 [
    i32 2, label %59
  ]

59:                                               ; preds = %57
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %60 = load double, ptr %4, align 8, !tbaa !9
  %61 = fdiv double %60, 1.000000e+06
  store double %61, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %62 = load double, ptr %5, align 8, !tbaa !9
  %63 = fdiv double %62, 1.000000e+06
  %64 = load double, ptr %11, align 8, !tbaa !9
  %65 = load double, ptr %11, align 8, !tbaa !9
  %66 = fneg double %64
  %67 = call double @llvm.fmuladd.f64(double %66, double %65, double %63)
  store double %67, ptr %12, align 8, !tbaa !9
  %68 = load ptr, ptr %2, align 8, !tbaa !11
  call void @qrng_free(ptr noundef %68)
  %69 = load double, ptr %11, align 8, !tbaa !9
  %70 = fsub double %69, 5.000000e-01
  %71 = call double @llvm.fabs.f64(double %70)
  %72 = fcmp ogt double %71, 1.000000e-02
  br i1 %72, label %78, label %73

73:                                               ; preds = %59
  %74 = load double, ptr %12, align 8, !tbaa !9
  %75 = fsub double %74, 0x3FB5555555555555
  %76 = call double @llvm.fabs.f64(double %75)
  %77 = fcmp ogt double %76, 1.000000e-02
  br i1 %77, label %78, label %82

78:                                               ; preds = %73, %59
  %79 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.13, ptr %79, align 8, !tbaa !13
  %80 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 0, ptr %80, align 8, !tbaa !15
  %81 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.15, ptr %81, align 8, !tbaa !16
  store i32 1, ptr %9, align 4
  br label %86

82:                                               ; preds = %73
  %83 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.13, ptr %83, align 8, !tbaa !13
  %84 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 1, ptr %84, align 8, !tbaa !15
  %85 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.3, ptr %85, align 8, !tbaa !16
  store i32 1, ptr %9, align 4
  br label %86

86:                                               ; preds = %82, %78
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %87

87:                                               ; preds = %86, %57
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  ret void
}

declare double @qrng_double(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fabs.f64(double) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !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 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0, !6, i64 8, !12, i64 16}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !12, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.test_result_t = type { ptr, i32, ptr }

@.str = external hidden unnamed_addr constant [24 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [25 x i8], align 1

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

declare i32 @qrng_init(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @qrng_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress 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 @test_double_distribution(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.test_result_t) align 8 %0) local_unnamed_addr #2 {
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  %3 = call i32 @qrng_init(ptr noundef nonnull %2, ptr noundef nonnull @.str, i64 noundef 23) #4
  br label %4

4:                                                ; preds = %1, %.critedge
  %.046 = phi double [ 0.000000e+00, %1 ], [ %10, %.critedge ]
  %.02445 = phi double [ 0.000000e+00, %1 ], [ %9, %.critedge ]
  %.03043 = phi i32 [ 0, %1 ], [ %11, %.critedge ]
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = call double @qrng_double(ptr noundef %5) #4
  %7 = fcmp uge double %6, 0.000000e+00
  %8 = fcmp ult double %6, 1.000000e+00
  %or.cond.not = and i1 %7, %8
  br i1 %or.cond.not, label %.critedge, label %12

.critedge:                                        ; preds = %4
  %9 = call double @llvm.fmuladd.f64(double %6, double %6, double %.02445)
  %10 = fadd double %.046, %6
  %11 = add nuw nsw i32 %.03043, 1
  %exitcond = icmp eq i32 %11, 1000000
  br i1 %exitcond, label %14, label %4, !llvm.loop !9

12:                                               ; preds = %4
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %13) #4
  br label %28

14:                                               ; preds = %.critedge
  %15 = fdiv double %10, 1.000000e+06
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %16) #4
  %17 = fadd double %15, -5.000000e-01
  %18 = call double @llvm.fabs.f64(double %17)
  %19 = fcmp ogt double %18, 1.000000e-02
  br i1 %19, label %28, label %20

20:                                               ; preds = %14
  %21 = fneg double %15
  %22 = fdiv double %9, 1.000000e+06
  %23 = call double @llvm.fmuladd.f64(double %21, double %15, double %22)
  %24 = fadd double %23, 0xBFB5555555555555
  %25 = call double @llvm.fabs.f64(double %24)
  %26 = fcmp ogt double %25, 1.000000e-02
  br i1 %26, label %28, label %27

27:                                               ; preds = %20
  br label %28

28:                                               ; preds = %14, %20, %12, %27
  %.sink = phi i32 [ 0, %12 ], [ 1, %27 ], [ 0, %20 ], [ 0, %14 ]
  %.str.14.sink = phi ptr [ @.str.14, %12 ], [ @.str.3, %27 ], [ @.str.15, %20 ], [ @.str.15, %14 ]
  store ptr @.str.13, ptr %0, align 8, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %.sink, ptr %29, align 8, !tbaa !14
  %30 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %.str.14.sink, ptr %30, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  ret void
}

declare double @qrng_double(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fabs.f64(double) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!12, !6, i64 16}


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