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

%struct.bitset = type { ptr, i64, i64, i32, ptr }

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @cmc_bidx_to_widx(i64 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @bs_clear_range(ptr noundef %0, i64 noundef %1, i64 noundef %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %20, label %23

20:                                               ; preds = %3
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.bitset, ptr %21, i32 0, i32 3
  store i32 4, ptr %22, align 8, !tbaa !11
  store i1 false, ptr %4, align 1
  br label %100

23:                                               ; preds = %3
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = add i64 %25, 1
  %27 = call zeroext i1 @bs_impl_resize(ptr noundef %24, i64 noundef %26, i1 noundef zeroext false)
  br i1 %27, label %29, label %28

28:                                               ; preds = %23
  store i1 false, ptr %4, align 1
  br label %100

29:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %30 = load i64, ptr %6, align 8, !tbaa !9
  %31 = call i64 @cmc_bidx_to_widx(i64 noundef %30)
  store i64 %31, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %32 = load i64, ptr %7, align 8, !tbaa !9
  %33 = call i64 @cmc_bidx_to_widx(i64 noundef %32)
  store i64 %33, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 32, ptr %10, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 -1, ptr %11, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = urem i64 %34, 32
  %36 = trunc i64 %35 to i32
  store i32 %36, ptr %12, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = add i64 %37, 1
  %39 = sub i64 0, %38
  %40 = trunc i64 %39 to i32
  %41 = urem i32 %40, 32
  store i32 %41, ptr %13, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %42 = load i32, ptr %12, align 4, !tbaa !14
  %43 = shl i32 -1, %42
  store i32 %43, ptr %14, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %44 = load i32, ptr %13, align 4, !tbaa !14
  %45 = lshr i32 -1, %44
  store i32 %45, ptr %15, align 4, !tbaa !14
  %46 = load i64, ptr %8, align 8, !tbaa !9
  %47 = load i64, ptr %9, align 8, !tbaa !9
  %48 = icmp eq i64 %46, %47
  br i1 %48, label %49, label %61

49:                                               ; preds = %29
  %50 = load i32, ptr %14, align 4, !tbaa !14
  %51 = load i32, ptr %15, align 4, !tbaa !14
  %52 = and i32 %50, %51
  %53 = xor i32 %52, -1
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.bitset, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !15
  %57 = load i64, ptr %9, align 8, !tbaa !9
  %58 = getelementptr inbounds i32, ptr %56, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !14
  %60 = and i32 %59, %53
  store i32 %60, ptr %58, align 4, !tbaa !14
  br label %97

61:                                               ; preds = %29
  %62 = load i32, ptr %14, align 4, !tbaa !14
  %63 = xor i32 %62, -1
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.bitset, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !15
  %67 = load i64, ptr %8, align 8, !tbaa !9
  %68 = getelementptr inbounds i32, ptr %66, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !14
  %70 = and i32 %69, %63
  store i32 %70, ptr %68, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  %71 = load i64, ptr %8, align 8, !tbaa !9
  %72 = add i64 %71, 1
  store i64 %72, ptr %16, align 8, !tbaa !9
  br label %73

73:                                               ; preds = %84, %61
  %74 = load i64, ptr %16, align 8, !tbaa !9
  %75 = load i64, ptr %9, align 8, !tbaa !9
  %76 = icmp ult i64 %74, %75
  br i1 %76, label %78, label %77

77:                                               ; preds = %73
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  br label %87

78:                                               ; preds = %73
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.bitset, ptr %79, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !15
  %82 = load i64, ptr %16, align 8, !tbaa !9
  %83 = getelementptr inbounds i32, ptr %81, i64 %82
  store i32 0, ptr %83, align 4, !tbaa !14
  br label %84

84:                                               ; preds = %78
  %85 = load i64, ptr %16, align 8, !tbaa !9
  %86 = add i64 %85, 1
  store i64 %86, ptr %16, align 8, !tbaa !9
  br label %73, !llvm.loop !16

87:                                               ; preds = %77
  %88 = load i32, ptr %15, align 4, !tbaa !14
  %89 = xor i32 %88, -1
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.bitset, ptr %90, i32 0, i32 0
  %92 = load ptr, ptr %91, align 8, !tbaa !15
  %93 = load i64, ptr %9, align 8, !tbaa !9
  %94 = getelementptr inbounds i32, ptr %92, i64 %93
  %95 = load i32, ptr %94, align 4, !tbaa !14
  %96 = and i32 %95, %89
  store i32 %96, ptr %94, align 4, !tbaa !14
  br label %97

97:                                               ; preds = %87, %49
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.bitset, ptr %98, i32 0, i32 3
  store i32 0, ptr %99, align 8, !tbaa !11
  store i1 true, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %100

100:                                              ; preds = %97, %28, %20
  %101 = load i1, ptr %4, align 1
  ret i1 %101
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 24}
!12 = !{!"bitset", !6, i64 0, !10, i64 8, !10, i64 16, !13, i64 24, !6, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @cmc_bidx_to_widx(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @bs_clear_range(ptr noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = icmp uge i64 %2, %1
  br i1 %4, label %5, label %.sink.split

5:                                                ; preds = %3
  %6 = add i64 %2, 1
  %7 = tail call zeroext i1 @bs_impl_resize(ptr noundef %0, i64 noundef %6, i1 noundef zeroext false)
  br i1 %7, label %8, label %38

8:                                                ; preds = %5
  %9 = tail call i64 @cmc_bidx_to_widx(i64 noundef %1)
  %10 = tail call i64 @cmc_bidx_to_widx(i64 noundef %2)
  %11 = trunc i64 %1 to i32
  %12 = and i32 %11, 31
  %13 = trunc i64 %2 to i32
  %14 = and i32 %13, 31
  %15 = xor i32 %14, 31
  %16 = shl nsw i32 -1, %12
  %17 = lshr i32 -1, %15
  %18 = icmp eq i64 %9, %10
  %19 = load ptr, ptr %0, align 8, !tbaa !5
  %20 = getelementptr inbounds i32, ptr %19, i64 %9
  br i1 %18, label %21, label %23

21:                                               ; preds = %8
  %22 = and i32 %17, %16
  br label %34

23:                                               ; preds = %8
  %24 = xor i32 %16, -1
  %25 = load i32, ptr %20, align 4, !tbaa !12
  %26 = and i32 %25, %24
  store i32 %26, ptr %20, align 4, !tbaa !12
  %.035 = add i64 %9, 1
  %27 = icmp ult i64 %.035, %10
  br i1 %27, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %23
  %28 = shl i64 %9, 2
  %29 = getelementptr i8, ptr %19, i64 %28
  %scevgep = getelementptr i8, ptr %29, i64 4
  %30 = xor i64 %9, -1
  %31 = add i64 %10, %30
  %32 = shl nuw i64 %31, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep, i8 0, i64 %32, i1 false), !tbaa !12
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %23
  %33 = getelementptr inbounds i32, ptr %19, i64 %10
  br label %34

34:                                               ; preds = %._crit_edge, %21
  %.sink = phi ptr [ %33, %._crit_edge ], [ %20, %21 ]
  %.sink39.in = phi i32 [ %17, %._crit_edge ], [ %22, %21 ]
  %.sink39 = xor i32 %.sink39.in, -1
  %35 = load i32, ptr %.sink, align 4, !tbaa !12
  %36 = and i32 %35, %.sink39
  store i32 %36, ptr %.sink, align 4, !tbaa !12
  br label %.sink.split

.sink.split:                                      ; preds = %3, %34
  %.sink40 = phi i32 [ 0, %34 ], [ 4, %3 ]
  %37 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %.sink40, ptr %37, align 8, !tbaa !13
  br label %38

38:                                               ; preds = %.sink.split, %5
  %.030 = phi i1 [ false, %5 ], [ %4, %.sink.split ]
  ret i1 %.030
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { 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 #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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, !7, i64 0}
!6 = !{!"bitset", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!6, !11, i64 24}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass