; LLVM IR from bartobri/data-structures-c :: priority-queue/linked-list-ordered (pq)

; 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.PQnode = type { i32, ptr }

@head = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local ptr @NEW(i32 noundef, 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local void @PQinsert(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %5 = load ptr, ptr @head, align 8, !tbaa !9
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %11

7:                                                ; preds = %1
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load ptr, ptr @head, align 8, !tbaa !9
  %10 = call ptr @NEW(i32 noundef %8, ptr noundef %9)
  store ptr %10, ptr @head, align 8, !tbaa !9
  store i32 1, ptr %4, align 4
  br label %50

11:                                               ; preds = %1
  %12 = load ptr, ptr @head, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.PQnode, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %11
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = load ptr, ptr @head, align 8, !tbaa !9
  %20 = call ptr @NEW(i32 noundef %18, ptr noundef %19)
  store ptr %20, ptr @head, align 8, !tbaa !9
  store i32 1, ptr %4, align 4
  br label %50

21:                                               ; preds = %11
  %22 = load ptr, ptr @head, align 8, !tbaa !9
  store ptr %22, ptr %3, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %38, %21
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.PQnode, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %42

28:                                               ; preds = %23
  %29 = load ptr, ptr %3, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.PQnode, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = getelementptr inbounds %struct.PQnode, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !11
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %28
  br label %42

37:                                               ; preds = %28
  br label %38

38:                                               ; preds = %37
  %39 = load ptr, ptr %3, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.PQnode, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !13
  store ptr %41, ptr %3, align 8, !tbaa !9
  br label %23, !llvm.loop !14

42:                                               ; preds = %36, %23
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = load ptr, ptr %3, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.PQnode, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  %47 = call ptr @NEW(i32 noundef %43, ptr noundef %46)
  %48 = load ptr, ptr %3, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.PQnode, ptr %48, i32 0, i32 1
  store ptr %47, ptr %49, align 8, !tbaa !13
  store i32 0, ptr %4, align 4
  br label %50

50:                                               ; preds = %42, %17, %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %51 = load i32, ptr %4, align 4
  switch i32 %51, label %53 [
    i32 0, label %52
    i32 1, label %52
  ]

52:                                               ; preds = %50, %50
  ret void

53:                                               ; preds = %50
  unreachable
}

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 = { 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 = !{!"PQnode", !6, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!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"

@head = external hidden local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local void @PQinsert(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @head, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call ptr @NEW(i32 noundef %0, ptr noundef null)
  store ptr %5, ptr @head, align 8, !tbaa !5
  br label %19

6:                                                ; preds = %1
  %7 = load i32, ptr %2, align 8, !tbaa !9
  %8 = icmp slt i32 %7, %0
  br i1 %8, label %9, label %.preheader

9:                                                ; preds = %6
  %10 = tail call ptr @NEW(i32 noundef %0, ptr noundef nonnull %2)
  store ptr %10, ptr @head, align 8, !tbaa !5
  br label %19

.preheader:                                       ; preds = %6, %13
  %.0 = phi ptr [ %12, %13 ], [ %2, %6 ]
  %11 = getelementptr inbounds i8, ptr %.0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %16, label %13

13:                                               ; preds = %.preheader
  %14 = load i32, ptr %12, align 8, !tbaa !9
  %15 = icmp slt i32 %14, %0
  br i1 %15, label %16, label %.preheader, !llvm.loop !13

16:                                               ; preds = %13, %.preheader
  %17 = getelementptr inbounds i8, ptr %.0, i64 8
  %18 = tail call ptr @NEW(i32 noundef %0, ptr noundef %12)
  store ptr %18, ptr %17, align 8, !tbaa !12
  br label %19

19:                                               ; preds = %16, %9, %4
  ret void
}

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

!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 0}
!10 = !{!"PQnode", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass