; 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_flip_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 %99

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 %99

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 %60

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 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.bitset, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !15
  %56 = load i64, ptr %9, align 8, !tbaa !9
  %57 = getelementptr inbounds i32, ptr %55, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !14
  %59 = xor i32 %58, %52
  store i32 %59, ptr %57, align 4, !tbaa !14
  br label %96

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

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

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

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

84:                                               ; preds = %76
  %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 %71, !llvm.loop !16

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

96:                                               ; preds = %87, %49
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.bitset, ptr %97, i32 0, i32 3
  store i32 0, ptr %98, 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 %99

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

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_flip_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 %45

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
  br i1 %18, label %19, label %23

19:                                               ; preds = %8
  %20 = and i32 %17, %16
  %21 = load ptr, ptr %0, align 8, !tbaa !5
  %22 = getelementptr inbounds i32, ptr %21, i64 %9
  br label %41

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

.lr.ph.preheader:                                 ; preds = %23
  %29 = xor i64 %9, -1
  %30 = add i64 %10, %29
  %min.iters.check = icmp ult i64 %30, 8
  br i1 %min.iters.check, label %.lr.ph.preheader43, label %vector.ph

.lr.ph.preheader43:                               ; preds = %middle.block, %.lr.ph.preheader
  %.036.ph = phi i64 [ %.035, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %30, -8
  %ind.end = add i64 %.035, %n.vec
  %31 = getelementptr i32, ptr %24, i64 %.035
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %32 = getelementptr i32, ptr %31, i64 %index
  %33 = getelementptr inbounds i8, ptr %32, i64 16
  %wide.load = load <4 x i32>, ptr %32, align 4, !tbaa !12
  %wide.load42 = load <4 x i32>, ptr %33, align 4, !tbaa !12
  %34 = xor <4 x i32> %wide.load, <i32 -1, i32 -1, i32 -1, i32 -1>
  %35 = xor <4 x i32> %wide.load42, <i32 -1, i32 -1, i32 -1, i32 -1>
  store <4 x i32> %34, ptr %32, align 4, !tbaa !12
  store <4 x i32> %35, ptr %33, align 4, !tbaa !12
  %index.next = add nuw i64 %index, 8
  %36 = icmp eq i64 %index.next, %n.vec
  br i1 %36, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %30, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader43

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %23
  %37 = getelementptr inbounds i32, ptr %24, i64 %10
  br label %41

.lr.ph:                                           ; preds = %.lr.ph.preheader43, %.lr.ph
  %.036 = phi i64 [ %.0, %.lr.ph ], [ %.036.ph, %.lr.ph.preheader43 ]
  %38 = getelementptr inbounds i32, ptr %24, i64 %.036
  %39 = load i32, ptr %38, align 4, !tbaa !12
  %40 = xor i32 %39, -1
  store i32 %40, ptr %38, align 4, !tbaa !12
  %.0 = add nuw i64 %.036, 1
  %exitcond.not = icmp eq i64 %.0, %10
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !17

41:                                               ; preds = %._crit_edge, %19
  %.sink = phi ptr [ %37, %._crit_edge ], [ %22, %19 ]
  %.sink39 = phi i32 [ %17, %._crit_edge ], [ %20, %19 ]
  %42 = load i32, ptr %.sink, align 4, !tbaa !12
  %43 = xor i32 %42, %.sink39
  store i32 %43, ptr %.sink, align 4, !tbaa !12
  br label %.sink.split

.sink.split:                                      ; preds = %3, %41
  %.sink40 = phi i32 [ 0, %41 ], [ 4, %3 ]
  %44 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %.sink40, ptr %44, align 8, !tbaa !18
  br label %45

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

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

!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 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !16, !15}
!18 = !{!6, !11, i64 24}


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