; 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 zeroext i8 @merry_channel_write(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @merry_subsys_close_all() #2 {
  %1 = alloca [5 x ptr], align 16
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr %1) #3
  %3 = getelementptr inbounds [5 x ptr], ptr %1, i64 0, i64 0
  store ptr null, ptr %3, align 16, !tbaa !5
  %4 = getelementptr inbounds [5 x ptr], ptr %1, i64 0, i64 1
  store ptr null, ptr %4, align 8, !tbaa !5
  %5 = getelementptr inbounds [5 x ptr], ptr %1, i64 0, i64 2
  store ptr null, ptr %5, align 16, !tbaa !5
  %6 = getelementptr inbounds [5 x ptr], ptr %1, i64 0, i64 3
  store ptr null, ptr %6, align 8, !tbaa !5
  %7 = getelementptr inbounds [5 x ptr], ptr %1, i64 0, i64 4
  store ptr null, ptr %7, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store i64 0, ptr %2, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %28, %0
  %9 = load i64, ptr %2, align 8, !tbaa !9
  %10 = load i64, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 3), align 8, !tbaa !11
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %31

13:                                               ; preds = %8
  %14 = load ptr, ptr @subsys, align 8, !tbaa !13
  %15 = load i64, ptr %2, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.MerrySubChannel, ptr %14, i64 %15
  %17 = getelementptr inbounds %struct.MerrySubChannel, ptr %16, i32 0, i32 2
  %18 = load i8, ptr %17, align 8, !tbaa !14
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 1
  br i1 %20, label %21, label %27

21:                                               ; preds = %13
  %22 = load ptr, ptr @subsys, align 8, !tbaa !13
  %23 = load i64, ptr %2, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.MerrySubChannel, ptr %22, i64 %23
  %25 = getelementptr inbounds [5 x ptr], ptr %1, i64 0, i64 0
  %26 = call zeroext i8 @merry_channel_write(ptr noundef %24, ptr noundef %25, i64 noundef 40)
  br label %27

27:                                               ; preds = %21, %13
  br label %28

28:                                               ; preds = %27
  %29 = load i64, ptr %2, align 8, !tbaa !9
  %30 = add i64 %29, 1
  store i64 %30, ptr %2, align 8, !tbaa !9
  br label %8, !llvm.loop !16

31:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 40, ptr %1) #3
  ret void
}

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 = { 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 24}
!12 = !{!"MerrySubSys", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !6, i64 40, !7, i64 48, !6, i64 56}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !7, i64 16}
!15 = !{!"MerrySubChannel", !6, i64 0, !6, i64 8, !7, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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 zeroext i8 @merry_channel_write(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @merry_subsys_close_all() local_unnamed_addr #2 {
  %1 = alloca [5 x ptr], align 16
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) %1, i8 0, i64 40, i1 false)
  %2 = load i64, ptr getelementptr inbounds (i8, ptr @subsys, i64 24), align 8, !tbaa !5
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %.pre5 = load ptr, ptr @subsys, align 8, !tbaa !11
  br label %.lr.ph

._crit_edge:                                      ; preds = %11, %0
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %1) #4
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %11
  %3 = phi i64 [ %12, %11 ], [ %2, %.lr.ph.preheader ]
  %4 = phi ptr [ %13, %11 ], [ %.pre5, %.lr.ph.preheader ]
  %.04 = phi i64 [ %14, %11 ], [ 0, %.lr.ph.preheader ]
  %5 = getelementptr inbounds %struct.MerrySubChannel, ptr %4, i64 %.04
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %7 = load i8, ptr %6, align 8, !tbaa !12
  %8 = icmp eq i8 %7, 1
  br i1 %8, label %9, label %11

9:                                                ; preds = %.lr.ph
  %10 = call zeroext i8 @merry_channel_write(ptr noundef nonnull %5, ptr noundef nonnull %1, i64 noundef 40) #4
  %.pre = load ptr, ptr @subsys, align 8, !tbaa !11
  %.pre6 = load i64, ptr getelementptr inbounds (i8, ptr @subsys, i64 24), align 8, !tbaa !5
  br label %11

11:                                               ; preds = %.lr.ph, %9
  %12 = phi i64 [ %3, %.lr.ph ], [ %.pre6, %9 ]
  %13 = phi ptr [ %4, %.lr.ph ], [ %.pre, %9 ]
  %14 = add nuw i64 %.04, 1
  %15 = icmp ult i64 %14, %12
  br i1 %15, label %.lr.ph, label %._crit_edge, !llvm.loop !14
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !10, i64 24}
!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, !7, i64 0}
!12 = !{!13, !8, i64 16}
!13 = !{!"MerrySubChannel", !7, i64 0, !7, i64 8, !8, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass