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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @h_empty(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @h_iter_next(ptr noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.heap_iter, ptr %4, i32 0, i32 3
  %6 = load i8, ptr %5, align 1, !tbaa !9, !range !13, !noundef !14
  %7 = trunc i8 %6 to i1
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i1 false, ptr %2, align 1
  br label %35

9:                                                ; preds = %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.heap_iter, ptr %10, i32 0, i32 1
  %12 = load i64, ptr %11, align 8, !tbaa !15
  %13 = add i64 %12, 1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.heap_iter, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %17 = getelementptr inbounds %struct.heap, ptr %16, i32 0, i32 2
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = icmp eq i64 %13, %18
  br i1 %19, label %20, label %23

20:                                               ; preds = %9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.heap_iter, ptr %21, i32 0, i32 3
  store i8 1, ptr %22, align 1, !tbaa !9
  store i1 false, ptr %2, align 1
  br label %35

23:                                               ; preds = %9
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.heap_iter, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !16
  %27 = call zeroext i1 @h_empty(ptr noundef %26)
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.heap_iter, ptr %28, i32 0, i32 2
  %30 = zext i1 %27 to i8
  store i8 %30, ptr %29, align 8, !tbaa !20
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.heap_iter, ptr %31, i32 0, i32 1
  %33 = load i64, ptr %32, align 8, !tbaa !15
  %34 = add i64 %33, 1
  store i64 %34, ptr %32, align 8, !tbaa !15
  store i1 true, ptr %2, align 1
  br label %35

35:                                               ; preds = %23, %20, %8
  %36 = load i1, ptr %2, align 1
  ret i1 %36
}

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

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


; 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 zeroext i1 @h_empty(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @h_iter_next(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 17
  %3 = load i8, ptr %2, align 1, !tbaa !5, !range !12, !noundef !13
  %4 = trunc nuw i8 %3 to i1
  br i1 %4, label %20, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !14
  %8 = add i64 %7, 1
  %9 = load ptr, ptr %0, align 8, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !16
  %12 = icmp eq i64 %8, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %5
  store i8 1, ptr %2, align 1, !tbaa !5
  br label %20

14:                                               ; preds = %5
  %15 = tail call zeroext i1 @h_empty(ptr noundef nonnull %9)
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %17 = zext i1 %15 to i8
  store i8 %17, ptr %16, align 8, !tbaa !19
  %18 = load i64, ptr %6, align 8, !tbaa !14
  %19 = add i64 %18, 1
  store i64 %19, ptr %6, align 8, !tbaa !14
  br label %20

20:                                               ; preds = %1, %14, %13
  %.0 = phi i1 [ false, %13 ], [ true, %14 ], [ false, %1 ]
  ret i1 %.0
}

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

!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, !11, i64 17}
!6 = !{!"heap_iter", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 17}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{i8 0, i8 2}
!13 = !{}
!14 = !{!6, !10, i64 8}
!15 = !{!6, !7, i64 0}
!16 = !{!17, !10, i64 16}
!17 = !{!"heap", !7, i64 0, !10, i64 8, !10, i64 16, !18, i64 24, !18, i64 28, !7, i64 32, !7, i64 40}
!18 = !{!"int", !8, i64 0}
!19 = !{!6, !11, i64 16}


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