; LLVM IR from buzz-lang/Buzz :: src/buzz (buzzdict)

; 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.buzzdict_s = type { ptr, i32, i32, ptr, ptr, ptr, i32, i32 }
%struct.buzzdarray_s = type { ptr, i64, i32, i32, ptr }
%struct.buzzdict_entry_s = type { ptr, ptr }

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

; 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 void @buzzdict_foreach(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %65, %3
  %11 = load i32, ptr %7, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.buzzdict_s, ptr %12, i32 0, i32 2
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = icmp ult i32 %11, %14
  br i1 %15, label %16, label %68

16:                                               ; preds = %10
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.buzzdict_s, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds ptr, ptr %19, i64 %21
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %64

25:                                               ; preds = %16
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %60, %25
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.buzzdict_s, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds ptr, ptr %31, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.buzzdarray_s, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = icmp slt i64 %28, %37
  br i1 %38, label %39, label %63

39:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.buzzdict_s, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !13
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds ptr, ptr %42, i64 %44
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.buzzdarray_s, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !17
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = zext i32 %49 to i64
  %51 = getelementptr inbounds %struct.buzzdict_entry_s, ptr %48, i64 %50
  store ptr %51, ptr %9, align 8, !tbaa !5
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.buzzdict_entry_s, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !18
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.buzzdict_entry_s, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !20
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  call void %52(ptr noundef %55, ptr noundef %58, ptr noundef %59)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %60

60:                                               ; preds = %39
  %61 = load i32, ptr %8, align 4, !tbaa !9
  %62 = add i32 %61, 1
  store i32 %62, ptr %8, align 4, !tbaa !9
  br label %26, !llvm.loop !21

63:                                               ; preds = %26
  br label %64

64:                                               ; preds = %63, %16
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %7, align 4, !tbaa !9
  %67 = add i32 %66, 1
  store i32 %67, ptr %7, align 4, !tbaa !9
  br label %10, !llvm.loop !23

68:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret void
}

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 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"buzzdict_s", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !10, i64 44}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !16, i64 8}
!15 = !{!"buzzdarray_s", !6, i64 0, !16, i64 8, !10, i64 16, !10, i64 20, !6, i64 24}
!16 = !{!"long", !7, i64 0}
!17 = !{!15, !6, i64 0}
!18 = !{!19, !6, i64 0}
!19 = !{!"buzzdict_entry_s", !6, i64 0, !6, i64 8}
!20 = !{!19, !6, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


; 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.buzzdict_entry_s = type { ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local void @buzzdict_foreach(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not20 = icmp eq i32 %5, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %3
  %.pre = load ptr, ptr %0, align 8, !tbaa !11
  br label %.lr.ph19

.lr.ph19:                                         ; preds = %.lr.ph19.preheader, %.loopexit
  %6 = phi i32 [ %5, %.lr.ph19.preheader ], [ %31, %.loopexit ]
  %7 = phi ptr [ %.pre, %.lr.ph19.preheader ], [ %32, %.loopexit ]
  %8 = phi ptr [ %.pre, %.lr.ph19.preheader ], [ %33, %.loopexit ]
  %indvars.iv = phi i64 [ 0, %.lr.ph19.preheader ], [ %indvars.iv.next, %.loopexit ]
  %9 = getelementptr inbounds ptr, ptr %8, i64 %indvars.iv
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %.lr.ph19
  %11 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = icmp sgt i64 %14, 0
  br i1 %15, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %16 = phi ptr [ %27, %.lr.ph ], [ %12, %.preheader ]
  %17 = phi i64 [ %24, %.lr.ph ], [ 0, %.preheader ]
  %.01517 = phi i32 [ %23, %.lr.ph ], [ 0, %.preheader ]
  %18 = load ptr, ptr %16, align 8, !tbaa !16
  %19 = getelementptr inbounds %struct.buzzdict_entry_s, ptr %18, i64 %17
  %20 = load ptr, ptr %19, align 8, !tbaa !17
  %21 = getelementptr inbounds i8, ptr %19, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !19
  tail call void %1(ptr noundef %20, ptr noundef %22, ptr noundef %2) #1
  %23 = add i32 %.01517, 1
  %24 = zext i32 %23 to i64
  %25 = load ptr, ptr %0, align 8, !tbaa !11
  %26 = getelementptr inbounds ptr, ptr %25, i64 %indvars.iv
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = icmp sgt i64 %29, %24
  br i1 %30, label %.lr.ph, label %.loopexit.loopexit, !llvm.loop !20

.loopexit.loopexit:                               ; preds = %.lr.ph
  %.pre22 = load i32, ptr %4, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %.preheader, %.lr.ph19
  %31 = phi i32 [ %.pre22, %.loopexit.loopexit ], [ %6, %.preheader ], [ %6, %.lr.ph19 ]
  %32 = phi ptr [ %25, %.loopexit.loopexit ], [ %7, %.preheader ], [ %7, %.lr.ph19 ]
  %33 = phi ptr [ %25, %.loopexit.loopexit ], [ %7, %.preheader ], [ %8, %.lr.ph19 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %34 = zext i32 %31 to i64
  %35 = icmp ult i64 %indvars.iv.next, %34
  br i1 %35, label %.lr.ph19, label %._crit_edge, !llvm.loop !22

._crit_edge:                                      ; preds = %.loopexit, %3
  ret void
}

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 }

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


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