; 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.8 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [20 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_bytes_generation(ptr dead_on_unwind noalias writable sret(%struct.test_result_t) align 8 %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca [1024 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %8 = call i32 @qrng_init(ptr noundef %2, ptr noundef @.str, i64 noundef 23)
  call void @llvm.lifetime.start.p0(i64 1024, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds [1024 x i8], ptr %3, i64 0, i64 0
  %11 = call i32 @qrng_bytes(ptr noundef %9, ptr noundef %10, i64 noundef 1024)
  store i32 %11, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %12

12:                                               ; preds = %26, %1
  %13 = load i64, ptr %6, align 8, !tbaa !11
  %14 = icmp ult i64 %13, 1024
  br i1 %14, label %16, label %15

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

16:                                               ; preds = %12
  %17 = load i64, ptr %6, align 8, !tbaa !11
  %18 = getelementptr inbounds [1024 x i8], ptr %3, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !13
  %20 = zext i8 %19 to i32
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %25

22:                                               ; preds = %16
  %23 = load i64, ptr %5, align 8, !tbaa !11
  %24 = add i64 %23, 1
  store i64 %24, ptr %5, align 8, !tbaa !11
  br label %25

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

26:                                               ; preds = %25
  %27 = load i64, ptr %6, align 8, !tbaa !11
  %28 = add i64 %27, 1
  store i64 %28, ptr %6, align 8, !tbaa !11
  br label %12, !llvm.loop !14

29:                                               ; preds = %15
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %30)
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %37

33:                                               ; preds = %29
  %34 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.8, ptr %34, align 8, !tbaa !16
  %35 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 0, ptr %35, align 8, !tbaa !18
  %36 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.9, ptr %36, align 8, !tbaa !19
  store i32 1, ptr %7, align 4
  br label %48

37:                                               ; preds = %29
  %38 = load i64, ptr %5, align 8, !tbaa !11
  %39 = icmp ugt i64 %38, 256
  br i1 %39, label %40, label %44

40:                                               ; preds = %37
  %41 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.8, ptr %41, align 8, !tbaa !16
  %42 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 0, ptr %42, align 8, !tbaa !18
  %43 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.10, ptr %43, align 8, !tbaa !19
  store i32 1, ptr %7, align 4
  br label %48

44:                                               ; preds = %37
  %45 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.8, ptr %45, align 8, !tbaa !16
  %46 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 1, ptr %46, align 8, !tbaa !18
  %47 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.3, ptr %47, align 8, !tbaa !19
  store i32 1, ptr %7, align 4
  br label %48

48:                                               ; preds = %44, %40, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 1024, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  ret void
}

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

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 = { 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", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !6, i64 0}
!17 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16}
!18 = !{!17, !10, i64 8}
!19 = !{!17, !6, i64 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"

%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.8 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [20 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_bytes_generation(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.test_result_t) align 8 %0) local_unnamed_addr #2 {
vector.ph:
  %1 = alloca ptr, align 8
  %2 = alloca [1024 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  %3 = call i32 @qrng_init(ptr noundef nonnull %1, ptr noundef nonnull @.str, i64 noundef 23) #4
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %2) #4
  %4 = load ptr, ptr %1, align 8, !tbaa !5
  %5 = call i32 @qrng_bytes(ptr noundef %4, ptr noundef nonnull %2, i64 noundef 1024) #4
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next.1, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %20, %vector.body ]
  %vec.phi9 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %21, %vector.body ]
  %6 = getelementptr inbounds [1024 x i8], ptr %2, i64 0, i64 %index
  %7 = getelementptr inbounds i8, ptr %6, i64 2
  %wide.load = load <2 x i8>, ptr %6, align 8, !tbaa !9
  %wide.load10 = load <2 x i8>, ptr %7, align 2, !tbaa !9
  %8 = icmp eq <2 x i8> %wide.load, zeroinitializer
  %9 = icmp eq <2 x i8> %wide.load10, zeroinitializer
  %10 = zext <2 x i1> %8 to <2 x i64>
  %11 = zext <2 x i1> %9 to <2 x i64>
  %12 = add <2 x i64> %vec.phi, %10
  %13 = add <2 x i64> %vec.phi9, %11
  %index.next = or disjoint i64 %index, 4
  %14 = getelementptr inbounds [1024 x i8], ptr %2, i64 0, i64 %index.next
  %15 = getelementptr inbounds i8, ptr %14, i64 2
  %wide.load.1 = load <2 x i8>, ptr %14, align 4, !tbaa !9
  %wide.load10.1 = load <2 x i8>, ptr %15, align 2, !tbaa !9
  %16 = icmp eq <2 x i8> %wide.load.1, zeroinitializer
  %17 = icmp eq <2 x i8> %wide.load10.1, zeroinitializer
  %18 = zext <2 x i1> %16 to <2 x i64>
  %19 = zext <2 x i1> %17 to <2 x i64>
  %20 = add <2 x i64> %12, %18
  %21 = add <2 x i64> %13, %19
  %index.next.1 = add nuw nsw i64 %index, 8
  %22 = icmp eq i64 %index.next.1, 1024
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !10

middle.block:                                     ; preds = %vector.body
  %23 = load ptr, ptr %1, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %23) #4
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %26, label %24

24:                                               ; preds = %middle.block
  store ptr @.str.8, ptr %0, align 8, !tbaa !14
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 0, ptr %25, align 8, !tbaa !17
  br label %32

26:                                               ; preds = %middle.block
  %bin.rdx = add <2 x i64> %21, %20
  %27 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %28 = icmp ugt i64 %27, 256
  store ptr @.str.8, ptr %0, align 8, !tbaa !14
  %29 = getelementptr inbounds i8, ptr %0, i64 8
  br i1 %28, label %30, label %31

30:                                               ; preds = %26
  store i32 0, ptr %29, align 8, !tbaa !17
  br label %32

31:                                               ; preds = %26
  store i32 1, ptr %29, align 8, !tbaa !17
  br label %32

32:                                               ; preds = %31, %30, %24
  %.str.3.sink = phi ptr [ @.str.3, %31 ], [ @.str.10, %30 ], [ @.str.9, %24 ]
  %33 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %.str.3.sink, ptr %33, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #4
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #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 = { 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11, !12, !13}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !16, i64 8, !6, i64 16}
!16 = !{!"int", !7, i64 0}
!17 = !{!15, !16, i64 8}
!18 = !{!15, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass