; LLVM IR from alexzheng42/sDMD :: src (CBT)

; 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.EventListStr = type { i32, ptr, ptr, ptr }
%struct.AtomStr = type { ptr, ptr }
%struct.DynamicStr = type { [4 x i32], [4 x double], [4 x double], %struct.HBStr, %struct.HBNeighborStr, %struct.InteractionEventStr }
%struct.HBStr = type { i32, i32, i8, i32 }
%struct.HBNeighborStr = type { [5 x i32], i32, i32 }
%struct.InteractionEventStr = type { i32, i64, i64, double, double, i32, i32 }

@atomnum = external global i32, align 4
@CBT = external global %struct.EventListStr, align 8
@atom = external global ptr, align 8

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

15:                                               ; preds = %33, %0
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = load i32, ptr @atomnum, align 4, !tbaa !5
  %18 = icmp sle i32 %16, %17
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %15
  %21 = load ptr, ptr @atom, align 8, !tbaa !14
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds %struct.AtomStr, ptr %21, i64 %23
  %25 = getelementptr inbounds %struct.AtomStr, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  %27 = getelementptr inbounds %struct.DynamicStr, ptr %26, i32 0, i32 5
  %28 = getelementptr inbounds %struct.InteractionEventStr, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr getelementptr inbounds (%struct.EventListStr, ptr @CBT, i32 0, i32 3), align 8, !tbaa !13
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds ptr, ptr %29, i64 %31
  store ptr %28, ptr %32, align 8, !tbaa !14
  br label %33

33:                                               ; preds = %20
  %34 = load i32, ptr %1, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %1, align 4, !tbaa !5
  br label %15, !llvm.loop !17

36:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %44, %36
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = load i32, ptr @atomnum, align 4, !tbaa !5
  %40 = icmp sle i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %47

42:                                               ; preds = %37
  %43 = load i32, ptr %2, align 4, !tbaa !5
  call void @InsertCBTRun(i32 noundef %43)
  br label %44

44:                                               ; preds = %42
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %2, align 4, !tbaa !5
  br label %37, !llvm.loop !19

47:                                               ; preds = %41
  ret void
}

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nounwind uwtable
declare dso_local void @InsertCBTRun(i32 noundef) #0

attributes #0 = { 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 allocsize(0,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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(0,1) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"EventListStr", !6, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 16}
!13 = !{!10, !11, i64 24}
!14 = !{!11, !11, i64 0}
!15 = !{!16, !11, i64 8}
!16 = !{!"AtomStr", !11, i64 0, !11, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; 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.EventListStr = type { i32, ptr, ptr, ptr }
%struct.AtomStr = type { ptr, ptr }

@atomnum = external local_unnamed_addr global i32, align 4
@CBT = external local_unnamed_addr global %struct.EventListStr, align 8
@atom = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @CreateCBT() local_unnamed_addr #0 {
  %1 = load i32, ptr @atomnum, align 4, !tbaa !5
  %2 = shl nsw i32 %1, 1
  %3 = sext i32 %2 to i64
  %4 = tail call noalias ptr @calloc(i64 noundef %3, i64 noundef 4) #2
  store ptr %4, ptr getelementptr inbounds (i8, ptr @CBT, i64 8), align 8, !tbaa !9
  %5 = add i32 %1, 1
  %6 = sext i32 %5 to i64
  %7 = tail call noalias ptr @calloc(i64 noundef %6, i64 noundef 4) #2
  store ptr %7, ptr getelementptr inbounds (i8, ptr @CBT, i64 16), align 8, !tbaa !12
  %8 = tail call noalias ptr @calloc(i64 noundef %6, i64 noundef 8) #2
  store ptr %8, ptr getelementptr inbounds (i8, ptr @CBT, i64 24), align 8, !tbaa !13
  %.not9 = icmp slt i32 %1, 1
  br i1 %.not9, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %wide.trip.count = zext i32 %5 to i64
  %9 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %9, 1
  %10 = icmp eq i32 %5, 2
  br i1 %10, label %.preheader.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %9, -2
  br label %.lr.ph

.preheader.unr-lcssa:                             ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader.unr-lcssa
  %11 = load ptr, ptr @atom, align 8, !tbaa !14
  %12 = getelementptr inbounds %struct.AtomStr, ptr %11, i64 %indvars.iv.unr, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !15
  %14 = getelementptr inbounds i8, ptr %13, i64 152
  %15 = load ptr, ptr getelementptr inbounds (i8, ptr @CBT, i64 24), align 8, !tbaa !13
  %16 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv.unr
  store ptr %14, ptr %16, align 8, !tbaa !14
  br label %.preheader

.preheader:                                       ; preds = %.preheader.unr-lcssa, %.lr.ph.epil
  br i1 %.not9, label %._crit_edge, label %.lr.ph13

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %17 = load ptr, ptr @atom, align 8, !tbaa !14
  %18 = getelementptr inbounds %struct.AtomStr, ptr %17, i64 %indvars.iv, i32 1
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = getelementptr inbounds i8, ptr %19, i64 152
  %21 = load ptr, ptr getelementptr inbounds (i8, ptr @CBT, i64 24), align 8, !tbaa !13
  %22 = getelementptr inbounds ptr, ptr %21, i64 %indvars.iv
  store ptr %20, ptr %22, align 8, !tbaa !14
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = load ptr, ptr @atom, align 8, !tbaa !14
  %24 = getelementptr inbounds %struct.AtomStr, ptr %23, i64 %indvars.iv.next, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = getelementptr inbounds i8, ptr %25, i64 152
  %27 = load ptr, ptr getelementptr inbounds (i8, ptr @CBT, i64 24), align 8, !tbaa !13
  %28 = getelementptr inbounds ptr, ptr %27, i64 %indvars.iv.next
  store ptr %26, ptr %28, align 8, !tbaa !14
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.preheader.unr-lcssa, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph13, %0, %.preheader
  ret void

.lr.ph13:                                         ; preds = %.preheader, %.lr.ph13
  %.012 = phi i32 [ %29, %.lr.ph13 ], [ 1, %.preheader ]
  tail call void @InsertCBTRun(i32 noundef %.012)
  %29 = add nuw nsw i32 %.012, 1
  %30 = load i32, ptr @atomnum, align 4, !tbaa !5
  %.not8.not = icmp slt i32 %.012, %30
  br i1 %.not8.not, label %.lr.ph13, label %._crit_edge, !llvm.loop !19
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @InsertCBTRun(i32 noundef) local_unnamed_addr #0

attributes #0 = { 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 nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #2 = { nounwind allocsize(0,1) }

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


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