; LLVM IR from godofecht/GANN-Object-Avoidance :: Box2D/Testbed/Framework/RenderGL3.ll (Z16AddGfxCmdScissoriiii)

; 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.GfxCmd = type { i8, i8, [2 x i8], i32, %union.anon }
%union.anon = type { %struct.GfxText }
%struct.GfxText = type { float, float, i32, ptr }
%struct.GfxRect = type { i16, i16, i16, i16, i16 }

@_ZL17g_gfxCmdQueueSize = external hidden global i32, align 4
@_ZL13g_gfxCmdQueue = external hidden global [5000 x %struct.GfxCmd], align 16

; 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 nounwind uwtable
define dso_local void @_Z16AddGfxCmdScissoriiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  %10 = load i32, ptr @_ZL17g_gfxCmdQueueSize, align 4, !tbaa !5
  %11 = icmp uge i32 %10, 5000
  br i1 %11, label %12, label %13

12:                                               ; preds = %4
  br label %49

13:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %14 = load i32, ptr @_ZL17g_gfxCmdQueueSize, align 4, !tbaa !5
  %15 = add i32 %14, 1
  store i32 %15, ptr @_ZL17g_gfxCmdQueueSize, align 4, !tbaa !5
  %16 = zext i32 %14 to i64
  %17 = getelementptr inbounds [5000 x %struct.GfxCmd], ptr @_ZL13g_gfxCmdQueue, i64 0, i64 %16
  store ptr %17, ptr %9, align 8, !tbaa !9
  %18 = load ptr, ptr %9, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.GfxCmd, ptr %18, i32 0, i32 0
  store i8 4, ptr %19, align 8, !tbaa !11
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = icmp slt i32 %20, 0
  %22 = zext i1 %21 to i64
  %23 = select i1 %21, i32 0, i32 1
  %24 = trunc i32 %23 to i8
  %25 = load ptr, ptr %9, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.GfxCmd, ptr %25, i32 0, i32 1
  store i8 %24, ptr %26, align 1, !tbaa !13
  %27 = load ptr, ptr %9, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.GfxCmd, ptr %27, i32 0, i32 3
  store i32 0, ptr %28, align 4, !tbaa !14
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = trunc i32 %29 to i16
  %31 = load ptr, ptr %9, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.GfxCmd, ptr %31, i32 0, i32 4
  %33 = getelementptr inbounds %struct.GfxRect, ptr %32, i32 0, i32 0
  store i16 %30, ptr %33, align 8, !tbaa !15
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = trunc i32 %34 to i16
  %36 = load ptr, ptr %9, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.GfxCmd, ptr %36, i32 0, i32 4
  %38 = getelementptr inbounds %struct.GfxRect, ptr %37, i32 0, i32 1
  store i16 %35, ptr %38, align 2, !tbaa !15
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = trunc i32 %39 to i16
  %41 = load ptr, ptr %9, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.GfxCmd, ptr %41, i32 0, i32 4
  %43 = getelementptr inbounds %struct.GfxRect, ptr %42, i32 0, i32 2
  store i16 %40, ptr %43, align 4, !tbaa !15
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = trunc i32 %44 to i16
  %46 = load ptr, ptr %9, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.GfxCmd, ptr %46, i32 0, i32 4
  %48 = getelementptr inbounds %struct.GfxRect, ptr %47, i32 0, i32 3
  store i16 %45, ptr %48, align 2, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %49

49:                                               ; preds = %13, %12
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"_ZTS6GfxCmd", !7, i64 0, !7, i64 1, !7, i64 2, !6, i64 4, !7, i64 8}
!13 = !{!12, !7, i64 1}
!14 = !{!12, !6, i64 4}
!15 = !{!7, !7, i64 0}


; 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.GfxCmd = type { i8, i8, [2 x i8], i32, %union.anon }
%union.anon = type { %struct.GfxText }
%struct.GfxText = type { float, float, i32, ptr }

@_ZL17g_gfxCmdQueueSize = external hidden local_unnamed_addr global i32, align 4
@_ZL13g_gfxCmdQueue = external hidden local_unnamed_addr global [5000 x %struct.GfxCmd], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z16AddGfxCmdScissoriiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = load i32, ptr @_ZL17g_gfxCmdQueueSize, align 4, !tbaa !5
  %6 = icmp ugt i32 %5, 4999
  br i1 %6, label %23, label %7

7:                                                ; preds = %4
  %8 = add nuw nsw i32 %5, 1
  store i32 %8, ptr @_ZL17g_gfxCmdQueueSize, align 4, !tbaa !5
  %9 = zext nneg i32 %5 to i64
  %10 = getelementptr inbounds [5000 x %struct.GfxCmd], ptr @_ZL13g_gfxCmdQueue, i64 0, i64 %9
  store i8 4, ptr %10, align 16, !tbaa !9
  %11 = icmp sgt i32 %0, -1
  %12 = zext i1 %11 to i8
  %13 = getelementptr inbounds i8, ptr %10, i64 1
  store i8 %12, ptr %13, align 1, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %10, i64 4
  store i32 0, ptr %14, align 4, !tbaa !12
  %15 = trunc i32 %0 to i16
  %16 = getelementptr inbounds i8, ptr %10, i64 8
  store i16 %15, ptr %16, align 8, !tbaa !13
  %17 = trunc i32 %1 to i16
  %18 = getelementptr inbounds i8, ptr %10, i64 10
  store i16 %17, ptr %18, align 2, !tbaa !13
  %19 = trunc i32 %2 to i16
  %20 = getelementptr inbounds i8, ptr %10, i64 12
  store i16 %19, ptr %20, align 4, !tbaa !13
  %21 = trunc i32 %3 to i16
  %22 = getelementptr inbounds i8, ptr %10, i64 14
  store i16 %21, ptr %22, align 2, !tbaa !13
  br label %23

23:                                               ; preds = %4, %7
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !7, i64 0}
!10 = !{!"_ZTS6GfxCmd", !7, i64 0, !7, i64 1, !7, i64 2, !6, i64 4, !7, i64 8}
!11 = !{!10, !7, i64 1}
!12 = !{!10, !6, i64 4}
!13 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass