; 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_linear_block_write_length(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 %54

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 volatile 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 volatile i64 %31, ptr %6, align 8, !tbaa !13
  %32 = load volatile i64, ptr %5, align 8, !tbaa !13
  %33 = load volatile i64, ptr %6, align 8, !tbaa !13
  %34 = icmp uge i64 %32, %33
  br i1 %34, label %35, label %47

35:                                               ; preds = %23
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.lwrb, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !12
  %39 = load volatile i64, ptr %5, align 8, !tbaa !13
  %40 = sub i64 %38, %39
  store i64 %40, ptr %4, align 8, !tbaa !13
  %41 = load volatile i64, ptr %6, align 8, !tbaa !13
  %42 = icmp eq i64 %41, 0
  br i1 %42, label %43, label %46

43:                                               ; preds = %35
  %44 = load i64, ptr %4, align 8, !tbaa !13
  %45 = add i64 %44, -1
  store i64 %45, ptr %4, align 8, !tbaa !13
  br label %46

46:                                               ; preds = %43, %35
  br label %52

47:                                               ; preds = %23
  %48 = load volatile i64, ptr %6, align 8, !tbaa !13
  %49 = load volatile i64, ptr %5, align 8, !tbaa !13
  %50 = sub i64 %48, %49
  %51 = sub i64 %50, 1
  store i64 %51, ptr %4, align 8, !tbaa !13
  br label %52

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

54:                                               ; preds = %52, %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
  %55 = load i64, ptr %2, align 8
  ret i64 %55
}

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 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: nofree norecurse nounwind memory(argmem: readwrite, inaccessiblemem: readwrite) uwtable
define dso_local i64 @lwrb_get_linear_block_write_length(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3)
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %21, label %4

4:                                                ; preds = %1
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %.not18 = icmp eq ptr %5, null
  br i1 %.not18, label %21, label %6

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !11
  %.not19 = icmp eq i64 %8, 0
  br i1 %.not19, label %21, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = load atomic i64, ptr %10 monotonic, align 8
  store volatile i64 %11, ptr %2, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load atomic i64, ptr %12 monotonic, align 8
  store volatile i64 %13, ptr %3, align 8, !tbaa !12
  %.0..0..0..0.4 = load volatile i64, ptr %2, align 8, !tbaa !12
  %.0..0..0..0. = load volatile i64, ptr %3, align 8, !tbaa !12
  %.not20 = icmp ult i64 %.0..0..0..0.4, %.0..0..0..0.
  br i1 %.not20, label %18, label %14

14:                                               ; preds = %9
  %.0..0..0..0.5 = load volatile i64, ptr %2, align 8, !tbaa !12
  %15 = sub i64 %8, %.0..0..0..0.5
  %.0..0..0..0.2 = load volatile i64, ptr %3, align 8, !tbaa !12
  %16 = icmp eq i64 %.0..0..0..0.2, 0
  %17 = sext i1 %16 to i64
  %spec.select = add i64 %15, %17
  br label %21

18:                                               ; preds = %9
  %.0..0..0..0.3 = load volatile i64, ptr %3, align 8, !tbaa !12
  %.0..0..0..0.6 = load volatile i64, ptr %2, align 8, !tbaa !12
  %19 = xor i64 %.0..0..0..0.6, -1
  %20 = add i64 %.0..0..0..0.3, %19
  br label %21

21:                                               ; preds = %14, %18, %1, %4, %6
  %.0 = phi i64 [ 0, %6 ], [ 0, %4 ], [ 0, %1 ], [ %20, %18 ], [ %spec.select, %14 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2)
  ret i64 %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nounwind 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 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}
!12 = !{!10, !10, i64 0}


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