; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_tab)

; 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.GCtab = type { %struct.GCRef, i8, i8, i8, i8, %struct.MRef, %struct.GCRef, %struct.GCRef, ptr, %struct.MRef, i32, i32 }
%struct.GCRef = type { i32 }
%struct.MRef = type { i32 }
%union.TValue = type { i64 }
%struct.anon = type { %union.anon, i32 }
%union.anon = type { %struct.GCRef }

; 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 hidden i32 @lj_tab_arraylen(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.GCtab, ptr %4, i32 0, i32 10
  %6 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %6, ptr %3, align 4, !tbaa !14
  br label %7

7:                                                ; preds = %26, %1
  %8 = load i32, ptr %3, align 4, !tbaa !14
  %9 = icmp ugt i32 %8, 1
  br i1 %9, label %10, label %24

10:                                               ; preds = %7
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.GCtab, ptr %11, i32 0, i32 5
  %13 = getelementptr inbounds %struct.MRef, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !15
  %15 = zext i32 %14 to i64
  %16 = inttoptr i64 %15 to ptr
  %17 = load i32, ptr %3, align 4, !tbaa !14
  %18 = sub i32 %17, 1
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds %union.TValue, ptr %16, i64 %19
  %21 = getelementptr inbounds %struct.anon, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !16
  %23 = icmp eq i32 %22, -1
  br label %24

24:                                               ; preds = %10, %7
  %25 = phi i1 [ false, %7 ], [ %23, %10 ]
  br i1 %25, label %26, label %29

26:                                               ; preds = %24
  %27 = load i32, ptr %3, align 4, !tbaa !14
  %28 = add i32 %27, -1
  store i32 %28, ptr %3, align 4, !tbaa !14
  br label %7, !llvm.loop !17

29:                                               ; preds = %24
  %30 = load i32, ptr %3, align 4, !tbaa !14
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %35

32:                                               ; preds = %29
  %33 = load i32, ptr %3, align 4, !tbaa !14
  %34 = add i32 %33, -1
  store i32 %34, ptr %3, align 4, !tbaa !14
  br label %35

35:                                               ; preds = %32, %29
  %36 = load i32, ptr %3, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %36
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 36}
!10 = !{!"GCtab", !11, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !13, i64 8, !11, i64 12, !11, i64 16, !6, i64 24, !13, i64 32, !12, i64 36, !12, i64 40}
!11 = !{!"GCRef", !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"MRef", !12, i64 0}
!14 = !{!12, !12, i64 0}
!15 = !{!10, !12, i64 8}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%union.TValue = type { i64 }

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define hidden range(i32 0, -1) i32 @lj_tab_arraylen(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 36
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = zext i32 %3 to i64
  %6 = icmp ne i32 %3, 0
  %umin = zext i1 %6 to i32
  br label %7

7:                                                ; preds = %9, %1
  %indvars.iv = phi i64 [ %13, %9 ], [ %5, %1 ]
  %8 = icmp ugt i64 %indvars.iv, 1
  br i1 %8, label %9, label %.critedge

9:                                                ; preds = %7
  %10 = load i32, ptr %4, align 8, !tbaa !13
  %11 = zext i32 %10 to i64
  %12 = inttoptr i64 %11 to ptr
  %13 = add nsw i64 %indvars.iv, -1
  %14 = getelementptr inbounds %union.TValue, ptr %12, i64 %13
  %15 = getelementptr inbounds i8, ptr %14, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !14
  %17 = icmp eq i32 %16, -1
  br i1 %17, label %7, label %.critedge.split.loop.exit8, !llvm.loop !15

.critedge.split.loop.exit8:                       ; preds = %9
  %18 = trunc nuw i64 %indvars.iv to i32
  br label %.critedge

.critedge:                                        ; preds = %7, %.critedge.split.loop.exit8
  %.0.lcssa = phi i32 [ %18, %.critedge.split.loop.exit8 ], [ %umin, %7 ]
  %spec.select = tail call i32 @llvm.usub.sat.i32(i32 %.0.lcssa, i32 1)
  ret i32 %spec.select
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.usub.sat.i32(i32, i32) #1

attributes #0 = { nofree norecurse nosync nounwind memory(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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !8, i64 36}
!6 = !{!"GCtab", !7, i64 0, !9, i64 4, !9, i64 5, !9, i64 6, !9, i64 7, !11, i64 8, !7, i64 12, !7, i64 16, !12, i64 24, !11, i64 32, !8, i64 36, !8, i64 40}
!7 = !{!"GCRef", !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"MRef", !8, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!6, !8, i64 8}
!14 = !{!9, !9, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass