; 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 }
%struct.buffer_fval = type { ptr, ptr, ptr, ptr, ptr, ptr }

@__func__.b_index_of = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @b_index_of(ptr noundef %0, i32 noundef %1, i1 noundef zeroext %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  %12 = zext i1 %2 to i8
  store i8 %12, ptr %7, align 1, !tbaa !11
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.buffer, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %16 = icmp eq ptr %15, null
  br i1 %16, label %24, label %17

17:                                               ; preds = %3
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.buffer, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = getelementptr inbounds %struct.buffer_fval, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !16
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %27

24:                                               ; preds = %17, %3
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.buffer, ptr %25, i32 0, i32 2
  store i32 11, ptr %26, align 8, !tbaa !18
  store i64 0, ptr %4, align 8
  br label %109

27:                                               ; preds = %17
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.buffer, ptr %28, i32 0, i32 2
  store i32 0, ptr %29, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.buffer, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !19
  store i64 %32, ptr %8, align 8, !tbaa !20
  %33 = load i8, ptr %7, align 1, !tbaa !11, !range !21, !noundef !22
  %34 = trunc i8 %33 to i1
  br i1 %34, label %35, label %65

35:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !20
  br label %36

36:                                               ; preds = %60, %35
  %37 = load i64, ptr %9, align 8, !tbaa !20
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.buffer, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !19
  %41 = icmp ult i64 %37, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %36
  store i32 2, ptr %10, align 4
  br label %63

43:                                               ; preds = %36
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.buffer, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  %47 = getelementptr inbounds %struct.buffer_fval, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !16
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.buffer, ptr %49, i32 0, i32 0
  %51 = load i64, ptr %9, align 8, !tbaa !20
  %52 = getelementptr inbounds [100 x i32], ptr %50, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = call i32 %48(i32 noundef %53, i32 noundef %54)
  %56 = icmp eq i32 0, %55
  br i1 %56, label %57, label %59

57:                                               ; preds = %43
  %58 = load i64, ptr %9, align 8, !tbaa !20
  store i64 %58, ptr %8, align 8, !tbaa !20
  store i32 2, ptr %10, align 4
  br label %63

59:                                               ; preds = %43
  br label %60

60:                                               ; preds = %59
  %61 = load i64, ptr %9, align 8, !tbaa !20
  %62 = add i64 %61, 1
  store i64 %62, ptr %9, align 8, !tbaa !20
  br label %36, !llvm.loop !23

63:                                               ; preds = %57, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %64

64:                                               ; preds = %63
  br label %97

65:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.buffer, ptr %66, i32 0, i32 1
  %68 = load i64, ptr %67, align 8, !tbaa !19
  store i64 %68, ptr %11, align 8, !tbaa !20
  br label %69

69:                                               ; preds = %92, %65
  %70 = load i64, ptr %11, align 8, !tbaa !20
  %71 = icmp ugt i64 %70, 0
  br i1 %71, label %73, label %72

72:                                               ; preds = %69
  store i32 5, ptr %10, align 4
  br label %95

73:                                               ; preds = %69
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.buffer, ptr %74, i32 0, i32 3
  %76 = load ptr, ptr %75, align 8, !tbaa !13
  %77 = getelementptr inbounds %struct.buffer_fval, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !16
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.buffer, ptr %79, i32 0, i32 0
  %81 = load i64, ptr %11, align 8, !tbaa !20
  %82 = sub i64 %81, 1
  %83 = getelementptr inbounds [100 x i32], ptr %80, i64 0, i64 %82
  %84 = load i32, ptr %83, align 4, !tbaa !9
  %85 = load i32, ptr %6, align 4, !tbaa !9
  %86 = call i32 %78(i32 noundef %84, i32 noundef %85)
  %87 = icmp eq i32 0, %86
  br i1 %87, label %88, label %91

88:                                               ; preds = %73
  %89 = load i64, ptr %11, align 8, !tbaa !20
  %90 = sub i64 %89, 1
  store i64 %90, ptr %8, align 8, !tbaa !20
  store i32 5, ptr %10, align 4
  br label %95

91:                                               ; preds = %73
  br label %92

92:                                               ; preds = %91
  %93 = load i64, ptr %11, align 8, !tbaa !20
  %94 = add i64 %93, -1
  store i64 %94, ptr %11, align 8, !tbaa !20
  br label %69, !llvm.loop !25

95:                                               ; preds = %88, %72
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %96

96:                                               ; preds = %95
  br label %97

97:                                               ; preds = %96, %64
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.buffer, ptr %98, i32 0, i32 5
  %100 = load ptr, ptr %99, align 8, !tbaa !26
  %101 = icmp ne ptr %100, null
  br i1 %101, label %102, label %107

102:                                              ; preds = %97
  %103 = load ptr, ptr %5, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.buffer, ptr %103, i32 0, i32 5
  %105 = load ptr, ptr %104, align 8, !tbaa !26
  %106 = load ptr, ptr %5, align 8, !tbaa !5
  call void %105(ptr noundef @__func__.b_index_of, ptr noundef %106)
  br label %107

107:                                              ; preds = %102, %97
  %108 = load i64, ptr %8, align 8, !tbaa !20
  store i64 %108, ptr %4, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %109

109:                                              ; preds = %107, %24
  %110 = load i64, ptr %4, align 8
  ret i64 %110
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !6, i64 416}
!14 = !{!"buffer", !7, i64 0, !15, i64 400, !10, i64 408, !6, i64 416, !6, i64 424, !6, i64 432}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !6, i64 0}
!17 = !{!"buffer_fval", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!18 = !{!14, !10, i64 408}
!19 = !{!14, !15, i64 400}
!20 = !{!15, !15, i64 0}
!21 = !{i8 0, i8 2}
!22 = !{}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = distinct !{!25, !24}
!26 = !{!14, !6, i64 432}


; 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_index_of = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @b_index_of(ptr noundef %0, i32 noundef %1, i1 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 416
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %10, label %7

7:                                                ; preds = %3
  %8 = load ptr, ptr %5, align 8, !tbaa !12
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %12

10:                                               ; preds = %7, %3
  %11 = getelementptr inbounds i8, ptr %0, i64 408
  store i32 11, ptr %11, align 8, !tbaa !14
  br label %37

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %0, i64 408
  store i32 0, ptr %13, align 8, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %0, i64 400
  %15 = load i64, ptr %14, align 8, !tbaa !15
  br i1 %2, label %.preheader, label %.preheader32

.preheader:                                       ; preds = %12
  %.not38 = icmp eq i64 %15, 0
  br i1 %.not38, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %22
  %.02635 = phi i64 [ %23, %22 ], [ 0, %.preheader ]
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = getelementptr inbounds [100 x i32], ptr %0, i64 0, i64 %.02635
  %19 = load i32, ptr %18, align 4, !tbaa !16
  %20 = tail call i32 %17(i32 noundef %19, i32 noundef %1) #1
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %.loopexit, label %22

22:                                               ; preds = %.lr.ph
  %23 = add nuw i64 %.02635, 1
  %24 = load i64, ptr %14, align 8, !tbaa !15
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %.lr.ph, label %.loopexit, !llvm.loop !17

.preheader32:                                     ; preds = %12, %26
  %.0 = phi i64 [ %29, %26 ], [ %15, %12 ]
  %.not = icmp eq i64 %.0, 0
  br i1 %.not, label %.loopexit, label %26

26:                                               ; preds = %.preheader32
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load ptr, ptr %27, align 8, !tbaa !12
  %29 = add i64 %.0, -1
  %30 = getelementptr inbounds [100 x i32], ptr %0, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !16
  %32 = tail call i32 %28(i32 noundef %31, i32 noundef %1) #1
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %.loopexit, label %.preheader32, !llvm.loop !19

.loopexit:                                        ; preds = %.preheader32, %26, %.lr.ph, %22, %.preheader
  %.2 = phi i64 [ 0, %.preheader ], [ %.02635, %.lr.ph ], [ %15, %22 ], [ %29, %26 ], [ %15, %.preheader32 ]
  %34 = getelementptr inbounds i8, ptr %0, i64 432
  %35 = load ptr, ptr %34, align 8, !tbaa !20
  %.not31 = icmp eq ptr %35, null
  br i1 %.not31, label %37, label %36

36:                                               ; preds = %.loopexit
  tail call void %35(ptr noundef nonnull @__func__.b_index_of, ptr noundef nonnull %0) #1
  br label %37

37:                                               ; preds = %.loopexit, %36, %10
  %.028 = phi i64 [ 0, %10 ], [ %.2, %36 ], [ %.2, %.loopexit ]
  ret i64 %.028
}

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, !11, i64 416}
!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 = !{!13, !11, i64 0}
!13 = !{!"buffer_fval", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40}
!14 = !{!6, !10, i64 408}
!15 = !{!6, !9, i64 400}
!16 = !{!10, !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = !{!6, !11, i64 432}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass