; LLVM IR from kala13x/libxutils :: src/data (buf)

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

; Function Attrs: nounwind uwtable
define dso_local void @XRingBuffer_Update(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %5 = load i32, ptr %4, align 4, !tbaa !9
  %6 = icmp ne i32 %5, 0
  br i1 %6, label %7, label %12

7:                                                ; preds = %2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.XRingBuffer, ptr %8, i32 0, i32 4
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = add i64 %10, 1
  store i64 %11, ptr %9, align 8, !tbaa !11
  br label %17

12:                                               ; preds = %2
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.XRingBuffer, ptr %13, i32 0, i32 3
  %15 = load i64, ptr %14, align 8, !tbaa !14
  %16 = add i64 %15, 1
  store i64 %16, ptr %14, align 8, !tbaa !14
  br label %17

17:                                               ; preds = %12, %7
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.XRingBuffer, ptr %18, i32 0, i32 3
  %20 = load i64, ptr %19, align 8, !tbaa !14
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.XRingBuffer, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = icmp uge i64 %20, %23
  br i1 %24, label %25, label %28

25:                                               ; preds = %17
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.XRingBuffer, ptr %26, i32 0, i32 3
  store i64 0, ptr %27, align 8, !tbaa !14
  br label %28

28:                                               ; preds = %25, %17
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.XRingBuffer, ptr %29, i32 0, i32 4
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.XRingBuffer, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !15
  %35 = icmp uge i64 %31, %34
  br i1 %35, label %36, label %39

36:                                               ; preds = %28
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.XRingBuffer, ptr %37, i32 0, i32 4
  store i64 0, ptr %38, align 8, !tbaa !11
  br label %39

39:                                               ; preds = %36, %28
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.XRingBuffer, ptr %40, i32 0, i32 4
  %42 = load i64, ptr %41, align 8, !tbaa !11
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.XRingBuffer, ptr %43, i32 0, i32 3
  %45 = load i64, ptr %44, align 8, !tbaa !14
  %46 = icmp ugt i64 %42, %45
  br i1 %46, label %47, label %55

47:                                               ; preds = %39
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.XRingBuffer, ptr %48, i32 0, i32 4
  %50 = load i64, ptr %49, align 8, !tbaa !11
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.XRingBuffer, ptr %51, i32 0, i32 3
  %53 = load i64, ptr %52, align 8, !tbaa !14
  %54 = sub i64 %50, %53
  br label %67

55:                                               ; preds = %39
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.XRingBuffer, ptr %56, i32 0, i32 2
  %58 = load i64, ptr %57, align 8, !tbaa !15
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.XRingBuffer, ptr %59, i32 0, i32 3
  %61 = load i64, ptr %60, align 8, !tbaa !14
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.XRingBuffer, ptr %62, i32 0, i32 4
  %64 = load i64, ptr %63, align 8, !tbaa !11
  %65 = sub i64 %61, %64
  %66 = sub i64 %58, %65
  br label %67

67:                                               ; preds = %55, %47
  %68 = phi i64 [ %54, %47 ], [ %66, %55 ]
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.XRingBuffer, ptr %69, i32 0, i32 1
  store i64 %68, ptr %70, align 8, !tbaa !16
  ret void
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 32}
!12 = !{!"XRingBuffer", !6, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 24}
!15 = !{!12, !13, i64 16}
!16 = !{!12, !13, i64 8}


; 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 nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local void @XRingBuffer_Update(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %7, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = add i64 %5, 1
  store i64 %6, ptr %4, align 8, !tbaa !5
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !11
  br label %11

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load i64, ptr %8, align 8, !tbaa !11
  %10 = add i64 %9, 1
  store i64 %10, ptr %8, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %7, %3
  %12 = phi i64 [ %10, %7 ], [ %.pre, %3 ]
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i64, ptr %14, align 8, !tbaa !12
  %.not18 = icmp ult i64 %12, %15
  br i1 %.not18, label %17, label %16

16:                                               ; preds = %11
  store i64 0, ptr %13, align 8, !tbaa !11
  br label %17

17:                                               ; preds = %16, %11
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %.not19 = icmp ult i64 %19, %15
  br i1 %.not19, label %21, label %20

20:                                               ; preds = %17
  store i64 0, ptr %18, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %20, %17
  %22 = phi i64 [ 0, %20 ], [ %19, %17 ]
  %23 = load i64, ptr %13, align 8, !tbaa !11
  %24 = icmp ugt i64 %22, %23
  %.neg = select i1 %24, i64 0, i64 %15
  %.v = add i64 %22, %.neg
  %25 = sub i64 %.v, %23
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %25, ptr %26, align 8, !tbaa !13
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: 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, !10, i64 32}
!6 = !{!"XRingBuffer", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !10, i64 8}


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