; 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_data = type { i64, ptr }
%struct.sc_heap = type { i64, 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 ptr @sc_heap_pop(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca %struct.sc_heap_data, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 2, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.sc_heap, ptr %8, i32 0, i32 1
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %98

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.sc_heap, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = getelementptr inbounds %struct.sc_heap_data, ptr %16, i64 0
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.sc_heap, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = getelementptr inbounds %struct.sc_heap_data, ptr %20, i64 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %17, ptr align 8 %21, i64 16, i1 false), !tbaa.struct !14
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.sc_heap, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.sc_heap, ptr %25, i32 0, i32 1
  %27 = load i64, ptr %26, align 8, !tbaa !11
  %28 = add i64 %27, -1
  store i64 %28, ptr %26, align 8, !tbaa !11
  %29 = getelementptr inbounds %struct.sc_heap_data, ptr %24, i64 %27
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %6, ptr align 8 %29, i64 16, i1 false), !tbaa.struct !14
  br label %30

30:                                               ; preds = %74, %13
  %31 = load i64, ptr %5, align 8, !tbaa !9
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.sc_heap, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = icmp ule i64 %31, %34
  br i1 %35, label %36, label %88

36:                                               ; preds = %30
  %37 = load i64, ptr %5, align 8, !tbaa !9
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.sc_heap, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = icmp ult i64 %37, %40
  br i1 %41, label %42, label %62

42:                                               ; preds = %36
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.sc_heap, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !13
  %46 = load i64, ptr %5, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.sc_heap_data, ptr %45, i64 %46
  %48 = getelementptr inbounds %struct.sc_heap_data, ptr %47, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !15
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.sc_heap, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !13
  %53 = load i64, ptr %5, align 8, !tbaa !9
  %54 = add i64 %53, 1
  %55 = getelementptr inbounds %struct.sc_heap_data, ptr %52, i64 %54
  %56 = getelementptr inbounds %struct.sc_heap_data, ptr %55, i32 0, i32 0
  %57 = load i64, ptr %56, align 8, !tbaa !15
  %58 = icmp sgt i64 %49, %57
  br i1 %58, label %59, label %62

59:                                               ; preds = %42
  %60 = load i64, ptr %5, align 8, !tbaa !9
  %61 = add i64 %60, 1
  store i64 %61, ptr %5, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %59, %42, %36
  %63 = getelementptr inbounds %struct.sc_heap_data, ptr %6, i32 0, i32 0
  %64 = load i64, ptr %63, align 8, !tbaa !15
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.sc_heap, ptr %65, i32 0, i32 2
  %67 = load ptr, ptr %66, align 8, !tbaa !13
  %68 = load i64, ptr %5, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.sc_heap_data, ptr %67, i64 %68
  %70 = getelementptr inbounds %struct.sc_heap_data, ptr %69, i32 0, i32 0
  %71 = load i64, ptr %70, align 8, !tbaa !15
  %72 = icmp sle i64 %64, %71
  br i1 %72, label %73, label %74

73:                                               ; preds = %62
  br label %88

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

88:                                               ; preds = %73, %30
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.sc_heap, ptr %89, i32 0, i32 2
  %91 = load ptr, ptr %90, align 8, !tbaa !13
  %92 = load i64, ptr %4, align 8, !tbaa !9
  %93 = getelementptr inbounds %struct.sc_heap_data, ptr %91, i64 %92
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %93, ptr align 8 %6, i64 16, i1 false), !tbaa.struct !14
  %94 = load ptr, ptr %3, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.sc_heap, ptr %94, i32 0, i32 2
  %96 = load ptr, ptr %95, align 8, !tbaa !13
  %97 = getelementptr inbounds %struct.sc_heap_data, ptr %96, i64 0
  store ptr %97, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %98

98:                                               ; preds = %88, %12
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %99 = load ptr, ptr %2, align 8
  ret ptr %99
}

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 }

!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, !6, i64 16}
!14 = !{i64 0, i64 8, !9, i64 8, i64 8, !5}
!15 = !{!16, !10, i64 0}
!16 = !{!"sc_heap_data", !10, i64 0, !6, i64 8}
!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.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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @sc_heap_pop(ptr nocapture noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %4 = icmp eq i64 %3, 0
  br i1 %4, label %32, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %7, ptr noundef nonnull align 8 dereferenceable(16) %8, i64 16, i1 false), !tbaa.struct !12
  %9 = load ptr, ptr %6, align 8, !tbaa !11
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = add i64 %10, -1
  store i64 %11, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.sc_heap_data, ptr %9, i64 %10
  %.sroa.0.0.copyload = load i64, ptr %12, align 8, !tbaa !13
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %12, i64 8
  %.sroa.5.0.copyload = load ptr, ptr %.sroa.5.0..sroa_idx, align 8, !tbaa !14
  %.not32 = icmp ult i64 %11, 2
  br i1 %.not32, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5, %25
  %13 = phi i64 [ %28, %25 ], [ %11, %5 ]
  %.02834 = phi i64 [ %.1, %25 ], [ 1, %5 ]
  %.02933 = phi i64 [ %27, %25 ], [ 2, %5 ]
  %14 = icmp ult i64 %.02933, %13
  %.pre = load ptr, ptr %6, align 8, !tbaa !11
  br i1 %14, label %15, label %22

15:                                               ; preds = %.lr.ph
  %16 = getelementptr inbounds %struct.sc_heap_data, ptr %.pre, i64 %.02933
  %17 = load i64, ptr %16, align 8, !tbaa !15
  %18 = or disjoint i64 %.02933, 1
  %19 = getelementptr inbounds %struct.sc_heap_data, ptr %.pre, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !15
  %21 = icmp sgt i64 %17, %20
  %spec.select = select i1 %21, i64 %18, i64 %.02933
  br label %22

22:                                               ; preds = %15, %.lr.ph
  %.1 = phi i64 [ %.02933, %.lr.ph ], [ %spec.select, %15 ]
  %23 = getelementptr inbounds %struct.sc_heap_data, ptr %.pre, i64 %.1
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %.not31 = icmp sgt i64 %.sroa.0.0.copyload, %24
  br i1 %.not31, label %25, label %._crit_edge

25:                                               ; preds = %22
  %26 = getelementptr inbounds %struct.sc_heap_data, ptr %.pre, i64 %.02834
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %26, ptr noundef nonnull align 8 dereferenceable(16) %23, i64 16, i1 false), !tbaa.struct !12
  %27 = shl i64 %.1, 1
  %28 = load i64, ptr %2, align 8, !tbaa !5
  %.not = icmp ugt i64 %27, %28
  br i1 %.not, label %.._crit_edge.loopexit_crit_edge, label %.lr.ph, !llvm.loop !17

.._crit_edge.loopexit_crit_edge:                  ; preds = %25
  %.pre37.pre = load ptr, ptr %6, align 8, !tbaa !11
  br label %._crit_edge

._crit_edge:                                      ; preds = %22, %.._crit_edge.loopexit_crit_edge, %5
  %29 = phi ptr [ %9, %5 ], [ %.pre37.pre, %.._crit_edge.loopexit_crit_edge ], [ %.pre, %22 ]
  %.028.lcssa = phi i64 [ 1, %5 ], [ %.1, %.._crit_edge.loopexit_crit_edge ], [ %.02834, %22 ]
  %30 = getelementptr inbounds %struct.sc_heap_data, ptr %29, i64 %.028.lcssa
  store i64 %.sroa.0.0.copyload, ptr %30, align 8, !tbaa !13
  %.sroa.5.0..sroa_idx2 = getelementptr inbounds i8, ptr %30, i64 8
  store ptr %.sroa.5.0.copyload, ptr %.sroa.5.0..sroa_idx2, align 8, !tbaa !14
  %31 = load ptr, ptr %6, align 8, !tbaa !11
  br label %32

32:                                               ; preds = %1, %._crit_edge
  %.0 = phi ptr [ %31, %._crit_edge ], [ null, %1 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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" }

!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, !10, i64 16}
!12 = !{i64 0, i64 8, !13, i64 8, i64 8, !14}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !10, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"sc_heap_data", !7, i64 0, !10, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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