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

; 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_rewind(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.heap_iter, ptr %6, i32 0, i32 2
  %8 = load i8, ptr %7, align 8, !tbaa !11, !range !14, !noundef !15
  %9 = trunc i8 %8 to i1
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  store i1 false, ptr %3, align 1
  br label %42

11:                                               ; preds = %2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.heap_iter, ptr %12, i32 0, i32 1
  %14 = load i64, ptr %13, align 8, !tbaa !16
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %11
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.heap_iter, ptr %17, i32 0, i32 2
  store i8 1, ptr %18, align 8, !tbaa !11
  store i1 false, ptr %3, align 1
  br label %42

19:                                               ; preds = %11
  %20 = load i64, ptr %5, align 8, !tbaa !9
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %28, label %22

22:                                               ; preds = %19
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.heap_iter, ptr %23, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !16
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = icmp ult i64 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %22, %19
  store i1 false, ptr %3, align 1
  br label %42

29:                                               ; preds = %22
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.heap_iter, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !17
  %33 = call zeroext i1 @h_empty(ptr noundef %32)
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.heap_iter, ptr %34, i32 0, i32 3
  %36 = zext i1 %33 to i8
  store i8 %36, ptr %35, align 1, !tbaa !18
  %37 = load i64, ptr %5, align 8, !tbaa !9
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.heap_iter, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !16
  %41 = sub i64 %40, %37
  store i64 %41, ptr %39, align 8, !tbaa !16
  store i1 true, ptr %3, align 1
  br label %42

42:                                               ; preds = %29, %28, %16, %10
  %43 = load i1, ptr %3, align 1
  ret i1 %43
}

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


; 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_rewind(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i8, ptr %3, align 8, !tbaa !5, !range !12, !noundef !13
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %20, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !14
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %6
  store i8 1, ptr %3, align 8, !tbaa !5
  br label %20

11:                                               ; preds = %6
  %12 = add i64 %1, -1
  %or.cond.not = icmp ult i64 %12, %8
  br i1 %or.cond.not, label %13, label %20

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

20:                                               ; preds = %11, %2, %13, %10
  %.0 = phi i1 [ false, %10 ], [ true, %13 ], [ false, %2 ], [ false, %11 ]
  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 16}
!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 = !{!6, !11, i64 17}


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