; LLVM IR from berry-lang/berry :: src (be_list)

; 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.blist = type { ptr, i8, i8, ptr, i32, i32, ptr }
%struct.bvalue = type { %union.bvaldata, i32 }
%union.bvaldata = type { double }

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

declare ptr @be_realloc(ptr noundef, ptr noundef, 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare i32 @be_nextsize(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @be_list_insert(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %14 = load i32, ptr %8, align 4, !tbaa !9
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %16, label %22

16:                                               ; preds = %4
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.blist, ptr %17, i32 0, i32 4
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = add nsw i32 %19, %20
  store i32 %21, ptr %8, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %16, %4
  %23 = load i32, ptr %8, align 4, !tbaa !9
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %31, label %25

25:                                               ; preds = %22
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.blist, ptr %27, i32 0, i32 4
  %29 = load i32, ptr %28, align 8, !tbaa !11
  %30 = icmp sgt i32 %26, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %25, %22
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %102

32:                                               ; preds = %25
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.blist, ptr %33, i32 0, i32 4
  %35 = load i32, ptr %34, align 8, !tbaa !11
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.blist, ptr %36, i32 0, i32 5
  %38 = load i32, ptr %37, align 4, !tbaa !13
  %39 = icmp sge i32 %35, %38
  br i1 %39, label %40, label %63

40:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.blist, ptr %41, i32 0, i32 5
  %43 = load i32, ptr %42, align 4, !tbaa !13
  %44 = call i32 @be_nextsize(i32 noundef %43)
  store i32 %44, ptr %13, align 4, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.blist, ptr %46, i32 0, i32 6
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.blist, ptr %49, i32 0, i32 5
  %51 = load i32, ptr %50, align 4, !tbaa !13
  %52 = sext i32 %51 to i64
  %53 = mul i64 %52, 16
  %54 = load i32, ptr %13, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = mul i64 %55, 16
  %57 = call ptr @be_realloc(ptr noundef %45, ptr noundef %48, i64 noundef %53, i64 noundef %56)
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.blist, ptr %58, i32 0, i32 6
  store ptr %57, ptr %59, align 8, !tbaa !14
  %60 = load i32, ptr %13, align 4, !tbaa !9
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.blist, ptr %61, i32 0, i32 5
  store i32 %60, ptr %62, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %63

63:                                               ; preds = %40, %32
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.blist, ptr %64, i32 0, i32 6
  %66 = load ptr, ptr %65, align 8, !tbaa !14
  store ptr %66, ptr %11, align 8, !tbaa !5
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.blist, ptr %67, i32 0, i32 4
  %69 = load i32, ptr %68, align 8, !tbaa !11
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %68, align 8, !tbaa !11
  store i32 %69, ptr %10, align 4, !tbaa !9
  br label %71

71:                                               ; preds = %85, %63
  %72 = load i32, ptr %10, align 4, !tbaa !9
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = icmp sgt i32 %72, %73
  br i1 %74, label %75, label %88

75:                                               ; preds = %71
  %76 = load ptr, ptr %11, align 8, !tbaa !5
  %77 = load i32, ptr %10, align 4, !tbaa !9
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds %struct.bvalue, ptr %76, i64 %78
  %80 = load ptr, ptr %11, align 8, !tbaa !5
  %81 = load i32, ptr %10, align 4, !tbaa !9
  %82 = sub nsw i32 %81, 1
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds %struct.bvalue, ptr %80, i64 %83
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %79, ptr align 8 %84, i64 16, i1 false), !tbaa.struct !15
  br label %85

85:                                               ; preds = %75
  %86 = load i32, ptr %10, align 4, !tbaa !9
  %87 = add nsw i32 %86, -1
  store i32 %87, ptr %10, align 4, !tbaa !9
  br label %71, !llvm.loop !17

88:                                               ; preds = %71
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.blist, ptr %89, i32 0, i32 6
  %91 = load ptr, ptr %90, align 8, !tbaa !14
  %92 = load i32, ptr %8, align 4, !tbaa !9
  %93 = sext i32 %92 to i64
  %94 = getelementptr inbounds %struct.bvalue, ptr %91, i64 %93
  store ptr %94, ptr %11, align 8, !tbaa !5
  %95 = load ptr, ptr %9, align 8, !tbaa !5
  %96 = icmp ne ptr %95, null
  br i1 %96, label %97, label %100

97:                                               ; preds = %88
  %98 = load ptr, ptr %11, align 8, !tbaa !5
  %99 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %98, ptr align 8 %99, i64 16, i1 false), !tbaa.struct !15
  br label %100

100:                                              ; preds = %97, %88
  %101 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %101, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %102

102:                                              ; preds = %100, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %103 = load ptr, ptr %5, align 8
  ret ptr %103
}

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

!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 24}
!12 = !{!"blist", !6, i64 0, !7, i64 8, !7, i64 9, !6, i64 16, !10, i64 24, !10, i64 28, !6, i64 32}
!13 = !{!12, !10, i64 28}
!14 = !{!12, !6, i64 32}
!15 = !{i64 0, i64 8, !16, i64 8, i64 4, !9}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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.bvalue = type { %union.bvaldata, i32 }
%union.bvaldata = type { double }

declare ptr @be_realloc(ptr noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

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

declare i32 @be_nextsize(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @be_list_insert(ptr noundef %0, ptr nocapture noundef %1, i32 noundef %2, ptr noundef readonly %3) local_unnamed_addr #2 {
  %5 = icmp slt i32 %2, 0
  %6 = getelementptr inbounds i8, ptr %1, i64 24
  %7 = load i32, ptr %6, align 8, !tbaa !5
  br i1 %5, label %8, label %.thread

8:                                                ; preds = %4
  %9 = add nsw i32 %7, %2
  %10 = icmp slt i32 %9, 0
  br i1 %10, label %45, label %.thread

.thread:                                          ; preds = %4, %8
  %.03239 = phi i32 [ %9, %8 ], [ %2, %4 ]
  %11 = getelementptr inbounds i8, ptr %1, i64 24
  %12 = icmp sgt i32 %.03239, %7
  br i1 %12, label %45, label %13

13:                                               ; preds = %.thread
  %14 = getelementptr inbounds i8, ptr %1, i64 28
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %.not = icmp slt i32 %7, %15
  br i1 %.not, label %._crit_edge43, label %16

._crit_edge43:                                    ; preds = %13
  %.phi.trans.insert44 = getelementptr inbounds i8, ptr %1, i64 32
  %.pre45 = load ptr, ptr %.phi.trans.insert44, align 8, !tbaa !12
  br label %26

16:                                               ; preds = %13
  %17 = tail call i32 @be_nextsize(i32 noundef %15) #3
  %18 = getelementptr inbounds i8, ptr %1, i64 32
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = load i32, ptr %14, align 4, !tbaa !11
  %21 = sext i32 %20 to i64
  %22 = shl nsw i64 %21, 4
  %23 = sext i32 %17 to i64
  %24 = shl nsw i64 %23, 4
  %25 = tail call ptr @be_realloc(ptr noundef %0, ptr noundef %19, i64 noundef %22, i64 noundef %24) #3
  store ptr %25, ptr %18, align 8, !tbaa !12
  store i32 %17, ptr %14, align 4, !tbaa !11
  %.pre46 = load i32, ptr %11, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %._crit_edge43, %16
  %27 = phi i32 [ %7, %._crit_edge43 ], [ %.pre46, %16 ]
  %28 = phi ptr [ %.pre45, %._crit_edge43 ], [ %25, %16 ]
  %29 = getelementptr inbounds i8, ptr %1, i64 32
  %30 = add nsw i32 %27, 1
  store i32 %30, ptr %11, align 8, !tbaa !5
  %invariant.gep = getelementptr i8, ptr %28, i64 -16
  %31 = icmp sgt i32 %27, %.03239
  br i1 %31, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %26
  %32 = zext i32 %27 to i64
  %33 = zext nneg i32 %.03239 to i64
  %34 = sub nsw i64 %32, %33
  %xtraiter = and i64 %34, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader
  %35 = getelementptr inbounds %struct.bvalue, ptr %28, i64 %32
  %gep.prol = getelementptr %struct.bvalue, ptr %invariant.gep, i64 %32
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %35, ptr noundef nonnull align 8 dereferenceable(16) %gep.prol, i64 16, i1 false), !tbaa.struct !13
  %indvars.iv.next.prol = add nsw i64 %32, -1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %32, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %36 = add nsw i64 %32, -1
  %37 = icmp eq i64 %36, %33
  br i1 %37, label %._crit_edge.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %38 = getelementptr inbounds %struct.bvalue, ptr %28, i64 %indvars.iv
  %gep = getelementptr %struct.bvalue, ptr %invariant.gep, i64 %indvars.iv
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %38, ptr noundef nonnull align 8 dereferenceable(16) %gep, i64 16, i1 false), !tbaa.struct !13
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %39 = getelementptr inbounds %struct.bvalue, ptr %28, i64 %indvars.iv.next
  %gep.1 = getelementptr %struct.bvalue, ptr %invariant.gep, i64 %indvars.iv.next
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %39, ptr noundef nonnull align 8 dereferenceable(16) %gep.1, i64 16, i1 false), !tbaa.struct !13
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %40 = icmp sgt i64 %indvars.iv.next.1, %33
  br i1 %40, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %.lr.ph, %.lr.ph.prol.loopexit
  %.pre47 = load ptr, ptr %29, align 8, !tbaa !12
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %26
  %41 = phi ptr [ %.pre47, %._crit_edge.loopexit ], [ %28, %26 ]
  %42 = zext nneg i32 %.03239 to i64
  %43 = getelementptr inbounds %struct.bvalue, ptr %41, i64 %42
  %.not37 = icmp eq ptr %3, null
  br i1 %.not37, label %45, label %44

44:                                               ; preds = %._crit_edge
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %43, ptr noundef nonnull align 8 dereferenceable(16) %3, i64 16, i1 false), !tbaa.struct !13
  br label %45

45:                                               ; preds = %._crit_edge, %44, %8, %.thread
  %.0 = phi ptr [ null, %.thread ], [ null, %8 ], [ %43, %44 ], [ %43, %._crit_edge ]
  ret ptr %.0
}

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


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