; LLVM IR from IOsonata/IOsonata :: src (cfifo)

; 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.__CFIFO_Header = type <{ i32, i32, i32, i8, [3 x i8], i32, 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 ptr @CFifoGet(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @CFifoGetMultiple(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @CFifoRead(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %23, label %15

15:                                               ; preds = %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.__CFIFO_Header, ptr %16, i32 0, i32 1
  %18 = load volatile i32, ptr %17, align 4, !tbaa !11
  %19 = icmp slt i32 %18, 0
  br i1 %19, label %23, label %20

20:                                               ; preds = %15
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %20, %15, %3
  store i32 0, ptr %4, align 4
  br label %97

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.__CFIFO_Header, ptr %26, i32 0, i32 6
  %28 = load i32, ptr %27, align 4, !tbaa !14
  %29 = icmp sle i32 %25, %28
  br i1 %29, label %30, label %45

30:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = call ptr @CFifoGet(ptr noundef %31)
  store ptr %32, ptr %9, align 8, !tbaa !5
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %41

35:                                               ; preds = %30
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %36, ptr align 1 %37, i64 %39, i1 false)
  %40 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %40, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %42

41:                                               ; preds = %30
  store i32 0, ptr %10, align 4
  br label %42

42:                                               ; preds = %41, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %43 = load i32, ptr %10, align 4
  switch i32 %43, label %96 [
    i32 0, label %44
  ]

44:                                               ; preds = %42
  br label %94

45:                                               ; preds = %24
  br label %46

46:                                               ; preds = %92, %45
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = icmp sgt i32 %47, 0
  br i1 %48, label %49, label %93

49:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.__CFIFO_Header, ptr %51, i32 0, i32 6
  %53 = load i32, ptr %52, align 4, !tbaa !14
  %54 = udiv i32 %50, %53
  store i32 %54, ptr %11, align 4, !tbaa !9
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.__CFIFO_Header, ptr %56, i32 0, i32 6
  %58 = load i32, ptr %57, align 4, !tbaa !14
  %59 = urem i32 %55, %58
  %60 = icmp ugt i32 %59, 0
  br i1 %60, label %61, label %64

61:                                               ; preds = %49
  %62 = load i32, ptr %11, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %11, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %61, %49
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = call ptr @CFifoGetMultiple(ptr noundef %65, ptr noundef %11)
  store ptr %66, ptr %12, align 8, !tbaa !5
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = icmp eq ptr %67, null
  br i1 %68, label %69, label %70

69:                                               ; preds = %64
  store i32 3, ptr %10, align 4
  br label %90

70:                                               ; preds = %64
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.__CFIFO_Header, ptr %71, i32 0, i32 6
  %73 = load i32, ptr %72, align 4, !tbaa !14
  %74 = load i32, ptr %11, align 4, !tbaa !9
  %75 = mul i32 %74, %73
  store i32 %75, ptr %11, align 4, !tbaa !9
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  %78 = load i32, ptr %11, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %76, ptr align 1 %77, i64 %79, i1 false)
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = sext i32 %80 to i64
  %83 = getelementptr inbounds i8, ptr %81, i64 %82
  store ptr %83, ptr %6, align 8, !tbaa !5
  %84 = load i32, ptr %11, align 4, !tbaa !9
  %85 = load i32, ptr %7, align 4, !tbaa !9
  %86 = sub nsw i32 %85, %84
  store i32 %86, ptr %7, align 4, !tbaa !9
  %87 = load i32, ptr %11, align 4, !tbaa !9
  %88 = load i32, ptr %8, align 4, !tbaa !9
  %89 = add nsw i32 %88, %87
  store i32 %89, ptr %8, align 4, !tbaa !9
  store i32 0, ptr %10, align 4
  br label %90

90:                                               ; preds = %70, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %91 = load i32, ptr %10, align 4
  switch i32 %91, label %99 [
    i32 0, label %92
    i32 3, label %93
  ]

92:                                               ; preds = %90
  br label %46, !llvm.loop !15

93:                                               ; preds = %90, %46
  br label %94

94:                                               ; preds = %93, %44
  %95 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %95, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %96

96:                                               ; preds = %94, %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %97

97:                                               ; preds = %96, %23
  %98 = load i32, ptr %4, align 4
  ret i32 %98

99:                                               ; preds = %90
  unreachable
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !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, !10, i64 4}
!12 = !{!"__CFIFO_Header", !10, i64 0, !10, i64 4, !10, i64 8, !13, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !6, i64 28}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!12, !10, i64 20}
!15 = distinct !{!15, !16}
!16 = !{!"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"

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

; Function Attrs: mustprogress 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 ptr @CFifoGet(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @CFifoGetMultiple(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @CFifoRead(ptr noundef %0, ptr noundef writeonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = alloca i32, align 4
  %5 = icmp eq ptr %0, null
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load volatile i32, ptr %7, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 0
  %10 = icmp eq ptr %1, null
  %or.cond = or i1 %10, %9
  br i1 %or.cond, label %.loopexit, label %11

11:                                               ; preds = %6
  %12 = getelementptr inbounds i8, ptr %0, i64 20
  %13 = load i32, ptr %12, align 4, !tbaa !12
  %.not = icmp slt i32 %13, %2
  br i1 %.not, label %.preheader, label %15

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

15:                                               ; preds = %11
  %16 = tail call ptr @CFifoGet(ptr noundef nonnull %0)
  %.not43 = icmp eq ptr %16, null
  br i1 %.not43, label %.loopexit, label %17

17:                                               ; preds = %15
  %18 = sext i32 %2 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %1, ptr nonnull align 1 %16, i64 %18, i1 false)
  br label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %25
  %.03052 = phi ptr [ %30, %25 ], [ %1, %.preheader ]
  %.03251 = phi i32 [ %32, %25 ], [ 0, %.preheader ]
  %.03550 = phi i32 [ %31, %25 ], [ %2, %.preheader ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %19 = load i32, ptr %12, align 4, !tbaa !12
  %20 = udiv i32 %.03550, %19
  %21 = urem i32 %.03550, %19
  %.not42 = icmp ne i32 %21, 0
  %22 = zext i1 %.not42 to i32
  %spec.select = add nuw nsw i32 %20, %22
  store i32 %spec.select, ptr %4, align 4, !tbaa !13
  %23 = call ptr @CFifoGetMultiple(ptr noundef nonnull %0, ptr noundef nonnull %4)
  %24 = icmp eq ptr %23, null
  br i1 %24, label %.thread, label %25

.thread:                                          ; preds = %.lr.ph
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  br label %.loopexit

25:                                               ; preds = %.lr.ph
  %26 = load i32, ptr %12, align 4, !tbaa !12
  %27 = load i32, ptr %4, align 4, !tbaa !13
  %28 = mul i32 %27, %26
  store i32 %28, ptr %4, align 4, !tbaa !13
  %29 = sext i32 %28 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.03052, ptr nonnull align 1 %23, i64 %29, i1 false)
  %30 = getelementptr inbounds i8, ptr %.03052, i64 %29
  %31 = sub nsw i32 %.03550, %28
  %32 = add nsw i32 %28, %.03251
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  %33 = icmp sgt i32 %31, 0
  br i1 %33, label %.lr.ph, label %.loopexit

.loopexit:                                        ; preds = %25, %.preheader, %.thread, %17, %15, %3, %6
  %.2 = phi i32 [ 0, %6 ], [ 0, %3 ], [ %2, %17 ], [ 0, %15 ], [ %.03251, %.thread ], [ 0, %.preheader ], [ %32, %25 ]
  ret i32 %.2
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 4}
!6 = !{!"__CFIFO_Header", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !11, i64 28}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 20}
!13 = !{!7, !7, i64 0}


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