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

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

@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @abort() #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
define dso_local ptr @buzzdarray_makeslot(ptr noundef %0, i32 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = zext i32 %7 to i64
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.buzzdarray_s, ptr %9, i32 0, i32 1
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = icmp slt i64 %8, %11
  br i1 %12, label %13, label %16

13:                                               ; preds = %2
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = zext i32 %14 to i64
  br label %20

16:                                               ; preds = %2
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.buzzdarray_s, ptr %17, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !11
  br label %20

20:                                               ; preds = %16, %13
  %21 = phi i64 [ %15, %13 ], [ %19, %16 ]
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %5, align 4, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.buzzdarray_s, ptr %23, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = add nsw i64 %25, 1
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.buzzdarray_s, ptr %27, i32 0, i32 3
  %29 = load i32, ptr %28, align 4, !tbaa !14
  %30 = zext i32 %29 to i64
  %31 = icmp sge i64 %26, %30
  br i1 %31, label %32, label %78

32:                                               ; preds = %20
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.buzzdarray_s, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 4, !tbaa !14
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %40

37:                                               ; preds = %32
  %38 = load ptr, ptr @stderr, align 8, !tbaa !5
  %39 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.1) #6
  call void @abort() #7
  unreachable

40:                                               ; preds = %32
  br label %41

41:                                               ; preds = %46, %40
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.buzzdarray_s, ptr %42, i32 0, i32 3
  %44 = load i32, ptr %43, align 4, !tbaa !14
  %45 = mul i32 %44, 2
  store i32 %45, ptr %43, align 4, !tbaa !14
  br label %46

46:                                               ; preds = %41
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.buzzdarray_s, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = add nsw i64 %49, 1
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.buzzdarray_s, ptr %51, i32 0, i32 3
  %53 = load i32, ptr %52, align 4, !tbaa !14
  %54 = zext i32 %53 to i64
  %55 = icmp sge i64 %50, %54
  br i1 %55, label %41, label %56, !llvm.loop !15

56:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.buzzdarray_s, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !17
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.buzzdarray_s, ptr %60, i32 0, i32 3
  %62 = load i32, ptr %61, align 4, !tbaa !14
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.buzzdarray_s, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 8, !tbaa !18
  %66 = mul i32 %62, %65
  %67 = zext i32 %66 to i64
  %68 = call ptr @realloc(ptr noundef %59, i64 noundef %67) #8
  store ptr %68, ptr %6, align 8, !tbaa !5
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = icmp ne ptr %69, null
  br i1 %70, label %74, label %71

71:                                               ; preds = %56
  %72 = load ptr, ptr @stderr, align 8, !tbaa !5
  %73 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %72, ptr noundef @.str.2) #6
  call void @abort() #7
  unreachable

74:                                               ; preds = %56
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.buzzdarray_s, ptr %76, i32 0, i32 0
  store ptr %75, ptr %77, align 8, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  br label %78

78:                                               ; preds = %74, %20
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.buzzdarray_s, ptr %79, i32 0, i32 1
  %81 = load i64, ptr %80, align 8, !tbaa !11
  %82 = icmp eq i64 %81, 0
  br i1 %82, label %123, label %83

83:                                               ; preds = %78
  %84 = load i32, ptr %5, align 4, !tbaa !9
  %85 = zext i32 %84 to i64
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.buzzdarray_s, ptr %86, i32 0, i32 1
  %88 = load i64, ptr %87, align 8, !tbaa !11
  %89 = icmp slt i64 %85, %88
  br i1 %89, label %90, label %123

90:                                               ; preds = %83
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.buzzdarray_s, ptr %91, i32 0, i32 0
  %93 = load ptr, ptr %92, align 8, !tbaa !17
  %94 = load i32, ptr %5, align 4, !tbaa !9
  %95 = add i32 %94, 1
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.buzzdarray_s, ptr %96, i32 0, i32 2
  %98 = load i32, ptr %97, align 8, !tbaa !18
  %99 = mul i32 %95, %98
  %100 = zext i32 %99 to i64
  %101 = getelementptr inbounds i8, ptr %93, i64 %100
  %102 = load ptr, ptr %3, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.buzzdarray_s, ptr %102, i32 0, i32 0
  %104 = load ptr, ptr %103, align 8, !tbaa !17
  %105 = load i32, ptr %5, align 4, !tbaa !9
  %106 = load ptr, ptr %3, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.buzzdarray_s, ptr %106, i32 0, i32 2
  %108 = load i32, ptr %107, align 8, !tbaa !18
  %109 = mul i32 %105, %108
  %110 = zext i32 %109 to i64
  %111 = getelementptr inbounds i8, ptr %104, i64 %110
  %112 = load ptr, ptr %3, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.buzzdarray_s, ptr %112, i32 0, i32 1
  %114 = load i64, ptr %113, align 8, !tbaa !11
  %115 = load i32, ptr %5, align 4, !tbaa !9
  %116 = zext i32 %115 to i64
  %117 = sub nsw i64 %114, %116
  %118 = load ptr, ptr %3, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.buzzdarray_s, ptr %118, i32 0, i32 2
  %120 = load i32, ptr %119, align 8, !tbaa !18
  %121 = zext i32 %120 to i64
  %122 = mul nsw i64 %117, %121
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %101, ptr align 1 %111, i64 %122, i1 false)
  br label %123

123:                                              ; preds = %90, %83, %78
  %124 = load ptr, ptr %3, align 8, !tbaa !5
  %125 = getelementptr inbounds %struct.buzzdarray_s, ptr %124, i32 0, i32 1
  %126 = load i64, ptr %125, align 8, !tbaa !11
  %127 = add nsw i64 %126, 1
  store i64 %127, ptr %125, align 8, !tbaa !11
  %128 = load ptr, ptr %3, align 8, !tbaa !5
  %129 = getelementptr inbounds %struct.buzzdarray_s, ptr %128, i32 0, i32 0
  %130 = load ptr, ptr %129, align 8, !tbaa !17
  %131 = load i32, ptr %4, align 4, !tbaa !9
  %132 = load ptr, ptr %3, align 8, !tbaa !5
  %133 = getelementptr inbounds %struct.buzzdarray_s, ptr %132, i32 0, i32 2
  %134 = load i32, ptr %133, align 8, !tbaa !18
  %135 = mul i32 %131, %134
  %136 = zext i32 %135 to i64
  %137 = getelementptr inbounds i8, ptr %130, i64 %136
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  ret ptr %137
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #5

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


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @abort() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @buzzdarray_makeslot(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = zext i32 %1 to i64
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp sgt i64 %5, %3
  %7 = trunc i64 %5 to i32
  %8 = select i1 %6, i32 %1, i32 %7
  %9 = add nsw i64 %5, 1
  %10 = getelementptr inbounds i8, ptr %0, i64 20
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %12 = zext i32 %11 to i64
  %.not = icmp slt i64 %9, %12
  br i1 %.not, label %32, label %13

13:                                               ; preds = %2
  %14 = icmp eq i32 %11, 0
  br i1 %14, label %15, label %.preheader

15:                                               ; preds = %13
  %16 = load ptr, ptr @stderr, align 8, !tbaa !13
  %17 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %16, ptr noundef nonnull @.str.1) #5
  tail call void @abort() #6
  unreachable

.preheader:                                       ; preds = %13, %.preheader
  %18 = phi i32 [ %19, %.preheader ], [ %11, %13 ]
  %19 = shl i32 %18, 1
  %20 = zext i32 %19 to i64
  %.not35 = icmp ult i64 %9, %20
  br i1 %.not35, label %21, label %.preheader, !llvm.loop !14

21:                                               ; preds = %.preheader
  store i32 %19, ptr %10, align 4, !tbaa !12
  %22 = load ptr, ptr %0, align 8, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %0, i64 16
  %24 = load i32, ptr %23, align 8, !tbaa !17
  %25 = mul i32 %24, %19
  %26 = zext i32 %25 to i64
  %27 = tail call ptr @realloc(ptr noundef %22, i64 noundef %26) #7
  %.not36 = icmp eq ptr %27, null
  br i1 %.not36, label %28, label %31

28:                                               ; preds = %21
  %29 = load ptr, ptr @stderr, align 8, !tbaa !13
  %30 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef nonnull @.str.2) #5
  tail call void @abort() #6
  unreachable

31:                                               ; preds = %21
  store ptr %27, ptr %0, align 8, !tbaa !16
  %.pre = load i64, ptr %4, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %31, %2
  %33 = phi i64 [ %.pre, %31 ], [ %5, %2 ]
  %34 = icmp eq i64 %33, 0
  br i1 %34, label %52, label %35

35:                                               ; preds = %32
  %36 = zext i32 %8 to i64
  %37 = icmp sgt i64 %33, %36
  br i1 %37, label %38, label %52

38:                                               ; preds = %35
  %39 = load ptr, ptr %0, align 8, !tbaa !16
  %40 = add i32 %8, 1
  %41 = getelementptr inbounds i8, ptr %0, i64 16
  %42 = load i32, ptr %41, align 8, !tbaa !17
  %43 = mul i32 %42, %40
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %39, i64 %44
  %46 = mul i32 %42, %8
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %39, i64 %47
  %49 = sub nsw i64 %33, %36
  %50 = zext i32 %42 to i64
  %51 = mul nsw i64 %49, %50
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %45, ptr align 1 %48, i64 %51, i1 false)
  %.pre38 = load i64, ptr %4, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %38, %35, %32
  %53 = phi i64 [ %.pre38, %38 ], [ %33, %35 ], [ 0, %32 ]
  %54 = add nsw i64 %53, 1
  store i64 %54, ptr %4, align 8, !tbaa !5
  %55 = load ptr, ptr %0, align 8, !tbaa !16
  %56 = getelementptr inbounds i8, ptr %0, i64 16
  %57 = load i32, ptr %56, align 8, !tbaa !17
  %58 = mul i32 %57, %1
  %59 = zext i32 %58 to i64
  %60 = getelementptr inbounds i8, ptr %55, i64 %59
  ret ptr %60
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #4

attributes #0 = { 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 #1 = { noreturn 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 = { 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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { cold nounwind }
attributes #6 = { noreturn nounwind }
attributes #7 = { nounwind allocsize(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, !10, i64 8}
!6 = !{!"buzzdarray_s", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 20, !7, 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 = !{!"int", !8, i64 0}
!12 = !{!6, !11, i64 20}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !7, i64 0}
!17 = !{!6, !11, i64 16}


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