; 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.28 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [31 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @test_state_measurement(ptr dead_on_unwind noalias writable sret(%struct.test_result_t) align 8 %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca [32 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %8 = call i32 @qrng_init(ptr noundef %2, ptr noundef @.str, i64 noundef 23)
  call void @llvm.lifetime.start.p0(i64 32, ptr %3) #4
  %9 = getelementptr inbounds [32 x i8], ptr %3, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %9, i8 -86, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds [32 x i8], ptr %3, i64 0, i64 0
  %12 = call i32 @qrng_measure_state(ptr noundef %10, ptr noundef %11, i64 noundef 32)
  store i32 %12, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %13

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

16:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %30

17:                                               ; preds = %13
  %18 = load i64, ptr %6, align 8, !tbaa !11
  %19 = getelementptr inbounds [32 x i8], ptr %3, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !13
  %21 = zext i8 %20 to i32
  %22 = icmp ne i32 %21, 170
  br i1 %22, label %23, label %26

23:                                               ; preds = %17
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %5, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %23, %17
  br label %27

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

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

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

38:                                               ; preds = %30
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %45

41:                                               ; preds = %38
  %42 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 0
  store ptr @.str.28, ptr %42, align 8, !tbaa !16
  %43 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 1
  store i32 0, ptr %43, align 8, !tbaa !18
  %44 = getelementptr inbounds %struct.test_result_t, ptr %0, i32 0, i32 2
  store ptr @.str.30, ptr %44, align 8, !tbaa !19
  store i32 1, ptr %7, align 4
  br label %49

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

49:                                               ; preds = %45, %41, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  ret void
}

declare i32 @qrng_measure_state(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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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", !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.28 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [31 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @test_state_measurement(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.test_result_t) align 8 %0) local_unnamed_addr #3 {
  %2 = alloca ptr, align 8
  %3 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %4 = call i32 @qrng_init(ptr noundef nonnull %2, ptr noundef nonnull @.str, i64 noundef 23) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %3, i8 -86, i64 32, i1 false)
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = call i32 @qrng_measure_state(ptr noundef %5, ptr noundef nonnull %3, i64 noundef 32) #5
  %7 = load <16 x i8>, ptr %3, align 16, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %3, i64 16
  %9 = load <8 x i8>, ptr %8, align 16, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %3, i64 24
  %11 = load <4 x i8>, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %3, i64 28
  %13 = load <4 x i8>, ptr %12, align 4, !tbaa !9
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %14) #5
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %18, label %15

15:                                               ; preds = %1
  store ptr @.str.28, ptr %0, align 8, !tbaa !10
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 0, ptr %16, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr @.str.29, ptr %17, align 8, !tbaa !14
  br label %46

18:                                               ; preds = %1
  %19 = icmp ne <16 x i8> %7, <i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86>
  %20 = icmp ne <8 x i8> %9, <i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86, i8 -86>
  %21 = icmp ne <4 x i8> %11, <i8 -86, i8 -86, i8 -86, i8 -86>
  %22 = icmp ne <4 x i8> %13, <i8 -86, i8 -86, i8 -86, i8 -86>
  %23 = extractelement <4 x i1> %22, i64 0
  %24 = zext i1 %23 to i32
  %25 = extractelement <4 x i1> %22, i64 1
  %26 = zext i1 %25 to i32
  %27 = extractelement <4 x i1> %22, i64 2
  %28 = zext i1 %27 to i32
  %29 = bitcast <16 x i1> %19 to i16
  %30 = call range(i16 0, 17) i16 @llvm.ctpop.i16(i16 %29)
  %31 = zext nneg i16 %30 to i32
  %32 = bitcast <8 x i1> %20 to i8
  %33 = call range(i8 0, 9) i8 @llvm.ctpop.i8(i8 %32)
  %34 = zext nneg i8 %33 to i32
  %op.rdx = add nuw nsw i32 %31, %34
  %35 = bitcast <4 x i1> %21 to i4
  %36 = call range(i4 0, 5) i4 @llvm.ctpop.i4(i4 %35)
  %37 = zext nneg i4 %36 to i32
  %op.rdx10 = add nuw nsw i32 %op.rdx, %37
  %op.rdx11 = add nuw nsw i32 %op.rdx10, %24
  %op.rdx12 = add nuw nsw i32 %26, %28
  %op.rdx13 = add nuw nsw i32 %op.rdx11, %op.rdx12
  %38 = extractelement <4 x i1> %22, i64 3
  %39 = zext i1 %38 to i32
  %40 = or i32 %op.rdx13, %39
  %41 = icmp eq i32 %40, 0
  store ptr @.str.28, ptr %0, align 8, !tbaa !10
  %42 = getelementptr inbounds i8, ptr %0, i64 8
  %43 = getelementptr inbounds i8, ptr %0, i64 16
  br i1 %41, label %44, label %45

44:                                               ; preds = %18
  store i32 0, ptr %42, align 8, !tbaa !13
  store ptr @.str.30, ptr %43, align 8, !tbaa !14
  br label %46

45:                                               ; preds = %18
  store i32 1, ptr %42, align 8, !tbaa !13
  store ptr @.str.3, ptr %43, align 8, !tbaa !14
  br label %46

46:                                               ; preds = %45, %44, %15
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.ctpop.i16(i16) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.ctpop.i8(i8) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i4 @llvm.ctpop.i4(i4) #4

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = !{!11, !6, i64 0}
!11 = !{!"", !6, i64 0, !12, i64 8, !6, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !12, i64 8}
!14 = !{!11, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalDCEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass