; 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.cmc_alloc_node = type { ptr, ptr, ptr, ptr }
%struct.bitset = type { ptr, i64, i64, i32, ptr }

@cmc_alloc_node_default = external hidden global %struct.cmc_alloc_node, align 8

; 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
define dso_local void @bs_init_custom(ptr dead_on_unwind noalias writable sret(%struct.bitset) align 8 %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  store ptr %3, ptr %7, align 8, !tbaa !9
  %10 = load ptr, ptr %7, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 8 %0, i8 0, i64 40, i1 false)
  %11 = load i64, ptr %5, align 8, !tbaa !5
  %12 = icmp ult i64 %11, 1
  br i1 %12, label %13, label %14

13:                                               ; preds = %4
  br label %40

14:                                               ; preds = %4
  %15 = load ptr, ptr %6, align 8, !tbaa !9
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store ptr @cmc_alloc_node_default, ptr %6, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %17, %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = sub i64 %19, 1
  %21 = call i64 @cmc_bidx_to_widx(i64 noundef %20)
  %22 = add i64 %21, 1
  store i64 %22, ptr %8, align 8, !tbaa !5
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.cmc_alloc_node, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = load i64, ptr %8, align 8, !tbaa !5
  %27 = call ptr %25(i64 noundef %26, i64 noundef 4)
  %28 = getelementptr inbounds %struct.bitset, ptr %0, i32 0, i32 0
  store ptr %27, ptr %28, align 8, !tbaa !13
  %29 = getelementptr inbounds %struct.bitset, ptr %0, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = icmp ne ptr %30, null
  br i1 %31, label %33, label %32

32:                                               ; preds = %18
  store i32 1, ptr %9, align 4
  br label %39

33:                                               ; preds = %18
  %34 = load i64, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.bitset, ptr %0, i32 0, i32 1
  store i64 %34, ptr %35, align 8, !tbaa !16
  %36 = getelementptr inbounds %struct.bitset, ptr %0, i32 0, i32 3
  store i32 0, ptr %36, align 8, !tbaa !17
  %37 = load ptr, ptr %6, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.bitset, ptr %0, i32 0, i32 4
  store ptr %37, ptr %38, align 8, !tbaa !18
  store i32 1, ptr %9, align 4
  br label %39

39:                                               ; preds = %33, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %40

40:                                               ; preds = %39, %13
  ret void
}

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!12, !10, i64 8}
!12 = !{!"cmc_alloc_node", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!14, !10, i64 0}
!14 = !{!"bitset", !10, i64 0, !6, i64 8, !6, i64 16, !15, i64 24, !10, i64 32}
!15 = !{!"int", !7, i64 0}
!16 = !{!14, !6, i64 8}
!17 = !{!14, !15, i64 24}
!18 = !{!14, !10, i64 32}


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

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

@cmc_alloc_node_default = external hidden global %struct.cmc_alloc_node, align 8

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

; Function Attrs: nounwind uwtable
define dso_local void @bs_init_custom(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.bitset) align 8 %0, i64 noundef %1, ptr noundef %2, ptr nocapture noundef readnone %3) local_unnamed_addr #1 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %0, i8 0, i64 40, i1 false)
  %5 = icmp eq i64 %1, 0
  br i1 %5, label %17, label %6

6:                                                ; preds = %4
  %.not = icmp eq ptr %2, null
  %spec.store.select = select i1 %.not, ptr @cmc_alloc_node_default, ptr %2
  %7 = add i64 %1, -1
  %8 = tail call i64 @cmc_bidx_to_widx(i64 noundef %7)
  %9 = add i64 %8, 1
  %10 = getelementptr inbounds i8, ptr %spec.store.select, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = tail call ptr %11(i64 noundef %9, i64 noundef 4) #3
  store ptr %12, ptr %0, align 8, !tbaa !10
  %.not7 = icmp eq ptr %12, null
  br i1 %.not7, label %17, label %13

13:                                               ; preds = %6
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %9, ptr %14, align 8, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 0, ptr %15, align 8, !tbaa !15
  %16 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %spec.store.select, ptr %16, align 8, !tbaa !16
  br label %17

17:                                               ; preds = %13, %6, %4
  ret void
}

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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, !7, i64 8}
!6 = !{!"cmc_alloc_node", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !7, i64 0}
!11 = !{!"bitset", !7, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !7, i64 32}
!12 = !{!"long", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!11, !12, i64 8}
!15 = !{!11, !13, i64 24}
!16 = !{!11, !7, i64 32}


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