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

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

27:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !14
  br label %28

28:                                               ; preds = %55, %27
  %29 = load i32, ptr %4, align 4, !tbaa !14
  %30 = load i32, ptr %5, align 4, !tbaa !14
  %31 = sub i32 %29, %30
  %32 = icmp ugt i32 %31, 1
  br i1 %32, label %33, label %56

33:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %34 = load i32, ptr %5, align 4, !tbaa !14
  %35 = load i32, ptr %4, align 4, !tbaa !14
  %36 = add i32 %34, %35
  %37 = udiv i32 %36, 2
  store i32 %37, ptr %6, align 4, !tbaa !14
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.GCtab, ptr %38, i32 0, i32 5
  %40 = getelementptr inbounds %struct.MRef, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !15
  %42 = zext i32 %41 to i64
  %43 = inttoptr i64 %42 to ptr
  %44 = load i32, ptr %6, align 4, !tbaa !14
  %45 = sub i32 %44, 1
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds %union.TValue, ptr %43, i64 %46
  %48 = getelementptr inbounds %struct.anon, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !16
  %50 = icmp eq i32 %49, -1
  br i1 %50, label %51, label %53

51:                                               ; preds = %33
  %52 = load i32, ptr %6, align 4, !tbaa !14
  store i32 %52, ptr %4, align 4, !tbaa !14
  br label %55

53:                                               ; preds = %33
  %54 = load i32, ptr %6, align 4, !tbaa !14
  store i32 %54, ptr %5, align 4, !tbaa !14
  br label %55

55:                                               ; preds = %53, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %28, !llvm.loop !17

56:                                               ; preds = %28
  %57 = load i32, ptr %5, align 4, !tbaa !14
  %58 = sub i32 %57, 1
  store i32 %58, ptr %2, align 4
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %76

59:                                               ; preds = %13, %1
  %60 = load i32, ptr %4, align 4, !tbaa !14
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %65

62:                                               ; preds = %59
  %63 = load i32, ptr %4, align 4, !tbaa !14
  %64 = add i32 %63, -1
  store i32 %64, ptr %4, align 4, !tbaa !14
  br label %65

65:                                               ; preds = %62, %59
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.GCtab, ptr %66, i32 0, i32 11
  %68 = load i32, ptr %67, align 8, !tbaa !19
  %69 = icmp ule i32 %68, 0
  br i1 %69, label %70, label %72

70:                                               ; preds = %65
  %71 = load i32, ptr %4, align 4, !tbaa !14
  store i32 %71, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %76

72:                                               ; preds = %65
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = load i32, ptr %4, align 4, !tbaa !14
  %75 = call i32 @unbound_search(ptr noundef %73, i32 noundef %74)
  store i32 %75, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %76

76:                                               ; preds = %72, %70, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %77 = load i32, ptr %2, align 4
  ret i32 %77
}

; Function Attrs: nounwind uwtable
declare hidden i32 @unbound_search(ptr noundef, i32 noundef) #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 = !{!"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"}
!19 = !{!10, !12, i64 40}


; 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: nounwind uwtable
define hidden i32 @lj_tab_len(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 36
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp ugt i32 %3, 1
  br i1 %4, label %5, label %27

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !13
  %8 = zext i32 %7 to i64
  %9 = inttoptr i64 %8 to ptr
  %10 = add i32 %3, -1
  %11 = zext i32 %10 to i64
  %12 = getelementptr inbounds %union.TValue, ptr %9, i64 %11
  %13 = getelementptr inbounds i8, ptr %12, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !14
  %15 = icmp eq i32 %14, -1
  br i1 %15, label %.preheader, label %27

.preheader:                                       ; preds = %5
  %invariant.gep = getelementptr inbounds i8, ptr %9, i64 4
  %16 = add i32 %3, -3
  %17 = icmp ult i32 %16, -2
  br i1 %17, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01926 = phi i32 [ %.019., %.lr.ph ], [ 1, %.preheader ]
  %.02025 = phi i32 [ %..020, %.lr.ph ], [ %3, %.preheader ]
  %18 = add i32 %.01926, %.02025
  %19 = lshr i32 %18, 1
  %20 = add nsw i32 %19, -1
  %21 = zext i32 %20 to i64
  %gep = getelementptr inbounds %union.TValue, ptr %invariant.gep, i64 %21
  %22 = load i32, ptr %gep, align 4, !tbaa !14
  %23 = icmp eq i32 %22, -1
  %..020 = select i1 %23, i32 %19, i32 %.02025
  %.019. = select i1 %23, i32 %.01926, i32 %19
  %24 = sub i32 %..020, %.019.
  %25 = icmp ugt i32 %24, 1
  br i1 %25, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %26 = add nsw i32 %.019., -1
  br label %._crit_edge

27:                                               ; preds = %5, %1
  %spec.select = tail call i32 @llvm.usub.sat.i32(i32 %3, i32 1)
  %28 = getelementptr inbounds i8, ptr %0, i64 40
  %29 = load i32, ptr %28, align 8, !tbaa !17
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %._crit_edge, label %31

31:                                               ; preds = %27
  %32 = tail call i32 @unbound_search(ptr noundef nonnull %0, i32 noundef %spec.select)
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader, %._crit_edge.loopexit, %27, %31
  %.0 = phi i32 [ %32, %31 ], [ %spec.select, %27 ], [ 0, %.preheader ], [ %26, %._crit_edge.loopexit ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @unbound_search(ptr noundef, i32 noundef) local_unnamed_addr #0

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

attributes #0 = { 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 #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"}
!17 = !{!6, !8, i64 40}


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