; LLVM IR from yourtion/LearningMasteringAlgorithms-C :: source (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.Heap_ = type { i32, ptr, ptr, 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @heap_insert(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Heap_, ptr %10, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Heap_, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !12
  %16 = add nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = mul i64 %17, 8
  %19 = call ptr @realloc(ptr noundef %12, i64 noundef %18) #4
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %103

22:                                               ; preds = %2
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Heap_, ptr %24, i32 0, i32 3
  store ptr %23, ptr %25, align 8, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.Heap_, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !9
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Heap_, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !12
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds ptr, ptr %29, i64 %33
  store ptr %26, ptr %34, align 8, !tbaa !5
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Heap_, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !12
  store i32 %37, ptr %7, align 4, !tbaa !13
  %38 = load i32, ptr %7, align 4, !tbaa !13
  %39 = sub nsw i32 %38, 1
  %40 = sdiv i32 %39, 2
  store i32 %40, ptr %8, align 4, !tbaa !13
  br label %41

41:                                               ; preds = %66, %22
  %42 = load i32, ptr %7, align 4, !tbaa !13
  %43 = icmp sgt i32 %42, 0
  br i1 %43, label %44, label %64

44:                                               ; preds = %41
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Heap_, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Heap_, ptr %48, i32 0, i32 3
  %50 = load ptr, ptr %49, align 8, !tbaa !9
  %51 = load i32, ptr %8, align 4, !tbaa !13
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds ptr, ptr %50, i64 %52
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.Heap_, ptr %55, i32 0, i32 3
  %57 = load ptr, ptr %56, align 8, !tbaa !9
  %58 = load i32, ptr %7, align 4, !tbaa !13
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds ptr, ptr %57, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = call i32 %47(ptr noundef %54, ptr noundef %61)
  %63 = icmp slt i32 %62, 0
  br label %64

64:                                               ; preds = %44, %41
  %65 = phi i1 [ false, %41 ], [ %63, %44 ]
  br i1 %65, label %66, label %98

66:                                               ; preds = %64
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Heap_, ptr %67, i32 0, i32 3
  %69 = load ptr, ptr %68, align 8, !tbaa !9
  %70 = load i32, ptr %8, align 4, !tbaa !13
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds ptr, ptr %69, i64 %71
  %73 = load ptr, ptr %72, align 8, !tbaa !5
  store ptr %73, ptr %6, align 8, !tbaa !5
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.Heap_, ptr %74, i32 0, i32 3
  %76 = load ptr, ptr %75, align 8, !tbaa !9
  %77 = load i32, ptr %7, align 4, !tbaa !13
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds ptr, ptr %76, i64 %78
  %80 = load ptr, ptr %79, align 8, !tbaa !5
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Heap_, ptr %81, i32 0, i32 3
  %83 = load ptr, ptr %82, align 8, !tbaa !9
  %84 = load i32, ptr %8, align 4, !tbaa !13
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds ptr, ptr %83, i64 %85
  store ptr %80, ptr %86, align 8, !tbaa !5
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.Heap_, ptr %88, i32 0, i32 3
  %90 = load ptr, ptr %89, align 8, !tbaa !9
  %91 = load i32, ptr %7, align 4, !tbaa !13
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds ptr, ptr %90, i64 %92
  store ptr %87, ptr %93, align 8, !tbaa !5
  %94 = load i32, ptr %8, align 4, !tbaa !13
  store i32 %94, ptr %7, align 4, !tbaa !13
  %95 = load i32, ptr %7, align 4, !tbaa !13
  %96 = sub nsw i32 %95, 1
  %97 = sdiv i32 %96, 2
  store i32 %97, ptr %8, align 4, !tbaa !13
  br label %41, !llvm.loop !15

98:                                               ; preds = %64
  %99 = load ptr, ptr %4, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.Heap_, ptr %99, i32 0, i32 0
  %101 = load i32, ptr %100, align 8, !tbaa !12
  %102 = add nsw i32 %101, 1
  store i32 %102, ptr %100, align 8, !tbaa !12
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %103

103:                                              ; preds = %98, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %104 = load i32, ptr %3, align 4
  ret i32 %104
}

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

attributes #0 = { nocallback nofree nosync 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 = { 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 #3 = { nounwind }
attributes #4 = { 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, !6, i64 24}
!10 = !{!"Heap_", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @heap_insert(ptr nocapture noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = load i32, ptr %0, align 8, !tbaa !11
  %6 = add nsw i32 %5, 1
  %7 = sext i32 %6 to i64
  %8 = shl nsw i64 %7, 3
  %9 = tail call ptr @realloc(ptr noundef %4, i64 noundef %8) #2
  %10 = icmp eq ptr %9, null
  br i1 %10, label %38, label %11

11:                                               ; preds = %2
  store ptr %9, ptr %3, align 8, !tbaa !5
  %12 = load i32, ptr %0, align 8, !tbaa !11
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds ptr, ptr %9, i64 %13
  store ptr %1, ptr %14, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = icmp sgt i32 %12, 0
  br i1 %16, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %11, %27
  %.02732 = phi i32 [ %.033, %27 ], [ %12, %11 ]
  %.033.in = add nsw i32 %.02732, -1
  %.033 = sdiv i32 %.033.in, 2
  %17 = load ptr, ptr %15, align 8, !tbaa !13
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = sext i32 %.033 to i64
  %20 = getelementptr inbounds ptr, ptr %18, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = zext nneg i32 %.02732 to i64
  %23 = getelementptr inbounds ptr, ptr %18, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  %25 = tail call i32 %17(ptr noundef %21, ptr noundef %24) #3
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %27, label %.critedge.loopexit

27:                                               ; preds = %.lr.ph
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds ptr, ptr %28, i64 %19
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = getelementptr inbounds ptr, ptr %28, i64 %22
  %32 = load ptr, ptr %31, align 8, !tbaa !12
  store ptr %32, ptr %29, align 8, !tbaa !12
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds ptr, ptr %33, i64 %22
  store ptr %30, ptr %34, align 8, !tbaa !12
  %35 = icmp sgt i32 %.02732, 2
  br i1 %35, label %.lr.ph, label %.critedge.loopexit, !llvm.loop !14

.critedge.loopexit:                               ; preds = %27, %.lr.ph
  %.pre = load i32, ptr %0, align 8, !tbaa !11
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %11
  %36 = phi i32 [ %.pre, %.critedge.loopexit ], [ %12, %11 ]
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %0, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %2, %.critedge
  %.028 = phi i32 [ 0, %.critedge ], [ -1, %2 ]
  ret i32 %.028
}

; 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 #1

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 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 #2 = { nounwind allocsize(1) }
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, !10, i64 24}
!6 = !{!"Heap_", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"int", !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 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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