; 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_advance(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 3
  %8 = load i8, ptr %7, align 1, !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 %60

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 = add i64 %14, 1
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.heap_iter, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !17
  %19 = getelementptr inbounds %struct.heap, ptr %18, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !18
  %21 = icmp eq i64 %15, %20
  br i1 %21, label %22, label %25

22:                                               ; preds = %11
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.heap_iter, ptr %23, i32 0, i32 3
  store i8 1, ptr %24, align 1, !tbaa !11
  store i1 false, ptr %3, align 1
  br label %60

25:                                               ; preds = %11
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %40, label %28

28:                                               ; preds = %25
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.heap_iter, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !16
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = add i64 %31, %32
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.heap_iter, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !17
  %37 = getelementptr inbounds %struct.heap, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !18
  %39 = icmp uge i64 %33, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %28, %25
  store i1 false, ptr %3, align 1
  br label %60

41:                                               ; preds = %28
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.heap_iter, ptr %42, i32 0, i32 3
  %44 = load i8, ptr %43, align 1, !tbaa !11, !range !14, !noundef !15
  %45 = trunc i8 %44 to i1
  br i1 %45, label %46, label %47

46:                                               ; preds = %41
  store i1 false, ptr %3, align 1
  br label %60

47:                                               ; preds = %41
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.heap_iter, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !17
  %51 = call zeroext i1 @h_empty(ptr noundef %50)
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.heap_iter, ptr %52, i32 0, i32 2
  %54 = zext i1 %51 to i8
  store i8 %54, ptr %53, align 8, !tbaa !21
  %55 = load i64, ptr %5, align 8, !tbaa !9
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.heap_iter, ptr %56, i32 0, i32 1
  %58 = load i64, ptr %57, align 8, !tbaa !16
  %59 = add i64 %58, %55
  store i64 %59, ptr %57, align 8, !tbaa !16
  store i1 true, ptr %3, align 1
  br label %60

60:                                               ; preds = %47, %46, %40, %22, %10
  %61 = load i1, ptr %3, align 1
  ret i1 %61
}

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 17}
!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 = !{!19, !10, i64 16}
!19 = !{!"heap", !6, i64 0, !10, i64 8, !10, i64 16, !20, i64 24, !20, i64 28, !6, i64 32, !6, i64 40}
!20 = !{!"int", !7, i64 0}
!21 = !{!12, !13, 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_advance(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 17
  %4 = load i8, ptr %3, align 1, !tbaa !5, !range !12, !noundef !13
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %24, label %6

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

14:                                               ; preds = %6
  store i8 1, ptr %3, align 1, !tbaa !5
  br label %24

15:                                               ; preds = %6
  %16 = icmp ne i64 %1, 0
  %17 = add i64 %8, %1
  %.not = icmp ult i64 %17, %12
  %or.cond = and i1 %16, %.not
  br i1 %or.cond, label %18, label %24

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

24:                                               ; preds = %15, %2, %18, %14
  %.0 = phi i1 [ false, %14 ], [ true, %18 ], [ false, %2 ], [ false, %15 ]
  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