; LLVM IR from tsotchke/quantum_rng :: examples/crypto (quantum_chain)

; 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.QuantumChain = type { ptr, ptr, i64, [32 x i8] }
%struct.QuantumBlock = type { i64, i64, [64 x i8], [64 x i8], [64 x i8], i64, [1024 x i8], ptr }

; 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 i32 @qrng_bytes(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare void @qrng_free(ptr noundef) #1

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @calculate_block_hash(ptr noundef) #4

; 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 i32 @quantum_chain_add_block(ptr noundef %0, ptr noundef %1, i64 noundef %2) #4 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, 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
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %20

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

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

20:                                               ; preds = %17, %14, %3
  store i32 -1, ptr %4, align 4
  br label %83

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %22 = call i32 @qrng_init(ptr noundef %8, ptr noundef null, i64 noundef 0)
  store i32 %22, ptr %9, align 4, !tbaa !11
  %23 = load i32, ptr %9, align 4, !tbaa !11
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %82

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %27 = call noalias ptr @malloc(i64 noundef 1248) #7
  store ptr %27, ptr %11, align 8, !tbaa !5
  %28 = load ptr, ptr %11, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %32, label %30

30:                                               ; preds = %26
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %31)
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %81

32:                                               ; preds = %26
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.QuantumChain, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.QuantumBlock, ptr %35, i32 0, i32 0
  %37 = load i64, ptr %36, align 8, !tbaa !15
  %38 = add i64 %37, 1
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.QuantumBlock, ptr %39, i32 0, i32 0
  store i64 %38, ptr %40, align 8, !tbaa !15
  %41 = call i64 @time(ptr noundef null) #6
  %42 = load ptr, ptr %11, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.QuantumBlock, ptr %42, i32 0, i32 1
  store i64 %41, ptr %43, align 8, !tbaa !17
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.QuantumBlock, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds [64 x i8], ptr %45, i64 0, i64 0
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.QuantumChain, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = getelementptr inbounds %struct.QuantumBlock, ptr %49, i32 0, i32 3
  %51 = getelementptr inbounds [64 x i8], ptr %50, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %46, ptr align 8 %51, i64 64, i1 false)
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.QuantumBlock, ptr %53, i32 0, i32 5
  store i64 %52, ptr %54, align 8, !tbaa !18
  %55 = load ptr, ptr %11, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.QuantumBlock, ptr %55, i32 0, i32 6
  %57 = getelementptr inbounds [1024 x i8], ptr %56, i64 0, i64 0
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %57, ptr align 1 %58, i64 %59, i1 false)
  %60 = load ptr, ptr %11, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.QuantumBlock, ptr %60, i32 0, i32 7
  store ptr null, ptr %61, align 8, !tbaa !19
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.QuantumBlock, ptr %63, i32 0, i32 4
  %65 = getelementptr inbounds [64 x i8], ptr %64, i64 0, i64 0
  %66 = call i32 @qrng_bytes(ptr noundef %62, ptr noundef %65, i64 noundef 64)
  %67 = load ptr, ptr %11, align 8, !tbaa !5
  call void @calculate_block_hash(ptr noundef %67)
  %68 = load ptr, ptr %11, align 8, !tbaa !5
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.QuantumChain, ptr %69, i32 0, i32 1
  %71 = load ptr, ptr %70, align 8, !tbaa !13
  %72 = getelementptr inbounds %struct.QuantumBlock, ptr %71, i32 0, i32 7
  store ptr %68, ptr %72, align 8, !tbaa !19
  %73 = load ptr, ptr %11, align 8, !tbaa !5
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.QuantumChain, ptr %74, i32 0, i32 1
  store ptr %73, ptr %75, align 8, !tbaa !13
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.QuantumChain, ptr %76, i32 0, i32 2
  %78 = load i64, ptr %77, align 8, !tbaa !20
  %79 = add i64 %78, 1
  store i64 %79, ptr %77, align 8, !tbaa !20
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  call void @qrng_free(ptr noundef %80)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %81

81:                                               ; preds = %32, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  br label %82

