; 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: inlinehint nounwind uwtable
declare hidden i64 @splitmix64(i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden i64 @get_runtime_entropy(ptr noundef) #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden double @quantum_noise(double noundef) #0

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @hadamard_gate(i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden i64 @measure_state(ptr noundef, double noundef, i64 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @phase_gate(i64 noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @quantum_step(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @qrng_reseed(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
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  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
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %3
  store i32 1, ptr %4, align 4
  br label %153

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

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

20:                                               ; preds = %17
  store i32 2, ptr %4, align 4
  br label %153

21:                                               ; preds = %17, %14
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store i32 3, ptr %4, align 4
  br label %153

25:                                               ; preds = %21
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = call i64 @get_runtime_entropy(ptr noundef %26)
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.qrng_ctx, ptr %28, i32 0, i32 2
  store i64 %27, ptr %29, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.qrng_ctx, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !11
  %33 = xor i64 -7046029254386353131, %32
  store i64 %33, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %139, %25
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = icmp ult i64 %35, %36
  br i1 %37, label %38, label %41

38:                                               ; preds = %34
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = icmp ult i64 %39, 16
  br label %41

41:                                               ; preds = %38, %34
  %42 = phi i1 [ false, %34 ], [ %40, %38 ]
  br i1 %42, label %44, label %43

43:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %142

44:                                               ; preds = %41
  %45 = load i64, ptr %8, align 8, !tbaa !9
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %9, align 8, !tbaa !9
  %48 = getelementptr inbounds i8, ptr %46, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !15
  %50 = zext i8 %49 to i64
  %51 = xor i64 %45, %50
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.qrng_ctx, ptr %52, i32 0, i32 2
  %54 = load i64, ptr %53, align 8, !tbaa !11
  %55 = xor i64 %51, %54
  %56 = call i64 @splitmix64(i64 noundef %55)
  store i64 %56, ptr %8, align 8, !tbaa !9
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.qrng_ctx, ptr %57, i32 0, i32 8
  %59 = load i64, ptr %9, align 8, !tbaa !9
  %60 = getelementptr inbounds [16 x i64], ptr %58, i64 0, i64 %59
  %61 = load i64, ptr %60, align 8, !tbaa !9
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = getelementptr inbounds i8, ptr %62, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !15
  %66 = zext i8 %65 to i64
  %67 = xor i64 %61, %66
  %68 = load i64, ptr %8, align 8, !tbaa !9
  %69 = xor i64 %67, %68
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.qrng_ctx, ptr %70, i32 0, i32 2
  %72 = load i64, ptr %71, align 8, !tbaa !11
  %73 = xor i64 %69, %72
  %74 = call i64 @hadamard_gate(i64 noundef %73)
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.qrng_ctx, ptr %75, i32 0, i32 8
  %77 = load i64, ptr %9, align 8, !tbaa !9
  %78 = getelementptr inbounds [16 x i64], ptr %76, i64 0, i64 %77
  store i64 %74, ptr %78, align 8, !tbaa !9
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.qrng_ctx, ptr %79, i32 0, i32 8
  %81 = load i64, ptr %9, align 8, !tbaa !9
  %82 = getelementptr inbounds [16 x i64], ptr %80, i64 0, i64 %81
  %83 = load i64, ptr %82, align 8, !tbaa !9
  %84 = uitofp i64 %83 to double
  %85 = fdiv double %84, 0x43F0000000000000
  %86 = load ptr, ptr %5, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.qrng_ctx, ptr %86, i32 0, i32 2
  %88 = load i64, ptr %87, align 8, !tbaa !11
  %89 = uitofp i64 %88 to double
  %90 = fdiv double %89, 0x43F0000000000000
  %91 = fadd double %85, %90
  %92 = call double @quantum_noise(double noundef %91)
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.qrng_ctx, ptr %93, i32 0, i32 7
  %95 = load i64, ptr %9, align 8, !tbaa !9
  %96 = getelementptr inbounds [16 x double], ptr %94, i64 0, i64 %95
  store double %92, ptr %96, align 8, !tbaa !16
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.qrng_ctx, ptr %98, i32 0, i32 7
  %100 = load i64, ptr %9, align 8, !tbaa !9
  %101 = getelementptr inbounds [16 x double], ptr %99, i64 0, i64 %100
  %102 = load double, ptr %101, align 8, !tbaa !16
  %103 = load ptr, ptr %6, align 8, !tbaa !5
  %104 = load i64, ptr %7, align 8, !tbaa !9
  %105 = sub i64 %104, 1
  %106 = load i64, ptr %9, align 8, !tbaa !9
  %107 = sub i64 %105, %106
  %108 = getelementptr inbounds i8, ptr %103, i64 %107
  %109 = load i8, ptr %108, align 1, !tbaa !15
  %110 = zext i8 %109 to i64
  %111 = load i64, ptr %8, align 8, !tbaa !9
  %112 = xor i64 %110, %111
  %113 = call i64 @measure_state(ptr noundef %97, double noundef %102, i64 noundef %112)
  %114 = load ptr, ptr %5, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.qrng_ctx, ptr %114, i32 0, i32 10
  %116 = load i64, ptr %9, align 8, !tbaa !9
  %117 = getelementptr inbounds [16 x i64], ptr %115, i64 0, i64 %116
  store i64 %113, ptr %117, align 8, !tbaa !9
  %118 = load ptr, ptr %5, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.qrng_ctx, ptr %118, i32 0, i32 10
  %120 = load i64, ptr %9, align 8, !tbaa !9
  %121 = getelementptr inbounds [16 x i64], ptr %119, i64 0, i64 %120
  %122 = load i64, ptr %121, align 8, !tbaa !9
  %123 = load ptr, ptr %6, align 8, !tbaa !5
  %124 = load i64, ptr %9, align 8, !tbaa !9
  %125 = getelementptr inbounds i8, ptr %123, i64 %124
  %126 = load i8, ptr %125, align 1, !tbaa !15
  %127 = zext i8 %126 to i64
  %128 = load i64, ptr %8, align 8, !tbaa !9
  %129 = xor i64 %127, %128
  %130 = load ptr, ptr %5, align 8, !tbaa !5
  %131 = getelementptr inbounds %struct.qrng_ctx, ptr %130, i32 0, i32 2
  %132 = load i64, ptr %131, align 8, !tbaa !11
  %133 = xor i64 %129, %132
  %134 = call i64 @phase_gate(i64 noundef %122, i64 noundef %133)
  %135 = load ptr, ptr %5, align 8, !tbaa !5
  %136 = getelementptr inbounds %struct.qrng_ctx, ptr %135, i32 0, i32 9
  %137 = load i64, ptr %9, align 8, !tbaa !9
  %138 = getelementptr inbounds [16 x i64], ptr %136, i64 0, i64 %137
  store i64 %134, ptr %138, align 8, !tbaa !9
  br label %139

139:                                              ; preds = %44
  %140 = load i64, ptr %9, align 8, !tbaa !9
  %141 = add i64 %140, 1
  store i64 %141, ptr %9, align 8, !tbaa !9
  br label %34, !llvm.loop !18

142:                                              ; preds = %43
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !20
  br label %143

143:                                              ; preds = %149, %142
  %144 = load i32, ptr %10, align 4, !tbaa !20
  %145 = icmp slt i32 %144, 8
  br i1 %145, label %147, label %146

146:                                              ; preds = %143
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %152

147:                                              ; preds = %143
  %148 = load ptr, ptr %5, align 8, !tbaa !5
  call void @quantum_step(ptr noundef %148)
  br label %149

149:                                              ; preds = %147
  %150 = load i32, ptr %10, align 4, !tbaa !20
  %151 = add nsw i32 %150, 1
  store i32 %151, ptr %10, align 4, !tbaa !20
  br label %143, !llvm.loop !21

152:                                              ; preds = %146
  store i32 0, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %153

153:                                              ; preds = %152, %24, %20, %13
  %154 = load i32, ptr %4, align 4
  ret i32 %154
}

attributes #0 = { inlinehint 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 = { 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 nosync 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 16}
!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 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"double", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!14, !14, i64 0}
!21 = distinct !{!21, !19}


; 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: inlinehint nounwind uwtable
declare hidden i64 @splitmix64(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @get_runtime_entropy(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden double @quantum_noise(double noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @hadamard_gate(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @measure_state(ptr noundef, double noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @phase_gate(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @quantum_step(ptr noundef) local_unnamed_addr #1

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

4:                                                ; preds = %3
  %5 = icmp eq ptr %1, null
  %6 = icmp ne i64 %2, 0
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %.loopexit, label %7

7:                                                ; preds = %4
  %8 = icmp eq i64 %2, 0
  br i1 %8, label %.loopexit, label %9

9:                                                ; preds = %7
  %10 = tail call i64 @get_runtime_entropy(ptr noundef nonnull %0)
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %10, ptr %11, align 8, !tbaa !5
  %12 = xor i64 %10, -7046029254386353131
  %invariant.umin = tail call i64 @llvm.umin.i64(i64 %2, i64 16)
  %invariant.gep = getelementptr i8, ptr %1, i64 %2
  %13 = getelementptr inbounds i8, ptr %0, i64 304
  %14 = getelementptr inbounds i8, ptr %0, i64 176
  %15 = getelementptr inbounds i8, ptr %0, i64 560
  %16 = getelementptr inbounds i8, ptr %0, i64 432
  br label %17

17:                                               ; preds = %9, %17
  %.04955 = phi i64 [ 0, %9 ], [ %55, %17 ]
  %.05054 = phi i64 [ %12, %9 ], [ %24, %17 ]
  %18 = getelementptr inbounds i8, ptr %1, i64 %.04955
  %19 = load i8, ptr %18, align 1, !tbaa !12
  %20 = zext i8 %19 to i64
  %21 = xor i64 %.05054, %20
  %22 = load i64, ptr %11, align 8, !tbaa !5
  %23 = xor i64 %21, %22
  %24 = tail call i64 @splitmix64(i64 noundef %23)
  %25 = getelementptr inbounds [16 x i64], ptr %13, i64 0, i64 %.04955
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = load i8, ptr %18, align 1, !tbaa !12
  %28 = zext i8 %27 to i64
  %29 = load i64, ptr %11, align 8, !tbaa !5
  %30 = xor i64 %26, %28
  %31 = xor i64 %30, %29
  %32 = xor i64 %31, %24
  %33 = tail call i64 @hadamard_gate(i64 noundef %32)
  store i64 %33, ptr %25, align 8, !tbaa !13
  %34 = uitofp i64 %33 to double
  %35 = fmul double %34, 0x3BF0000000000000
  %36 = load i64, ptr %11, align 8, !tbaa !5
  %37 = uitofp i64 %36 to double
  %38 = fmul double %37, 0x3BF0000000000000
  %39 = fadd double %35, %38
  %40 = tail call double @quantum_noise(double noundef %39)
  %41 = getelementptr inbounds [16 x double], ptr %14, i64 0, i64 %.04955
  store double %40, ptr %41, align 8, !tbaa !14
  %42 = xor i64 %.04955, -1
  %gep = getelementptr i8, ptr %invariant.gep, i64 %42
  %43 = load i8, ptr %gep, align 1, !tbaa !12
  %44 = zext i8 %43 to i64
  %45 = xor i64 %24, %44
  %46 = tail call i64 @measure_state(ptr noundef nonnull %0, double noundef %40, i64 noundef %45)
  %47 = getelementptr inbounds [16 x i64], ptr %15, i64 0, i64 %.04955
  store i64 %46, ptr %47, align 8, !tbaa !13
  %48 = load i8, ptr %18, align 1, !tbaa !12
  %49 = zext i8 %48 to i64
  %50 = load i64, ptr %11, align 8, !tbaa !5
  %51 = xor i64 %50, %49
  %52 = xor i64 %51, %24
  %53 = tail call i64 @phase_gate(i64 noundef %46, i64 noundef %52)
  %54 = getelementptr inbounds [16 x i64], ptr %16, i64 0, i64 %.04955
  store i64 %53, ptr %54, align 8, !tbaa !13
  %55 = add nuw nsw i64 %.04955, 1
  %exitcond.not = icmp eq i64 %55, %invariant.umin
  br i1 %exitcond.not, label %.preheader.preheader, label %17, !llvm.loop !16

.preheader.preheader:                             ; preds = %17
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  tail call void @quantum_step(ptr noundef nonnull %0)
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.preheader, %7, %4, %3
  %.051 = phi i32 [ 1, %3 ], [ 2, %4 ], [ 3, %7 ], [ 0, %.preheader.preheader ]
  ret i32 %.051
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #2

attributes #0 = { inlinehint 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 = { 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 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 16}
!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 = !{!8, !8, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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