; LLVM IR from PyCee/NUS_library :: src/gpu (NUS_binding)

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

%struct.NUS_binding = type { ptr, ptr, ptr, i32 }
%struct.NUS_queue_family = type { ptr, ptr, i32, i32, i32, i32 }
%struct.NUS_submission_queue = type { ptr, ptr, %struct.VkSubmitInfo, [10 x ptr], [10 x ptr], [10 x i32], [10 x ptr] }
%struct.VkSubmitInfo = type { i32, ptr, i32, ptr, ptr, i32, ptr, i32, ptr }

@nus_gpu_binding = external hidden global %struct.NUS_binding, 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: nounwind uwtable
define dso_local i32 @nus_select_submission_queue(ptr noundef byval(%struct.NUS_queue_family) align 8 %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 -1, ptr %3, align 4, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %35, %1
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = getelementptr inbounds %struct.NUS_queue_family, ptr %0, i32 0, i32 2
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = icmp ult i32 %5, %7
  br i1 %8, label %9, label %38

9:                                                ; preds = %4
  %10 = getelementptr inbounds %struct.NUS_queue_family, ptr %0, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds %struct.NUS_submission_queue, ptr %11, i64 %13
  %15 = getelementptr inbounds %struct.NUS_submission_queue, ptr %14, i32 0, i32 2
  %16 = getelementptr inbounds %struct.VkSubmitInfo, ptr %15, i32 0, i32 5
  %17 = load i32, ptr %16, align 8, !tbaa !13
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp ult i32 %17, %18
  br i1 %19, label %20, label %34

20:                                               ; preds = %9
  %21 = getelementptr inbounds %struct.NUS_queue_family, ptr %0, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !12
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds %struct.NUS_submission_queue, ptr %22, i64 %24
  %26 = getelementptr inbounds %struct.NUS_submission_queue, ptr %25, i32 0, i32 2
  %27 = getelementptr inbounds %struct.VkSubmitInfo, ptr %26, i32 0, i32 5
  %28 = load i32, ptr %27, align 8, !tbaa !13
  store i32 %28, ptr %3, align 4, !tbaa !5
  %29 = getelementptr inbounds %struct.NUS_queue_family, ptr %0, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds %struct.NUS_submission_queue, ptr %30, i64 %32
  store ptr %33, ptr getelementptr inbounds (%struct.NUS_binding, ptr @nus_gpu_binding, i32 0, i32 2), align 8, !tbaa !16
  br label %34

34:                                               ; preds = %20, %9
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = add i32 %36, 1
  store i32 %37, ptr %2, align 4, !tbaa !5
  br label %4, !llvm.loop !18

38:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  ret i32 1
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"NUS_queue_family", !11, i64 0, !11, i64 8, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!14, !6, i64 56}
!14 = !{!"NUS_submission_queue", !11, i64 0, !11, i64 8, !15, i64 16, !7, i64 88, !7, i64 168, !7, i64 248, !7, i64 288}
!15 = !{!"VkSubmitInfo", !6, i64 0, !11, i64 8, !6, i64 16, !11, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !6, i64 56, !11, i64 64}
!16 = !{!17, !11, i64 16}
!17 = !{!"NUS_binding", !11, i64 0, !11, i64 8, !11, i64 16, !6, i64 24}
!18 = distinct !{!18, !19}
!19 = !{!"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"

%struct.NUS_binding = type { ptr, ptr, ptr, i32 }
%struct.NUS_queue_family = type { ptr, ptr, i32, i32, i32, i32 }
%struct.NUS_submission_queue = type { ptr, ptr, %struct.VkSubmitInfo, [10 x ptr], [10 x ptr], [10 x i32], [10 x ptr] }
%struct.VkSubmitInfo = type { i32, ptr, i32, ptr, ptr, i32, ptr, i32, ptr }

@nus_gpu_binding = external hidden local_unnamed_addr global %struct.NUS_binding, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @nus_select_submission_queue(ptr nocapture noundef readonly byval(%struct.NUS_queue_family) align 8 %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !11
  %wide.trip.count = zext i32 %3 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %5 = icmp eq i32 %3, 1
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %6

6:                                                ; preds = %18, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %18 ]
  %.09 = phi i32 [ -1, %.lr.ph.new ], [ %.1.1, %18 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %18 ]
  %7 = getelementptr inbounds %struct.NUS_submission_queue, ptr %4, i64 %indvars.iv
  %8 = getelementptr inbounds i8, ptr %7, i64 56
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %10 = icmp ult i32 %9, %.09
  br i1 %10, label %11, label %12

11:                                               ; preds = %6
  store ptr %7, ptr getelementptr inbounds (i8, ptr @nus_gpu_binding, i64 16), align 8, !tbaa !15
  br label %12

12:                                               ; preds = %6, %11
  %.1 = phi i32 [ %9, %11 ], [ %.09, %6 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %13 = getelementptr inbounds %struct.NUS_submission_queue, ptr %4, i64 %indvars.iv.next
  %14 = getelementptr inbounds i8, ptr %13, i64 56
  %15 = load i32, ptr %14, align 8, !tbaa !12
  %16 = icmp ult i32 %15, %.1
  br i1 %16, label %17, label %18

17:                                               ; preds = %12
  store ptr %13, ptr getelementptr inbounds (i8, ptr @nus_gpu_binding, i64 16), align 8, !tbaa !15
  br label %18

18:                                               ; preds = %17, %12
  %.1.1 = phi i32 [ %15, %17 ], [ %.1, %12 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %6, !llvm.loop !17

._crit_edge.loopexit.unr-lcssa:                   ; preds = %18, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %18 ]
  %.09.unr = phi i32 [ -1, %.lr.ph ], [ %.1.1, %18 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa
  %19 = getelementptr inbounds %struct.NUS_submission_queue, ptr %4, i64 %indvars.iv.unr
  %20 = getelementptr inbounds i8, ptr %19, i64 56
  %21 = load i32, ptr %20, align 8, !tbaa !12
  %22 = icmp ult i32 %21, %.09.unr
  br i1 %22, label %23, label %._crit_edge

23:                                               ; preds = %.epil.preheader
  store ptr %19, ptr getelementptr inbounds (i8, ptr @nus_gpu_binding, i64 16), align 8, !tbaa !15
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %23, %.epil.preheader, %1
  ret i32 1
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) 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, !10, i64 16}
!6 = !{!"NUS_queue_family", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !10, i64 56}
!13 = !{!"NUS_submission_queue", !7, i64 0, !7, i64 8, !14, i64 16, !8, i64 88, !8, i64 168, !8, i64 248, !8, i64 288}
!14 = !{!"VkSubmitInfo", !10, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !7, i64 48, !10, i64 56, !7, i64 64}
!15 = !{!16, !7, i64 16}
!16 = !{!"NUS_binding", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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