; LLVM IR from ksw2000/Data-Structure-in-C :: Heap (01-simple-max-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.element = type { i32 }

@n = external dso_local global i32, align 4
@__const.main.array = external hidden unnamed_addr constant [10 x i32], align 16
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @push(i32, ptr 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; 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 i32 @pop(ptr noundef) #0

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

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

12:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %13 = getelementptr inbounds %struct.element, ptr %5, i32 0, i32 0
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %13, align 4, !tbaa !9
  %18 = getelementptr inbounds %struct.element, ptr %5, i32 0, i32 0
  %19 = load i32, ptr %18, align 4
  call void @push(i32 %19, ptr noundef @n)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %20

20:                                               ; preds = %12
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %4, align 4, !tbaa !5
  br label %8, !llvm.loop !11

23:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %34, %23
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %28, label %37

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %29 = call i32 @pop(ptr noundef @n)
  %30 = getelementptr inbounds %struct.element, ptr %7, i32 0, i32 0
  store i32 %29, ptr %30, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %7, i64 4, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %31 = getelementptr inbounds %struct.element, ptr %6, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %32)
  br label %34

34:                                               ; preds = %28
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %4, align 4, !tbaa !5
  br label %24, !llvm.loop !14

37:                                               ; preds = %24
  %38 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %2) #4
  ret i32 0
}

declare i32 @printf(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 = !{!"", !6, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i64 0, i64 4, !5}
!14 = distinct !{!14, !12}


; 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"

@n = external dso_local global i32, align 4
@__const.main.array = external hidden unnamed_addr constant [10 x i32], align 16
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
.preheader.preheader:
  %0 = load i32, ptr @__const.main.array, align 16, !tbaa !5
  tail call void @push(i32 %0, ptr noundef nonnull @n)
  %1 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 4), align 4, !tbaa !5
  tail call void @push(i32 %1, ptr noundef nonnull @n)
  %2 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 8), align 8, !tbaa !5
  tail call void @push(i32 %2, ptr noundef nonnull @n)
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 12), align 4, !tbaa !5
  tail call void @push(i32 %3, ptr noundef nonnull @n)
  %4 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 16), align 16, !tbaa !5
  tail call void @push(i32 %4, ptr noundef nonnull @n)
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 20), align 4, !tbaa !5
  tail call void @push(i32 %5, ptr noundef nonnull @n)
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 24), align 8, !tbaa !5
  tail call void @push(i32 %6, ptr noundef nonnull @n)
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 28), align 4, !tbaa !5
  tail call void @push(i32 %7, ptr noundef nonnull @n)
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 32), align 16, !tbaa !5
  tail call void @push(i32 %8, ptr noundef nonnull @n)
  %9 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.array, i64 36), align 4, !tbaa !5
  tail call void @push(i32 %9, ptr noundef nonnull @n)
  %10 = tail call i32 @pop(ptr noundef nonnull @n)
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %10)
  %12 = tail call i32 @pop(ptr noundef nonnull @n)
  %13 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %12)
  %14 = tail call i32 @pop(ptr noundef nonnull @n)
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %14)
  %16 = tail call i32 @pop(ptr noundef nonnull @n)
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %16)
  %18 = tail call i32 @pop(ptr noundef nonnull @n)
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %18)
  %20 = tail call i32 @pop(ptr noundef nonnull @n)
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %20)
  %22 = tail call i32 @pop(ptr noundef nonnull @n)
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %22)
  %24 = tail call i32 @pop(ptr noundef nonnull @n)
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %24)
  %26 = tail call i32 @pop(ptr noundef nonnull @n)
  %27 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %26)
  %28 = tail call i32 @pop(ptr noundef nonnull @n)
  %29 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %28)
  %30 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) 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 = { 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" }

!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"}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass