; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (quantum_rng)

; 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.qrng_ctx = type { i64, i64, i64, i64, i64, [16 x double], i64, [16 x double], [16 x i64], [16 x i64], [16 x i64], %struct.timeval, i32, %union.anon, i64 }
%struct.timeval = type { i64, i64 }
%union.anon = type { [128 x i64] }

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

; 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
declare hidden void @quantum_step(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @qrng_bytes(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %12, label %11

11:                                               ; preds = %3
  store i32 1, ptr %4, align 4
  br label %63

12:                                               ; preds = %3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  store i32 2, ptr %4, align 4
  br label %63

16:                                               ; preds = %12
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  store i32 3, ptr %4, align 4
  br label %63

20:                                               ; preds = %16
  br label %21

21:                                               ; preds = %41, %20
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = icmp ugt i64 %22, 0
  br i1 %23, label %24, label %62

24:                                               ; preds = %21
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.qrng_ctx, ptr %25, i32 0, i32 14
  %27 = load i64, ptr %26, align 8, !tbaa !11
  %28 = icmp uge i64 %27, 1024
  br i1 %28, label %29, label %31

29:                                               ; preds = %24
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  call void @quantum_step(ptr noundef %30)
  br label %31

31:                                               ; preds = %29, %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.qrng_ctx, ptr %32, i32 0, i32 14
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = sub i64 1024, %34
  store i64 %35, ptr %8, align 8, !tbaa !9
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = icmp ugt i64 %36, %37
  br i1 %38, label %39, label %41

39:                                               ; preds = %31
  %40 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %40, ptr %8, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %39, %31
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.qrng_ctx, ptr %43, i32 0, i32 13
  %45 = getelementptr inbounds [1024 x i8], ptr %44, i64 0, i64 0
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.qrng_ctx, ptr %46, i32 0, i32 14
  %48 = load i64, ptr %47, align 8, !tbaa !11
  %49 = getelementptr inbounds i8, ptr %45, i64 %48
  %50 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %42, ptr align 1 %49, i64 %50, i1 false)
  %51 = load i64, ptr %8, align 8, !tbaa !9
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.qrng_ctx, ptr %52, i32 0, i32 14
  %54 = load i64, ptr %53, align 8, !tbaa !11
  %55 = add i64 %54, %51
  store i64 %55, ptr %53, align 8, !tbaa !11
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds i8, ptr %57, i64 %56
  store ptr %58, ptr %6, align 8, !tbaa !5
  %59 = load i64, ptr %8, align 8, !tbaa !9
  %60 = load i64, ptr %7, align 8, !tbaa !9
  %61 = sub i64 %60, %59
  store i64 %61, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %21, !llvm.loop !15

62:                                               ; preds = %21
  store i32 0, ptr %4, align 4
  br label %63

63:                                               ; preds = %62, %19, %15, %11
  %64 = load i32, ptr %4, align 4
  ret i32 %64
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 1736}
!12 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !10, i64 168, !7, i64 176, !7, i64 304, !7, i64 432, !7, i64 560, !13, i64 688, !14, i64 704, !7, i64 712, !10, i64 1736}
!13 = !{!"timeval", !10, i64 0, !10, i64 8}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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
declare hidden void @quantum_step(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @qrng_bytes(ptr noundef %0, ptr noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %4

4:                                                ; preds = %3
  %.not27 = icmp eq ptr %1, null
  br i1 %.not27, label %.loopexit, label %5

5:                                                ; preds = %4
  %6 = icmp eq i64 %2, 0
  br i1 %6, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %5
  %7 = getelementptr inbounds i8, ptr %0, i64 1736
  %8 = getelementptr inbounds i8, ptr %0, i64 712
  %.pre = load i64, ptr %7, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %.preheader, %13
  %10 = phi i64 [ %.pre, %.preheader ], [ %18, %13 ]
  %.02030 = phi i64 [ %2, %.preheader ], [ %20, %13 ]
  %.02129 = phi ptr [ %1, %.preheader ], [ %19, %13 ]
  %11 = icmp ugt i64 %10, 1023
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  tail call void @quantum_step(ptr noundef nonnull %0)
  %.pre31 = load i64, ptr %7, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %12, %9
  %14 = phi i64 [ %.pre31, %12 ], [ %10, %9 ]
  %15 = sub i64 1024, %14
  %spec.select = tail call i64 @llvm.umin.i64(i64 %15, i64 %.02030)
  %16 = getelementptr inbounds i8, ptr %8, i64 %14
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.02129, ptr nonnull align 1 %16, i64 %spec.select, i1 false)
  %17 = load i64, ptr %7, align 8, !tbaa !5
  %18 = add i64 %17, %spec.select
  store i64 %18, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.02129, i64 %spec.select
  %20 = sub i64 %.02030, %spec.select
  %.not28 = icmp eq i64 %20, 0
  br i1 %.not28, label %.loopexit, label %9, !llvm.loop !12

.loopexit:                                        ; preds = %13, %5, %4, %3
  %.022 = phi i32 [ 1, %3 ], [ 2, %4 ], [ 3, %5 ], [ 0, %13 ]
  ret i32 %.022
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 1736}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !8, i64 40, !7, i64 168, !8, i64 176, !8, i64 304, !8, i64 432, !8, i64 560, !10, i64 688, !11, i64 704, !8, i64 712, !7, i64 1736}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"timeval", !7, i64 0, !7, i64 8}
!11 = !{!"int", !8, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass