; 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: nounwind uwtable
define dso_local ptr @bs_new_custom(i64 noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  %11 = load ptr, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = icmp ult i64 %12, 1
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  br label %59

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

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

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

30:                                               ; preds = %19
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %58

31:                                               ; preds = %19
  %32 = load ptr, ptr %6, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.cmc_alloc_node, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  %35 = load i64, ptr %8, align 8, !tbaa !5
  %36 = call ptr %34(i64 noundef %35, i64 noundef 4)
  %37 = load ptr, ptr %9, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.bitset, ptr %37, i32 0, i32 0
  store ptr %36, ptr %38, align 8, !tbaa !14
  %39 = load ptr, ptr %9, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.bitset, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = icmp ne ptr %41, null
  br i1 %42, label %48, label %43

43:                                               ; preds = %31
  %44 = load ptr, ptr %6, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.cmc_alloc_node, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !17
  %47 = load ptr, ptr %9, align 8, !tbaa !9
  call void %46(ptr noundef %47)
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %58

48:                                               ; preds = %31
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = load ptr, ptr %9, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.bitset, ptr %50, i32 0, i32 1
  store i64 %49, ptr %51, align 8, !tbaa !18
  %52 = load ptr, ptr %9, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.bitset, ptr %52, i32 0, i32 3
  store i32 0, ptr %53, align 8, !tbaa !19
  %54 = load ptr, ptr %6, align 8, !tbaa !9
  %55 = load ptr, ptr %9, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.bitset, ptr %55, i32 0, i32 4
  store ptr %54, ptr %56, align 8, !tbaa !20
  %57 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %57, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %58

58:                                               ; preds = %48, %43, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %59

59:                                               ; preds = %58, %14
  %60 = load ptr, ptr %4, align 8
  ret ptr %60
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 = !{!"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 0}
!12 = !{!"cmc_alloc_node", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 8}
!14 = !{!15, !10, i64 0}
!15 = !{!"bitset", !10, i64 0, !6, i64 8, !6, i64 16, !16, i64 24, !10, i64 32}
!16 = !{!"int", !7, i64 0}
!17 = !{!12, !10, i64 24}
!18 = !{!15, !6, i64 8}
!19 = !{!15, !16, i64 24}
!20 = !{!15, !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 }

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

; Function Attrs: nounwind uwtable
define dso_local ptr @bs_new_custom(i64 noundef %0, ptr noundef %1, ptr nocapture noundef readnone %2) local_unnamed_addr #0 {
  %4 = icmp eq i64 %0, 0
  br i1 %4, label %22, label %5

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

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %spec.store.select, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !10
  %14 = tail call ptr %13(i64 noundef %8, i64 noundef 4) #2
  store ptr %14, ptr %10, align 8, !tbaa !11
  %.not21 = icmp eq ptr %14, null
  br i1 %.not21, label %15, label %18

15:                                               ; preds = %11
  %16 = getelementptr inbounds i8, ptr %spec.store.select, i64 24
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  tail call void %17(ptr noundef nonnull %10) #2
  br label %22

18:                                               ; preds = %11
  %19 = getelementptr inbounds i8, ptr %10, i64 8
  store i64 %8, ptr %19, align 8, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %10, i64 24
  store i32 0, ptr %20, align 8, !tbaa !17
  %21 = getelementptr inbounds i8, ptr %10, i64 32
  store ptr %spec.store.select, ptr %21, align 8, !tbaa !18
  br label %22

22:                                               ; preds = %15, %18, %5, %3
  %.1 = phi ptr [ null, %3 ], [ %10, %18 ], [ null, %15 ], [ null, %5 ]
  ret ptr %.1
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @cmc_bidx_to_widx(i64 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 = { 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 }

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


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