; LLVM IR from tezc/sc :: heap (sc_heap)

; 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.sc_heap = type { i64, i64, ptr }
%struct.sc_heap_data = type { i64, 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; 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 zeroext i1 @sc_heap_add(ptr noundef %0, i64 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.sc_heap, ptr %13, i32 0, i32 1
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = add i64 %15, 1
  store i64 %16, ptr %14, align 8, !tbaa !11
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.sc_heap, ptr %17, i32 0, i32 0
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = icmp uge i64 %16, %19
  br i1 %20, label %21, label %56

21:                                               ; preds = %3
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.sc_heap, ptr %22, i32 0, i32 0
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %31

26:                                               ; preds = %21
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.sc_heap, ptr %27, i32 0, i32 0
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = mul i64 %29, 2
  br label %32

31:                                               ; preds = %21
  br label %32

32:                                               ; preds = %31, %26
  %33 = phi i64 [ %30, %26 ], [ 4, %31 ]
  store i64 %33, ptr %9, align 8, !tbaa !9
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = mul i64 %34, 2
  %36 = mul i64 %35, 16
  store i64 %36, ptr %10, align 8, !tbaa !9
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.sc_heap, ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !13
  %40 = icmp uge i64 %39, 576460752303423487
  br i1 %40, label %48, label %41

41:                                               ; preds = %32
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.sc_heap, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = load i64, ptr %10, align 8, !tbaa !9
  %46 = call ptr @realloc(ptr noundef %44, i64 noundef %45) #5
  store ptr %46, ptr %11, align 8, !tbaa !5
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %41, %32
  store i1 false, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %105

49:                                               ; preds = %41
  %50 = load ptr, ptr %11, align 8, !tbaa !5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.sc_heap, ptr %51, i32 0, i32 2
  store ptr %50, ptr %52, align 8, !tbaa !14
  %53 = load i64, ptr %9, align 8, !tbaa !9
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.sc_heap, ptr %54, i32 0, i32 0
  store i64 %53, ptr %55, align 8, !tbaa !13
  br label %56

56:                                               ; preds = %49, %3
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.sc_heap, ptr %57, i32 0, i32 1
  %59 = load i64, ptr %58, align 8, !tbaa !11
  store i64 %59, ptr %8, align 8, !tbaa !9
  br label %60

60:                                               ; preds = %76, %56
  %61 = load i64, ptr %8, align 8, !tbaa !9
  %62 = icmp ne i64 %61, 1
  br i1 %62, label %63, label %74

63:                                               ; preds = %60
  %64 = load i64, ptr %6, align 8, !tbaa !9
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.sc_heap, ptr %65, i32 0, i32 2
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = load i64, ptr %8, align 8, !tbaa !9
  %69 = udiv i64 %68, 2
  %70 = getelementptr inbounds %struct.sc_heap_data, ptr %67, i64 %69
  %71 = getelementptr inbounds %struct.sc_heap_data, ptr %70, i32 0, i32 0
  %72 = load i64, ptr %71, align 8, !tbaa !15
  %73 = icmp slt i64 %64, %72
  br label %74

74:                                               ; preds = %63, %60
  %75 = phi i1 [ false, %60 ], [ %73, %63 ]
  br i1 %75, label %76, label %90

76:                                               ; preds = %74
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.sc_heap, ptr %77, i32 0, i32 2
  %79 = load ptr, ptr %78, align 8, !tbaa !14
  %80 = load i64, ptr %8, align 8, !tbaa !9
  %81 = getelementptr inbounds %struct.sc_heap_data, ptr %79, i64 %80
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.sc_heap, ptr %82, i32 0, i32 2
  %84 = load ptr, ptr %83, align 8, !tbaa !14
  %85 = load i64, ptr %8, align 8, !tbaa !9
  %86 = udiv i64 %85, 2
  %87 = getelementptr inbounds %struct.sc_heap_data, ptr %84, i64 %86
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %81, ptr align 8 %87, i64 16, i1 false), !tbaa.struct !17
  %88 = load i64, ptr %8, align 8, !tbaa !9
  %89 = udiv i64 %88, 2
  store i64 %89, ptr %8, align 8, !tbaa !9
  br label %60, !llvm.loop !18

90:                                               ; preds = %74
  %91 = load i64, ptr %6, align 8, !tbaa !9
  %92 = load ptr, ptr %5, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.sc_heap, ptr %92, i32 0, i32 2
  %94 = load ptr, ptr %93, align 8, !tbaa !14
  %95 = load i64, ptr %8, align 8, !tbaa !9
  %96 = getelementptr inbounds %struct.sc_heap_data, ptr %94, i64 %95
  %97 = getelementptr inbounds %struct.sc_heap_data, ptr %96, i32 0, i32 0
  store i64 %91, ptr %97, align 8, !tbaa !15
  %98 = load ptr, ptr %7, align 8, !tbaa !5
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.sc_heap, ptr %99, i32 0, i32 2
  %101 = load ptr, ptr %100, align 8, !tbaa !14
  %102 = load i64, ptr %8, align 8, !tbaa !9
  %103 = getelementptr inbounds %struct.sc_heap_data, ptr %101, i64 %102
  %104 = getelementptr inbounds %struct.sc_heap_data, ptr %103, i32 0, i32 1
  store ptr %98, ptr %104, align 8, !tbaa !20
  store i1 true, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %105

105:                                              ; preds = %90, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %106 = load i1, ptr %4, align 1
  ret i1 %106
}

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

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


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

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @sc_heap_add(ptr nocapture noundef %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = add i64 %5, 1
  store i64 %6, ptr %4, align 8, !tbaa !5
  %7 = load i64, ptr %0, align 8, !tbaa !11
  %.not = icmp ult i64 %6, %7
  br i1 %.not, label %18, label %8

8:                                                ; preds = %3
  %.not31 = icmp eq i64 %7, 0
  %9 = shl i64 %7, 1
  %spec.select = select i1 %.not31, i64 4, i64 %9
  %10 = icmp ugt i64 %7, 576460752303423486
  br i1 %10, label %31, label %11

11:                                               ; preds = %8
  %12 = shl i64 %spec.select, 5
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = tail call ptr @realloc(ptr noundef %14, i64 noundef %12) #3
  %16 = icmp eq ptr %15, null
  br i1 %16, label %31, label %17

17:                                               ; preds = %11
  store ptr %15, ptr %13, align 8, !tbaa !12
  store i64 %spec.select, ptr %0, align 8, !tbaa !11
  %.pre = load i64, ptr %4, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %17, %3
  %19 = phi i64 [ %.pre, %17 ], [ %6, %3 ]
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  %.not3233 = icmp eq i64 %19, 1
  br i1 %.not3233, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %18, %26
  %.02834 = phi i64 [ %22, %26 ], [ %19, %18 ]
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = lshr i64 %.02834, 1
  %23 = getelementptr inbounds %struct.sc_heap_data, ptr %21, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = icmp sgt i64 %24, %1
  br i1 %25, label %26, label %.critedge

26:                                               ; preds = %.lr.ph
  %27 = getelementptr inbounds %struct.sc_heap_data, ptr %21, i64 %.02834
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %27, ptr noundef nonnull align 8 dereferenceable(16) %23, i64 16, i1 false), !tbaa.struct !15
  %.not32 = icmp eq i64 %22, 1
  br i1 %.not32, label %.critedge, label %.lr.ph, !llvm.loop !18

.critedge:                                        ; preds = %.lr.ph, %26, %18
  %.028.lcssa = phi i64 [ 1, %18 ], [ 1, %26 ], [ %.02834, %.lr.ph ]
  %28 = load ptr, ptr %20, align 8, !tbaa !12
  %29 = getelementptr inbounds %struct.sc_heap_data, ptr %28, i64 %.028.lcssa
  store i64 %1, ptr %29, align 8, !tbaa !13
  %30 = getelementptr inbounds %struct.sc_heap_data, ptr %28, i64 %.028.lcssa, i32 1
  store ptr %2, ptr %30, align 8, !tbaa !20
  br label %31

31:                                               ; preds = %8, %11, %.critedge
  %.0 = phi i1 [ true, %.critedge ], [ false, %11 ], [ false, %8 ]
  ret i1 %.0
}

; 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

attributes #0 = { mustprogress nocallback nofree 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 = { 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 = { 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, !7, i64 8}
!6 = !{!"sc_heap", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !10, i64 16}
!13 = !{!14, !7, i64 0}
!14 = !{!"sc_heap_data", !7, i64 0, !10, i64 8}
!15 = !{i64 0, i64 8, !16, i64 8, i64 8, !17}
!16 = !{!7, !7, i64 0}
!17 = !{!10, !10, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!14, !10, i64 8}


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