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

; 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.data = type { i32, ptr }
%struct.sc_heap = type { i64, i64, ptr }
%struct.sc_heap_data = type { i64, ptr }

@__const.main.n = external hidden unnamed_addr constant [5 x %struct.data], align 16
@.str.5 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [5 x %struct.data], align 16
  %3 = alloca ptr, align 8
  %4 = alloca %struct.sc_heap, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 80, ptr %2) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %2, ptr align 16 @__const.main.n, i64 80, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 24, ptr %4) #4
  %7 = call zeroext i1 @sc_heap_init(ptr noundef %4, i64 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %25, %0
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 5
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %28

12:                                               ; preds = %8
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [5 x %struct.data], ptr %2, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.data, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 16, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [5 x %struct.data], ptr %2, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.data, ptr %21, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !12
  %24 = call zeroext i1 @sc_heap_add(ptr noundef %4, i64 noundef %18, ptr noundef %23)
  br label %25

25:                                               ; preds = %12
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !13

28:                                               ; preds = %11
  br label %29

29:                                               ; preds = %32, %28
  %30 = call ptr @sc_heap_pop(ptr noundef %4)
  store ptr %30, ptr %3, align 8, !tbaa !15
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %41

32:                                               ; preds = %29
  %33 = load ptr, ptr %3, align 8, !tbaa !15
  %34 = getelementptr inbounds %struct.sc_heap_data, ptr %33, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !16
  %36 = trunc i64 %35 to i32
  %37 = load ptr, ptr %3, align 8, !tbaa !15
  %38 = getelementptr inbounds %struct.sc_heap_data, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i32 noundef %36, ptr noundef %39)
  br label %29, !llvm.loop !20

41:                                               ; preds = %29
  %42 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %61, %41
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = icmp slt i32 %44, 5
  br i1 %45, label %47, label %46

46:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %64

47:                                               ; preds = %43
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [5 x %struct.data], ptr %2, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.data, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 16, !tbaa !9
  %53 = sub nsw i32 0, %52
  %54 = sext i32 %53 to i64
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [5 x %struct.data], ptr %2, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.data, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !12
  %60 = call zeroext i1 @sc_heap_add(ptr noundef %4, i64 noundef %54, ptr noundef %59)
  br label %61

61:                                               ; preds = %47
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %6, align 4, !tbaa !5
  br label %43, !llvm.loop !21

64:                                               ; preds = %46
  br label %65

65:                                               ; preds = %68, %64
  %66 = call ptr @sc_heap_pop(ptr noundef %4)
  store ptr %66, ptr %3, align 8, !tbaa !15
  %67 = icmp ne ptr %66, null
  br i1 %67, label %68, label %77

68:                                               ; preds = %65
  %69 = load ptr, ptr %3, align 8, !tbaa !15
  %70 = getelementptr inbounds %struct.sc_heap_data, ptr %69, i32 0, i32 0
  %71 = load i64, ptr %70, align 8, !tbaa !16
  %72 = trunc i64 %71 to i32
  %73 = load ptr, ptr %3, align 8, !tbaa !15
  %74 = getelementptr inbounds %struct.sc_heap_data, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !19
  %76 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i32 noundef %72, ptr noundef %75)
  br label %65, !llvm.loop !22

77:                                               ; preds = %65
  call void @sc_heap_term(ptr noundef %4)
  call void @llvm.lifetime.end.p0(i64 24, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 80, ptr %2) #4
  ret i32 0
}

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

; 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 zeroext i1 @sc_heap_init(ptr noundef, i64 noundef) #3

declare zeroext i1 @sc_heap_add(ptr noundef, i64 noundef, ptr noundef) #3

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

declare ptr @sc_heap_pop(ptr noundef) #3

declare i32 @printf(ptr noundef, ...) #3

declare void @sc_heap_term(ptr noundef) #3

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"data", !6, i64 0, !11, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!11, !11, i64 0}
!16 = !{!17, !18, i64 0}
!17 = !{!"sc_heap_data", !18, i64 0, !11, i64 8}
!18 = !{!"long", !7, i64 0}
!19 = !{!17, !11, i64 8}
!20 = distinct !{!20, !14}
!21 = distinct !{!21, !14}
!22 = distinct !{!22, !14}


