; LLVM IR from matz/streem :: src (core)

; 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.strm_stream = type { i32, i32, i32, ptr, ptr, ptr, ptr, ptr, i64, i64, ptr, i32, ptr, i32 }

@stream_count = external hidden global i32, align 4

; 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 @strm_task_push(ptr noundef, ptr noundef, i64 noundef) #1

declare i64 @strm_nil_value() #2

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

11:                                               ; preds = %1
  store i32 1, ptr %4, align 4
  br label %90

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.strm_stream, ptr %13, i32 0, i32 11
  %15 = atomicrmw sub ptr %14, i32 1 seq_cst, align 8
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.strm_stream, ptr %16, i32 0, i32 11
  %18 = load i32, ptr %17, align 8, !tbaa !14
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %12
  store i32 1, ptr %4, align 4
  br label %90

21:                                               ; preds = %12
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.strm_stream, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %3, align 4, !tbaa !13
  %25 = cmpxchg ptr %23, i32 %24, i32 4 seq_cst seq_cst, align 8
  %26 = extractvalue { i32, i1 } %25, 1
  br i1 %26, label %28, label %27

27:                                               ; preds = %21
  store i32 1, ptr %4, align 4
  br label %90

28:                                               ; preds = %21
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.strm_stream, ptr %29, i32 0, i32 4
  %31 = load ptr, ptr %30, align 8, !tbaa !15
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %43

33:                                               ; preds = %28
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.strm_stream, ptr %34, i32 0, i32 4
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = call i64 @strm_nil_value()
  %39 = call i32 %36(ptr noundef %37, i64 noundef %38)
  %40 = icmp eq i32 %39, 1
  br i1 %40, label %41, label %42

41:                                               ; preds = %33
  store i32 1, ptr %4, align 4
  br label %90

42:                                               ; preds = %33
  br label %49

43:                                               ; preds = %28
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.strm_stream, ptr %44, i32 0, i32 5
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  call void @free(ptr noundef %46) #4
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.strm_stream, ptr %47, i32 0, i32 5
  store ptr null, ptr %48, align 8, !tbaa !16
  br label %49

49:                                               ; preds = %43, %42
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.strm_stream, ptr %50, i32 0, i32 6
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %59

54:                                               ; preds = %49
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.strm_stream, ptr %55, i32 0, i32 6
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = call i64 @strm_nil_value()
  call void @strm_task_push(ptr noundef %57, ptr noundef @strm_stream_close, i64 noundef %58)
  br label %59

59:                                               ; preds = %54, %49
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.strm_stream, ptr %60, i32 0, i32 7
  %62 = load ptr, ptr %61, align 8, !tbaa !18
  %63 = icmp ne ptr %62, null
  br i1 %63, label %64, label %88

64:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %65

65:                                               ; preds = %81, %64
  %66 = load i32, ptr %5, align 4, !tbaa !13
  %67 = sext i32 %66 to i64
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.strm_stream, ptr %68, i32 0, i32 8
  %70 = load i64, ptr %69, align 8, !tbaa !19
  %71 = icmp ult i64 %67, %70
  br i1 %71, label %72, label %84

72:                                               ; preds = %65
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.strm_stream, ptr %73, i32 0, i32 7
  %75 = load ptr, ptr %74, align 8, !tbaa !18
  %76 = load i32, ptr %5, align 4, !tbaa !13
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds ptr, ptr %75, i64 %77
  %79 = load ptr, ptr %78, align 8, !tbaa !5
  %80 = call i64 @strm_nil_value()
  call void @strm_task_push(ptr noundef %79, ptr noundef @strm_stream_close, i64 noundef %80)
  br label %81

81:                                               ; preds = %72
  %82 = load i32, ptr %5, align 4, !tbaa !13
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %5, align 4, !tbaa !13
  br label %65, !llvm.loop !20

84:                                               ; preds = %65
  %85 = load ptr, ptr %2, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.strm_stream, ptr %85, i32 0, i32 7
  %87 = load ptr, ptr %86, align 8, !tbaa !18
  call void @free(ptr noundef %87) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %88

88:                                               ; preds = %84, %59
  %89 = atomicrmw sub ptr @stream_count, i32 1 seq_cst, align 4
  store i32 0, ptr %4, align 4
  br label %90

90:                                               ; preds = %88, %41, %27, %20, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  %91 = load i32, ptr %4, align 4
  switch i32 %91, label %93 [
    i32 0, label %92
    i32 1, label %92
  ]

92:                                               ; preds = %90, %90
  ret void

93:                                               ; preds = %90
  unreachable
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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 = { "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 #3 = { nounwind "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 #4 = { 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 8}
!10 = !{!"strm_stream", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !12, i64 64, !6, i64 72, !11, i64 80, !6, i64 88, !11, i64 96}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 80}
!15 = !{!10, !6, i64 24}
!16 = !{!10, !6, i64 32}
!17 = !{!10, !6, i64 40}
!18 = !{!10, !6, i64 48}
!19 = !{!10, !12, i64 56}
!20 = distinct !{!20, !21}
!21 = !{!"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"

@stream_count = external hidden global i32, align 4

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

declare i64 @strm_nil_value() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @strm_stream_close(ptr noundef %0) #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp eq i32 %3, 4
  br i1 %4, label %42, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 80
  %7 = atomicrmw sub ptr %6, i32 1 seq_cst, align 8
  %8 = load i32, ptr %6, align 8, !tbaa !12
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %42, label %10

10:                                               ; preds = %5
  %11 = cmpxchg ptr %2, i32 %3, i32 4 seq_cst seq_cst, align 8
  %12 = extractvalue { i32, i1 } %11, 1
  br i1 %12, label %13, label %42

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 24
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %.not = icmp eq ptr %15, null
  br i1 %.not, label %20, label %16

16:                                               ; preds = %13
  %17 = tail call i64 @strm_nil_value() #3
  %18 = tail call i32 %15(ptr noundef nonnull %0, i64 noundef %17) #3
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %42, label %23

20:                                               ; preds = %13
  %21 = getelementptr inbounds i8, ptr %0, i64 32
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  tail call void @free(ptr noundef %22) #3
  store ptr null, ptr %21, align 8, !tbaa !14
  br label %23

23:                                               ; preds = %16, %20
  %24 = getelementptr inbounds i8, ptr %0, i64 40
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %.not23 = icmp eq ptr %25, null
  br i1 %.not23, label %28, label %26

26:                                               ; preds = %23
  %27 = tail call i64 @strm_nil_value() #3
  tail call void @strm_task_push(ptr noundef nonnull %25, ptr noundef nonnull @strm_stream_close, i64 noundef %27)
  br label %28

28:                                               ; preds = %26, %23
  %29 = getelementptr inbounds i8, ptr %0, i64 48
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %.not24 = icmp eq ptr %30, null
  br i1 %.not24, label %40, label %.preheader

.preheader:                                       ; preds = %28
  %31 = getelementptr inbounds i8, ptr %0, i64 56
  %32 = load i64, ptr %31, align 8, !tbaa !17
  %.not26 = icmp eq i64 %32, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader ]
  %33 = load ptr, ptr %29, align 8, !tbaa !16
  %34 = getelementptr inbounds ptr, ptr %33, i64 %indvars.iv
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  %36 = tail call i64 @strm_nil_value() #3
  tail call void @strm_task_push(ptr noundef %35, ptr noundef nonnull @strm_stream_close, i64 noundef %36)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %37 = load i64, ptr %31, align 8, !tbaa !17
  %38 = icmp ugt i64 %37, %indvars.iv.next
  br i1 %38, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !19

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %29, align 8, !tbaa !16
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %39 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %30, %.preheader ]
  tail call void @free(ptr noundef %39) #3
  br label %40

40:                                               ; preds = %._crit_edge, %28
  %41 = atomicrmw sub ptr @stream_count, i32 1 seq_cst, align 4
  br label %42

42:                                               ; preds = %16, %10, %5, %1, %40
  ret void
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

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 = { "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #3 = { 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, !7, i64 8}
!6 = !{!"strm_stream", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !11, i64 56, !11, i64 64, !10, i64 72, !7, i64 80, !10, i64 88, !7, i64 96}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !7, i64 80}
!13 = !{!6, !10, i64 24}
!14 = !{!6, !10, i64 32}
!15 = !{!6, !10, i64 40}
!16 = !{!6, !10, i64 48}
!17 = !{!6, !11, i64 56}
!18 = !{!10, !10, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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