; LLVM IR from hutusi/rethink-c :: src (prime)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @prime_number_sieve(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #6
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sub i32 %11, 1
  %13 = udiv i32 %12, 2
  store i32 %13, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = call ptr @bitmap_new(i32 noundef %14)
  store ptr %15, ptr %4, align 8, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  call void @bitmap_set_all(ptr noundef %16)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = uitofp i32 %17 to double
  %19 = call double @sqrt(double noundef %18) #6
  store double %19, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  %20 = load double, ptr %5, align 8, !tbaa !11
  %21 = call double @llvm.ceil.f64(double %20)
  %22 = fptoui double %21 to i32
  %23 = udiv i32 %22, 2
  %24 = add i32 %23, 1
  store i32 %24, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %57, %1
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = icmp ult i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %60

30:                                               ; preds = %25
  %31 = load ptr, ptr %4, align 8, !tbaa !9
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = call i32 @bitmap_get(ptr noundef %31, i32 noundef %32)
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %36, label %35

35:                                               ; preds = %30
  br label %57

36:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = call i32 @bit_map_index_to_number(i32 noundef %37)
  store i32 %38, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = mul i32 %39, %40
  store i32 %41, ptr %9, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %46, %36
  %43 = load i32, ptr %9, align 4, !tbaa !5
  %44 = load i32, ptr %2, align 4, !tbaa !5
  %45 = icmp ule i32 %43, %44
  br i1 %45, label %46, label %56

46:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = call i32 @bit_map_number_to_index(i32 noundef %47)
  store i32 %48, ptr %10, align 4, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  %50 = load i32, ptr %10, align 4, !tbaa !5
  call void @bitmap_clear(ptr noundef %49, i32 noundef %50)
  %51 = load i32, ptr %8, align 4, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = add i32 %51, %52
  %54 = load i32, ptr %9, align 4, !tbaa !5
  %55 = add i32 %54, %53
  store i32 %55, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  br label %42, !llvm.loop !13

56:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  br label %57

57:                                               ; preds = %56, %35
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = add i32 %58, 1
  store i32 %59, ptr %7, align 4, !tbaa !5
  br label %25, !llvm.loop !15

60:                                               ; preds = %29
  %61 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #6
  ret ptr %61
}

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

declare ptr @bitmap_new(i32 noundef) #2

declare void @bitmap_set_all(ptr noundef) #2

; Function Attrs: nounwind
declare double @sqrt(double noundef) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.ceil.f64(double) #4

declare i32 @bitmap_get(ptr noundef, i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @bit_map_index_to_number(i32 noundef) #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @bit_map_number_to_index(i32 noundef) #5

declare void @bitmap_clear(ptr noundef, i32 noundef) #2

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

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 = { "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 "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"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, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 noundef ptr @prime_number_sieve(i32 noundef %0) local_unnamed_addr #0 {
  %2 = add i32 %0, -1
  %3 = lshr i32 %2, 1
  %4 = tail call ptr @bitmap_new(i32 noundef %3) #5
  tail call void @bitmap_set_all(ptr noundef %4) #5
  %5 = uitofp i32 %0 to double
  %sqrt = tail call double @llvm.sqrt.f64(double %5)
  %6 = tail call double @llvm.ceil.f64(double %sqrt)
  %7 = fptoui double %6 to i32
  %8 = lshr i32 %7, 1
  br label %10

9:                                                ; preds = %.loopexit
  ret ptr %4

10:                                               ; preds = %1, %.loopexit
  %.026 = phi i32 [ 0, %1 ], [ %19, %.loopexit ]
  %11 = tail call i32 @bitmap_get(ptr noundef %4, i32 noundef %.026) #5
  %.not22 = icmp eq i32 %11, 0
  br i1 %.not22, label %.loopexit, label %12

12:                                               ; preds = %10
  %13 = tail call i32 @bit_map_index_to_number(i32 noundef %.026)
  %14 = mul i32 %13, %13
  %.not2324 = icmp ugt i32 %14, %0
  br i1 %.not2324, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %12
  %15 = shl i32 %13, 1
  br label %16

16:                                               ; preds = %.lr.ph, %16
  %.02125 = phi i32 [ %14, %.lr.ph ], [ %18, %16 ]
  %17 = tail call i32 @bit_map_number_to_index(i32 noundef %.02125)
  tail call void @bitmap_clear(ptr noundef %4, i32 noundef %17) #5
  %18 = add i32 %.02125, %15
  %.not23 = icmp ugt i32 %18, %0
  br i1 %.not23, label %.loopexit, label %16, !llvm.loop !5

.loopexit:                                        ; preds = %16, %12, %10
  %19 = add nuw i32 %.026, 1
  %exitcond.not = icmp eq i32 %.026, %8
  br i1 %exitcond.not, label %9, label %10, !llvm.loop !7
}

declare ptr @bitmap_new(i32 noundef) local_unnamed_addr #1

declare void @bitmap_set_all(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.ceil.f64(double) #2

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @bit_map_index_to_number(i32 noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @bit_map_number_to_index(i32 noundef) local_unnamed_addr #3

declare void @bitmap_clear(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.sqrt.f64(double) #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 = { "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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: AggressiveInstCombinePass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass