; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @bs_impl_resize(ptr noundef %0, i64 noundef %1, i1 noundef zeroext %2) #3 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  %11 = zext i1 %2 to i8
  store i8 %11, ptr %7, align 1, !tbaa !11
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %17

14:                                               ; preds = %3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.bitset, ptr %15, i32 0, i32 3
  store i32 4, ptr %16, align 8, !tbaa !13
  store i1 false, ptr %4, align 1
  br label %74

17:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = sub i64 %18, 1
  %20 = call i64 @cmc_bidx_to_widx(i64 noundef %19)
  %21 = add i64 %20, 1
  store i64 %21, ptr %8, align 8, !tbaa !9
  %22 = load i8, ptr %7, align 1, !tbaa !11, !range !16, !noundef !17
  %23 = trunc i8 %22 to i1
  br i1 %23, label %31, label %24

24:                                               ; preds = %17
  %25 = load i64, ptr %8, align 8, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.bitset, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !18
  %29 = icmp ule i64 %25, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %24
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %73

31:                                               ; preds = %24, %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.bitset, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !19
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = mul i64 %35, 4
  %37 = call ptr @realloc(ptr noundef %34, i64 noundef %36) #6
  store ptr %37, ptr %10, align 8, !tbaa !5
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %43, label %40

40:                                               ; preds = %31
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.bitset, ptr %41, i32 0, i32 3
  store i32 1, ptr %42, align 8, !tbaa !13
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %72

43:                                               ; preds = %31
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.bitset, ptr %45, i32 0, i32 0
  store ptr %44, ptr %46, align 8, !tbaa !19
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.bitset, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !18
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = icmp ult i64 %49, %50
  br i1 %51, label %52, label %66

52:                                               ; preds = %43
  %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 !19
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.bitset, ptr %56, i32 0, i32 1
  %58 = load i64, ptr %57, align 8, !tbaa !18
  %59 = getelementptr inbounds i32, ptr %55, i64 %58
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.bitset, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !18
  %64 = sub i64 %60, %63
  %65 = mul i64 %64, 4
  call void @llvm.memset.p0.i64(ptr align 4 %59, i8 0, i64 %65, i1 false)
  br label %66

66:                                               ; preds = %52, %43
  %67 = load i64, ptr %8, align 8, !tbaa !9
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.bitset, ptr %68, i32 0, i32 1
  store i64 %67, ptr %69, align 8, !tbaa !18
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.bitset, ptr %70, i32 0, i32 3
  store i32 0, ptr %71, align 8, !tbaa !13
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %72

72:                                               ; preds = %66, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %73

73:                                               ; preds = %72, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %74

74:                                               ; preds = %73, %14
  %75 = load i1, ptr %4, align 1
  ret i1 %75
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #4

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

!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, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !15, i64 24}
!14 = !{!"bitset", !6, i64 0, !10, i64 8, !10, i64 16, !15, i64 24, !6, i64 32}
!15 = !{!"int", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!14, !10, i64 8}
!19 = !{!14, !6, i64 0}


; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

5:                                                ; preds = %3
  %6 = add i64 %1, -1
  %7 = tail call i64 @cmc_bidx_to_widx(i64 noundef %6)
  %8 = add i64 %7, 1
  br i1 %2, label %12, label %9

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %.not = icmp ugt i64 %8, %11
  br i1 %.not, label %12, label %26

12:                                               ; preds = %9, %5
  %13 = load ptr, ptr %0, align 8, !tbaa !12
  %14 = shl i64 %8, 2
  %15 = tail call ptr @realloc(ptr noundef %13, i64 noundef %14) #4
  %.not25.not = icmp eq ptr %15, null
  br i1 %.not25.not, label %.sink.split, label %16

16:                                               ; preds = %12
  store ptr %15, ptr %0, align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = icmp ult i64 %18, %8
  br i1 %19, label %20, label %24

20:                                               ; preds = %16
  %21 = getelementptr inbounds i32, ptr %15, i64 %18
  %22 = sub i64 %8, %18
  %23 = shl i64 %22, 2
  tail call void @llvm.memset.p0.i64(ptr nonnull align 4 %21, i8 0, i64 %23, i1 false)
  br label %24

24:                                               ; preds = %20, %16
  store i64 %8, ptr %17, align 8, !tbaa !5
  br label %.sink.split

.sink.split:                                      ; preds = %12, %3, %24
  %.sink = phi i32 [ 0, %24 ], [ 4, %3 ], [ 1, %12 ]
  %.2.ph = phi i1 [ true, %24 ], [ false, %3 ], [ false, %12 ]
  %25 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %.sink, ptr %25, align 8, !tbaa !13
  br label %26

26:                                               ; preds = %.sink.split, %9
  %.2 = phi i1 [ true, %9 ], [ %.2.ph, %.sink.split ]
  ret i1 %.2
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(1) }

!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, !10, i64 8}
!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 = !{!6, !7, i64 0}
!13 = !{!6, !11, i64 24}


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