; LLVM IR from filipwasil/fillwave :: ext/nv_helpers_gl/nv_dds.ll (ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE)

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

@.str.1 = external hidden unnamed_addr constant [82 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [47 x i8], align 1
@__PRETTY_FUNCTION__._ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE = external hidden unnamed_addr constant [64 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE(ptr noundef nonnull align 8 dereferenceable(104) %0, ptr noundef nonnull align 8 dereferenceable(112) %1, ptr noundef %2) #2 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8
  %10 = call noundef i32 @_ZN6nv_dds9CDDSImage15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(104) %9)
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = call noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112) %11)
  %13 = icmp eq i32 %10, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  br label %17

15:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str.17, ptr noundef @.str.1, i32 noundef 534, ptr noundef @__PRETTY_FUNCTION__._ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE) #7
  unreachable

16:                                               ; No predecessors!
  br label %17

17:                                               ; preds = %16, %14
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = call noundef ptr @_ZNK6nv_dds8CSurfacecvPhEv(ptr noundef nonnull align 8 dereferenceable(32) %18)
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = call noundef i32 @_ZNK6nv_dds8CSurface8get_sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %20)
  %22 = zext i32 %21 to i64
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = call i64 @fwrite(ptr noundef %19, i64 noundef 1, i64 noundef %22, ptr noundef %23)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #8
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %42, %17
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112) %27)
  %29 = icmp ult i32 %26, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #8
  br label %45

31:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #8
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = call noundef nonnull align 8 dereferenceable(32) ptr @_ZNK6nv_dds8CTexture10get_mipmapEj(ptr noundef nonnull align 8 dereferenceable(112) %32, i32 noundef %33)
  store ptr %34, ptr %8, align 8, !tbaa !5
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = call noundef ptr @_ZNK6nv_dds8CSurfacecvPhEv(ptr noundef nonnull align 8 dereferenceable(32) %35)
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = call noundef i32 @_ZNK6nv_dds8CSurface8get_sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %37)
  %39 = zext i32 %38 to i64
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = call i64 @fwrite(ptr noundef %36, i64 noundef 1, i64 noundef %39, ptr noundef %40)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #8
  br label %42

42:                                               ; preds = %31
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = add i32 %43, 1
  store i32 %44, ptr %7, align 4, !tbaa !9
  br label %25, !llvm.loop !11

45:                                               ; preds = %30
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN6nv_dds9CDDSImage15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(104)) #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112)) #4 align 2

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #5

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK6nv_dds8CSurfacecvPhEv(ptr noundef nonnull align 8 dereferenceable(32)) #6 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK6nv_dds8CSurface8get_sizeEv(ptr noundef nonnull align 8 dereferenceable(32)) #4 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(32) ptr @_ZNK6nv_dds8CTexture10get_mipmapEj(ptr noundef nonnull align 8 dereferenceable(112), i32 noundef) #4 align 2

attributes #0 = { noreturn 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 = { 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 = { "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 = { 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 #7 = { noreturn nounwind }
attributes #8 = { 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 = distinct !{!11, !12}
!12 = !{!"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"

@.str.1 = external hidden unnamed_addr constant [82 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [47 x i8], align 1
@__PRETTY_FUNCTION__._ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE = external hidden unnamed_addr constant [64 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE(ptr noundef nonnull align 8 dereferenceable(104) %0, ptr noundef nonnull align 8 dereferenceable(112) %1, ptr nocapture noundef %2) local_unnamed_addr #1 align 2 {
  %4 = tail call noundef i32 @_ZN6nv_dds9CDDSImage15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(104) %0)
  %5 = tail call noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112) %1)
  %6 = icmp eq i32 %4, %5
  br i1 %6, label %8, label %7

7:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str.17, ptr noundef nonnull @.str.1, i32 noundef 534, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6nv_dds9CDDSImage13write_textureERKNS_8CTextureEP8_IO_FILE) #6
  unreachable

8:                                                ; preds = %3
  %9 = tail call noundef ptr @_ZNK6nv_dds8CSurfacecvPhEv(ptr noundef nonnull align 8 dereferenceable(32) %1)
  %10 = tail call noundef i32 @_ZNK6nv_dds8CSurface8get_sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %1)
  %11 = zext i32 %10 to i64
  %12 = tail call i64 @fwrite(ptr noundef %9, i64 noundef 1, i64 noundef %11, ptr noundef %2)
  %13 = tail call noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112) %1)
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %8
  ret void

.lr.ph:                                           ; preds = %8, %.lr.ph
  %.012 = phi i32 [ %19, %.lr.ph ], [ 0, %8 ]
  %14 = tail call noundef nonnull align 8 dereferenceable(32) ptr @_ZNK6nv_dds8CTexture10get_mipmapEj(ptr noundef nonnull align 8 dereferenceable(112) %1, i32 noundef %.012)
  %15 = tail call noundef ptr @_ZNK6nv_dds8CSurfacecvPhEv(ptr noundef nonnull align 8 dereferenceable(32) %14)
  %16 = tail call noundef i32 @_ZNK6nv_dds8CSurface8get_sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %14)
  %17 = zext i32 %16 to i64
  %18 = tail call i64 @fwrite(ptr noundef %15, i64 noundef 1, i64 noundef %17, ptr noundef %2)
  %19 = add nuw i32 %.012, 1
  %20 = tail call noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112) %1)
  %21 = icmp ult i32 %19, %20
  br i1 %21, label %.lr.ph, label %._crit_edge, !llvm.loop !5
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN6nv_dds9CDDSImage15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(104)) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK6nv_dds8CTexture15get_num_mipmapsEv(ptr noundef nonnull align 8 dereferenceable(112)) local_unnamed_addr #3 align 2

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK6nv_dds8CSurfacecvPhEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #5 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK6nv_dds8CSurface8get_sizeEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(32) ptr @_ZNK6nv_dds8CTexture10get_mipmapEj(ptr noundef nonnull align 8 dereferenceable(112), i32 noundef) local_unnamed_addr #3 align 2

attributes #0 = { noreturn 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 #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 = { 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 #3 = { 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 #4 = { 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 #5 = { 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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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