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

; Function Attrs: nounwind uwtable
declare dso_local ptr @h_new_custom(i64 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local ptr @h_copy_of(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.heap, ptr %7, i32 0, i32 1
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.heap, ptr %10, i32 0, i32 3
  %12 = load i32, ptr %11, align 8, !tbaa !13
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.heap, ptr %13, i32 0, i32 5
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.heap, ptr %16, i32 0, i32 6
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  %19 = call ptr @h_new_custom(i64 noundef %9, i32 noundef %12, ptr noundef %15, ptr noundef %18, ptr noundef null)
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %25, label %22

22:                                               ; preds = %1
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.heap, ptr %23, i32 0, i32 4
  store i32 7, ptr %24, align 4, !tbaa !16
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %82

25:                                               ; preds = %1
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.heap, ptr %26, i32 0, i32 5
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = getelementptr inbounds %struct.heap_fval, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !17
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %62

32:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !19
  br label %33

33:                                               ; preds = %58, %32
  %34 = load i64, ptr %6, align 8, !tbaa !19
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.heap, ptr %35, i32 0, i32 2
  %37 = load i64, ptr %36, align 8, !tbaa !20
  %38 = icmp ult i64 %34, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %33
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %61

40:                                               ; preds = %33
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.heap, ptr %41, i32 0, i32 5
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %44 = getelementptr inbounds %struct.heap_fval, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !17
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.heap, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !21
  %49 = load i64, ptr %6, align 8, !tbaa !19
  %50 = getelementptr inbounds i32, ptr %48, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !22
  %52 = call i32 %45(i32 noundef %51)
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.heap, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !21
  %56 = load i64, ptr %6, align 8, !tbaa !19
  %57 = getelementptr inbounds i32, ptr %55, i64 %56
  store i32 %52, ptr %57, align 4, !tbaa !22
  br label %58

58:                                               ; preds = %40
  %59 = load i64, ptr %6, align 8, !tbaa !19
  %60 = add i64 %59, 1
  store i64 %60, ptr %6, align 8, !tbaa !19
  br label %33, !llvm.loop !23

61:                                               ; preds = %39
  br label %73

62:                                               ; preds = %25
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.heap, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !21
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.heap, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !21
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.heap, ptr %69, i32 0, i32 2
  %71 = load i64, ptr %70, align 8, !tbaa !20
  %72 = mul i64 4, %71
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %65, ptr align 4 %68, i64 %72, i1 false)
  br label %73

73:                                               ; preds = %62, %61
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.heap, ptr %74, i32 0, i32 2
  %76 = load i64, ptr %75, align 8, !tbaa !20
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.heap, ptr %77, i32 0, i32 2
  store i64 %76, ptr %78, align 8, !tbaa !20
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.heap, ptr %79, i32 0, i32 4
  store i32 0, ptr %80, align 4, !tbaa !16
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %81, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %82

82:                                               ; preds = %73, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %83 = load ptr, ptr %2, align 8
  ret ptr %83
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !11, i64 8}
!10 = !{!"heap", !6, i64 0, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 28, !6, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 24}
!14 = !{!10, !6, i64 32}
!15 = !{!10, !6, i64 40}
!16 = !{!10, !12, i64 28}
!17 = !{!18, !6, i64 8}
!18 = !{!"heap_fval", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!19 = !{!11, !11, i64 0}
!20 = !{!10, !11, i64 16}
!21 = !{!10, !6, i64 0}
!22 = !{!12, !12, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"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: nounwind uwtable
declare dso_local ptr @h_new_custom(i64 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @h_copy_of(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !12
  %6 = getelementptr inbounds i8, ptr %0, i64 32
  %7 = load ptr, ptr %6, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = tail call ptr @h_new_custom(i64 noundef %3, i32 noundef %5, ptr noundef %7, ptr noundef %9, ptr noundef null)
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %37, label %11

11:                                               ; preds = %1
  %12 = load ptr, ptr %6, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %.not24 = icmp eq ptr %14, null
  br i1 %.not24, label %29, label %.preheader

.preheader:                                       ; preds = %11
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !17
  %.not26 = icmp eq i64 %16, 0
  br i1 %.not26, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.025 = phi i64 [ %26, %.lr.ph ], [ 0, %.preheader ]
  %17 = load ptr, ptr %6, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = load ptr, ptr %0, align 8, !tbaa !18
  %21 = getelementptr inbounds i32, ptr %20, i64 %.025
  %22 = load i32, ptr %21, align 4, !tbaa !19
  %23 = tail call i32 %19(i32 noundef %22) #2
  %24 = load ptr, ptr %10, align 8, !tbaa !18
  %25 = getelementptr inbounds i32, ptr %24, i64 %.025
  store i32 %23, ptr %25, align 4, !tbaa !19
  %26 = add nuw i64 %.025, 1
  %27 = load i64, ptr %15, align 8, !tbaa !17
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %.lr.ph, label %.loopexit, !llvm.loop !20

29:                                               ; preds = %11
  %30 = load ptr, ptr %10, align 8, !tbaa !18
  %31 = load ptr, ptr %0, align 8, !tbaa !18
  %32 = getelementptr inbounds i8, ptr %0, i64 16
  %33 = load i64, ptr %32, align 8, !tbaa !17
  %34 = shl i64 %33, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %30, ptr align 4 %31, i64 %34, i1 false)
  %.pre = load i64, ptr %32, align 8, !tbaa !17
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %29
  %35 = phi i64 [ 0, %.preheader ], [ %.pre, %29 ], [ %27, %.lr.ph ]
  %36 = getelementptr inbounds i8, ptr %10, i64 16
  store i64 %35, ptr %36, align 8, !tbaa !17
  br label %37

37:                                               ; preds = %1, %.loopexit
  %.sink = phi i32 [ 0, %.loopexit ], [ 7, %1 ]
  %38 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 %.sink, ptr %38, align 4, !tbaa !22
  ret ptr %10
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 = { mustprogress nocallback nofree 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, !10, i64 8}
!6 = !{!"heap", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !11, i64 28, !7, i64 32, !7, i64 40}
!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, !11, i64 24}
!13 = !{!6, !7, i64 32}
!14 = !{!6, !7, i64 40}
!15 = !{!16, !7, i64 8}
!16 = !{!"heap_fval", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!17 = !{!6, !10, i64 16}
!18 = !{!6, !7, i64 0}
!19 = !{!11, !11, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!6, !11, i64 28}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass