; 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
define dso_local ptr @CFifoGet(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %15, label %10

10:                                               ; preds = %1
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.__CFIFO_Header, ptr %11, i32 0, i32 1
  %13 = load volatile i32, ptr %12, align 4, !tbaa !9
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %10, %1
  store ptr null, ptr %2, align 8
  br label %51

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.__CFIFO_Header, ptr %17, i32 0, i32 1
  %19 = load volatile i32, ptr %18, align 4, !tbaa !9
  store i32 %19, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %20 = load i32, ptr %4, align 4, !tbaa !13
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %5, align 4, !tbaa !13
  %22 = load i32, ptr %5, align 4, !tbaa !13
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.__CFIFO_Header, ptr %23, i32 0, i32 2
  %25 = load i32, ptr %24, align 4, !tbaa !14
  %26 = icmp sge i32 %22, %25
  br i1 %26, label %27, label %28

27:                                               ; preds = %16
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %28

28:                                               ; preds = %27, %16
  %29 = load i32, ptr %5, align 4, !tbaa !13
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.__CFIFO_Header, ptr %30, i32 0, i32 0
  %32 = load volatile i32, ptr %31, align 4, !tbaa !15
  %33 = icmp eq i32 %29, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %28
  store i32 -1, ptr %5, align 4, !tbaa !13
  br label %35

35:                                               ; preds = %34, %28
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.__CFIFO_Header, ptr %36, i32 0, i32 1
  %38 = load i32, ptr %5, align 4, !tbaa !13
  store i32 %38, ptr %6, align 4, !tbaa !13
  %39 = load i32, ptr %6, align 4
  store atomic i32 %39, ptr %37 seq_cst, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.__CFIFO_Header, ptr %40, i32 0, i32 8
  %42 = load ptr, ptr %41, align 4, !tbaa !16
  %43 = load i32, ptr %4, align 4, !tbaa !13
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.__CFIFO_Header, ptr %44, i32 0, i32 6
  %46 = load i32, ptr %45, align 4, !tbaa !17
  %47 = mul i32 %43, %46
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %42, i64 %48
  store ptr %49, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %50, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %51

51:                                               ; preds = %35, %15
  %52 = load ptr, ptr %2, align 8
  ret ptr %52
}

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 4}
!10 = !{!"__CFIFO_Header", !11, i64 0, !11, i64 4, !11, i64 8, !12, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !6, i64 28}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 8}
!15 = !{!10, !11, i64 0}
!16 = !{!10, !6, i64 28}
!17 = !{!10, !11, i64 20}


; 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 nofree norecurse nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) uwtable
define dso_local ptr @CFifoGet(ptr noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %21, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load volatile i32, ptr %4, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 0
  br i1 %6, label %21, label %7

7:                                                ; preds = %3
  %8 = load volatile i32, ptr %4, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %.not = icmp slt i32 %9, %11
  %spec.store.select = select i1 %.not, i32 %9, i32 0
  %12 = load volatile i32, ptr %0, align 4, !tbaa !13
  %13 = icmp eq i32 %spec.store.select, %12
  %spec.select = select i1 %13, i32 -1, i32 %spec.store.select
  store atomic i32 %spec.select, ptr %4 seq_cst, align 4
  %14 = getelementptr inbounds i8, ptr %0, i64 28
  %15 = load ptr, ptr %14, align 4, !tbaa !14
  %16 = getelementptr inbounds i8, ptr %0, i64 20
  %17 = load i32, ptr %16, align 4, !tbaa !15
  %18 = mul i32 %17, %8
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %15, i64 %19
  br label %21

21:                                               ; preds = %1, %3, %7
  %.0 = phi ptr [ %20, %7 ], [ null, %3 ], [ null, %1 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nofree norecurse nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) 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, !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 8}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !11, i64 28}
!15 = !{!6, !7, i64 20}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass