; LLVM IR from heremaps/tin-terrain :: src/BinaryIO.ll (ZN4tntn8BinaryIO9read_implEPvmmPKcRNS_20BinaryIOErrorTrackerE)

; 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.tntn::BinaryIO" = type { %"class.std::shared_ptr", i32, i64, i64 }
%"class.std::shared_ptr" = type { %"class.std::__shared_ptr" }
%"class.std::__shared_ptr" = type { ptr, %"class.std::__shared_count" }
%"class.std::__shared_count" = type { ptr }
%"struct.tntn::BinaryIOErrorTracker" = type { %"struct.tntn::BinaryIOError", %"struct.tntn::BinaryIOError" }
%"struct.tntn::BinaryIOError" = type { ptr, i64, i32, i64, i64 }

; 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 i64 @_ZN4tntn8BinaryIO9read_implEPvmmPKcRNS_20BinaryIOErrorTrackerE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef nonnull align 8 dereferenceable(80) %5) #1 align 2 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !5
  %17 = load ptr, ptr %7, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %18, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %19 = load i64, ptr %9, align 8, !tbaa !9
  %20 = load i64, ptr %10, align 8, !tbaa !9
  %21 = mul i64 %19, %20
  store i64 %21, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %22 = getelementptr inbounds %"class.tntn::BinaryIO", ptr %17, i32 0, i32 0
  %23 = call noundef ptr @_ZNKSt19__shared_ptr_accessIN4tntn8FileLikeELN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1) %22) #5
  %24 = getelementptr inbounds %"class.tntn::BinaryIO", ptr %17, i32 0, i32 2
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = load i64, ptr %14, align 8, !tbaa !9
  %28 = load ptr, ptr %23, align 8, !tbaa !17
  %29 = getelementptr inbounds ptr, ptr %28, i64 5
  %30 = load ptr, ptr %29, align 8
  %31 = call noundef i64 %30(ptr noundef nonnull align 8 dereferenceable(8) %23, i64 noundef %25, ptr noundef %26, i64 noundef %27)
  store i64 %31, ptr %15, align 8, !tbaa !9
  %32 = load i64, ptr %15, align 8, !tbaa !9
  %33 = load i64, ptr %14, align 8, !tbaa !9
  %34 = icmp ne i64 %32, %33
  br i1 %34, label %35, label %67

35:                                               ; preds = %6
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = load ptr, ptr %12, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %37, i32 0, i32 1
  %39 = getelementptr inbounds %"struct.tntn::BinaryIOError", ptr %38, i32 0, i32 0
  store ptr %36, ptr %39, align 8, !tbaa !19
  %40 = getelementptr inbounds %"class.tntn::BinaryIO", ptr %17, i32 0, i32 2
  %41 = load i64, ptr %40, align 8, !tbaa !11
  %42 = load ptr, ptr %12, align 8, !tbaa !5
  %43 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %42, i32 0, i32 1
  %44 = getelementptr inbounds %"struct.tntn::BinaryIOError", ptr %43, i32 0, i32 1
  store i64 %41, ptr %44, align 8, !tbaa !23
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %45, i32 0, i32 1
  %47 = getelementptr inbounds %"struct.tntn::BinaryIOError", ptr %46, i32 0, i32 2
  store i32 1, ptr %47, align 8, !tbaa !24
  %48 = load i64, ptr %15, align 8, !tbaa !9
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  %50 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %49, i32 0, i32 1
  %51 = getelementptr inbounds %"struct.tntn::BinaryIOError", ptr %50, i32 0, i32 4
  store i64 %48, ptr %51, align 8, !tbaa !25
  %52 = load i64, ptr %14, align 8, !tbaa !9
  %53 = load ptr, ptr %12, align 8, !tbaa !5
  %54 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %53, i32 0, i32 1
  %55 = getelementptr inbounds %"struct.tntn::BinaryIOError", ptr %54, i32 0, i32 3
  store i64 %52, ptr %55, align 8, !tbaa !26
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %56, i32 0, i32 0
  %58 = getelementptr inbounds %"struct.tntn::BinaryIOError", ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !27
  %60 = icmp ne ptr %59, null
  br i1 %60, label %66, label %61

61:                                               ; preds = %35
  %62 = load ptr, ptr %12, align 8, !tbaa !5
  %63 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = getelementptr inbounds %"struct.tntn::BinaryIOErrorTracker", ptr %64, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %65, ptr align 8 %63, i64 40, i1 false), !tbaa.struct !28
  br label %66

66:                                               ; preds = %61, %35
  br label %67

67:                                               ; preds = %66, %6
  %68 = getelementptr inbounds %"class.tntn::BinaryIO", ptr %17, i32 0, i32 1
  %69 = load i32, ptr %68, align 8, !tbaa !30
  %70 = icmp ne i32 1, %69
  br i1 %70, label %71, label %90

71:                                               ; preds = %67
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  %72 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %72, ptr %16, align 8, !tbaa !5
  br label %73

73:                                               ; preds = %85, %71
  %74 = load ptr, ptr %16, align 8, !tbaa !5
  %75 = load ptr, ptr %13, align 8, !tbaa !5
  %76 = load i64, ptr %14, align 8, !tbaa !9
  %77 = getelementptr inbounds i8, ptr %75, i64 %76
  %78 = icmp ult ptr %74, %77
  br i1 %78, label %80, label %79

79:                                               ; preds = %73
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  br label %89

80:                                               ; preds = %73
  %81 = load ptr, ptr %16, align 8, !tbaa !5
  %82 = load ptr, ptr %16, align 8, !tbaa !5
  %83 = load i64, ptr %9, align 8, !tbaa !9
  %84 = getelementptr inbounds i8, ptr %82, i64 %83
  call void @_ZSt7reverseIPhEvT_S1_(ptr noundef %81, ptr noundef %84)
  br label %85

85:                                               ; preds = %80
  %86 = load i64, ptr %9, align 8, !tbaa !9
  %87 = load ptr, ptr %16, align 8, !tbaa !5
  %88 = getelementptr inbounds i8, ptr %87, i64 %86
  store ptr %88, ptr %16, align 8, !tbaa !5
  br label %73, !llvm.loop !31

89:                                               ; preds = %79
  br label %90

90:                                               ; preds = %89, %67
  %91 = load i64, ptr %15, align 8, !tbaa !9
  %92 = getelementptr inbounds %"class.tntn::BinaryIO", ptr %17, i32 0, i32 2
  %93 = load i64, ptr %92, align 8, !tbaa !11
  %94 = add i64 %93, %91
  store i64 %94, ptr %92, align 8, !tbaa !11
  %95 = load i64, ptr %15, align 8, !tbaa !9
  %96 = load i64, ptr %9, align 8, !tbaa !9
  %97 = udiv i64 %95, %96
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  ret i64 %97
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNKSt19__shared_ptr_accessIN4tntn8FileLikeELN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1)) #2 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt7reverseIPhEvT_S1_(ptr noundef, ptr noundef) #4

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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" }
attributes #5 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"_ZTSN4tntn8BinaryIOE", !13, i64 0, !16, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!"_ZTSSt10shared_ptrIN4tntn8FileLikeEE", !14, i64 0}
!14 = !{!"_ZTSSt12__shared_ptrIN4tntn8FileLikeELN9__gnu_cxx12_Lock_policyE2EE", !6, i64 0, !15, i64 8}
!15 = !{!"_ZTSSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EE", !6, i64 0}
!16 = !{!"_ZTSN4tntn10EndiannessE", !7, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"vtable pointer", !8, i64 0}
!19 = !{!20, !6, i64 40}
!20 = !{!"_ZTSN4tntn20BinaryIOErrorTrackerE", !21, i64 0, !21, i64 40}
!21 = !{!"_ZTSN4tntn13BinaryIOErrorE", !6, i64 0, !10, i64 8, !22, i64 16, !10, i64 24, !10, i64 32}
!22 = !{!"_ZTSN4tntn13BinaryIOError7InOrOutE", !7, i64 0}
!23 = !{!20, !10, i64 48}
!24 = !{!20, !22, i64 56}
!25 = !{!20, !10, i64 72}
!26 = !{!20, !10, i64 64}
!27 = !{!20, !6, i64 0}
!28 = !{i64 0, i64 8, !5, i64 8, i64 8, !9, i64 16, i64 4, !29, i64 24, i64 8, !9, i64 32, i64 8, !9}
!29 = !{!22, !22, i64 0}
!30 = !{!12, !16, i64 16}
!31 = distinct !{!31, !32}
!32 = !{!"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"

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN4tntn8BinaryIO9read_implEPvmmPKcRNS_20BinaryIOErrorTrackerE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4, ptr nocapture noundef nonnull align 8 dereferenceable(80) %5) local_unnamed_addr #0 align 2 {
  %7 = mul i64 %3, %2
  %8 = tail call noundef ptr @_ZNKSt19__shared_ptr_accessIN4tntn8FileLikeELN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1) %0) #4
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = load ptr, ptr %8, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %11, i64 40
  %13 = load ptr, ptr %12, align 8
  %14 = tail call noundef i64 %13(ptr noundef nonnull align 8 dereferenceable(8) %8, i64 noundef %10, ptr noundef %1, i64 noundef %7)
  %.not = icmp eq i64 %14, %7
  br i1 %.not, label %24, label %15

