; LLVM IR from yudi-matsuzake/libgenerics :: src (priority_queue)

; 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.priority_queue_t = type { i64, i64, ptr, ptr, %struct.vector_t }
%struct.vector_t = type { ptr, i64, i64, i64 }

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

declare ptr @vector_ptr_at(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @nswap(ptr noundef, ptr noundef, i64 noundef) #2

; 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 void @max_heapify(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = add i64 %12, 1
  %14 = mul i64 %13, 2
  %15 = sub i64 %14, 1
  store i64 %15, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %16 = load i64, ptr %4, align 8, !tbaa !9
  %17 = add i64 %16, 1
  %18 = mul i64 %17, 2
  %19 = sub i64 %18, 1
  %20 = add i64 %19, 1
  store i64 %20, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.priority_queue_t, ptr %21, i32 0, i32 4
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = call ptr @vector_ptr_at(ptr noundef %22, i64 noundef %23)
  store ptr %24, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.priority_queue_t, ptr %25, i32 0, i32 4
  %27 = load i64, ptr %5, align 8, !tbaa !9
  %28 = call ptr @vector_ptr_at(ptr noundef %26, i64 noundef %27)
  store ptr %28, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.priority_queue_t, ptr %29, i32 0, i32 4
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = call ptr @vector_ptr_at(ptr noundef %30, i64 noundef %31)
  store ptr %32, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.priority_queue_t, ptr %34, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = icmp ult i64 %33, %36
  br i1 %37, label %38, label %52

38:                                               ; preds = %2
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.priority_queue_t, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.priority_queue_t, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  %47 = call i32 %41(ptr noundef %42, ptr noundef %43, ptr noundef %46)
  %48 = icmp eq i32 %47, -1
  br i1 %48, label %49, label %52

49:                                               ; preds = %38
  %50 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %50, ptr %7, align 8, !tbaa !9
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %51, ptr %11, align 8, !tbaa !5
  br label %55

52:                                               ; preds = %38, %2
  %53 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %53, ptr %7, align 8, !tbaa !9
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %54, ptr %11, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %52, %49
  %56 = load i64, ptr %6, align 8, !tbaa !9
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.priority_queue_t, ptr %57, i32 0, i32 0
  %59 = load i64, ptr %58, align 8, !tbaa !11
  %60 = icmp ult i64 %56, %59
  br i1 %60, label %61, label %75

61:                                               ; preds = %55
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.priority_queue_t, ptr %62, i32 0, i32 2
  %64 = load ptr, ptr %63, align 8, !tbaa !14
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = load ptr, ptr %11, align 8, !tbaa !5
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.priority_queue_t, ptr %67, i32 0, i32 3
  %69 = load ptr, ptr %68, align 8, !tbaa !15
  %70 = call i32 %64(ptr noundef %65, ptr noundef %66, ptr noundef %69)
  %71 = icmp eq i32 %70, -1
  br i1 %71, label %72, label %75

72:                                               ; preds = %61
  %73 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %73, ptr %7, align 8, !tbaa !9
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %74, ptr %11, align 8, !tbaa !5
  br label %75

75:                                               ; preds = %72, %61, %55
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = load i64, ptr %4, align 8, !tbaa !9
  %78 = icmp ne i64 %76, %77
  br i1 %78, label %79, label %87

79:                                               ; preds = %75
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.priority_queue_t, ptr %82, i32 0, i32 1
  %84 = load i64, ptr %83, align 8, !tbaa !16
  call void @nswap(ptr noundef %80, ptr noundef %81, i64 noundef %84)
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = load i64, ptr %7, align 8, !tbaa !9
  call void @max_heapify(ptr noundef %85, i64 noundef %86)
  br label %87

87:                                               ; preds = %79, %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #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 #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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"priority_queue_t", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 24, !13, i64 32}
!13 = !{!"vector_t", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!14 = !{!12, !6, i64 16}
!15 = !{!12, !6, i64 24}
!16 = !{!12, !10, i64 8}


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

declare ptr @vector_ptr_at(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @nswap(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @max_heapify(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  br label %tailrecurse

tailrecurse:                                      ; preds = %30, %2
  %.tr37 = phi i64 [ %1, %2 ], [ %.133, %30 ]
  %7 = shl i64 %.tr37, 1
  %8 = add i64 %7, 2
  %9 = or disjoint i64 %7, 1
  %10 = tail call ptr @vector_ptr_at(ptr noundef nonnull %3, i64 noundef %.tr37) #2
  %11 = tail call ptr @vector_ptr_at(ptr noundef nonnull %3, i64 noundef %9) #2
  %12 = tail call ptr @vector_ptr_at(ptr noundef nonnull %3, i64 noundef %8) #2
  %13 = load i64, ptr %0, align 8, !tbaa !5
  %14 = icmp ult i64 %9, %13
  br i1 %14, label %15, label %20

15:                                               ; preds = %tailrecurse
  %16 = load ptr, ptr %4, align 8, !tbaa !12
  %17 = load ptr, ptr %5, align 8, !tbaa !13
  %18 = tail call i32 %16(ptr noundef %11, ptr noundef %10, ptr noundef %17) #2
  %19 = icmp eq i32 %18, -1
  br i1 %19, label %21, label %20

20:                                               ; preds = %15, %tailrecurse
  br label %21

21:                                               ; preds = %15, %20
  %.032 = phi i64 [ %.tr37, %20 ], [ %9, %15 ]
  %.0 = phi ptr [ %10, %20 ], [ %11, %15 ]
  %22 = load i64, ptr %0, align 8, !tbaa !5
  %23 = icmp ult i64 %8, %22
  br i1 %23, label %24, label %29

24:                                               ; preds = %21
  %25 = load ptr, ptr %4, align 8, !tbaa !12
  %26 = load ptr, ptr %5, align 8, !tbaa !13
  %27 = tail call i32 %25(ptr noundef %12, ptr noundef %.0, ptr noundef %26) #2
  %28 = icmp eq i32 %27, -1
  %spec.select = select i1 %28, i64 %8, i64 %.032
  %spec.select36 = select i1 %28, ptr %12, ptr %.0
  br label %29

29:                                               ; preds = %24, %21
  %.133 = phi i64 [ %.032, %21 ], [ %spec.select, %24 ]
  %.1 = phi ptr [ %.0, %21 ], [ %spec.select36, %24 ]
  %.not = icmp eq i64 %.133, %.tr37
  br i1 %.not, label %32, label %30

30:                                               ; preds = %29
  %31 = load i64, ptr %6, align 8, !tbaa !14
  tail call void @nswap(ptr noundef %10, ptr noundef %.1, i64 noundef %31)
  br label %tailrecurse

32:                                               ; preds = %29
  ret void
}

attributes #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 = { 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 }

!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 = !{!"priority_queue_t", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24, !11, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"vector_t", !10, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !10, i64 24}
!14 = !{!6, !7, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,SROAPass,SimplifyCFGPass,TailCallElimPass