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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN14FileBackedGrid9_readNextEv(ptr noundef nonnull align 8 dereferenceable(177) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8
  %4 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 7
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 8
  %7 = load i32, ptr %6, align 4, !tbaa !18
  %8 = add nsw i32 %5, %7
  %9 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 5
  %10 = load i32, ptr %9, align 8, !tbaa !19
  %11 = icmp slt i32 %8, %10
  br i1 %11, label %12, label %40

12:                                               ; preds = %1
  %13 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 3
  %14 = load ptr, ptr %13, align 8, !tbaa !20
  %15 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 4
  %16 = load ptr, ptr %15, align 8, !tbaa !21
  %17 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 6
  %18 = load i32, ptr %17, align 4, !tbaa !22
  %19 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 7
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 8
  %22 = load i32, ptr %21, align 4, !tbaa !18
  %23 = add nsw i32 %20, %22
  %24 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 8
  %25 = load i32, ptr %24, align 4, !tbaa !18
  %26 = srem i32 %23, %25
  %27 = mul nsw i32 %18, %26
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %16, i64 %28
  %30 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 6
  %31 = load i32, ptr %30, align 4, !tbaa !22
  %32 = sext i32 %31 to i64
  %33 = call noundef i64 @_Z14ReadFileHandleP11_FileHandlePhx(ptr noundef %14, ptr noundef %29, i64 noundef %32)
  %34 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 6
  %35 = load i32, ptr %34, align 4, !tbaa !22
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !23
  %39 = add nsw i64 %38, %36
  store i64 %39, ptr %37, align 8, !tbaa !23
  br label %40

40:                                               ; preds = %12, %1
  %41 = getelementptr inbounds %class.FileBackedGrid, ptr %3, i32 0, i32 7
  %42 = load i32, ptr %41, align 8, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %41, align 8, !tbaa !9
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_Z14ReadFileHandleP11_FileHandlePhx(ptr noundef, ptr noundef, i64 noundef) #1

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" }
attributes #1 = { inlinehint 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !16, i64 168}
!10 = !{!"_ZTS14FileBackedGrid", !11, i64 0, !17, i64 128, !17, i64 136, !6, i64 144, !6, i64 152, !16, i64 160, !16, i64 164, !16, i64 168, !16, i64 172, !14, i64 176}
!11 = !{!"_ZTS13StreamingGrid", !12, i64 0}
!12 = !{!"_ZTS8IOClient", !13, i64 8, !6, i64 120}
!13 = !{!"_ZTSN5boost21recursive_timed_mutexE", !7, i64 0, !7, i64 40, !14, i64 88, !15, i64 96, !16, i64 104}
!14 = !{!"bool", !7, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!"long long", !7, i64 0}
!18 = !{!10, !16, i64 172}
!19 = !{!10, !16, i64 160}
!20 = !{!10, !6, i64 144}
!21 = !{!10, !6, i64 152}
!22 = !{!10, !16, i64 164}
!23 = !{!10, !17, i64 128}


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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN14FileBackedGrid9_readNextEv(ptr nocapture noundef nonnull align 8 dereferenceable(177) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 168
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 172
  %5 = load i32, ptr %4, align 4, !tbaa !17
  %6 = add nsw i32 %5, %3
  %7 = getelementptr inbounds i8, ptr %0, i64 160
  %8 = load i32, ptr %7, align 8, !tbaa !18
  %9 = icmp slt i32 %6, %8
  br i1 %9, label %10, label %28

10:                                               ; preds = %1
  %11 = getelementptr inbounds i8, ptr %0, i64 144
  %12 = load ptr, ptr %11, align 8, !tbaa !19
  %13 = getelementptr inbounds i8, ptr %0, i64 152
  %14 = load ptr, ptr %13, align 8, !tbaa !20
  %15 = getelementptr inbounds i8, ptr %0, i64 164
  %16 = load i32, ptr %15, align 4, !tbaa !21
  %17 = srem i32 %6, %5
  %18 = mul nsw i32 %17, %16
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %14, i64 %19
  %21 = sext i32 %16 to i64
  %22 = tail call noundef i64 @_Z14ReadFileHandleP11_FileHandlePhx(ptr noundef %12, ptr noundef %20, i64 noundef %21)
  %23 = load i32, ptr %15, align 4, !tbaa !21
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %0, i64 128
  %26 = load i64, ptr %25, align 8, !tbaa !22
  %27 = add nsw i64 %26, %24
  store i64 %27, ptr %25, align 8, !tbaa !22
  %.pre = load i32, ptr %2, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %10, %1
  %29 = phi i32 [ %.pre, %10 ], [ %3, %1 ]
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %2, align 8, !tbaa !5
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_Z14ReadFileHandleP11_FileHandlePhx(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

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" }
attributes #1 = { inlinehint 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 168}
!6 = !{!"_ZTS14FileBackedGrid", !7, i64 0, !16, i64 128, !16, i64 136, !15, i64 144, !15, i64 152, !14, i64 160, !14, i64 164, !14, i64 168, !14, i64 172, !12, i64 176}
!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 = !{!"long long", !10, i64 0}
!17 = !{!6, !14, i64 172}
!18 = !{!6, !14, i64 160}
!19 = !{!6, !15, i64 144}
!20 = !{!6, !15, i64 152}
!21 = !{!6, !14, i64 164}
!22 = !{!6, !16, i64 128}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass