; LLVM IR from aagdev/mlimgsynth :: src/ccommon (stream)

; 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.Stream = type { ptr, ptr, ptr, ptr, ptr, %struct.StreamInt, i64, i32 }
%struct.StreamInt = type { [3 x ptr] }

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_ibuffer_set(ptr noalias noundef, i64 noundef, ptr noalias noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @stream_ibuffer_increase(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i32, align 4
  %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.Stream, ptr %6, i32 0, i32 2
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = icmp ne ptr %8, null
  br i1 %9, label %14, label %10

10:                                               ; preds = %2
  %11 = load i64, ptr %5, align 8, !tbaa !9
  %12 = icmp ult i64 %11, 4096
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  store i64 4096, ptr %5, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %13, %10, %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Stream, ptr %15, i32 0, i32 2
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %17, i64 %18
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Stream, ptr %20, i32 0, i32 3
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = icmp ule ptr %19, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %14
  store i32 0, ptr %3, align 4
  br label %29

25:                                               ; preds = %14
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i64, ptr %5, align 8, !tbaa !9
  %28 = call i32 @stream_ibuffer_set(ptr noundef %26, i64 noundef %27, ptr noundef null)
  store i32 %28, ptr %3, align 4
  br label %29

29:                                               ; preds = %25, %24
  %30 = load i32, ptr %3, align 4
  ret i32 %30
}

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, !6, i64 16}
!12 = !{!"Stream", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40, !10, i64 64, !14, i64 72}
!13 = !{!"StreamInt", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 24}


; 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 i32 @stream_ibuffer_set(ptr noalias noundef, i64 noundef, ptr noalias noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @stream_ibuffer_increase(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  %6 = tail call i64 @llvm.umax.i64(i64 %1, i64 4096)
  %spec.store.select = select i1 %5, i64 %6, i64 %1
  %7 = getelementptr inbounds i8, ptr %4, i64 %spec.store.select
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  %.not = icmp ugt ptr %7, %9
  br i1 %.not, label %10, label %12

10:                                               ; preds = %2
  %11 = tail call i32 @stream_ibuffer_set(ptr noundef nonnull %0, i64 noundef %spec.store.select, ptr noundef null)
  br label %12

12:                                               ; preds = %2, %10
  %.0 = phi i32 [ %11, %10 ], [ 0, %2 ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #1

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !7, i64 16}
!6 = !{!"Stream", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !11, i64 64, !12, i64 72}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"StreamInt", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !7, i64 24}


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