82:                                               ; preds = %81, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  br label %83

83:                                               ; preds = %82, %20
  %84 = load i32, ptr %4, align 4
  ret i32 %84
}

; 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) #5

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 allocsize(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 "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 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 #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !7, i64 24}
!15 = !{!16, !10, i64 0}
!16 = !{!"QuantumBlock", !10, i64 0, !10, i64 8, !7, i64 16, !7, i64 80, !7, i64 144, !10, i64 208, !7, i64 216, !6, i64 1240}
!17 = !{!16, !10, i64 8}
!18 = !{!16, !10, i64 208}
!19 = !{!16, !6, i64 1240}
!20 = !{!14, !10, 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"

; 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 i32 @qrng_bytes(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

declare void @qrng_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #3

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

; 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 range(i32 -1, 1) i32 @quantum_chain_add_block(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #4 {
  %4 = alloca ptr, align 8
  %5 = icmp eq ptr %0, null
  %6 = icmp eq ptr %1, null
  %or.cond.not29 = or i1 %5, %6
  %7 = icmp ugt i64 %2, 1024
  %or.cond3 = or i1 %or.cond.not29, %7
  br i1 %or.cond3, label %34, label %8

8:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #6
  %9 = call i32 @qrng_init(ptr noundef nonnull %4, ptr noundef null, i64 noundef 0) #6
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %33

10:                                               ; preds = %8
  %11 = call noalias dereferenceable_or_null(1248) ptr @malloc(i64 noundef 1248) #7
  %.not30 = icmp eq ptr %11, null
  br i1 %.not30, label %.sink.split, label %12

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = add i64 %15, 1
  store i64 %16, ptr %11, align 8, !tbaa !11
  %17 = call i64 @time(ptr noundef null) #6
  %18 = getelementptr inbounds i8, ptr %11, i64 8
  store i64 %17, ptr %18, align 8, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %11, i64 16
  %20 = load ptr, ptr %13, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 80
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %19, ptr noundef nonnull align 8 dereferenceable(64) %21, i64 64, i1 false)
  %22 = getelementptr inbounds i8, ptr %11, i64 208
  store i64 %2, ptr %22, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %11, i64 216
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %23, ptr nonnull align 1 %1, i64 %2, i1 false)
  %24 = getelementptr inbounds i8, ptr %11, i64 1240
  store ptr null, ptr %24, align 8, !tbaa !15
  %25 = load ptr, ptr %4, align 8, !tbaa !16
  %26 = getelementptr inbounds i8, ptr %11, i64 144
  %27 = call i32 @qrng_bytes(ptr noundef %25, ptr noundef nonnull %26, i64 noundef 64) #6
  call void @calculate_block_hash(ptr noundef nonnull %11)
  %28 = load ptr, ptr %13, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i64 1240
  store ptr %11, ptr %29, align 8, !tbaa !15
  store ptr %11, ptr %13, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %0, i64 16
  %31 = load i64, ptr %30, align 8, !tbaa !17
  %32 = add i64 %31, 1
  store i64 %32, ptr %30, align 8, !tbaa !17
  br label %.sink.split

.sink.split:                                      ; preds = %10, %12
  %.1.ph = phi i32 [ 0, %12 ], [ -1, %10 ]
  %.sink = load ptr, ptr %4, align 8, !tbaa !16
  call void @qrng_free(ptr noundef %.sink) #6
  br label %33

33:                                               ; preds = %.sink.split, %8
  %.1 = phi i32 [ -1, %8 ], [ %.1.ph, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #6
  br label %34

34:                                               ; preds = %3, %33
  %.2 = phi i32 [ %.1, %33 ], [ -1, %3 ]
  ret i32 %.2
}

; 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) #5

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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 "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 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 #5 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 8}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !8, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"QuantumBlock", !10, i64 0, !10, i64 8, !8, i64 16, !8, i64 80, !8, i64 144, !10, i64 208, !8, i64 216, !7, i64 1240}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 208}
!15 = !{!12, !7, i64 1240}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !10, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass