; LLVM IR from Mac-Rsh/mr-library :: source (service)

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

; 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
declare dso_local i64 @mr_ringbuf_get_space_size(ptr noundef) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @mr_ringbuf_write(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %11, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = call i64 @mr_ringbuf_get_space_size(ptr noundef %12)
  store i64 %13, ptr %9, align 8, !tbaa !9
  %14 = load i64, ptr %9, align 8, !tbaa !9
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %112

17:                                               ; preds = %3
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = load i64, ptr %9, align 8, !tbaa !9
  %20 = icmp ugt i64 %18, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %17
  %22 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %22, ptr %7, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %21, %17
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mr_ringbuf, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !11
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mr_ringbuf, ptr %27, i32 0, i32 4
  %29 = load i64, ptr %28, align 8, !tbaa !14
  %30 = sub i64 %26, %29
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = icmp ugt i64 %30, %31
  br i1 %32, label %33, label %49

33:                                               ; preds = %23
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mr_ringbuf, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mr_ringbuf, ptr %37, i32 0, i32 4
  %39 = load i64, ptr %38, align 8, !tbaa !14
  %40 = getelementptr inbounds i8, ptr %36, i64 %39
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %40, ptr align 1 %41, i64 %42, i1 false)
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mr_ringbuf, ptr %44, i32 0, i32 4
  %46 = load i64, ptr %45, align 8, !tbaa !14
  %47 = add i64 %46, %43
  store i64 %47, ptr %45, align 8, !tbaa !14
  %48 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %48, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %112

49:                                               ; preds = %23
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.mr_ringbuf, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !15
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.mr_ringbuf, ptr %53, i32 0, i32 4
  %55 = load i64, ptr %54, align 8, !tbaa !14
  %56 = getelementptr inbounds i8, ptr %52, i64 %55
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mr_ringbuf, ptr %58, i32 0, i32 1
  %60 = load i64, ptr %59, align 8, !tbaa !11
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mr_ringbuf, ptr %61, i32 0, i32 4
  %63 = load i64, ptr %62, align 8, !tbaa !14
  %64 = sub i64 %60, %63
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %56, ptr align 1 %57, i64 %64, i1 false)
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.mr_ringbuf, ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !15
  %68 = getelementptr inbounds i8, ptr %67, i64 0
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.mr_ringbuf, ptr %70, i32 0, i32 1
  %72 = load i64, ptr %71, align 8, !tbaa !11
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mr_ringbuf, ptr %73, i32 0, i32 4
  %75 = load i64, ptr %74, align 8, !tbaa !14
  %76 = sub i64 %72, %75
  %77 = getelementptr inbounds i8, ptr %69, i64 %76
  %78 = load i64, ptr %7, align 8, !tbaa !9
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.mr_ringbuf, ptr %79, i32 0, i32 1
  %81 = load i64, ptr %80, align 8, !tbaa !11
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.mr_ringbuf, ptr %82, i32 0, i32 4
  %84 = load i64, ptr %83, align 8, !tbaa !14
  %85 = sub i64 %81, %84
  %86 = sub i64 %78, %85
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %68, ptr align 1 %77, i64 %86, i1 false)
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.mr_ringbuf, ptr %87, i32 0, i32 2
  %89 = load i32, ptr %88, align 8
  %90 = lshr i32 %89, 1
  %91 = and i32 %90, 1
  %92 = xor i32 %91, -1
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.mr_ringbuf, ptr %93, i32 0, i32 2
  %95 = load i32, ptr %94, align 8
  %96 = and i32 %92, 1
  %97 = shl i32 %96, 1
  %98 = and i32 %95, -3
  %99 = or i32 %98, %97
  store i32 %99, ptr %94, align 8
  %100 = load i64, ptr %7, align 8, !tbaa !9
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.mr_ringbuf, ptr %101, i32 0, i32 1
  %103 = load i64, ptr %102, align 8, !tbaa !11
  %104 = load ptr, ptr %5, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.mr_ringbuf, ptr %104, i32 0, i32 4
  %106 = load i64, ptr %105, align 8, !tbaa !14
  %107 = sub i64 %103, %106
  %108 = sub i64 %100, %107
  %109 = load ptr, ptr %5, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.mr_ringbuf, ptr %109, i32 0, i32 4
  store i64 %108, ptr %110, align 8, !tbaa !14
  %111 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %111, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %112

112:                                              ; preds = %49, %33, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %113 = load i64, ptr %4, align 8
  ret i64 %113
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !10, i64 8}
!12 = !{!"mr_ringbuf", !6, i64 0, !10, i64 8, !13, i64 16, !13, i64 16, !13, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !10, i64 32}
!15 = !{!12, !6, 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: nounwind uwtable
declare dso_local i64 @mr_ringbuf_get_space_size(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @mr_ringbuf_write(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call i64 @mr_ringbuf_get_space_size(ptr noundef %0)
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %31, label %6

6:                                                ; preds = %3
  %spec.select = tail call i64 @llvm.umin.i64(i64 %4, i64 %2)
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = sub i64 %8, %10
  %12 = icmp ugt i64 %11, %spec.select
  %13 = load ptr, ptr %0, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %13, i64 %10
  br i1 %12, label %15, label %18

15:                                               ; preds = %6
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %14, ptr align 1 %1, i64 %spec.select, i1 false)
  %16 = load i64, ptr %9, align 8, !tbaa !12
  %17 = add i64 %16, %spec.select
  br label %.sink.split

18:                                               ; preds = %6
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %14, ptr align 1 %1, i64 %11, i1 false)
  %19 = load ptr, ptr %0, align 8, !tbaa !13
  %20 = load i64, ptr %7, align 8, !tbaa !5
  %21 = load i64, ptr %9, align 8, !tbaa !12
  %22 = sub i64 %20, %21
  %23 = getelementptr inbounds i8, ptr %1, i64 %22
  %24 = sub i64 %spec.select, %22
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %19, ptr align 1 %23, i64 %24, i1 false)
  %25 = getelementptr inbounds i8, ptr %0, i64 16
  %26 = load i32, ptr %25, align 8
  %27 = xor i32 %26, 2
  store i32 %27, ptr %25, align 8
  %28 = load i64, ptr %7, align 8, !tbaa !5
  %29 = load i64, ptr %9, align 8, !tbaa !12
  %.neg = sub i64 %spec.select, %28
  %30 = add i64 %.neg, %29
  br label %.sink.split

.sink.split:                                      ; preds = %15, %18
  %.sink = phi i64 [ %30, %18 ], [ %17, %15 ]
  store i64 %.sink, ptr %9, align 8, !tbaa !12
  br label %31

31:                                               ; preds = %.sink.split, %3
  %.0 = phi i64 [ 0, %3 ], [ %spec.select, %.sink.split ]
  ret i64 %.0
}

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

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

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


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