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

; 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.example.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 @example() #0 {
  %1 = alloca [5 x %struct.data], align 16
  %2 = alloca ptr, align 8
  %3 = alloca %struct.sc_heap, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 80, ptr %1) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %1, ptr align 16 @__const.example.n, i64 80, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 24, ptr %3) #4
  %6 = call zeroext i1 @sc_heap_init(ptr noundef %3, i64 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %7

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

10:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %27

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

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

27:                                               ; preds = %10
  br label %28

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

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

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

42:                                               ; preds = %60, %40
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = icmp slt i32 %43, 5
  br i1 %44, label %46, label %45

45:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %63

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

60:                                               ; preds = %46
  %61 = load i32, ptr %5, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %5, align 4, !tbaa !5
  br label %42, !llvm.loop !21

63:                                               ; preds = %45
  br label %64

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

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

76:                                               ; preds = %64
  call void @sc_heap_term(ptr noundef %3)
  call void @llvm.lifetime.end.p0(i64 24, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 80, ptr %1) #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.example.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 @example() 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.example.n, align 16, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.example.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.example.n, i64 16), align 16, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.example.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.example.n, i64 32), align 16, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.example.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.example.n, i64 48), align 16, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.example.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.example.n, i64 64), align 16, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @__const.example.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