; LLVM IR from paroj/gltut :: glsdk/glutil/source/Shader.ll (ZN6glutil11LinkProgramEjN4refs9array_refIjEE)

; 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.refs::array_ref.13" = type { ptr, i64 }

@_ZN2gl12AttachShaderE = external global ptr, align 8
@_ZN2gl11LinkProgramE = external global ptr, align 8
@_ZN2gl12DetachShaderE = external global ptr, align 8

; 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
declare hidden void @_ZN6glutil12_GLOBAL__N_124ThrowIfProgramLinkFailedEj(i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6glutil11LinkProgramEjN4refs9array_refIjEE(i32 noundef %0, ptr %1, i64 %2) #1 {
  %4 = alloca %"class.refs::array_ref.13", align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = getelementptr inbounds { ptr, i64 }, ptr %4, i32 0, i32 0
  store ptr %1, ptr %8, align 8
  %9 = getelementptr inbounds { ptr, i64 }, ptr %4, i32 0, i32 1
  store i64 %2, ptr %9, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %21, %3
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = call noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %24

15:                                               ; preds = %10
  %16 = load ptr, ptr @_ZN2gl12AttachShaderE, align 8, !tbaa !11
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK4refs9array_refIjEixEm(ptr noundef nonnull align 8 dereferenceable(16) %4, i64 noundef %18)
  %20 = load i32, ptr %19, align 4, !tbaa !5
  call void %16(i32 noundef %17, i32 noundef %20)
  br label %21

21:                                               ; preds = %15
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = add i64 %22, 1
  store i64 %23, ptr %6, align 8, !tbaa !9
  br label %10, !llvm.loop !13

24:                                               ; preds = %14
  %25 = load ptr, ptr @_ZN2gl11LinkProgramE, align 8, !tbaa !11
  %26 = load i32, ptr %5, align 4, !tbaa !5
  call void %25(i32 noundef %26)
  %27 = load i32, ptr %5, align 4, !tbaa !5
  call void @_ZN6glutil12_GLOBAL__N_124ThrowIfProgramLinkFailedEj(i32 noundef %27)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %39, %24
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = call noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %31 = icmp ult i64 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %42

33:                                               ; preds = %28
  %34 = load ptr, ptr @_ZN2gl12DetachShaderE, align 8, !tbaa !11
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK4refs9array_refIjEixEm(ptr noundef nonnull align 8 dereferenceable(16) %4, i64 noundef %36)
  %38 = load i32, ptr %37, align 4, !tbaa !5
  call void %34(i32 noundef %35, i32 noundef %38)
  br label %39

39:                                               ; preds = %33
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = add i64 %40, 1
  store i64 %41, ptr %7, align 8, !tbaa !9
  br label %28, !llvm.loop !15

42:                                               ; preds = %32
  %43 = load i32, ptr %5, align 4, !tbaa !5
  ret i32 %43
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNK4refs9array_refIjEixEm(ptr noundef nonnull align 8 dereferenceable(16), i64 noundef) #2 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 = { 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 = { 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 = !{!"int", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

%"class.refs::array_ref.13" = type { ptr, i64 }

@_ZN2gl12AttachShaderE = external local_unnamed_addr global ptr, align 8
@_ZN2gl11LinkProgramE = external local_unnamed_addr global ptr, align 8
@_ZN2gl12DetachShaderE = external local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN6glutil12_GLOBAL__N_124ThrowIfProgramLinkFailedEj(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6glutil11LinkProgramEjN4refs9array_refIjEE(i32 noundef returned %0, ptr %1, i64 %2) local_unnamed_addr #0 {
  %4 = alloca %"class.refs::array_ref.13", align 8
  store ptr %1, ptr %4, align 8
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  store i64 %2, ptr %5, align 8
  %6 = call noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %3
  %7 = load ptr, ptr @_ZN2gl11LinkProgramE, align 8, !tbaa !5
  call void %7(i32 noundef %0)
  call void @_ZN6glutil12_GLOBAL__N_124ThrowIfProgramLinkFailedEj(i32 noundef %0)
  %8 = call noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %.not17 = icmp eq i64 %8, 0
  br i1 %.not17, label %._crit_edge16, label %.lr.ph15

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.01112 = phi i64 [ %12, %.lr.ph ], [ 0, %3 ]
  %9 = load ptr, ptr @_ZN2gl12AttachShaderE, align 8, !tbaa !5
  %10 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK4refs9array_refIjEixEm(ptr noundef nonnull align 8 dereferenceable(16) %4, i64 noundef %.01112)
  %11 = load i32, ptr %10, align 4, !tbaa !9
  call void %9(i32 noundef %0, i32 noundef %11)
  %12 = add nuw i64 %.01112, 1
  %13 = call noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge16:                                    ; preds = %.lr.ph15, %._crit_edge
  ret i32 %0

.lr.ph15:                                         ; preds = %._crit_edge, %.lr.ph15
  %.013 = phi i64 [ %18, %.lr.ph15 ], [ 0, %._crit_edge ]
  %15 = load ptr, ptr @_ZN2gl12DetachShaderE, align 8, !tbaa !5
  %16 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK4refs9array_refIjEixEm(ptr noundef nonnull align 8 dereferenceable(16) %4, i64 noundef %.013)
  %17 = load i32, ptr %16, align 4, !tbaa !9
  call void %15(i32 noundef %0, i32 noundef %17)
  %18 = add nuw i64 %.013, 1
  %19 = call noundef i64 @_ZNK4refs9array_refIjE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %20 = icmp ult i64 %18, %19
  br i1 %20, label %.lr.ph15, label %._crit_edge16, !llvm.loop !13
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNK4refs9array_refIjEixEm(ptr noundef nonnull align 8 dereferenceable(16), i64 noundef) local_unnamed_addr #1 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" }
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" }

!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"}
!13 = distinct !{!13, !12}


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