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

@stderr = external global ptr, align 8
@heap = external dso_local global [200 x %struct.element], align 16
@.str.1 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

; 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) #3

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

; Function Attrs: nounwind uwtable
define dso_local i32 @pop(ptr noundef %0) #4 {
  %2 = alloca %struct.element, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.element, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = load i32, ptr %6, align 4, !tbaa !9
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %12, label %9

9:                                                ; preds = %1
  %10 = load ptr, ptr @stderr, align 8, !tbaa !5
  %11 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %10, ptr noundef @.str.1) #5
  call void @exit(i32 noundef 1) #6
  unreachable

12:                                               ; preds = %1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %2, ptr align 4 getelementptr inbounds ([200 x %struct.element], ptr @heap, i64 0, i64 1), i64 4, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = add nsw i32 %14, -1
  store i32 %15, ptr %13, align 4, !tbaa !9
  %16 = sext i32 %14 to i64
  %17 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %16
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 4 %17, i64 4, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 2, ptr %5, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %54, %12
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = icmp sle i32 %19, %21
  br i1 %22, label %23, label %64

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

28:                                               ; preds = %23
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.element, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 4, !tbaa !12
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.element, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !12
  %40 = icmp slt i32 %33, %39
  br i1 %40, label %41, label %44

41:                                               ; preds = %28
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %5, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %41, %28, %23
  %45 = getelementptr inbounds %struct.element, ptr %4, i32 0, i32 0
  %46 = load i32, ptr %45, align 4, !tbaa !12
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.element, ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 4, !tbaa !12
  %52 = icmp sge i32 %46, %51
  br i1 %52, label %53, label %54

53:                                               ; preds = %44
  br label %64

54:                                               ; preds = %44
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = sdiv i32 %55, 2
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %57
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %60
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %58, ptr align 4 %61, i64 4, i1 false), !tbaa.struct !11
  %62 = load i32, ptr %5, align 4, !tbaa !9
  %63 = mul nsw i32 %62, 2
  store i32 %63, ptr %5, align 4, !tbaa !9
  br label %18, !llvm.loop !14

64:                                               ; preds = %53, %18
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = sdiv i32 %65, 2
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %67
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %68, ptr align 4 %4, i64 4, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  %69 = getelementptr inbounds %struct.element, ptr %2, i32 0, i32 0
  %70 = load i32, ptr %69, align 4
  ret i32 %70
}

attributes #0 = { 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 #1 = { noreturn 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn 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 = !{i64 0, i64 4, !9}
!12 = !{!13, !10, i64 0}
!13 = !{!"", !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%struct.element = type { i32 }

@stderr = external local_unnamed_addr global ptr, align 8
@heap = external dso_local local_unnamed_addr global [200 x %struct.element], align 16
@.str.1 = external hidden unnamed_addr constant [20 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @pop(ptr nocapture noundef %0) local_unnamed_addr #2 {
  %2 = load i32, ptr %0, align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %3, label %6

3:                                                ; preds = %1
  %4 = load ptr, ptr @stderr, align 8, !tbaa !9
  %5 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %4, ptr noundef nonnull @.str.1) #3
  tail call void @exit(i32 noundef 1) #4
  unreachable

6:                                                ; preds = %1
  %.sroa.014.0.copyload = load i32, ptr getelementptr inbounds (i8, ptr @heap, i64 4), align 4, !tbaa !5
  %7 = add nsw i32 %2, -1
  store i32 %7, ptr %0, align 4, !tbaa !5
  %8 = sext i32 %2 to i64
  %9 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %8
  %.sroa.0.0.copyload = load i32, ptr %9, align 4, !tbaa !5
  %.not1719 = icmp slt i32 %2, 3
  br i1 %.not1719, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %25
  %10 = phi i32 [ %30, %25 ], [ %7, %6 ]
  %.020 = phi i32 [ %29, %25 ], [ 2, %6 ]
  %11 = icmp slt i32 %.020, %10
  br i1 %11, label %12, label %21

12:                                               ; preds = %.lr.ph
  %13 = sext i32 %.020 to i64
  %14 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %13
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = or disjoint i32 %.020, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = icmp slt i32 %15, %19
  %spec.select = select i1 %20, i32 %16, i32 %.020
  br label %21

21:                                               ; preds = %12, %.lr.ph
  %.1 = phi i32 [ %.020, %.lr.ph ], [ %spec.select, %12 ]
  %22 = sext i32 %.1 to i64
  %23 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %.not18 = icmp slt i32 %.sroa.0.0.copyload, %24
  br i1 %.not18, label %25, label %._crit_edge.loopexit

25:                                               ; preds = %21
  %26 = sdiv i32 %.1, 2
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %27
  store i32 %24, ptr %28, align 4, !tbaa !5
  %29 = shl nsw i32 %.1, 1
  %30 = load i32, ptr %0, align 4, !tbaa !5
  %.not17 = icmp sgt i32 %29, %30
  br i1 %.not17, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !13

._crit_edge.loopexit:                             ; preds = %21, %25
  %.2.ph = phi i32 [ %29, %25 ], [ %.1, %21 ]
  %31 = sdiv i32 %.2.ph, 2
  %32 = sext i32 %31 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %6
  %.2 = phi i64 [ 1, %6 ], [ %32, %._crit_edge.loopexit ]
  %33 = getelementptr inbounds [200 x %struct.element], ptr @heap, i64 0, i64 %.2
  store i32 %.sroa.0.0.copyload, ptr %33, align 4, !tbaa !5
  ret i32 %.sroa.014.0.copyload
}

attributes #0 = { 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 #1 = { noreturn 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 #2 = { 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 #3 = { cold nounwind }
attributes #4 = { noreturn 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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