15:                                               ; preds = %6
  %16 = getelementptr inbounds i8, ptr %5, i64 40
  store ptr %4, ptr %16, align 8, !tbaa !17
  %17 = load i64, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %5, i64 48
  store i64 %17, ptr %18, align 8, !tbaa !21
  %19 = getelementptr inbounds i8, ptr %5, i64 56
  store i32 1, ptr %19, align 8, !tbaa !22
  %20 = getelementptr inbounds i8, ptr %5, i64 72
  store i64 %14, ptr %20, align 8, !tbaa !23
  %21 = getelementptr inbounds i8, ptr %5, i64 64
  store i64 %7, ptr %21, align 8, !tbaa !24
  %22 = load ptr, ptr %5, align 8, !tbaa !25
  %.not32 = icmp eq ptr %22, null
  br i1 %.not32, label %23, label %24

23:                                               ; preds = %15
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %5, ptr noundef nonnull align 8 dereferenceable(40) %16, i64 40, i1 false), !tbaa.struct !26
  br label %24

24:                                               ; preds = %15, %23, %6
  %25 = getelementptr inbounds i8, ptr %0, i64 16
  %26 = load i32, ptr %25, align 8, !tbaa !30
  %.not33 = icmp eq i32 %26, 1
  br i1 %.not33, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %24
  %27 = getelementptr inbounds i8, ptr %1, i64 %7
  %28 = icmp sgt i64 %7, 0
  br i1 %28, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.034 = phi ptr [ %29, %.lr.ph ], [ %1, %.preheader ]
  %29 = getelementptr inbounds i8, ptr %.034, i64 %2
  tail call void @_ZSt7reverseIPhEvT_S1_(ptr noundef %.034, ptr noundef %29)
  %30 = icmp ult ptr %29, %27
  br i1 %30, label %.lr.ph, label %.loopexit, !llvm.loop !31

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %24
  %31 = load i64, ptr %9, align 8, !tbaa !5
  %32 = add i64 %31, %14
  store i64 %32, ptr %9, align 8, !tbaa !5
  %33 = udiv i64 %14, %2
  ret i64 %33
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNKSt19__shared_ptr_accessIN4tntn8FileLikeELN9__gnu_cxx12_Lock_policyE2ELb0ELb0EEptEv(ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt7reverseIPhEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #3

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 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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" }
attributes #4 = { 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, !14, i64 24}
!6 = !{!"_ZTSN4tntn8BinaryIOE", !7, i64 0, !13, i64 16, !14, i64 24, !14, i64 32}
!7 = !{!"_ZTSSt10shared_ptrIN4tntn8FileLikeEE", !8, i64 0}
!8 = !{!"_ZTSSt12__shared_ptrIN4tntn8FileLikeELN9__gnu_cxx12_Lock_policyE2EE", !9, i64 0, !12, i64 8}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"_ZTSSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EE", !9, i64 0}
!13 = !{!"_ZTSN4tntn10EndiannessE", !10, i64 0}
!14 = !{!"long", !10, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"vtable pointer", !11, i64 0}
!17 = !{!18, !9, i64 40}
!18 = !{!"_ZTSN4tntn20BinaryIOErrorTrackerE", !19, i64 0, !19, i64 40}
!19 = !{!"_ZTSN4tntn13BinaryIOErrorE", !9, i64 0, !14, i64 8, !20, i64 16, !14, i64 24, !14, i64 32}
!20 = !{!"_ZTSN4tntn13BinaryIOError7InOrOutE", !10, i64 0}
!21 = !{!18, !14, i64 48}
!22 = !{!18, !20, i64 56}
!23 = !{!18, !14, i64 72}
!24 = !{!18, !14, i64 64}
!25 = !{!18, !9, i64 0}
!26 = !{i64 0, i64 8, !27, i64 8, i64 8, !28, i64 16, i64 4, !29, i64 24, i64 8, !28, i64 32, i64 8, !28}
!27 = !{!9, !9, i64 0}
!28 = !{!14, !14, i64 0}
!29 = !{!20, !20, i64 0}
!30 = !{!6, !13, i64 16}
!31 = distinct !{!31, !32}
!32 = !{!"llvm.loop.mustprogress"}


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