; 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: nounwind uwtable
define dso_local i32 @pqueue_add(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %13, label %12

12:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  br label %79

13:                                               ; preds = %2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.priority_queue_t, ptr %14, i32 0, i32 4
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = call i32 @vector_add(ptr noundef %15, ptr noundef %16)
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.priority_queue_t, ptr %18, i32 0, i32 4
  %20 = getelementptr inbounds %struct.vector_t, ptr %19, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.priority_queue_t, ptr %22, i32 0, i32 0
  store i64 %21, ptr %23, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.priority_queue_t, ptr %24, i32 0, i32 0
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = sub i64 %26, 1
  store i64 %27, ptr %6, align 8, !tbaa !14
  %28 = load i64, ptr %6, align 8, !tbaa !14
  %29 = icmp ugt i64 %28, 0
  br i1 %29, label %30, label %78

30:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  br label %31

31:                                               ; preds = %75, %30
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.priority_queue_t, ptr %32, i32 0, i32 4
  %34 = load i64, ptr %6, align 8, !tbaa !14
  %35 = call ptr @vector_ptr_at(ptr noundef %33, i64 noundef %34)
  store ptr %35, ptr %7, align 8, !tbaa !5
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.priority_queue_t, ptr %36, i32 0, i32 4
  %38 = load i64, ptr %6, align 8, !tbaa !14
  %39 = sub i64 %38, 1
  %40 = udiv i64 %39, 2
  %41 = call ptr @vector_ptr_at(ptr noundef %37, i64 noundef %40)
  store ptr %41, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.priority_queue_t, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.priority_queue_t, ptr %47, i32 0, i32 3
  %49 = load ptr, ptr %48, align 8, !tbaa !16
  %50 = call i32 %44(ptr noundef %45, ptr noundef %46, ptr noundef %49)
  store i32 %50, ptr %9, align 4, !tbaa !17
  %51 = load i64, ptr %6, align 8, !tbaa !14
  %52 = icmp ugt i64 %51, 0
  br i1 %52, label %53, label %62

53:                                               ; preds = %31
  %54 = load i32, ptr %9, align 4, !tbaa !17
  %55 = icmp eq i32 %54, -1
  br i1 %55, label %56, label %62

56:                                               ; preds = %53
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.priority_queue_t, ptr %59, i32 0, i32 1
  %61 = load i64, ptr %60, align 8, !tbaa !19
  call void @nswap(ptr noundef %57, ptr noundef %58, i64 noundef %61)
  br label %62

62:                                               ; preds = %56, %53, %31
  %63 = load i64, ptr %6, align 8, !tbaa !14
  %64 = sub i64 %63, 1
  %65 = udiv i64 %64, 2
  store i64 %65, ptr %6, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %66

66:                                               ; preds = %62
  %67 = load i64, ptr %6, align 8, !tbaa !14
  %68 = icmp ugt i64 %67, 0
  br i1 %68, label %69, label %75

69:                                               ; preds = %66
  %70 = load i64, ptr %6, align 8, !tbaa !14
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.priority_queue_t, ptr %71, i32 0, i32 0
  %73 = load i64, ptr %72, align 8, !tbaa !13
  %74 = icmp ult i64 %70, %73
  br label %75

75:                                               ; preds = %69, %66
  %76 = phi i1 [ false, %66 ], [ %74, %69 ]
  br i1 %76, label %31, label %77, !llvm.loop !20

77:                                               ; preds = %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %78

78:                                               ; preds = %77, %13
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %79

79:                                               ; preds = %78, %12
  %80 = load i32, ptr %3, align 4
  ret i32 %80
}

declare i32 @vector_add(ptr noundef, ptr noundef) #1

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

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) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { "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 = { 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, !11, i64 40}
!10 = !{!"priority_queue_t", !11, i64 0, !11, i64 8, !6, i64 16, !6, i64 24, !12, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!"vector_t", !6, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!13 = !{!10, !11, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !6, i64 16}
!16 = !{!10, !6, i64 24}
!17 = !{!18, !18, i64 0}
!18 = !{!"int", !7, i64 0}
!19 = !{!10, !11, i64 8}
!20 = distinct !{!20, !21}
!21 = !{!"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 0, 2) i32 @pqueue_add(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.critedge, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = tail call i32 @vector_add(ptr noundef nonnull %4, ptr noundef %1) #2
  %6 = getelementptr inbounds i8, ptr %0, i64 40
  %7 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %7, ptr %0, align 8, !tbaa !12
  %8 = add i64 %7, -1
  %.not29 = icmp eq i64 %8, 0
  br i1 %.not29, label %.critedge, label %.preheader

.preheader:                                       ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  br label %12

12:                                               ; preds = %.preheader, %24
  %.025 = phi i64 [ %15, %24 ], [ %8, %.preheader ]
  %13 = tail call ptr @vector_ptr_at(ptr noundef nonnull %4, i64 noundef %.025) #2
  %14 = add i64 %.025, -1
  %15 = lshr i64 %14, 1
  %16 = tail call ptr @vector_ptr_at(ptr noundef nonnull %4, i64 noundef %15) #2
  %17 = load ptr, ptr %9, align 8, !tbaa !13
  %18 = load ptr, ptr %10, align 8, !tbaa !14
  %19 = tail call i32 %17(ptr noundef %13, ptr noundef %16, ptr noundef %18) #2
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %23

21:                                               ; preds = %12
  %22 = load i64, ptr %11, align 8, !tbaa !15
  tail call void @nswap(ptr noundef %13, ptr noundef %16, i64 noundef %22)
  br label %23

23:                                               ; preds = %21, %12
  %.not30 = icmp ult i64 %14, 2
  br i1 %.not30, label %.critedge, label %24

24:                                               ; preds = %23
  %25 = load i64, ptr %0, align 8, !tbaa !12
  %26 = icmp ult i64 %15, %25
  br i1 %26, label %12, label %.critedge, !llvm.loop !16

.critedge:                                        ; preds = %23, %24, %3, %2
  %.0 = phi i32 [ 1, %2 ], [ 0, %3 ], [ 0, %24 ], [ 0, %23 ]
  ret i32 %.0
}

declare i32 @vector_add(ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

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 = { "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 40}
!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, !7, i64 0}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !10, i64 24}
!15 = !{!6, !7, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass