; LLVM IR from LeoVen/C-Macro-Collections :: examples (sieve)

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

@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @bs_new(i64 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
declare dso_local zeroext i1 @bs_set_range(ptr noundef, i64 noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bs_clear(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bs_get(ptr noundef, i64 noundef) #0

; 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 i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  store i64 1000000000, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %10 = call ptr @bs_new(i64 noundef 1000000000)
  store ptr %10, ptr %3, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !9
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %0
  store i32 1, ptr %1, align 4
  store i32 1, ptr %4, align 4
  br label %70

14:                                               ; preds = %0
  %15 = load ptr, ptr %3, align 8, !tbaa !9
  %16 = call zeroext i1 @bs_set_range(ptr noundef %15, i64 noundef 0, i64 noundef 1000000000)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %17 = call double @sqrt(double noundef 1.000000e+09) #4
  %18 = fadd double %17, 1.000000e+00
  %19 = fptoui double %18 to i64
  store i64 %19, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 2, ptr %6, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %46, %14
  %21 = load i64, ptr %6, align 8, !tbaa !5
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %49

25:                                               ; preds = %20
  %26 = load ptr, ptr %3, align 8, !tbaa !9
  %27 = load i64, ptr %6, align 8, !tbaa !5
  %28 = call zeroext i1 @bs_get(ptr noundef %26, i64 noundef %27)
  br i1 %28, label %29, label %45

29:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %30 = load i64, ptr %6, align 8, !tbaa !5
  %31 = mul i64 2, %30
  store i64 %31, ptr %7, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %40, %29
  %33 = load i64, ptr %7, align 8, !tbaa !5
  %34 = icmp ult i64 %33, 1000000000
  br i1 %34, label %36, label %35

35:                                               ; preds = %32
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %44

36:                                               ; preds = %32
  %37 = load ptr, ptr %3, align 8, !tbaa !9
  %38 = load i64, ptr %7, align 8, !tbaa !5
  %39 = call zeroext i1 @bs_clear(ptr noundef %37, i64 noundef %38)
  br label %40

40:                                               ; preds = %36
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = load i64, ptr %7, align 8, !tbaa !5
  %43 = add i64 %42, %41
  store i64 %43, ptr %7, align 8, !tbaa !5
  br label %32, !llvm.loop !11

44:                                               ; preds = %35
  br label %45

45:                                               ; preds = %44, %25
  br label %46

46:                                               ; preds = %45
  %47 = load i64, ptr %6, align 8, !tbaa !5
  %48 = add i64 %47, 1
  store i64 %48, ptr %6, align 8, !tbaa !5
  br label %20, !llvm.loop !13

49:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %50 = call noalias ptr @fopen(ptr noundef @.str, ptr noundef @.str.1)
  store ptr %50, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 2, ptr %9, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %64, %49
  %52 = load i64, ptr %9, align 8, !tbaa !5
  %53 = icmp ult i64 %52, 1000000000
  br i1 %53, label %55, label %54

54:                                               ; preds = %51
  store i32 8, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %67

55:                                               ; preds = %51
  %56 = load ptr, ptr %3, align 8, !tbaa !9
  %57 = load i64, ptr %9, align 8, !tbaa !5
  %58 = call zeroext i1 @bs_get(ptr noundef %56, i64 noundef %57)
  br i1 %58, label %59, label %63

59:                                               ; preds = %55
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  %61 = load i64, ptr %9, align 8, !tbaa !5
  %62 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %60, ptr noundef @.str.2, i64 noundef %61) #4
  br label %63

63:                                               ; preds = %59, %55
  br label %64

64:                                               ; preds = %63
  %65 = load i64, ptr %9, align 8, !tbaa !5
  %66 = add i64 %65, 1
  store i64 %66, ptr %9, align 8, !tbaa !5
  br label %51, !llvm.loop !14

67:                                               ; preds = %54
  %68 = load ptr, ptr %8, align 8, !tbaa !9
  %69 = call i32 @fclose(ptr noundef %68)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  store i32 0, ptr %4, align 4
  br label %70

70:                                               ; preds = %67, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %71 = load i32, ptr %4, align 4
  switch i32 %71, label %74 [
    i32 0, label %72
    i32 1, label %72
  ]

72:                                               ; preds = %70, %70
  %73 = load i32, ptr %1, align 4
  ret i32 %73

74:                                               ; preds = %70
  unreachable
}

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

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

declare i32 @fclose(ptr noundef) #3

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 "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 = { "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 = { 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 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bs_set_range(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bs_clear(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bs_get(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @main() local_unnamed_addr #0 {
  %1 = tail call ptr @bs_new(i64 noundef 1000000000)
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %21, label %2

2:                                                ; preds = %0
  %3 = tail call zeroext i1 @bs_set_range(ptr noundef nonnull %1, i64 noundef 0, i64 noundef 1000000000)
  br label %6

4:                                                ; preds = %.loopexit
  %5 = tail call noalias ptr @fopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1)
  br label %15

6:                                                ; preds = %2, %.loopexit
  %.02225 = phi i64 [ 2, %2 ], [ %12, %.loopexit ]
  %7 = tail call zeroext i1 @bs_get(ptr noundef nonnull %1, i64 noundef %.02225)
  br i1 %7, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %6
  %8 = shl nuw nsw i64 %.02225, 1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.02124 = phi i64 [ %10, %.lr.ph ], [ %8, %.lr.ph.preheader ]
  %9 = tail call zeroext i1 @bs_clear(ptr noundef nonnull %1, i64 noundef %.02124)
  %10 = add nuw nsw i64 %.02124, %.02225
  %11 = icmp ult i64 %10, 1000000000
  br i1 %11, label %.lr.ph, label %.loopexit, !llvm.loop !5

.loopexit:                                        ; preds = %.lr.ph, %6
  %12 = add nuw nsw i64 %.02225, 1
  %exitcond.not = icmp eq i64 %12, 31623
  br i1 %exitcond.not, label %4, label %6, !llvm.loop !7

13:                                               ; preds = %19
  %14 = tail call i32 @fclose(ptr noundef %5)
  br label %21

15:                                               ; preds = %4, %19
  %.027 = phi i64 [ 2, %4 ], [ %20, %19 ]
  %16 = tail call zeroext i1 @bs_get(ptr noundef nonnull %1, i64 noundef %.027)
  br i1 %16, label %17, label %19

17:                                               ; preds = %15
  %18 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.2, i64 noundef %.027) #2
  br label %19

19:                                               ; preds = %15, %17
  %20 = add nuw nsw i64 %.027, 1
  %exitcond29.not = icmp eq i64 %20, 1000000000
  br i1 %exitcond29.not, label %13, label %15, !llvm.loop !8

21:                                               ; preds = %0, %13
  %.020 = phi i32 [ 0, %13 ], [ 1, %0 ]
  ret i32 %.020
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #1

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

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #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 = { 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 #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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


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