; 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.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 allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; 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) #4

; Function Attrs: nounwind uwtable
define dso_local void @buzzdarray_remove(ptr noundef %0, i32 noundef %1) #5 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %6 = load i32, ptr %4, align 4, !tbaa !9
  %7 = zext i32 %6 to i64
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.buzzdarray_s, ptr %8, i32 0, i32 1
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = icmp sge i64 %7, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  br label %105

13:                                               ; preds = %2
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.buzzdarray_s, ptr %14, i32 0, i32 4
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.buzzdarray_s, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.buzzdarray_s, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %23, align 8, !tbaa !16
  %25 = mul i32 %21, %24
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %20, i64 %26
  call void %16(i32 noundef %17, ptr noundef %27, ptr noundef null)
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.buzzdarray_s, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.buzzdarray_s, ptr %32, i32 0, i32 2
  %34 = load i32, ptr %33, align 8, !tbaa !16
  %35 = mul i32 %31, %34
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %30, i64 %36
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.buzzdarray_s, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = add i32 %41, 1
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.buzzdarray_s, ptr %43, i32 0, i32 2
  %45 = load i32, ptr %44, align 8, !tbaa !16
  %46 = mul i32 %42, %45
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %40, i64 %47
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.buzzdarray_s, ptr %49, i32 0, i32 1
  %51 = load i64, ptr %50, align 8, !tbaa !11
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = zext i32 %52 to i64
  %54 = sub nsw i64 %51, %53
  %55 = sub nsw i64 %54, 1
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.buzzdarray_s, ptr %56, i32 0, i32 2
  %58 = load i32, ptr %57, align 8, !tbaa !16
  %59 = zext i32 %58 to i64
  %60 = mul nsw i64 %55, %59
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %37, ptr align 1 %48, i64 %60, i1 false)
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.buzzdarray_s, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !11
  %64 = add nsw i64 %63, -1
  store i64 %64, ptr %62, align 8, !tbaa !11
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.buzzdarray_s, ptr %65, i32 0, i32 1
  %67 = load i64, ptr %66, align 8, !tbaa !11
  %68 = icmp sgt i64 %67, 0
  br i1 %68, label %69, label %105

69:                                               ; preds = %13
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.buzzdarray_s, ptr %70, i32 0, i32 1
  %72 = load i64, ptr %71, align 8, !tbaa !11
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.buzzdarray_s, ptr %73, i32 0, i32 3
  %75 = load i32, ptr %74, align 4, !tbaa !17
  %76 = udiv i32 %75, 2
  %77 = zext i32 %76 to i64
  %78 = icmp sle i64 %72, %77
  br i1 %78, label %79, label %105

79:                                               ; preds = %69
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.buzzdarray_s, ptr %80, i32 0, i32 3
  %82 = load i32, ptr %81, align 4, !tbaa !17
  %83 = udiv i32 %82, 2
  store i32 %83, ptr %81, align 4, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.buzzdarray_s, ptr %84, i32 0, i32 0
  %86 = load ptr, ptr %85, align 8, !tbaa !15
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.buzzdarray_s, ptr %87, i32 0, i32 3
  %89 = load i32, ptr %88, align 4, !tbaa !17
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.buzzdarray_s, ptr %90, i32 0, i32 2
  %92 = load i32, ptr %91, align 8, !tbaa !16
  %93 = mul i32 %89, %92
  %94 = zext i32 %93 to i64
  %95 = call ptr @realloc(ptr noundef %86, i64 noundef %94) #7
  store ptr %95, ptr %5, align 8, !tbaa !5
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = icmp ne ptr %96, null
  br i1 %97, label %101, label %98

98:                                               ; preds = %79
  %99 = load ptr, ptr @stderr, align 8, !tbaa !5
  %100 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %99, ptr noundef @.str.2) #6
  call void @abort() #8
  unreachable

101:                                              ; preds = %79
  %102 = load ptr, ptr %5, align 8, !tbaa !5
  %103 = load ptr, ptr %3, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.buzzdarray_s, ptr %103, i32 0, i32 0
  store ptr %102, ptr %104, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %105

105:                                              ; preds = %101, %69, %13, %12
  ret void
}

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 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(1) }
attributes #8 = { noreturn 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, !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, !6, i64 24}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !10, i64 16}
!17 = !{!12, !10, i64 20}


; 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.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: 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 #2

; 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) #3

; Function Attrs: nounwind uwtable
define dso_local void @buzzdarray_remove(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #4 {
  %3 = zext i32 %1 to i64
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %.not = icmp sgt i64 %5, %3
  br i1 %.not, label %6, label %47

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = load ptr, ptr %0, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = load i32, ptr %10, align 8, !tbaa !14
  %12 = mul i32 %11, %1
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %9, i64 %13
  tail call void %8(i32 noundef %1, ptr noundef %14, ptr noundef null) #5
  %15 = load ptr, ptr %0, align 8, !tbaa !13
  %16 = load i32, ptr %10, align 8, !tbaa !14
  %17 = mul i32 %16, %1
  %18 = zext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %15, i64 %18
  %20 = add i32 %1, 1
  %21 = mul i32 %16, %20
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %15, i64 %22
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = xor i64 %3, -1
  %26 = add i64 %24, %25
  %27 = zext i32 %16 to i64
  %28 = mul nsw i64 %26, %27
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %19, ptr align 1 %23, i64 %28, i1 false)
  %29 = load i64, ptr %4, align 8, !tbaa !5
  %30 = add nsw i64 %29, -1
  store i64 %30, ptr %4, align 8, !tbaa !5
  %31 = icmp sgt i64 %29, 1
  br i1 %31, label %32, label %47

32:                                               ; preds = %6
  %33 = getelementptr inbounds i8, ptr %0, i64 20
  %34 = load i32, ptr %33, align 4, !tbaa !15
  %35 = lshr i32 %34, 1
  %36 = zext nneg i32 %35 to i64
  %.not28 = icmp ugt i64 %30, %36
  br i1 %.not28, label %47, label %37

37:                                               ; preds = %32
  store i32 %35, ptr %33, align 4, !tbaa !15
  %38 = load ptr, ptr %0, align 8, !tbaa !13
  %39 = load i32, ptr %10, align 8, !tbaa !14
  %40 = mul i32 %39, %35
  %41 = zext i32 %40 to i64
  %42 = tail call ptr @realloc(ptr noundef %38, i64 noundef %41) #6
  %.not29 = icmp eq ptr %42, null
  br i1 %.not29, label %43, label %46

43:                                               ; preds = %37
  %44 = load ptr, ptr @stderr, align 8, !tbaa !16
  %45 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %44, ptr noundef nonnull @.str.2) #7
  tail call void @abort() #8
  unreachable

46:                                               ; preds = %37
  store ptr %42, ptr %0, align 8, !tbaa !13
  br label %47

47:                                               ; preds = %2, %46, %32, %6
  ret void
}

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 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(1) }
attributes #7 = { cold nounwind }
attributes #8 = { noreturn 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 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, !7, i64 24}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !11, i64 16}
!15 = !{!6, !11, i64 20}
!16 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass