; LLVM IR from DanEngelbrecht/longtail :: cmd (main)

; 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.Longtail_FrameBufferAPI = type { %struct.Longtail_API, ptr, ptr, ptr }
%struct.Longtail_API = type { ptr }
%struct.MonitorBlockInfo = type { i32, i64, i32, ptr, i32 }

@MonitorBlockInfosCount = external dso_local global i32, align 4
@MonitorBlockInfos = external dso_local global ptr, align 8
@MonitorAssetInfos = external dso_local global ptr, align 8
@MonitorChunkInfos = external dso_local global ptr, align 8
@FrameBufferAPI = external dso_local global ptr, align 8
@FrameBuffer = external dso_local global ptr, align 8
@MonitorWindowBuffer = external hidden global ptr, 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

; Function Attrs: nounwind uwtable
define dso_local void @DisposeMonitor() #1 {
  %1 = alloca i32, align 4
  %2 = load ptr, ptr @FrameBufferAPI, align 8, !tbaa !5
  %3 = icmp ne ptr %2, null
  br i1 %3, label %4, label %15

4:                                                ; preds = %0
  %5 = load ptr, ptr @FrameBuffer, align 8, !tbaa !5
  %6 = icmp ne ptr %5, null
  br i1 %6, label %7, label %14

7:                                                ; preds = %4
  %8 = load ptr, ptr @FrameBufferAPI, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.Longtail_FrameBufferAPI, ptr %8, i32 0, i32 2
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr @FrameBufferAPI, align 8, !tbaa !5
  %12 = load ptr, ptr @FrameBuffer, align 8, !tbaa !5
  %13 = call i32 %10(ptr noundef %11, ptr noundef %12)
  store ptr null, ptr @FrameBuffer, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %7, %4
  br label %15

15:                                               ; preds = %14, %0
  %16 = load ptr, ptr @MonitorWindowBuffer, align 8, !tbaa !5
  call void @Longtail_Free(ptr noundef %16)
  store ptr null, ptr @MonitorWindowBuffer, align 8, !tbaa !5
  %17 = load ptr, ptr @MonitorAssetInfos, align 8, !tbaa !5
  call void @Longtail_Free(ptr noundef %17)
  store ptr null, ptr @MonitorAssetInfos, align 8, !tbaa !5
  %18 = load ptr, ptr @MonitorChunkInfos, align 8, !tbaa !5
  call void @Longtail_Free(ptr noundef %18)
  store ptr null, ptr @MonitorChunkInfos, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %19 = load i32, ptr @MonitorBlockInfosCount, align 4, !tbaa !12
  store i32 %19, ptr %1, align 4, !tbaa !12
  br label %20

20:                                               ; preds = %32, %15
  %21 = load i32, ptr %1, align 4, !tbaa !12
  %22 = icmp ugt i32 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %35

24:                                               ; preds = %20
  %25 = load ptr, ptr @MonitorBlockInfos, align 8, !tbaa !5
  %26 = load i32, ptr %1, align 4, !tbaa !12
  %27 = sub i32 %26, 1
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds %struct.MonitorBlockInfo, ptr %25, i64 %28
  %30 = getelementptr inbounds %struct.MonitorBlockInfo, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  call void @Longtail_Free(ptr noundef %31)
  br label %32

32:                                               ; preds = %24
  %33 = load i32, ptr %1, align 4, !tbaa !12
  %34 = add i32 %33, -1
  store i32 %34, ptr %1, align 4, !tbaa !12
  br label %20, !llvm.loop !17

35:                                               ; preds = %23
  %36 = load ptr, ptr @MonitorBlockInfos, align 8, !tbaa !5
  call void @Longtail_Free(ptr noundef %36)
  store ptr null, ptr @MonitorBlockInfos, align 8, !tbaa !5
  store i32 0, ptr @MonitorBlockInfosCount, align 4, !tbaa !12
  ret void
}

declare void @Longtail_Free(ptr noundef) #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 = { "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, !6, i64 16}
!10 = !{!"Longtail_FrameBufferAPI", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"Longtail_API", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 24}
!15 = !{!"MonitorBlockInfo", !13, i64 0, !16, i64 8, !13, i64 16, !6, i64 24, !13, i64 32}
!16 = !{!"long", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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.MonitorBlockInfo = type { i32, i64, i32, ptr, i32 }

@MonitorBlockInfosCount = external dso_local local_unnamed_addr global i32, align 4
@MonitorBlockInfos = external dso_local local_unnamed_addr global ptr, align 8
@MonitorAssetInfos = external dso_local local_unnamed_addr global ptr, align 8
@MonitorChunkInfos = external dso_local local_unnamed_addr global ptr, align 8
@FrameBufferAPI = external dso_local local_unnamed_addr global ptr, align 8
@FrameBuffer = external dso_local local_unnamed_addr global ptr, align 8
@MonitorWindowBuffer = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @DisposeMonitor() local_unnamed_addr #0 {
  %1 = load ptr, ptr @FrameBufferAPI, align 8, !tbaa !5
  %2 = icmp ne ptr %1, null
  %3 = load ptr, ptr @FrameBuffer, align 8
  %4 = icmp ne ptr %3, null
  %or.cond = select i1 %2, i1 %4, i1 false
  br i1 %or.cond, label %5, label %9

5:                                                ; preds = %0
  %6 = getelementptr inbounds i8, ptr %1, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = tail call i32 %7(ptr noundef nonnull %1, ptr noundef nonnull %3) #2
  store ptr null, ptr @FrameBuffer, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %5, %0
  %10 = load ptr, ptr @MonitorWindowBuffer, align 8, !tbaa !5
  tail call void @Longtail_Free(ptr noundef %10) #2
  store ptr null, ptr @MonitorWindowBuffer, align 8, !tbaa !5
  %11 = load ptr, ptr @MonitorAssetInfos, align 8, !tbaa !5
  tail call void @Longtail_Free(ptr noundef %11) #2
  store ptr null, ptr @MonitorAssetInfos, align 8, !tbaa !5
  %12 = load ptr, ptr @MonitorChunkInfos, align 8, !tbaa !5
  tail call void @Longtail_Free(ptr noundef %12) #2
  store ptr null, ptr @MonitorChunkInfos, align 8, !tbaa !5
  %13 = load i32, ptr @MonitorBlockInfosCount, align 4, !tbaa !12
  %.not6 = icmp eq i32 %13, 0
  br i1 %.not6, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %9
  %14 = zext i32 %13 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %9
  %15 = load ptr, ptr @MonitorBlockInfos, align 8, !tbaa !5
  tail call void @Longtail_Free(ptr noundef %15) #2
  store ptr null, ptr @MonitorBlockInfos, align 8, !tbaa !5
  store i32 0, ptr @MonitorBlockInfosCount, align 4, !tbaa !12
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %14, %.lr.ph.preheader ], [ %17, %.lr.ph ]
  %16 = load ptr, ptr @MonitorBlockInfos, align 8, !tbaa !5
  %17 = add nsw i64 %indvars.iv, -1
  %18 = getelementptr inbounds %struct.MonitorBlockInfo, ptr %16, i64 %17, i32 3
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  tail call void @Longtail_Free(ptr noundef %19) #2
  %.not.wide = icmp eq i64 %17, 0
  br i1 %.not.wide, label %._crit_edge, label %.lr.ph, !llvm.loop !17
}

declare void @Longtail_Free(ptr noundef) local_unnamed_addr #1

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 = { "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 }

!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, !6, i64 16}
!10 = !{!"Longtail_FrameBufferAPI", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"Longtail_API", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 24}
!15 = !{!"MonitorBlockInfo", !13, i64 0, !16, i64 8, !13, i64 16, !6, i64 24, !13, i64 32}
!16 = !{!"long", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass