; LLVM IR from gvinciguerra/PGM-index :: c-interface/examples (updates)

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

@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmp(ptr 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 i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  store i64 1000000, ptr %2, align 8, !tbaa !5
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %3, align 8
  %12 = alloca %struct.pair_int32_t, i64 %10, align 16
  store i64 %10, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %30, %0
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = icmp ult i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %33

19:                                               ; preds = %13
  %20 = call i32 @rand() #5
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.pair_int32_t, ptr %12, i64 %22
  %24 = getelementptr inbounds %struct.pair_int32_t, ptr %23, i32 0, i32 0
  store i32 %20, ptr %24, align 8, !tbaa !11
  %25 = call i32 @rand() #5
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.pair_int32_t, ptr %12, i64 %27
  %29 = getelementptr inbounds %struct.pair_int32_t, ptr %28, i32 0, i32 1
  store i32 %25, ptr %29, align 4, !tbaa !13
  br label %30

30:                                               ; preds = %19
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %5, align 4, !tbaa !9
  br label %13, !llvm.loop !14

33:                                               ; preds = %18
  %34 = load i64, ptr %2, align 8, !tbaa !5
  call void @qsort(ptr noundef %12, i64 noundef %34, i64 noundef 8, ptr noundef @cmp)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %35 = load i64, ptr %2, align 8, !tbaa !5
  %36 = call ptr @dynamic_pgm_index_int32_create(ptr noundef %12, i64 noundef %35)
  store ptr %36, ptr %6, align 8, !tbaa !16
  %37 = load ptr, ptr %6, align 8, !tbaa !16
  call void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef %37, i32 noundef 2, i32 noundef 4)
  %38 = load ptr, ptr %6, align 8, !tbaa !16
  call void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef %38, i32 noundef 4, i32 noundef 8)
  %39 = load ptr, ptr %6, align 8, !tbaa !16
  call void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef %39, i32 noundef 8, i32 noundef 16)
  %40 = load ptr, ptr %6, align 8, !tbaa !16
  call void @dynamic_pgm_index_int32_erase(ptr noundef %40, i32 noundef 4)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %41 = load ptr, ptr %6, align 8, !tbaa !16
  %42 = call zeroext i1 @dynamic_pgm_index_int32_find(ptr noundef %41, i32 noundef 4, ptr noundef %8)
  br i1 %42, label %43, label %45

43:                                               ; preds = %33
  %44 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  br label %47

45:                                               ; preds = %33
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %47

47:                                               ; preds = %45, %43
  %48 = load ptr, ptr %6, align 8, !tbaa !16
  %49 = call zeroext i1 @dynamic_pgm_index_int32_find(ptr noundef %48, i32 noundef 8, ptr noundef %8)
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %50)
  %52 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %53 = load ptr, ptr %6, align 8, !tbaa !16
  %54 = call ptr @dynamic_pgm_index_int32_lower_bound(ptr noundef %53, i32 noundef 1)
  store ptr %54, ptr %9, align 8, !tbaa !16
  br label %55

55:                                               ; preds = %64, %47
  %56 = load ptr, ptr %6, align 8, !tbaa !16
  %57 = load ptr, ptr %9, align 8, !tbaa !16
  %58 = call zeroext i1 @dynamic_pgm_index_int32_iterator_next(ptr noundef %56, ptr noundef %57, ptr noundef %7, ptr noundef %8)
  br i1 %58, label %59, label %62

59:                                               ; preds = %55
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = icmp slt i32 %60, 10000
  br label %62

62:                                               ; preds = %59, %55
  %63 = phi i1 [ false, %55 ], [ %61, %59 ]
  br i1 %63, label %64, label %68

64:                                               ; preds = %62
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %65, i32 noundef %66)
  br label %55, !llvm.loop !18

68:                                               ; preds = %62
  %69 = load ptr, ptr %9, align 8, !tbaa !16
  call void @dynamic_pgm_index_int32_iterator_destroy(ptr noundef %69)
  %70 = load ptr, ptr %6, align 8, !tbaa !16
  call void @dynamic_pgm_index_int32_destroy(ptr noundef %70)
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %71 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %71)
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  %72 = load i32, ptr %1, align 4
  ret i32 %72
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nounwind
declare i32 @rand() #3

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #4

declare ptr @dynamic_pgm_index_int32_create(ptr noundef, i64 noundef) #4

declare void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef, i32 noundef, i32 noundef) #4

declare void @dynamic_pgm_index_int32_erase(ptr noundef, i32 noundef) #4

declare zeroext i1 @dynamic_pgm_index_int32_find(ptr noundef, i32 noundef, ptr noundef) #4

declare i32 @printf(ptr noundef, ...) #4

declare ptr @dynamic_pgm_index_int32_lower_bound(ptr noundef, i32 noundef) #4

declare zeroext i1 @dynamic_pgm_index_int32_iterator_next(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #4

declare void @dynamic_pgm_index_int32_iterator_destroy(ptr noundef) #4

declare void @dynamic_pgm_index_int32_destroy(ptr noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 #4 = { "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 }

!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 = !{!"long", !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 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"any pointer", !7, i64 0}
!18 = distinct !{!18, !15}


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

@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [10 x i8], align 1

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

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

; Function Attrs: mustprogress 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 noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca [1000000 x %struct.pair_int32_t], align 16
  br label %17

4:                                                ; preds = %17
  call void @qsort(ptr noundef nonnull %3, i64 noundef 1000000, i64 noundef 8, ptr noundef nonnull @cmp) #6
  %5 = call ptr @dynamic_pgm_index_int32_create(ptr noundef nonnull %3, i64 noundef 1000000) #6
  call void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef %5, i32 noundef 2, i32 noundef 4) #6
  call void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef %5, i32 noundef 4, i32 noundef 8) #6
  call void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef %5, i32 noundef 8, i32 noundef 16) #6
  call void @dynamic_pgm_index_int32_erase(ptr noundef %5, i32 noundef 4) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #6
  %6 = call zeroext i1 @dynamic_pgm_index_int32_find(ptr noundef %5, i32 noundef 4, ptr noundef nonnull %2) #6
  %.str..str.1 = select i1 %6, ptr @.str, ptr @.str.1
  %7 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str..str.1)
  %8 = call zeroext i1 @dynamic_pgm_index_int32_find(ptr noundef %5, i32 noundef 8, ptr noundef nonnull %2) #6
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %9)
  %11 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %12 = call ptr @dynamic_pgm_index_int32_lower_bound(ptr noundef %5, i32 noundef 1) #6
  %13 = call zeroext i1 @dynamic_pgm_index_int32_iterator_next(ptr noundef %5, ptr noundef %12, ptr noundef nonnull %1, ptr noundef nonnull %2) #6
  %14 = load i32, ptr %1, align 4
  %15 = icmp slt i32 %14, 10000
  %16 = select i1 %13, i1 %15, i1 false
  br i1 %16, label %.lr.ph, label %._crit_edge

17:                                               ; preds = %0, %17
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next, %17 ]
  %18 = tail call i32 @rand() #6
  %19 = getelementptr inbounds %struct.pair_int32_t, ptr %3, i64 %indvars.iv
  store i32 %18, ptr %19, align 8, !tbaa !9
  %20 = tail call i32 @rand() #6
  %21 = getelementptr inbounds i8, ptr %19, i64 4
  store i32 %20, ptr %21, align 4, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 1000000
  br i1 %exitcond.not, label %4, label %17, !llvm.loop !12

.lr.ph:                                           ; preds = %4, %.lr.ph
  %22 = phi i32 [ %26, %.lr.ph ], [ %14, %4 ]
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %22, i32 noundef %23)
  %25 = call zeroext i1 @dynamic_pgm_index_int32_iterator_next(ptr noundef %5, ptr noundef %12, ptr noundef nonnull %1, ptr noundef nonnull %2) #6
  %26 = load i32, ptr %1, align 4
  %27 = icmp slt i32 %26, 10000
  %28 = select i1 %25, i1 %27, i1 false
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %4
  call void @dynamic_pgm_index_int32_iterator_destroy(ptr noundef %12) #6
  call void @dynamic_pgm_index_int32_destroy(ptr noundef %5) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #6
  ret i32 0
}

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #2

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

declare ptr @dynamic_pgm_index_int32_create(ptr noundef, i64 noundef) local_unnamed_addr #4

declare void @dynamic_pgm_index_int32_insert_or_assign(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #4

declare void @dynamic_pgm_index_int32_erase(ptr noundef, i32 noundef) local_unnamed_addr #4

declare zeroext i1 @dynamic_pgm_index_int32_find(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

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

declare ptr @dynamic_pgm_index_int32_lower_bound(ptr noundef, i32 noundef) local_unnamed_addr #4

declare zeroext i1 @dynamic_pgm_index_int32_iterator_next(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

declare void @dynamic_pgm_index_int32_iterator_destroy(ptr noundef) local_unnamed_addr #4

declare void @dynamic_pgm_index_int32_destroy(ptr noundef) local_unnamed_addr #4

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nofree "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 = { "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 = { 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 #6 = { 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, !6, i64 4}
!11 = !{!10, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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