; 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 }

@.str.1 = external hidden unnamed_addr constant [3 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

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

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

declare noalias ptr @fopen(ptr noundef, ptr noundef) #2

declare i32 @fclose(ptr noundef) #2

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

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

16:                                               ; preds = %13, %2
  store i32 -1, ptr %3, align 4
  br label %81

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = call noalias ptr @fopen(ptr noundef %18, ptr noundef @.str.1)
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %23, label %22

22:                                               ; preds = %17
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %80

23:                                               ; preds = %17
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.QuantumChain, ptr %24, i32 0, i32 2
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = call i64 @fread(ptr noundef %25, i64 noundef 8, i64 noundef 1, ptr noundef %26)
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.QuantumChain, ptr %28, i32 0, i32 3
  %30 = getelementptr inbounds [32 x i8], ptr %29, i64 0, i64 0
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = call i64 @fread(ptr noundef %30, i64 noundef 1, i64 noundef 32, ptr noundef %31)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store ptr null, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %68, %23
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.QuantumChain, ptr %35, i32 0, i32 2
  %37 = load i64, ptr %36, align 8, !tbaa !11
  %38 = icmp ult i64 %34, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %33
  store i32 2, ptr %7, align 4
  br label %71

40:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %41 = call noalias ptr @malloc(i64 noundef 1248) #5
  store ptr %41, ptr %10, align 8, !tbaa !5
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = icmp ne ptr %42, null
  br i1 %43, label %47, label %44

44:                                               ; preds = %40
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = call i32 @fclose(ptr noundef %45)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %65

47:                                               ; preds = %40
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = call i64 @fread(ptr noundef %48, i64 noundef 1248, i64 noundef 1, ptr noundef %49)
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.QuantumBlock, ptr %51, i32 0, i32 7
  store ptr null, ptr %52, align 8, !tbaa !13
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = icmp ne ptr %53, null
  br i1 %54, label %59, label %55

55:                                               ; preds = %47
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.QuantumChain, ptr %57, i32 0, i32 0
  store ptr %56, ptr %58, align 8, !tbaa !15
  br label %63

59:                                               ; preds = %47
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.QuantumBlock, ptr %61, i32 0, i32 7
  store ptr %60, ptr %62, align 8, !tbaa !13
  br label %63

63:                                               ; preds = %59, %55
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %64, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %7, align 4
  br label %65

65:                                               ; preds = %63, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %66 = load i32, ptr %7, align 4
  switch i32 %66, label %71 [
    i32 0, label %67
  ]

67:                                               ; preds = %65
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %9, align 8, !tbaa !9
  %70 = add i64 %69, 1
  store i64 %70, ptr %9, align 8, !tbaa !9
  br label %33, !llvm.loop !16

71:                                               ; preds = %65, %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %72 = load i32, ptr %7, align 4
  switch i32 %72, label %79 [
    i32 2, label %73
  ]

73:                                               ; preds = %71
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.QuantumChain, ptr %75, i32 0, i32 1
  store ptr %74, ptr %76, align 8, !tbaa !18
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = call i32 @fclose(ptr noundef %77)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %79

79:                                               ; preds = %73, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %80

80:                                               ; preds = %79, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %81

81:                                               ; preds = %80, %16
  %82 = load i32, ptr %3, align 4
  ret i32 %82
}

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { "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 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 }
attributes #5 = { 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, !10, i64 16}
!12 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !7, i64 24}
!13 = !{!14, !6, i64 1240}
!14 = !{!"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}
!15 = !{!12, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !6, i64 8}


; 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"

@.str.1 = external hidden unnamed_addr constant [3 x i8], align 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 #0

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @quantum_chain_import(ptr noundef %0, ptr noundef readonly %1) local_unnamed_addr #2 {
  %3 = icmp ne ptr %0, null
  %4 = icmp ne ptr %1, null
  %or.cond = and i1 %3, %4
  br i1 %or.cond, label %5, label %22

5:                                                ; preds = %2
  %6 = tail call noalias ptr @fopen(ptr noundef nonnull %1, ptr noundef nonnull @.str.1)
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %22, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = tail call i64 @fread(ptr noundef nonnull %8, i64 noundef 8, i64 noundef 1, ptr noundef nonnull %6)
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = tail call i64 @fread(ptr noundef nonnull %10, i64 noundef 1, i64 noundef 32, ptr noundef nonnull %6)
  %12 = load i64, ptr %8, align 8, !tbaa !5
  %.not4147.not = icmp eq i64 %12, 0
  br i1 %.not4147.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %14
  %.02949 = phi i64 [ %18, %14 ], [ 0, %7 ]
  %.03048 = phi ptr [ %13, %14 ], [ null, %7 ]
  %13 = tail call noalias dereferenceable_or_null(1248) ptr @malloc(i64 noundef 1248) #3
  %.not39.not = icmp eq ptr %13, null
  br i1 %.not39.not, label %.sink.split, label %14

14:                                               ; preds = %.lr.ph
  %15 = tail call i64 @fread(ptr noundef nonnull %13, i64 noundef 1248, i64 noundef 1, ptr noundef nonnull %6)
  %16 = getelementptr inbounds i8, ptr %13, i64 1240
  store ptr null, ptr %16, align 8, !tbaa !11
  %.not40 = icmp eq ptr %.03048, null
  %17 = getelementptr inbounds i8, ptr %.03048, i64 1240
  %.sink = select i1 %.not40, ptr %0, ptr %17
  store ptr %13, ptr %.sink, align 8, !tbaa !13
  %18 = add nuw i64 %.02949, 1
  %19 = load i64, ptr %8, align 8, !tbaa !5
  %.not41 = icmp ult i64 %18, %19
  br i1 %.not41, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %14, %7
  %.030.lcssa = phi ptr [ null, %7 ], [ %13, %14 ]
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %.030.lcssa, ptr %20, align 8, !tbaa !16
  br label %.sink.split

.sink.split:                                      ; preds = %.lr.ph, %._crit_edge
  %.5.ph = phi i32 [ 0, %._crit_edge ], [ -1, %.lr.ph ]
  %21 = tail call i32 @fclose(ptr noundef nonnull %6)
  br label %22

22:                                               ; preds = %.sink.split, %5, %2
  %.5 = phi i32 [ -1, %2 ], [ -1, %5 ], [ %.5.ph, %.sink.split ]
  ret i32 %.5
}

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

attributes #0 = { 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 #1 = { nofree 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 #2 = { nofree 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 #3 = { 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, !10, i64 16}
!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 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !7, i64 8}


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