; LLVM IR from zhemao/libds :: heaptest (main)

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmpfunc(ptr noundef, i32 noundef, i32 noundef) #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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local void @print_heap(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %4 = call ptr @create_heap(ptr noundef @cmpfunc)
  store ptr %4, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %12, %0
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = icmp slt i32 %6, 8
  br i1 %7, label %8, label %15

8:                                                ; preds = %5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.heap, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  call void @vector_add(ptr noundef %11, ptr noundef %3, i64 noundef 4)
  br label %12

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

15:                                               ; preds = %5
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  call void @build_heap(ptr noundef %16)
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.heap, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  call void @print_heap(ptr noundef %19)
  store i32 8, ptr %3, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %25, %15
  %21 = load i32, ptr %3, align 4, !tbaa !9
  %22 = icmp slt i32 %21, 10
  br i1 %22, label %23, label %28

23:                                               ; preds = %20
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  call void @heap_insert(ptr noundef %24, ptr noundef %3, i32 noundef 4)
  br label %25

25:                                               ; preds = %23
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %3, align 4, !tbaa !9
  br label %20, !llvm.loop !15

28:                                               ; preds = %20
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.heap, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  call void @print_heap(ptr noundef %31)
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  call void @heap_remove(ptr noundef %32)
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.heap, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  call void @print_heap(ptr noundef %35)
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  call void @destroy_heap(ptr noundef %36)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  ret i32 0
}

declare ptr @create_heap(ptr noundef) #2

declare void @vector_add(ptr noundef, ptr noundef, i64 noundef) #2

declare void @build_heap(ptr noundef) #2

declare void @heap_insert(ptr noundef, ptr noundef, i32 noundef) #2

declare void @heap_remove(ptr noundef) #2

declare void @destroy_heap(ptr noundef) #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 = { 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 = { 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, !6, i64 0}
!12 = !{!"heap", !6, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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"

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmpfunc(ptr noundef, i32 noundef, i32 noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @print_heap(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = tail call ptr @create_heap(ptr noundef nonnull @cmpfunc) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %0, %3
  %4 = load ptr, ptr %2, align 8, !tbaa !9
  call void @vector_add(ptr noundef %4, ptr noundef nonnull %1, i64 noundef 4) #3
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1
  store i32 %6, ptr %1, align 4, !tbaa !5
  %7 = icmp slt i32 %5, 7
  br i1 %7, label %3, label %8, !llvm.loop !12

8:                                                ; preds = %3
  call void @build_heap(ptr noundef nonnull %2) #3
  %9 = load ptr, ptr %2, align 8, !tbaa !9
  call void @print_heap(ptr noundef %9)
  store i32 8, ptr %1, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %8, %10
  call void @heap_insert(ptr noundef nonnull %2, ptr noundef nonnull %1, i32 noundef 4) #3
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr %1, align 4, !tbaa !5
  %13 = icmp slt i32 %11, 9
  br i1 %13, label %10, label %14, !llvm.loop !14

14:                                               ; preds = %10
  %15 = load ptr, ptr %2, align 8, !tbaa !9
  call void @print_heap(ptr noundef %15)
  call void @heap_remove(ptr noundef nonnull %2) #3
  %16 = load ptr, ptr %2, align 8, !tbaa !9
  call void @print_heap(ptr noundef %16)
  call void @destroy_heap(ptr noundef nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0
}

declare ptr @create_heap(ptr noundef) local_unnamed_addr #2

declare void @vector_add(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @build_heap(ptr noundef) local_unnamed_addr #2

declare void @heap_insert(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @heap_remove(ptr noundef) local_unnamed_addr #2

declare void @destroy_heap(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 = { 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, !11, i64 0}
!10 = !{!"heap", !11, i64 0, !11, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass