; 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_data_size(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @mr_ringbuf_read(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_data_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 %110

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 3
  %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 %8, align 8, !tbaa !5
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mr_ringbuf, ptr %35, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mr_ringbuf, ptr %38, i32 0, i32 3
  %40 = load i64, ptr %39, align 8, !tbaa !14
  %41 = getelementptr inbounds i8, ptr %37, i64 %40
  %42 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %34, 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 3
  %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 %110

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

110:                                              ; 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
  %111 = load i64, ptr %4, align 8
  ret i64 %111
}

; 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

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 24}
!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_data_size(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @mr_ringbuf_read(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call i64 @mr_ringbuf_get_data_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 24
  %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 %1, ptr align 1 %14, 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 %1, ptr align 1 %14, i64 %11, i1 false)
  %19 = load i64, ptr %7, align 8, !tbaa !5
  %20 = load i64, ptr %9, align 8, !tbaa !12
  %21 = sub i64 %19, %20
  %22 = getelementptr inbounds i8, ptr %1, i64 %21
  %23 = load ptr, ptr %0, align 8, !tbaa !13
  %24 = sub i64 %spec.select, %21
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %22, 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, 1
  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: 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: 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 24}
!13 = !{!6, !7, i64 0}


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