; LLVM IR from caryll/otfcc :: lib/bk (bkgraph)

; 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.bk_Graph = type { i32, i32, ptr }
%struct.bk_GraphNode = type { i32, i32, i32, i32, ptr }
%struct.__caryll_bkblock = type { i32, i32, i32, i32, i32, i32, ptr }

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @__caryll_allocate_clean(i64 noundef, i64 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: nounwind
declare void @free(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i64 @otfcc_bkblock_size(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i64 @bk_estimateSizeOfGraph(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.bk_Graph, ptr %6, i32 0, i32 0
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = add i32 %8, 1
  %10 = zext i32 %9 to i64
  %11 = mul i64 8, %10
  %12 = call ptr @__caryll_allocate_clean(i64 noundef %11, i64 noundef 370)
  store ptr %12, ptr %3, align 8, !tbaa !5
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds i64, ptr %13, i64 0
  store i64 0, ptr %14, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !14
  br label %15

15:                                               ; preds = %67, %1
  %16 = load i32, ptr %4, align 4, !tbaa !14
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.bk_Graph, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !9
  %20 = icmp ult i32 %16, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %70

22:                                               ; preds = %15
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.bk_Graph, ptr %23, i32 0, i32 2
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = load i32, ptr %4, align 4, !tbaa !14
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds %struct.bk_GraphNode, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.bk_GraphNode, ptr %28, i32 0, i32 4
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %31 = getelementptr inbounds %struct.__caryll_bkblock, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !18
  %33 = icmp eq i32 %32, 2
  br i1 %33, label %34, label %55

34:                                               ; preds = %22
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !14
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds i64, ptr %35, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !12
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.bk_Graph, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = load i32, ptr %4, align 4, !tbaa !14
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds %struct.bk_GraphNode, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.bk_GraphNode, ptr %45, i32 0, i32 4
  %47 = load ptr, ptr %46, align 8, !tbaa !16
  %48 = call i64 @otfcc_bkblock_size(ptr noundef %47)
  %49 = add i64 %39, %48
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = load i32, ptr %4, align 4, !tbaa !14
  %52 = add i32 %51, 1
  %53 = zext i32 %52 to i64
  %54 = getelementptr inbounds i64, ptr %50, i64 %53
  store i64 %49, ptr %54, align 8, !tbaa !12
  br label %66

55:                                               ; preds = %22
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load i32, ptr %4, align 4, !tbaa !14
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds i64, ptr %56, i64 %58
  %60 = load i64, ptr %59, align 8, !tbaa !12
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = load i32, ptr %4, align 4, !tbaa !14
  %63 = add i32 %62, 1
  %64 = zext i32 %63 to i64
  %65 = getelementptr inbounds i64, ptr %61, i64 %64
  store i64 %60, ptr %65, align 8, !tbaa !12
  br label %66

66:                                               ; preds = %55, %34
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %4, align 4, !tbaa !14
  %69 = add i32 %68, 1
  store i32 %69, ptr %4, align 4, !tbaa !14
  br label %15, !llvm.loop !20

70:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load ptr, ptr %2, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.bk_Graph, ptr %72, i32 0, i32 0
  %74 = load i32, ptr %73, align 8, !tbaa !9
  %75 = zext i32 %74 to i64
  %76 = getelementptr inbounds i64, ptr %71, i64 %75
  %77 = load i64, ptr %76, align 8, !tbaa !12
  store i64 %77, ptr %5, align 8, !tbaa !12
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %78) #4
  store ptr null, ptr %3, align 8, !tbaa !5
  %79 = load i64, ptr %5, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i64 %79
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 }

!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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !6, i64 8}
!16 = !{!17, !6, i64 16}
!17 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !6, i64 16}
!18 = !{!19, !11, i64 0}
!19 = !{!"__caryll_bkblock", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !6, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.bk_GraphNode = type { i32, i32, i32, i32, ptr }

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @bk_estimateSizeOfGraph(ptr nocapture noundef readonly %0) local_unnamed_addr #2 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = add i32 %2, 1
  %4 = zext i32 %3 to i64
  %5 = shl nuw nsw i64 %4, 3
  %6 = tail call ptr @__caryll_allocate_clean(i64 noundef %5, i64 noundef 370)
  store i64 0, ptr %6, align 8, !tbaa !11
  %7 = load i32, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  br label %10

._crit_edge.loopexit:                             ; preds = %27
  %.phi.trans.insert = getelementptr inbounds i64, ptr %6, i64 %29
  %.pre24 = load i64, ptr %.phi.trans.insert, align 8, !tbaa !11
  br label %._crit_edge

._crit_edge:                                      ; preds = %1, %._crit_edge.loopexit
  %9 = phi i64 [ %.pre24, %._crit_edge.loopexit ], [ 0, %1 ]
  tail call void @free(ptr noundef nonnull %6) #3
  ret i64 %9

10:                                               ; preds = %.lr.ph, %27
  %11 = phi i32 [ %7, %.lr.ph ], [ %28, %27 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.pre-phi, %27 ]
  %12 = load ptr, ptr %8, align 8, !tbaa !13
  %13 = getelementptr inbounds %struct.bk_GraphNode, ptr %12, i64 %indvars.iv, i32 4
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = load i32, ptr %14, align 8, !tbaa !16
  %16 = icmp eq i32 %15, 2
  %17 = getelementptr inbounds i64, ptr %6, i64 %indvars.iv
  %18 = load i64, ptr %17, align 8, !tbaa !11
  br i1 %16, label %19, label %24

19:                                               ; preds = %10
  %20 = tail call i64 @otfcc_bkblock_size(ptr noundef nonnull %14)
  %21 = add i64 %20, %18
  %22 = add nuw nsw i64 %indvars.iv, 1
  %23 = getelementptr inbounds i64, ptr %6, i64 %22
  store i64 %21, ptr %23, align 8, !tbaa !11
  %.pre = load i32, ptr %0, align 8, !tbaa !5
  br label %27

24:                                               ; preds = %10
  %25 = add nuw nsw i64 %indvars.iv, 1
  %26 = getelementptr inbounds i64, ptr %6, i64 %25
  store i64 %18, ptr %26, align 8, !tbaa !11
  br label %27

27:                                               ; preds = %19, %24
  %indvars.iv.next.pre-phi = phi i64 [ %22, %19 ], [ %25, %24 ]
  %28 = phi i32 [ %.pre, %19 ], [ %11, %24 ]
  %29 = zext i32 %28 to i64
  %30 = icmp ult i64 %indvars.iv.next.pre-phi, %29
  br i1 %30, label %10, label %._crit_edge.loopexit, !llvm.loop !18
}

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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #2 = { 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 }

!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 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!15, !10, i64 16}
!15 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16}
!16 = !{!17, !7, i64 0}
!17 = !{!"__caryll_bkblock", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !10, i64 24}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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