; LLVM IR from LeoVen/C-Macro-Collections :: examples (heapsort)

; 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.heap = type { ptr, i64, i64, i32, i32, ptr, ptr }

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

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

17:                                               ; preds = %5
  store ptr null, ptr %6, align 8
  br label %77

18:                                               ; preds = %5
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = icmp ne i32 %19, -1
  br i1 %20, label %21, label %25

21:                                               ; preds = %18
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = icmp ne i32 %22, 1
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store ptr null, ptr %6, align 8
  br label %77

25:                                               ; preds = %21, %18
  %26 = load ptr, ptr %9, align 8, !tbaa !11
  %27 = icmp ne ptr %26, null
  br i1 %27, label %29, label %28

28:                                               ; preds = %25
  store ptr null, ptr %6, align 8
  br label %77

29:                                               ; preds = %25
  %30 = load ptr, ptr %10, align 8, !tbaa !11
  %31 = icmp ne ptr %30, null
  br i1 %31, label %33, label %32

32:                                               ; preds = %29
  store ptr @cmc_alloc_node_default, ptr %10, align 8, !tbaa !11
  br label %33

33:                                               ; preds = %32, %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %34 = load ptr, ptr %10, align 8, !tbaa !11
  %35 = getelementptr inbounds %struct.cmc_alloc_node, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !13
  %37 = call ptr %36(i64 noundef 48)
  store ptr %37, ptr %12, align 8, !tbaa !11
  %38 = load ptr, ptr %12, align 8, !tbaa !11
  %39 = icmp ne ptr %38, null
  br i1 %39, label %41, label %40

40:                                               ; preds = %33
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %76

41:                                               ; preds = %33
  %42 = load ptr, ptr %10, align 8, !tbaa !11
  %43 = getelementptr inbounds %struct.cmc_alloc_node, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load i64, ptr %7, align 8, !tbaa !5
  %46 = call ptr %44(i64 noundef %45, i64 noundef 4)
  %47 = load ptr, ptr %12, align 8, !tbaa !11
  %48 = getelementptr inbounds %struct.heap, ptr %47, i32 0, i32 0
  store ptr %46, ptr %48, align 8, !tbaa !16
  %49 = load ptr, ptr %12, align 8, !tbaa !11
  %50 = getelementptr inbounds %struct.heap, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !16
  %52 = icmp ne ptr %51, null
  br i1 %52, label %58, label %53

53:                                               ; preds = %41
  %54 = load ptr, ptr %10, align 8, !tbaa !11
  %55 = getelementptr inbounds %struct.cmc_alloc_node, ptr %54, i32 0, i32 3
  %56 = load ptr, ptr %55, align 8, !tbaa !18
  %57 = load ptr, ptr %12, align 8, !tbaa !11
  call void %56(ptr noundef %57)
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %76

58:                                               ; preds = %41
  %59 = load i64, ptr %7, align 8, !tbaa !5
  %60 = load ptr, ptr %12, align 8, !tbaa !11
  %61 = getelementptr inbounds %struct.heap, ptr %60, i32 0, i32 1
  store i64 %59, ptr %61, align 8, !tbaa !19
  %62 = load ptr, ptr %12, align 8, !tbaa !11
  %63 = getelementptr inbounds %struct.heap, ptr %62, i32 0, i32 2
  store i64 0, ptr %63, align 8, !tbaa !20
  %64 = load i32, ptr %8, align 4, !tbaa !9
  %65 = load ptr, ptr %12, align 8, !tbaa !11
  %66 = getelementptr inbounds %struct.heap, ptr %65, i32 0, i32 3
  store i32 %64, ptr %66, align 8, !tbaa !21
  %67 = load ptr, ptr %12, align 8, !tbaa !11
  %68 = getelementptr inbounds %struct.heap, ptr %67, i32 0, i32 4
  store i32 0, ptr %68, align 4, !tbaa !22
  %69 = load ptr, ptr %9, align 8, !tbaa !11
  %70 = load ptr, ptr %12, align 8, !tbaa !11
  %71 = getelementptr inbounds %struct.heap, ptr %70, i32 0, i32 5
  store ptr %69, ptr %71, align 8, !tbaa !23
  %72 = load ptr, ptr %10, align 8, !tbaa !11
  %73 = load ptr, ptr %12, align 8, !tbaa !11
  %74 = getelementptr inbounds %struct.heap, ptr %73, i32 0, i32 6
  store ptr %72, ptr %74, align 8, !tbaa !24
  %75 = load ptr, ptr %12, align 8, !tbaa !11
  store ptr %75, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %76

