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

; 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.buffer = type { [100 x i32], i64, i32, ptr, ptr, ptr }

@__func__.b_get = external hidden unnamed_addr constant [6 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @b_get(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = call zeroext i1 @b_empty(ptr noundef %8)
  br i1 %9, label %10, label %24

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.buffer, ptr %11, i32 0, i32 2
  store i32 2, ptr %12, align 8, !tbaa !11
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.buffer, ptr %13, i32 0, i32 5
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %22

17:                                               ; preds = %10
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.buffer, ptr %18, i32 0, i32 5
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  call void %20(ptr noundef @__func__.b_get, ptr noundef %21)
  br label %22

22:                                               ; preds = %17, %10
  store i32 0, ptr %6, align 4, !tbaa !15
  %23 = load i32, ptr %6, align 4, !tbaa !15
  store i32 %23, ptr %3, align 4
  br label %62

24:                                               ; preds = %2
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.buffer, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !16
  %29 = icmp uge i64 %25, %28
  br i1 %29, label %30, label %44

30:                                               ; preds = %24
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.buffer, ptr %31, i32 0, i32 2
  store i32 5, ptr %32, align 8, !tbaa !11
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.buffer, ptr %33, i32 0, i32 5
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %42

37:                                               ; preds = %30
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.buffer, ptr %38, i32 0, i32 5
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  call void %40(ptr noundef @__func__.b_get, ptr noundef %41)
  br label %42

42:                                               ; preds = %37, %30
  store i32 0, ptr %7, align 4, !tbaa !15
  %43 = load i32, ptr %7, align 4, !tbaa !15
  store i32 %43, ptr %3, align 4
  br label %62

44:                                               ; preds = %24
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.buffer, ptr %45, i32 0, i32 2
  store i32 0, ptr %46, align 8, !tbaa !11
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.buffer, ptr %47, i32 0, i32 5
  %49 = load ptr, ptr %48, align 8, !tbaa !14
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %56

51:                                               ; preds = %44
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.buffer, ptr %52, i32 0, i32 5
  %54 = load ptr, ptr %53, align 8, !tbaa !14
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  call void %54(ptr noundef @__func__.b_get, ptr noundef %55)
  br label %56

56:                                               ; preds = %51, %44
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.buffer, ptr %57, i32 0, i32 0
  %59 = load i64, ptr %5, align 8, !tbaa !9
  %60 = getelementptr inbounds [100 x i32], ptr %58, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !15
  store i32 %61, ptr %3, align 4
  br label %62

62:                                               ; preds = %56, %42, %22
  %63 = load i32, ptr %3, align 4
  ret i32 %63
}

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 408}
!12 = !{!"buffer", !7, i64 0, !10, i64 400, !13, i64 408, !6, i64 416, !6, i64 424, !6, i64 432}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 432}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !10, i64 400}


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

@__func__.b_get = external hidden unnamed_addr constant [6 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @b_get(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = tail call zeroext i1 @b_empty(ptr noundef %0)
  br i1 %3, label %4, label %9

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 408
  store i32 2, ptr %5, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 432
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %.not25 = icmp eq ptr %7, null
  br i1 %.not25, label %23, label %8

8:                                                ; preds = %4
  tail call void %7(ptr noundef nonnull @__func__.b_get, ptr noundef nonnull %0) #1
  br label %23

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 400
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %.not = icmp ugt i64 %11, %1
  %12 = getelementptr inbounds i8, ptr %0, i64 408
  %13 = getelementptr inbounds i8, ptr %0, i64 432
  br i1 %.not, label %17, label %14

14:                                               ; preds = %9
  store i32 5, ptr %12, align 8, !tbaa !5
  %15 = load ptr, ptr %13, align 8, !tbaa !12
  %.not24 = icmp eq ptr %15, null
  br i1 %.not24, label %23, label %16

16:                                               ; preds = %14
  tail call void %15(ptr noundef nonnull @__func__.b_get, ptr noundef nonnull %0) #1
  br label %23

17:                                               ; preds = %9
  store i32 0, ptr %12, align 8, !tbaa !5
  %18 = load ptr, ptr %13, align 8, !tbaa !12
  %.not23 = icmp eq ptr %18, null
  br i1 %.not23, label %20, label %19

19:                                               ; preds = %17
  tail call void %18(ptr noundef nonnull @__func__.b_get, ptr noundef nonnull %0) #1
  br label %20

20:                                               ; preds = %19, %17
  %21 = getelementptr inbounds [100 x i32], ptr %0, i64 0, i64 %1
  %22 = load i32, ptr %21, align 4, !tbaa !14
  br label %23

23:                                               ; preds = %14, %16, %4, %8, %20
  %.0 = phi i32 [ %22, %20 ], [ 0, %8 ], [ 0, %4 ], [ 0, %16 ], [ 0, %14 ]
  ret i32 %.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" }
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, !10, i64 408}
!6 = !{!"buffer", !7, i64 0, !9, i64 400, !10, i64 408, !11, i64 416, !11, i64 424, !11, i64 432}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!6, !11, i64 432}
!13 = !{!6, !9, i64 400}
!14 = !{!10, !10, i64 0}


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