; LLVM IR from mkazhdan/DMG :: Util/GridStream.ll (ZN14FileBackedGridD2Ev)

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

%class.FileBackedGrid = type <{ %class.StreamingGrid, i64, i64, ptr, ptr, i32, i32, i32, i32, i8, [7 x i8] }>
%class.StreamingGrid = type { %class.IOClient }
%class.IOClient = type { ptr, %"class.boost::recursive_timed_mutex", ptr }
%"class.boost::recursive_timed_mutex" = type <{ %union.pthread_mutex_t, %union.pthread_cond_t, i8, [7 x i8], i64, i32, [4 x i8] }>
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }

@_ZTV14FileBackedGrid = external dso_local unnamed_addr constant { [12 x ptr] }, align 8
@.str.14 = external hidden unnamed_addr constant [35 x i8], align 1
@stdout = external 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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) #1

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN14FileBackedGridD2Ev(ptr noundef nonnull align 8 dereferenceable(177) %0) unnamed_addr #3 align 2 personality ptr @__gxx_personality_v0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8
  store ptr getelementptr inbounds inrange(-16, 80) ({ [12 x ptr] }, ptr @_ZTV14FileBackedGrid, i32 0, i32 0, i32 2), ptr %4, align 8, !tbaa !9
  %5 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 9
  %6 = load i8, ptr %5, align 8, !tbaa !11, !range !20, !noundef !21
  %7 = trunc i8 %6 to i1
  br i1 %7, label %21, label %8

8:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #7
  store i32 0, ptr %3, align 4, !tbaa !22
  br label %9

9:                                                ; preds = %17, %8
  %10 = load i32, ptr %3, align 4, !tbaa !22
  %11 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 8
  %12 = load i32, ptr %11, align 4, !tbaa !23
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %9
  invoke void @_ZN14FileBackedGrid10_writeNextEv(ptr noundef nonnull align 8 dereferenceable(177) %4)
          to label %16 unwind label %47

16:                                               ; preds = %15
  br label %17

17:                                               ; preds = %16
  %18 = load i32, ptr %3, align 4, !tbaa !22
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %3, align 4, !tbaa !22
  br label %9, !llvm.loop !24

20:                                               ; preds = %14
  br label %21

21:                                               ; preds = %20, %1
  %22 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 4
  %23 = load ptr, ptr %22, align 8, !tbaa !26
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %29

25:                                               ; preds = %21
  %26 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 4
  %27 = load ptr, ptr %26, align 8, !tbaa !26
  call void @free(ptr noundef %27) #7
  %28 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 4
  store ptr null, ptr %28, align 8, !tbaa !26
  br label %29

29:                                               ; preds = %25, %21
  %30 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 8
  store i32 0, ptr %30, align 4, !tbaa !23
  %31 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 7
  store i32 0, ptr %31, align 8, !tbaa !27
  %32 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 6
  store i32 0, ptr %32, align 4, !tbaa !28
  %33 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 5
  store i32 0, ptr %33, align 8, !tbaa !29
  %34 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 3
  invoke void @_Z15CloseFileHandleRP11_FileHandle(ptr noundef nonnull align 8 dereferenceable(8) %34)
          to label %35 unwind label %47

35:                                               ; preds = %29
  %36 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !30
  %38 = ashr i64 %37, 20
  %39 = getelementptr inbounds %class.FileBackedGrid, ptr %4, i32 0, i32 2
  %40 = load i64, ptr %39, align 8, !tbaa !31
  %41 = ashr i64 %40, 20
  %42 = invoke i32 (ptr, ...) @printf(ptr noundef @.str.14, i64 noundef %38, i64 noundef %41)
          to label %43 unwind label %47

43:                                               ; preds = %35
  %44 = load ptr, ptr @stdout, align 8, !tbaa !5
  %45 = invoke i32 @fflush(ptr noundef %44)
          to label %46 unwind label %47

46:                                               ; preds = %43
  call void @_ZN13StreamingGridD2Ev(ptr noundef nonnull align 8 dereferenceable(128) %4) #7
  ret void

47:                                               ; preds = %43, %35, %29, %15
  %48 = landingpad { ptr, i32 }
          catch ptr null
  %49 = extractvalue { ptr, i32 } %48, 0
  call void @__clang_call_terminate(ptr %49) #8
  unreachable
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14FileBackedGrid10_writeNextEv(ptr noundef nonnull align 8 dereferenceable(177)) #4 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z15CloseFileHandleRP11_FileHandle(ptr noundef nonnull align 8 dereferenceable(8)) #5

declare i32 @printf(ptr noundef, ...) #6

declare i32 @fflush(ptr noundef) #6

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN13StreamingGridD2Ev(ptr noundef nonnull align 8 dereferenceable(128)) unnamed_addr #3 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noinline noreturn nounwind uwtable "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 "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 = { mustprogress 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 = { mustprogress 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 #5 = { inlinehint mustprogress 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 #6 = { "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 #7 = { nounwind }
attributes #8 = { noreturn 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"vtable pointer", !8, i64 0}
!11 = !{!12, !16, i64 176}
!12 = !{!"_ZTS14FileBackedGrid", !13, i64 0, !19, i64 128, !19, i64 136, !6, i64 144, !6, i64 152, !18, i64 160, !18, i64 164, !18, i64 168, !18, i64 172, !16, i64 176}
!13 = !{!"_ZTS13StreamingGrid", !14, i64 0}
!14 = !{!"_ZTS8IOClient", !15, i64 8, !6, i64 120}
!15 = !{!"_ZTSN5boost21recursive_timed_mutexE", !7, i64 0, !7, i64 40, !16, i64 88, !17, i64 96, !18, i64 104}
!16 = !{!"bool", !7, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = !{!"int", !7, i64 0}
!19 = !{!"long long", !7, i64 0}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = !{!18, !18, i64 0}
!23 = !{!12, !18, i64 172}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!12, !6, i64 152}
!27 = !{!12, !18, i64 168}
!28 = !{!12, !18, i64 164}
!29 = !{!12, !18, i64 160}
!30 = !{!12, !19, i64 128}
!31 = !{!12, !19, i64 136}


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

@_ZTV14FileBackedGrid = external dso_local unnamed_addr constant { [12 x ptr] }, align 8
@.str.14 = external hidden unnamed_addr constant [35 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) 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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN14FileBackedGridD2Ev(ptr noundef nonnull align 8 dereferenceable(177) %0) unnamed_addr #2 align 2 personality ptr @__gxx_personality_v0 {
  store ptr getelementptr inbounds inrange(-16, 80) (i8, ptr @_ZTV14FileBackedGrid, i64 16), ptr %0, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %0, i64 176
  %3 = load i8, ptr %2, align 8, !tbaa !8, !range !19, !noundef !20
  %4 = trunc nuw i8 %3 to i1
  br i1 %4, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 172
  %6 = load i32, ptr %5, align 4, !tbaa !21
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %8
  %.03 = phi i32 [ %9, %8 ], [ 0, %.preheader ]
  invoke void @_ZN14FileBackedGrid10_writeNextEv(ptr noundef nonnull align 8 dereferenceable(177) %0)
          to label %8 unwind label %27

8:                                                ; preds = %.lr.ph
  %9 = add nuw nsw i32 %.03, 1
  %10 = load i32, ptr %5, align 4, !tbaa !21
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %.lr.ph, label %.loopexit, !llvm.loop !22

.loopexit:                                        ; preds = %8, %.preheader, %1
  %12 = getelementptr inbounds i8, ptr %0, i64 152
  %13 = load ptr, ptr %12, align 8, !tbaa !24
  %.not = icmp eq ptr %13, null
  br i1 %.not, label %15, label %14

14:                                               ; preds = %.loopexit
  tail call void @free(ptr noundef nonnull %13) #7
  store ptr null, ptr %12, align 8, !tbaa !24
  br label %15

15:                                               ; preds = %14, %.loopexit
  %16 = getelementptr inbounds i8, ptr %0, i64 160
  %17 = getelementptr inbounds i8, ptr %0, i64 144
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %16, i8 0, i64 16, i1 false)
  tail call void @_Z15CloseFileHandleRP11_FileHandle(ptr noundef nonnull align 8 dereferenceable(8) %17)
  %18 = getelementptr inbounds i8, ptr %0, i64 128
  %19 = load i64, ptr %18, align 8, !tbaa !25
  %20 = ashr i64 %19, 20
  %21 = getelementptr inbounds i8, ptr %0, i64 136
  %22 = load i64, ptr %21, align 8, !tbaa !26
  %23 = ashr i64 %22, 20
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.14, i64 noundef %20, i64 noundef %23)
  %25 = load ptr, ptr @stdout, align 8, !tbaa !27
  %26 = tail call i32 @fflush(ptr noundef %25)
  tail call void @_ZN13StreamingGridD2Ev(ptr noundef nonnull align 8 dereferenceable(128) %0) #7
  ret void

27:                                               ; preds = %.lr.ph
  %28 = landingpad { ptr, i32 }
          catch ptr null
  %29 = extractvalue { ptr, i32 } %28, 0
  tail call void @__clang_call_terminate(ptr %29) #8
  unreachable
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14FileBackedGrid10_writeNextEv(ptr noundef nonnull align 8 dereferenceable(177)) local_unnamed_addr #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z15CloseFileHandleRP11_FileHandle(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #4

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

; Function Attrs: nofree nounwind
declare noundef i32 @fflush(ptr nocapture noundef) local_unnamed_addr #5

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN13StreamingGridD2Ev(ptr noundef nonnull align 8 dereferenceable(128)) unnamed_addr #2 align 2

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

attributes #0 = { noinline noreturn nounwind uwtable "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 = { mustprogress 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 = { mustprogress 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 = { inlinehint mustprogress 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 #5 = { 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 #6 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #7 = { nounwind }
attributes #8 = { noreturn 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 = !{!"vtable pointer", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !14, i64 176}
!9 = !{!"_ZTS14FileBackedGrid", !10, i64 0, !18, i64 128, !18, i64 136, !17, i64 144, !17, i64 152, !16, i64 160, !16, i64 164, !16, i64 168, !16, i64 172, !14, i64 176}
!10 = !{!"_ZTS13StreamingGrid", !11, i64 0}
!11 = !{!"_ZTS8IOClient", !12, i64 8, !17, i64 120}
!12 = !{!"_ZTSN5boost21recursive_timed_mutexE", !13, i64 0, !13, i64 40, !14, i64 88, !15, i64 96, !16, i64 104}
!13 = !{!"omnipotent char", !7, i64 0}
!14 = !{!"bool", !13, i64 0}
!15 = !{!"long", !13, i64 0}
!16 = !{!"int", !13, i64 0}
!17 = !{!"any pointer", !13, i64 0}
!18 = !{!"long long", !13, i64 0}
!19 = !{i8 0, i8 2}
!20 = !{}
!21 = !{!9, !16, i64 172}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!9, !17, i64 152}
!25 = !{!9, !18, i64 128}
!26 = !{!9, !18, i64 136}
!27 = !{!17, !17, i64 0}


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