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

; 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.approx_pos_t = type { i64, i64, i64 }

@.str = external hidden unnamed_addr constant [27 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [11 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 i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca %struct.approx_pos_t, align 8
  %10 = alloca ptr, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #6
  store i64 1000000, ptr %2, align 8, !tbaa !5
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = call ptr @llvm.stacksave.p0()
  store ptr %12, ptr %3, align 8
  %13 = alloca i64, i64 %11, align 16
  store i64 %11, ptr %4, align 8
  %14 = getelementptr inbounds i64, ptr %13, i64 0
  store i64 42, ptr %14, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %15

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

20:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  br label %31

21:                                               ; preds = %15
  %22 = call i32 @rand() #6
  %23 = srem i32 %22, 10000000
  %24 = sext i32 %23 to i64
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i64, ptr %13, i64 %26
  store i64 %24, ptr %27, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %21
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %5, align 4, !tbaa !9
  br label %15, !llvm.loop !11

31:                                               ; preds = %20
  %32 = load i64, ptr %2, align 8, !tbaa !5
  call void @qsort(ptr noundef %13, i64 noundef %32, i64 noundef 8, ptr noundef @cmp)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  store i64 32, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %33 = load i64, ptr %2, align 8, !tbaa !5
  %34 = call ptr @pgm_index_uint64_create(ptr noundef %13, i64 noundef %33, i64 noundef 32)
  store ptr %34, ptr %7, align 8, !tbaa !13
  %35 = load ptr, ptr %7, align 8, !tbaa !13
  %36 = call i64 @pgm_index_uint64_size_in_bytes(ptr noundef %35)
  %37 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %36)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  store i64 42, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr %9) #6
  %38 = load ptr, ptr %7, align 8, !tbaa !13
  %39 = load i64, ptr %8, align 8, !tbaa !5
  call void @pgm_index_uint64_search(ptr dead_on_unwind writable sret(%struct.approx_pos_t) align 8 %9, ptr noundef %38, i64 noundef %39)
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %40 = getelementptr inbounds %struct.approx_pos_t, ptr %9, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !15
  %42 = getelementptr inbounds i64, ptr %13, i64 %41
  %43 = getelementptr inbounds %struct.approx_pos_t, ptr %9, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !17
  %45 = getelementptr inbounds %struct.approx_pos_t, ptr %9, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !15
  %47 = sub i64 %44, %46
  %48 = call ptr @bsearch(ptr noundef %8, ptr noundef %42, i64 noundef %47, i64 noundef 8, ptr noundef @cmp)
  store ptr %48, ptr %10, align 8, !tbaa !13
  %49 = load ptr, ptr %10, align 8, !tbaa !13
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %59

51:                                               ; preds = %31
  %52 = load i64, ptr %8, align 8, !tbaa !5
  %53 = load ptr, ptr %10, align 8, !tbaa !13
  %54 = ptrtoint ptr %53 to i64
  %55 = ptrtoint ptr %13 to i64
  %56 = sub i64 %54, %55
  %57 = sdiv exact i64 %56, 8
  %58 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %52, i64 noundef %57)
  br label %61

59:                                               ; preds = %31
  %60 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %61

61:                                               ; preds = %59, %51
  %62 = load ptr, ptr %7, align 8, !tbaa !13
  call void @pgm_index_uint64_destroy(ptr noundef %62)
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  %63 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %63)
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #6
  %64 = load i32, ptr %1, align 4
  ret i32 %64
}

; 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 @pgm_index_uint64_create(ptr noundef, i64 noundef, i64 noundef) #4

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

declare i64 @pgm_index_uint64_size_in_bytes(ptr noundef) #4

declare void @pgm_index_uint64_search(ptr dead_on_unwind writable sret(%struct.approx_pos_t) align 8, ptr noundef, i64 noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare ptr @bsearch(ptr noundef nonnull, ptr noundef nonnull, i64 noundef, i64 noundef, ptr noundef nonnull) #5

declare void @pgm_index_uint64_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 = { inlinehint 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 #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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!16, !6, i64 8}
!16 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!17 = !{!16, !6, i64 16}


; 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.approx_pos_t = type { i64, i64, i64 }

@.str = external hidden unnamed_addr constant [27 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [11 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 i64, align 8
  %2 = alloca %struct.approx_pos_t, align 8
  %3 = alloca [1000000 x i64], align 16
  store i64 42, ptr %3, align 16, !tbaa !5
  br label %15

4:                                                ; preds = %15
  call void @qsort(ptr noundef nonnull %3, i64 noundef 1000000, i64 noundef 8, ptr noundef nonnull @cmp) #7
  %5 = call ptr @pgm_index_uint64_create(ptr noundef nonnull %3, i64 noundef 1000000, i64 noundef 32) #7
  %6 = call i64 @pgm_index_uint64_size_in_bytes(ptr noundef %5) #7
  %7 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %6)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #7
  store i64 42, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %2) #7
  call void @pgm_index_uint64_search(ptr dead_on_unwind nonnull writable sret(%struct.approx_pos_t) align 8 %2, ptr noundef %5, i64 noundef 42) #7
  %8 = getelementptr inbounds i8, ptr %2, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds i64, ptr %3, i64 %9
  %11 = getelementptr inbounds i8, ptr %2, i64 16
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = sub i64 %12, %9
  %14 = call ptr @bsearch(ptr noundef %1, ptr noundef %10, i64 noundef %13, i64 noundef 8, ptr noundef @cmp)
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %27, label %20

15:                                               ; preds = %0, %15
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next, %15 ]
  %16 = tail call i32 @rand() #7
  %17 = srem i32 %16, 10000000
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv
  store i64 %18, ptr %19, align 8, !tbaa !5
  %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 %15, !llvm.loop !12

20:                                               ; preds = %4
  %21 = load i64, ptr %1, align 8, !tbaa !5
  %22 = ptrtoint ptr %14 to i64
  %23 = ptrtoint ptr %3 to i64
  %24 = sub i64 %22, %23
  %25 = ashr exact i64 %24, 3
  %26 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %21, i64 noundef %25)
  br label %29

27:                                               ; preds = %4
  %28 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %29

29:                                               ; preds = %27, %20
  call void @pgm_index_uint64_destroy(ptr noundef %5) #7
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %2) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #7
  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 @pgm_index_uint64_create(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #4

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

declare i64 @pgm_index_uint64_size_in_bytes(ptr noundef) local_unnamed_addr #4

declare void @pgm_index_uint64_search(ptr dead_on_unwind writable sret(%struct.approx_pos_t) align 8, ptr noundef, i64 noundef) local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare ptr @bsearch(ptr noundef nonnull, ptr noundef nonnull, i64 noundef, i64 noundef, ptr noundef nonnull) local_unnamed_addr #6

declare void @pgm_index_uint64_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 = { inlinehint 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 #7 = { 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, !6, i64 8}
!10 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!10, !6, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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