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

; 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 noundef ptr @_ZN16SharedMemoryGridixEi(ptr noundef nonnull align 8 dereferenceable(176) %0, i32 noundef %1) unnamed_addr #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %6 = load ptr, ptr %3, align 8
  %7 = getelementptr inbounds %class.SharedMemoryGrid, ptr %6, i32 0, i32 2
  %8 = load i8, ptr %7, align 1, !tbaa !11, !range !18, !noundef !19
  %9 = trunc i8 %8 to i1
  br i1 %9, label %30, label %10

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

11:                                               ; preds = %26, %10
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = getelementptr inbounds %class.SharedMemoryGrid, ptr %6, i32 0, i32 9
  %14 = load i32, ptr %13, align 4, !tbaa !20
  %15 = icmp slt i32 %12, %14
  br i1 %15, label %17, label %16

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

17:                                               ; preds = %11
  %18 = getelementptr inbounds %class.SharedMemoryGrid, ptr %6, i32 0, i32 7
  %19 = load ptr, ptr %18, align 8, !tbaa !21
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds %struct.Signal, ptr %19, i64 %21
  %23 = getelementptr inbounds %class.SharedMemoryGrid, ptr %6, i32 0, i32 8
  %24 = load i8, ptr %23, align 8, !tbaa !22, !range !18, !noundef !19
  %25 = trunc i8 %24 to i1
  call void @_ZN9Signaller5StartER6Signalbxb(ptr noundef nonnull align 8 dereferenceable(136) %22, i1 noundef zeroext %25, i64 noundef 100, i1 noundef zeroext false)
  br label %26

26:                                               ; preds = %17
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !23

29:                                               ; preds = %16
  br label %30

30:                                               ; preds = %29, %2
  %31 = getelementptr inbounds %class.SharedMemoryGrid, ptr %6, i32 0, i32 2
  store i8 1, ptr %31, align 1, !tbaa !11
  %32 = getelementptr inbounds %class.SharedMemoryGrid, ptr %6, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !25
  ret ptr %33
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9Signaller5StartER6Signalbxb(ptr noundef nonnull align 8 dereferenceable(136), i1 noundef zeroext, i64 noundef, 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, !16, i64 129}
!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 = !{i8 0, i8 2}
!19 = !{}
!20 = !{!12, !10, i64 172}
!21 = !{!12, !6, i64 160}
!22 = !{!12, !16, i64 168}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!12, !6, 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"

%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 noundef ptr @_ZN16SharedMemoryGridixEi(ptr nocapture noundef nonnull align 8 dereferenceable(176) %0, i32 noundef %1) unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 129
  %4 = load i8, ptr %3, align 1, !tbaa !5, !range !16, !noundef !17
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %.loopexit, label %.preheader

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

.lr.ph:                                           ; preds = %.preheader
  %9 = getelementptr inbounds i8, ptr %0, i64 160
  %10 = getelementptr inbounds i8, ptr %0, i64 168
  br label %11

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

.loopexit:                                        ; preds = %11, %.preheader, %2
  store i8 1, ptr %3, align 1, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %0, i64 136
  %20 = load ptr, ptr %19, align 8, !tbaa !23
  ret ptr %20
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9Signaller5StartER6Signalbxb(ptr noundef nonnull align 8 dereferenceable(136), i1 noundef zeroext, i64 noundef, 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, !12, i64 129}
!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 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!6, !14, i64 172}
!19 = !{!6, !15, i64 160}
!20 = !{!6, !12, i64 168}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!6, !15, i64 136}


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