; 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.data = type { i32, ptr }
%struct.sc_heap = type { i64, i64, ptr }

@__const.main.n = external hidden unnamed_addr constant [5 x %struct.data], align 16
@.str.5 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
.preheader15:
  %0 = alloca %struct.sc_heap, align 8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %0) #4
  %1 = call zeroext i1 @sc_heap_init(ptr noundef nonnull %0, i64 noundef 0) #4
  %2 = load i32, ptr @__const.main.n, align 16, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 8), align 8, !tbaa !11
  %5 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %3, ptr noundef %4) #4
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 16), align 16, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 24), align 8, !tbaa !11
  %9 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %7, ptr noundef %8) #4
  %10 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 32), align 16, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 40), align 8, !tbaa !11
  %13 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %11, ptr noundef %12) #4
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 48), align 16, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 56), align 8, !tbaa !11
  %17 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %15, ptr noundef %16) #4
  %18 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 64), align 16, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.main.n, i64 72), align 8, !tbaa !11
  %21 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %19, ptr noundef %20) #4
  %22 = call ptr @sc_heap_pop(ptr noundef nonnull %0) #4
  %.not17 = icmp eq ptr %22, null
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader15, %.lr.ph
  %23 = phi ptr [ %29, %.lr.ph ], [ %22, %.preheader15 ]
  %24 = load i64, ptr %23, align 8, !tbaa !12
  %25 = trunc i64 %24 to i32
  %26 = getelementptr inbounds i8, ptr %23, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  %28 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i32 noundef %25, ptr noundef %27)
  %29 = call ptr @sc_heap_pop(ptr noundef nonnull %0) #4
  %.not = icmp eq ptr %29, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph, %.preheader15
  %30 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  %31 = sub nsw i32 0, %2
  %32 = sext i32 %31 to i64
  %33 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %32, ptr noundef %4) #4
  %34 = sub nsw i32 0, %6
  %35 = sext i32 %34 to i64
  %36 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %35, ptr noundef %8) #4
  %37 = sub nsw i32 0, %10
  %38 = sext i32 %37 to i64
  %39 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %38, ptr noundef %12) #4
  %40 = sub nsw i32 0, %14
  %41 = sext i32 %40 to i64
  %42 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %41, ptr noundef %16) #4
  %43 = sub nsw i32 0, %18
  %44 = sext i32 %43 to i64
  %45 = call zeroext i1 @sc_heap_add(ptr noundef nonnull %0, i64 noundef %44, ptr noundef %20) #4
  %46 = call ptr @sc_heap_pop(ptr noundef nonnull %0) #4
  %.not1419 = icmp eq ptr %46, null
  br i1 %.not1419, label %._crit_edge21, label %.lr.ph20

.lr.ph20:                                         ; preds = %._crit_edge, %.lr.ph20
  %47 = phi ptr [ %53, %.lr.ph20 ], [ %46, %._crit_edge ]
  %48 = load i64, ptr %47, align 8, !tbaa !12
  %49 = trunc i64 %48 to i32
  %50 = getelementptr inbounds i8, ptr %47, i64 8
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i32 noundef %49, ptr noundef %51)
  %53 = call ptr @sc_heap_pop(ptr noundef nonnull %0) #4
  %.not14 = icmp eq ptr %53, null
  br i1 %.not14, label %._crit_edge21, label %.lr.ph20, !llvm.loop !18

._crit_edge21:                                    ; preds = %.lr.ph20, %._crit_edge
  call void @sc_heap_term(ptr noundef nonnull %0) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %0) #4
  ret i32 0
}

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

declare zeroext i1 @sc_heap_init(ptr noundef, i64 noundef) local_unnamed_addr #2

declare zeroext i1 @sc_heap_add(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

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

declare ptr @sc_heap_pop(ptr noundef) local_unnamed_addr #2

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

declare void @sc_heap_term(ptr noundef) local_unnamed_addr #2

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


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