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

; 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.SharedMemoryGrid = type { %class.StreamingGrid, i8, i8, ptr, i32, i32, i32, ptr, i8, i32 }
%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 }
%struct.Signal = type { %"class.boost::mutex", i8, %"class.boost::condition_variable" }
%"class.boost::mutex" = type { %union.pthread_mutex_t }
%"class.boost::condition_variable" = type { %union.pthread_mutex_t, %union.pthread_cond_t }

; 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: mustprogress uwtable
define dso_local void @_ZN16SharedMemoryGrid7advanceEv(ptr noundef nonnull align 8 dereferenceable(176) %0) unnamed_addr #1 align 2 {
  %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
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %20, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = getelementptr inbounds %class.SharedMemoryGrid, ptr %4, i32 0, i32 9
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = icmp slt i32 %6, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %5
  %12 = getelementptr inbounds %class.SharedMemoryGrid, ptr %4, i32 0, i32 7
  %13 = load ptr, ptr %12, align 8, !tbaa !18
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds %struct.Signal, ptr %13, i64 %15
  %17 = getelementptr inbounds %class.SharedMemoryGrid, ptr %4, i32 0, i32 8
  %18 = load i8, ptr %17, align 8, !tbaa !19, !range !20, !noundef !21
  %19 = trunc i8 %18 to i1
  call void @_ZN9Signaller4StopER6Signalb(ptr noundef nonnull align 8 dereferenceable(136) %16, i1 noundef zeroext %19)
  br label %20

20:                                               ; preds = %11
  %21 = load i32, ptr %3, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !22

23:                                               ; preds = %10
  %24 = getelementptr inbounds %class.SharedMemoryGrid, ptr %4, i32 0, i32 2
  store i8 0, ptr %24, align 1, !tbaa !24
  %25 = getelementptr inbounds %class.SharedMemoryGrid, ptr %4, i32 0, i32 6
  %26 = load i32, ptr %25, align 8, !tbaa !25
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %25, align 8, !tbaa !25
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9Signaller4StopER6Signalb(ptr noundef nonnull align 8 dereferenceable(136), i1 noundef zeroext) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 172}
!12 = !{!"_ZTS16SharedMemoryGrid", !13, i64 0, !16, i64 128, !16, i64 129, !6, i64 136, !10, i64 144, !10, i64 148, !10, i64 152, !6, i64 160, !16, i64 168, !10, i64 172}
!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, !10, i64 104}
!16 = !{!"bool", !7, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = !{!12, !6, i64 160}
!19 = !{!12, !16, i64 168}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!12, !16, i64 129}
!25 = !{!12, !10, i64 152}


; 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.Signal = type { %"class.boost::mutex", i8, %"class.boost::condition_variable" }
%"class.boost::mutex" = type { %union.pthread_mutex_t }
%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 }
%"class.boost::condition_variable" = type { %union.pthread_mutex_t, %union.pthread_cond_t }
%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 }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN16SharedMemoryGrid7advanceEv(ptr nocapture noundef nonnull align 8 dereferenceable(176) %0) unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 172
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 160
  %6 = getelementptr inbounds i8, ptr %0, i64 168
  br label %11

._crit_edge:                                      ; preds = %11, %1
  %7 = getelementptr inbounds i8, ptr %0, i64 129
  store i8 0, ptr %7, align 1, !tbaa !16
  %8 = getelementptr inbounds i8, ptr %0, i64 152
  %9 = load i32, ptr %8, align 8, !tbaa !17
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %8, align 8, !tbaa !17
  ret void

11:                                               ; preds = %.lr.ph, %11
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %11 ]
  %12 = load ptr, ptr %5, align 8, !tbaa !18
  %13 = getelementptr inbounds %struct.Signal, ptr %12, i64 %indvars.iv
  %14 = load i8, ptr %6, align 8, !tbaa !19, !range !20, !noundef !21
  %15 = trunc nuw i8 %14 to i1
  tail call void @_ZN9Signaller4StopER6Signalb(ptr noundef nonnull align 8 dereferenceable(136) %13, i1 noundef zeroext %15)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next, %17
  br i1 %18, label %11, label %._crit_edge, !llvm.loop !22
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9Signaller4StopER6Signalb(ptr noundef nonnull align 8 dereferenceable(136), i1 noundef zeroext) local_unnamed_addr #0 align 2

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

!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, !14, i64 172}
!6 = !{!"_ZTS16SharedMemoryGrid", !7, i64 0, !12, i64 128, !12, i64 129, !15, i64 136, !14, i64 144, !14, i64 148, !14, i64 152, !15, i64 160, !12, i64 168, !14, i64 172}
!7 = !{!"_ZTS13StreamingGrid", !8, i64 0}
!8 = !{!"_ZTS8IOClient", !9, i64 8, !15, i64 120}
!9 = !{!"_ZTSN5boost21recursive_timed_mutexE", !10, i64 0, !10, i64 40, !12, i64 88, !13, i64 96, !14, i64 104}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"bool", !10, i64 0}
!13 = !{!"long", !10, i64 0}
!14 = !{!"int", !10, i64 0}
!15 = !{!"any pointer", !10, i64 0}
!16 = !{!6, !12, i64 129}
!17 = !{!6, !14, i64 152}
!18 = !{!6, !15, i64 160}
!19 = !{!6, !12, i64 168}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


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