; 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: nounwind uwtable
declare dso_local i64 @lwrb_get_free(ptr noundef) #1

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

13:                                               ; preds = %2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.lwrb, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %26

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

23:                                               ; preds = %18
  %24 = load i64, ptr %5, align 8, !tbaa !9
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %23, %18, %13, %2
  store i64 0, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %77

27:                                               ; preds = %23
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = call i64 @lwrb_get_free(ptr noundef %28)
  store i64 %29, ptr %6, align 8, !tbaa !9
  %30 = load i64, ptr %5, align 8, !tbaa !9
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = icmp ult i64 %30, %31
  br i1 %32, label %33, label %35

33:                                               ; preds = %27
  %34 = load i64, ptr %5, align 8, !tbaa !9
  br label %37

35:                                               ; preds = %27
  %36 = load i64, ptr %6, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %35, %33
  %38 = phi i64 [ %34, %33 ], [ %36, %35 ]
  store i64 %38, ptr %5, align 8, !tbaa !9
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.lwrb, ptr %39, i32 0, i32 3
  %41 = load atomic i64, ptr %40 monotonic, align 8
  store i64 %41, ptr %9, align 8
  %42 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %42, ptr %7, align 8, !tbaa !9
  %43 = load i64, ptr %5, align 8, !tbaa !9
  %44 = load i64, ptr %7, align 8, !tbaa !9
  %45 = add i64 %44, %43
  store i64 %45, ptr %7, align 8, !tbaa !9
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.lwrb, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !13
  %50 = icmp uge i64 %46, %49
  br i1 %50, label %51, label %57

51:                                               ; preds = %37
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.lwrb, ptr %52, i32 0, i32 1
  %54 = load i64, ptr %53, align 8, !tbaa !13
  %55 = load i64, ptr %7, align 8, !tbaa !9
  %56 = sub i64 %55, %54
  store i64 %56, ptr %7, align 8, !tbaa !9
  br label %57

57:                                               ; preds = %51, %37
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.lwrb, ptr %58, i32 0, i32 3
  %60 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %60, ptr %10, align 8, !tbaa !9
  %61 = load i64, ptr %10, align 8
  store atomic i64 %61, ptr %59 release, align 8
  br label %62

62:                                               ; preds = %57
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.lwrb, ptr %63, i32 0, i32 4
  %65 = load ptr, ptr %64, align 8, !tbaa !14
  %66 = icmp ne ptr %65, null
  br i1 %66, label %67, label %73

67:                                               ; preds = %62
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.lwrb, ptr %68, i32 0, i32 4
  %70 = load ptr, ptr %69, align 8, !tbaa !14
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = load i64, ptr %5, align 8, !tbaa !9
  call void %70(ptr noundef %71, i32 noundef 1, i64 noundef %72)
  br label %73

73:                                               ; preds = %67, %62
  br label %74

74:                                               ; preds = %73
  br label %75

75:                                               ; preds = %74
  %76 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %76, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %77

77:                                               ; preds = %75, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %78 = load i64, ptr %3, align 8
  ret i64 %78
}

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


; 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: nounwind uwtable
declare dso_local i64 @lwrb_get_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @lwrb_advance(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %21, label %3

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

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

10:                                               ; preds = %5
  %11 = tail call i64 @lwrb_get_free(ptr noundef nonnull %0)
  %12 = tail call i64 @llvm.umin.i64(i64 %11, i64 %1)
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = load atomic i64, ptr %13 monotonic, align 8
  %15 = add i64 %14, %12
  %16 = load i64, ptr %6, align 8, !tbaa !11
  %.not33 = icmp ult i64 %15, %16
  %17 = select i1 %.not33, i64 0, i64 %16
  %spec.select = sub i64 %15, %17
  store atomic i64 %spec.select, ptr %13 release, align 8
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %.not34 = icmp eq ptr %19, null
  br i1 %.not34, label %21, label %20

20:                                               ; preds = %10
  tail call void %19(ptr noundef nonnull %0, i32 noundef 1, i64 noundef %12) #2
  br label %21

21:                                               ; preds = %10, %20, %2, %3, %5
  %.0 = phi i64 [ 0, %5 ], [ 0, %3 ], [ 0, %2 ], [ %12, %20 ], [ %12, %10 ]
  ret i64 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !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 = !{!6, !7, i64 32}


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