76:                                               ; preds = %58, %53, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %77

77:                                               ; preds = %76, %28, %24, %17
  %78 = load ptr, ptr %6, align 8
  ret ptr %78
}

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

; 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"cmc_alloc_node", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!15 = !{!14, !12, i64 8}
!16 = !{!17, !12, i64 0}
!17 = !{!"heap", !12, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !12, i64 32, !12, i64 40}
!18 = !{!14, !12, i64 24}
!19 = !{!17, !6, i64 8}
!20 = !{!17, !6, i64 16}
!21 = !{!17, !10, i64 24}
!22 = !{!17, !10, i64 28}
!23 = !{!17, !12, i64 32}
!24 = !{!17, !12, i64 40}


; 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 @h_new_custom(i64 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr nocapture noundef readnone %4) local_unnamed_addr #0 {
  %6 = icmp eq i64 %0, 0
  br i1 %6, label %26, label %7

7:                                                ; preds = %5
  switch i32 %1, label %26 [
    i32 -1, label %8
    i32 1, label %8
  ]

8:                                                ; preds = %7, %7
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %26, label %9

9:                                                ; preds = %8
  %.not29 = icmp eq ptr %3, null
  %spec.store.select = select i1 %.not29, ptr @cmc_alloc_node_default, ptr %3
  %10 = load ptr, ptr %spec.store.select, align 8, !tbaa !5
  %11 = tail call ptr %10(i64 noundef 48) #1
  %.not30 = icmp eq ptr %11, null
  br i1 %.not30, label %26, label %12

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %spec.store.select, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !10
  %15 = tail call ptr %14(i64 noundef %0, i64 noundef 4) #1
  store ptr %15, ptr %11, align 8, !tbaa !11
  %.not31 = icmp eq ptr %15, null
  br i1 %.not31, label %16, label %19

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

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %11, i64 8
  store i64 %0, ptr %20, align 8, !tbaa !16
  %21 = getelementptr inbounds i8, ptr %11, i64 16
  store i64 0, ptr %21, align 8, !tbaa !17
  %22 = getelementptr inbounds i8, ptr %11, i64 24
  store i32 %1, ptr %22, align 8, !tbaa !18
  %23 = getelementptr inbounds i8, ptr %11, i64 28
  store i32 0, ptr %23, align 4, !tbaa !19
  %24 = getelementptr inbounds i8, ptr %11, i64 32
  store ptr %2, ptr %24, align 8, !tbaa !20
  %25 = getelementptr inbounds i8, ptr %11, i64 40
  store ptr %spec.store.select, ptr %25, align 8, !tbaa !21
  br label %26

26:                                               ; preds = %16, %19, %9, %8, %7, %5
  %.1 = phi ptr [ null, %5 ], [ null, %7 ], [ null, %8 ], [ %11, %19 ], [ null, %16 ], [ null, %9 ]
  ret ptr %.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 = { 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 = !{!"heap", !7, i64 0, !13, i64 8, !13, i64 16, !14, i64 24, !14, i64 28, !7, i64 32, !7, i64 40}
!13 = !{!"long", !8, i64 0}
!14 = !{!"int", !8, i64 0}
!15 = !{!6, !7, i64 24}
!16 = !{!12, !13, i64 8}
!17 = !{!12, !13, i64 16}
!18 = !{!12, !14, i64 24}
!19 = !{!12, !14, i64 28}
!20 = !{!12, !7, i64 32}
!21 = !{!12, !7, i64 40}


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