; 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

; Function Attrs: nounwind uwtable
declare hidden void @calculate_block_hash(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: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @quantum_chain_verify(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca [64 x i8], align 16
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %15

10:                                               ; preds = %1
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.QuantumChain, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %16, label %15

15:                                               ; preds = %10, %1
  store i32 0, ptr %2, align 4
  br label %61

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.QuantumChain, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !9
  store ptr %19, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.QuantumChain, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.QuantumBlock, ptr %22, i32 0, i32 7
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  store ptr %24, ptr %5, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %58, %16
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %59

28:                                               ; preds = %25
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.QuantumBlock, ptr %29, i32 0, i32 2
  %31 = getelementptr inbounds [64 x i8], ptr %30, i64 0, i64 0
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.QuantumBlock, ptr %32, i32 0, i32 3
  %34 = getelementptr inbounds [64 x i8], ptr %33, i64 0, i64 0
  %35 = call i32 @memcmp(ptr noundef %31, ptr noundef %34, i64 noundef 64) #5
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %28
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

38:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 64, ptr %7) #4
  %39 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.QuantumBlock, ptr %40, i32 0, i32 3
  %42 = getelementptr inbounds [64 x i8], ptr %41, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %39, ptr align 8 %42, i64 64, i1 false)
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  call void @calculate_block_hash(ptr noundef %43)
  %44 = getelementptr inbounds [64 x i8], ptr %7, i64 0, i64 0
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.QuantumBlock, ptr %45, i32 0, i32 3
  %47 = getelementptr inbounds [64 x i8], ptr %46, i64 0, i64 0
  %48 = call i32 @memcmp(ptr noundef %44, ptr noundef %47, i64 noundef 64) #5
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %38
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %56

51:                                               ; preds = %38
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %52, ptr %4, align 8, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.QuantumBlock, ptr %53, i32 0, i32 7
  %55 = load ptr, ptr %54, align 8, !tbaa !12
  store ptr %55, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %6, align 4
  br label %56

56:                                               ; preds = %51, %50
  call void @llvm.lifetime.end.p0(i64 64, ptr %7) #4
  %57 = load i32, ptr %6, align 4
  switch i32 %57, label %60 [
    i32 0, label %58
  ]

58:                                               ; preds = %56
  br label %25, !llvm.loop !14

59:                                               ; preds = %25
  store i32 1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

60:                                               ; preds = %59, %56, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %61

61:                                               ; preds = %60, %15
  %62 = load i32, ptr %2, align 4
  ret i32 %62
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #3

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 willreturn memory(read) "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 }
attributes #5 = { nounwind willreturn memory(read) }

!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, !6, i64 0}
!10 = !{!"", !6, i64 0, !6, i64 8, !11, i64 16, !7, i64 24}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !6, i64 1240}
!13 = !{!"QuantumBlock", !11, i64 0, !11, i64 8, !7, i64 16, !7, i64 80, !7, i64 144, !11, i64 208, !7, i64 216, !6, i64 1240}
!14 = distinct !{!14, !15}
!15 = !{!"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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare hidden void @calculate_block_hash(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: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @quantum_chain_verify(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = alloca [64 x i8], align 16
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %.not20 = icmp eq ptr %4, null
  br i1 %.not20, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %4, i64 1240
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %.not2127 = icmp eq ptr %7, null
  br i1 %.not2127, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %5, %12
  %.01329 = phi ptr [ %14, %12 ], [ %7, %5 ]
  %.01428 = phi ptr [ %.01329, %12 ], [ %4, %5 ]
  %8 = getelementptr inbounds i8, ptr %.01329, i64 16
  %9 = getelementptr inbounds i8, ptr %.01428, i64 80
  %bcmp = tail call i32 @bcmp(ptr noundef nonnull dereferenceable(64) %8, ptr noundef nonnull dereferenceable(64) %9, i64 64)
  %.not22 = icmp eq i32 %bcmp, 0
  br i1 %.not22, label %10, label %.loopexit

10:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %2) #4
  %11 = getelementptr inbounds i8, ptr %.01329, i64 80
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %2, ptr noundef nonnull align 8 dereferenceable(64) %11, i64 64, i1 false)
  tail call void @calculate_block_hash(ptr noundef nonnull %.01329)
  %bcmp23 = call i32 @bcmp(ptr noundef nonnull dereferenceable(64) %2, ptr noundef nonnull dereferenceable(64) %11, i64 64)
  %.not24 = icmp eq i32 %bcmp23, 0
  br i1 %.not24, label %12, label %.thread

.thread:                                          ; preds = %10
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %2) #4
  br label %.loopexit

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %.01329, i64 1240
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %2) #4
  %.not21 = icmp eq ptr %14, null
  br i1 %.not21, label %.loopexit, label %.lr.ph, !llvm.loop !13

.loopexit:                                        ; preds = %12, %.lr.ph, %5, %.thread, %1, %3
  %.3 = phi i32 [ 0, %3 ], [ 0, %1 ], [ 0, %.thread ], [ 1, %5 ], [ 1, %12 ], [ 0, %.lr.ph ]
  ret i32 %.3
}

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #3

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nofree nounwind willreturn memory(argmem: read) }
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, !7, i64 0}
!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, !7, i64 1240}
!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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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