; 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
@stderr = external global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [80 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #0

declare void @merry_task_queue_destroy(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nounwind uwtable
define dso_local void @merry_destroy_subsys() #3 {
  %1 = alloca i64, align 8
  br label %2

2:                                                ; preds = %0
  %3 = load ptr, ptr @subsys, align 8, !tbaa !5
  %4 = icmp ne ptr %3, null
  br i1 %4, label %10, label %5

5:                                                ; preds = %2
  br label %6

6:                                                ; preds = %5
  %7 = load ptr, ptr @stderr, align 8, !tbaa !11
  %8 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef @.str.13, ptr noundef @.str.14, ptr noundef @.str.15, i32 noundef 165, ptr noundef @.str.16, ptr noundef @.str.17, ptr noundef @.str.15) #4
  br label %9

9:                                                ; preds = %6
  br label %10

10:                                               ; preds = %9, %2
  br label %11

11:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  store i64 0, ptr %1, align 8, !tbaa !12
  br label %12

12:                                               ; preds = %21, %11
  %13 = load i64, ptr %1, align 8, !tbaa !12
  %14 = load i64, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 3), align 8, !tbaa !13
  %15 = icmp ult i64 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  br label %24

17:                                               ; preds = %12
  %18 = load ptr, ptr @subsys, align 8, !tbaa !5
  %19 = load i64, ptr %1, align 8, !tbaa !12
  %20 = getelementptr inbounds %struct.MerrySubChannel, ptr %18, i64 %19
  call void @merry_close_channel(ptr noundef %20)
  br label %21

21:                                               ; preds = %17
  %22 = load i64, ptr %1, align 8, !tbaa !12
  %23 = add i64 %22, 1
  store i64 %23, ptr %1, align 8, !tbaa !12
  br label %12, !llvm.loop !14

24:                                               ; preds = %16
  %25 = load ptr, ptr @subsys, align 8, !tbaa !5
  call void @free(ptr noundef %25) #4
  store ptr null, ptr @subsys, align 8, !tbaa !5
  %26 = load ptr, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 5), align 8, !tbaa !16
  call void @merry_mutex_destroy(ptr noundef %26)
  %27 = load ptr, ptr getelementptr inbounds (%struct.MerrySubSys, ptr @subsys, i32 0, i32 4), align 8, !tbaa !17
  call void @merry_task_queue_destroy(ptr noundef %27)
  ret void
}

declare void @merry_close_channel(ptr noundef) #1

declare void @merry_mutex_destroy(ptr noundef) #1

attributes #0 = { nounwind "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 = { "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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, !7, i64 0}
!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 = !{!7, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 24}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !7, i64 40}
!17 = !{!6, !7, 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"

%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
@stderr = external local_unnamed_addr global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.16 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [80 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

declare void @merry_task_queue_destroy(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @merry_destroy_subsys() local_unnamed_addr #3 {
  %1 = load ptr, ptr @subsys, align 8, !tbaa !5
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %2, label %5

2:                                                ; preds = %0
  %3 = load ptr, ptr @stderr, align 8, !tbaa !11
  %4 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %3, ptr noundef nonnull @.str.13, ptr noundef nonnull @.str.14, ptr noundef nonnull @.str.15, i32 noundef 165, ptr noundef nonnull @.str.16, ptr noundef nonnull @.str.17, ptr noundef nonnull @.str.15) #4
  br label %5

5:                                                ; preds = %0, %2
  %6 = load i64, ptr getelementptr inbounds (i8, ptr @subsys, i64 24), align 8, !tbaa !12
  %.not4 = icmp eq i64 %6, 0
  br i1 %.not4, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %5
  %7 = load ptr, ptr @subsys, align 8, !tbaa !5
  tail call void @free(ptr noundef %7) #5
  store ptr null, ptr @subsys, align 8, !tbaa !5
  %8 = load ptr, ptr getelementptr inbounds (i8, ptr @subsys, i64 40), align 8, !tbaa !13
  tail call void @merry_mutex_destroy(ptr noundef %8) #5
  %9 = load ptr, ptr getelementptr inbounds (i8, ptr @subsys, i64 32), align 8, !tbaa !14
  tail call void @merry_task_queue_destroy(ptr noundef %9) #5
  ret void

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.03 = phi i64 [ %12, %.lr.ph ], [ 0, %5 ]
  %10 = load ptr, ptr @subsys, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.MerrySubChannel, ptr %10, i64 %.03
  tail call void @merry_close_channel(ptr noundef %11) #5
  %12 = add nuw i64 %.03, 1
  %13 = load i64, ptr getelementptr inbounds (i8, ptr @subsys, i64 24), align 8, !tbaa !12
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !15
}

declare void @merry_close_channel(ptr noundef) local_unnamed_addr #2

declare void @merry_mutex_destroy(ptr noundef) local_unnamed_addr #2

attributes #0 = { nofree nounwind "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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { "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 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 #4 = { cold nounwind }
attributes #5 = { 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 = !{!"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 = !{!7, !7, i64 0}
!12 = !{!6, !10, i64 24}
!13 = !{!6, !7, i64 40}
!14 = !{!6, !7, i64 32}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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