; LLVM IR from Aeternum-Veritas/Merry :: merry (merry_subsystem)

; 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.MerrySubSys = type { ptr, i64, i64, i64, ptr, ptr, i8, ptr }
%struct.MerrySubChannel = type { ptr, ptr, i8 }

@subsys = external hidden global %struct.MerrySubSys, align 8

; 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

declare void @merry_mutex_lock(ptr noundef) #1

declare void @merry_mutex_unlock(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @merry_subsys_write(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4, i64 noundef %5) #2 {
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca [5 x i64], align 16
  store i64 %0, ptr %8, align 8, !tbaa !5
  store i64 %1, ptr %9, align 8, !tbaa !5
  store i64 %2, ptr %10, align 8, !tbaa !5
  store i64 %3, ptr %11, align 8, !tbaa !5
  store i64 %4, ptr %12, align 8, !tbaa !5
  store i64 %5, ptr %13, align 8, !tbaa !5
  %15 = load ptr, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 5), align 8, !tbaa !9
  call void @merry_mutex_lock(ptr noundef %15)
  %16 = load i64, ptr %8, align 8, !tbaa !5
  %17 = load i64, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 1), align 8, !tbaa !12
  %18 = icmp uge i64 %16, %17
  br i1 %18, label %27, label %19

19:                                               ; preds = %6
  %20 = load ptr, ptr @subsys, align 8, !tbaa !13
  %21 = load i64, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MerrySubChannel, ptr %20, i64 %21
  %23 = getelementptr inbounds %struct.MerrySubChannel, ptr %22, i32 0, i32 2
  %24 = load i8, ptr %23, align 8, !tbaa !14
  %25 = zext i8 %24 to i32
  %26 = icmp eq i32 %25, 0
  br label %27

27:                                               ; preds = %19, %6
  %28 = phi i1 [ true, %6 ], [ %26, %19 ]
  %29 = xor i1 %28, true
  %30 = xor i1 %29, true
  %31 = zext i1 %30 to i32
  %32 = sext i32 %31 to i64
  %33 = call i64 @llvm.expect.i64(i64 %32, i64 0)
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %27
  %36 = load ptr, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 5), align 8, !tbaa !9
  call void @merry_mutex_unlock(ptr noundef %36)
  store i8 1, ptr %7, align 1
  br label %54

37:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 40, ptr %14) #4
  %38 = load i64, ptr %9, align 8, !tbaa !5
  %39 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 0
  store i64 %38, ptr %39, align 16, !tbaa !5
  %40 = load i64, ptr %10, align 8, !tbaa !5
  %41 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 1
  store i64 %40, ptr %41, align 8, !tbaa !5
  %42 = load i64, ptr %11, align 8, !tbaa !5
  %43 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 2
  store i64 %42, ptr %43, align 16, !tbaa !5
  %44 = load i64, ptr %12, align 8, !tbaa !5
  %45 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 3
  store i64 %44, ptr %45, align 8, !tbaa !5
  %46 = load i64, ptr %13, align 8, !tbaa !5
  %47 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 4
  store i64 %46, ptr %47, align 16, !tbaa !5
  %48 = load ptr, ptr @subsys, align 8, !tbaa !13
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.MerrySubChannel, ptr %48, i64 %49
  %51 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 0
  %52 = call zeroext i8 @merry_channel_write(ptr noundef %50, ptr noundef %51, i64 noundef 40)
  %53 = load ptr, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 5), align 8, !tbaa !9
  call void @merry_mutex_unlock(ptr noundef %53)
  store i8 0, ptr %7, align 1
  call void @llvm.lifetime.end.p0(i64 40, ptr %14) #4
  br label %54

54:                                               ; preds = %37, %35
  %55 = load i8, ptr %7, align 1
  ret i8 %55
}

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

declare zeroext i8 @merry_channel_write(ptr noundef, ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #4 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 40}
!10 = !{!"MerrySubSys", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 40, !7, i64 48, !11, i64 56}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !11, i64 0}
!14 = !{!15, !7, i64 16}
!15 = !{!"MerrySubChannel", !11, i64 0, !11, i64 8, !7, i64 16}


; 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"

%struct.MerrySubSys = type { ptr, i64, i64, i64, ptr, ptr, i8, ptr }
%struct.MerrySubChannel = type { ptr, ptr, i8 }

@subsys = external hidden local_unnamed_addr global %struct.MerrySubSys, align 8

; 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

declare void @merry_mutex_lock(ptr noundef) local_unnamed_addr #1

declare void @merry_mutex_unlock(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @merry_subsys_write(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4, i64 noundef %5) local_unnamed_addr #2 {
  %7 = alloca [5 x i64], align 16
  %8 = load ptr, ptr getelementptr inbounds (i8, ptr @subsys, i64 40), align 8, !tbaa !5
  tail call void @merry_mutex_lock(ptr noundef %8) #3
  %9 = load i64, ptr getelementptr inbounds (i8, ptr @subsys, i64 8), align 8, !tbaa !11
  %.not = icmp ugt i64 %9, %0
  br i1 %.not, label %10, label %.critedge, !prof !12

10:                                               ; preds = %6
  %11 = load ptr, ptr @subsys, align 8, !tbaa !13
  %12 = getelementptr inbounds %struct.MerrySubChannel, ptr %11, i64 %0, i32 2
  %13 = load i8, ptr %12, align 8, !tbaa !14
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %.critedge, label %16, !prof !16

.critedge:                                        ; preds = %6, %10
  %15 = load ptr, ptr getelementptr inbounds (i8, ptr @subsys, i64 40), align 8, !tbaa !5
  tail call void @merry_mutex_unlock(ptr noundef %15) #3
  br label %24

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %7) #3
  store i64 %1, ptr %7, align 16, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %7, i64 8
  store i64 %2, ptr %17, align 8, !tbaa !17
  %18 = getelementptr inbounds i8, ptr %7, i64 16
  store i64 %3, ptr %18, align 16, !tbaa !17
  %19 = getelementptr inbounds i8, ptr %7, i64 24
  store i64 %4, ptr %19, align 8, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %7, i64 32
  store i64 %5, ptr %20, align 16, !tbaa !17
  %21 = getelementptr inbounds %struct.MerrySubChannel, ptr %11, i64 %0
  %22 = call zeroext i8 @merry_channel_write(ptr noundef %21, ptr noundef nonnull %7, i64 noundef 40) #3
  %23 = load ptr, ptr getelementptr inbounds (i8, ptr @subsys, i64 40), align 8, !tbaa !5
  call void @merry_mutex_unlock(ptr noundef %23) #3
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %7) #3
  br label %24

24:                                               ; preds = %16, %.critedge
  %.0 = phi i8 [ 1, %.critedge ], [ 0, %16 ]
  ret i8 %.0
}

declare zeroext i8 @merry_channel_write(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #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, !7, i64 40}
!6 = !{!"MerrySubSys", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !7, i64 40, !8, i64 48, !7, i64 56}
!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 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!13 = !{!6, !7, i64 0}
!14 = !{!15, !8, i64 16}
!15 = !{!"MerrySubChannel", !7, i64 0, !7, i64 8, !8, i64 16}
!16 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!17 = !{!10, !10, i64 0}


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