; LLVM IR from MaJerle/lwpkt :: libs/lwrb/src/lwrb (lwrb)

; 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.lwrb = type { ptr, i64, i64, i64, 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 i64 @lwrb_get_full(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %22

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.lwrb, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %22

17:                                               ; preds = %12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.lwrb, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !12
  %21 = icmp ugt i64 %20, 0
  br i1 %21, label %23, label %22

22:                                               ; preds = %17, %12, %1
  store i64 0, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %55

23:                                               ; preds = %17
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.lwrb, ptr %24, i32 0, i32 3
  %26 = load atomic i64, ptr %25 monotonic, align 8
  store i64 %26, ptr %8, align 8
  %27 = load i64, ptr %8, align 8, !tbaa !13
  store i64 %27, ptr %5, align 8, !tbaa !13
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.lwrb, ptr %28, i32 0, i32 2
  %30 = load atomic i64, ptr %29 monotonic, align 8
  store i64 %30, ptr %9, align 8
  %31 = load i64, ptr %9, align 8, !tbaa !13
  store i64 %31, ptr %6, align 8, !tbaa !13
  %32 = load i64, ptr %5, align 8, !tbaa !13
  %33 = load i64, ptr %6, align 8, !tbaa !13
  %34 = icmp eq i64 %32, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %23
  store i64 0, ptr %4, align 8, !tbaa !13
  br label %53

36:                                               ; preds = %23
  %37 = load i64, ptr %5, align 8, !tbaa !13
  %38 = load i64, ptr %6, align 8, !tbaa !13
  %39 = icmp ugt i64 %37, %38
  br i1 %39, label %40, label %44

40:                                               ; preds = %36
  %41 = load i64, ptr %5, align 8, !tbaa !13
  %42 = load i64, ptr %6, align 8, !tbaa !13
  %43 = sub i64 %41, %42
  store i64 %43, ptr %4, align 8, !tbaa !13
  br label %52

44:                                               ; preds = %36
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.lwrb, ptr %45, i32 0, i32 1
  %47 = load i64, ptr %46, align 8, !tbaa !12
  %48 = load i64, ptr %6, align 8, !tbaa !13
  %49 = load i64, ptr %5, align 8, !tbaa !13
  %50 = sub i64 %48, %49
  %51 = sub i64 %47, %50
  store i64 %51, ptr %4, align 8, !tbaa !13
  br label %52

52:                                               ; preds = %44, %40
  br label %53

53:                                               ; preds = %52, %35
  %54 = load i64, ptr %4, align 8, !tbaa !13
  store i64 %54, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %55

55:                                               ; preds = %53, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %56 = load i64, ptr %2, align 8
  ret i64 %56
}

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


; 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) uwtable
define dso_local i64 @lwrb_get_full(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %19, label %2

2:                                                ; preds = %1
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %.not21 = icmp eq ptr %3, null
  br i1 %.not21, label %19, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %.not22 = icmp eq i64 %6, 0
  br i1 %.not22, label %19, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load atomic i64, ptr %8 monotonic, align 8
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = load atomic i64, ptr %10 monotonic, align 8
  %12 = icmp eq i64 %9, %11
  br i1 %12, label %19, label %13

13:                                               ; preds = %7
  %14 = icmp ugt i64 %9, %11
  br i1 %14, label %15, label %17

15:                                               ; preds = %13
  %16 = sub i64 %9, %11
  br label %19

17:                                               ; preds = %13
  %.neg = add i64 %9, %6
  %18 = sub i64 %.neg, %11
  br label %19

19:                                               ; preds = %17, %15, %7, %1, %2, %4
  %.0 = phi i64 [ 0, %4 ], [ 0, %2 ], [ 0, %1 ], [ %16, %15 ], [ %18, %17 ], [ 0, %7 ]
  ret i64 %.0
}

attributes #0 = { mustprogress nofree norecurse 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, !7, i64 0}
!6 = !{!"lwrb", !7, i64 0, !10, i64 8, !8, i64 16, !8, i64 24, !7, 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 8}


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