; LLVM IR from PaulBatchelor/sndkit :: graforge (graforge)

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

; 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
declare dso_local void @gf_buffer_mark(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @gf_bufferpool_nextfree(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.gf_bufferpool, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.gf_bufferpool, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !12
  %14 = icmp sge i32 %10, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %2
  store i32 11, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

16:                                               ; preds = %2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.gf_bufferpool, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 8, !tbaa !13
  %20 = icmp sge i32 %19, 0
  br i1 %20, label %21, label %33

21:                                               ; preds = %16
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.gf_bufferpool, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.gf_bufferpool, ptr %25, i32 0, i32 3
  %27 = load i32, ptr %26, align 8, !tbaa !13
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds %struct.gf_buffer, ptr %24, i64 %28
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %29, ptr %30, align 8, !tbaa !5
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.gf_bufferpool, ptr %31, i32 0, i32 3
  store i32 -1, ptr %32, align 8, !tbaa !13
  br label %63

33:                                               ; preds = %16
  store i32 0, ptr %6, align 4, !tbaa !15
  br label %34

34:                                               ; preds = %59, %33
  %35 = load i32, ptr %6, align 4, !tbaa !15
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.gf_bufferpool, ptr %36, i32 0, i32 1
  %38 = load i32, ptr %37, align 8, !tbaa !12
  %39 = icmp slt i32 %35, %38
  br i1 %39, label %40, label %62

40:                                               ; preds = %34
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.gf_bufferpool, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %44 = load i32, ptr %6, align 4, !tbaa !15
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.gf_buffer, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.gf_buffer, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 4, !tbaa !16
  %49 = icmp eq i32 %48, 0
  br i1 %49, label %50, label %58

50:                                               ; preds = %40
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.gf_bufferpool, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !14
  %54 = load i32, ptr %6, align 4, !tbaa !15
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds %struct.gf_buffer, ptr %53, i64 %55
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %56, ptr %57, align 8, !tbaa !5
  br label %62

58:                                               ; preds = %40
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %6, align 4, !tbaa !15
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %6, align 4, !tbaa !15
  br label %34, !llvm.loop !18

62:                                               ; preds = %50, %34
  br label %63

63:                                               ; preds = %62, %21
  br label %64

64:                                               ; preds = %63
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.gf_bufferpool, ptr %65, i32 0, i32 2
  %67 = load i32, ptr %66, align 4, !tbaa !9
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %66, align 4, !tbaa !9
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = load ptr, ptr %69, align 8, !tbaa !5
  call void @gf_buffer_mark(ptr noundef %70)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

71:                                               ; preds = %64, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %72 = load i32, ptr %3, align 4
  ret i32 %72
}

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, !11, i64 12}
!10 = !{!"gf_bufferpool", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 16}
!14 = !{!10, !6, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!17, !11, i64 4}
!17 = !{!"gf_buffer", !11, i64 0, !11, i64 4, !6, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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

%struct.gf_buffer = type { i32, i32, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @gf_buffer_mark(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 12) i32 @gf_bufferpool_nextfree(ptr nocapture noundef %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 12
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %.not = icmp slt i32 %4, %6
  br i1 %.not, label %7, label %26

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %10 = icmp sgt i32 %9, -1
  br i1 %10, label %13, label %.preheader

.preheader:                                       ; preds = %7
  %11 = icmp sgt i32 %6, 0
  br i1 %11, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %12 = load ptr, ptr %0, align 8, !tbaa !13
  %wide.trip.count = zext nneg i32 %6 to i64
  br label %18

13:                                               ; preds = %7
  %14 = load ptr, ptr %0, align 8, !tbaa !13
  %15 = zext nneg i32 %9 to i64
  %16 = getelementptr inbounds %struct.gf_buffer, ptr %14, i64 %15
  store ptr %16, ptr %1, align 8, !tbaa !14
  store i32 -1, ptr %8, align 8, !tbaa !12
  br label %.loopexit

17:                                               ; preds = %18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %18, !llvm.loop !15

18:                                               ; preds = %.lr.ph, %17
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %17 ]
  %19 = getelementptr inbounds %struct.gf_buffer, ptr %12, i64 %indvars.iv
  %20 = getelementptr inbounds i8, ptr %19, i64 4
  %21 = load i32, ptr %20, align 4, !tbaa !17
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %17

23:                                               ; preds = %18
  store ptr %19, ptr %1, align 8, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %17, %.preheader, %13, %23
  %24 = add nsw i32 %4, 1
  store i32 %24, ptr %3, align 4, !tbaa !5
  %25 = load ptr, ptr %1, align 8, !tbaa !14
  tail call void @gf_buffer_mark(ptr noundef %25)
  br label %26

26:                                               ; preds = %2, %.loopexit
  %.017 = phi i32 [ 0, %.loopexit ], [ 11, %2 ]
  ret i32 %.017
}

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, !10, i64 12}
!6 = !{!"gf_bufferpool", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !10, i64 4}
!18 = !{!"gf_buffer", !10, i64 0, !10, i64 4, !7, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass